Skip to content

Commit

Permalink
Support for 4-Byte UTF-8 for MySQL
Browse files Browse the repository at this point in the history
It is recommended to do a backup of both your files and database before
upgrading.
Slow DB update.
  • Loading branch information
MioVisman committed May 26, 2018
1 parent 96cebbf commit 4c9b5de
Show file tree
Hide file tree
Showing 9 changed files with 643 additions and 601 deletions.
1,173 changes: 621 additions & 552 deletions db_update.php

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
$errors[] = $lang_post['No subject after censoring'];

poll_form_validate($cur_post['tid'], $errors);

} else { // MOD warnings - Visman
$subject = $cur_post['subject'];
}
Expand Down Expand Up @@ -128,9 +128,6 @@
if (!$is_admmod)
$stick_topic = $cur_post['sticky'];

// Replace four-byte characters (MySQL cannot handle them)
$message = strip_bad_multibyte_chars($message);

// Visman
$edit_post = isset($_POST['editpost']) ? '1' : '0';
if ($pun_user['g_id'] != PUN_ADMIN)
Expand Down Expand Up @@ -162,7 +159,7 @@
{
// Update the topic and any redirect topics
$db->query('UPDATE '.$db->prefix.'topics SET stick_fp='.$stick_fp.', subject=\''.$db->escape($subject).'\', sticky='.$stick_topic.' WHERE id='.$cur_post['tid'].' OR moved_to='.$cur_post['tid']) or error('Unable to update topic', __FILE__, __LINE__, $db->error());

// Is the current topic last? - last topic on index - Visman
$result = $db->query('SELECT 1 FROM '.$db->prefix.'posts WHERE id='.$cur_post['last_post_id'].' AND topic_id='.$cur_post['tid']);
if ($db->num_rows($result))
Expand Down
2 changes: 1 addition & 1 deletion include/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Define the version and database revision that this code was written for
define('FORUM_VERSION', '1.5.10');

define('FORUM_VER_REVISION', 78); // номер сборки - Visman
define('FORUM_VER_REVISION', 79); // номер сборки - Visman

$page_js = array();

Expand Down
12 changes: 8 additions & 4 deletions include/dblayer/mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function __construct($db_host, $db_username, $db_password, $db_name, $db_prefix,
error('Unable to set the character set.', __FILE__, __LINE__);
}
}


function start_transaction()
{
return;
Expand Down Expand Up @@ -202,6 +202,10 @@ function get_names()

function set_names($names)
{
if ('utf8' === $names)
{
$names = 'utf8mb4';
}
return @mysqli_set_charset($this->link_id, $names);
}

Expand Down Expand Up @@ -264,7 +268,7 @@ function create_table($table_name, $schema, $no_prefix = false)
$query .= $field_name.' '.$field_data['datatype'];

if (isset($field_data['collation']))
$query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
$query .= 'CHARACTER SET utf8mb4 COLLATE utf8mb4_'.$field_data['collation'];

if (!$field_data['allow_null'])
$query .= ' NOT NULL';
Expand Down Expand Up @@ -294,7 +298,7 @@ function create_table($table_name, $schema, $no_prefix = false)
}

// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8';
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'MyISAM').' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci';

return $this->query($query) ? true : false;
}
Expand Down
8 changes: 6 additions & 2 deletions include/dblayer/mysqli_innodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ function get_names()

function set_names($names)
{
if ('utf8' === $names)
{
$names = 'utf8mb4';
}
return @mysqli_set_charset($this->link_id, $names);
}

Expand Down Expand Up @@ -277,7 +281,7 @@ function create_table($table_name, $schema, $no_prefix = false)
$query .= $field_name.' '.$field_data['datatype'];

if (isset($field_data['collation']))
$query .= 'CHARACTER SET utf8 COLLATE utf8_'.$field_data['collation'];
$query .= 'CHARACTER SET utf8mb4 COLLATE utf8mb4_'.$field_data['collation'];

if (!$field_data['allow_null'])
$query .= ' NOT NULL';
Expand Down Expand Up @@ -307,7 +311,7 @@ function create_table($table_name, $schema, $no_prefix = false)
}

// We remove the last two characters (a newline and a comma) and add on the ending
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8';
$query = substr($query, 0, strlen($query) - 2)."\n".') ENGINE = '.(isset($schema['ENGINE']) ? $schema['ENGINE'] : 'InnoDB').' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci';

return $this->query($query) ? true : false;
}
Expand Down
29 changes: 0 additions & 29 deletions include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2100,35 +2100,6 @@ function url_valid($url)
return $m; // return TRUE == array of useful named $matches plus the valid $url.
}

//
// Replace four-byte characters with a question mark
//
// As MySQL cannot properly handle four-byte characters with the default utf-8
// charset up until version 5.5.3 (where a special charset has to be used), they
// need to be replaced, by question marks in this case.
//
function strip_bad_multibyte_chars($str)
{
$result = '';
$length = strlen($str);

for ($i = 0; $i < $length; $i++)
{
// Replace four-byte characters (11110www 10zzzzzz 10yyyyyy 10xxxxxx)
$ord = ord($str[$i]);
if ($ord >= 240 && $ord <= 244)
{
$result .= '?';
$i += 3;
}
else
{
$result .= $str[$i];
}
}

return $result;
}

//
// Check whether a file/folder is writable.
Expand Down
6 changes: 3 additions & 3 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
// The FluxBB version this script installs
define('FORUM_VERSION', '1.5.10');

define('FORUM_VER_REVISION', 78); // номер сборки - Visman
define('FORUM_VER_REVISION', 79); // номер сборки - Visman

define('FORUM_DB_REVISION', 21);
define('FORUM_SI_REVISION', 2.1);
define('FORUM_PARSER_REVISION', 2);

define('MIN_PHP_VERSION', '5.6.0');
define('MIN_MYSQL_VERSION', '5.0.7');
define('MIN_MYSQL_VERSION', '5.5.3');
define('MIN_PGSQL_VERSION', '7.0.0');
define('PUN_SEARCH_MIN_WORD', 3);
define('PUN_SEARCH_MAX_WORD', 20);
Expand Down Expand Up @@ -662,7 +662,7 @@ function process_form(the_form)
$schema = array(
'FIELDS' => array(
'conf_name' => array(
'datatype' => 'VARCHAR(255)',
'datatype' => 'VARCHAR(190)',
'allow_null' => false,
'default' => '\'\''
),
Expand Down
5 changes: 1 addition & 4 deletions post.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@
$subscribe = isset($_POST['subscribe']) ? '1' : '0';
$stick_topic = isset($_POST['stick_topic']) && $is_admmod ? '1' : '0';

// Replace four-byte characters (MySQL cannot handle them)
$message = strip_bad_multibyte_chars($message);

$now = time();

poll_form_validate($tid, $errors); // Poll Mod - Visman
Expand Down Expand Up @@ -358,7 +355,7 @@

update_forum($fid);
poll_save($new_tid);

// Should we send out notifications?
if ($pun_config['o_forum_subscriptions'] == '1')
{
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ of the other forums have whilst not sacrificing essential functionality or usabi

* A webserver
* PHP 5.6.0 or later
* A database such as MySQL 5.0.7 or later, PostgreSQL 7.0 or later, or SQLite 2 or 3
* A database such as MySQL 5.5.3 or later, PostgreSQL 7.0 or later, or SQLite 2 or 3

## Recommendations

Expand Down

0 comments on commit 4c9b5de

Please sign in to comment.