3d54680c58f30859dae28060b05947c289529864
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 /** array of children */
24 /** true if post is read */
26 /** number of posts deeper in this branch of tree */
28 /** same as desc, but counts only unread posts */
32 * @param $_date INTEGER timestamp of post
33 * @param $_subject STRING subject of post
34 * @param $_from STRING author of post
35 * @param $_desc INTEGER desc value (1 for a new post)
36 * @param $_read BOOLEAN true if read
37 * @param $_descunread INTEGER descunread value (0 for a new post)
40 function spoolhead($_date,$_subject,$_from,$_desc=1,$_read=true
,$_descunread=0) {
42 $this->subject
=$_subject;
44 $this->children
=array();
47 $this->descunread
=$_descunread;
52 * builds and updates spool
61 /** array msgid => msgnum */
65 * @param $_nntp RESOURCE NNTP socket filehandle
66 * @param $_group STRING group name
67 * @param $_display INTEGER 1 => all posts, 2 => only threads with new posts
68 * @param $_since INTEGER time stamp (used for read/unread)
71 function spool(&$_nntp,$_group,$_display=0,$_since=""){
73 $groupinfo = $_nntp->group($_group);
78 $spoolfile=realpath("./spool/spool-$_group.dat");
79 if (file_exists($spoolfile)) {
80 $f = fopen($spoolfile,"r");
81 $this = unserialize(fread($f,filesize($spoolfile)));
83 $keys = array_values($this->ids
);
85 $first = max($groupinfo[2]-$news['maxspool'],$groupinfo[1]);
86 $last = $groupinfo[2];
87 // remove expired messages
88 $msgids=array_flip($this->ids
);
89 for ($id=min(array_keys($this->overview
)); $id<$first; $id++
) {
92 $this->ids
=array_flip($msgids);
93 $first=max(array_keys($this->overview
))+
1;
95 $first = max($groupinfo[2]-$news['maxspool'],$groupinfo[1]);
96 $last = $groupinfo[2];
97 $this->group
= $_group;
100 if (($first<=$groupinfo[2]) && ($groupinfo[0]!=0)) {
101 $dates = array_map("strtotime",
102 $_nntp->xhdr("Date","$first-$last"));
103 $msgids=$_nntp->xhdr("Message-ID","$first-$last");
104 $subjects = array_map("headerdecode",$_nntp->xhdr("Subject",
106 $froms = array_map("headerdecode",$_nntp->xhdr("From",
108 $refs = $_nntp->xhdr("References","$first-$last");
109 # $this->date=$nntp->date;
110 if (isset($this->ids
)) {
111 $this->ids
=array_merge($this->ids
,array_flip($msgids));
113 $this->ids
=array_flip($msgids);
116 foreach ($msgids as $id=>$msgid) {
117 if (isset($this->overview
[$id])) {
118 $msg = $this->overview
[$id];
121 $msg = new spoolhead($dates[$id],$subjects[$id],$froms[$id],1);
123 $refs[$id]=str_replace("><","> <",$refs[$id]);
124 $msgrefs=preg_split("/( |\t)/",strtr($refs[$id],$this->ids
));
125 $parents=preg_grep("/^\d+$/",$msgrefs);
126 $msg->parent
=array_pop($parents);
129 if (isset($this->overview
[$p])) {
130 $this->overview
[$p]->desc++
;
131 if (isset($this->overview
[$p]->parent
)) {
132 $p = $this->overview
[$p]->parent
;
137 $this->overview
[$p] = new spoolhead($dates[$p],$subjects[$p],$froms[$p],1);
141 if ($msg->parent
!="")
142 $this->overview
[$msg->parent
]->children
[]=$id;
143 $this->overview
[$id] = $msg;
145 uasort($this->overview
,"spoolcompare");
146 $f = fopen("spool/spool-$_group.dat","w");
147 fputs($f,serialize($this));
152 $newpostsids = $_nntp->newnews($_since,$_group);
153 if (sizeof($newpostsids)) {
154 $newpostsids = array_intersect($newpostsids,
155 array_keys($this->ids
));
156 if ($newpostsids && !is_null($newpostsids)) {
157 foreach ($newpostsids as $mid) {
158 $this->overview
[$this->ids
[$mid]]->isread
=false
;
159 $this->overview
[$this->ids
[$mid]]->descunread
=1;
160 $parentmid = $this->overview
[$this->ids
[$mid]]->parent
;
161 while (!is_null($parentmid)) {
162 $this->overview
[$parentmid]->descunread++
;
163 $parentmid = $this->overview
[$parentmid]->parent
;
168 if (sizeof($newpostsids)>0) {
169 $flipids = array_flip($this->ids
);
172 foreach ($this->overview
as $i=>$p) {
173 if (isset($this->overview
[$i]) &&
174 !isset($this->overview
[$i]->parent
) &&
175 ($this->overview
[$i]->descunread
==0)) {
181 foreach ($this->overview
as $i=>$p) {
183 unset($this->overview
[$i]);
187 $this->ids
=array_flip($flipids);
195 /** kill post and childrens
196 * @param $_id MSGNUM of post
199 function killdesc($_id) {
200 if (sizeof($this->overview
[$_id]->children
)) {
201 foreach ($this->overview
[$_id]->children
as $c) {
205 unset($this->overview
[$_id]);
206 # $flipid=array_flip($this->ids);
207 # unset($flipid[$id]);
208 # $this->ids=array_flip($flipid);
211 /** delete a post from overview
212 * @param $_id MSGNUM of post
215 function delid($_id) {
216 if (isset($this->overview
[$_id])) {
217 if (sizeof($this->overview
[$_id]->parent
)) {
218 $this->overview
[$this->overview
[$_id]->parent
]->children
=
219 array_diff($this->overview
[$this->overview
[$_id]->parent
]->children
,
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
,
224 $this->overview
[$_id]->children
);
225 foreach ($this->overview
[$_id]->children
as $c) {
226 $this->overview
[$c]->parent
= $this->overview
[$_id]->parent
;
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 $ids = array_flip($this->ids
);
242 $this->ids
= array_flip($ids);
243 $f = fopen("spool/spool-{$this->group}.dat","w");
244 fputs($f,serialize($this));
249 /** displays children tree of a post
250 * @param $_id INTEGER MSGNUM of post
251 * @param $_index INTEGER linear number of post in the tree
252 * @param $_first INTEGER linear number of first post displayed
253 * @param $_last INTEGER linear number of last post displayed
254 * @param $_ref STRING MSGNUM of current post
255 * @param $_pfx_node STRING prefix used for current node
256 * @param $_pfx_end STRING prefix used for children of current node
257 * @param $_head BOOLEAN true if first post in thread
260 function disp_desc($_id,$_index="",$_first=0,$_last=0,$_ref="",
261 $_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_T = '<img src="img/T.gif" height="21" width="12" alt="+" />';
267 $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />';
268 $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />';
269 $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />';
270 $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />';
272 if ($_index == "") $_index = $this->getndx($_id);
274 if (!sizeof($this->overview
[$_id]->children
) && ($_index<=$_last)
275 && ($_index>=$_first)) {
276 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
277 echo "<td class=\"{$css['date']}\">"
278 .formatSpoolHeader("date",$this->overview
[$_id]->date
,$_id,
279 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
281 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
282 .$_pfx_node.($_head?
$spfx_f:$spfx_s)."</div>"
283 .formatSpoolHeader("subject",$this->overview
[$_id]->subject
,$_id,
284 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
285 .($debug?
" $_id $_index ".
286 $this->overview
[$_id]->desc
." ".$this->overview
[$_id]->descunread
." ":"")." </td>\n";
287 echo "<td class=\"{$css['author']}\">"
288 .formatSpoolHeader("from",$this->overview
[$_id]->from
,$_id,
289 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
293 $children = $this->overview
[$_id]->children
;
294 if (($_index<=$_last) && ($_index>=$_first)) {
295 echo '<tr class="'.($_index%2?
$css["pair"]:$css["impair"])."\">\n";
296 echo "<td class=\"{$css['date']}\">"
297 .formatSpoolHeader("date",$this->overview
[$_id]->date
,$_id,
298 $this->group
,($_index==$_ref),$this->overview
[$_id]->isread
)
300 echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">"
301 .$_pfx_node.$spfx_n."</div>"
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 while ($child = array_shift($children)) {
313 if (($index+
$this->overview
[$child]->desc
-1>=$_first)
315 if (sizeof($children)) {
316 $this->disp_desc($child,$index,$_first,$_last,$_ref,
317 $_pfx_end.$spfx_T,$_pfx_end.$spfx_I,false
);
319 $this->disp_desc($child,$index,$_first,$_last,$_ref,
320 $_pfx_end.$spfx_L,$_pfx_end.$spfx_e,false
);
323 $index +
= $this->overview
[$child]->desc
;
327 /** Displays overview
328 * @param $_first INTEGER MSGNUM of first post
329 * @param $_last INTEGER MSGNUM of last post
330 * @param $_ref STRING MSGNUM of current/selectionned post
333 function disp($_first=0,$_last=0,$_ref="") {
336 if (sizeof($this->overview
)) {
337 foreach ($this->overview
as $id=>$msg) {
338 if (!sizeof($msg->parent
)) {
339 $this->disp_desc($id,$index,$_first,$_last,$_ref);
340 $index +
= $msg->desc
;
344 echo "<tr class=\"{$css['pair']}\">\n";
345 echo "\t<td colspan=\"3\">\n";
346 echo "\t\tNo post in this newsgroup\n";
352 /** computes linear post index
353 * @param $_id INTEGER MSGNUM of post
354 * @return INTEGER linear index of post
357 function getndx($_id) {
359 // on remonte l'arbre
360 $id_parent = $this->overview
[$_id]->parent
;
362 while (!is_null($id_parent)) {
363 for ($i=0; $i<array_search($id_curr,
364 $this->overview
[$id_parent]->children
) ; $i++
) {
365 $ndx +
= $this->overview
[$this->overview
[$id_parent]->children
[$i]]->desc
;
368 $id_curr = $id_parent;
369 $id_parent = $this->overview
[$id_curr]->parent
;
371 // on compte les threads précédents
372 foreach ($this->overview
as $i=>$p) {
376 if (is_null($p->parent
)) {
377 $ndx +
= $this->overview
[$i]->desc
;