Asterisk manager Example: PHP

Asterisk Manager Example: PHP


PHP Example

This example was provided by Gus at fibertel.com.ar:


<?php

$socket = fsockopen("192.168.0.53","5038", $errno, $errstr, $timeout);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: admin\r\n");
fputs($socket, "Secret: blabla\r\n\r\n");

fputs($socket, "Action: Command\r\n");
fputs($socket, "Command: reload\r\n\r\n");
$wrets=fgets($socket,128);

?>


Modified PHP Example

Thanks Gus!:

/etc/asterisk/manager.conf

[general]
enabled = yes
port = 5038
bindaddr = 127.0.0.1

[test]
secret = test
read = system,call,log,verbose,command,agent,user
write = system,call,log,verbose,command,agent,user
deny=0.0.0.0/0.0.0.0
permit=127.0.0.1/255.255.255.255


/home/user/www/junk.php

~/np~
~np~<?php~/np~
~np~ $socket = fsockopen("127.0.0.1","5038", $errno, $errstr, $timeout);~/np~
~np~ fputs($socket, "Action: Login\r\n");~/np~
~np~ fputs($socket, "UserName: test\r\n");~/np~
~np~ fputs($socket, "Secret: test\r\n\r\n");~/np~
~np~# fputs($socket, "\r\n");~/np~
~np~# fputs($socket, "\r\n\r\n");~/np~
~np~ fputs($socket, "Action: ListCommands\r\n\r\n");~/np~
~np~ fputs($socket, "Action: Logoff\r\n\r\n");~/np~
~np~while (!feof($socket)) {~/np~
~np~  $wrets .= fread($socket, 8192);~/np~
~np~}~/np~
~np~fclose($socket);~/np~
~np~echo <<<ASTERISKMANAGEREND~/np~
~np~ASTERISK MANAGER OUTPUT:~/np~
~np~$wrets~/np~
~np~ASTERISKMANAGEREND;~/np~
~np~?>~/np~
~np~


PHP-class from Modified PHP Example

Includes wrappers for database functions, i wrote this to access call-forward settings etc. pnsystem at comhem.se

<?php
class AstMan {

var $socket;
var $error;

function AstMan()
{
$this->socket = FALSE;
$this->error = "";
}

function Login($host="localhost", $username="admin", $password="amp111"){

$this->socket = @fsockopen("127.0.0.1","5038", $errno, $errstr, 1);
if (!$this->socket) {
$this->error = "Could not connect - $errstr ($errno)";
return FALSE;
}else{
stream_set_timeout($this->socket, 1);

$wrets = $this->Query("Action: Login\r\nUserName: $username\r\nSecret: $password\r\nEvents: off\r\n\r\n");

if (strpos($wrets, "Message: Authentication accepted") != FALSE){
return true;
}else{
$this->error = "Could not login - Authentication failed";
fclose($this->socket);
$this->socket = FALSE;
return FALSE;
}
}
}

function Logout(){
if ($this->socket){
fputs($this->socket, "Action: Logoff\r\n\r\n");
while (!feof($this->socket)) {
$wrets .= fread($this->socket, 8192);
}
fclose($this->socket);
$this->socket = "FALSE";
}
return;
}

function Query($query){
$wrets = "";

if ($this->socket === FALSE)
return FALSE;

fputs($this->socket, $query);
do
{
$line = fgets($this->socket, 4096);
$wrets .= $line;
$info = stream_get_meta_data($this->socket);
}while ($line != "\r\n" && $infotimed_out>'timed_out' == false );
return $wrets;
}

function GetError(){
return $this->error;
}

function GetDB($family, $key){
$value = "";

$wrets = $this->Query("Action: Command\r\nCommand: database get $family $key\r\n\r\n");

if ($wrets){
$value_start = strpos($wrets, "Value: ") + 7;
$value_stop = strpos($wrets, "\n", $value_start);
if ($value_start > 8){
$value = substr($wrets, $value_start, $value_stop - $value_start);
}
}
return $value;
}

function PutDB($family, $key, $value){
$wrets = $this->Query("Action: Command\r\nCommand: database put $family $key $value\r\n\r\n");

if (strpos($wrets, "Updated database successfully") != FALSE){
return TRUE;
}
$this->error = "Could not updated database";
return FALSE;
}

function DelDB($family, $key){
$wrets = $this->Query("Action: Command\r\nCommand: database del $family $key\r\n\r\n");

if (strpos($wrets, "Database entry removed.") != FALSE){
return TRUE;
}
$this->error = "Database entry does not exist";
return FALSE;
}


function GetFamilyDB($family){
$wrets = $this->Query("Action: Command\r\nCommand: database show $family\r\n\r\n");
if ($wrets){
$value_start = strpos($wrets, "Response: Follows\r\n") + 19;
$value_stop = strpos($wrets, "--END COMMAND--\r\n", $value_start);
if ($value_start > 18){
$wrets = substr($wrets, $value_start, $value_stop - $value_start);
}
$lines = explode("\n", $wrets);
foreach($lines as $line){
if (strlen($line) > 4){
$value_start = strpos($line, ": ") + 2;
$value_stop = strpos($line, " ", $value_start);
$key = trim(substr($line, strlen($family) + 2, strpos($line, " ") - strlen($family) + 2));
$value$key = trim(substr($line, $value_start));
}
}
return $value;
}
return FALSE;
}
}
?>


