pid . "\"\n"); fwrite($fp, $indent . " user=\"" . $this->user . "\"\n"); fwrite($fp, $indent . " title=\"" . convert_xml($this->title) . "\"\n"); fwrite($fp, $indent . " date=\"" . $this->date . "\">\n"); fwrite($fp, $indent . " " . convert_xml($this->message) . "\n"); foreach($this->replies as $reply) { $reply->write($fp, $indent . " "); } fwrite($fp, $indent . "\n"); } public function add($post) { $key = $post->pid; $this->replies[$key] = $post; } public function getPost($pid) { $result = false; foreach($this->replies as $post) { if($post->pid == $pid) return $post; $result = $post->getPost($pid); if($result) return $result; } return $result; } public function show($indent = " ", $recurse = true) { $str = ""; global $users, $fid, $tid, $current_user, $client_is_mobile_device; $user = $users->getUser($this->user); $str .= $indent . "
\n"; if($client_is_mobile_device) { $avatar = "mobileavatar.gif"; } else { if($user->avatar) $avatar = $user->avatar; else $avatar = "default.gif"; } $str .= $indent . " \"avatar\"\n"; if(!$client_is_mobile_device) { $str .= $indent . "
ID: " . $this->pid . "
\n"; $str .= $indent . "
Title: " . convert_xml($this->title) . "
\n"; } $str .= $indent . "
"; if(!$client_is_mobile_device) $str .= "UserID: "; $str .= $user->name . "
\n"; $str .= $indent . "
"; if(!$client_is_mobile_device) $str .= "Date: "; $str .= date("j. M Y - G:i", $this->date) . "
\n"; $str .= $indent . "
\n"; $str .= parse($this->message, $indent . " ") . "\n"; if(trim($user->signature) != "") { $str .= $indent . "
\n"; $str .= parse("--------------------------\n" . $user->signature, $indent . " ") . "\n"; $str .= $indent . "
\n"; } $str .= $indent . "
\n"; $str .= $indent . "
\n"; if($current_user->uid == $this->user) { $str .= $indent . " "; $str .= "\"edit\"\n"; } $str .= $indent . " "; $str .= "\"quote\"\n"; $str .= $indent . " "; $str .= "\"reply\"\n"; $str .= $indent . "
\n"; $str .= $indent . "
\n"; if($recurse) { foreach($this->replies as $reply) { $str .= $reply->show($indent . " "); } } $str .= $indent . "
\n"; $str .= $indent . "
\n"; return $str; } public function Post($pid, $title, $user, $date, $message = "") { $this->pid = $pid; $this->title = $title; $this->user = $user; $this->date = $date; $this->message = $message; } } class Posts { private $file; private $posts = array(); public $thread; private $posts_linear = array(); private $maxkey = 0; public function nextkey() { $this->maxkey++; return $this->maxkey; } public function add($post) { $key = $post->pid; $this->posts[$key] = $post; } public function write() { $fp = fopen($this->file, "w"); $block = TRUE; flock($fp, LOCK_EX, $block); // do an exclusive lock fwrite($fp, "\n"); if($this->thread->lastseen) { foreach($this->thread->lastseen as $key => $value) { if($lastseenstr != "") $lastseenstr .= ","; $lastseenstr .= $key . "=" . $value; } } fwrite($fp, "thread->tid . "\"\n"); fwrite($fp, " name=\"" . convert_xml($this->thread->name) . "\"\n"); fwrite($fp, " lastpost=\"" . $this->thread->lastpost . "\"\n"); fwrite($fp, " lastseen=\"" . $lastseenstr . "\">\n"); foreach($this->posts as $post) { $post->write($fp, " "); } fwrite($fp, "\n"); fclose($fp); } public function getPost($pid) { $result = false; foreach($this->posts as $post) { if($post->pid == $pid) return $post; $result = $post->getPost($pid); if($result) return $result; } return $result; } public function show() { global $current_user; $str = ""; $str .= "

Down to the bottom"; foreach($this->posts_linear as $post) { if($post->date > $this->thread->lastseen[$current_user->uid]) { $str .= " Down to first unread\n"; break; } } $str .= "

\n"; $str .= "

" . $this->thread->name . "

\n"; /* // Recursive foreach($this->posts as $post) { $post->show(); } */ // Linear $firstunread = false; foreach($this->posts_linear as $post) { if($post->date > $this->thread->lastseen[$current_user->uid] && $firstunread == false) { $firstunread = true; $str .= "
\n"; $str .= "
\n"; } $str .= $post->show(" ", false); } if($firstunread == true) { $str .= "
\n"; } $this->thread->lastseen[$current_user->uid] = time(); $this->write(); $str .= "

Up to the top

\n"; return $str; } private function recurser($parentpost, $element) { if($element->tagName != "post") return; $post = new Post($element->getAttribute('pid'), $element->getAttribute('title'), $element->getAttribute('user'), $element->getAttribute('date')); $this->posts_linear[$post->date . "-" . $post->pid] = $post; if($post->pid > $this->maxkey) $this->maxkey = $post->pid; if($parentpost) $parentpost->add($post); else $this->add($post); foreach($element->childNodes as $child) { if($child->tagName == "post") $this->recurser($post, $child); if($child->tagName == "message") $post->message = $child->textContent; } } private function read() { $dom = new DomDocument; $dom->resolveExternals = FALSE; $dom->substituteEntities = FALSE; $dom->preserveWhiteSpace = FALSE; $dom->load($this->file); $thread = $dom->documentElement; $this->thread = new Thread($thread->getAttribute('tid'), $thread->getAttribute('name'), $thread->getAttribute('lastpost'), $thread->getAttribute('lastseen')); foreach($thread->childNodes as $child) { $this->recurser(false, $child); } // The linear list should be sorted. sort($this->posts_linear); } public function Posts($file) { $this->file = $file; if(file_exists($this->file)) $this->read(); } } ?>