- check if mb string functions available otherwise fallback to normal string functions

This commit is contained in:
Uwe.Tews
2009-04-10 15:52:59 +00:00
parent 1262b7d873
commit ba61f12384
12 changed files with 224 additions and 150 deletions

View File

@@ -1,4 +1,5 @@
04/10/2009 04/10/2009
- check if mb string functions available otherwise fallback to normal string functions
- added global variable scope SMARTY_GLOBAL_SCOPE - added global variable scope SMARTY_GLOBAL_SCOPE
- enable 'variable' filter by default - enable 'variable' filter by default
- fixed {$smarty.block.parent.foo} - fixed {$smarty.block.parent.foo}

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase {
// config var settings // config var settings
public $config_overwrite = true; //Controls whether variables with the same name overwrite each other. public $config_overwrite = true; //Controls whether variables with the same name overwrite each other.
public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean
public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file. public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file.
// config vars // config vars
public $config_vars = array(); public $config_vars = array();
// assigned tpl vars // assigned tpl vars
@@ -167,13 +167,18 @@ class Smarty extends Smarty_Internal_TemplateBase {
// start time for execution time calculation // start time for execution time calculation
public $start_time = 0; public $start_time = 0;
// set default time zone // set default time zone
public $set_timezone = true; public $set_timezone = true;
// has multibyte string functions?
public $has_mb = false;
/** /**
* Class constructor, initializes basic smarty properties * Class constructor, initializes basic smarty properties
*/ */
public function __construct() public function __construct()
{ {
mb_internal_encoding($this->resource_char_set); if (is_callable('mb_internal_encoding')) {
$this->has_mb = true;
mb_internal_encoding($this->resource_char_set);
}
if ($this->set_timezone and function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get")) { if ($this->set_timezone and function_exists("date_default_timezone_set") and function_exists("date_default_timezone_get")) {
date_default_timezone_set(date_default_timezone_get()); date_default_timezone_set(date_default_timezone_get());
} }
@@ -199,7 +204,6 @@ class Smarty extends Smarty_Internal_TemplateBase {
$this->plugin_handler = new Smarty_Internal_Plugin_Handler; $this->plugin_handler = new Smarty_Internal_Plugin_Handler;
$this->loadPlugin('Smarty_Internal_Run_Filter'); $this->loadPlugin('Smarty_Internal_Run_Filter');
$this->filter_handler = new Smarty_Internal_Run_Filter; $this->filter_handler = new Smarty_Internal_Run_Filter;
if (!$this->debugging && $this->debugging_ctrl == 'URL') { if (!$this->debugging && $this->debugging_ctrl == 'URL') {
$_query_string = $this->request_use_auto_globals ? isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING']:'' : isset($GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']) ? $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']:''; $_query_string = $this->request_use_auto_globals ? isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING']:'' : isset($GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']) ? $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']:'';
if (false !== strpos($_query_string, $this->smarty_debug_id)) { if (false !== strpos($_query_string, $this->smarty_debug_id)) {

View File

@@ -72,7 +72,11 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null)
case 'mail': case 'mail':
// safe way to display e-mail address on a web page // safe way to display e-mail address on a web page
return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string); if ($smarty->has_mb) {
return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
} else {
return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
}
case 'nonstd': case 'nonstd':
// escape non-standard chars, such as ms document quotes // escape non-standard chars, such as ms document quotes

View File

@@ -1,26 +1,31 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* @package Smarty *
* @subpackage PluginsModifier * @package Smarty
*/ * @subpackage PluginsModifier
*/
/** /**
* Smarty lower modifier plugin * Smarty lower modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: lower<br> * Name: lower<br>
* Purpose: convert string to lowercase * Purpose: convert string to lowercase
* @link http://smarty.php.net/manual/en/language.modifier.lower.php *
* lower (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.lower.php lower (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string * @param string $
* @return string * @return string
*/ */
function smarty_modifier_lower($string) function smarty_modifier_lower($string)
{ {
return mb_strtolower($string); $smarty = Smarty::instance();
} if ($smarty->has_mb) {
return mb_strtolower($string);
} else {
return strtolower($string);
}
}
?> ?>

View File

@@ -1,46 +1,61 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* @package Smarty *
* @subpackage PluginsModifier * @package Smarty
*/ * @subpackage PluginsModifier
*/
/** /**
* Smarty regex_replace modifier plugin * Smarty regex_replace modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: regex_replace<br> * Name: regex_replace<br>
* Purpose: regular expression search/replace * Purpose: regular expression search/replace
* @link http://smarty.php.net/manual/en/language.modifier.regex.replace.php *
* regex_replace (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.regex.replace.php regex_replace (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string * @param string $
* @param string|array * @param string $ |array
* @param string|array * @param string $ |array
* @return string * @return string
*/ */
function smarty_modifier_regex_replace($string, $search, $replace) function smarty_modifier_regex_replace($string, $search, $replace)
{ {
if(is_array($search)) { $smarty = Smarty::instance();
foreach($search as $idx => $s)
if (is_array($search)) {
foreach($search as $idx => $s)
$search[$idx] = _smarty_regex_replace_check($s); $search[$idx] = _smarty_regex_replace_check($s);
} else { } else {
$search = _smarty_regex_replace_check($search); $search = _smarty_regex_replace_check($search);
} }
return mb_ereg_replace($search, $replace, $string); if ($smarty->has_mb) {
} return mb_ereg_replace($search, $replace, $string);
} else {
return ereg_replace($search, $replace, $string);
}
}
function _smarty_regex_replace_check($search) function _smarty_regex_replace_check($search)
{ {
if (($pos = mb_strpos($search,"\0")) !== false) if ($smarty->has_mb) {
$search = mb_substr($search,0,$pos); if (($pos = mb_strpos($search, "\0")) !== false)
if (mb_preg_match('!([a-zA-Z\s]+)$!s', $search, $match) && (mb_strpos($match[1], 'e') !== false)) { $search = mb_substr($search, 0, $pos);
/* remove eval-modifier from $search */ if (mb_preg_match('!([a-zA-Z\s]+)$!s', $search, $match) && (mb_strpos($match[1], 'e') !== false)) {
$search = mb_substr($search, 0, -mb_strlen($match[1])) . mb_ereg_replace('![e\s]+!', '', $match[1]); /* remove eval-modifier from $search */
} $search = mb_substr($search, 0, - mb_strlen($match[1])) . mb_ereg_replace('![e\s]+!', '', $match[1]);
}
} else {
if (($pos = strpos($search, "\0")) !== false)
$search = substr($search, 0, $pos);
if (mb_preg_match('!([a-zA-Z\s]+)$!s', $search, $match) && (strpos($match[1], 'e') !== false)) {
/* remove eval-modifier from $search */
$search = substr($search, 0, - strlen($match[1])) . ereg_replace('![e\s]+!', '', $match[1]);
}
}
return $search; return $search;
} }
?> ?>

View File

@@ -38,7 +38,12 @@ function smarty_modifier_replace($string, $search, $replace)
return $haystack; return $haystack;
} }
} }
return mb_str_replace($search, $replace, $string); $smarty = Smarty::instance();
if ($smarty->has_mb) {
return mb_str_replace($search, $replace, $string);
} else {
return str_replace($search, $replace, $string);
}
} }
?> ?>

