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