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

BootstrapController::getEnvironmentAction()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 24
nop 0
dl 0
loc 23
rs 9.0444
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\Bootstrap;
28
use SP\Core\Crypt\CryptPKI;
29
use SP\Modules\Web\Controllers\Traits\JsonTrait;
30
use SP\Plugin\PluginManager;
31
use SP\Providers\Auth\Browser\Browser;
32
use SP\Services\Import\ImportService;
33
34
/**
35
 * Class BootstrapController
36
 *
37
 * @package SP\Modules\Web\Controllers
38
 */
39
final class BootstrapController extends SimpleControllerBase
40
{
41
    use JsonTrait;
42
43
    /**
44
     * Returns environment data
45
     *
46
     * @throws \SP\Core\Exceptions\FileNotFoundException
47
     * @throws \SP\Core\Exceptions\SPException
48
     */
49
    public function getEnvironmentAction()
50
    {
51
        $checkStatus = $this->session->getAuthCompleted() && ($this->session->getUserData()->getIsAdminApp() || $this->configData->isDemoEnabled());
52
53
        $data = [
54
            'lang' => $this->getJsLang(),
55
            'locale' => $this->configData->getSiteLang(),
56
            'app_root' => Bootstrap::$WEBURI,
57
            'max_file_size' => $this->configData->getFilesAllowedSize(),
58
            'check_updates' => $checkStatus && $this->configData->isCheckUpdates(),
59
            'check_notices' => $checkStatus && $this->configData->isChecknotices(),
60
            'timezone' => date_default_timezone_get(),
61
            'debug' => DEBUG || $this->configData->isDebug(),
62
            'cookies_enabled' => $this->getCookiesEnabled(),
63
            'plugins' => $this->getPlugins(),
64
            'loggedin' => $this->session->isLoggedIn(),
65
            'authbasic_autologin' => $this->getAuthBasicAutologinEnabled(),
66
            'pk' => $this->getPublicKey(),
67
            'import_allowed_exts' => ImportService::ALLOWED_EXTS,
68
            'files_allowed_exts' => $this->configData->getFilesAllowedExts()
69
        ];
70
71
        $this->returnJsonResponseData($data);
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    private function getJsLang()
78
    {
79
        return require CONFIG_PATH . DIRECTORY_SEPARATOR . 'strings.js.inc';
80
    }
81
82
    /**
83
     * @return bool
84
     */
85
    private function getCookiesEnabled()
86
    {
87
        return $this->router->request()->cookies()->get(session_name()) !== null;
88
    }
89
90
    /**
91
     * @return array
92
     * @throws \SP\Core\Exceptions\ConstraintException
93
     * @throws \SP\Core\Exceptions\QueryException
94
     */
95
    private function getPlugins()
96
    {
97
        return $this->dic->get(PluginManager::class)->getEnabledPlugins();
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    private function getAuthBasicAutologinEnabled()
104
    {
105
        return Browser::getServerAuthUser() && $this->configData->isAuthBasicAutoLoginEnabled();
106
    }
107
108
    /**
109
     * @return string
110
     * @throws \SP\Core\Exceptions\FileNotFoundException
111
     * @throws \SP\Core\Exceptions\SPException
112
     */
113
    private function getPublicKey()
114
    {
115
        return $this->session->getPublicKey() ?: $this->dic->get(CryptPKI::class)->getPublicKey();
116
    }
117
}