View File

@@ -1,27 +1,32 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* @package Smarty *
* @subpackage PluginsModifier * @package Smarty
*/ * @subpackage PluginsModifier
*/
/** /**
* Smarty spacify modifier plugin * Smarty spacify modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: spacify<br> * Name: spacify<br>
* Purpose: add spaces between characters in a string * Purpose: add spaces between characters in a string
* @link http://smarty.php.net/manual/en/language.modifier.spacify.php *
* spacify (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.spacify.php spacify (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string * @param string $
* @param string * @param string $
* @return string * @return string
*/ */
function smarty_modifier_spacify($string, $spacify_char = ' ') function smarty_modifier_spacify($string, $spacify_char = ' ')
{ {
return implode($spacify_char, $smarty = Smarty::instance();
mb_split('//', $string, -1)); if ($smarty->has_mb) {
} return implode($spacify_char, mb_split('//', $string, -1));
} else {
return implode($spacify_char, split('//', $string, -1));
}
}
?> ?>

View File

@@ -1,30 +1,36 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* @package Smarty *
* @subpackage PluginsModifier * @package Smarty
*/ * @subpackage PluginsModifier
*/
/** /**
* Smarty strip modifier plugin * Smarty strip modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: strip<br> * Name: strip<br>
* Purpose: Replace all repeated spaces, newlines, tabs * Purpose: Replace all repeated spaces, newlines, tabs
* with a single space or supplied replacement string.<br> * with a single space or supplied replacement string.<br>
* Example: {$var|strip} {$var|strip:"&nbsp;"} * Example: {$var|strip} {$var|strip:"&nbsp;"}
* Date: September 25th, 2002 * Date: September 25th, 2002
* @link http://smarty.php.net/manual/en/language.modifier.strip.php *
* strip (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.strip.php strip (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @version 1.0 * @version 1.0
* @param string * @param string $
* @param string * @param string $
* @return string * @return string
*/ */
function smarty_modifier_strip($text, $replace = ' ') function smarty_modifier_strip($text, $replace = ' ')
{ {
return mb_ereg_replace('!\s+!', $replace, $text,'p'); $smarty = Smarty::instance();
} if ($smarty->has_mb) {
return mb_ereg_replace('!\s+!', $replace, $text, 'p');
} else {
return ereg_replace('!\s+!', $replace, $text, 'p');
}
}
?> ?>

View File

