init
[garradin.git] / include / libs / template_lite / plugins / modifier.escape.php
1 <?php
2 /**
3 * Template Lite plugin converted from Smarty
4 * @package Smarty
5 * @subpackage plugins
6 */
7
8
9
10 /**
11 * Smarty escape modifier plugin
12 *
13 * Type: modifier<br>
14 * Name: escape<br>
15 * Purpose: Escape the string according to escapement type
16 * @link http://smarty.php.net/manual/en/language.modifier.escape.php
17 * escape (Smarty online manual)
18 * @author Monte Ohrt <monte at ohrt dot com>
19 * @param string
20 * @param html|htmlall|url|quotes|hex|hexentity|javascript
21 * @return string
22 */
23 function tpl_modifier_escape($string, $esc_type = 'html', $char_set = 'ISO-8859-1', $double_encode = true)
24 {
25 switch ($esc_type)
26 {
27 case 'html':
28 if (version_compare(PHP_VERSION, '5.2.3') === 1)
29 return htmlspecialchars($string, ENT_QUOTES, $char_set, $double_encode);
30 else
31 return htmlspecialchars($string, ENT_QUOTES, $char_set);
32
33 case 'htmlall':
34 if (version_compare(PHP_VERSION, '5.2.3') === 1)
35 return htmlentities($string, ENT_QUOTES, $char_set, $double_encode);
36 else
37 return htmlentities($string, ENT_QUOTES, $char_set);
38
39 case 'url':
40 return rawurlencode($string);
41
42 case 'urlpathinfo':
43 return str_replace('%2F','/',rawurlencode($string));
44
45 case 'quotes':
46 // escape unescaped single quotes
47 return preg_replace("%(?<!\\\\)'%", "\\'", $string);
48
49 case 'hex':
50 // escape every character into hex
51 $return = '';
52 for ($x=0; $x < strlen($string); $x++) {
53 $return .= '%' . bin2hex($string[$x]);
54 }
55 return $return;
56
57 case 'hexentity':
58 $return = '';
59 for ($x=0; $x < strlen($string); $x++) {
60 $return .= '&#x' . bin2hex($string[$x]) . ';';
61 }
62 return $return;
63
64 case 'decentity':
65 $return = '';
66 for ($x=0; $x < strlen($string); $x++) {
67 $return .= '&#' . ord($string[$x]) . ';';
68 }
69 return $return;
70
71 case 'javascript':
72 // escape quotes and backslashes, newlines, etc.
73 return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
74
75 case 'mail':
76 // safe way to display e-mail address on a web page
77 return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string);
78
79 case 'nonstd':
80 // escape non-standard chars, such as ms document quotes
81 $_res = '';
82 for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++)
83 {
84 $_ord = ord(substr($string, $_i, 1));
85 // non-standard char, escape it
86 if($_ord >= 126)
87 {
88 $_res .= '&#' . $_ord . ';';
89 }
90 else
91 {
92 $_res .= substr($string, $_i, 1);
93 }
94 }
95 return $_res;
96
97 default:
98 return $string;
99 }
100 }
101
102 ?>