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