Completed
Push — master ( c9730b...91790a )
by Henry
13:42 queued 03:44
created

modules/Tinymce/Tinymce.php (1 issue)

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\Tinymce;
3
4
use Redaxscript\Dater;
5
use Redaxscript\Head;
6
use Redaxscript\Header;
7
use Redaxscript\Module;
8
use function chmod;
9
use function current;
10
use function in_array;
11
use function is_dir;
12
use function is_uploaded_file;
13
use function json_encode;
14
use function mkdir;
15
use function move_uploaded_file;
16
use function pathinfo;
17
18
/**
19
 * javascript powered wysiwyg editor
20
 *
21
 * @since 3.0.0
22
 *
23
 * @package Redaxscript
24
 * @category Modules
25
 * @author Henry Ruhs
26
 */
27
28
class Tinymce extends Module\Metadata
29
{
30
	/**
31
	 * array of the module
32
	 *
33
	 * @var array
34
	 */
35
36
	protected static $_moduleArray =
37
	[
38
		'name' => 'Tinymce',
39
		'alias' => 'Tinymce',
40
		'author' => 'Redaxmedia',
41
		'description' => 'JavaScript powered WYSIWYG editor',
42
		'version' => '4.3.0'
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 3.0.0
67
	 */
68
69
	public function renderStart() : void
70
	{
71
		if ($this->_registry->get('loggedIn') === $this->_registry->get('token'))
72
		{
73
			$script = Head\Script::getInstance();
74
			$script
75
				->init('foot')
76
				->appendFile(
77
				[
78
					'https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.9.6/tinymce.min.js',
79
					'modules/Tinymce/assets/scripts/init.js',
80
					'modules/Tinymce/dist/scripts/tinymce.min.js'
81
				]);
82
83
			/* handle upload */
84
85
			if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'tinymce' && $this->_registry->get('thirdParameter') === 'upload' && $this->_registry->get('tokenParameter'))
86
			{
87
				$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...
88
				echo $this->_upload();
89
			}
90
		}
91
	}
92
93
	/**
94
	 * adminNotification
95
	 *
96
	 * @since 3.0.0
97
	 *
98
	 * @return array
99
	 */
100
101
	public function adminNotification() : array
102
	{
103
		if (!mkdir($uploadDirectory = $this->_optionArray['uploadDirectory']) && !is_dir($uploadDirectory))
104
		{
105
			$this->setNotification('error', $this->_language->get('directory_not_found') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
106
		}
107
		else if (!chmod($this->_optionArray['uploadDirectory'], 0777))
108
		{
109
			$this->setNotification('error', $this->_language->get('directory_permission_grant') . $this->_language->get('colon') . ' ' . $this->_optionArray['uploadDirectory'] . $this->_language->get('point'));
110
		}
111
		return $this->getNotificationArray();
112
	}
113
114
	/**
115
	 * upload
116
	 *
117
	 * @since 3.0.0
118
	 *
119
	 * @return string|null
120
	 */
121
122
	protected function _upload() : ?string
123
	{
124
		$dater = new Dater();
125
		$dater->init();
126
		$filesArray = current($this->_request->getFiles());
127
		$fileExtention = pathinfo($filesArray['name'], PATHINFO_EXTENSION);
128
		$path = $this->_optionArray['uploadDirectory'] . DIRECTORY_SEPARATOR . $dater->getDateTime()->getTimestamp() . '.' . $fileExtention;
129
130
		/* handle upload */
131
132
		if (in_array($fileExtention, $this->_optionArray['extension']) && is_uploaded_file($filesArray['tmp_name']) && move_uploaded_file($filesArray['tmp_name'], $path))
133
		{
134
			Header::contentType('application/json');
135
			return json_encode(
136
			[
137
				'location' => $path
138
			]);
139
		}
140
		Header::responseCode(404);
141
		exit;
142
	}
143
}
144