@@ -1,29 +1,36 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* @package Smarty *
* @subpackage PluginsModifier * @package Smarty
*/ * @subpackage PluginsModifier
*/
/** /**
* Smarty strip_tags modifier plugin * Smarty strip_tags modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: strip_tags<br> * Name: strip_tags<br>
* Purpose: strip html tags from text * Purpose: strip html tags from text
* @link http://smarty.php.net/manual/en/language.modifier.strip.tags.php *
* strip_tags (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.strip.tags.php strip_tags (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string * @param string $
* @param boolean * @param boolean $
* @return string * @return string
*/ */
function smarty_modifier_strip_tags($string, $replace_with_space = true) function smarty_modifier_strip_tags($string, $replace_with_space = true)
{ {
if ($replace_with_space) $smarty = Smarty::instance();
return mb_ereg_replace('!<[^>]*?>!', ' ', $string,'p'); if ($replace_with_space) {
else if ($smarty->has_mb) {
return mb_ereg_replace('!<[^>]*?>!', ' ', $string, 'p');
} else {
return ereg_replace('!<[^>]*?>!', ' ', $string, 'p');
}
} else {
return strip_tags($string); return strip_tags($string);
} }
}
?> ?>

View File

@@ -12,29 +12,30 @@
* Type: modifier<br> * Type: modifier<br>
* Name: truncate<br> * Name: truncate<br>
* Purpose: Truncate a string to a certain length if necessary, * Purpose: Truncate a string to a certain length if necessary,
* optionally splitting in the middle of a word, and * optionally splitting in the middle of a word, and
* appending the $etc string or inserting $etc into the middle. * appending the $etc string or inserting $etc into the middle.
* *
* @link http://smarty.php.net/manual/en/language.modifier.truncate.php truncate (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.truncate.php truncate (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string $string input string * @param string $string input string
* @param integer $length lenght of truncated text * @param integer $length lenght of truncated text
* @param string $etc end string * @param string $etc end string
* @param boolean $break_words truncate at word boundary * @param boolean $break_words truncate at word boundary
* @param boolean $middle truncate in the middle of text * @param boolean $middle truncate in the middle of text
* @return string truncated string * @return string truncated string
*/ */
function smarty_modifier_truncate($string, $length = 80, $etc = '...', function smarty_modifier_truncate($string, $length = 80, $etc = '...',
$break_words = false, $middle = false) $break_words = false, $middle = false)
{ {
if ($length == 0)
if ($length == 0) return '';
return '';
$smarty = Smarty::instance();
if ($smarty->has_mb) {
if (mb_strlen($string) > $length) { if (mb_strlen($string) > $length) {
$length -= min($length, mb_strlen($etc)); $length -= min($length, mb_strlen($etc));
if (!$break_words && !$middle) { if (!$break_words && !$middle) {
$string = mb_ereg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length + 1),'p'); $string = mb_ereg_replace('/\s+?(\S+)?$/', '', mb_substr($string, 0, $length + 1), 'p');
} }
if (!$middle) { if (!$middle) {
return mb_substr($string, 0, $length) . $etc; return mb_substr($string, 0, $length) . $etc;
@@ -44,5 +45,21 @@
} else { } else {
return $string; return $string;
} }
} else {
if (strlen($string) > $length) {
$length -= min($length, strlen($etc));
if (!$break_words && !$middle) {
$string = ereg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1), 'p');
}
if (!$middle) {
return substr($string, 0, $length) . $etc;
} else {
return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
}
} else {
return $string;
}
} }
}
?> ?>

View File

@@ -1,26 +1,31 @@
<?php <?php
/** /**
* Smarty plugin * Smarty plugin
* @package Smarty *
* @subpackage PluginsModifier * @package Smarty
*/ * @subpackage PluginsModifier
*/
/** /**
* Smarty upper modifier plugin * Smarty upper modifier plugin
* *
* Type: modifier<br> * Type: modifier<br>
* Name: upper<br> * Name: upper<br>
* Purpose: convert string to uppercase * Purpose: convert string to uppercase
* @link http://smarty.php.net/manual/en/language.modifier.upper.php *
* upper (Smarty online manual) * @link http://smarty.php.net/manual/en/language.modifier.upper.php upper (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com> * @author Monte Ohrt <monte at ohrt dot com>
* @param string * @param string $
* @return string * @return string
*/ */
function smarty_modifier_upper($string) function smarty_modifier_upper($string)
{ {
return mb_strtoupper($string); $smarty = Smarty::instance();
} if ($smarty->has_mb) {
return mb_strtoupper($string);
} else {
return strtoupper($string);
}
}
?> ?>

View File

@@ -55,8 +55,8 @@ abstract class Smarty_Internal_CompileBase
if ($this->optional_attributes != array('_any')) { if ($this->optional_attributes != array('_any')) {
$tmp_array = array_merge($this->required_attributes, $this->optional_attributes); $tmp_array = array_merge($this->required_attributes, $this->optional_attributes);
foreach ($args as $key => $dummy) { foreach ($args as $key => $dummy) {
if (!in_array($key, $tmp_array) && $key != 0) { if (!in_array($key, $tmp_array) && $key !== 0) {
$this->compiler->trigger_template_error("unexspected \"" . $key . "\" attribute"); $this->compiler->trigger_template_error("unexspected \"" . $key . "\" attribute");
} }
} }
} }