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