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