Completed
Push — master ( 5f1ca1...a7b5ff )
by Henry
04:31
created

modules/ImageUpload/ImageUpload.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Modules\ImageUpload;
3
4
use Redaxscript\Filesystem;
5
use Redaxscript\Head;
6
use Redaxscript\Header;
7
use Redaxscript\Module;
8
use function chmod;
9
use function in_array;
10
use function is_dir;
11
use function json_encode;
12
use function mkdir;
13
use function move_uploaded_file;
14
use function pathinfo;
15
use function sha1_file;
16
17
/**
18
 * shared module to upload images
19
 *
20
 * @since 4.3.0
21
 *
22
 * @package Redaxscript
23
 * @category Modules
24
 * @author Henry Ruhs
25
 */
26
27
class ImageUpload extends Module\Metadata
28
{
29
	/**
30
	 * array of the module
31
	 *
32
	 * @var array
33
	 */
34
35
	protected static $_moduleArray =
36
	[
37
		'name' => 'Image Upload',
38
		'alias' => 'ImageUpload',
39
		'author' => 'Redaxmedia',
40
		'description' => 'Shared module to upload images',
41
		'version' => '4.5.0',
42
		'license' => 'MIT',
43
		'access' => '[1]'
44
	];
45
46
	/**
47
	 * array of the option
48
	 *
49
	 * @var array
50
	 */
51
52
	protected $_optionArray =
53
	[
54
		'uploadDirectory' => 'upload',
55
		'mimeTypeArray' =>
56
		[
57
			'image/gif',
58
			'image/jpeg',
59
			'image/png',
60
			'image/svg+xml'
61
		]
62
	];
63
64
	/**
65
	 * renderStart
66
	 *
67
	 * @since 4.3.0
68
	 */
69
70
	public function renderStart() : void
71
	{
72
		/* script */
73
74
		$script = Head\Script::getInstance();
75
		$script
76
			->init('foot')
77
			->appendFile(
78
			[
79
				'modules/ImageUpload/assets/scripts/init.js',
80
				'modules/ImageUpload/dist/scripts/image-upload.min.js'
81
			]);
82
83
		/* list and upload */
84
85
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'image-upload' && $this->_registry->get('tokenParameter'))
86
		{
87
			if ($this->_registry->get('thirdParameter') === 'list')
88
			{
89
				$this->_registry->set('renderBreak', true);
0 ignored issues
show
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
				echo $this->_list();
91
			}
92
			if ($this->_registry->get('thirdParameter') === 'upload')
93
			{
94
				$this->_registry->set('renderBreak', true);
0 ignored issues
show
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
95
				echo $this->_upload();
96
			}
97
		}
98
	}
99
100
	/**
101
	 * adminNotification
102
	 *
103
	 * @since 4.3.0
104
	 *
105
	 * @return array
106
	 */
107
108
	public function adminNotification() : array
109
	{
110
		if (!mkdir($directory = $this->_optionArray['uploadDirectory']) && !is_dir($directory))
111
		{
112
			$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
113
		}
114
		else if (!chmod($this->_optionArray['uploadDirectory'], 0777))
115
		{
116
			$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
117
		}
118
		return $this->getNotificationArray();
119
	}
120
121
	/**
122
	 * list
123
	 *
124
	 * @since 4.3.0
125
	 *
126
	 * @return string
127
	 */
128
129
	protected function _list() : string
130
	{
131
		$uploadFilesystem = new Filesystem\Filesystem();
132
		$uploadFilesystem->init($this->_optionArray['uploadDirectory']);
133
		$uploadFilesystemArray = $uploadFilesystem->getSortArray();
134
135
		/* handle list */
136
137
		Header::contentType('application/json');
138
		return json_encode($uploadFilesystemArray);
139
	}
140
141
	/**
142
	 * upload
143
	 *
144
	 * @since 4.3.0
145
	 *
146
	 * @return string
147
	 */
148
149
	protected function _upload() : ?string
150
	{
151
		$filesArray = $this->_request->getArray()['files'];
152
		$uploadArray = [];
153
154
		/* process files */
155
156
		foreach ($filesArray as $key => $file)
157
		{
158
			$fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
159
			$fileName = sha1_file($file['tmp_name']) . '.' . $fileExtension;
160
			$uploadPath = $this->_optionArray['uploadDirectory'] . DIRECTORY_SEPARATOR . $fileName;
161
			if (in_array($file['type'], $this->_optionArray['mimeTypeArray']) && move_uploaded_file($file['tmp_name'], $uploadPath))
162
			{
163
				$uploadArray[] = $uploadPath;
164
			}
165
		}
166
167
		/* handle upload */
168
169
		Header::contentType('application/json');
170
		return json_encode($uploadArray);
171
	}
172
}
173