PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_akeeba/akeeba/drivers/mysqli.php

https://github.com/CCI-Studios/Wee-Magazine
PHP | 365 lines | 184 code | 43 blank | 138 comment | 29 complexity | 859d902b834cad51abc19e90d9631acc MD5 | raw file
  1. <?php
  2. /**
  3. * Akeeba Engine
  4. * The modular PHP5 site backup engine
  5. * @copyright Copyright (c)2009-2012 Nicholas K. Dionysopoulos
  6. * @license GNU GPL version 3 or, at your option, any later version
  7. * @package akeebaengine
  8. */
  9. // Protection against direct access
  10. defined('AKEEBAENGINE') or die();
  11. /**
  12. * MySQL Improved (mysqli) database driver for Akeeba Engine
  13. *
  14. * Based on Joomla! Platform 11.2
  15. */
  16. class AEDriverMysqli extends AEDriverMysql
  17. {
  18. /**
  19. * The name of the database driver.
  20. *
  21. * @var string
  22. * @since 11.1
  23. */
  24. public $name = 'mysqli';
  25. /**
  26. * Database object constructor
  27. * @param array List of options used to configure the connection
  28. */
  29. public function __construct( $options )
  30. {
  31. $this->driverType = 'mysql';
  32. // Init
  33. $this->nameQuote = '`';
  34. $host = array_key_exists('host', $options) ? $options['host'] : 'localhost';
  35. $port = array_key_exists('port', $options) ? $options['port'] : '';
  36. $user = array_key_exists('user', $options) ? $options['user'] : '';
  37. $password = array_key_exists('password',$options) ? $options['password'] : '';
  38. $database = array_key_exists('database',$options) ? $options['database'] : '';
  39. $prefix = array_key_exists('prefix', $options) ? $options['prefix'] : '';
  40. $select = array_key_exists('select', $options) ? $options['select'] : true;
  41. // Figure out if a port is included in the host name
  42. if(empty($port))
  43. {
  44. // Unlike mysql_connect(), mysqli_connect() takes the port and socket
  45. // as separate arguments. Therefore, we have to extract them from the
  46. // host string.
  47. $port = NULL;
  48. $socket = NULL;
  49. $targetSlot = substr( strstr( $host, ":" ), 1 );
  50. if (!empty( $targetSlot )) {
  51. // Get the port number or socket name
  52. if (is_numeric( $targetSlot ))
  53. $port = $targetSlot;
  54. else
  55. $socket = $targetSlot;
  56. // Extract the host name only
  57. $host = substr( $host, 0, strlen( $host ) - (strlen( $targetSlot ) + 1) );
  58. // This will take care of the following notation: ":3306"
  59. if($host == '')
  60. $host = 'localhost';
  61. }
  62. }
  63. // finalize initialization
  64. parent::__construct($options);
  65. // Open the connection
  66. $this->host = $host;
  67. $this->user = $user;
  68. $this->password = $password;
  69. $this->port = $port;
  70. $this->socket = $socket;
  71. $this->_database = $database;
  72. $this->selectDatabase = $select;
  73. if(!is_object($this->connection)) $this->open();
  74. }
  75. public function open()
  76. {
  77. if($this->connected()) {
  78. return;
  79. } else {
  80. $this->close();
  81. }
  82. // perform a number of fatality checks, then return gracefully
  83. if (!function_exists( 'mysqli_connect' )) {
  84. $this->errorNum = 1;
  85. $this->errorMsg = 'The MySQL adapter "mysqli" is not available.';
  86. return;
  87. }
  88. // connect to the server
  89. if (!($this->connection = @mysqli_connect($this->host, $this->user, $this->password, NULL, $this->port, $this->socket))) {
  90. $this->errorNum = 2;
  91. $this->errorMsg = 'Could not connect to MySQL';
  92. return;
  93. }
  94. // Set sql_mode to non_strict mode
  95. mysqli_query($this->connection, "SET @@SESSION.sql_mode = '';");
  96. if ($this->selectDatabase && !empty($this->_database)) {
  97. $this->select($this->_database);
  98. }
  99. $this->setUTF();
  100. }
  101. public function close()
  102. {
  103. $return = false;
  104. if (is_resource($this->cursor)) {
  105. mysqli_free_result($this->cursor);
  106. }
  107. if (is_object($this->connection)) {
  108. $return = @mysqli_close($this->connection);
  109. }
  110. $this->connection = null;
  111. return $return;
  112. }
  113. /**
  114. * Method to escape a string for usage in an SQL statement.
  115. *
  116. * @param string $text The string to be escaped.
  117. * @param boolean $extra Optional parameter to provide extra escaping.
  118. *
  119. * @return string The escaped string.
  120. */
  121. public function escape($text, $extra = false)
  122. {
  123. $result = @mysqli_real_escape_string($this->getConnection(), $text);
  124. if ($extra)
  125. {
  126. $result = addcslashes($result, '%_');
  127. }
  128. return $result;
  129. }
  130. /**
  131. * Test to see if the MySQL connector is available.
  132. *
  133. * @return boolean True on success, false otherwise.
  134. */
  135. public static function test()
  136. {
  137. return (function_exists('mysqli_connect'));
  138. }
  139. /**
  140. * Determines if the connection to the server is active.
  141. *
  142. * @return boolean True if connected to the database engine.
  143. */
  144. public function connected()
  145. {
  146. if (is_object($this->connection))
  147. {
  148. return mysqli_ping($this->connection);
  149. }
  150. return false;
  151. }
  152. /**
  153. * Get the number of affected rows for the previous executed SQL statement.
  154. *
  155. * @return integer The number of affected rows.
  156. */
  157. public function getAffectedRows()
  158. {
  159. return mysqli_affected_rows($this->connection);
  160. }
  161. /**
  162. * Get the number of returned rows for the previous executed SQL statement.
  163. *
  164. * @param resource $cursor An optional database cursor resource to extract the row count from.
  165. *
  166. * @return integer The number of returned rows.
  167. */
  168. public function getNumRows($cursor = null)
  169. {
  170. return mysqli_num_rows($cursor ? $cursor : $this->cursor);
  171. }
  172. /**
  173. * Get the current or query, or new JDatabaseQuery object.
  174. *
  175. * @param boolean $new False to return the last query set, True to return a new JDatabaseQuery object.
  176. *
  177. * @return mixed The current value of the internal SQL variable or a new JDatabaseQuery object.
  178. */
  179. public function getQuery($new = false)
  180. {
  181. if ($new)
  182. {
  183. return new AEQueryMysql($this);
  184. }
  185. else
  186. {
  187. return $this->sql;
  188. }
  189. }
  190. /**
  191. * Get the version of the database connector.
  192. *
  193. * @return string The database connector version.
  194. */
  195. public function getVersion()
  196. {
  197. return mysqli_get_server_info($this->connection);
  198. }
  199. /**
  200. * Determines if the database engine supports UTF-8 character encoding.
  201. *
  202. * @return boolean True if supported.
  203. */
  204. public function hasUTF()
  205. {
  206. $verParts = explode( '.', $this->getVersion() );
  207. return ($verParts[0] == 5 || ($verParts[0] == 4 && $verParts[1] == 1 && (int)$verParts[2] >= 2));
  208. }
  209. /**
  210. * Method to get the auto-incremented value from the last INSERT statement.
  211. *
  212. * @return integer The value of the auto-increment field from the last inserted row.
  213. */
  214. public function insertid()
  215. {
  216. return mysqli_insert_id($this->connection);
  217. }
  218. /**
  219. * Execute the SQL statement.
  220. *
  221. * @return mixed A database cursor resource on success, boolean false on failure.
  222. */
  223. public function query()
  224. {
  225. if (!is_object($this->connection))
  226. {
  227. return false;
  228. }
  229. // Take a local copy so that we don't modify the original query and cause issues later
  230. $sql = $this->replacePrefix((string) $this->sql);
  231. if ($this->limit > 0 || $this->offset > 0)
  232. {
  233. $sql .= ' LIMIT ' . $this->offset . ', ' . $this->limit;
  234. }
  235. // Reset the error values.
  236. $this->errorNum = 0;
  237. $this->errorMsg = '';
  238. // Execute the query.
  239. $this->cursor = mysqli_query($this->connection, $sql);
  240. // If an error occurred handle it.
  241. if (!$this->cursor)
  242. {
  243. $this->errorNum = (int) mysqli_errno($this->connection);
  244. $this->errorMsg = (string) mysqli_error($this->connection) . ' SQL=' . $sql;
  245. return false;
  246. }
  247. return $this->cursor;
  248. }
  249. /**
  250. * Select a database for use.
  251. *
  252. * @param string $database The name of the database to select for use.
  253. *
  254. * @return boolean True if the database was successfully selected.
  255. */
  256. public function select($database)
  257. {
  258. if (!$database)
  259. {
  260. return false;
  261. }
  262. if (!mysqli_select_db($this->connection, $database))
  263. {
  264. return false;
  265. }
  266. return true;
  267. }
  268. /**
  269. * Set the connection to use UTF-8 character encoding.
  270. *
  271. * @return boolean True on success.
  272. */
  273. public function setUTF()
  274. {
  275. mysqli_query($this->connection, "SET NAMES 'utf8'");
  276. }
  277. /**
  278. * Method to fetch a row from the result set cursor as an array.
  279. *
  280. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  281. *
  282. * @return mixed Either the next row from the result set or false if there are no more rows.
  283. */
  284. protected function fetchArray($cursor = null)
  285. {
  286. return mysqli_fetch_row($cursor ? $cursor : $this->cursor);
  287. }
  288. /**
  289. * Method to fetch a row from the result set cursor as an associative array.
  290. *
  291. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  292. *
  293. * @return mixed Either the next row from the result set or false if there are no more rows.
  294. */
  295. public function fetchAssoc($cursor = null)
  296. {
  297. return mysqli_fetch_assoc($cursor ? $cursor : $this->cursor);
  298. }
  299. /**
  300. * Method to fetch a row from the result set cursor as an object.
  301. *
  302. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  303. * @param string $class The class name to use for the returned row object.
  304. *
  305. * @return mixed Either the next row from the result set or false if there are no more rows.
  306. */
  307. protected function fetchObject($cursor = null, $class = 'stdClass')
  308. {
  309. return mysqli_fetch_object($cursor ? $cursor : $this->cursor, $class);
  310. }
  311. /**
  312. * Method to free up the memory used for the result set.
  313. *
  314. * @param mixed $cursor The optional result set cursor from which to fetch the row.
  315. *
  316. * @return void
  317. */
  318. public function freeResult($cursor = null)
  319. {
  320. mysqli_free_result($cursor ? $cursor : $this->cursor);
  321. }
  322. }