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 compile-time filters shipped with Nette Framework (https://nette.org)
17: *
18: * @author David Grudl
19: * @package Nette\Templates
20: */
21: final class NTemplateFilters
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: /********************* Filter removePhp ****************d*g**/
35:
36:
37:
38: /**
39: * Filters out PHP code.
40: * @param string
41: * @return string
42: */
43: public static function removePhp($s)
44: {
45: return preg_replace('#\x01@php:p\d+@\x02#', '<?php ?>', $s); // Template hides PHP code in these snippets
46: }
47:
48:
49:
50: /********************* Filter relativeLinks ****************d*g**/
51:
52:
53:
54: /**
55: * Filter relativeLinks: prepends root to relative links.
56: * @param string
57: * @return string
58: */
59: public static function relativeLinks($s)
60: {
61: return preg_replace(
62: '#(src|href|action)\s*=\s*(["\'])(?![a-z]+:|[\x01/\\#])#', // \x01 is PHP snippet
63: '$1=$2<?php echo \\$baseUri ?>',
64: $s
65: );
66: }
67:
68:
69:
70: /********************* Filter netteLinks ****************d*g**/
71:
72:
73:
74: /**
75: * Filter netteLinks: translates links "nette:...".
76: * nette:destination?arg
77: * @param string
78: * @return string
79: */
80: public static function netteLinks($s)
81: {
82: return preg_replace_callback(
83: '#(src|href|action)\s*=\s*(["\'])(nette:.*?)([\#"\'])#',
84: array(__CLASS__, 'netteLinksCb'),
85: $s
86: );
87: }
88:
89:
90:
91: /**
92: * Callback for self::netteLinks.
93: * Parses a "nette" URI (scheme is 'nette') and converts to real URI
94: */
95: private static function netteLinksCb($m)
96: {
97: list(, $attr, $quote, $uri, $fragment) = $m;
98:
99: $parts = parse_url($uri);
100: if (isset($parts['scheme']) && $parts['scheme'] === 'nette') {
101: return $attr . '=' . $quote . '<?php echo $template->escape($control->'
102: . "link('"
103: . (isset($parts['path']) ? $parts['path'] : 'this!')
104: . (isset($parts['query']) ? '?' . $parts['query'] : '')
105: . '\'))?>'
106: . $fragment;
107: } else {
108: return $m[0];
109: }
110: }
111:
112:
113:
114: /********************* Filter texyElements ****************d*g**/
115:
116:
117:
118: /** @var NTexy */
119: public static $texy;
120:
121:
122:
123: /**
124: * Process <texy>...</texy> elements.
125: * @param string
126: * @return string
127: */
128: public static function texyElements($s)
129: {
130: return preg_replace_callback(
131: '#<texy([^>]*)>(.*?)</texy>#s',
132: array(__CLASS__, 'texyCb'),
133: $s
134: );
135: }
136:
137:
138:
139: /**
140: * Callback for self::texyBlocks.
141: */
142: private static function texyCb($m)
143: {
144: list(, $mAttrs, $mContent) = $m;
145:
146: // parse attributes
147: $attrs = array();
148: if ($mAttrs) {
149: preg_match_all(
150: '#([a-z0-9:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
151: $mAttrs,
152: $arr,
153: PREG_SET_ORDER
154: );
155:
156: foreach ($arr as $m) {
157: $key = strtolower($m[1]);
158: $val = $m[2];
159: if ($val == NULL) $attrs[$key] = TRUE;
160: elseif ($val{0} === '\'' || $val{0} === '"') $attrs[$key] = html_entity_decode(substr($val, 1, -1), ENT_QUOTES, 'UTF-8');
161: else $attrs[$key] = html_entity_decode($val, ENT_QUOTES, 'UTF-8');
162: }
163: }
164:
165: return self::$texy->process($m[2]);
166: }
167:
168: }
169: