2009-03-22 16:09:05 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2010-07-06 20:00:41 +00:00
|
|
|
* Smarty plugin
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2010-07-06 20:00:41 +00:00
|
|
|
* @subpackage PluginsModifier
|
|
|
|
|
*/
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
2010-07-06 20:00:41 +00:00
|
|
|
* Smarty escape modifier plugin
|
2017-11-11 07:11:33 +01:00
|
|
|
* Type: modifier
|
|
|
|
|
* Name: escape
|
2010-07-06 20:00:41 +00:00
|
|
|
* Purpose: escape string for output
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2021-10-13 12:15:17 +02:00
|
|
|
* @link https://www.smarty.net/docs/en/language.modifier.escape
|
2011-09-16 14:19:56 +00:00
|
|
|
* @author Monte Ohrt <monte at ohrt dot com>
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2011-09-16 14:19:56 +00:00
|
|
|
* @param string $string input string
|
|
|
|
|
* @param string $esc_type escape type
|
|
|
|
|
* @param string $char_set character set, used for htmlspecialchars() or htmlentities()
|
|
|
|
|
* @param boolean $double_encode encode already encoded entitites again, used for htmlspecialchars() or htmlentities()
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2010-07-06 20:00:41 +00:00
|
|
|
* @return string escaped input string
|
|
|
|
|
*/
|
2011-09-16 14:19:56 +00:00
|
|
|
function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true)
|
2009-03-22 16:09:05 +00:00
|
|
|
{
|
2011-09-16 14:19:56 +00:00
|
|
|
if (!$char_set) {
|
2022-11-30 00:25:27 +01:00
|
|
|
$char_set = \Smarty\Smarty::$_CHARSET;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2022-02-08 13:30:19 +01:00
|
|
|
|
|
|
|
|
$string = (string)$string;
|
|
|
|
|
|
2009-03-22 16:09:05 +00:00
|
|
|
switch ($esc_type) {
|
2018-08-19 02:35:46 +02:00
|
|
|
case 'html':
|
2022-11-22 21:22:57 +01:00
|
|
|
return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
|
2018-08-31 16:45:09 +02:00
|
|
|
// no break
|
2018-08-19 02:35:46 +02:00
|
|
|
case 'htmlall':
|
2022-12-01 21:56:12 +01:00
|
|
|
$string = mb_convert_encoding($string, 'UTF-8', $char_set);
|
|
|
|
|
return htmlentities($string, ENT_QUOTES, 'UTF-8', $double_encode);
|
2018-08-31 16:45:09 +02:00
|
|
|
// no break
|
2018-08-19 02:35:46 +02:00
|
|
|
case 'url':
|
|
|
|
|
return rawurlencode($string);
|
|
|
|
|
case 'urlpathinfo':
|
|
|
|
|
return str_replace('%2F', '/', rawurlencode($string));
|
|
|
|
|
case 'quotes':
|
|
|
|
|
// escape unescaped single quotes
|
|
|
|
|
return preg_replace("%(?<!\\\\)'%", "\\'", $string);
|
|
|
|
|
case 'hex':
|
|
|
|
|
// escape every byte into hex
|
|
|
|
|
// Note that the UTF-8 encoded character ä will be represented as %c3%a4
|
2009-03-22 16:09:05 +00:00
|
|
|
$return = '';
|
2018-08-19 02:35:46 +02:00
|
|
|
$_length = strlen($string);
|
|
|
|
|
for ($x = 0; $x < $_length; $x++) {
|
|
|
|
|
$return .= '%' . bin2hex($string[ $x ]);
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2009-03-22 16:09:05 +00:00
|
|
|
return $return;
|
2018-08-19 02:35:46 +02:00
|
|
|
case 'hexentity':
|
|
|
|
|
$return = '';
|
2022-12-01 21:56:12 +01:00
|
|
|
foreach (smarty_mb_to_unicode($string, \Smarty\Smarty::$_CHARSET) as $unicode) {
|
|
|
|
|
$return .= '&#x' . strtoupper(dechex($unicode)) . ';';
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2018-08-19 02:35:46 +02:00
|
|
|
return $return;
|
|
|
|
|
case 'decentity':
|
2009-03-22 16:09:05 +00:00
|
|
|
$return = '';
|
2022-12-01 21:56:12 +01:00
|
|
|
foreach (smarty_mb_to_unicode($string, \Smarty\Smarty::$_CHARSET) as $unicode) {
|
|
|
|
|
$return .= '&#' . $unicode . ';';
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2009-03-22 16:09:05 +00:00
|
|
|
return $return;
|
2018-08-19 02:35:46 +02:00
|
|
|
case 'javascript':
|
|
|
|
|
// escape quotes and backslashes, newlines, etc.
|
|
|
|
|
return strtr(
|
|
|
|
|
$string,
|
2018-08-31 16:45:09 +02:00
|
|
|
array(
|
|
|
|
|
'\\' => '\\\\',
|
|
|
|
|
"'" => "\\'",
|
|
|
|
|
'"' => '\\"',
|
|
|
|
|
"\r" => '\\r',
|
|
|
|
|
"\n" => '\\n',
|
2021-02-28 16:43:54 +03:00
|
|
|
'</' => '<\/',
|
|
|
|
|
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
|
|
|
|
|
'<!--' => '<\!--',
|
|
|
|
|
'<s' => '<\s',
|
|
|
|
|
'<S' => '<\S'
|
2018-08-31 16:45:09 +02:00
|
|
|
)
|
2018-08-19 02:35:46 +02:00
|
|
|
);
|
|
|
|
|
case 'mail':
|
2022-12-01 21:56:12 +01:00
|
|
|
return smarty_mb_str_replace(
|
2018-08-31 16:45:09 +02:00
|
|
|
array(
|
|
|
|
|
'@',
|
|
|
|
|
'.'
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
' [AT] ',
|
|
|
|
|
' [DOT] '
|
|
|
|
|
),
|
2018-08-19 02:35:46 +02:00
|
|
|
$string
|
2018-06-12 09:58:15 +02:00
|
|
|
);
|
2018-08-19 02:35:46 +02:00
|
|
|
case 'nonstd':
|
|
|
|
|
// escape non-standard chars, such as ms document quotes
|
|
|
|
|
$return = '';
|
2022-12-01 21:56:12 +01:00
|
|
|
foreach (smarty_mb_to_unicode($string, \Smarty\Smarty::$_CHARSET) as $unicode) {
|
|
|
|
|
if ($unicode >= 126) {
|
|
|
|
|
$return .= '&#' . $unicode . ';';
|
2009-03-22 16:09:05 +00:00
|
|
|
} else {
|
2022-12-01 21:56:12 +01:00
|
|
|
$return .= chr($unicode);
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $return;
|
2018-08-19 02:35:46 +02:00
|
|
|
default:
|
2021-03-21 20:21:55 +00:00
|
|
|
trigger_error("escape: unsupported type: $esc_type - returning unmodified string", E_USER_NOTICE);
|
2018-08-19 02:35:46 +02:00
|
|
|
return $string;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
|
}
|