From cce5e7710295021b41d9aaecc503a60fb99256be Mon Sep 17 00:00:00 2001 From: deva Date: Sat, 4 Oct 2008 10:38:03 +0000 Subject: Initial revision --- forum/utils/files.php | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 forum/utils/files.php (limited to 'forum/utils/files.php') diff --git a/forum/utils/files.php b/forum/utils/files.php new file mode 100644 index 0000000..d714ba2 --- /dev/null +++ b/forum/utils/files.php @@ -0,0 +1,134 @@ +fid; + } + + public function show() + { + global $PERMSTORE; + echo "
\n"; + echo " fid . "\">Delete\n"; + echo "
Filename: link() . "\">" . $this->name . "(" . $this->mimetype. ")
\n"; + echo "
Size: " . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb
\n"; + echo "
\n"; + } + + public function File($fid, $name, $mimetype) + { + $this->fid = $fid; + $this->name = $name; + $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, " name=\"" . + htmlspecialchars($file->name, 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() + { + foreach($this->files as $file) { + $file->show(); + } + } + + public function getFile($fid) + { + $file = $this->files[$fid]; + return $file; + } + + public function newFile($tempfile, $name) + { + global $PERMSTORE; + $fid = time(); + + // move tempfile to permstore and put it in db. + move_uploaded_file($tempfile, $PERMSTORE . "/" . $fid); + + $f = new File($fid, $name, 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('name'), + $f->getAttribute('mimetype')); + + $this->add($file); + } + + } + + public function Files($file) + { + $this->file = $file; + $this->read(); + } + +} +?> \ No newline at end of file -- cgit v1.2.3