|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Controllers\Ajax; |
|
3
|
|
|
|
|
4
|
|
|
use Core\AjaxController; |
|
5
|
|
|
|
|
6
|
|
|
class ImageUpload extends AjaxController{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var string the image upload folder, must be writable |
|
9
|
|
|
*/ |
|
10
|
|
|
private $imageFolder = "uploaded_images/"; |
|
11
|
|
|
|
|
12
|
|
|
public function tinymceUpload(){ |
|
13
|
|
|
|
|
14
|
|
|
//image uploader for tinymce |
|
15
|
|
|
//grabbed from https://www.codexworld.com/tinymce-upload-image-to-server-using-php/ |
|
16
|
|
|
|
|
17
|
|
|
// Allowed origins to upload images |
|
18
|
|
|
$accepted_origins = array("http://localhost"); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
// Images upload path |
|
21
|
|
|
$imageFolder = "uploaded_images/"; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
$temp = $this->container->getRequest()->getUploadedFiles(); |
|
24
|
|
|
|
|
25
|
|
|
//need to clean up |
|
26
|
|
|
if(is_uploaded_file($temp['tmp_name'])){ |
|
27
|
|
|
/*if(isset($_SERVER['HTTP_ORIGIN'])){ |
|
28
|
|
|
// Same-origin requests won't set an origin. If the origin is set, it must be valid. |
|
29
|
|
|
if(in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)){ |
|
30
|
|
|
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']); |
|
31
|
|
|
}else{ |
|
32
|
|
|
header("HTTP/1.1 403 Origin Denied"); |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
}*/ |
|
36
|
|
|
|
|
37
|
|
|
// Sanitize input |
|
38
|
|
|
if(preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])){ |
|
39
|
|
|
header("HTTP/1.1 400 Invalid file name."); |
|
40
|
|
|
return; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Verify extension |
|
44
|
|
|
if(!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))){ |
|
45
|
|
|
header("HTTP/1.1 400 Invalid extension."); |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Accept upload if there was no origin, or if it is an accepted origin |
|
50
|
|
|
$filetowrite = $this->imageFolder . $temp['name']; |
|
51
|
|
|
move_uploaded_file($temp['tmp_name'], $filetowrite); |
|
52
|
|
|
|
|
53
|
|
|
// Respond to the successful upload with JSON. |
|
54
|
|
|
echo json_encode(array('location' => $filetowrite)); |
|
55
|
|
|
} else { |
|
56
|
|
|
// Notify editor that the upload failed |
|
57
|
|
|
header("HTTP/1.1 500 Server Error"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
} |