-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
137 lines (105 loc) · 3.96 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
This is the PhpExec plugin.
This file contains the PhpExec plugin. It provides a PHP execution feature
and two shortcuts to trigger the execution. One shortcut directly writes the
PHP output into the content while the other shortcut escapes the output
before it writes the PHP output into the content.
@package urlaube\phpexec
@version 0.1a1
@author Yahe <[email protected]>
@since 0.1a0
*/
// ===== DO NOT EDIT HERE =====
// prevent script from getting called directly
if (!defined("URLAUBE")) { die(""); }
class PhpExec extends BaseSingleton implements Plugin {
// CONSTANTS
const CONSTANT = "constant";
const EXTENSION = ".php";
const FILENAME = "filename";
const PHP = "~\[php (?P<filename>[^\]]+)\]~";
const PHPRAW = "~\[php\:raw (?P<filename>[^\]]+)\]~";
const PLACEHOLDER = "~\{\%(?P<constant>[^\}]+)\}~";
// HELPER FUNCTIONS
protected static function getConstant($matches) {
$result = null;
// check if the constant name is set
if (isset($matches[static::CONSTANT])) {
$constant = $matches[static::CONSTANT];
if (defined($constant)) {
$result = constant($constant);
}
}
return $result;
}
protected static function getContent($matches, $raw = false) {
$result = null;
// check if the filename is set
if (isset($matches[static::FILENAME])) {
$filename = $matches[static::FILENAME];
// replace placeholder with PHP constant
$filename = preg_replace_callback(static::PLACEHOLDER,
function ($matches) { return static::getConstant($matches, false); },
$filename);
// fix $filename
$filename = realpath($filename);
// check if the file name ends with .php and is referencing an existing file
if (istrail($filename, static::EXTENSION) && is_file($filename)) {
// start output buffering
$level = _startBuffer();
if (null !== $level) {
try {
// include the file to execute the PHP source
include($filename);
} finally {
// finish output buffering and retrieve the content
$result = _stopBuffer($level);
// escape the result if it's not supposed to be used raw
if (!$raw) {
$result = html($result);
}
}
}
}
}
return $result;
}
protected static function handleShortcodes($content) {
$result = $content;
if (is_string($result)) {
// replace shortcode with PHP output
$result = preg_replace_callback(static::PHP,
function ($matches) { return static::getContent($matches, false); },
$result);
// replace shortcode with raw PHP output
$result = preg_replace_callback(static::PHPRAW,
function ($matches) { return static::getContent($matches, true); },
$result);
}
return $result;
}
// RUNTIME FUNCTIONS
public static function run($content) {
$result = $content;
if ($result instanceof Content) {
if ($result->isset(CONTENT)) {
$result->set(CONTENT, static::handleShortcodes(value($result, CONTENT)));
}
} else {
if (is_array($result)) {
// iterate through all content items
foreach ($result as $result_item) {
if ($result_item instanceof Content) {
if ($result_item->isset(CONTENT)) {
$result_item->set(CONTENT, static::handleShortcodes(value($result_item, CONTENT)));
}
}
}
}
}
return $result;
}
}
// register plugin
Plugins::register(PhpExec::class, "run", FILTER_CONTENT);