1
0
mirror of https://github.com/e107inc/e107.git synced 2025-03-14 17:39:46 +01:00

Bugtracker #2230 - possible fix - manage database utf8 encoding

This commit is contained in:
marj 2009-07-17 14:20:26 +00:00
parent 9ab872b199
commit e4c1ecd33a
3 changed files with 618 additions and 464 deletions

View File

@ -9,9 +9,9 @@
* mySQL Handler * mySQL Handler
* *
* $Source: /cvs_backup/e107_0.8/e107_handlers/mysql_class.php,v $ * $Source: /cvs_backup/e107_0.8/e107_handlers/mysql_class.php,v $
* $Revision: 1.38 $ * $Revision: 1.39 $
* $Date: 2009-05-24 15:34:37 $ * $Date: 2009-07-17 14:20:26 $
* $Author: e107steved $ * $Author: marj_nl_fr $
*/ */
if(defined('MYSQL_LIGHT')) if(defined('MYSQL_LIGHT'))
@ -44,8 +44,8 @@ $db_ConnectionID = NULL; // Stores ID for the first DB connection used - which s
* MySQL Abstraction class * MySQL Abstraction class
* *
* @package e107 * @package e107
* @version $Revision: 1.38 $ * @version $Revision: 1.39 $
* @author $Author: e107steved $ * @author $Author: marj_nl_fr $
*/ */
class db { class db {
@ -65,6 +65,11 @@ class db {
var $mySQLinfo; var $mySQLinfo;
var $tabset; var $tabset;
/**
* @public MySQL Charset
**/
var $mySQLcharset;
var $total_results; // Total number of results var $total_results; // Total number of results
/** /**
@ -91,60 +96,69 @@ class db {
} }
/** /**
* @return null or string error code * @access public
* @param string $mySQLserver IP Or hostname of the MySQL server * Connects to mySQL server and selects database - generally not required if your table is in the main DB.<br />
* @param string $mySQLuser MySQL username * <br />
* @param string $mySQLpassword MySQL Password * Example using e107 database with variables defined in e107_config.php:<br />
* @param string $mySQLdefaultdb The database schema to connect to * <code>$sql = new db;
* @desc Connects to mySQL server and selects database - generally not required if your table is in the main DB.<br /> * $sql->db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb);</code>
* <br /> * <br />
* Example using e107 database with variables defined in config.php:<br /> * OR to connect an other database:<br />
* <code>$sql = new db; * <code>$sql = new db;
* $sql->db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb);</code> * $sql->db_Connect('url_server_database', 'user_database', 'password_database', 'name_of_database');</code>
* <br /> *
* OR to connect an other database:<br /> * @param string $mySQLserver IP Or hostname of the MySQL server
* <code>$sql = new db; * @param string $mySQLuser MySQL username
* $sql->db_Connect('url_server_database', 'user_database', 'password_database', 'name_of_database');</code> * @param string $mySQLpassword MySQL Password
* * @param string $mySQLdefaultdb The database schema to connect to
* @access public * @param string $newLink force a new link connection if TRUE. Default FALSE
*/ * @param string $mySQLPrefix Tables prefix. Default to $mySQLPrefix from e107_config.php
* @return null|string error code
*/
function db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb, $newLink = FALSE, $mySQLPrefix = MPREFIX) function db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefaultdb, $newLink = FALSE, $mySQLPrefix = MPREFIX)
{ {
global $eTraffic, $db_ConnectionID, $db_defaultPrefix; global $eTraffic, $db_ConnectionID, $db_defaultPrefix;
$eTraffic->BumpWho('db Connect', 1); $eTraffic->BumpWho('db Connect', 1);
$this->mySQLserver = $mySQLserver; $this->mySQLserver = $mySQLserver;
$this->mySQLuser = $mySQLuser; $this->mySQLuser = $mySQLuser;
$this->mySQLpassword = $mySQLpassword; $this->mySQLpassword = $mySQLpassword;
$this->mySQLdefaultdb = $mySQLdefaultdb; $this->mySQLdefaultdb = $mySQLdefaultdb;
$this->mySQLPrefix = $mySQLPrefix; $this->mySQLPrefix = $mySQLPrefix;
$temp = $this->mySQLerror; $temp = $this->mySQLerror;
$this->mySQLerror = FALSE; $this->mySQLerror = FALSE;
if(defined("USE_PERSISTANT_DB") && USE_PERSISTANT_DB == true) if(defined("USE_PERSISTANT_DB") && USE_PERSISTANT_DB == TRUE)
{
if (!$this->mySQLaccess = @mysql_pconnect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword)) // No persistent link parameter permitted
{ {
return 'e1'; // No persistent link parameter permitted
if ( ! $this->mySQLaccess = @mysql_pconnect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword))
{
return 'e1';
}
} }
} else
else
{
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{ {
return 'e1'; if ( ! $this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{
return 'e1';
}
} }
}
if (!@mysql_select_db($this->mySQLdefaultdb,$this->mySQLaccess)) // Set utf8 connection?
{ //@TODO: simplify when yet undiscovered side-effects will be fixed
return 'e2'; $this->db_Set_Charset();
}
$this->dbError('dbConnect/SelectDB'); if ( ! @mysql_select_db($this->mySQLdefaultdb, $this->mySQLaccess))
{
return 'e2';
}
if ($db_ConnectionID == NULL) $db_ConnectionID = $this->mySQLaccess; // Save the connection resource $this->dbError('dbConnect/SelectDB');
return TRUE;
// Save the connection resource
if ($db_ConnectionID == NULL)
$db_ConnectionID = $this->mySQLaccess;
return TRUE;
} }
@ -1126,6 +1140,58 @@ class db {
{ {
return $this->mySQLlastErrText; // Text of last error (empty string if no error) return $this->mySQLlastErrText; // Text of last error (empty string if no error)
} }
/**
* Check if MySQL version is utf8 compatible and may be used as it accordingly to the user choice
*
* @TODO Simplify when the conversion script will be available
*
* @access public
* @param string MySQL charset may be forced in special occasion.
* UTF-8 encoding and decoding is left to the progammer
* @param bool TRUE enter debug mode. default FALSE
* @return string hardcoded error message
*/
function db_Set_Charset($charset = '', $debug = FALSE)
{
// Get the default user choice
global $mySQLcharset;
if (varset($mySQLcharset) != 'utf8')
{
// Only utf8 is accepted
$mySQLcharset = '';
}
$charset = ($charset ? $charset : $mySQLcharset);
$message = (( ! $charset && $debug) ? 'Empty charset!' : '');
if($charset)
{
if ( ! $debug)
{
@mysql_query("SET NAMES `$charset`");
}
else
{
// Check if MySQL version is utf8 compatible
preg_match('/^(.*?)($|-)/', mysql_get_server_info(), $mysql_version);
if (version_compare($mysql_version[1], '4.1.2', '<'))
{
// reset utf8
//@TODO reset globally? $mySQLcharset = '';
$charset = '';
$message = 'MySQL version is not utf8 compatible!';
}
else
{
// Use db_Query() debug handler
$this->db_Query("SET NAMES `$charset`", NULL, '', $debug);
}
}
}
// Save mySQLcharset for further uses within this connection
$this->mySQLcharset = $charset;
return $message;
}
} }
?> ?>

