Changeset 10953 for freepbx/trunk

Show
Ignore:
Timestamp:
01/12/11 22:20:37 (2 years ago)
Author:
p_lindheimer
Message:

initial changes for amportal.conf class setup (probably will want to rename config.functions.php to freepbx_settings.class.php) re #4740 - partially done but should still be operational

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • freepbx/trunk/amp_conf/htdocs/admin/bootstrap.php

    r10866 r10953  
    5959//keep old values as well so that we have the db settings handy 
    6060// get settings 
    61 $amp_conf = $amp_conf + parse_amportal_conf("/etc/amportal.conf"); 
    62 $asterisk_conf = parse_asterisk_conf($amp_conf["ASTETCDIR"] . "/asterisk.conf"); 
     61$freepbx_conf =& freepbx_conf::create(); 
     62 
     63// passing by reference, this means that the $amp_conf available to everyone is the same one as present 
     64// within the class, which is probably a direction we want to go to use the class. 
     65// 
     66$amp_conf =& $freepbx_conf->parse_amportal_conf("/etc/amportal.conf",$amp_conf); 
     67$asterisk_conf =& $freepbx_conf->get_asterisk_conf(); 
    6368$bootstrap_settings['amportal_conf_initialized'] = true; 
    6469 
  • freepbx/trunk/amp_conf/htdocs/admin/libraries/config.functions.php

    r10951 r10953  
    11<?php 
    2  
    3 $amp_conf_defaults = array( 
     2class freepbx_conf { 
     3 
     4  var $conf_defaults = array( 
    45  'AMPDBENGINE'    => array('std' , 'mysql'), 
    56  'AMPDBNAME'      => array('std' , 'asterisk'), 
     
    6162  'TCMAINT'         => array('bool' , true), 
    6263  'DAYNIGHTTCHOOK'  => array('bool' , false), 
    63 ); 
    64  
    65 // TODO: this becomes part of the class 
    66 function parse_amportal_conf($filename) { 
    67   global $amp_conf_defaults, $db; 
    68  
    69   /* defaults 
    70    * This defines defaults and formating to assure consistency across the system so that 
    71    * components don't have to keep being 'gun shy' about these variables. 
    72    *  
    73    * we will read these settings out of the db, but only when $filename is writeable 
    74    * otherwise, we read the $filename 
    75    */ 
    76   if (!is_writable($filename)) { 
    77     $file = file($filename);  
    78       if (is_array($file)) {  
    79         foreach ($file as $line) {  
    80           if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&-@=_!<>\"\']+)\s*$/",$line,$matches)) {  
    81             $conf[ $matches[1] ] = $matches[2];  
    82           }  
    83         }  
    84         $conf['amportal_canwrite'] = false; 
    85       } else {  
    86         die_freepbx("<h1>".sprintf(_("Missing or unreadable config file (%s)...cannot continue"), $filename)."</h1>");  
    87       } 
    88   } else { 
    89     $sql = 'SELECT `keyword`, value FROM freepbx_settings'; 
    90     $conf = $db->getAssoc($sql); 
    91     if(DB::IsError($conf)) {      
    92       die_freepbx($conf->getMessage());  
    93     } else { 
    94       $conf['amportal_canwrite'] = true; 
    95     } 
    96      
    97   } 
    98    
    99  
    100   // set defaults 
    101   foreach ($amp_conf_defaults as $key=>$arr) { 
    102  
    103     switch ($arr[0]) { 
    104       // for type dir, make sure there is no trailing '/' to keep consistent everwhere 
    105       // 
    106       case 'dir': 
    107         if (!isset($conf[$key]) || trim($conf[$key]) == '') { 
    108           $conf[$key] = $arr[1]; 
    109         } else { 
    110           $conf[$key] = rtrim($conf[$key],'/'); 
    111         } 
    112         break; 
    113       // booleans: 
    114       // "yes", "true", "on", true, 1 (case-insensitive) will be treated as true, everything else is false 
    115       // 
    116       case 'bool': 
    117         if (!isset($conf[$key])) { 
    118           $conf[$key] = $arr[1]; 
    119         } else { 
    120           $conf[$key] = ($conf[$key] === true || strtolower($conf[$key]) == 'true' || $conf[$key] === 1 || $conf[$key] == '1'  
    121                                               || strtolower($conf[$key]) == 'yes' ||  strtolower($conf[$key]) == 'on'); 
    122         } 
    123         break; 
    124       default: 
    125         if (!isset($conf[$key])) { 
    126           $conf[$key] = $arr[1]; 
    127         } else { 
    128           $conf[$key] = trim($conf[$key]); 
    129         } 
    130     } 
    131   } 
    132   return $conf; 
     64  ); 
     65 
     66  var $db_store; 
     67 
     68  var $conf = array(); 
     69 
     70  var $asterisk_conf = array(); 
     71 
     72  var $parsed_from_db = false; 
     73  var $amportal_canwrite; 
     74 
     75  function &create() { 
     76    static $obj; 
     77    global $db; 
     78    if (!isset($obj)) { 
     79      $obj = new freepbx_conf(); 
     80    } 
     81    return $obj; 
     82  } 
     83 
     84  function freepbx_conf($var) { 
     85    $this->__construct(); 
     86  } 
     87  function __construct() { 
     88    $db_raw = sql('SELECT * from freepbx_settings', 'getAll', DB_FETCHMODE_ASSOC); 
     89    foreach($db_raw as $setting) { 
     90      $this->db_store[$setting['keyword']] = $setting; 
     91      $this->db_store[$setting['keyword']]['modified'] = false; 
     92      // setup the conf array also 
     93      $this->conf[$setting['keyword']] = $setting['value']; 
     94    } 
     95    unset($db_raw); 
     96  } 
     97 
     98  function parse_amportal_conf($filename, $bootstrap_conf = array()) { 
     99    global $db; 
     100 
     101    // if we have loaded for the db, then just return what we already have 
     102    if ($this->parsed_from_db) { 
     103      return $this->conf; 
     104    } 
     105 
     106    /* defaults 
     107    * This defines defaults and formating to assure consistency across the system so that 
     108    * components don't have to keep being 'gun shy' about these variables. 
     109    *  
     110    * we will read these settings out of the db, but only when $filename is writeable 
     111    * otherwise, we read the $filename 
     112    */ 
     113    // If conf file is not writable, then we use it as the master so parse it. 
     114    if (!is_writable($filename)) { 
     115      $file = file($filename);  
     116      if (is_array($file)) {  
     117        $write_back = false; 
     118        foreach ($file as $line) {  
     119          if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&-@=_!<>\"\']+)\s*$/",$line,$matches)) {  
     120            // overrite anything that was initialized from the db with the conf file authoritative source 
     121            // if different from the db value then let's write it back to the db 
     122            if (!isset($this->conf[$matches[1]]) || $this->conf[$matches[1]] != $matches[2]) { 
     123              $this->conf[ $matches[1] ] = $matches[2];  
     124              if (isset($this->db_store[$matches[1]])) { 
     125                $this->db_store[$matches[1]]['value'] = $matches[2]; 
     126                $this->db_store[$matches[1]]['modified'] = true; 
     127                $write_back = true; 
     128              } 
     129            } 
     130          }  
     131        }  
     132        $this->conf['amportal_canwrite'] = false; 
     133        $this->amportal_canwrite = false; 
     134        if ($write_back) { 
     135          $this->commit_settings(); 
     136        } 
     137      } else {  
     138        die_freepbx("<h1>".sprintf(_("Missing or unreadable config file (%s)...cannot continue"), $filename)."</h1>");  
     139      } 
     140      // Need to handle transitionary period where modules are adding new settings. So once we parsed the file 
     141      // we still go read from the database and add anything that isn't there from the conf file. 
     142      // 
     143    } else { 
     144      $this->conf['amportal_canwrite'] = true; 
     145      $this->amportal_canwrite = true; 
     146      $this->parsed_from_db = true; 
     147    } 
     148    // TODO: am I correct to NOT write these back to the db_store? 
     149    foreach ($bootstrap_conf as $key => $value) { 
     150      $this->conf['key'] = $value; 
     151    } 
     152 
     153    // TODO: have a closer look at these in light of default values in the db. 
     154    //       this does set some minimum settings that should be there but the db 
     155    //       could have different defaults right?  
     156    // 
     157    // set defaults 
     158    foreach ($this->conf_defaults as $key=>$arr) { 
     159 
     160      switch ($arr[0]) { 
     161        // for type dir, make sure there is no trailing '/' to keep consistent everwhere 
     162        // 
     163        case 'dir': 
     164          if (!isset($this->conf[$key]) || trim($this->conf[$key]) == '') { 
     165            $this->conf[$key] = $arr[1]; 
     166          } else { 
     167            $this->conf[$key] = rtrim($this->conf[$key],'/'); 
     168          } 
     169          break; 
     170        // booleans: 
     171        // "yes", "true", "on", true, 1 (case-insensitive) will be treated as true, everything else is false 
     172        // 
     173        case 'bool': 
     174          if (!isset($this->conf[$key])) { 
     175            $this->conf[$key] = $arr[1]; 
     176          } else { 
     177            $this->conf[$key] = ($this->conf[$key] === true || strtolower($this->conf[$key]) == 'true' || $this->conf[$key] === 1 || $this->conf[$key] == '1'  
     178                                                || strtolower($this->conf[$key]) == 'yes' ||  strtolower($this->conf[$key]) == 'on'); 
     179          } 
     180          break; 
     181        default: 
     182          if (!isset($this->conf[$key])) { 
     183            $this->conf[$key] = $arr[1]; 
     184          } else { 
     185            $this->conf[$key] = trim($this->conf[$key]); 
     186          } 
     187      } 
     188    } 
     189 
     190    $convert = array( 
     191      'astetcdir'    => 'ASTETCDIR', 
     192      'astmoddir'    => 'ASTMODDIR', 
     193      'astvarlibdir' => 'ASTVARLIBDIR', 
     194      'astagidir'    => 'ASTAGIDIR', 
     195      'astspooldir'  => 'ASTSPOOLDIR', 
     196      'astrundir'    => 'ASTRUNDIR', 
     197      'astlogdir'    => 'ASTLOGDIR' 
     198    ); 
     199 
     200    $file = file($this->conf['ASTETCDIR'].'/asterisk.conf'); 
     201    foreach ($file as $line) { 
     202      if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {  
     203        $this->asterisk_conf[ $matches[1] ] = rtrim($matches[2],"/ \t"); 
     204      } 
     205    } 
     206 
     207    // Now that we parsed asterisk.conf, we need to make sure $amp_conf is consistent 
     208    // so just set it to what we found, since this is what asterisk will use anyhow. 
     209    // 
     210    foreach ($convert as $ast_conf_key => $amp_conf_key) { 
     211      if (isset($this->conf[$ast_conf_key])) { 
     212        $this->conf[$amp_conf_key] = $this->asterisk_conf[$ast_conf_key]; 
     213      } 
     214    } 
     215 
     216    return $this->conf; 
     217  } 
     218 
     219  function get_asterisk_conf() { 
     220    return $this->asterisk_conf; 
     221  } 
     222 
     223  // Commit back any dirty settings to the database, if they are not modified then 
     224  // don't bother. 
     225  function commit_settings() { 
     226    // TODO: foreach db_store that modified === true, package up to do an update 
     227    //       then mark clean 
     228  } 
     229 
     230  function get_setting($keyword) { 
     231    if (isset($this->db_store[$keyword])) { 
     232      return $this->db_store[$keyword]; 
     233    } else { 
     234      return false; 
     235    } 
     236  } 
     237 
     238  // TODO: used to insert or update an existing setting such as in an install 
     239  //       script. $vars will include some required fields and we will be strict 
     240  //       with a die_freebpx() if they are missing. Some fields are optional 
     241  //       and we will set default values also if appropriate. 
     242  // 
     243  function update_setting($keyword,$vars,$commit=false) { 
     244  } 
     245 
     246  // Used to remove settings from the database that are no longer needed like with an 
     247  // uninstall script. 
     248  // TODO: remove from db_store, from conf, delete from table directly 
     249  function del_setting($keyword) { 
     250  } 
     251 
    133252} 
    134253 
    135 function parse_asterisk_conf($filename) { 
    136   //TODO: Should the correction of $amp_conf be passed by refernce and optional? 
    137   // 
    138   global $amp_conf; 
    139   $conf = array(); 
    140      
    141   $convert = array( 
    142     'astetcdir'    => 'ASTETCDIR', 
    143     'astmoddir'    => 'ASTMODDIR', 
    144     'astvarlibdir' => 'ASTVARLIBDIR', 
    145     'astagidir'    => 'ASTAGIDIR', 
    146     'astspooldir'  => 'ASTSPOOLDIR', 
    147     'astrundir'    => 'ASTRUNDIR', 
    148     'astlogdir'    => 'ASTLOGDIR' 
    149   ); 
    150  
    151   $file = file($filename); 
    152   foreach ($file as $line) { 
    153     if (preg_match("/^\s*([a-zA-Z0-9]+)\s* => \s*(.*)\s*([;#].*)?/",$line,$matches)) {  
    154       $conf[ $matches[1] ] = rtrim($matches[2],"/ \t"); 
    155     } 
    156   } 
    157  
    158   // Now that we parsed asterisk.conf, we need to make sure $amp_conf is consistent 
    159   // so just set it to what we found, since this is what asterisk will use anyhow. 
    160   // 
    161   foreach ($convert as $ast_conf_key => $amp_conf_key) { 
    162     if (isset($conf[$ast_conf_key])) { 
    163       $amp_conf[$amp_conf_key] = $conf[$ast_conf_key]; 
    164     } 
    165   } 
    166   return $conf; 
    167 
     254//TODO: if running in crippled mode, then at retrieve_conf time I think we should write to 
     255//      the notification systems that they need to make the file writable to us since 
     256//      we need to live with this transition at least for a while. 
    168257 
    169258/** Replaces variables in a string with the values from ampconf 
     
    171260 */ 
    172261function ampconf_string_replace($string) { 
    173   global $amp_conf
     262  $freepbx_conf =& freepbx_conf::create()
    174263   
    175264  $target = array(); 
    176265  $replace = array(); 
    177266   
    178   foreach ($amp_conf as $key=>$value) { 
     267  foreach ($freepbx_conf->conf as $key=>$value) { 
    179268    $target[] = '%'.$key.'%'; 
    180269    $replace[] = $value; 
  • freepbx/trunk/amp_conf/htdocs/admin/libraries/framework_view.functions.php

    r10865 r10953  
    9191 
    9292function frameworkPasswordCheck() { 
    93   global $amp_conf, $amp_conf_defaults; 
     93  global $amp_conf; 
     94 
     95  $freepbx_conf =& freepbx_conf::create(); 
     96  $amp_conf_defaults =& $freepbx_conf->conf_defaults; 
     97 
    9498  $nt = notifications::create($db); 
    9599  if ($amp_conf['AMPMGRPASS'] == $amp_conf_defaults['AMPMGRPASS'][1]) {