From 6a02dfd5aa63f1a003ace12126d9439bf784528c Mon Sep 17 00:00:00 2001 From: Savetheinternet Date: Wed, 7 Dec 2011 17:47:36 +1100 Subject: [PATCH] use multibyte-aware mb_strlen() instead of strlen(); fixed issue #8) --- inc/contrib/Twig/Extensions/Extension/Tinyboard.php | 2 +- inc/functions.php | 3 ++- mod.php | 12 ++++++------ post.php | 19 ++++++++++++------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/inc/contrib/Twig/Extensions/Extension/Tinyboard.php b/inc/contrib/Twig/Extensions/Extension/Tinyboard.php index 49db4046..5748c0d3 100644 --- a/inc/contrib/Twig/Extensions/Extension/Tinyboard.php +++ b/inc/contrib/Twig/Extensions/Extension/Tinyboard.php @@ -72,7 +72,7 @@ function twig_sprintf_filter( $value, $var) { } function twig_truncate_filter($value, $length = 30, $preserve = false, $separator = '…') { - if (strlen($value) > $length) { + if (mb_strlen($value) > $length) { if ($preserve) { if (false !== ($breakpoint = strpos($value, ' ', $length))) { $length = $breakpoint; diff --git a/inc/functions.php b/inc/functions.php index 3a5e0ecf..7dcdc088 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -7,7 +7,8 @@ require 'contrib/gettext/gettext.inc'; - register_shutdown_function('fatal_error_handler'); + register_shutdown_function('fatal_error_handler'); + mb_internal_encoding('UTF-8'); loadConfig(); function loadConfig() { diff --git a/mod.php b/mod.php index 5822e3b5..970586b0 100644 --- a/mod.php +++ b/mod.php @@ -1360,10 +1360,10 @@ // Bug fix for https://github.com/savetheinternet/Tinyboard/issues/21 $po->body = truncate($po->body, $po->link(), $config['body_truncate'] - substr_count($append_html, '
')); - if(strlen($po->body) + strlen($append_html) > $config['body_truncate_char']) { + if(mb_strlen($po->body) + mb_strlen($append_html) > $config['body_truncate_char']) { // still too long. temporarily increase limit in the config $__old_body_truncate_char = $config['body_truncate_char']; - $config['body_truncate_char'] = strlen($po->body) + strlen($append_html); + $config['body_truncate_char'] = mb_strlen($po->body) + mb_strlen($append_html); } $po->body .= $append_html; @@ -1731,7 +1731,7 @@ if(empty($value)) $value = 'empty'; else - $value = '' . utf8tohtml(substr($value, 0, 110) . (strlen($value) > 110 ? '…' : '')) . ''; + $value = '' . utf8tohtml(substr($value, 0, 110) . (mb_strlen($value) > 110 ? '…' : '')) . ''; } elseif(gettype($value) == 'integer') { $value = '' . $value . ''; } @@ -1787,11 +1787,11 @@ error(sprintf($config['error']['required'], 'title')); // Check string lengths - if(strlen($b['uri']) > 8) + if(mb_strlen($b['uri']) > 8) error(sprintf($config['error']['toolong'], 'URI')); - if(strlen($b['title']) > 20) + if(mb_strlen($b['title']) > 20) error(sprintf($config['error']['toolong'], 'title')); - if(strlen($b['subtitle']) > 40) + if(mb_strlen($b['subtitle']) > 40) error(sprintf($config['error']['toolong'], 'subtitle')); if(!preg_match('/^\w+$/', $b['uri'])) diff --git a/post.php b/post.php index 352cbdb8..a91b6530 100644 --- a/post.php +++ b/post.php @@ -323,11 +323,16 @@ } // Check string lengths - if(strlen($post['name']) > 50) error(sprintf($config['error']['toolong'], 'name')); - if(strlen($post['email']) > 40) error(sprintf($config['error']['toolong'], 'email')); - if(strlen($post['subject']) > 100) error(sprintf($config['error']['toolong'], 'subject')); - if(!$mod && strlen($post['body']) > $config['max_body']) error($config['error']['toolong_body']); - if(strlen($post['password']) > 20) error(sprintf($config['error']['toolong'], 'password')); + if(mb_strlen($post['name']) > 35) + error(sprintf($config['error']['toolong'], 'name')); + if(mb_strlen($post['email']) > 40) + error(sprintf($config['error']['toolong'], 'email')); + if(mb_strlen($post['subject']) > 100) + error(sprintf($config['error']['toolong'], 'subject')); + if(!$mod && mb_strlen($post['body']) > $config['max_body']) + error($config['error']['toolong_body']); + if(mb_strlen($post['password']) > 20) + error(sprintf($config['error']['toolong'], 'password')); wordfilters($post['body']); @@ -550,9 +555,9 @@ // Remove DIR_* before inserting them into the database. if($post['has_file']) { - $post['file'] = substr_replace($post['file'], '', 0, strlen($board['dir'] . $config['dir']['img'])); + $post['file'] = substr_replace($post['file'], '', 0, mb_strlen($board['dir'] . $config['dir']['img'])); if($is_an_image && $post['thumb'] != 'spoiler') - $post['thumb'] = substr_replace($post['thumb'], '', 0, strlen($board['dir'] . $config['dir']['thumb'])); + $post['thumb'] = substr_replace($post['thumb'], '', 0, mb_strlen($board['dir'] . $config['dir']['thumb'])); } $id = post($post, $OP);