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 ********************************************************************************/
11 * class used in thread overviews
15 /** date (timestamp) */
21 /** reference of parent */
23 /** paren is direct */
25 /** array of children */
26 var $children = Array();
27 /** true if post is read */
29 /** number of posts deeper in this branch of tree */
31 /** same as desc, but counts only unread posts */
35 * @param $_date INTEGER timestamp of post
36 * @param $_subject STRING subject of post
37 * @param $_from STRING author of post
38 * @param $_desc INTEGER desc value (1 for a new post)
39 * @param $_read BOOLEAN true if read
40 * @param $_descunread INTEGER descunread value (0 for a new post)
43 function SpoolHead($_date, $_subject, $_from, $_desc=1, $_read=true
, $_descunread=0)
46 $this->subject
= $_subject;
49 $this->isread
= $_read;
50 $this->descunread
= $_descunread;
55 * builds and updates spool
65 /** array msgid => msgnum */
69 * @param $_nntp RESOURCE NNTP socket filehandle
70 * @param $_group STRING group name
71 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
72 * @param $_since INTEGER time stamp (used for read/unread)
75 function spool(&$_nntp, $_group, $_display=0, $_since="")
78 $spool_path = dirname(dirname(__FILE__
)).'/spool';
79 $groupinfo = $_nntp->group($_group);
84 $spoolfile = realpath("$spool_path/spool-$_group.dat");
85 if (file_exists($spoolfile)) {
86 $this = unserialize(file_get_contents($spoolfile));
87 $keys = array_values($this->ids
);
89 $first = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
90 $last = $groupinfo[2];
91 // remove expired messages
92 $msgids = array_flip($this->ids
);
93 for ($id=min(array_keys($this->overview
)); $id<$first; $id++
) {
96 $this->ids
= array_flip($msgids);
97 $first = max(array_keys($this->overview
))+
1;
99 $first = max($groupinfo[2]-$news['maxspool'], $groupinfo[1]);
100 $last = $groupinfo[2];
101 $this->group
= $_group;
104 if (($first<=$groupinfo[2]) && ($groupinfo[0]!=0)) {
105 $dates = array_map("strtotime", $_nntp->xhdr("Date","$first-$last"));
106 $msgids = $_nntp->xhdr("Message-ID","$first-$last");
107 $subjects = array_map("headerdecode",$_nntp->xhdr("Subject", "$first-$last"));
108 $froms = array_map("headerdecode",$_nntp->xhdr("From", "$first-$last"));
109 $refs = $_nntp->xhdr("References","$first-$last");
110 #$this->date=$nntp->date;
111 if (isset($this->ids
)) {
112 $this->ids
= array_merge($this->ids
, array_flip($msgids));
114 $this->ids
= array_flip($msgids);
117 foreach ($msgids as $id=>$msgid) {
118 if (isset($this->overview
[$id])) {
119 $msg = $this->overview
[$id];
122 $msg = new spoolhead($dates[$id], $subjects[$id], $froms[$id], 1);
124 $refs[$id] = str_replace("><","> <", $refs[$id]);
125 $msgrefs = preg_split("/( |\t)/", strtr($refs[$id],$this->ids
));
126 $parents = preg_grep("/^\d+$/", $msgrefs);
127 $msg->parent
= array_pop($parents);
128 $msg->parent_direct
= preg_match("/^\d+$/",array_pop($msgrefs));
131 if (isset($this->overview
[$p])) {
132 $this->overview
[$p]->desc++
;
133 $p = isset($this->overview
[$p]->parent
) ?
$this->overview
[$p]->parent
: false
;
135 $this->overview
[$p] = new spoolhead($dates[$p], $subjects[$p], $froms[$p], 1);
139 if ($msg->parent
!="") {
140 $this->overview
[$msg->parent
]->children
[] = $id;
142 $this->overview
[$id] = $msg;
144 uasort($this->overview
, "spoolcompare");
145 file_put_contents("$spool_path/spool-$_group.dat", serialize($this));
149 $newpostsids = $_nntp->newnews($_since,$_group);
150 if (sizeof($newpostsids)) {
151 $newpostsids = array_intersect($newpostsids, array_keys($this->ids
));
152 if ($newpostsids && !is_null($newpostsids)) {
153 foreach ($newpostsids as $mid) {
154 $this->overview
[$this->ids
[$mid]]->isread
= false
;
155 $this->overview
[$this->ids
[$mid]]->descunread
= 1;
156 $parentmid = $this->ids
[$mid];
157 while (isset($parentmid)) {
158 $this->overview
[$parentmid]->descunread++
;
159 $parentmid = $this->overview
[$parentmid]->parent
;
164 if (sizeof($newpostsids)>0) {
165 $flipids = array_flip($this->ids
);
168 foreach ($this->overview
as $i=>$p) {
169 if (isset($this->overview
[$i]) &&
170 !isset($this->overview
[$i]->parent
) &&
171 ($this->overview
[$i]->descunread
==0))
179 foreach ($this->overview
as $i=>$p) {
181 unset($this->overview
[$i]);
185 $this->ids
= array_flip($flipids);
193 /** kill post and childrens
194 * @param $_id MSGNUM of post
197 function killdesc($_id)
199 if (sizeof($this->overview
[$_id]->children
)) {
200 foreach ($this->overview
[$_id]->children
as $c) {
204 unset($this->overview
[$_id]);
205 $msgid = array_search($_id, $this->ids
);
207 unset($this->ids
[$msgid]);
211 /** delete a post from overview
212 * @param $_id MSGNUM of post
217 if (isset($this->overview
[$_id])) {
218 if (sizeof($this->overview
[$_id]->parent
)) {
219 $this->overview
[$this->overview
[$_id]->parent
]->children
=
220 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
, array($_id));
221 if (sizeof($this->overview
[$_id]->children
)) {
222 $this->overview
[$this->overview
[$_id]->parent
]->children
=
223 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
, $this->overview
[$_id]->children
);
224 foreach ($this->overview
[$_id]->children
as $c) {
225 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
226 $this->overview
[$c]->parent_direct
= false
;
229 $p = $this->overview
[$_id]->parent
;
231 $this->overview
[$p]->desc
--;
232 $p = $this->overview
[$p]->parent
;
234 } elseif (sizeof($this->overview
[$_id]->children
)) {
235 foreach ($this->overview
[$_id]->children
as $c) {
236 $this->overview
[$c]->parent
= null
;
239 unset($this->overview
[$_id]);
240 $msgid = array_search($_id,$this->ids
);
242 unset($this->ids
[$msgid]);
245 $spool_path = dirname(dirname(__FILE__
)).'/spool';
246 file_put_contents("$spool_path/spool-$_group.dat", serialize($this));
250 /** displays children tree of a post
251 * @param $_id INTEGER MSGNUM of post
252 * @param $_index INTEGER linear number of post in the tree
253 * @param $_first INTEGER linear number of first post displayed
254 * @param $_last INTEGER linear number of last post displayed
255 * @param $_ref STRING MSGNUM of current post
256 * @param $_pfx_node STRING prefix used for current node
257 * @param $_pfx_end STRING prefix used for children of current node
258 * @param $_head BOOLEAN true if first post in thread
261 function disp_desc($_id,$_index="",$_first=0,$_last=0,$_ref="", $_pfx_node="", $_pfx_end="",$_head=true
) {
264 $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />';
265 $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />';
266 $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
267 $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />';
268 $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />';
269 $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
270 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
271 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
272 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />';
273 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
276 $_index = $this->getndx($_id);
279 if (!sizeof($this->overview
[$_id]->children
) && ($_index<=$_last) && ($_index>=$_first)) {
280 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
281 echo "<td class=\"{$css['date']}\">"
282 .formatSpoolHeader("date",$this->overview
[$_id]->date
,$_id,
283 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
285 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
286 .$_pfx_node.($_head?
$spfx_f:
287 ($this->overview
[$_id]->parent_direct?
$spfx_s:$spfx_snd))
289 .formatSpoolHeader("subject",$this->overview
[$_id]->subject
,$_id,
290 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
291 .($debug?
" $_id $_index ".
292 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
293 echo "<td class=\"{$css['author']}\">"
294 .formatSpoolHeader("from",$this->overview
[$_id]->from
,$_id,
295 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
299 $children = $this->overview
[$_id]->children
;
300 if (($_index<=$_last) && ($_index>=$_first)) {
301 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
302 echo "<td class=\"{$css['date']}\">"
303 .formatSpoolHeader("date",$this->overview
[$_id]->date
,$_id,
304 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
306 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
307 .$_pfx_node.$spfx_n."</div>"
308 .formatSpoolHeader("subject",$this->overview
[$_id]->subject
,$_id,
309 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
310 .($debug?
" $_id $_index ".
311 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
312 echo "<td class=\"{$css['author']}\">"
313 .formatSpoolHeader("from",$this->overview
[$_id]->from
,$_id,
314 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
318 while ($child = array_shift($children)) {
319 if (($index+
$this->overview
[$child]->desc
-1>=$_first)
321 if (sizeof($children)) {
322 $this->disp_desc($child,$index,$_first,$_last,$_ref,$_pfx_end.
323 ($this->overview
[$child]->parent_direct?
$spfx_T:$spfx_Tnd),
324 $_pfx_end.$spfx_I,false
);
326 $this->disp_desc($child,$index,$_first,$_last,$_ref,$_pfx_end.
327 ($this->overview
[$child]->parent_direct?
$spfx_L:$spfx_Lnd),
328 $_pfx_end.$spfx_e,false
);
331 $index +
= $this->overview
[$child]->desc
;
335 /** Displays overview
336 * @param $_first INTEGER MSGNUM of first post
337 * @param $_last INTEGER MSGNUM of last post
338 * @param $_ref STRING MSGNUM of current/selectionned post
341 function disp($_first=0,$_last=0,$_ref="") {
344 if (sizeof($this->overview
)) {
345 foreach ($this->overview
as $id=>$msg) {
346 if (!isset($msg->parent
)) {
347 $this->disp_desc($id,$index,$_first,$_last,$_ref);
348 $index +
= $msg->desc
;
352 echo "<tr class=\"{$css['pair']}\">\n";
353 echo "\t<td colspan=\"3\">\n";
354 echo "\t\tNo post in this newsgroup\n";
360 /** computes linear post index
361 * @param $_id INTEGER MSGNUM of post
362 * @return INTEGER linear index of post
365 function getndx($_id) {
367 // on remonte l'arbre
368 $id_parent = $this->overview
[$_id]->parent
;
370 while (!is_null($id_parent)) {
371 for ($i=0; $i<array_search($id_curr, $this->overview
[$id_parent]->children
) ; $i++
) {
372 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
375 $id_curr = $id_parent;
376 $id_parent = $this->overview
[$id_curr]->parent
;
378 // on compte les threads précédents
379 foreach ($this->overview
as $i=>$p) {
383 if (is_null($p->parent
)) {
384 $ndx +
= $this->overview
[$i]->desc
;