Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config_File.class.php 12 KiB

пре 18 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. * @link http://smarty.php.net/
  20. * @version 2.6.16
  21. * @copyright Copyright: 2001-2005 New Digital Group, Inc.
  22. * @author Andrei Zmievski <andrei@php.net>
  23. * @access public
  24. * @package Smarty
  25. */
  26. /* $Id: Config_File.class.php,v 1.86 2006/11/30 17:01:28 mohrt Exp $ */
  27. /**
  28. * Config file reading class
  29. * @package Smarty
  30. */
  31. class Config_File {
  32. /**#@+
  33. * Options
  34. * @var boolean
  35. */
  36. /**
  37. * Controls whether variables with the same name overwrite each other.
  38. */
  39. var $overwrite = true;
  40. /**
  41. * Controls whether config values of on/true/yes and off/false/no get
  42. * converted to boolean values automatically.
  43. */
  44. var $booleanize = true;
  45. /**
  46. * Controls whether hidden config sections/vars are read from the file.
  47. */
  48. var $read_hidden = true;
  49. /**
  50. * Controls whether or not to fix mac or dos formatted newlines.
  51. * If set to true, \r or \r\n will be changed to \n.
  52. */
  53. var $fix_newlines = true;
  54. /**#@-*/
  55. /** @access private */
  56. var $_config_path = "";
  57. var $_config_data = array();
  58. /**#@-*/
  59. /**
  60. * Constructs a new config file class.
  61. *
  62. * @param string $config_path (optional) path to the config files
  63. */
  64. function Config_File($config_path = NULL)
  65. {
  66. if (isset($config_path))
  67. $this->set_path($config_path);
  68. }
  69. /**
  70. * Set the path where configuration files can be found.
  71. *
  72. * @param string $config_path path to the config files
  73. */
  74. function set_path($config_path)
  75. {
  76. if (!empty($config_path)) {
  77. if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
  78. $this->_trigger_error_msg("Bad config file path '$config_path'");
  79. return;
  80. }
  81. if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
  82. $config_path .= DIRECTORY_SEPARATOR;
  83. }
  84. $this->_config_path = $config_path;
  85. }
  86. }
  87. /**
  88. * Retrieves config info based on the file, section, and variable name.
  89. *
  90. * @param string $file_name config file to get info for
  91. * @param string $section_name (optional) section to get info for
  92. * @param string $var_name (optional) variable to get info for
  93. * @return string|array a value or array of values
  94. */
  95. function get($file_name, $section_name = NULL, $var_name = NULL)
  96. {
  97. if (empty($file_name)) {
  98. $this->_trigger_error_msg('Empty config file name');
  99. return;
  100. } else {
  101. $file_name = $this->_config_path . $file_name;
  102. if (!isset($this->_config_data[$file_name]))
  103. $this->load_file($file_name, false);
  104. }
  105. if (!empty($var_name)) {
  106. if (empty($section_name)) {
  107. return $this->_config_data[$file_name]["vars"][$var_name];
  108. } else {
  109. if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
  110. return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
  111. else
  112. return array();
  113. }
  114. } else {
  115. if (empty($section_name)) {
  116. return (array)$this->_config_data[$file_name]["vars"];
  117. } else {
  118. if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
  119. return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
  120. else
  121. return array();
  122. }
  123. }
  124. }
  125. /**
  126. * Retrieves config info based on the key.
  127. *
  128. * @param $file_name string config key (filename/section/var)
  129. * @return string|array same as get()
  130. * @uses get() retrieves information from config file and returns it
  131. */
  132. function &get_key($config_key)
  133. {
  134. list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
  135. $result = &$this->get($file_name, $section_name, $var_name);
  136. return $result;
  137. }
  138. /**
  139. * Get all loaded config file names.
  140. *
  141. * @return array an array of loaded config file names
  142. */
  143. function get_file_names()
  144. {
  145. return array_keys($this->_config_data);
  146. }
  147. /**
  148. * Get all section names from a loaded file.
  149. *
  150. * @param string $file_name config file to get section names from
  151. * @return array an array of section names from the specified file
  152. */
  153. function get_section_names($file_name)
  154. {
  155. $file_name = $this->_config_path . $file_name;
  156. if (!isset($this->_config_data[$file_name])) {
  157. $this->_trigger_error_msg("Unknown config file '$file_name'");
  158. return;
  159. }
  160. return array_keys($this->_config_data[$file_name]["sections"]);
  161. }
  162. /**
  163. * Get all global or section variable names.
  164. *
  165. * @param string $file_name config file to get info for
  166. * @param string $section_name (optional) section to get info for
  167. * @return array an array of variables names from the specified file/section
  168. */
  169. function get_var_names($file_name, $section = NULL)
  170. {
  171. if (empty($file_name)) {
  172. $this->_trigger_error_msg('Empty config file name');
  173. return;
  174. } else if (!isset($this->_config_data[$file_name])) {
  175. $this->_trigger_error_msg("Unknown config file '$file_name'");
  176. return;
  177. }
  178. if (empty($section))
  179. return array_keys($this->_config_data[$file_name]["vars"]);
  180. else
  181. return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
  182. }
  183. /**
  184. * Clear loaded config data for a certain file or all files.
  185. *
  186. * @param string $file_name file to clear config data for
  187. */
  188. function clear($file_name = NULL)
  189. {
  190. if ($file_name === NULL)
  191. $this->_config_data = array();
  192. else if (isset($this->_config_data[$file_name]))
  193. $this->_config_data[$file_name] = array();
  194. }
  195. /**
  196. * Load a configuration file manually.
  197. *
  198. * @param string $file_name file name to load
  199. * @param boolean $prepend_path whether current config path should be
  200. * prepended to the filename
  201. */
  202. function load_file($file_name, $prepend_path = true)
  203. {
  204. if ($prepend_path && $this->_config_path != "")
  205. $config_file = $this->_config_path . $file_name;
  206. else
  207. $config_file = $file_name;
  208. ini_set('track_errors', true);
  209. $fp = @fopen($config_file, "r");
  210. if (!is_resource($fp)) {
  211. $this->_trigger_error_msg("Could not open config file '$config_file'");
  212. return false;
  213. }
  214. $contents = ($size = filesize($config_file)) ? fread($fp, $size) : '';
  215. fclose($fp);
  216. $this->_config_data[$config_file] = $this->parse_contents($contents);
  217. return true;
  218. }
  219. /**
  220. * Store the contents of a file manually.
  221. *
  222. * @param string $config_file file name of the related contents
  223. * @param string $contents the file-contents to parse
  224. */
  225. function set_file_contents($config_file, $contents)
  226. {
  227. $this->_config_data[$config_file] = $this->parse_contents($contents);
  228. return true;
  229. }
  230. /**
  231. * parse the source of a configuration file manually.
  232. *
  233. * @param string $contents the file-contents to parse
  234. */
  235. function parse_contents($contents)
  236. {
  237. if($this->fix_newlines) {
  238. // fix mac/dos formatted newlines
  239. $contents = preg_replace('!\r\n?!', "\n", $contents);
  240. }
  241. $config_data = array();
  242. $config_data['sections'] = array();
  243. $config_data['vars'] = array();
  244. /* reference to fill with data */
  245. $vars =& $config_data['vars'];
  246. /* parse file line by line */
  247. preg_match_all('!^.*\r?\n?!m', $contents, $match);
  248. $lines = $match[0];
  249. for ($i=0, $count=count($lines); $i<$count; $i++) {
  250. $line = $lines[$i];
  251. if (empty($line)) continue;
  252. if ( substr($line, 0, 1) == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
  253. /* section found */
  254. if (substr($match[1], 0, 1) == '.') {
  255. /* hidden section */
  256. if ($this->read_hidden) {
  257. $section_name = substr($match[1], 1);
  258. } else {
  259. /* break reference to $vars to ignore hidden section */
  260. unset($vars);
  261. $vars = array();
  262. continue;
  263. }
  264. } else {
  265. $section_name = $match[1];
  266. }
  267. if (!isset($config_data['sections'][$section_name]))
  268. $config_data['sections'][$section_name] = array('vars' => array());
  269. $vars =& $config_data['sections'][$section_name]['vars'];
  270. continue;
  271. }
  272. if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
  273. /* variable found */
  274. $var_name = rtrim($match[1]);
  275. if (strpos($match[2], '"""') === 0) {
  276. /* handle multiline-value */
  277. $lines[$i] = substr($match[2], 3);
  278. $var_value = '';
  279. while ($i<$count) {
  280. if (($pos = strpos($lines[$i], '"""')) === false) {
  281. $var_value .= $lines[$i++];
  282. } else {
  283. /* end of multiline-value */
  284. $var_value .= substr($lines[$i], 0, $pos);
  285. break;
  286. }
  287. }
  288. $booleanize = false;
  289. } else {
  290. /* handle simple value */
  291. $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
  292. $booleanize = $this->booleanize;
  293. }
  294. $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
  295. }
  296. /* else unparsable line / means it is a comment / means ignore it */
  297. }
  298. return $config_data;
  299. }
  300. /**#@+ @access private */
  301. /**
  302. * @param array &$container
  303. * @param string $var_name
  304. * @param mixed $var_value
  305. * @param boolean $booleanize determines whether $var_value is converted to
  306. * to true/false
  307. */
  308. function _set_config_var(&$container, $var_name, $var_value, $booleanize)
  309. {
  310. if (substr($var_name, 0, 1) == '.') {
  311. if (!$this->read_hidden)
  312. return;
  313. else
  314. $var_name = substr($var_name, 1);
  315. }
  316. if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
  317. $this->_trigger_error_msg("Bad variable name '$var_name'");
  318. return;
  319. }
  320. if ($booleanize) {
  321. if (preg_match("/^(on|true|yes)$/i", $var_value))
  322. $var_value = true;
  323. else if (preg_match("/^(off|false|no)$/i", $var_value))
  324. $var_value = false;
  325. }
  326. if (!isset($container[$var_name]) || $this->overwrite)
  327. $container[$var_name] = $var_value;
  328. else {
  329. settype($container[$var_name], 'array');
  330. $container[$var_name][] = $var_value;
  331. }
  332. }
  333. /**
  334. * @uses trigger_error() creates a PHP warning/error
  335. * @param string $error_msg
  336. * @param integer $error_type one of
  337. */
  338. function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
  339. {
  340. trigger_error("Config_File error: $error_msg", $error_type);
  341. }
  342. /**#@-*/
  343. }
  344. ?>