PageRenderTime 53ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/bblog/libs/Config_File.class.php

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