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; |
107 | $first = $banana->maxspool ? max($groupinfo[2]-$banana->maxspool, $groupinfo[1]) : $groupinfo[1]; |
01972ace |
108 | $last = $groupinfo[2]; |
78cd27b3 |
109 | if ($this->version == BANANA_SPOOL_VERSION && is_array($this->overview)) { |
110 | if (count($this->overview)) { |
111 | for ($id = min(array_keys($this->overview)); $id<$first; $id++) { |
112 | $this->delid($id, false); |
113 | $do_save = true; |
114 | } |
115 | } |
116 | if (!empty($this->overview)) { |
117 | $first = max(array_keys($this->overview))+1; |
78cd27b3 |
118 | } |
119 | } else { |
120 | unset($this->overview, $this->ids); |
121 | $this->version = BANANA_SPOOL_VERSION; |
122 | } |
123 | |
124 | if ($first<=$last && $groupinfo[0]) { |
125 | $do_save = true; |
126 | $this->_updateSpool("$first-$last"); |
127 | } |
128 | |
129 | if ($do_save) { $this->_saveToFile(); } |
130 | |
131 | $this->_updateUnread($_since, $_display); |
132 | } |
133 | |
134 | function _readFromFile() |
135 | { |
136 | $file = $this->_spoolfile(); |
137 | if (file_exists($file)) { |
50cd076e |
138 | $temp = unserialize(file_get_contents($file)); |
139 | foreach (get_object_vars($temp) as $key=>$val) { |
140 | $this->$key = $val; |
141 | } |
78cd27b3 |
142 | } |
143 | } |
144 | |
145 | function _saveToFile() |
146 | { |
147 | $file = $this->_spoolfile(); |
148 | uasort($this->overview, "spoolcompare"); |
149 | |
150 | $this->roots = Array(); |
151 | foreach($this->overview as $id=>$msg) { |
152 | if (is_null($msg->parent)) { |
153 | $this->roots[] = $id; |
154 | } |
155 | } |
01972ace |
156 | |
78cd27b3 |
157 | file_put_contents($file, serialize($this)); |
158 | } |
159 | |
160 | function _spoolfile() |
161 | { |
162 | global $banana; |
163 | $url = parse_url($banana->host); |
164 | $file = $url['host'].'_'.$url['port'].'_'.$this->group; |
165 | return dirname(dirname(__FILE__)).'/spool/'.$file; |
166 | } |
167 | |
168 | function _updateSpool($arg) |
169 | { |
170 | global $banana; |
171 | $dates = array_map('strtotime', $banana->nntp->xhdr('Date', $arg)); |
172 | $subjects = array_map('headerdecode', $banana->nntp->xhdr('Subject', $arg)); |
173 | $froms = array_map('headerdecode', $banana->nntp->xhdr('From', $arg)); |
174 | $msgids = $banana->nntp->xhdr('Message-ID', $arg); |
175 | $refs = $banana->nntp->xhdr('References', $arg); |
176 | |
177 | if (is_array($this->ids)) { |
178 | $this->ids = array_merge($this->ids, array_flip($msgids)); |
179 | } else { |
180 | $this->ids = array_flip($msgids); |
181 | } |
182 | |
183 | foreach ($msgids as $id=>$msgid) { |
184 | $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]); |
185 | $refs[$id] = str_replace('><', '> <', $refs[$id]); |
186 | $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids)); |
187 | $parents = preg_grep('/^\d+$/', $msgrefs); |
188 | $msg->parent = array_pop($parents); |
189 | $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs)); |
190 | |
191 | if (isset($this->overview[$id])) { |
192 | $msg->desc = $this->overview[$id]->desc; |
193 | $msg->children = $this->overview[$id]->children; |
194 | } |
195 | $this->overview[$id] = $msg; |
196 | |
197 | if ($p = $msg->parent) { |
198 | if (empty($this->overview[$p])) { |
199 | $this->overview[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1); |
200 | } |
201 | $this->overview[$p]->children[] = $id; |
202 | |
203 | while ($p) { |
204 | $this->overview[$p]->desc += $msg->desc; |
205 | $p = $this->overview[$p]->parent; |
206 | } |
207 | } |
208 | } |
209 | } |
210 | |
211 | function _updateUnread($since, $mode) |
212 | { |
213 | global $banana; |
214 | if (empty($since)) { return; } |
215 | |
216 | if (is_array($newpostsids = $banana->nntp->newnews($since, $this->group))) { |
217 | if (!is_array($this->ids)) { $this->ids = array(); } |
218 | $newpostsids = array_intersect($newpostsids, array_keys($this->ids)); |
219 | foreach ($newpostsids as $mid) { |
220 | $this->overview[$this->ids[$mid]]->isread = false; |
221 | $this->overview[$this->ids[$mid]]->descunread = 1; |
222 | $parentmid = $this->ids[$mid]; |
223 | while (isset($parentmid)) { |
224 | $this->overview[$parentmid]->descunread ++; |
225 | $parentmid = $this->overview[$parentmid]->parent; |
226 | } |
227 | } |
228 | |
229 | if (count($newpostsids)) { |
230 | switch ($mode) { |
231 | case 1: |
232 | foreach ($this->roots as $k=>$i) { |
233 | if ($this->overview[$i]->descunread==0) { |
234 | $this->killdesc($i); |
235 | unset($this->roots[$k]); |
236 | } |
237 | } |
238 | break; |
239 | } |
240 | } |
241 | } |
242 | } |
243 | |
244 | /** kill post and childrens |
245 | * @param $_id MSGNUM of post |
246 | */ |
247 | |
248 | function killdesc($_id) |
249 | { |
250 | if (sizeof($this->overview[$_id]->children)) { |
251 | foreach ($this->overview[$_id]->children as $c) { |
252 | $this->killdesc($c); |
253 | } |
254 | } |
255 | unset($this->overview[$_id]); |
256 | if (($msgid = array_search($_id, $this->ids)) !== false) { |
257 | unset($this->ids[$msgid]); |
258 | } |
259 | } |
260 | |
261 | /** delete a post from overview |
262 | * @param $_id MSGNUM of post |
263 | */ |
264 | |
265 | function delid($_id, $write=true) |
266 | { |
267 | if (isset($this->overview[$_id])) { |
268 | if (sizeof($this->overview[$_id]->parent)) { |
269 | $this->overview[$this->overview[$_id]->parent]->children = |
270 | array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id)); |
271 | if (sizeof($this->overview[$_id]->children)) { |
272 | $this->overview[$this->overview[$_id]->parent]->children = |
273 | array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children); |
274 | foreach ($this->overview[$_id]->children as $c) { |
275 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
276 | $this->overview[$c]->parent_direct = false; |
277 | } |
278 | } |
279 | $p = $this->overview[$_id]->parent; |
280 | while ($p) { |
281 | $this->overview[$p]->desc--; |
282 | $p = $this->overview[$p]->parent; |
283 | } |
284 | } elseif (sizeof($this->overview[$_id]->children)) { |
285 | foreach ($this->overview[$_id]->children as $c) { |
286 | $this->overview[$c]->parent = null; |
287 | } |
288 | } |
289 | unset($this->overview[$_id]); |
290 | $msgid = array_search($_id, $this->ids); |
291 | if ($msgid) { |
292 | unset($this->ids[$msgid]); |
293 | } |
294 | |
295 | if ($write) { $this->_saveToFile(); } |
296 | } |
297 | } |
298 | |
299 | /** displays children tree of a post |
300 | * @param $_id INTEGER MSGNUM of post |
301 | * @param $_index INTEGER linear number of post in the tree |
302 | * @param $_first INTEGER linear number of first post displayed |
303 | * @param $_last INTEGER linear number of last post displayed |
304 | * @param $_ref STRING MSGNUM of current post |
305 | * @param $_pfx_node STRING prefix used for current node |
306 | * @param $_pfx_end STRING prefix used for children of current node |
307 | * @param $_head BOOLEAN true if first post in thread |
308 | */ |
309 | |
310 | function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) |
311 | { |
312 | $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />'; |
313 | $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />'; |
314 | $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />'; |
315 | $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />'; |
316 | $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />'; |
317 | $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />'; |
318 | $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />'; |
319 | $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />'; |
320 | $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />'; |
321 | $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />'; |
322 | |
323 | if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) { |
324 | return; |
325 | } |
326 | |
327 | $res = ''; |
328 | |
329 | if ($_index>=$_first) { |
330 | $hc = empty($this->overview[$_id]->children); |
331 | |
332 | $res .= '<tr class="'.($_index%2?'pair':'impair').($this->overview[$_id]->isread?'':' new')."\">\n"; |
333 | $res .= "<td class='date'>".fancyDate($this->overview[$_id]->date)." </td>\n"; |
334 | $res .= "<td class='subj'>" |
335 | ."<div class='tree'>$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n) |
336 | ."</div>"; |
71d6c865 |
337 | $subject = $this->overview[$_id]->subject; |
338 | if (strlen($subject) == 0) { |
339 | $subject = _b_('(pas de sujet)'); |
340 | } |
78cd27b3 |
341 | if ($_index == $_ref) { |
71d6c865 |
342 | $res .= '<span class="cur">'.htmlentities($subject).'</span>'; |
78cd27b3 |
343 | } else { |
8e519bd8 |
344 | $res .= makeHREF(Array('group' => $this->group, |
345 | 'artid' => $_id), |
346 | htmlentities($subject)); |
78cd27b3 |
347 | } |
348 | $res .= "</td>\n<td class='from'>".formatFrom($this->overview[$_id]->from)."</td>\n</tr>"; |
349 | |
350 | if ($hc) { return $res; } |
351 | } |
352 | |
353 | $_index ++; |
354 | |
355 | $children = $this->overview[$_id]->children; |
356 | while ($child = array_shift($children)) { |
357 | if ($_index > $_last) { return $res; } |
358 | if ($_index+$this->overview[$child]->desc >= $_first) { |
359 | if (sizeof($children)) { |
360 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
361 | $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd), |
362 | $_pfx_end.$spfx_I, false); |
363 | } else { |
364 | $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref, |
365 | $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd), |
366 | $_pfx_end.$spfx_e, false); |
367 | } |
368 | } |
369 | $_index += $this->overview[$child]->desc; |
370 | } |
371 | |
372 | return $res; |
373 | } |
374 | |
375 | /** Displays overview |
376 | * @param $_first INTEGER MSGNUM of first post |
377 | * @param $_last INTEGER MSGNUM of last post |
378 | * @param $_ref STRING MSGNUM of current/selectionned post |
379 | */ |
380 | |
381 | function to_html($_first=0, $_last=0, $_ref = null) |
382 | { |
383 | $res = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">'; |
384 | |
385 | if (is_null($_ref)) { |
386 | $res .= '<tr><th>'._b_('Date').'</th>'; |
387 | $res .= '<th>'._b_('Sujet').'</th>'; |
388 | $res .= '<th>'._b_('Auteur').'</th></tr>'; |
389 | } |
390 | |
391 | $index = 1; |
392 | if (sizeof($this->overview)) { |
393 | foreach ($this->roots as $id) { |
394 | $res .= $this->_to_html($id, $index, $_first, $_last, $_ref); |
395 | $index += $this->overview[$id]->desc ; |
396 | if ($index > $_last) { break; } |
397 | } |
398 | } else { |
399 | $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>'; |
400 | } |
401 | |
402 | return $res .= '</table>'; |
403 | } |
404 | |
405 | /** computes linear post index |
406 | * @param $_id INTEGER MSGNUM of post |
407 | * @return INTEGER linear index of post |
408 | */ |
409 | |
410 | function getndx($_id) |
411 | { |
412 | $ndx = 1; |
413 | $id_cur = $_id; |
414 | while (true) { |
415 | $id_parent = $this->overview[$id_cur]->parent; |
416 | if (is_null($id_parent)) break; |
417 | $pos = array_search($id_cur, $this->overview[$id_parent]->children); |
418 | |
419 | for ($i = 0; $i < $pos ; $i++) { |
420 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
421 | } |
422 | $ndx++; //noeud père |
423 | |
424 | $id_cur = $id_parent; |
425 | } |
426 | |
427 | foreach ($this->roots as $i) { |
428 | if ($i==$id_cur) { |
429 | break; |
430 | } |
431 | $ndx += $this->overview[$i]->desc; |
432 | } |
433 | return $ndx; |
434 | } |
435 | } |
436 | |
437 | ?> |