Passed
Push — develop ( 7f2f11...c7ac81 )
by Портнов
04:20 queued 10s
created

PbxSettings::itHasCronParametersChanges()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
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\Common\Models;
21
22
use Phalcon\Validation;
23
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
24
25
/**
26
 * Class PbxSettings
27
 *
28
 * @method static mixed findFirstByKey(string $string)
29
 *
30
 * @package MikoPBX\Common\Models
31
 */
32
class PbxSettings extends ModelsBase
33
{
34
35
    /**
36
     * @Primary
37
     * @Column(type="string", nullable=false)
38
     */
39
    public string $key = '';
40
41
    /**
42
     * @Column(type="string", nullable=true)
43
     */
44
    public ?string $value = null;
45
46
    /**
47
     *  Returns default or saved values for all predefined keys if it exists on DB
48
     *
49
     * @return array
50
     */
51
    public static function getAllPbxSettings(): array
52
    {
53
        $arrayOfSettings = self::getDefaultArrayValues();
54
55
        $cacheKey        = explode('\\', static::class)[3];
56
        $parameters      = [
57
            'cache' => [
58
                'key'      => $cacheKey . '-getAllPbxSettings',
59
                'lifetime' => 300,
60
            ],
61
        ];
62
        $currentSettings = parent::find($parameters);
63
64
        foreach ($currentSettings as $record) {
65
            if (isset($record->value)) {
66
                $arrayOfSettings[$record->key] = $record->value;
67
            }
68
        }
69
70
        return $arrayOfSettings;
71
    }
72
73
    /**
74
     * Prepares default values for PbxSettings keys
75
     *
76
     * @return array default values
77
     */
78
    public static function getDefaultArrayValues(): array
79
    {
80
        return [
81
            'Name'                            => 'PBX system',
82
            'VirtualHardwareType'             => 'REAL',//VMWARE,HYPERV,AWS,AZURE
83
            'Description'                     => '',
84
            'RestartEveryNight'               => '0',
85
            'SIPPort'                         => '5060',
86
            'SIPDefaultExpiry'                => '120',
87
            'SIPMinExpiry'                    => '60',
88
            'SIPMaxExpiry'                    => '3600',
89
            'RTPPortFrom'                     => '10000',
90
            'RTPPortTo'                       => '10200',
91
            'IAXPort'                         => '4569',
92
            'AMIEnabled'                      => '1',
93
            'AMIPort'                         => '5038',
94
            'AJAMEnabled'                     => '1',
95
            'AJAMPort'                        => '8088',
96
            'AJAMPortTLS'                     => '8089',
97
            'SSHPort'                         => '22',
98
            'SSHPassword'                     => 'admin',
99
            'SSHRsaKey'                       => '',
100
            'SSHDssKey'                       => '',
101
            'SSHAuthorizedKeys'               => '',
102
            'SSHecdsaKey'                     => '',
103
            'SSHLanguage'                     => 'en',
104
            'WEBPort'                         => '80',
105
            'WEBHTTPSPort'                    => '443',
106
            'WEBHTTPSPublicKey'               => '',
107
            'WEBHTTPSPrivateKey'              => '',
108
            'RedirectToHttps'                 => '0',
109
            'MailSMTPUseTLS'                  => '0',
110
            'MailSMTPCertCheck'               => '0',
111
            'MailSMTPHost'                    => '',
112
            'MailSMTPPort'                    => '25',
113
            'MailSMTPUsername'                => '',
114
            'MailSMTPPassword'                => '',
115
            'MailSMTPFromUsername'            => 'PBX',
116
            'MailSMTPSenderAddress'           => '',
117
            'MailEnableNotifications'         => '0',
118
            'MailTplMissedCallSubject'        => 'You have missing call from <MailSMTPSenderAddress>',
119
            'MailTplMissedCallBody'           => 'You have missed calls (NOTIFICATION_MISSEDCAUSE) from <NOTIFICATION_CALLERID> at <NOTIFICATION_DATE>',
120
            'MailTplMissedCallFooter'         => '',
121
            'MailTplVoicemailSubject'         => 'VoiceMail from PBX',
122
            'MailTplVoicemailBody'            => 'See attach',
123
            'MailTplVoicemailFooter'          => '',
124
            'NTPServer'                       => '0.pool.ntp.org' . PHP_EOL . '1.pool.ntp.org' . PHP_EOL,
125
            'VoicemailNotificationsEmail'     => '[email protected]',
126
            'VoicemailExten'                  => '*001',
127
            'PBXLanguage'                     => 'en-en',
128
            'PBXInternalExtensionLength'      => '3',// Длина внутреннего номера
129
            'PBXRecordCalls'                  => '1',
130
            'PBXSplitAudioThread'             => '0',
131
            'PBXCallParkingExt'               => '800',
132
            'PBXCallParkingStartSlot'         => '801',
133
            'PBXCallParkingEndSlot'           => '820',
134
            'PBXFeatureAttendedTransfer'      => '##',
135
            'PBXFeatureBlindTransfer'         => '**',
136
            'PBXFeaturePickupExten'           => '*8',
137
            'PBXFeatureDigitTimeout'          => '2500',
138
            'PBXFeatureAtxferNoAnswerTimeout' => '45',
139
            'PBXFeatureTransferDigitTimeout'  => '3',
140
            'PBXFirewallEnabled'              => '0',
141
            'PBXFail2BanEnabled'              => '0',
142
            'PBXTimezone'                     => 'Europe/Moscow',
143
            'PBXVersion'                      => '2020.1.1',
144
            'PBXAllowGuestCalls'              => '0',
145
            'WebAdminLogin'                   => 'admin',
146
            'WebAdminPassword'                => 'admin',
147
            'WebAdminLanguage'                => 'en',
148
            'SystemNotificationsEmail'        => '[email protected]',
149
            'SendMetrics'                     => '1',
150
151
        ];
152
    }
153
154
    /**
155
     * Returns default or saved value for key if it exists on DB
156
     *
157
     * @param $key string value key
158
     *
159
     * @return string
160
     */
161
    public static function getValueByKey(string $key): string
162
    {
163
        $cacheKey        = explode('\\', static::class)[3];
164
        $parameters      = [
165
            'cache' => [
166
                'key'      => $cacheKey . '-getValueByKey',
167
                'lifetime' => 300,
168
            ],
169
        ];
170
        $currentSettings = parent::find($parameters);
171
172
        foreach ($currentSettings as $record) {
173
            if ($record->key === $key
174
                && isset($record->value)
175
            ) {
176
                return $record->value;
177
            }
178
        }
179
180
        $arrOfDefaultValues = self::getDefaultArrayValues();
181
        if (array_key_exists($key, $arrOfDefaultValues)) {
182
            return $arrOfDefaultValues[$key];
183
        }
184
185
        return '';
186
    }
187
188
    public function initialize(): void
189
    {
190
        $this->setSource('m_PbxSettings');
191
        parent::initialize();
192
    }
193
194
    /**
195
     * Before PbxSettings entity save callback
196
     *
197
     * @return bool
198
     */
199
    public function validation(): bool
200
    {
201
        $validation = new Validation();
202
        $validation->add(
203
            'key',
204
            new UniquenessValidator(
205
                [
206
                    'message' => $this->t('mo_ThisKeyMustBeUniqueForPbxSettingsModels'),
207
                ]
208
            )
209
        );
210
211
        return $this->validate($validation);
212
    }
213
214
    /**
215
     * After PbxSettings entity save callback
216
     */
217
    public function afterSave(): void
218
    {
219
        parent::afterSave();
220
        if ($this->itHasFirewallParametersChanges()) {
221
            FirewallRules::updatePorts($this);
222
        }
223
    }
224
225
    /**
226
     *  Check if this changes influence to the iptables daemon
227
     *
228
     * @return bool
229
     */
230
    public function itHasFirewallParametersChanges(): bool
231
    {
232
        switch ($this->key) {
233
            case 'SIPPort':
234
            case 'RTPPortFrom':
235
            case 'RTPPortTo':
236
            case 'IAXPort':
237
            case 'AMIPort':
238
            case 'AJAMPort':
239
            case 'AJAMPortTLS':
240
            case 'WEBPort':
241
            case 'WEBHTTPSPort':
242
            case 'SSHPort':
243
            case 'PBXFirewallEnabled':
244
            case 'PBXFail2BanEnabled':
245
                return true;
246
            default:
247
                if (strpos($this->key, 'FirewallSettings') !== false) {
248
                    return true;
249
                }
250
        }
251
252
        return false;
253
    }
254
255
}