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