summaryrefslogtreecommitdiff
path: root/forum/utils/files.php
blob: 7356ff81036b4638c9ef707cd413269e2b442ab5 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?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();
		}
	}
}
?>