| 68 |
lars |
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 |
*
|
| 2257 |
lars |
9 |
* Copyright (c) 2014 - 2018, British Columbia Institute of Technology
|
| 68 |
lars |
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/)
|
| 2257 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
|
| 68 |
lars |
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 |
/**
|
|
|
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/user_guide/libraries/config.html
|
|
|
50 |
*/
|
|
|
51 |
class CI_Config {
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* List of all loaded config values
|
|
|
55 |
*
|
|
|
56 |
* @var array
|
|
|
57 |
*/
|
|
|
58 |
public $config = array();
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* List of all loaded config files
|
|
|
62 |
*
|
|
|
63 |
* @var array
|
|
|
64 |
*/
|
|
|
65 |
public $is_loaded = array();
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* List of paths to search when trying to load a config file.
|
|
|
69 |
*
|
|
|
70 |
* @used-by CI_Loader
|
|
|
71 |
* @var array
|
|
|
72 |
*/
|
|
|
73 |
public $_config_paths = array(APPPATH);
|
|
|
74 |
|
|
|
75 |
// --------------------------------------------------------------------
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Class constructor
|
|
|
79 |
*
|
|
|
80 |
* Sets the $config data from the primary config.php file as a class variable.
|
|
|
81 |
*
|
|
|
82 |
* @return void
|
|
|
83 |
*/
|
|
|
84 |
public function __construct()
|
|
|
85 |
{
|
|
|
86 |
$this->config =& get_config();
|
|
|
87 |
|
|
|
88 |
// Set the base_url automatically if none was provided
|
|
|
89 |
if (empty($this->config['base_url']))
|
|
|
90 |
{
|
|
|
91 |
if (isset($_SERVER['SERVER_ADDR']))
|
|
|
92 |
{
|
|
|
93 |
if (strpos($_SERVER['SERVER_ADDR'], ':') !== FALSE)
|
|
|
94 |
{
|
|
|
95 |
$server_addr = '['.$_SERVER['SERVER_ADDR'].']';
|
|
|
96 |
}
|
|
|
97 |
else
|
|
|
98 |
{
|
|
|
99 |
$server_addr = $_SERVER['SERVER_ADDR'];
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
$base_url = (is_https() ? 'https' : 'http').'://'.$server_addr
|
|
|
103 |
.substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
|
|
|
104 |
}
|
|
|
105 |
else
|
|
|
106 |
{
|
|
|
107 |
$base_url = 'http://localhost/';
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$this->set_item('base_url', $base_url);
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
log_message('info', 'Config Class Initialized');
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
// --------------------------------------------------------------------
|
|
|
117 |
|
|
|
118 |
/**
|
|
|
119 |
* Load Config File
|
|
|
120 |
*
|
|
|
121 |
* @param string $file Configuration file name
|
|
|
122 |
* @param bool $use_sections Whether configuration values should be loaded into their own section
|
|
|
123 |
* @param bool $fail_gracefully Whether to just return FALSE or display an error message
|
|
|
124 |
* @return bool TRUE if the file was loaded correctly or FALSE on failure
|
|
|
125 |
*/
|
|
|
126 |
public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
|
|
|
127 |
{
|
|
|
128 |
$file = ($file === '') ? 'config' : str_replace('.php', '', $file);
|
|
|
129 |
$loaded = FALSE;
|
|
|
130 |
|
|
|
131 |
foreach ($this->_config_paths as $path)
|
|
|
132 |
{
|
|
|
133 |
foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location)
|
|
|
134 |
{
|
|
|
135 |
$file_path = $path.'config/'.$location.'.php';
|
|
|
136 |
if (in_array($file_path, $this->is_loaded, TRUE))
|
|
|
137 |
{
|
|
|
138 |
return TRUE;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
if ( ! file_exists($file_path))
|
|
|
142 |
{
|
|
|
143 |
continue;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
include($file_path);
|
|
|
147 |
|
|
|
148 |
if ( ! isset($config) OR ! is_array($config))
|
|
|
149 |
{
|
|
|
150 |
if ($fail_gracefully === TRUE)
|
|
|
151 |
{
|
|
|
152 |
return FALSE;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
if ($use_sections === TRUE)
|
|
|
159 |
{
|
|
|
160 |
$this->config[$file] = isset($this->config[$file])
|
|
|
161 |
? array_merge($this->config[$file], $config)
|
|
|
162 |
: $config;
|
|
|
163 |
}
|
|
|
164 |
else
|
|
|
165 |
{
|
|
|
166 |
$this->config = array_merge($this->config, $config);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
$this->is_loaded[] = $file_path;
|
|
|
170 |
$config = NULL;
|
|
|
171 |
$loaded = TRUE;
|
|
|
172 |
log_message('debug', 'Config file loaded: '.$file_path);
|
|
|
173 |
}
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if ($loaded === TRUE)
|
|
|
177 |
{
|
|
|
178 |
return TRUE;
|
|
|
179 |
}
|
|
|
180 |
elseif ($fail_gracefully === TRUE)
|
|
|
181 |
{
|
|
|
182 |
return FALSE;
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
show_error('The configuration file '.$file.'.php does not exist.');
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// --------------------------------------------------------------------
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Fetch a config file item
|
|
|
192 |
*
|
|
|
193 |
* @param string $item Config item name
|
|
|
194 |
* @param string $index Index name
|
|
|
195 |
* @return string|null The configuration item or NULL if the item doesn't exist
|
|
|
196 |
*/
|
|
|
197 |
public function item($item, $index = '')
|
|
|
198 |
{
|
|
|
199 |
if ($index == '')
|
|
|
200 |
{
|
|
|
201 |
return isset($this->config[$item]) ? $this->config[$item] : NULL;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
// --------------------------------------------------------------------
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Fetch a config file item with slash appended (if not empty)
|
|
|
211 |
*
|
|
|
212 |
* @param string $item Config item name
|
|
|
213 |
* @return string|null The configuration item or NULL if the item doesn't exist
|
|
|
214 |
*/
|
|
|
215 |
public function slash_item($item)
|
|
|
216 |
{
|
|
|
217 |
if ( ! isset($this->config[$item]))
|
|
|
218 |
{
|
|
|
219 |
return NULL;
|
|
|
220 |
}
|
|
|
221 |
elseif (trim($this->config[$item]) === '')
|
|
|
222 |
{
|
|
|
223 |
return '';
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
return rtrim($this->config[$item], '/').'/';
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
// --------------------------------------------------------------------
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Site URL
|
|
|
233 |
*
|
|
|
234 |
* Returns base_url . index_page [. uri_string]
|
|
|
235 |
*
|
|
|
236 |
* @uses CI_Config::_uri_string()
|
|
|
237 |
*
|
|
|
238 |
* @param string|string[] $uri URI string or an array of segments
|
|
|
239 |
* @param string $protocol
|
|
|
240 |
* @return string
|
|
|
241 |
*/
|
|
|
242 |
public function site_url($uri = '', $protocol = NULL)
|
|
|
243 |
{
|
|
|
244 |
$base_url = $this->slash_item('base_url');
|
|
|
245 |
|
|
|
246 |
if (isset($protocol))
|
|
|
247 |
{
|
|
|
248 |
// For protocol-relative links
|
|
|
249 |
if ($protocol === '')
|
|
|
250 |
{
|
|
|
251 |
$base_url = substr($base_url, strpos($base_url, '//'));
|
|
|
252 |
}
|
|
|
253 |
else
|
|
|
254 |
{
|
|
|
255 |
$base_url = $protocol.substr($base_url, strpos($base_url, '://'));
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
if (empty($uri))
|
|
|
260 |
{
|
|
|
261 |
return $base_url.$this->item('index_page');
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
$uri = $this->_uri_string($uri);
|
|
|
265 |
|
|
|
266 |
if ($this->item('enable_query_strings') === FALSE)
|
|
|
267 |
{
|
|
|
268 |
$suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '';
|
|
|
269 |
|
|
|
270 |
if ($suffix !== '')
|
|
|
271 |
{
|
|
|
272 |
if (($offset = strpos($uri, '?')) !== FALSE)
|
|
|
273 |
{
|
|
|
274 |
$uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset);
|
|
|
275 |
}
|
|
|
276 |
else
|
|
|
277 |
{
|
|
|
278 |
$uri .= $suffix;
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
return $base_url.$this->slash_item('index_page').$uri;
|
|
|
283 |
}
|
|
|
284 |
elseif (strpos($uri, '?') === FALSE)
|
|
|
285 |
{
|
|
|
286 |
$uri = '?'.$uri;
|
|
|
287 |
}
|
|
|
288 |
|
|
|
289 |
return $base_url.$this->item('index_page').$uri;
|
|
|
290 |
}
|
|
|
291 |
|
|
|
292 |
// -------------------------------------------------------------
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Base URL
|
|
|
296 |
*
|
|
|
297 |
* Returns base_url [. uri_string]
|
|
|
298 |
*
|
|
|
299 |
* @uses CI_Config::_uri_string()
|
|
|
300 |
*
|
|
|
301 |
* @param string|string[] $uri URI string or an array of segments
|
|
|
302 |
* @param string $protocol
|
|
|
303 |
* @return string
|
|
|
304 |
*/
|
|
|
305 |
public function base_url($uri = '', $protocol = NULL)
|
|
|
306 |
{
|
|
|
307 |
$base_url = $this->slash_item('base_url');
|
|
|
308 |
|
|
|
309 |
if (isset($protocol))
|
|
|
310 |
{
|
|
|
311 |
// For protocol-relative links
|
|
|
312 |
if ($protocol === '')
|
|
|
313 |
{
|
|
|
314 |
$base_url = substr($base_url, strpos($base_url, '//'));
|
|
|
315 |
}
|
|
|
316 |
else
|
|
|
317 |
{
|
|
|
318 |
$base_url = $protocol.substr($base_url, strpos($base_url, '://'));
|
|
|
319 |
}
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
return $base_url.$this->_uri_string($uri);
|
|
|
323 |
}
|
|
|
324 |
|
|
|
325 |
// -------------------------------------------------------------
|
|
|
326 |
|
|
|
327 |
/**
|
|
|
328 |
* Build URI string
|
|
|
329 |
*
|
|
|
330 |
* @used-by CI_Config::site_url()
|
|
|
331 |
* @used-by CI_Config::base_url()
|
|
|
332 |
*
|
|
|
333 |
* @param string|string[] $uri URI string or an array of segments
|
|
|
334 |
* @return string
|
|
|
335 |
*/
|
|
|
336 |
protected function _uri_string($uri)
|
|
|
337 |
{
|
|
|
338 |
if ($this->item('enable_query_strings') === FALSE)
|
|
|
339 |
{
|
|
|
340 |
is_array($uri) && $uri = implode('/', $uri);
|
|
|
341 |
return ltrim($uri, '/');
|
|
|
342 |
}
|
|
|
343 |
elseif (is_array($uri))
|
|
|
344 |
{
|
|
|
345 |
return http_build_query($uri);
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
return $uri;
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
// --------------------------------------------------------------------
|
|
|
352 |
|
|
|
353 |
/**
|
|
|
354 |
* System URL
|
|
|
355 |
*
|
|
|
356 |
* @deprecated 3.0.0 Encourages insecure practices
|
|
|
357 |
* @return string
|
|
|
358 |
*/
|
|
|
359 |
public function system_url()
|
|
|
360 |
{
|
|
|
361 |
$x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
|
|
|
362 |
return $this->slash_item('base_url').end($x).'/';
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
// --------------------------------------------------------------------
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* Set a config file item
|
|
|
369 |
*
|
|
|
370 |
* @param string $item Config item key
|
|
|
371 |
* @param string $value Config item value
|
|
|
372 |
* @return void
|
|
|
373 |
*/
|
|
|
374 |
public function set_item($item, $value)
|
|
|
375 |
{
|
|
|
376 |
$this->config[$item] = $value;
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
}
|