Completed
Push — master ( 9458ed...7d322b )
by Henry
10:04
created

includes/Controller/Login.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\Controller;
3
4
use Redaxscript\Auth;
5
use Redaxscript\Filter;
6
use Redaxscript\Model;
7
use Redaxscript\Validator;
8
9
/**
10
 * children class to process the login request
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 * @author Balázs Szilágyi
18
 */
19
20
class Login extends ControllerAbstract
21
{
22
	/**
23
	 * process the class
24
	 *
25
	 * @since 3.0.0
26
	 *
27
	 * @return string
28
	 */
29
30 6
	public function process() : string
31
	{
32 6
		$postArray = $this->_normalizePost($this->_sanitizePost());
33 6
		$validateArray = $this->_validatePost($postArray);
34 6
		$user = $this->_getUser($postArray);
35
36
		/* validate post */
37
38 6
		if ($validateArray)
39
		{
40 3
			return $this->_error(
41
			[
42 3
				'route' => 'login',
43 3
				'message' => $validateArray
44
			]);
45
		}
46
47
		/* handle login */
48
49 3
		if ($this->_login($user->id))
50
		{
51 2
			return $this->_success(
52
			[
53 2
				'route' => 'admin',
54 2
				'timeout' => 0,
55 2
				'message' => $this->_language->get('logged_in'),
56 2
				'title' => $this->_language->get('welcome')
57
			]);
58
		}
59
60
		/* handle error */
61
62 1
		return $this->_error(
63
		[
64 1
			'route' => 'login'
65
		]);
66
	}
67
68
	/**
69
	 * sanitize the post
70
	 *
71
	 * @since 4.0.0
72
	 *
73
	 * @return array
74
	 */
75
76 6
	protected function _sanitizePost() : array
77
	{
78 6
		$numberFilter = new Filter\Number();
79 6
		$specialFilter = new Filter\Special();
80 6
		$emailFilter = new Filter\Email();
81 6
		$emailValidator = new Validator\Email();
82 6
		$userValidator = new Validator\User();
83
84
		/* sanitize post */
85
86
		return
87
		[
88 6
			'email' => $emailValidator->validate($this->_request->getPost('user')) ? $emailFilter->sanitize($this->_request->getPost('user')) : null,
89 6
			'user' => $userValidator->validate($this->_request->getPost('user')) ? $specialFilter->sanitize($this->_request->getPost('user')) : null,
0 ignored issues
show
It seems like $this->_request->getPost('user') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::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...
90 6
			'password' => $specialFilter->sanitize($this->_request->getPost('password')),
0 ignored issues
show
It seems like $this->_request->getPost('password') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::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...
91 6
			'task' => $numberFilter->sanitize($this->_request->getPost('task')),
92 6
			'solution' => $this->_request->getPost('solution')
93
		];
94
	}
95
96
	/**
97
	 * validate the post
98
	 *
99
	 * @since 3.0.0
100
	 *
101
	 * @param array $postArray array of the post
102
	 *
103
	 * @return array
104
	 */
105
106 6
	protected function _validatePost(array $postArray = []) : array
107
	{
108 6
		$passwordValidator = new Validator\Password();
109 6
		$captchaValidator = new Validator\Captcha();
110 6
		$settingModel = new Model\Setting();
111 6
		$user = $this->_getUser($postArray);
112 6
		$validateArray = [];
113
114
		/* validate post */
115
116 6
		if (!$postArray['user'] && !$postArray['email'])
117
		{
118 1
			$validateArray[] = $this->_language->get('user_empty');
119
		}
120 5
		else if (!$user->id)
121
		{
122 1
			$validateArray[] = $this->_language->get('user_incorrect');
123
		}
124 6
		if (!$postArray['password'])
125
		{
126 1
			$validateArray[] = $this->_language->get('password_empty');
127
		}
128 5
		else if (!$passwordValidator->validate($postArray['password']) || !$passwordValidator->matchHash($postArray['password'], $user->password))
129
		{
130 2
			$validateArray[] = $this->_language->get('password_incorrect');
131
		}
132 6
		if ($settingModel->get('captcha') > 0 && !$captchaValidator->validate($postArray['task'], $postArray['solution']))
133
		{
134 1
			$validateArray[] = $this->_language->get('captcha_incorrect');
135
		}
136 6
		return $validateArray;
137
	}
138
139
	/**
140
	 * get the user
141
	 *
142
	 * @since 4.0.0
143
	 *
144
	 * @param array $postArray array of the post
145
	 *
146
	 * @return object|null
147
	 */
148
149 6
	protected function _getUser(array $postArray = []) : ?object
150
	{
151 6
		$userModel = new Model\User();
152 6
		return $userModel->getByUserOrEmail($postArray['user'], $postArray['email']);
153
	}
154
155
	/**
156
	 * login the user
157
	 *
158
	 * @since 3.0.0
159
	 *
160
	 * @param int $userId identifier of the user
161
	 *
162
	 * @return int
163
	 */
164
165 2
	protected function _login(int $userId = null) : int
166
	{
167 2
		$auth = new Auth($this->_request);
168 2
		return $auth->login($userId);
169
	}
170
}
171