PageRenderTime 58ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/PhpDocumentor/phpDocumentor/Smarty-2.6.0/libs/Config_File.class.php

https://github.com/atutor/phpdoc2
PHP | 365 lines | 185 code | 47 blank | 133 comment | 56 complexity | 679aeadb1be28c5cb32160ba2626aa9f MD5 | raw file
  1. <?php
  2. /**
  3. * Config_File class.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * You may contact the author of Config_File by e-mail at:
  20. * {@link andrei@php.net}
  21. *
  22. * The latest version of Config_File can be obtained from:
  23. * http://smarty.php.net/
  24. *
  25. * @link http://smarty.php.net/
  26. * @version 2.6.0
  27. * @copyright Copyright: 2001-2003 ispi of Lincoln, Inc.
  28. * @author Andrei Zmievski <andrei@php.net>
  29. * @access public
  30. * @package Smarty
  31. */
  32. /* $Id: Config_File.class.php,v 1.1 2005/10/17 18:37:39 jeichorn Exp $ */
  33. /**
  34. * Config file reading class
  35. * @package Smarty
  36. */
  37. class Config_File {
  38. /**#@+
  39. * Options
  40. * @var boolean
  41. */
  42. /**
  43. * Controls whether variables with the same name overwrite each other.
  44. */
  45. var $overwrite = true;
  46. /**
  47. * Controls whether config values of on/true/yes and off/false/no get
  48. * converted to boolean values automatically.
  49. */
  50. var $booleanize = true;
  51. /**
  52. * Controls whether hidden config sections/vars are read from the file.
  53. */
  54. var $read_hidden = true;
  55. /**
  56. * 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. */
  59. var $fix_newlines = true;
  60. /**#@-*/
  61. /** @access private */
  62. var $_config_path = "";
  63. var $_config_data = array();
  64. /**#@-*/
  65. /**
  66. * Constructs a new config file class.
  67. *
  68. * @param string $config_path (optional) path to the config files
  69. */
  70. function Config_File($config_path = NULL)
  71. {
  72. if (isset($config_path))
  73. $this->set_path($config_path);
  74. }
  75. /**
  76. * Set the path where configuration files can be found.
  77. *
  78. * @param string $config_path path to the config files
  79. */
  80. function set_path($config_path)
  81. {
  82. if (!empty($config_path)) {
  83. if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
  84. $this->_trigger_error_msg("Bad config file path '$config_path'");
  85. return;
  86. }
  87. if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
  88. $config_path .= DIRECTORY_SEPARATOR;
  89. }
  90. $this->_config_path = $config_path;
  91. }
  92. }
  93. /**
  94. * Retrieves config info based on the file, section, and variable name.
  95. *
  96. * @param string $file_name config file to get info for
  97. * @param string $section_name (optional) section to get info for
  98. * @param string $var_name (optional) variable to get info for
  99. * @return string|array a value or array of values
  100. */
  101. function &get($file_name, $section_name = NULL, $var_name = NULL)
  102. {
  103. if (empty($file_name)) {
  104. $this->_trigger_error_msg('Empty config file name');
  105. return;
  106. } else {
  107. $file_name = $this->_config_path . $file_name;
  108. if (!isset($this->_config_data[$file_name]))
  109. $this->load_file($file_name, false);
  110. }
  111. if (!empty($var_name)) {
  112. if (empty($section_name)) {
  113. return $this->_config_data[$file_name]["vars"][$var_name];
  114. } else {
  115. if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
  116. return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
  117. else
  118. return array();
  119. }
  120. } else {
  121. if (empty($section_name)) {
  122. return (array)$this->_config_data[$file_name]["vars"];
  123. } else {
  124. if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
  125. return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
  126. else
  127. return array();
  128. }
  129. }
  130. }
  131. /**
  132. * Retrieves config info based on the key.
  133. *
  134. * @param $file_name string config key (filename/section/var)
  135. * @return string|array same as get()
  136. * @uses get() retrieves information from config file and returns it
  137. */
  138. function &get_key($config_key)
  139. {
  140. list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
  141. $result = &$this->get($file_name, $section_name, $var_name);
  142. return $result;
  143. }
  144. /**
  145. * Get all loaded config file names.
  146. *
  147. * @return array an array of loaded config file names
  148. */
  149. function get_file_names()
  150. {
  151. return array_keys($this->_config_data);
  152. }
  153. /**
  154. * Get all section names from a loaded file.
  155. *
  156. * @param string $file_name config file to get section names from
  157. * @return array an array of section names from the specified file
  158. */
  159. function get_section_names($file_name)
  160. {
  161. $file_name = $this->_config_path . $file_name;
  162. if (!isset($this->_config_data[$file_name])) {
  163. $this->_trigger_error_msg("Unknown config file '$file_name'");
  164. return;
  165. }
  166. return array_keys($this->_config_data[$file_name]["sections"]);
  167. }
  168. /**
  169. * Get all global or section variable names.
  170. *
  171. * @param string $file_name config file to get info for
  172. * @param string $section_name (optional) section to get info for
  173. * @return array an array of variables names from the specified file/section
  174. */
  175. function get_var_names($file_name, $section = NULL)
  176. {
  177. if (empty($file_name)) {
  178. $this->_trigger_error_msg('Empty config file name');
  179. return;
  180. } else if (!isset($this->_config_data[$file_name])) {
  181. $this->_trigger_error_msg("Unknown config file '$file_name'");
  182. return;
  183. }
  184. if (empty($section))
  185. return array_keys($this->_config_data[$file_name]["vars"]);
  186. else
  187. return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
  188. }
  189. /**
  190. * Clear loaded config data for a certain file or all files.
  191. *
  192. * @param string $file_name file to clear config data for
  193. */
  194. function clear($file_name = NULL)
  195. {
  196. if ($file_name === NULL)
  197. $this->_config_data = array();
  198. else if (isset($this->_config_data[$file_name]))
  199. $this->_config_data[$file_name] = array();
  200. }
  201. /**
  202. * Load a configuration file manually.
  203. *
  204. * @param string $file_name file name to load
  205. * @param boolean $prepend_path whether current config path should be
  206. * prepended to the filename
  207. */
  208. function load_file($file_name, $prepend_path = true)
  209. {
  210. if ($prepend_path && $this->_config_path != "")
  211. $config_file = $this->_config_path . $file_name;
  212. else
  213. $config_file = $file_name;
  214. ini_set('track_errors', true);
  215. $fp = @fopen($config_file, "r");
  216. if (!is_resource($fp)) {
  217. $this->_trigger_error_msg("Could not open config file '$config_file'");
  218. return false;
  219. }
  220. $contents = fread($fp, filesize($config_file));
  221. fclose($fp);
  222. if($this->fix_newlines) {
  223. // fix mac/dos formatted newlines
  224. $contents = preg_replace('!\r\n?!',"\n",$contents);
  225. }
  226. $config_data = array();
  227. /* replace all multi-line values by placeholders */
  228. if (preg_match_all('/"""(.*)"""/Us', $contents, $match)) {
  229. $_triple_quotes = $match[1];
  230. $_i = 0;
  231. $contents = preg_replace('/""".*"""/Use', '"\x1b\x1b\x1b".$_i++."\x1b\x1b\x1b"', $contents);
  232. } else {
  233. $_triple_quotes = null;
  234. }
  235. /* Get global variables first. */
  236. if ($contents{0} != '[' && preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
  237. $config_data["vars"] = $this->_parse_config_block($match[1], $_triple_quotes);
  238. /* Get section variables. */
  239. $config_data["sections"] = array();
  240. preg_match_all("/^\[(.*?)\]/m", $contents, $match);
  241. foreach ($match[1] as $section) {
  242. if ($section{0} == '.' && !$this->read_hidden)
  243. continue;
  244. if (preg_match("/\[".preg_quote($section, '/')."\](.*?)(\n\[|\Z)/s", $contents, $match))
  245. if ($section{0} == '.')
  246. $section = substr($section, 1);
  247. $config_data["sections"][$section]["vars"] = $this->_parse_config_block($match[1], $_triple_quotes);
  248. }
  249. $this->_config_data[$config_file] = $config_data;
  250. return true;
  251. }
  252. /**#@+ @access private */
  253. /**
  254. * @var string $config_block
  255. */
  256. function _parse_config_block($config_block, $triple_quotes)
  257. {
  258. $vars = array();
  259. /* First we grab the multi-line values. */
  260. if (preg_match_all("/^([^=\n]+)=\s*\x1b\x1b\x1b(\d+)\x1b\x1b\x1b\s*$/ms", $config_block, $match, PREG_SET_ORDER)) {
  261. for ($i = 0; $i < count($match); $i++) {
  262. $this->_set_config_var($vars, trim($match[$i][1]), $triple_quotes[$match[$i][2]], false);
  263. }
  264. $config_block = preg_replace("/^[^=\n]+=\s*\x1b\x1b\x1b\d+\x1b\x1b\x1b\s*$/ms", "", $config_block);
  265. }
  266. $config_lines = preg_split("/\n+/", $config_block);
  267. foreach ($config_lines as $line) {
  268. if (preg_match("/^\s*(\.?\w+)\s*=(.*)/", $line, $match)) {
  269. $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', trim($match[2]));
  270. $this->_set_config_var($vars, trim($match[1]), $var_value, $this->booleanize);
  271. }
  272. }
  273. return $vars;
  274. }
  275. /**
  276. * @param array &$container
  277. * @param string $var_name
  278. * @param mixed $var_value
  279. * @param boolean $booleanize determines whether $var_value is converted to
  280. * to true/false
  281. */
  282. function _set_config_var(&$container, $var_name, $var_value, $booleanize)
  283. {
  284. if ($var_name{0} == '.') {
  285. if (!$this->read_hidden)
  286. return;
  287. else
  288. $var_name = substr($var_name, 1);
  289. }
  290. if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
  291. $this->_trigger_error_msg("Bad variable name '$var_name'");
  292. return;
  293. }
  294. if ($booleanize) {
  295. if (preg_match("/^(on|true|yes)$/i", $var_value))
  296. $var_value = true;
  297. else if (preg_match("/^(off|false|no)$/i", $var_value))
  298. $var_value = false;
  299. }
  300. if (!isset($container[$var_name]) || $this->overwrite)
  301. $container[$var_name] = $var_value;
  302. else {
  303. settype($container[$var_name], 'array');
  304. $container[$var_name][] = $var_value;
  305. }
  306. }
  307. /**
  308. * @uses trigger_error() creates a PHP warning/error
  309. * @param string $error_msg
  310. * @param integer $error_type one of
  311. */
  312. function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
  313. {
  314. trigger_error("Config_File error: $error_msg", $error_type);
  315. }
  316. /**#@-*/
  317. }
  318. ?>