Passed
Push — develop ( 2448c8...de9fda )
by Nikolay
06:00 queued 10s
created

PbxSettings::itHasVisualLanguageSettings()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * Copyright © MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Alexey Portnov, 12 2019
7
 */
8
9
namespace MikoPBX\Common\Models;
10
11
use Phalcon\Validation;
12
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
13
14
/**
15
 * Class PbxSettings
16
 *
17
 * @method static mixed findFirstByKey(string $string)
18
 *
19
 * @package MikoPBX\Common\Models
20
 */
21
class PbxSettings extends ModelsBase
22
{
23
24
    /**
25
     * @Primary
26
     * @Column(type="string", nullable=false)
27
     */
28
    public string $key;
29
30
    /**
31
     * @Column(type="string", nullable=true)
32
     */
33
    public ?string $value;
34
35
    /**
36
     * Значениея по умолчанию для переменных станции
37
     *
38
     * @return array - результат из базы или значение по умолчанию
39
     */
40
    public static function getAllPbxSettings(): array
41
    {
42
        $arrOfDefaultValues = self::getDefaultArrayValues();
43
        foreach ($arrOfDefaultValues as $key => $record) {
44
            $arrOfDefaultValues[$key] = self::getValueByKey($key);
45
        }
46
47
        return $arrOfDefaultValues;
48
    }
49
50
    /**
51
     * Значения по умолчанию для переменных станции
52
     *
53
     * @return array - значения по умолчанию
54
     */
55
    public static function getDefaultArrayValues(): array
56
    {
57
        return [
58
            'Name'                            => 'PBX system',
59
            'VirtualHardwareType'             => 'REAL',//VMWARE,HYPERV,AWS,AZURE
60
            'Description'                     => '',
61
            'RestartEveryNight'               => '0',
62
            'SIPPort'                         => '5060',
63
            'SIPDefaultExpiry'                => '120',
64
            'SIPMinExpiry'                    => '60',
65
            'SIPMaxExpiry'                    => '3600',
66
            'RTPPortFrom'                     => '10000',
67
            'RTPPortTo'                       => '10200',
68
            'IAXPort'                         => '4569',
69
            'AMIEnabled'                      => '1',
70
            'AMIPort'                         => '5038',
71
            'AJAMEnabled'                     => '1',
72
            'AJAMPort'                        => '8088',
73
            'AJAMPortTLS'                     => '8089',
74
            'SSHPort'                         => '22',
75
            'SSHPassword'                     => 'admin',
76
            'SSHRsaKey'                       => '',
77
            'SSHDssKey'                       => '',
78
            'SSHAuthorizedKeys'               => '',
79
            'SSHecdsaKey'                     => '',
80
            'SSHLanguage'                     => 'en',
81
            'WEBPort'                         => '80',
82
            'WEBHTTPSPort'                    => '443',
83
            'WEBHTTPSPublicKey'               => '',
84
            'WEBHTTPSPrivateKey'              => '',
85
            'RedirectToHttps'                 => '0',
86
            'MailSMTPUseTLS'                  => '0',
87
            'MailSMTPCertCheck'               => '0',
88
            'MailSMTPHost'                    => '',
89
            'MailSMTPPort'                    => '25',
90
            'MailSMTPUsername'                => '',
91
            'MailSMTPPassword'                => '',
92
            'MailSMTPFromUsername'            => 'PBX',
93
            'MailSMTPSenderAddress'           => '',
94
            'MailEnableNotifications'         => '0',
95
            'MailTplMissedCallSubject'        => 'You have missing call from <MailSMTPSenderAddress>',
96
            'MailTplMissedCallBody'           => 'You have missed calls (NOTIFICATION_MISSEDCAUSE) from <NOTIFICATION_CALLERID> at <NOTIFICATION_DATE>',
97
            'MailTplMissedCallFooter'         => '',
98
            'MailTplVoicemailSubject'         => 'VoiceMail from PBX',
99
            'MailTplVoicemailBody'            => 'See attach',
100
            'VoicemailNotificationsEmail'     => '[email protected]',
101
            'VoicemailExten'                  => '*001',
102
            'PBXLanguage'                     => 'en-en',
103
            'PBXInternalExtensionLength'      => '3',// Длина внутреннего номера
104
            'PBXRecordCalls'                  => '1',
105
            'PBXSplitAudioThread'             => '0',
106
            'PBXCallParkingExt'               => '800',
107
            'PBXCallParkingStartSlot'         => '801',
108
            'PBXCallParkingEndSlot'           => '820',
109
            'PBXFeatureAttendedTransfer'      => '##',
110
            'PBXFeatureBlindTransfer'         => '**',
111
            'PBXFeatureDigitTimeout'          => '2500',
112
            'PBXFeatureAtxferNoAnswerTimeout' => '45',
113
            'PBXFirewallEnabled'              => '0',
114
            'PBXFail2BanEnabled'              => '0',
115
            'PBXTimezone'                     => 'Europe/Moscow',
116
            'PBXVersion'                      => '1',
117
            'PickupExten'                     => '*8',
118
            'WebAdminLogin'                   => 'admin',
119
            'WebAdminPassword'                => 'admin',
120
            'WebAdminLanguage'                => 'en',
121
            'SystemNotificationsEmail'        => '[email protected]',
122
            'SendMetrics'                     => '1',
123
124
125
        ];
126
    }
127
128
    /**
129
     * Значение по умолчанию для переменных станции
130
     *
131
     * @param $key string - ключ значения
132
     *
133
     * @return string - результат из базы или значение по умолчанию
134
     */
135
    public static function getValueByKey(string $key): string
136
    {
137
138
        $result = parent::findFirstByKey($key);
139
        if ($result === null || $result->value === null) {
140
            $arrOfDefaultValues = self::getDefaultArrayValues();
141
            if ( ! array_key_exists($key, $arrOfDefaultValues)) {
142
                return '';
143
            }
144
145
            return $arrOfDefaultValues[$key];
146
        }
147
148
        return $result->value;
149
    }
150
151
    public function initialize(): void
152
    {
153
        $this->setSource('m_PbxSettings');
154
        parent::initialize();
155
    }
156
157
    public function validation(): bool
158
    {
159
        $validation = new Validation();
160
        $validation->add(
161
            'key',
162
            new UniquenessValidator(
163
                [
164
                    'message' => $this->t('mo_ThisKeyMustBeUniqueForPbxSettingsModels'),
165
                ]
166
            )
167
        );
168
169
        return $this->validate($validation);
170
    }
171
172
    public function afterSave(): void
173
    {
174
        parent::afterSave();
175
        if ($this->itHasFirewallParametersChanges()) {
176
            FirewallRules::updatePorts($this);
177
        }
178
    }
179
180
    /**
181
     * Проверяем на наличие параметров сетевых портов и firewall
182
     *
183
     * @return bool
184
     */
185
    public function itHasFirewallParametersChanges(): bool
186
    {
187
        switch ($this->key) {
188
            case 'SIPPort':
189
            case 'RTPPortFrom':
190
            case 'RTPPortTo':
191
            case 'IAXPort':
192
            case 'AMIPort':
193
            case 'AJAMPort':
194
            case 'AJAMPortTLS':
195
            case 'WEBPort':
196
            case 'WEBHTTPSPort':
197
            case 'SSHPort':
198
            case 'PBXFirewallEnabled':
199
            case 'PBXFail2BanEnabled':
200
                return true;
201
            default:
202
                if (strpos($this->key, 'FirewallSettings') !== false) {
203
                    return true;
204
                }
205
        }
206
207
        return false;
208
    }
209
210
    /**
211
     * Проверяем на наличие параметров SIP
212
     *
213
     * @return bool
214
     */
215
    public function itHasSipParametersChanges(): bool
216
    {
217
        switch ($this->key) {
218
            case 'SIPPort':
219
            case 'RTPPortFrom':
220
            case 'RTPPortTo':
221
            case 'SIPDefaultExpiry':
222
            case 'SIPMinExpiry':
223
            case 'SIPMaxExpiry':
224
            case 'PBXLanguage':
225
                return true;
226
            default:
227
                return false;
228
        }
229
    }
230
231
    /**
232
     * Проверяем на наличие параметров IAX портов
233
     *
234
     * @return bool
235
     */
236
    public function itHasIaxParametersChanges(): bool
237
    {
238
        switch ($this->key) {
239
            case 'IAXPort':
240
                return true;
241
            default:
242
                return false;
243
        }
244
    }
245
246
    /**
247
     * Проверяем на наличие параметров AMI AJAM портов
248
     *
249
     * @return bool
250
     */
251
    public function itHasAMIParametersChanges(): bool
252
    {
253
        switch ($this->key) {
254
            case 'AMIPort':
255
            case 'AJAMPort':
256
            case 'AJAMPortTLS':
257
                return true;
258
            default:
259
                return false;
260
        }
261
    }
262
263
    /**
264
     * Проверяем на наличие параметров изменения настроек features
265
     *
266
     * @return bool
267
     */
268
    public function itHasFeaturesSettingsChanges(): bool
269
    {
270
        switch ($this->key) {
271
            case 'PBXLanguage':
272
            case 'PBXInternalExtensionLength':
273
            case 'PBXRecordCalls':
274
            case 'PBXCallParkingExt':
275
            case 'PBXCallParkingStartSlot':
276
            case 'PBXCallParkingEndSlot':
277
            case 'PBXFeatureAttendedTransfer':
278
            case 'PBXFeatureBlindTransfer':
279
            case 'PBXFeatureDigitTimeout':
280
            case 'PBXFeatureAtxferNoAnswerTimeout':
281
                return true;
282
            default:
283
                return false;
284
        }
285
    }
286
287
    /**
288
     * Проверяем на наличие параметров SSH
289
     *
290
     * @return bool
291
     */
292
    public function itHasSSHParametersChanges(): bool
293
    {
294
        switch ($this->key) {
295
            case 'SSHPort':
296
            case 'SSHPassword':
297
            case 'SSHAuthorizedKeys':
298
            case 'SSHRsaKey':
299
            case 'SSHDssKey':
300
            case 'SSHecdsaKey':
301
                return true;
302
            default:
303
                return false;
304
        }
305
    }
306
307
    /**
308
     * Проверяем на наличие параметров Web
309
     *
310
     * @return bool
311
     */
312
    public function itHasWebParametersChanges(): bool
313
    {
314
        switch ($this->key) {
315
            case 'WEBPort':
316
            case 'WEBHTTPSPort':
317
            case 'WEBHTTPSPublicKey':
318
            case 'WEBHTTPSPrivateKey':
319
            case 'RedirectToHttps':
320
                return true;
321
            default:
322
                return false;
323
        }
324
    }
325
326
    /**
327
     * Проверяем на наличие параметров требующих перезапуска cron
328
     *
329
     * @return bool
330
     */
331
    public function itHasCronParametersChanges(): bool
332
    {
333
        switch ($this->key) {
334
            case 'RestartEveryNight':
335
                return true;
336
            default:
337
                return false;
338
        }
339
    }
340
341
    /**
342
     * Проверяем на наличие параметров требующих перезапуска dialplan
343
     *
344
     * @return bool
345
     */
346
    public function itHasDialplanParametersChanges(): bool
347
    {
348
        switch ($this->key) {
349
            case 'PBXLanguage':
350
                return true;
351
            default:
352
                return false;
353
        }
354
    }
355
356
    /**
357
     * Check if this changes influence to the voicemail.conf
358
     *
359
     * @return bool
360
     */
361
    public function itHasVoiceMailParametersChanges(): bool
362
    {
363
        switch ($this->key) {
364
            case 'MailTplVoicemailSubject':
365
            case 'MailTplVoicemailBody':
366
            case 'MailSMTPSenderAddress':
367
            case 'MailSMTPUsername':
368
            case 'PBXTimezone':
369
            case 'VoicemailNotificationsEmail':
370
            case 'SystemNotificationsEmail':
371
                return true;
372
            default:
373
                return false;
374
        }
375
    }
376
377
    /**
378
     * Check if this changes influence to translations
379
     *
380
     * @return bool
381
     */
382
    public function itHasVisualLanguageSettings(): bool
383
    {
384
        switch ($this->key) {
385
            case 'SSHLanguage':
386
            case 'WebAdminLanguage':
387
            return true;
388
            default:
389
                return false;
390
        }
391
    }
392
393
}