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: private static $helpers = array(
24: 'normalize' => 'NStrings::normalize',
25: 'toascii' => 'NStrings::toAscii',
26: 'webalize' => 'NStrings::webalize',
27: 'truncate' => 'NStrings::truncate',
28: 'lower' => 'NStrings::lower',
29: 'upper' => 'NStrings::upper',
30: 'firstupper' => 'NStrings::firstUpper',
31: 'capitalize' => 'NStrings::capitalize',
32: 'trim' => 'NStrings::trim',
33: 'padleft' => 'NStrings::padLeft',
34: 'padright' => 'NStrings::padRight',
35: 'reverse' => 'NStrings::reverse',
36: 'replacere' => 'NStrings::replace',
37: 'url' => 'rawurlencode',
38: 'striptags' => 'strip_tags',
39: 'nl2br' => 'nl2br',
40: 'substr' => 'NStrings::substring',
41: 'repeat' => 'str_repeat',
42: 'implode' => 'implode',
43: 'number' => 'number_format',
44: );
45:
46:
47: public static $dateFormat = '%x';
48:
49:
50:
51: 52: 53: 54: 55:
56: public static function loader($helper)
57: {
58: if (method_exists(__CLASS__, $helper)) {
59: return new NCallback(__CLASS__, $helper);
60: } elseif (isset(self::$helpers[$helper])) {
61: return self::$helpers[$helper];
62: }
63: }
64:
65:
66:
67: 68: 69: 70: 71: 72:
73: public static function escapeHtml($s, $quotes = ENT_QUOTES)
74: {
75: if (is_object($s) && ($s instanceof ITemplate || $s instanceof NHtml || $s instanceof NForm)) {
76: return $s->__toString(TRUE);
77: }
78: return htmlSpecialChars($s, $quotes);
79: }
80:
81:
82:
83: 84: 85: 86: 87:
88: public static function ($s)
89: {
90:
91: return str_replace('--', '--><!-- ', $s);
92: }
93:
94:
95:
96: 97: 98: 99: 100:
101: public static function escapeXML($s)
102: {
103:
104:
105:
106: return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
107: }
108:
109:
110:
111: 112: 113: 114: 115:
116: public static function escapeCss($s)
117: {
118:
119: return addcslashes($s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
120: }
121:
122:
123:
124: 125: 126: 127: 128:
129: public static function escapeJs($s)
130: {
131: if (is_object($s) && ($s instanceof ITemplate || $s instanceof NHtml || $s instanceof NForm)) {
132: $s = $s->__toString(TRUE);
133: }
134: return str_replace(']]>', ']]\x3E', NJson::encode($s));
135: }
136:
137:
138:
139: 140: 141: 142: 143:
144: public static function escapeICal($s)
145: {
146:
147: return addcslashes(preg_replace('#[\x00-\x08\x0B\x0C-\x1F]+#', '', $s), "\";\\,:\n");
148: }
149:
150:
151:
152: 153: 154: 155: 156:
157: public static function strip($s)
158: {
159: return NStrings::replace(
160: $s,
161: '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|\z)#si',
162: new NCallback(create_function('$m', '
163: return trim(preg_replace(\'#[ \\t\\r\\n]+#\', " ", $m[0]));
164: ')));
165: }
166:
167:
168:
169: 170: 171: 172: 173: 174: 175:
176: public static function indent($s, $level = 1, $chars = "\t")
177: {
178: if ($level >= 1) {
179: $s = NStrings::replace($s, '#<(textarea|pre).*?</\\1#si', new NCallback(create_function('$m', '
180: return strtr($m[0], " \\t\\r\\n", "\\x1F\\x1E\\x1D\\x1A");
181: ')));
182: $s = NStrings::indent($s, $level, $chars);
183: $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
184: }
185: return $s;
186: }
187:
188:
189:
190: 191: 192: 193: 194: 195:
196: public static function date($time, $format = NULL)
197: {
198: if ($time == NULL) {
199: return NULL;
200: }
201:
202: if (!isset($format)) {
203: $format = self::$dateFormat;
204: }
205:
206: $time = NDateTime53::from($time);
207: return NStrings::contains($format, '%')
208: ? strftime($format, $time->format('U'))
209: : $time->format($format);
210: }
211:
212:
213:
214: 215: 216: 217: 218: 219:
220: public static function bytes($bytes, $precision = 2)
221: {
222: $bytes = round($bytes);
223: $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
224: foreach ($units as $unit) {
225: if (abs($bytes) < 1024 || $unit === end($units)) {
226: break;
227: }
228: $bytes = $bytes / 1024;
229: }
230: return round($bytes, $precision) . ' ' . $unit;
231: }
232:
233:
234:
235: 236: 237: 238: 239:
240: public static function length($var)
241: {
242: return is_string($var) ? NStrings::length($var) : count($var);
243: }
244:
245:
246:
247: 248: 249: 250: 251: 252: 253:
254: public static function replace($subject, $search, $replacement = '')
255: {
256: return str_replace($search, $replacement, $subject);
257: }
258:
259:
260:
261: 262: 263: 264: 265: 266:
267: public static function dataStream($data, $type = NULL)
268: {
269: if ($type === NULL) {
270: $type = NMimeTypeDetector::fromString($data);
271: }
272: return 'data:' . ($type ? "$type;" : '') . 'base64,' . base64_encode($data);
273: }
274:
275:
276:
277: 278: 279: 280: 281:
282: public static function null($value)
283: {
284: return '';
285: }
286:
287:
288:
289:
290:
291:
292:
293: 294: 295: 296: 297:
298: public static function optimizePhp($source, $lineLength = 80, $existenceOfThisParameterSolvesDamnBugInPHP535 = NULL)
299: {
300: $res = $php = '';
301: $lastChar = ';';
302: $tokens = new ArrayIterator(token_get_all($source));
303: foreach ($tokens as $key => $token) {
304: if (is_array($token)) {
305: if ($token[0] === T_INLINE_HTML) {
306: $lastChar = '';
307: $res .= $token[1];
308:
309: } elseif ($token[0] === T_CLOSE_TAG) {
310: $next = isset($tokens[$key + 1]) ? $tokens[$key + 1] : NULL;
311: if (substr($res, -1) !== '<' && preg_match('#^<\?php\s*\z#', $php)) {
312: $php = '';
313:
314: } elseif (is_array($next) && $next[0] === T_OPEN_TAG) {
315: if (!strspn($lastChar, ';{}:/')) {
316: $php .= $lastChar = ';';
317: }
318: if (substr($next[1], -1) === "\n") {
319: $php .= "\n";
320: }
321: $tokens->next();
322:
323: } elseif ($next) {
324: $res .= preg_replace('#;?(\s)*\z#', '$1', $php) . $token[1];
325: if (strlen($res) - strrpos($res, "\n") > $lineLength
326: && (!is_array($next) || strpos($next[1], "\n") === FALSE)
327: ) {
328: $res .= "\n";
329: }
330: $php = '';
331:
332: } else {
333: if (!strspn($lastChar, '};')) {
334: $php .= ';';
335: }
336: }
337:
338: } elseif ($token[0] === T_ELSE || $token[0] === T_ELSEIF) {
339: if ($tokens[$key + 1] === ':' && $lastChar === '}') {
340: $php .= ';';
341: }
342: $lastChar = '';
343: $php .= $token[1];
344:
345: } else {
346: if (!in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG))) {
347: $lastChar = '';
348: }
349: $php .= $token[1];
350: }
351: } else {
352: $php .= $lastChar = $token;
353: }
354: }
355: return $res . $php;
356: }
357:
358: }
359: