-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadClass.php
256 lines (238 loc) · 8.28 KB
/
UploadClass.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
class Upload
{
/** Defines if will use ES6+ or not*/
protected static $JSMode = 1; // 1 -> old ; 2 -> modern
/** Object's name for frontend handle */
private static $JSObject = "Upload";
/** Frontend function to be called */
private static $JSCall = "newUpload";
/** Root dir */
protected static $rootDir = null;
/** This is the input name to be called in frontend, each binded to a specific profile or the same */
protected static $inputs = array();
/** Array with all available upload profiles */
private static $profiles = array();
/**
* @param $name = profile name
* @param $config = the profile setup array
*
*/
public static function addProfile($name, $config)
{
/* $template = array(
"url" => "where the request will be sent",
"types" => array("jpeg", "jpg", "png"),
"folder" => "./uploads/",
"maxSize" => 260000,
"maxFiles" => 10,
"vars" => array(), // give additional variables
); */
if (isset($config['folder'])) {
$last = substr($config['folder'], -1);
if ($last == '/' || $last == "\\") {
$config['folder'] = substr($config['folder'], 0, -1);
}
}
self::$profiles[$name] = array(
"config" => $config,
"key" => md5($name),
"name" => $name
);
}
/**
* @param $name || $key = profile identifier
* @return array = profile config
* */
public static function getProfile($profile)
{
if (array_key_exists($profile, self::$profiles)) {
return self::$profiles[$profile];
}
$md5 = md5($profile);
if (array_key_exists($md5, self::$profiles)) {
return self::$profiles[$md5];
}
return false;
}
/**
* @param $input = input name on frontend
* @param $profile = profile name for binded input
*/
public static function addInput($input, $profile)
{
if (!isset(self::$inputs[$profile])) {
self::$inputs[$profile] = [];
}
self::$inputs[$profile][] = $input;
}
/**
* @param string $varKey = variable key to be added
* @param string $varValue = variable value to be added
* @param string $profile = which profile variable should be added
*/
public static function addVar($varKey, $varValue, $profile)
{
self::$profiles[$profile]['vars'][] = [$varKey => $varValue];
}
/**
* @param (string)$dir = root dir for the server
*/
public static function setRootDir($dir)
{
self::$rootDir = $dir;
}
/**
* @param (bool)$tags = Defines if output adds script tags as well
* @param $profile = Which profile to output
*/
public static function setProfiles($tags = true, $profile = "all")
{
foreach (self::$profiles as $profileName => $profileData) {
unset(self::$profiles[$profileName]['config']['folder']);
}
if ($profile !== "all" && !isset($profile, self::$profiles)) {
return false;
}
if ($profile === "all") {
foreach (self::$inputs as $uploadProfile => $inputs) {
if (array_key_exists($uploadProfile, self::$profiles)) {
self::$profiles[$uploadProfile]['inputNames'] = $inputs;
}
}
} else {
self::$profiles[$profile]['inputNames'] = self::$inputs[$profile];
}
if ($tags) {
echo "<script type='text/javascript'>";
echo (self::$JSMode == 1 ? "var" : "const") . " uploadProfiles = " . json_encode($profile == 'all' ? self::$profiles : self::$profiles[$profile]) . ";";
echo "</script>";
return true;
}
echo json_encode($profile == "all" ? self::$profiles : self::$profiles[$profile]);
return true;
}
/**
* @param $input = input name
* @param $profile = which profile to bind
* @param $tags = if tags will be echoed
*/
public static function setTo($input, $profile, $tags = true)
{
if ($tags) {
echo "<script type='text/javascript'>";
echo self::$JSObject . "." . self::$JSCall . "('" . $input . "','" . $profile . "');";
echo "</script>";
return true;
}
echo self::$JSObject . "." . self::$JSCall . "('" . $input . "','" . $profile . "');";
return true;
}
/**
* @param $fileName = file name...
* @param $data = part of file contents
* @param $folder = upload folder
*/
public static function saveFile($fileName, $data, $folder)
{
if (!is_dir($folder)) {
mkdir($folder, 0775, true);
chmod($folder, 0775);
}
$id = md5($fileName);
$jsonData = array(
"name" => $fileName,
"date" => date("Y-m-d"),
"status" => "uploading"
);
if (file_exists($folder . DIRECTORY_SEPARATOR . "upload.log.json")) {
$log = json_decode(file_get_contents($folder . DIRECTORY_SEPARATOR . "upload.log.json"), true);
$log[$id] = $jsonData;
} else {
$log = array();
$log[$id] = $jsonData;
}
file_put_contents($folder . DIRECTORY_SEPARATOR . "upload.log.json", json_encode($log));
self::removeTrash($folder);
$file = @fopen($folder . DIRECTORY_SEPARATOR . $fileName, 'a');
if ($file) {
$data = explode(',', $data);
fwrite($file, base64_decode(str_replace(" ", "+", $data[1])));
fclose($file);
return true;
} else {
// script doesn't have permission to create folders or files.
return false;
}
}
/**
* @param $folder = clears up canceled uploaded files
*/
private static function removeTrash($folder)
{
if (file_exists($folder . DIRECTORY_SEPARATOR . "upload.log.json")) {
$json = json_decode(file_get_contents($folder . DIRECTORY_SEPARATOR . "upload.log.json"), true);
$date = date("Y-m-d");
foreach ($json as $id => $data) {
if ($data["date"] !== $date) {
@unlink($folder . DIRECTORY_SEPARATOR . $data["name"]);
unset($json[$id]);
}
}
file_put_contents($folder . DIRECTORY_SEPARATOR . "upload.log.json", json_encode($json));
}
return true;
}
/**
* @param $fileName = file name...
* @param $folder = folder where file is uploaded
*/
public static function unsetLog($fileName, $folder)
{
if (file_exists($folder . DIRECTORY_SEPARATOR . "upload.log.json")) {
$json = json_decode(file_get_contents($folder . DIRECTORY_SEPARATOR . "upload.log.json"), true);
unset($json[md5($fileName)]);
if (empty($json)) {
@unlink($folder . DIRECTORY_SEPARATOR . "upload.log.json");
return true;
} else {
file_put_contents($folder . DIRECTORY_SEPARATOR . "upload.log.json", json_encode($json));
return true;
}
}
}
/**
* @param $path = path to delete file
*/
public static function delete($path)
{
if (file_exists($path) && !preg_match("/(\.)(php|js|css|html)/", $path)) {
if (unlink($path)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
/** @param $source = creates a new file name from source name */
public static function setNewName($source)
{
$aux = explode(".", $source);
$extension = $aux[count($aux) - 1];
unset($aux[count($aux) - 1]);
$aux = implode('.', $aux);
return preg_replace("/[^a-zA-Z0-9\.]/", "-", $aux) . md5($source) . md5(time()) . '.' . $extension;
}
/** @param $tags = defines if script tags will be appended to profile */
public static function init($tags = true)
{
self::setProfiles($tags);
foreach (self::$inputs as $profile => $inputs) {
foreach ($inputs as $input) {
self::setTo($input, $profile);
}
}
}
}