| 1 | <?php |
| 2 | /******************************************************************************** |
| 3 | * include/spool.inc.php : spool subroutines |
| 4 | * ----------------------- |
| 5 | * |
| 6 | * This file is part of the banana distribution |
| 7 | * Copyright: See COPYING files that comes with this distribution |
| 8 | ********************************************************************************/ |
| 9 | |
| 10 | /** Class spoolhead |
| 11 | * class used in thread overviews |
| 12 | */ |
| 13 | class spoolhead { |
| 14 | /** date (timestamp) */ |
| 15 | var $date; |
| 16 | /** subject */ |
| 17 | var $subject; |
| 18 | /** author */ |
| 19 | var $from; |
| 20 | /** reference of parent */ |
| 21 | var $parent; |
| 22 | /** array of children */ |
| 23 | var $children; |
| 24 | /** true if post is read */ |
| 25 | var $isread; |
| 26 | /** number of posts deeper in this branch of tree */ |
| 27 | var $desc; |
| 28 | /** same as desc, but counts only unread posts */ |
| 29 | var $descunread; |
| 30 | |
| 31 | /** constructor |
| 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) |
| 38 | */ |
| 39 | |
| 40 | function spoolhead($_date,$_subject,$_from,$_desc=1,$_read=true,$_descunread=0) { |
| 41 | $this->date=$_date; |
| 42 | $this->subject=$_subject; |
| 43 | $this->from=$_from; |
| 44 | $this->children=array(); |
| 45 | $this->desc=$_desc; |
| 46 | $this->isread=$_read; |
| 47 | $this->descunread=$_descunread; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** Class spool |
| 52 | * builds and updates spool |
| 53 | */ |
| 54 | |
| 55 | class spool { |
| 56 | # var $date; |
| 57 | /** spool */ |
| 58 | var $overview; |
| 59 | /** group name */ |
| 60 | var $group; |
| 61 | /** array msgid => msgnum */ |
| 62 | var $ids; |
| 63 | |
| 64 | /** constructor |
| 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) |
| 69 | */ |
| 70 | |
| 71 | function spool(&$_nntp,$_group,$_display=0,$_since=""){ |
| 72 | global $news; |
| 73 | $prefix_path=(preg_match("/\/scripts\/?$/",getcwd())?"..":"."); |
| 74 | $groupinfo = $_nntp->group($_group); |
| 75 | if (!$groupinfo) { |
| 76 | $this = false; |
| 77 | return false; |
| 78 | } |
| 79 | $spoolfile=realpath("$prefix_path/spool/spool-$_group.dat"); |
| 80 | if (file_exists($spoolfile)) { |
| 81 | $f = fopen($spoolfile,"r"); |
| 82 | $this = unserialize(fread($f,filesize($spoolfile))); |
| 83 | fclose($f); |
| 84 | $keys = array_values($this->ids); |
| 85 | rsort($keys); |
| 86 | $first = max($groupinfo[2]-$news['maxspool'],$groupinfo[1]); |
| 87 | $last = $groupinfo[2]; |
| 88 | // remove expired messages |
| 89 | $msgids=array_flip($this->ids); |
| 90 | for ($id=min(array_keys($this->overview)); $id<$first; $id++) { |
| 91 | $this->delid($id); |
| 92 | } |
| 93 | $this->ids=array_flip($msgids); |
| 94 | $first=max(array_keys($this->overview))+1; |
| 95 | } else { |
| 96 | $first = max($groupinfo[2]-$news['maxspool'],$groupinfo[1]); |
| 97 | $last = $groupinfo[2]; |
| 98 | $this->group = $_group; |
| 99 | } |
| 100 | |
| 101 | if (($first<=$groupinfo[2]) && ($groupinfo[0]!=0)) { |
| 102 | $dates = array_map("strtotime", |
| 103 | $_nntp->xhdr("Date","$first-$last")); |
| 104 | $msgids=$_nntp->xhdr("Message-ID","$first-$last"); |
| 105 | $subjects = array_map("headerdecode",$_nntp->xhdr("Subject", |
| 106 | "$first-$last")); |
| 107 | $froms = array_map("headerdecode",$_nntp->xhdr("From", |
| 108 | "$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)); |
| 113 | } else { |
| 114 | $this->ids=array_flip($msgids); |
| 115 | } |
| 116 | |
| 117 | foreach ($msgids as $id=>$msgid) { |
| 118 | if (isset($this->overview[$id])) { |
| 119 | $msg = $this->overview[$id]; |
| 120 | $msg->desc++; |
| 121 | } else { |
| 122 | $msg = new spoolhead($dates[$id],$subjects[$id],$froms[$id],1); |
| 123 | } |
| 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 | $p = $msg->parent; |
| 129 | while ($p) { |
| 130 | if (isset($this->overview[$p])) { |
| 131 | $this->overview[$p]->desc++; |
| 132 | if (isset($this->overview[$p]->parent)) { |
| 133 | $p = $this->overview[$p]->parent; |
| 134 | } else { |
| 135 | $p = false; |
| 136 | } |
| 137 | } else { |
| 138 | $this->overview[$p] = new spoolhead($dates[$p],$subjects[$p],$froms[$p],1); |
| 139 | $p = false; |
| 140 | } |
| 141 | } |
| 142 | if ($msg->parent!="") |
| 143 | $this->overview[$msg->parent]->children[]=$id; |
| 144 | $this->overview[$id] = $msg; |
| 145 | } |
| 146 | uasort($this->overview,"spoolcompare"); |
| 147 | $f = fopen("$prefix_path/spool/spool-$_group.dat","w"); |
| 148 | fputs($f,serialize($this)); |
| 149 | fclose($f); |
| 150 | } |
| 151 | |
| 152 | if ($_since != "") { |
| 153 | $newpostsids = $_nntp->newnews($_since,$_group); |
| 154 | if (sizeof($newpostsids)) { |
| 155 | $newpostsids = array_intersect($newpostsids, |
| 156 | array_keys($this->ids)); |
| 157 | if ($newpostsids && !is_null($newpostsids)) { |
| 158 | foreach ($newpostsids as $mid) { |
| 159 | $this->overview[$this->ids[$mid]]->isread=false; |
| 160 | $this->overview[$this->ids[$mid]]->descunread=1; |
| 161 | $parentmid = $this->overview[$this->ids[$mid]]->parent; |
| 162 | while (!is_null($parentmid)) { |
| 163 | $this->overview[$parentmid]->descunread++; |
| 164 | $parentmid = $this->overview[$parentmid]->parent; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | if (sizeof($newpostsids)>0) { |
| 170 | $flipids = array_flip($this->ids); |
| 171 | switch ($_display) { |
| 172 | case 1: |
| 173 | foreach ($this->overview as $i=>$p) { |
| 174 | if (isset($this->overview[$i]) && |
| 175 | !isset($this->overview[$i]->parent) && |
| 176 | ($this->overview[$i]->descunread==0)) { |
| 177 | $this->killdesc($i); |
| 178 | } |
| 179 | } |
| 180 | break; |
| 181 | case 2: |
| 182 | foreach ($this->overview as $i=>$p) { |
| 183 | if ($p->isread) { |
| 184 | unset($this->overview[$i]); |
| 185 | unset($flipids[$i]); |
| 186 | } |
| 187 | } |
| 188 | $this->ids=array_flip($flipids); |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | /** kill post and childrens |
| 197 | * @param $_id MSGNUM of post |
| 198 | */ |
| 199 | |
| 200 | function killdesc($_id) { |
| 201 | if (sizeof($this->overview[$_id]->children)) { |
| 202 | foreach ($this->overview[$_id]->children as $c) { |
| 203 | $this->killdesc($c); |
| 204 | } |
| 205 | } |
| 206 | unset($this->overview[$_id]); |
| 207 | # $flipid=array_flip($this->ids); |
| 208 | # unset($flipid[$id]); |
| 209 | # $this->ids=array_flip($flipid); |
| 210 | } |
| 211 | |
| 212 | /** delete a post from overview |
| 213 | * @param $_id MSGNUM of post |
| 214 | */ |
| 215 | |
| 216 | function delid($_id) { |
| 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, |
| 221 | array($_id)); |
| 222 | if (sizeof($this->overview[$_id]->children)) { |
| 223 | $this->overview[$this->overview[$_id]->parent]->children = |
| 224 | array_merge($this->overview[$this->overview[$_id]->parent]->children, |
| 225 | $this->overview[$_id]->children); |
| 226 | foreach ($this->overview[$_id]->children as $c) { |
| 227 | $this->overview[$c]->parent = $this->overview[$_id]->parent; |
| 228 | } |
| 229 | } |
| 230 | $p = $this->overview[$_id]->parent; |
| 231 | while ($p) { |
| 232 | $this->overview[$p]->desc--; |
| 233 | $p = $this->overview[$p]->parent; |
| 234 | } |
| 235 | } elseif (sizeof($this->overview[$_id]->children)) { |
| 236 | foreach ($this->overview[$_id]->children as $c) { |
| 237 | $this->overview[$c]->parent = null; |
| 238 | } |
| 239 | } |
| 240 | unset($this->overview[$_id]); |
| 241 | $ids = array_flip($this->ids); |
| 242 | unset($ids[$_id]); |
| 243 | $this->ids = array_flip($ids); |
| 244 | $prefix_path=(preg_match("/\/scripts\/?$/",getcwd())?"..":"."); |
| 245 | $f = fopen("$prefix_path/spool/spool-{$this->group}.dat","w"); |
| 246 | fputs($f,serialize($this)); |
| 247 | fclose($f); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** displays children tree of a post |
| 252 | * @param $_id INTEGER MSGNUM of post |
| 253 | * @param $_index INTEGER linear number of post in the tree |
| 254 | * @param $_first INTEGER linear number of first post displayed |
| 255 | * @param $_last INTEGER linear number of last post displayed |
| 256 | * @param $_ref STRING MSGNUM of current post |
| 257 | * @param $_pfx_node STRING prefix used for current node |
| 258 | * @param $_pfx_end STRING prefix used for children of current node |
| 259 | * @param $_head BOOLEAN true if first post in thread |
| 260 | */ |
| 261 | |
| 262 | function disp_desc($_id,$_index="",$_first=0,$_last=0,$_ref="", |
| 263 | $_pfx_node="", $_pfx_end="",$_head=true) { |
| 264 | global $css; |
| 265 | $debug = false; |
| 266 | $spfx_f = '<img src="img/k1.gif" height="21" width="9" alt="o" />'; |
| 267 | $spfx_n = '<img src="img/k2.gif" height="21" width="9" alt="*" />'; |
| 268 | $spfx_T = '<img src="img/T.gif" height="21" width="12" alt="+" />'; |
| 269 | $spfx_L = '<img src="img/L.gif" height="21" width="12" alt="`" />'; |
| 270 | $spfx_s = '<img src="img/s.gif" height="21" width="5" alt="-" />'; |
| 271 | $spfx_e = '<img src="img/e.gif" height="21" width="12" alt=" " />'; |
| 272 | $spfx_I = '<img src="img/I.gif" height="21" width="12"alt="|" />'; |
| 273 | |
| 274 | if ($_index == "") $_index = $this->getndx($_id); |
| 275 | |
| 276 | if (!sizeof($this->overview[$_id]->children) && ($_index<=$_last) |
| 277 | && ($_index>=$_first)) { |
| 278 | echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"])."\">\n"; |
| 279 | echo "<td class=\"{$css['date']}\">" |
| 280 | .formatSpoolHeader("date",$this->overview[$_id]->date,$_id, |
| 281 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
| 282 | ." </td>\n"; |
| 283 | echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">" |
| 284 | .$_pfx_node.($_head?$spfx_f:$spfx_s)."</div>" |
| 285 | .formatSpoolHeader("subject",$this->overview[$_id]->subject,$_id, |
| 286 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
| 287 | .($debug?" $_id $_index ". |
| 288 | $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n"; |
| 289 | echo "<td class=\"{$css['author']}\">" |
| 290 | .formatSpoolHeader("from",$this->overview[$_id]->from,$_id, |
| 291 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
| 292 | ." </td>\n</tr>"; |
| 293 | return true; |
| 294 | } |
| 295 | $children = $this->overview[$_id]->children; |
| 296 | if (($_index<=$_last) && ($_index>=$_first)) { |
| 297 | echo '<tr class="'.($_index%2?$css["pair"]:$css["impair"])."\">\n"; |
| 298 | echo "<td class=\"{$css['date']}\">" |
| 299 | .formatSpoolHeader("date",$this->overview[$_id]->date,$_id, |
| 300 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
| 301 | ." </td>\n"; |
| 302 | echo "<td class=\"{$css['subject']}\"><div class=\"{$css['tree']}\">" |
| 303 | .$_pfx_node.$spfx_n."</div>" |
| 304 | .formatSpoolHeader("subject",$this->overview[$_id]->subject,$_id, |
| 305 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
| 306 | .($debug?" $_id $_index ". |
| 307 | $this->overview[$_id]->desc." ".$this->overview[$_id]->descunread." ":"")." </td>\n"; |
| 308 | echo "<td class=\"{$css['author']}\">" |
| 309 | .formatSpoolHeader("from",$this->overview[$_id]->from,$_id, |
| 310 | $this->group,($_index==$_ref),$this->overview[$_id]->isread) |
| 311 | ." </td>\n</tr>"; |
| 312 | } |
| 313 | $index=$_index+1; |
| 314 | while ($child = array_shift($children)) { |
| 315 | if (($index+$this->overview[$child]->desc-1>=$_first) |
| 316 | ||($index<$_last)){ |
| 317 | if (sizeof($children)) { |
| 318 | $this->disp_desc($child,$index,$_first,$_last,$_ref, |
| 319 | $_pfx_end.$spfx_T,$_pfx_end.$spfx_I,false); |
| 320 | } else { |
| 321 | $this->disp_desc($child,$index,$_first,$_last,$_ref, |
| 322 | $_pfx_end.$spfx_L,$_pfx_end.$spfx_e,false); |
| 323 | } |
| 324 | } |
| 325 | $index += $this->overview[$child]->desc; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** Displays overview |
| 330 | * @param $_first INTEGER MSGNUM of first post |
| 331 | * @param $_last INTEGER MSGNUM of last post |
| 332 | * @param $_ref STRING MSGNUM of current/selectionned post |
| 333 | */ |
| 334 | |
| 335 | function disp($_first=0,$_last=0,$_ref="") { |
| 336 | global $css; |
| 337 | $index=1; |
| 338 | if (sizeof($this->overview)) { |
| 339 | foreach ($this->overview as $id=>$msg) { |
| 340 | if (!sizeof($msg->parent)) { |
| 341 | $this->disp_desc($id,$index,$_first,$_last,$_ref); |
| 342 | $index += $msg->desc; |
| 343 | } |
| 344 | } |
| 345 | } else { |
| 346 | echo "<tr class=\"{$css['pair']}\">\n"; |
| 347 | echo "\t<td colspan=\"3\">\n"; |
| 348 | echo "\t\tNo post in this newsgroup\n"; |
| 349 | echo "\t</td>\n"; |
| 350 | echo "</tr>\n"; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** computes linear post index |
| 355 | * @param $_id INTEGER MSGNUM of post |
| 356 | * @return INTEGER linear index of post |
| 357 | */ |
| 358 | |
| 359 | function getndx($_id) { |
| 360 | $ndx = 1; |
| 361 | // on remonte l'arbre |
| 362 | $id_parent = $this->overview[$_id]->parent; |
| 363 | $id_curr = $_id; |
| 364 | while (!is_null($id_parent)) { |
| 365 | for ($i=0; $i<array_search($id_curr, |
| 366 | $this->overview[$id_parent]->children) ; $i++) { |
| 367 | $ndx += $this->overview[$this->overview[$id_parent]->children[$i]]->desc; |
| 368 | } |
| 369 | $ndx++; //noeud père |
| 370 | $id_curr = $id_parent; |
| 371 | $id_parent = $this->overview[$id_curr]->parent; |
| 372 | } |
| 373 | // on compte les threads précédents |
| 374 | foreach ($this->overview as $i=>$p) { |
| 375 | if ($i==$id_curr) { |
| 376 | break; |
| 377 | } |
| 378 | if (is_null($p->parent)) { |
| 379 | $ndx += $this->overview[$i]->desc; |
| 380 | } |
| 381 | } |
| 382 | return $ndx; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | ?> |