|
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
|
|
|
'H' => '', |
|
24
|
|
|
'p' => '', |
|
25
|
|
|
'u' => 'rawurlencode', |
|
26
|
|
|
'f' => 'nl2br', |
|
27
|
|
|
's' => '', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Register PHP function as string formatter to the Midgard formatting engine. |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function register($name, $function) |
|
34
|
|
|
{ |
|
35
|
|
|
self::$_filters["x{$name}"] = $function; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Return a string as formatted by the specified filter |
|
40
|
|
|
* Note: The p filter is not supported here |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function format(string $content, string $name) : string |
|
43
|
|
|
{ |
|
44
|
|
|
if (!isset(self::$_filters[$name]) || !is_callable(self::$_filters[$name])) { |
|
45
|
|
|
return $content; |
|
46
|
|
|
} |
|
47
|
|
|
return call_user_func(self::$_filters[$name], $content); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Compile template to php code |
|
52
|
|
|
*/ |
|
53
|
201 |
|
public static function compile(string $content) : string |
|
54
|
|
|
{ |
|
55
|
|
|
return preg_replace_callback("%&\(([^)]*)\);%i", function ($variable) |
|
56
|
|
|
{ |
|
57
|
115 |
|
$variable_parts = explode(':', $variable[1]); |
|
58
|
115 |
|
$variable = '$' . $variable_parts[0]; |
|
59
|
|
|
|
|
60
|
115 |
|
if (strpos($variable, '.') !== false) { |
|
61
|
12 |
|
$parts = explode('.', $variable); |
|
62
|
12 |
|
$variable = $parts[0] . '->' . $parts[1]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
115 |
|
if ( isset($variable_parts[1]) |
|
66
|
115 |
|
&& array_key_exists($variable_parts[1], self::$_filters)) { |
|
67
|
49 |
|
switch ($variable_parts[1]) { |
|
68
|
49 |
|
case 's': |
|
69
|
|
|
//display as-is |
|
70
|
49 |
|
case 'h': |
|
71
|
|
|
case 'H': |
|
72
|
|
|
//According to documentation, these two should do something, but actually they don't... |
|
73
|
49 |
|
$command = 'echo ' . $variable; |
|
74
|
49 |
|
break; |
|
75
|
|
|
case 'p': |
|
76
|
|
|
$command = 'eval(\'?>\' . ' . $variable . ')'; |
|
77
|
|
|
break; |
|
78
|
|
|
default: |
|
79
|
|
|
$function = self::$_filters[$variable_parts[1]]; |
|
80
|
|
|
$command = 'echo ' . $function . '(' . $variable . ')'; |
|
81
|
49 |
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
99 |
|
$command = 'echo htmlentities(' . $variable . ', ENT_COMPAT, midcom::get()->i18n->get_current_charset())'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
115 |
|
return "<?php $command; ?>"; |
|
88
|
201 |
|
}, $content); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|