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
184
185
186
187
188
189
190
191
192
193
|
<?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&fid=" . $this->fid;
}
public function show()
{
global $PERMSTORE, $current_user, $users;
$str = "";
$str .= " <div class=\"file\">\n";
if($current_user->uid == 0) {
$str .= " <a class=\"delete\" onclick=\"return confirm('Really delete?')\" onkeypress=\"return confirm('Really delete?')\" href=\"?mode=filehandler&task=delete&fid=" . $this->fid . "\">X</a>\n";
}
$str .= " <div class=\"preview\"><a href=\"" . $this->link() . "\"><img src=\"?mode=file&preview=1&fid=" . $this->fid . "\"/></a></div>\n";
$str .= " <div class=\"insertcommand\">{{" . $this->fid . "}}</div>\n";
$str .= " <div class=\"filename\"><a href=\"" . $this->link() . "\">" . $this->name . "</a></div>\n";
$str .= " <div class=\"mimetype\">" . $this->mimetype. "</div>\n";
$str .= " <div class=\"fileuser\">" . $users->getUser($this->uid)->name . "</div>\n";
$str .= " <div class=\"filesize\">" . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb</div>\n";
$str .= " <div class=\"filedate\">" . date("M jS Y - G:i", $this->date) . "</div>\n";
$str .= " </div>\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, "<?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;
$str = "";
$str .= "<div class=\"files\">\n";
$str .= " <div class=\"file\">\n";
$str .= " <div class=\"fileheader\">\n";
if($current_user->uid == 0) {
$str .= " <div class=\"delete\"></div>\n";
}
$str .= " <div class=\"preview\">Preview</div>\n";
$str .= " <div class=\"insertcommand\">Command</div>\n";
$str .= " <div class=\"filename\">Filename</div>\n";
$str .= " <div class=\"mimetype\">Mimetype</div>\n";
$str .= " <div class=\"fileuser\">User</div>\n";
$str .= " <div class=\"filesize\">Size</div>\n";
$str .= " <div class=\"filedate\">Date</div>\n";
$str .= " </div>\n";
$str .= " </div>\n";
foreach($this->files as $file) {
$str .= $file->show();
}
$str .= "</div>\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();
}
}
}
?>
|