1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: final class NTemplateHelpers
22: {
23:
24: 25: 26:
27: final public function __construct()
28: {
29: throw new LogicException("Cannot instantiate static class " . get_class($this));
30: }
31:
32:
33:
34: 35: 36: 37: 38:
39: public static function loader($helper)
40: {
41: $callback = callback('NTemplateHelpers', $helper);
42: if ($callback->isCallable()) {
43: return $callback;
44: }
45: $callback = callback('NString', $helper);
46: if ($callback->isCallable()) {
47: return $callback;
48: }
49: }
50:
51:
52:
53: 54: 55: 56: 57:
58: public static function escapeHtml($s)
59: {
60: if (is_object($s) && ($s instanceof ITemplate || $s instanceof NHtml || $s instanceof NForm)) {
61: return $s->__toString(TRUE);
62: }
63: return htmlSpecialChars($s, ENT_QUOTES);
64: }
65:
66:
67:
68: 69: 70: 71: 72:
73: public static function escapeHtmlComment($s)
74: {
75:
76: return str_replace('--', '--><!-- ', $s);
77: }
78:
79:
80:
81: 82: 83: 84: 85:
86: public static function escapeXML($s)
87: {
88:
89:
90: return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
91: }
92:
93:
94:
95: 96: 97: 98: 99:
100: public static function escapeCss($s)
101: {
102:
103: return addcslashes($s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
104: }
105:
106:
107:
108: 109: 110: 111: 112:
113: public static function escapeHtmlCss($s)
114: {
115: return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
116: }
117:
118:
119:
120: 121: 122: 123: 124:
125: public static function escapeJs($s)
126: {
127: if (is_object($s) && ($s instanceof ITemplate || $s instanceof NHtml || $s instanceof NForm)) {
128: $s = $s->__toString(TRUE);
129: }
130: return str_replace(']]>', ']]\x3E', json_encode($s));
131: }
132:
133:
134:
135: 136: 137: 138: 139:
140: public static function escapeHtmlJs($s)
141: {
142: return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
143: }
144:
145:
146:
147: 148: 149: 150: 151:
152: public static function strip($s)
153: {
154: return preg_replace_callback(
155: '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
156: create_function('$m', 'return trim(preg_replace("#[ \t\r\n]+#", " ", $m[0]));'),
157: $s
158: );
159: }
160:
161:
162:
163: 164: 165: 166: 167: 168: 169:
170: public static function indent($s, $level = 1, $chars = "\t")
171: {
172: if ($level >= 1) {
173: $s = preg_replace_callback('#<(textarea|pre).*?</\\1#si', create_function('$m', 'return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");'), $s);
174: $s = NString::indent($s, $level, $chars);
175: $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
176: }
177: return $s;
178: }
179:
180:
181:
182: 183: 184: 185: 186: 187:
188: public static function date($time, $format = "%x")
189: {
190: if ($time == NULL) {
191: return NULL;
192: }
193:
194: $time = NDateTime53::from($time);
195: return strpos($format, '%') === FALSE
196: ? $time->format($format)
197: : strftime($format, $time->format('U'));
198: }
199:
200:
201:
202: 203: 204: 205: 206: 207:
208: public static function bytes($bytes, $precision = 2)
209: {
210: $bytes = round($bytes);
211: $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
212: foreach ($units as $unit) {
213: if (abs($bytes) < 1024 || $unit === end($units)) break;
214: $bytes = $bytes / 1024;
215: }
216: return round($bytes, $precision) . ' ' . $unit;
217: }
218:
219:
220:
221: 222: 223: 224: 225:
226: public static function length($var)
227: {
228: return is_string($var) ? iconv_strlen($var, 'UTF-8') : count($var);
229: }
230:
231:
232:
233: 234: 235: 236: 237: 238: 239:
240: public static function replace($subject, $search, $replacement = '')
241: {
242: return str_replace($search, $replacement, $subject);
243: }
244:
245:
246:
247: 248: 249: 250: 251: 252: 253:
254: public static function replaceRe($subject, $pattern, $replacement = '')
255: {
256: return preg_replace($pattern, $replacement, $subject);
257: }
258:
259:
260:
261: 262: 263: 264: 265:
266: public static function null($value)
267: {
268: return '';
269: }
270:
271: }
272: