Files
aastra-xml-php/asterisk sample source/dnd.php
2018-02-15 22:59:05 +01:00

151 lines
3.8 KiB
PHP

<?
#####################################################################
# Asterisk DND for Aastra SIP Phones R2.0.0 or better
#
# php source code
# Provided by Aastra Telecom Ltd 2006
#
# @user=extension
# @action=change or check
# @key identification of the key used for the function
#####################################################################
include (dirname(__FILE__)."/phpagi/misc.php");
include (dirname(__FILE__)."/phpagi/phpagi-asmanager.php");
#####################################################################
# Aastra_decode_HTTP_header
#
# Returns an array
# 0 Phone Type
# 1 Phone MAC Address
# 2 Phone firmware version
#####################################################################
function Aastra_decode_HTTP_header()
{
$user_agent=$_SERVER["HTTP_USER_AGENT"];
if(stristr($user_agent,"Aastra"))
{
$value=preg_split("/ MAC:/",$user_agent);
$fin=preg_split("/ /",$value[1]);
$value[1]=preg_replace("/\-/","",$fin[0]);
$value[2]=preg_replace("/V:/","",$fin[1]);
}
else
{
$value[0]="MSIE";
$value[1]="NA";
$value[2]="NA";
}
return($value);
}
function Aastra_manage_dnd($user,$action)
{
# Connect to AGI
$as = new AGI_AsteriskManager();
$res = $as->connect();
#DND GET
$res = $as->Command('database get DND '.$user);
$line=split("\n", $res['data']);
$value=split(" ", $line[0]);
if($value[1]=="YES") $dnd=1;
if($dnd==0)
{
$value=split(" ", $line[1]);
if($value[1]=="YES") $dnd=1;
}
if($action=='change')
{
# change DND status
if($dnd==0)
{
$res = $as->Command('database put DND '.$user.' YES');
$dnd=1;
}
else
{
$res = $as->Command('database del DND '.$user);
$dnd=0;
}
}
# Disconnect properly
$as->disconnect();
return($dnd);
}
#####################################################################
# Global parameters
#####################################################################
$Server = "http://".$_SERVER['SERVER_ADDR'].$_SERVER['SCRIPT_NAME'];
$dnd=0;
# Retrieve parameters
$user=$_GET['user'];
$action=$_GET['action'];
$status=$_GET['status'];
$key=$_GET['key'];
# Force default action
if($action=="") $action="change";
# Get header info
$header=Aastra_decode_HTTP_header();
# Setup header type
header("Content-Type: text/xml");
# Depending on action
switch($action)
{
case 'display':
$output = "<AastraIPPhoneFormattedTextScreen destroyOnExit=\"yes\" Timeout=\"2\">\n";
$output .= "<Line/>\n";
$output .= "<Line/>\n";
if ($status==1) $output .= "<Line Size=\"double\" Align=\"center\">DND activated</Line>\n";
else $output .= "<Line Size=\"double\" Align=\"center\">DND deactivated</Line>\n";
$output .= "<SoftKey index=\"6\">\n";
$output .= "<Label> </Label>\n";
$output .= "<URI>SoftKey:Exit</URI>\n";
$output .= "</SoftKey>\n";
$output .= "</AastraIPPhoneFormattedTextScreen>\n";
break;
case 'msg':
$output = "<AastraIPPhoneStatus>\n";
$output .= "<Session>CFDND</Session>\n";
if ($status==1) $output .= "<Message index=\"0\">DND activated</Message>\n";
else $output .= "<Message index=\"0\"></Message>\n";
$output .= "</AastraIPPhoneStatus>\n";
break;
case 'change':
case 'check':
$dnd=Aastra_manage_dnd($user,$action);
$output = "<AastraIPPhoneExecute>\n";
$output .= "<ExecuteItem URI=\"".$Server."?action=msg&amp;status=".$dnd."\"/>\n";
if ($dnd==1) $output .= "<ExecuteItem URI=\"Led: ".$key."=on\"/>\n";
else $output .= "<ExecuteItem URI=\"Led: ".$key."=off\"/>\n";
switch($header[0])
{
case "Aastra53i":
break;
default:
if($action=='change') $output .= "<ExecuteItem URI=\"".$Server."?action=display&amp;status=".$dnd."\"/>\n";
break;
}
$output .= "</AastraIPPhoneExecute>\n";
break;
}
header("Content-Length: ".strlen($output));
echo $output;
?>