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