body=str_replace("\n", "
", $text); } public function generateXHTML($post=null, $pre=null) { if($pre!=null) echo call_user_func($pre, $this); echo("

Subject: $this->subject

\n"); echo("

Email: $this->email

\n"); echo("

$this->body

\n"); if($post!=null) echo call_user_func($post, $this); echo("
\n"); foreach($this->comments as $comment) $comment->generateXHTML($post, $pre); echo("
\n"); } private function doRead($file) { $this->subject=trim(fgets($file)); $this->email=trim(fgets($file)); $this->body=trim(fgets($file)); $numComments=trim(fgets($file)); for($i=0; $i<$numComments; $i++) $this->comments[]=&self::readSingle($file); } public static function &readSingle($file) { $id=(int)trim(fgets($file)); if($id >= self::$nextID) self::$nextID=$id+1; self::$entries[$id]=new BlogEntry(); $entry=&self::$entries[$id]; $entry->id=$id; $entry->doRead($file); return $entry; } public static function readAll($filename) { self::$entries=array(); self::$rootEntries=array(); self::$nextID=0; $file=fopen($filename, "r"); if($file==false) return; while(!feof($file)) self::$rootEntries[]=&self::readSingle($file); fclose($file); } public function write($file) { fwrite($file, $this->id."\n"); fwrite($file, $this->subject."\n"); fwrite($file, $this->email."\n"); fwrite($file, $this->body."\n"); fwrite($file, count($this->comments)); foreach($this->comments as $comment) { fwrite($file, "\n"); $comment->write($file); } } public static function writeAll($filename) { $file=fopen($filename, "w"); $first=true; foreach(self::$rootEntries as $entry) { if(!$first) fwrite($file, "\n"); $first=false; $entry->write($file); } fclose($file); } public function &makeComment() { $result=self::makeEntry(); $this->comments[]=&$result; return $result; } private static function &makeEntry() { self::$entries[self::$nextID]=new BlogEntry(); $result=&self::$entries[self::$nextID]; $result->id=self::$nextID; self::$nextID++; return $result; } public static function &makeRootEntry() { $result=self::makeEntry(); self::$rootEntries[]=&$result; return $result; } public static function &getEntry($id) { return self::$entries[(int)$id]; } } ?>