fid;
	}
	public function show()
	{
		global $PERMSTORE, $current_user, $users;
		$str = "";
		$str .= "  
\n";
		if($current_user->uid == 0) {
			$str .= "    
fid . "\">X\n";
		}
		$str .= "    
\n";
		$str .= "    
{{" . $this->fid . "}}
\n";
		$str .= "    
\n";
		$str .= "    
" . $this->mimetype. "
\n";
		$str .= "    
" . $users->getUser($this->uid)->name . "
\n";
		$str .= "    
" . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb
\n";
		$str .= "    
" . date("M jS Y - G:i", $this->date) . "
\n";
		$str .= "  
 \n";
		return $str;
	}
	public function File($fid, $uid, $name, $date, $mimetype)
	{
		$this->fid = $fid;
		$this->uid = $uid;
		$this->name = $name;
		$this->date = $date;
		$this->mimetype = $mimetype;
	}
}
class Files {
	private $file;
	public $files = array();
	
	public function add($file) {
		$key = $file->fid;
		$this->files[$key] = $file;
	}
	
	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->files as $file) {
			fwrite($fp, "  fid, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "        uid=\"" . 
						 htmlspecialchars($file->uid, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "        name=\"" . 
						 htmlspecialchars($file->name, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "        date=\"" . 
						 htmlspecialchars($file->date, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "        mimetype=\"" . 
						 htmlspecialchars($file->mimetype, ENT_QUOTES, "UTF-8") . "\">\n");
			fwrite($fp, "  \n");
		}
		fwrite($fp, "\n");
		fclose($fp);
	}
	public function show()
	{
		global $current_user;
		$str = "";
		$str .= "\n";
		$str .= "  
\n";
		$str .= "  \n";
		$str .= "  
\n";
 		foreach($this->files as $file) {
			$str .= $file->show();
		}
		$str .= "
 \n";
		return $str;
	}
	public function getFile($fid)
	{
		$file = $this->files[$fid]; 
		return $file;
	}
	public function newFile($tempfile, $name)
	{
		global $PERMSTORE, $current_user;
		$fid = time();
		// move tempfile to permstore and put it in db.
		move_uploaded_file($tempfile, $PERMSTORE . "/" . $fid);
		$f = new File($fid, $current_user->uid, $name, time(), getMimeType($name)->name);
		$this->add($f);
		// We cannot wait to write the db, otherwise we'll get inconsistency!
		$this->write();
		// Return new file id.
		return $fid;
	}
	public function deleteFile($fid)
	{
		global $PERMSTORE;
		unlink($PERMSTORE . "/" . $fid);
		unset($this->files[$fid]);
		// We cannot wait to write the db, otherwise we'll get inconsitency!
		$this->write();
	}
	private function read()
	{
		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);
		$files = $dom->getElementsByTagName('file');
		foreach ($files as $f) {
			$file = new File($f->getAttribute('fid'),
											 $f->getAttribute('uid'),
											 $f->getAttribute('name'),
											 $f->getAttribute('date'),
											 $f->getAttribute('mimetype'));
			$this->add($file);
		}
		krsort($this->files);
	}
	public function Files($file)
	{
		global $PERMSTORE;
		$this->file = $file;
		if(file_exists($file)) $this->read();
		if(!file_exists($PERMSTORE)) {
			if(!mkdir($PERMSTORE)) {
				echo"Could not create directory: " . $PERMSTORE;
				die();
			}
		}
		if(!is_dir($PERMSTORE)) {
			echo $PERMSTORE . " exists but is not a directory";
			die();
		}
		if(!is_readable($PERMSTORE) || !is_writeable($PERMSTORE) || !is_executable($PERMSTORE)) {
			echo $PERMSTORE . " exists but does not have the correct permissions. (r/w/x)";
			die();
		}
	}
}
?>