From daa3ce35912ae93eaa82f34f6e0b1583a56a432b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= Date: Thu, 17 Dec 2015 21:10:14 +0100 Subject: [PATCH 1/4] [Qual] Mysqli: set charset with recommended method --- htdocs/admin/tools/export.php | 15 +++++++++++---- htdocs/core/db/mysqli.class.php | 17 +++++++---------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/htdocs/admin/tools/export.php b/htdocs/admin/tools/export.php index c9e4211842d..c89fafbe052 100644 --- a/htdocs/admin/tools/export.php +++ b/htdocs/admin/tools/export.php @@ -1,6 +1,7 @@ - * Copyright (C) 2011 Juanjo Menent +/* Copyright (C) 2006-2014 Laurent Destailleur + * Copyright (C) 2011 Juanjo Menent + * Copyright (C) 2015 Raphaël Doursenaud * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -417,8 +418,14 @@ function backup_tables($outputfile, $tables='*') global $errormsg; // Set to UTF-8 - $db->query('SET NAMES utf8'); - $db->query('SET CHARACTER SET utf8'); + if(is_a($db, 'DoliDBMysqli')) { + /** @var DoliDBMysqli $db */ + $db->db->set_charset('utf8'); + } else { + /** @var DoliDB $db */ + $db->query('SET NAMES utf8'); + $db->query('SET CHARACTER SET utf8'); + } //get all of the tables if ($tables == '*') diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 04f6372055f..05e84d6dd50 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -4,6 +4,7 @@ * Copyright (C) 2004-2011 Laurent Destailleur * Copyright (C) 2006 Andre Cianfarani * Copyright (C) 2005-2012 Regis Houssin + * Copyright (C) 2015 Raphaël Doursenaud * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -115,11 +116,9 @@ class DoliDBMysqli extends DoliDB $clientmustbe=''; if (preg_match('/UTF-8/i',$conf->file->character_set_client)) $clientmustbe='utf8'; if (preg_match('/ISO-8859-1/i',$conf->file->character_set_client)) $clientmustbe='latin1'; - if ($this->db->character_set_name() != $clientmustbe) - { - $this->query("SET NAMES '".$clientmustbe."'", $this->db); - //$this->query("SET CHARACTER SET ". $this->forcecharset); - } + if ($this->db->character_set_name() != $clientmustbe) { + $this->db->set_charset($clientmustbe); + } } else { @@ -141,11 +140,9 @@ class DoliDBMysqli extends DoliDB $clientmustbe=''; if (preg_match('/UTF-8/i',$conf->file->character_set_client)) $clientmustbe='utf8'; if (preg_match('/ISO-8859-1/i',$conf->file->character_set_client)) $clientmustbe='latin1'; - if ($this->db->character_set_name() != $clientmustbe) - { - $this->query("SET NAMES '".$clientmustbe."'", $this->db); - //$this->query("SET CHARACTER SET ". $this->forcecharset); - } + if ($this->db->character_set_name() != $clientmustbe) { + $this->db->set_charset($clientmustbe); + } } } From 87762f14b52d794c0df90ed1b4d4530601971474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= Date: Thu, 17 Dec 2015 21:19:05 +0100 Subject: [PATCH 2/4] [Qual] Mysqli: OOP style --- htdocs/core/db/mysqli.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 05e84d6dd50..356a74ad348 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -200,7 +200,7 @@ class DoliDBMysqli extends DoliDB */ function getVersion() { - return $this->db->get_server_info(); + return $this->db->server_info; } /** @@ -210,7 +210,7 @@ class DoliDBMysqli extends DoliDB */ function getDriverInfo() { - return $this->db->get_client_info(); + return $this->db->client_info; } From aeb3935e312794c2da3a183ab7351e68123443f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= Date: Thu, 17 Dec 2015 21:53:09 +0100 Subject: [PATCH 3/4] [Qual] Mysqli: escape with the proper method --- htdocs/core/db/mysqli.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 356a74ad348..2fac388a34b 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -378,7 +378,7 @@ class DoliDBMysqli extends DoliDB */ function escape($stringtoencode) { - return addslashes($stringtoencode); + return $this->db->real_escape_string($stringtoencode); } /** From 9eff599f976fc2cf5d6b5bb23180dda6a72d1f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Doursenaud?= Date: Thu, 17 Dec 2015 22:39:23 +0100 Subject: [PATCH 4/4] [Qual] Mysqli: constructor cannot return a value --- htdocs/core/db/mysqli.class.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 2fac388a34b..d21caecea36 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -76,7 +76,6 @@ class DoliDBMysqli extends DoliDB $this->ok = false; $this->error="Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver."; dol_syslog(get_class($this)."::DoliDBMysqli : Mysqli PHP functions for using Mysqli driver are not available in this version of PHP. Try to use another driver.",LOG_ERR); - return $this->ok; } if (! $host) @@ -85,7 +84,6 @@ class DoliDBMysqli extends DoliDB $this->ok = false; $this->error=$langs->trans("ErrorWrongHostParameter"); dol_syslog(get_class($this)."::DoliDBMysqli : Connect error, wrong host parameters",LOG_ERR); - return $this->ok; } // Try server connection @@ -97,7 +95,6 @@ class DoliDBMysqli extends DoliDB $this->ok = false; $this->error = $this->db->connect_error; dol_syslog(get_class($this) . "::DoliDBMysqli Connect error: " . $this->error, LOG_ERR); - return $this->ok; } else { $this->connected = true; $this->ok = true; @@ -145,8 +142,6 @@ class DoliDBMysqli extends DoliDB } } } - - return $this->ok; }