Menu

[r17]: / trunk / phpagi_1.php  Maximize  Restore  History

Download this file

657 lines (590 with data), 18.5 kB

  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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<?php
/**
* phpagi-asmanager.php : PHP Asterisk Manager functions
* Website: http://phpagi.sourceforge.net
*
* $Id: phpagi_1.php,v 1.1 2005/05/27 00:03:18 masham Exp $
*
* Copyright (c) 2004, 2005 Matthew Asham <matthewa@bcwireless.net>, David Eder <david@eder.us>
* All Rights Reserved.
*
* This software is released under the terms of the GNU Lesser General Public License v2.1
* A copy of which is available from http://www.gnu.org/copyleft/lesser.html
*
* We would be happy to list your phpagi based application on the phpagi
* website. Drop me an Email if you'd like us to list your program.
*
* @package phpAGI
* @version 2.0
*/
/**
* Written for PHP 4.3.4, should work with older PHP 4.x versions.
* Please submit bug reports, patches, etc to http://sourceforge.net/projects/phpagi/
* Gracias. :)
*
*/
if(!class_exists('AGI'))
{
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'phpagi.php');
}
/**
* Asterisk Manager class
*
* @link http://www.voip-info.org/wiki-Asterisk+config+manager.conf
* @link http://www.voip-info.org/wiki-Asterisk+manager+API
* @example examples/sip_show_peer.php Get information about a sip peer
* @package phpAGI
*/
class AGI_1 extends AGI
{
/**
* Response structure
*
* array('code'=>$code, 'result'=>$result, 'data'=>$data)
*
* @var array
* @access public
*/
var $response;
/**
* Constructor
*
* @param string $config is the name of the config file to parse
* @param array $optconfig is an array of configuration vars and vals, stuffed into $this->config['phpagi']
*/
function AGI_1($config=false, $optconfig=false)
{
if(!$config) $config = NULL;
if(!$optconfig) $optconfig = array();
parent::AGI($config, $optconfig);
}
/**
* Evaluate an AGI command
*
* @access private
* @param string $command
* @return array ('code'=>$code, 'result'=>$result, 'data'=>$data)
*/
function evalutate($command)
{
$this->response = parent::evalute($command);
return $this->response;
}
/**
* Execute an AGI command
*
* @access private
* @param string $str
* @return array ('code'=>$code, 'result'=>$result, 'data'=>$data)
*/
function agi_exec($str)
{
return $this->evaluate($str);
}
/**
* Check for error in result structure
*
* @param array $retarr
* @return boolean true on error
*/
function agi_is_error($retarr)
{
// Returns TRUE if the command returned an error.
if($retarr['code'] != AGIRES_OK)
{
$this->conlog("DEBUG: Bad command? Returned code is {$retarr['code']} result={$retarr['result']}");
return true;
}
if(!isset($retarr['result']))
{
$this->conlog("DEBUG: No 'result' value returned from asterisk! Eww!");
return true;
}
if($retarr['result'] == -1)
return true;
return false;
}
/**
* Read the result from Asterisk
*
* @return array
*/
function agi_readresult()
{
return $this->response;
}
/**
* Sends $message to the Asterisk console via the 'verbose' message system.
*
* If the Asterisk verbosity level is $vbl or greater, send $str to the console.
*
* The Asterisk verbosity system works as follows. The Asterisk user gets to set the desired verbosity at startup time or later
* using the console 'set verbose' command. Messages are displayed on the console if their verbose level is less than or equal
* to desired verbosity set by the user. More important messages should have a low verbose level; less important messages
* should have a high verbose level.
*
* @link http://www.voip-info.org/wiki-verbose
* @param string $str
* @param integer $vbl from 1 to 4
*/
function agi_verbose($str, $vbl=1)
{
$this->verbose($str, $vbl);
}
/**
* Get the response code from the last command
*
* @return integer
*/
function agi_response_code()
{
return $this->response['code'];
}
/**
* Get the result code from the last command
*
* @return integer
*/
function agi_response_result()
{
$this->conlog("result is {$this->response['result']}");
return $this->response['result'];
}
/**
* Get the response data from the last command
*
* @return string
*/
function agi_response_data()
{
return $this->response['data'];
}
/**
* Get the response variable from the last command
*
* @param string $var
* @return mixed
*/
function agi_response_var($var)
{
if(!isset($this->response[$var]))
return false;
return $this->response[$var];
}
/**
* Check for error in response
*
* @return boolean true on error
*/
function agi_response_is_error()
{
return $this->agi_is_error($this->response);
}
/**
* Log to console if debug mode
*
* @param array $arr to print
* @param string $label
* @param integer $vbl verbose level
*/
function con_print_r($arr, $label='', $lvl=0)
{
if($lvl == 0 && $label != '')
$this->conlog("debug: $label");
$this->conlog(print_r($arr, true), $lvl);
}
/**
* Plays the given file and receives DTMF data.
*
* 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.
*
* 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 integer $len number of digits to read
* @param integer $timeout milliseconds
* @param string $terminator character on which to quit
* @param string $prompt file to play. Do not include file extension.
* @return array of characters
*/
function agi_getdtmf($len, $timeout, $terminator=false, $prompt=false)
{
if($prompt)
{
if(!is_array($prompt)) $prompt = array($prompt);
foreach($prompt as $p)
{
if($p[0] == '$')
{
$this->text2wav(substr($p, 1));
}
else
{
$this->stream_file($p, '#');
}
}
}
$ret = array();
for($i = 0; $i < $len; $i++)
{
$res = $this->wait_for_digit($timeout);
$this->con_print_r($res);
if($this->agi_response_is_error())
{
$this->conlog('error?');
return false;
}
$ch = chr($res['result']);
$this->conlog("got $ch");
if($terminator && $ch == $terminator)
return $ret;
$ret[$i] = $ch;
}
return($ret);
}
/**
* Read $len characters as DTMF codes
*
* @param integer $len
* @return string
*/
function agi_dtmf2text($len)
{
$numbers=array('1'=>'1', '2'=>'2abc', '3'=>'3def', '4'=>'4ghi', '5'=>'5jkl', '6'=>'6mno',
'7'=>'7pqrs', '8'=>'8tuv', '9'=>'9wxyz', '0'=>'0');
$last = false;
$i = $times = 0;
$abort = 0;
$char = '';
do
{
$res = $this->agi_getdtmf(1, 4000);
$res = $res[0];
if($res == false) break;
if($last == false)
$last = $res;
elseif($last != $res || $res == false)
{
$ret[$i] = $char;
$this->conlog("Character $i is $char");
// $this->text2wav($char);
$times = 0;
$i++;
}
$char = $numbers[$res][$times++];
$this->conlog("Number $res is '$char'");
if(strlen($numbers[$res]) == $times) $times = 0;
$last = $res;
} while($i < $len && !$abort);
$str = '';
foreach($ret as $k) $str .= $k . ' ';
$this->text2wav($str);
return($str);
}
/**
* Alias of PHP join function
*
* @param array $arr
* @return string
*/
function arr2str($arr)
{
return trim(join(' ', $arr));
}
/**
* Retrieves an entry in the Asterisk database for a given family and key.
*
* @link http://www.voip-info.org/wiki-database+get
* @param string $family
* @param string $key
* @return string
*/
function db_get($family, $key)
{
$res = $this->database_get($family, $key);
if($res['code'] != AGIRES_OK || $res['result'] == 0) return false;
return $res['data'];
}
/**
* Adds or updates an entry in the Asterisk database for a given family, key, and value.
*
* @param string $family
* @param string $key
* @param string $val
* @return integer result code
*/
function db_put($family, $key, $val)
{
$res = $this->database_put($family, $key, $val);
return $res['code'];
}
/**
* Deletes an entry in the Asterisk database for a given family and key.
*
* @link http://www.voip-info.org/wiki-database+del
* @param string $family
* @param string $key
* @return integer result code
*/
function db_del($family, $key)
{
$res = $this->database_del($family, $key);
return $res['code'];
}
/**
* Fetch the value of a variable.
*
* Does not work with global variables. Does not work with some variables that are generated by modules.
*
* @link http://www.voip-info.org/wiki-get+variable
* @link http://www.voip-info.org/wiki-Asterisk+variables
* @param string $var variable name
* @return string
*/
function get_var($var)
{
$res = $this->get_variable($var);
if($res['code'] != AGIRES_OK || $res['result'] == 0) return false;
return $res['data'];
}
/**
* Sets a variable to the specified value. The variables so created can later be used by later using ${<variablename>}
* in the dialplan.
*
* These variables live in the channel Asterisk creates when you pickup a phone and as such they are both local and temporary.
* Variables created in one channel can not be accessed by another channel. When you hang up the phone, the channel is deleted
* and any variables in that channel are deleted as well.
*
* @link http://www.voip-info.org/wiki-set+variable
* @param string $var is case sensitive
* @param string $val
* @return integer result code
*/
function set_var($var, $val)
{
$res = $this->set_variable($var, $val);
return $res['code'];
}
/**
* Hangup the current channel.
*
* @link http://www.voip-info.org/wiki-hangup
* @param string $channel
*/
function agi_hangup()
{
$this->hangup();
}
/**
* Get the status of the specified channel.
*
* @link http://www.voip-info.org/wiki-channel+status
* @param string $channel
* @return array, ('status'=>$res['result'], 'description'=>$res['data'])
*/
function agi_channel_status($channel)
{
$res = $this->channel_status($channel);
return array('status'=>$res['result'], 'description'=>$res['data']);
}
/**
* Record sound to a file until an acceptable DTMF digit is received or a specified amount of
* time has passed. Optionally the file BEEP is played before recording begins.
*
* @link http://www.voip-info.org/wiki-record+file
* @param string $file to record, without extension, often created in /var/lib/asterisk/sounds
* @param string $format of the file. GSM and WAV are commonly used formats. MP3 is read-only and thus cannot be used.
* @param integer $timeout is the maximum record time in milliseconds, or -1 for no timeout.
* @param string $prompt to play
*/
function agi_recordfile($file, $format, $timeout=5000, $prompt=FALSE)
{
if($prompt) $this->stream_file($prompt);
$this->record_file($file, $format, '#', $timeout, true);
}
/**
* Play the given audio file, allowing playback to be interrupted by a #. 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.
*
* @link http://www.voip-info.org/wiki-stream+file
* @param string $file filename without extension, often in /var/lib/asterisk/sounds
*/
function agi_play($file)
{
$this->stream_file($file, '#');
}
/**
* Goto - Set context, extension and priority
*
* @param string $con context
* @param string $ext extension
* @param string $pri priority
*/
function agi_goto($con,$ext='s',$pri=1)
{
$this->goto($con, $ext, $pri);
}
/**
* Say the given digit string, returning early if # is received on the channel.
*
* @link http://www.voip-info.org/wiki-say+digits
* @param integer $digits
*/
function agi_saydigits($digits)
{
$this->say_digits($digits, '#');
}
/**
* Say the given number, returning early if # is received on the channel.
*
* @link http://www.voip-info.org/wiki-say+number
* @param integer $number
*/
function agi_saynumber($number)
{
$this->say_number($number, '#');
}
/**
* Say a given time, returning early if # is received on the channel.
*
* @link http://www.voip-info.org/wiki-say+time
* @param integer $time number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
*/
function agi_saytime($time='')
{
if($time == '') $time = time();
$this->say_time($time, '#');
}
/**
* Set Language
*
* @param string $language code
*/
function agi_setlanguage($language='en')
{
$this->exec_setlanguage($language);
}
/**
* Perform enum lookup
*
* @param string $telnumber
* @param string $rDNS
* @return array
*/
function enum_lookup($telnumber, $rDNS='e164.org')
{
// returns a sorted array of enum records
if($telnumber[0] == '+')
$telnumber = substr($telnumber, 1);
for($i = 0; $i < strlen($telnumber); $i++)
$rDNS = $telnumber[$i] . '.' . $rDNS;
if(!isset($this->config['phpagi']['dig'])) $this->config['phpagi']['dig'] = $this->which('dig');
$dig=trim($this->config['phpagi']['dig']);
$execstr= $dig . " +short " . escapeshellarg($rDNS) . " NAPTR";
$lines = trim(`$execstr`);
$lines = explode("\n", $lines);
$arr = array();
foreach($lines as $line)
{
$line = trim($line);
$line = str_replace("\t", ' ', $line);
while(strstr($line, ' '))
$line = str_replace(' ', ' ', $line);
$line = str_replace("\"", '', $line);
$line = str_replace("\'", '', $line);
$line = str_replace(' ', '|', $line);
$bits = explode('|', $line);
$bit = explode('!', stripslashes($bits[4]));
$URI = @ereg_replace($bit[1], $bit[2], '+' . $telnumber);
if($URI[3] == ':') $URI[3] = '/';
if($URI[4] == ':') $URI[4] = '/';
$arr[] = array('order'=>$bits[0], 'prio'=>$bits[1], 'tech'=>$bits[3], 'URI'=>$URI);
}
foreach($arr as $key=>$row)
{
$order[$key] = $row['order'];
$prio[$key] = $row['prio'];
}
array_multisort($order, SORT_ASC, $prio, SORT_ASC, $arr);
return($arr);
}
/**
* Perform enum txt lookup
*
* @param string $telnumber
* @param string $rDNS
* @return string
*/
function enum_txtlookup($telnumber, $rDNS='e164.org')
{
// returns the contents of a TXT record associated with an ENUM dns record.
// ala reverse caller ID lookup.
if($telnumber[0] == '+')
$telnumber = substr($telnumber, 1);
for($i = 0; $i < strlen($telnumber); $i++)
$rDNS = $telnumber[$i] . '.' . $rDNS;
if(!isset($this->config['phpagi']['dig'])) $this->config['phpagi']['dig'] = $this->which('dig');
$dig=trim($this->config['phpagi']['dig']);
$execstr = $dig . ' +short ' . escapeshellarg($rDNS) . ' TXT';
$lines = trim(`$execstr`);
$lines = explode("\n", $lines);
foreach($lines as $line)
{
$line = str_replace("\t", ' ', trim($line));
while(strstr($line, ' ')) $line = str_replace(' ', ' ', $line);
$line = str_replace("\"", '', $line);
$line = str_replace("\'", '', $line);
$ret .= $line;
}
$ret = trim($ret);
if($ret == '') return false;
return $ret;
}
/**
* Send the given text to the connected channel.
*
* Most channels do not support transmission of text.
*
* @link http://www.voip-info.org/wiki-send+text
* @param $text
* @return boolean true on success
*/
function send_text($txt)
{
$res = parent::send_text($txt);
if($res['code'] != AGIRES_OK || $res['result'] == -1) return false;
return true;
}
/**
* Send the specified image on a channel.
*
* Most channels do not support the transmission of images.
*
* @link http://www.voip-info.org/wiki-send+image
* @param string $image without extension, often in /var/lib/asterisk/images
* @return boolean true on success
*/
function send_image($image)
{
$res = parent::send_image($image);
if($res['code'] != AGIRES_OK || $res['result'] == -1) return false;
return true;
}
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.