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