|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers\Ajax; |
|
4
|
|
|
|
|
5
|
|
|
use Core\AjaxController; |
|
6
|
|
|
|
|
7
|
|
|
class ImageUpload extends AjaxController |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string the image upload folder, must be writable |
|
11
|
|
|
*/ |
|
12
|
|
|
private $imageFolder = "uploaded_images/"; |
|
13
|
|
|
private $configFolder = "config_images/"; |
|
14
|
|
|
private $userFolder = "user_images/"; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* check if the image name is valid |
|
18
|
|
|
* @param $image string filename to check |
|
19
|
|
|
* @return bool if image name is valid |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
private function isimageValid($image) |
|
23
|
|
|
{ |
|
24
|
|
|
// Sanitize input |
|
25
|
|
|
if (preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $image)) { |
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// Verify extension |
|
30
|
|
|
if (!in_array(strtolower(pathinfo($image, PATHINFO_EXTENSION)), array("gif", "jpg", "png"))) { |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Upload images from TinyMCE |
|
39
|
|
|
* grabbed from https://www.codexworld.com/tinymce-upload-image-to-server-using-php/ |
|
40
|
|
|
*/ |
|
41
|
|
|
public function tinymceUpload() |
|
42
|
|
|
{ |
|
43
|
|
|
//security checks, only admins can upload images to posts |
|
44
|
|
|
$this->onlyAdmin(); |
|
45
|
|
|
if (!$this->container->getRequest()->isPost()) { |
|
46
|
|
|
throw new \Core\JsonException('Call is not post'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$tempFile = $this->request->getUploadedFiles(); |
|
50
|
|
|
|
|
51
|
|
|
//need to clean up |
|
52
|
|
|
if (is_uploaded_file($tempFile['tmp_name'])) { |
|
53
|
|
|
if (!$this->isimageValid($tempFile['name'])) { |
|
54
|
|
|
header("HTTP/1.1 400 Invalid file name or file extension."); |
|
55
|
|
|
return; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$filetowrite = $this->imageFolder . $tempFile['name']; |
|
59
|
|
|
move_uploaded_file($tempFile['tmp_name'], $filetowrite); |
|
60
|
|
|
|
|
61
|
|
|
// Respond to the successful upload with JSON. |
|
62
|
|
|
echo json_encode(array('location' => $filetowrite)); |
|
63
|
|
|
} else { |
|
64
|
|
|
// Notify editor that the upload failed |
|
65
|
|
|
header("HTTP/1.1 500 Server Error"); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Upload for the file input in the configuration |
|
72
|
|
|
*/ |
|
73
|
|
|
public function fileInputConfigUpload() |
|
74
|
|
|
{ |
|
75
|
|
|
//security checks, only admins can upload images to config |
|
76
|
|
|
$this->onlyAdmin(); |
|
77
|
|
|
if (!$this->container->getRequest()->isPost()) { |
|
78
|
|
|
throw new JsonException('Call is not post'); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
$tempFile = $this->request->getUploadedFiles(); |
|
81
|
|
|
|
|
82
|
|
|
//need to clean up |
|
83
|
|
|
if (is_uploaded_file($tempFile['tmp_name'])) { |
|
84
|
|
|
if (!$this->isimageValid($tempFile['name'])) { |
|
85
|
|
|
echo json_encode(array('error' => 'Invalid name or file extension')); |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$filetowrite = $this->configFolder . $tempFile['name']; |
|
90
|
|
|
move_uploaded_file($tempFile['tmp_name'], $filetowrite); |
|
91
|
|
|
|
|
92
|
|
|
// Respond to the successful upload with JSON. |
|
93
|
|
|
echo json_encode(array('location' => $filetowrite)); |
|
94
|
|
|
} else { |
|
95
|
|
|
// Notify editor that the upload failed |
|
96
|
|
|
echo json_encode(array('error' => 'Upload failed')); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
} |