<?php

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/mimetypes.php");

class File {
	public $fid;
	public $uid;
	public $name;
	public $date;
	public $mimetype;

	public function link()
	{
		return "?mode=file&amp;fid=" . $this->fid;
	}

	public function show()
	{
		global $PERMSTORE, $current_user, $users;
		echo "  <div class=\"file\">\n";
		if($current_user->uid == 0) {
			echo "    <a class=\"delete\" onclick=\"return confirm('Really delete?')\" onkeypress=\"return confirm('Really delete?')\" href=\"?mode=filehandler&amp;task=delete&amp;fid=" . $this->fid . "\">X</a>\n";
		}
		echo "    <div class=\"preview\"><a href=\"" . $this->link() . "\"><img src=\"?mode=file&amp;preview=1&amp;fid=" . $this->fid . "\"/></a></div>\n";
		echo "    <div class=\"insertcommand\">{{" . $this->fid . "}}</div>\n";
		echo "    <div class=\"filename\"><a href=\"" . $this->link() . "\">" . $this->name  . "</a></div>\n";
		echo "    <div class=\"mimetype\">" . $this->mimetype. "</div>\n";
		echo "    <div class=\"fileuser\">" . $users->getUser($this->uid)->name . "</div>\n";
		echo "    <div class=\"filesize\">" . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb</div>\n";
		echo "    <div class=\"filedate\">" . date("M jS Y - G:i", $this->date) . "</div>\n";
		echo "  </div>\n";
	}

	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, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<files>\n");
 		foreach($this->files as $file) {
			fwrite($fp, "  <file fid=\"" .
						 htmlspecialchars($file->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, "  </file>\n");
		}
		fwrite($fp, "</files>\n");

		fclose($fp);
	}

	public function show()
	{
		global $current_user;
		echo "<div class=\"files\">\n";
		echo "  <div class=\"file\">\n";
		echo "  <div class=\"fileheader\">\n";
		if($current_user->uid == 0) {
			echo "    <div class=\"delete\"></div>\n";
		}
		echo "    <div class=\"preview\">Preview</div>\n";
		echo "    <div class=\"insertcommand\">Command</div>\n";
		echo "    <div class=\"filename\">Filename</div>\n";
		echo "    <div class=\"mimetype\">Mimetype</div>\n";
		echo "    <div class=\"fileuser\">User</div>\n";
		echo "    <div class=\"filesize\">Size</div>\n";
		echo "    <div class=\"filedate\">Date</div>\n";
		echo "  </div>\n";
		echo "  </div>\n";
 		foreach($this->files as $file) {
			$file->show();
		}
		echo "</div>\n";
	}

	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);
		}

	}

	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();
		}
	}
}
?>