Passed
Push — devel-3.0 ( 218a48...a00b1f )
by Rubén
03:54
created

InstallController::indexAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use Psr\Container\ContainerExceptionInterface;
28
use Psr\Container\NotFoundExceptionInterface;
29
use SP\Core\Exceptions\SPException;
30
use SP\Core\Language;
31
use SP\Core\PhpExtensionChecker;
32
use SP\Http\JsonResponse;
33
use SP\Modules\Web\Controllers\Helpers\LayoutHelper;
34
use SP\Modules\Web\Controllers\Traits\JsonTrait;
35
use SP\Mvc\View\Components\SelectItemAdapter;
36
use SP\Services\Install\InstallData;
37
use SP\Services\Install\Installer;
38
39
/**
40
 * Class InstallController
41
 *
42
 * @package SP\Modules\Web\Controllers
43
 */
44
final class InstallController extends ControllerBase
45
{
46
    use JsonTrait;
47
48
    /**
49
     * @throws ContainerExceptionInterface
50
     * @throws NotFoundExceptionInterface
51
     */
52
    public function indexAction()
53
    {
54
        $layoutHelper = $this->dic->get(LayoutHelper::class);
55
        $layoutHelper->getPublicLayout('index', 'install');
56
57
        $errors = [];
58
59
        foreach ($this->dic->get(PhpExtensionChecker::class)->getMissing() as $module) {
60
            $error[] = [
61
                'type' => SPException::WARNING,
62
                'description' => sprintf('%s (%s)', __('Módulo no disponible'), $module),
63
                'hint' => __('Sin este módulo la aplicación puede no funcionar correctamente.')
64
            ];
65
        }
66
67
        $this->view->assign('errors', $errors);
68
        $this->view->assign('langs', SelectItemAdapter::factory(Language::getAvailableLanguages())->getItemsFromArraySelected([Language::$globalLang]));
69
70
        $this->view();
71
    }
72
73
    /**
74
     * Performs sysPass installation
75
     *
76
     * @throws ContainerExceptionInterface
77
     * @throws NotFoundExceptionInterface
78
     */
79
    public function installAction()
80
    {
81
        $installData = new InstallData();
82
        $installData->setSiteLang($this->request->analyzeString('sitelang', 'en_US'));
83
        $installData->setAdminLogin($this->request->analyzeString('adminlogin', 'admin'));
84
        $installData->setAdminPass($this->request->analyzeEncrypted('adminpass'));
85
        $installData->setMasterPassword($this->request->analyzeEncrypted('masterpassword'));
86
        $installData->setDbAdminUser($this->request->analyzeString('dbuser', 'root'));
87
        $installData->setDbAdminPass($this->request->analyzeEncrypted('dbpass'));
88
        $installData->setDbName($this->request->analyzeString('dbname', 'syspass'));
89
        $installData->setDbHost($this->request->analyzeString('dbhost', 'localhost'));
90
        $installData->setHostingMode($this->request->analyzeBool('hostingmode', false));
91
92
        try {
93
            $this->dic->get(Installer::class)->run($installData);
94
95
            $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Instalación finalizada'));
96
        } catch (\Exception $e) {
97
            processException($e);
98
99
            $this->returnJsonResponseException($e);
100
        }
101
    }
102
}