PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/db/DbPDO.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 298 lines | 213 code | 21 blank | 64 comment | 10 complexity | 3316941849eb842ebace5ef6a7d2a128 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. /**
  27. * @since 1.5.0
  28. */
  29. class DbPDOCore extends Db
  30. {
  31. protected static function _getPDO($host, $user, $password, $dbname, $timeout = 5)
  32. {
  33. $dsn = 'mysql:';
  34. if ($dbname)
  35. $dsn .= 'dbname='.$dbname.';';
  36. if (preg_match('/^(.*):([0-9]+)$/', $host, $matches))
  37. $dsn .= 'host='.$matches[1].';port='.$matches[2];
  38. elseif (preg_match('#^.*:(/.*)$#', $host, $matches))
  39. $dsn .= 'unix_socket='.$matches[1];
  40. else
  41. $dsn .= 'host='.$host;
  42. return new PDO($dsn, $user, $password, array(PDO::ATTR_TIMEOUT => $timeout, PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
  43. }
  44. public static function createDatabase($host, $user, $password, $dbname, $dropit = false)
  45. {
  46. try {
  47. $link = DbPDO::_getPDO($host, $user, $password, false);
  48. $success = $link->exec('CREATE DATABASE `'.str_replace('`', '\\`', $dbname).'`');
  49. if ($dropit && ($link->exec('DROP DATABASE `'.str_replace('`', '\\`', $dbname).'`') !== false))
  50. return true;
  51. } catch (PDOException $e) {
  52. return false;
  53. }
  54. return $success;
  55. }
  56. /**
  57. * @see DbCore::connect()
  58. */
  59. public function connect()
  60. {
  61. try {
  62. $this->link = $this->_getPDO($this->server, $this->user, $this->password, $this->database, 5);
  63. } catch (PDOException $e) {
  64. die(sprintf(Tools::displayError('Link to database cannot be established: %s'), utf8_encode($e->getMessage())));
  65. }
  66. // UTF-8 support
  67. if ($this->link->exec('SET NAMES \'utf8\'') === false)
  68. die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.'));
  69. return $this->link;
  70. }
  71. /**
  72. * @see DbCore::disconnect()
  73. */
  74. public function disconnect()
  75. {
  76. unset($this->link);
  77. }
  78. /**
  79. * @see DbCore::_query()
  80. */
  81. protected function _query($sql)
  82. {
  83. return $this->link->query($sql);
  84. }
  85. /**
  86. * @see DbCore::nextRow()
  87. */
  88. public function nextRow($result = false)
  89. {
  90. if (!$result)
  91. $result = $this->result;
  92. if (!is_object($result))
  93. return false;
  94. return $result->fetch(PDO::FETCH_ASSOC);
  95. }
  96. /**
  97. * @see DbCore::getAll()
  98. */
  99. protected function getAll($result = false)
  100. {
  101. if (!$result)
  102. $result = $this->result;
  103. if (!is_object($result))
  104. return false;
  105. return $result->fetchAll(PDO::FETCH_ASSOC);
  106. }
  107. /**
  108. * @see DbCore::_numRows()
  109. */
  110. protected function _numRows($result)
  111. {
  112. return $result->rowCount();
  113. }
  114. /**
  115. * @see DbCore::Insert_ID()
  116. */
  117. public function Insert_ID()
  118. {
  119. return $this->link->lastInsertId();
  120. }
  121. /**
  122. * @see DbCore::Affected_Rows()
  123. */
  124. public function Affected_Rows()
  125. {
  126. return $this->result->rowCount();
  127. }
  128. /**
  129. * @see DbCore::getMsgError()
  130. */
  131. public function getMsgError($query = false)
  132. {
  133. $error = $this->link->errorInfo();
  134. return ($error[0] == '00000') ? '' : $error[2];
  135. }
  136. /**
  137. * @see DbCore::getNumberError()
  138. */
  139. public function getNumberError()
  140. {
  141. $error = $this->link->errorInfo();
  142. return isset($error[1]) ? $error[1] : 0;
  143. }
  144. /**
  145. * @see DbCore::getVersion()
  146. */
  147. public function getVersion()
  148. {
  149. return $this->getValue('SELECT VERSION()');
  150. }
  151. /**
  152. * @see DbCore::_escape()
  153. */
  154. public function _escape($str)
  155. {
  156. $search = array("\\", "\0", "\n", "\r", "\x1a", "'", '"');
  157. $replace = array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"');
  158. return str_replace($search, $replace, $str);
  159. }
  160. /**
  161. * @see DbCore::set_db()
  162. */
  163. public function set_db($db_name)
  164. {
  165. return $this->link->exec('USE '.pSQL($db_name));
  166. }
  167. /**
  168. * @see Db::hasTableWithSamePrefix()
  169. */
  170. public static function hasTableWithSamePrefix($server, $user, $pwd, $db, $prefix)
  171. {
  172. try {
  173. $link = DbPDO::_getPDO($server, $user, $pwd, $db, 5);
  174. } catch (PDOException $e) {
  175. return false;
  176. }
  177. $sql = 'SHOW TABLES LIKE \''.$prefix.'%\'';
  178. $result = $link->query($sql);
  179. return (bool)$result->fetch();
  180. }
  181. public static function checkCreatePrivilege($server, $user, $pwd, $db, $prefix, $engine = null)
  182. {
  183. try {
  184. $link = DbPDO::_getPDO($server, $user, $pwd, $db, 5);
  185. } catch (PDOException $e) {
  186. return false;
  187. }
  188. if ($engine === null)
  189. $engine = 'MyISAM';
  190. $result = $link->query('
  191. CREATE TABLE `'.$prefix.'test` (
  192. `test` tinyint(1) unsigned NOT NULL
  193. ) ENGINE='.$engine);
  194. if (!$result)
  195. {
  196. $error = $link->errorInfo();
  197. return $error[2];
  198. }
  199. $link->query('DROP TABLE `'.$prefix.'test`');
  200. return true;
  201. }
  202. /**
  203. * @see Db::checkConnection()
  204. */
  205. public static function tryToConnect($server, $user, $pwd, $db, $newDbLink = true, $engine = null, $timeout = 5)
  206. {
  207. try {
  208. $link = DbPDO::_getPDO($server, $user, $pwd, $db, $timeout);
  209. } catch (PDOException $e) {
  210. // hhvm wrongly reports error status 42000 when the database does not exist - might change in the future
  211. return ($e->getCode() == 1049 || (defined('HHVM_VERSION') && $e->getCode() == 42000)) ? 2 : 1;
  212. }
  213. unset($link);
  214. return 0;
  215. }
  216. public function getBestEngine()
  217. {
  218. $value = 'InnoDB';
  219. $sql = 'SHOW VARIABLES WHERE Variable_name = \'have_innodb\'';
  220. $result = $this->link->query($sql);
  221. if (!$result)
  222. $value = 'MyISAM';
  223. $row = $result->fetch();
  224. if (!$row || strtolower($row['Value']) != 'yes')
  225. $value = 'MyISAM';
  226. /* MySQL >= 5.6 */
  227. $sql = 'SHOW ENGINES';
  228. $result = $this->link->query($sql);
  229. while ($row = $result->fetch())
  230. if ($row['Engine'] == 'InnoDB')
  231. {
  232. if (in_array($row['Support'], array('DEFAULT', 'YES')))
  233. $value = 'InnoDB';
  234. break;
  235. }
  236. return $value;
  237. }
  238. /**
  239. * @see Db::checkEncoding()
  240. */
  241. public static function tryUTF8($server, $user, $pwd)
  242. {
  243. try {
  244. $link = DbPDO::_getPDO($server, $user, $pwd, false, 5);
  245. } catch (PDOException $e) {
  246. return false;
  247. }
  248. $result = $link->exec('SET NAMES \'utf8\'');
  249. unset($link);
  250. return ($result === false) ? false : true;
  251. }
  252. public static function checkAutoIncrement($server, $user, $pwd)
  253. {
  254. try {
  255. $link = DbPDO::_getPDO($server, $user, $pwd, false, 5);
  256. } catch (PDOException $e) {
  257. return false;
  258. }
  259. $ret = (bool)(($result = $link->query('SELECT @@auto_increment_increment as aii')) && ($row = $result->fetch()) && $row['aii'] == 1);
  260. $ret &= (bool)(($result = $link->query('SELECT @@auto_increment_offset as aio')) && ($row = $result->fetch()) && $row['aio'] == 1);
  261. unset($link);
  262. return $ret;
  263. }
  264. }