| 
									
										
										
										
											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
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2014-07-01 19:33:25 +00:00
										 |  |  |  * @link   http://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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |     static $_double_encode = null; | 
					
						
							| 
									
										
										
										
											2018-03-28 07:35:52 +02:00
										 |  |  |     static $is_loaded_1 = false; | 
					
						
							|  |  |  |     static $is_loaded_2 = false; | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |     if ($_double_encode === null) { | 
					
						
							|  |  |  |         $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>='); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     if (!$char_set) { | 
					
						
							| 
									
										
										
										
											2011-12-18 18:20:09 +00:00
										 |  |  |         $char_set = Smarty::$_CHARSET; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     switch ($esc_type) { | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |         case 'html': | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |             if ($_double_encode) { | 
					
						
							|  |  |  |                 // php >=5.3.2 - go native
 | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                 return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode); | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 if ($double_encode) { | 
					
						
							| 
									
										
										
										
											2012-08-14 13:18:24 +00:00
										 |  |  |                     // php <5.2.3 - only handle double encoding
 | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                     return htmlspecialchars($string, ENT_QUOTES, $char_set); | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2012-08-14 13:18:24 +00:00
										 |  |  |                     // php <5.2.3 - prevent double encoding
 | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |                     $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string); | 
					
						
							|  |  |  |                     $string = htmlspecialchars($string, ENT_QUOTES, $char_set); | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                     $string = str_replace( | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |                         array( | 
					
						
							|  |  |  |                             '%%%SMARTY_START%%%', | 
					
						
							|  |  |  |                             '%%%SMARTY_END%%%' | 
					
						
							|  |  |  |                         ), | 
					
						
							|  |  |  |                         array( | 
					
						
							|  |  |  |                             '&', | 
					
						
							|  |  |  |                             ';' | 
					
						
							|  |  |  |                         ), | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                         $string | 
					
						
							|  |  |  |                     ); | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |                     return $string; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |         // no break
 | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |         case 'htmlall': | 
					
						
							|  |  |  |             if (Smarty::$_MBSTRING) { | 
					
						
							|  |  |  |                 // mb_convert_encoding ignores htmlspecialchars()
 | 
					
						
							|  |  |  |                 if ($_double_encode) { | 
					
						
							|  |  |  |                     // php >=5.3.2 - go native
 | 
					
						
							|  |  |  |                     $string = htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     if ($double_encode) { | 
					
						
							|  |  |  |                         // php <5.2.3 - only handle double encoding
 | 
					
						
							|  |  |  |                         $string = htmlspecialchars($string, ENT_QUOTES, $char_set); | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         // php <5.2.3 - prevent double encoding
 | 
					
						
							|  |  |  |                         $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string); | 
					
						
							|  |  |  |                         $string = htmlspecialchars($string, ENT_QUOTES, $char_set); | 
					
						
							|  |  |  |                         $string = | 
					
						
							|  |  |  |                             str_replace( | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |                                 array( | 
					
						
							|  |  |  |                                     '%%%SMARTY_START%%%', | 
					
						
							|  |  |  |                                     '%%%SMARTY_END%%%' | 
					
						
							|  |  |  |                                 ), | 
					
						
							|  |  |  |                                 array( | 
					
						
							|  |  |  |                                     '&', | 
					
						
							|  |  |  |                                     ';' | 
					
						
							|  |  |  |                                 ), | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                                 $string | 
					
						
							|  |  |  |                             ); | 
					
						
							|  |  |  |                         return $string; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // htmlentities() won't convert everything, so use mb_convert_encoding
 | 
					
						
							|  |  |  |                 return mb_convert_encoding($string, 'HTML-ENTITIES', $char_set); | 
					
						
							| 
									
										
										
										
											2018-06-12 09:58:15 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |             // no MBString fallback
 | 
					
						
							|  |  |  |             if ($_double_encode) { | 
					
						
							|  |  |  |                 return htmlentities($string, ENT_QUOTES, $char_set, $double_encode); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 if ($double_encode) { | 
					
						
							|  |  |  |                     return htmlentities($string, ENT_QUOTES, $char_set); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string); | 
					
						
							|  |  |  |                     $string = htmlentities($string, ENT_QUOTES, $char_set); | 
					
						
							|  |  |  |                     $string = str_replace( | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |                         array( | 
					
						
							|  |  |  |                             '%%%SMARTY_START%%%', | 
					
						
							|  |  |  |                             '%%%SMARTY_END%%%' | 
					
						
							|  |  |  |                         ), | 
					
						
							|  |  |  |                         array( | 
					
						
							|  |  |  |                             '&', | 
					
						
							|  |  |  |                             ';' | 
					
						
							|  |  |  |                         ), | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                         $string | 
					
						
							|  |  |  |                     ); | 
					
						
							|  |  |  |                     return $string; | 
					
						
							| 
									
										
										
										
											2012-06-24 18:15:58 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											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 = ''; | 
					
						
							|  |  |  |             if (Smarty::$_MBSTRING) { | 
					
						
							|  |  |  |                 if (!$is_loaded_1) { | 
					
						
							|  |  |  |                     if (!is_callable('smarty_mb_to_unicode')) { | 
					
						
							|  |  |  |                         include_once SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     $is_loaded_1 = true; | 
					
						
							| 
									
										
										
										
											2016-09-02 01:08:50 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02: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++) { | 
					
						
							|  |  |  |                 $return .= '&#x' . bin2hex($string[ $x ]) . ';'; | 
					
						
							| 
									
										
										
										
											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 = ''; | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |             if (Smarty::$_MBSTRING) { | 
					
						
							|  |  |  |                 if (!$is_loaded_1) { | 
					
						
							|  |  |  |                     if (!is_callable('smarty_mb_to_unicode')) { | 
					
						
							|  |  |  |                         include_once SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     $is_loaded_1 = true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 $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++) { | 
					
						
							|  |  |  |                 $return .= '&#' . ord($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 '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', | 
					
						
							| 
									
										
										
										
											2023-03-24 12:19:34 +01:00
										 |  |  |                     '<S'   => '<\S', | 
					
						
							|  |  |  | 	                "`" => "\\\\`", | 
					
						
							|  |  |  | 	                "\${" => "\\\\\\$\\{" | 
					
						
							| 
									
										
										
										
											2018-08-31 16:45:09 +02:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |             ); | 
					
						
							|  |  |  |         case 'mail': | 
					
						
							|  |  |  |             if (Smarty::$_MBSTRING) { | 
					
						
							|  |  |  |                 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; | 
					
						
							| 
									
										
										
										
											2016-09-02 01:08:50 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02: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 | 
					
						
							|  |  |  |                 ); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |             // no MBString fallback
 | 
					
						
							|  |  |  |             return 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 = ''; | 
					
						
							|  |  |  |             if (Smarty::$_MBSTRING) { | 
					
						
							|  |  |  |                 if (!$is_loaded_1) { | 
					
						
							|  |  |  |                     if (!is_callable('smarty_mb_to_unicode')) { | 
					
						
							|  |  |  |                         include_once SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     $is_loaded_1 = true; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) { | 
					
						
							|  |  |  |                     if ($unicode >= 126) { | 
					
						
							|  |  |  |                         $return .= '&#' . $unicode . ';'; | 
					
						
							|  |  |  |                     } else { | 
					
						
							|  |  |  |                         $return .= chr($unicode); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2016-09-02 01:08:50 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                 return $return; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |             $_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 . ';'; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2018-08-19 02:35:46 +02:00
										 |  |  |                     $return .= substr($string, $_i, 1); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | } |