|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright © 2017-2023 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\PBXCoreREST\Lib; |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
use MikoPBX\Core\System\BeanstalkClient; |
|
24
|
|
|
use MikoPBX\Core\System\Util; |
|
25
|
|
|
use MikoPBX\Core\Workers\WorkerCdr; |
|
26
|
|
|
use Phalcon\Di\Injectable; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class CdrDBProcessor |
|
30
|
|
|
* |
|
31
|
|
|
* @package MikoPBX\PBXCoreREST\Lib |
|
32
|
|
|
* |
|
33
|
|
|
*/ |
|
34
|
|
|
class CdrDBProcessor extends Injectable |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Processes CDR table requests |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $request |
|
40
|
|
|
* |
|
41
|
|
|
* @return PBXApiResult An object containing the result of the API call. |
|
42
|
|
|
* |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function callBack(array $request): PBXApiResult |
|
45
|
|
|
{ |
|
46
|
|
|
$res = new PBXApiResult(); |
|
47
|
|
|
$res->processor = __METHOD__; |
|
48
|
|
|
$action = $request['action']; |
|
49
|
|
|
switch ($action) { |
|
50
|
|
|
case 'getActiveChannels': |
|
51
|
|
|
$res = self::getActiveChannels(); |
|
52
|
|
|
break; |
|
53
|
|
|
case 'getActiveCalls': |
|
54
|
|
|
$res = self::getActiveCalls(); |
|
55
|
|
|
break; |
|
56
|
|
|
default: |
|
57
|
|
|
$res->messages[] = "Unknown action - {$action} in cdrCallBack"; |
|
58
|
|
|
break; |
|
59
|
|
|
} |
|
60
|
|
|
$res->function = $action; |
|
61
|
|
|
return $res; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get active channels. These are the unfinished calls (endtime IS NULL). |
|
66
|
|
|
* |
|
67
|
|
|
* @return PBXApiResult An object containing the result of the API call. |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function getActiveChannels(): PBXApiResult |
|
70
|
|
|
{ |
|
71
|
|
|
$res = new PBXApiResult(); |
|
72
|
|
|
$res->processor = __METHOD__; |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
$res->success = true; |
|
76
|
|
|
|
|
77
|
|
|
$filter = [ |
|
78
|
|
|
'endtime=""', |
|
79
|
|
|
'order' => 'id', |
|
80
|
|
|
'columns' => 'start,answer,src_chan,dst_chan,src_num,dst_num,did,linkedid', |
|
81
|
|
|
'miko_tmp_db' => true, |
|
82
|
|
|
'miko_result_in_file' => true, |
|
83
|
|
|
]; |
|
84
|
|
|
$client = new BeanstalkClient(WorkerCdr::SELECT_CDR_TUBE); |
|
85
|
|
|
$message = $client->request(json_encode($filter), 2); |
|
86
|
|
|
if ($message === false) { |
|
87
|
|
|
$res->data = []; |
|
88
|
|
|
} else { |
|
89
|
|
|
$am = Util::getAstManager('off'); |
|
90
|
|
|
$active_chans = $am->GetChannels(true); |
|
91
|
|
|
$result_data = []; |
|
92
|
|
|
|
|
93
|
|
|
$result = json_decode($message); |
|
94
|
|
|
if (file_exists($result)) { |
|
95
|
|
|
$data = json_decode(file_get_contents($result), true); |
|
96
|
|
|
unlink($result); |
|
97
|
|
|
foreach ($data as $row) { |
|
98
|
|
|
if (!isset($active_chans[$row['linkedid']])) { |
|
99
|
|
|
// The call no longer exists. |
|
100
|
|
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
if (empty($row['dst_chan']) && empty($row['src_chan'])) { |
|
103
|
|
|
// This is an erroneous situation. Ignore such a call. |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
$channels = $active_chans[$row['linkedid']]; |
|
107
|
|
|
if ((empty($row['src_chan']) || in_array($row['src_chan'], $channels)) |
|
108
|
|
|
&& (empty($row['dst_chan']) || in_array($row['dst_chan'], $channels))) { |
|
109
|
|
|
$result_data[] = $row; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
$res->data = $result_data; |
|
114
|
|
|
} |
|
115
|
|
|
} catch (\Throwable $e) { |
|
116
|
|
|
$res->success = false; |
|
117
|
|
|
$res->messages[] = $e->getMessage(); |
|
118
|
|
|
} |
|
119
|
|
|
return $res; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get active calls based on CDR data. |
|
124
|
|
|
* |
|
125
|
|
|
* @return PBXApiResult An object containing the result of the API call. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function getActiveCalls(): PBXApiResult |
|
128
|
|
|
{ |
|
129
|
|
|
$res = new PBXApiResult(); |
|
130
|
|
|
$res->processor = __METHOD__; |
|
131
|
|
|
$res->success = true; |
|
132
|
|
|
$filter = [ |
|
133
|
|
|
'order' => 'id', |
|
134
|
|
|
'columns' => 'start,answer,endtime,src_num,dst_num,did,linkedid', |
|
135
|
|
|
'miko_tmp_db' => true, |
|
136
|
|
|
]; |
|
137
|
|
|
$client = new BeanstalkClient(WorkerCdr::SELECT_CDR_TUBE); |
|
138
|
|
|
$message = $client->request(json_encode($filter), 2); |
|
139
|
|
|
if ($message === false) { |
|
140
|
|
|
$res->data = []; |
|
141
|
|
|
}else{ |
|
142
|
|
|
$res->data[] = $message; |
|
143
|
|
|
} |
|
144
|
|
|
return $res; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |