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):bool |
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
|
|
|
* Check if file exists and add a number to avoid overwrite |
39
|
|
|
* @param string $folder destination folder |
40
|
|
|
* @param string $file destination filename |
41
|
|
|
* @return string the unique file name |
42
|
|
|
*/ |
43
|
|
|
private function getFilename(string $folder, string $file):string |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
$fileUrl = $folder . $file; |
47
|
|
|
$filePath = $_SERVER['DOCUMENT_ROOT']."/public/".$fileUrl; |
48
|
|
|
if(file_exists($filePath) !== 1) |
49
|
|
|
{ |
50
|
|
|
$fileNum = 0; |
51
|
|
|
while(file_exists($filePath)) |
52
|
|
|
{ |
53
|
|
|
$fileUrl = $folder.$fileNum."_".$file; |
54
|
|
|
$filePath = $_SERVER['DOCUMENT_ROOT']."/public/".$fileUrl; |
55
|
|
|
$fileNum += 1; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $fileUrl; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $tempFile array |
63
|
|
|
* @param $folder string |
64
|
|
|
*/ |
65
|
|
|
private function fileInputUpload(array $tempFile, string $folder) |
66
|
|
|
{ |
67
|
|
|
if (is_uploaded_file($tempFile['tmp_name'])) { |
68
|
|
|
if (!$this->isImageValid($tempFile['name'])) { |
69
|
|
|
echo json_encode(array('error' => 'Invalid name or file extension')); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$filetowrite = $this->getFilename($folder, $tempFile['name']); |
74
|
|
|
move_uploaded_file($tempFile['tmp_name'], $filetowrite); |
75
|
|
|
|
76
|
|
|
// Respond to the successful upload with JSON. |
77
|
|
|
echo json_encode(array('location' => $filetowrite)); |
78
|
|
|
} else { |
79
|
|
|
// Notify editor that the upload failed |
80
|
|
|
echo json_encode(array('error' => 'Upload failed')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Upload images from TinyMCE |
87
|
|
|
* grabbed from https://www.codexworld.com/tinymce-upload-image-to-server-using-php/ |
88
|
|
|
*/ |
89
|
|
|
public function tinymceUpload() |
90
|
|
|
{ |
91
|
|
|
//security checks, only admins can upload images to posts |
92
|
|
|
$this->onlyAdmin(); |
93
|
|
|
if (!$this->container->getRequest()->isPost()) { |
94
|
|
|
throw new \Core\JsonException('Call is not post'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$tempFile = $this->request->getUploadedFiles(); |
98
|
|
|
|
99
|
|
|
//need to clean up |
100
|
|
|
if (is_uploaded_file($tempFile['tmp_name'])) { |
101
|
|
|
if (!$this->isImageValid($tempFile['name'])) { |
102
|
|
|
header("HTTP/1.1 400 Invalid file name or file extension."); |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$filetowrite = $this->getFilename($this->imageFolder, $tempFile['name']); |
107
|
|
|
move_uploaded_file($tempFile['tmp_name'], $filetowrite); |
108
|
|
|
|
109
|
|
|
// Respond to the successful upload with JSON. |
110
|
|
|
echo json_encode(array('location' => $filetowrite)); |
111
|
|
|
} else { |
112
|
|
|
// Notify editor that the upload failed |
113
|
|
|
header("HTTP/1.1 500 Server Error"); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Upload for the file input in the configuration |
120
|
|
|
*/ |
121
|
|
|
public function fileInputConfigUpload() |
122
|
|
|
{ |
123
|
|
|
//security checks, only admins can upload images to config |
124
|
|
|
$this->onlyAdmin(); |
125
|
|
|
if (!$this->container->getRequest()->isPost()) { |
126
|
|
|
throw new \Core\JsonException('Call is not post'); |
127
|
|
|
} |
128
|
|
|
$tempFile = $this->request->getUploadedFiles(); |
129
|
|
|
|
130
|
|
|
$this->fileInputUpload($tempFile, $this->configFolder); |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Upload for the file input in the configuration |
136
|
|
|
*/ |
137
|
|
|
public function fileInputPostUpload() |
138
|
|
|
{ |
139
|
|
|
//security checks, only admins can upload images to config |
140
|
|
|
$this->onlyAdmin(); |
141
|
|
|
if (!$this->container->getRequest()->isPost()) { |
142
|
|
|
throw new \Core\JsonException('Call is not post'); |
143
|
|
|
} |
144
|
|
|
$tempFile = $this->request->getUploadedFiles(); |
145
|
|
|
|
146
|
|
|
$this->fileInputUpload($tempFile, $this->imageFolder); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |