Passed
Push — 3.0 ( 690da2...c040a7 )
by Rubén
03:49
created

ConfigManagerController::getBackupConfig()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 20
nop 0
dl 0
loc 35
rs 8.9137
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\Acl;
28
use SP\Core\AppInfoInterface;
29
use SP\Core\Crypt\CryptSessionHandler;
30
use SP\Core\Events\Event;
31
use SP\Core\Language;
32
use SP\Core\MimeTypes;
33
use SP\Modules\Web\Controllers\Helpers\TabsHelper;
34
use SP\Mvc\View\Components\DataTab;
35
use SP\Mvc\View\Components\SelectItemAdapter;
36
use SP\Plugin\PluginManager;
37
use SP\Providers\Auth\Ldap\LdapTypeInterface;
38
use SP\Providers\Log\LogInterface;
39
use SP\Providers\Mail\MailHandler;
40
use SP\Services\Account\AccountService;
41
use SP\Services\Backup\FileBackupService;
42
use SP\Services\Config\ConfigService;
43
use SP\Services\Crypt\TemporaryMasterPassService;
44
use SP\Services\Export\XmlExportService;
45
use SP\Services\Task\Task;
46
use SP\Services\User\UserService;
47
use SP\Services\UserGroup\UserGroupService;
48
use SP\Services\UserProfile\UserProfileService;
49
use SP\Storage\Database\DatabaseUtil;
50
use SP\Storage\File\FileException;
51
use SP\Storage\File\FileHandler;
52
use SP\Util\Util;
53
54
/**
55
 * Class ConfigManagerController
56
 *
57
 * @package SP\Modules\Web\Controllers
58
 */
