1 | <?php |
||
2 | |||
3 | /* Divine CMS - Open source CMS for widespread use. |
||
4 | Copyright (c) 2019 Mykola Burakov ([email protected]) |
||
5 | |||
6 | See SOURCE.txt for other and additional information. |
||
7 | |||
8 | This file is part of Divine CMS. |
||
9 | |||
10 | This program is free software: you can redistribute it and/or modify |
||
11 | it under the terms of the GNU General Public License as published by |
||
12 | the Free Software Foundation, either version 3 of the License, or |
||
13 | (at your option) any later version. |
||
14 | |||
15 | This program is distributed in the hope that it will be useful, |
||
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
18 | GNU General Public License for more details. |
||
19 | |||
20 | You should have received a copy of the GNU General Public License |
||
21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
||
22 | |||
23 | class ControllerToolUpload extends \Divine\Engine\Core\Controller |
||
0 ignored issues
–
show
|
|||
24 | { |
||
25 | public function index() |
||
0 ignored issues
–
show
|
|||
26 | { |
||
27 | $this->load->language('tool/upload'); |
||
28 | |||
29 | $json = array(); |
||
30 | |||
31 | if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) { |
||
32 | // Sanitize the filename |
||
33 | $filename = basename(preg_replace('/[^a-zA-Z0-9\.\-\s+]/', '', html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'))); |
||
34 | |||
35 | // Validate the filename length |
||
36 | if ((\voku\helper\UTF8::strlen($filename) < 3) || (\voku\helper\UTF8::strlen($filename) > 64)) { |
||
37 | $json['error'] = $this->language->get('error_filename'); |
||
38 | } |
||
39 | |||
40 | // Allowed file extension types |
||
41 | $allowed = array(); |
||
42 | |||
43 | $extension_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_ext_allowed')); |
||
44 | |||
45 | $filetypes = explode("\n", $extension_allowed); |
||
46 | |||
47 | foreach ($filetypes as $filetype) { |
||
48 | $allowed[] = trim($filetype); |
||
49 | } |
||
50 | |||
51 | if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) { |
||
52 | $json['error'] = $this->language->get('error_filetype'); |
||
53 | } |
||
54 | |||
55 | // Allowed file mime types |
||
56 | $allowed = array(); |
||
57 | |||
58 | $mime_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('config_file_mime_allowed')); |
||
59 | |||
60 | $filetypes = explode("\n", $mime_allowed); |
||
61 | |||
62 | foreach ($filetypes as $filetype) { |
||
63 | $allowed[] = trim($filetype); |
||
64 | } |
||
65 | |||
66 | if (!in_array($this->request->files['file']['type'], $allowed)) { |
||
67 | $json['error'] = $this->language->get('error_filetype'); |
||
68 | } |
||
69 | |||
70 | // Check to see if any PHP files are trying to be uploaded |
||
71 | $content = file_get_contents($this->request->files['file']['tmp_name']); |
||
72 | |||
73 | if (preg_match('/\<\?php/i', $content)) { |
||
74 | $json['error'] = $this->language->get('error_filetype'); |
||
75 | } |
||
76 | |||
77 | // Return any upload error |
||
78 | if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) { |
||
79 | $json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']); |
||
80 | } |
||
81 | } else { |
||
82 | $json['error'] = $this->language->get('error_upload'); |
||
83 | } |
||
84 | |||
85 | if (!$json) { |
||
86 | $file = $filename . '.' . (new \Tokenly\TokenGenerator\TokenGenerator())->generateToken(32, 'SR'); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
87 | |||
88 | move_uploaded_file($this->request->files['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/storage/upload/' . $file); |
||
89 | |||
90 | // Hide the uploaded file name so people can not link to it directly. |
||
91 | $this->load->model('tool/upload'); |
||
92 | |||
93 | $json['code'] = $this->model_tool_upload->addUpload($filename, $file); |
||
94 | |||
95 | $json['success'] = $this->language->get('text_upload'); |
||
96 | } |
||
97 | |||
98 | $this->response->addHeader('Content-Type: application/json'); |
||
99 | $this->response->setOutput(json_encode($json)); |
||
100 | } |
||
101 | } |
||
102 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.