Config.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /**
  29. * List of all loaded config values
  30. *
  31. * @var array
  32. */
  33. var $config = array();
  34. /**
  35. * List of all loaded config files
  36. *
  37. * @var array
  38. */
  39. var $is_loaded = array();
  40. /**
  41. * List of paths to search when trying to load a config file
  42. *
  43. * @var array
  44. */
  45. var $_config_paths = array(APPPATH);
  46. /**
  47. * Constructor
  48. *
  49. * Sets the $config data from the primary config.php file as a class variable
  50. *
  51. * @access public
  52. * @param string the config file name
  53. * @param boolean if configuration values should be loaded into their own section
  54. * @param boolean true if errors should just return false, false if an error message should be displayed
  55. * @return boolean if the file was successfully loaded or not
  56. */
  57. function __construct()
  58. {
  59. $this->config =& get_config();
  60. log_message('debug', "Config Class Initialized");
  61. // Set the base_url automatically if none was provided
  62. if ($this->config['base_url'] == '')
  63. {
  64. if (isset($_SERVER['HTTP_HOST']))
  65. {
  66. $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  67. $base_url .= '://'. $_SERVER['HTTP_HOST'];
  68. $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  69. }
  70. else
  71. {
  72. $base_url = 'http://localhost/';
  73. }
  74. $this->set_item('base_url', $base_url);
  75. }
  76. }
  77. // --------------------------------------------------------------------
  78. /**
  79. * Load Config File
  80. *
  81. * @access public
  82. * @param string the config file name
  83. * @param boolean if configuration values should be loaded into their own section
  84. * @param boolean true if errors should just return false, false if an error message should be displayed
  85. * @return boolean if the file was loaded correctly
  86. */
  87. function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  88. {
  89. $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
  90. $found = FALSE;
  91. $loaded = FALSE;
  92. $check_locations = defined('ENVIRONMENT')
  93. ? array(ENVIRONMENT.'/'.$file, $file)
  94. : array($file);
  95. foreach ($this->_config_paths as $path)
  96. {
  97. foreach ($check_locations as $location)
  98. {
  99. $file_path = $path.'config/'.$location.'.php';
  100. if (in_array($file_path, $this->is_loaded, TRUE))
  101. {
  102. $loaded = TRUE;
  103. continue 2;
  104. }
  105. if (file_exists($file_path))
  106. {
  107. $found = TRUE;
  108. break;
  109. }
  110. }
  111. if ($found === FALSE)
  112. {
  113. continue;
  114. }
  115. include($file_path);
  116. if ( ! isset($config) OR ! is_array($config))
  117. {
  118. if ($fail_gracefully === TRUE)
  119. {
  120. return FALSE;
  121. }
  122. show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
  123. }
  124. if ($use_sections === TRUE)
  125. {
  126. if (isset($this->config[$file]))
  127. {
  128. $this->config[$file] = array_merge($this->config[$file], $config);
  129. }
  130. else
  131. {
  132. $this->config[$file] = $config;
  133. }
  134. }
  135. else
  136. {
  137. $this->config = array_merge($this->config, $config);
  138. }
  139. $this->is_loaded[] = $file_path;
  140. unset($config);
  141. $loaded = TRUE;
  142. log_message('debug', 'Config file loaded: '.$file_path);
  143. break;
  144. }
  145. if ($loaded === FALSE)
  146. {
  147. if ($fail_gracefully === TRUE)
  148. {
  149. return FALSE;
  150. }
  151. show_error('The configuration file '.$file.'.php does not exist.');
  152. }
  153. return TRUE;
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Fetch a config file item
  158. *
  159. *
  160. * @access public
  161. * @param string the config item name
  162. * @param string the index name
  163. * @param bool
  164. * @return string
  165. */
  166. function item($item, $index = '')
  167. {
  168. if ($index == '')
  169. {
  170. if ( ! isset($this->config[$item]))
  171. {
  172. return FALSE;
  173. }
  174. $pref = $this->config[$item];
  175. }
  176. else
  177. {
  178. if ( ! isset($this->config[$index]))
  179. {
  180. return FALSE;
  181. }
  182. if ( ! isset($this->config[$index][$item]))
  183. {
  184. return FALSE;
  185. }
  186. $pref = $this->config[$index][$item];
  187. }
  188. return $pref;
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * Fetch a config file item - adds slash after item (if item is not empty)
  193. *
  194. * @access public
  195. * @param string the config item name
  196. * @param bool
  197. * @return string
  198. */
  199. function slash_item($item)
  200. {
  201. if ( ! isset($this->config[$item]))
  202. {
  203. return FALSE;
  204. }
  205. if( trim($this->config[$item]) == '')
  206. {
  207. return '';
  208. }
  209. return rtrim($this->config[$item], '/').'/';
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Site URL
  214. * Returns base_url . index_page [. uri_string]
  215. *
  216. * @access public
  217. * @param string the URI string
  218. * @return string
  219. */
  220. function site_url($uri = '')
  221. {
  222. if ($uri == '')
  223. {
  224. return $this->slash_item('base_url').$this->item('index_page');
  225. }
  226. if ($this->item('enable_query_strings') == FALSE)
  227. {
  228. $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
  229. return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
  230. }
  231. else
  232. {
  233. return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
  234. }
  235. }
  236. // -------------------------------------------------------------
  237. /**
  238. * Base URL
  239. * Returns base_url [. uri_string]
  240. *
  241. * @access public
  242. * @param string $uri
  243. * @return string
  244. */
  245. function base_url($uri = '')
  246. {
  247. return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
  248. }
  249. // -------------------------------------------------------------
  250. /**
  251. * Build URI string for use in Config::site_url() and Config::base_url()
  252. *
  253. * @access protected
  254. * @param $uri
  255. * @return string
  256. */
  257. protected function _uri_string($uri)
  258. {
  259. if ($this->item('enable_query_strings') == FALSE)
  260. {
  261. if (is_array($uri))
  262. {
  263. $uri = implode('/', $uri);
  264. }
  265. $uri = trim($uri, '/');
  266. }
  267. else
  268. {
  269. if (is_array($uri))
  270. {
  271. $i = 0;
  272. $str = '';
  273. foreach ($uri as $key => $val)
  274. {
  275. $prefix = ($i == 0) ? '' : '&';
  276. $str .= $prefix.$key.'='.$val;
  277. $i++;
  278. }
  279. $uri = $str;
  280. }
  281. }
  282. return $uri;
  283. }
  284. // --------------------------------------------------------------------
  285. /**
  286. * System URL
  287. *
  288. * @access public
  289. * @return string
  290. */
  291. function system_url()
  292. {
  293. $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
  294. return $this->slash_item('base_url').end($x).'/';
  295. }
  296. // --------------------------------------------------------------------
  297. /**
  298. * Set a config file item
  299. *
  300. * @access public
  301. * @param string the config item key
  302. * @param string the config item value
  303. * @return void
  304. */
  305. function set_item($item, $value)
  306. {
  307. $this->config[$item] = $value;
  308. }
  309. // --------------------------------------------------------------------
  310. /**
  311. * Assign to Config
  312. *
  313. * This function is called by the front controller (CodeIgniter.php)
  314. * after the Config class is instantiated. It permits config items
  315. * to be assigned or overriden by variables contained in the index.php file
  316. *
  317. * @access private
  318. * @param array
  319. * @return void
  320. */
  321. function _assign_to_config($items = array())
  322. {
  323. if (is_array($items))
  324. {
  325. foreach ($items as $key => $val)
  326. {
  327. $this->set_item($key, $val);
  328. }
  329. }
  330. }
  331. }
  332. // END CI_Config class
  333. /* End of file Config.php */
  334. /* Location: ./system/core/Config.php */