PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/db/MySQL.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 263 lines | 156 code | 33 blank | 74 comment | 33 complexity | e493aed3f13e8716d9f890d535583a4e MD5 | raw file
Possible License(s): CC-BY-SA-3.0, 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 DbCore::getAll()
  136. */
  137. protected function getAll($result = false)
  138. {
  139. if (!$result)
  140. $result = $this->result;
  141. $data = array();
  142. while ($row = $this->nextRow($result))
  143. $data[] = $row;
  144. return $data;
  145. }
  146. /**
  147. * @see Db::hasTableWithSamePrefix()
  148. */
  149. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  150. {
  151. if (!$link = @mysql_connect($server, $user, $pwd, true))
  152. return false;
  153. if (!@mysql_select_db($db, $link))
  154. return false;
  155. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  156. $result = mysql_query($sql);
  157. return (bool)@mysql_fetch_assoc($result);
  158. }
  159. /**
  160. * @see Db::checkConnection()
  161. */
  162. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  163. {
  164. ini_set('mysql.connect_timeout', $timeout);
  165. if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink))
  166. return 1;
  167. if (!@mysql_select_db($db, $link))
  168. return 2;
  169. @mysql_close($link);
  170. return 0;
  171. }
  172. public function getBestEngine()
  173. {
  174. $value = 'InnoDB';
  175. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  176. $result = mysql_query($sql);
  177. if (!$result)
  178. $value = 'MyISAM';
  179. $row = mysql_fetch_assoc($result);
  180. if (!$row || strtolower($row['Value']) != 'yes')
  181. $value = 'MyISAM';
  182. /* MySQL >= 5.6 */
  183. $sql = 'SHOW ENGINES';
  184. $result = mysql_query($sql);
  185. while ($row = mysql_fetch_assoc($result))
  186. if ($row['Engine'] == 'InnoDB')
  187. {
  188. if (in_array($row['Support'], array('DEFAULT', 'YES')))
  189. $value = 'InnoDB';
  190. break;
  191. }
  192. return $value;
  193. }
  194. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null)
  195. {
  196. ini_set('mysql.connect_timeout', 5);
  197. if (!$link = @mysql_connect($server, $user, $pwd, true))
  198. return false;
  199. if (!@mysql_select_db($db, $link))
  200. return false;
  201. if ($engine === null)
  202. $engine = 'MyISAM';
  203. $result = mysql_query('
  204. CREATE TABLE `'.$prefix.'test` (
  205. `test` tinyint(1) unsigned NOT NULL
  206. ) ENGINE='.$engine, $link);
  207. if (!$result)
  208. return mysql_error($link);
  209. mysql_query('DROP TABLE `'.$prefix.'test`', $link);
  210. return true;
  211. }
  212. /**
  213. * @see Db::checkEncoding()
  214. */
  215. public static function tryUTF8($server, $user, $pwd)
  216. {
  217. $link = @mysql_connect($server, $user, $pwd);
  218. $ret = mysql_query('SET NAMES \'utf8\'', $link);
  219. @mysql_close($link);
  220. return $ret;
  221. }
  222. public static function checkAutoIncrement($server, $user, $pwd)
  223. {
  224. $link = @mysql_connect($server, $user, $pwd);
  225. $ret = (bool)(($result = mysql_query('SELECT @@auto_increment_increment as aii', $link)) && ($row = mysql_fetch_assoc($result)) && $row['aii'] == 1);
  226. $ret &= (bool)(($result = mysql_query('SELECT @@auto_increment_offset as aio', $link)) && ($row = mysql_fetch_assoc($result)) && $row['aio'] == 1);
  227. @mysql_close($link);
  228. return $ret;
  229. }
  230. }