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 | */ |
13 | class 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 | |
55 | class 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; |
110 | if ($this->ids) { |
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) { |
126 | $this->overview[$p]->desc++; |
127 | $p = $this->overview[$p]->parent; |
128 | } |
129 | if ($msg->parent!="") |
130 | $this->overview[$msg->parent]->children[]=$id; |
131 | $this->overview[$id] = $msg; |
132 | } |
133 | uasort($this->overview,"spoolcompare"); |
134 | $f = fopen("spool/spool-$_group.dat","w"); |
135 | fputs($f,serialize($this)); |
136 | fclose($f); |
137 | } |
138 | |
139 | if ($_since != "") { |
140 | $newpostsids = $_nntp->newnews($_since,$_group); |
141 | if (sizeof($newpostsids)) { |
142 | $newpostsids = array_intersect($newpostsids, |
143 | array_keys($this->ids)); |
144 | if ($newpostsids && !is_null($newpostsids)) { |
145 | foreach ($newpostsids as $mid) { |
146 | $this->overview[$this->ids[$mid]]->isread=false; |
147 | $this->overview[$this->ids[$mid]]->descunread=1; |
148 | $parentmid = $this->overview[$this->ids[$mid]]->parent; |
149 | while (!is_null($parentmid)) { |
150 | $this->overview[$parentmid]->descunread++; |
151 | $parentmid = $this->overview[$parentmid]->parent; |
152 | } |
153 | } |
154 | } |
155 | } |
156 | if (sizeof($newpostsids)>0) { |
157 | $flipids = array_flip($this->ids); |
158 | switch ($_display) { |
159 | case 1: |
160 | foreach ($this->overview as $i=>$p) { |
161 | if (!isset($this->overview[$i]->parent) && |
162 | ($this->overview[$i]->descunread==0)) { |
163 | $this->killdesc($i); |
164 | } |
165 | } |
166 | break; |
167 | case 2: |
168 | foreach ($this->overview as $i=>$p) { |
169 | if ($p->isread) { |
170 | unset($this->overview[$i]); |
171 | unset($flipids[$i]); |
172 | } |
173 | } |
174 | $this->ids=array_flip($flipids); |
175 | break; |
176 | } |
177 | } |
178 | } |
179 | return true; |
180 | } |
181 | |
182 | /** kill post and childrens |
183 | * @param $_id MSGNUM of post |
184 | */ |
185 | |
186 | function killdesc($_id) { |
187 | if (sizeof($this->overview[$_id]->children)) { |
188 | foreach ($this->overview[$_id]->children as $c) { |
189 | $this->killdesc($c); |
190 | } |
191 | } |
192 | unset($this->overview[$_id]); |
193 | # $flipid=array_flip($this->ids); |
194 | # unset($flipid[$id]); |
195 | # $this->ids=array_flip($flipid); |
196 | } |
197 | |
198 | /** delete a post from overview |
199 | * @param $_id MSGNUM of post |
200 | */ |
201 | |
202 | function delid($_id) { |
203 | if (isset($this->overview[$_id])) { |
204 | if (sizeof($this->overview[$_id]->parent)) { |
205 | $this->overview[$this->overview[$_id]->parent]->children = |
206 | array_diff($this->overview[$this->overview[$_id]->parent]->children, |
207 | array($_id)); |
208 | if (sizeof($this->overview[$_id]->children)) { |
209 | $this->overview[$this->overview[$_id]->parent]->children = |
210 | array_merge($this->overview[$this->overview[$_id]->parent]->children, |
211 | $this->overview[$_id]->children); |
212 | foreach ($this->overview[$_id]->children as $c) { |
213 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
214 | } |
215 | } |
216 | $p = $this->overview[$_id]->parent; |
217 | while ($p) { |
218 | $this->overview[$p]->desc--; |
219 | $p = $this->overview[$p]->parent; |
220 | } |
221 | } elseif (sizeof($this->overview[$_id]->children)) { |
222 | foreach ($this->overview[$_id]->children as $c) { |
223 | $this->overview[$c]->parent = null; |
224 | } |
225 | } |
226 | unset($this->overview[$_id]); |
227 | $ids = array_flip($this->ids); |
228 | unset($ids[$_id]); |
229 | $this->ids = array_flip($ids); |
230 | $f = fopen("spool/spool-{$this->group}.dat","w"); |
231 | fputs($f,serialize($this)); |
232 | fclose($f); |
233 | } |
234 | } |
235 | |
236 | /** displays children tree of a post |
237 | * @param $_id INTEGER MSGNUM of post |
238 | * @param $_index INTEGER linear number of post in the tree |
239 | * @param $_first INTEGER linear number of first post displayed |
240 | * @param $_last INTEGER linear number of last post displayed |
241 | * @param $_ref STRING MSGNUM of current post |
242 | * @param $_pfx_node STRING prefix used for current node |
243 | * @param $_pfx_end STRING prefix used for children of current node |
244 | * @param $_head BOOLEAN true if first post in thread |
245 | */ |
246 | |
247 | function disp_desc($_id,$_index="",$_first=0,$_last=0,$_ref="", |
248 | $_pfx_node="", $_pfx_end="",$_head=true) { |
249 | $debug = false; |
250 | $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />'; |
251 | $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />'; |
252 | $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />'; |
253 | $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />'; |
254 | $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />'; |
255 | $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />'; |
256 | $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />'; |
257 | |
258 | if ($_index == "") $_index = $this->getndx($_id); |
259 | |
260 | if (!sizeof($this->overview[$_id]->children) && ($_index<=$_last) |
261 | && ($_index>=$_first)) { |
262 | echo '<tr class="'.($_index%2?"pair":"impair")."\">\n"; |
263 | echo "<td class=\"date\">" |
264 | .formatSpoolHeader("date",$this->overview[$_id]->date,$_id, |
265 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
266 | ." </td>\n"; |
267 | echo "<td class=\"subject\"><div class=\"tree\">" |
268 | .$_pfx_node.($_head?$spfx_f:$spfx_s)."</div>" |
269 | .formatSpoolHeader("subject",$this->overview[$_id]->subject,$_id, |
270 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
271 | .($debug?" $_id $_index ". |
272 | $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n"; |
273 | echo "<td class=\"author\">" |
274 | .formatSpoolHeader("from",$this->overview[$_id]->from,$_id, |
275 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
276 | ." </td>\n</tr>"; |
277 | return true; |
278 | } |
279 | $children = $this->overview[$_id]->children; |
280 | if (($_index<=$_last) && ($_index>=$_first)) { |
281 | echo '<tr class="'.($_index%2?"pair":"impair")."\">\n"; |
282 | echo "<td class=\"date\">" |
283 | .formatSpoolHeader("date",$this->overview[$_id]->date,$_id, |
284 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
285 | ." </td>\n"; |
286 | echo "<td class=\"subject\"><div class=\"tree\">" |
287 | .$_pfx_node.$spfx_n."</div>" |
288 | .formatSpoolHeader("subject",$this->overview[$_id]->subject,$_id, |
289 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
290 | .($debug?" $_id $_index ". |
291 | $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n"; |
292 | echo "<td class=\"author\">" |
293 | .formatSpoolHeader("from",$this->overview[$_id]->from,$_id, |
294 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
295 | ." </td>\n</tr>"; |
296 | } |
297 | $index=$_index+1; |
298 | while ($child = array_shift($children)) { |
299 | if (($index+$this->overview[$child]->desc-1>=$_first) |
300 | ||($index<$_last)){ |
301 | if (sizeof($children)) { |
302 | $this->disp_desc($child,$index,$_first,$_last,$_ref, |
303 | $_pfx_end.$spfx_T,$_pfx_end.$spfx_I,false); |
304 | } else { |
305 | $this->disp_desc($child,$index,$_first,$_last,$_ref, |
306 | $_pfx_end.$spfx_L,$_pfx_end.$spfx_e,false); |
307 | } |
308 | } |
309 | $index += $this->overview[$child]->desc; |
310 | } |
311 | } |
312 | |
313 | /** Displays overview |
314 | * @param $_first INTEGER MSGNUM of first post |
315 | * @param $_last INTEGER MSGNUM of last post |
316 | * @param $_ref STRING MSGNUM of current/selectionned post |
317 | */ |
318 | |
319 | function disp($_first=0,$_last=0,$_ref="") { |
320 | $index=1; |
321 | if (sizeof($this->overview)) { |
322 | foreach ($this->overview as $id=>$msg) { |
323 | if (!sizeof($msg->parent)) { |
324 | $this->disp_desc($id,$index,$_first,$_last,$_ref); |
325 | $index += $msg->desc; |
326 | } |
327 | } |
328 | } else { |
329 | echo "<tr class=\"pair\">\n"; |
330 | echo "\t<td colspan=\"3\">\n"; |
331 | echo "\t\tNo post in this newsgroup\n"; |
332 | echo "\t</td>\n"; |
333 | echo "</tr>\n"; |
334 | } |
335 | } |
336 | |
337 | /** computes linear post index |
338 | * @param $_id INTEGER MSGNUM of post |
339 | * @return INTEGER linear index of post |
340 | */ |
341 | |
342 | function getndx($_id) { |
343 | $ndx = 1; |
344 | // on remonte l'arbre |
345 | $id_parent = $this->overview[$_id]->parent; |
346 | $id_curr = $_id; |
347 | while (!is_null($id_parent)) { |
348 | for ($i=0; $i<array_search($id_curr, |
349 | $this->overview[$id_parent]->children) ; $i++) { |
350 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
351 | } |
352 | $ndx++; //noeud père |
353 | $id_curr = $id_parent; |
354 | $id_parent = $this->overview[$id_curr]->parent; |
355 | } |
356 | // on compte les threads précédents |
357 | foreach ($this->overview as $i=>$p) { |
358 | if ($i==$id_curr) { |
359 | break; |
360 | } |
361 | if (is_null($p->parent)) { |
362 | $ndx += $this->overview[$i]->desc; |
363 | } |
364 | } |
365 | return $ndx; |
366 | } |
367 | } |
368 | |
369 | ?> |