#!/usr/bin/php -q
<?php
// argv[1] contains sip.conf
// argv[2] contains sip-peer
if ($argc != 3) {
execute_agi("VERBOSE \"Wrong number of arguments\" 0");
exit;
}
$output = "no";
$filename = $argv[1];
$sippeer = $argv[2];
$stream = fopen($filename,'r');
if ($stream) {
while ($stream && $line = fgets($stream)) {
if (preg_match("/\[$sippeer\]/", $line, $regs)) {
exec("asterisk -rx \"sip show peer $sippeer\"", $status);
for ($i = 0; $i < count($status); $i++) {
if (preg_match("/Status/",$status[$i])) {
if (preg_match("/OK/",$status[$i])) {
$output = "yes";
}
break;
}
}
}
}
fclose($stream);
}
execute_agi("SET VARIABLE SIPOK $output 1");
function execute_agi( $command )
{
$stdin = STDIN;
$stdout = STDOUT;
$stdlog = STDERR;
$parm_debug_on = false;
fputs( $stdout, $command . "\n" );
fflush( $stdout );
if ($parm_debug_on)
fputs( $stdlog, $command . "\n" );
$resp = fgets( $stdin, 4096 );
if ($parm_debug_on)
fputs( $stdlog, $resp );
if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) )
{
if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match))
{
$arr['code'] = $matches[1];
$arr['result'] = $match[1];
if (isset($match[3]) && $match[3])
$arr['data'] = $match[3];
return $arr;
}
else
{
if ($parm_debug_on)
fputs( $stdlog, "Couldn't figure out returned string, Returning code=$matches[1] result=0\n" );
$arr['code'] = $matches[1];
$arr['result'] = 0;
return $arr;
}
}
else
{
if ($parm_debug_on)
fputs( $stdlog, "Could not process string, Returning -1\n" );
$arr['code'] = -1;
$arr['result'] = -1;
return $arr;
}
}
?>