Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

includes/Admin/Controller/Extra.php (2 issues)

Labels
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\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Filter;
6
use Redaxscript\Validator;
7
use function json_encode;
8
use function strtotime;
9
10
/**
11
 * children class to process the admin extra request
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 */
19
20
class Extra extends ControllerAbstract
21
{
22
	/**
23
	 * process the class
24
	 *
25
	 * @since 4.0.0
26
	 *
27
	 * @param string $action action to process
28
	 *
29
	 * @return string
30
	 */
31
32
	public function process(string $action = null) : string
33
	{
34
		$postArray = $this->_normalizePost($this->_sanitizePost());
35
		$validateArray = $this->_validatePost($postArray);
36
		$myUser = $this->_registry->get('myUser');
37
		$now = $this->_registry->get('now');
38
39
		/* validate post */
40
41
		if ($validateArray)
42
		{
43
			return $this->_error(
44
			[
45
				'route' => $this->_getErrorRoute($postArray),
46
				'message' => $validateArray
47
			]);
48
		}
49
50
		/* handle create */
51
52
		if ($action === 'create')
53
		{
54
			$createArray =
55
			[
56
				'title' => $postArray['title'],
57
				'alias' => $postArray['alias'],
58
				'author' => $myUser,
59
				'text' => $postArray['text'],
60
				'language' => $postArray['language'],
61
				'sibling' => $postArray['sibling'],
62
				'category' => $postArray['category'],
63
				'article' => $postArray['article'],
64
				'headline' => $postArray['headline'],
65
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
66
				'rank' => $postArray['rank'],
67
				'access' => $postArray['access'],
68
				'date' => $postArray['date'] ? : $now
69
			];
70
			if ($this->_create($createArray))
71
			{
72
				return $this->_success(
73
				[
74
					'route' => $this->_getSuccessRoute($postArray),
75
					'timeout' => 2
76
				]);
77
			}
78
		}
79
80
		/* handle update */
81
82
		if ($action === 'update')
83
		{
84
			$updateArray =
85
			[
86
				'title' => $postArray['title'],
87
				'alias' => $postArray['alias'],
88
				'author' => $myUser,
89
				'text' => $postArray['text'],
90
				'language' => $postArray['language'],
91
				'sibling' => $postArray['sibling'],
92
				'category' => $postArray['category'],
93
				'article' => $postArray['article'],
94
				'headline' => $postArray['headline'],
95
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
96
				'rank' => $postArray['rank'],
97
				'access' => $postArray['access'],
98
				'date' => $postArray['date'] ? : $now
99
			];
100
			if ($this->_update($postArray['id'], $updateArray))
101
			{
102
				return $this->_success(
103
				[
104
					'route' => $this->_getSuccessRoute($postArray),
105
					'timeout' => 2
106
				]);
107
			}
108
		}
109
110
		/* handle error */
111
112
		return $this->_error(
113
		[
114
			'route' => $this->_getErrorRoute($postArray)
115
		]);
116
	}
117
118
	/**
119
	 * sanitize the post
120
	 *
121
	 * @since 4.0.0
122
	 *
123
	 * @return array
124
	 */
125
126
	protected function _sanitizePost() : array
127
	{
128
		$aliasFilter = new Filter\Alias();
129
		$htmlFilter = new Filter\Html();
130
		$numberFilter = new Filter\Number();
131
		$specialFilter = new Filter\Special();
132
		$toggleFilter = new Filter\Toggle();
133
134
		/* sanitize post */
135
136
		return
137
		[
138
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
139
			'title' => $this->_request->getPost('title'),
140
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
141
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
142
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
143
			'sibling' => $this->_request->getPost('sibling'),
144
			'category' => $this->_request->getPost('category'),
145
			'article' => $this->_request->getPost('article'),
146
			'headline' => $toggleFilter->sanitize($this->_request->getPost('headline')),
0 ignored issues
show
It seems like $this->_request->getPost('headline') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
147
			'status' => $toggleFilter->sanitize($this->_request->getPost('status')),
0 ignored issues
show
It seems like $this->_request->getPost('status') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
148
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
149
			'access' => json_encode($this->_request->getPost('access')),
150
			'date' => strtotime($this->_request->getPost('date'))
151
		];
152
	}
153
154
	/**
155
	 * validate the post
156
	 *
157
	 * @since 4.0.0
158
	 *
159
	 * @param array $postArray array of the post
160
	 *
161
	 * @return array
162
	 */
163
164
	protected function _validatePost(array $postArray = []) : array
165
	{
166
		$aliasValidator = new Validator\Alias();
167
		$extraModel = new Admin\Model\Extra();
168
		$validateArray = [];
169
170
		/* validate post */
171
172
		if (!$postArray['title'])
173
		{
174
			$validateArray[] = $this->_language->get('title_empty');
175
		}
176
		if (!$postArray['alias'])
177
		{
178
			$validateArray[] = $this->_language->get('alias_empty');
179
		}
180
		else if (!$aliasValidator->validate($postArray['alias'], 'general'))
181
		{
182
			$validateArray[] = $this->_language->get('alias_incorrect');
183
		}
184
		else if (!$extraModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
185
		{
186
			$validateArray[] = $this->_language->get('alias_exists');
187
		}
188
		if (!$postArray['text'])
189
		{
190
			$validateArray[] = $this->_language->get('extra_empty');
191
		}
192
		return $validateArray;
193
	}
194
195
	/**
196
	 * create the extra
197
	 *
198
	 * @since 4.0.0
199
	 *
200
	 * @param array $createArray array of the create
201
	 *
202
	 * @return bool
203
	 */
204
205
	protected function _create(array $createArray = []) : bool
206
	{
207
		$extraModel = new Admin\Model\Extra();
208
		return $extraModel->createByArray($createArray);
209
	}
210
211
	/**
212
	 * update the extra
213
	 *
214
	 * @since 4.0.0
215
	 *
216
	 * @param int $extraId identifier of the extra
217
	 * @param array $updateArray array of the update
218
	 *
219
	 * @return bool
220
	 */
221
222
	protected function _update(int $extraId = null, array $updateArray = []) : bool
223
	{
224
		$extraModel = new Admin\Model\Extra();
225
		return $extraModel->updateByIdAndArray($extraId, $updateArray);
226
	}
227
228
	/**
229
	 * get success route
230
	 *
231
	 * @since 4.0.0
232
	 *
233
	 * @param array $postArray array of the post
234
	 *
235
	 * @return string
236
	 */
237
238
	protected function _getSuccessRoute(array $postArray = []) : string
239
	{
240
		if ($this->_registry->get('extrasEdit') && $postArray['id'])
241
		{
242
			return 'admin/view/extras#row-' . $postArray['id'];
243
		}
244
		if ($this->_registry->get('extrasEdit') && $postArray['alias'])
245
		{
246
			$extraModel = new Admin\Model\Extra();
247
			return 'admin/view/extras#row-' . $extraModel->getByAlias($postArray['alias'])->id;
248
		}
249
		return 'admin';
250
	}
251
252
	/**
253
	 * get error route
254
	 *
255
	 * @since 4.0.0
256
	 *
257
	 * @param array $postArray array of the post
258
	 *
259
	 * @return string
260
	 */
261
262
	protected function _getErrorRoute(array $postArray = []) : string
263
	{
264
		if ($this->_registry->get('extrasEdit') && $postArray['id'])
265
		{
266
			return 'admin/edit/extras/' . $postArray['id'];
267
		}
268
		if ($this->_registry->get('extrasNew'))
269
		{
270
			return 'admin/new/extras';
271
		}
272
		return 'admin';
273
	}
274
}
275