summaryrefslogtreecommitdiff
path: root/forum/utils/files.php
blob: b91957b02741da3ad6f761143d945da8d27b83cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

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

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

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

	public function show()
	{
		global $PERMSTORE, $current_user;
		echo "<div class=\"file\">\n";
		if($current_user->uid == 0) {
			echo "  <a class=\"delete\" href=\"?mode=filehandler&amp;task=delete&amp;fid=" . $this->fid . "\">Delete</a>\n";
		}
		echo "  <div class=\"filename\">Filename: <a href=\"" . $this->link() . "\">" . $this->name  . "</a>(" . $this->mimetype. ")</div>\n";
		echo "  <div class=\"filesize\">Size: " . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb</div>\n";
		echo "  <div class=\"insertcommand\">Use this command to insert the file: {{" . $this->fid . "}}</div>\n";
		echo "</div>\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, "<?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, "        name=\"" . 
						 htmlspecialchars($file->name, 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()
	{
 		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)
	{
		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();
		}
	}
}
?>