2070a67ba812922b9d178d25d0114805dd40ac03
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) {
12 $fp = fopen($filename, 'w');
14 trigger_error('file_put_contents cannot write in file '.$filename, E_USER_ERROR
);
25 * class used in thread overviews
29 /** date (timestamp) */
35 /** reference of parent */
37 /** paren is direct */
39 /** array of children */
40 var $children = Array();
41 /** true if post is read */
43 /** number of posts deeper in this branch of tree */
45 /** same as desc, but counts only unread posts */
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)
57 function SpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true
, $_descunread=0)
60 $this->subject
= $_subject;
63 $this->isread
= $_read;
64 $this->descunread
= $_descunread;
69 * builds and updates spool
72 define("BANANA_SPOOL_VERSION", '0.2');
81 /** array msgid => msgnum */
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)
91 function spool(&$_nntp, $_group, $_display=0, $_since="")
95 $spool_path = dirname(dirname(__FILE__
)).'/spool';
96 $spoolfile = "$spool_path/spool-$_group.dat";
98 $groupinfo = $_nntp->group($_group);
99 $first = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
100 $last = $groupinfo[2];
106 if (file_exists($spoolfile)) {
107 $this = unserialize(file_get_contents($spoolfile));
110 if ($this->version
== BANANA_SPOOL_VERSION
) {
111 $keys = array_values($this->ids
);
113 // remove expired messages
114 for ($id=min(array_keys($this->overview
)); $id<$first; $id++
) {
115 $this->delid($id, false
);
117 $first = max(array_keys($this->overview
))+
1;
119 unset($this->overview
, $this->ids
);
120 $this->group
= $_group;
121 $this->version
= BANANA_SPOOL_VERSION
;
124 if (($first<$last) && $groupinfo[0]) {
125 $dates = array_map("strtotime", $_nntp->xhdr("Date", "$first-$last"));
126 $subjects = array_map("headerdecode", $_nntp->xhdr("Subject", "$first-$last"));
127 $froms = array_map("headerdecode", $_nntp->xhdr("From", "$first-$last"));
128 $msgids = $_nntp->xhdr("Message-ID", "$first-$last");
129 $refs = $_nntp->xhdr("References", "$first-$last");
131 if (isset($this->ids
)) {
132 $this->ids
= array_merge($this->ids
, array_flip($msgids));
134 $this->ids
= array_flip($msgids);
137 foreach ($msgids as $id=>$msgid) {
138 $msg = new spoolhead($dates[$id], $subjects[$id], $froms[$id], 1);
139 $refs[$id] = str_replace("><", "> <", $refs[$id]);
140 $msgrefs = preg_split("/( |\t)/", strtr($refs[$id], $this->ids
));
141 $parents = preg_grep("/^\d+$/", $msgrefs);
142 $msg->parent
= array_pop($parents);
143 $msg->parent_direct
= preg_match("/^\d+$/", array_pop($msgrefs));
147 if (isset($this->overview
[$p])) {
148 $this->overview
[$p]->desc++
;
149 $p = $this->overview
[$p]->parent
;
151 $this->overview
[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
156 $this->overview
[$msg->parent
]->children
[] = $id;
158 $this->overview
[$id] = $msg;
160 uasort($this->overview
, "spoolcompare");
161 file_put_contents($spoolfile, serialize($this));
165 $newpostsids = $_nntp->newnews($_since, $_group);
166 if (sizeof($newpostsids)) {
167 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
168 if ($newpostsids && !is_null($newpostsids)) {
169 foreach ($newpostsids as $mid) {
170 $this->overview
[$this->ids
[$mid]]->isread
= false
;
171 $this->overview
[$this->ids
[$mid]]->descunread
= 1;
172 $parentmid = $this->ids
[$mid];
173 while (isset($parentmid)) {
174 $this->overview
[$parentmid]->descunread ++
;
175 $parentmid = $this->overview
[$parentmid]->parent
;
180 if (sizeof($newpostsids)>0) {
183 foreach ($this->overview
as $i=>$p) {
184 if (isset($this->overview
[$i]) &&
185 !isset($this->overview
[$i]->parent
) &&
186 ($this->overview
[$i]->descunread
==0))
194 $flipids = array_flip($this->ids
);
195 foreach ($this->overview
as $i=>$p) {
197 unset($this->overview
[$i]);
201 $this->ids
= array_flip($flipids);
209 /** kill post and childrens
210 * @param $_id MSGNUM of post
213 function killdesc($_id)
215 if (sizeof($this->overview
[$_id]->children
)) {
216 foreach ($this->overview
[$_id]->children
as $c) {
220 unset($this->overview
[$_id]);
221 $msgid = array_search($_id, $this->ids
);
223 unset($this->ids
[$msgid]);
227 /** delete a post from overview
228 * @param $_id MSGNUM of post
231 function delid($_id, $write=true
)
233 if (isset($this->overview
[$_id])) {
234 if (sizeof($this->overview
[$_id]->parent
)) {
235 $this->overview
[$this->overview
[$_id]->parent
]->children
=
236 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
237 if (sizeof($this->overview
[$_id]->children
)) {
238 $this->overview
[$this->overview
[$_id]->parent
]->children
=
239 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
240 foreach ($this->overview
[$_id]->children
as $c) {
241 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
242 $this->overview
[$c]->parent_direct
= false
;
245 $p = $this->overview
[$_id]->parent
;
247 $this->overview
[$p]->desc
--;
248 $p = $this->overview
[$p]->parent
;
250 } elseif (sizeof($this->overview
[$_id]->children
)) {
251 foreach ($this->overview
[$_id]->children
as $c) {
252 $this->overview
[$c]->parent
= null
;
255 unset($this->overview
[$_id]);
256 $msgid = array_search($_id, $this->ids
);
258 unset($this->ids
[$msgid]);
262 $spool_path = dirname(dirname(__FILE__
)).'/spool';
263 file_put_contents("$spool_path/spool-$_group.dat", serialize($this));
268 /** displays children tree of a post
269 * @param $_id INTEGER MSGNUM of post
270 * @param $_index INTEGER linear number of post in the tree
271 * @param $_first INTEGER linear number of first post displayed
272 * @param $_last INTEGER linear number of last post displayed
273 * @param $_ref STRING MSGNUM of current post
274 * @param $_pfx_node STRING prefix used for current node
275 * @param $_pfx_end STRING prefix used for children of current node
276 * @param $_head BOOLEAN true if first post in thread
279 function disp_desc($_id, $_index="", $_first=0, $_last=0, $_ref="", $_pfx_node="", $_pfx_end="", $_head=true
) {
282 $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />';
283 $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />';
284 $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
285 $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />';
286 $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />';
287 $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
288 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
289 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
290 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />';
291 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
294 $_index = $this->getndx($_id);
297 if (!sizeof($this->overview
[$_id]->children
) && ($_index<=$_last) && ($_index>=$_first)) {
298 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
299 echo "<td class=\"{$css['date']}\">"
300 .formatSpoolHeader("date", $this->overview
[$_id]->date
, $_id,
301 $this->group
, ($_index==$_ref), $this->overview
[$_id]->isread
)
303 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
304 .$_pfx_node.($_head?
$spfx_f:
305 ($this->overview
[$_id]->parent_direct?
$spfx_s:$spfx_snd))
307 .formatSpoolHeader("subject", $this->overview
[$_id]->subject
, $_id,
308 $this->group
, ($_index==$_ref), $this->overview
[$_id]->isread
)
309 .($debug?
" $_id $_index ".
310 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
311 echo "<td class=\"{$css['author']}\">"
312 .formatSpoolHeader("from", $this->overview
[$_id]->from
, $_id,
313 $this->group
, ($_index==$_ref), $this->overview
[$_id]->isread
)
317 $children = $this->overview
[$_id]->children
;
318 if (($_index<=$_last) && ($_index>=$_first)) {
319 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
320 echo "<td class=\"{$css['date']}\">"
321 .formatSpoolHeader("date", $this->overview
[$_id]->date
, $_id,
322 $this->group
, ($_index==$_ref), $this->overview
[$_id]->isread
)
324 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
325 .$_pfx_node.$spfx_n."</div>"
326 .formatSpoolHeader("subject", $this->overview
[$_id]->subject
, $_id,
327 $this->group
, ($_index==$_ref), $this->overview
[$_id]->isread
)
328 .($debug?
" $_id $_index ".
329 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
330 echo "<td class=\"{$css['author']}\">"
331 .formatSpoolHeader("from", $this->overview
[$_id]->from
, $_id,
332 $this->group
, ($_index==$_ref), $this->overview
[$_id]->isread
)
336 while ($child = array_shift($children)) {
337 if (($index+
$this->overview
[$child]->desc
-1>=$_first)
339 if (sizeof($children)) {
340 $this->disp_desc($child, $index, $_first, $_last, $_ref, $_pfx_end.
341 ($this->overview
[$child]->parent_direct?
$spfx_T:$spfx_Tnd),
342 $_pfx_end.$spfx_I, false
);
344 $this->disp_desc($child, $index, $_first, $_last, $_ref, $_pfx_end.
345 ($this->overview
[$child]->parent_direct?
$spfx_L:$spfx_Lnd),
346 $_pfx_end.$spfx_e, false
);
349 $index +
= $this->overview
[$child]->desc
;
353 /** Displays overview
354 * @param $_first INTEGER MSGNUM of first post
355 * @param $_last INTEGER MSGNUM of last post
356 * @param $_ref STRING MSGNUM of current/selectionned post
359 function disp($_first=0, $_last=0, $_ref="") {
362 if (sizeof($this->overview
)) {
363 foreach ($this->overview
as $id=>$msg) {
364 if (!isset($msg->parent
)) {
365 $this->disp_desc($id, $index, $_first, $_last, $_ref);
366 $index +
= $msg->desc
;
370 echo "<tr class=\"{$css['pair']}\">\n";
371 echo "\t<td colspan=\"3\">\n";
372 echo "\t\tNo post in this newsgroup\n";
378 /** computes linear post index
379 * @param $_id INTEGER MSGNUM of post
380 * @return INTEGER linear index of post
383 function getndx($_id) {
385 // on remonte l'arbre
386 $id_parent = $this->overview
[$_id]->parent
;
388 while (!is_null($id_parent)) {
389 for ($i=0; $i<array_search($id_curr, $this->overview
[$id_parent]->children
) ; $i++
) {
390 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
393 $id_curr = $id_parent;
394 $id_parent = $this->overview
[$id_curr]->parent
;
396 // on compte les threads précédents
397 foreach ($this->overview
as $i=>$p) {
401 if (is_null($p->parent
)) {
402 $ndx +
= $this->overview
[$i]->desc
;