From 174947be5bec94c6ade7a8b838daabce807d2d3d Mon Sep 17 00:00:00 2001 From: rMalie Date: Wed, 7 Sep 2011 08:16:43 +0000 Subject: [PATCH] [*] Project: MySQL::trytoconnect() and MySQL::tryUTF8() methods replaced with Db::checkConnection() and Db::checkEncoding() git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@8380 b9a71923-0436-4b27-9f14-aed3839534dd --- admin-dev/tabs/AdminDb.php | 19 ++++-- classes/Db.php | 94 ++++++++++++++++++++++------ classes/DbMySQLi.php | 50 ++++++++++----- classes/MySQL.php | 27 ++++++++ install-dev/classes/ToolsInstall.php | 2 +- 5 files changed, 151 insertions(+), 41 deletions(-) diff --git a/admin-dev/tabs/AdminDb.php b/admin-dev/tabs/AdminDb.php index c0846e4bd..267f33770 100644 --- a/admin-dev/tabs/AdminDb.php +++ b/admin-dev/tabs/AdminDb.php @@ -1,4 +1,4 @@ -optionsList['database']['fields'] as $k => $data) if ($value = Tools::getValue($k)) $settings['_'.Tools::strtoupper($k).'_'] = $value; - rewriteSettingsFile(NULL, NULL, $settings); - Tools::redirectAdmin(self::$currentIndex.'&conf=6'.'&token='.$this->token); + + if (Db::checkConnection( + isset($settings['_DB_SERVER_']) ? $settings['_DB_SERVER_'] : _DB_SERVER_, + isset($settings['_DB_USER_']) ? $settings['_DB_USER_'] : _DB_USER_, + isset($settings['_DB_PASSWD_']) ? $settings['_DB_PASSWD_'] : _DB_PASSWD_, + isset($settings['_DB_NAME_']) ? $settings['_DB_NAME_'] : _DB_NAME_, + true + ) == 0) + { + rewriteSettingsFile(NULL, NULL, $settings); + Tools::redirectAdmin(self::$currentIndex.'&conf=6'.'&token='.$this->token); + } + else + $this->_errors[] = Tools::displayError('Unable to connect to a database with these identifiers.'); } } else @@ -94,7 +106,6 @@ class AdminDb extends AdminPreferences $engineType = pSQL(Tools::getValue('engineType')); /* Datas are not saved in database but in config/settings.inc.php */ - // @todo add a Db::trytoconnect() $settings = array('_MYSQL_ENGINE_' => $engineType); rewriteSettingsFile(NULL, NULL, $settings); diff --git a/classes/Db.php b/classes/Db.php index a3fe13e8e..d8749b6eb 100644 --- a/classes/Db.php +++ b/classes/Db.php @@ -136,18 +136,24 @@ abstract class DbCore */ abstract public function getNumberError(); + /** + * Try a connection to te database + * + * @param string $newDbLink + * @return int + */ + abstract public function tryConnection($newDbLink = true); + + /** + * Try to change encoding on a database + * + * @param string $encoding + * @return bool + */ + abstract public function tryEncoding($encoding = 'UTF8'); + /* do not remove, useful for some modules */ abstract public function set_db($db_name); - - /** - * Try a connection - */ - //abstract static public function tryToConnect($server, $user, $pwd, $db); - - /** - * Try to change UTF8 - */ - //abstract static public function tryUTF8($server, $user, $pwd); /** * Get Db object instance @@ -169,16 +175,29 @@ abstract class DbCore } if (!isset(self::$_instance[$idServer])) - { - $class = 'MySQL'; - if (class_exists('mysqli', false)) - $class = 'DbMySQLi'; - self::$_instance[$idServer] = new $class(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']); - } + self::$_instance[$idServer] = Db::newInstance(self::$_servers[$idServer]['server'], self::$_servers[$idServer]['user'], self::$_servers[$idServer]['password'], self::$_servers[$idServer]['database']); return self::$_instance[$idServer]; } + /** + * Get child layer class instance + * + * @param string $server Server address + * @param string $user User login + * @param string $password User password + * @param string $database Database name + * @param bool $connect If false, don't connect in constructor + * @return Db + */ + public static function newInstance($server, $user, $password, $database, $connect = true) + { + $class = 'MySQL'; + if (class_exists('mysqli', false)) + $class = 'DbMySQLi'; + return new $class($server, $user, $password, $database, $connect); + } + /** * Instantiate database connection * @@ -186,8 +205,9 @@ abstract class DbCore * @param string $user User login * @param string $password User password * @param string $database Database name + * @param bool $connect If false, don't connect in constructor (since 1.5.0) */ - public function __construct($server, $user, $password, $database) + public function __construct($server, $user, $password, $database, $connect = true) { $this->_server = $server; $this->_user = $user; @@ -198,7 +218,8 @@ abstract class DbCore if (!defined('_PS_DEBUG_SQL_')) define('_PS_DEBUG_SQL_', false); - $this->connect(); + if ($connect) + $this->connect(); } /** @@ -206,7 +227,8 @@ abstract class DbCore */ public function __destruct() { - $this->disconnect(); + if ($this->_link) + $this->disconnect(); } /** @@ -475,7 +497,39 @@ abstract class DbCore return $string; } - + + /** + * Try a connection to te database + * + * @param string $server Server address + * @param string $user Login for database connection + * @param string $pwd Password for database connection + * @param string $db Database name + * @return int + */ + static public function checkConnection($server, $user, $pwd, $db) + { + $instance = Db::newInstance($server, $user, $pwd, $db, false); + return $instance->tryConnection(); + } + + /** + * Try a connection to te database + * + * @param string $server Server address + * @param string $user Login for database connection + * @param string $pwd Password for database connection + * @param string $db Database name + * @param string $newDbLink Database name + * @return int + * @todo remake with static + */ + static public function checkEncoding($server, $user, $pwd, $encoding = 'UTF8') + { + $instance = Db::newInstance($server, $user, $pwd, '', false); + return $instance->tryEncoding($encoding); + } + /** * Alias of Db::getInstance()->ExecuteS * diff --git a/classes/DbMySQLi.php b/classes/DbMySQLi.php index c0f58bffa..e52a4b55e 100644 --- a/classes/DbMySQLi.php +++ b/classes/DbMySQLi.php @@ -134,36 +134,54 @@ class DbMySQLiCore extends Db { return $this->_link->query('USE '.pSQL($db_name)); } - + /** - * tryToConnect return 0 if the connection succeed and the database can be selected. - * @since 1.4.4.0, the parameter $newDbLink (default true) has been added. - * - * @param string $server mysql server name - * @param string $user mysql user - * @param string $pwd mysql user password - * @param string $db mysql database name - * @param boolean $newDbLink Param not used in this class - * @return integer + * @see DbCore::tryConnection() */ - static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true) + public function tryConnection($newDbLink = true) { - $link = new mysqli($server, $user, $password, $db); + $link = @new mysqli($this->_server, $this->_user, $this->_password, $this->_database); // Do not use object way for error because this work bad before PHP 5.2.9 if (mysqli_connect_error()) return 1; $link->close(); return 0; + } + + /** + * @see DbCore::tryEncoding() + */ + public function tryEncoding($encoding = 'UTF8') + { + $link = @new mysqli($this->_server, $this->_user, $this->_password, $this->_database); + $ret = $link->query("SET NAMES '".pSQL($encoding)."'"); + $link->close(); + return $ret; + } + + /** + * @deprecated since 1.5.0 + */ + static public function tryToConnect($server, $user, $pwd, $db, $newDbLink = true) + { + Tools::displayAsDeprecated(); - // UTF-8 support - if (!$this->_link->query('SET NAMES \'utf8\'')) - die(Tools::displayError('PrestaShop Fatal error: no utf-8 support. Please check your server configuration.')); + $link = new mysqli($server, $user, $pwd, $db); + if (mysqli_connect_error()) + return 1; + $link->close(); + return 0; } + /** + * @deprecated since 1.5.0 + */ static public function tryUTF8($server, $user, $pwd) { - $link = new mysqli($server, $user, $password, $db); + Tools::displayAsDeprecated(); + + $link = new mysqli($server, $user, $pwd, $db); $ret = $link->query("SET NAMES 'UTF8'"); $link->close(); return $ret; diff --git a/classes/MySQL.php b/classes/MySQL.php index 886d7d92e..ef52da264 100644 --- a/classes/MySQL.php +++ b/classes/MySQL.php @@ -136,6 +136,33 @@ class MySQLCore extends Db return mysql_select_db($db_name, $this->_link); } + /** + * @see DbCore::tryConnection() + */ + public function tryConnection($server, $user, $pwd, $db, $newDbLink = true) + { + if (!$link = @mysql_connect($server, $user, $pwd, $newDbLink)) + return 1; + if (!@mysql_select_db($db, $link)) + return 2; + @mysql_close($link); + return 0; + } + + /** + * @see DbCore::tryEncoding() + */ + public function tryEncoding($server, $user, $pwd, $encoding = 'UTF8') + { + $link = @mysql_connect($server, $user, $pwd); + if (!mysql_query('SET NAMES \''.pSQL($encoding).'\'', $link)) + $ret = false; + else + $ret = true; + @mysql_close($link); + return $ret; + } + /** * tryToConnect return 0 if the connection succeed and the database can be selected. * @since 1.4.4.0, the parameter $newDbLink (default true) has been added. diff --git a/install-dev/classes/ToolsInstall.php b/install-dev/classes/ToolsInstall.php index 7a2154f6d..8dcb38b79 100644 --- a/install-dev/classes/ToolsInstall.php +++ b/install-dev/classes/ToolsInstall.php @@ -65,7 +65,7 @@ class ToolsInstall return 8; } - switch(MySQL::tryToConnect(trim($srv), trim($login), trim($password), trim($name))) + switch (Db::checkConnection(trim($srv), trim($login), trim($password), trim($name))) { case 0: if (MySQL::tryUTF8(trim($srv), trim($login), trim($password)))