Passed
Push — Showing-Posts ( 555668...fc2dfc )
by Stone
02:28
created

ImageUpload   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B tinymceUpload() 0 46 6
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/";
0 ignored issues
show
introduced by
The private property $imageFolder is not used, and could be removed.
Loading history...
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()->getUploadeFiles();
0 ignored issues
show
Bug introduced by
The method getUploadeFiles() does not exist on Core\Dependency\Request. Did you maybe mean getUploadedFiles()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        $temp = $this->container->getRequest()->/** @scrutinizer ignore-call */ getUploadeFiles();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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 = $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
}