PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Config.php

https://gitlab.com/dksaputra/scarf
PHP | 274 lines | 123 code | 40 blank | 111 comment | 35 complexity | acd39305ab7fc96104ac1794bbb379ab MD5 | raw file
  1. <?php
  2. if (!defined('BASEPATH'))
  3. exit('No direct script access allowed');
  4. /**
  5. * CodeIgniter
  6. *
  7. * An open source application development framework for PHP 5.1.6 or newer
  8. *
  9. * @package CodeIgniter
  10. * @author ExpressionEngine Dev Team
  11. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  12. * @license http://codeigniter.com/user_guide/license.html
  13. * @link http://codeigniter.com
  14. * @since Version 1.0
  15. * @filesource
  16. */
  17. // ------------------------------------------------------------------------
  18. /**
  19. * CodeIgniter Config Class
  20. *
  21. * This class contains functions that enable config files to be managed
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Libraries
  25. * @category Libraries
  26. * @author ExpressionEngine Dev Team
  27. * @link http://codeigniter.com/user_guide/libraries/config.html
  28. */
  29. class CI_Config {
  30. var $config = array();
  31. var $is_loaded = array();
  32. var $_config_paths = array(APPPATH);
  33. /**
  34. * Constructor
  35. *
  36. * Sets the $config data from the primary config.php file as a class variable
  37. *
  38. * @access public
  39. * @param string the config file name
  40. * @param boolean if configuration values should be loaded into their own section
  41. * @param boolean true if errors should just return false, false if an error message should be displayed
  42. * @return boolean if the file was successfully loaded or not
  43. */
  44. function __construct() {
  45. $this->config = & get_config();
  46. log_message('debug', "Config Class Initialized");
  47. // Set the base_url automatically if none was provided
  48. if ($this->config['base_url'] == '') {
  49. if (isset($_SERVER['HTTP_HOST'])) {
  50. $base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
  51. $base_url .= '://' . $_SERVER['HTTP_HOST'];
  52. $base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
  53. } else {
  54. $base_url = 'http://localhost/';
  55. }
  56. $this->set_item('base_url', $base_url);
  57. }
  58. }
  59. // --------------------------------------------------------------------
  60. /**
  61. * Load Config File
  62. *
  63. * @access public
  64. * @param string the config file name
  65. * @return boolean if the file was loaded correctly
  66. */
  67. function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) {
  68. $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
  69. $loaded = FALSE;
  70. foreach ($this->_config_paths as $path) {
  71. $file_path = $path . 'config/' . $file . EXT;
  72. if (in_array($file_path, $this->is_loaded, TRUE)) {
  73. $loaded = TRUE;
  74. continue;
  75. }
  76. if (!file_exists($path . 'config/' . $file . EXT)) {
  77. continue;
  78. }
  79. include($file_path);
  80. if (!isset($config) OR !is_array($config)) {
  81. if ($fail_gracefully === TRUE) {
  82. return FALSE;
  83. }
  84. show_error('Your ' . $file_path . ' file does not appear to contain a valid configuration array.');
  85. }
  86. if ($use_sections === TRUE) {
  87. if (isset($this->config[$file])) {
  88. $this->config[$file] = array_merge($this->config[$file], $config);
  89. } else {
  90. $this->config[$file] = $config;
  91. }
  92. } else {
  93. $this->config = array_merge($this->config, $config);
  94. }
  95. $this->is_loaded[] = $file_path;
  96. unset($config);
  97. $loaded = TRUE;
  98. log_message('debug', 'Config file loaded: ' . $file_path);
  99. }
  100. if ($loaded === FALSE) {
  101. if ($fail_gracefully === TRUE) {
  102. return FALSE;
  103. }
  104. show_error('The configuration file ' . $file . EXT . ' does not exist.');
  105. }
  106. return TRUE;
  107. }
  108. // --------------------------------------------------------------------
  109. /**
  110. * Fetch a config file item
  111. *
  112. *
  113. * @access public
  114. * @param string the config item name
  115. * @param string the index name
  116. * @param bool
  117. * @return string
  118. */
  119. function item($item, $index = '') {
  120. if ($index == '') {
  121. if (!isset($this->config[$item])) {
  122. return FALSE;
  123. }
  124. $pref = $this->config[$item];
  125. } else {
  126. if (!isset($this->config[$index])) {
  127. return FALSE;
  128. }
  129. if (!isset($this->config[$index][$item])) {
  130. return FALSE;
  131. }
  132. $pref = $this->config[$index][$item];
  133. }
  134. return $pref;
  135. }
  136. // --------------------------------------------------------------------
  137. /**
  138. * Fetch a config file item - adds slash after item
  139. *
  140. * The second parameter allows a slash to be added to the end of
  141. * the item, in the case of a path.
  142. *
  143. * @access public
  144. * @param string the config item name
  145. * @param bool
  146. * @return string
  147. */
  148. function slash_item($item) {
  149. if (!isset($this->config[$item])) {
  150. return FALSE;
  151. }
  152. return rtrim($this->config[$item], '/') . '/';
  153. }
  154. // --------------------------------------------------------------------
  155. /**
  156. * Site URL
  157. *
  158. * @access public
  159. * @param string the URI string
  160. * @return string
  161. */
  162. function site_url($uri = '') {
  163. if ($uri == '') {
  164. return $this->slash_item('base_url') . $this->item('index_page');
  165. }
  166. if ($this->item('enable_query_strings') == FALSE) {
  167. if (is_array($uri)) {
  168. $uri = implode('/', $uri);
  169. }
  170. $index = $this->item('index_page') == '' ? '' : $this->slash_item('index_page');
  171. $suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
  172. return $this->slash_item('base_url') . $index . trim($uri, '/') . $suffix;
  173. } else {
  174. if (is_array($uri)) {
  175. $i = 0;
  176. $str = '';
  177. foreach ($uri as $key => $val) {
  178. $prefix = ($i == 0) ? '' : '&';
  179. $str .= $prefix . $key . '=' . $val;
  180. $i++;
  181. }
  182. $uri = $str;
  183. }
  184. return $this->slash_item('base_url') . $this->item('index_page') . '?' . $uri;
  185. }
  186. }
  187. // --------------------------------------------------------------------
  188. /**
  189. * System URL
  190. *
  191. * @access public
  192. * @return string
  193. */
  194. function system_url() {
  195. $x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
  196. return $this->slash_item('base_url') . end($x) . '/';
  197. }
  198. // --------------------------------------------------------------------
  199. /**
  200. * Set a config file item
  201. *
  202. * @access public
  203. * @param string the config item key
  204. * @param string the config item value
  205. * @return void
  206. */
  207. function set_item($item, $value) {
  208. $this->config[$item] = $value;
  209. }
  210. // --------------------------------------------------------------------
  211. /**
  212. * Assign to Config
  213. *
  214. * This function is called by the front controller (CodeIgniter.php)
  215. * after the Config class is instantiated. It permits config items
  216. * to be assigned or overriden by variables contained in the index.php file
  217. *
  218. * @access private
  219. * @param array
  220. * @return void
  221. */
  222. function _assign_to_config($items = array()) {
  223. if (is_array($items)) {
  224. foreach ($items as $key => $val) {
  225. $this->set_item($key, $val);
  226. }
  227. }
  228. }
  229. }
  230. // END CI_Config class
  231. /* End of file Config.php */
  232. /* Location: ./system/core/Config.php */