6c17d622bda5ace9a3525681a593f77b76d0c698
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 */
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)
92 function BananaSpool($_group, $_display=0, $_since="")
95 $this->group
= $_group;
96 $groupinfo = $banana->nntp
->group($_group);
97 if (!$groupinfo) { return ($this = null
); }
99 $this->_readFromFile();
102 $first = $banana->maxspool ?
max($groupinfo[2]-$banana->maxspool
, $groupinfo[1]) : $groupinfo[1];
103 $last = $groupinfo[2];
104 if ($this->version
== BANANA_SPOOL_VERSION
&& is_array($this->overview
)) {
105 if (count($this->overview
)) {
106 for ($id = min(array_keys($this->overview
)); $id<$first; $id++
) {
107 $this->delid($id, false
);
111 if (!empty($this->overview
)) {
112 $first = max(array_keys($this->overview
))+
1;
117 unset($this->overview
, $this->ids
);
118 $this->version
= BANANA_SPOOL_VERSION
;
121 if ($first<=$last && $groupinfo[0]) {
123 $this->_updateSpool("$first-$last");
126 if ($do_save) { $this->_saveToFile(); }
128 $this->_updateUnread($_since, $_display);
131 function _readFromFile()
133 $file = $this->_spoolfile();
134 if (file_exists($file)) {
135 $this = unserialize(file_get_contents($file));
139 function _saveToFile()
141 $file = $this->_spoolfile();
142 uasort($this->overview
, "spoolcompare");
144 $this->roots
= Array();
145 foreach($this->overview
as $id=>$msg) {
146 if (is_null($msg->parent
)) {
147 $this->roots
[] = $id;
151 file_put_contents($file, serialize($this));
154 function _spoolfile()
157 $url = parse_url($banana->host
);
158 $file = $url['host'].'_'.$url['port'].'_'.$this->group
;
159 return dirname(dirname(__FILE__
)).'/spool/'.$file;
162 function _updateSpool($arg)
165 $dates = array_map('strtotime', $banana->nntp
->xhdr('Date', $arg));
166 $subjects = array_map('headerdecode', $banana->nntp
->xhdr('Subject', $arg));
167 $froms = array_map('headerdecode', $banana->nntp
->xhdr('From', $arg));
168 $msgids = $banana->nntp
->xhdr('Message-ID', $arg);
169 $refs = $banana->nntp
->xhdr('References', $arg);
171 if (is_array($this->ids
)) {
172 $this->ids
= array_merge($this->ids
, array_flip($msgids));
174 $this->ids
= array_flip($msgids);
177 foreach ($msgids as $id=>$msgid) {
178 $msg = new BananaSpoolHead($dates[$id], $subjects[$id], $froms[$id]);
179 $refs[$id] = str_replace('><', '> <', $refs[$id]);
180 $msgrefs = preg_split("/[ \t]/", strtr($refs[$id], $this->ids
));
181 $parents = preg_grep('/^\d+$/', $msgrefs);
182 $msg->parent
= array_pop($parents);
183 $msg->parent_direct
= preg_match('/^\d+$/', array_pop($msgrefs));
185 if (isset($this->overview
[$id])) {
186 $msg->desc
= $this->overview
[$id]->desc
;
187 $msg->children
= $this->overview
[$id]->children
;
189 $this->overview
[$id] = $msg;
191 if ($p = $msg->parent
) {
192 if (empty($this->overview
[$p])) {
193 $this->overview
[$p] = new BananaSpoolHead($dates[$p], $subjects[$p], $froms[$p], 1);
195 $this->overview
[$p]->children
[] = $id;
198 $this->overview
[$p]->desc +
= $msg->desc
;
199 $p = $this->overview
[$p]->parent
;
205 function _updateUnread($since, $mode)
208 if (empty($since)) { return; }
210 if (is_array($newpostsids = $banana->nntp
->newnews($since, $this->group
))) {
211 if (!is_array($this->ids
)) { $this->ids
= array(); }
212 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
213 foreach ($newpostsids as $mid) {
214 $this->overview
[$this->ids
[$mid]]->isread
= false
;
215 $this->overview
[$this->ids
[$mid]]->descunread
= 1;
216 $parentmid = $this->ids
[$mid];
217 while (isset($parentmid)) {
218 $this->overview
[$parentmid]->descunread ++
;
219 $parentmid = $this->overview
[$parentmid]->parent
;
223 if (count($newpostsids)) {
226 foreach ($this->roots
as $k=>$i) {
227 if ($this->overview
[$i]->descunread
==0) {
229 unset($this->roots
[$k]);
238 /** kill post and childrens
239 * @param $_id MSGNUM of post
242 function killdesc($_id)
244 if (sizeof($this->overview
[$_id]->children
)) {
245 foreach ($this->overview
[$_id]->children
as $c) {
249 unset($this->overview
[$_id]);
250 if (($msgid = array_search($_id, $this->ids
)) !== false
) {
251 unset($this->ids
[$msgid]);
255 /** delete a post from overview
256 * @param $_id MSGNUM of post
259 function delid($_id, $write=true
)
261 if (isset($this->overview
[$_id])) {
262 if (sizeof($this->overview
[$_id]->parent
)) {
263 $this->overview
[$this->overview
[$_id]->parent
]->children
=
264 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
265 if (sizeof($this->overview
[$_id]->children
)) {
266 $this->overview
[$this->overview
[$_id]->parent
]->children
=
267 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
268 foreach ($this->overview
[$_id]->children
as $c) {
269 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
270 $this->overview
[$c]->parent_direct
= false
;
273 $p = $this->overview
[$_id]->parent
;
275 $this->overview
[$p]->desc
--;
276 $p = $this->overview
[$p]->parent
;
278 } elseif (sizeof($this->overview
[$_id]->children
)) {
279 foreach ($this->overview
[$_id]->children
as $c) {
280 $this->overview
[$c]->parent
= null
;
283 unset($this->overview
[$_id]);
284 $msgid = array_search($_id, $this->ids
);
286 unset($this->ids
[$msgid]);
289 if ($write) { $this->_saveToFile(); }
293 /** displays children tree of a post
294 * @param $_id INTEGER MSGNUM of post
295 * @param $_index INTEGER linear number of post in the tree
296 * @param $_first INTEGER linear number of first post displayed
297 * @param $_last INTEGER linear number of last post displayed
298 * @param $_ref STRING MSGNUM of current post
299 * @param $_pfx_node STRING prefix used for current node
300 * @param $_pfx_end STRING prefix used for children of current node
301 * @param $_head BOOLEAN true if first post in thread
304 function _to_html($_id, $_index, $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
)
306 $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />';
307 $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />';
308 $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
309 $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />';
310 $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />';
311 $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
312 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
313 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
314 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />';
315 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
317 if ($_index +
$this->overview
[$_id]->desc
< $_first ||
$_index > $_last) {
323 if ($_index>=$_first) {
324 $hc = empty($this->overview
[$_id]->children
);
326 $res .= '<tr class="'.($_index%2?
'pair':'impair').($this->overview
[$_id]->isread?
'':' new')."\">\n";
327 $res .= "<td class='date'>".fancyDate($this->overview
[$_id]->date
)." </td>\n";
328 $res .= "<td class='subj'>"
329 ."<div class='tree'>$_pfx_node".($hc?
($_head?
$spfx_f:($this->overview
[$_id]->parent_direct?
$spfx_s:$spfx_snd)):$spfx_n)
331 if ($_index == $_ref) {
332 $res .= '<span class="cur">'.htmlentities($this->overview
[$_id]->subject
).'</span>';
334 $res .= "<a href='?group={$this->group}&artid=$_id'>".htmlentities($this->overview
[$_id]->subject
).'</a>';
336 $res .= "</td>\n<td class='from'>".formatFrom($this->overview
[$_id]->from
)."</td>\n</tr>";
338 if ($hc) { return $res; }
343 $children = $this->overview
[$_id]->children
;
344 while ($child = array_shift($children)) {
345 if ($_index > $_last) { return $res; }
346 if ($_index+
$this->overview
[$child]->desc
>= $_first) {
347 if (sizeof($children)) {
348 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
349 $_pfx_end.($this->overview
[$child]->parent_direct?
$spfx_T:$spfx_Tnd),
350 $_pfx_end.$spfx_I, false
);
352 $res .= $this->_to_html($child, $_index, $_first, $_last, $_ref,
353 $_pfx_end.($this->overview
[$child]->parent_direct?
$spfx_L:$spfx_Lnd),
354 $_pfx_end.$spfx_e, false
);
357 $_index +
= $this->overview
[$child]->desc
;
363 /** Displays overview
364 * @param $_first INTEGER MSGNUM of first post
365 * @param $_last INTEGER MSGNUM of last post
366 * @param $_ref STRING MSGNUM of current/selectionned post
369 function to_html($_first=0, $_last=0, $_ref = null
)
371 $res = '<table class="bicol banana_thread" cellpadding="0" cellspacing="0">';
373 if (is_null($_ref)) {
374 $res .= '<tr><th>'._b_('Date').'</th>';
375 $res .= '<th>'._b_('Sujet').'</th>';
376 $res .= '<th>'._b_('Auteur').'</th></tr>';
380 if (sizeof($this->overview
)) {
381 foreach ($this->roots
as $id) {
382 $res .= $this->_to_html($id, $index, $_first, $_last, $_ref);
383 $index +
= $this->overview
[$id]->desc
;
384 if ($index > $_last) { break; }
387 $res .= '<tr><td colspan="3">'._b_('Aucun message dans ce forum').'</td></tr>';
390 return $res .= '</table>';
393 /** computes linear post index
394 * @param $_id INTEGER MSGNUM of post
395 * @return INTEGER linear index of post
398 function getndx($_id)
403 $id_parent = $this->overview
[$id_cur]->parent
;
404 if (is_null($id_parent)) break;
405 $pos = array_search($id_cur, $this->overview
[$id_parent]->children
);
407 for ($i = 0; $i < $pos ; $i++
) {
408 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
412 $id_cur = $id_parent;
415 foreach ($this->roots
as $i) {
419 $ndx +
= $this->overview
[$i]->desc
;