Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Derek Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html |
| 11 | * @link http://codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Initialize the database |
| 20 | * |
| 21 | * @category Database |
| 22 | * @author ExpressionEngine Dev Team |
| 23 | * @link http://codeigniter.com/user_guide/database/ |
| 24 | */ |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 25 | function &DB($params = '', $active_record_override = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 26 | { |
| 27 | // Load the DB config file if a DSN string wasn't passed |
| 28 | if (is_string($params) AND strpos($params, '://') === FALSE) |
| 29 | { |
joelcox | e3da428 | 2011-01-16 15:00:04 +0100 | [diff] [blame^] | 30 | |
| 31 | $file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT; |
| 32 | |
| 33 | if ( ! file_exists($file_path)) |
| 34 | { |
| 35 | log_message('debug', 'Database config for '.ENVIRONMENT.' environment is not found. Trying global config.'); |
| 36 | $file_path = APPPATH.'config/database'.EXT; |
| 37 | |
| 38 | if ( ! file_exists($file_path)) |
| 39 | { |
| 40 | continue; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | include($file_path); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 45 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | if ( ! isset($db) OR count($db) == 0) |
| 47 | { |
| 48 | show_error('No database connection settings were found in the database config file.'); |
| 49 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | if ($params != '') |
| 52 | { |
| 53 | $active_group = $params; |
| 54 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 55 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | if ( ! isset($active_group) OR ! isset($db[$active_group])) |
| 57 | { |
| 58 | show_error('You have specified an invalid database connection group.'); |
| 59 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | $params = $db[$active_group]; |
| 62 | } |
| 63 | elseif (is_string($params)) |
| 64 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | /* parse the URL from the DSN string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | * Database settings can be passed as discreet |
| 68 | * parameters or as a data source name in the first |
| 69 | * parameter. DSNs must have this prototype: |
| 70 | * $dsn = 'driver://username:password@hostname/database'; |
| 71 | */ |
| 72 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | if (($dns = @parse_url($params)) === FALSE) |
| 74 | { |
| 75 | show_error('Invalid DB Connection String'); |
| 76 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 77 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | $params = array( |
| 79 | 'dbdriver' => $dns['scheme'], |
| 80 | 'hostname' => (isset($dns['host'])) ? rawurldecode($dns['host']) : '', |
| 81 | 'username' => (isset($dns['user'])) ? rawurldecode($dns['user']) : '', |
| 82 | 'password' => (isset($dns['pass'])) ? rawurldecode($dns['pass']) : '', |
| 83 | 'database' => (isset($dns['path'])) ? rawurldecode(substr($dns['path'], 1)) : '' |
| 84 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 85 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | // were additional config items set? |
| 87 | if (isset($dns['query'])) |
| 88 | { |
| 89 | parse_str($dns['query'], $extra); |
| 90 | |
| 91 | foreach($extra as $key => $val) |
| 92 | { |
| 93 | // booleans please |
| 94 | if (strtoupper($val) == "TRUE") |
| 95 | { |
| 96 | $val = TRUE; |
| 97 | } |
| 98 | elseif (strtoupper($val) == "FALSE") |
| 99 | { |
| 100 | $val = FALSE; |
| 101 | } |
| 102 | |
| 103 | $params[$key] = $val; |
| 104 | } |
| 105 | } |
| 106 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | // No DB specified yet? Beat them senseless... |
| 109 | if ( ! isset($params['dbdriver']) OR $params['dbdriver'] == '') |
| 110 | { |
| 111 | show_error('You have not selected a database type to connect to.'); |
| 112 | } |
| 113 | |
| 114 | // Load the DB classes. Note: Since the active record class is optional |
| 115 | // we need to dynamically create a class that extends proper parent class |
| 116 | // based on whether we're using the active record class or not. |
| 117 | // Kudos to Paul for discovering this clever use of eval() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 118 | |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 119 | if ($active_record_override !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | { |
Derek Jones | ffca6c2 | 2009-12-05 15:31:44 +0000 | [diff] [blame] | 121 | $active_record = $active_record_override; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | require_once(BASEPATH.'database/DB_driver'.EXT); |
| 125 | |
| 126 | if ( ! isset($active_record) OR $active_record == TRUE) |
| 127 | { |
| 128 | require_once(BASEPATH.'database/DB_active_rec'.EXT); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | if ( ! class_exists('CI_DB')) |
| 131 | { |
| 132 | eval('class CI_DB extends CI_DB_active_record { }'); |
| 133 | } |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | if ( ! class_exists('CI_DB')) |
| 138 | { |
| 139 | eval('class CI_DB extends CI_DB_driver { }'); |
| 140 | } |
| 141 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver'.EXT); |
| 144 | |
| 145 | // Instantiate the DB adapter |
| 146 | $driver = 'CI_DB_'.$params['dbdriver'].'_driver'; |
Pascal Kriete | 5856002 | 2010-11-10 16:01:20 -0500 | [diff] [blame] | 147 | $DB = new $driver($params); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | if ($DB->autoinit == TRUE) |
| 150 | { |
| 151 | $DB->initialize(); |
| 152 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Jones | c34e578 | 2010-03-02 17:35:55 -0600 | [diff] [blame] | 154 | if (isset($params['stricton']) && $params['stricton'] == TRUE) |
| 155 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | $DB->query('SET SESSION sql_mode="STRICT_ALL_TABLES"'); |
Derek Jones | c34e578 | 2010-03-02 17:35:55 -0600 | [diff] [blame] | 157 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | return $DB; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | |
| 162 | |
| 163 | |
| 164 | /* End of file DB.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 165 | /* Location: ./system/database/DB.php */ |