PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/db/MySQL.php

https://github.com/netplayer/PrestaShop
PHP | 241 lines | 141 code | 29 blank | 71 comment | 25 complexity | b1b165cc294c572ffe4ca5d70eddca46 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class MySQLCore extends Db
  27. {
  28. /**
  29. * @see DbCore::connect()
  30. */
  31. public function connect()
  32. {
  33. if (!defined('_PS_MYSQL_REAL_ESCAPE_STRING_'))
  34. define('_PS_MYSQL_REAL_ESCAPE_STRING_', function_exists('mysql_real_escape_string'));
  35. if (!$this->link = mysql_connect($this->server, $this->user, $this->password))
  36. throw new PrestaShopDatabaseException(Tools::displayError('Link to database cannot be established.'));
  37. if (!$this->set_db($this->database))
  38. throw new PrestaShopDatabaseException(Tools::displayError('The database selection cannot be made.'));
  39. // UTF-8 support
  40. if (!mysql_query('SET NAMES \'utf8\'', $this->link))
  41. throw new PrestaShopDatabaseException(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
  42. return $this->link;
  43. }
  44. public static function createDatabase($host, $user, $password, $dbname, $dropit = false)
  45. {
  46. $link = mysql_connect($host, $user, $password);
  47. $success = mysql_query('CREATE DATABASE `'.str_replace('`', '\\`', $dbname).'`', $link);
  48. if ($dropit && (mysql_query('DROP DATABASE `'.str_replace('`', '\\`', $dbname).'`', $link) !== false))
  49. return true;
  50. return $success;
  51. }
  52. /**
  53. * @see DbCore::disconnect()
  54. */
  55. public function disconnect()
  56. {
  57. mysql_close($this->link);
  58. }
  59. /**
  60. * @see DbCore::_query()
  61. */
  62. protected function _query($sql)
  63. {
  64. return mysql_query($sql, $this->link);
  65. }
  66. /**
  67. * @see DbCore::nextRow()
  68. */
  69. public function nextRow($result = false)
  70. {
  71. $return = false;
  72. if(is_resource($result) && $result)
  73. $return = mysql_fetch_assoc($result);
  74. elseif(is_resource($this->_result) && $this->_result)
  75. $return = mysql_fetch_assoc($this->_result);
  76. return $return;
  77. }
  78. /**
  79. * @see DbCore::_numRows()
  80. */
  81. protected function _numRows($result)
  82. {
  83. return mysql_num_rows($result);
  84. }
  85. /**
  86. * @see DbCore::Insert_ID()
  87. */
  88. public function Insert_ID()
  89. {
  90. return mysql_insert_id($this->link);
  91. }
  92. /**
  93. * @see DbCore::Affected_Rows()
  94. */
  95. public function Affected_Rows()
  96. {
  97. return mysql_affected_rows($this->link);
  98. }
  99. /**
  100. * @see DbCore::getMsgError()
  101. */
  102. public function getMsgError($query = false)
  103. {
  104. return mysql_error($this->link);
  105. }
  106. /**
  107. * @see DbCore::getNumberError()
  108. */
  109. public function getNumberError()
  110. {
  111. return mysql_errno($this->link);
  112. }
  113. /**
  114. * @see DbCore::getVersion()
  115. */
  116. public function getVersion()
  117. {
  118. return mysql_get_server_info($this->link);
  119. }
  120. /**
  121. * @see DbCore::_escape()
  122. */
  123. public function _escape($str)
  124. {
  125. return _PS_MYSQL_REAL_ESCAPE_STRING_ ? mysql_real_escape_string($str, $this->link) : addslashes($str);
  126. }
  127. /**
  128. * @see DbCore::set_db()
  129. */
  130. public function set_db($db_name)
  131. {
  132. return mysql_select_db($db_name, $this->link);
  133. }
  134. /**
  135. * @see Db::hasTableWithSamePrefix()
  136. */
  137. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  138. {
  139. if (!$link = @mysql_connect($server, $user, $pwd, true))
  140. return false;
  141. if (!@mysql_select_db($db, $link))
  142. return false;
  143. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  144. $result = mysql_query($sql);
  145. return (bool)@mysql_fetch_assoc($result);
  146. }
  147. /**
  148. * @see Db::checkConnection()
  149. */
  150. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  151. {
  152. ini_set('mysql.connect_timeout', $timeout);
  153. if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink))
  154. return 1;
  155. if (!@mysql_select_db($db, $link))
  156. return 2;
  157. @mysql_close($link);
  158. return 0;
  159. }
  160. public function getBestEngine()
  161. {
  162. $value = 'InnoDB';
  163. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  164. $result = mysql_query($sql);
  165. if (!$result)
  166. $value = 'MyISAM';
  167. $row = mysql_fetch_assoc($result);
  168. if (!$row || strtolower($row['Value']) != 'yes')
  169. $value = 'MyISAM';
  170. /* MySQL >= 5.6 */
  171. $sql = 'SHOW ENGINES';
  172. $result = mysql_query($sql);
  173. while ($row = mysql_fetch_assoc($result))
  174. if ($row['Engine'] == 'InnoDB')
  175. {
  176. if (in_array($row['Support'], array('DEFAULT', 'YES')))
  177. $value = 'InnoDB';
  178. break;
  179. }
  180. return $value;
  181. }
  182. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null)
  183. {
  184. ini_set('mysql.connect_timeout', 5);
  185. if (!$link = @mysql_connect($server, $user, $pwd, true))
  186. return false;
  187. if (!@mysql_select_db($db, $link))
  188. return false;
  189. $sql = '
  190. CREATE TABLE `'.$prefix.'test` (
  191. `test` tinyint(1) unsigned NOT NULL
  192. ) ENGINE=MyISAM';
  193. $result = mysql_query($sql, $link);
  194. if (!$result)
  195. return mysql_error($link);
  196. mysql_query('DROP TABLE `'.$prefix.'test`', $link);
  197. return true;
  198. }
  199. /**
  200. * @see Db::checkEncoding()
  201. */
  202. static public function tryUTF8($server, $user, $pwd)
  203. {
  204. $link = @mysql_connect($server, $user, $pwd);
  205. if (!mysql_query('SET NAMES \'utf8\'', $link))
  206. $ret = false;
  207. else
  208. $ret = true;
  209. @mysql_close($link);
  210. return $ret;
  211. }
  212. }