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