Passed
Push — 3.0 ( e37bc1...f6f9cd )
by Rubén
04:03
created

ConfigWikiController::initialize()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 3
nop 0
dl 0
loc 12
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\Acl;
28
use SP\Core\Acl\UnauthorizedPageException;
29
use SP\Core\Events\Event;
30
use SP\Core\Events\EventMessage;
31
use SP\Http\JsonResponse;
32
use SP\Modules\Web\Controllers\Traits\ConfigTrait;
33
34
/**
35
 * Class ConfigWikiController
36
 *
37
 * @package SP\Modules\Web\Controllers
38
 */
39
final class ConfigWikiController extends SimpleControllerBase
40
{
41
    use ConfigTrait;
42
43
    /**
44
     * saveAction
45
     *
46
     * @throws \SP\Core\Exceptions\SPException
47
     */
48
    public function saveAction()
49
    {
50
        $this->checkSecurityToken($this->previousSk, $this->request);
51
52
        $eventMessage = EventMessage::factory();
53
        $configData = $this->config->getConfigData();
54
55
        // Wiki
56
        $wikiEnabled = $this->request->analyzeBool('wiki_enabled', false);
57
        $wikiSearchUrl = $this->request->analyzeString('wiki_searchurl');
58
        $wikiPageUrl = $this->request->analyzeString('wiki_pageurl');
59
        $wikiFilter = $this->request->analyzeString('wiki_filter');
60
61
        // Valores para la conexión a la Wiki
62
        if ($wikiEnabled && (!$wikiSearchUrl || !$wikiPageUrl || !$wikiFilter)) {
63
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Missing Wiki parameters'));
64
        }
65
66
        if ($wikiEnabled) {
67
            $configData->setWikiEnabled(true);
68
            $configData->setWikiSearchurl($wikiSearchUrl);
69
            $configData->setWikiPageurl($wikiPageUrl);
70
            $configData->setWikiFilter(explode(',', $wikiFilter));
71
72
            if ($configData->isWikiEnabled() === false) {
73
                $eventMessage->addDescription(__u('Wiki enabled'));
74
            }
75
        } elseif ($wikiEnabled === false && $configData->isWikiEnabled()) {
76
            $configData->setWikiEnabled(false);
77
78
            $eventMessage->addDescription(__u('Wiki disabled'));
79
        }
80
81
        return $this->saveConfig($configData, $this->config, function () use ($eventMessage) {
82
            $this->eventDispatcher->notifyEvent('save.config.wiki', new Event($this, $eventMessage));
83
        });
84
    }
85
86
    /**
87
     * @return bool
88
     */
89
    protected function initialize()
90
    {
91
        try {
92
            $this->checks();
93
            $this->checkAccess(Acl::CONFIG_WIKI);
94
        } catch (UnauthorizedPageException $e) {
95
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
96
97
            return $this->returnJsonResponseException($e);
98
        }
99
100
        return true;
101
    }
102
}