-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathupload.php
More file actions
167 lines (153 loc) · 5.32 KB
/
Copy pathupload.php
File metadata and controls
167 lines (153 loc) · 5.32 KB
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
<?php
/**
* μlogger
*
* Copyright(C) 2019 Bartek Fabiszewski (www.fabiszewski.net)
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
require_once(ROOT_DIR . "/helpers/db.php");
require_once(ROOT_DIR . "/helpers/utils.php");
/**
* Uploaded files
*/
class uUpload {
const META_TYPE = "type";
const META_NAME = "name";
const META_TMP_NAME = "tmp_name";
const META_ERROR = "error";
const META_SIZE = "size";
public static $uploadDir = ROOT_DIR . "/uploads/";
private static $filePattern = "/[a-z0-9_.]{20,}/";
private static $mimeMap = [];
/**
* @return string[] Mime to extension mapping
*/
private static function getMimeMap() {
if (empty(self::$mimeMap)) {
self::$mimeMap["image/jpeg"] = "jpg";
self::$mimeMap["image/jpg"] = "jpg";
self::$mimeMap["image/x-ms-bmp"] = "bmp";
self::$mimeMap["image/gif"] = "gif";
self::$mimeMap["image/png"] = "png";
}
return self::$mimeMap;
}
/**
* Is mime accepted type
* @param string $mime Mime type
* @return bool True if known
*/
private static function isKnownMime($mime) {
return array_key_exists($mime, self::getMimeMap());
}
/**
* Get file extension for given mime
* @param $mime
* @return string|null Extension or null if not found
*/
private static function getExtension($mime) {
if (self::isKnownMime($mime)) {
return self::getMimeMap()[$mime];
}
return null;
}
/**
* Save file to uploads, basic sanitizing
* @param array $uploaded File meta array from $_FILES[]
* @param int $trackId
* @return string|null Unique file name, null on error
*/
public static function add($uploaded, $trackId) {
try {
$fileMeta = self::sanitizeUpload($uploaded);
} catch (Exception $e) {
syslog(LOG_ERR, $e->getMessage());
// save exception to txt file as image replacement?
return null;
}
$extension = self::getExtension($fileMeta[self::META_TYPE]);
do {
$fileName = uniqid("{$trackId}_") . ".$extension";
} while (file_exists(self::$uploadDir . $fileName));
if (move_uploaded_file($fileMeta[self::META_TMP_NAME], self::$uploadDir . $fileName)) {
return $fileName;
}
return null;
}
/**
* Delete upload from database and filesystem
* @param String $path File relative path
* @return bool False if file exists but can't be unlinked
*/
public static function delete($path) {
$ret = true;
if (preg_match(self::$filePattern, $path)) {
$path = self::$uploadDir . $path;
if (file_exists($path)) {
$ret = unlink($path);
}
}
return $ret;
}
/**
* @param array $fileMeta File meta array from $_FILES[]
* @param boolean $checkMime Check with known mime types
* @return array File metadata array
* @throws ErrorException Internal server exception
* @throws Exception File upload exception
*/
public static function sanitizeUpload($fileMeta, $checkMime = true) {
if (!isset($fileMeta) ||
!isset($fileMeta[self::META_NAME]) || !isset($fileMeta[self::META_TYPE]) ||
!isset($fileMeta[self::META_SIZE]) || !isset($fileMeta[self::META_TMP_NAME])) {
$message = "no uploaded file";
$lastErr = error_get_last();
if (!empty($lastErr)) {
$message = $lastErr["message"];
}
throw new ErrorException($message);
}
$uploadErrors = [];
$uploadErrors[UPLOAD_ERR_INI_SIZE] = "Uploaded file exceeds the upload_max_filesize directive in php.ini";
$uploadErrors[UPLOAD_ERR_FORM_SIZE] = "Uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
$uploadErrors[UPLOAD_ERR_PARTIAL] = "File was only partially uploaded";
$uploadErrors[UPLOAD_ERR_NO_FILE] = "No file was uploaded";
$uploadErrors[UPLOAD_ERR_NO_TMP_DIR] = "Missing a temporary folder";
$uploadErrors[UPLOAD_ERR_CANT_WRITE] = "Failed to write file to disk";
$uploadErrors[UPLOAD_ERR_EXTENSION] = "A PHP extension stopped file upload";
$file = null;
$fileError = isset($fileMeta[self::META_ERROR]) ? $fileMeta[self::META_ERROR] : UPLOAD_ERR_OK;
if ($fileMeta[self::META_SIZE] > uUtils::getSystemUploadLimit() && $fileError == UPLOAD_ERR_OK) {
$fileError = UPLOAD_ERR_FORM_SIZE;
}
if ($fileError == UPLOAD_ERR_OK) {
$file = $fileMeta[self::META_TMP_NAME];
} else {
$message = "Unknown error";
if (isset($uploadErrors[$fileError])) {
$message = $uploadErrors[$fileError];
}
$message .= " ($fileError)";
throw new Exception($message);
}
if (!$file || !file_exists($file)) {
throw new ErrorException("File not found");
}
if ($checkMime && !self::isKnownMime($fileMeta[self::META_TYPE])) {
throw new Exception("Unsupported mime type");
}
return $fileMeta;
}
}