PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/external_lib/Smarty/Config_File.class.php

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