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