1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Utils { |
4
|
|
|
|
5
|
|
|
use Modules\Informer, Utils\Messages, Explorer, Language, Request; |
6
|
|
|
|
7
|
|
|
abstract class Uploader { |
8
|
|
|
|
9
|
|
|
# Translate error code |
10
|
|
|
|
11
|
|
|
private static function translateError(int $error) { |
12
|
|
|
|
13
|
|
|
if ($error === UPLOAD_ERR_INI_SIZE) return 'UPLOADER_ERROR_INI_SIZE'; |
14
|
|
|
|
15
|
|
|
if ($error === UPLOAD_ERR_FORM_SIZE) return 'UPLOADER_ERROR_FORM_SIZE'; |
16
|
|
|
|
17
|
|
|
if ($error === UPLOAD_ERR_PARTIAL) return 'UPLOADER_ERROR_PARTIAL'; |
18
|
|
|
|
19
|
|
|
if ($error === UPLOAD_ERR_NO_FILE) return 'UPLOADER_ERROR_NO_FILE'; |
20
|
|
|
|
21
|
|
|
if ($error === UPLOAD_ERR_NO_TMP_DIR) return 'UPLOADER_ERROR_NO_TMP_DIR'; |
22
|
|
|
|
23
|
|
|
if ($error === UPLOAD_ERR_CANT_WRITE) return 'UPLOADER_ERROR_CANT_WRITE'; |
24
|
|
|
|
25
|
|
|
if ($error === UPLOAD_ERR_EXTENSION) return 'UPLOADER_ERROR_EXTENSION'; |
26
|
|
|
|
27
|
|
|
# ------------------------ |
28
|
|
|
|
29
|
|
|
return 'UPLOADER_ERROR_UNKNOWN'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
# Save uploaded file |
33
|
|
|
|
34
|
|
|
public static function save(string $name, string $dir_name) { |
35
|
|
|
|
36
|
|
|
if ((false === ($file = Request::file($name))) || !is_uploaded_file($file['tmp_name'])) return false; |
37
|
|
|
|
38
|
|
|
# Check for demo mode |
39
|
|
|
|
40
|
|
|
if (Informer::isDemoMode()) return 'DEMO_MODE_RESTRICTION'; |
41
|
|
|
|
42
|
|
|
# Check for upload errors |
43
|
|
|
|
44
|
|
|
if ($file['error'] !== UPLOAD_ERR_OK) return self::translateError($file['error']); |
45
|
|
|
|
46
|
|
|
# Check size |
47
|
|
|
|
48
|
|
|
if ($file['size'] > CONFIG_UPLOADS_MAX_SIZE) return 'UPLOADER_ERROR_SIZE'; |
49
|
|
|
|
50
|
|
|
# Check file extension |
51
|
|
|
|
52
|
|
|
$extensions = ['php', 'phtml', 'php3', 'php4', 'php5', 'phps']; |
53
|
|
|
|
54
|
|
|
if (in_array(Explorer::extension($file['name'], false), $extensions)) return 'UPLOADER_ERROR_TYPE'; |
55
|
|
|
|
56
|
|
|
# Check target directory |
57
|
|
|
|
58
|
|
|
if (!Explorer::isDir($dir_name)) return 'UPLOADER_ERROR_DIR'; |
59
|
|
|
|
60
|
|
|
# Check target file |
61
|
|
|
|
62
|
|
|
$file_name = ($dir_name . '/' . basename($file['name'])); |
63
|
|
|
|
64
|
|
|
if (Explorer::isDir($file_name) || Explorer::isFile($file_name)) return 'UPLOADER_ERROR_EXISTS'; |
65
|
|
|
|
66
|
|
|
# Save uploaded file |
67
|
|
|
|
68
|
|
|
if (!@move_uploaded_file($file['tmp_name'], $file_name)) return 'UPLOADER_ERROR_SAVE'; |
69
|
|
|
|
70
|
|
|
# ------------------------ |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
# Submit uploaded file |
76
|
|
|
|
77
|
|
|
public static function submit(string $name, string $dir_name) { |
78
|
|
|
|
79
|
|
|
$result = self::save($name, $dir_name); |
80
|
|
|
|
81
|
|
|
if (is_string($result)) { Messages::error(Language::get($result)); return false; } |
82
|
|
|
|
83
|
|
|
# ------------------------ |
84
|
|
|
|
85
|
|
|
return (true === $result); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|