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

304 lines
7.7 KiB
PHP

<?php
#
# Startup.php
# Sample script for self configuration with Digium/Asterisk platform.
# Copyright Aastra Telecom 2007
#
#
# Includes
#
require_once('include/config.inc.php');
include_once('include/backend.inc.php');
#
# Aastra_decode_HTTP_header
#
# Returns an array
# 0 Phone Type
# 1 Phone MAC Address
# 2 Phone firmware version
# 3 IP address
#
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";
}
$value[3]=$_SERVER["REMOTE_ADDR"];
return($value);
}
#
# lookup_config_file(extension)
#
# Returns 1 if extension already in use
#
function lookup_config_file($extension)
{
$config="startup.cfg";
# Init return
$return=0;
# Read config file
$array = @parse_ini_file($config, true);
# Test MAC address
if($array[$extension]['mac']!="") $return=1;
return($return);
}
#
# update_config_file(extension,mac,ip,model)
#
# Update the config file with the new extension
#
function update_config_file($extension,$mac,$ip,$model)
{
$config="startup.cfg";
# Read config file
$array = @parse_ini_file($config, true);
if($array==NULL) $array=array();
# Update value
$array[$extension]['mac']=$mac;
$array[$extension]['ip']=$ip;
$array[$extension]['model']=$model;
# Update config file
reset($array);
$handle = @fopen($config, "w");
if($handle)
{
while ($v = current($array))
{
fputs($handle,"[".key($array)."]"."\n");
fputs($handle,"mac=".$v['mac']."\n");
fputs($handle,"ip=".$v['ip']."\n");
fputs($handle,"model=".$v['model']."\n\n");
next($array);
}
fclose($handle);
}
}
#
# create_mac(extension,mac,username,secret,callerid,model,version)
#
# Creates the MAC.cfg file for the user.
#
function create_mac($mac,$extension,$username,$secret,$callerid,$model,$version)
{
Global $AA_PROXY_SERVER,$AA_REGISTRAR_SERVER;
$value=preg_split("/ /",$callerid);
$result=$value[0]." ".$value[1];
$result=preg_replace("/</","(",$result);
$result=preg_replace("/>/",")",$result);
# Prepare replace strings
$search=array('/\$\$AA_SIPAUTHNAME_AA\$\$/','/\$\$AA_SIPSECRET_AA\$\$/','/\$\$AA_SIPUSERNAME_AA\$\$/','/\$\$AA_SIPCALLERID_AA\$\$/','/\$\$AA_PROXY_SERVER_AA\$\$/','/\$\$AA_REGISTRAR_SERVER_AA\$\$/');
$replace=array($username,$secret,$extension,$result,$AA_PROXY_SERVER,$AA_REGISTRAR_SERVER);
$read = @fopen($model.".cfg", "r");
if($read)
{
$write = @fopen("/tftpboot/".$mac.".cfg", "w");
if($write)
{
# Create file header
while($line=fgets($read,200))
{
$line = preg_replace($search, $replace, $line);
fputs($write,$line);
}
fputs($write,"\n");
fclose($write);
}
fclose($read);
}
}
##########################################
# GLOBAL VARIABLES
$XML_SERVER = "http://".$_SERVER['SERVER_ADDR'].$_SERVER['SCRIPT_NAME'];
$AA_PROXY_SERVER = $_SERVER['SERVER_ADDR'];
$AA_REGISTRAR_SERVER = $_SERVER['SERVER_ADDR'];
$location = "/etc/asterisk/";
##########################################
# Retrieve parameters
$extension=$_GET["extension"];
$password=$_GET["password"];
$action=$_GET["action"];
$step=$_GET["step"];
# Set content type
header("Content-Type: text/xml");
if($action=="countdown")
{
# Display Reboot message
$output = "<AastraIPPhoneTextScreen destroyOnExit=\"yes\">\n";
$output .= "<Title>Reboot needed</Title>\n";
$output .= "<Text>Your phone will reboot in ".$step." seconds.</Text>\n";
$step--;
$output .= "</AastraIPPhoneTextScreen>\n";
header("Content-Type: text/xml");
header("Content-Length: ".strlen($output));
if($step==0) header("Refresh: 1; url=".$XML_SERVER."?action=reboot");
else header("Refresh: 1; url=".$XML_SERVER."?action=countdown&step=".$step);
echo $output;
exit;
}
if($action=="reboot")
{
$output = "<AastraIPPhoneExecute>\n";
$output .= "<ExecuteItem URI=\"Command: Reset\"/>\n";
$output .= "</AastraIPPhoneExecute>\n";
header("Content-Length: ".strlen($output));
echo $output;
exit;
}
# Input Extension
if ($extension=="")
{
$output = "<AastraIPPhoneInputScreen type=\"number\" LockIn=\"yes\">\n";
$output .= "<Title>Initial startup</Title>\n";
$output .= "<Prompt>Enter Extension</Prompt>\n";
$output .= "<URL>$XML_SERVER</URL>\n";
$output .= "<Parameter>extension</Parameter>\n";
$output .= "<Default></Default>\n";
$output .= "</AastraIPPhoneInputScreen>\n";
header("Content-Length: ".strlen($output));
echo $output;
exit;
}
# Input Password
if ($password=="")
{
$output = "<AastraIPPhoneInputScreen type=\"number\" password=\"yes\" LockIn=\"yes\" destroyOnExit=\"yes\">\n";
$output .= "<Title>Initial startup</Title>\n";
$output .= "<Prompt>Enter Password</Prompt>\n";
$output .= "<URL>$XML_SERVER?extension=$extension</URL>\n";
$output .= "<Parameter>password</Parameter>\n";
$output .= "<Default></Default>\n";
$output .= "</AastraIPPhoneInputScreen>\n";
header("Content-Length: ".strlen($output));
echo $output;
exit;
}
# IF authentication failed
if (!$userinfo = verify_user($extension, $password))
{
# Display error
$output = "<AastraIPPhoneTextScreen LockIn=\"yes\" destroyOnExit=\"yes\">\n";
$output .= "<Title>Authentication failed</Title>\n";
$output .= "<Text>Wrong user and password.</Text>\n";
$output .= "</AastraIPPhoneTextScreen>\n";
header("Content-Type: text/xml");
header("Content-Length: ".strlen($output));
echo $output;
exit;
}
# IF already configured
if(lookup_config_file($extension)==1)
{
# Display error
$output = "<AastraIPPhoneTextScreen LockIn=\"yes\" destroyOnExit=\"yes\">\n";
$output .= "<Title>Error</Title>\n";
$output .= "<Text>Extension already in use.</Text>\n";
$output .= "</AastraIPPhoneTextScreen>\n";
header("Content-Type: text/xml");
header("Content-Length: ".strlen($output));
echo $output;
exit;
}
# Get all the user data
$sip_array = parse_ini_file($location."sip_additional.conf", true);
# If user not found
if ($sip_array[$extension]==NULL)
{
# Display error
$output = "<AastraIPPhoneTextScreen LockIn=\"yes\" destroyOnExit=\"yes\">\n";
$output .= "<Title>Internal error</Title>\n";
$output .= "<Text>Extension is not provisioned.</Text>\n";
$output .= "</AastraIPPhoneTextScreen>\n";
header("Content-Type: text/xml");
header("Content-Length: ".strlen($output));
echo $output;
exit;
}
else
{
# Collect data
$username=$sip_array[$extension]['username'];
$secret=$sip_array[$extension]['secret'];
$callerid=$sip_array[$extension]['callerid'];
}
# Get MAC address and type of phone
$value=Aastra_decode_HTTP_header();
# Create mac.cfg
create_mac($value[1],$extension,$username,$secret,$callerid,$value[0],$value[2]);
# Update config file
update_config_file($extension,$value[1],$value[3],$value[0]);
# Create Reboot screen
$output = "<AastraIPPhoneTextScreen destroyOnExit=\"yes\">\n";
switch($value[0])
{
case "Aastra53i":
$output = "<AastraIPPhoneTextScreen>\n";
$output .= "<Title>REBOOT</Title>\n";
$output .= "<Text>Reboot</Text>\n";
$output .= "</AastraIPPhoneTextScreen>\n";
header("Refresh: 1; url=".$XML_SERVER."?action=reboot");
break;
default:
$output = "<AastraIPPhoneTextScreen>\n";
$output .= "<Title>Reboot needed</Title>\n";
$output .= "<Text>Your phone will reboot in 5 seconds.</Text>\n";
$output .= "</AastraIPPhoneTextScreen>\n";
header("Refresh: 1; url=".$XML_SERVER."?action=countdown&step=4");
break;
}
# Display XML Object
header("Content-Type: text/xml");
header("Content-Length: ".strlen($output));
echo $output;
exit;
?>