00001 <?php 00002 00032 /* $Id: Config_File.class.php 3149 2009-05-23 20:59:25Z monte.ohrt $ */ 00033 00038 class Config_File { 00046 var $overwrite = true; 00047 00052 var $booleanize = true; 00053 00057 var $read_hidden = true; 00058 00063 var $fix_newlines = true; 00067 var $_config_path = ""; 00068 var $_config_data = array(); 00076 function Config_File($config_path = NULL) 00077 { 00078 if (isset($config_path)) 00079 $this->set_path($config_path); 00080 } 00081 00082 00088 function set_path($config_path) 00089 { 00090 if (!empty($config_path)) { 00091 if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) { 00092 $this->_trigger_error_msg("Bad config file path '$config_path'"); 00093 return; 00094 } 00095 if(substr($config_path, -1) != DIRECTORY_SEPARATOR) { 00096 $config_path .= DIRECTORY_SEPARATOR; 00097 } 00098 00099 $this->_config_path = $config_path; 00100 } 00101 } 00102 00103 00112 function get($file_name, $section_name = NULL, $var_name = NULL) 00113 { 00114 if (empty($file_name)) { 00115 $this->_trigger_error_msg('Empty config file name'); 00116 return; 00117 } else { 00118 $file_name = $this->_config_path . $file_name; 00119 if (!isset($this->_config_data[$file_name])) 00120 $this->load_file($file_name, false); 00121 } 00122 00123 if (!empty($var_name)) { 00124 if (empty($section_name)) { 00125 return $this->_config_data[$file_name]["vars"][$var_name]; 00126 } else { 00127 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name])) 00128 return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]; 00129 else 00130 return array(); 00131 } 00132 } else { 00133 if (empty($section_name)) { 00134 return (array)$this->_config_data[$file_name]["vars"]; 00135 } else { 00136 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"])) 00137 return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"]; 00138 else 00139 return array(); 00140 } 00141 } 00142 } 00143 00144 00152 function &get_key($config_key) 00153 { 00154 list($file_name, $section_name, $var_name) = explode('/', $config_key, 3); 00155 $result = &$this->get($file_name, $section_name, $var_name); 00156 return $result; 00157 } 00158 00164 function get_file_names() 00165 { 00166 return array_keys($this->_config_data); 00167 } 00168 00169 00176 function get_section_names($file_name) 00177 { 00178 $file_name = $this->_config_path . $file_name; 00179 if (!isset($this->_config_data[$file_name])) { 00180 $this->_trigger_error_msg("Unknown config file '$file_name'"); 00181 return; 00182 } 00183 00184 return array_keys($this->_config_data[$file_name]["sections"]); 00185 } 00186 00187 00195 function get_var_names($file_name, $section = NULL) 00196 { 00197 if (empty($file_name)) { 00198 $this->_trigger_error_msg('Empty config file name'); 00199 return; 00200 } else if (!isset($this->_config_data[$file_name])) { 00201 $this->_trigger_error_msg("Unknown config file '$file_name'"); 00202 return; 00203 } 00204 00205 if (empty($section)) 00206 return array_keys($this->_config_data[$file_name]["vars"]); 00207 else 00208 return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]); 00209 } 00210 00211 00217 function clear($file_name = NULL) 00218 { 00219 if ($file_name === NULL) 00220 $this->_config_data = array(); 00221 else if (isset($this->_config_data[$file_name])) 00222 $this->_config_data[$file_name] = array(); 00223 } 00224 00225 00233 function load_file($file_name, $prepend_path = true) 00234 { 00235 if ($prepend_path && $this->_config_path != "") 00236 $config_file = $this->_config_path . $file_name; 00237 else 00238 $config_file = $file_name; 00239 00240 ini_set('track_errors', true); 00241 $fp = @fopen($config_file, "r"); 00242 if (!is_resource($fp)) { 00243 $this->_trigger_error_msg("Could not open config file '$config_file'"); 00244 return false; 00245 } 00246 00247 $contents = ($size = filesize($config_file)) ? fread($fp, $size) : ''; 00248 fclose($fp); 00249 00250 $this->_config_data[$config_file] = $this->parse_contents($contents); 00251 return true; 00252 } 00253 00260 function set_file_contents($config_file, $contents) 00261 { 00262 $this->_config_data[$config_file] = $this->parse_contents($contents); 00263 return true; 00264 } 00265 00271 function parse_contents($contents) 00272 { 00273 if($this->fix_newlines) { 00274 // fix mac/dos formatted newlines 00275 $contents = preg_replace('!\r\n?!', "\n", $contents); 00276 } 00277 00278 $config_data = array(); 00279 $config_data['sections'] = array(); 00280 $config_data['vars'] = array(); 00281 00282 /* reference to fill with data */ 00283 $vars =& $config_data['vars']; 00284 00285 /* parse file line by line */ 00286 preg_match_all('!^.*\r?\n?!m', $contents, $match); 00287 $lines = $match[0]; 00288 for ($i=0, $count=count($lines); $i<$count; $i++) { 00289 $line = $lines[$i]; 00290 if (empty($line)) continue; 00291 00292 if ( substr($line, 0, 1) == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) { 00293 /* section found */ 00294 if (substr($match[1], 0, 1) == '.') { 00295 /* hidden section */ 00296 if ($this->read_hidden) { 00297 $section_name = substr($match[1], 1); 00298 } else { 00299 /* break reference to $vars to ignore hidden section */ 00300 unset($vars); 00301 $vars = array(); 00302 continue; 00303 } 00304 } else { 00305 $section_name = $match[1]; 00306 } 00307 if (!isset($config_data['sections'][$section_name])) 00308 $config_data['sections'][$section_name] = array('vars' => array()); 00309 $vars =& $config_data['sections'][$section_name]['vars']; 00310 continue; 00311 } 00312 00313 if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) { 00314 /* variable found */ 00315 $var_name = rtrim($match[1]); 00316 if (strpos($match[2], '"""') === 0) { 00317 /* handle multiline-value */ 00318 $lines[$i] = substr($match[2], 3); 00319 $var_value = ''; 00320 while ($i<$count) { 00321 if (($pos = strpos($lines[$i], '"""')) === false) { 00322 $var_value .= $lines[$i++]; 00323 } else { 00324 /* end of multiline-value */ 00325 $var_value .= substr($lines[$i], 0, $pos); 00326 break; 00327 } 00328 } 00329 $booleanize = false; 00330 00331 } else { 00332 /* handle simple value */ 00333 $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2])); 00334 $booleanize = $this->booleanize; 00335 00336 } 00337 $this->_set_config_var($vars, $var_name, $var_value, $booleanize); 00338 } 00339 /* else unparsable line / means it is a comment / means ignore it */ 00340 } 00341 return $config_data; 00342 } 00343 00352 function _set_config_var(&$container, $var_name, $var_value, $booleanize) 00353 { 00354 if (substr($var_name, 0, 1) == '.') { 00355 if (!$this->read_hidden) 00356 return; 00357 else 00358 $var_name = substr($var_name, 1); 00359 } 00360 00361 if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) { 00362 $this->_trigger_error_msg("Bad variable name '$var_name'"); 00363 return; 00364 } 00365 00366 if ($booleanize) { 00367 if (preg_match("/^(on|true|yes)$/i", $var_value)) 00368 $var_value = true; 00369 else if (preg_match("/^(off|false|no)$/i", $var_value)) 00370 $var_value = false; 00371 } 00372 00373 if (!isset($container[$var_name]) || $this->overwrite) 00374 $container[$var_name] = $var_value; 00375 else { 00376 settype($container[$var_name], 'array'); 00377 $container[$var_name][] = $var_value; 00378 } 00379 } 00380 00386 function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING) 00387 { 00388 trigger_error("Config_File error: $error_msg", $error_type); 00389 } 00391 } 00392
| Copyright © 2003 - 2009 MyOOS [Shopsystem]. All rights reserved. MyOOS [Shopsystem] is Free Software released under the GNU/GPL License. Webmaster: info@r23.de (Impressum) |
|
