Source for file phpagi_1.php
Documentation is available at phpagi_1.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>
* 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 http://sourceforge.net/projects/phpagi/
require_once(dirname(__FILE__
) .
DIRECTORY_SEPARATOR .
'phpagi.php');
* @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
* array('code'=>$code, 'result'=>$result, 'data'=>$data)
* @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
* @return array ('code'=>$code, 'result'=>$result, 'data'=>$data)
$this->response =
parent::evalute($command);
* @return array ('code'=>$code, 'result'=>$result, 'data'=>$data)
* Check for error in result structure
* @return boolean true on error
// Returns TRUE if the command returned an error.
$this->conlog("DEBUG: Bad command? Returned code is {$retarr['code']} result={$retarr['result']}");
if(!isset
($retarr['result']))
$this->conlog("DEBUG: No 'result' value returned from asterisk! Eww!");
if($retarr['result'] == -
1)
* Read the result from Asterisk
* 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 integer $vbl from 1 to 4
* Get the response code from the last command
* Get the result code from the last command
* Get the response data from the last command
* Get the response variable from the last command
* Check for error in response
* @return boolean true on error
* Log to console if debug mode
* @param array $arr to print
* @param integer $vbl verbose level
if($lvl ==
0 &&
$label !=
'')
$this->conlog("debug: $label");
* 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(!is_array($prompt)) $prompt =
array($prompt);
for($i =
0; $i <
$len; $i++
)
$ch =
chr($res['result']);
if($terminator &&
$ch ==
$terminator)
* Read $len characters as DTMF codes
$numbers=
array('1'=>
'1', '2'=>
'2abc', '3'=>
'3def', '4'=>
'4ghi', '5'=>
'5jkl', '6'=>
'6mno',
'7'=>
'7pqrs', '8'=>
'8tuv', '9'=>
'9wxyz', '0'=>
'0');
elseif($last !=
$res ||
$res ==
false)
$this->conlog("Character $i is $char");
// $this->text2wav($char);
$char =
$numbers[$res][$times++
];
$this->conlog("Number $res is '$char'");
if(strlen($numbers[$res]) ==
$times) $times =
0;
} while($i <
$len &&
!$abort);
foreach($ret as $k) $str .=
$k .
' ';
* Alias of PHP join function
* Retrieves an entry in the Asterisk database for a given family and key.
* @link http://www.voip-info.org/wiki-database+get
function db_get($family, $key)
if($res['code'] !=
AGIRES_OK ||
$res['result'] ==
0) return false;
* Adds or updates an entry in the Asterisk database for a given family, key, and value.
* @return integer result code
function db_put($family, $key, $val)
* Deletes an entry in the Asterisk database for a given family and key.
* @link http://www.voip-info.org/wiki-database+del
* @return integer result code
function db_del($family, $key)
* 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
if($res['code'] !=
AGIRES_OK ||
$res['result'] ==
0) return false;
* Sets a variable to the specified value. The variables so created can later be used by later using ${<variablename>}
* 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
* @return integer result code
* Hangup the current channel.
* @link http://www.voip-info.org/wiki-hangup
* Get the status of the specified channel.
* @link http://www.voip-info.org/wiki-channel+status
* @return array, ('status'=>$res['result'], 'description'=>$res['data'])
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
$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
* Goto - Set context, extension and priority
* @param string $con context
* @param string $ext extension
* @param string $pri priority
$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
* Say the given number, returning early if # is received on the channel.
* @link http://www.voip-info.org/wiki-say+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).
if($time ==
'') $time =
time();
* @param string $language code
* @param string $telnumber
// returns a sorted array of enum records
$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');
$lines =
trim(`
$execstr`
);
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'];
* Perform enum txt lookup
* @param string $telnumber
// returns the contents of a TXT record associated with an ENUM dns record.
// ala reverse caller ID lookup.
$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');
$lines =
trim(`
$execstr`
);
if($ret ==
'') return false;
* 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
* @return boolean true on success
if($res['code'] !=
AGIRES_OK ||
$res['result'] == -
1) return false;
* 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
if($res['code'] !=
AGIRES_OK ||
$res['result'] == -
1) return false;
Documentation generated on Thu, 30 Sep 2010 02:22:07 -0700 by phpDocumentor 1.4.2