1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: final class TemplateHelpers
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('TemplateHelpers', $helper);
44: if ($callback->isCallable()) {
45: return $callback;
46: }
47: $callback = callback('String', $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 Html || $s instanceof Form)) {
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: return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
93: }
94:
95:
96:
97: 98: 99: 100: 101:
102: public static function escapeCss($s)
103: {
104: 105: return addcslashes($s, "\x00..\x2C./:;<=>?@[\\]^`{|}~");
106: }
107:
108:
109:
110: 111: 112: 113: 114:
115: public static function escapeHtmlCss($s)
116: {
117: return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
118: }
119:
120:
121:
122: 123: 124: 125: 126:
127: public static function escapeJs($s)
128: {
129: if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
130: $s = $s->__toString(TRUE);
131: }
132: return str_replace(']]>', ']]\x3E', Json::encode($s));
133: }
134:
135:
136:
137: 138: 139: 140: 141:
142: public static function escapeHtmlJs($s)
143: {
144: return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
145: }
146:
147:
148:
149: 150: 151: 152: 153:
154: public static function strip($s)
155: {
156: return String::replace(
157: $s,
158: '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
159: callback(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:
173: public static function indent($s, $level = 1, $chars = "\t")
174: {
175: if ($level >= 1) {
176: $s = String::replace($s, '#<(textarea|pre).*?</\\1#si', callback(create_function('$m', '
177: return strtr($m[0], " \\t\\r\\n", "\\x1F\\x1E\\x1D\\x1A");
178: ')));
179: $s = String::indent($s, $level, $chars);
180: $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
181: }
182: return $s;
183: }
184:
185:
186:
187: 188: 189: 190: 191: 192:
193: public static function date($time, $format = NULL)
194: {
195: if ($time == NULL) { 196: return NULL;
197: }
198:
199: if (!isset($format)) {
200: $format = self::$dateFormat;
201: }
202:
203: $time = Tools::createDateTime($time);
204: return strpos($format, '%') === FALSE
205: ? $time->format($format) 206: : strftime($format, $time->format('U')); 207: }
208:
209:
210:
211: 212: 213: 214: 215: 216:
217: public static function bytes($bytes, $precision = 2)
218: {
219: $bytes = round($bytes);
220: $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
221: foreach ($units as $unit) {
222: if (abs($bytes) < 1024 || $unit === end($units)) break;
223: $bytes = $bytes / 1024;
224: }
225: return round($bytes, $precision) . ' ' . $unit;
226: }
227:
228:
229:
230: 231: 232: 233: 234:
235: public static function length($var)
236: {
237: return is_string($var) ? String::length($var) : count($var);
238: }
239:
240:
241:
242: 243: 244: 245: 246: 247: 248:
249: public static function replace($subject, $search, $replacement = '')
250: {
251: return str_replace($search, $replacement, $subject);
252: }
253:
254:
255:
256: 257: 258: 259: 260:
261: public static function null($value)
262: {
263: return '';
264: }
265:
266: }