/modules/autoupgrade/db/MySQL.php

https://gitlab.com/ptisky/API_prestashop · PHP · 224 lines · 128 code · 26 blank · 70 comment · 21 complexity · 0ea7026e090db66e7ec3a7ce820755b0 MD5 · raw file

  1. <?php
  2. /**
  3. * 2007-2015 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-2015 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. {
  37. Tools14::displayError('Link to database cannot be established.');
  38. exit();
  39. }
  40. if (!$this->set_db($this->database))
  41. {
  42. Tools14::displayError('The database selection cannot be made.');
  43. exit();
  44. }
  45. // UTF-8 support
  46. if (!mysql_query('SET NAMES \'utf8\'', $this->link))
  47. Tools14::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.');
  48. return $this->link;
  49. }
  50. /**
  51. * @see DbCore::disconnect()
  52. */
  53. public function disconnect()
  54. {
  55. mysql_close($this->link);
  56. }
  57. /**
  58. * @see DbCore::_query()
  59. */
  60. protected function _query($sql)
  61. {
  62. return mysql_query($sql, $this->link);
  63. }
  64. /**
  65. * @see DbCore::nextRow()
  66. */
  67. public function nextRow($result = false)
  68. {
  69. $return = false;
  70. if(is_resource($result) && $result)
  71. $return = mysql_fetch_assoc($result);
  72. elseif(is_resource($this->result) && $this->result)
  73. $return = mysql_fetch_assoc($this->result);
  74. return $return;
  75. }
  76. /**
  77. * @see DbCore::_numRows()
  78. */
  79. protected function _numRows($result)
  80. {
  81. return mysql_num_rows($result);
  82. }
  83. /**
  84. * @see DbCore::Insert_ID()
  85. */
  86. public function Insert_ID()
  87. {
  88. return mysql_insert_id($this->link);
  89. }
  90. /**
  91. * @see DbCore::Affected_Rows()
  92. */
  93. public function Affected_Rows()
  94. {
  95. return mysql_affected_rows($this->link);
  96. }
  97. /**
  98. * @see DbCore::getMsgError()
  99. */
  100. public function getMsgError($query = false)
  101. {
  102. return mysql_error($this->link);
  103. }
  104. /**
  105. * @see DbCore::getNumberError()
  106. */
  107. public function getNumberError()
  108. {
  109. return mysql_errno($this->link);
  110. }
  111. /**
  112. * @see DbCore::getVersion()
  113. */
  114. public function getVersion()
  115. {
  116. return mysql_get_server_info($this->link);
  117. }
  118. /**
  119. * @see DbCore::_escape()
  120. */
  121. public function _escape($str)
  122. {
  123. return _PS_MYSQL_REAL_ESCAPE_STRING_ ? mysql_real_escape_string($str, $this->link) : addslashes($str);
  124. }
  125. /**
  126. * @see DbCore::set_db()
  127. */
  128. public function set_db($db_name)
  129. {
  130. return mysql_select_db($db_name, $this->link);
  131. }
  132. /**
  133. * @see Db::hasTableWithSamePrefix()
  134. */
  135. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  136. {
  137. if (!$link = @mysql_connect($server, $user, $pwd, true))
  138. return false;
  139. if (!@mysql_select_db($db, $link))
  140. return false;
  141. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  142. $result = mysql_query($sql);
  143. return (bool)@mysql_fetch_assoc($result);
  144. }
  145. /**
  146. * @see Db::checkConnection()
  147. */
  148. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  149. {
  150. ini_set('mysql.connect_timeout', $timeout);
  151. if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink))
  152. return 1;
  153. if (!@mysql_select_db($db, $link))
  154. return 2;
  155. if (strtolower($engine) == 'innodb')
  156. {
  157. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  158. $result = mysql_query($sql);
  159. if (!$result)
  160. return 4;
  161. $row = mysql_fetch_assoc($result);
  162. if (!$row || strtolower($row['Value']) != 'yes')
  163. return 4;
  164. }
  165. @mysql_close($link);
  166. return 0;
  167. }
  168. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine)
  169. {
  170. ini_set('mysql.connect_timeout', 5);
  171. if (!$link = @mysql_connect($server, $user, $pwd, true))
  172. return false;
  173. if (!@mysql_select_db($db, $link))
  174. return false;
  175. $sql = '
  176. CREATE TABLE `'.$prefix.'test` (
  177. `test` tinyint(1) unsigned NOT NULL
  178. ) ENGINE=MyISAM';
  179. $result = mysql_query($sql, $link);
  180. if (!$result)
  181. return mysql_error($link);
  182. mysql_query('DROP TABLE `'.$prefix.'test`', $link);
  183. return true;
  184. }
  185. /**
  186. * @see Db::checkEncoding()
  187. */
  188. static public function tryUTF8($server, $user, $pwd)
  189. {
  190. $link = @mysql_connect($server, $user, $pwd);
  191. if (!mysql_query('SET NAMES \'utf8\'', $link))
  192. $ret = false;
  193. else
  194. $ret = true;
  195. @mysql_close($link);
  196. return $ret;
  197. }
  198. }