diff --git a/change_log.txt b/change_log.txt index 98166f25..e46f8a9f 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,7 +1,11 @@ ===== 3.1.32 - dev === +05.8.2017 + - bugfix wordwrap modifier could fail if used in nocache code. + converted plugin file shared.mb_wordwrap.php into modifier.mb_wordwrap.php + 31.7.2017 - Call clearstatcache() after mkdir() failure https://github.com/smarty-php/smarty/pull/379 - + 30.7.2017 - rewrite mkdir() bugfix to retry automatically see https://github.com/smarty-php/smarty/pull/377 https://github.com/smarty-php/smarty/pull/379 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 7b958684..7b9a3a09 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -108,7 +108,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.32-dev-16'; + const SMARTY_VERSION = '3.1.32-dev-17'; /** * define variable scopes diff --git a/libs/plugins/block.textformat.php b/libs/plugins/block.textformat.php index 5bdf62db..1e5c0fef 100644 --- a/libs/plugins/block.textformat.php +++ b/libs/plugins/block.textformat.php @@ -35,12 +35,15 @@ */ function smarty_block_textformat($params, $content, $template, &$repeat) { + static $mb_wordwrap_loaded = false; if (is_null($content)) { return; } - if (!isset($template->smarty->_cache[ '_required_smw' ])) { - require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php'); - $template->smarty->_cache[ '_required_smw' ] = true; + if (Smarty::$_MBSTRING && !$mb_wordwrap_loaded) { + if (!is_callable('smarty_modifier_mb_wordwrap')) { + require_once(SMARTY_PLUGINS_DIR . 'modifier.mb_wordwrap.php'); + } + $mb_wordwrap_loaded = true; } $style = null; @@ -98,7 +101,7 @@ function smarty_block_textformat($params, $content, $template, &$repeat) } // wordwrap sentences if (Smarty::$_MBSTRING) { - $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut); + $_paragraph = smarty_modifier_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut); } else { $_paragraph = wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut); } diff --git a/libs/plugins/modifier.mb_wordwrap.php b/libs/plugins/modifier.mb_wordwrap.php new file mode 100644 index 00000000..9da3017a --- /dev/null +++ b/libs/plugins/modifier.mb_wordwrap.php @@ -0,0 +1,75 @@ + + * Name: mb_wordwrap
+ * Purpose: Wrap a string to a given number of characters + * + + * @link http://php.net/manual/en/function.wordwrap.php for similarity + * + * @param string $str the string to wrap + * @param int $width the width of the output + * @param string $break the character used to break the line + * @param boolean $cut ignored parameter, just for the sake of + * + * @return string wrapped string + * @author Rodney Rehm + */ +function smarty_modifier_mb_wordwrap($str, $width = 75, $break = "\n", $cut = false) +{ + // break words into tokens using white space as a delimiter + $tokens = preg_split('!(\s)!S' . Smarty::$_UTF8_MODIFIER, $str, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); + $length = 0; + $t = ''; + $_previous = false; + $_space = false; + + foreach ($tokens as $_token) { + $token_length = mb_strlen($_token, Smarty::$_CHARSET); + $_tokens = array($_token); + if ($token_length > $width) { + if ($cut) { + $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, + $_token, + -1, + PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); + } + } + + foreach ($_tokens as $token) { + $_space = !!preg_match('!^\s$!S' . Smarty::$_UTF8_MODIFIER, $token); + $token_length = mb_strlen($token, Smarty::$_CHARSET); + $length += $token_length; + + if ($length > $width) { + // remove space before inserted break + if ($_previous) { + $t = mb_substr($t, 0, -1, Smarty::$_CHARSET); + } + + if (!$_space) { + // add the break before the token + if (!empty($t)) { + $t .= $break; + } + $length = $token_length; + } + } else if ($token === "\n") { + // hard break must reset counters + $length = 0; + } + $_previous = $_space; + // add the token + $t .= $token; + } + } + + return $t; +} diff --git a/libs/plugins/modifiercompiler.wordwrap.php b/libs/plugins/modifiercompiler.wordwrap.php index 91849738..f518d14a 100644 --- a/libs/plugins/modifiercompiler.wordwrap.php +++ b/libs/plugins/modifiercompiler.wordwrap.php @@ -20,7 +20,7 @@ * * @return string with compiled code */ -function smarty_modifiercompiler_wordwrap($params, $compiler) +function smarty_modifiercompiler_wordwrap($params, Smarty_Internal_TemplateCompilerBase $compiler) { if (!isset($params[ 1 ])) { $params[ 1 ] = 80; @@ -33,19 +33,7 @@ function smarty_modifiercompiler_wordwrap($params, $compiler) } $function = 'wordwrap'; if (Smarty::$_MBSTRING) { - if ($compiler->template->caching && ($compiler->tag_nocache | $compiler->nocache)) { - $compiler->parent_compiler->template->compiled->required_plugins[ 'nocache' ][ 'wordwrap' ][ 'modifier' ][ 'file' ] = - SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php'; - $compiler->template->required_plugins[ 'nocache' ][ 'wordwrap' ][ 'modifier' ][ 'function' ] = - 'smarty_mb_wordwrap'; - } else { - $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ 'wordwrap' ][ 'modifier' ][ 'file' ] = - SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php'; - $compiler->parent_compiler->template->compiled->required_plugins[ 'compiled' ][ 'wordwrap' ][ 'modifier' ][ 'function' ] = - 'smarty_mb_wordwrap'; - } - $function = 'smarty_mb_wordwrap'; + $function = $compiler->getPlugin('mb_wordwrap','modifier'); } - return $function . '(' . $params[ 0 ] . ',' . $params[ 1 ] . ',' . $params[ 2 ] . ',' . $params[ 3 ] . ')'; } diff --git a/libs/plugins/shared.mb_wordwrap.php b/libs/plugins/shared.mb_wordwrap.php deleted file mode 100644 index 21632a1c..00000000 --- a/libs/plugins/shared.mb_wordwrap.php +++ /dev/null @@ -1,75 +0,0 @@ - $width) { - if ($cut) { - $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, $_token, - 1, - PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE); - } - } - - foreach ($_tokens as $token) { - $_space = !!preg_match('!^\s$!S' . Smarty::$_UTF8_MODIFIER, $token); - $token_length = mb_strlen($token, Smarty::$_CHARSET); - $length += $token_length; - - if ($length > $width) { - // remove space before inserted break - if ($_previous) { - $t = mb_substr($t, 0, - 1, Smarty::$_CHARSET); - } - - if (!$_space) { - // add the break before the token - if (!empty($t)) { - $t .= $break; - } - $length = $token_length; - } - } elseif ($token == "\n") { - // hard break must reset counters - $_previous = 0; - $length = 0; - } - $_previous = $_space; - // add the token - $t .= $token; - } - } - - return $t; - } -} diff --git a/libs/sysplugins/smarty_internal_testinstall.php b/libs/sysplugins/smarty_internal_testinstall.php index 7cb57557..e3b7dab3 100644 --- a/libs/sysplugins/smarty_internal_testinstall.php +++ b/libs/sysplugins/smarty_internal_testinstall.php @@ -64,7 +64,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'template_dir' ] = $message; + $errors['template_dir'] = $message; } continue; @@ -75,7 +75,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'template_dir' ] = $message; + $errors['template_dir'] = $message; } continue; @@ -88,15 +88,15 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'template_dir' ] = $message; + $errors['template_dir'] = $message; } - } elseif (!is_readable($template_dir)) { + } else if (!is_readable($template_dir)) { $status = false; $message = "FAILED: $template_dir is not readable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'template_dir' ] = $message; + $errors['template_dir'] = $message; } } else { if ($errors === null) { @@ -118,31 +118,31 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'compile_dir' ] = $message; + $errors['compile_dir'] = $message; } - } elseif (!is_dir($_compile_dir)) { + } else if (!is_dir($_compile_dir)) { $status = false; $message = "FAILED: {$_compile_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'compile_dir' ] = $message; + $errors['compile_dir'] = $message; } - } elseif (!is_readable($_compile_dir)) { + } else if (!is_readable($_compile_dir)) { $status = false; $message = "FAILED: {$_compile_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'compile_dir' ] = $message; + $errors['compile_dir'] = $message; } - } elseif (!is_writable($_compile_dir)) { + } else if (!is_writable($_compile_dir)) { $status = false; $message = "FAILED: {$_compile_dir} is not writable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'compile_dir' ] = $message; + $errors['compile_dir'] = $message; } } else { if ($errors === null) { @@ -183,7 +183,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'plugins_dir' ] = $message; + $errors['plugins_dir'] = $message; } continue; @@ -194,7 +194,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'plugins_dir' ] = $message; + $errors['plugins_dir'] = $message; } continue; @@ -207,17 +207,17 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'plugins_dir' ] = $message; + $errors['plugins_dir'] = $message; } - } elseif (!is_readable($plugin_dir)) { + } else if (!is_readable($plugin_dir)) { $status = false; $message = "FAILED: $plugin_dir is not readable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'plugins_dir' ] = $message; + $errors['plugins_dir'] = $message; } - } elseif ($_core_plugins_dir && $_core_plugins_dir == realpath($plugin_dir)) { + } else if ($_core_plugins_dir && $_core_plugins_dir == realpath($plugin_dir)) { $_core_plugins_available = true; if ($errors === null) { echo "$plugin_dir is OK.\n"; @@ -233,8 +233,8 @@ class Smarty_Internal_TestInstall $message = "WARNING: Smarty's own libs/plugins is not available"; if ($errors === null) { echo $message . ".\n"; - } elseif (!isset($errors[ 'plugins_dir' ])) { - $errors[ 'plugins_dir' ] = $message; + } else if (!isset($errors['plugins_dir'])) { + $errors['plugins_dir'] = $message; } } @@ -251,31 +251,31 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'cache_dir' ] = $message; + $errors['cache_dir'] = $message; } - } elseif (!is_dir($_cache_dir)) { + } else if (!is_dir($_cache_dir)) { $status = false; $message = "FAILED: {$_cache_dir} is not a directory"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'cache_dir' ] = $message; + $errors['cache_dir'] = $message; } - } elseif (!is_readable($_cache_dir)) { + } else if (!is_readable($_cache_dir)) { $status = false; $message = "FAILED: {$_cache_dir} is not readable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'cache_dir' ] = $message; + $errors['cache_dir'] = $message; } - } elseif (!is_writable($_cache_dir)) { + } else if (!is_writable($_cache_dir)) { $status = false; $message = "FAILED: {$_cache_dir} is not writable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'cache_dir' ] = $message; + $errors['cache_dir'] = $message; } } else { if ($errors === null) { @@ -312,7 +312,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'config_dir' ] = $message; + $errors['config_dir'] = $message; } continue; @@ -323,7 +323,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'config_dir' ] = $message; + $errors['config_dir'] = $message; } continue; @@ -336,15 +336,15 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'config_dir' ] = $message; + $errors['config_dir'] = $message; } - } elseif (!is_readable($config_dir)) { + } else if (!is_readable($config_dir)) { $status = false; $message = "FAILED: $config_dir is not readable"; if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'config_dir' ] = $message; + $errors['config_dir'] = $message; } } else { if ($errors === null) { @@ -514,9 +514,9 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'sysplugins' ] = $message; + $errors['sysplugins'] = $message; } - } elseif ($errors === null) { + } else if ($errors === null) { echo "... OK\n"; } } else { @@ -525,7 +525,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'sysplugins_dir_constant' ] = $message; + $errors['sysplugins_dir_constant'] = $message; } } @@ -543,8 +543,9 @@ class Smarty_Internal_TestInstall 'function.html_select_time.php' => true, 'function.html_table.php' => true, 'function.mailto.php' => true, 'function.math.php' => true, 'modifier.capitalize.php' => true, 'modifier.date_format.php' => true, 'modifier.debug_print_var.php' => true, - 'modifier.escape.php' => true, 'modifier.regex_replace.php' => true, - 'modifier.replace.php' => true, 'modifier.spacify.php' => true, 'modifier.truncate.php' => true, + 'modifier.escape.php' => true, 'modifier.mb_wordwrap.php' => true, + 'modifier.regex_replace.php' => true, 'modifier.replace.php' => true, + 'modifier.spacify.php' => true, 'modifier.truncate.php' => true, 'modifiercompiler.cat.php' => true, 'modifiercompiler.count_characters.php' => true, 'modifiercompiler.count_paragraphs.php' => true, 'modifiercompiler.count_sentences.php' => true, 'modifiercompiler.count_words.php' => true, 'modifiercompiler.default.php' => true, @@ -557,7 +558,7 @@ class Smarty_Internal_TestInstall 'outputfilter.trimwhitespace.php' => true, 'shared.escape_special_chars.php' => true, 'shared.literal_compiler_param.php' => true, 'shared.make_timestamp.php' => true, 'shared.mb_str_replace.php' => true, 'shared.mb_unicode.php' => true, - 'shared.mb_wordwrap.php' => true, 'variablefilter.htmlspecialchars.php' => true,); + 'variablefilter.htmlspecialchars.php' => true,); $iterator = new DirectoryIterator($source); foreach ($iterator as $file) { if (!$file->isDot()) { @@ -573,9 +574,9 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'plugins' ] = $message; + $errors['plugins'] = $message; } - } elseif ($errors === null) { + } else if ($errors === null) { echo "... OK\n"; } } else { @@ -584,7 +585,7 @@ class Smarty_Internal_TestInstall if ($errors === null) { echo $message . ".\n"; } else { - $errors[ 'plugins_dir_constant' ] = $message; + $errors['plugins_dir_constant'] = $message; } }