Changeset 4661

Show
Ignore:
Timestamp:
08/02/07 23:06:46 (1 year ago)
Author:
p_lindheimer
Message:

replace sip, iax, zap and queues perl script with php functions. Also add call-limit to sip extensions for Asterisk 1.4 and above, see #2127

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/branches/2.3/amp_conf/bin/libfreepbx.confgen.php

    r4594 r4661  
    1 #!/usr/bin/php -q 
    2  
    31<?php 
    4 ini_set('error_reporting', E_ALL & ~E_NOTICE); 
    5  
    6 define("AMP_CONF", "/etc/amportal.conf"); 
    7 $amportalconf = AMP_CONF; 
    8  
    9 //define("ASTERISK_CONF", "/etc/asterisk/asterisk.conf"); 
    10 define("WARNING_BANNER", "; "._("do not edit this file, this is an auto-generated file by freepbx\n")."; "._("all modifications must be done from the web gui")."\n\n\n"); 
    11  
    12 // Emulate gettext extension functions if gettext is not available 
    13 if (!function_exists('_')) { 
    14         function _($str) { 
    15                 return $str; 
    16         } 
    17 
    18  
    19 function out($text) { 
    20         echo $text."\n"; 
    21 
    22  
    23 function outn($text) { 
    24         echo $text; 
    25 
    26  
    27 function error($text) { 
    28         echo "[ERROR] ".$text."\n"; 
    29 
    30  
    31 function fatal($text, $extended_text="", $type="FATAL") { 
    32         echo "[$type] ".$text." ".$extended_text."\n"; 
    33  
    34         $nt = notifications::create($db); 
    35         $nt->add_critical('retrieve_conf', $type, $text, $extended_text); 
    36  
    37         exit(1); 
    38 
    39  
    40 function debug($text) { 
    41         global $debug; 
    42          
    43         if ($debug) echo "[DEBUG-preDB] ".$text."\n"; 
    44 
    45  
    46 function showHelp() { 
    47         out(_("Optional parameters:")); 
    48         out(_("  --help, -h, -?           Show this help")); 
    49         out(_("  --debug                  Enable debug output")); 
    50         out(_("  --dry-run                Don't actually do anything")); 
    51 
    52  
    53  
    54 // bootstrap retrieve_conf by getting the AMPWEBROOT since that is currently where the necessary 
    55 // functions.inc.php resides, and then use that parser to properly parse the file and get all 
    56 // the defaults as needed. 
    57 // 
    58 function parse_amportal_conf_bootstrap($filename) { 
    59         $file = file($filename); 
    60         foreach ($file as $line) { 
    61                 if (preg_match("/^\s*([\w]+)\s*=\s*\"?([\w\/\:\.\*\%-]*)\"?\s*([;#].*)?/",$line,$matches)) { 
    62                         $conf[ $matches[1] ] = $matches[2]; 
    63                 } 
    64         } 
    65         if ( !isset($conf["AMPWEBROOT"]) || ($conf["AMPWEBROOT"] == "")) { 
    66                 $conf["AMPWEBROOT"] = "/var/www/html"; 
     2 
     3function generate_configurations_sip($ast_version) { 
     4 
     5        global $amp_conf; 
     6        global $db; 
     7 
     8        $additional = ""; 
     9 
     10        $sip_conf = $amp_conf['ASTETCDIR']."/sip_additional.conf"; 
     11        $sip_reg  = $amp_conf['ASTETCDIR']."/sip_registrations.conf"; 
     12 
     13        $table_name = "sip"; 
     14 
     15        $warning_banner = _("; do not edit this file, this is an auto-generated file by freepbx\n; all modifications must be done from the web gui\n\n"); 
     16 
     17        // Asterisk 1.4 requires call-limit be set for hints to work properly 
     18        // 
     19        if (version_compare($ast_version['version'], "1.4", "ge")) {  
     20                $call_limit = "call-limit=50\n"; 
    6721        } else { 
    68                 $conf["AMPWEBROOT"] = rtrim($conf["AMPWEBROOT"],'/'); 
    69         } 
    70  
    71         return $conf; 
    72 
    73  
    74 /** Adds a trailing slash to a directory, if it doesn't already have one 
    75  */ 
    76 function addslash($dir) { 
    77         return (($dir[ strlen($dir)-1 ] == '/') ? $dir : $dir.'/'); 
    78 
    79  
    80  
    81 /********************************************************************************************************************/ 
    82  
    83 // **** Make sure we have STDIN etc 
    84  
    85 // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline 
    86 if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) { 
    87         define('STDIN',fopen("php://stdin","r")); 
    88         define('STDOUT',fopen("php://stdout","r")); 
    89         define('STDERR',fopen("php://stderr","r")); 
    90         register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) ); 
    91 
    92     
    93 // **** Make sure we have PEAR's DB.php, and include it 
    94  
    95 outn(_("Checking for PEAR DB..")); 
    96 if (! @ include('DB.php')) { 
    97         out(_("FAILED")); 
    98         fatal(_("PEAR Missing"),sprintf(_("PEAR must be installed (requires DB.php). Include path: %s "), ini_get("include_path"))); 
    99 
    100 out(_("OK")); 
    101  
    102  
    103 // **** Make sure we have PEAR's GetOpts.php, and include it 
    104  
    105 outn(_("Checking for PEAR Console::Getopt..")); 
    106 if (! @ include("Console/Getopt.php")) { 
    107         out(_("FAILED")); 
    108         fatal(_("PEAR Getopt.php Missing"),sprintf(_("PEAR must be installed (requires Console/Getopt.php). Include path: %s"), ini_get("include_path"))); 
    109 
    110 out(_("OK")); 
    111  
    112  
    113 // **** Parse out command-line options 
    114  
    115 $shortopts = "h?u:p:"; 
    116 $longopts = array("help","debug","dry-run","run-install","amportalconf="); 
    117  
    118 $args = Console_Getopt::getopt(Console_Getopt::readPHPArgv(), $shortopts, $longopts); 
    119 if (is_object($args)) { 
    120         // assume it's PEAR_ERROR 
    121         out($args->message); 
    122         exit(255); 
    123 
    124  
    125 $debug = false; 
    126 $dryrun = false; 
    127 $run_install = false; 
    128  
    129 foreach ($args[0] as $arg) { 
    130         switch ($arg[0]) { 
    131                 case "--help": case "h": case "?": 
    132                         showHelp(); 
    133                         exit(10); 
    134                 break; 
    135                 case "--dry-run": 
    136                         out(_("Dry-run only, no files will be written")); 
    137                         $dryrun = true; 
    138                 break; 
    139                 case "--debug": 
    140                         $debug = true; 
    141                         debug(_("Debug mode enabled")); 
    142                 break; 
    143                 case "--run-install": 
    144                         $run_install = true; 
    145                         out(_("Running module install.php and install.sql scripts")); 
    146                 break; 
    147                 case "--amportalconf": 
    148                         $amportalconf = $arg[1]; 
    149                         out(sprintf(_("Using %s configuration file"), $amportalconf)); 
    150                 break; 
    151         } 
    152 
    153  
    154 // **** Check for amportal.conf 
    155  
    156 outn(sprintf(_("Checking for %s "), $amportalconf)._("..")); 
    157 if (!file_exists($amportalconf)) { 
    158         fatal(_("amportal.conf access problem: "),sprintf(_("The %s file does not exist, or is inaccessible"), $amportalconf)); 
    159 
    160 out(_("OK")); 
    161  
    162 // **** read amportal.conf 
    163  
    164 outn(sprintf(_("Bootstrapping %s .."), $amportalconf)); 
    165 $amp_conf = parse_amportal_conf_bootstrap($amportalconf); 
    166 if (count($amp_conf) == 0) { 
    167         fatal(_("amportal.conf parsing failure"),sprintf(_("no entries found in %s"), $amportalconf)); 
    168 
    169 out(_("OK")); 
    170  
    171 outn(sprintf(_("Parsing %s .."), $amportalconf)); 
    172 require_once($amp_conf['AMPWEBROOT']."/admin/functions.inc.php"); 
    173 $amp_conf = parse_amportal_conf($amportalconf); 
    174 if (count($amp_conf) == 0) { 
    175         fatal(_("amportal.conf parsing failure"),sprintf(_("no entries found in %s"), $amportalconf)); 
    176 
    177 out(_("OK")); 
    178  
    179 $asterisk_conf_file = $amp_conf["ASTETCDIR"]."/asterisk.conf"; 
    180 outn(sprintf(_("Parsing %s .."), $asterisk_conf_file)); 
    181 $asterisk_conf = parse_asterisk_conf($asterisk_conf_file); 
    182 if (count($asterisk_conf) == 0) { 
    183         fatal(_("asterisk.conf parsing failure"),sprintf(_("no entries found in %s"), $asterisk_conf_file)); 
    184 
    185 out(_("OK")); 
    186  
    187 // **** Connect to database 
    188  
    189 outn(_("Connecting to database..")); 
    190  
    191 # the engine to be used for the SQL queries, 
    192 # if none supplied, backfall to mysql 
    193 $db_engine = "mysql"; 
    194 if (isset($amp_conf["AMPDBENGINE"])){ 
    195         $db_engine = $amp_conf["AMPDBENGINE"]; 
    196 
    197  
    198 // Define the notification class for logging to the dashboard 
    199 // 
    200 $nt = notifications::create($db); 
    201  
    202 // **** Create symlinks array 
    203 $symlink_dirs = array(); 
    204 $symlink_dirs['sounds'] = $amp_conf['ASTVARLIBDIR'].'/sounds'; 
    205 $symlink_dirs['bin']    = $amp_conf['AMPBIN']; 
    206 $symlink_dirs['etc']    = $amp_conf['ASTETCDIR']; 
    207 $symlink_dirs['images'] = $amp_conf['AMPWEBROOT']."/admin/images";  
    208  
    209 $cp_errors = ""; 
    210 $cp_dirs = array(); 
    211 $cp_dirs['agi-bin'] = $amp_conf['ASTAGIDIR']; 
    212  
    213 switch ($db_engine) 
    214 
    215         case "pgsql": 
    216         case "mysql": 
    217                 /* datasource in in this style: 
    218                 dbengine://username:password@host/database */ 
    219          
    220                 $db_user = $amp_conf["AMPDBUSER"]; 
    221                 $db_pass = $amp_conf["AMPDBPASS"]; 
    222                 $db_host = $amp_conf["AMPDBHOST"]; 
    223                 $db_name = $amp_conf["AMPDBNAME"]; 
    224          
    225                 $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name; 
    226                 $db = DB::connect($datasource); // attempt connection 
    227                 break; 
    228          
    229         case "sqlite": 
    230                 require_once('DB/sqlite.php'); 
    231          
    232                 if (!isset($amp_conf["AMPDBFILE"])) 
    233                         fatal(_("AMPDBFILE not setup properly"),sprintf(_("You must setup properly AMPDBFILE in %s "), $amportalconf)); 
    234          
    235                 if (isset($amp_conf["AMPDBFILE"]) == "") 
    236                         fatal(_("AMPDBFILE not setup properly"),sprintf(_("AMPDBFILE in %s cannot be blank"), $amportalconf)); 
    237          
    238                 $DSN = array ( 
    239                         "database" => $amp_conf["AMPDBFILE"], 
    240                         "mode" => 0666 
    241                 ); 
    242          
    243                 $db = new DB_sqlite(); 
    244                 $db->connect( $DSN ); 
    245                 break; 
    246          
    247         case "sqlite3": 
    248                 if (!isset($amp_conf["AMPDBFILE"])) 
    249                         fatal("You must setup properly AMPDBFILE in $amportalconf"); 
    250                          
    251                 if (isset($amp_conf["AMPDBFILE"]) == "") 
    252                         fatal("AMPDBFILE in $amportalconf cannot be blank"); 
    253  
    254                 require_once('DB/sqlite3.php'); 
    255                 $datasource = "sqlite3:///" . $amp_conf["AMPDBFILE"] . "?mode=0666"; 
    256                 $db = DB::connect($datasource); 
    257                 break; 
    258  
    259         default: 
    260                 fatal( "Unknown SQL engine: [$db_engine]"); 
    261 
    262  
    263 if(DB::isError($db)) { 
    264         out(_("FAILED")); 
    265         debug($db->userinfo); 
    266         fatal(_("database connection failure"),("failed trying to connect to the configured database")); 
    267          
    268 
    269 out(_("OK")); 
    270  
    271  
    272 //TODO : make this engine-neutral 
    273 outn(_("Connecting to Asterisk manager interface..")); 
    274 // connect to asterisk manager 
    275 require_once($amp_conf['AMPWEBROOT'].'/admin/common/php-asmanager.php'); 
    276 $astman = new AGI_AsteriskManager();  
    277 if (! $res = $astman->connect("127.0.0.1:".$amp_conf["ASTMANAGERPORT"], $amp_conf["AMPMGRUSER"] , $amp_conf["AMPMGRPASS"])) { 
    278         out(_("FAILED")); 
    279         fatal(_("Asterisk Manager Connection Failure"),sprintf(_("Failed to connect to the Asterisk manager through port: %s"), $amp_conf['ASTMANAGERPORT'])); 
    280 
    281 out(_("OK")); 
    282  
    283 //include common functions 
    284 require_once($amp_conf['AMPWEBROOT']."/admin/extensions.class.php"); 
    285 freepbx_log("retrieve_conf", "devel-debug", "Started retrieve_conf, DB Connection OK"); 
    286  
    287 // query for our modules 
    288 // var_dump( $db ); 
    289 $modules = module_getinfo(); 
    290  
    291 //Putting the core module last, to move outbound-allroutes  
    292 // last in from-internals-additional 
    293 if (array_key_exists('core', $modules)) { 
    294         $core_tmp = $modules['core']; 
    295         unset($modules['core']); 
    296         $modules['core'] = $core_tmp; 
    297 
    298  
    299 // include any module global functions 
    300 if(is_array($modules)){ 
    301         foreach($modules as $key => $module) { 
    302                 //only use this module if it's enabled (status=2) 
    303                 if (isset($module['status']) && $module['status'] == MODULE_STATUS_ENABLED) { 
    304                         // Make sure the module is installed and up to date 
    305                         if ($run_install) module_install($key); 
    306                         // active_modules array used in genConf function 
    307                         $active_modules[] = $key; 
    308                         //include module functions 
    309                         if (is_file($amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php")) { 
    310                                 freepbx_log('retrieve_conf', 'devel-debug', 'Including '.$amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php"); 
    311                                 include_once($amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php"); 
    312                                 freepbx_log('retrieve_conf', 'devel-debug', $amp_conf['AMPWEBROOT']."/admin/modules/{$key}/functions.inc.php processed OK"); 
     22                $call_limit = ""; 
     23        } 
     24 
     25        $sip_conf_fh = fopen($sip_conf,"w"); 
     26        if ($sip_conf_fh === false) { 
     27                fatal(_("Cannot write SIP configurations"),sprintf(_("Failed creating/overwriting SIP extensions file: %s"),$sip_conf)); 
     28        } 
     29        $sip_reg_fh = fopen($sip_reg,"w"); 
     30        if ($sip_reg_fh === false) { 
     31                fatal(_("Cannot write SIP registrations"),sprintf(_("Failed creating/overwriting SIP registrations file: %s"),$sip_reg)); 
     32        } 
     33 
     34        fwrite($sip_conf_fh, $warning_banner); 
     35        fwrite($sip_reg_fh, $warning_banner); 
     36 
     37        $sql = "SELECT keyword,data from $table_name where id=-1 and keyword <> 'account' and flags <> 1"; 
     38        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     39        if(DB::IsError($results)) { 
     40        die($results->getMessage()); 
     41        } 
     42        foreach ($results as $result) { 
     43                $additional .= $result['keyword']."=".$result['data']."\n"; 
     44        } 
     45 
     46        // items with id like 9999999% get put in registrations file 
     47        // 
     48        $sql = "SELECT keyword,data from $table_name where id LIKE '9999999%' and keyword <> 'account' and flags <> 1"; 
     49        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     50        if(DB::IsError($results)) { 
     51        die($results->getMessage()); 
     52        } 
     53 
     54        $top = ""; 
     55        foreach ($results as $result) { 
     56                $top .= $result['keyword']."=".$result['data']."\n"; 
     57        } 
     58 
     59        fwrite($sip_reg_fh, $top."\n"); 
     60 
     61        $sql = "SELECT data,id from $table_name where keyword='account' and flags <> 1 group by data"; 
     62        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     63        if(DB::IsError($results)) { 
     64        die($results->getMessage()); 
     65        } 
     66 
     67        foreach ($results as $result) { 
     68                $account = $result['data']; 
     69                $id = $result['id']; 
     70                fwrite($sip_conf_fh,"[$account]\n"); 
     71 
     72                $sql = "SELECT keyword,data from $table_name where id=$id and keyword <> 'account' and flags <> 1 order by keyword DESC"; 
     73                $results2 = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     74                if(DB::IsError($results2)) { 
     75                die($results2->getMessage()); 
     76                } 
     77                foreach ($results2 as $result2) { 
     78                        $options = explode("&", $result2['data']); 
     79                        foreach ($options as $option) { 
     80                                fwrite($sip_conf_fh,$result2['keyword']."=$option\n"); 
    31381                        } 
    314  
    315                         // create symlinks for files in appropriate sub directories 
    316                         symlink_subdirs( $amp_conf['AMPWEBROOT'].'/admin/modules/'.$key ); 
    317                         cp_subdirs( $amp_conf['AMPWEBROOT'].'/admin/modules/'.$key ); 
    318                 } 
    319         } 
    320 
    321  
    322 // create an object of the extensions class 
    323 require_once($amp_conf['AMPWEBROOT']."/admin/extensions.class.php"); 
    324 $ext = new extensions; 
    325  
    326 // create objects for any module classes 
    327 // currently only 1 class can be declared per module, not sure if that will be an issue 
    328 if(isset($active_modules) && is_array($active_modules)){ 
    329         foreach($active_modules as $active_module) {  
    330                 freepbx_log('retrieve_conf', 'devel-debug', "Creating ".$active_module."_conf class"); 
    331                 $classname = $active_module."_conf"; 
    332                 if(class_exists($classname)) { 
    333                         ${$classname} = new $classname; 
    334                 } 
    335         } 
    336 
    337  
    338  
    339 $engineinfo = engine_getinfo(); 
    340 if($engineinfo['version'] == 0){ 
    341         freepbx_log('retrieve_conf', 'fatal', "Failed to get engine information (engine_getinfo: {$engineinfo['engine']})"); 
    342         fatal(_("Failed to get engine_info"),_("retreive_conf failed to get engine information and cannot configure up a softwitch with out it. Error: {$engineinfo['engine']}")); 
    343 
    344 // was setting these variables before, assume we still need them 
    345 $engine = $engineinfo['engine']; 
    346 $version = $engineinfo['version']; 
    347  
    348 // run all of the *_get_config and _hookGet_config functions, which will populate the appropriate objects 
    349 if(isset($active_modules) && is_array($active_modules)){ 
    350         foreach($active_modules as $module) { 
    351                 $funcname = $module."_get_config"; 
    352                 if (function_exists($funcname)) {  
    353                         freepbx_log('retrieve_conf', 'devel-debug', 'Calling '.$funcname.'()'); 
    354                         $funcname($engine); 
    355                 } 
    356         } 
    357         foreach($active_modules as $module) { 
    358                 $funcname = $module."_hookGet_config"; 
    359                 if (function_exists($funcname)) {  
    360                         freepbx_log('retrieve_conf', 'devel-debug', 'Calling '.$funcname.'()'); 
    361                         $funcname($engine); 
    362                 } 
    363         } 
    364 
    365  
    366 // extensions_additional.conf 
    367 // create the from-internal-additional context so other can add to it 
    368 $ext->add('from-internal-additional', 'h', '', new ext_hangup('')); 
    369 //echo $ext->get_filename(); 
    370 //echo $ext->generateConf(); 
    371 write_file($ext->get_filename(),$ext->generateConf()); 
    372  
    373 // now we write out our conf files for modules 
    374 // check for any objects for each of the active modules 
    375 // ** conferences is an example of a module that write a conf 
    376 if(isset($active_modules) && is_array($active_modules)){ 
    377         foreach($active_modules as $active_module) {  
    378                 $classname = $active_module."_conf"; 
    379                 if(class_exists($classname) && get_class(${$classname}) !== false) { 
    380                         //echo ${$classname}->get_filename(); 
    381                         //echo ${$classname}->generateConf(); 
    382                          
    383                         // if the module returns an array, it wants to write multiple files 
    384                         // ** pinsets is an example of a module that does this 
    385                         if (is_array(${$classname}->get_filename())) { 
    386                                 foreach(${$classname}->get_filename() as $modconf) { 
    387                                         freepbx_log('retrieve_conf', 'devel-debug', 'generateConf from '.$classname.'->'.$modconf.''); 
    388                                         write_file($modconf,${$classname}->generateConf($modconf)); 
    389                                 } 
     82                } 
     83                if ($call_limit && ($id < 999900)) { 
     84                        fwrite($sip_conf_fh, $call_limit); 
     85                } 
     86                fwrite($sip_conf_fh, $additional."\n"); 
     87        } 
     88        fclose($sip_conf_fh); 
     89        fclose($sip_reg_fh); 
     90
     91 
     92 
     93function generate_configurations_iax($ast_version) { 
     94 
     95        global $amp_conf; 
     96        global $db; 
     97 
     98        $additional = ""; 
     99 
     100        $iax_conf = $amp_conf['ASTETCDIR']."/iax_additional.conf"; 
     101        $iax_reg  = $amp_conf['ASTETCDIR']."/iax_registrations.conf"; 
     102 
     103        $table_name = "iax"; 
     104 
     105        $warning_banner = _("; do not edit this file, this is an auto-generated file by freepbx\n; all modifications must be done from the web gui\n\n"); 
     106 
     107        $iax_conf_fh = fopen($iax_conf,"w"); 
     108        if ($iax_conf_fh === false) { 
     109                fatal(_("Cannot write IAX configurations"),sprintf(_("Failed creating/overwriting IAX extensions file: %s"),$iax_conf)); 
     110        } 
     111        $iax_reg_fh = fopen($iax_reg,"w"); 
     112        if ($iax_reg_fh === false) { 
     113                fatal(_("Cannot write IAX registrations"),sprintf(_("Failed creating/overwriting IAX registrations file: %s"),$iax_reg)); 
     114        } 
     115 
     116        fwrite($iax_conf_fh, $warning_banner); 
     117        fwrite($iax_reg_fh, $warning_banner); 
     118 
     119        $sql = "SELECT keyword,data from $table_name where id=-1 and keyword <> 'account' and flags <> 1"; 
     120        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     121        if(DB::IsError($results)) { 
     122        die($results->getMessage()); 
     123        } 
     124        foreach ($results as $result) { 
     125                $additional .= $result['keyword']."=".$result['data']."\n"; 
     126        } 
     127 
     128        // items with id like 9999999% get put in the registration file 
     129        // 
     130        $sql = "SELECT keyword,data from $table_name where id LIKE '9999999%' and keyword <> 'account' and flags <> 1"; 
     131        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     132        if(DB::IsError($results)) { 
     133        die($results->getMessage()); 
     134        } 
     135 
     136        $top = ""; 
     137        foreach ($results as $result) { 
     138                $top .= $result['keyword']."=".$result['data']."\n"; 
     139        } 
     140 
     141        fwrite($iax_reg_fh, $top."\n"); 
     142 
     143        $sql = "SELECT data,id from $table_name where keyword='account' and flags <> 1 group by data"; 
     144        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     145        if(DB::IsError($results)) { 
     146        die($results->getMessage()); 
     147        } 
     148 
     149        foreach ($results as $result) { 
     150                $account = $result['data']; 
     151                $id = $result['id']; 
     152                fwrite($iax_conf_fh,"[$account]\n"); 
     153 
     154                $sql = "SELECT keyword,data from $table_name where id=$id and keyword <> 'account' and flags <> 1 order by keyword DESC"; 
     155                $results2 = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     156                if(DB::IsError($results2)) { 
     157                die($results2->getMessage()); 
     158                } 
     159                foreach ($results2 as $result2) { 
     160                        $options = explode("&", $result2['data']); 
     161                        foreach ($options as $option) { 
     162                                fwrite($iax_conf_fh,$result2['keyword']."=$option\n"); 
     163                        } 
     164                } 
     165                fwrite($iax_conf_fh, $additional."\n"); 
     166        } 
     167        fclose($iax_conf_fh); 
     168        fclose($iax_reg_fh); 
     169
     170 
     171function generate_configurations_zap($ast_version) { 
     172 
     173        global $amp_conf; 
     174        global $db; 
     175 
     176        $additional = ""; 
     177 
     178        $zap_conf = $amp_conf['ASTETCDIR']."/zapata_additional.conf"; 
     179 
     180        $table_name = "zap"; 
     181 
     182        $warning_banner = _("; do not edit this file, this is an auto-generated file by freepbx\n; all modifications must be done from the web gui\n\n"); 
     183 
     184        $zap_conf_fh = fopen($zap_conf,"w"); 
     185        if ($zap_conf_fh === false) { 
     186                fatal(_("Cannot write ZAP configurations"),sprintf(_("Failed creating/overwriting Zapata extensions file: %s"),$zap_conf)); 
     187        } 
     188 
     189        fwrite($zap_conf_fh, $warning_banner); 
     190 
     191        $sql = "SELECT keyword,data from $table_name where id=-1 and keyword <> 'account' and flags <> 1"; 
     192        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     193        if(DB::IsError($results)) { 
     194        die($results->getMessage()); 
     195        } 
     196        foreach ($results as $result) { 
     197                $additional .= $result['keyword']."=".$result['data']."\n"; 
     198        } 
     199 
     200        $sql = "SELECT data,id from $table_name where keyword='account' and flags <> 1 group by data"; 
     201        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     202        if(DB::IsError($results)) { 
     203        die($results->getMessage()); 
     204        } 
     205 
     206        foreach ($results as $result) { 
     207                $account = $result['data']; 
     208                $id = $result['id']; 
     209                fwrite($zap_conf_fh,";;;;;;[$account]\n"); 
     210 
     211                $sql = "SELECT keyword,data from $table_name where id=$id and keyword <> 'account' and flags <> 1 order by keyword DESC"; 
     212                $results2 = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     213                if(DB::IsError($results2)) { 
     214                die($results2->getMessage()); 
     215                } 
     216                $zapchannel=""; 
     217                foreach ($results2 as $result2) { 
     218                        if ($result2['keyword'] == 'channel') { 
     219                                $zapchannel = $result2['data']; 
    390220                        } else { 
    391                                 freepbx_log('retrieve_conf', 'devel-debug', 'generateConf from '.$classname); 
    392                                 write_file(${$classname}->get_filename(),${$classname}->generateConf()); 
     221                                fwrite($zap_conf_fh,$result2['keyword']."=".$result2['data']."\n"); 
    393222                        } 
    394223                } 
    395         } 
    396 
    397  
    398  
    399 function write_file($filename,$contents) { 
    400         global $asterisk_conf; 
    401         freepbx_log('retrieve_conf', 'devel-debug', 'Writing '.$filename); 
    402         if (isset($filename) && !empty($filename)) { 
    403                 if ($fd = fopen(addslash($asterisk_conf['astetcdir']).$filename, "w")) { 
    404                         fwrite($fd, WARNING_BANNER ); 
    405                         fwrite($fd, $contents); 
    406                         fclose($fd); 
    407                 } 
    408         } 
    409 
    410  
    411 function symlink_subdirs($moduledir) { 
    412         global $symlink_dirs; 
    413         $symlink_errors = false; 
    414  
    415         $nt = notifications::create($db); 
    416  
    417         foreach ($symlink_dirs as $subdir => $targetdir) { 
    418                 $dir = addslash($moduledir).$subdir; 
    419                 if (is_dir($dir)) { 
    420                         $d = opendir($dir); 
    421                         while ($file = readdir($d)) { 
    422                                 if ($file[0] != '.') { 
    423                                         $src = addslash($dir).$file; 
    424                                         $dest = addslash($targetdir).$file; 
    425                                         if (file_exists($dest)) { 
    426                                                 if (!is_link($dest)) { 
    427                                                         freepbx_log('retrieve-conf', 'error', $dest.' already exists, and is not a symlink!'); 
    428                                                         $nt->add_error('retrieve_conf', 'SYMLINK', _("symlink from modules failed"), sprintf(_("retrieve_conf failed to sym link the %s file from modules"),$dest)); 
    429                                                         $symlink_errors = true; 
    430                                                 } else if (readlink($dest) != $src) { 
    431                                                         // TODO : is this the proper handling? should we just overwrite..? 
    432                                                         freepbx_log('retrieve-conf', 'error', $dest.' already exists, and is linked to something else!'); 
    433                                                         $nt->add_error('retrieve_conf', 'SYMLINK', _("symlink from modules failed"), sprintf(_("retrieve_conf failed to sym link the %s file from modules"),$dest)); 
    434                                                         $symlink_errors = true; 
    435                                                 } else { 
    436                                                         freepbx_log('retrieve-conf', 'devel-debug', $dest.' already points to '.$src.' - OK'); 
    437                                                 } 
    438                                         } else { 
    439 //                                              // symlink, unlike copy, doesn't overwrite - have to delete first 
    440 //                                              if (is_link($dest) || file_exists($dest)) { 
    441 //                                                      unlink($dest); 
    442 //                                              } 
    443                                                 if (symlink($src, $dest)) { 
    444                                                         freepbx_log('retrieve-conf', 'devel-debug', 'Symlinked '.$src.' to '.$dest); 
    445                                                 } else { 
    446                                                         freepbx_log('retreive-conf', 'devel-debug', 'Cannot symlink '.$src.' to '.$dest.'. Check Permissions?'); 
    447                                                 } 
    448                                         } 
    449                                 } 
    450                         } 
    451                         closedir($d); 
    452                 } 
    453         } 
    454         if (!$symlink_errors) { 
    455                 $nt->delete('retrieve_conf', 'SYMLINK'); 
    456         } 
    457 
    458  
    459 // wrap copy with error handler 
    460 // 
    461 function err_copy($source, $dest) { 
    462         $ret = false; 
    463         set_error_handler("report_errors"); 
    464         if (copy($source, $dest)) { 
    465                 $ret = chmod($dest,'0754'); 
    466         } 
    467         restore_error_handler(); 
    468         return $ret; 
    469 
    470  
    471 // wrap unlink with error handler 
    472 // 
    473 function err_unlink($dest) { 
    474         set_error_handler("report_errors"); 
    475         $ret = unlink($dest); 
    476         restore_error_handler(); 
    477         return $ret; 
    478 
    479  
    480 function cp_subdirs($moduledir) { 
    481         global $cp_errors; 
    482         global $cp_dirs; 
    483  
    484         $cp_errors = ""; 
    485         foreach ($cp_dirs as $subdir => $targetdir) { 
    486                 $dir = addslash($moduledir).$subdir; 
    487                 if (is_dir($dir)) { 
    488                         $d = opendir($dir); 
    489                         while ($file = readdir($d)) { 
    490                                 if ($file[0] != '.') { 
    491                                         $sourcefile = addslash($dir).$file; 
    492                                         $targetfile = addslash($targetdir).$file; 
    493  
    494                                         if (file_exists($targetfile)) { 
    495                                                 if (is_link($targetfile)) { 
    496                                                         if (err_unlink($targetfile)) { 
    497                                                                 freepbx_log('retrieve-conf', 'devel-debug', "$targetfile was symbolic link, unlink successful"); 
    498                                                         } else { 
    499                                                                 freepbx_log('retrieve-conf', 'error', "$targetfile is a symblolic link, failed to unlink!"); 
    500                                                                 break; 
    501                                                         } 
    502                                                 } 
    503                                         } 
    504                                         // OK, now either the file is a regular file or isn't there, so proceed 
    505                                         // 
    506                                         if (err_copy($sourcefile,$targetfile)) { 
    507                                                 freepbx_log('retrieve-conf', 'devel-debug', "$targetfile successfully copied"); 
    508                                                 // copy was successful, make sure it has execute permissions 
    509                                                 chmod($targetfile,0754); 
    510                                         } else { 
    511                                                 freepbx_log('retrieve-conf', 'error', "$targetfile failed to copy from module directory"); 
    512                                         } 
    513                                 } 
    514                         } 
    515                         closedir($d); 
    516                 } 
    517         } 
    518         $nt = notifications::create($db); 
    519         if ($cp_errors) { 
    520                 $nt->add_error('retrieve_conf', 'CPAGIBIN', _("Failed to copy from module agi-bin"), sprintf(_("Retrieve conf failed to copy file(s) from a module's agi-bin dir: %s"),$cp_errors)); 
    521         } else { 
    522                 $nt->delete('retrieve_conf', 'CPAGIBIN'); 
    523         } 
    524 
    525  
    526 function report_errors($errno, $errstr, $errfile, $errline) { 
    527         global $cp_errors; 
    528         freepbx_log('retrieve-conf', 'error', "php reported: '".mysql_real_escape_string($errstr)."' after copy or unlink attempt!"); 
    529         $cp_errors .= $errstr."\n"; 
    530 
    531  
    532 /** Check if there is a job running, if one is found then all is good, if one is not found, it will be added and a 
    533  *  notification will be sent. 
    534  */ 
    535 function install_cron_scheduler() { 
    536         global $amp_conf; 
    537         global $nt; 
    538  
    539         // crontab appears to return an error when no entries, os only fail if error returned AND a list of entries. 
    540         // Don't know if this will ever happen, but a failure and a list could indicate something wrong. 
    541         // 
    542         exec("/usr/bin/crontab -l", $outlines, $ret); 
    543         if ($ret && count($outlines)) { 
    544                 $nt->add_error('retrieve_conf', 'CRONMGR', _("Failed to check crontab for cron manager"), sprintf(_("crontab returned %s error code when checking for crontab entries to start freepbx-cron-scheduler.php crontab manager"),$ret)); 
    545         } else { 
    546                 $nt->delete('retrieve_conf', 'CRONMGR'); 
    547                 $outlines2 = preg_grep("/freepbx-cron-scheduler.php/",$outlines); 
    548                 $cnt = count($outlines2); 
    549                 switch ($cnt) { 
    550                         case 0: 
    551                                 /** grab any other cronjobs that are running as asterisk and NOT associated with backups 
    552                                 *  this code was taken from the backup module for the most part. But seems to work... 
    553                                 */ 
    554                                 $outlines = array(); 
    555                                 exec("/usr/bin/crontab -l | grep -v ^#\ DO\ NOT | grep -v ^#\ \( |  grep -v freepbx-cron-scheduler.php", $outlines, $ret); 
    556                                 $crontab_entry = ""; 
    557                                 foreach ($outlines as $line) { 
    558                                         $crontab_entry .= $line."\n"; 
    559                                 } 
    560                                 // schedule to run hourly, at a random time. The random time is explicit to accomodate things like module_admin online update checking 
    561                                 // since we will want a random access to the server. In the case of module_admin, that will also be scheduled randomly within the hour 
    562                                 // that it is to run 
    563                                 // 
    564                                 $crontab_entry .= rand(0,59)." * * * * ".$amp_conf['AMPBIN']."/freepbx-cron-scheduler.php"; 
    565                                 system("/bin/echo '$crontab_entry' | /usr/bin/crontab -"); 
    566                                 break; 
    567                         case 1: 
    568                                 // already running, nothing to do 
    569                                 break; 
    570                         default: 
    571                                 // error, there should never be more than one running 
    572                                 echo "TODO: deal with error here\n"; 
    573                                 $nt->add_error('retrieve_conf', 'CRONMGR', _("Multiple freepbx-cron-scheduler.php running"), sprintf(_("There were %s freepbx-cron-scheduler.php instances running. There should be only 1."),$cnt)); 
    574                 } 
    575         } 
    576 
    577  
    578  
    579 // run legacy retrieve scripts 
    580 // TODO these legacy retrieve scripts should be obsoleted, in favor of classes like extensions class 
    581  
    582 //script to write op_server.cfg file from mysql  
    583 $script = $amp_conf['AMPBIN'].'/retrieve_op_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    584 exec($script); 
    585  
    586 //script to write sip conf file from mysql 
    587 $script = $amp_conf['AMPBIN'].'/retrieve_sip_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    588 exec($script); 
    589  
    590 //script to write iax2 conf file from mysql 
    591 $script = $amp_conf['AMPBIN'].'/retrieve_iax_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    592 exec($script); 
    593  
    594 //script to write zap conf file from mysql 
    595 $script = $amp_conf['AMPBIN'].'/retrieve_zap_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    596 exec($script); 
    597  
    598 //script to write queues conf file from mysql 
    599 $script = $amp_conf['AMPBIN'].'/retrieve_queues_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    600 exec($script); 
    601          
    602 // Check and install the freepbx-cron-scheduler.php manager 
    603 // 
    604 install_cron_scheduler(); 
    605  
    606 // **** Set reload flag for AMP admin 
    607 needreload(); 
    608 if (isset($amp_conf["AMPWEBADDRESS"]) && $amp_conf["AMPWEBADDRESS"]) 
    609 
    610         out(sprintf(_("Please update your modules and reload Asterisk by visiting %s"), "http://".$amp_conf["AMPWEBADDRESS"]."/admin")); 
    611 
    612 else 
    613 
    614         out(_("Please update your modules and reload Asterisk by browsing to your server.")); 
    615 
    616         $nt->delete('retrieve_conf', 'FATAL'); 
     224                fwrite($zap_conf_fh, "channel=>$zapchannel\n"); 
     225                fwrite($zap_conf_fh, $additional."\n"); 
     226        } 
     227        fclose($zap_conf_fh); 
     228
     229 
     230 
     231function generate_configurations_queues($ast_version) { 
     232 
     233        global $amp_conf; 
     234        global $db; 
     235 
     236        $additional = ""; 
     237 
     238        $queues_conf = $amp_conf['ASTETCDIR']."/queues_additional.conf"; 
     239 
     240        $table_name = "queues"; 
     241 
     242        $warning_banner = _("; do not edit this file, this is an auto-generated file by freepbx\n; all modifications must be done from the web gui\n\n"); 
     243 
     244        $queues_conf_fh = fopen($queues_conf,"w"); 
     245        if ($queues_conf_fh === false) { 
     246                fatal(_("Cannot write Queues configurations"),sprintf(_("Failed creating/overwriting Queues extensions file: %s"),$queues_conf)); 
     247        } 
     248 
     249        fwrite($queues_conf_fh, $warning_banner); 
     250 
     251        $sql = "SELECT keyword,data from $table_name where id='-1' and keyword <> 'account'"; 
     252        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     253        if(DB::IsError($results)) { 
     254        die($results->getMessage()); 
     255        } 
     256        foreach ($results as $result) { 
     257                $additional .= $result['keyword']."=".$result['data']."\n"; 
     258        } 
     259 
     260        $sql = "SELECT data,id from $table_name where keyword='account' group by data"; 
     261        $results = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     262        if(DB::IsError($results)) { 
     263        die($results->getMessage()); 
     264        } 
     265 
     266        foreach ($results as $result) { 
     267                $account = $result['data']; 
     268                $id = $result['id']; 
     269                fwrite($queues_conf_fh,"[$account]\n"); 
     270 
     271                $sql = "SELECT keyword,data from $table_name where id='$id' and keyword <> 'account' and keyword <> 'rtone' order by flags"; 
     272                $results2 = $db->getAll($sql, DB_FETCHMODE_ASSOC); 
     273                if(DB::IsError($results2)) { 
     274                die($results2->getMessage()); 
     275                } 
     276                $queueschannel=""; 
     277                foreach ($results2 as $result2) { 
     278                        fwrite($queues_conf_fh,$result2['keyword']."=".$result2['data']."\n"); 
     279                } 
     280                fwrite($queues_conf_fh, $additional."\n"); 
     281        } 
     282        fclose($queues_conf_fh); 
     283
     284 
    617285?> 
  • freepbx/branches/2.3/amp_conf/bin/retrieve_conf

    r4594 r4661  
    576576} 
    577577 
     578include("libfreepbx.confgen.php"); 
    578579 
    579580// run legacy retrieve scripts 
     
    584585exec($script); 
    585586 
    586 //script to write sip conf file from mysql 
    587 $script = $amp_conf['AMPBIN'].'/retrieve_sip_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    588 exec($script); 
    589  
    590 //script to write iax2 conf file from mysql 
    591 $script = $amp_conf['AMPBIN'].'/retrieve_iax_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    592 exec($script); 
    593  
    594 //script to write zap conf file from mysql 
    595 $script = $amp_conf['AMPBIN'].'/retrieve_zap_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    596 exec($script); 
    597  
    598 //script to write queues conf file from mysql 
    599 $script = $amp_conf['AMPBIN'].'/retrieve_queues_conf_from_mysql.pl '.$amportalconf.' '.rtrim($asterisk_conf['astetcdir'],DIRECTORY_SEPARATOR); 
    600 exec($script); 
     587// generate configuration files 
     5