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 | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2015, 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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2015, 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 |
| 34 | * @link http://codeigniter.com |
| 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 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | * @link http://codeigniter.com/user_guide/libraries/config.html |
| 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 | 815ac8a | 2014-10-28 21:32:20 +0200 | [diff] [blame] | 91 | // The regular expression is only a basic validation for a valid "Host" header. |
| 92 | // It's not exhaustive, only checks for valid characters. |
| 93 | if (isset($_SERVER['HTTP_HOST']) && preg_match('/^((\[[0-9a-f:]+\])|(\d{1,3}(\.\d{1,3}){3})|[a-z0-9\-\.]+)(:\d+)?$/i', $_SERVER['HTTP_HOST'])) |
vlakoff | 7ead65b | 2014-04-01 02:09:25 +0200 | [diff] [blame] | 94 | { |
Andrey Andreev | f35ae5e | 2014-10-10 15:40:56 +0300 | [diff] [blame] | 95 | $base_url = (is_https() ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'] |
| 96 | .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME']))); |
vlakoff | 7ead65b | 2014-04-01 02:09:25 +0200 | [diff] [blame] | 97 | } |
| 98 | else |
| 99 | { |
| 100 | $base_url = 'http://localhost/'; |
| 101 | } |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 102 | |
| 103 | $this->set_item('base_url', $base_url); |
| 104 | } |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 105 | |
| 106 | log_message('info', 'Config Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 108 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | // -------------------------------------------------------------------- |
| 110 | |
| 111 | /** |
| 112 | * Load Config File |
| 113 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 114 | * @param string $file Configuration file name |
| 115 | * @param bool $use_sections Whether configuration values should be loaded into their own section |
| 116 | * @param bool $fail_gracefully Whether to just return FALSE or display an error message |
| 117 | * @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] | 118 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 119 | public function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 121 | $file = ($file === '') ? 'config' : str_replace('.php', '', $file); |
Fu Xu | b335519 | 2014-06-12 16:45:00 +0800 | [diff] [blame] | 122 | $loaded = FALSE; |
Andrey Andreev | 9438e26 | 2012-10-05 13:16:27 +0300 | [diff] [blame] | 123 | |
Pascal Kriete | 5d5895f | 2011-02-14 13:27:07 -0500 | [diff] [blame] | 124 | foreach ($this->_config_paths as $path) |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 125 | { |
Andrey Andreev | 7f310d6 | 2015-03-15 19:03:43 +0200 | [diff] [blame] | 126 | foreach (array($file, ENVIRONMENT.DIRECTORY_SEPARATOR.$file) as $location) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 127 | { |
Andrey Andreev | d52b242 | 2012-01-07 21:28:32 +0200 | [diff] [blame] | 128 | $file_path = $path.'config/'.$location.'.php'; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 129 | if (in_array($file_path, $this->is_loaded, TRUE)) |
| 130 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 131 | return TRUE; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 134 | if ( ! file_exists($file_path)) |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 135 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 136 | continue; |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 137 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 138 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 139 | include($file_path); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 141 | if ( ! isset($config) OR ! is_array($config)) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 142 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 143 | if ($fail_gracefully === TRUE) |
| 144 | { |
| 145 | return FALSE; |
| 146 | } |
| 147 | |
| 148 | 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] | 149 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 151 | if ($use_sections === TRUE) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 152 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 153 | $this->config[$file] = isset($this->config[$file]) |
| 154 | ? array_merge($this->config[$file], $config) |
| 155 | : $config; |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 159 | $this->config = array_merge($this->config, $config); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 160 | } |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 161 | |
| 162 | $this->is_loaded[] = $file_path; |
| 163 | $config = NULL; |
| 164 | $loaded = TRUE; |
| 165 | log_message('debug', 'Config file loaded: '.$file_path); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 166 | } |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 167 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Andrey Andreev | 137aa20 | 2015-03-05 11:36:25 +0200 | [diff] [blame] | 169 | if ($loaded === TRUE) |
| 170 | { |
| 171 | return TRUE; |
| 172 | } |
| 173 | elseif ($fail_gracefully === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | { |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 175 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | } |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 177 | |
Andrey Andreev | 42bc6d5 | 2014-12-16 16:16:45 +0200 | [diff] [blame] | 178 | show_error('The configuration file '.$file.'.php does not exist.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | // -------------------------------------------------------------------- |
| 182 | |
| 183 | /** |
| 184 | * Fetch a config file item |
| 185 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 186 | * @param string $item Config item name |
| 187 | * @param string $index Index name |
vlakoff | 90f316a | 2013-07-25 04:33:56 +0200 | [diff] [blame] | 188 | * @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] | 189 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 190 | public function item($item, $index = '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | { |
Andrey Andreev | 9ba661b | 2012-06-04 14:44:34 +0300 | [diff] [blame] | 192 | if ($index == '') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | { |
vlakoff | 184cf1b | 2013-07-24 03:43:39 +0200 | [diff] [blame] | 194 | return isset($this->config[$item]) ? $this->config[$item] : NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | } |
| 196 | |
vlakoff | 184cf1b | 2013-07-24 03:43:39 +0200 | [diff] [blame] | 197 | 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] | 198 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 199 | |
| 200 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | |
| 202 | /** |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 203 | * Fetch a config file item with slash appended (if not empty) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 205 | * @param string $item Config item name |
vlakoff | c1044cb | 2013-07-25 12:18:43 +0200 | [diff] [blame] | 206 | * @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] | 207 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 208 | public function slash_item($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
| 210 | if ( ! isset($this->config[$item])) |
| 211 | { |
vlakoff | c1044cb | 2013-07-25 12:18:43 +0200 | [diff] [blame] | 212 | return NULL; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | } |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 214 | elseif (trim($this->config[$item]) === '') |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 215 | { |
| 216 | return ''; |
| 217 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | |
Phil Sturgeon | 4df8b22 | 2010-12-15 14:23:14 +0000 | [diff] [blame] | 219 | return rtrim($this->config[$item], '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | // -------------------------------------------------------------------- |
| 223 | |
| 224 | /** |
| 225 | * Site URL |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 226 | * |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 227 | * Returns base_url . index_page [. uri_string] |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 229 | * @uses CI_Config::_uri_string() |
| 230 | * |
| 231 | * @param string|string[] $uri URI string or an array of segments |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 232 | * @param string $protocol |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | * @return string |
| 234 | */ |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 235 | public function site_url($uri = '', $protocol = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 237 | $base_url = $this->slash_item('base_url'); |
| 238 | |
| 239 | if (isset($protocol)) |
| 240 | { |
Andrey Andreev | 392f8da | 2015-09-14 14:52:48 +0300 | [diff] [blame^] | 241 | // For protocol-relative links |
| 242 | if ($protocol === '') |
| 243 | { |
| 244 | $base_url = substr($base_url, strpos($base_url, '//')); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | $base_url = $protocol.substr($base_url, strpos($base_url, '://')); |
| 249 | } |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Andrey Andreev | 1764dd7 | 2012-06-16 18:48:19 +0300 | [diff] [blame] | 252 | if (empty($uri)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 254 | return $base_url.$this->item('index_page'); |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 255 | } |
| 256 | |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 257 | $uri = $this->_uri_string($uri); |
| 258 | |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 259 | if ($this->item('enable_query_strings') === FALSE) |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 260 | { |
vlakoff | 4d9fd19 | 2012-12-03 11:31:00 +0100 | [diff] [blame] | 261 | $suffix = isset($this->config['url_suffix']) ? $this->config['url_suffix'] : ''; |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 262 | |
vlakoff | 66cb413 | 2012-12-03 10:43:44 +0100 | [diff] [blame] | 263 | if ($suffix !== '') |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 264 | { |
vlakoff | 66cb413 | 2012-12-03 10:43:44 +0100 | [diff] [blame] | 265 | if (($offset = strpos($uri, '?')) !== FALSE) |
| 266 | { |
| 267 | $uri = substr($uri, 0, $offset).$suffix.substr($uri, $offset); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | $uri .= $suffix; |
| 272 | } |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 273 | } |
| 274 | |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 275 | return $base_url.$this->slash_item('index_page').$uri; |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 276 | } |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 277 | elseif (strpos($uri, '?') === FALSE) |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 278 | { |
Andrey Andreev | 95d78cf | 2012-06-16 19:54:33 +0300 | [diff] [blame] | 279 | $uri = '?'.$uri; |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 280 | } |
Andrey Andreev | 1764dd7 | 2012-06-16 18:48:19 +0300 | [diff] [blame] | 281 | |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 282 | return $base_url.$this->item('index_page').$uri; |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 283 | } |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 284 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 285 | // ------------------------------------------------------------- |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 286 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Base URL |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 289 | * |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 290 | * Returns base_url [. uri_string] |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 291 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 292 | * @uses CI_Config::_uri_string() |
| 293 | * |
| 294 | * @param string|string[] $uri URI string or an array of segments |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 295 | * @param string $protocol |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 296 | * @return string |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 297 | */ |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 298 | public function base_url($uri = '', $protocol = NULL) |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 299 | { |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 300 | $base_url = $this->slash_item('base_url'); |
| 301 | |
| 302 | if (isset($protocol)) |
| 303 | { |
Andrey Andreev | 392f8da | 2015-09-14 14:52:48 +0300 | [diff] [blame^] | 304 | // For protocol-relative links |
| 305 | if ($protocol === '') |
| 306 | { |
| 307 | $base_url = substr($base_url, strpos($base_url, '//')); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | $base_url = $protocol.substr($base_url, strpos($base_url, '://')); |
| 312 | } |
vlakoff | 4c07fce | 2013-10-25 01:20:32 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return $base_url.ltrim($this->_uri_string($uri), '/'); |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 316 | } |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 317 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 318 | // ------------------------------------------------------------- |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 319 | |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 320 | /** |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 321 | * Build URI string |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 322 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 323 | * @used-by CI_Config::site_url() |
| 324 | * @used-by CI_Config::base_url() |
| 325 | * |
| 326 | * @param string|string[] $uri URI string or an array of segments |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 327 | * @return string |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 328 | */ |
| 329 | protected function _uri_string($uri) |
| 330 | { |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 331 | if ($this->item('enable_query_strings') === FALSE) |
anaxamaxan@blackdog.local | d09c51a | 2011-02-02 23:00:16 -0800 | [diff] [blame] | 332 | { |
Derek Jones | 6d743e2 | 2010-03-02 13:22:03 -0600 | [diff] [blame] | 333 | if (is_array($uri)) |
| 334 | { |
| 335 | $uri = implode('/', $uri); |
| 336 | } |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 337 | return trim($uri, '/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | } |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 339 | elseif (is_array($uri)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | { |
Andrey Andreev | 1764dd7 | 2012-06-16 18:48:19 +0300 | [diff] [blame] | 341 | return http_build_query($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 | |
Greg Aker | 03abee3 | 2011-12-25 00:31:29 -0600 | [diff] [blame] | 344 | return $uri; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 346 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | // -------------------------------------------------------------------- |
David Behler | 2d2c9c6 | 2011-08-14 20:03:08 +0200 | [diff] [blame] | 348 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 349 | /** |
| 350 | * System URL |
| 351 | * |
Andrey Andreev | e4e1091 | 2014-02-08 19:58:48 +0200 | [diff] [blame] | 352 | * @deprecated 3.0.0 Encourages insecure practices |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | * @return string |
| 354 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 355 | public function system_url() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | { |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 357 | $x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | return $this->slash_item('base_url').end($x).'/'; |
| 359 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | // -------------------------------------------------------------------- |
| 362 | |
| 363 | /** |
| 364 | * Set a config file item |
| 365 | * |
Andrey Andreev | 5232ba0 | 2012-10-27 15:25:05 +0300 | [diff] [blame] | 366 | * @param string $item Config item key |
| 367 | * @param string $value Config item value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | * @return void |
| 369 | */ |
Andrey Andreev | ccabcfd | 2012-01-07 19:30:47 +0200 | [diff] [blame] | 370 | public function set_item($item, $value) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | { |
| 372 | $this->config[$item] = $value; |
| 373 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | } |