78cd27b3 |
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 | |
50cd076e |
10 | if(!function_exists('file_put_contents')) { |
78cd27b3 |
11 | function file_put_contents($filename, $data) |
12 | { |
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); |
20 | } |
21 | } |
22 | |
23 | function spoolCompare($a,$b) { return ($b->date>=$a->date); } |
24 | |
25 | /** Class spoolhead |
26 | * class used in thread overviews |
27 | */ |
28 | class BananaSpoolHead |
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 */ |
39 | var $parent_direct; |
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; |
48 | |
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 | */ |
57 | |
58 | function BananaSpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true, $_descunread=0) |
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 | } |
67 | } |
68 | |
69 | /** Class spool |
70 | * builds and updates spool |
71 | */ |
72 | |
73 | define("BANANA_SPOOL_VERSION", '0.2'); |
74 | |
75 | class BananaSpool |
76 | { |
77 | var $version; |
78 | /** spool */ |
79 | var $overview; |
80 | /** group name */ |
81 | var $group; |
82 | /** array msgid => msgnum */ |
83 | var $ids; |
84 | /** thread starts */ |
85 | var $roots; |
856dc84a |
86 | /** test validity */ |
87 | var $valid = true; |
78cd27b3 |
88 | |
89 | /** constructor |
90 | * @param $_group STRING group name |
91 | * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts |
92 | * @param $_since INTEGER time stamp (used for read/unread) |
93 | */ |
94 | function BananaSpool($_group, $_display=0, $_since="") |
95 | { |
96 | global $banana; |
97 | $this->group = $_group; |
98 | $groupinfo = $banana->nntp->group($_group); |
856dc84a |
99 | if (!$groupinfo) { |
100 | $this->valid = false; |
101 | return null; |
102 | } |
78cd27b3 |
103 | |
104 | $this->_readFromFile(); |
105 | |
106 | $do_save = false; |
9d18c838 |
107 | $first = $banana->maxspool ? max($groupinfo[2] - $banana->maxspool, $groupinfo[1]) : $groupinfo[1]; |
108 | $last = $groupinfo[2]; |
109 | |
f6df9eb2 |
110 | if ($this->version == BANANA_SPOOL_VERSION && is_array($this->overview)) { |
111 | $mids = array_keys($this->overview); |
112 | foreach ($mids as $id) { |
113 | if (($first <= $last && ($id < $first || $id > $last)) |
114 | || ($first > $last && $id < $first && $id > $last)) |
115 | { |
78cd27b3 |
116 | $this->delid($id, false); |
117 | $do_save = true; |
118 | } |
119 | } |
120 | if (!empty($this->overview)) { |
121 | $first = max(array_keys($this->overview))+1; |
f6df9eb2 |
122 | } |
78cd27b3 |
123 | } else { |
124 | unset($this->overview, $this->ids); |
125 | $this->version = BANANA_SPOOL_VERSION; |
126 | } |
127 | |
128 | if ($first<=$last && $groupinfo[0]) { |
129 | $do_save = true; |
130 | $this->_updateSpool("$first-$last"); |
131 | } |
132 | |
133 | if ($do_save) { $this->_saveToFile(); } |
134 | |
135 | $this->_updateUnread($_since, $_display); |
136 | } |
137 | |
138 | function _readFromFile() |
139 | { |
140 | $file = $this->_spoolfile(); |
141 | if (file_exists($file)) { |
50cd076e |
142 | $temp = unserialize(file_get_contents($file)); |
143 | foreach (get_object_vars($temp) as $key=>$val) { |
144 | $this->$key = $val; |
145 | } |
78cd27b3 |
146 | } |
147 | } |
148 | |
149 | function _saveToFile() |
150 | { |
151 | $file = $this->_spoolfile(); |
152 | uasort($this->overview, "spoolcompare"); |
153 | |
154 | $this->roots = Array(); |
155 | foreach($this->overview as $id=>$msg) { |
156 | if (is_null($msg->parent)) { |
157 | $this->roots[] = $id; |
158 | } |
159 | } |
01972ace |
160 | |
78cd27b3 |
161 | file_put_contents($file, serialize($this)); |
162 | } |
163 | |
164 | function _spoolfile() |
165 | { |
166 | global $banana; |
167 | $url = parse_url($banana->host); |
168 | $file = $url['host'].'_'.$url['port'].'_'.$this->group; |
169 | return dirname(dirname(__FILE__)).'/spool/'.$file; |
170 | } |
171 | |
172 | function _updateSpool($arg) |
173 | { |
174 | global $banana; |
175 | $dates = array_map('strtotime', $banana->nntp->xhdr('Date', $arg)); |
176 | $subjects = array_map('headerdecode', $banana->nntp->xhdr('Subject', $arg)); |
177 | $froms = array_map('headerdecode', $banana->nntp->xhdr('From', $arg)); |
178 | $msgids = $banana->nntp->xhdr('Message-ID', $arg); |
179 | $refs = $banana->nntp->xhdr('References', $arg); |
180 | |
181 | if (is_array($this->ids)) { |
182 | $this->ids = array_merge($this->ids, array_flip($msgids)); |
183 | } else { |
184 | $this->ids = array_flip($msgids); |
185 | } |
186 | |
187 | foreach ($msgids as $id=>$msgid) { |
188 | $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]); |
77eba745 |
189 | if (isset($ref[$id])) { |
190 | $refs[$id] = str_replace('><', '> <', $refs[$id]); |
191 | $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids)); |
192 | $parents = preg_grep('/^\d+$/', $msgrefs); |
193 | $msg->parent = array_pop($parents); |
194 | $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs)); |
195 | } else { |
196 | $msg->parent = null; |
197 | $msg->parent_direct = null; |
198 | } |
78cd27b3 |
199 | |
200 | if (isset($this->overview[$id])) { |
201 | $msg->desc = $this->overview[$id]->desc; |
202 | $msg->children = $this->overview[$id]->children; |
203 | } |
204 | $this->overview[$id] = $msg; |
205 | |
206 | if ($p = $msg->parent) { |
207 | if (empty($this->overview[$p])) { |
208 | $this->overview[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1); |
209 | } |
210 | $this->overview[$p]->children[] = $id; |
211 | |
212 | while ($p) { |
213 | $this->overview[$p]->desc += $msg->desc; |
214 | $p = $this->overview[$p]->parent; |
215 | } |
216 | } |
217 | } |
218 | } |
219 | |
220 | function _updateUnread($since, $mode) |
221 | { |
222 | global $banana; |
223 | if (empty($since)) { return; } |
224 | |
225 | if (is_array($newpostsids = $banana->nntp->newnews($since, $this->group))) { |
226 | if (!is_array($this->ids)) { $this->ids = array(); } |
227 | $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); |
228 | foreach ($newpostsids as $mid) { |
229 | $this->overview[$this->ids[$mid]]->isread = false; |
230 | $this->overview[$this->ids[$mid]]->descunread = 1; |
231 | $parentmid = $this->ids[$mid]; |
232 | while (isset($parentmid)) { |
233 | $this->overview[$parentmid]->descunread ++; |
234 | $parentmid = $this->overview[$parentmid]->parent; |
235 | } |
236 | } |
237 | |
238 | if (count($newpostsids)) { |
239 | switch ($mode) { |
240 | case 1: |
241 | foreach ($this->roots as $k=>$i) { |
242 | if ($this->overview[$i]->descunread==0) { |
243 | $this->killdesc($i); |
244 | unset($this->roots[$k]); |
245 | } |
246 | } |
247 | break; |
248 | } |
249 | } |
250 | } |
251 | } |
252 | |
253 | /** kill post and childrens |
254 | * @param $_id MSGNUM of post |
255 | */ |
256 | |
257 | function killdesc($_id) |
258 | { |
259 | if (sizeof($this->overview[$_id]->children)) { |
260 | foreach ($this->overview[$_id]->children as $c) { |
261 | $this->killdesc($c); |
262 | } |
263 | } |
264 | unset($this->overview[$_id]); |
265 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
266 | unset($this->ids[$msgid]); |
267 | } |
268 | } |
269 | |
270 | /** delete a post from overview |
271 | * @param $_id MSGNUM of post |
272 | */ |
273 | |
274 | function delid($_id, $write=true) |
275 | { |
276 | if (isset($this->overview[$_id])) { |
277 | if (sizeof($this->overview[$_id]->parent)) { |
278 | $this->overview[$this->overview[$_id]->parent]->children = |
279 | array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id)); |
280 | if (sizeof($this->overview[$_id]->children)) { |
281 | $this->overview[$this->overview[$_id]->parent]->children = |
282 | array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children); |
283 | foreach ($this->overview[$_id]->children as $c) { |
284 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
285 | $this->overview[$c]->parent_direct = false; |
286 | } |
287 | } |
288 | $p = $this->overview[$_id]->parent; |
289 | while ($p) { |
290 | $this->overview[$p]->desc--; |
291 | $p = $this->overview[$p]->parent; |
292 | } |
293 | } elseif (sizeof($this->overview[$_id]->children)) { |
294 | foreach ($this->overview[$_id]->children as $c) { |
295 | $this->overview[$c]->parent = null; |
296 | } |
297 | } |
298 | unset($this->overview[$_id]); |
299 | $msgid = array_search($_id, $this->ids); |
300 | if ($msgid) { |
301 | unset($this->ids[$msgid]); |
302 | } |
303 | |
304 | if ($write) { $this->_saveToFile(); } |
305 | } |
306 | } |
307 | |
308 | /** displays children tree of a post |
309 | * @param $_id INTEGER MSGNUM of post |
310 | * @param $_index INTEGER linear number of post in the tree |
311 | * @param $_first INTEGER linear number of first post displayed |
312 | * @param $_last INTEGER linear number of last post displayed |
313 | * @param $_ref STRING MSGNUM of current post |
314 | * @param $_pfx_node STRING prefix used for current node |
315 | * @param $_pfx_end STRING prefix used for children of current node |
316 | * @param $_head BOOLEAN true if first post in thread |
e2347ffc |
317 | * |
318 | * If you want to analyse subject, you can define the function hook_getSubject(&$subject) which |
319 | * take the subject as a reference parameter, transform this subject to be displaid in the spool |
320 | * view and return a string. This string will be put after the subject. |
78cd27b3 |
321 | */ |
322 | |
323 | function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
324 | { |
2ba32e89 |
325 | $spfx_f = makeImg('k1.gif', 'o', 21, 9); |
326 | $spfx_n = makeImg('k2.gif', '*', 21, 9); |
327 | $spfx_Tnd = makeImg('T-direct.gif', '+', 21, 12); |
328 | $spfx_Lnd = makeImg('L-direct.gif', '`', 21, 12); |
329 | $spfx_snd = makeImg('s-direct.gif', '-', 21, 5); |
330 | $spfx_T = makeImg('T.gif', '+', 21, 12); |
331 | $spfx_L = makeImg('L.gif', '`', 21, 12); |
332 | $spfx_s = makeImg('s.gif', '-', 21, 5); |
333 | $spfx_e = makeImg('e.gif', ' ', 21, 12); |
334 | $spfx_I = makeImg('I.gif', '|', 21, 12); |
78cd27b3 |
335 | |
336 | if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) { |
337 | return; |
338 | } |
339 | |
340 | $res = ''; |
341 | |
342 | if ($_index>=$_first) { |
343 | $hc = empty($this->overview[$_id]->children); |
344 | |
345 | $res .= '<tr class="'.($_index%2?'pair':'impair').($this->overview[$_id]->isread?'':' new')."\">\n"; |
346 | $res .= "<td class='date'>".fancyDate($this->overview[$_id]->date)." </td>\n"; |
347 | $res .= "<td class='subj'>" |
348 | ."<div class='tree'>$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n) |
349 | ."</div>"; |
71d6c865 |
350 | $subject = $this->overview[$_id]->subject; |
351 | if (strlen($subject) == 0) { |
352 | $subject = _b_('(pas de sujet)'); |
353 | } |
e2347ffc |
354 | $link = null; |
355 | if (function_exists('hook_getSubject')) { |
356 | $link = hook_getSubject($subject); |
357 | } |
820ce67b |
358 | $subject = formatPlainText(htmlentities($subject)); |
78cd27b3 |
359 | if ($_index == $_ref) { |
e2347ffc |
360 | $res .= '<span class="cur">' . $subject . $link . '</span>'; |
78cd27b3 |
361 | } else { |
8e519bd8 |
362 | $res .= makeHREF(Array('group' => $this->group, |
f6df9eb2 |
363 | 'artid' => $_id), |
e2347ffc |
364 | $subject, |
365 | $subject) |
366 | . $link; |
78cd27b3 |
367 | } |
368 | $res .= "</td>\n<td class='from'>".formatFrom($this->overview[$_id]->from)."</td>\n</tr>"; |
369 | |
370 | if ($hc) { return $res; } |
371 | } |
372 | |
373 | $_index ++; |
374 | |
375 | $children = $this->overview[$_id]->children; |
376 | while ($child = array_shift($children)) { |
377 | if ($_index > $_last) { return $res; } |
378 | if ($_index+$this->overview[$child]->desc >= $_first) { |
379 | if (sizeof($children)) { |
380 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
381 | $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd), |
382 | $_pfx_end.$spfx_I, false); |
383 | } else { |
384 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
385 | $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd), |
386 | $_pfx_end.$spfx_e, false); |
387 | } |
388 | } |
389 | $_index += $this->overview[$child]->desc; |
390 | } |
391 | |
392 | return $res; |
393 | } |
394 | |
395 | /** Displays overview |
396 | * @param $_first INTEGER MSGNUM of first post |
397 | * @param $_last INTEGER MSGNUM of last post |
398 | * @param $_ref STRING MSGNUM of current/selectionned post |
399 | */ |
400 | |
401 | function to_html($_first=0, $_last=0, $_ref = null) |
402 | { |
403 | $res = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">'; |
404 | |
2ba32e89 |
405 | $new = '<div class="banana_action">' |
406 | . makeImgLink(Array('group' => $this->group, |
edfc2d84 |
407 | 'action' => 'new'), |
2ba32e89 |
408 | 'post.gif', |
edfc2d84 |
409 | _b_('Nouveau message')); |
2ba32e89 |
410 | $new .= '</div>'; |
411 | |
78cd27b3 |
412 | if (is_null($_ref)) { |
00bf3555 |
413 | $res .= '<tr><th>' . _b_('Date') . '</th>'; |
414 | $res .= '<th>' . $new . _b_('Sujet') . '</th>'; |
415 | $res .= '<th>' . _b_('Auteur') . '</th></tr>'; |
2ba32e89 |
416 | } else { |
417 | $res .= '<tr><th colspan="3">' . _b_('Aperçu de ') |
418 | . makeHREF(Array('group' => $this->group), |
419 | $this->group) |
420 | . '</th></tr>'; |
78cd27b3 |
421 | } |
422 | |
423 | $index = 1; |
424 | if (sizeof($this->overview)) { |
425 | foreach ($this->roots as $id) { |
426 | $res .= $this->_to_html($id, $index, $_first, $_last, $_ref); |
427 | $index += $this->overview[$id]->desc ; |
428 | if ($index > $_last) { break; } |
429 | } |
430 | } else { |
431 | $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>'; |
432 | } |
433 | |
e230b41a |
434 | global $banana; |
435 | if (is_object($banana->groups)) { |
436 | $res .= '<tr><td colspan="3" class="subs">' |
437 | . $banana->groups->to_html() |
438 | . '</td></tr>'; |
439 | } |
78cd27b3 |
440 | return $res .= '</table>'; |
441 | } |
442 | |
443 | /** computes linear post index |
444 | * @param $_id INTEGER MSGNUM of post |
445 | * @return INTEGER linear index of post |
446 | */ |
447 | |
448 | function getndx($_id) |
449 | { |
450 | $ndx = 1; |
451 | $id_cur = $_id; |
452 | while (true) { |
453 | $id_parent = $this->overview[$id_cur]->parent; |
454 | if (is_null($id_parent)) break; |
455 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
456 | |
457 | for ($i = 0; $i < $pos ; $i++) { |
458 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
459 | } |
460 | $ndx++; //noeud père |
461 | |
462 | $id_cur = $id_parent; |
463 | } |
464 | |
465 | foreach ($this->roots as $i) { |
466 | if ($i==$id_cur) { |
467 | break; |
468 | } |
469 | $ndx += $this->overview[$i]->desc; |
470 | } |
471 | return $ndx; |
472 | } |
2ba32e89 |
473 | |
474 | /** Return root message of the given thread |
475 | * @param id INTEGER id of a message |
476 | */ |
477 | function root($id) |
478 | { |
479 | $id_cur = $id; |
480 | while (true) { |
481 | $id_parent = $this->overview[$id_cur]->parent; |
482 | if (is_null($id_parent)) break; |
483 | $id_cur = $id_parent; |
484 | } |
485 | return $id_cur; |
486 | } |
487 | |
488 | /** Returns previous thread root index |
489 | * @param id INTEGER message number |
490 | */ |
491 | function prevThread($id) |
492 | { |
493 | $root = $this->root($id); |
494 | $last = null; |
495 | foreach ($this->roots as $i) { |
496 | if ($i == $root) { |
497 | return $last; |
498 | } |
499 | $last = $i; |
500 | } |
501 | return $last; |
502 | } |
503 | |
504 | /** Returns next thread root index |
505 | * @param id INTEGER message number |
506 | */ |
507 | function nextThread($id) |
508 | { |
509 | $root = $this->root($id); |
510 | $ok = false; |
511 | foreach ($this->roots as $i) { |
512 | if ($ok) { |
513 | return $i; |
514 | } |
515 | if ($i == $root) { |
516 | $ok = true; |
517 | } |
518 | } |
519 | return null; |
520 | } |
521 | |
522 | /** Return prev post in the thread |
523 | * @param id INTEGER message number |
524 | */ |
525 | function prevPost($id) |
526 | { |
527 | $parent = $this->overview[$id]->parent; |
528 | if (is_null($parent)) { |
529 | return null; |
530 | } |
531 | $last = $parent; |
532 | foreach ($this->overview[$parent]->children as $child) { |
533 | if ($child == $id) { |
534 | return $last; |
535 | } |
536 | $last = $child; |
537 | } |
538 | return null; |
539 | } |
540 | |
541 | /** Return next post in the thread |
542 | * @param id INTEGER message number |
543 | */ |
544 | function nextPost($id) |
545 | { |
546 | if (count($this->overview[$id]->children) != 0) { |
547 | return $this->overview[$id]->children[0]; |
548 | } |
549 | |
550 | $cur = $id; |
551 | while (true) { |
552 | $parent = $this->overview[$cur]->parent; |
553 | if (is_null($parent)) { |
554 | return null; |
555 | } |
556 | $ok = false; |
557 | foreach ($this->overview[$parent]->children as $child) { |
558 | if ($ok) { |
559 | return $child; |
560 | } |
561 | if ($child == $cur) { |
562 | $ok = true; |
563 | } |
564 | } |
565 | $cur = $parent; |
566 | } |
567 | return null; |
568 | } |
78cd27b3 |
569 | } |
570 | |
f6df9eb2 |
571 | // vim:set et sw=4 sts=4 ts=4 |
78cd27b3 |
572 | ?> |