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
| <?php header('Content-Type: text/html; charset=utf-8');
$max_execution_time = 5; $memory_limit = '128M'; $disabled_functions = [ 'exec', 'passthru', 'shell_exec', 'system', 'proc_open', 'popen', 'curl_exec', 'curl_multi_exec', 'parse_ini_file', 'show_source', 'pcntl_exec', 'posix_kill', 'posix_mkfifo', 'posix_setpgid', 'posix_setsid', 'posix_setuid', 'posix_setgid', 'posix_uname', 'dl', 'openlog', 'syslog', 'closelog' ];
ini_set('max_execution_time', $max_execution_time); ini_set('memory_limit', $memory_limit);
$code = isset($_POST['code']) ? $_POST['code'] : '';
if (empty($code)) { die('错误: 没有提供PHP代码'); }
foreach ($disabled_functions as $func) { if (preg_match('/\b' . preg_quote($func, '/') . '\s*\(/i', $code)) { die("安全错误: 不允许使用 {$func}() 函数"); } }
$dangerous_patterns = [ '/`.*`/', '/eval\s*\(/i', '/create_function\s*\(/i', '/include\s*\(/i', '/require\s*\(/i', '/include_once\s*\(/i', '/require_once\s*\(/i', '/file_put_contents\s*\(/i', '/file_get_contents\s*\(/i', '/unlink\s*\(/i', '/phpinfo\s*\(/i', '/chmod\s*\(/i', '/chown\s*\(/i', '/chgrp\s*\(/i', '/putenv\s*\(/i', '/ini_set\s*\(/i', '/extract\s*\(/i', '/parse_str\s*\(/i', '/assert\s*\(/i', '/preg_replace\s*\(.*\/e.*\)/i', '/proc_terminate\s*\(/i', '/pcntl_fork\s*\(/i', '/posix_getpwuid\s*\(/i', '/posix_kill\s*\(/i', '/posix_setuid\s*\(/i', '/posix_setgid\s*\(/i' ];
foreach ($dangerous_patterns as $pattern) { if (preg_match($pattern, $code)) { die("安全错误: 检测到潜在的危险操作"); } }
ob_start();
$old_error_reporting = error_reporting(E_ALL); $old_display_errors = ini_set('display_errors', '1');
try { eval('?>' . $code); } catch (ParseError $e) { echo "解析错误: " . $e->getMessage() . "\n"; echo "位于行: " . $e->getLine() . "\n"; } catch (Throwable $e) { echo "运行时错误: " . $e->getMessage() . "\n"; echo "位于行: " . $e->getLine() . "\n"; }
error_reporting($old_error_reporting); ini_set('display_errors', $old_display_errors);
$output = ob_get_clean();
$sensitive_patterns = [ '/\/home\/.*/i', '/\/var\/www\/.*/i', '/\[internal function\]/i' ];
foreach ($sensitive_patterns as $pattern) { $output = preg_replace($pattern, '[隐藏信息]', $output); }
echo htmlspecialchars($output, ENT_QUOTES, 'UTF-8'); ?>
|