2 /********************************************************************************
3 * include/spool.inc.php : spool subroutines
4 * -----------------------
6 * This file is part of the banana distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
10 if(!function_exists('file_put_contents')) {
11 function file_put_contents($filename, $data)
13 $fp = fopen($filename, 'w');
15 trigger_error('file_put_contents cannot write in file '.$filename, E_USER_ERROR
);
23 function spoolCompare($a,$b) { return ($b->date
>=$a->date
); }
26 * class used in thread overviews
30 /** date (timestamp) */
36 /** reference of parent */
38 /** paren is direct */
40 /** array of children */
41 var $children = Array();
42 /** true if post is read */
44 /** number of posts deeper in this branch of tree */
46 /** same as desc, but counts only unread posts */
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)
58 function BananaSpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true
, $_descunread=0)
61 $this->subject
= $_subject;
64 $this->isread
= $_read;
65 $this->descunread
= $_descunread;
70 * builds and updates spool
73 define("BANANA_SPOOL_VERSION", '0.2');
82 /** array msgid => msgnum */
90 * @param $_group STRING group name
91 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
92 * @param $_since INTEGER time stamp (used for read/unread)
94 function BananaSpool($_group, $_display=0, $_since="")
97 $this->group
= $_group;
98 $groupinfo = $banana->nntp
->group($_group);
100 $this->valid
= false
;
104 $this->_readFromFile();
107 $first = $banana->maxspool ?
max($groupinfo[2] - $banana->maxspool
, $groupinfo[1]) : $groupinfo[1];
108 $last = $groupinfo[2];
110 if ($this->version
== BANANA_SPOOL_VERSION
&& is_array($this->overview
)) {
111 $mids = array_keys($this->overview
);
112 foreach ($mids as $id) {
113 if (($first <= $last && ($id < $first ||
$id > $last))
114 ||
($first > $last && $id < $first && $id > $last))
116 $this->delid($id, false
);
120 if (!empty($this->overview
)) {
121 $first = max(array_keys($this->overview
))+
1;
124 unset($this->overview
, $this->ids
);
125 $this->version
= BANANA_SPOOL_VERSION
;
128 if ($first<=$last && $groupinfo[0]) {
130 $this->_updateSpool("$first-$last");
133 if ($do_save) { $this->_saveToFile(); }
135 $this->_updateUnread($_since, $_display);
138 function _readFromFile()
140 $file = $this->_spoolfile();
141 if (file_exists($file)) {
142 $temp = unserialize(file_get_contents($file));
143 foreach (get_object_vars($temp) as $key=>$val) {
149 function _saveToFile()
151 $file = $this->_spoolfile();
152 uasort($this->overview
, "spoolcompare");
154 $this->roots
= Array();
155 foreach($this->overview
as $id=>$msg) {
156 if (is_null($msg->parent
)) {
157 $this->roots
[] = $id;
161 file_put_contents($file, serialize($this));
164 function _spoolfile()
167 $url = parse_url($banana->host
);
168 $file = $url['host'].'_'.$url['port'].'_'.$this->group
;
169 return dirname(dirname(__FILE__
)).'/spool/'.$file;
172 function _updateSpool($arg)
175 $dates = array_map('strtotime', $banana->nntp
->xhdr('Date', $arg));
176 $subjects = array_map('headerdecode', $banana->nntp
->xhdr('Subject', $arg));
177 $froms = array_map('headerdecode', $banana->nntp
->xhdr('From', $arg));
178 $msgids = $banana->nntp
->xhdr('Message-ID', $arg);
179 $refs = $banana->nntp
->xhdr('References', $arg);
181 if (is_array($this->ids
)) {
182 $this->ids
= array_merge($this->ids
, array_flip($msgids));
184 $this->ids
= array_flip($msgids);
187 foreach ($msgids as $id=>$msgid) {
188 $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]);
189 $refs[$id] = str_replace('><', '> <', $refs[$id]);
190 $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids
));
191 $parents = preg_grep('/^\d+$/', $msgrefs);
192 $msg->parent
= array_pop($parents);
193 $msg->parent_direct
= preg_match('/^\d+$/', array_pop($msgrefs));
195 if (isset($this->overview
[$id])) {
196 $msg->desc
= $this->overview
[$id]->desc
;
197 $msg->children
= $this->overview
[$id]->children
;
199 $this->overview
[$id] = $msg;
201 if ($p = $msg->parent
) {
202 if (empty($this->overview
[$p])) {
203 $this->overview
[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1);
205 $this->overview
[$p]->children
[] = $id;
208 $this->overview
[$p]->desc +
= $msg->desc
;
209 $p = $this->overview
[$p]->parent
;
215 function _updateUnread($since, $mode)
218 if (empty($since)) { return; }
220 if (is_array($newpostsids = $banana->nntp
->newnews($since, $this->group
))) {
221 if (!is_array($this->ids
)) { $this->ids
= array(); }
222 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
223 foreach ($newpostsids as $mid) {
224 $this->overview
[$this->ids
[$mid]]->isread
= false
;
225 $this->overview
[$this->ids
[$mid]]->descunread
= 1;
226 $parentmid = $this->ids
[$mid];
227 while (isset($parentmid)) {
228 $this->overview
[$parentmid]->descunread ++
;
229 $parentmid = $this->overview
[$parentmid]->parent
;
233 if (count($newpostsids)) {
236 foreach ($this->roots
as $k=>$i) {
237 if ($this->overview
[$i]->descunread
==0) {
239 unset($this->roots
[$k]);
248 /** kill post and childrens
249 * @param $_id MSGNUM of post
252 function killdesc($_id)
254 if (sizeof($this->overview
[$_id]->children
)) {
255 foreach ($this->overview
[$_id]->children
as $c) {
259 unset($this->overview
[$_id]);
260 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
261 unset($this->ids
[$msgid]);
265 /** delete a post from overview
266 * @param $_id MSGNUM of post
269 function delid($_id, $write=true
)
271 if (isset($this->overview
[$_id])) {
272 if (sizeof($this->overview
[$_id]->parent
)) {
273 $this->overview
[$this->overview
[$_id]->parent
]->children
=
274 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
275 if (sizeof($this->overview
[$_id]->children
)) {
276 $this->overview
[$this->overview
[$_id]->parent
]->children
=
277 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
278 foreach ($this->overview
[$_id]->children
as $c) {
279 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
280 $this->overview
[$c]->parent_direct
= false
;
283 $p = $this->overview
[$_id]->parent
;
285 $this->overview
[$p]->desc
--;
286 $p = $this->overview
[$p]->parent
;
288 } elseif (sizeof($this->overview
[$_id]->children
)) {
289 foreach ($this->overview
[$_id]->children
as $c) {
290 $this->overview
[$c]->parent
= null
;
293 unset($this->overview
[$_id]);
294 $msgid = array_search($_id, $this->ids
);
296 unset($this->ids
[$msgid]);
299 if ($write) { $this->_saveToFile(); }
303 /** displays children tree of a post
304 * @param $_id INTEGER MSGNUM of post
305 * @param $_index INTEGER linear number of post in the tree
306 * @param $_first INTEGER linear number of first post displayed
307 * @param $_last INTEGER linear number of last post displayed
308 * @param $_ref STRING MSGNUM of current post
309 * @param $_pfx_node STRING prefix used for current node
310 * @param $_pfx_end STRING prefix used for children of current node
311 * @param $_head BOOLEAN true if first post in thread
314 function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
316 $spfx_f = makeImg('k1', 'o', 21, 9);
317 $spfx_n = makeImg('k2', '*', 21, 9);
318 $spfx_Tnd = makeImg('T-direct', '+', 21, 12);
319 $spfx_Lnd = makeImg('L-direct', '`', 21, 12);
320 $spfx_snd = makeImg('s-direct', '-', 21, 5);
321 $spfx_T = makeImg('T', '+', 21, 12);
322 $spfx_L = makeImg('L', '`', 21, 12);
323 $spfx_s = makeImg('s', '-', 21, 5);
324 $spfx_e = makeImg('e', ' ', 21, 12);
325 $spfx_I = makeImg('I', '|', 21, 12);
327 if ($_index +
$this->overview
[$_id]->desc
< $_first ||
$_index > $_last) {
333 if ($_index>=$_first) {
334 $hc = empty($this->overview
[$_id]->children
);
336 $res .= '<tr class="'.($_index%2?
'pair':'impair').($this->overview
[$_id]->isread?
'':' new')."\">\n";
337 $res .= "<td class='date'>".fancyDate($this->overview
[$_id]->date
)." </td>\n";
338 $res .= "<td class='subj'>"
339 ."<div class='tree'>$_pfx_node".($hc?
($_head?
$spfx_f:($this->overview
[$_id]->parent_direct?
$spfx_s:$spfx_snd)):$spfx_n)
341 $subject = $this->overview
[$_id]->subject
;
342 if (strlen($subject) == 0) {
343 $subject = _b_('(pas de sujet)');
345 if ($_index == $_ref) {
346 $res .= '<span class="cur">'.htmlentities($subject).'</span>';
348 $res .= makeHREF(Array('group' => $this->group
,
350 htmlentities($subject));
352 $res .= "</td>\n<td class='from'>".formatFrom($this->overview
[$_id]->from
)."</td>\n</tr>";
354 if ($hc) { return $res; }
359 $children = $this->overview
[$_id]->children
;
360 while ($child = array_shift($children)) {
361 if ($_index > $_last) { return $res; }
362 if ($_index+
$this->overview
[$child]->desc
>= $_first) {
363 if (sizeof($children)) {
364 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
365 $_pfx_end.($this->overview
[$child]->parent_direct?
$spfx_T:$spfx_Tnd),
366 $_pfx_end.$spfx_I, false
);
368 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
369 $_pfx_end.($this->overview
[$child]->parent_direct?
$spfx_L:$spfx_Lnd),
370 $_pfx_end.$spfx_e, false
);
373 $_index +
= $this->overview
[$child]->desc
;
379 /** Displays overview
380 * @param $_first INTEGER MSGNUM of first post
381 * @param $_last INTEGER MSGNUM of last post
382 * @param $_ref STRING MSGNUM of current/selectionned post
385 function to_html($_first=0, $_last=0, $_ref = null
)
387 $res = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">';
389 if (is_null($_ref)) {
390 $res .= '<tr><th>'._b_('Date').'</th>';
391 $res .= '<th>'._b_('Sujet').'</th>';
392 $res .= '<th>'._b_('Auteur').'</th></tr>';
396 if (sizeof($this->overview
)) {
397 foreach ($this->roots
as $id) {
398 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
399 $index +
= $this->overview
[$id]->desc
;
400 if ($index > $_last) { break; }
403 $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>';
406 return $res .= '</table>';
409 /** computes linear post index
410 * @param $_id INTEGER MSGNUM of post
411 * @return INTEGER linear index of post
414 function getndx($_id)
419 $id_parent = $this->overview
[$id_cur]->parent
;
420 if (is_null($id_parent)) break;
421 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
423 for ($i = 0; $i < $pos ; $i++
) {
424 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
428 $id_cur = $id_parent;
431 foreach ($this->roots
as $i) {
435 $ndx +
= $this->overview
[$i]->desc
;
441 // vim:set et sw=4 sts=4 ts=4