Router::_processLogin()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript\Router;
3
4
use Redaxscript\Controller;
5
use Redaxscript\Filter;
6
use Redaxscript\Header;
7
use Redaxscript\Model;
8
use Redaxscript\Module;
9
use Redaxscript\Validator;
10
use Redaxscript\View;
11
12
/**
13
 * parent class to provide the router
14
 *
15
 * @since 3.3.0
16
 *
17
 * @package Redaxscript
18
 * @category Router
19
 * @author Henry Ruhs
20
 */
21
22
class Router extends RouterAbstract
23
{
24
	/**
25
	 * route the header
26
	 *
27
	 * @since 3.3.0
28
	 *
29
	 * @return bool
30
	 */
31
32 3
	public function routeHeader() : bool
33
	{
34 3
		Module\Hook::trigger('routeHeader');
35
36
		/* handle break */
37
38 3
		if ($this->_registry->get('routerBreak'))
39
		{
40 1
			Header::responseCode(200);
41
		}
42
43
		/* handle guard */
44
45 3
		if ($this->_tokenGuard())
46
		{
47 1
			Header::responseCode(403);
48
		}
49 3
		if ($this->_authGuard())
50
		{
51
			Header::responseCode(403);
52
		}
53
54
		/* handle validator */
55
56 3
		if ($this->_aliasValidator())
57
		{
58
			Header::responseCode(200);
59
		}
60 3
		else if (!$this->_contentValidator())
61
		{
62
			Header::responseCode(404);
63
		}
64
65
		/* handle post */
66
67 3
		if ($this->_request->getPost('Redaxscript\View\SearchForm'))
68
		{
69 1
			return $this->_redirectSearch();
70
		}
71 2
		return (bool)$this->_registry->get('routerBreak');
72
	}
73
74
	/**
75
	 * route the content
76
	 *
77
	 * @since 3.3.0
78
	 *
79
	 * @return string|null
80
	 */
81
82 17
	public function routeContent() : ?string
83
	{
84 17
		Module\Hook::trigger('routeContent');
85 17
		$firstParameter = $this->getFirst();
86
87
		/* handle break */
88
89 17
		if ($this->_registry->get('routerBreak'))
90
		{
91
			return '<!-- routerBreak -->';
92
		}
93
94
		/* handle guard */
95
96 17
		if ($this->_tokenGuard())
97
		{
98 1
			return $this->_errorToken();
99
		}
100 16
		if ($this->_authGuard())
101
		{
102
			return $this->_errorAccess();
103
		}
104
105
		/* handle post */
106
107 16
		if ($this->_request->getPost('Redaxscript\View\CommentForm'))
108
		{
109 1
			return $this->_processComment();
110
		}
111 15
		if ($this->_request->getPost('Redaxscript\View\LoginForm'))
112
		{
113 1
			return $this->_processLogin();
114
		}
115 14
		if ($this->_request->getPost('Redaxscript\View\ResetForm'))
116
		{
117 1
			return $this->_processReset();
118
		}
119 13
		if ($this->_request->getPost('Redaxscript\View\RecoverForm'))
120
		{
121 1
			return $this->_processRecover();
122
		}
123 12
		if ($this->_request->getPost('Redaxscript\View\RegisterForm'))
124
		{
125 1
			return $this->_processRegister();
126
		}
127 11
		if (!$this->_installGuard() && $this->_request->getPost('Redaxscript\View\InstallForm'))
128
		{
129 1
			return $this->_processInstall();
130
		}
131
132
		/* handle route */
133
134 10
		if ($firstParameter === 'search')
135
		{
136 1
			return $this->_processSearch();
137
		}
138 9
		if ($firstParameter === 'login')
139
		{
140 4
			return $this->_renderLogin();
141
		}
142 5
		if ($firstParameter === 'logout')
143
		{
144 1
			return $this->_processLogout();
145
		}
146 4
		if ($firstParameter === 'register')
147
		{
148 2
			return $this->_renderRegister();
149
		}
150 2
		if (!$this->_installGuard())
151
		{
152 1
			return $this->_renderInstall();
153
		}
154 1
		return null;
155
	}
156
157
	/**
158
	 * token guard
159
	 *
160
	 * @since 3.3.0
161
	 *
162
	 * @return bool
163
	 */
164
165 20
	protected function _tokenGuard() : bool
166
	{
167 20
		return $this->_request->get('post') && $this->_request->getPost('token') !== $this->_registry->get('token');
168
	}
169
170
	/**
171
	 * auth guard
172
	 *
173
	 * @since 3.3.0
174
	 *
175
	 * @return bool
176
	 */
177
178 19
	protected function _authGuard() : bool
179
	{
180 19
		return $this->_registry->get('token') !== $this->_registry->get('loggedIn') && $this->_registry->get('firstParameter') === 'admin';
181
	}
182
183
	/**
184
	 * install guard
185
	 *
186
	 * @since 4.5.0
187
	 *
188
	 * @return bool
189
	 */
190
191 11
	protected function _installGuard() : bool
192
	{
193 11
		return $this->_registry->get('file') !== 'install.php';
194
	}
195
196
	/**
197
	 * alias validator
198
	 *
199
	 * @since 4.0.0
200
	 *
201
	 * @return bool
202
	 */
203
204 3
	protected function _aliasValidator() : bool
205
	{
206 3
		$aliasValidator = new Validator\Alias();
207 3
		return $aliasValidator->matchSystem($this->_registry->get('firstParameter'));
208
	}
209
210
	/**
211
	 * content validator
212
	 *
213
	 * @since 4.0.0
214
	 *
215
	 * @return bool
216
	 */
217
218 3
	protected function _contentValidator() : bool
219
	{
220 3
		return $this->_registry->get('lastId') > 0;
221
	}
222
223
	/**
224
	 * redirect the search
225
	 *
226
	 * @since 3.3.0
227
	 *
228
	 * @return bool
229
	 */
230
231 1
	protected function _redirectSearch() : bool
232
	{
233 1
		$aliasFilter = new Filter\Alias();
234 1
		$root = $this->_registry->get('root');
235 1
		$parameterRoute = $this->_registry->get('parameterRoute');
236
237
		/* handle post */
238
239 1
		$table = $aliasFilter->sanitize($this->_request->getPost('table'));
240 1
		$search = $aliasFilter->sanitize($this->_request->getPost('search'));
241 1
		$tableString = $table ? '/' . $table : null;
242 1
		$searchString = $search ? '/' . $search : null;
243
244
		/* redirect */
245
246 1
		return Header::doRedirect($root . '/' . $parameterRoute . 'search' . $tableString . $searchString);
247
	}
248
249
	/**
250
	 * process the search
251
	 *
252
	 * @since 3.3.0
253
	 *
254
	 * @return string
255
	 */
256
257 1
	protected function _processSearch() : string
258
	{
259 1
		$searchController = new Controller\Search($this->_registry, $this->_request, $this->_language, $this->_config);
260 1
		return $searchController->process();
261
	}
262
263
	/**
264
	 * process the comment
265
	 *
266
	 * @since 3.3.0
267
	 *
268
	 * @return string
269
	 */
270
271 1
	protected function _processComment() : string
272
	{
273 1
		$commentController = new Controller\Comment($this->_registry, $this->_request, $this->_language, $this->_config);
274 1
		return $commentController->process();
275
	}
276
277
	/**
278
	 * process the login
279
	 *
280
	 * @since 3.3.0
281
	 *
282
	 * @return string
283
	 */
284
285 1
	protected function _processLogin() : string
286
	{
287 1
		$loginController = new Controller\Login($this->_registry, $this->_request, $this->_language, $this->_config);
288 1
		return $loginController->process();
289
	}
290
291
	/**
292
	 * process the reset
293
	 *
294
	 * @since 3.3.0
295
	 *
296
	 * @return string
297
	 */
298
299 1
	protected function _processReset() : string
300
	{
301 1
		$resetController = new Controller\Reset($this->_registry, $this->_request, $this->_language, $this->_config);
302 1
		return $resetController->process();
303
	}
304
305
	/**
306
	 * process the recover
307
	 *
308
	 * @since 3.3.0
309
	 *
310
	 * @return string
311
	 */
312
313 1
	protected function _processRecover() : string
314
	{
315 1
		$recoverController = new Controller\Recover($this->_registry, $this->_request, $this->_language, $this->_config);
316 1
		return $recoverController->process();
317
	}
318
319
	/**
320
	 * process the register
321
	 *
322
	 * @since 3.3.0
323
	 *
324
	 * @return string
325
	 */
326
327 1
	protected function _processRegister() : string
328
	{
329 1
		$registerController = new Controller\Register($this->_registry, $this->_request, $this->_language, $this->_config);
330 1
		return $registerController->process();
331
	}
332
333
	/**
334
	 * process the logout
335
	 *
336
	 * @since 3.3.0
337
	 *
338
	 * @return string
339
	 */
340
341 1
	protected function _processLogout() : string
342
	{
343 1
		$logoutController = new Controller\Logout($this->_registry, $this->_request, $this->_language, $this->_config);
344 1
		return $logoutController->process();
345
	}
346
347
	/**
348
	 * process the install
349
	 *
350
	 * @since 3.3.0
351
	 *
352
	 * @return string
353
	 */
354
355 1
	protected function _processInstall() : string
356
	{
357 1
		$emailFilter = new Filter\Email();
358 1
		$passwordFilter = new Filter\Password();
359 1
		$textFilter = new Filter\Text();
360 1
		$userFilter = new Filter\User();
361 1
		$this->_request->setSession('installArray',
362
		[
363 1
			'dbType' => $this->_request->getPost('db-type'),
364 1
			'dbHost' => $this->_request->getPost('db-host'),
365 1
			'dbName' => $this->_request->getPost('db-name'),
366 1
			'dbUser' => $this->_request->getPost('db-user'),
367 1
			'dbPassword' => $this->_request->getPost('db-password'),
368 1
			'dbPrefix' => $this->_request->getPost('db-prefix'),
369 1
			'adminName' => $textFilter->sanitize($this->_request->getPost('admin-name')),
370 1
			'adminUser' => $userFilter->sanitize($this->_request->getPost('admin-user')),
371 1
			'adminPassword' => $passwordFilter->sanitize($this->_request->getPost('admin-password')),
372 1
			'adminEmail' => $emailFilter->sanitize($this->_request->getPost('admin-email')),
373
		]);
374 1
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
375 1
		return $installController->process();
376
	}
377
378
	/**
379
	 * render the login
380
	 *
381
	 * @since 3.3.0
382
	 *
383
	 * @return string
384
	 */
385
386 4
	protected function _renderLogin() : string
387
	{
388 4
		$secondParameter = $this->getSecond();
389 4
		$thirdParameter = $this->getThird();
390 4
		$thirdSubParameter = $this->getThirdSub();
391 4
		$settingModel = new Model\Setting();
392
393
		/* handle login */
394
395 4
		if ($settingModel->get('recovery'))
0 ignored issues
show
Bug Best Practice introduced by
The expression $settingModel->get('recovery') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
396
		{
397 2
			if ($secondParameter === 'recover')
398
			{
399 1
				$recoverForm = new View\RecoverForm($this->_registry, $this->_language);
400 1
				return $recoverForm->render();
401
			}
402 1
			if ($secondParameter === 'reset' && $thirdParameter && $thirdSubParameter)
403
			{
404 1
				$resetForm = new View\ResetForm($this->_registry, $this->_language);
405 1
				return $resetForm->render();
406
			}
407
		}
408 2
		if (!$secondParameter)
409
		{
410 1
			$loginForm = new View\LoginForm($this->_registry, $this->_language);
411 1
			return $loginForm->render();
412
		}
413 1
		return $this->_errorAccess();
414
	}
415
416
	/**
417
	 * render the register
418
	 *
419
	 * @since 3.3.0
420
	 *
421
	 * @return string
422
	 */
423
424 2
	protected function _renderRegister() : string
425
	{
426 2
		$settingModel = new Model\Setting();
427 2
		if ($settingModel->get('registration'))
0 ignored issues
show
Bug Best Practice introduced by
The expression $settingModel->get('registration') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
428
		{
429 1
			$registerForm = new View\RegisterForm($this->_registry, $this->_language);
430 1
			return $registerForm->render();
431
		}
432 1
		return $this->_errorAccess();
433
	}
434
435
	/**
436
	 * render the install
437
	 *
438
	 * @since 3.3.0
439
	 *
440
	 * @return string
441
	 */
442
443 1
	protected function _renderInstall() : string
444
	{
445 1
		$installArray = $this->_request->getSession('installArray');
446 1
		$systemStatus = new View\SystemStatus($this->_registry, $this->_language);
447 1
		$installForm = new View\InstallForm($this->_registry, $this->_language);
448 1
		return $systemStatus->render() . $installForm->render($installArray ? : []);
449
	}
450
451
	/**
452
	 * messenger factory
453
	 *
454
	 * @since 4.0.0
455
	 *
456
	 * @return View\Helper\Messenger
457
	 */
458
459 3
	protected function _messengerFactory() : View\Helper\Messenger
460
	{
461 3
		return new View\Helper\Messenger($this->_registry);
462
	}
463
464
	/**
465
	 * show the token error
466
	 *
467
	 * @since 3.3.0
468
	 *
469
	 * @return string
470
	 */
471
472 1
	protected function _errorToken() : string
473
	{
474 1
		$messenger = $this->_messengerFactory();
475
		return $messenger
476 1
			->setUrl($this->_language->get('home'), $this->_registry->get('root'))
477 1
			->error($this->_language->get('token_incorrect'), $this->_language->get('error_occurred'));
478
	}
479
480
	/**
481
	 * show the access error
482
	 *
483
	 * @since 3.3.0
484
	 *
485
	 * @return string
486
	 */
487
488 2
	protected function _errorAccess() : string
489
	{
490 2
		$messenger = $this->_messengerFactory();
491
		return $messenger
492 2
			->setUrl($this->_language->get('home'), $this->_registry->get('root'))
493 2
			->error($this->_language->get('access_no'), $this->_language->get('error_occurred'));
494
	}
495
}
496