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 | 'adminUser' => $postArray['adminUser'], |
|
119 | 3 | 'adminName' => $postArray['adminName'], |
|
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 | if (!$this->_mail($adminArray)) |
|
135 | { |
||
136 | 1 | return $this->_warning( |
|
137 | [ |
||
138 | 1 | 'url' => 'index.php', |
|
139 | 1 | 'message' => $this->_language->get('email_failed') |
|
140 | ]); |
||
141 | } |
||
142 | |||
143 | /* handle success */ |
||
144 | |||
145 | 1 | return $this->_success( |
|
146 | [ |
||
147 | 1 | 'url' => 'index.php', |
|
148 | 1 | 'message' => $this->_language->get('installation_completed') |
|
149 | ]); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * sanitize the post |
||
154 | * |
||
155 | * @since 4.0.0 |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | |||
160 | 8 | protected function _sanitizePost() : array |
|
161 | { |
||
162 | 8 | $emailFilter = new Filter\Email(); |
|
163 | 8 | $specialFilter = new Filter\Special(); |
|
164 | |||
165 | /* sanitize post */ |
||
166 | |||
167 | return |
||
168 | [ |
||
169 | 8 | 'dbType' => $this->_request->getPost('db-type'), |
|
170 | 8 | 'dbHost' => $this->_request->getPost('db-host'), |
|
171 | 8 | 'dbName' => $this->_request->getPost('db-name'), |
|
172 | 8 | 'dbUser' => $this->_request->getPost('db-user'), |
|
173 | 8 | 'dbPassword' => $this->_request->getPost('db-password'), |
|
174 | 8 | 'dbPrefix' => $this->_request->getPost('db-prefix'), |
|
175 | 8 | 'adminName' => $specialFilter->sanitize($this->_request->getPost('admin-name')), |
|
176 | 8 | 'adminUser' => $specialFilter->sanitize($this->_request->getPost('admin-user')), |
|
177 | 8 | 'adminPassword' => $specialFilter->sanitize($this->_request->getPost('admin-password')), |
|
178 | 8 | 'adminEmail' => $emailFilter->sanitize($this->_request->getPost('admin-email')), |
|
179 | 8 | 'refreshConnection' => $this->_request->getPost('refresh-connection') |
|
180 | ]; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * validate the database |
||
185 | * |
||
186 | * @since 3.0.0 |
||
187 | * |
||
188 | * @param array $postArray array of the post |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | |||
193 | 11 | protected function _validateDatabase(array $postArray = []) : array |
|
194 | { |
||
195 | 11 | $validateArray = []; |
|
196 | 11 | if (!$postArray['dbType']) |
|
197 | { |
||
198 | 2 | $validateArray[] = $this->_language->get('type_empty'); |
|
199 | } |
||
200 | 11 | if (!$postArray['dbHost']) |
|
201 | { |
||
202 | 4 | $validateArray[] = $this->_language->get('host_empty'); |
|
203 | } |
||
204 | 11 | if ($postArray['dbType'] !== 'sqlite') |
|
205 | { |
||
206 | 3 | if (!$postArray['dbName']) |
|
207 | { |
||
208 | 3 | $validateArray[] = $this->_language->get('name_empty'); |
|
209 | } |
||
210 | 3 | if (!$postArray['dbUser']) |
|
211 | { |
||
212 | 3 | $validateArray[] = $this->_language->get('user_empty'); |
|
213 | } |
||
214 | } |
||
215 | 11 | return $validateArray; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * validate the account |
||
220 | * |
||
221 | * @since 3.0.0 |
||
222 | * |
||
223 | * @param array $postArray array of the post |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | |||
228 | 10 | protected function _validateAccount(array $postArray = []) : array |
|
229 | { |
||
230 | 10 | $emailValidator = new Validator\Email(); |
|
231 | 10 | $loginValidator = new Validator\Login(); |
|
232 | 10 | $validateArray = []; |
|
233 | |||
234 | /* validate post */ |
||
235 | |||
236 | 10 | if (!$postArray['adminName']) |
|
237 | { |
||
238 | 3 | $validateArray[] = $this->_language->get('name_empty'); |
|
239 | } |
||
240 | 10 | if (!$postArray['adminUser']) |
|
241 | { |
||
242 | 2 | $validateArray[] = $this->_language->get('user_empty'); |
|
243 | } |
||
244 | 8 | else if (!$loginValidator->validate($postArray['adminUser'])) |
|
245 | { |
||
246 | 1 | $validateArray[] = $this->_language->get('user_incorrect'); |
|
247 | } |
||
248 | 10 | if (!$postArray['adminPassword']) |
|
249 | { |
||
250 | 2 | $validateArray[] = $this->_language->get('password_empty'); |
|
251 | } |
||
252 | 8 | else if (!$loginValidator->validate($postArray['adminPassword'])) |
|
253 | { |
||
254 | 1 | $validateArray[] = $this->_language->get('password_incorrect'); |
|
255 | } |
||
256 | 10 | if (!$postArray['adminEmail']) |
|
257 | { |
||
258 | 2 | $validateArray[] = $this->_language->get('email_empty'); |
|
259 | } |
||
260 | 8 | else if (!$emailValidator->validate($postArray['adminEmail'])) |
|
261 | { |
||
262 | 1 | $validateArray[] = $this->_language->get('email_incorrect'); |
|
263 | } |
||
264 | 10 | return $validateArray; |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * touch sqlite file |
||
269 | * |
||
270 | * @since 3.0.0 |
||
271 | * |
||
272 | * @param array $configArray |
||
273 | * |
||
274 | * @return bool |
||
275 | */ |
||
276 | |||
277 | 5 | protected function _touch(array $configArray = []) : bool |
|
278 | { |
||
279 | 5 | if ($configArray['dbType'] === 'sqlite') |
|
280 | { |
||
281 | 5 | $file = $configArray['dbHost'] . '.tmp'; |
|
282 | 5 | return touch($file) && unlink($file); |
|
283 | } |
||
284 | return true; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * write config file |
||
289 | * |
||
290 | * @since 3.0.0 |
||
291 | * |
||
292 | * @param array $configArray |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | |||
297 | 4 | protected function _write(array $configArray = []) : bool |
|
298 | { |
||
299 | 4 | $this->_config->set('dbType', $configArray['dbType']); |
|
300 | 4 | $this->_config->set('dbHost', $configArray['dbHost']); |
|
301 | 4 | $this->_config->set('dbName', $configArray['dbName']); |
|
302 | 4 | $this->_config->set('dbUser', $configArray['dbUser']); |
|
303 | 4 | $this->_config->set('dbPassword', $configArray['dbPassword']); |
|
304 | 4 | $this->_config->set('dbPrefix', $configArray['dbPrefix']); |
|
305 | 4 | return $this->_config->write(); |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * get the status |
||
310 | * |
||
311 | * @since 3.0.0 |
||
312 | * |
||
313 | * @return int |
||
314 | */ |
||
315 | |||
316 | 4 | protected function _getStatus() : int |
|
317 | { |
||
318 | 4 | return Db::getStatus(); |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * refresh the connection |
||
323 | * |
||
324 | * @since 3.0.0 |
||
325 | */ |
||
326 | |||
327 | protected function _refreshConnection() : void |
||
328 | { |
||
329 | Db::resetDb(); |
||
330 | Db::init(); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * install the database |
||
335 | * |
||
336 | * @since 3.0.0 |
||
337 | * |
||
338 | * @param array $installArray |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | |||
343 | 4 | protected function _install(array $installArray = []) : bool |
|
344 | { |
||
345 | 4 | $adminName = $installArray['adminName']; |
|
346 | 4 | $adminUser = $installArray['adminUser']; |
|
347 | 4 | $adminPassword = $installArray['adminPassword']; |
|
348 | 4 | $adminEmail = $installArray['adminEmail']; |
|
349 | 4 | if ($adminName && $adminUser && $adminPassword && $adminEmail) |
|
350 | { |
||
351 | 3 | $installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config); |
|
352 | 3 | $installer->init(); |
|
353 | 3 | $installer->rawDrop(); |
|
354 | 3 | $installer->rawCreate(); |
|
355 | 3 | $installer->insertData( |
|
356 | [ |
||
357 | 3 | 'adminName' => $installArray['adminName'], |
|
358 | 3 | 'adminUser' => $installArray['adminUser'], |
|
359 | 3 | 'adminPassword' => $installArray['adminPassword'], |
|
360 | 3 | 'adminEmail' => $installArray['adminEmail'] |
|
361 | ]); |
||
362 | 3 | return $this->_getStatus() === 2; |
|
363 | } |
||
364 | 1 | return false; |
|
365 | } |
||
366 | |||
367 | /** |
||
368 | * send the mail |
||
369 | * |
||
370 | * @since 3.0.0 |
||
371 | * |
||
372 | * @param array $mailArray |
||
373 | * |
||
374 | * @return bool |
||
375 | */ |
||
376 | |||
377 | 1 | protected function _mail(array $mailArray = []) : bool |
|
378 | { |
||
379 | 1 | $settingModel = new Model\Setting(); |
|
380 | |||
381 | /* html element */ |
||
382 | |||
383 | 1 | $linkElement = new Html\Element(); |
|
384 | $linkElement |
||
385 | 1 | ->init('a', |
|
386 | [ |
||
387 | 1 | 'href' => $this->_registry->get('root') |
|
388 | ]) |
||
389 | 1 | ->text($this->_registry->get('root')); |
|
390 | |||
391 | /* prepare mail */ |
||
392 | |||
393 | $toArray = |
||
394 | [ |
||
395 | 1 | $mailArray['adminName'] => $mailArray['adminEmail'] |
|
396 | ]; |
||
397 | $fromArray = |
||
398 | [ |
||
399 | 1 | $settingModel->get('author') => $settingModel->get('email') |
|
400 | ]; |
||
401 | 1 | $subject = $this->_language->get('installation'); |
|
402 | $bodyArray = |
||
403 | [ |
||
404 | 1 | $this->_language->get('user') . $this->_language->get('colon') . ' ' . $mailArray['adminUser'], |
|
405 | 1 | '<br />', |
|
406 | 1 | $this->_language->get('password') . $this->_language->get('colon') . ' ' . $mailArray['adminPassword'], |
|
407 | 1 | '<br />', |
|
408 | 1 | $this->_language->get('url') . $this->_language->get('colon') . ' ' . $linkElement |
|
409 | ]; |
||
410 | |||
411 | /* send mail */ |
||
412 | |||
413 | 1 | $mailer = new Mailer(); |
|
414 | 1 | $mailer->init($toArray, $fromArray, $subject, $bodyArray); |
|
415 | 1 | return $mailer->send(); |
|
416 | } |
||
417 | |||
418 | /** |
||
419 | * show the success |
||
420 | * |
||
421 | * @since 3.0.0 |
||
422 | * |
||
423 | * @param array $successArray array of the success |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | |||
428 | 1 | protected function _success(array $successArray = []) : string |
|
429 | { |
||
430 | 1 | $messenger = $this->_messengerFactory(); |
|
431 | return $messenger |
||
432 | 1 | ->setUrl($this->_language->get('home'), $successArray['url']) |
|
0 ignored issues
–
show
|
|||
433 | 1 | ->doRedirect() |
|
434 | 1 | ->success($successArray['message'], $successArray['title']); |
|
435 | } |
||
436 | |||
437 | /** |
||
438 | * show the warning |
||
439 | * |
||
440 | * @since 3.0.0 |
||
441 | * |
||
442 | * @param array $warningArray array of the warning |
||
443 | * |
||
444 | * @return string |
||
445 | */ |
||
446 | |||
447 | 1 | protected function _warning(array $warningArray = []) : string |
|
448 | { |
||
449 | 1 | $messenger = $this->_messengerFactory(); |
|
450 | return $messenger |
||
451 | 1 | ->setUrl($this->_language->get('home'), $warningArray['url']) |
|
0 ignored issues
–
show
It seems like
$this->_language->get('home') targeting Redaxscript\Language::get() can also be of type array ; however, Redaxscript\View\Helper\Messenger::setUrl() 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...
|
|||
452 | 1 | ->doRedirect() |
|
453 | 1 | ->warning($warningArray['message'], $warningArray['title']); |
|
454 | } |
||
455 | |||
456 | /** |
||
457 | * show the error |
||
458 | * |
||
459 | * @since 3.0.0 |
||
460 | * |
||
461 | * @param array $errorArray array of the error |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | |||
466 | 6 | protected function _error(array $errorArray = []) : string |
|
467 | { |
||
468 | 6 | $messenger = $this->_messengerFactory(); |
|
469 | return $messenger |
||
470 | 6 | ->setUrl($this->_language->get('back'), $errorArray['url']) |
|
0 ignored issues
–
show
It seems like
$this->_language->get('back') targeting Redaxscript\Language::get() can also be of type array ; however, Redaxscript\View\Helper\Messenger::setUrl() 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...
|
|||
471 | 6 | ->error($errorArray['message'], $errorArray['title']); |
|
472 | } |
||
473 | } |
||
474 |
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.