Passed
Push — develop ( 82ab63...3ea9cd )
by Nikolay
05:40
created

WikiLinksController::customModuleWikiLinks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 3
nc 3
nop 1
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\AdminCabinet\Controllers;
21
22
use MikoPBX\Common\Models\PbxExtensionModules;
23
use MikoPBX\Common\Providers\ManagedCacheProvider;
24
use MikoPBX\Core\System\Util;
25
use Phalcon\Cache\Adapter\Redis;
26
use Exception;
27
use GuzzleHttp;
28
29
class WikiLinksController extends BaseController
30
{
31
    private const WIKI_LINKS_CACHE_KEY = 'WIKI_LINKS';
32
33
    private const WIKI_LINKS = '/var/etc/wiki-links-LANG.json';
34
35
    /**
36
     * Prepares array of new wiki links and return ajax answer
37
     *
38
     */
39
    public function getWikiLinksReplacementAction(): void
40
    {
41
        if ( ! $this->request->isPost()) {
42
            return;
43
        }
44
        $moduleUniqueId = $this->request->getPost('globalModuleUniqueId');
45
        if (!empty($moduleUniqueId)) {
46
            $this->customModuleWikiLinks($moduleUniqueId);
47
        } else {
48
            $this->customWikiLinks();
49
        }
50
51
    }
52
53
    /**
54
     * Customization of links to the wiki documentation.
55
     * @return void
56
     */
57
    private function customWikiLinks(): void
58
    {
59
        /** @var Redis $redis */
60
        $redis = $this->di->getShared(ManagedCacheProvider::SERVICE_NAME);
61
        $links = $redis->get(self::WIKI_LINKS_CACHE_KEY);
62
63
        if ($links === null) {
64
            $ttl = 86400;
65
            $client = new GuzzleHttp\Client();
66
            $url = 'https://raw.githubusercontent.com/mikopbx/Core/master/src/Common/WikiLinks/' . $this->language . '.json';
67
            try {
68
                $res = $client->request('GET', $url, ['timeout' => 5, 'connect_timeout' => 5, 'read_timeout' => 5]);
69
            } catch (GuzzleHttp\Exception\GuzzleException $e) {
70
                $res = null;
71
                $ttl = 3600;
72
                if ($e->getCode() !== 404) {
73
                    Util::sysLogMsg('WikiLinksController', 'Error access to raw.githubusercontent.com');
74
                }
75
            }
76
            $links = null;
77
            if ($res && $res->getStatusCode() === 200) {
78
                try {
79
                    $links = json_decode($res->getBody(), true, 512, JSON_THROW_ON_ERROR);
80
                } catch (Exception $e) {
81
                    $ttl = 3600;
82
                }
83
            }
84
            if (!is_array($links)) {
85
                $links = [];
86
            }
87
            $redis->set(self::WIKI_LINKS_CACHE_KEY, $links, $ttl);
88
        }
89
        if (empty($links)) {
90
            $filename = str_replace('LANG', $this->language, self::WIKI_LINKS);
91
            if (file_exists($filename)) {
92
                try {
93
                    $links = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR);
94
                } catch (\Exception $e) {
95
                    Util::sysLogMsg('WikiLinksController', $e->getMessage());
96
                }
97
            }
98
        }
99
        $this->view->success = true;
100
        $this->view->message = $links;
101
    }
102
103
    /**
104
     * Customization of links to the wiki documentation for modules.
105
     * @param string $uniqid
106
     * @return void
107
     */
108
    private function customModuleWikiLinks(string $uniqid): void
109
    {
110
        $module = PbxExtensionModules::findFirstByUniqid($uniqid);
111
        $links = [];
112
        if ($module!==null){
113
            try {
114
                $links = json_decode($module->wiki_links, true, 512, JSON_THROW_ON_ERROR);
115
            } catch (\JsonException $e) {
116
                Util::sysLogMsg(__CLASS__, $e->getMessage());
117
            }
118
        }
119
        $this->view->success = true;
120
        $this->view->message = $links[$this->language]??[];
121
    }
122
}