/bonfire/codeigniter/core/Config.php

https://github.com/jeffmay/Bonfire · PHP · 321 lines · 167 code · 41 blank · 113 comment · 31 complexity · a7aa94b014f2860e1656f87e8cc17cbe MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter Config Class
  18. *
  19. * This class contains functions that enable config files to be managed
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Libraries
  23. * @category Libraries
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/libraries/config.html
  26. */
  27. class CI_Config {
  28. var $config = array();
  29. var $is_loaded = array();
  30. var $_config_paths = array(APPPATH);
  31. /**
  32. * Constructor
  33. *
  34. * Sets the $config data from the primary config.php file as a class variable
  35. *
  36. * @access public
  37. * @param string the config file name
  38. * @param boolean if configuration values should be loaded into their own section
  39. * @param boolean true if errors should just return false, false if an error message should be displayed
  40. * @return boolean if the file was successfully loaded or not
  41. */
  42. function __construct()
  43. {
  44. $this->config =& get_config();
  45. log_message('debug', "Config Class Initialized");
  46. // Set the base_url automatically if none was provided
  47. if ($this->config['base_url'] == '')
  48. {
  49. if (isset($_SERVER['HTTP_HOST']))
  50. {
  51. $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  52. $base_url .= '://'. $_SERVER['HTTP_HOST'];
  53. $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  54. }
  55. else
  56. {
  57. $base_url = 'http://localhost/';
  58. }
  59. $this->set_item('base_url', $base_url);
  60. }
  61. }
  62. // --------------------------------------------------------------------
  63. /**
  64. * Load Config File
  65. *
  66. * @access public
  67. * @param string the config file name
  68. * @param boolean if configuration values should be loaded into their own section
  69. * @param boolean true if errors should just return false, false if an error message should be displayed
  70. * @return boolean if the file was loaded correctly
  71. */
  72. function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  73. {
  74. $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
  75. $loaded = FALSE;
  76. foreach ($this->_config_paths as $path)
  77. {
  78. $file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT;
  79. if (in_array($file_path, $this->is_loaded, TRUE))
  80. {
  81. $loaded = TRUE;
  82. continue;
  83. }
  84. if ( ! file_exists($file_path))
  85. {
  86. log_message('debug', 'Config for '.ENVIRONMENT.' environment is not found. Trying global config.');
  87. $file_path = $path.'config/'.$file.EXT;
  88. if ( ! file_exists($file_path))
  89. {
  90. continue;
  91. }
  92. }
  93. include($file_path);
  94. if ( ! isset($config) OR ! is_array($config))
  95. {
  96. if ($fail_gracefully === TRUE)
  97. {
  98. return FALSE;
  99. }
  100. show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
  101. }
  102. if ($use_sections === TRUE)
  103. {
  104. if (isset($this->config[$file]))
  105. {
  106. $this->config[$file] = array_merge($this->config[$file], $config);
  107. }
  108. else
  109. {
  110. $this->config[$file] = $config;
  111. }
  112. }
  113. else
  114. {
  115. $this->config = array_merge($this->config, $config);
  116. }
  117. $this->is_loaded[] = $file_path;
  118. unset($config);
  119. $loaded = TRUE;
  120. log_message('debug', 'Config file loaded: '.$file_path);
  121. }
  122. if ($loaded === FALSE)
  123. {
  124. if ($fail_gracefully === TRUE)
  125. {
  126. return FALSE;
  127. }
  128. show_error('The configuration file '.ENVIRONMENT.'/'.$file.EXT.' and '.$file.EXT.' do not exist.');
  129. }
  130. return TRUE;
  131. }
  132. // --------------------------------------------------------------------
  133. /**
  134. * Fetch a config file item
  135. *
  136. *
  137. * @access public
  138. * @param string the config item name
  139. * @param string the index name
  140. * @param bool
  141. * @return string
  142. */
  143. function item($item, $index = '')
  144. {
  145. if ($index == '')
  146. {
  147. if ( ! isset($this->config[$item]))
  148. {
  149. return FALSE;
  150. }
  151. $pref = $this->config[$item];
  152. }
  153. else
  154. {
  155. if ( ! isset($this->config[$index]))
  156. {
  157. return FALSE;
  158. }
  159. if ( ! isset($this->config[$index][$item]))
  160. {
  161. return FALSE;
  162. }
  163. $pref = $this->config[$index][$item];
  164. }
  165. return $pref;
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Fetch a config file item - adds slash after item
  170. *
  171. * The second parameter allows a slash to be added to the end of
  172. * the item, in the case of a path.
  173. *
  174. * @access public
  175. * @param string the config item name
  176. * @param bool
  177. * @return string
  178. */
  179. function slash_item($item)
  180. {
  181. if ( ! isset($this->config[$item]))
  182. {
  183. return FALSE;
  184. }
  185. return rtrim($this->config[$item], '/').'/';
  186. }
  187. // --------------------------------------------------------------------
  188. /**
  189. * Site URL
  190. *
  191. * @access public
  192. * @param string the URI string
  193. * @return string
  194. */
  195. function site_url($uri = '')
  196. {
  197. if ($uri == '')
  198. {
  199. return $this->slash_item('base_url').$this->item('index_page');
  200. }
  201. if ($this->item('enable_query_strings') == FALSE)
  202. {
  203. if (is_array($uri))
  204. {
  205. $uri = implode('/', $uri);
  206. }
  207. $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page');
  208. $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
  209. return $this->slash_item('base_url').$index.trim($uri, '/').$suffix;
  210. }
  211. else
  212. {
  213. if (is_array($uri))
  214. {
  215. $i = 0;
  216. $str = '';
  217. foreach ($uri as $key => $val)
  218. {
  219. $prefix = ($i == 0) ? '' : '&';
  220. $str .= $prefix.$key.'='.$val;
  221. $i++;
  222. }
  223. $uri = $str;
  224. }
  225. return $this->slash_item('base_url').$this->item('index_page').'?'.$uri;
  226. }
  227. }
  228. // --------------------------------------------------------------------
  229. /**
  230. * System URL
  231. *
  232. * @access public
  233. * @return string
  234. */
  235. function system_url()
  236. {
  237. $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
  238. return $this->slash_item('base_url').end($x).'/';
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * Set a config file item
  243. *
  244. * @access public
  245. * @param string the config item key
  246. * @param string the config item value
  247. * @return void
  248. */
  249. function set_item($item, $value)
  250. {
  251. $this->config[$item] = $value;
  252. }
  253. // --------------------------------------------------------------------
  254. /**
  255. * Assign to Config
  256. *
  257. * This function is called by the front controller (CodeIgniter.php)
  258. * after the Config class is instantiated. It permits config items
  259. * to be assigned or overriden by variables contained in the index.php file
  260. *
  261. * @access private
  262. * @param array
  263. * @return void
  264. */
  265. function _assign_to_config($items = array())
  266. {
  267. if (is_array($items))
  268. {
  269. foreach ($items as $key => $val)
  270. {
  271. $this->set_item($key, $val);
  272. }
  273. }
  274. }
  275. }
  276. // END CI_Config class
  277. /* End of file Config.php */
  278. /* Location: ./system/core/Config.php */