Passed
Push — develop ( 4df8ba...2db068 )
by Nikolay
04:59
created

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