Passed
Push — Showing-Posts ( 83050b...c031f7 )
by Stone
01:57
created

ImageUpload::fileInputPostUpload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
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/";
0 ignored issues
show
introduced by
The private property $userFolder is not used, and could be removed.
Loading history...
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
     * @param $tempFile array
39
     * @param $folder string
40
     */
41
    private function fileInputUpload(array $tempFile, string $folder)
42
    {
43
        if (is_uploaded_file($tempFile['tmp_name'])) {
44
            if (!$this->isimageValid($tempFile['name'])) {
45
                echo json_encode(array('error' => 'Invalid name or file extension'));
46
                return;
47
            }
48
49
            $filetowrite = $folder . $tempFile['name'];
50
            move_uploaded_file($tempFile['tmp_name'], $filetowrite);
51
52
            // Respond to the successful upload with JSON.
53
            echo json_encode(array('location' => $filetowrite));
54
        } else {
55
            // Notify editor that the upload failed
56
            echo json_encode(array('error' => 'Upload failed'));
57
        }
58
59
    }
60
61
    /**
62
     * Upload images from TinyMCE
63
     * grabbed from https://www.codexworld.com/tinymce-upload-image-to-server-using-php/
64
     */
65
    public function tinymceUpload()
66
    {
67
        //security checks, only admins can upload images to posts
68
        $this->onlyAdmin();
69
        if (!$this->container->getRequest()->isPost()) {
70
            throw new \Core\JsonException('Call is not post');
71
        }
72
73
        $tempFile = $this->request->getUploadedFiles();
74
75
        //need to clean up
76
        if (is_uploaded_file($tempFile['tmp_name'])) {
77
            if (!$this->isimageValid($tempFile['name'])) {
78
                header("HTTP/1.1 400 Invalid file name or file extension.");
79
                return;
80
            }
81
82
            $filetowrite = $this->imageFolder . $tempFile['name'];
83
            move_uploaded_file($tempFile['tmp_name'], $filetowrite);
84
85
            // Respond to the successful upload with JSON.
86
            echo json_encode(array('location' => $filetowrite));
87
        } else {
88
            // Notify editor that the upload failed
89
            header("HTTP/1.1 500 Server Error");
90
        }
91
    }
92
93
94
    /**
95
     * Upload for the file input in the configuration
96
     */
97
    public function fileInputConfigUpload()
98
    {
99
        //security checks, only admins can upload images to config
100
        $this->onlyAdmin();
101
        if (!$this->container->getRequest()->isPost()) {
102
            throw new \Core\JsonException('Call is not post');
103
        }
104
        $tempFile = $this->request->getUploadedFiles();
105
106
        $this->fileInputUpload($tempFile, $this->configFolder);
107
108
    }
109
110
    /**
111
     * Upload for the file input in the configuration
112
     */
113
    public function fileInputPostUpload()
114
    {
115
        //security checks, only admins can upload images to config
116
        $this->onlyAdmin();
117
        if (!$this->container->getRequest()->isPost()) {
118
            throw new \Core\JsonException('Call is not post');
119
        }
120
        $tempFile = $this->request->getUploadedFiles();
121
122
        $this->fileInputUpload($tempFile, $this->imageFolder);
123
    }
124
125
}