|
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
|
204 |
|
public static function compile(string $content) : string |
|
52
|
|
|
{ |
|
53
|
|
|
return preg_replace_callback("%&\(([^)]*)\);%i", function ($variable) |
|
54
|
|
|
{ |
|
55
|
115 |
|
$parts = explode(':', $variable[1]); |
|
56
|
115 |
|
$variable = '$' . str_replace('.', '->', $parts[0]); |
|
57
|
|
|
|
|
58
|
115 |
|
if ( isset($parts[1]) |
|
59
|
115 |
|
&& array_key_exists($parts[1], self::$_filters)) { |
|
60
|
48 |
|
if ($parts[1] == 'p') { |
|
61
|
|
|
$command = 'eval(\'?>\' . ' . $variable . ')'; |
|
62
|
|
|
} else { |
|
63
|
48 |
|
$function = self::$_filters[$parts[1]]; |
|
64
|
48 |
|
$command = 'echo ' . $function . '(' . $variable . ')'; |
|
65
|
|
|
} |
|
66
|
|
|
} else { |
|
67
|
99 |
|
$command = 'echo htmlentities(' . $variable . ', ENT_COMPAT, midcom::get()->i18n->get_current_charset())'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
115 |
|
return "<?php $command; ?>"; |
|
71
|
204 |
|
}, $content); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|