From b668db242de35f13de0b317ceaa209574458e9c8 Mon Sep 17 00:00:00 2001 From: Robert Ros Date: Thu, 18 Sep 2014 21:33:22 +0200 Subject: [PATCH] Convert the MySQL charset to utf8mb4 to support the full range of unicode characters --- inc/poche/Database.class.php | 8 +++++--- install/index.php | 6 ++++-- install/mysql.sql | 12 ++++++------ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/inc/poche/Database.class.php b/inc/poche/Database.class.php index dfd7ae3..7aaf974 100755 --- a/inc/poche/Database.class.php +++ b/inc/poche/Database.class.php @@ -24,15 +24,17 @@ class Database { switch (STORAGE) { case 'sqlite': // Check if /db is writeable - if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) { + if ( !is_writable(STORAGE_SQLITE) || !is_writable(dirname(STORAGE_SQLITE))) { die('An error occured: "db" directory must be writeable for your web server user!'); } $db_path = 'sqlite:' . STORAGE_SQLITE; $this->handle = new PDO($db_path); break; case 'mysql': - $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; - $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD); + $db_path = 'mysql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB . ';charset=utf8mb4'; + $this->handle = new PDO($db_path, STORAGE_USER, STORAGE_PASSWORD, array( + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', + )); break; case 'postgres': $db_path = 'pgsql:host=' . STORAGE_SERVER . ';dbname=' . STORAGE_DB; diff --git a/install/index.php b/install/index.php index 1ae782a..2b080c1 100755 --- a/install/index.php +++ b/install/index.php @@ -101,12 +101,14 @@ else if (isset($_POST['install'])) { $content = file_get_contents('inc/poche/config.inc.php'); if ($_POST['db_engine'] == 'mysql') { - $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database']; + $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'] . ';charset=utf8mb4'; $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['mysql_server']."');", $content); $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['mysql_database']."');", $content); $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['mysql_user']."');", $content); $content = str_replace("define ('STORAGE_PASSWORD', 'poche');", "define ('STORAGE_PASSWORD', '".$_POST['mysql_password']."');", $content); - $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password']); + $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password'], array( + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', + )); $sql_structure = file_get_contents('install/mysql.sql'); } diff --git a/install/mysql.sql b/install/mysql.sql index de5640e..1b65cd3 100644 --- a/install/mysql.sql +++ b/install/mysql.sql @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS `config` ( `name` varchar(255) NOT NULL, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `entries` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -14,7 +14,7 @@ CREATE TABLE IF NOT EXISTS `entries` ( `content` blob NOT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -23,7 +23,7 @@ CREATE TABLE IF NOT EXISTS `users` ( `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `users_config` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -31,13 +31,13 @@ CREATE TABLE IF NOT EXISTS `users_config` ( `name` varchar(255) NOT NULL, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value` varchar(255) NOT NULL, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `tags_entries` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -46,4 +46,4 @@ CREATE TABLE IF NOT EXISTS `tags_entries` ( FOREIGN KEY(entry_id) REFERENCES entries(id) ON DELETE CASCADE, FOREIGN KEY(tag_id) REFERENCES tags(id) ON DELETE CASCADE, PRIMARY KEY (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;