810ac1df |
1 | <?php |
2 | /******************************************************************************** |
3 | * include/spool.inc.php : spool subroutines |
4 | * ----------------------- |
5 | * |
6 | * This file is part of the banana distribution |
7 | * Copyright: See COPYING files that comes with this distribution |
8 | ********************************************************************************/ |
9 | |
3ca86dfe |
10 | if(!function_exists('_file_put_contents')) { |
75ff2f64 |
11 | function file_put_contents($filename, $data) |
12 | { |
3ca86dfe |
13 | $fp = fopen($filename, 'w'); |
14 | if(!$fp) { |
15 | trigger_error('file_put_contents cannot write in file '.$filename, E_USER_ERROR); |
16 | return; |
17 | } |
18 | fputs($fp, $data); |
19 | fclose($fp); |
3ca86dfe |
20 | } |
21 | } |
22 | |
d4c19591 |
23 | function spoolCompare($a,$b) { return ($b->date>=$a->date); } |
01681efd |
24 | |
810ac1df |
25 | /** Class spoolhead |
26 | * class used in thread overviews |
27 | */ |
d4c19591 |
28 | class BananaSpoolHead |
e785d91c |
29 | { |
30 | /** date (timestamp) */ |
31 | var $date; |
32 | /** subject */ |
33 | var $subject; |
34 | /** author */ |
35 | var $from; |
36 | /** reference of parent */ |
37 | var $parent; |
38 | /** paren is direct */ |
3ca86dfe |
39 | var $parent_direct; |
e785d91c |
40 | /** array of children */ |
41 | var $children = Array(); |
42 | /** true if post is read */ |
43 | var $isread; |
44 | /** number of posts deeper in this branch of tree */ |
45 | var $desc; |
46 | /** same as desc, but counts only unread posts */ |
47 | var $descunread; |
810ac1df |
48 | |
e785d91c |
49 | /** constructor |
50 | * @param $_date INTEGER timestamp of post |
51 | * @param $_subject STRING subject of post |
52 | * @param $_from STRING author of post |
53 | * @param $_desc INTEGER desc value (1 for a new post) |
54 | * @param $_read BOOLEAN true if read |
55 | * @param $_descunread INTEGER descunread value (0 for a new post) |
56 | */ |
810ac1df |
57 | |
d4c19591 |
58 | function BananaSpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true, $_descunread=0) |
e785d91c |
59 | { |
60 | $this->date = $_date; |
61 | $this->subject = $_subject; |
62 | $this->from = $_from; |
63 | $this->desc = $_desc; |
64 | $this->isread = $_read; |
65 | $this->descunread = $_descunread; |
66 | } |
810ac1df |
67 | } |
68 | |
69 | /** Class spool |
70 | * builds and updates spool |
71 | */ |
72 | |
3ca86dfe |
73 | define("BANANA_SPOOL_VERSION", '0.2'); |
74 | |
d4c19591 |
75 | class BananaSpool |
e785d91c |
76 | { |
3ca86dfe |
77 | var $version; |
e785d91c |
78 | /** spool */ |
79 | var $overview; |
80 | /** group name */ |
81 | var $group; |
82 | /** array msgid => msgnum */ |
83 | var $ids; |
cced14b6 |
84 | /** thread starts */ |
85 | var $roots; |
810ac1df |
86 | |
e785d91c |
87 | /** constructor |
88 | * @param $_nntp RESOURCE NNTP socket filehandle |
89 | * @param $_group STRING group name |
90 | * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts |
91 | * @param $_since INTEGER time stamp (used for read/unread) |
92 | */ |
d4c19591 |
93 | function BananaSpool(&$_nntp, $_group, $_display=0, $_since="") |
e785d91c |
94 | { |
95 | global $news; |
dd7d1c59 |
96 | $this->group = $_group; |
97 | $groupinfo = $_nntp->group($_group); |
98 | if (!$groupinfo) { return ($this = null); } |
3ca86dfe |
99 | |
dd7d1c59 |
100 | $this->_readFromFile(); |
3ca86dfe |
101 | |
dd7d1c59 |
102 | $do_save = false; |
103 | $first = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]); |
104 | $last = $groupinfo[2]; |
3ca86dfe |
105 | if ($this->version == BANANA_SPOOL_VERSION) { |
cced14b6 |
106 | for ($id = min(array_keys($this->overview)); $id<$first; $id++) { |
3ca86dfe |
107 | $this->delid($id, false); |
cced14b6 |
108 | $do_save = true; |
e785d91c |
109 | } |
dd7d1c59 |
110 | $first = max(array_keys($this->overview))+1; |
82c17a91 |
111 | } else { |
3ca86dfe |
112 | unset($this->overview, $this->ids); |
3ca86dfe |
113 | $this->version = BANANA_SPOOL_VERSION; |
82c17a91 |
114 | } |
e785d91c |
115 | |
526f9bd0 |
116 | if ($first<=$last && $groupinfo[0]) { |
dd7d1c59 |
117 | $do_save = true; |
118 | $this->_updateSpool($_nntp, "$first-$last"); |
119 | } |
45651257 |
120 | |
dd7d1c59 |
121 | if ($do_save) { $this->_saveToFile(); } |
3ca86dfe |
122 | |
dd7d1c59 |
123 | $this->_updateUnread($_nntp, $_since, $_display); |
124 | } |
125 | |
126 | function _readFromFile() |
127 | { |
128 | $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat"; |
129 | if (file_exists($file)) { |
130 | $this = unserialize(file_get_contents($file)); |
131 | } |
132 | } |
133 | |
134 | function _saveToFile() |
135 | { |
136 | $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat"; |
137 | uasort($this->overview, "spoolcompare"); |
138 | |
139 | $this->roots = Array(); |
140 | foreach($this->overview as $id=>$msg) { |
141 | if (is_null($msg->parent)) { |
142 | $this->roots[] = $id; |
143 | } |
144 | } |
145 | |
146 | file_put_contents($file, serialize($this)); |
147 | } |
148 | |
149 | function _updateSpool(&$_nntp, $arg) |
150 | { |
151 | $dates = array_map(strtotime, $_nntp->xhdr("Date", $arg)); |
152 | $subjects = array_map(headerdecode, $_nntp->xhdr("Subject", $arg)); |
153 | $froms = array_map(headerdecode, $_nntp->xhdr("From", $arg)); |
154 | $msgids = $_nntp->xhdr("Message-ID", $arg); |
155 | $refs = $_nntp->xhdr("References", $arg); |
156 | |
157 | if (is_array($this->ids)) { |
158 | $this->ids = array_merge($this->ids, array_flip($msgids)); |
159 | } else { |
160 | $this->ids = array_flip($msgids); |
161 | } |
162 | |
163 | foreach ($msgids as $id=>$msgid) { |
d4c19591 |
164 | $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]); |
dd7d1c59 |
165 | $refs[$id] = str_replace('><', '> <', $refs[$id]); |
166 | $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids)); |
167 | $parents = preg_grep('/^\d+$/', $msgrefs); |
168 | $msg->parent = array_pop($parents); |
169 | $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs)); |
170 | |
171 | if (isset($this->overview[$id])) { |
172 | $msg->desc = $this->overview[$id]->desc; |
173 | $msg->children = $this->overview[$id]->children; |
3316e34e |
174 | } |
dd7d1c59 |
175 | $this->overview[$id] = $msg; |
e785d91c |
176 | |
dd7d1c59 |
177 | if ($p = $msg->parent) { |
178 | if (empty($this->overview[$p])) { |
d4c19591 |
179 | $this->overview[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1); |
4ced5065 |
180 | } |
dd7d1c59 |
181 | $this->overview[$p]->children[] = $id; |
4ced5065 |
182 | |
dd7d1c59 |
183 | while ($p) { |
184 | $this->overview[$p]->desc += $msg->desc; |
185 | $p = $this->overview[$p]->parent; |
e785d91c |
186 | } |
810ac1df |
187 | } |
810ac1df |
188 | } |
dd7d1c59 |
189 | } |
e785d91c |
190 | |
75ff2f64 |
191 | function _updateUnread(&$nntp, $since, $mode) |
192 | { |
dd7d1c59 |
193 | if (empty($since)) { return; } |
194 | |
195 | if (is_array($newpostsids = $nntp->newnews($since, $this->group))) { |
196 | $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); |
197 | foreach ($newpostsids as $mid) { |
198 | $this->overview[$this->ids[$mid]]->isread = false; |
199 | $this->overview[$this->ids[$mid]]->descunread = 1; |
200 | $parentmid = $this->ids[$mid]; |
201 | while (isset($parentmid)) { |
202 | $this->overview[$parentmid]->descunread ++; |
203 | $parentmid = $this->overview[$parentmid]->parent; |
e785d91c |
204 | } |
810ac1df |
205 | } |
dd7d1c59 |
206 | |
207 | if (count($newpostsids)) { |
208 | switch ($mode) { |
e785d91c |
209 | case 1: |
45f3ac9b |
210 | foreach ($this->roots as $k=>$i) { |
cced14b6 |
211 | if ($this->overview[$i]->descunread==0) { |
e785d91c |
212 | $this->killdesc($i); |
45f3ac9b |
213 | unset($this->roots[$k]); |
e785d91c |
214 | } |
215 | } |
216 | break; |
e785d91c |
217 | } |
810ac1df |
218 | } |
810ac1df |
219 | } |
cced14b6 |
220 | } |
221 | |
e785d91c |
222 | /** kill post and childrens |
223 | * @param $_id MSGNUM of post |
224 | */ |
810ac1df |
225 | |
e785d91c |
226 | function killdesc($_id) |
227 | { |
228 | if (sizeof($this->overview[$_id]->children)) { |
229 | foreach ($this->overview[$_id]->children as $c) { |
230 | $this->killdesc($c); |
231 | } |
232 | } |
233 | unset($this->overview[$_id]); |
dd7d1c59 |
234 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
e785d91c |
235 | unset($this->ids[$msgid]); |
236 | } |
810ac1df |
237 | } |
e785d91c |
238 | |
239 | /** delete a post from overview |
240 | * @param $_id MSGNUM of post |
241 | */ |
242 | |
3ca86dfe |
243 | function delid($_id, $write=true) |
e785d91c |
244 | { |
245 | if (isset($this->overview[$_id])) { |
246 | if (sizeof($this->overview[$_id]->parent)) { |
247 | $this->overview[$this->overview[$_id]->parent]->children = |
248 | array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id)); |
249 | if (sizeof($this->overview[$_id]->children)) { |
250 | $this->overview[$this->overview[$_id]->parent]->children = |
251 | array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children); |
252 | foreach ($this->overview[$_id]->children as $c) { |
253 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
254 | $this->overview[$c]->parent_direct = false; |
255 | } |
256 | } |
257 | $p = $this->overview[$_id]->parent; |
258 | while ($p) { |
259 | $this->overview[$p]->desc--; |
260 | $p = $this->overview[$p]->parent; |
261 | } |
262 | } elseif (sizeof($this->overview[$_id]->children)) { |
263 | foreach ($this->overview[$_id]->children as $c) { |
264 | $this->overview[$c]->parent = null; |
265 | } |
266 | } |
267 | unset($this->overview[$_id]); |
3ca86dfe |
268 | $msgid = array_search($_id, $this->ids); |
e785d91c |
269 | if ($msgid) { |
270 | unset($this->ids[$msgid]); |
271 | } |
272 | |
dd7d1c59 |
273 | if ($write) { $this->_saveToFile(); } |
e785d91c |
274 | } |
35ca8036 |
275 | } |
810ac1df |
276 | |
e785d91c |
277 | /** displays children tree of a post |
278 | * @param $_id INTEGER MSGNUM of post |
279 | * @param $_index INTEGER linear number of post in the tree |
280 | * @param $_first INTEGER linear number of first post displayed |
281 | * @param $_last INTEGER linear number of last post displayed |
282 | * @param $_ref STRING MSGNUM of current post |
283 | * @param $_pfx_node STRING prefix used for current node |
284 | * @param $_pfx_end STRING prefix used for children of current node |
285 | * @param $_head BOOLEAN true if first post in thread |
286 | */ |
810ac1df |
287 | |
75ff2f64 |
288 | function _disp_desc($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
289 | { |
e785d91c |
290 | global $css; |
e785d91c |
291 | $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />'; |
292 | $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />'; |
293 | $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />'; |
294 | $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />'; |
295 | $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />'; |
296 | $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />'; |
297 | $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />'; |
298 | $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />'; |
299 | $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />'; |
300 | $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />'; |
301 | |
cced14b6 |
302 | if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) { |
303 | return; |
810ac1df |
304 | } |
e785d91c |
305 | |
cced14b6 |
306 | if ($_index>=$_first) { |
307 | $us = ($_index == $_ref); |
308 | $hc = empty($this->overview[$_id]->children); |
309 | |
dd7d1c59 |
310 | echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"]).($this->overview[$_id]->isread?'':' new')."\">\n"; |
e2cae7e3 |
311 | echo "<td class=\"{$css['date']}\">".fancyDate($this->overview[$_id]->date)." </td>\n"; |
dd7d1c59 |
312 | echo "<td class=\"{$css['subject']}\">" |
313 | ."<div class=\"{$css['tree']}\">$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n) |
314 | ."</div>"; |
315 | if ($_index == $_ref) { |
316 | echo '<span class="isref">'.htmlentities($this->overview[$_id]->subject).'</span>'; |
317 | } else { |
318 | echo "<a href='article.php?group={$this->group}&id=$_id'>".htmlentities($this->overview[$_id]->subject).'</a>'; |
cced14b6 |
319 | } |
dd7d1c59 |
320 | echo "</td>\n<td class=\"{$css['author']}\">".formatFrom($this->overview[$_id]->from)."</td>\n</tr>"; |
321 | |
322 | if ($hc) { return; } |
cced14b6 |
323 | } |
324 | |
dd7d1c59 |
325 | $_index ++; |
cced14b6 |
326 | |
327 | $children = $this->overview[$_id]->children; |
e785d91c |
328 | while ($child = array_shift($children)) { |
dd7d1c59 |
329 | if ($_index > $_last) { return; } |
330 | if ($_index+$this->overview[$child]->desc >= $_first) { |
e785d91c |
331 | if (sizeof($children)) { |
3f555388 |
332 | $this->_disp_desc($child, $_index, $_first, $_last, $_ref, |
dd7d1c59 |
333 | $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd), |
3ca86dfe |
334 | $_pfx_end.$spfx_I, false); |
e785d91c |
335 | } else { |
3f555388 |
336 | $this->_disp_desc($child, $_index, $_first, $_last, $_ref, |
dd7d1c59 |
337 | $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd), |
3ca86dfe |
338 | $_pfx_end.$spfx_e, false); |
e785d91c |
339 | } |
340 | } |
dd7d1c59 |
341 | $_index += $this->overview[$child]->desc; |
810ac1df |
342 | } |
810ac1df |
343 | } |
810ac1df |
344 | |
e785d91c |
345 | /** Displays overview |
346 | * @param $_first INTEGER MSGNUM of first post |
347 | * @param $_last INTEGER MSGNUM of last post |
348 | * @param $_ref STRING MSGNUM of current/selectionned post |
349 | */ |
810ac1df |
350 | |
75ff2f64 |
351 | function disp($_first=0, $_last=0, $_ref="") |
352 | { |
e785d91c |
353 | global $css; |
354 | $index = 1; |
355 | if (sizeof($this->overview)) { |
cced14b6 |
356 | foreach ($this->roots as $id) { |
3f555388 |
357 | $this->_disp_desc($id, $index, $_first, $_last, $_ref); |
cced14b6 |
358 | $index += $this->overview[$id]->desc ; |
dd7d1c59 |
359 | if ($index > $_last) { break; } |
e785d91c |
360 | } |
810ac1df |
361 | } else { |
e785d91c |
362 | echo "<tr class=\"{$css['pair']}\">\n"; |
363 | echo "\t<td colspan=\"3\">\n"; |
364 | echo "\t\tNo post in this newsgroup\n"; |
365 | echo "\t</td>\n"; |
366 | echo "</tr>\n"; |
810ac1df |
367 | } |
810ac1df |
368 | } |
810ac1df |
369 | |
e785d91c |
370 | /** computes linear post index |
371 | * @param $_id INTEGER MSGNUM of post |
372 | * @return INTEGER linear index of post |
373 | */ |
810ac1df |
374 | |
75ff2f64 |
375 | function getndx($_id) |
376 | { |
cced14b6 |
377 | $ndx = 1; |
378 | $id_cur = $_id; |
4ced5065 |
379 | while (true) { |
cced14b6 |
380 | $id_parent = $this->overview[$id_cur]->parent; |
4ced5065 |
381 | if (is_null($id_parent)) break; |
cced14b6 |
382 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
4ced5065 |
383 | |
cced14b6 |
384 | for ($i = 0; $i < $pos ; $i++) { |
e785d91c |
385 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
386 | } |
387 | $ndx++; //noeud père |
cced14b6 |
388 | |
389 | $id_cur = $id_parent; |
810ac1df |
390 | } |
cced14b6 |
391 | |
392 | foreach ($this->roots as $i) { |
393 | if ($i==$id_cur) { |
e785d91c |
394 | break; |
395 | } |
cced14b6 |
396 | $ndx += $this->overview[$i]->desc; |
e785d91c |
397 | } |
398 | return $ndx; |
810ac1df |
399 | } |
810ac1df |
400 | } |
401 | |
402 | ?> |