DB.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  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:
  17. *
  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
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Initialize the database
  41. *
  42. * @category Database
  43. * @author EllisLab Dev Team
  44. * @link https://codeigniter.com/user_guide/database/
  45. *
  46. * @param string|string[] $params
  47. * @param bool $query_builder_override
  48. * Determines if query builder should be used or not
  49. */
  50. function &DB($params = '', $query_builder_override = NULL)
  51. {
  52. // Load the DB config file if a DSN string wasn't passed
  53. if (is_string($params) && strpos($params, '://') === FALSE)
  54. {
  55. // Is the config file in the environment folder?
  56. if ( ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/database.php')
  57. && ! file_exists($file_path = APPPATH.'config/database.php'))
  58. {
  59. show_error('The configuration file database.php does not exist.');
  60. }
  61. include($file_path);
  62. // Make packages contain database config files,
  63. // given that the controller instance already exists
  64. if (class_exists('CI_Controller', FALSE))
  65. {
  66. foreach (get_instance()->load->get_package_paths() as $path)
  67. {
  68. if ($path !== APPPATH)
  69. {
  70. if (file_exists($file_path = $path.'config/'.ENVIRONMENT.'/database.php'))
  71. {
  72. include($file_path);
  73. }
  74. elseif (file_exists($file_path = $path.'config/database.php'))
  75. {
  76. include($file_path);
  77. }
  78. }
  79. }
  80. }
  81. if ( ! isset($db) OR count($db) === 0)
  82. {
  83. show_error('No database connection settings were found in the database config file.');
  84. }
  85. if ($params !== '')
  86. {
  87. $active_group = $params;
  88. }
  89. if ( ! isset($active_group))
  90. {
  91. show_error('You have not specified a database connection group via $active_group in your config/database.php file.');
  92. }
  93. elseif ( ! isset($db[$active_group]))
  94. {
  95. show_error('You have specified an invalid database connection group ('.$active_group.') in your config/database.php file.');
  96. }
  97. $params = $db[$active_group];
  98. }
  99. elseif (is_string($params))
  100. {
  101. /**
  102. * Parse the URL from the DSN string
  103. * Database settings can be passed as discreet
  104. * parameters or as a data source name in the first
  105. * parameter. DSNs must have this prototype:
  106. * $dsn = 'driver://username:password@hostname/database';
  107. */
  108. if (($dsn = @parse_url($params)) === FALSE)
  109. {
  110. show_error('Invalid DB Connection String');
  111. }
  112. $params = array(
  113. 'dbdriver' => $dsn['scheme'],
  114. 'hostname' => isset($dsn['host']) ? rawurldecode($dsn['host']) : '',
  115. 'port' => isset($dsn['port']) ? rawurldecode($dsn['port']) : '',
  116. 'username' => isset($dsn['user']) ? rawurldecode($dsn['user']) : '',
  117. 'password' => isset($dsn['pass']) ? rawurldecode($dsn['pass']) : '',
  118. 'database' => isset($dsn['path']) ? rawurldecode(substr($dsn['path'], 1)) : ''
  119. );
  120. // Were additional config items set?
  121. if (isset($dsn['query']))
  122. {
  123. parse_str($dsn['query'], $extra);
  124. foreach ($extra as $key => $val)
  125. {
  126. if (is_string($val) && in_array(strtoupper($val), array('TRUE', 'FALSE', 'NULL')))
  127. {
  128. $val = var_export($val, TRUE);
  129. }
  130. $params[$key] = $val;
  131. }
  132. }
  133. }
  134. // No DB specified yet? Beat them senseless...
  135. if (empty($params['dbdriver']))
  136. {
  137. show_error('You have not selected a database type to connect to.');
  138. }
  139. // Load the DB classes. Note: Since the query builder class is optional
  140. // we need to dynamically create a class that extends proper parent class
  141. // based on whether we're using the query builder class or not.
  142. if ($query_builder_override !== NULL)
  143. {
  144. $query_builder = $query_builder_override;
  145. }
  146. // Backwards compatibility work-around for keeping the
  147. // $active_record config variable working. Should be
  148. // removed in v3.1
  149. elseif ( ! isset($query_builder) && isset($active_record))
  150. {
  151. $query_builder = $active_record;
  152. }
  153. require_once(BASEPATH.'database/DB_driver.php');
  154. if ( ! isset($query_builder) OR $query_builder === TRUE)
  155. {
  156. require_once(BASEPATH.'database/DB_query_builder.php');
  157. if ( ! class_exists('CI_DB', FALSE))
  158. {
  159. /**
  160. * CI_DB
  161. *
  162. * Acts as an alias for both CI_DB_driver and CI_DB_query_builder.
  163. *
  164. * @see CI_DB_query_builder
  165. * @see CI_DB_driver
  166. */
  167. class CI_DB extends CI_DB_query_builder { }
  168. }
  169. }
  170. elseif ( ! class_exists('CI_DB', FALSE))
  171. {
  172. /**
  173. * @ignore
  174. */
  175. class CI_DB extends CI_DB_driver { }
  176. }
  177. // Load the DB driver
  178. $driver_file = BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php';
  179. file_exists($driver_file) OR show_error('Invalid DB driver');
  180. require_once($driver_file);
  181. // Instantiate the DB adapter
  182. $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
  183. $DB = new $driver($params);
  184. // Check for a subdriver
  185. if ( ! empty($DB->subdriver))
  186. {
  187. $driver_file = BASEPATH.'database/drivers/'.$DB->dbdriver.'/subdrivers/'.$DB->dbdriver.'_'.$DB->subdriver.'_driver.php';
  188. if (file_exists($driver_file))
  189. {
  190. require_once($driver_file);
  191. $driver = 'CI_DB_'.$DB->dbdriver.'_'.$DB->subdriver.'_driver';
  192. $DB = new $driver($params);
  193. }
  194. }
  195. $DB->initialize();
  196. return $DB;
  197. }