- bugfix wordwrap modifier could fail if used in nocache code.

converted plugin file shared.mb_wordwrap.php into modifier.mb_wordwrap.php
This commit is contained in:
Uwe Tews
2017-08-05 19:59:23 +02:00
parent c3b331d2ad
commit 5cc9b27784
7 changed files with 132 additions and 136 deletions

View File

@@ -1,4 +1,8 @@
===== 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

View File

@@ -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

View File

@@ -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);
}

View File

@@ -0,0 +1,75 @@
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty wordwrap modifier plugin
* Type: modifier<br>
* Name: mb_wordwrap<br>
* 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;
}

View File

@@ -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 = $compiler->getPlugin('mb_wordwrap','modifier');
}
$function = 'smarty_mb_wordwrap';
}
return $function . '(' . $params[ 0 ] . ',' . $params[ 1 ] . ',' . $params[ 2 ] . ',' . $params[ 3 ] . ')';
}

View File

@@ -1,75 +0,0 @@
<?php
/**
* Smarty shared plugin
*
* @package Smarty
* @subpackage PluginsShared
*/
if (!function_exists('smarty_mb_wordwrap')) {
/**
* 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_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;
}
} elseif ($token == "\n") {
// hard break must reset counters
$_previous = 0;
$length = 0;
}
$_previous = $_space;
// add the token
$t .= $token;
}
}
return $t;
}
}

View File

@@ -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()) {