Browse.php
15.3 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
<?php
N2Loader::import('libraries.image.image');
class N2SystemBackendBrowseControllerAjax extends N2BackendControllerAjax {
public function actionIndex() {
$this->validateToken();
$root = N2Filesystem::fixPathSeparator(N2Filesystem::getImagesFolder());
$path = N2Filesystem::realpath($root . '/' . ltrim(rtrim(N2Request::getVar('path', ''), '/'), '/'));
if (strpos($path, $root) !== 0) {
$path = $root;
}
$_directories = glob($path . NDS . '*', GLOB_ONLYDIR);
(object)$directories = array();
for ($i = 0; $i < count($_directories); $i++) {
$directories[basename($_directories[$i])] = N2Filesystem::toLinux($this->relative($_directories[$i], $root));
}
$extensions = array(
'jpg',
'jpeg',
'png',
'gif',
'mp4',
'mp3',
'svg'
);
$_files = scandir($path);
$files = array();
for ($i = 0; $i < count($_files); $i++) {
$_files[$i] = $path . NDS . $_files[$i];
$ext = strtolower(pathinfo($_files[$i], PATHINFO_EXTENSION));
if (self::check_utf8($_files[$i]) && in_array($ext, $extensions)) {
$files[basename($_files[$i])] = N2ImageHelper::dynamic(N2Filesystem::pathToAbsoluteURL($_files[$i]));
}
}
$relativePath = N2Filesystem::toLinux($this->relative($path, $root));
if (!$relativePath) {
$relativePath = '';
}
$this->response->respond(array(
'path' => $relativePath,
'directories' => $directories,
'files' => (object)$files
));
}
private static function check_utf8($str) {
$len = strlen($str);
for ($i = 0; $i < $len; $i++) {
$c = ord($str[$i]);
if ($c > 128) {
if (($c > 247)) return false; elseif ($c > 239) $bytes = 4;
elseif ($c > 223) $bytes = 3;
elseif ($c > 191) $bytes = 2;
else return false;
if (($i + $bytes) > $len) return false;
while ($bytes > 1) {
$i++;
$b = ord($str[$i]);
if ($b < 128 || $b > 191) return false;
$bytes--;
}
}
}
return true;
}
public function actionUpload() {
if (defined('N2_IMAGE_UPLOAD_DISABLE')) {
N2Message::error(n2_('You are not allowed to upload!'));
$this->response->error();
}
$this->validateToken();
$root = N2Filesystem::getImagesFolder();
$folder = ltrim(rtrim(N2Request::getVar('path', ''), '/'), '/');
$path = N2Filesystem::realpath($root . '/' . $folder);
if ($path === false || $path == '') {
$folder = preg_replace("/[^A-Za-z0-9]/", '', $folder);
if (empty($folder)) {
N2Message::error(n2_('Folder is missing!'));
$this->response->error();
} else {
N2Filesystem::createFolder($root . '/' . $folder);
$path = N2Filesystem::realpath($root . '/' . $folder);
}
}
$relativePath = N2Filesystem::toLinux($this->relative($path, $root));
if (!$relativePath) {
$relativePath = '';
}
$response = array(
'path' => $relativePath
);
try {
if (isset($_FILES) && isset($_FILES['image']) && isset($_FILES['image']['name'])) {
$info = pathinfo($_FILES['image']['name']);
$fileName = preg_replace('/[^a-zA-Z0-9_-]/', '', $info['filename']);
if (strlen($fileName) == 0) {
$fileName = '';
}
$upload = new N2BulletProof();
$file = $upload->uploadDir($path)
->upload($_FILES['image'], $fileName);
$response['name'] = basename($file);
$response['url'] = N2ImageHelper::dynamic(N2Filesystem::pathToAbsoluteURL($file));
N2ImageHelper::onImageUploaded($file);
}
} catch (Exception $e) {
N2Message::error($e->getMessage());
$this->response->error();
}
$this->response->respond($response);
}
private function relative($path, $root) {
return substr(N2Filesystem::fixPathSeparator($path), strlen($root));
}
}
/**
* BULLETPROOF,
*
* This is a one-file solution for a quick and safe way of
* uploading, watermarking, cropping and resizing images
* during and after uploads with PHP with best security.
*
* This class is heavily commented, to be as much friendly as possible.
* Please help out by posting out some bugs/flaws if you encounter any. Thanks!
*
* @category Image uploader
* @package BulletProof
* @version 1.4.0
* @author samayo
* @link https://github.com/samayo/BulletProof
* @license Luke 3:11 ( Free )
*/
class N2ImageUploaderException extends Exception {
}
class N2BulletProof {
/*
|--------------------------------------------------------------------------
| Image Upload Properties
\--------------------------------------------------------------------------*/
/**
* Set a group of default image types to upload.
*
* @var array
*/
protected $imageType = array(
"jpg",
"jpeg",
"png",
"gif"
);
/**
* Set a default file size to upload. Values are in bytes. Remember: 1kb ~ 1000 bytes.
*
* @var array
*/
protected $imageSize = array(
"min" => 1,
"max" => 20000000
);
/**
* Set a default min & maximum height & width for image to upload.
*
* @var array
*/
protected $imageDimension = array(
"height" => 10000,
"width" => 10000
);
/**
* Set a default folder to upload images, if it does not exist, it will be created.
*
* @var string
*/
protected $uploadDir = "uploads";
/**
* To get the real image/mime type. i.e gif, jpeg, png, ....
*
* @var string
*/
protected $getMimeType;
/*
|--------------------------------------------------------------------------
| Image Upload Methods
\--------------------------------------------------------------------------*/
/**
* Stores image types to upload
*
* @param array $fileTypes - ex: ['jpg', 'doc', 'txt'].
*
* @return $this
*/
public function fileTypes(array $fileTypes) {
$this->imageType = $fileTypes;
return $this;
}
/**
* Minimum and Maximum allowed image size for upload (in bytes),
*
* @param array $fileSize - ex: ['min'=>500, 'max'=>1000]
*
* @return $this
*/
public function limitSize(array $fileSize) {
$this->imageSize = $fileSize;
return $this;
}
/**
* Default & maximum allowed height and width image to download.
*
* @param array $dimensions
*
* @return $this
*/
public function limitDimension(array $dimensions) {
$this->imageDimension = $dimensions;
return $this;
}
/**
* Get the real image's Extension/mime type
*
* @param $imageName
*
* @return mixed
* @throws N2ImageUploaderException
*/
protected function getMimeType($imageName) {
if (!file_exists($imageName)) {
throw new N2ImageUploaderException("Image " . $imageName . " does not exist");
}
$listOfMimeTypes = array(
1 => "gif",
"jpeg",
"png",
"swf",
"psd",
"bmp",
"tiff",
"tiff",
"jpc",
"jp2",
"jpx",
"jb2",
"swc",
"iff",
"wbmp",
"xmb",
"ico",
"svg"
);
$imageType = N2Image::exif_imagetype($imageName);
if (isset($listOfMimeTypes[$imageType])) {
return $listOfMimeTypes[$imageType];
}
return false;
}
/**
* Handy method for getting image dimensions (W & H) in pixels.
*
* @param $getImage - The image name
*
* @return array
*/
protected function getPixels($getImage) {
list($width, $height) = getImageSize($getImage);
return array(
"width" => $width,
"height" => $height
);
}
/**
* Rename file either from method or by generating a random one.
*
* @param $isNameProvided - A new name for the file.
*
* @return string
*/
protected function imageRename($isNameProvided) {
if ($isNameProvided) {
return $isNameProvided . "." . $this->getMimeType;
}
return uniqid(true) . "_" . str_shuffle(implode(range("E", "Q"))) . "." . $this->getMimeType;
}
/**
* Get the specified upload dir, if it does not exist, create a new one.
*
* @param $directoryName - directory name where you want your files to be uploaded
* @param $filePermissions - octal representation of file permissions in linux environment
*
* @return $this
* @throws N2ImageUploaderException
*/
public function uploadDir($directoryName) {
if (!file_exists($directoryName) && !is_dir($directoryName)) {
$createFolder = mkdir("" . $directoryName, N2Filesystem::$dirPermission, true);
if (!$createFolder) {
throw new N2ImageUploaderException("Folder " . $directoryName . " could not be created");
}
}
$this->uploadDir = $directoryName;
return $this;
}
/**
* For getting common error messages from FILES[] array during upload.
*
* @return array
*/
protected function commonUploadErrors($key) {
$uploadErrors = array(
UPLOAD_ERR_OK => "...",
UPLOAD_ERR_INI_SIZE => "File is larger than the specified amount set by the server",
UPLOAD_ERR_FORM_SIZE => "File is larger than the specified amount specified by browser",
UPLOAD_ERR_PARTIAL => "File could not be fully uploaded. Please try again later",
UPLOAD_ERR_NO_FILE => "File is not found",
UPLOAD_ERR_NO_TMP_DIR => "Can't write to disk, due to server configuration ( No tmp dir found )",
UPLOAD_ERR_CANT_WRITE => "Failed to write file to disk. Please check you file permissions",
UPLOAD_ERR_EXTENSION => "A PHP extension has halted this file upload process"
);
return $uploadErrors[$key];
}
/**
* Simple file check and delete wrapper.
*
* @param $fileToDelete
*
* @return bool
* @throws N2ImageUploaderException
*/
public function deleteFile($fileToDelete) {
if (file_exists($fileToDelete) && !unlink($fileToDelete)) {
throw new N2ImageUploaderException("File may have been deleted or does not exist");
}
return true;
}
/**
* Final image uploader method, to check for errors and upload
*
* @param $fileToUpload
* @param null $isNameProvided
*
* @return string
* @throws N2ImageUploaderException
*/
public function upload($fileToUpload, $isNameProvided = null) {
$isMedia = false;
// Check if any errors are thrown by the FILES[] array
if ($fileToUpload["error"]) {
throw new N2ImageUploaderException($this->commonUploadErrors($fileToUpload["error"]));
}
if (function_exists("mime_content_type")) {
$rawMime = mime_content_type($fileToUpload["tmp_name"]);
} else {
$path_parts = pathinfo($_FILES["tmp_name"]["name"]);
switch ($path_parts['extension']) {
case 'mp4':
$rawMime = 'video/mp4';
break;
case 'mp3':
$rawMime = 'audio/mpeg';
break;
}
}
switch ($rawMime) {
case 'video/mp4':
$this->getMimeType = 'mp4';
$isMedia = true;
break;
case 'audio/mpeg':
$this->getMimeType = 'mp3';
$isMedia = true;
break;
}
if (!$isMedia) {
// First get the real file extension
$this->getMimeType = $this->getMimeType($fileToUpload["tmp_name"]);
// Check if this file type is allowed for upload
if (!in_array($this->getMimeType, $this->imageType)) {
throw new N2ImageUploaderException(" This is not allowed file type!
Please only upload ( " . implode(", ", $this->imageType) . " ) file types");
}
//Check if size (in bytes) of the image are above or below of defined in 'limitSize()'
if ($fileToUpload["size"] < $this->imageSize["min"] || $fileToUpload["size"] > $this->imageSize["max"]) {
throw new N2ImageUploaderException("File sizes must be between " . implode(" to ", $this->imageSize) . " bytes");
}
// check if image is valid pixel-wise.
$pixel = $this->getPixels($fileToUpload["tmp_name"]);
if ($pixel["width"] < 4 || $pixel["height"] < 4) {
throw new N2ImageUploaderException("This file is either too small or corrupted to be an image");
}
if ($pixel["height"] > $this->imageDimension["height"] || $pixel["width"] > $this->imageDimension["width"]) {
throw new N2ImageUploaderException("Image pixels/size must be below " . implode(", ", $this->imageDimension) . " pixels");
}
}
// create upload directory if it does not exist
$this->uploadDir($this->uploadDir);
$i = '';
$newFileName = $this->imageRename($isNameProvided);
while (file_exists($this->uploadDir . "/" . $newFileName)) {
// The file already uploaded, nothing to do here
if (self::isFilesIdentical($this->uploadDir . "/" . $newFileName, $fileToUpload["tmp_name"])) {
return $this->uploadDir . "/" . $newFileName;
}
$i++;
$newFileName = $this->imageRename($isNameProvided . $i);
}
// Upload the file
$moveUploadedFile = $this->moveUploadedFile($fileToUpload["tmp_name"], $this->uploadDir . "/" . $newFileName);
if ($moveUploadedFile) {
return $this->uploadDir . "/" . $newFileName;
} else {
throw new N2ImageUploaderException(" File could not be uploaded. Unknown error occurred. ");
}
}
public function moveUploadedFile($uploaded_file, $new_file) {
if (!is_uploaded_file($uploaded_file)) {
return copy($uploaded_file, $new_file);
}
return move_uploaded_file($uploaded_file, $new_file);
}
private static function isFilesIdentical($fn1, $fn2) {
if (filetype($fn1) !== filetype($fn2)) return FALSE;
if (filesize($fn1) !== filesize($fn2)) return FALSE;
if (sha1_file($fn1) != sha1_file($fn2)) return false;
return true;
}
}