|
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\PBXCoreREST\Lib; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use MikoPBX\Common\Models\Iax; |
|
13
|
|
|
use MikoPBX\Core\System\Util; |
|
14
|
|
|
use Phalcon\Di\Injectable; |
|
15
|
|
|
|
|
16
|
|
|
class IAXStackProcessor extends Injectable |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Processes IAX requests |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $request |
|
22
|
|
|
* |
|
23
|
|
|
* @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function callBack(array $request): PBXApiResult |
|
26
|
|
|
{ |
|
27
|
|
|
$action = $request['action']; |
|
28
|
|
|
switch ($action) { |
|
29
|
|
|
case 'getRegistry': |
|
30
|
|
|
$res = IAXStackProcessor::getRegistry(); |
|
31
|
|
|
break; |
|
32
|
|
|
default: |
|
33
|
|
|
$res = new PBXApiResult(); |
|
34
|
|
|
$res->processor = __METHOD__; |
|
35
|
|
|
$res->messages[] = "Unknown action - {$action} in iaxCallBack"; |
|
36
|
|
|
break; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$res->function = $action; |
|
40
|
|
|
|
|
41
|
|
|
return $res; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Получение статусов регистраций IAX |
|
46
|
|
|
* |
|
47
|
|
|
* @return PBXApiResult |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getRegistry(): PBXApiResult |
|
50
|
|
|
{ |
|
51
|
|
|
$res = new PBXApiResult(); |
|
52
|
|
|
$res->processor = __METHOD__; |
|
53
|
|
|
$peers = []; |
|
54
|
|
|
$providers = Iax::find(); |
|
55
|
|
|
foreach ($providers as $provider) { |
|
56
|
|
|
$peers[] = [ |
|
57
|
|
|
'state' => 'OFF', |
|
58
|
|
|
'id' => $provider->uniqid, |
|
59
|
|
|
'username' => trim($provider->username), |
|
60
|
|
|
'host' => trim($provider->host), |
|
61
|
|
|
'noregister' => $provider->noregister, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (Iax::findFirst("disabled = '0'") !== null) { |
|
66
|
|
|
// Find them over AMI |
|
67
|
|
|
$am = Util::getAstManager('off'); |
|
68
|
|
|
$amiRegs = $am->IAXregistry(); // Registrations |
|
69
|
|
|
$amiPeers = $am->IAXpeerlist(); // Peers |
|
70
|
|
|
foreach ($amiPeers as $amiPeer) { |
|
71
|
|
|
$key = array_search($amiPeer['ObjectName'], array_column($peers, 'id'), true); |
|
72
|
|
|
if ($key !== false) { |
|
73
|
|
|
$currentPeer = &$peers[$key]; |
|
74
|
|
|
if ($currentPeer['noregister'] === '1') { |
|
75
|
|
|
// Пир без регистрации. |
|
76
|
|
|
$arr_status = explode(' ', $amiPeer['Status']); |
|
77
|
|
|
$currentPeer['state'] = strtoupper($arr_status[0]); |
|
78
|
|
|
$currentPeer['time-response'] = strtoupper(str_replace(['(', ')'], '', $arr_status[1])); |
|
79
|
|
|
} else { |
|
80
|
|
|
$currentPeer['state'] = 'Error register.'; |
|
81
|
|
|
// Parse active registrations |
|
82
|
|
|
foreach ($amiRegs as $reg) { |
|
83
|
|
|
if ( |
|
84
|
|
|
strcasecmp($reg['Addr'], $currentPeer['host']) === 0 |
|
85
|
|
|
&& strcasecmp($reg['Username'], $currentPeer['username']) === 0 |
|
86
|
|
|
) { |
|
87
|
|
|
$currentPeer['state'] = $reg['State']; |
|
88
|
|
|
break; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$res->data = $peers; |
|
97
|
|
|
$res->success = true; |
|
98
|
|
|
|
|
99
|
|
|
return $res; |
|
100
|
|
|
} |
|
101
|
|
|
} |