-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathrun.php
More file actions
187 lines (158 loc) · 6.68 KB
/
Copy pathrun.php
File metadata and controls
187 lines (158 loc) · 6.68 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Run PHP Code
*
* This script gives you the ability to quickly test snippets of PHP code locally.
*
* @copyright Copyright 2011-2022, Website Duck LLC (https://www.websiteduck.com)
* @link https://github.com/websiteduck/Run-PHP-Code Run PHP Code
* @license MIT License (https://www.opensource.org/licenses/mit-license.php)
*/
// This application is meant to be run locally and should not be made publicly accessible.
if (!in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1', '::ffff:127.0.0.1'))) die();
$originUrl = parse_url($_SERVER['HTTP_ORIGIN'] ?? $_SERVER['HTTP_REFERER'] ?? '') ?: [];
$originHost = strtolower($originUrl['host'] ?? '');
if (!empty($originUrl['port'])) $originHost .= ':' . $originUrl['port'];
if ($originHost === '' || $originHost !== strtolower($_SERVER['HTTP_HOST'] ?? '')) die();
define('NL', PHP_EOL);
if (!isset($_POST['runphp_data'])) {
die();
}
$runPhp = json_decode($_POST['runphp_data']);
if (!is_object($runPhp) || !isset($runPhp->action)) {
die();
}
if ($runPhp->action == 'run') {
header('Expires: Mon, 16 Apr 2012 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Content-Type: application/json; charset=utf-8');
header('Pragma: no-cache');
ini_set('display_errors', 1);
$fatal = E_ERROR | E_PARSE | E_COMPILE_ERROR;
$warning = $fatal | E_WARNING;
$deprecated = $warning | E_DEPRECATED | E_USER_DEPRECATED;
$notice = $deprecated | E_NOTICE;
switch ($runPhp->settings->errorReporting)
{
case 'fatal': error_reporting($fatal); break;
case 'warning': error_reporting($warning); break;
case 'deprecated': error_reporting($deprecated); break;
case 'notice': error_reporting($notice); break;
case 'all': error_reporting(-1); break;
case 'none': default: error_reporting(0); break;
}
$outputModeOverride = null;
if (preg_match('/@run-php-code\s+output\s*=\s*(html|console|markdown)\b/i', $runPhp->code, $match)) {
$outputModeOverride = strtolower($match[1]);
}
$runPhp->code = '?>' . ltrim($runPhp->code);
$ctx = [
'sent' => false,
'start' => hrtime(true),
'settings' => $runPhp->settings,
'color' => $runPhp->color ?? null,
'background_color' => $runPhp->background_color ?? null,
'output_mode_override' => $outputModeOverride,
];
$buildHtml = function ($html) use (&$ctx) {
$settings = $ctx['settings'];
$outputMode = $ctx['output_mode_override']
?? $settings->outputMode
?? (!empty($settings->preWrap) ? 'console' : 'html');
$ctx['output_mode'] = $outputMode;
if ($outputMode === 'console') {
$html = htmlspecialchars($html, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$html = '<style id="runphpcode-console-style">'
. 'html { margin: 0; }'
. 'body { margin: 0; padding: 8px; overflow-wrap: anywhere; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; line-height: 1.4; white-space: pre-wrap; }'
. '</style>'
. $html;
} elseif ($outputMode === 'markdown') {
require_once __DIR__ . '/lib/Parsedown.php';
$parsedown = new Parsedown();
$parsedown->setSafeMode(true);
$html = $parsedown->text($html);
$html = '<style id="runphpcode-markdown-style">'
. 'html { margin: 0; }'
. 'body { margin: 0; padding: 12px 16px; overflow-wrap: anywhere; font-family: Georgia, "Times New Roman", serif; font-size: 15px; line-height: 1.5; }'
. 'body > :first-child { margin-top: 0; }'
. 'body > :last-child { margin-bottom: 0; }'
. 'h1, h2, h3, h4, h5, h6 { font-family: Verdana, sans-serif; line-height: 1.25; }'
. 'pre, code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.9em; }'
. 'pre { padding: 10px 12px; overflow-x: auto; white-space: pre-wrap; overflow-wrap: anywhere; }'
. 'code { padding: 0.1em 0.3em; }'
. 'pre code { padding: 0; }'
. 'blockquote { margin-left: 0; padding-left: 1em; border-left: 3px solid currentColor; opacity: 0.9; }'
. 'table { border-collapse: collapse; max-width: 100%; }'
. 'th, td { border: 1px solid currentColor; padding: 4px 8px; }'
. '</style>'
. $html;
}
if (!empty($settings->colorize)) {
$colorPattern = '/^(#[0-9A-Fa-f]{3,8}|rgba?\([\d\s.,%]+\))$/';
$color = (isset($ctx['color']) && preg_match($colorPattern, $ctx['color'])) ? $ctx['color'] : '#000000';
$background = (isset($ctx['background_color']) && preg_match($colorPattern, $ctx['background_color'])) ? $ctx['background_color'] : '#ffffff';
$html = $html . '
<style id="runphpcode-style" media="all">
html, body { background-color: ' . $background . '; color: ' . $color . '; }
.xdebug-error th { background-color: ' . $background . '; font-weight: normal; font-family: sans-serif; }
.xdebug-error td { color: ' . $color . '; }
.xdebug-error th span { background-color: ' . $background . ' !important; }
</style>
';
}
if (strncmp($html, '<!DOCTYPE', 9) !== 0) {
$html = '<!DOCTYPE html>' . $html;
}
return $html;
};
$sendResponse = function ($html, $fatalError = null) use (&$ctx, $buildHtml) {
if ($ctx['sent']) {
return;
}
$ctx['sent'] = true;
$durationMs = (hrtime(true) - $ctx['start']) / 1e6;
echo json_encode([
'html' => $buildHtml($html),
'duration_ms' => round($durationMs, 3),
'memory_bytes' => memory_get_peak_usage(true),
'php_version' => PHP_VERSION,
'output_mode' => $ctx['output_mode'] ?? 'html',
'fatal_error' => $fatalError,
], JSON_INVALID_UTF8_SUBSTITUTE);
};
register_shutdown_function(function () use (&$ctx, $sendResponse) {
if ($ctx['sent']) {
return;
}
$error = error_get_last();
$fatalTypes = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR];
$fatalError = null;
$html = ob_get_level() > 0 ? ob_get_clean() : '';
if ($error && in_array($error['type'], $fatalTypes, true)) {
$fatalError = $error['message'];
if (!empty($error['file'])) {
$fatalError .= ' in ' . $error['file'];
}
if (!empty($error['line'])) {
$fatalError .= ' on line ' . $error['line'];
}
}
$sendResponse($html === false ? '' : $html, $fatalError);
});
ob_start();
$fatalError = null;
try {
eval($runPhp->code);
} catch (Throwable $e) {
$fatalError = $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
}
$html = ob_get_clean();
if ($html === false) {
$html = '';
}
$sendResponse($html, $fatalError);
die();
}