summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Foster <savetheinternet@tinyboard.org>2013-07-31 03:12:06 -0400
committerMichael Foster <savetheinternet@tinyboard.org>2013-07-31 03:12:06 -0400
commit37d769646b7754f37928f9ceb8b0a2a9eea58c6b (patch)
tree35d929c3a32ecb79b362105a2fdfa40a90253338
parent90b82b1a0635d78d536bc92decbdc273d8f7e323 (diff)
Markup/quote fix for multibyte
-rw-r--r--inc/functions.php28
1 files changed, 23 insertions, 5 deletions
diff --git a/inc/functions.php b/inc/functions.php
index 773f8eb6..2d648df8 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -297,6 +297,10 @@ function sprintf3($str, $vars, $delim = '%') {
array_values($replaces), $str);
}
+function mb_substr_replace($string, $replacement, $start, $length) {
+ return mb_substr($string, 0, $start) . $replacement . mb_substr($string, $start + $length);
+}
+
function setupBoard($array) {
global $board, $config;
@@ -1451,6 +1455,7 @@ function markup(&$body, $track_cites = false) {
}
$skip_chars = 0;
+ $body_tmp = $body;
foreach ($cites as $matches) {
$cite = $matches[2][0];
@@ -1458,20 +1463,26 @@ function markup(&$body, $track_cites = false) {
$query->bindValue(':id', $cite);
$query->execute() or error(db_error($query));
+ // preg_replace is not multibyte-safe
+ foreach ($matches as &$match) {
+ $match[1] = mb_strlen(substr($body_tmp, 0, $match[1]));
+ }
+
if ($post = $query->fetch()) {
$replacement = '<a onclick="highlightReply(\''.$cite.'\');" href="' .
$config['root'] . $board['dir'] . $config['dir']['res'] . ($post['thread']?$post['thread']:$post['id']) . '.html#' . $cite . '">' .
'&gt;&gt;' . $cite .
'</a>';
- $body = substr_replace($body, $matches[1][0] . $replacement . $matches[3][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
- $skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[3][0]) - mb_strlen($matches[0][0]);
+ $body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[3][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
+ $skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[3][0]) - mb_strlen($matches[0][0]);
+
if ($track_cites && $config['track_cites'])
$tracked_cites[] = array($board['uri'], $post['id']);
}
}
}
-
+
// Cross-board linking
if (preg_match_all('/(^|\s)&gt;&gt;&gt;\/(' . $config['board_regex'] . 'f?)\/(\d+)?([\s,.)?]|$)/um', $body, $cites, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) {
if (count($cites[0]) > $config['max_cites']) {
@@ -1479,11 +1490,17 @@ function markup(&$body, $track_cites = false) {
}
$skip_chars = 0;
+ $body_tmp = $body;
foreach ($cites as $matches) {
$_board = $matches[2][0];
$cite = @$matches[3][0];
+ // preg_replace is not multibyte-safe
+ foreach ($matches as &$match) {
+ $match[1] = mb_strlen(substr($body_tmp, 0, $match[1]));
+ }
+
// Temporarily store board information because it will be overwritten
$tmp_board = $board['uri'];
@@ -1499,7 +1516,8 @@ function markup(&$body, $track_cites = false) {
$config['root'] . $board['dir'] . $config['dir']['res'] . ($post['thread']?$post['thread']:$post['id']) . '.html#' . $cite . '">' .
'&gt;&gt;&gt;/' . $_board . '/' . $cite .
'</a>';
- $body = substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
+
+ $body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
$skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[4][0]) - mb_strlen($matches[0][0]);
if ($track_cites && $config['track_cites'])
@@ -1510,7 +1528,7 @@ function markup(&$body, $track_cites = false) {
$config['root'] . $board['dir'] . $config['file_index'] . '">' .
'&gt;&gt;&gt;/' . $_board . '/' .
'</a>';
- $body = substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
+ $body = mb_substr_replace($body, $matches[1][0] . $replacement . $matches[4][0], $matches[0][1] + $skip_chars, mb_strlen($matches[0][0]));
$skip_chars += mb_strlen($matches[1][0] . $replacement . $matches[4][0]) - mb_strlen($matches[0][0]);
}
}