Passed
Push — develop ( e4768a...6d9d8a )
by Nikolay
04:23
created

ConfigClass::onAfterExecuteRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Assets\Manager;
27
use Phalcon\Config;
28
use Phalcon\Di\Injectable;
29
use Phalcon\Forms\Form;
30
use Phalcon\Mvc\Controller;
31
use Phalcon\Mvc\Router;
32
use Phalcon\Mvc\View;
33
use ReflectionClass as ReflectionClassAlias;
34
35
abstract class ConfigClass extends Injectable implements SystemConfigInterface,
36
                             RestAPIConfigInterface,
37
                             WebUIConfigInterface
38
{
39
    // The module hook applying priority
40
    protected int $priority = 10000;
41
42
    /**
43
     * Config file name i.e. extensions.conf
44
     */
45
    protected string $description;
46
47
    /**
48
     * Easy way to get or set the PbxSettings values
49
     *
50
     * @var \MikoPBX\Core\System\MikoPBXConfig
51
     */
52
    protected MikoPBXConfig $mikoPBXConfig;
53
54
    /**
55
     * Access to the /etc/inc/mikopbx-settings.json values
56
     *
57
     * @var \Phalcon\Config
58
     */
59
    protected Config $config;
60
61
62
    /**
63
     * Error and notice messages
64
     *
65
     * @var array
66
     */
67
    protected array $messages;
68
69
    /**
70
     * Array of PbxSettings values
71
     */
72
    protected array $generalSettings;
73
74
75
    /**
76
     * External module UniqueID
77
     */
78
    public string $moduleUniqueId;
79
80
    /**
81
     * Additional module directory
82
     */
83
    protected string $moduleDir;
84
85
    /**
86
     * ConfigClass constructor.
87
     *
88
     */
89
    public function __construct()
90
    {
91
        $this->config          = $this->di->getShared(ConfigProvider::SERVICE_NAME);
92
        $this->mikoPBXConfig   = new MikoPBXConfig();
93
        $this->generalSettings = $this->mikoPBXConfig->getGeneralSettings();
94
        $this->messages        = [];
95
96
        // Get child class parameters and define module Dir and UniqueID
97
        $reflector        = new ReflectionClassAlias(static::class);
98
        $partsOfNameSpace = explode('\\', $reflector->getNamespaceName());
99
        if (count($partsOfNameSpace) === 3 && $partsOfNameSpace[0] === 'Modules') {
100
            $modulesDir           = $this->config->path('core.modulesDir');
101
            $this->moduleUniqueId = $partsOfNameSpace[1];
102
            $this->moduleDir      = $modulesDir . '/' . $this->moduleUniqueId;
103
        }
104
105
        $this->messages = [];
106
    }
107
108
    /**
109
     * Allows overriding the execution priority of a method when called through hookModulesMethod.
110
     *
111
     * @param string $methodName
112
     * @return int
113
     */
114
    public function getMethodPriority(string $methodName=''):int
115
    {
116
        switch ($methodName){
117
            case SystemConfigInterface::CREATE_CRON_TASKS:
118
                //...
119
            default:
120
                $result = $this->priority;
121
        }
122
        return $result;
123
    }
124
125
    /**
126
     * Returns the messages variable
127
     *
128
     * @return array
129
     */
130
    public function getMessages(): array
131
    {
132
        return $this->messages;
133
    }
134
135
    /**
136
     * Returns array of additional routes for the PBXCoreREST interface from module
137
     *
138
     * @return array
139
     */
140
    public function getPBXCoreRESTAdditionalRoutes(): array
141
    {
142
        return [];
143
    }
144
145
    /**
146
     * Process PBXCoreREST requests under root rights
147
     *
148
     * @param array $request GET/POST parameters
149
     *
150
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
151
     */
152
    public function moduleRestAPICallback(array $request): PBXApiResult
153
    {
154
        $res            = new PBXApiResult();
155
        $res->processor = __METHOD__;
156
        $action         = strtoupper($request['action']);
157
        switch ($action) {
158
            case 'CHECK':
159
                $res->success = true;
160
                break;
161
            default:
162
                $res->success    = false;
163
                $res->messages[] = 'API action not found in moduleRestAPICallback';
164
        }
165
166
        return $res;
167
    }
168
169
    /**
170
     * This method calls in the WorkerModelsEvents after receive each models change
171
     *
172
     * @param $data
173
     */
174
    public function modelsEventChangeData($data): void
175
    {
176
    }
177
178
    /**
179
     * This method calls in the WorkerModelsEvents after finished process models changing
180
     *
181
     * @param array $modified_tables list of modified models
182
     */
183
    public function modelsEventNeedReload(array $modified_tables): void
184
    {
185
    }
186
187
    /**
188
     * Returns array of workers classes for WorkerSafeScripts
189
     *
190
     * @return array
191
     */
192
    public function getModuleWorkers(): array
193
    {
194
        return [];
195
    }
196
197
    /**
198
     * Returns array of additional firewall rules for module
199
     *
200
     * @return array
201
     */
202
    public function getDefaultFirewallRules(): array
203
    {
204
        return [];
205
    }
206
207
    /**
208
     * Process module enable request
209
     *
210
     * @return bool
211
     */
212
    public function onBeforeModuleEnable(): bool
213
    {
214
        return true;
215
    }
216
217
    /**
218
     * Process some actions after module enable
219
     *
220
     * @return void
221
     */
222
    public function onAfterModuleEnable(): void
223
    {
224
    }
225
226
    /**
227
     * Process module disable request
228
     *
229
     * @return bool
230
     */
231
    public function onBeforeModuleDisable(): bool
232
    {
233
        return true;
234
    }
235
236
    /**
237
     * Process some actions after module disable
238
     *
239
     * @return void
240
     */
241
    public function onAfterModuleDisable(): void
242
    {
243
    }
244
245
    /**
246
     * Create additional Nginx locations from modules
247
     *
248
     * @return string
249
     */
250
    public function createNginxLocations(): string
251
    {
252
        return '';
253
    }
254
255
    /**
256
     * Generates additional fail2ban jail conf rules from modules
257
     *
258
     * @return string
259
     */
260
    public function generateFail2BanJails(): string
261
    {
262
        return '';
263
    }
264
265
    /**
266
     * Generates the modules.conf file
267
     *
268
     * @return string
269
     */
270
    public function generateModulesConf(): string
271
    {
272
        return '';
273
    }
274
275
    /**
276
     * Prepares crontab rules strings
277
     *
278
     * @param array $tasks
279
     */
280
    public function createCronTasks(&$tasks): void
281
    {
282
    }
283
284
    /**
285
     * This module's method calls after the asterisk service started
286
     */
287
    public function onAfterPbxStarted(): void
288
    {
289
    }
290
291
292
    /**
293
     * Authenticates user over external module
294
     *
295
     * @param string $login
296
     * @param string $password
297
     * @return array session data
298
     */
299
    public function authenticateUser(string $login, string $password): array
300
    {
301
        return [];
302
    }
303
304
    /**
305
     * Prepares list of additional ACL roles and rules
306
     *
307
     * @param  AclList $aclList
308
     * @return void
309
     */
310
    public function onAfterACLPrepared(AclList $aclList): void
311
    {
312
    }
313
314
    /**
315
     * Modifies system menu
316
     *
317
     * @param array $menuItems
318
     * @return void
319
     */
320
    public function onBeforeHeaderMenuShow(array &$menuItems):void
321
    {
322
    }
323
324
    /**
325
     * Modifies system routes
326
     *
327
     * @param Router $router
328
     * @return void
329
     */
330
    public function onAfterRoutesPrepared(Router $router):void
331
    {
332
    }
333
334
    /**
335
     * Modifies system assets
336
     *
337
     * @param Manager $assets
338
     * @return void
339
     */
340
    public function onAfterAssetsPrepared(Manager $assets):void
341
    {
342
    }
343
344
    /**
345
     * Prepares include block within volt template
346
     *
347
     * @param string $controller
348
     * @param string $blockName
349
     * @param View $view
350
     * @return string
351
     */
352
    public function onVoltBlockCompile(string $controller, string $blockName, View $view):string
353
    {
354
        return '';
355
    }
356
357
    /**
358
     * Calls from BaseForm before form initialized
359
     *
360
     * @param Form $form
361
     * @param $entity
362
     * @param $options
363
     * @return void
364
     */
365
    public function onBeforeFormInitialize(Form $form, $entity, $options):void
366
    {
367
    }
368
369
370
    /**
371
     * Calls from BaseController on afterExecuteRoute function
372
     *
373
     * @param Controller $controller
374
     * @return void
375
     */
376
    public function onAfterExecuteRoute(Controller $controller):void
377
    {
378
    }
379
380
    /**
381
     * Calls from BaseController on beforeExecuteRoute function
382
     *
383
     * @param Controller $controller
384
     * @return void
385
     */
386
    public function onBeforeExecuteRoute(Controller $controller):void
387
    {
388
389
    }
390
}