Passed
Push — develop ( d6f831...b3ddee )
by Nikolay
05:02
created

ConfigClass::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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\Modules\Config;
21
22
use MikoPBX\Common\Providers\ConfigProvider;
23
use MikoPBX\Core\System\MikoPBXConfig;
24
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
25
use Phalcon\Acl\Adapter\Memory as AclList;
26
use Phalcon\Config;
27
use Phalcon\Di\Injectable;
28
use Phalcon\Mvc\Router;
29
use ReflectionClass as ReflectionClassAlias;
30
31
class ConfigClass extends Injectable implements SystemConfigInterface,
32
                             RestAPIConfigInterface,
33
                             WebUIConfigInterface
34
{
35
36
    /**
37
     * Config file name i.e. extensions.conf
38
     */
39
    protected string $description;
40
41
    /**
42
     * Easy way to get or set the PbxSettings values
43
     *
44
     * @var \MikoPBX\Core\System\MikoPBXConfig
45
     */
46
    protected MikoPBXConfig $mikoPBXConfig;
47
48
    /**
49
     * Access to the /etc/inc/mikopbx-settings.json values
50
     *
51
     * @var \Phalcon\Config
52
     */
53
    protected Config $config;
54
55
56
    /**
57
     * Error and notice messages
58
     *
59
     * @var array
60
     */
61
    protected array $messages;
62
63
    /**
64
     * Array of PbxSettings values
65
     */
66
    protected array $generalSettings;
67
68
69
    /**
70
     * External module UniqueID
71
     */
72
    public string $moduleUniqueId;
73
74
    /**
75
     * Additional module directory
76
     */
77
    protected string $moduleDir;
78
79
    /**
80
     * ConfigClass constructor.
81
     *
82
     */
83
    public function __construct()
84
    {
85
        $this->config          = $this->di->getShared(ConfigProvider::SERVICE_NAME);
86
        $this->mikoPBXConfig   = new MikoPBXConfig();
87
        $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings();
88
        $this->messages        = [];
89
90
        // Get child class parameters and define module Dir and UniqueID
91
        $reflector        = new ReflectionClassAlias(static::class);
92
        $partsOfNameSpace = explode('\\', $reflector->getNamespaceName());
93
        if (count($partsOfNameSpace) === 3 && $partsOfNameSpace[0] === 'Modules') {
94
            $modulesDir           = $this->config->path('core.modulesDir');
95
            $this->moduleUniqueId = $partsOfNameSpace[1];
96
            $this->moduleDir      = $modulesDir . '/' . $this->moduleUniqueId;
97
        }
98
99
        $this->messages = [];
100
    }
101
102
    /**
103
     * Returns the messages variable
104
     *
105
     * @return array
106
     */
107
    public function getMessages(): array
108
    {
109
        return $this->messages;
110
    }
111
112
    /**
113
     * Returns array of additional routes for the PBXCoreREST interface from module
114
     *
115
     * @return array
116
     */
117
    public function getPBXCoreRESTAdditionalRoutes(): array
118
    {
119
        return [];
120
    }
121
122
    /**
123
     * Process PBXCoreREST requests under root rights
124
     *
125
     * @param array $request GET/POST parameters
126
     *
127
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
128
     */
129
    public function moduleRestAPICallback(array $request): PBXApiResult
130
    {
131
        $res            = new PBXApiResult();
132
        $res->processor = __METHOD__;
133
        $action         = strtoupper($request['action']);
134
        switch ($action) {
135
            case 'CHECK':
136
                $res->success = true;
137
                break;
138
            default:
139
                $res->success    = false;
140
                $res->messages[] = 'API action not found in moduleRestAPICallback';
141
        }
142
143
        return $res;
144
    }
145
146
    /**
147
     * This method calls in the WorkerModelsEvents after receive each models change
148
     *
149
     * @param $data
150
     */
151
    public function modelsEventChangeData($data): void
152
    {
153
    }
154
155
    /**
156
     * This method calls in the WorkerModelsEvents after finished process models changing
157
     *
158
     * @param array $modified_tables list of modified models
159
     */
160
    public function modelsEventNeedReload(array $modified_tables): void
161
    {
162
    }
163
164
    /**
165
     * Returns array of workers classes for WorkerSafeScripts
166
     *
167
     * @return array
168
     */
169
    public function getModuleWorkers(): array
170
    {
171
        return [];
172
    }
173
174
    /**
175
     * Returns array of additional firewall rules for module
176
     *
177
     * @return array
178
     */
179
    public function getDefaultFirewallRules(): array
180
    {
181
        return [];
182
    }
183
184
    /**
185
     * Process module enable request
186
     *
187
     * @return bool
188
     */
189
    public function onBeforeModuleEnable(): bool
190
    {
191
        return true;
192
    }
193
194
    /**
195
     * Process some actions after module enable
196
     *
197
     * @return void
198
     */
199
    public function onAfterModuleEnable(): void
200
    {
201
    }
202
203
    /**
204
     * Process module disable request
205
     *
206
     * @return bool
207
     */
208
    public function onBeforeModuleDisable(): bool
209
    {
210
        return true;
211
    }
212
213
    /**
214
     * Process some actions after module disable
215
     *
216
     * @return void
217
     */
218
    public function onAfterModuleDisable(): void
219
    {
220
    }
221
222
    /**
223
     * Create additional Nginx locations from modules
224
     *
225
     * @return string
226
     */
227
    public function createNginxLocations(): string
228
    {
229
        return '';
230
    }
231
232
    /**
233
     * Generates additional fail2ban jail conf rules from modules
234
     *
235
     * @return string
236
     */
237
    public function generateFail2BanJails(): string
238
    {
239
        return '';
240
    }
241
242
    /**
243
     * Generates the modules.conf file
244
     *
245
     * @return string
246
     */
247
    public function generateModulesConf(): string
248
    {
249
        return '';
250
    }
251
252
    /**
253
     * Prepares crontab rules strings
254
     *
255
     * @param array $tasks
256
     */
257
    public function createCronTasks(&$tasks): void
258
    {
259
    }
260
261
    /**
262
     * This module's method calls after the asterisk service started
263
     */
264
    public function onAfterPbxStarted(): void
265
    {
266
    }
267
268
    /**
269
     * Makes pretty module text block into config file
270
     *
271
     * @param string $addition
272
     *
273
     * @return string
274
     */
275
    protected function confBlockWithComments(string $addition): string
276
    {
277
        $result = '';
278
        if (empty($addition)) {
279
            return $result;
280
        }
281
        $result = PHP_EOL . '; ***** BEGIN BY ' . $this->moduleUniqueId . PHP_EOL;
282
        $result .= rtrim($addition);
283
        $result .= PHP_EOL . '; ***** END BY ' . $this->moduleUniqueId . PHP_EOL;
284
285
        return $result;
286
    }
287
288
289
    /**
290
     * Authenticates user over external module
291
     *
292
     * @param string $login
293
     * @param string $password
294
     * @return array session data
295
     */
296
    public function authenticateUser(string $login, string $password): array
297
    {
298
        return [];
299
    }
300
301
    /**
302
     * Prepares list of additional ACL roles and rules
303
     *
304
     * @param  AclList $aclList
305
     * @return void
306
     */
307
    public function onAfterACLPrepared(AclList $aclList): void
308
    {
309
    }
310
311
    /**
312
     * Modifies system menu
313
     *
314
     * @param array $menuItems
315
     * @return void
316
     */
317
    public function onBeforeHeaderMenuShow(array &$menuItems):void
318
    {
319
    }
320
321
    /**
322
     * Modifies system routes
323
     *
324
     * @param Router $router
325
     * @return void
326
     */
327
    public function onAfterRoutesPrepared(Router $router):void
328
    {
329
    }
330
331
}