Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

includes/Admin/Router/Router.php (2 issues)

mismatching argument types.

Documentation Minor

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\Router;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Header;
6
use Redaxscript\Module;
7
use Redaxscript\Router\RouterAbstract;
8
9
/**
10
 * parent class to provide the admin router
11
 *
12
 * @since 3.3.0
13
 *
14
 * @package Redaxscript
15
 * @category Router
16
 * @author Henry Ruhs
17
 */
18
19
class Router extends RouterAbstract
20
{
21
	/**
22
	 * route the header
23
	 *
24
	 * @since 3.3.0
25
	 *
26
	 * @return bool
27
	 */
28
29 2
	public function routeHeader() : bool
30
	{
31 2
		Module\Hook::trigger('adminRouteHeader');
32 2
		$adminParameter = $this->getAdmin();
33
34
		/* handle break */
35
36 2
		if ($this->_registry->get('adminRouterBreak'))
37
		{
38 1
			Header::responseCode(202);
39
		}
40
41
		/* handle guard */
42
43 2
		if ($adminParameter && ($this->_tokenGuard() || $this->_authGuard()))
44
		{
45
			Header::responseCode(403);
46
		}
47 2
		return (bool)$this->_registry->get('adminRouterBreak');
48
	}
49
50
	/**
51
	 * route the content
52
	 *
53
	 * @since 3.3.0
54
	 *
55
	 * @return string|null
56
	 */
57
58 10
	public function routeContent() : ?string
59
	{
60 10
		Module\Hook::trigger('adminRouteContent');
61 10
		$firstParameter = $this->getFirst();
62 10
		$adminParameter = $this->getAdmin();
63 10
		$tableParameter = $this->getTable();
64
65
		/* handle admin */
66
67 10
		if ($firstParameter === 'admin')
68
		{
69
			/* handle guard */
70
71 9
			if ($adminParameter)
72
			{
73 9
				if ($this->_tokenGuard())
74
				{
75 1
					return $this->_errorToken();
76
				}
77 8
				if ($this->_authGuard())
78
				{
79
					return $this->_errorAccess();
80
				}
81
			}
82
83
			/* handle update */
84
85 8
			if (!$adminParameter || $adminParameter == 'view' && $tableParameter == 'users' || $this->_registry->get('cronUpdate'))
86
			{
87
				$this->_updateLast();
88
			}
89
90
			/* handle post */
91
92 8
			if ($this->_request->getPost('Redaxscript\Admin\View\CategoryForm'))
93
			{
94 1
				return $this->_processCategory();
95
			}
96 7
			if ($this->_request->getPost('Redaxscript\Admin\View\ArticleForm'))
97
			{
98 1
				return $this->_processArticle();
99
			}
100 6
			if ($this->_request->getPost('Redaxscript\Admin\View\ExtraForm'))
101
			{
102 1
				return $this->_processExtra();
103
			}
104 5
			if ($this->_request->getPost('Redaxscript\Admin\View\CommentForm'))
105
			{
106 1
				return $this->_processComment();
107
			}
108 4
			if ($this->_request->getPost('Redaxscript\Admin\View\UserForm'))
109
			{
110 1
				return $this->_processUser();
111
			}
112 3
			if ($this->_request->getPost('Redaxscript\Admin\View\GroupForm'))
113
			{
114 1
				return $this->_processGroup();
115
			}
116 2
			if ($this->_request->getPost('Redaxscript\Admin\View\ModuleForm'))
117
			{
118 1
				return $this->_processModule();
119
			}
120 1
			if ($this->_request->getPost('Redaxscript\Admin\View\SettingForm'))
121
			{
122 1
				return $this->_processSetting();
123
			}
124
125
			/* handle route */
126
127
			if ($adminParameter === 'view')
128
			{
129
				return $this->_renderView();
130
			}
131
			if ($adminParameter === 'new')
132
			{
133
				return $this->_renderNew();
134
			}
135
			if ($adminParameter === 'edit')
136
			{
137
				return $this->_renderEdit();
138
			}
139
			return $this->_processCommon();
140
		}
141 1
		if ($this->_registry->get('adminRouterBreak'))
142
		{
143
			return '<!-- adminRouterBreak -->';
144
		}
145 1
		return null;
146
	}
147
148
	/**
149
	 * token guard
150
	 *
151
	 * @since 3.3.0
152
	 *
153
	 * @return bool
154
	 */
155
156 9
	protected function _tokenGuard() : bool
157
	{
158 9
		$adminParameter = $this->getAdmin();
159 9
		$tokenParameter = $this->getToken();
160
		$tokenArray =
161
		[
162 9
			'enable',
163
			'disable',
164
			'publish',
165
			'unpublish',
166
			'install',
167
			'uninstall',
168
			'delete'
169
		];
170 9
		return $this->_request->getPost() && $this->_request->getPost('token') !== $this->_registry->get('token') || in_array($adminParameter, $tokenArray) && !$tokenParameter;
171
	}
172
173
	/**
174
	 * auth guard
175
	 *
176
	 * @since 3.3.0
177
	 *
178
	 * @return bool
179
	 */
180
181 8
	protected function _authGuard() : bool
182
	{
183 8
		$adminParameter = $this->getAdmin();
184 8
		$tableParameter = $this->getTable();
185 8
		$idParameter = $this->getId();
186
		$editArray =
187
		[
188 8
			'edit',
189
			'view',
190
			'enable',
191
			'disable',
192
			'publish',
193
			'unpublish'
194
		];
195 8
		$permissionNew = $adminParameter === 'new' && $this->_registry->get('tableNew');
196 8
		$permissionEdit = in_array($adminParameter, $editArray) && $this->_registry->get('tableEdit');
197 8
		$permissionDelete = $adminParameter === 'delete' && $this->_registry->get('tableDelete');
198 8
		$permissionInstall = $adminParameter === 'install' && $this->_registry->get('tableInstall');
199 8
		$permissionUninstall = $adminParameter === 'uninstall' && $this->_registry->get('tableUninstall');
200 8
		$permissionProfile = $tableParameter === 'users' && $idParameter === $this->_registry->get('myId');
201 8
		return !$permissionNew && !$permissionEdit && !$permissionDelete && !$permissionInstall && !$permissionUninstall && !$permissionProfile;
202
	}
203
204
	/**
205
	 * update last
206
	 *
207
	 * @since 4.00
208
	 */
209
210
	protected function _updateLast()
211
	{
212
		$userModel = new Admin\Model\User();
213
		if ($this->_registry->get('myId'))
214
		{
215
			$userModel->updateLastById($this->_registry->get('myId'), $this->_registry->get('now'));
0 ignored issues
show
$this->_registry->get('myId') is of type string|array, but the function expects a null|integer.

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...
$this->_registry->get('now') is of type string|array, but the function expects a null|integer.

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...
216
		}
217
	}
218
219
	/**
220
	 * process the category
221
	 *
222
	 * @since 4.00
223
	 *
224
	 * @return string
225
	 */
226
227 1
	protected function _processCategory() : string
228
	{
229 1
		$categoryController = new Admin\Controller\Category($this->_registry, $this->_request, $this->_language, $this->_config);
230 1
		return $categoryController->process($this->_request->getPost('Redaxscript\Admin\View\CategoryForm'));
231
	}
232
233
	/**
234
	 * process the article
235
	 *
236
	 * @since 4.00
237
	 *
238
	 * @return string
239
	 */
240
241 1
	protected function _processArticle() : string
242
	{
243 1
		$articleController = new Admin\Controller\Article($this->_registry, $this->_request, $this->_language, $this->_config);
244 1
		return $articleController->process($this->_request->getPost('Redaxscript\Admin\View\ArticleForm'));
245
	}
246
247
	/**
248
	 * process the extra
249
	 *
250
	 * @since 4.00
251
	 *
252
	 * @return string
253
	 */
254
255 1
	protected function _processExtra() : string
256
	{
257 1
		$extraController = new Admin\Controller\Extra($this->_registry, $this->_request, $this->_language, $this->_config);
258 1
		return $extraController->process($this->_request->getPost('Redaxscript\Admin\View\ExtraForm'));
259
	}
260
261
	/**
262
	 * process the comment
263
	 *
264
	 * @since 4.00
265
	 *
266
	 * @return string
267
	 */
268
269 1
	protected function _processComment() : string
270
	{
271 1
		$commentController = new Admin\Controller\Comment($this->_registry, $this->_request, $this->_language, $this->_config);
272 1
		return $commentController->process($this->_request->getPost('Redaxscript\Admin\View\CommentForm'));
273
	}
274
275
	/**
276
	 * process the user
277
	 *
278
	 * @since 4.00
279
	 *
280
	 * @return string
281
	 */
282
283 1
	protected function _processUser() : string
284
	{
285 1
		$userController = new Admin\Controller\User($this->_registry, $this->_request, $this->_language, $this->_config);
286 1
		return $userController->process($this->_request->getPost('Redaxscript\Admin\View\UserForm'));
287
	}
288
289
	/**
290
	 * process the group
291
	 *
292
	 * @since 4.00
293
	 *
294
	 * @return string
295
	 */
296
297 1
	protected function _processGroup() : string
298
	{
299 1
		$groupController = new Admin\Controller\Group($this->_registry, $this->_request, $this->_language, $this->_config);
300 1
		return $groupController->process($this->_request->getPost('Redaxscript\Admin\View\GroupForm'));
301
	}
302
303
	/**
304
	 * process the module
305
	 *
306
	 * @since 4.00
307
	 *
308
	 * @return string
309
	 */
310
311 1
	protected function _processModule() : string
312
	{
313 1
		$moduleController = new Admin\Controller\Module($this->_registry, $this->_request, $this->_language, $this->_config);
314 1
		return $moduleController->process($this->_request->getPost('Redaxscript\Admin\View\ModuleForm'));
315
	}
316
317
	/**
318
	 * process the setting
319
	 *
320
	 * @since 4.00
321
	 *
322
	 * @return string
323
	 */
324
325 1
	protected function _processSetting() : string
326
	{
327 1
		$settingController = new Admin\Controller\Setting($this->_registry, $this->_request, $this->_language, $this->_config);
328 1
		return $settingController->process($this->_request->getPost('Redaxscript\Admin\View\SettingForm'));
329
	}
330
331
	/**
332
	 * process the common
333
	 *
334
	 * @since 4.00
335
	 *
336
	 * @return string|null
337
	 */
338
339
	protected function _processCommon() : ?string
340
	{
341
		$adminParameter = $this->getAdmin();
342
		$commonArray =
343
		[
344
			'enable',
345
			'disable',
346
			'publish',
347
			'unpublish',
348
			'install',
349
			'uninstall',
350
			'delete'
351
		];
352
		if (in_array($adminParameter, $commonArray))
353
		{
354
			$commonController = new Admin\Controller\Common($this->_registry, $this->_request, $this->_language, $this->_config);
355
			return $commonController->process($adminParameter);
356
		}
357
		return null;
358
	}
359
360
	/**
361
	 * render the view
362
	 *
363
	 * @since 3.3.0
364
	 *
365
	 * @return string|null
366
	 */
367
368
	protected function _renderView() : ?string
369
	{
370
		$tableParameter = $this->getTable();
371
372
		/* handle table */
373
374
		if ($tableParameter == 'categories')
375
		{
376
			$categoryTable = new Admin\View\CategoryTable($this->_registry, $this->_language);
377
			return $categoryTable->render();
378
		}
379
		if ($tableParameter == 'articles')
380
		{
381
			$articleTable = new Admin\View\ArticleTable($this->_registry, $this->_language);
382
			return $articleTable->render();
383
		}
384
		if ($tableParameter == 'extras')
385
		{
386
			$extraTable = new Admin\View\ExtraTable($this->_registry, $this->_language);
387
			return $extraTable->render();
388
		}
389
		if ($tableParameter == 'comments')
390
		{
391
			$commentTable = new Admin\View\CommentTable($this->_registry, $this->_language);
392
			return $commentTable->render();
393
		}
394
		if ($tableParameter == 'users')
395
		{
396
			$userTable = new Admin\View\UserTable($this->_registry, $this->_language);
397
			return $userTable->render();
398
		}
399
		if ($tableParameter == 'groups')
400
		{
401
			$groupTable = new Admin\View\GroupTable($this->_registry, $this->_language);
402
			return $groupTable->render();
403
		}
404
		if ($tableParameter == 'modules')
405
		{
406
			$moduleTable = new Admin\View\ModuleTable($this->_registry, $this->_language);
407
			return $moduleTable->render();
408
		}
409
		return null;
410
	}
411
412
	/**
413
	 * render the new
414
	 *
415
	 * @since 3.3.0
416
	 *
417
	 * @return string|null
418
	 */
419
420
	protected function _renderNew() : ?string
421
	{
422
		$tableParameter = $this->getTable();
423
424
		/* handle table */
425
426
		if ($tableParameter == 'categories')
427
		{
428
			$categoryForm = new Admin\View\CategoryForm($this->_registry, $this->_language);
429
			return $categoryForm->render();
430
		}
431
		if ($tableParameter == 'articles')
432
		{
433
			$articleForm = new Admin\View\ArticleForm($this->_registry, $this->_language);
434
			return $articleForm->render();
435
		}
436
		if ($tableParameter == 'extras')
437
		{
438
			$extraForm = new Admin\View\ExtraForm($this->_registry, $this->_language);
439
			return $extraForm->render();
440
		}
441
		if ($tableParameter == 'comments')
442
		{
443
			$commentForm = new Admin\View\CommentForm($this->_registry, $this->_language);
444
			return $commentForm->render();
445
		}
446
		if ($tableParameter == 'users')
447
		{
448
			$userForm = new Admin\View\UserForm($this->_registry, $this->_language);
449
			return $userForm->render();
450
		}
451
		if ($tableParameter == 'groups')
452
		{
453
			$groupForm = new Admin\View\GroupForm($this->_registry, $this->_language);
454
			return $groupForm->render();
455
		}
456
		return null;
457
	}
458
459
	/**
460
	 * render the edit
461
	 *
462
	 * @since 3.3.0
463
	 *
464
	 * @return string|null
465
	 */
466
467
	protected function _renderEdit() : ?string
468
	{
469
		$tableParameter = $this->getTable();
470
		$idParameter = $this->getId();
471
472
		/* handle table */
473
474
		if ($tableParameter == 'categories' && $idParameter)
475
		{
476
			$categoryForm = new Admin\View\CategoryForm($this->_registry, $this->_language);
477
			return $categoryForm->render($idParameter);
478
		}
479
		if ($tableParameter == 'articles' && $idParameter)
480
		{
481
			$articleForm = new Admin\View\ArticleForm($this->_registry, $this->_language);
482
			return $articleForm->render($idParameter);
483
		}
484
		if ($tableParameter == 'extras' && $idParameter)
485
		{
486
			$extraForm = new Admin\View\ExtraForm($this->_registry, $this->_language);
487
			return $extraForm->render($idParameter);
488
		}
489
		if ($tableParameter == 'comments' && $idParameter)
490
		{
491
			$commentForm = new Admin\View\CommentForm($this->_registry, $this->_language);
492
			return $commentForm->render($idParameter);
493
		}
494
		if ($tableParameter == 'users' && $idParameter)
495
		{
496
			$userForm = new Admin\View\UserForm($this->_registry, $this->_language);
497
			return $userForm->render($idParameter);
498
		}
499
		if ($tableParameter == 'groups' && $idParameter)
500
		{
501
			$groupForm = new Admin\View\GroupForm($this->_registry, $this->_language);
502
			return $groupForm->render($idParameter);
503
		}
504
		if ($tableParameter == 'modules' && $idParameter)
505
		{
506
			$moduleForm = new Admin\View\ModuleForm($this->_registry, $this->_language);
507
			return $moduleForm->render($idParameter);
508
		}
509
		if ($tableParameter == 'settings')
510
		{
511
			$settingForm = new Admin\View\SettingForm($this->_registry, $this->_language);
512
			return $settingForm->render();
513
		}
514
		return null;
515
	}
516
517
	/**
518
	 * messenger factory
519
	 *
520
	 * @since 4.0.0
521
	 *
522
	 * @return Admin\Messenger
523
	 */
524
525 1
	protected function _messengerFactory() : Admin\Messenger
526
	{
527 1
		return new Admin\Messenger($this->_registry);
528
	}
529
530
	/**
531
	 * show the token error
532
	 *
533
	 * @since 3.3.0
534
	 *
535
	 * @return string
536
	 */
537
538 1
	protected function _errorToken() : string
539
	{
540 1
		$messenger = $this->_messengerFactory();
541
		return $messenger
542 1
			->setRoute($this->_language->get('back'), 'admin')
543 1
			->error($this->_language->get('token_incorrect'), $this->_language->get('error_occurred'));
544
	}
545
546
	/**
547
	 * show the access error
548
	 *
549
	 * @since 3.3.0
550
	 *
551
	 * @return string
552
	 */
553
554
	protected function _errorAccess() : string
555
	{
556
		$messenger = $this->_messengerFactory();
557
		return $messenger
558
			->setRoute($this->_language->get('back'), 'admin')
559
			->error($this->_language->get('access_no'), $this->_language->get('error_occurred'));
560
	}
561
}
562