Passed
Push — devel-3.0 ( 5b8f21...739e49 )
by Rubén
03:22
created

BootstrapController::getEnvironmentAction()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 24
nop 0
dl 0
loc 25
rs 8.9777
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
use SP\Storage\File\FileException;
34
35
/**
36
 * Class BootstrapController
37
 *
38
 * @package SP\Modules\Web\Controllers
39
 */
40
final class BootstrapController extends SimpleControllerBase
41
{
42
    use JsonTrait;
43
44
    /**
45
     * Returns environment data
46
     */
47
    public function getEnvironmentAction()
48
    {
49
        $checkStatus = $this->session->getAuthCompleted()
50
            && ($this->session->getUserData()->getIsAdminApp() || $this->configData->isDemoEnabled());
51
52
        $data = [
53
            'lang' => $this->getJsLang(),
54
            'locale' => $this->configData->getSiteLang(),
55
            'app_root' => Bootstrap::$WEBURI,
56
            'max_file_size' => $this->configData->getFilesAllowedSize(),
57
            'check_updates' => $checkStatus && $this->configData->isCheckUpdates(),
58
            'check_notices' => $checkStatus && $this->configData->isChecknotices(),
59
            'timezone' => date_default_timezone_get(),
60
            'debug' => DEBUG || $this->configData->isDebug(),
61
            'cookies_enabled' => $this->getCookiesEnabled(),
62
            'plugins' => $this->getPlugins(),
63
            'loggedin' => $this->session->isLoggedIn(),
64
            'authbasic_autologin' => $this->getAuthBasicAutologinEnabled(),
65
            'pki_key' => $this->getPublicKey(),
66
            'pki_max_size' => CryptPKI::getMaxDataSize(),
67
            'import_allowed_exts' => ImportService::ALLOWED_EXTS,
68
            'files_allowed_exts' => $this->configData->getFilesAllowedExts()
69
        ];
70
71
        return $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
     */
93
    private function getPlugins()
94
    {
95
        try {
96
            return $this->dic->get(PluginManager::class)->getEnabledPlugins();
97
        } catch (\Exception $e) {
98
            processException($e);
99
        }
100
101
        return [];
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    private function getAuthBasicAutologinEnabled()
108
    {
109
        return $this->dic->get(Browser::class)->getServerAuthUser() !== null
110
            && $this->configData->isAuthBasicAutoLoginEnabled();
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    private function getPublicKey()
117
    {
118
        try {
119
            return $this->session->getPublicKey() ?: $this->dic->get(CryptPKI::class)->getPublicKey();
120
        } catch (FileException $e) {
121
            processException($e);
122
123
            return '';
124
        }
125
    }
126
}