Completed
Push — master ( 91790a...c455a4 )
by Henry
17:22 queued 07:23
created

ImageUpload::adminNotification()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
namespace Redaxscript\Modules\ImageUpload;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Header;
6
use Redaxscript\Module;
7
use function chmod;
8
use function current;
9
use function in_array;
10
use function is_dir;
11
use function is_uploaded_file;
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
		'extension' =>
55
		[
56
			'gif',
57
			'jpg',
58
			'png',
59
			'svg'
60
		]
61
	];
62
63
	/**
64
	 * renderStart
65
	 *
66
	 * @since 4.3.0
67
	 */
68
69
	public function renderStart() : void
70
	{
71
		if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'image-upload' && $this->_registry->get('tokenParameter'))
72
		{
73
			$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...
74
			echo $this->_upload();
75
		}
76
	}
77
78
	/**
79
	 * adminNotification
80
	 *
81
	 * @since 4.3.0
82
	 *
83
	 * @return array
84
	 */
85
86
	public function adminNotification() : array
87
	{
88
		if (!mkdir($uploadDirectory = $this->_optionArray['uploadDirectory']) && !is_dir($uploadDirectory))
89
		{
90
			$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
91
		}
92
		else if (!chmod($this->_optionArray['uploadDirectory'], 0777))
93
		{
94
			$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
95
		}
96
		return $this->getNotificationArray();
97
	}
98
99
	/**
100
	 * upload
101
	 *
102
	 * @since 4.3.0
103
	 *
104
	 * @return string|null
105
	 */
106
107
	protected function _upload() : ?string
108
	{
109
		$dater = new Dater();
110
		$dater->init();
111
		$filesArray = current($this->_request->getFiles());
112
		$fileExtension = pathinfo($filesArray['name'], PATHINFO_EXTENSION);
113
		$path = $this->_optionArray['uploadDirectory'] . DIRECTORY_SEPARATOR . $dater->getDateTime()->getTimestamp() . '.' . $fileExtension;
114
115
		/* handle upload */
116
117
		if (in_array($fileExtension, $this->_optionArray['extension']) && is_uploaded_file($filesArray['tmp_name']) && move_uploaded_file($filesArray['tmp_name'], $path))
118
		{
119
			Header::contentType('application/json');
120
			return json_encode(
121
			[
122
				'location' => $path
123
			]);
124
		}
125
		Header::responseCode(404);
126
		exit;
127
	}
128
}
129