id, ENT_QUOTES, "UTF-8") . "\">\n");
foreach($this->values as $key => $value) {
fwrite($fp, " ");
fwrite($fp, htmlspecialchars($value, ENT_QUOTES, "UTF-8"));
fwrite($fp, "\n");
}
fwrite($fp, " \n");
}
public function value($key)
{
if(!isset($this->values[$key])) return "";
return $this->values[$key];
}
public function setValue($key, $value)
{
$this->values[$key] = $value;
}
public function CacheEntry($id)
{
$this->id = $id;
}
}
class Cache {
private $file;
public $entries = array();
public function rebuild($what = "all")
{
global $FORUMS_DIR, $users;
if($what == "all") $this->entries = array();
if($what == "forum_numberofthreads" || $what == "all") {
$entry = new CacheEntry("forum_numberofthreads");
$forums = new Forums($FORUMS_DIR . "/forums.xml");
foreach($forums->forums as $forum) {
$threads = new Threads($FORUMS_DIR . "/" . $forum->fid);
$entry->setValue($forum->fid, sizeof($threads->threads));
}
$this->add($entry);
}
if($what == "forum_lastpost" || $what == "all") {
$entry = new CacheEntry("forum_lastpost");
$forums = new Forums($FORUMS_DIR . "/forums.xml");
foreach($forums->forums as $forum) {
$threads = new Threads($FORUMS_DIR . "/" . $forum->fid);
foreach($users->users as $user) {
$unread = false;
foreach($threads->threads as $thread) {
if($thread->lastseen[$user->uid] < $thread->lastpost) {
$unread = true;
break;
}
}
$entry->setValue($forum->fid . "-" . $user->uid, $unread);
}
}
$this->add($entry);
}
$this->write();
}
public function add($entry) {
$key = $entry->id;
$this->entries[$key] = $entry;
}
public function write()
{
$fp = fopen($this->file, "w");
$block = TRUE;
flock($fp, LOCK_EX, $block); // do an exclusive lock
fwrite($fp, "\n");
fwrite($fp, "\n");
foreach($this->entries as $entry) {
$entry->write($fp);
}
fwrite($fp, "\n");
fclose($fp);
}
public function get($id)
{
return $this->entries[$id];
}
private function read()
{
$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load($this->file);
$ces = $dom->getElementsByTagName('entry');
foreach($ces as $c) {
$entry = new CacheEntry($c->getAttribute('id'));
foreach($c->childNodes as $v) {
if($v->tagName != "value") continue;
$entry->setValue($v->getAttribute('key'), $v->textContent);
}
$this->add($entry);
}
}
public function Cache($file)
{
$this->file = $file;
if(file_exists($this->file)) $this->read();
}
}
?>