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

includes/Admin/Controller/Comment.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 function json_encode;
7
use function strtotime;
8
9
/**
10
 * children class to process the admin comment request
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 */
18
19
class Comment extends ControllerAbstract
20
{
21
	/**
22
	 * process the class
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @param string $action action to process
27
	 *
28
	 * @return string
29
	 */
30
31
	public function process(string $action = null) : string
32
	{
33
		$postArray = $this->_normalizePost($this->_sanitizePost());
34
		$validateArray = $this->_validatePost($postArray);
35
		$myUser = $this->_registry->get('myUser');
36
		$myEmail = $this->_registry->get('myEmail');
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
				'author' => $myUser,
57
				'email' => $myEmail,
58
				'url' => $postArray['url'],
59
				'text' => $postArray['text'],
60
				'language' => $postArray['language'],
61
				'article' => $postArray['article'],
62
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
63
				'rank' => $postArray['rank'],
64
				'access' => $postArray['access'],
65
				'date' => $postArray['date'] ? : $now
66
			];
67
			if ($this->_create($createArray))
68
			{
69
				return $this->_success(
70
				[
71
					'route' => $this->_getSuccessRoute($postArray),
72
					'timeout' => 2
73
				]);
74
			}
75
		}
76
77
		/* handle update */
78
79
		if ($action === 'update')
80
		{
81
			$updateArray =
82
			[
83
				'url' => $postArray['url'],
84
				'text' => $postArray['text'],
85
				'language' => $postArray['language'],
86
				'article' => $postArray['article'],
87
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
88
				'rank' => $postArray['rank'],
89
				'access' => $postArray['access'],
90
				'date' => $postArray['date'] ? : $now
91
			];
92
			if ($this->_update($postArray['id'], $updateArray))
93
			{
94
				return $this->_success(
95
				[
96
					'route' => $this->_getSuccessRoute($postArray),
97
					'timeout' => 2
98
				]);
99
			}
100
		}
101
102
		/* handle error */
103
104
		return $this->_error(
105
		[
106
			'route' => $this->_getErrorRoute($postArray)
107
		]);
108
	}
109
110
	/**
111
	 * sanitize the post
112
	 *
113
	 * @since 4.0.0
114
	 *
115
	 * @return array
116
	 */
117
118
	protected function _sanitizePost() : array
119
	{
120
		$htmlFilter = new Filter\Html();
121
		$numberFilter = new Filter\Number();
122
		$specialFilter = new Filter\Special();
123
		$toggleFilter = new Filter\Toggle();
124
		$urlFilter = new Filter\Url();
125
126
		/* sanitize post */
127
128
		return
129
		[
130
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
131
			'url' => $urlFilter->sanitize($this->_request->getPost('url')),
132
			'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...
133
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
134
			'article' => $this->_request->getPost('article'),
135
			'status' => $toggleFilter->sanitize($this->_request->getPost('status')),
136
			'rank' => $numberFilter->sanitize($this->_request->getPost('rank')),
137
			'access' => json_encode($this->_request->getPost('access')),
138
			'date' => strtotime($this->_request->getPost('date'))
139
		];
140
	}
141
142
	/**
143
	 * validate the post
144
	 *
145
	 * @since 4.0.0
146
	 *
147
	 * @param array $postArray array of the post
148
	 *
149
	 * @return array
150
	 */
151
152
	protected function _validatePost(array $postArray = []) : array
153
	{
154
		$validateArray = [];
155
156
		/* validate post */
157
158
		if (!$postArray['text'])
159
		{
160
			$validateArray[] = $this->_language->get('comment_empty');
161
		}
162
		if (!$postArray['article'])
163
		{
164
			$validateArray[] = $this->_language->get('article_empty');
165
		}
166
		return $validateArray;
167
	}
168
169
	/**
170
	 * create the comment
171
	 *
172
	 * @since 4.0.0
173
	 *
174
	 * @param array $createArray array of the create
175
	 *
176
	 * @return bool
177
	 */
178
179
	protected function _create(array $createArray = []) : bool
180
	{
181
		$commentModel = new Admin\Model\Comment();
182
		return $commentModel->createByArray($createArray);
183
	}
184
185
	/**
186
	 * update the comment
187
	 *
188
	 * @since 4.0.0
189
	 *
190
	 * @param int $commentId identifier of the comment
191
	 * @param array $updateArray array of the update
192
	 *
193
	 * @return bool
194
	 */
195
196
	protected function _update(int $commentId = null, array $updateArray = []) : bool
197
	{
198
		$commentModel = new Admin\Model\Comment();
199
		return $commentModel->updateByIdAndArray($commentId, $updateArray);
200
	}
201
202
	/**
203
	 * get success route
204
	 *
205
	 * @since 4.0.0
206
	 *
207
	 * @param array $postArray array of the post
208
	 *
209
	 * @return string
210
	 */
211
212
	protected function _getSuccessRoute(array $postArray = []) : string
213
	{
214
		if ($this->_registry->get('commentsEdit'))
215
		{
216
			if ($postArray['id'])
217
			{
218
				return 'admin/view/comments#row-' . $postArray['id'];
219
			}
220
			return 'admin/view/comments';
221
		}
222
		return 'admin';
223
	}
224
225
	/**
226
	 * get error route
227
	 *
228
	 * @since 4.0.0
229
	 *
230
	 * @param array $postArray array of the post
231
	 *
232
	 * @return string
233
	 */
234
235
	protected function _getErrorRoute(array $postArray = []) : string
236
	{
237
		if ($this->_registry->get('commentsEdit') && $postArray['id'])
238
		{
239
			return 'admin/edit/comments/' . $postArray['id'];
240
		}
241
		if ($this->_registry->get('commentsNew'))
242
		{
243
			return 'admin/new/comments';
244
		}
245
		return 'admin';
246
	}
247
}
248