Completed
Push — master ( 278401...20cce2 )
by Henry
05:52
created

ImageUpload::renderStart()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8337
c 0
b 0
f 0
cc 6
nc 5
nop 0
1
<?php
2
namespace Redaxscript\Modules\ImageUpload;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Filesystem;
6
use Redaxscript\Head;
7
use Redaxscript\Header;
8
use Redaxscript\Module;
9
use function chmod;
10
use function in_array;
11
use function is_dir;
12
use function json_encode;
13
use function mkdir;
14
use function move_uploaded_file;
15
use function pathinfo;
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.3.0',
42
		'access' => '[1]'
43
	];
44
45
	/**
46
	 * array of the option
47
	 *
48
	 * @var array
49
	 */
50
51
	protected $_optionArray =
52
	[
53
		'uploadDirectory' => 'upload',
54
		'mimeTypeArray' =>
55
		[
56
			'image/gif',
57
			'image/jpeg',
58
			'image/png',
59
			'image/svg+xml'
60
		]
61
	];
62
63
	/**
64
	 * renderStart
65
	 *
66
	 * @since 4.3.0
67
	 */
68
69
	public function renderStart() : void
70
	{
71
		/* script */
72
73
		$script = Head\Script::getInstance();
74
		$script
75
			->init('foot')
76
			->appendFile(
77
			[
78
				'modules/ImageUpload/assets/scripts/init.js',
79
				'modules/ImageUpload/dist/scripts/image-upload.min.js'
80
			]);
81
82
		/* list and upload */
83
84
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'image-upload' && $this->_registry->get('tokenParameter'))
85
		{
86
			if ($this->_registry->get('thirdParameter') === 'list')
87
			{
88
				$this->_registry->set('renderBreak', true);
0 ignored issues
show
Documentation introduced by redaxmedia
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...
89
				echo $this->_list();
90
			}
91
			if ($this->_registry->get('thirdParameter') === 'upload')
92
			{
93
				$this->_registry->set('renderBreak', true);
0 ignored issues
show
Documentation introduced by redaxmedia
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...
94
				echo $this->_upload();
95
			}
96
		}
97
	}
98
99
	/**
100
	 * adminNotification
101
	 *
102
	 * @since 4.3.0
103
	 *
104
	 * @return array
105
	 */
106
107
	public function adminNotification() : array
108
	{
109
		if (!mkdir($directory = $this->_optionArray['uploadDirectory']) && !is_dir($directory))
110
		{
111
			$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
112
		}
113
		else if (!chmod($this->_optionArray['uploadDirectory'], 0777))
114
		{
115
			$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
116
		}
117
		return $this->getNotificationArray();
118
	}
119
120
	/**
121
	 * list
122
	 *
123
	 * @since 4.3.0
124
	 *
125
	 * @return string
126
	 */
127
128
	protected function _list() : string
129
	{
130
		$uploadFilesystem = new Filesystem\Filesystem();
131
		$uploadFilesystem->init($this->_optionArray['uploadDirectory']);
132
		$uploadFilesystemArray = $uploadFilesystem->getSortArray();
133
134
		/* handle list */
135
136
		Header::contentType('application/json');
137
		return json_encode($uploadFilesystemArray);
138
	}
139
140
	/**
141
	 * upload
142
	 *
143
	 * @since 4.3.0
144
	 *
145
	 * @return string
146
	 */
147
148
	protected function _upload() : ?string
149
	{
150
		$dater = new Dater();
151
		$dater->init();
152
		$filesArray = $this->_request->getArray()['files'];
153
		$uploadArray = [];
154
155
		/* process files */
156
157
		foreach ($filesArray as $key => $file)
158
		{
159
			$fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
160
			$fileName = $dater->getDateTime()->getTimestamp() . '.' . $key . '.' . $fileExtension;
161
			$filePath = $this->_optionArray['uploadDirectory'] . DIRECTORY_SEPARATOR . $fileName;
162
			if (in_array($file['type'], $this->_optionArray['mimeTypeArray']) && move_uploaded_file($file['tmp_name'], $filePath))
163
			{
164
				$uploadArray[] = $filePath;
165
			}
166
		}
167
168
		/* handle upload */
169
170
		Header::contentType('application/json');
171
		return json_encode($uploadArray);
172
	}
173
}
174