/[cvs]/nfo/php/libs/net.php.smarty/Config_File.class.php
ViewVC logotype

Diff of /nfo/php/libs/net.php.smarty/Config_File.class.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by cvsjoko, Wed Oct 9 00:45:29 2002 UTC revision 1.3 by joko, Wed Jun 16 21:58:11 2004 UTC
# Line 3  Line 3 
3  /**  /**
4   * Config_File class.   * Config_File class.
5   *   *
  * @version 2.3.0  
  * @author Andrei Zmievski <andrei@php.net>  
  * @access public  
  *  
  * Copyright: 2001,2002 ispi of Lincoln, Inc.  
  *  
6   * This library is free software; you can redistribute it and/or   * This library is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU Lesser General Public   * modify it under the terms of the GNU Lesser General Public
8   * License as published by the Free Software Foundation; either   * License as published by the Free Software Foundation; either
# Line 23  Line 17 
17   * License along with this library; if not, write to the Free Software   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *   *
20   * You may contact the author of Config_File by e-mail at:   * @link http://smarty.php.net/
21   * andrei@php.net   * @version 2.6.3
22   *   * @copyright Copyright: 2001-2004 ispi of Lincoln, Inc.
23   * Or, write to:   * @author Andrei Zmievski <andrei@php.net>
24   * Andrei Zmievski   * @access public
25   * Software Engineer, ispi   * @package Smarty
  * 237 S. 70th suite 220  
  * Lincoln, NE 68510  
  *  
  * The latest version of Config_File can be obtained from:  
  * http://www.phpinsider.com  
26   */   */
27    
28    /* $Id$ */
29    
30    /**
31     * Config file reading class
32     * @package Smarty
33     */
34  class Config_File {  class Config_File {
35          /* Options */      /**#@+
36          /**       * Options
37           * Controls whether variables with the same name overwrite each other.       * @var boolean
38           *       */
39           * @access public      /**
40           */       * Controls whether variables with the same name overwrite each other.
41          var $overwrite          =       true;       */
42        var $overwrite        =    true;
43          /**  
44           * Controls whether config values of on/true/yes and off/false/no get      /**
45           * converted to boolean values automatically.       * Controls whether config values of on/true/yes and off/false/no get
46           *       * converted to boolean values automatically.
47           * @access public       */
48           */      var $booleanize        =    true;
49          var $booleanize         =       true;  
50        /**
51          /**       * Controls whether hidden config sections/vars are read from the file.
52           * Controls whether hidden config sections/vars are read from the file.       */
53           *      var $read_hidden     =    true;
54           * @access public  
55           */      /**
56          var $read_hidden        =       true;       * Controls whether or not to fix mac or dos formatted newlines.
57         * If set to true, \r or \r\n will be changed to \n.
58          /* Private variables */       */
59          var $_config_path       = "";      var $fix_newlines =    true;
60          var $_config_data       = array();      /**#@-*/
61          var $_separator         = "";  
62        /** @access private */
63        var $_config_path    = "";
64          /**      var $_config_data    = array();
65           * Constructs a new config file class.      /**#@-*/
66           *  
67           * @param $config_path string (optional) path to the config files      /**
68           * @access public       * Constructs a new config file class.
69           */       *
70          function Config_File($config_path = NULL)       * @param string $config_path (optional) path to the config files
71          {       */
72                  if (substr(PHP_OS, 0, 3) == "WIN" || substr(PHP_OS, 0, 4) == "OS/2")      function Config_File($config_path = NULL)
73                          $this->_separator = "\\";      {
74                  else          if (isset($config_path))
75                          $this->_separator = "/";              $this->set_path($config_path);
76        }
77                  if (isset($config_path))  
78                          $this->set_path($config_path);  
79          }      /**
80         * Set the path where configuration files can be found.
81         *
82          /**       * @param string $config_path path to the config files
83           * Set the path where configuration files can be found.       */
84           *      function set_path($config_path)
85           * @param $config_path string  path to the config files      {
86           * @access public          if (!empty($config_path)) {
87           */              if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
88          function set_path($config_path)                  $this->_trigger_error_msg("Bad config file path '$config_path'");
89          {                  return;
90                  if (!empty($config_path)) {              }
91                          if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {              if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
92                                  $this->_trigger_error_msg("Bad config file path '$config_path'");                  $config_path .= DIRECTORY_SEPARATOR;
93                                  return;              }
94                          }  
95                $this->_config_path = $config_path;
96                          $this->_config_path = $config_path . $this->_separator;          }
97                  }      }
98          }  
99    
100                /**
101          /**       * Retrieves config info based on the file, section, and variable name.
102           * Retrieves config info based on the file, section, and variable name.       *
103           *       * @param string $file_name config file to get info for
104           * @access public       * @param string $section_name (optional) section to get info for
105           * @param $file_name string config file to get info for       * @param string $var_name (optional) variable to get info for
106           * @param $section_name string (optional) section to get info for       * @return string|array a value or array of values
107           * @param $var_name string (optional) variable to get info for       */
108           * @return mixed a value or array of values      function &get($file_name, $section_name = NULL, $var_name = NULL)
109           */      {
110          function &get($file_name, $section_name = NULL, $var_name = NULL)          if (empty($file_name)) {
111          {              $this->_trigger_error_msg('Empty config file name');
112                  if (empty($file_name)) {              return;
113                          $this->_trigger_error_msg('Empty config file name');          } else {
114                          return;              $file_name = $this->_config_path . $file_name;
115                  } else {              if (!isset($this->_config_data[$file_name]))
116                          $file_name = $this->_config_path . $file_name;                  $this->load_file($file_name, false);
117                          if (!isset($this->_config_data[$file_name]))          }
118                                  $this->load_file($file_name, false);  
119                  }          if (!empty($var_name)) {
120                                if (empty($section_name)) {
121                  if (!empty($var_name)) {                  return $this->_config_data[$file_name]["vars"][$var_name];
122                          if (empty($section_name)) {              } else {
123                                  return $this->_config_data[$file_name]["vars"][$var_name];                  if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
124                          } else {                      return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
125                                  if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))                  else
126                                          return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];                      return array();
127                                  else              }
128                                          return array();          } else {
129                          }              if (empty($section_name)) {
130                  } else {                  return (array)$this->_config_data[$file_name]["vars"];
131                          if (empty($section_name)) {              } else {
132                                  return (array)$this->_config_data[$file_name]["vars"];                  if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
133                          } else {                      return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
134                                  if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))                  else
135                                          return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];                      return array();
136                                  else              }
137                                          return array();          }
138                          }      }
139                  }  
140          }  
141                /**
142         * Retrieves config info based on the key.
143          /**       *
144           * Retrieves config info based on the key.       * @param $file_name string config key (filename/section/var)
145           *       * @return string|array same as get()
146           * @access public       * @uses get() retrieves information from config file and returns it
147           * @param $file_name string config key (filename/section/var)       */
148           * @return mixed a value or array of values      function &get_key($config_key)
149           */      {
150          function &get_key($config_key)          list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
151          {          $result = &$this->get($file_name, $section_name, $var_name);
152                  list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);          return $result;
153                  $result = &$this->get($file_name, $section_name, $var_name);      }
154                  return $result;  
155          }      /**
156         * Get all loaded config file names.
157          /**       *
158           * Get all loaded config file names.       * @return array an array of loaded config file names
159           *       */
160           * @access public      function get_file_names()
161           * @return array an array of loaded config file names      {
162           */          return array_keys($this->_config_data);
163          function get_file_names()      }
164          {  
165                  return array_keys($this->_config_data);  
166          }      /**
167                 * Get all section names from a loaded file.
168         *
169          /**       * @param string $file_name config file to get section names from
170           * Get all section names from a loaded file.       * @return array an array of section names from the specified file
171           *       */
172           * @access public      function get_section_names($file_name)
173           * @param  $file_name string config file to get section names from      {
174           * @return array an array of section names from the specified file          $file_name = $this->_config_path . $file_name;
175           */          if (!isset($this->_config_data[$file_name])) {
176          function get_section_names($file_name)              $this->_trigger_error_msg("Unknown config file '$file_name'");
177          {              return;
178                  $file_name = $this->_config_path . $file_name;          }
179                  if (!isset($this->_config_data[$file_name])) {  
180                          $this->_trigger_error_msg("Unknown config file '$file_name'");          return array_keys($this->_config_data[$file_name]["sections"]);
181                          return;      }
182                  }  
183                    
184                  return array_keys($this->_config_data[$file_name]["sections"]);      /**
185          }       * Get all global or section variable names.
186                 *
187         * @param string $file_name config file to get info for
188          /**       * @param string $section_name (optional) section to get info for
189           * Get all global or section variable names.       * @return array an array of variables names from the specified file/section
190           *       */
191           * @access public      function get_var_names($file_name, $section = NULL)
192           * @param $file_name string config file to get info for      {
193           * @param $section_name string (optional) section to get info for          if (empty($file_name)) {
194           * @return array an array of variables names from the specified file/section              $this->_trigger_error_msg('Empty config file name');
195           */              return;
196          function get_var_names($file_name, $section = NULL)          } else if (!isset($this->_config_data[$file_name])) {
197          {              $this->_trigger_error_msg("Unknown config file '$file_name'");
198                  if (empty($file_name)) {              return;
199                          $this->_trigger_error_msg('Empty config file name');          }
200                          return;  
201                  } else if (!isset($this->_config_data[$file_name])) {          if (empty($section))
202                          $this->_trigger_error_msg("Unknown config file '$file_name'");              return array_keys($this->_config_data[$file_name]["vars"]);
203                          return;          else
204                  }              return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
205                        }
206                  if (empty($section))  
207                          return array_keys($this->_config_data[$file_name]["vars"]);  
208                  else      /**
209                          return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);       * Clear loaded config data for a certain file or all files.
210          }       *
211                 * @param string $file_name file to clear config data for
212         */
213          /**      function clear($file_name = NULL)
214           * Clear loaded config data for a certain file or all files.      {
215           *          if ($file_name === NULL)
216           * @access public              $this->_config_data = array();
217           * @param  $file_name string file to clear config data for          else if (isset($this->_config_data[$file_name]))
218           */              $this->_config_data[$file_name] = array();
219          function clear($file_name = NULL)      }
220          {  
221                  if ($file_name === NULL)  
222                          $this->_config_data = array();      /**
223                  else if (isset($this->_config_data[$file_name]))       * Load a configuration file manually.
224                          $this->_config_data[$file_name] = array();       *
225          }       * @param string $file_name file name to load
226         * @param boolean $prepend_path whether current config path should be
227         *                              prepended to the filename
228          /**       */
229           * Load a configuration file manually.      function load_file($file_name, $prepend_path = true)
230           *      {
231           * @access public          if ($prepend_path && $this->_config_path != "")
232           * @param  $file_name string file name to load              $config_file = $this->_config_path . $file_name;
233           * @param  $prepend_path boolean whether current config path should be prepended to the filename          else
234           */              $config_file = $file_name;
235          function load_file($file_name, $prepend_path = true)  
236          {          ini_set('track_errors', true);
237                  if ($prepend_path && $this->_config_path != "")          $fp = @fopen($config_file, "r");
238                          $config_file = $this->_config_path . $file_name;          if (!is_resource($fp)) {
239                  else              $this->_trigger_error_msg("Could not open config file '$config_file'");
240                          $config_file = $file_name;              return false;
241            }
242                  ini_set('track_errors', true);  
243                  $fp = @fopen($config_file, "r");          $contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
244                  if (!is_resource($fp)) {          fclose($fp);
245                          $this->_trigger_error_msg("Could not open config file '$config_file'");  
246                          return;          $this->_config_data[$config_file] = $this->parse_contents($contents);
247                  }          return true;
248        }
249                  $contents = fread($fp, filesize($config_file));  
250                  fclose($fp);      /**
251         * Store the contents of a file manually.
252                  $config_data = array();       *
253         * @param string $config_file file name of the related contents
254                  /* Get global variables first. */       * @param string $contents the file-contents to parse
255                  if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))       */
256                          $config_data["vars"] = $this->_parse_config_block($match[1]);      function set_file_contents($config_file, $contents)
257                        {
258                  /* Get section variables. */          $this->_config_data[$config_file] = $this->parse_contents($contents);
259                  $config_data["sections"] = array();          return true;
260                  preg_match_all("/^\[(.*?)\]/m", $contents, $match);      }
261                  foreach ($match[1] as $section) {  
262                          if ($section{0} == '.' && !$this->read_hidden)      /**
263                                  continue;       * parse the source of a configuration file manually.
264                          if (preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s", $contents, $match))       *
265                                  if ($section{0} == '.')       * @param string $contents the file-contents to parse
266                                          $section = substr($section, 1);       */
267                                  $config_data["sections"][$section]["vars"] = $this->_parse_config_block($match[1]);      function parse_contents($contents)
268                  }      {
269            if($this->fix_newlines) {
270                  $this->_config_data[$config_file] = $config_data;              // fix mac/dos formatted newlines
271          }              $contents = preg_replace('!\r\n?!', "\n", $contents);
272            }
273            
274          function _parse_config_block($config_block)          $config_data = array();
275          {          $config_data['sections'] = array();
276                  $vars = array();          $config_data['vars'] = array();
277    
278                  /* First we grab the multi-line values. */          /* reference to fill with data */
279                  if (preg_match_all("/^([^=\n]+)=\s*\"{3}(.*?)\"{3}\s*$/ms", $config_block, $match, PREG_SET_ORDER)) {          $vars =& $config_data['vars'];
280                          for ($i = 0; $i < count($match); $i++) {  
281                                  $this->_set_config_var($vars, trim($match[$i][1]), $match[$i][2], false);          /* parse file line by line */
282                          }          preg_match_all('!^.*\r?\n?!m', $contents, $match);
283                          $config_block = preg_replace("/^[^=\n]+=\s*\"{3}.*?\"{3}\s*$/ms", "", $config_block);          $lines = $match[0];
284                  }          for ($i=0, $count=count($lines); $i<$count; $i++) {
285                                $line = $lines[$i];
286                                if (empty($line)) continue;
287                  $config_lines = preg_split("/\n+/", $config_block);  
288                if ( $line{0} == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
289                  foreach ($config_lines as $line) {                  /* section found */
290                          if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) {                  if ($match[1]{0} == '.') {
291                                  $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', trim($match[2]));                      /* hidden section */
292                                  $this->_set_config_var($vars, trim($match[1]), $var_value, $this->booleanize);                      if ($this->read_hidden) {
293                          }                          $section_name = substr($match[1], 1);
294                  }                      } else {
295                            /* break reference to $vars to ignore hidden section */
296                  return $vars;                          unset($vars);
297          }                          $vars = array();
298                                    continue;
299          function _set_config_var(&$container, $var_name, $var_value, $booleanize)                      }
300          {                  } else {                    
301                  if ($var_name{0} == '.') {                      $section_name = $match[1];
302                          if (!$this->read_hidden)                  }
303                                  return;                  if (!isset($config_data['sections'][$section_name]))
304                          else                      $config_data['sections'][$section_name] = array('vars' => array());
305                                  $var_name = substr($var_name, 1);                  $vars =& $config_data['sections'][$section_name]['vars'];
306                  }                  continue;
307                }
308                  if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {  
309                          $this->_trigger_error_msg("Bad variable name '$var_name'");              if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
310                          return;                  /* variable found */
311                  }                  $var_name = rtrim($match[1]);
312                    if (strpos($match[2], '"""') === 0) {
313                  if ($booleanize) {                      /* handle multiline-value */
314                          if (preg_match("/^(on|true|yes)$/i", $var_value))                      $lines[$i] = substr($match[2], 3);
315                                  $var_value = true;                      $var_value = '';
316                          else if (preg_match("/^(off|false|no)$/i", $var_value))                      while ($i<$count) {
317                                  $var_value = false;                          if (($pos = strpos($lines[$i], '"""')) === false) {
318                  }                              $var_value .= $lines[$i++];
319                                                            } else {
320                  if (!isset($container[$var_name]) || $this->overwrite)                              /* end of multiline-value */
321                          $container[$var_name] = $var_value;                              $var_value .= substr($lines[$i], 0, $pos);
322                  else {                              break;
323                          settype($container[$var_name], 'array');                          }
324                          $container[$var_name][] = $var_value;                      }
325                  }                      $booleanize = false;
326          }  
327                    } else {
328          function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)                      /* handle simple value */
329          {                      $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
330                  trigger_error("Config_File error: $error_msg", $error_type);                      $booleanize = $this->booleanize;
331          }  
332                    }
333                    $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
334                }
335                /* else unparsable line / means it is a comment / means ignore it */
336            }
337            return $config_data;
338        }
339    
340        /**#@+ @access private */
341        /**
342         * @param array &$container
343         * @param string $var_name
344         * @param mixed $var_value
345         * @param boolean $booleanize determines whether $var_value is converted to
346         *                            to true/false
347         */
348        function _set_config_var(&$container, $var_name, $var_value, $booleanize)
349        {
350            if ($var_name{0} == '.') {
351                if (!$this->read_hidden)
352                    return;
353                else
354                    $var_name = substr($var_name, 1);
355            }
356    
357            if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
358                $this->_trigger_error_msg("Bad variable name '$var_name'");
359                return;
360            }
361    
362            if ($booleanize) {
363                if (preg_match("/^(on|true|yes)$/i", $var_value))
364                    $var_value = true;
365                else if (preg_match("/^(off|false|no)$/i", $var_value))
366                    $var_value = false;
367            }
368    
369            if (!isset($container[$var_name]) || $this->overwrite)
370                $container[$var_name] = $var_value;
371            else {
372                settype($container[$var_name], 'array');
373                $container[$var_name][] = $var_value;
374            }
375        }
376    
377        /**
378         * @uses trigger_error() creates a PHP warning/error
379         * @param string $error_msg
380         * @param integer $error_type one of
381         */
382        function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
383        {
384            trigger_error("Config_File error: $error_msg", $error_type);
385        }
386        /**#@-*/
387  }  }
388    
389  ?>  ?>

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.3

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed