1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette\Templates
11: */
12:
13:
14:
15: /**
16: * Standard template run-time helpers shipped with Nette Framework (https://nette.org)
17: *
18: * @author David Grudl
19: * @package Nette\Templates
20: */
21: final class NTemplateHelpers
22: {
23:
24: /**
25: * Static class - cannot be instantiated.
26: */
27: final public function __construct()
28: {
29: throw new LogicException("Cannot instantiate static class " . get_class($this));
30: }
31:
32:
33:
34: /**
35: * Try to load the requested helper.
36: * @param string helper name
37: * @return callback
38: */
39: public static function loader($helper)
40: {
41: $callback = callback('NTemplateHelpers', $helper);
42: if ($callback->isCallable()) {
43: return $callback;
44: }
45: $callback = callback('NString', $helper);
46: if ($callback->isCallable()) {
47: return $callback;
48: }
49: }
50:
51:
52:
53: /**
54: * Escapes string for use inside HTML template.
55: * @param mixed UTF-8 encoding or 8-bit
56: * @return string
57: */
58: public static function escapeHtml($s)
59: {
60: if (is_object($s) && ($s instanceof ITemplate || $s instanceof NHtml || $s instanceof NForm)) {
61: return $s->__toString(TRUE);
62: }
63: return htmlSpecialChars($s, ENT_QUOTES);
64: }
65:
66:
67:
68: /**
69: * Escapes string for use inside HTML comments.
70: * @param mixed UTF-8 encoding or 8-bit
71: * @return string
72: */
73: public static function escapeHtmlComment($s)
74: {
75: // -- has special meaning in different browsers
76: return str_replace('--', '--><!-- ', $s); // HTML tags have no meaning inside comments
77: }
78:
79:
80:
81: /**
82: * Escapes string for use inside XML 1.0 template.
83: * @param string UTF-8 encoding or 8-bit
84: * @return string
85: */
86: public static function escapeXML($s)
87: {
88: // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
89: // XML 1.1: \x00 forbidden directly and as a character reference, \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
90: return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
91: }
92:
93:
94:
95: /**
96: * Escapes string for use inside CSS template.
97: * @param string UTF-8 encoding or 8-bit
98: * @return string
99: */
100: public static function escapeCss($s)
101: {
102: // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
103: return addcslashes($s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
104: }
105:
106:
107:
108: /**
109: * Escapes string for use inside HTML style attribute.
110: * @param string UTF-8 encoding or 8-bit
111: * @return string
112: */
113: public static function escapeHtmlCss($s)
114: {
115: return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
116: }
117:
118:
119:
120: /**
121: * Escapes string for use inside JavaScript template.
122: * @param mixed UTF-8 encoding
123: * @return string
124: */
125: public static function escapeJs($s)
126: {
127: if (is_object($s) && ($s instanceof ITemplate || $s instanceof NHtml || $s instanceof NForm)) {
128: $s = $s->__toString(TRUE);
129: }
130: return str_replace(']]>', ']]\x3E', json_encode($s));
131: }
132:
133:
134:
135: /**
136: * Escapes string for use inside HTML JavaScript attribute.
137: * @param mixed UTF-8 encoding
138: * @return string
139: */
140: public static function escapeHtmlJs($s)
141: {
142: return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
143: }
144:
145:
146:
147: /**
148: * Replaces all repeated white spaces with a single space.
149: * @param string UTF-8 encoding or 8-bit
150: * @return string
151: */
152: public static function strip($s)
153: {
154: return preg_replace_callback(
155: '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
156: create_function('$m', 'return trim(preg_replace("#[ \t\r\n]+#", " ", $m[0]));'),
157: $s
158: );
159: }
160:
161:
162:
163: /**
164: * Indents the HTML content from the left.
165: * @param string UTF-8 encoding or 8-bit
166: * @param int
167: * @param string
168: * @return string
169: */
170: public static function indent($s, $level = 1, $chars = "\t")
171: {
172: if ($level >= 1) {
173: $s = preg_replace_callback('#<(textarea|pre).*?</\\1#si', create_function('$m', 'return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");'), $s);
174: $s = NString::indent($s, $level, $chars);
175: $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
176: }
177: return $s;
178: }
179:
180:
181:
182: /**
183: * Date/time formatting.
184: * @param string|int|DateTime
185: * @param string
186: * @return string
187: */
188: public static function date($time, $format = "%x")
189: {
190: if ($time == NULL) { // intentionally ==
191: return NULL;
192: }
193:
194: $time = NDateTime53::from($time);
195: return strpos($format, '%') === FALSE
196: ? $time->format($format) // formats using date()
197: : strftime($format, $time->format('U')); // formats according to locales
198: }
199:
200:
201:
202: /**
203: * Converts to human readable file size.
204: * @param int
205: * @param int
206: * @return string
207: */
208: public static function bytes($bytes, $precision = 2)
209: {
210: $bytes = round($bytes);
211: $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
212: foreach ($units as $unit) {
213: if (abs($bytes) < 1024 || $unit === end($units)) break;
214: $bytes = $bytes / 1024;
215: }
216: return round($bytes, $precision) . ' ' . $unit;
217: }
218:
219:
220:
221: /**
222: * Returns array of string length.
223: * @param mixed
224: * @return int
225: */
226: public static function length($var)
227: {
228: return is_string($var) ? iconv_strlen($var, 'UTF-8') : count($var);
229: }
230:
231:
232:
233: /**
234: * Performs a search and replace.
235: * @param string
236: * @param string
237: * @param string
238: * @return string
239: */
240: public static function replace($subject, $search, $replacement = '')
241: {
242: return str_replace($search, $replacement, $subject);
243: }
244:
245:
246:
247: /**
248: * Performs a regular expression search and replace.
249: * @param string
250: * @param string
251: * @param string
252: * @return string
253: */
254: public static function replaceRe($subject, $pattern, $replacement = '')
255: {
256: return preg_replace($pattern, $replacement, $subject);
257: }
258:
259:
260:
261: /**
262: * /dev/null.
263: * @param mixed
264: * @return string
265: */
266: public static function null($value)
267: {
268: return '';
269: }
270:
271: }
272: