PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Config.php

https://gitlab.com/strongpapazola/gempabmkg
PHP | 380 lines | 184 code | 48 blank | 148 comment | 22 complexity | 0ec3b69f200d437ba93149d89cba1e0d MD5 | raw file
  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) 2019 - 2022, CodeIgniter Foundation
  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. * @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/)
  34. * @license https://opensource.org/licenses/MIT MIT License
  35. * @link https://codeigniter.com
  36. * @since Version 1.0.0
  37. * @filesource
  38. */
  39. defined('BASEPATH') OR exit('No direct script access allowed');
  40. /**
  41. * Config Class
  42. *
  43. * This class contains functions that enable config files to be managed
  44. *
  45. * @package CodeIgniter
  46. * @subpackage Libraries
  47. * @category Libraries
  48. * @author EllisLab Dev Team
  49. * @link https://codeigniter.com/userguide3/libraries/config.html
  50. */
  51. class CI_Config {
  52. /**
  53. * List of all loaded config values
  54. *
  55. * @var array
  56. */
  57. public $config = array();
  58. /**
  59. * List of all loaded config files
  60. *
  61. * @var array
  62. */
  63. public $is_loaded = array();
  64. /**
  65. * List of paths to search when trying to load a config file.
  66. *
  67. * @used-by CI_Loader
  68. * @var array
  69. */
  70. public $_config_paths = array(APPPATH);
  71. // --------------------------------------------------------------------
  72. /**
  73. * Class constructor
  74. *
  75. * Sets the $config data from the primary config.php file as a class variable.
  76. *
  77. * @return void
  78. */
  79. public function __construct()
  80. {
  81. $this->config =& get_config();
  82. // Set the base_url automatically if none was provided
  83. if (empty($this->config['base_url']))
  84. {
  85. if (isset($_SERVER['SERVER_ADDR']))
  86. {
  87. if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE)
  88. {
  89. $server_addr = '['.$_SERVER['SERVER_ADDR'].']';
  90. }
  91. else
  92. {
  93. $server_addr = $_SERVER['SERVER_ADDR'];
  94. }
  95. $base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
  96. .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
  97. }
  98. else
  99. {
  100. $base_url = 'http://localhost/';
  101. }
  102. $this->set_item('base_url', $base_url);
  103. }
  104. log_message('info', 'Config Class Initialized');
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Load Config File
  109. *
  110. * @param string $file Configuration file name
  111. * @param bool $use_sections Whether configuration values should be loaded into their own section
  112. * @param bool $fail_gracefully Whether to just return FALSE or display an error message
  113. * @return bool TRUE if the file was loaded correctly or FALSE on failure
  114. */
  115. public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
  116. {
  117. $file = ($file === '') ? 'config' : str_replace('.php', '', $file);
  118. $loaded = FALSE;
  119. foreach ($this->_config_paths as $path)
  120. {
  121. foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location)
  122. {
  123. $file_path = $path.'config/'.$location.'.php';
  124. if (in_array($file_path, $this->is_loaded, TRUE))
  125. {
  126. return TRUE;
  127. }
  128. if ( ! file_exists($file_path))
  129. {
  130. continue;
  131. }
  132. include($file_path);
  133. if ( ! isset($config) OR ! is_array($config))
  134. {
  135. if ($fail_gracefully === TRUE)
  136. {
  137. return FALSE;
  138. }
  139. show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
  140. }
  141. if ($use_sections === TRUE)
  142. {
  143. $this->config[$file] = isset($this->config[$file])
  144. ? array_merge($this->config[$file], $config)
  145. : $config;
  146. }
  147. else
  148. {
  149. $this->config = array_merge($this->config, $config);
  150. }
  151. $this->is_loaded[] = $file_path;
  152. $config = NULL;
  153. $loaded = TRUE;
  154. log_message('debug', 'Config file loaded: '.$file_path);
  155. }
  156. }
  157. if ($loaded === TRUE)
  158. {
  159. return TRUE;
  160. }
  161. elseif ($fail_gracefully === TRUE)
  162. {
  163. return FALSE;
  164. }
  165. show_error('The configuration file '.$file.'.php does not exist.');
  166. }
  167. // --------------------------------------------------------------------
  168. /**
  169. * Fetch a config file item
  170. *
  171. * @param string $item Config item name
  172. * @param string $index Index name
  173. * @return string|null The configuration item or NULL if the item doesn't exist
  174. */
  175. public function item($item, $index = '')
  176. {
  177. if ($index == '')
  178. {
  179. return isset($this->config[$item]) ? $this->config[$item] : NULL;
  180. }
  181. return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * Fetch a config file item with slash appended (if not empty)
  186. *
  187. * @param string $item Config item name
  188. * @return string|null The configuration item or NULL if the item doesn't exist
  189. */
  190. public function slash_item($item)
  191. {
  192. if ( ! isset($this->config[$item]))
  193. {
  194. return NULL;
  195. }
  196. elseif (trim($this->config[$item]) === '')
  197. {
  198. return '';
  199. }
  200. return rtrim($this->config[$item], '/').'/';
  201. }
  202. // --------------------------------------------------------------------
  203. /**
  204. * Site URL
  205. *
  206. * Returns base_url . index_page [. uri_string]
  207. *
  208. * @uses CI_Config::_uri_string()
  209. *
  210. * @param string|string[] $uri URI string or an array of segments
  211. * @param string $protocol
  212. * @return string
  213. */
  214. public function site_url($uri = '', $protocol = NULL)
  215. {
  216. $base_url = $this->slash_item('base_url');
  217. if (isset($protocol))
  218. {
  219. // For protocol-relative links
  220. if ($protocol === '')
  221. {
  222. $base_url = substr($base_url, strpos($base_url, '//'));
  223. }
  224. else
  225. {
  226. $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
  227. }
  228. }
  229. if (empty($uri))
  230. {
  231. return $base_url.$this->item('index_page');
  232. }
  233. $uri = $this->_uri_string($uri);
  234. if ($this->item('enable_query_strings') === FALSE)
  235. {
  236. $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
  237. if ($suffix !== '')
  238. {
  239. if (($offset = strpos($uri, '?')) !== FALSE)
  240. {
  241. $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
  242. }
  243. else
  244. {
  245. $uri .= $suffix;
  246. }
  247. }
  248. return $base_url.$this->slash_item('index_page').$uri;
  249. }
  250. elseif (strpos($uri, '?') === FALSE)
  251. {
  252. $uri = '?'.$uri;
  253. }
  254. return $base_url.$this->item('index_page').$uri;
  255. }
  256. // -------------------------------------------------------------
  257. /**
  258. * Base URL
  259. *
  260. * Returns base_url [. uri_string]
  261. *
  262. * @uses CI_Config::_uri_string()
  263. *
  264. * @param string|string[] $uri URI string or an array of segments
  265. * @param string $protocol
  266. * @return string
  267. */
  268. public function base_url($uri = '', $protocol = NULL)
  269. {
  270. $base_url = $this->slash_item('base_url');
  271. if (isset($protocol))
  272. {
  273. // For protocol-relative links
  274. if ($protocol === '')
  275. {
  276. $base_url = substr($base_url, strpos($base_url, '//'));
  277. }
  278. else
  279. {
  280. $base_url = $protocol.substr($base_url, strpos($base_url, '://'));
  281. }
  282. }
  283. return $base_url.$this->_uri_string($uri);
  284. }
  285. // -------------------------------------------------------------
  286. /**
  287. * Build URI string
  288. *
  289. * @used-by CI_Config::site_url()
  290. * @used-by CI_Config::base_url()
  291. *
  292. * @param string|string[] $uri URI string or an array of segments
  293. * @return string
  294. */
  295. protected function _uri_string($uri)
  296. {
  297. if ($this->item('enable_query_strings') === FALSE)
  298. {
  299. is_array($uri) && $uri = implode('/', $uri);
  300. return ltrim($uri, '/');
  301. }
  302. elseif (is_array($uri))
  303. {
  304. return http_build_query($uri);
  305. }
  306. return $uri;
  307. }
  308. // --------------------------------------------------------------------
  309. /**
  310. * System URL
  311. *
  312. * @deprecated 3.0.0 Encourages insecure practices
  313. * @return string
  314. */
  315. public function system_url()
  316. {
  317. $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
  318. return $this->slash_item('base_url').end($x).'/';
  319. }
  320. // --------------------------------------------------------------------
  321. /**
  322. * Set a config file item
  323. *
  324. * @param string $item Config item key
  325. * @param string $value Config item value
  326. * @return void
  327. */
  328. public function set_item($item, $value)
  329. {
  330. $this->config[$item] = $value;
  331. }
  332. }