c301e5de695f6526169bdfefa0abbf11ff5579cb
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
14 /** date (timestamp) */
20 /** reference of parent */
22 /** paren is direct */
24 /** array of children */
26 /** true if post is read */
28 /** number of posts deeper in this branch of tree */
30 /** same as desc, but counts only unread posts */
34 * @param $_date INTEGER timestamp of post
35 * @param $_subject STRING subject of post
36 * @param $_from STRING author of post
37 * @param $_desc INTEGER desc value (1 for a new post)
38 * @param $_read BOOLEAN true if read
39 * @param $_descunread INTEGER descunread value (0 for a new post)
42 function spoolhead($_date,$_subject,$_from,$_desc=1,$_read=true
,$_descunread=0) {
44 $this->subject
=$_subject;
46 $this->children
=array();
49 $this->descunread
=$_descunread;
54 * builds and updates spool
63 /** array msgid => msgnum */
67 * @param $_nntp RESOURCE NNTP socket filehandle
68 * @param $_group STRING group name
69 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
70 * @param $_since INTEGER time stamp (used for read/unread)
73 function spool(&$_nntp,$_group,$_display=0,$_since=""){
75 $prefix_path=(preg_match("/\/scripts\/?$/",getcwd())?
"..":".");
76 $groupinfo = $_nntp->group($_group);
81 $spoolfile=realpath("$prefix_path/spool/spool-$_group.dat");
82 if (file_exists($spoolfile)) {
83 $f = fopen($spoolfile,"r");
84 $this = unserialize(fread($f,filesize($spoolfile)));
86 $keys = array_values($this->ids
);
88 $first = max($groupinfo[2]-$news['maxspool'],$groupinfo[1]);
89 $last = $groupinfo[2];
90 // remove expired messages
91 $msgids=array_flip($this->ids
);
92 for ($id=min(array_keys($this->overview
)); $id<$first; $id++
) {
95 $this->ids
=array_flip($msgids);
96 $first=max(array_keys($this->overview
))+
1;
98 $first = max($groupinfo[2]-$news['maxspool'],$groupinfo[1]);
99 $last = $groupinfo[2];
100 $this->group
= $_group;
103 if (($first<=$groupinfo[2]) && ($groupinfo[0]!=0)) {
104 $dates = array_map("strtotime",
105 $_nntp->xhdr("Date","$first-$last"));
106 $msgids=$_nntp->xhdr("Message-ID","$first-$last");
107 $subjects = array_map("headerdecode",$_nntp->xhdr("Subject",
109 $froms = array_map("headerdecode",$_nntp->xhdr("From",
111 $refs = $_nntp->xhdr("References","$first-$last");
112 # $this->date=$nntp->date;
113 if (isset($this->ids
)) {
114 $this->ids
=array_merge($this->ids
,array_flip($msgids));
116 $this->ids
=array_flip($msgids);
119 foreach ($msgids as $id=>$msgid) {
120 if (isset($this->overview
[$id])) {
121 $msg = $this->overview
[$id];
124 $msg = new spoolhead($dates[$id],$subjects[$id],$froms[$id],1);
126 $refs[$id]=str_replace("><","> <",$refs[$id]);
127 $msgrefs=preg_split("/( |\t)/",strtr($refs[$id],$this->ids
));
128 $parents=preg_grep("/^\d+$/",$msgrefs);
129 $msg->parent
=array_pop($parents);
130 $msg->parent_direct
=preg_match("/^\d+$/",array_pop($msgrefs));
133 if (isset($this->overview
[$p])) {
134 $this->overview
[$p]->desc++
;
135 if (isset($this->overview
[$p]->parent
)) {
136 $p = $this->overview
[$p]->parent
;
141 $this->overview
[$p] = new spoolhead($dates[$p],$subjects[$p],$froms[$p],1);
145 if ($msg->parent
!="")
146 $this->overview
[$msg->parent
]->children
[]=$id;
147 $this->overview
[$id] = $msg;
149 uasort($this->overview
,"spoolcompare");
150 $f = fopen("$prefix_path/spool/spool-$_group.dat","w");
151 fputs($f,serialize($this));
156 $newpostsids = $_nntp->newnews($_since,$_group);
157 if (sizeof($newpostsids)) {
158 $newpostsids = array_intersect($newpostsids,
159 array_keys($this->ids
));
160 if ($newpostsids && !is_null($newpostsids)) {
161 foreach ($newpostsids as $mid) {
162 $this->overview
[$this->ids
[$mid]]->isread
=false
;
163 $this->overview
[$this->ids
[$mid]]->descunread
=1;
164 $parentmid=$this->ids
[$mid];
165 while (isset($this->overview
[$parentmid]->parent
)) {
166 $this->overview
[$parentmid]->descunread++
;
167 $parentmid = $this->overview
[$parentmid]->parent
;
172 if (sizeof($newpostsids)>0) {
173 $flipids = array_flip($this->ids
);
176 foreach ($this->overview
as $i=>$p) {
177 if (isset($this->overview
[$i]) &&
178 !isset($this->overview
[$i]->parent
) &&
179 ($this->overview
[$i]->descunread
==0)) {
185 foreach ($this->overview
as $i=>$p) {
187 unset($this->overview
[$i]);
191 $this->ids
=array_flip($flipids);
199 /** kill post and childrens
200 * @param $_id MSGNUM of post
203 function killdesc($_id) {
204 if (sizeof($this->overview
[$_id]->children
)) {
205 foreach ($this->overview
[$_id]->children
as $c) {
209 unset($this->overview
[$_id]);
210 $msgid=array_search($_id,$this->ids
);
212 unset($this->ids
[$msgid]);
214 # $flipid=array_flip($this->ids);
215 # unset($flipid[$id]);
216 # $this->ids=array_flip($flipid);
219 /** delete a post from overview
220 * @param $_id MSGNUM of post
223 function delid($_id) {
224 if (isset($this->overview
[$_id])) {
225 if (sizeof($this->overview
[$_id]->parent
)) {
226 $this->overview
[$this->overview
[$_id]->parent
]->children
=
227 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
,
229 if (sizeof($this->overview
[$_id]->children
)) {
230 $this->overview
[$this->overview
[$_id]->parent
]->children
=
231 array_merge($this->overview
[$this->overview
[$_id]->parent
]->children
,
232 $this->overview
[$_id]->children
);
233 foreach ($this->overview
[$_id]->children
as $c) {
234 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
235 $this->overview
[$c]->parent_direct
= false
;
238 $p = $this->overview
[$_id]->parent
;
240 $this->overview
[$p]->desc
--;
241 $p = $this->overview
[$p]->parent
;
243 } elseif (sizeof($this->overview
[$_id]->children
)) {
244 foreach ($this->overview
[$_id]->children
as $c) {
245 $this->overview
[$c]->parent
= null
;
248 unset($this->overview
[$_id]);
249 $msgid=array_search($_id,$this->ids
);
251 unset($this->ids
[$msgid]);
253 # $ids = array_flip($this->ids);
255 # $this->ids = array_flip($ids);
256 $prefix_path=(preg_match("/\/scripts\/?$/",getcwd())?
"..":".");
257 $f = fopen("$prefix_path/spool/spool-{$this->group}.dat","w");
258 fputs($f,serialize($this));
263 /** displays children tree of a post
264 * @param $_id INTEGER MSGNUM of post
265 * @param $_index INTEGER linear number of post in the tree
266 * @param $_first INTEGER linear number of first post displayed
267 * @param $_last INTEGER linear number of last post displayed
268 * @param $_ref STRING MSGNUM of current post
269 * @param $_pfx_node STRING prefix used for current node
270 * @param $_pfx_end STRING prefix used for children of current node
271 * @param $_head BOOLEAN true if first post in thread
274 function disp_desc($_id,$_index="",$_first=0,$_last=0,$_ref="",
275 $_pfx_node="", $_pfx_end="",$_head=true
) {
278 $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />';
279 $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />';
280 $spfx_Tnd = '<img src="img/T-direct.gif" height="21" width="12" alt="+" />';
281 $spfx_Lnd = '<img src="img/L-direct.gif" height="21" width="12" alt="`" />';
282 $spfx_snd = '<img src="img/s-direct.gif" height="21" width="5" alt="-" />';
283 $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
284 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
285 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
286 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />';
287 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
289 if ($_index == "") $_index = $this->getndx($_id);
291 if (!sizeof($this->overview
[$_id]->children
) && ($_index<=$_last)
292 && ($_index>=$_first)) {
293 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
294 echo "<td class=\"{$css['date']}\">"
295 .formatSpoolHeader("date",$this->overview
[$_id]->date
,$_id,
296 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
298 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
299 .$_pfx_node.($_head?
$spfx_f:
300 ($this->overview
[$_id]->parent_direct?
$spfx_s:$spfx_snd))
302 .formatSpoolHeader("subject",$this->overview
[$_id]->subject
,$_id,
303 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
304 .($debug?
" $_id $_index ".
305 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
306 echo "<td class=\"{$css['author']}\">"
307 .formatSpoolHeader("from",$this->overview
[$_id]->from
,$_id,
308 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
312 $children = $this->overview
[$_id]->children
;
313 if (($_index<=$_last) && ($_index>=$_first)) {
314 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
315 echo "<td class=\"{$css['date']}\">"
316 .formatSpoolHeader("date",$this->overview
[$_id]->date
,$_id,
317 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
319 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
320 .$_pfx_node.$spfx_n."</div>"
321 .formatSpoolHeader("subject",$this->overview
[$_id]->subject
,$_id,
322 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
323 .($debug?
" $_id $_index ".
324 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
325 echo "<td class=\"{$css['author']}\">"
326 .formatSpoolHeader("from",$this->overview
[$_id]->from
,$_id,
327 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
331 while ($child = array_shift($children)) {
332 if (($index+
$this->overview
[$child]->desc
-1>=$_first)
334 if (sizeof($children)) {
335 $this->disp_desc($child,$index,$_first,$_last,$_ref,$_pfx_end.
336 ($this->overview
[$child]->parent_direct?
$spfx_T:$spfx_Tnd),
337 $_pfx_end.$spfx_I,false
);
339 $this->disp_desc($child,$index,$_first,$_last,$_ref,$_pfx_end.
340 ($this->overview
[$child]->parent_direct?
$spfx_L:$spfx_Lnd),
341 $_pfx_end.$spfx_e,false
);
344 $index +
= $this->overview
[$child]->desc
;
348 /** Displays overview
349 * @param $_first INTEGER MSGNUM of first post
350 * @param $_last INTEGER MSGNUM of last post
351 * @param $_ref STRING MSGNUM of current/selectionned post
354 function disp($_first=0,$_last=0,$_ref="") {
357 if (sizeof($this->overview
)) {
358 foreach ($this->overview
as $id=>$msg) {
359 if (!isset($msg->parent
)) {
360 $this->disp_desc($id,$index,$_first,$_last,$_ref);
361 $index +
= $msg->desc
;
365 echo "<tr class=\"{$css['pair']}\">\n";
366 echo "\t<td colspan=\"3\">\n";
367 echo "\t\tNo post in this newsgroup\n";
373 /** computes linear post index
374 * @param $_id INTEGER MSGNUM of post
375 * @return INTEGER linear index of post
378 function getndx($_id) {
380 // on remonte l'arbre
381 $id_parent = $this->overview
[$_id]->parent
;
383 while (!is_null($id_parent)) {
384 for ($i=0; $i<array_search($id_curr,
385 $this->overview
[$id_parent]->children
) ; $i++
) {
386 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
389 $id_curr = $id_parent;
390 $id_parent = $this->overview
[$id_curr]->parent
;
392 // on compte les threads précédents
393 foreach ($this->overview
as $i=>$p) {
397 if (is_null($p->parent
)) {
398 $ndx +
= $this->overview
[$i]->desc
;