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, 9 2020 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MikoPBX\Core\System; |
10
|
|
|
|
11
|
|
|
use DateTime; |
12
|
|
|
use DateTimeZone; |
13
|
|
|
use MikoPBX\Common\Models\CustomFiles; |
14
|
|
|
use MikoPBX\Common\Models\PbxSettings; |
15
|
|
|
use MikoPBX\Core\System\Configs\CronConf; |
16
|
|
|
use MikoPBX\Core\System\Configs\IptablesConf; |
17
|
|
|
use MikoPBX\Core\System\Configs\PHPConf; |
18
|
|
|
use MikoPBX\Core\System\Configs\NTPConf; |
19
|
|
|
use MikoPBX\Core\Asterisk\Configs\{QueueConf}; |
20
|
|
|
use Phalcon\Di; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
class System extends Di\Injectable |
24
|
|
|
{ |
25
|
|
|
private MikoPBXConfig $mikoPBXConfig; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* System constructor |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->mikoPBXConfig = new MikoPBXConfig(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns logs dir |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public static function getLogDir(): string |
41
|
|
|
{ |
42
|
|
|
$di = Di::getDefault(); |
43
|
|
|
if ($di !== null) { |
44
|
|
|
return $di->getConfig()->path('core.logsDir'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return '/var/log'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Refresh networks configs and restarts network daemon |
52
|
|
|
*/ |
53
|
|
|
public static function networkReload(): void |
54
|
|
|
{ |
55
|
|
|
$network = new Network(); |
56
|
|
|
$network->hostnameConfigure(); |
57
|
|
|
$network->resolvConfGenerate(); |
58
|
|
|
$network->loConfigure(); |
59
|
|
|
$network->lanConfigure(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Updates custom changes in config files |
64
|
|
|
*/ |
65
|
|
|
public static function updateCustomFiles() |
66
|
|
|
{ |
67
|
|
|
$actions = []; |
68
|
|
|
/** @var \MikoPBX\Common\Models\CustomFiles $res_data */ |
69
|
|
|
$res_data = CustomFiles::find("changed = '1'"); |
70
|
|
|
foreach ($res_data as $file_data) { |
71
|
|
|
// Always restart asterisk after any custom file change |
72
|
|
|
$actions['asterisk_core_reload'] = 100; |
73
|
|
|
$filename = basename($file_data->filepath); |
74
|
|
|
switch ($filename) { |
75
|
|
|
case 'manager.conf': |
76
|
|
|
$actions['manager'] = 10; |
77
|
|
|
break; |
78
|
|
|
case 'musiconhold.conf': |
79
|
|
|
$actions['musiconhold'] = 100; |
80
|
|
|
break; |
81
|
|
|
case 'modules.conf': |
82
|
|
|
$actions['modules'] = 10; |
83
|
|
|
break; |
84
|
|
|
case 'http.conf': |
85
|
|
|
$actions['manager'] = 10; // |
86
|
|
|
break; |
87
|
|
|
case 'root': // crontabs |
88
|
|
|
$actions['cron'] = 10; |
89
|
|
|
break; |
90
|
|
|
case 'queues.conf': |
91
|
|
|
$actions['queues'] = 10; |
92
|
|
|
break; |
93
|
|
|
case 'features.conf': |
94
|
|
|
$actions['features'] = 10; |
95
|
|
|
break; |
96
|
|
|
case 'ntp.conf': |
97
|
|
|
$actions['ntp'] = 100; |
98
|
|
|
break; |
99
|
|
|
case 'jail.local': // fail2ban |
100
|
|
|
$actions['firewall'] = 100; |
101
|
|
|
break; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
asort($actions); |
106
|
|
|
self::invokeActions($actions); |
107
|
|
|
foreach ($res_data as $file_data) { |
108
|
|
|
/** @var \MikoPBX\Common\Models\CustomFiles $file_data */ |
109
|
|
|
$file_data->writeAttribute("changed", '0'); |
110
|
|
|
$file_data->save(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Batch module restart |
116
|
|
|
* |
117
|
|
|
* @param $actions |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
public static function invokeActions($actions): void |
121
|
|
|
{ |
122
|
|
|
foreach ($actions as $action => $value) { |
123
|
|
|
switch ($action) { |
124
|
|
|
case 'manager': |
125
|
|
|
PBX::managerReload(); |
126
|
|
|
break; |
127
|
|
|
case 'musiconhold': |
128
|
|
|
PBX::musicOnHoldReload(); |
129
|
|
|
break; |
130
|
|
|
case 'modules': |
131
|
|
|
PBX::modulesReload(); |
132
|
|
|
break; |
133
|
|
|
case 'cron': |
134
|
|
|
$cron = new CronConf(); |
135
|
|
|
$cron->reStart(); |
136
|
|
|
break; |
137
|
|
|
case 'queues': |
138
|
|
|
QueueConf::queueReload(); |
139
|
|
|
break; |
140
|
|
|
case 'features': |
141
|
|
|
PBX::managerReload(); // |
142
|
|
|
break; |
143
|
|
|
case 'ntp': |
144
|
|
|
NTPConf::configure(); |
145
|
|
|
break; |
146
|
|
|
case 'firewall': |
147
|
|
|
IptablesConf::reloadFirewall(); |
148
|
|
|
break; |
149
|
|
|
case 'asterisk_core_reload': |
150
|
|
|
PBX::sipReload(); |
151
|
|
|
PBX::iaxReload(); |
152
|
|
|
PBX::dialplanReload(); |
153
|
|
|
PBX::coreReload(); |
154
|
|
|
break; |
155
|
|
|
default: |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Setup system time |
162
|
|
|
* |
163
|
|
|
* @param int $timeStamp |
164
|
|
|
* @param string $remote_tz |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
* @throws \Exception |
168
|
|
|
*/ |
169
|
|
|
public static function setDate(int $timeStamp, string $remote_tz): bool |
170
|
|
|
{ |
171
|
|
|
$datePath = Util::which('date'); |
172
|
|
|
$db_tz = PbxSettings::getValueByKey('PBXTimezone'); |
173
|
|
|
$origin_tz = ''; |
174
|
|
|
if (file_exists('/etc/TZ')) { |
175
|
|
|
$origin_tz = file_get_contents("/etc/TZ"); |
176
|
|
|
} |
177
|
|
|
if ($origin_tz !== $db_tz){ |
178
|
|
|
self::timezoneConfigure(); |
179
|
|
|
} |
180
|
|
|
$origin_tz = $db_tz; |
181
|
|
|
$origin_dtz = new DateTimeZone($origin_tz); |
182
|
|
|
$remote_dtz = new DateTimeZone($remote_tz); |
183
|
|
|
$origin_dt = new DateTime('now', $origin_dtz); |
184
|
|
|
$remote_dt = new DateTime('now', $remote_dtz); |
185
|
|
|
$offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); |
186
|
|
|
$timeStamp = $timeStamp - $offset; |
187
|
|
|
Processes::mwExec("{$datePath} +%s -s @{$timeStamp}"); |
188
|
|
|
// Для 1 января должно быть передано 1577829662 |
189
|
|
|
// Установлено 1577818861 |
190
|
|
|
|
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Reboots the system after calling system_reboot_cleanup() |
196
|
|
|
*/ |
197
|
|
|
public static function rebootSync(): void |
198
|
|
|
{ |
199
|
|
|
$mikopbx_rebootPath = Util::which('mikopbx_reboot'); |
200
|
|
|
Processes::mwExec("{$mikopbx_rebootPath} > /dev/null 2>&1"); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Reboots the system after calling system_reboot_cleanup() |
205
|
|
|
*/ |
206
|
|
|
public static function rebootSyncBg(): void |
207
|
|
|
{ |
208
|
|
|
$mikopbx_rebootPath = Util::which('mikopbx_reboot'); |
209
|
|
|
Processes::mwExecBg("{$mikopbx_rebootPath} > /dev/null 2>&1"); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Shutdown the system |
214
|
|
|
*/ |
215
|
|
|
public static function shutdown(): void |
216
|
|
|
{ |
217
|
|
|
$shutdownPath = Util::which('shutdown'); |
218
|
|
|
Processes::mwExec("{$shutdownPath} > /dev/null 2>&1"); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Populates /etc/TZ with an appropriate time zone |
224
|
|
|
*/ |
225
|
|
|
public static function timezoneConfigure(): void |
226
|
|
|
{ |
227
|
|
|
$timezone = PbxSettings::getValueByKey('PBXTimezone'); |
228
|
|
|
if (file_exists('/etc/TZ')) { |
229
|
|
|
unlink("/etc/TZ"); |
230
|
|
|
} |
231
|
|
|
if (file_exists('/etc/localtime')) { |
232
|
|
|
unlink("/etc/localtime"); |
233
|
|
|
} |
234
|
|
|
if ($timezone) { |
235
|
|
|
$zone_file = "/usr/share/zoneinfo/{$timezone}"; |
236
|
|
|
if ( ! file_exists($zone_file)) { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
$cpPath = Util::which('cp'); |
240
|
|
|
Processes::mwExec("{$cpPath} {$zone_file} /etc/localtime"); |
241
|
|
|
file_put_contents('/etc/TZ', $timezone); |
242
|
|
|
putenv("TZ={$timezone}"); |
243
|
|
|
Processes::mwExec("export TZ;"); |
244
|
|
|
|
245
|
|
|
PHPConf::phpTimeZoneConfigure(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Loads additional kernel modules |
252
|
|
|
*/ |
253
|
|
|
public function loadKernelModules(): void |
254
|
|
|
{ |
255
|
|
|
$modprobePath = Util::which('modprobe'); |
256
|
|
|
$ulimitPath = Util::which('ulimit'); |
257
|
|
|
|
258
|
|
|
Processes::mwExec("{$modprobePath} -q dahdi"); |
259
|
|
|
Processes::mwExec("{$modprobePath} -q dahdi_transcode"); |
260
|
|
|
Processes::mwExec("{$ulimitPath} -n 4096"); |
261
|
|
|
Processes::mwExec("{$ulimitPath} -p 4096"); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Restart asterisk processor |
266
|
|
|
*/ |
267
|
|
|
public function onAfterPbxStarted(): void |
268
|
|
|
{ |
269
|
|
|
$additionalModules = $this->di->getShared('pbxConfModules'); |
270
|
|
|
foreach ($additionalModules as $appClass) { |
271
|
|
|
try { |
272
|
|
|
/** @var \MikoPBX\Modules\Config\ConfigClass $appClass */ |
273
|
|
|
$appClass->onAfterPbxStarted(); |
274
|
|
|
}catch (Throwable $e){ |
275
|
|
|
Util::sysLogMsg('onAfterPbxStarted', $e->getMessage()); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|