/
WorkerCallEvents.php
338 lines (315 loc) · 10.8 KB
/
WorkerCallEvents.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/*
* MikoPBX - free phone system for small business
* Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/
namespace MikoPBX\Core\Workers;
require_once 'Globals.php';
use MikoPBX\Core\System\{BeanstalkClient, Storage, Util};
use MikoPBX\Common\Models\Extensions;
use MikoPBX\Common\Models\PbxSettings;
use MikoPBX\Common\Models\Sip;
use MikoPBX\Core\Asterisk\Configs\CelConf;
use MikoPBX\Core\Workers\Libs\WorkerCallEvents\ActionCelAnswer;
use MikoPBX\Core\Workers\Libs\WorkerCallEvents\SelectCDR;
use MikoPBX\Core\Workers\Libs\WorkerCallEvents\UpdateDataInDB;
use Phalcon\Text;
class WorkerCallEvents extends WorkerBase
{
public array $mixMonitorChannels = [];
protected bool $record_calls = true;
protected bool $split_audio_thread = false;
public array $checkChanHangupTransfer = [];
private array $activeChannels = [];
private array $innerNumbers = [];
private array $exceptionsNumbers = [];
private bool $notRecInner = false;
public const REC_DISABLE = 'Conversation recording is disabled';
/**
* Наполняем кэш реальных каналов.
* @param string $channel
* @return void
*/
public function addActiveChan(string $channel):void
{
if(stripos($channel, 'local') === 0){
return;
}
$this->activeChannels[$channel] = true;
}
/**
* Очищаем кэш реальных каналов.
* @param string $channel
* @return void
*/
public function removeActiveChan(string $channel):void
{
unset($this->activeChannels[$channel]);
}
/**
* Проверяет существует ли канал в кэш.
* @param string $channel
* @return void
*/
public function existsActiveChan(string $channel):bool
{
return isset($this->activeChannels[$channel]);
}
/**
* @param string $src
* @param string $dst
* @param string $error
* @return bool
*/
public function enableMonitor(string $src, string $dst):bool
{
$src = substr($src,-9);
$dst = substr($dst,-9);
$enable = true;
$isInner = in_array($src, $this->innerNumbers,true) && in_array($dst, $this->innerNumbers,true);
if(($this->notRecInner && $isInner) ||
in_array($src, $this->exceptionsNumbers,true) || in_array($dst, $this->exceptionsNumbers,true)){
$enable = false;
}
return $enable;
}
/**
* Инициирует запись разговора на канале.
*
* @param string $channel
* @param ?string $file_name
* @param ?string $sub_dir
* @param ?string $full_name
*
* @return string
*/
public function MixMonitor($channel, $file_name = null, $sub_dir = null, $full_name = null, string $actionID=''): string
{
$resFile = $this->mixMonitorChannels[$channel]??'';
if($resFile !== ''){
return $resFile;
}
$resFile = '';
$file_name = str_replace('/', '_', $file_name);
if ($this->record_calls) {
[$f, $options] = $this->setMonitorFilenameOptions($full_name, $sub_dir, $file_name);
$arr = $this->am->GetChannels(false);
if(!in_array($channel, $arr, true)){
return '';
}
$srcFile = "{$f}.wav";
$resFile = "{$f}.mp3";
$this->am->MixMonitor($channel, $srcFile, $options, '', $actionID);
$this->mixMonitorChannels[$channel] = $resFile;
$this->am->UserEvent('StartRecording', ['recordingfile' => $resFile, 'recchan' => $channel]);
}
return $resFile;
}
/**
* @param string|null $full_name
* @param string|null $sub_dir
* @param string|null $file_name
* @return array
*/
public function setMonitorFilenameOptions(?string $full_name, ?string $sub_dir, ?string $file_name): array{
if (!file_exists((string)$full_name)) {
$monitor_dir = Storage::getMonitorDir();
if ($sub_dir === null) {
$sub_dir = date('Y/m/d/H/');
}
$f = "{$monitor_dir}/{$sub_dir}{$file_name}";
} else {
$f = Util::trimExtensionForFile($full_name);
}
if ($this->split_audio_thread) {
$options = "abSr({$f}_in.wav)t({$f}_out.wav)";
} else {
$options = 'ab';
}
return array($f, $options);
}
/**
* Останавливает запись разговора на канале.
* @param string $channel
* @param string $actionID
*/
public function StopMixMonitor($channel, string $actionID=''): void
{
if(isset($this->mixMonitorChannels[$channel])){
unset($this->mixMonitorChannels[$channel]);
}else{
return;
}
if ($this->record_calls) {
$this->am->StopMixMonitor($channel, $actionID);
}
}
/**
*
* @param $params
*/
public function start($params): void
{
$this->updateRecordingOptions();
$this->mixMonitorChannels = [];
$this->checkChanHangupTransfer = [];
$this->am = Util::getAstManager('off');
// PID сохраняем при начале работы Worker.
$client = new BeanstalkClient(self::class);
if($client->isConnected() === false){
Util::sysLogMsg(self::class, 'Fail connect to beanstalkd...');
sleep(2);
return;
}
$client->subscribe(CelConf::BEANSTALK_TUBE, [$this, 'callEventsWorker']);
$client->subscribe(self::class, [$this, 'otherEvents']);
$client->subscribe(WorkerCdr::SELECT_CDR_TUBE, [$this, 'selectCDRWorker']);
$client->subscribe(WorkerCdr::UPDATE_CDR_TUBE, [$this, 'updateCDRWorker']);
$client->subscribe($this->makePingTubeName(self::class), [$this, 'pingCallBack']);
$client->setErrorHandler([$this, 'errorHandler']);
while ($this->needRestart === false) {
$client->wait();
}
}
/**
* @return void
*/
private function updateRecordingOptions():void
{
$usersNumbers = [];
$users = [];
$filter = [
'conditions' => 'userid <> "" and userid>0 ',
'columns' => 'userid,number,type',
'order' => 'type DESC'
];
$extensionsData = Extensions::find($filter);
/** @var Extensions $extension */
foreach ($extensionsData as $extension){
if($extension->type === "SIP"){
$usersNumbers[$extension->number][] = $extension->number;
$users[$extension->userid] = $extension->number;
}else{
$internalNumber = $users[$extension->userid]??'';
if($internalNumber !==''){
$usersNumbers[$internalNumber][] = $extension->number;
}
}
}
unset($users, $extensionsData);
$filter = [
'conditions' => 'type="peer"',
'columns' => 'extension,enableRecording',
];
$peers = Sip::find($filter);
foreach ($peers as $peer) {
$numbers = $usersNumbers[$peer->extension]??[];
foreach ($numbers as $num){
$num = substr($num,-9);
$this->innerNumbers[] = $num;
if($peer->enableRecording === '0'){
$this->exceptionsNumbers[] = $num;
}
}
}
$this->notRecInner = PbxSettings::getValueByKey('PBXRecordCallsInner') === '0';
$this->record_calls = PbxSettings::getValueByKey('PBXRecordCalls') === '1';
$this->split_audio_thread = PbxSettings::getValueByKey('PBXSplitAudioThread') === '1';
}
/**
* Ping callback for keep alive check
*
* @param BeanstalkClient $message
*/
public function pingCallBack(BeanstalkClient $message): void
{
parent::pingCallBack($message);
$this->updateRecordingOptions();
}
/**
* @param $tube
* @param $data
* @return void
*/
public function otherEvents($tube, array $data=[]): void
{
if(empty($data)){
$data = json_decode($tube->getBody(), true);
}
$funcName = "Action_".$data['action']??'';
if ( method_exists($this, $funcName) ) {
$this->$funcName($data);
}
$className = __NAMESPACE__.'\Libs\WorkerCallEvents\\'.Text::camelize($funcName, '_');
if( method_exists($className, 'execute') ){
$className::execute($this, $data);
}
}
/**
* Обработчик событий изменения состояния звонка.
*
* @param array | BeanstalkClient $tube
*/
public function callEventsWorker($tube): void
{
$data = json_decode($tube->getBody(), true);
$event = $data['EventName']??'';
if('ANSWER' === $event){
ActionCelAnswer::execute($this, $data);
return;
}elseif('USER_DEFINED' !== $event){
return;
}
try {
$data = json_decode(
base64_decode($data['AppData']??''),
true,
512,
JSON_THROW_ON_ERROR
);
}catch (\Throwable $e){
$data = [];
}
$this->otherEvents($tube, $data);
}
/**
* Получения CDR к обработке.
*
* @param array | BeanstalkClient $tube
*/
public function updateCDRWorker($tube): void
{
$task = $tube->getBody();
$data = json_decode($task, true);
UpdateDataInDB::execute($data);
$tube->reply(json_encode(true));
}
/**
* @param array | BeanstalkClient $tube
*/
public function selectCDRWorker($tube): void
{
$filter = json_decode($tube->getBody(), true);
$res_data = SelectCDR::execute($filter);
$tube->reply($res_data);
}
public function errorHandler($m): void
{
Util::sysLogMsg(self::class . '_ERROR', $m, LOG_ERR);
}
}
// Start worker process
WorkerCallEvents::startWorker($argv??null);