Config.php 8.1 KB

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