1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* MikoPBX - free phone system for small business |
5
|
|
|
* Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
18
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace MikoPBX\Common\Models; |
22
|
|
|
|
23
|
|
|
use MikoPBX\Common\Handlers\CriticalErrorsHandler; |
24
|
|
|
use MikoPBX\Common\Providers\ManagedCacheProvider; |
25
|
|
|
use Phalcon\Di\Di; |
26
|
|
|
use Phalcon\Filter\Validation; |
27
|
|
|
use Phalcon\Filter\Validation\Validator\Uniqueness as UniquenessValidator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class PbxSettings |
31
|
|
|
* |
32
|
|
|
* @method static mixed findFirstByKey(string $string) |
33
|
|
|
* |
34
|
|
|
* @package MikoPBX\Common\Models |
35
|
|
|
*/ |
36
|
|
|
class PbxSettings extends ModelsBase |
37
|
|
|
{ |
38
|
|
|
use PbxSettingsConstantsTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Key by which the value is stored |
42
|
|
|
* |
43
|
|
|
* @Primary |
44
|
|
|
* @Column(type="string", nullable=false) |
45
|
|
|
*/ |
46
|
|
|
public string $key = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Stored value |
50
|
|
|
* |
51
|
|
|
* @Column(type="string", nullable=true) |
52
|
|
|
*/ |
53
|
|
|
public ?string $value = null; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns default or saved values for all predefined keys if it exists on DB |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public static function getAllPbxSettings(): array |
61
|
|
|
{ |
62
|
|
|
$arrayOfSettings = self::getDefaultArrayValues(); |
63
|
|
|
$redis = Di::GetDefault()->getShared(ManagedCacheProvider::SERVICE_NAME); |
64
|
|
|
$cacheKey = ModelsBase::makeCacheKey(PbxSettings::class, 'getAllPbxSettings'); |
65
|
|
|
$currentSettings = $redis->get($cacheKey); |
66
|
|
|
if (empty($currentSettings)) { |
67
|
|
|
$currentSettings = parent::find(); |
68
|
|
|
$redis->set($cacheKey, $currentSettings, 3600); |
69
|
|
|
} |
70
|
|
|
foreach ($currentSettings as $record) { |
71
|
|
|
if (isset($record->value)) { |
72
|
|
|
$arrayOfSettings[$record->key] = $record->value; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $arrayOfSettings; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Prepares default values for PbxSettings keys |
81
|
|
|
* |
82
|
|
|
* @return array default values |
83
|
|
|
*/ |
84
|
|
|
public static function getDefaultArrayValues(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
PbxSettings::PBX_NAME => 'PBX system', |
88
|
|
|
PbxSettings::VIRTUAL_HARDWARE_TYPE => 'BARE METAL', |
89
|
|
|
PbxSettings::PBX_DESCRIPTION => '', |
90
|
|
|
PbxSettings::RESTART_EVERY_NIGHT => '0', |
91
|
|
|
PbxSettings::SIP_PORT => '5060', |
92
|
|
|
PbxSettings::EXTERNAL_SIP_PORT => '5060', |
93
|
|
|
PbxSettings::TLS_PORT => '5061', |
94
|
|
|
PbxSettings::EXTERNAL_TLS_PORT => '5061', |
95
|
|
|
PbxSettings::SIP_DEFAULT_EXPIRY => '120', |
96
|
|
|
PbxSettings::SIP_MIN_EXPIRY => '60', |
97
|
|
|
PbxSettings::SIP_MAX_EXPIRY => '3600', |
98
|
|
|
PbxSettings::RTP_PORT_FROM => '10000', |
99
|
|
|
PbxSettings::RTP_PORT_TO => '10200', |
100
|
|
|
PbxSettings::RTP_STUN_SERVER => '', |
101
|
|
|
PbxSettings::USE_WEB_RTC => '0', |
102
|
|
|
PbxSettings::IAX_PORT => '4569', |
103
|
|
|
PbxSettings::AMI_ENABLED => '1', |
104
|
|
|
PbxSettings::AMI_PORT => '5038', |
105
|
|
|
PbxSettings::AJAM_ENABLED => '1', |
106
|
|
|
PbxSettings::AJAM_PORT => '8088', |
107
|
|
|
PbxSettings::AJAM_PORT_TLS => '8089', |
108
|
|
|
PbxSettings::SSH_PORT => '22', |
109
|
|
|
PbxSettings::SSH_LOGIN => 'root', |
110
|
|
|
PbxSettings::SSH_PASSWORD => 'admin', |
111
|
|
|
PbxSettings::SSH_RSA_KEY => '', |
112
|
|
|
PbxSettings::SSH_DSS_KEY => '', |
113
|
|
|
PbxSettings::SSH_AUTHORIZED_KEYS => '', |
114
|
|
|
PbxSettings::SSH_ECDSA_KEY => '', |
115
|
|
|
PbxSettings::SSH_DISABLE_SSH_PASSWORD => '0', |
116
|
|
|
PbxSettings::SSH_LANGUAGE => 'en', |
117
|
|
|
PbxSettings::WEB_PORT => '80', |
118
|
|
|
PbxSettings::WEB_HTTPS_PORT => '443', |
119
|
|
|
PbxSettings::WEB_HTTPS_PUBLIC_KEY => '', |
120
|
|
|
PbxSettings::WEB_HTTPS_PRIVATE_KEY => '', |
121
|
|
|
PbxSettings::REDIRECT_TO_HTTPS => '0', |
122
|
|
|
PbxSettings::MAIL_SMTP_USE_TLS => '0', |
123
|
|
|
PbxSettings::MAIL_SMTP_CERT_CHECK => '0', |
124
|
|
|
PbxSettings::MAIL_SMTP_HOST => '', |
125
|
|
|
PbxSettings::MAIL_SMTP_PORT => '25', |
126
|
|
|
PbxSettings::MAIL_SMTP_USERNAME => '', |
127
|
|
|
PbxSettings::MAIL_SMTP_PASSWORD => '', |
128
|
|
|
PbxSettings::MAIL_SMTP_FROM_USERNAME => 'PBX', |
129
|
|
|
PbxSettings::MAIL_SMTP_SENDER_ADDRESS => '', |
130
|
|
|
PbxSettings::MAIL_ENABLE_NOTIFICATIONS => '0', |
131
|
|
|
PbxSettings::MAIL_TPL_MISSED_CALL_SUBJECT => 'You have missing call from <MailSMTPSenderAddress>', |
132
|
|
|
PbxSettings::MAIL_TPL_MISSED_CALL_BODY => 'You have missed calls (NOTIFICATION_MISSEDCAUSE) from <NOTIFICATION_CALLERID> at <NOTIFICATION_DATE>', |
133
|
|
|
PbxSettings::MAIL_TPL_MISSED_CALL_FOOTER => '', |
134
|
|
|
PbxSettings::MAIL_TPL_VOICEMAIL_SUBJECT => 'VoiceMail from PBX', |
135
|
|
|
PbxSettings::MAIL_TPL_VOICEMAIL_BODY => 'See attach', |
136
|
|
|
PbxSettings::MAIL_TPL_VOICEMAIL_FOOTER => '', |
137
|
|
|
PbxSettings::NTP_SERVER => '0.pool.ntp.org' . PHP_EOL . '1.pool.ntp.org' . PHP_EOL, |
138
|
|
|
PbxSettings::VOICEMAIL_NOTIFICATIONS_EMAIL => '[email protected]', |
139
|
|
|
PbxSettings::VOICEMAIL_EXTENSION => '*001', |
140
|
|
|
PbxSettings::PBX_LANGUAGE => 'en-en', |
141
|
|
|
PbxSettings::PBX_INTERNAL_EXTENSION_LENGTH => '3', |
142
|
|
|
PbxSettings::PBX_RECORD_CALLS => '1', |
143
|
|
|
PbxSettings::PBX_RECORD_CALLS_INNER => '1', |
144
|
|
|
PbxSettings::PBX_SPLIT_AUDIO_THREAD => '0', |
145
|
|
|
PbxSettings::PBX_RECORD_ANNOUNCEMENT_IN => '', |
146
|
|
|
PbxSettings::PBX_RECORD_ANNOUNCEMENT_OUT => '', |
147
|
|
|
PbxSettings::PBX_RECORD_SAVE_PERIOD => '', |
148
|
|
|
PbxSettings::PBX_CALL_PARKING_EXT => '800', |
149
|
|
|
PbxSettings::PBX_CALL_PARKING_FEATURE => '*2', |
150
|
|
|
PbxSettings::PBX_CALL_PARKING_DURATION => '50', |
151
|
|
|
PbxSettings::PBX_CALL_PARKING_START_SLOT => '801', |
152
|
|
|
PbxSettings::PBX_CALL_PARKING_END_SLOT => '820', |
153
|
|
|
PbxSettings::PBX_FEATURE_ATTENDED_TRANSFER => '##', |
154
|
|
|
PbxSettings::PBX_FEATURE_BLIND_TRANSFER => '**', |
155
|
|
|
PbxSettings::PBX_FEATURE_PICKUP_EXTEN => '*8', |
156
|
|
|
PbxSettings::PBX_FEATURE_DIGIT_TIMEOUT => '2500', |
157
|
|
|
PbxSettings::PBX_FEATURE_ATXFER_NO_ANSWER_TIMEOUT => '45', |
158
|
|
|
PbxSettings::PBX_FEATURE_TRANSFER_DIGIT_TIMEOUT => '3', |
159
|
|
|
PbxSettings::PBX_FIREWALL_ENABLED => '0', |
160
|
|
|
PbxSettings::PBX_FAIL2BAN_ENABLED => '0', |
161
|
|
|
PbxSettings::PBX_TIMEZONE => 'Europe/Moscow', |
162
|
|
|
PbxSettings::PBX_MANUAL_TIME_SETTINGS => '0', |
163
|
|
|
PbxSettings::PBX_VERSION => '2020.1.1', |
164
|
|
|
PbxSettings::PBX_ALLOW_GUEST_CALLS => '0', |
165
|
|
|
PbxSettings::WEB_ADMIN_LOGIN => 'admin', |
166
|
|
|
PbxSettings::WEB_ADMIN_PASSWORD => 'admin', |
167
|
|
|
PbxSettings::WEB_ADMIN_LANGUAGE => 'en', |
168
|
|
|
PbxSettings::SYSTEM_NOTIFICATIONS_EMAIL => '', |
169
|
|
|
PbxSettings::SYSTEM_EMAIL_FOR_MISSED => '', |
170
|
|
|
PbxSettings::SEND_METRICS => '1', |
171
|
|
|
PbxSettings::CLOUD_INSTANCE_ID => '', |
172
|
|
|
PbxSettings::DISABLE_ALL_MODULES => '0', |
173
|
|
|
PbxSettings::PBX_LICENSE => '', |
174
|
|
|
PbxSettings::ENABLE_USE_NAT => '0', |
175
|
|
|
PbxSettings::AUTO_UPDATE_EXTERNAL_IP => '0', |
176
|
|
|
PbxSettings::EXTERNAL_SIP_HOST_NAME => '', |
177
|
|
|
PbxSettings::EXTERNAL_SIP_IP_ADDR => '', |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns default or saved value for key if it exists on DB |
183
|
|
|
* |
184
|
|
|
* @param $key string value key |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
public static function getValueByKey(string $key): string |
189
|
|
|
{ |
190
|
|
|
try { |
191
|
|
|
$redis = Di::GetDefault()->getShared(ManagedCacheProvider::SERVICE_NAME); |
192
|
|
|
$cacheKey = ModelsBase::makeCacheKey(PbxSettings::class, 'getValueByKey'); |
193
|
|
|
$currentSettings = $redis->get($cacheKey); |
194
|
|
|
if (empty($currentSettings)) { |
195
|
|
|
$currentSettings = parent::find(); |
196
|
|
|
$redis->set($cacheKey, $currentSettings, 3600); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
foreach ($currentSettings as $record) { |
200
|
|
|
if ( |
201
|
|
|
$record->key === $key |
202
|
|
|
&& isset($record->value) |
203
|
|
|
) { |
204
|
|
|
return $record->value; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$arrOfDefaultValues = self::getDefaultArrayValues(); |
209
|
|
|
if (array_key_exists($key, $arrOfDefaultValues)) { |
210
|
|
|
return $arrOfDefaultValues[$key]; |
211
|
|
|
} |
212
|
|
|
} catch (\Throwable $e) { |
213
|
|
|
CriticalErrorsHandler::handleException($e); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return ''; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set value for a key |
221
|
|
|
* @param $key string settings key |
222
|
|
|
* @param $value string value |
223
|
|
|
* @param $messages array error messages |
224
|
|
|
* @return bool Whether the save was successful or not. |
225
|
|
|
*/ |
226
|
|
|
public static function setValue(string $key, string $value, array &$messages = []): bool |
227
|
|
|
{ |
228
|
|
|
$record = self::findFirstByKey($key); |
229
|
|
|
if ($record === null) { |
230
|
|
|
$record = new self(); |
231
|
|
|
$record->key = $key; |
232
|
|
|
} |
233
|
|
|
if (isset($record->value) && $record->value === $value) { |
234
|
|
|
return true; |
235
|
|
|
} |
236
|
|
|
$record->value = $value; |
237
|
|
|
$result = $record->save(); |
238
|
|
|
if (!$result) { |
239
|
|
|
$messages[] = $record->getMessages(); |
240
|
|
|
}else{ |
241
|
|
|
// Clean the cache in redis |
242
|
|
|
try { |
243
|
|
|
$redis = Di::GetDefault()->getShared(ManagedCacheProvider::SERVICE_NAME); |
244
|
|
|
foreach (['getValueByKey', 'getAllPbxSettings'] as $index){ |
245
|
|
|
$cacheKey = ModelsBase::makeCacheKey(PbxSettings::class, $index); |
246
|
|
|
$redis->set($cacheKey, '', 1); |
247
|
|
|
} |
248
|
|
|
} catch (\Throwable $e) { |
249
|
|
|
CriticalErrorsHandler::handleException($e); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
return $result; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Initialize the model. |
257
|
|
|
*/ |
258
|
|
|
public function initialize(): void |
259
|
|
|
{ |
260
|
|
|
$this->setSource('m_PbxSettings'); |
261
|
|
|
parent::initialize(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Perform validation on the model. |
266
|
|
|
* |
267
|
|
|
* @return bool Whether the validation was successful or not. |
268
|
|
|
*/ |
269
|
|
|
public function validation(): bool |
270
|
|
|
{ |
271
|
|
|
$validation = new Validation(); |
272
|
|
|
$validation->add( |
273
|
|
|
'key', |
274
|
|
|
new UniquenessValidator( |
275
|
|
|
[ |
276
|
|
|
'message' => $this->t('mo_ThisKeyMustBeUniqueForPbxSettingsModels'), |
277
|
|
|
] |
278
|
|
|
) |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
return $this->validate($validation); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|