1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package midcom.helper |
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Formatting helpers for style elements. |
11
|
|
|
* |
12
|
|
|
* @package midcom.helper |
13
|
|
|
*/ |
14
|
|
|
class midcom_helper_formatter |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Filter registry |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private static $_filters = [ |
22
|
|
|
'h' => '', |
23
|
|
|
'p' => '', |
24
|
|
|
'u' => 'rawurlencode', |
25
|
|
|
'f' => 'nl2br', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register PHP function as string formatter to the Midgard formatting engine. |
30
|
|
|
*/ |
31
|
|
|
public static function register(string $name, callable $function) |
32
|
|
|
{ |
33
|
|
|
self::$_filters["x{$name}"] = $function; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return a string as formatted by the specified filter |
38
|
|
|
* Note: The p filter is not supported here |
39
|
|
|
*/ |
40
|
|
|
public static function format(string $content, string $name) : string |
41
|
|
|
{ |
42
|
|
|
if (!isset(self::$_filters[$name]) || !is_callable(self::$_filters[$name])) { |
43
|
|
|
return $content; |
44
|
|
|
} |
45
|
|
|
return self::$_filters[$name]($content); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Compile template to php code |
50
|
|
|
*/ |
51
|
206 |
|
public static function compile(string $content) : string |
52
|
|
|
{ |
53
|
206 |
|
return preg_replace_callback("%&\(([^)]*)\);%i", function ($variable) |
54
|
|
|
{ |
55
|
117 |
|
$parts = explode(':', $variable[1]); |
56
|
117 |
|
$variable = '$' . str_replace('.', '->', $parts[0]); |
57
|
|
|
|
58
|
117 |
|
if ( isset($parts[1]) |
59
|
117 |
|
&& array_key_exists($parts[1], self::$_filters)) { |
60
|
50 |
|
if ($parts[1] == 'p') { |
61
|
|
|
$command = 'eval(\'?>\' . ' . $variable . ')'; |
62
|
|
|
} else { |
63
|
50 |
|
$function = self::$_filters[$parts[1]]; |
64
|
50 |
|
$command = 'echo ' . $function . '(' . $variable . ')'; |
65
|
|
|
} |
66
|
|
|
} else { |
67
|
101 |
|
$command = 'echo htmlentities(' . $variable . ', ENT_COMPAT, midcom::get()->i18n->get_current_charset())'; |
68
|
|
|
} |
69
|
|
|
|
70
|
117 |
|
return "<?php $command; ?>"; |
71
|
206 |
|
}, $content); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|