1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Templates;
13:
14: use Nette,
15: Nette\String,
16: Nette\Forms\Form,
17: Nette\Web\Html;
18:
19:
20:
21: 22: 23: 24: 25:
26: final class TemplateHelpers
27: {
28:
29:
30: public static $dateFormat = '%x';
31:
32: 33: 34:
35: final public function __construct()
36: {
37: throw new \LogicException("Cannot instantiate static class " . get_class($this));
38: }
39:
40:
41:
42: 43: 44: 45: 46:
47: public static function loader($helper)
48: {
49: $callback = callback('Nette\Templates\TemplateHelpers', $helper);
50: if ($callback->isCallable()) {
51: return $callback;
52: }
53: $callback = callback('Nette\String', $helper);
54: if ($callback->isCallable()) {
55: return $callback;
56: }
57: }
58:
59:
60:
61: 62: 63: 64: 65:
66: public static function escapeHtml($s)
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, ENT_QUOTES);
72: }
73:
74:
75:
76: 77: 78: 79: 80:
81: public static function escapeHtmlComment($s)
82: {
83: 84: return str_replace('--', '--><!-- ', $s); 85: }
86:
87:
88:
89: 90: 91: 92: 93:
94: public static function escapeXML($s)
95: {
96: 97: 98: 99: return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
100: }
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:
122: public static function escapeHtmlCss($s)
123: {
124: return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
125: }
126:
127:
128:
129: 130: 131: 132: 133:
134: public static function escapeJs($s)
135: {
136: if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
137: $s = $s->__toString(TRUE);
138: }
139: return str_replace(']]>', ']]\x3E', Nette\Json::encode($s));
140: }
141:
142:
143:
144: 145: 146: 147: 148:
149: public static function escapeHtmlJs($s)
150: {
151: return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
152: }
153:
154:
155:
156: 157: 158: 159: 160:
161: public static function strip($s)
162: {
163: return String::replace(
164: $s,
165: '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
166: function($m) {
167: return trim(preg_replace("#[ \t\r\n]+#", " ", $m[0]));
168: });
169: }
170:
171:
172:
173: 174: 175: 176: 177: 178: 179:
180: public static function indent($s, $level = 1, $chars = "\t")
181: {
182: if ($level >= 1) {
183: $s = String::replace($s, '#<(textarea|pre).*?</\\1#si', function($m) {
184: return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");
185: });
186: $s = String::indent($s, $level, $chars);
187: $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
188: }
189: return $s;
190: }
191:
192:
193:
194: 195: 196: 197: 198: 199:
200: public static function date($time, $format = NULL)
201: {
202: if ($time == NULL) { 203: return NULL;
204: }
205:
206: if (!isset($format)) {
207: $format = self::$dateFormat;
208: }
209:
210: $time = Nette\DateTime::from($time);
211: return strpos($format, '%') === FALSE
212: ? $time->format($format) 213: : strftime($format, $time->format('U')); 214: }
215:
216:
217:
218: 219: 220: 221: 222: 223:
224: public static function bytes($bytes, $precision = 2)
225: {
226: $bytes = round($bytes);
227: $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
228: foreach ($units as $unit) {
229: if (abs($bytes) < 1024 || $unit === end($units)) break;
230: $bytes = $bytes / 1024;
231: }
232: return round($bytes, $precision) . ' ' . $unit;
233: }
234:
235:
236:
237: 238: 239: 240: 241:
242: public static function length($var)
243: {
244: return is_string($var) ? String::length($var) : count($var);
245: }
246:
247:
248:
249: 250: 251: 252: 253: 254: 255:
256: public static function replace($subject, $search, $replacement = '')
257: {
258: return str_replace($search, $replacement, $subject);
259: }
260:
261:
262:
263: 264: 265: 266: 267: 268:
269: public static function dataStream($data, $type = NULL)
270: {
271: if ($type === NULL) {
272: $type = Nette\MimeTypeDetector::fromString($data, NULL);
273: }
274: return 'data:' . ($type ? "$type;" : '') . 'base64,' . base64_encode($data);
275: }
276:
277:
278:
279: 280: 281: 282: 283:
284: public static function null($value)
285: {
286: return '';
287: }
288:
289: }