forked from welltime/phpagi
-
Notifications
You must be signed in to change notification settings - Fork 11
/
phpagi.php
1840 lines (1685 loc) · 66.6 KB
/
phpagi.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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* phpagi.php : PHP AGI Functions for Asterisk
* @see https://github.com/welltime/phpagi
* @filesource http://phpagi.sourceforge.net/
*
* $Id: phpagi.php,v 2.20 2010/09/30 02:21:00 masham Exp $
*
* Copyright (c) 2003 - 2010 Matthew Asham <matthew@ochrelabs.com>, David Eder <david@eder.us> and others
* 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.
*
*
* Written for PHP 4.3.4, should work with older PHP 4.x versions.
*
* Please submit bug reports, patches, etc to https://github.com/welltime/phpagi
*
*
* @package phpAGI
* @version 2.20
*/
if (!class_exists('AGI_AsteriskManager'))
{
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'phpagi-asmanager.php');
}
define('AST_CONFIG_DIR', '/etc/asterisk/');
define('AST_SPOOL_DIR', '/var/spool/asterisk/');
define('AST_TMP_DIR', AST_SPOOL_DIR . '/tmp/');
define('DEFAULT_PHPAGI_CONFIG', AST_CONFIG_DIR . '/phpagi.conf');
define('AST_DIGIT_ANY', '0123456789#*');
define('AGIRES_OK', 200);
define('AST_STATE_DOWN', 0);
define('AST_STATE_RESERVED', 1);
define('AST_STATE_OFFHOOK', 2);
define('AST_STATE_DIALING', 3);
define('AST_STATE_RING', 4);
define('AST_STATE_RINGING', 5);
define('AST_STATE_UP', 6);
define('AST_STATE_BUSY', 7);
define('AST_STATE_DIALING_OFFHOOK', 8);
define('AST_STATE_PRERING', 9);
define('AUDIO_FILENO', 3); // STDERR_FILENO + 1
/**
* 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 AGI
{
/**
* Request variables read in on initialization.
*
* Often contains any/all of the following:
* agi_request - name of agi script
* agi_channel - current channel
* agi_language - current language
* agi_type - channel type (SIP, ZAP, IAX, ...)
* agi_uniqueid - unique id based on unix time
* agi_callerid - callerID string
* agi_dnid - dialed number id
* agi_rdnis - referring DNIS number
* agi_context - current context
* agi_extension - extension dialed
* agi_priority - current priority
* agi_enhanced - value is 1.0 if started as an EAGI script
* agi_accountcode - set by SetAccount in the dialplan
* agi_network - value is yes if this is a fastagi
* agi_network_script - name of the script to execute
*
* NOTE: program arguments are still in $_SERVER['argv'].
*
* @var array
* @access public
*/
var $request;
/**
* Config variables
*
* @var array
* @access public
*/
var $config;
/**
* Asterisk Manager
*
* @var AGI_AsteriskManager
* @access public
*/
var $asmanager;
/**
* Input Stream
*
* @access private
*/
var $in = NULL;
/**
* Output Stream
*
* @access private
*/
var $out = NULL;
/**
* Audio Stream
*
* @access public
*/
var $audio = NULL;
/**
* Application option delimiter
*
* @access public
*/
public $option_delim = ",";
/**
* 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 __construct($config=NULL, $optconfig=array())
{
// load config
if(!is_null($config) && file_exists($config))
$this->config = parse_ini_file($config, true);
elseif(file_exists(DEFAULT_PHPAGI_CONFIG))
$this->config = parse_ini_file(DEFAULT_PHPAGI_CONFIG, true);
// If optconfig is specified, stuff vals and vars into 'phpagi' config array.
foreach($optconfig as $var=>$val)
$this->config['phpagi'][$var] = $val;
// add default values to config for uninitialized values
if(!isset($this->config['phpagi']['error_handler'])) $this->config['phpagi']['error_handler'] = true;
if(!isset($this->config['phpagi']['debug'])) $this->config['phpagi']['debug'] = false;
if(!isset($this->config['phpagi']['admin'])) $this->config['phpagi']['admin'] = NULL;
if(!isset($this->config['phpagi']['tempdir'])) $this->config['phpagi']['tempdir'] = AST_TMP_DIR;
// festival TTS config
if(!isset($this->config['festival']['text2wave'])) $this->config['festival']['text2wave'] = $this->which('text2wave');
// swift TTS config
if(!isset($this->config['cepstral']['swift'])) $this->config['cepstral']['swift'] = $this->which('swift');
ob_implicit_flush(true);
// open stdin & stdout
$this->in = defined('STDIN') ? STDIN : fopen('php://stdin', 'r');
$this->out = defined('STDOUT') ? STDOUT : fopen('php://stdout', 'w');
// initialize error handler
if($this->config['phpagi']['error_handler'] == true)
{
set_error_handler('phpagi_error_handler');
global $phpagi_error_handler_email;
$phpagi_error_handler_email = $this->config['phpagi']['admin'];
error_reporting(E_ALL);
}
// make sure temp folder exists
$this->make_folder($this->config['phpagi']['tempdir']);
// read the request
$str = fgets($this->in);
while($str != "\n")
{
$this->request[substr($str, 0, strpos($str, ':'))] = trim(substr($str, strpos($str, ':') + 1));
$str = fgets($this->in);
}
// open audio if eagi detected
if($this->request['agi_enhanced'] == '1.0')
{
if(file_exists('/proc/' . getmypid() . '/fd/3'))
$this->audio = fopen('/proc/' . getmypid() . '/fd/3', 'r');
elseif(file_exists('/dev/fd/3'))
{
// may need to mount fdescfs
$this->audio = fopen('/dev/fd/3', 'r');
}
else
$this->conlog('Unable to open audio stream');
if($this->audio) stream_set_blocking($this->audio, 0);
}
$this->conlog('AGI Request:');
$this->conlog(print_r($this->request, true));
$this->conlog('PHPAGI internal configuration:');
$this->conlog(print_r($this->config, true));
}
// *********************************************************************************************************
// ** COMMANDS **
// *********************************************************************************************************
/**
* Answer channel if not already in answer state.
*
* @link http://www.voip-info.org/wiki-answer
* @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
*
* @return array, see evaluate for return information. ['result'] is 0 on success, -1 on failure.
*/
function answer()
{
return $this->evaluate('ANSWER');
}
/**
* Get the status of the specified channel. If no channel name is specified, return the status of the current channel.
*
* @link http://www.voip-info.org/wiki-channel+status
* @param string $channel
* @return array, see evaluate for return information. ['data'] contains description.
*/
function channel_status($channel='')
{
$ret = $this->evaluate("CHANNEL STATUS $channel");
switch($ret['result'])
{
case -1: $ret['data'] = trim("There is no channel that matches $channel"); break;
case AST_STATE_DOWN: $ret['data'] = 'Channel is down and available'; break;
case AST_STATE_RESERVED: $ret['data'] = 'Channel is down, but reserved'; break;
case AST_STATE_OFFHOOK: $ret['data'] = 'Channel is off hook'; break;
case AST_STATE_DIALING: $ret['data'] = 'Digits (or equivalent) have been dialed'; break;
case AST_STATE_RING: $ret['data'] = 'Line is ringing'; break;
case AST_STATE_RINGING: $ret['data'] = 'Remote end is ringing'; break;
case AST_STATE_UP: $ret['data'] = 'Line is up'; break;
case AST_STATE_BUSY: $ret['data'] = 'Line is busy'; break;
case AST_STATE_DIALING_OFFHOOK: $ret['data'] = 'Digits (or equivalent) have been dialed while offhook'; break;
case AST_STATE_PRERING: $ret['data'] = 'Channel has detected an incoming call and is waiting for ring'; break;
default: $ret['data'] = "Unknown ({$ret['result']})"; break;
}
return $ret;
}
/**
* 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 array, see evaluate for return information. ['result'] is 1 on sucess, 0 otherwise.
*/
function database_del($family, $key)
{
return $this->evaluate("DATABASE DEL \"$family\" \"$key\"");
}
/**
* Deletes a family or specific keytree within a family in the Asterisk database.
*
* @link http://www.voip-info.org/wiki-database+deltree
* @param string $family
* @param string $keytree
* @return array, see evaluate for return information. ['result'] is 1 on sucess, 0 otherwise.
*/
function database_deltree($family, $keytree='')
{
$cmd = "DATABASE DELTREE \"$family\"";
if($keytree != '') $cmd .= " \"$keytree\"";
return $this->evaluate($cmd);
}
/**
* 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 array, see evaluate for return information. ['result'] is 1 on sucess, 0 failure. ['data'] holds the value
*/
function database_get($family, $key)
{
return $this->evaluate("DATABASE GET \"$family\" \"$key\"");
}
/**
* Adds or updates an entry in the Asterisk database for a given family, key, and value.
*
* @param string $family
* @param string $key
* @param string $value
* @return array, see evaluate for return information. ['result'] is 1 on sucess, 0 otherwise
*/
function database_put($family, $key, $value)
{
$value = str_replace("\n", '\n', addslashes($value));
return $this->evaluate("DATABASE PUT \"$family\" \"$key\" \"$value\"");
}
/**
* Sets a global variable, using Asterisk 1.6 syntax.
*
* @link http://www.voip-info.org/wiki/view/Asterisk+cmd+Set
*
* @param string $pVariable
* @param string|int|float $pValue
* @return array, see evaluate for return information. ['result'] is 1 on sucess, 0 otherwise
*/
function set_global_var($pVariable, $pValue)
{
if (is_numeric($pValue))
return $this->evaluate("Set({$pVariable}={$pValue},g);");
else
return $this->evaluate("Set({$pVariable}=\"{$pValue}\",g);");
}
/**
* Sets a variable, using Asterisk 1.6 syntax.
*
* @link http://www.voip-info.org/wiki/view/Asterisk+cmd+Set
*
* @param string $pVariable
* @param string|int|float $pValue
* @return array, see evaluate for return information. ['result'] is 1 on sucess, 0 otherwise
*/
function set_var($pVariable, $pValue)
{
if (is_numeric($pValue))
return $this->evaluate("Set({$pVariable}={$pValue});");
else
return $this->evaluate("Set({$pVariable}=\"{$pValue}\");");
}
/**
* Executes the specified Asterisk application with given options.
*
* @link http://www.voip-info.org/wiki-exec
* @link http://www.voip-info.org/wiki-Asterisk+-+documentation+of+application+commands
* @param string $application
* @param mixed $options
* @return array, see evaluate for return information. ['result'] is whatever the application returns, or -2 on failure to find application
*/
function exec($application, $options)
{
if(is_array($options)) $options = join('|', $options);
return $this->evaluate("EXEC $application $options");
}
/**
* 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.
*
* 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.
*
* @example examples/ping.php Ping an IP address
*
* @link http://www.voip-info.org/wiki-get+data
* @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 get_data($filename, $timeout=NULL, $max_digits=NULL)
{
return $this->evaluate(rtrim("GET DATA $filename $timeout $max_digits"));
}
/**
* 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 $variable name
* @param boolean $getvalue return the value only
* @return array, see evaluate for return information. ['result'] is 0 if variable hasn't been set, 1 if it has. ['data'] holds the value. returns value if $getvalue is TRUE
*/
function get_variable($variable,$getvalue=FALSE)
{
$res=$this->evaluate("GET VARIABLE $variable");
if($getvalue==FALSE)
return($res);
return($res['data']);
}
/**
* Fetch the value of a full variable.
*
*
* @link http://www.voip-info.org/wiki/view/get+full+variable
* @link http://www.voip-info.org/wiki-Asterisk+variables
* @param string $variable name
* @param string $channel channel
* @param boolean $getvalue return the value only
* @return array, see evaluate for return information. ['result'] is 0 if variable hasn't been set, 1 if it has. ['data'] holds the value. returns value if $getvalue is TRUE
*/
function get_fullvariable($variable,$channel=FALSE,$getvalue=FALSE)
{
if($channel==FALSE){
$req = $variable;
} else {
$req = $variable.' '.$channel;
}
$res=$this->evaluate('GET FULL VARIABLE '.$req);
if($getvalue==FALSE)
return($res);
return($res['data']);
}
/**
* Hangup the specified channel. If no channel name is given, hang up the current channel.
*
* With power comes responsibility. Hanging up channels other than your own isn't something
* that is done routinely. If you are not sure why you are doing so, then don't.
*
* @link http://www.voip-info.org/wiki-hangup
* @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
*
* @param string $channel
* @return array, see evaluate for return information. ['result'] is 1 on success, -1 on failure.
*/
function hangup($channel='')
{
return $this->evaluate("HANGUP $channel");
}
/**
* Does nothing.
*
* @link http://www.voip-info.org/wiki-noop
* @return array, see evaluate for return information.
*/
function noop($string="")
{
return $this->evaluate("NOOP \"$string\"");
}
/**
* Receive a character of text from a connected channel. Waits up to $timeout milliseconds for
* a character to arrive, or infinitely if $timeout is zero.
*
* @link http://www.voip-info.org/wiki-receive+char
* @param integer $timeout milliseconds
* @return array, see evaluate for return information. ['result'] is 0 on timeout or not supported, -1 on failure. Otherwise
* it is the decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
function receive_char($timeout=-1)
{
return $this->evaluate("RECEIVE CHAR $timeout");
}
/**
* 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 string $escape_digits
* @param integer $timeout is the maximum record time in milliseconds, or -1 for no timeout.
* @param integer $offset to seek to without exceeding the end of the file.
* @param boolean $beep
* @param integer $silence number of seconds of silence allowed before the function returns despite the
* lack of dtmf digits or reaching timeout.
* @return array, see evaluate for return information. ['result'] is -1 on error, 0 on hangup, otherwise a decimal value of the
* DTMF tone. Use chr() to convert to ASCII.
*/
function record_file($file, $format, $escape_digits='', $timeout=-1, $offset=NULL, $beep=false, $silence=NULL)
{
$cmd = trim("RECORD FILE $file $format \"$escape_digits\" $timeout $offset");
if($beep) $cmd .= ' BEEP';
if(!is_null($silence)) $cmd .= " s=$silence";
return $this->evaluate($cmd);
}
/**
* Say a given character string, returning early if any of the given DTMF digits are received on the channel.
*
* @link https://www.voip-info.org/say-alpha
* @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.
*/
function say_alpha($text, $escape_digits='')
{
return $this->evaluate("SAY ALPHA $text \"$escape_digits\"");
}
/**
* Say the given digit string, returning early if any of the given DTMF escape digits are received on the channel.
*
* @link http://www.voip-info.org/wiki-say+digits
* @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.
*/
function say_digits($digits, $escape_digits='')
{
return $this->evaluate("SAY DIGITS $digits \"$escape_digits\"");
}
/**
* Say the given number, returning early if any of the given DTMF escape digits are received on the channel.
*
* @link http://www.voip-info.org/wiki-say+number
* @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.
*/
function say_number($number, $escape_digits='')
{
return $this->evaluate("SAY NUMBER $number \"$escape_digits\"");
}
/**
* Say the given character string, returning early if any of the given DTMF escape digits are received on the channel.
*
* @link http://www.voip-info.org/wiki-say+phonetic
* @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.
*/
function say_phonetic($text, $escape_digits='')
{
return $this->evaluate("SAY PHONETIC $text \"$escape_digits\"");
}
/**
* Say a given time, returning early if any of the given DTMF escape digits are 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).
* @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 say_time($time=NULL, $escape_digits='')
{
if(is_null($time)) $time = time();
return $this->evaluate("SAY TIME $time \"$escape_digits\"");
}
/**
* 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 array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if the image is sent or
* channel does not support image transmission.
*/
function send_image($image)
{
return $this->evaluate("SEND IMAGE $image");
}
/**
* 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 array, see evaluate for return information. ['result'] is -1 on hangup or error, 0 if the text is sent or
* channel does not support text transmission.
*/
function send_text($text)
{
return $this->evaluate("SEND TEXT \"$text\"");
}
/**
* Cause the channel to automatically hangup at $time seconds in the future.
* If $time is 0 then the autohangup feature is disabled on this channel.
*
* If the channel is hungup prior to $time seconds, this setting has no effect.
*
* @link http://www.voip-info.org/wiki-set+autohangup
* @param integer $time until automatic hangup
* @return array, see evaluate for return information.
*/
function set_autohangup($time=0)
{
return $this->evaluate("SET AUTOHANGUP $time");
}
/**
* Changes the caller ID of the current channel.
*
* @link http://www.voip-info.org/wiki-set+callerid
* @param string $cid example: "John Smith"<1234567>
* This command will let you take liberties with the <caller ID specification> but the format shown in the example above works
* well: the name enclosed in double quotes followed immediately by the number inside angle brackets. If there is no name then
* you can omit it. If the name contains no spaces you can omit the double quotes around it. The number must follow the name
* immediately; don't put a space between them. The angle brackets around the number are necessary; if you omit them the
* number will be considered to be part of the name.
* @return array, see evaluate for return information.
*/
function set_callerid($cid)
{
return $this->evaluate("SET CALLERID $cid");
}
/**
* Sets the context for continuation upon exiting the application.
*
* Setting the context does NOT automatically reset the extension and the priority; if you want to start at the top of the new
* context you should set extension and priority yourself.
*
* If you specify a non-existent context you receive no error indication (['result'] is still 0) but you do get a
* warning message on the Asterisk console.
*
* @link http://www.voip-info.org/wiki-set+context
* @param string $context
* @return array, see evaluate for return information.
*/
function set_context($context)
{
return $this->evaluate("SET CONTEXT $context");
}
/**
* Set the extension to be used for continuation upon exiting the application.
*
* Setting the extension does NOT automatically reset the priority. If you want to start with the first priority of the
* extension you should set the priority yourself.
*
* If you specify a non-existent extension you receive no error indication (['result'] is still 0) but you do
* get a warning message on the Asterisk console.
*
* @link http://www.voip-info.org/wiki-set+extension
* @param string $extension
* @return array, see evaluate for return information.
*/
function set_extension($extension)
{
return $this->evaluate("SET EXTENSION $extension");
}
/**
* Enable/Disable Music on hold generator.
*
* @link http://www.voip-info.org/wiki-set+music
* @param boolean $enabled
* @param string $class
* @return array, see evaluate for return information.
*/
function set_music($enabled=true, $class='')
{
$enabled = ($enabled) ? 'ON' : 'OFF';
return $this->evaluate("SET MUSIC $enabled $class");
}
/**
* Set the priority to be used for continuation upon exiting the application.
*
* If you specify a non-existent priority you receive no error indication (['result'] is still 0)
* and no warning is issued on the Asterisk console.
*
* @link http://www.voip-info.org/wiki-set+priority
* @param integer $priority
* @return array, see evaluate for return information.
*/
function set_priority($priority)
{
return $this->evaluate("SET PRIORITY $priority");
}
/**
* 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 $variable is case sensitive
* @param string $value
* @return array, see evaluate for return information.
*/
function set_variable($variable, $value)
{
$value = str_replace("\n", '\n', addslashes($value));
return $this->evaluate("SET VARIABLE $variable \"$value\"");
}
/**
* 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.
*
* @example examples/ping.php Ping an IP address
*
* @link http://www.voip-info.org/wiki-stream+file
* @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 stream_file($filename, $escape_digits='', $offset=0)
{
return $this->evaluate("STREAM FILE $filename \"$escape_digits\" $offset");
}
/**
* Enable or disable TDD transmission/reception on the current channel.
*
* @link http://www.voip-info.org/wiki-tdd+mode
* @param string $setting can be on, off or mate
* @return array, see evaluate for return information. ['result'] is 1 on sucess, 0 if the channel is not TDD capable.
*/
function tdd_mode($setting)
{
return $this->evaluate("TDD MODE $setting");
}
/**
* Sends $message to the Asterisk console via the 'verbose' message system.
*
* If the Asterisk verbosity level is $level or greater, send $message 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 $message
* @param integer $level from 1 to 4
* @return array, see evaluate for return information.
*/
function verbose($message, $level=1)
{
foreach(explode("\n", str_replace("\r\n", "\n", print_r($message, true))) as $msg)
{
@syslog(LOG_WARNING, $msg);
$ret = $this->evaluate("VERBOSE \"$msg\" $level");
}
return $ret;
}
/**
* Waits up to $timeout milliseconds for channel to receive a DTMF digit.
*
* @link http://www.voip-info.org/wiki-wait+for+digit
* @param integer $timeout in millisecons. Use -1 for the timeout value if you want the call to wait indefinitely.
* @return array, see evaluate for return information. ['result'] is 0 if wait completes with no
* digit received, otherwise a decimal value of the DTMF tone. Use chr() to convert to ASCII.
*/
function wait_for_digit($timeout=-1)
{
return $this->evaluate("WAIT FOR DIGIT $timeout");
}
// *********************************************************************************************************
// ** APPLICATIONS **
// *********************************************************************************************************
/**
* Set absolute maximum time of call.
*
* Note that the timeout is set from the current time forward, not counting the number of seconds the call has already been up.
* Each time you call AbsoluteTimeout(), all previous absolute timeouts are cancelled.
* Will return the call to the T extension so that you can playback an explanatory note to the calling party (the called party
* will not hear that)
*
* @link http://www.voip-info.org/wiki-Asterisk+-+documentation+of+application+commands
* @link http://www.dynx.net/ASTERISK/AGI/ccard/agi-ccard.agi
* @param $seconds allowed, 0 disables timeout
* @return array, see evaluate for return information.
*/
function exec_absolutetimeout($seconds=0)
{
return $this->exec('AbsoluteTimeout', $seconds);
}
/**
* Executes an AGI compliant application.
*
* @param string $command
* @return array, see evaluate for return information. ['result'] is -1 on hangup or if application requested hangup, or 0 on non-hangup exit.
* @param string $args
*/
function exec_agi($command, $args)
{
return $this->exec("AGI $command", $args);
}
/**
* Set Language.
*
* @param string $language code
* @return array, see evaluate for return information.
*/
function exec_setlanguage($language='en')
{
return $this->exec('Set', 'CHANNEL(language)='. $language);
}
/**
* Do ENUM Lookup.
*
* Note: to retrieve the result, use
* get_variable('ENUM');
*
* @param $exten
* @return array, see evaluate for return information.
*/
function exec_enumlookup($exten)
{
return $this->exec('EnumLookup', $exten);
}
/**
* Dial.
*
* Dial takes input from ${VXML_URL} to send XML Url to Cisco 7960
* Dial takes input from ${ALERT_INFO} to set ring cadence for Cisco phones
* Dial returns ${CAUSECODE}: If the dial failed, this is the errormessage.
* Dial returns ${DIALSTATUS}: Text code returning status of last dial attempt.
*
* @link http://www.voip-info.org/wiki-Asterisk+cmd+Dial
* @param string $type
* @param string $identifier
* @param integer $timeout
* @param string $options
* @param string $url
* @return array, see evaluate for return information.
*/
function exec_dial($type, $identifier, $timeout=NULL, $options=NULL, $url=NULL)
{
return $this->exec('Dial', trim("$type/$identifier".$this->option_delim.$timeout.$this->option_delim.$options.$this->option_delim.$url, $this->option_delim));
}
/**
* Goto.
*
* This function takes three arguments: context,extension, and priority, but the leading arguments
* are optional, not the trailing arguments. Thuse goto($z) sets the priority to $z.
*
* @param string $a
* @param string $b;
* @param string $c;
* @return array, see evaluate for return information.
*/
function exec_goto($a, $b=NULL, $c=NULL)
{
return $this->exec('Goto', trim($a.$this->option_delim.$b.$this->option_delim.$c, $this->option_delim));
}
// *********************************************************************************************************
// ** FAST PASSING **
// *********************************************************************************************************
/**
* 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.
*/
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.
*/
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.
*/
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']);