提交 944755c6 authored 作者: Tristan Mahé's avatar Tristan Mahé

needed for tests

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/contrib@15082 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 5ab40e20
#!/usr/bin/php -q
<?php
require_once('phpagi-2.14/phpagi.php');
$agi = new AGI();
logthis("New call incoming");
$agi->answer();
logthis("Answered");
$res = $agi->stream_file('/telephony/freeswitch/sounds/en/us/callie/ivr/8000/ivr-you_have_dialed_an_invalid_extension.wav',"*");
logthis("Stream_file res:".print_r($res,true));
$agi->hangup();
?>
差异被折叠。
PHPAGI README
$Id: README,v 1.4 2005/05/19 13:19:37 pinhole Exp $
---------------------------------------
Welcome to PHPAGI.
phpagi is a set of PHP classes for use in developing applications with
the Asterisk Gateway Interface, and is licensed under the GNU Lesser
General Public License (see COPYING for terms).
This release (version 2) of the phpagi classes is a significant overhaul
from the old version 1 library. API functions have been renamed and
restructured.
Version 1 of phpagi is no longer supported, but will continue to be
available for historical purposes. We strongly encourage you to migrate
to this new version.
If you have developed software based around phpagi, we'd like to hear from
you! Drop us a note, and indicate whether you'd like us to list your
application on our website.
FILES
-----
phpagi.php - The main phpagi class.
phpagi-asmanager.php - The Asterisk Manager class.
phpagi-fastagi.php - FastAGI class.
phpagi_1.php - Compatability class for 1.12 release
docs/ - README files for the classes.
api-docs/ - API Documentation (html)
DOCS
----
README.phpagi - The main phpagi README
README.phpagi-asmanager - The phpagi asterisk manager README
README.phpagi-fastagi - phpagi fastagi README
CHANGELOG - Change Log.
phpagi.conf - An example configuration file for phpagi.
fastagi.xinetd - xinetd.conf sample configuration for fastagi
SUPPORT
-------
Support for phpagi is available from the project website.
* http://phpagi.sourceforge.net
* http://sourceforge.net/projects/phpagi/
----------
CHANGES
----------
May 25, 2005 - David Eder
* Added fastpass support.
May 18, 2005 - David Eder
* Added phpagi_1.php as a wrapper class for 1.12 compatibility.
March 25, 2005 - David Eder
* Changed the way text2wave and swift are executed for better compatibility.
* Added caching to text2wav and swift.
March 12, 2005 - David Eder
* fixed autohangup in phpagi-asmanager.php, renamed to set_autohangup.
* Added more documentation to phpagi-asmanager.php.
* Added weather example.
March 4, 2005 - David Eder
* FastAGI via xinetd
February 17, 2005 - David Eder
* Fixed bugs with error handler.
February 16, 2005 - David Eder
* Added Cepstral swift TTS patch from C. Arbusti of Soluzioni Vocali ( http://soluzionivocali.it ).
* Extended swift TTS functionality.
* Moved tempdir from [festival] to [phpagi] so that it can be shared with other extensions.
February 2, 2005 - David Eder
* More documentation.
* Reworked and reintegrated AGI_AsteriskManager class.
January 21, 2005 - David Eder
* Added exec_dial($type, $identifier, $timeout=NULL, $options=NULL, $url=NULL).
* Added exec_goto($a, $b=NULL, $c=NULL).
* Fixed bugs in evaluate, better support of multiline and closed input and output.
* Fixed bugs in config initialization.
January 19, 2005 - David Eder
* SUMMARY:
* Massive restructuring!
* Updated to use more PHP internal functions.
* Updated function arguments to represent their AGI function's arguments.
* Functions now return a consistent result array.
* Added phpdoc documentation.
* Removed functions that can be done with internal PHP or AGI functions.
* Enhanced error handler.
*
* Goals of changes:
*
* It is important for a language API to not wander too far from the general
* API. Functions that are named differently have been updated. Function
* arguments that do not match the AGI API have been updated. Underscores
* have been substituted for spaces to make them compatible with PHP function
* names. Optional arguments in AGI should remain optional if possible. Return
* values should be consistent. Functions need to be better documented.
*
* Result:
*
* The return from most functions is now
* array('code'=>$code, 'result'=>$result, 'data'=>$data)
* ['data'] still needs some work.
*
* removed class variables:
* $response - It was no longer used with the new return structure.
*
* removed functions:
* agi_is_error($retarr) - It was no longer used with the new return
* structure. Each function has it's own result that must be evalutated
* by the programmer, as the return values are often specific to the function.
* agi_readresult($str=FALSE) - It was no longer used with the new return structure.
* agi_response_code() - It was no longer used with the new return structure.
* agi_response_result() - It was no longer used with the new return structure.
* agi_response_data() - It was no longer used with the new return structure.
* agi_response_var($var) - It was no longer used with the new return structure.
* agi_response_is_error() - see agi_is_error
* agi_read() - It was deprecated
* con_print_r($arr,$label='',$lvl=0) - Use print_r($arr, true) in conjunction with conlog
* agi_getdtmf($len,$timeout,$terminator=FALSE,$prompt=FALSE) - use get_data
* agi_dtmf2text($len,$timeout,$terminator=FALSE,$prompt=FALSE) - use text_input
* arr2str($arr) - use PHP function join
* config_load($file) - use PHP function parse_ini_file
* enum_lookup($telnumber,$rDNS="e164.org") - use exec_enumlookup
* enum_txtlookup($telnumber,$rDNS="e164.org") - use exec_enumlookup
*
* added functions:
* answer()
* autohangup($time=0)
* exec($application, $options)
* get_data($filename, $timeout=NULL, $max_digits=NULL)
* receive_char($timeout=-1)
* say_phonetic($text, $escape_digits='')
* set_context($context)
* set_extension($extension)
* set_priority($priority)
* tdd_mode($setting)
* wait_for_digit($timeout=-1)
* database_deltree($family, $keytree='')
* noop()
* set_music($enabled=true, $class='')
* exec_absolutetimeout($seconds=0)
* exec_agi($command, $args)
* exec_enumlookup($exten)
* text_input($mode='NUMERIC')
* say_punctuation($text, $escape_digits='', $frequency=8000)
* which($cmd, $checkpath=NULL)
* make_folder($folder, $perms=0755)
* updated functions:
* agi_exec($str) -> evaluate($command) - exec is an AGI function
* agi_verbose($str,$vbl=1) -> verbose($message, $level=1) - consistency with AGI
* db_get($family,$key) -> database_get($family, $key) - consistency with AGI
* db_put($family,$key,$val) -> database_put($family, $key, $value) - consistency with AGI
* db_del($family,$key) -> database_del($family, $key) - consistency with AGI
* get_var($var) -> get_variable($variable) - consistency with AGI
* set_var($var,$val) -> set_variable($variable, $value) - consistency with AGI
* agi_hangup() -> hangup($channel='') - consistency with AGI
* agi_channel_status($channel) -> channel_status($channel='') - consistency with AGI
* agi_recordfile($file,$format,$timeout=5000,$prompt=FALSE) -> record_file($file, $format, $escape_digits='', $timeout=-1, $beep=false, $silence=NULL) - consistency with AGI
* agi_play($file) -> stream_file($filename, $escape_digits='', $offset=0) - consistency with AGI
* agi_goto($con,$ext='s',$pri=1) -> goto($context, $extension='s', $priority=1) - consistency with AGI
* agi_saydigits($digits) -> say_digits($digits, $escape_digits='') - consistency with AGI
* agi_saynumber($number) -> say_number($number, $escape_digits='') - consistency with AGI
* agi_saytime($time="") -> say_time($time=NULL, $escape_digits='') - consistency with AGI
* agi_setlanguage($language="en") -> exec_setlanguage($language='en') - consistency with AGI
* text2wav($text) -> text2wav($text, $escape_digits='', $frequency=8000) - consistency with AGI
* phpagi_error_handler($errno, $errstr, $errfile, $errline) -> phpagi_error_handler($level, $message, $file, $line, $context) - I needed better error handling
August 29, 2004 -
* Fixed db_get now returns a value
* Fixed db_put now stores a value
* Added enum_lookup, requires external "dig" utility.
* Tweak con_print_r now dumps arrays, displays variable type.
* Added enum_txtlookup, requires external "dig" utility".
* Added parse_callerid, thanks to http://www.sbuehl.com/projects/asterisk/asterisk-howto-3.html
August 26, 2004 -
* Added agi_verbose
* Added debug config-option to shut conlog up
* Added agi_saydigits, agi_saynumber, agi_saytime, agi_setlanguage
April 8, 2004 - v1.5 internal
* Added agi_goto - David Croft
* Added this changelog (maintain it eh?) - Matthew Asham
* Added quote fixing to conlog - David Croft
* Added new parameter to AGI constructor, $configopt array - Matthew Asham
- Added phpagi_error_handler - David Croft
March 20, 2004 - v1.5
* Fixed buffering bugs - David Croft
* added demo app. - Matthew Asham
* misc stuff. - Matthew Asham
November 2003
* Initial version
/CHANGELOG/1.4/Wed May 25 18:43:57 2005//
/README.phpagi/1.1/Mon Mar 14 19:38:20 2005//
/README.phpagi-asmanager/1.1/Mon Mar 14 19:38:20 2005//
/README.phpagi-fastagi/1.1/Mon Mar 14 19:38:20 2005//
/fastagi.xinetd/1.1/Mon Mar 14 19:38:20 2005//
/phpagi.example.conf/1.1/Mon Mar 14 19:38:20 2005//
D
:ext:masham@cvs.sourceforge.net:/cvsroot/phpagi
phpagi: an AGI class written in PHP
Matthew Asham <matthewa@bcwireless.net>
http://phpagi.sourceforge.net
Contributions by:
Florian Overkamp <florian@speakup.nl>
David Eder <david@eder.us>
OVERVIEW
--------
Include it:
require 'phpagi.php';
Construct it:
$agi = new AGI();
This creates a new AGI object with all the agi vars read in, you're now ready
to rock and roll.
Answer the line.
$agi->answer();
Play a file.
$agi->stream_file('somefile.gsm');
Record a file.
$agi->record_file($file, $format, $escape_digits='', $timeout=-1, $beep=false, $silence=NULL)
There's more, read the class or refer to the PHPAGI website for more information.
This README is seriousley lacking.
CONFIGURATION
-------------
phpagi supports an ini style configuration file, and run time configuration.
By default the class reads in the contents of /etc/asterisk/phpagi.conf into
$this->config. The format of the ini file is as follows:
[examplesection]
foo=bar
bar=dew
dew=pale ale
This is read into $this->config as:
$this->config['examplesection']['foo']='bar';
$this->config['examplesection']['bar]='dew';
$this->config['examplesection']['dew']='pale ale';
Run time configuration is also supported. Pass an array of variables and values as the
second parameter to the AGI constructor, these fields are stored in $this->config['phpagi'].
ie:
$myconfig=array(
"error_handler"="true"
);
$agi=new AGI("/etc/asterisk/phpagi.conf",$myconfig);
The following run-time configuration options are used by the phpagi class to change behaviour. They
are all contained in the $this->config['phpagi'] array.
* error_handler - set to "true" (string) to enable php debugging
EXAMPLE APPLICATION
-------------------
ping.php is an example phpagi application. it asks for an IP address to be
entered, and reads back the results of a "ping" summary.
to use it, you'll need festival installed. see http://www.voip-info.org/tiki-index.php?page=Asterisk+festival+installation
for all the groovy bits to get festival working with asterisk, then configure
phpagi to use it too:
in /etc/asterisk/phpagi.conf:
[festival]
text2wave=/usr/src/festival/bin/text2wave
tempdir=/var/lib/asterisk/sounds/tmp/
phpagi-asmanager: an Asterisk Manager class written in PHP
Matthew Asham <matthewa@bcwireless.net>
http://phpagi.sourceforge.net
------------------------------------------------------------------------------
------------------------------------------------------------------------------
SECURITY
------------------------------------------------------------------------------
Validation:
******If asterisk is running as root, the manager interface may allow the
execution of arbitrary shell commands as root. If the user can update any
configuration file that can execute arbitrary command (like the dialplan),
the system may be compromised.
Also, look out for command injection. Consider the following example:
$as->Events($_POST['events_status']);
We expect either 'on' or 'off', but the attacker uses:
"\r\n\r\nAction: Command\r\nCommand: database put forward 54321 19005551212";
Validation is a *must* for all user data.
Username and Secret:
Storing the username and secret in the config file will isolate them from your
code.
Isolation of username and secret in the config file does not mean that the
script cannot simple read the config file. The config file must be readable
by the script.
CREATING A NEW INSTANCE OF THE CLASS
------------------------------------------------------------------------------
The class can be created standalone of phpagi.php, or through phpagi.
STANDALONE:
require "phpagi-asmanager.php";
$as = new AGI_AsteriskManager();
FROM PHPAGI:
require "phpagi.php";
$agi = new AGI();
$as = $agi->new_AsteriskManager();
Notes:
* If the class is created using $agi->new_AsteriskManager(),
AGI_AsteriskManager will use the parent phpagi for logging to the Asterisk
console.
* phpagi.php will include phpagi-asmanager.php by itself.
* If phpagi-asmanager.php is included _before_ phpagi.php, phpagi.php will
not attempt to re-include it.
* If phpagi.php tries to include phpagi-asmanager.php but is unable to do
so, an error will be echoed to the asterisk console and the script will
continue running normally. in this case the return value of
new_AsteriskManager() will be FALSE.
------------------------------------------------------------------------------
CONFIGURATION
------------------------------------------------------------------------------
phpagi-asmanager uses the same configuration file as phpagi.conf (usually
/etc/asterisk/phpagi.conf). All configuration information specific to
phpagi-asmanager is contained in the [asmanager] section of the .conf file.
supported directives:
[asmanager]
# server to connect to
server=localhost
# default manager port
port=5038
#username for login
username=me_and_only_me
#password for login
secret=i_am_not_telling
------------------------------------------------------------------------------
CONNECTING
------------------------------------------------------------------------------
$res = $as->connect("localhost", "username", "password");
if($res == FALSE) {
echo "Connection failed.\n";
}
elseif($res == TRUE){
echo "Connection established.\n";
}
A port can also be specified for the hostname. eg:
$res = $as->connect("my.asterisk.server:1234", "username", "port");
If the no parameters are specified, the defaults from the config will be used.
------------------------------------------------------------------------------
DISCONNECTING
------------------------------------------------------------------------------
$as->disconnect();
------------------------------------------------------------------------------
SENDING REQUESTS
------------------------------------------------------------------------------
$as->send_request($eventname, $arrayofparameterstopass);
send_request() calls wait_request and returns an array of returned data from
the manager. If something went wrong, it returns false.
wait_request() shouldn't need to be called from a script directly unless you
are implementing merely an event listener.
wait_request() will also detect events and dispatch any registered event
handlers for the event.
examples:
$res = $as->send_request('EventName',
array('Channel'=>'Zap/1/16045551212',
'SomeParameter'=>'data'));
echo "Dump of returned data:\n";
foreach($res as $var=>$val)
echo "$var = $val\n";
$res['Response'] will generally be 'Success' on success and 'Error' on
failure. But this is not always true. If $res['Response'] == 'Follows', a
multi-line response will be stored in $res['data'].
Several manager commands have been aliased for convenience. See below.
------------------------------------------------------------------------------
EVENTS
------------------------------------------------------------------------------
TODO: non-blocking socket i/o.
The class uses event callbacks to process events received from the manager.
The event callback prototype looks like:
function dump_event($ecode, $data, $server, $port)
{
echo "received event '$ecode' from $server:$port\n";
print_r($data);
}
To register an event call back:
$as->add_event_handler('eventname', 'eventfunction');
eg:
$as->add_event_handler('registry', 'dump_event');
The special eventname "*" can also be registered. any eventname not
specifically registered will be handled by the "*" handler. If no "*" handler
is defined, the event will be silently ignored.
------------------------------------------------------------------------------
PRECANNED FUNCTIONS
------------------------------------------------------------------------------
The following Manager functions have been aliased for convenience:
AbsoluteTimeout
ChangeMonitor
Command
Events
ExtensionState
GetVar
Hangup
IAXPeers
ListCommands
Logoff
MailboxCount
MailboxStatus
Monitor
Originate
ParkedCalls
Ping
Queues
QueueStatus
Redirect
SetCDRUserField
SetVar
SIPpeers
Status
StopMontor
ZapDialOffhook
ZapDNDoff
ZapDNDon
ZapHangup
ZapTransfer
First, in /etc/services, at this line:
fastagi 4573/tcp # Asterisk AGI
Second, create /etc/xnetd.d/fastagi with:
# default: off
# description: fastagi is a remote AGI interface
service fastagi
{
socket_type = stream
user = root
group = nobody
server = <path to phpagi-fastagi.php>
wait = no
protocol = tcp
bind = 127.0.0.1
disable = no
}
Make sure you set the path to the phpagi-fastagi.php script. Set the user
and group to a non-root user if none of your scripts need root access. You
might consider using posix_setuid and friends to reduce privileges. Change
the bind address to your outbound IP address or to 0.0.0.0 to allow anyone
to connect. Be sure to read up about xinetd and take advantage of security
features it provides. Fast AGI doesn't provide authentification. It's up
to you to keep unwanted visitors from extracting information from your AGI
implementation.
Third, write your code.
Take special notice of how fastagi.php works:
1. $fastagi is initialized as a new AGI.
2. The script determines which script was requested.
3. The script is called using
reqire_once($fastagi->request['agi_request']).
4. Your script takes over. You must not create a new AGI, but
insead use the $fastagi instance that has already been created.
In your dialplan:
exten => 5551212, 1, Agi(agi://127.0.0.1/myscript.php)
# default: off
# description: fastagi is a remote AGI interface
service fastagi
{
socket_type = stream
user = root
group = nobody
server = /var/lib/asterisk/agi-bin/phpagi/phpagi-fastagi.php
wait = no
protocol = tcp
bind = 127.0.0.1
disable = no
}
; example phpagi.conf
[phpagi]
debug=true ; enable debuging
error_handler=true ; use internal error handler
admin=errors@mydomain.com ; mail errors to
hostname=sip.mydomain.com ; host name of this server
tempdir=/var/spool/asterisk/tmp/ ; temporary directory for storing temporary output
[asmanager]
server=localhost ; server to connect to
port=5038 ; default manager port
username=me_and_only_me ; username for login
secret=i_am_not_telling ; password for login
[fastagi]
setuid=true ; drop privileges to owner of script
basedir=/var/lib/asterisk/agi-bin/ ; path to script folder
[festival] ; text to speech engine
text2wave=/usr/bin/text2wave ; path to text2wave binary
[cepstral] ; alternate text to speech engine
swift=/opt/swift/bin/swift ; path to switft binary
voice=David ; default voice
#!/usr/local/bin/php -q
<?php
$cwd = getcwd();
passthru("phpdoc -t \"$cwd/api-docs\" -f \"$cwd/phpagi.php,$cwd/phpagi-asmanager.php,$cwd/phpagi-fastagi.php,$cwd/phpagi_1.php\" -pp on -ti \"phpAGI\" -o HTML:frames:earthli -s on");
?>
差异被折叠。
#!/usr/local/bin/php -q
<?php
/**
* phpagi-fastagi.php : PHP FastAGI bootstrap
* Website: http://phpagi.sourceforge.net
*
* $Id: phpagi-fastagi.php,v 1.2 2005/05/25 18:43:48 pinhole 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
* @example docs/fastagi.xinetd Example xinetd config file
*/
/**
* 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. :)
*
*/
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'phpagi.php');
$fastagi = new AGI();
$fastagi->verbose(print_r($fastagi, true));
if(!isset($fastagi->config['fastagi']['basedir']))
$fastagi->config['fastagi']['basedir'] = dirname(__FILE__);
// perform some security checks
$script = $fastagi->config['fastagi']['basedir'] . DIRECTORY_SEPARATOR . $fastagi->request['agi_network_script'];
// in the same directory (or subdirectory)
$mydir = dirname($fastagi->config['fastagi']['basedir']) . DIRECTORY_SEPARATOR;
$dir = dirname($script) . DIRECTORY_SEPARATOR;
if(substr($dir, 0, strlen($mydir)) != $mydir)
{
$fastagi->conlog("$script is not allowed to execute.");
exit;
}
// make sure it exists
if(!file_exists($script))
{
$fastagi->conlog("$script does not exist.");
exit;
}
// drop privileges
if(isset($fastagi->config['fastagi']['setuid']) && $fastagi->config['fastagi']['setuid'])
{
$owner = fileowner($script);
$group = filegroup($script);
if(!posix_setgid($group) || !posix_setegid($group) || !posix_setuid($owner) || !posix_seteuid($owner))
{
$fastagi->conlog("failed to lower privileges.");
exit;
}
}
// make sure script is still readable
if(!is_readable($script))
{
$fastagi->conlog("$script is not readable.");
exit;
}
require_once($script);
?>
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论