Config.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Config Class
  41. *
  42. * This class contains functions that enable config files to be managed
  43. *
  44. * @package CodeIgniter
  45. * @subpackage Libraries
  46. * @category Libraries
  47. * @author EllisLab Dev Team
  48. * @link https://codeigniter.com/user_guide/libraries/config.html
  49. */
  50. class CI_Config {
  51. /**
  52. * List of all loaded config values
  53. *
  54. * @var array
  55. */
  56. public $config = array();
  57. /**
  58. * List of all loaded config files
  59. *
  60. * @var array
  61. */
  62. public $is_loaded = array();
  63. /**
  64. * List of paths to search when trying to load a config file.
  65. *
  66. * @used-by CI_Loader
  67. * @var array
  68. */
  69. public $_config_paths = array(APPPATH);
  70. // --------------------------------------------------------------------
  71. /**
  72. * Class constructor
  73. *
  74. * Sets the $config data from the primary config.php file as a class variable.
  75. *
  76. * @return void
  77. */
  78. public function __construct()
  79. {
  80. $this->config =& get_config();
  81. // Set the base_url automatically if none was provided
  82. if (empty($this->config['base_url']))
  83. {
  84. if (isset($_SERVER['SERVER_ADDR']))
  85. {
  86. if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE)
  87. {
  88. $server_addr = '['.$_SERVER['SERVER_ADDR'].']';
  89. }
  90. else
  91. {
  92. $server_addr = $_SERVER['SERVER_ADDR'];
  93. }
  94. $base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
  95. .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
  96. }
  97. else
  98. {
  99. $base_url = 'http://localhost/';
  100. }
  101. $this->set_item('base_url', $base_url);
  102. }
  103. log_message('info', 'Config Class Initialized');
  104. }
  105. // --------------------------------------------------------------------
  106. /**
  107. * Load Config File
  108. *
  109. * @param string $file Configuration file name
  110. * @param bool $use_sections Whether configuration values should be loaded into their own section
  111. * @param bool $fail_gracefully Whether to just return FALSE or display an error message
  112. * @return bool TRUE if the file was loaded correctly or FALSE on failure
  113. */
  114. public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  115. {
  116. $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
  117. $loaded = FALSE;
  118. foreach ($this->_config_paths as $path)
  119. {
  120. foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location)
  121. {
  122. $file_path = $path.'config/'.$location.'.php';
  123. if (in_array($file_path, $this->is_loaded, TRUE))
  124. {
  125. return TRUE;
  126. }
  127. if ( ! file_exists($file_path))
  128. {
  129. continue;
  130. }
  131. include($file_path);
  132. if ( ! isset($config) OR ! is_array($config))
  133. {
  134. if ($fail_gracefully === TRUE)
  135. {
  136. return FALSE;
  137. }
  138. show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
  139. }
  140. if ($use_sections === TRUE)
  141. {
  142. $this->config[$file] = isset($this->config[$file])
  143. ? array_merge($this->config[$file], $config)
  144. : $config;
  145. }
  146. else
  147. {
  148. $this->config = array_merge($this->config, $config);
  149. }
  150. $this->is_loaded[] = $file_path;
  151. $config = NULL;
  152. $loaded = TRUE;
  153. log_message('debug', 'Config file loaded: '.$file_path);
  154. }
  155. }
  156. if ($loaded === TRUE)
  157. {
  158. return TRUE;
  159. }
  160. elseif ($fail_gracefully === TRUE)
  161. {
  162. return FALSE;
  163. }
  164. show_error('The configuration file '.$file.'.php does not exist.');
  165. }
  166. // --------------------------------------------------------------------
  167. /**
  168. * Fetch a config file item
  169. *
  170. * @param string $item Config item name
  171. * @param string $index Index name
  172. * @return string|null The configuration item or NULL if the item doesn't exist
  173. */
  174. public function item($item, $index = '')
  175. {
  176. if ($index == '')
  177. {
  178. return isset($this->config[$item]) ? $this->config[$item] : NULL;
  179. }
  180. return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
  181. }
  182. // --------------------------------------------------------------------
  183. /**
  184. * Fetch a config file item with slash appended (if not empty)
  185. *
  186. * @param string $item Config item name
  187. * @return string|null The configuration item or NULL if the item doesn't exist
  188. */
  189. public function slash_item($item)
  190. {
  191. if ( ! isset($this->config[$item]))
  192. {
  193. return NULL;
  194. }
  195. elseif (trim($this->config[$item]) === '')
  196. {
  197. return '';
  198. }
  199. return rtrim($this->config[$item], '/').'/';
  200. }
  201. // --------------------------------------------------------------------
  202. /**
  203. * Site URL
  204. *
  205. * Returns base_url . index_page [. uri_string]
  206. *
  207. * @uses CI_Config::_uri_string()
  208. *
  209. * @param string|string[] $uri URI string or an array of segments
  210. * @param string $protocol
  211. * @return string
  212. */
  213. public function site_url($uri = '', $protocol = NULL)
  214. {
  215. $base_url = $this->slash_item('base_url');
  216. if (isset($protocol))
  217. {
  218. // For protocol-relative links
  219. if ($protocol === '')
  220. {
  221. $base_url = substr($base_url, strpos($base_url, '//'));
  222. }
  223. else
  224. {
  225. $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
  226. }
  227. }
  228. if (empty($uri))
  229. {
  230. return $base_url.$this->item('index_page');
  231. }
  232. $uri = $this->_uri_string($uri);
  233. if ($this->item('enable_query_strings') === FALSE)
  234. {
  235. $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
  236. if ($suffix !== '')
  237. {
  238. if (($offset = strpos($uri, '?')) !== FALSE)
  239. {
  240. $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
  241. }
  242. else
  243. {
  244. $uri .= $suffix;
  245. }
  246. }
  247. return $base_url.$this->slash_item('index_page').$uri;
  248. }
  249. elseif (strpos($uri, '?') === FALSE)
  250. {
  251. $uri = '?'.$uri;
  252. }
  253. return $base_url.$this->item('index_page').$uri;
  254. }
  255. // -------------------------------------------------------------
  256. /**
  257. * Base URL
  258. *
  259. * Returns base_url [. uri_string]
  260. *
  261. * @uses CI_Config::_uri_string()
  262. *
  263. * @param string|string[] $uri URI string or an array of segments
  264. * @param string $protocol
  265. * @return string
  266. */
  267. public function base_url($uri = '', $protocol = NULL)
  268. {
  269. $base_url = $this->slash_item('base_url');
  270. if (isset($protocol))
  271. {
  272. // For protocol-relative links
  273. if ($protocol === '')
  274. {
  275. $base_url = substr($base_url, strpos($base_url, '//'));
  276. }
  277. else
  278. {
  279. $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
  280. }
  281. }
  282. return $base_url.$this->_uri_string($uri);
  283. }
  284. // -------------------------------------------------------------
  285. /**
  286. * Build URI string
  287. *
  288. * @used-by CI_Config::site_url()
  289. * @used-by CI_Config::base_url()
  290. *
  291. * @param string|string[] $uri URI string or an array of segments
  292. * @return string
  293. */
  294. protected function _uri_string($uri)
  295. {
  296. if ($this->item('enable_query_strings') === FALSE)
  297. {
  298. is_array($uri) && $uri = implode('/', $uri);
  299. return ltrim($uri, '/');
  300. }
  301. elseif (is_array($uri))
  302. {
  303. return http_build_query($uri);
  304. }
  305. return $uri;
  306. }
  307. // --------------------------------------------------------------------
  308. /**
  309. * System URL
  310. *
  311. * @deprecated 3.0.0 Encourages insecure practices
  312. * @return string
  313. */
  314. public function system_url()
  315. {
  316. $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
  317. return $this->slash_item('base_url').end($x).'/';
  318. }
  319. // --------------------------------------------------------------------
  320. /**
  321. * Set a config file item
  322. *
  323. * @param string $item Config item key
  324. * @param string $value Config item value
  325. * @return void
  326. */
  327. public function set_item($item, $value)
  328. {
  329. $this->config[$item] = $value;
  330. }
  331. }