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 | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 8 | * |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2019, British Columbia Institute of Technology |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +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/) |
Instructor, BCIT | 0e59db6 | 2019-01-01 08:34:36 -0800 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/) |
| 33 | * @license https://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 | /** |
| 41 | * Initialize the database |
| 42 | * |
| 43 | * @category Database |
Andrey Andreev | 7554611 | 2012-07-05 11:01:29 +0300 | [diff] [blame] | 44 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 45 | * @link https://codeigniter.com/user_guide/database/ |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 46 | * |
| 47 | * @param string|string[] $params |
| 48 | * @param bool $query_builder_override |
| 49 | * Determines if query builder should be used or not |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 51 | function &DB($params = '', $query_builder_override = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | { |
| 53 | // Load the DB config file if a DSN string wasn't passed |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 54 | if (is_string($params) && strpos($params, '://') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | { |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 56 | // Is the config file in the environment folder? |
Andrey Andreev | db529ca | 2013-01-28 11:00:02 +0200 | [diff] [blame] | 57 | if ( ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php') |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 58 | && ! file_exists($file_path = APPPATH.'config/database.php')) |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 59 | { |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 60 | show_error('The configuration file database.php does not exist.'); |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 61 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 62 | |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame] | 63 | include($file_path); |
Andrey Andreev | 74d0e23 | 2015-02-05 13:36:00 +0200 | [diff] [blame] | 64 | |
| 65 | // Make packages contain database config files, |
| 66 | // given that the controller instance already exists |
| 67 | if (class_exists('CI_Controller', FALSE)) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 68 | { |
Andrey Andreev | 74d0e23 | 2015-02-05 13:36:00 +0200 | [diff] [blame] | 69 | foreach (get_instance()->load->get_package_paths() as $path) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 70 | { |
Andrey Andreev | 74d0e23 | 2015-02-05 13:36:00 +0200 | [diff] [blame] | 71 | if ($path !== APPPATH) |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 72 | { |
Andrey Andreev | 74d0e23 | 2015-02-05 13:36:00 +0200 | [diff] [blame] | 73 | if (file_exists($file_path = $path.'config/'.ENVIRONMENT.'/database.php')) |
| 74 | { |
| 75 | include($file_path); |
| 76 | } |
| 77 | elseif (file_exists($file_path = $path.'config/database.php')) |
| 78 | { |
| 79 | include($file_path); |
| 80 | } |
Rene Brokholm | f5a8530 | 2012-07-05 09:05:32 +0200 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 84 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 85 | if ( ! isset($db) OR count($db) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | { |
| 87 | show_error('No database connection settings were found in the database config file.'); |
| 88 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 90 | if ($params !== '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | { |
| 92 | $active_group = $params; |
| 93 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Andrey Andreev | f46b16b | 2013-07-18 19:36:24 +0300 | [diff] [blame] | 95 | if ( ! isset($active_group)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | { |
Andrey Andreev | f46b16b | 2013-07-18 19:36:24 +0300 | [diff] [blame] | 97 | show_error('You have not specified a database connection group via $active_group in your config/database.php file.'); |
| 98 | } |
| 99 | elseif ( ! isset($db[$active_group])) |
| 100 | { |
| 101 | show_error('You have specified an invalid database connection group ('.$active_group.') in your config/database.php file.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | $params = $db[$active_group]; |
| 105 | } |
| 106 | elseif (is_string($params)) |
| 107 | { |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 108 | /** |
| 109 | * Parse the URL from the DSN string |
| 110 | * Database settings can be passed as discreet |
| 111 | * parameters or as a data source name in the first |
| 112 | * parameter. DSNs must have this prototype: |
| 113 | * $dsn = 'driver://username:password@hostname/database'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | */ |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 115 | if (($dsn = @parse_url($params)) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | { |
| 117 | show_error('Invalid DB Connection String'); |
| 118 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | $params = array( |
Andrey Andreev | 7e96351 | 2014-02-27 15:43:24 +0200 | [diff] [blame] | 121 | 'dbdriver' => $dsn['scheme'], |
| 122 | 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '', |
| 123 | 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '', |
| 124 | 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '', |
| 125 | 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '', |
| 126 | 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : '' |
| 127 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | |
vkeranov | d008c97 | 2012-11-12 14:29:49 +0200 | [diff] [blame] | 129 | // Were additional config items set? |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 130 | if (isset($dsn['query'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | { |
Andrey Andreev | c2697db | 2012-03-26 13:05:24 +0300 | [diff] [blame] | 132 | parse_str($dsn['query'], $extra); |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 133 | |
Pascal Kriete | c3a4a8d | 2011-02-14 13:40:08 -0500 | [diff] [blame] | 134 | foreach ($extra as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | { |
Taufan Aditya | ee2f5d0 | 2012-03-30 06:29:11 +0700 | [diff] [blame] | 136 | if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | { |
João | be67685 | 2015-01-14 09:10:41 +0000 | [diff] [blame] | 138 | $val = var_export($val, TRUE); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | $params[$key] = $val; |
| 142 | } |
| 143 | } |
| 144 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 146 | // No DB specified yet? Beat them senseless... |
Andrey Andreev | e4c3019 | 2012-06-04 15:08:24 +0300 | [diff] [blame] | 147 | if (empty($params['dbdriver'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | { |
| 149 | show_error('You have not selected a database type to connect to.'); |
| 150 | } |
| 151 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 152 | // Load the DB classes. Note: Since the query builder class is optional |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | // we need to dynamically create a class that extends proper parent class |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 154 | // based on whether we're using the query builder class or not. |
| 155 | if ($query_builder_override !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 157 | $query_builder = $query_builder_override; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | } |
Andrey Andreev | d45f950 | 2012-05-24 19:31:39 +0300 | [diff] [blame] | 159 | // Backwards compatibility work-around for keeping the |
| 160 | // $active_record config variable working. Should be |
| 161 | // removed in v3.1 |
| 162 | elseif ( ! isset($query_builder) && isset($active_record)) |
| 163 | { |
| 164 | $query_builder = $active_record; |
| 165 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 167 | require_once(BASEPATH.'database/DB_driver.php'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | |
Alex Bilbie | 48a2baf | 2012-06-02 11:09:54 +0100 | [diff] [blame] | 169 | if ( ! isset($query_builder) OR $query_builder === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | { |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 171 | require_once(BASEPATH.'database/DB_query_builder.php'); |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 172 | if ( ! class_exists('CI_DB', FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | { |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 174 | /** |
| 175 | * CI_DB |
| 176 | * |
| 177 | * Acts as an alias for both CI_DB_driver and CI_DB_query_builder. |
| 178 | * |
| 179 | * @see CI_DB_query_builder |
| 180 | * @see CI_DB_driver |
| 181 | */ |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 182 | class CI_DB extends CI_DB_query_builder { } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 185 | elseif ( ! class_exists('CI_DB', FALSE)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | { |
Andrey Andreev | ae85eb4 | 2012-11-02 01:42:31 +0200 | [diff] [blame] | 187 | /** |
| 188 | * @ignore |
| 189 | */ |
Andrey Andreev | 24276a3 | 2012-01-08 02:44:38 +0200 | [diff] [blame] | 190 | class CI_DB extends CI_DB_driver { } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 192 | |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 193 | // Load the DB driver |
| 194 | $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php'; |
| 195 | |
Andrey Andreev | 7e96351 | 2014-02-27 15:43:24 +0200 | [diff] [blame] | 196 | file_exists($driver_file) OR show_error('Invalid DB driver'); |
Taufan Aditya | a8a2e33 | 2012-03-29 03:56:46 +0700 | [diff] [blame] | 197 | require_once($driver_file); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | |
| 199 | // Instantiate the DB adapter |
| 200 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 201 | $DB = new $driver($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 202 | |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 203 | // Check for a subdriver |
Andrey Andreev | 754e221 | 2012-06-23 20:54:06 +0300 | [diff] [blame] | 204 | if ( ! empty($DB->subdriver)) |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 205 | { |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 206 | $driver_file = BASEPATH.'database/drivers/'.$DB->dbdriver.'/subdrivers/'.$DB->dbdriver.'_'.$DB->subdriver.'_driver.php'; |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 207 | |
| 208 | if (file_exists($driver_file)) |
| 209 | { |
| 210 | require_once($driver_file); |
Andrey Andreev | 4410777 | 2012-06-25 18:38:34 +0300 | [diff] [blame] | 211 | $driver = 'CI_DB_'.$DB->dbdriver.'_'.$DB->subdriver.'_driver'; |
Andrey Andreev | 3345ee5 | 2012-06-23 20:51:47 +0300 | [diff] [blame] | 212 | $DB = new $driver($params); |
| 213 | } |
| 214 | } |
| 215 | |
Andrey Andreev | 1a36756 | 2015-02-19 16:48:23 +0200 | [diff] [blame] | 216 | $DB->initialize(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | return $DB; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | } |