Passed
Push — master ( 221005...26261c )
by Rubén
03:12
created

UpgradeController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
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 SP\Core\Acl\Actions;
28
use SP\Http\JsonResponse;
29
use SP\Modules\Web\Controllers\Helpers\LayoutHelper;
30
use SP\Modules\Web\Controllers\Traits\JsonTrait;
31
use SP\Services\Upgrade\UpgradeAppService;
32
use SP\Services\Upgrade\UpgradeDatabaseService;
33
34
/**
35
 * Class UpgradeController
36
 *
37
 * @package SP\Modules\Web\Controllers
38
 */
39
final class UpgradeController extends ControllerBase
40
{
41
    use JsonTrait;
42
43
    /**
44
     * indexAction
45
     *
46
     * @throws \DI\DependencyException
47
     * @throws \DI\NotFoundException
48
     * @throws \SP\Storage\File\FileException
49
     */
50
    public function indexAction()
51
    {
52
        $layoutHelper = $this->dic->get(LayoutHelper::class);
53
        $layoutHelper->getPublicLayout('index', 'upgrade');
54
55
        $this->dic->get(Actions::class)->reset();
56
57
        $this->view();
58
    }
59
60
    /**
61
     * upgradeAction
62
     */
63
    public function upgradeAction()
64
    {
65
        if ($this->request->analyzeBool('chkConfirm', false) === false) {
66
            return $this->returnJsonResponse(
67
                JsonResponse::JSON_ERROR,
68
                __u('The updating need to be confirmed')
69
            );
70
        }
71
72
        if ($this->request->analyzeString('key') !== $this->configData->getUpgradeKey()) {
73
            return $this->returnJsonResponse(
74
                JsonResponse::JSON_ERROR,
75
                __u('Wrong security code')
76
            );
77
        }
78
79
        try {
80
            $dbVersion = $this->configData->getDatabaseVersion();
81
            $dbVersion = empty($dbVersion) ? '0.0' : $dbVersion;
82
83
            if (UpgradeDatabaseService::needsUpgrade($dbVersion)) {
84
                $this->dic->get(UpgradeDatabaseService::class)
85
                    ->upgrade($dbVersion, $this->configData);
86
            }
87
88
            $appVersion = $this->configData->getAppVersion();
89
            $appVersion = empty($appVersion) ? '0.0' : $appVersion;
90
91
            if (UpgradeAppService::needsUpgrade($appVersion)) {
92
                $this->dic->get(UpgradeAppService::class)
93
                    ->upgrade($appVersion, $this->configData);
94
            }
95
96
            $this->configData->setUpgradeKey(null);
97
            $this->config->saveConfig($this->configData, false);
98
99
            return $this->returnJsonResponse(
100
                JsonResponse::JSON_SUCCESS,
101
                __u('Application successfully updated'),
102
                [__u('You will be redirected to log in within 5 seconds')]
103
            );
104
        } catch (\Exception $e) {
105
            processException($e);
106
107
            return $this->returnJsonResponseException($e);
108
        }
109
    }
110
111
    /**
112
     * @return void
113
     */
114
    protected function initialize()
115
    {
116
        // TODO: Implement initialize() method.
117
    }
118
}