00001 <?php
00002
00028
00029
00034 class Config_File {
00042 var $overwrite = true;
00043
00048 var $booleanize = true;
00049
00053 var $read_hidden = true;
00054
00059 var $fix_newlines = true;
00063 var $_config_path = "";
00064 var $_config_data = array();
00072 function Config_File($config_path = NULL)
00073 {
00074 if (isset($config_path))
00075 $this->set_path($config_path);
00076 }
00077
00078
00084 function set_path($config_path)
00085 {
00086 if (!empty($config_path)) {
00087 if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
00088 $this->_trigger_error_msg("Bad config file path '$config_path'");
00089 return;
00090 }
00091 if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
00092 $config_path .= DIRECTORY_SEPARATOR;
00093 }
00094
00095 $this->_config_path = $config_path;
00096 }
00097 }
00098
00099
00108 function get($file_name, $section_name = NULL, $var_name = NULL)
00109 {
00110 if (empty($file_name)) {
00111 $this->_trigger_error_msg('Empty config file name');
00112 return;
00113 } else {
00114 $file_name = $this->_config_path . $file_name;
00115 if (!isset($this->_config_data[$file_name]))
00116 $this->load_file($file_name, false);
00117 }
00118
00119 if (!empty($var_name)) {
00120 if (empty($section_name)) {
00121 return $this->_config_data[$file_name]["vars"][$var_name];
00122 } else {
00123 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
00124 return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
00125 else
00126 return array();
00127 }
00128 } else {
00129 if (empty($section_name)) {
00130 return (array)$this->_config_data[$file_name]["vars"];
00131 } else {
00132 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
00133 return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
00134 else
00135 return array();
00136 }
00137 }
00138 }
00139
00140
00148 function &get_key($config_key)
00149 {
00150 list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
00151 $result = &$this->get($file_name, $section_name, $var_name);
00152 return $result;
00153 }
00154
00160 function get_file_names()
00161 {
00162 return array_keys($this->_config_data);
00163 }
00164
00165
00172 function get_section_names($file_name)
00173 {
00174 $file_name = $this->_config_path . $file_name;
00175 if (!isset($this->_config_data[$file_name])) {
00176 $this->_trigger_error_msg("Unknown config file '$file_name'");
00177 return;
00178 }
00179
00180 return array_keys($this->_config_data[$file_name]["sections"]);
00181 }
00182
00183
00191 function get_var_names($file_name, $section = NULL)
00192 {
00193 if (empty($file_name)) {
00194 $this->_trigger_error_msg('Empty config file name');
00195 return;
00196 } else if (!isset($this->_config_data[$file_name])) {
00197 $this->_trigger_error_msg("Unknown config file '$file_name'");
00198 return;
00199 }
00200
00201 if (empty($section))
00202 return array_keys($this->_config_data[$file_name]["vars"]);
00203 else
00204 return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
00205 }
00206
00207
00213 function clear($file_name = NULL)
00214 {
00215 if ($file_name === NULL)
00216 $this->_config_data = array();
00217 else if (isset($this->_config_data[$file_name]))
00218 $this->_config_data[$file_name] = array();
00219 }
00220
00221
00229 function load_file($file_name, $prepend_path = true)
00230 {
00231 if ($prepend_path && $this->_config_path != "")
00232 $config_file = $this->_config_path . $file_name;
00233 else
00234 $config_file = $file_name;
00235
00236 ini_set('track_errors', true);
00237 $fp = @fopen($config_file, "r");
00238 if (!is_resource($fp)) {
00239 $this->_trigger_error_msg("Could not open config file '$config_file'");
00240 return false;
00241 }
00242
00243 $contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
00244 fclose($fp);
00245
00246 $this->_config_data[$config_file] = $this->parse_contents($contents);
00247 return true;
00248 }
00249
00256 function set_file_contents($config_file, $contents)
00257 {
00258 $this->_config_data[$config_file] = $this->parse_contents($contents);
00259 return true;
00260 }
00261
00267 function parse_contents($contents)
00268 {
00269 if($this->fix_newlines) {
00270
00271 $contents = preg_replace('!\r\n?!', "\n", $contents);
00272 }
00273
00274 $config_data = array();
00275 $config_data['sections'] = array();
00276 $config_data['vars'] = array();
00277
00278
00279 $vars =& $config_data['vars'];
00280
00281
00282 preg_match_all('!^.*\r?\n?!m', $contents, $match);
00283 $lines = $match[0];
00284 for ($i=0, $count=count($lines); $i<$count; $i++) {
00285 $line = $lines[$i];
00286 if (empty($line)) continue;
00287
00288 if ( $line{0} == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
00289
00290 if ($match[1]{0} == '.') {
00291
00292 if ($this->read_hidden) {
00293 $section_name = substr($match[1], 1);
00294 } else {
00295
00296 unset($vars);
00297 $vars = array();
00298 continue;
00299 }
00300 } else {
00301 $section_name = $match[1];
00302 }
00303 if (!isset($config_data['sections'][$section_name]))
00304 $config_data['sections'][$section_name] = array('vars' => array());
00305 $vars =& $config_data['sections'][$section_name]['vars'];
00306 continue;
00307 }
00308
00309 if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
00310
00311 $var_name = rtrim($match[1]);
00312 if (strpos($match[2], '"""') === 0) {
00313
00314 $lines[$i] = substr($match[2], 3);
00315 $var_value = '';
00316 while ($i<$count) {
00317 if (($pos = strpos($lines[$i], '"""')) === false) {
00318 $var_value .= $lines[$i++];
00319 } else {
00320
00321 $var_value .= substr($lines[$i], 0, $pos);
00322 break;
00323 }
00324 }
00325 $booleanize = false;
00326
00327 } else {
00328
00329 $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
00330 $booleanize = $this->booleanize;
00331
00332 }
00333 $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
00334 }
00335
00336 }
00337 return $config_data;
00338 }
00339
00348 function _set_config_var(&$container, $var_name, $var_value, $booleanize)
00349 {
00350 if ($var_name{0} == '.') {
00351 if (!$this->read_hidden)
00352 return;
00353 else
00354 $var_name = substr($var_name, 1);
00355 }
00356
00357 if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
00358 $this->_trigger_error_msg("Bad variable name '$var_name'");
00359 return;
00360 }
00361
00362 if ($booleanize) {
00363 if (preg_match("/^(on|true|yes)$/i", $var_value))
00364 $var_value = true;
00365 else if (preg_match("/^(off|false|no)$/i", $var_value))
00366 $var_value = false;
00367 }
00368
00369 if (!isset($container[$var_name]) || $this->overwrite)
00370 $container[$var_name] = $var_value;
00371 else {
00372 settype($container[$var_name], 'array');
00373 $container[$var_name][] = $var_value;
00374 }
00375 }
00376
00382 function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
00383 {
00384 trigger_error("Config_File error: $error_msg", $error_type);
00385 }
00387 }
00388
00389 ?>