Context menu dial from Internet Explorer using PHP

Here is a rough example of how to let users dial by highlighting a phone number on a web page (e.g. a company directory list)
First, you will need the following two PHP pages. The first is initiated by the Context Menu option, the other is chained after that.

<!--// p.php to be initiated from Context Menu //-->
<!--// opening html tag for a jscript/javascript because tiki parsed it out //-->
var hlobj = external.menuArguments.window.document.selection;
var hlrng = hlobj.createRange()
var hlarg = hlrng.text;
location.replace( "http://192.168.165.1/o.php?n=" + hlarg + "&x=<?=$_REQUEST['x']?>" );
<!--// closing html tag for a jscript/javascript //-->


<!--// o.php - initiated by p.php, above //-->
<html>
<head>
<title>Dialing...</title>
</head>
<body style="background: blue; color: yellow; font: 12pt bold sans-serif;">
<?

if (( !empty( $_REQUEST[['n'] ) ) && ( !empty( $_REQUEST[['x'] ) ) )
{
        $num = $_REQUEST[['n'];
        $ext = $_REQUEST[['x'];

        ~np~$num = preg_replace( "/\D/", "", $num );~/np~

        if ( ! empty( $num ) )
        {
                echo "Dialing $num\r\n";

                $timeout = 10;
                $asterisk_ip = "192.168.0.100";

                $socket = fsockopen($asterisk_ip,"5038", $errno, $errstr, $timeout);
                fputs($socket, "Action: Login\r\n");
                fputs($socket, "UserName: manager\r\n");
                fputs($socket, "Secret: gandalf\r\n\r\n");

                $wrets=fgets($socket,128);

                echo $wrets;

                fputs($socket, "Action: Originate\r\n" );
                fputs($socket, "Channel: SIP/$ext\r\n" );
                fputs($socket, "Exten: 9$num\r\n" );
                fputs($socket, "Context: outbound-dialing\r\n" );
                fputs($socket, "Priority: 1\r\n" );
                fputs($socket, "Async: yes\r\n\r\n" );

                $wrets=fgets($socket,128);
                echo $wrets;
        }
        else
        {
                echo "Unable to determine number from (" . $_REQUEST[['n'] . ")\r\n";
        }
}
else
{?>
Please enter a number to dial.
<?}

?>

</body>
</html>

Make sure to substitute appropriate values. The final step is to tweak the registry. Add a new key:
  • HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt\&Dial
In this key, add two values:
  • (Default) REG_SZ "http://192.168.0.100/p.php?x=99
  • Contexts REG_DWORD 0x10
In the default, substitute the IP or name of your * (or the PHP-enabled web server that will talk to *). Change 99 to the extension for that user.
--bsdbigot

Back


PHP CODE: Pulldown Menu containing Manager's CLI Action

Code provided by villkram.
[ if you want to have asterisk CLI in your webpage other than console, you may want to use the function createActionList().
if you want Actions to accept arguments, a seperate class must be provided. :) ]]
Provide servername, username, port ans password for arguments

function createActionList($server,$username,$secret,$port)
{
$socket = fsockopen($server,$port, $errno, $errstr, 1);
fputs($socket, "Action: Login\r\n");
fputs($socket, "UserName: $username\r\n");
fputs($socket, "Secret: $secret\r\n\r\n");
fputs($socket, "Action: ListCommands\r\n\r\n");
fputs($socket, "Action: Logoff\r\n\r\n");
$count=0;$array;
while (!feof($socket)) {
$wrets = fgets($socket, 8192);
$token = strtok($wrets,':(');
$j=0;
while($token!=false & $count>=5)
{
$array[$count][$j]=$token;
$j++; $token = strtok(':(');
}
$count++;
$wrets .= '<br>';
}

echo '<select name="managersAction">';
for($i=5;$i<$count-4;$i++){ echo '<option value="'.$array[$i][0].'">'.$array[$i][1].'</option>'; }
echo '</select>';
fclose($socket);
}
?>


Created by: flobi, Last modification: Sat 08 of Dec, 2007 (17:23 UTC) by swagger


Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - support@voip-info.org

Page Changes | Comments

 

Featured -

Search: