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