-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfastpass.php
312 lines (302 loc) · 11.4 KB
/
fastpass.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
<?php
/**
* phpagi-asmanager.php : PHP Asterisk Manager functions
* Website: http://phpagi.sourceforge.net
*
* Copyright (c) 2004 - 2010 Matthew Asham <[email protected]>, David Eder <[email protected]>
* Copyright (c) 2005 - 2015 Schmooze Com, Inc
* All Rights Reserved.
*
* This software is released under the terms of the GNU Public License v2
* A copy of which is available from http://www.fsf.org/licenses/gpl.txt
*
* @package phpAGI
*/
namespace Asterisk\AMI;
if(!class_exists('AGI')) {
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'phpagi.php');
}
/**
* AGI class
*
* @package phpAGI
* @link http://www.voip-info.org/wiki-Asterisk+agi
* @example examples/dtmf.php Get DTMF tones from the user and say the digits
* @example examples/input.php Get text input from the user and say it back
* @example examples/ping.php Ping an IP address
*/
class fastpass extends AGI {
/**
* Say the given digit string, returning early if any of the given DTMF escape digits are received on the channel.
* Return early if $buffer is adequate for request.
*
* @link http://www.voip-info.org/wiki-say+digits
* @param string $buffer
* @param integer $digits
* @param string $escape_digits
* @return array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if playback completes with no
* digit received, otherwise a decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
public function fastpass_say_digits(&$buffer, $digits, $escape_digits='') {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1}))
$proceed = true;
}
if($buffer == '' || $proceed) {
$res = $this->say_digits($digits, $escape_digits);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}));
}
/**
* Say the given number, returning early if any of the given DTMF escape digits are received on the channel.
* Return early if $buffer is adequate for request.
*
* @link http://www.voip-info.org/wiki-say+number
* @param string $buffer
* @param integer $number
* @param string $escape_digits
* @return array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if playback completes with no
* digit received, otherwise a decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
public function fastpass_say_number(&$buffer, $number, $escape_digits='') {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1})) {
$proceed = true;
}
}
if($buffer == '' || $proceed) {
$res = $this->say_number($number, $escape_digits);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}));
}
/**
* Say the given character string, returning early if any of the given DTMF escape digits are received on the channel.
* Return early if $buffer is adequate for request.
*
* @link http://www.voip-info.org/wiki-say+phonetic
* @param string $buffer
* @param string $text
* @param string $escape_digits
* @return array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if playback completes with no
* digit received, otherwise a decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
public function fastpass_say_phonetic(&$buffer, $text, $escape_digits='') {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1})) {
$proceed = true;
}
}
if($buffer == '' || $proceed) {
$res = $this->say_phonetic($text, $escape_digits);
if($res['code'] == AGIRES_OK && $res['result'] > 0)
$buffer .= chr($res['result']);
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}));
}
/**
* Say a given time, returning early if any of the given DTMF escape digits are received on the channel.
* Return early if $buffer is adequate for request.
*
* @link http://www.voip-info.org/wiki-say+time
* @param string $buffer
* @param integer $time number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
* @param string $escape_digits
* @return array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if playback completes with no
* digit received, otherwise a decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
function fastpass_say_time(&$buffer, $time=NULL, $escape_digits='') {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1})) {
$proceed = true;
}
}
if($buffer == '' || $proceed) {
$res = $this->say_time($time, $escape_digits);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}));
}
/**
* Play the given audio file, allowing playback to be interrupted by a DTMF digit. This command is similar to the GET DATA
* command but this command returns after the first DTMF digit has been pressed while GET DATA can accumulated any number of
* digits before returning.
* Return early if $buffer is adequate for request.
*
* @link http://www.voip-info.org/wiki-stream+file
* @param string $buffer
* @param string $filename without extension, often in /var/lib/asterisk/sounds
* @param string $escape_digits
* @param integer $offset
* @return array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if playback completes with no
* digit received, otherwise a decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
function fastpass_stream_file(&$buffer, $filename, $escape_digits='', $offset=0) {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1})) {
$proceed = true;
}
}
if($buffer == '' || $proceed) {
$res = $this->stream_file($filename, $escape_digits, $offset);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}), 'endpos'=>0);
}
/**
* Use festival to read text.
* Return early if $buffer is adequate for request.
*
* @link http://www.cstr.ed.ac.uk/projects/festival/
* @param string $buffer
* @param string $text
* @param string $escape_digits
* @param integer $frequency
* @return array, see evaluate for return information.
*/
function fastpass_text2wav(&$buffer, $text, $escape_digits='', $frequency=8000) {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1})) {
$proceed = true;
}
}
if($buffer == '' || $proceed) {
$res = $this->text2wav($text, $escape_digits, $frequency);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}), 'endpos'=>0);
}
/**
* Use Cepstral Swift to read text.
* Return early if $buffer is adequate for request.
*
* @link http://www.cepstral.com/
* @param string $buffer
* @param string $text
* @param string $escape_digits
* @param integer $frequency
* @return array, see evaluate for return information.
*/
function fastpass_swift(&$buffer, $text, $escape_digits='', $frequency=8000, $voice=NULL) {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1}))
$proceed = true;
}
if($buffer == '' || $proceed) {
$res = $this->swift($text, $escape_digits, $frequency, $voice);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}), 'endpos'=>0);
}
/**
* Say Puncutation in a string.
* Return early if $buffer is adequate for request.
*
* @param string $buffer
* @param string $text
* @param string $escape_digits
* @param integer $frequency
* @return array, see evaluate for return information.
*/
function fastpass_say_punctuation(&$buffer, $text, $escape_digits='', $frequency=8000) {
$proceed = false;
if($escape_digits != '' && $buffer != '') {
if(!strpos(chr(255) . $escape_digits, $buffer{strlen($buffer)-1}))
$proceed = true;
}
if($buffer == '' || $proceed) {
$res = $this->say_punctuation($text, $escape_digits, $frequency);
if($res['code'] == AGIRES_OK && $res['result'] > 0) {
$buffer .= chr($res['result']);
}
return $res;
}
return array('code'=>AGIRES_OK, 'result'=>ord($buffer{strlen($buffer)-1}));
}
/**
* Plays the given file and receives DTMF data.
* Return early if $buffer is adequate for request.
*
* This is similar to STREAM FILE, but this command can accept and return many DTMF digits,
* while STREAM FILE returns immediately after the first DTMF digit is detected.
*
* Asterisk looks for the file to play in /var/lib/asterisk/sounds by default.
*
* If the user doesn't press any keys when the message plays, there is $timeout milliseconds
* of silence then the command ends.
*
* The user has the opportunity to press a key at any time during the message or the
* post-message silence. If the user presses a key while the message is playing, the
* message stops playing. When the first key is pressed a timer starts counting for
* $timeout milliseconds. Every time the user presses another key the timer is restarted.
* The command ends when the counter goes to zero or the maximum number of digits is entered,
* whichever happens first.
*
* If you don't specify a time out then a default timeout of 2000 is used following a pressed
* digit. If no digits are pressed then 6 seconds of silence follow the message.
*
* If you don't specify $max_digits then the user can enter as many digits as they want.
*
* Pressing the # key has the same effect as the timer running out: the command ends and
* any previously keyed digits are returned. A side effect of this is that there is no
* way to read a # key using this command.
*
* @link http://www.voip-info.org/wiki-get+data
* @param string $buffer
* @param string $filename file to play. Do not include file extension.
* @param integer $timeout milliseconds
* @param integer $max_digits
* @return array, see evaluate for return information. ['result'] holds the digits and ['data'] holds the timeout if present.
*
* This differs from other commands with return DTMF as numbers representing ASCII characters.
*/
function fastpass_get_data(&$buffer, $filename, $timeout=NULL, $max_digits=NULL) {
if(is_null($max_digits) || strlen($buffer) < $max_digits) {
if($buffer == '') {
$res = $this->get_data($filename, $timeout, $max_digits);
if($res['code'] == AGIRES_OK)
$buffer .= $res['result'];
return $res;
} else {
while(is_null($max_digits) || strlen($buffer) < $max_digits) {
$res = $this->wait_for_digit();
if($res['code'] != AGIRES_OK) {
return $res;
}
if($res['result'] == ord('#')) {
break;
}
$buffer .= chr($res['result']);
}
}
}
return array('code'=>AGIRES_OK, 'result'=>$buffer);
}
}