La idea detrás del astconf-parser
es contar con una función simple pero efectiva que parsee nuestros ficheros de configuración de Asterisk y nos lo devuelva en un array multi-dimensional.
Esta versión soporta la sintaxis estandar de Asterisk, como así también los llamados "Templates". También soporta la instrucción #include
ya que es recursiva. Está originalmente diseñada para leer los ficheros sip.conf
, iax.conf
aunque también es capaz de leer otros como por ejemplo extconfig.conf
y res_mysql.conf
.
Actualmente este código forma parte del paquete Windows Operator Panel
del cual formo parte del equipo de desarrollo. Lo pueden descargar en forma gratuita desde el sitio del Asterisk WOP.
[php]
/*-
* Copyright (c) 2009 Victor Villarreal
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************
File: wop-install.sh
Version: v1.0
Date: 11-03-2009
Brief: Install-Import WOP routine.
**************************************************************/
function sip_clear_line( $string )
{
// Cleaning from spaces and odd characters
$clear01 = trim($string);
// Trimming out commentaries
$clear02 = explode( ";",$clear01 );
return $clear02[0];
}
function wop_parseSIP( $file )
{
GLOBAL $wop_asteriskPath;
// Variables must be static for them to last in time, because of function being recursive
static $sipTemplates = array();
static $users = array();
static $ftpl = 0;
static $sip_line = "";
// Parse sip.conf
$fp = @fopen($file, "r");
if ($fp)
{
static $sip_config = array();
while (!feof($fp))
{
$sip_config_line = fgets($fp, 1024);
$sip_line = sip_clear_line($sip_config_line);
// If string is empty, do nothing. It must be either blank or a commentary
if( !empty($sip_line) )
{
if( strpos($sip_line, "](")!==FALSE )
{
$sip_param = explode( "](",$sip_line );
$sip_param[0] = trim($sip_param[0]);
$sip_param[0] = substr( $sip_param[0],1 );
$sip_param[1] = trim($sip_param[1]);
$sip_param[1] = substr( $sip_param[1],0,-1 );
if( $sip_param[1]=="!" )
{
$ftpl = 1;
$usr = $sip_param[0];
$sipTemplates[] = (string)$usr;
}
else
{
$ftpl = 0;
$usr = $sip_param[0];
$users[(string)$usr]['type'] = "friend";
$users[$usr] = array_merge( $users[$usr],$sipTemplates[$sip_param[1]] );
}
}
else
{
if( strpos($sip_line, "[")!==FALSE )
{
$sip_line = substr( $sip_line,1,-1 );
$ftpl = 0;
$usr = $sip_line;
//$users[] = $usr;
//$users[(string)$usr]['name'] = (string)$usr;
$users[(string)$usr]['type'] = "friend";
}
else
{
if( strpos($sip_line, "=>")!==FALSE )
$sip_param = explode( "=>",$sip_line );
elseif( strpos($sip_line, "#")!==FALSE )
$sip_param = explode( " ",$sip_line );
else
$sip_param = explode( "=",$sip_line );
// $sip_param[0] = Key...
$sip_param[0] = trim($sip_param[0]);
// $sip_param[1] = Value...
$sip_param[1] = trim($sip_param[1]);
if( $sip_param[0]=="#include" )
{
if( $includedFile = wop_parseSIP($wop_asteriskPath.$sip_param[1]) )
{
foreach( $includedFile as $key => $value )
$users[$key] = array_merge( $includedFile[$key] );
}
}
else
{
if($ftpl)
{
// Concatenate parameter if already exists for this user
if( isset($sipTemplates[$usr]) && array_key_exists($sip_param[0],$sipTemplates[$usr]) )
$sipTemplates[(string)$usr][(string)$sip_param[0]] .= ";".$sip_param[1];
else
$sipTemplates[(string)$usr][(string)$sip_param[0]] = (string)$sip_param[1];
}
else
{
// Concatenate parameter if already exists for this user
if( isset($users[$usr]) && array_key_exists( (string)$sip_param[0], $users[$usr]) && $sip_param[0]=="allow" )
$users[(string)$usr][(string)$sip_param[0]] .= ";".$sip_param[1];
else
$users[(string)$usr][(string)$sip_param[0]] = (string)$sip_param[1];
}
}
}
}
}
}
fclose($fp);
return $users;
}
else
return FALSE;
}
[/php]
publicado por Víctor Villarreal en Software