View File

@ -21,7 +21,7 @@ define("LANINS_015", "PHP Version");
define("LANINS_016", "MySQL"); define("LANINS_016", "MySQL");
define("LANINS_017", "PASS"); define("LANINS_017", "PASS");
define("LANINS_018", "Ensure all the listed files exist and are writable by the server. This normally involves CHMODing them 777, but environments vary - contact your host if you have any problems."); define("LANINS_018", "Ensure all the listed files exist and are writable by the server. This normally involves CHMODing them 777, but environments vary - contact your host if you have any problems.");
define("LANINS_019", "The version of PHP installed on your server isn't capable of running e107. e107 requires a PHP version of at least 4.3.0 to run correctly. Either upgrade your PHP version, or contact your host for an upgrade."); define("LANINS_019", "The version of PHP installed on your server isn't capable of running e107. e107 requires a PHP version of at least ".MIN_PHP_VERSION." to run correctly. Either upgrade your PHP version, or contact your host for an upgrade.");
define("LANINS_020", "Continue Installation"); define("LANINS_020", "Continue Installation");
define("LANINS_021", "2"); define("LANINS_021", "2");
define("LANINS_022", "MySQL Server Details"); define("LANINS_022", "MySQL Server Details");

File diff suppressed because it is too large Load Diff