-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathukcallback.php
246 lines (195 loc) · 6.85 KB
/
ukcallback.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
<?php
define("SERVER", "server-fqdn");
define("AUDIOFOLDER", "ukcallbackaudio/");
define("SUPERUSER", "username");
define("PASSWORD", "password");
define("CLIENTID", "clientid");
define("CLIENTSECRET", "clientsecret");
define("DIALDIGIT", "3");
define("ANONYMOUS", "anonymous");
define("HOURCLOCK", 24); // 12 or 24 hour clock
session_start();
header("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';
if (!isset($_REQUEST["Digits"]))
{
// this case is no digits are entered - so initial entry into web responder
$http_response = "";
// get NS-API token
$token = __getToken();
// get caller user and domain
$user=$_REQUEST["AccountUser"];
$domain=$_REQUEST["AccountDomain"];
// NS-API data structure to get last call
$query = array(
'object' => 'cdr2',
'action'=> 'read',
'uid' => "{$user}@{$domain}",
'type' => 'Inbound',
'limit' => '1',
'format' => 'json',
);
// do API call and decode json
$lastcall = __doCurl("https://".SERVER."/ns-api/", CURLOPT_POST, "Authorization: Bearer " . $token, $query, null, $http_response);
$lastcall = json_decode($lastcall,true);
// create random temp file name and path
$tempfile = mt_rand() . ".wav";
$tempfileuri = AUDIOFOLDER ."tmp/" . $tempfile;
// gets the actual caller number and call epoch time from the data returned from the API call
$caller = $lastcall[0]["number"];
$callerepoch = $lastcall[0]["time_start"];
// get user data to get their timezone
$query = array(
'object' => 'subscriber',
'action'=> 'read',
'uid' => "{$user}@{$domain}",
'format' => 'json',
);
$userinfo = __doCurl("https://".SERVER."/ns-api/", CURLOPT_POST, "Authorization: Bearer " . $token, $query, null, $http_response);
$userinfo = json_decode($userinfo,true);
// set timezone to user value
date_default_timezone_set($userinfo[0]["time_zone"]);
// convert epoch to format like Friday July 17 5 09
if (HOURCLOCK == 24)
{
$calldatetime = date('l F j G i', $callerepoch);
}
else
{
$calldatetime = date('l F j g i a', $callerepoch);
}
// turn the datetime string into an array to process later into wav files
$datetimearray = explode(' ', $calldatetime);
// few corner case checks before we continue...
if (substr($caller,0,1) == "+")
{
// remove leading +
$caller = substr($caller,1);
}
else if ($caller == ANONYMOUS)
{
// handle anonymous last caller
echo "<Play> http://" . SERVER . "/" . AUDIOFOLDER . "anon.wav</Play>";
exit();
}
// generate sox command to combine the prompts last caller number, and datetime
$wavfiles = "sox " . AUDIOFOLDER . "lastcaller.wav ";
// gets the digits from the caller
$callerloop = $caller;
while (strlen($callerloop) > 0)
{
$digit = substr($callerloop,0,1);
$callerloop = substr($callerloop, 1);
$wavfiles .= AUDIOFOLDER. "nms-word-{$digit}.wav ";
}
// datetime wavs
// day of the week
$wavfiles .= AUDIOFOLDER . strtolower($datetimearray[0]) . '.wav ';
// month
$wavfiles .= AUDIOFOLDER . 'month-' . strtolower($datetimearray[1]) . '.wav ';
// day of the month
$wavfiles .= AUDIOFOLDER . 'monthday-' . strtolower($datetimearray[2]) . '.wav ';
// at
$wavfiles .= AUDIOFOLDER . 'nms-word-at.wav ';
// hour
$wavfiles .= AUDIOFOLDER . 'Hour-' . strtolower($datetimearray[3]) . '.wav ';
// minutes is tricky because less than 21 we have exact files while over 20 we need to combine two files
if ($datetimearray[4] <21)
{
$wavfiles .= AUDIOFOLDER . 'time-' . $datetimearray[4] . '.wav ';
}
else
{
$wavfiles .= AUDIOFOLDER . 'time-' . substr($datetimearray[4],0,1) . '0.wav ';
if (substr($datetimearray[4],1,2) != 0) // if we're at the top of the hour, nothing more to play
{
$wavfiles .= AUDIOFOLDER . 'nms-word-' . substr($datetimearray[4],1,2) . '.wav ';
}
}
if (HOURCLOCK == 12)
{ //am pm playback
$wavfiles .= AUDIOFOLDER . 'time-' . $datetimearray[5] . '.wav ';
}
// adds the final prompt and output filename
$wavfiles .= AUDIOFOLDER. "toreturn.wav {$tempfileuri}";
// runs the sox command
exec($wavfiles);
// do the web responder gather function, post back last caller
echo "<Gather numDigits='1' action='ukcallback.php?caller={$caller}'><Play>http://" . SERVER . "/" . AUDIOFOLDER. "tmp/{$tempfile}</Play></Gather>";
}
else if ($_REQUEST["Digits"] == DIALDIGIT)
{
// caller entered digit to return call, do forward to the last caller number
echo "<Forward>" . $_REQUEST["caller"] . "</Forward>";
}
else
{
// caller entered invalid input
echo "<Play> http://" . SERVER . "/" . AUDIOFOLDER . "invalid.wav</Play>";
}
// last incoming call, including the date and time of the call. Additionally, a voice prompt asks if you want to return the call to that phone number by pressing 1
function __getToken()
{
/* First Step is to get a new Access token to given server.*/
$query = array(
'grant_type' => 'password',
'username' => SUPERUSER,
'password' => PASSWORD,
'client_id' => CLIENTID,
'client_secret' => CLIENTSECRET,
);
$postFields = http_build_query($query);
$http_response = "";
$curl_result = __doCurl("https://".SERVER."/ns-api/oauth2/token", CURLOPT_POST, NULL, NULL, $postFields, $http_response);
if (!$curl_result){
echo "error doing curl getting key";
exit;
}
$token = json_decode($curl_result, /*assoc*/true);
if (!isset($token['access_token'])) {
echo "failure getting access token";
exit;
}
return $token['access_token'];
}
function __doCurl($url, $method, $authorization, $query, $postFields, &$http_response)
{
$start= microtime(true);
$curl_options = array(
CURLOPT_URL => $url . ($query ? '?' . http_build_query($query) : ''),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_TIMEOUT => 60
);
$headers = array();
if ($authorization != NULL)
{
if ("bus:bus" == $authorization)
$curl_options[CURLOPT_USERPWD]=$authorization;
else
$headers[$authorization]=$authorization;
}
$curl_options[$method] = true;
if ($postFields != NULL )
{
$curl_options[CURLOPT_POSTFIELDS] = $postFields;
}
if (sizeof($headers)>0)
$curl_options[CURLOPT_HTTPHEADER] = $headers;
$curl_handle = curl_init();
curl_setopt_array($curl_handle, $curl_options);
$curl_result = curl_exec($curl_handle);
$http_response = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
//print_r($http_response);
curl_close($curl_handle);
$end = microtime(true);
if (!$curl_result)
return NULL;
else if ($http_response >= 400)
return NULL;
else
return $curl_result;
}
?>