Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Controller/Register.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\Controller;
3
4
use Redaxscript\Filter;
5
use Redaxscript\Hash;
6
use Redaxscript\Html;
7
use Redaxscript\Mailer;
8
use Redaxscript\Model;
9
use Redaxscript\Validator;
10
use function uniqid;
11
12
/**
13
 * children class to process the register request
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Controller
19
 * @author Henry Ruhs
20
 * @author Balázs Szilágyi
21
 */
22
23
class Register extends ControllerAbstract
24
{
25
	/**
26
	 * process
27
	 *
28
	 * @since 3.0.0
29
	 *
30
	 * @return string
31
	 */
32
33 5
	public function process() : string
34
	{
35 5
		$passwordHash = new Hash();
36 5
		$passwordHash->init(uniqid());
37 5
		$groupModel = new Model\Group();
38 5
		$settingModel = new Model\Setting();
39 5
		$postArray = $this->_normalizePost($this->_sanitizePost());
40 5
		$validateArray = $this->_validatePost($postArray);
41
42
		/* validate post */
43
44 5
		if ($validateArray)
45
		{
46 3
			return $this->_error(
47
			[
48 3
				'route' => 'register',
49 3
				'message' => $validateArray
50
			]);
51
		}
52
53
		/* handle create */
54
55
		$createArray =
56
		[
57 2
			'name' => $postArray['name'],
58 2
			'user' => $postArray['user'],
59 2
			'password' => $passwordHash->getHash(),
60 2
			'email' => $postArray['email'],
61 2
			'language' => $this->_registry->get('language'),
62 2
			'groups' => $groupModel->getByAlias('members')->id,
63 2
			'status' => $settingModel->get('verification') ? 0 : 1
64
		];
65 2
		if (!$this->_create($createArray))
66
		{
67
			return $this->_error(
68
			[
69
				'route' => 'register'
70
			]);
71
		}
72
73
		/* handle mail */
74
75
		$mailArray =
76
		[
77 2
			'name' => $postArray['name'],
78 2
			'user' => $postArray['user'],
79 2
			'password' => $passwordHash->getRaw(),
80 2
			'email' => $postArray['email']
81
		];
82 2
		if (!$this->_mail($mailArray))
83
		{
84 1
			return $this->_error(
85
			[
86 1
				'route' => 'register',
87 1
				'message' => $this->_language->get('email_failed')
88
			]);
89
		}
90
91
		/* handle success */
92
93 1
		return $this->_success(
94
		[
95 1
			'route' => 'login',
96 1
			'timeout' => 2,
97 1
			'message' => $settingModel->get('verification') ? $this->_language->get('registration_verification') : $this->_language->get('registration_sent')
98
		]);
99
	}
100
101
	/**
102
	 * sanitize the post
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @return array
107
	 */
108
109 5
	protected function _sanitizePost() : array
110
	{
111 5
		$numberFilter = new Filter\Number();
112 5
		$specialFilter = new Filter\Special();
113 5
		$emailFilter = new Filter\Email();
114
115
		/* sanitize post */
116
117
		return
118
		[
119 5
			'name' => $specialFilter->sanitize($this->_request->getPost('name')),
120 5
			'user' => $specialFilter->sanitize($this->_request->getPost('user')),
121 5
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
122 5
			'task' => $numberFilter->sanitize($this->_request->getPost('task')),
123 5
			'solution' => $this->_request->getPost('solution')
124
		];
125
	}
126
127
	/**
128
	 * validate the post
129
	 *
130
	 * @since 3.0.0
131
	 *
132
	 * @param array $postArray array of the post
133
	 *
134
	 * @return array
135
	 */
136
137 5
	protected function _validatePost(array $postArray = []) : array
138
	{
139 5
		$loginValidator = new Validator\Login();
140 5
		$emailValidator = new Validator\Email();
141 5
		$captchaValidator = new Validator\Captcha();
142 5
		$settingModel = new Model\Setting();
143 5
		$userModel = new Model\User();
144 5
		$validateArray = [];
145
146
		/* validate post */
147
148 5
		if (!$postArray['name'])
149
		{
150 1
			$validateArray[] = $this->_language->get('name_empty');
151
		}
152 5
		if (!$postArray['user'])
153
		{
154 1
			$validateArray[] = $this->_language->get('user_empty');
155
		}
156 4
		else if (!$loginValidator->validate($postArray['user']))
157
		{
158 1
			$validateArray[] = $this->_language->get('user_incorrect');
159
		}
160 3
		else if ($userModel->query()->where('user', $postArray['user'])->findOne()->id)
161
		{
162 1
			$validateArray[] = $this->_language->get('user_exists');
163
		}
164 5
		if (!$postArray['email'])
165
		{
166 1
			$validateArray[] = $this->_language->get('email_empty');
167
		}
168 4
		else if (!$emailValidator->validate($postArray['email']))
169
		{
170 1
			$validateArray[] = $this->_language->get('email_incorrect');
171
		}
172 5
		if ($settingModel->get('captcha') > 0 && !$captchaValidator->validate($postArray['task'], $postArray['solution']))
173
		{
174 1
			$validateArray[] = $this->_language->get('captcha_incorrect');
175
		}
176 5
		return $validateArray;
177
	}
178
179
	/**
180
	 * create the user
181
	 *
182
	 * @since 3.0.0
183
	 *
184
	 * @param array $createArray
185
	 *
186
	 * @return bool
187
	 */
188
189 2
	protected function _create(array $createArray = []) : bool
190
	{
191 2
		$userModel = new Model\User();
192 2
		return $userModel->createByArray($createArray);
193
	}
194
195
	/**
196
	 * send the mail
197
	 *
198
	 * @since 3.0.0
199
	 *
200
	 * @param array $mailArray
201
	 *
202
	 * @return bool
203
	 */
204
205 1
	protected function _mail(array $mailArray = []) : bool
206
	{
207 1
		$settingModel = new Model\Setting();
208 1
		$urlLogin = $this->_registry->get('root') . '/' . $this->_registry->get('parameterRoute') . 'login';
209
210
		/* html element */
211
212 1
		$linkElement = new Html\Element();
213
		$linkElement
214 1
			->init('a',
215
			[
216 1
				'href' => $urlLogin
217
			])
218 1
			->text($urlLogin);
219
220
		/* prepare mail */
221
222
		$toArray =
223
		[
224 1
			$mailArray['name'] => $mailArray['email'],
225 1
			$settingModel->get('author') => $settingModel->get('notification') ? $settingModel->get('email') : null
226
		];
227
		$fromArray =
228
		[
229 1
			$mailArray['name'] => $mailArray['email']
230
		];
231 1
		$subject = $this->_language->get('registration');
232
		$bodyArray =
233
		[
234 1
			$this->_language->get('name') . $this->_language->get('colon') . ' ' . $mailArray['name'],
235 1
			'<br />',
236 1
			$this->_language->get('user') . $this->_language->get('colon') . ' ' . $mailArray['user'],
237 1
			'<br />',
238 1
			$this->_language->get('password') . $this->_language->get('colon') . ' ' . $mailArray['password'],
239 1
			'<br />',
240 1
			$this->_language->get('login') . $this->_language->get('colon') . ' ' . $linkElement
241
		];
242
243
		/* send mail */
244
245 1
		$mailer = new Mailer();
246 1
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
0 ignored issues
show
It seems like $subject defined by $this->_language->get('registration') on line 231 can also be of type array; however, Redaxscript\Mailer::init() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
247 1
		return $mailer->send();
248
	}
249
}
250