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

includes/Controller/Install.php (3 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\Db;
5
use Redaxscript\Filter;
6
use Redaxscript\Html;
7
use Redaxscript\Installer;
8
use Redaxscript\Mailer;
9
use Redaxscript\Model;
10
use Redaxscript\Validator;
11
use function touch;
12
use function unlink;
13
14
/**
15
 * children class to process install
16
 *
17
 * @since 3.0.0
18
 *
19
 * @package Redaxscript
20
 * @category Controller
21
 * @author Henry Ruhs
22
 * @author Balázs Szilágyi
23
 */
24
25
class Install extends ControllerAbstract
26
{
27
	/**
28
	 * process the class
29
	 *
30
	 * @since 3.0.0
31
	 *
32
	 * @return string
33
	 */
34
35 8
	public function process() : string
36
	{
37 8
		$postArray = $this->_normalizePost($this->_sanitizePost());
38
39
		/* validate database */
40
41 8
		$validateArray = $this->_validateDatabase($postArray);
42 8
		if ($validateArray)
43
		{
44 1
			return $this->_error(
45
			[
46 1
				'url' => 'install.php',
47 1
				'title' => $this->_language->get('database'),
48 1
				'message' => $validateArray
49
			]);
50
		}
51
52
		/* validate account */
53
54 7
		$validateArray = $this->_validateAccount($postArray);
55 7
		if ($validateArray)
56
		{
57 1
			return $this->_error(
58
			[
59 1
				'url' => 'install.php',
60 1
				'title' => $this->_language->get('account'),
61 1
				'message' => $validateArray
62
			]);
63
		}
64
65
		/* touch config */
66
67
		$configArray =
68
		[
69 6
			'dbType' => $postArray['dbType'],
70 6
			'dbHost' => $postArray['dbHost'],
71 6
			'dbName' => $postArray['dbName'],
72 6
			'dbUser' => $postArray['dbUser'],
73 6
			'dbPassword' => $postArray['dbPassword'],
74 6
			'dbPrefix' => $postArray['dbPrefix']
75
		];
76 6
		if (!$this->_touch($configArray))
77
		{
78 1
			return $this->_error(
79
			[
80 1
				'url' => 'install.php',
81 1
				'message' => $this->_language->get('directory_permission_grant') . $this->_language->get('point')
82
			]);
83
		}
84
85
		/* write config */
86
87 5
		if (!$this->_write($configArray))
88
		{
89 1
			return $this->_error(
90
			[
91 1
				'url' => 'install.php',
92 1
				'message' => $this->_language->get('file_permission_grant') . $this->_language->get('colon') . ' config.php'
93
			]);
94
		}
95
96
		/* refresh connection */
97
98 4
		if ($postArray['refreshConnection'])
99
		{
100
			$this->_refreshConnection();
101
		}
102
103
		/* handle database */
104
105 4
		if (!$this->_getStatus())
106
		{
107 1
			return $this->_error(
108
			[
109 1
				'url' => 'install.php',
110 1
				'message' => $this->_language->get('database_failed')
111
			]);
112
		}
113
114
		/* handle install */
115
116
		$adminArray =
117
		[
118 3
			'adminName' => $postArray['adminName'],
119 3
			'adminUser' => $postArray['adminUser'],
120 3
			'adminEmail' => $postArray['adminEmail'],
121 3
			'adminPassword' => $postArray['adminPassword']
122
		];
123 3
		if (!$this->_install($adminArray))
124
		{
125 1
			return $this->_error(
126
			[
127 1
				'url' => 'install.php',
128 1
				'message' => $this->_language->get('installation_failed')
129
			]);
130
		}
131
132
		/* handle mail */
133
134 2
		$mailArray =
135
		[
136 1
			'adminName' => $postArray['adminName'],
137
			'adminUser' => $postArray['adminUser'],
138 1
			'adminEmail' => $postArray['adminEmail']
139 1
		];
140
		if (!$this->_mail($mailArray))
141
		{
142
			return $this->_warning(
143
			[
144
				'url' => 'index.php',
145 1
				'message' => $this->_language->get('email_failed')
146
			]);
147 1
		}
148 1
149
		/* handle success */
150
151
		return $this->_success(
152
		[
153
			'url' => 'index.php',
154
			'message' => $this->_language->get('installation_completed')
155
		]);
156
	}
157
158
	/**
159
	 * sanitize the post
160 8
	 *
161
	 * @since 4.0.0
162 8
	 *
163 8
	 * @return array
164
	 */
165
166
	protected function _sanitizePost() : array
167
	{
168
		$emailFilter = new Filter\Email();
169 8
		$nameFilter = new Filter\Name();
170 8
		$passwordFilter = new Filter\Password();
171 8
		$userFilter = new Filter\User();
172 8
173 8
		/* sanitize post */
174 8
175 8
		return
176 8
		[
177 8
			'dbType' => $this->_request->getPost('db-type'),
178 8
			'dbHost' => $this->_request->getPost('db-host'),
179 8
			'dbName' => $this->_request->getPost('db-name'),
180
			'dbUser' => $this->_request->getPost('db-user'),
181
			'dbPassword' => $this->_request->getPost('db-password'),
182
			'dbPrefix' => $this->_request->getPost('db-prefix'),
183
			'adminName' => $nameFilter->sanitize($this->_request->getPost('admin-name')),
0 ignored issues
show
It seems like $this->_request->getPost('admin-name') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Name::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...
184
			'adminUser' => $userFilter->sanitize($this->_request->getPost('admin-user')),
0 ignored issues
show
It seems like $this->_request->getPost('admin-user') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\User::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...
185
			'adminPassword' => $passwordFilter->sanitize($this->_request->getPost('admin-password')),
0 ignored issues
show
It seems like $this->_request->getPost('admin-password') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Password::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...
186
			'adminEmail' => $emailFilter->sanitize($this->_request->getPost('admin-email')),
187
			'refreshConnection' => $this->_request->getPost('refresh-connection')
188
		];
189
	}
190
191
	/**
192
	 * validate the database
193 11
	 *
194
	 * @since 3.0.0
195 11
	 *
196 11
	 * @param array $postArray array of the post
197
	 *
198 2
	 * @return array
199
	 */
200 11
201
	protected function _validateDatabase(array $postArray = []) : array
202 4
	{
203
		$validateArray = [];
204 11
		if (!$postArray['dbType'])
205
		{
206 3
			$validateArray[] = $this->_language->get('type_empty');
207
		}
208 3
		if (!$postArray['dbHost'])
209
		{
210 3
			$validateArray[] = $this->_language->get('host_empty');
211
		}
212 3
		if ($postArray['dbType'] !== 'sqlite')
213
		{
214
			if (!$postArray['dbName'])
215 11
			{
216
				$validateArray[] = $this->_language->get('name_empty');
217
			}
218
			if (!$postArray['dbUser'])
219
			{
220
				$validateArray[] = $this->_language->get('user_empty');
221
			}
222
		}
223
		return $validateArray;
224
	}
225
226
	/**
227
	 * validate the account
228 10
	 *
229
	 * @since 3.0.0
230 10
	 *
231 10
	 * @param array $postArray array of the post
232 10
	 *
233 10
	 * @return array
234
	 */
235
236
	protected function _validateAccount(array $postArray = []) : array
237 10
	{
238
		$nameValidator = new Validator\Name();
239 3
		$emailValidator = new Validator\Email();
240
		$userValidator = new Validator\User();
241 10
		$passwordValidator = new Validator\Password();
242
		$validateArray = [];
243 2
244
		/* validate post */
245 8
246
		if (!$postArray['adminName'])
247 1
		{
248
			$validateArray[] = $this->_language->get('name_empty');
249 10
		}
250
		else if (!$nameValidator->validate($postArray['adminName']))
251 2
		{
252
			$validateArray[] = $this->_language->get('name_incorrect');
253 8
		}
254
		if (!$postArray['adminUser'])
255 1
		{
256
			$validateArray[] = $this->_language->get('user_empty');
257 10
		}
258
		else if (!$userValidator->validate($postArray['adminUser']))
259 2
		{
260
			$validateArray[] = $this->_language->get('user_incorrect');
261 8
		}
262
		if (!$postArray['adminPassword'])
263 1
		{
264
			$validateArray[] = $this->_language->get('password_empty');
265 10
		}
266
		else if (!$passwordValidator->validate($postArray['adminPassword']))
267
		{
268
			$validateArray[] = $this->_language->get('password_incorrect');
269
		}
270
		if (!$postArray['adminEmail'])
271
		{
272
			$validateArray[] = $this->_language->get('email_empty');
273
		}
274
		else if (!$emailValidator->validate($postArray['adminEmail']))
275
		{
276
			$validateArray[] = $this->_language->get('email_incorrect');
277
		}
278 5
		return $validateArray;
279
	}
280 5
281
	/**
282 5
	 * touch sqlite file
283 5
	 *
284
	 * @since 3.0.0
285
	 *
286
	 * @param array $configArray
287
	 *
288
	 * @return bool
289
	 */
290
291
	protected function _touch(array $configArray = []) : bool
292
	{
293
		if ($configArray['dbType'] === 'sqlite')
294
		{
295
			$file = $configArray['dbHost'] . '.tmp';
296
			return touch($file) && unlink($file);
297
		}
298 4
		return true;
299
	}
300 4
301 4
	/**
302 4
	 * write config file
303 4
	 *
304 4
	 * @since 3.0.0
305 4
	 *
306 4
	 * @param array $configArray
307
	 *
308
	 * @return bool
309
	 */
310
311
	protected function _write(array $configArray = []) : bool
312
	{
313
		$this->_config->set('dbType', $configArray['dbType']);
314
		$this->_config->set('dbHost', $configArray['dbHost']);
315
		$this->_config->set('dbName', $configArray['dbName']);
316
		$this->_config->set('dbUser', $configArray['dbUser']);
317 4
		$this->_config->set('dbPassword', $configArray['dbPassword']);
318
		$this->_config->set('dbPrefix', $configArray['dbPrefix']);
319 4
		return $this->_config->write();
320
	}
321
322
	/**
323
	 * get the status
324
	 *
325
	 * @since 3.0.0
326
	 *
327
	 * @return int
328
	 */
329
330
	protected function _getStatus() : int
331
	{
332
		return Db::getStatus();
333
	}
334
335
	/**
336
	 * refresh the connection
337
	 *
338
	 * @since 3.0.0
339
	 */
340
341
	protected function _refreshConnection() : void
342
	{
343
		Db::resetDb();
344 4
		Db::init();
345
	}
346 4
347 4
	/**
348 4
	 * install the database
349 4
	 *
350 4
	 * @since 3.0.0
351
	 *
352 3
	 * @param array $installArray
353 3
	 *
354 3
	 * @return bool
355 3
	 */
356 3
357
	protected function _install(array $installArray = []) : bool
358 3
	{
359 3
		$adminName = $installArray['adminName'];
360 3
		$adminUser = $installArray['adminUser'];
361 3
		$adminPassword = $installArray['adminPassword'];
362
		$adminEmail = $installArray['adminEmail'];
363 3
		if ($adminName && $adminUser && $adminPassword && $adminEmail)
364
		{
365 1
			$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
366
			$installer->init();
367
			$installer->rawDrop();
368
			$installer->rawCreate();
369
			$installer->insertData(
370
			[
371
				'adminName' => $installArray['adminName'],
372
				'adminUser' => $installArray['adminUser'],
373
				'adminPassword' => $installArray['adminPassword'],
374
				'adminEmail' => $installArray['adminEmail']
375
			]);
376
			return $this->_getStatus() === 2;
377
		}
378 1
		return false;
379
	}
380 1
381
	/**
382
	 * send the mail
383
	 *
384 1
	 * @since 3.0.0
385
	 *
386 1
	 * @param array $mailArray
387
	 *
388 1
	 * @return bool
389
	 */
390 1
391
	protected function _mail(array $mailArray = []) : bool
392
	{
393
		$settingModel = new Model\Setting();
394
		$urlLogin = $this->_registry->get('root') . '/' . $this->_registry->get('parameterRoute') . 'login';
395
396 1
		/* html element */
397
398
		$linkElement = new Html\Element();
399
		$linkElement
400 1
			->init('a',
401
			[
402 1
				'href' => $urlLogin
403
			])
404
			->text($urlLogin);
405 1
406 1
		/* prepare mail */
407 1
408 1
		$toArray =
409 1
		[
410
			$mailArray['adminName'] => $mailArray['adminEmail']
411
		];
412
		$fromArray =
413
		[
414 1
			$settingModel->get('author') => $settingModel->get('email')
415 1
		];
416 1
		$subject = $this->_language->get('installation');
417
		$bodyArray =
418
		[
419
			$this->_language->get('user') . $this->_language->get('colon') . ' ' . $mailArray['adminUser'],
420
			'<br />',
421
			$this->_language->get('login') . $this->_language->get('colon') . ' ' . $linkElement
422
		];
423
424
		/* send mail */
425
426
		$mailer = new Mailer();
427
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
428
		return $mailer->send();
429 1
	}
430
431 1
	/**
432
	 * show the success
433 1
	 *
434 1
	 * @since 3.0.0
435 1
	 *
436
	 * @param array $successArray array of the success
437
	 *
438
	 * @return string
439
	 */
440
441
	protected function _success(array $successArray = []) : string
442
	{
443
		$messenger = $this->_messengerFactory();
444
		return $messenger
445
			->setUrl($this->_language->get('home'), $successArray['url'])
446
			->doRedirect()
447
			->success($successArray['message'], $successArray['title']);
448 1
	}
449
450 1
	/**
451
	 * show the warning
452 1
	 *
453 1
	 * @since 3.0.0
454 1
	 *
455
	 * @param array $warningArray array of the warning
456
	 *
457
	 * @return string
458
	 */
459
460
	protected function _warning(array $warningArray = []) : string
461
	{
462
		$messenger = $this->_messengerFactory();
463
		return $messenger
464
			->setUrl($this->_language->get('home'), $warningArray['url'])
465
			->doRedirect()
466
			->warning($warningArray['message'], $warningArray['title']);
467 6
	}
468
469 6
	/**
470
	 * show the error
471 6
	 *
472 6
	 * @since 3.0.0
473
	 *
474
	 * @param array $errorArray array of the error
475
	 *
476
	 * @return string
477
	 */
478
479
	protected function _error(array $errorArray = []) : string
480
	{
481
		$messenger = $this->_messengerFactory();
482
		return $messenger
483
			->setUrl($this->_language->get('back'), $errorArray['url'])
484
			->error($errorArray['message'], $errorArray['title']);
485
	}
486
}
487