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

includes/Admin/Controller/Article.php (1 issue)

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 article request
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 */
19
20
class Article 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
				'description' => $postArray['description'],
60
				'keywords' => $postArray['keywords'],
61
				'robots' => $postArray['robots'],
62
				'text' => $postArray['text'],
63
				'language' => $postArray['language'],
64
				'template' => $postArray['template'],
65
				'sibling' => $postArray['sibling'],
66
				'category' => $postArray['category'],
67
				'headline' => $postArray['headline'],
68
				'byline' => $postArray['byline'],
69
				'comments' => $postArray['comments'],
70
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
71
				'rank' => $postArray['rank'],
72
				'access' => $postArray['access'],
73
				'date' => $postArray['date'] ? : $now
74
			];
75
			if ($this->_create($createArray))
76
			{
77
				return $this->_success(
78
				[
79
					'route' => $this->_getSuccessRoute($postArray),
80
					'timeout' => 2
81
				]);
82
			}
83
		}
84
85
		/* handle update */
86
87
		if ($action === 'update')
88
		{
89
			$updateArray =
90
			[
91
				'title' => $postArray['title'],
92
				'alias' => $postArray['alias'],
93
				'author' => $myUser,
94
				'description' => $postArray['description'],
95
				'keywords' => $postArray['keywords'],
96
				'robots' => $postArray['robots'],
97
				'text' => $postArray['text'],
98
				'language' => $postArray['language'],
99
				'template' => $postArray['template'],
100
				'sibling' => $postArray['sibling'],
101
				'category' => $postArray['category'],
102
				'headline' => $postArray['headline'],
103
				'byline' => $postArray['byline'],
104
				'comments' => $postArray['comments'],
105
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
106
				'rank' => $postArray['rank'],
107
				'access' => $postArray['access'],
108
				'date' => $postArray['date'] ? : $now
109
			];
110
			if ($this->_update($postArray['id'], $updateArray))
111
			{
112
				return $this->_success(
113
				[
114
					'route' => $this->_getSuccessRoute($postArray),
115
					'timeout' => 2
116
				]);
117
			}
118
		}
119
120
		/* handle error */
121
122
		return $this->_error(
123
		[
124
			'route' => $this->_getErrorRoute($postArray)
125
		]);
126
	}
127
128
	/**
129
	 * sanitize the post
130
	 *
131
	 * @since 4.0.0
132
	 *
133
	 * @return array
134
	 */
135
136
	protected function _sanitizePost() : array
137
	{
138
		$aliasFilter = new Filter\Alias();
139
		$htmlFilter = new Filter\Html();
140
		$numberFilter = new Filter\Number();
141
		$specialFilter = new Filter\Special();
142
		$toggleFilter = new Filter\Toggle();
143
144
		/* sanitize post */
145
146
		return
147
		[
148
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
149
			'title' => $this->_request->getPost('title'),
150
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
151
			'description' => $this->_request->getPost('description'),
152
			'keywords' => $this->_request->getPost('keywords'),
153
			'robots' => $this->_request->getPost('robots'),
154
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
0 ignored issues
show
$this->_registry->get('filter') is of type string|array|null, but the function expects a boolean.

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...
155
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
156
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
157
			'sibling' => $this->_request->getPost('sibling'),
158
			'category' => $this->_request->getPost('category'),
159
			'headline' => $toggleFilter->sanitize($this->_request->getPost('headline')),
160
			'byline' => $toggleFilter->sanitize($this->_request->getPost('byline')),
161
			'comments' => $toggleFilter->sanitize($this->_request->getPost('comments')),
162
			'status' => $toggleFilter->sanitize($this->_request->getPost('status')),
163
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
164
			'access' => json_encode($this->_request->getPost('access')),
165
			'date' => strtotime($this->_request->getPost('date'))
166
		];
167
	}
168
169
	/**
170
	 * validate the post
171
	 *
172
	 * @since 4.0.0
173
	 *
174
	 * @param array $postArray array of the post
175
	 *
176
	 * @return array
177
	 */
178
179
	protected function _validatePost(array $postArray = []) : array
180
	{
181
		$aliasValidator = new Validator\Alias();
182
		$articleModel = new Admin\Model\Article();
183
		$validateArray = [];
184
185
		/* validate post */
186
187
		if (!$postArray['title'])
188
		{
189
			$validateArray[] = $this->_language->get('title_empty');
190
		}
191
		if (!$postArray['alias'])
192
		{
193
			$validateArray[] = $this->_language->get('alias_empty');
194
		}
195
		else if (!$aliasValidator->validate($postArray['alias'], 'general') || !$aliasValidator->validate($postArray['alias'], 'system'))
196
		{
197
			$validateArray[] = $this->_language->get('alias_incorrect');
198
		}
199
		else if (!$articleModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
200
		{
201
			$validateArray[] = $this->_language->get('alias_exists');
202
		}
203
		if (!$postArray['text'])
204
		{
205
			$validateArray[] = $this->_language->get('article_empty');
206
		}
207
		return $validateArray;
208
	}
209
210
	/**
211
	 * create the article
212
	 *
213
	 * @since 4.0.0
214
	 *
215
	 * @param array $createArray array of the create
216
	 *
217
	 * @return bool
218
	 */
219
220
	protected function _create(array $createArray = []) : bool
221
	{
222
		$articleModel = new Admin\Model\Article();
223
		return $articleModel->createByArray($createArray);
224
	}
225
226
	/**
227
	 * update the article
228
	 *
229
	 * @since 4.0.0
230
	 *
231
	 * @param int $articleId identifier of the article
232
	 * @param array $updateArray array of the update
233
	 *
234
	 * @return bool
235
	 */
236
237
	protected function _update(int $articleId = null, array $updateArray = []) : bool
238
	{
239
		$articleModel = new Admin\Model\Article();
240
		return $articleModel->updateByIdAndArray($articleId, $updateArray);
241
	}
242
243
	/**
244
	 * get success route
245
	 *
246
	 * @since 4.0.0
247
	 *
248
	 * @param array $postArray array of the post
249
	 *
250
	 * @return string
251
	 */
252
253
	protected function _getSuccessRoute(array $postArray = []) : string
254
	{
255
		if ($this->_registry->get('articlesEdit') && $postArray['id'])
256
		{
257
			return 'admin/view/articles#row-' . $postArray['id'];
258
		}
259
		if ($this->_registry->get('articlesEdit') && $postArray['alias'])
260
		{
261
			$articleModel = new Admin\Model\Article();
262
			return 'admin/view/articles#row-' . $articleModel->getByAlias($postArray['alias'])->id;
263
		}
264
		return 'admin';
265
	}
266
267
	/**
268
	 * get error route
269
	 *
270
	 * @since 4.0.0
271
	 *
272
	 * @param array $postArray array of the post
273
	 *
274
	 * @return string
275
	 */
276
277
	protected function _getErrorRoute(array $postArray = []) : string
278
	{
279
		if ($this->_registry->get('articlesEdit') && $postArray['id'])
280
		{
281
			return 'admin/edit/articles/' . $postArray['id'];
282
		}
283
		if ($this->_registry->get('articlesNew'))
284
		{
285
			return 'admin/new/articles';
286
		}
287
		return 'admin';
288
	}
289
}
290