Config.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 - 2016, 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 - 2016, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://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_NAME']))
  85. {
  86. if (strpos($_SERVER['SERVER_NAME'], ':') !== FALSE)
  87. {
  88. $server_addr = '['.$_SERVER['SERVER_NAME'].']';
  89. }
  90. else
  91. {
  92. $server_addr = $_SERVER['SERVER_NAME'];
  93. }
  94. $port = $_SERVER['SERVER_PORT'];
  95. if($port != '80' && $port != '443'){
  96. $server_addr .= ":".$port;
  97. }
  98. $base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
  99. .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
  100. }
  101. else
  102. {
  103. $base_url = 'http://localhost/';
  104. }
  105. $this->set_item('base_url', $base_url);
  106. }
  107. log_message('info', 'Config Class Initialized');
  108. }
  109. // --------------------------------------------------------------------
  110. /**
  111. * Load Config File
  112. *
  113. * @param string $file Configuration file name
  114. * @param bool $use_sections Whether configuration values should be loaded into their own section
  115. * @param bool $fail_gracefully Whether to just return FALSE or display an error message
  116. * @return bool TRUE if the file was loaded correctly or FALSE on failure
  117. */
  118. public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  119. {
  120. $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
  121. $loaded = FALSE;
  122. foreach ($this->_config_paths as $path)
  123. {
  124. foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location)
  125. {
  126. $file_path = $path.'config/'.$location.'.php';
  127. if (in_array($file_path, $this->is_loaded, TRUE))
  128. {
  129. return TRUE;
  130. }
  131. if ( ! file_exists($file_path))
  132. {
  133. continue;
  134. }
  135. include($file_path);
  136. if ( ! isset($config) OR ! is_array($config))
  137. {
  138. if ($fail_gracefully === TRUE)
  139. {
  140. return FALSE;
  141. }
  142. show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
  143. }
  144. if ($use_sections === TRUE)
  145. {
  146. $this->config[$file] = isset($this->config[$file])
  147. ? array_merge($this->config[$file], $config)
  148. : $config;
  149. }
  150. else
  151. {
  152. $this->config = array_merge($this->config, $config);
  153. }
  154. $this->is_loaded[] = $file_path;
  155. $config = NULL;
  156. $loaded = TRUE;
  157. log_message('debug', 'Config file loaded: '.$file_path);
  158. }
  159. }
  160. if ($loaded === TRUE)
  161. {
  162. return TRUE;
  163. }
  164. elseif ($fail_gracefully === TRUE)
  165. {
  166. return FALSE;
  167. }
  168. show_error('The configuration file '.$file.'.php does not exist.');
  169. }
  170. // --------------------------------------------------------------------
  171. /**
  172. * Fetch a config file item
  173. *
  174. * @param string $item Config item name
  175. * @param string $index Index name
  176. * @return string|null The configuration item or NULL if the item doesn't exist
  177. */
  178. public function item($item, $index = '')
  179. {
  180. if ($index == '')
  181. {
  182. return isset($this->config[$item]) ? $this->config[$item] : NULL;
  183. }
  184. return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Fetch a config file item with slash appended (if not empty)
  189. *
  190. * @param string $item Config item name
  191. * @return string|null The configuration item or NULL if the item doesn't exist
  192. */
  193. public function slash_item($item)
  194. {
  195. if ( ! isset($this->config[$item]))
  196. {
  197. return NULL;
  198. }
  199. elseif (trim($this->config[$item]) === '')
  200. {
  201. return '';
  202. }
  203. return rtrim($this->config[$item], '/').'/';
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Site URL
  208. *
  209. * Returns base_url . index_page [. uri_string]
  210. *
  211. * @uses CI_Config::_uri_string()
  212. *
  213. * @param string|string[] $uri URI string or an array of segments
  214. * @param string $protocol
  215. * @return string
  216. */
  217. public function site_url($uri = '', $protocol = NULL)
  218. {
  219. $base_url = $this->slash_item('base_url');
  220. if (isset($protocol))
  221. {
  222. // For protocol-relative links
  223. if ($protocol === '')
  224. {
  225. $base_url = substr($base_url, strpos($base_url, '//'));
  226. }
  227. else
  228. {
  229. $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
  230. }
  231. }
  232. if (empty($uri))
  233. {
  234. return $base_url.$this->item('index_page');
  235. }
  236. $uri = $this->_uri_string($uri);
  237. if ($this->item('enable_query_strings') === FALSE)
  238. {
  239. $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
  240. if ($suffix !== '')
  241. {
  242. if (($offset = strpos($uri, '?')) !== FALSE)
  243. {
  244. $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
  245. }
  246. else
  247. {
  248. $uri .= $suffix;
  249. }
  250. }
  251. return $base_url.$this->slash_item('index_page').$uri;
  252. }
  253. elseif (strpos($uri, '?') === FALSE)
  254. {
  255. $uri = '?'.$uri;
  256. }
  257. return $base_url.$this->item('index_page').$uri;
  258. }
  259. // -------------------------------------------------------------
  260. /**
  261. * Base URL
  262. *
  263. * Returns base_url [. uri_string]
  264. *
  265. * @uses CI_Config::_uri_string()
  266. *
  267. * @param string|string[] $uri URI string or an array of segments
  268. * @param string $protocol
  269. * @return string
  270. */
  271. public function base_url($uri = '', $protocol = NULL)
  272. {
  273. $base_url = $this->slash_item('base_url');
  274. if (isset($protocol))
  275. {
  276. // For protocol-relative links
  277. if ($protocol === '')
  278. {
  279. $base_url = substr($base_url, strpos($base_url, '//'));
  280. }
  281. else
  282. {
  283. $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
  284. }
  285. }
  286. return $base_url.$this->_uri_string($uri);
  287. }
  288. // -------------------------------------------------------------
  289. /**
  290. * Build URI string
  291. *
  292. * @used-by CI_Config::site_url()
  293. * @used-by CI_Config::base_url()
  294. *
  295. * @param string|string[] $uri URI string or an array of segments
  296. * @return string
  297. */
  298. protected function _uri_string($uri)
  299. {
  300. if ($this->item('enable_query_strings') === FALSE)
  301. {
  302. is_array($uri) && $uri = implode('/', $uri);
  303. return ltrim($uri, '/');
  304. }
  305. elseif (is_array($uri))
  306. {
  307. return http_build_query($uri);
  308. }
  309. return $uri;
  310. }
  311. // --------------------------------------------------------------------
  312. /**
  313. * System URL
  314. *
  315. * @deprecated 3.0.0 Encourages insecure practices
  316. * @return string
  317. */
  318. public function system_url()
  319. {
  320. $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
  321. return $this->slash_item('base_url').end($x).'/';
  322. }
  323. // --------------------------------------------------------------------
  324. /**
  325. * Set a config file item
  326. *
  327. * @param string $item Config item key
  328. * @param string $value Config item value
  329. * @return void
  330. */
  331. public function set_item($item, $value)
  332. {
  333. $this->config[$item] = $value;
  334. }
  335. }