59
final class ConfigManagerController extends ControllerBase
60
{
61
    /**
62
     * @var TabsHelper
63
     */
64
    protected $tabsHelper;
65
66
    /**
67
     * @throws \Psr\Container\ContainerExceptionInterface
68
     * @throws \Psr\Container\NotFoundExceptionInterface
69
     * @throws \SP\Core\Exceptions\SPException
70
     */
71
    public function indexAction()
72
    {
73
        $this->getTabs();
74
    }
75
76
    /**
77
     * Returns a tabbed grid with items
78
     *
79
     * @throws \Psr\Container\ContainerExceptionInterface
80
     * @throws \Psr\Container\NotFoundExceptionInterface
81
     * @throws \SP\Core\Exceptions\SPException
82
     */
83
    protected function getTabs()
84
    {
85
        $this->tabsHelper = $this->dic->get(TabsHelper::class);
86
87
        if ($this->checkAccess(Acl::CONFIG_GENERAL)) {
88
            $this->tabsHelper->addTab($this->getConfigGeneral());
89
        }
90
91
        if ($this->checkAccess(Acl::CONFIG_ACCOUNT)) {
92
            $this->tabsHelper->addTab($this->getAccountConfig());
93
        }
94
95
        if ($this->checkAccess(Acl::CONFIG_WIKI)) {
96
            $this->tabsHelper->addTab($this->getWikiConfig());
97
        }
98
99
        if ($this->checkAccess(Acl::CONFIG_LDAP)) {
100
            $this->tabsHelper->addTab($this->getLdapConfig());
101
        }
102
103
        if ($this->checkAccess(Acl::CONFIG_MAIL)) {
104
            $this->tabsHelper->addTab($this->getMailConfig());
105
        }
106
107
        if ($this->checkAccess(Acl::CONFIG_CRYPT)) {
108
            $this->tabsHelper->addTab($this->getEncryptionConfig());
109
        }
110
111
        if ($this->checkAccess(Acl::CONFIG_BACKUP)) {
112
            $this->tabsHelper->addTab($this->getBackupConfig());
113
        }
114
115
        if ($this->checkAccess(Acl::CONFIG_IMPORT)) {
116
            $this->tabsHelper->addTab($this->getImportConfig());
117
        }
118
119
        if ($this->checkAccess(Acl::CONFIG_GENERAL)) {
120
            $this->tabsHelper->addTab($this->getInfo());
121
        }
122
123
124
        $this->eventDispatcher->notifyEvent('show.config', new Event($this));
125
126
        $this->tabsHelper->renderTabs(Acl::getActionRoute(Acl::CONFIG), $this->request->analyzeInt('tabIndex', 0));
127
128
        $this->view();
129
    }
130
131
    /**
132
     * @return DataTab
133
     * @throws \Psr\Container\ContainerExceptionInterface
134
     * @throws \Psr\Container\NotFoundExceptionInterface
135
     * @throws \SP\Core\Exceptions\CheckException
136
     */
137
    protected function getConfigGeneral()
138
    {
139
        $template = clone $this->view;
140
        $template->setBase('config');
141
        $template->addTemplate('general');
142
143
        $template->assign('langs', SelectItemAdapter::factory(Language::getAvailableLanguages())->getItemsFromArraySelected([$this->configData->getSiteLang()]));
144
        $template->assign('themes', SelectItemAdapter::factory($this->theme->getThemesAvailable())->getItemsFromArraySelected([$this->configData->getSiteTheme()]));
145
        $template->assign('isDemoMode', $this->configData->isDemoEnabled() && !$this->userData->getIsAdminApp());
146
        $template->assign('isDisabled', $this->configData->isDemoEnabled() && !$this->userData->getIsAdminApp() ? 'disabled' : '');
147
148
        $template->assign('users', SelectItemAdapter::factory(UserService::getItemsBasic())->getItemsFromModel());
149
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
150
        $template->assign('userProfiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
151
152
        $template->assign('curlIsAvailable', $this->extensionChecker->checkCurlAvailable());
153
154
        $events = array_merge(LogInterface::EVENTS, $this->configData->getLogEvents());
155
156
        sort($events, SORT_STRING);
157
158
        $template->assign('logEvents', SelectItemAdapter::factory($events)
159
            ->getItemsFromArraySelected($this->configData->getLogEvents(), true)
160
        );
161
162
        return new DataTab(__('General'), $template);
163
    }
164
165
    /**
166
     * @return DataTab
167
     * @throws \DI\DependencyException
168
     * @throws \DI\NotFoundException
169
     * @throws \SP\Core\Exceptions\CheckException
170
     * @throws \SP\Core\Exceptions\SPException
171
     */
172
    protected function getAccountConfig()
173
    {
174
        $template = clone $this->view;
175
        $template->setBase('config');
176
        $template->addTemplate('accounts');
177
        $template->assign('gdIsAvailable', $this->extensionChecker->checkGdAvailable());
178
179
        $mimeTypesAvailable = array_map(function ($value) {
180
            return $value['type'];
181
        }, $this->dic->get(MimeTypes::class)->getMimeTypes());
182
183
        $mimeTypes = SelectItemAdapter::factory(
184
            array_merge($mimeTypesAvailable, $this->configData->getFilesAllowedMime())
185
        );
186
187
        $template->assign('mimeTypes', $mimeTypes->getItemsFromArraySelected(
188
            $this->configData->getFilesAllowedMime(),
189
            true)
190
        );
191
192
        return new DataTab(__('Accounts'), $template);
193
    }
194
195
    /**
196
     * @return DataTab
197
     * @throws \SP\Core\Exceptions\CheckException
198
     */
199
    protected function getWikiConfig()
200
    {
201
        $template = clone $this->view;
202
        $template->setBase('config');
203
        $template->addTemplate('wiki');
204
205
        $template->assign('curlIsAvailable', $this->extensionChecker->checkCurlAvailable());
206
207
        return new DataTab(__('Wiki'), $template);
208
    }
209
210
    /**
211
     * @return DataTab
212
     * @throws \Psr\Container\ContainerExceptionInterface
213
     * @throws \Psr\Container\NotFoundExceptionInterface
214
     * @throws \SP\Core\Exceptions\CheckException
215
     */
216
    protected function getLdapConfig()
217
    {
218
        $template = clone $this->view;
219
        $template->setBase('config');
220
        $template->addTemplate('ldap');
221
222
        $template->assign('ldapIsAvailable', $this->extensionChecker->checkIsAvailable('ldap'));
223
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
224
        $template->assign('userProfiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
225
226
        $serverTypes = [
227
            LdapTypeInterface::LDAP_STD => 'Standard',
228
            LdapTypeInterface::LDAP_ADS => 'Active Directory',
229
            LdapTypeInterface::LDAP_AZURE => 'Azure Active Directory',
230
        ];
231
232
        $template->assign('serverTypes', SelectItemAdapter::factory($serverTypes)->getItemsFromArraySelected([$this->configData->getLdapType()]));
233
234
        return new DataTab(__('LDAP'), $template);
235
    }
236
237
    /**
238
     * @return DataTab
239
     * @throws \Psr\Container\ContainerExceptionInterface
240
     * @throws \Psr\Container\NotFoundExceptionInterface
241
     */
242
    protected function getMailConfig()
243
    {
244
        $template = clone $this->view;
245
        $template->setBase('config');
246
        $template->addTemplate('mail');
247
248
        $template->assign('mailSecurity', ['SSL', 'TLS']);
249
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
250
        $template->assign('userProfiles', SelectItemAdapter::factory(UserProfileService::getItemsBasic())->getItemsFromModel());
251
252
        $events = array_merge(MailHandler::EVENTS, $this->configData->getMailEvents());
253
254
        sort($events, SORT_STRING);
255
256
        $template->assign('mailEvents', SelectItemAdapter::factory($events)
257
            ->getItemsFromArraySelected($this->configData->getMailEvents(), true)
258
        );
259
260
        return new DataTab(__('Mail'), $template);
261
    }
262
263
    /**
264
     * @return DataTab
265
     * @throws \DI\DependencyException
266
     * @throws \DI\NotFoundException
267
     * @throws \SP\Core\Exceptions\ConstraintException
268
     * @throws \SP\Core\Exceptions\QueryException
269
     * @throws \SP\Repositories\NoSuchItemException
270
     * @throws \SP\Services\ServiceException
271
     */
272
    protected function getEncryptionConfig()
273
    {
274
        $template = clone $this->view;
275
        $template->setBase('config');
276
        $template->addTemplate('encryption');
277
278
        $numAccounts = $this->dic->get(AccountService::class)->getTotalNumAccounts();
279
        $template->assign('numAccounts', $numAccounts);
280
281
        if ($numAccounts > 100) {
282
            $template->assign('taskId', Task::genTaskId('masterpass'));
283
        }
284
285
        $configService = $this->dic->get(ConfigService::class);
286
287
        $template->assign('lastUpdateMPass', $configService->getByParam('lastupdatempass', 0));
288
289
        $template->assign('tempMasterPassTime', $configService->getByParam(TemporaryMasterPassService::PARAM_TIME, 0));
290
        $template->assign('tempMasterMaxTime', $configService->getByParam(TemporaryMasterPassService::PARAM_MAX_TIME, 0));
291
292
        $tempMasterAttempts = sprintf('%d/%d',
293
            $configService->getByParam(TemporaryMasterPassService::PARAM_ATTEMPTS, 0),
294
            TemporaryMasterPassService::MAX_ATTEMPTS);
295
296
        $template->assign('tempMasterAttempts', $tempMasterAttempts);
297
        $template->assign('tempMasterPass', $this->session->getTemporaryMasterPass());
298
299
        $template->assign('userGroups', SelectItemAdapter::factory(UserGroupService::getItemsBasic())->getItemsFromModel());
300
301
        return new DataTab(__('Encryption'), $template);
302
    }
303
304
    /**
305
     * @return DataTab
306
     * @throws \SP\Core\Exceptions\CheckException
307
     */
308
    protected function getBackupConfig()
309
    {
310
        $template = clone $this->view;
311
        $template->setBase('config');
312
        $template->addTemplate('backup');
313
        $template->assign('pharIsAvailable', $this->extensionChecker->checkPharAvailable());
314
315
        $template->assign('siteName', AppInfoInterface::APP_NAME);
316
317
        $backupAppFile = new FileHandler(FileBackupService::getAppBackupFilename(BACKUP_PATH, $this->configData->getBackupHash() ?: '', true));
318
        $backupDbFile = new FileHandler(FileBackupService::getDbBackupFilename(BACKUP_PATH, $this->configData->getBackupHash() ?: '', true));
319
        $exportFile = new FileHandler(XmlExportService::getExportFilename(BACKUP_PATH, $this->configData->getExportHash() ?: '', true));
320
321
        try {
322
            $backupAppFile->checkFileExists();
323
            $backupDbFile->checkFileExists();
324
325
            $template->assign('hasBackup', true);
326
            $template->assign('lastBackupTime', date('r', $backupAppFile->getFileTime()));
327
        } catch (FileException $e) {
328
            $template->assign('hasBackup', false);
329
            $template->assign('lastBackupTime', __('There aren\'t any backups available'));
330
        }
331
332
        try {
333
            $exportFile->checkFileExists();
334
335
            $template->assign('hasExport', true);
336
            $template->assign('lastExportTime', date('r', $exportFile->getFileTime()));
337
        } catch (FileException $e) {
338
            $template->assign('hasExport', false);
339
            $template->assign('lastExportTime', __('No export file found'));
340
        }
341
342
        return new DataTab(__('Backup'), $template);
343
    }
344
345
    /**
346
     * @return DataTab
347
     * @throws \Psr\Container\ContainerExceptionInterface
348
     * @throws \Psr\Container\NotFoundExceptionInterface
349
     */
350
    protected function getImportConfig()
351
    {
352
        $template = clone $this->view;
353
        $template->setBase('config');
354
        $template->addTemplate('import');
355
356
        $template->assign('userGroups', SelectItemAdapter::factory(
357
            UserGroupService::getItemsBasic())
358
            ->getItemsFromModelSelected([$this->userData->getUserGroupId()])
359
        );
360
        $template->assign('users', SelectItemAdapter::factory(
361
            UserService::getItemsBasic())
362
            ->getItemsFromModelSelected([$this->userData->getId()])
363
        );
364
365
        return new DataTab(__('Import Accounts'), $template);
366
    }
367
368
    /**
369
     * @return DataTab
370
     * @throws \DI\DependencyException
371
     * @throws \DI\NotFoundException
372
     * @throws \SP\Repositories\NoSuchItemException
373
     * @throws \SP\Services\ServiceException
374
     */
375
    protected function getInfo()
376
    {
377
        $template = clone $this->view;
378
        $template->setBase('config');
379
        $template->addTemplate('info');
380
381
        $databaseUtil = $this->dic->get(DatabaseUtil::class);
382
383
        $template->assign('dbInfo', $databaseUtil->getDBinfo());
384
        $template->assign('dbName', $this->configData->getDbName() . '@' . $this->configData->getDbHost());
385
        $template->assign('configBackupDate', date('r', $this->dic->get(ConfigService::class)->getByParam('config_backup_date', 0)));
386
        $template->assign('plugins', $this->dic->get(PluginManager::class)->getLoadedPlugins());
387
        $template->assign('locale', Language::$localeStatus ?: sprintf('%s (%s)', $this->configData->getSiteLang(), __('Not installed')));
388
        $template->assign('securedSession', CryptSessionHandler::$isSecured);
389
        $template->assign('missingExtensions', $this->extensionChecker->getMissing());
390
        $template->assign('downloadRate', round(Util::getMaxDownloadChunk() / 1024 / 1024));
391
392
        $isDemo = $this->configData->isDemoEnabled();
393
394
        $template->assign('downloadConfigBackup', !$isDemo && $this->userData->getIsAdminApp());
395
        $template->assign('downloadLog', !$isDemo && is_readable(LOG_FILE) && $this->userData->getIsAdminApp());
396
397
        return new DataTab(__('Information'), $template);
398
    }
399
400
    /**
401
     * @return TabsHelper
402
     */
403
    public function getTabsHelper()
404
    {
405
        return $this->tabsHelper;
406
    }
407
408
    /**
409
     * @throws \Psr\Container\ContainerExceptionInterface
410
     * @throws \Psr\Container\NotFoundExceptionInterface
411
     * @throws \SP\Services\Auth\AuthException
412
     */
413
    protected function initialize()
414
    {
415
        $this->checkLoggedIn();
416
    }
417
}