-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfs_cdr.php
127 lines (114 loc) · 4.64 KB
/
fs_cdr.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
<?php
/**
* @package FS_CURL
* @license BSD
* @author Raymond Chandler (intralanman) <[email protected]>
* @version 0.1
* Class for inserting xml CDR records
* @return object
*/
class fs_cdr extends fs_curl
{
/**
* This object is the objectified representation of the XML CDR
* @var SimpleXMLElement
*/
public $xml_cdr;
/**
* This array will hold the db field and their corresponding value
* @var array
*/
public $values = [];
/**
* This is where we instantiate our parent and set up our CDR object
*/
public function __construct()
{
parent::__construct();
$cdr = stripslashes($this->request['cdr']);
$this->xml_cdr = new SimpleXMLElement($cdr);
}
/**
* This is where we run the bulk of our logic through other methods
*/
public function main()
{
$this->set_record_values();
$this->insert_cdr();
}
/**
* This method will take the db fields and paths defined above and
* set the values array to be used for the insert
*/
public function set_record_values()
{
/**
* @var stdClass $xml
*/
$xml = $this->xml_cdr;
$callflow = is_array($xml->callflow) ? $xml->callflow[0] : $xml->callflow;
$caller_profile = $callflow->caller_profile;
$variables = $xml->variables;
$stats = $xml->{'call-stats'};
$aLeg = substr($this->request['uuid'], 0, 2) == 'a_';
// calc traffic
$inbound_bytes = 0;
$outbound_bytes = 0;
if (isset($stats->audio)) {
$inbound_bytes += (int)@$stats->audio->inbound->raw_bytes;
$outbound_bytes += (int)@$stats->audio->outbound->raw_bytes;
}
if (isset($stats->video)) {
$inbound_bytes += (int)@$stats->video->inbound->raw_bytes;
$outbound_bytes += (int)@$stats->video->outbound->raw_bytes;
}
$this->values = [
'is_aleg' => $aLeg ? 1 : 0,
'direction' => (string)$variables->direction,
'start_stamp' => urldecode((string)$variables->start_stamp),
'answer_stamp' => urldecode((string)$variables->answer_stamp) ?: null,
'end_stamp' => urldecode((string)$variables->end_stamp),
'duration' => (int)$variables->duration,
'billsec' => (int)$variables->billsec,
'hangup_cause' => (string)$variables->hangup_cause_q850,
'uuid' => (string)$variables->uuid,
'accountcode' => (string)$variables->accountcode ?: null,
'read_codec' => (string)$variables->read_codec ?: null,
'write_codec' => (string)$variables->write_codec ?: null,
'endpoint_disposition' => urldecode((string)$variables->endpoint_disposition) ?: null,
'dialstatus' => urldecode((string)$variables->DIALSTATUS) ?: null,
'inbound_bytes' => $inbound_bytes,
'outbound_bytes' => $outbound_bytes,
'sip_user_agent' => mb_substr(urldecode((string)$variables->sip_user_agent), 0, 512),
'originator_uuid' => (string)$variables->originator ?: null,
'last_bridge_role' => (string)$variables->last_bridge_role ?: null,
'call_record_id' => (string)$variables->call_record_id ?: null,
'caller_id_name' => urldecode((string)$variables->effective_caller_id_name) ?: (string)$caller_profile->caller_id_name,
'caller_id_number' => urldecode((string)$variables->effective_caller_id_number) ?: (string)$caller_profile->caller_id_number,
'context' => (string)$caller_profile->context,
];
if ($aLeg) {
$values = [
'username' => (string)$caller_profile->username ?: null,
'destination_number' => (string)$caller_profile->destination_number,
];
} else {
$values = [
'username' => (string)$variables->dialed_user ?: ((string)$caller_profile->callee_id_number ?: null),
];
}
$this->values += $values;
$this->debug($this->values);
}
/**
* finally do the insert of the CDR
*/
public function insert_cdr()
{
$keys = array_keys($this->values);
$query = sprintf("INSERT INTO cdr (%s) VALUES (:%s)", implode(', ', $keys), implode(', :', $keys));
$this->debug($query);
$statement = $this->db->prepare($query);
$statement->execute($this->values);
}
}