- 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
- check if mb string functions available otherwise fallback to normal string functions
- added global variable scope SMARTY_GLOBAL_SCOPE
- enable 'variable' filter by default
- fixed {$smarty.block.parent.foo}

View File

@@ -168,12 +168,17 @@ class Smarty extends Smarty_Internal_TemplateBase {
public $start_time = 0;
// set default time zone
public $set_timezone = true;
// has multibyte string functions?
public $has_mb = false;
/**
* Class constructor, initializes basic smarty properties
*/
public function __construct()
{
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")) {
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->loadPlugin('Smarty_Internal_Run_Filter');
$this->filter_handler = new Smarty_Internal_Run_Filter;
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']:'';
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':
// safe way to display e-mail address on a web page
if ($smarty->has_mb) {
return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
} else {
return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
}
case 'nonstd':
// escape non-standard chars, such as ms document quotes

View File

@@ -1,26 +1,31 @@
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty lower modifier plugin
*
* Type: modifier<br>
* Name: lower<br>
* 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>
* @param string
* @param string $
* @return string
*/
function smarty_modifier_lower($string)
{
$smarty = Smarty::instance();
if ($smarty->has_mb) {
return mb_strtolower($string);
} else {
return strtolower($string);
}
}
?>

View File

@@ -1,27 +1,29 @@
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty regex_replace modifier plugin
*
* Type: modifier<br>
* Name: regex_replace<br>
* 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>
* @param string
* @param string|array
* @param string|array
* @param string $
* @param string $ |array
* @param string $ |array
* @return string
*/
function smarty_modifier_regex_replace($string, $search, $replace)
{
$smarty = Smarty::instance();
if (is_array($search)) {
foreach($search as $idx => $s)
$search[$idx] = _smarty_regex_replace_check($s);
@@ -29,17 +31,30 @@ function smarty_modifier_regex_replace($string, $search, $replace)
$search = _smarty_regex_replace_check($search);
}
if ($smarty->has_mb) {
return mb_ereg_replace($search, $replace, $string);
} else {
return ereg_replace($search, $replace, $string);
}
}
function _smarty_regex_replace_check($search)
{
if ($smarty->has_mb) {
if (($pos = mb_strpos($search, "\0")) !== false)
$search = mb_substr($search, 0, $pos);
if (mb_preg_match('!([a-zA-Z\s]+)$!s', $search, $match) && (mb_strpos($match[1], 'e') !== false)) {
/* 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;
}

View File

@@ -38,7 +38,12 @@ function smarty_modifier_replace($string, $search, $replace)
return $haystack;
}
}
$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
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty spacify modifier plugin
*
* Type: modifier<br>
* Name: spacify<br>
* 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>
* @param string
* @param string
* @param string $
* @param string $
* @return string
*/
function smarty_modifier_spacify($string, $spacify_char = ' ')
{
return implode($spacify_char,
mb_split('//', $string, -1));
$smarty = Smarty::instance();
if ($smarty->has_mb) {
return implode($spacify_char, mb_split('//', $string, -1));
} else {
return implode($spacify_char, split('//', $string, -1));
}
}
?>

View File

@@ -1,11 +1,11 @@
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty strip modifier plugin
*
@@ -15,16 +15,22 @@
* with a single space or supplied replacement string.<br>
* Example: {$var|strip} {$var|strip:"&nbsp;"}
* 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>
* @version 1.0
* @param string
* @param string
* @param string $
* @param string $
* @return string
*/
function smarty_modifier_strip($text, $replace = ' ')
{
$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
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty strip_tags modifier plugin
*
* Type: modifier<br>
* Name: strip_tags<br>
* 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>
* @param string
* @param boolean
* @param string $
* @param boolean $
* @return string
*/
function smarty_modifier_strip_tags($string, $replace_with_space = true)
{
if ($replace_with_space)
$smarty = Smarty::instance();
if ($replace_with_space) {
if ($smarty->has_mb) {
return mb_ereg_replace('!<[^>]*?>!', ' ', $string, 'p');
else
} else {
return ereg_replace('!<[^>]*?>!', ' ', $string, 'p');
}
} else {
return strip_tags($string);
}
}
?>

View File

@@ -27,10 +27,11 @@
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
$break_words = false, $middle = false)
{
if ($length == 0)
return '';
$smarty = Smarty::instance();
if ($smarty->has_mb) {
if (mb_strlen($string) > $length) {
$length -= min($length, mb_strlen($etc));
if (!$break_words && !$middle) {
@@ -44,5 +45,21 @@
} else {
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
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsModifier
*/
/**
* Smarty upper modifier plugin
*
* Type: modifier<br>
* Name: upper<br>
* 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>
* @param string
* @param string $
* @return string
*/
function smarty_modifier_upper($string)
{
$smarty = Smarty::instance();
if ($smarty->has_mb) {
return mb_strtoupper($string);
} else {
return strtoupper($string);
}
}
?>

View File

@@ -55,7 +55,7 @@ abstract class Smarty_Internal_CompileBase
if ($this->optional_attributes != array('_any')) {
$tmp_array = array_merge($this->required_attributes, $this->optional_attributes);
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");
}
}