Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 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 |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 41 | * Config Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
| 43 | * This class contains functions that enable config files to be managed |
| 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
| 47 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/libraries/config.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_Config { |
| 52 | |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 53 | /** |
| 54 | * List of all loaded config values |
| 55 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 56 | * @var array |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 57 | */ |
Eric Roberts | aa141a5 | 2012-07-04 02:13:56 -0500 | [diff] [blame] | 58 | public $config = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 59 | |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 60 | /** |
| 61 | * List of all loaded config files |
| 62 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 63 | * @var array |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 64 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 65 | public $is_loaded = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 66 | |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 67 | /** |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 68 | * List of paths to search when trying to load a config file. |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 69 | * |
Andrey Andreev | 1887ec6 | 2012-10-27 16:22:07 +0300 | [diff] [blame] | 70 | * @used-by CI_Loader |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 71 | * @var array |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 72 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 73 | public $_config_paths = array(APPPATH); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 74 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 75 | // -------------------------------------------------------------------- |
| 76 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | /** |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 78 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 80 | * Sets the $config data from the primary config.php file as a class variable. |
| 81 | * |
| 82 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 84 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | $this->config =& get_config(); |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 87 | |
| 88 | // Set the base_url automatically if none was provided |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 89 | if (empty($this->config['base_url'])) |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 90 | { |
Andrey Andreev | 0a6b066 | 2015-10-26 15:31:38 +0200 | [diff] [blame] | 91 | if (isset($_SERVER['SERVER_ADDR'])) |
vlakoff | 7ead65b | 2014-04-01 02:09:25 +0200 | [diff] [blame] | 92 | { |
Andrey Andreev | 20edad8 | 2015-11-09 10:56:30 +0200 | [diff] [blame] | 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 |
Andrey Andreev | f35ae5e | 2014-10-10 15:40:56 +0300 | [diff] [blame] | 103 | .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))); |
vlakoff | 7ead65b | 2014-04-01 02:09:25 +0200 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
| 107 | $base_url = 'http://localhost/'; |
| 108 | } |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 109 | |
| 110 | $this->set_item('base_url', $base_url); |
| 111 | } |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 112 | |
| 113 | log_message('info', 'Config Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 115 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * Load Config File |
| 120 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 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 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 126 | public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 128 | $file = ($file === '') ? 'config' : str_replace('.php', '', $file); |
Fu Xu | b335519 | 2014-06-12 16:45:00 +0800 | [diff] [blame] | 129 | $loaded = FALSE; |
Andrey Andreev | 9438e26 | 2012-10-05 13:16:27 +0300 | [diff] [blame] | 130 | |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 131 | foreach ($this->_config_paths as $path) |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 132 | { |
Andrey Andreev | 7f310d6 | 2015-03-15 19:03:43 +0200 | [diff] [blame] | 133 | foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 134 | { |
Andrey Andreev | d52b242 | 2012-01-07 21:28:32 +0200 | [diff] [blame] | 135 | $file_path = $path.'config/'.$location.'.php'; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 136 | if (in_array($file_path, $this->is_loaded, TRUE)) |
| 137 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 138 | return TRUE; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 141 | if ( ! file_exists($file_path)) |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 142 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 143 | continue; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 144 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 145 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 146 | include($file_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 148 | if ( ! isset($config) OR ! is_array($config)) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 149 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 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.'); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 156 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 157 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 158 | if ($use_sections === TRUE) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 159 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 160 | $this->config[$file] = isset($this->config[$file]) |
| 161 | ? array_merge($this->config[$file], $config) |
| 162 | : $config; |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 163 | } |
| 164 | else |
| 165 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 166 | $this->config = array_merge($this->config, $config); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 167 | } |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 168 | |
| 169 | $this->is_loaded[] = $file_path; |
| 170 | $config = NULL; |
| 171 | $loaded = TRUE; |
| 172 | log_message('debug', 'Config file loaded: '.$file_path); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 173 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 174 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Andrey Andreev | 137aa20 | 2015-03-05 11:36:25 +0200 | [diff] [blame] | 176 | if ($loaded === TRUE) |
| 177 | { |
| 178 | return TRUE; |
| 179 | } |
| 180 | elseif ($fail_gracefully === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 182 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 184 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 185 | show_error('The configuration file '.$file.'.php does not exist.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 187 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | // -------------------------------------------------------------------- |
| 189 | |
| 190 | /** |
| 191 | * Fetch a config file item |
| 192 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 193 | * @param string $item Config item name |
| 194 | * @param string $index Index name |
vlakoff | 90f316a | 2013-07-25 04:33:56 +0200 | [diff] [blame] | 195 | * @return string|null The configuration item or NULL if the item doesn't exist |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 197 | public function item($item, $index = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | { |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 199 | if ($index == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 200 | { |
vlakoff | 184cf1b | 2013-07-24 03:43:39 +0200 | [diff] [blame] | 201 | return isset($this->config[$item]) ? $this->config[$item] : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | } |
| 203 | |
vlakoff | 184cf1b | 2013-07-24 03:43:39 +0200 | [diff] [blame] | 204 | return isset($this->config[$index], $this->config[$index][$item]) ? $this->config[$index][$item] : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
| 207 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | |
| 209 | /** |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 210 | * Fetch a config file item with slash appended (if not empty) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 212 | * @param string $item Config item name |
vlakoff | c1044cb | 2013-07-25 12:18:43 +0200 | [diff] [blame] | 213 | * @return string|null The configuration item or NULL if the item doesn't exist |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 215 | public function slash_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | { |
| 217 | if ( ! isset($this->config[$item])) |
| 218 | { |
vlakoff | c1044cb | 2013-07-25 12:18:43 +0200 | [diff] [blame] | 219 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 221 | elseif (trim($this->config[$item]) === '') |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 222 | { |
| 223 | return ''; |
| 224 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 226 | return rtrim($this->config[$item], '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | // -------------------------------------------------------------------- |
| 230 | |
| 231 | /** |
| 232 | * Site URL |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 233 | * |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 234 | * Returns base_url . index_page [. uri_string] |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 236 | * @uses CI_Config::_uri_string() |
| 237 | * |
| 238 | * @param string|string[] $uri URI string or an array of segments |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 239 | * @param string $protocol |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | * @return string |
| 241 | */ |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 242 | public function site_url($uri = '', $protocol = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 243 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 244 | $base_url = $this->slash_item('base_url'); |
| 245 | |
| 246 | if (isset($protocol)) |
| 247 | { |
Andrey Andreev | 392f8da | 2015-09-14 14:52:48 +0300 | [diff] [blame] | 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 | } |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Andrey Andreev | 1764dd7 | 2012-06-16 18:48:19 +0300 | [diff] [blame] | 259 | if (empty($uri)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 261 | return $base_url.$this->item('index_page'); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 262 | } |
| 263 | |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 264 | $uri = $this->_uri_string($uri); |
| 265 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 266 | if ($this->item('enable_query_strings') === FALSE) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 267 | { |
vlakoff | 4d9fd19 | 2012-12-03 11:31:00 +0100 | [diff] [blame] | 268 | $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : ''; |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 269 | |
vlakoff | 66cb413 | 2012-12-03 10:43:44 +0100 | [diff] [blame] | 270 | if ($suffix !== '') |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 271 | { |
vlakoff | 66cb413 | 2012-12-03 10:43:44 +0100 | [diff] [blame] | 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 | } |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 280 | } |
| 281 | |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 282 | return $base_url.$this->slash_item('index_page').$uri; |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 283 | } |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 284 | elseif (strpos($uri, '?') === FALSE) |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 285 | { |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 286 | $uri = '?'.$uri; |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 287 | } |
Andrey Andreev | 1764dd7 | 2012-06-16 18:48:19 +0300 | [diff] [blame] | 288 | |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 289 | return $base_url.$this->item('index_page').$uri; |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 290 | } |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 291 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 292 | // ------------------------------------------------------------- |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 293 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 294 | /** |
| 295 | * Base URL |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 296 | * |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 297 | * Returns base_url [. uri_string] |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 298 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 299 | * @uses CI_Config::_uri_string() |
| 300 | * |
| 301 | * @param string|string[] $uri URI string or an array of segments |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 302 | * @param string $protocol |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 303 | * @return string |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 304 | */ |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 305 | public function base_url($uri = '', $protocol = NULL) |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 306 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 307 | $base_url = $this->slash_item('base_url'); |
| 308 | |
| 309 | if (isset($protocol)) |
| 310 | { |
Andrey Andreev | 392f8da | 2015-09-14 14:52:48 +0300 | [diff] [blame] | 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 | } |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Andrey Andreev | 4ac24c2 | 2016-04-28 14:28:07 +0300 | [diff] [blame] | 322 | return $base_url.$this->_uri_string($uri); |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 323 | } |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 324 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 325 | // ------------------------------------------------------------- |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 326 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 327 | /** |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 328 | * Build URI string |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 329 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 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 |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 334 | * @return string |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 335 | */ |
| 336 | protected function _uri_string($uri) |
| 337 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 338 | if ($this->item('enable_query_strings') === FALSE) |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 339 | { |
Andrey Andreev | 4ac24c2 | 2016-04-28 14:28:07 +0300 | [diff] [blame] | 340 | is_array($uri) && $uri = implode('/', $uri); |
| 341 | return ltrim($uri, '/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | } |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 343 | elseif (is_array($uri)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | { |
Andrey Andreev | 1764dd7 | 2012-06-16 18:48:19 +0300 | [diff] [blame] | 345 | return http_build_query($uri); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | } |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 347 | |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 348 | return $uri; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | // -------------------------------------------------------------------- |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 352 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | /** |
| 354 | * System URL |
| 355 | * |
Andrey Andreev | e4e1091 | 2014-02-08 19:58:48 +0200 | [diff] [blame] | 356 | * @deprecated 3.0.0 Encourages insecure practices |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | * @return string |
| 358 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 359 | public function system_url() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | { |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 361 | $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | return $this->slash_item('base_url').end($x).'/'; |
| 363 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | // -------------------------------------------------------------------- |
| 366 | |
| 367 | /** |
| 368 | * Set a config file item |
| 369 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 370 | * @param string $item Config item key |
| 371 | * @param string $value Config item value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | * @return void |
| 373 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 374 | public function set_item($item, $value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
| 376 | $this->config[$item] = $value; |
| 377 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | } |