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