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