d7579ce119dafb64daaf4c5de23ea5cdd0253863
[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 function spoolcompare($a,$b) { return ($b->date>=$a->date); }
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 /** thread starts */
84 var $roots;
85
86 /** constructor
87 * @param $_nntp RESOURCE NNTP socket filehandle
88 * @param $_group STRING group name
89 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
90 * @param $_since INTEGER time stamp (used for read/unread)
91 */
92 function spool(&$_nntp, $_group, $_display=0, $_since="")
93 {
94 global $news;
95 $this->group = $_group;
96 $groupinfo = $_nntp->group($_group);
97 if (!$groupinfo) { return ($this = null); }
98
99 $this->_readFromFile();
100
101 $do_save = false;
102 $first = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
103 $last = $groupinfo[2];
104 if ($this->version == BANANA_SPOOL_VERSION) {
105 for ($id = min(array_keys($this->overview)); $id<$first; $id++) {
106 $this->delid($id, false);
107 $do_save = true;
108 }
109 $first = max(array_keys($this->overview))+1;
110 } else {
111 unset($this->overview, $this->ids);
112 $this->version = BANANA_SPOOL_VERSION;
113 }
114
115 if ($first<$last && $groupinfo[0]) {
116 $do_save = true;
117 $this->_updateSpool($_nntp, "$first-$last");
118 }
119
120 if ($do_save) { $this->_saveToFile(); }
121
122 $this->_updateUnread($_nntp, $_since, $_display);
123 }
124
125 function _readFromFile()
126 {
127 $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat";
128 if (file_exists($file)) {
129 $this = unserialize(file_get_contents($file));
130 }
131 }
132
133 function _saveToFile()
134 {
135 $file = dirname(dirname(__FILE__))."/spool/spool-{$this->group}.dat";
136 uasort($this->overview, "spoolcompare");
137
138 $this->roots = Array();
139 foreach($this->overview as $id=>$msg) {
140 if (is_null($msg->parent)) {
141 $this->roots[] = $id;
142 }
143 }
144
145 file_put_contents($file, serialize($this));
146 }
147
148 function _updateSpool(&$_nntp, $arg)
149 {
150 $dates = array_map(strtotime, $_nntp->xhdr("Date", $arg));
151 $subjects = array_map(headerdecode, $_nntp->xhdr("Subject", $arg));
152 $froms = array_map(headerdecode, $_nntp->xhdr("From", $arg));
153 $msgids = $_nntp->xhdr("Message-ID", $arg);
154 $refs = $_nntp->xhdr("References", $arg);
155
156 if (is_array($this->ids)) {
157 $this->ids = array_merge($this->ids, array_flip($msgids));
158 } else {
159 $this->ids = array_flip($msgids);
160 }
161
162 foreach ($msgids as $id=>$msgid) {
163 $msg = new spoolhead($dates[$id], $subjects[$id], $froms[$id]);
164 $refs[$id] = str_replace('><', '> <', $refs[$id]);
165 $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids));
166 $parents = preg_grep('/^\d+$/', $msgrefs);
167 $msg->parent = array_pop($parents);
168 $msg->parent_direct = preg_match('/^\d+$/', array_pop($msgrefs));
169
170 if (isset($this->overview[$id])) {
171 $msg->desc = $this->overview[$id]->desc;
172 $msg->children = $this->overview[$id]->children;
173 }
174 $this->overview[$id] = $msg;
175
176 if ($p = $msg->parent) {
177 if (empty($this->overview[$p])) {
178 $this->overview[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
179 }
180 $this->overview[$p]->children[] = $id;
181
182 while ($p) {
183 $this->overview[$p]->desc += $msg->desc;
184 $p = $this->overview[$p]->parent;
185 }
186 }
187 }
188 }
189
190 function _updateUnread(&$nntp, $since, $mode) {
191 if (empty($since)) { return; }
192
193 if (is_array($newpostsids = $nntp->newnews($since, $this->group))) {
194 $newpostsids = array_intersect($newpostsids, array_keys($this->ids));
195 foreach ($newpostsids as $mid) {
196 $this->overview[$this->ids[$mid]]->isread = false;
197 $this->overview[$this->ids[$mid]]->descunread = 1;
198 $parentmid = $this->ids[$mid];
199 while (isset($parentmid)) {
200 $this->overview[$parentmid]->descunread ++;
201 $parentmid = $this->overview[$parentmid]->parent;
202 }
203 }
204
205 if (count($newpostsids)) {
206 switch ($mode) {
207 case 1:
208 foreach ($this->roots as $k=>$i) {
209 if ($this->overview[$i]->descunread==0) {
210 $this->killdesc($i);
211 unset($this->roots[$k]);
212 }
213 }
214 break;
215 }
216 }
217 }
218 }
219
220 /** kill post and childrens
221 * @param $_id MSGNUM of post
222 */
223
224 function killdesc($_id)
225 {
226 if (sizeof($this->overview[$_id]->children)) {
227 foreach ($this->overview[$_id]->children as $c) {
228 $this->killdesc($c);
229 }
230 }
231 unset($this->overview[$_id]);
232 if (($msgid = array_search($_id, $this->ids)) !== false) {
233 unset($this->ids[$msgid]);
234 }
235 }
236
237 /** delete a post from overview
238 * @param $_id MSGNUM of post
239 */
240
241 function delid($_id, $write=true)
242 {
243 if (isset($this->overview[$_id])) {
244 if (sizeof($this->overview[$_id]->parent)) {
245 $this->overview[$this->overview[$_id]->parent]->children =
246 array_diff($this->overview[$this->overview[$_id]->parent]->children, array($_id));
247 if (sizeof($this->overview[$_id]->children)) {
248 $this->overview[$this->overview[$_id]->parent]->children =
249 array_merge($this->overview[$this->overview[$_id]->parent]->children, $this->overview[$_id]->children);
250 foreach ($this->overview[$_id]->children as $c) {
251 $this->overview[$c]->parent = $this->overview[$_id]->parent;
252 $this->overview[$c]->parent_direct = false;
253 }
254 }
255 $p = $this->overview[$_id]->parent;
256 while ($p) {
257 $this->overview[$p]->desc--;
258 $p = $this->overview[$p]->parent;
259 }
260 } elseif (sizeof($this->overview[$_id]->children)) {
261 foreach ($this->overview[$_id]->children as $c) {
262 $this->overview[$c]->parent = null;
263 }
264 }
265 unset($this->overview[$_id]);
266 $msgid = array_search($_id, $this->ids);
267 if ($msgid) {
268 unset($this->ids[$msgid]);
269 }
270
271 if ($write) { $this->_saveToFile(); }
272 }
273 }
274
275 /** displays children tree of a post
276 * @param $_id INTEGER MSGNUM of post
277 * @param $_index INTEGER linear number of post in the tree
278 * @param $_first INTEGER linear number of first post displayed
279 * @param $_last INTEGER linear number of last post displayed
280 * @param $_ref STRING MSGNUM of current post
281 * @param $_pfx_node STRING prefix used for current node
282 * @param $_pfx_end STRING prefix used for children of current node
283 * @param $_head BOOLEAN true if first post in thread
284 */
285
286 function _disp_desc($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true) {
287 global $css;
288 $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />';
289 $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />';
290 $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
291 $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />';
292 $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />';
293 $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
294 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
295 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
296 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt="&nbsp;" />';
297 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
298
299 if ($_index + $this->overview[$_id]->desc < $_first || $_index > $_last) {
300 return;
301 }
302
303 if ($_index>=$_first) {
304 $us = ($_index == $_ref);
305 $hc = empty($this->overview[$_id]->children);
306
307 echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"]).($this->overview[$_id]->isread?'':' new')."\">\n";
308 echo "<td class=\"{$css['date']}\">".fancyDate($this->overview[$_id]->date)." </td>\n";
309 echo "<td class=\"{$css['subject']}\">"
310 ."<div class=\"{$css['tree']}\">$_pfx_node".($hc?($_head?$spfx_f:($this->overview[$_id]->parent_direct?$spfx_s:$spfx_snd)):$spfx_n)
311 ."</div>";
312 if ($_index == $_ref) {
313 echo '<span class="isref">'.htmlentities($this->overview[$_id]->subject).'</span>';
314 } else {
315 echo "<a href='article.php?group={$this->group}&amp;id=$_id'>".htmlentities($this->overview[$_id]->subject).'</a>';
316 }
317 echo "</td>\n<td class=\"{$css['author']}\">".formatFrom($this->overview[$_id]->from)."</td>\n</tr>";
318
319 if ($hc) { return; }
320 }
321
322 $_index ++;
323
324 $children = $this->overview[$_id]->children;
325 while ($child = array_shift($children)) {
326 if ($_index > $_last) { return; }
327 if ($_index+$this->overview[$child]->desc >= $_first) {
328 if (sizeof($children)) {
329 $this->_disp_desc($child, $_index, $_first, $_last, $_ref,
330 $_pfx_end.($this->overview[$child]->parent_direct?$spfx_T:$spfx_Tnd),
331 $_pfx_end.$spfx_I, false);
332 } else {
333 $this->_disp_desc($child, $_index, $_first, $_last, $_ref,
334 $_pfx_end.($this->overview[$child]->parent_direct?$spfx_L:$spfx_Lnd),
335 $_pfx_end.$spfx_e, false);
336 }
337 }
338 $_index += $this->overview[$child]->desc;
339 }
340 }
341
342 /** Displays overview
343 * @param $_first INTEGER MSGNUM of first post
344 * @param $_last INTEGER MSGNUM of last post
345 * @param $_ref STRING MSGNUM of current/selectionned post
346 */
347
348 function disp($_first=0, $_last=0, $_ref="") {
349 global $css;
350 $index = 1;
351 if (sizeof($this->overview)) {
352 foreach ($this->roots as $id) {
353 $this->_disp_desc($id, $index, $_first, $_last, $_ref);
354 $index += $this->overview[$id]->desc ;
355 if ($index > $_last) { break; }
356 }
357 } else {
358 echo "<tr class=\"{$css['pair']}\">\n";
359 echo "\t<td colspan=\"3\">\n";
360 echo "\t\tNo post in this newsgroup\n";
361 echo "\t</td>\n";
362 echo "</tr>\n";
363 }
364 }
365
366 /** computes linear post index
367 * @param $_id INTEGER MSGNUM of post
368 * @return INTEGER linear index of post
369 */
370
371 function getndx($_id) {
372 $ndx = 1;
373 $id_cur = $_id;
374 while (true) {
375 $id_parent = $this->overview[$id_cur]->parent;
376 if (is_null($id_parent)) break;
377 $pos = array_search($id_cur, $this->overview[$id_parent]->children);
378
379 for ($i = 0; $i < $pos ; $i++) {
380 $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc;
381 }
382 $ndx++; //noeud père
383
384 $id_cur = $id_parent;
385 }
386
387 foreach ($this->roots as $i) {
388 if ($i==$id_cur) {
389 break;
390 }
391 $ndx += $this->overview[$i]->desc;
392 }
393 return $ndx;
394 }
395 }
396
397 ?>