fusionpbx/resources/templates/engine/smarty/plugins/modifier.escape.php

190 lines
6.7 KiB
PHP
Raw Normal View History

2013-11-09 21:02:56 +01:00
<?php
/**
* Smarty plugin
*
2018-11-07 08:18:14 +01:00
* @package Smarty
2013-11-09 21:02:56 +01:00
* @subpackage PluginsModifier
*/
/**
* Smarty escape modifier plugin
2018-11-07 08:18:14 +01:00
* Type: modifier
* Name: escape
2013-11-09 21:02:56 +01:00
* Purpose: escape string for output
*
2023-05-08 17:50:42 +02:00
* @link https://www.smarty.net/docs/en/language.modifier.escape
2013-11-09 21:02:56 +01:00
* @author Monte Ohrt <monte at ohrt dot com>
2018-11-07 08:18:14 +01:00
*
2013-11-09 21:02:56 +01: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()
2018-11-07 08:18:14 +01:00
*
2013-11-09 21:02:56 +01:00
* @return string escaped input string
*/
function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true)
{
2018-11-07 08:18:14 +01:00
static $is_loaded_1 = false;
static $is_loaded_2 = false;
2013-11-09 21:02:56 +01:00
if (!$char_set) {
$char_set = Smarty::$_CHARSET;
}
2023-05-08 17:50:42 +02:00
$string = (string)$string;
2013-11-09 21:02:56 +01:00
switch ($esc_type) {
case 'html':
2023-05-08 17:50:42 +02:00
return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
2018-11-07 08:18:14 +01:00
// no break
2013-11-09 21:02:56 +01:00
case 'htmlall':
if (Smarty::$_MBSTRING) {
2023-05-08 17:50:42 +02:00
$string = mb_convert_encoding($string, 'UTF-8', $char_set);
return htmlentities($string, ENT_QUOTES, 'UTF-8', $double_encode);
2013-11-09 21:02:56 +01:00
}
// no MBString fallback
2023-05-08 17:50:42 +02:00
return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
2018-11-07 08:18:14 +01:00
// no break
2013-11-09 21:02:56 +01: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
$return = '';
$_length = strlen($string);
for ($x = 0; $x < $_length; $x++) {
2018-11-07 08:18:14 +01:00
$return .= '%' . bin2hex($string[ $x ]);
2013-11-09 21:02:56 +01:00
}
return $return;
case 'hexentity':
$return = '';
if (Smarty::$_MBSTRING) {
2018-11-07 08:18:14 +01:00
if (!$is_loaded_1) {
if (!is_callable('smarty_mb_to_unicode')) {
include_once SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php';
}
$is_loaded_1 = true;
}
2013-11-09 21:02:56 +01:00
$return = '';
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
$return .= '&#x' . strtoupper(dechex($unicode)) . ';';
}
return $return;
}
// no MBString fallback
$_length = strlen($string);
for ($x = 0; $x < $_length; $x++) {
2018-11-07 08:18:14 +01:00
$return .= '&#x' . bin2hex($string[ $x ]) . ';';
2013-11-09 21:02:56 +01:00
}
return $return;
case 'decentity':
$return = '';
if (Smarty::$_MBSTRING) {
2018-11-07 08:18:14 +01:00
if (!$is_loaded_1) {
if (!is_callable('smarty_mb_to_unicode')) {
include_once SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php';
}
$is_loaded_1 = true;
}
2013-11-09 21:02:56 +01:00
$return = '';
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
$return .= '&#' . $unicode . ';';
}
return $return;
}
// no MBString fallback
$_length = strlen($string);
for ($x = 0; $x < $_length; $x++) {
2018-11-07 08:18:14 +01:00
$return .= '&#' . ord($string[ $x ]) . ';';
2013-11-09 21:02:56 +01:00
}
return $return;
case 'javascript':
// escape quotes and backslashes, newlines, etc.
2018-11-07 08:18:14 +01:00
return strtr(
$string,
array(
'\\' => '\\\\',
"'" => "\\'",
'"' => '\\"',
"\r" => '\\r',
"\n" => '\\n',
2023-05-08 17:50:42 +02:00
'</' => '<\/',
// see https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
'<!--' => '<\!--',
'<s' => '<\s',
'<S' => '<\S',
"`" => "\\\\`",
"\${" => "\\\\\\$\\{"
2018-11-07 08:18:14 +01:00
)
);
2013-11-09 21:02:56 +01:00
case 'mail':
if (Smarty::$_MBSTRING) {
2018-11-07 08:18:14 +01:00
if (!$is_loaded_2) {
if (!is_callable('smarty_mb_str_replace')) {
include_once SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php';
}
$is_loaded_2 = true;
}
return smarty_mb_str_replace(
array(
'@',
'.'
),
array(
' [AT] ',
' [DOT] '
),
$string
);
2013-11-09 21:02:56 +01:00
}
// no MBString fallback
2018-11-07 08:18:14 +01:00
return str_replace(
array(
'@',
'.'
),
array(
' [AT] ',
' [DOT] '
),
$string
);
2013-11-09 21:02:56 +01:00
case 'nonstd':
// escape non-standard chars, such as ms document quotes
$return = '';
if (Smarty::$_MBSTRING) {
2018-11-07 08:18:14 +01:00
if (!$is_loaded_1) {
if (!is_callable('smarty_mb_to_unicode')) {
include_once SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php';
}
$is_loaded_1 = true;
}
2013-11-09 21:02:56 +01:00
foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) {
if ($unicode >= 126) {
$return .= '&#' . $unicode . ';';
} else {
$return .= chr($unicode);
}
}
return $return;
}
$_length = strlen($string);
for ($_i = 0; $_i < $_length; $_i++) {
$_ord = ord(substr($string, $_i, 1));
// non-standard char, escape it
if ($_ord >= 126) {
$return .= '&#' . $_ord . ';';
} else {
$return .= substr($string, $_i, 1);
}
}
return $return;
default:
2023-05-08 17:50:42 +02:00
trigger_error("escape: unsupported type: $esc_type - returning unmodified string", E_USER_NOTICE);
2013-11-09 21:02:56 +01:00
return $string;
}
}