Passed
Push — develop ( 06fb87...d66898 )
by Nikolay
23:43
created

ConfigClass::generateFail2BanJails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 1
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\Core\Asterisk\Configs\CoreConfigClass;
23
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
24
use ReflectionClass as ReflectionClassAlias;
25
26
class ConfigClass extends CoreConfigClass implements SystemConfigInterface, AsteriskConfigInterface,
27
                                                     RestAPIConfigInterface
28
{
29
    public const MODELS_EVENT_NEED_RELOAD = 'modelsEventNeedReload';
30
31
    public const MODELS_EVENT_CHANGE_DATA = 'modelsEventChangeData';
32
33
    public const CREATE_CRON_TASKS = 'createCronTasks';
34
35
    public const CREATE_NGINX_LOCATIONS = 'createNginxLocations';
36
37
    public const GENERATE_FAIL2BAN_JAILS = 'generateFail2BanJails';
38
39
    public const ON_AFTER_MODULE_DISABLE = 'onAfterModuleDisable';
40
41
    public const ON_AFTER_MODULE_ENABLE = 'onAfterModuleEnable';
42
43
    public const GET_MODULE_WORKERS = 'getModuleWorkers';
44
45
    public const GET_PBXCORE_REST_ADDITIONAL_ROUTES = 'getPBXCoreRESTAdditionalRoutes';
46
47
48
    /**
49
     * External module UniqueID
50
     */
51
    public string $moduleUniqueId;
52
53
    /**
54
     * Additional module directory
55
     */
56
    protected string $moduleDir;
57
58
    /**
59
     * ConfigClass constructor.
60
     *
61
     */
62
    public function __construct()
63
    {
64
        parent::__construct();
65
        // Get child class parameters and define module Dir and UniqueID
66
        $reflector        = new ReflectionClassAlias(static::class);
67
        $partsOfNameSpace = explode('\\', $reflector->getNamespaceName());
68
        if (count($partsOfNameSpace) === 3 && $partsOfNameSpace[0] === 'Modules') {
69
            $modulesDir           = $this->config->path('core.modulesDir');
70
            $this->moduleUniqueId = $partsOfNameSpace[1];
71
            $this->moduleDir      = $modulesDir . '/' . $this->moduleUniqueId;
72
        }
73
74
        $this->messages = [];
75
    }
76
77
    /**
78
     * Returns array of additional routes for the PBXCoreREST interface from module
79
     *
80
     * @return array
81
     */
82
    public function getPBXCoreRESTAdditionalRoutes(): array
83
    {
84
        return [];
85
    }
86
87
    /**
88
     * Process PBXCoreREST requests under root rights
89
     *
90
     * @param array $request
91
     *
92
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
93
     */
94
    public function moduleRestAPICallback(array $request): PBXApiResult
95
    {
96
        $res            = new PBXApiResult();
97
        $res->processor = __METHOD__;
98
        $action         = strtoupper($request['action']);
99
        switch ($action) {
100
            case 'CHECK':
101
                $res->success = true;
102
                break;
103
            default:
104
                $res->success    = false;
105
                $res->messages[] = 'API action not found in moduleRestAPICallback';
106
        }
107
108
        return $res;
109
    }
110
111
    /**
112
     * This method calls in the WorkerModelsEvents after receive each models change
113
     *
114
     * @param $data
115
     */
116
    public function modelsEventChangeData($data): void
117
    {
118
    }
119
120
    /**
121
     * This method calls in the WorkerModelsEvents after finished process models changing
122
     *
123
     * @param array $modified_tables list of modified models
124
     */
125
    public function modelsEventNeedReload(array $modified_tables): void
126
    {
127
    }
128
129
    /**
130
     * Returns array of workers classes for WorkerSafeScripts
131
     *
132
     * @return array
133
     */
134
    public function getModuleWorkers(): array
135
    {
136
        return [];
137
    }
138
139
    /**
140
     * Returns array of additional firewall rules for module
141
     *
142
     * @return array
143
     */
144
    public function getDefaultFirewallRules(): array
145
    {
146
        return [];
147
    }
148
149
    /**
150
     * Process module enable request
151
     *
152
     * @return bool
153
     */
154
    public function onBeforeModuleEnable(): bool
155
    {
156
        return true;
157
    }
158
159
    /**
160
     * Process some actions after module enable
161
     *
162
     * @return void
163
     */
164
    public function onAfterModuleEnable(): void
165
    {
166
    }
167
168
    /**
169
     * Process module disable request
170
     *
171
     * @return bool
172
     */
173
    public function onBeforeModuleDisable(): bool
174
    {
175
        return true;
176
    }
177
178
    /**
179
     * Process some actions after module disable
180
     *
181
     * @return void
182
     */
183
    public function onAfterModuleDisable(): void
184
    {
185
    }
186
187
    /**
188
     * Create additional Nginx locations from modules
189
     *
190
     * @return string
191
     */
192
    public function createNginxLocations(): string
193
    {
194
        return '';
195
    }
196
197
    /**
198
     * Generates additional fail2ban jail conf rules from modules
199
     *
200
     * @return string
201
     */
202
    public function generateFail2BanJails(): string
203
    {
204
        return '';
205
    }
206
207
    /**
208
     * Generates the modules.conf file
209
     *
210
     * @return string
211
     */
212
    public function generateModulesConf(): string
213
    {
214
        return '';
215
    }
216
217
    /**
218
     * Prepares crontab rules strings
219
     *
220
     * @param array $tasks
221
     */
222
    public function createCronTasks(&$tasks): void
223
    {
224
    }
225
226
    /**
227
     * This module's method calls after the asterisk service started
228
     */
229
    public function onAfterPbxStarted(): void
230
    {
231
    }
232
233
    /**
234
     * Makes pretty module text block into config file
235
     *
236
     * @param string $addition
237
     *
238
     * @return string
239
     */
240
    protected function confBlockWithComments(string $addition): string
241
    {
242
        $result = '';
243
        if (empty($addition)) {
244
            return $result;
245
        }
246
        $result = PHP_EOL . '; ***** BEGIN BY ' . $this->moduleUniqueId . PHP_EOL;
247
        $result .= $addition;
248
        if (substr($addition, -1) !== "\t") {
249
            $result .= "\t";
250
        }
251
        $result .= PHP_EOL . '; ***** END BY ' . $this->moduleUniqueId . PHP_EOL;
252
253
        return $result;
254
    }
255
}