2025-06-29 14:59:47 -04:00
|
|
|
<?php
|
2025-07-04 20:30:15 -04:00
|
|
|
// @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
|
2025-06-29 14:59:47 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Project: Update API
|
|
|
|
* Author: Vontainment
|
|
|
|
* URL: https://vontainment.com
|
|
|
|
* File: PluginUpdateFormHandler.php
|
|
|
|
* Description: WordPress Update API
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlFormHandler
|
|
|
|
{
|
2025-07-04 20:16:50 -04:00
|
|
|
public function handleRequest(): void
|
2025-06-29 14:59:47 -04:00
|
|
|
{
|
|
|
|
if (
|
|
|
|
$_SERVER['REQUEST_METHOD'] === 'POST'
|
|
|
|
&& isset($_POST['csrf_token'], $_SESSION['csrf_token'])
|
|
|
|
&& $_POST['csrf_token'] === $_SESSION['csrf_token']
|
|
|
|
) {
|
2025-07-04 21:29:02 -04:00
|
|
|
// Validate POST and FILES inputs
|
2025-06-29 14:59:47 -04:00
|
|
|
if (isset($_FILES['plugin_file'])) {
|
|
|
|
$this->uploadPluginFiles();
|
|
|
|
} elseif (isset($_POST['delete_plugin'])) {
|
2025-07-04 21:29:02 -04:00
|
|
|
$plugin_name = isset($_POST['plugin_name']) ? SecurityHandler::validateSlug($_POST['plugin_name']) : null;
|
2025-06-29 14:59:47 -04:00
|
|
|
$this->deletePlugin($plugin_name);
|
|
|
|
} else {
|
|
|
|
die('Invalid form action.');
|
|
|
|
}
|
|
|
|
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
die('Invalid CSRF token.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-04 20:16:50 -04:00
|
|
|
private function uploadPluginFiles(): void
|
2025-06-29 14:59:47 -04:00
|
|
|
{
|
|
|
|
$allowed_extensions = ['zip'];
|
|
|
|
$total_files = count($_FILES['plugin_file']['name']);
|
|
|
|
|
|
|
|
for ($i = 0; $i < $total_files; $i++) {
|
|
|
|
$file_name = isset($_FILES['plugin_file']['name'][$i])
|
2025-07-04 21:29:02 -04:00
|
|
|
? SecurityHandler::validateFilename($_FILES['plugin_file']['name'][$i])
|
2025-06-29 14:59:47 -04:00
|
|
|
: '';
|
|
|
|
$file_tmp = isset($_FILES['plugin_file']['tmp_name'][$i])
|
|
|
|
? $_FILES['plugin_file']['tmp_name'][$i]
|
|
|
|
: '';
|
|
|
|
$file_size = isset($_FILES['plugin_file']['size'][$i])
|
|
|
|
? filter_var($_FILES['plugin_file']['size'][$i], FILTER_VALIDATE_INT)
|
|
|
|
: 0;
|
|
|
|
$file_error = isset($_FILES['plugin_file']['error'][$i])
|
|
|
|
? filter_var($_FILES['plugin_file']['error'][$i], FILTER_VALIDATE_INT)
|
|
|
|
: UPLOAD_ERR_NO_FILE;
|
|
|
|
$file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
|
|
|
|
$plugin_slug = explode("_", $file_name)[0];
|
|
|
|
$existing_plugins = glob(PLUGINS_DIR . '/' . $plugin_slug . '_*');
|
|
|
|
foreach ($existing_plugins as $plugin) {
|
|
|
|
if (is_file($plugin)) {
|
|
|
|
unlink($plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_error !== UPLOAD_ERR_OK || !in_array($file_extension, $allowed_extensions)) {
|
|
|
|
echo '<script>'
|
|
|
|
. 'alert("Error uploading: '
|
|
|
|
. htmlspecialchars($file_name, ENT_QUOTES, 'UTF-8')
|
|
|
|
. '. Only .zip files are allowed.");'
|
|
|
|
. 'window.location.href = "/plupdate";'
|
|
|
|
. '</script>';
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$plugin_path = PLUGINS_DIR . '/' . $file_name;
|
|
|
|
if (move_uploaded_file($file_tmp, $plugin_path)) {
|
|
|
|
echo '<script>'
|
|
|
|
. 'alert("'
|
|
|
|
. htmlspecialchars($file_name, ENT_QUOTES, 'UTF-8')
|
|
|
|
. ' uploaded successfully.");'
|
|
|
|
. 'window.location.href = "/plupdate";'
|
|
|
|
. '</script>';
|
|
|
|
} else {
|
|
|
|
echo '<script>'
|
|
|
|
. 'alert("Error uploading: '
|
|
|
|
. htmlspecialchars($file_name, ENT_QUOTES, 'UTF-8')
|
|
|
|
. '");'
|
|
|
|
. 'window.location.href = "/plupdate";'
|
|
|
|
. '</script>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-04 20:16:50 -04:00
|
|
|
private function deletePlugin(?string $plugin_name): void
|
2025-06-29 14:59:47 -04:00
|
|
|
{
|
2025-07-04 21:29:02 -04:00
|
|
|
$plugin_name = SecurityHandler::validateFilename($plugin_name);
|
|
|
|
$plugin_name = basename((string) $plugin_name);
|
2025-06-29 14:59:47 -04:00
|
|
|
$plugin_path = PLUGINS_DIR . '/' . $plugin_name;
|
2025-07-04 19:30:40 -04:00
|
|
|
if (
|
|
|
|
file_exists($plugin_path)
|
|
|
|
&& dirname(realpath($plugin_path)) === realpath(PLUGINS_DIR)
|
|
|
|
) {
|
2025-06-29 14:59:47 -04:00
|
|
|
if (unlink($plugin_path)) {
|
|
|
|
echo '<script>'
|
|
|
|
. 'alert("Plugin deleted successfully!");'
|
|
|
|
. 'window.location.href = "/plupdate";'
|
|
|
|
. '</script>';
|
|
|
|
} else {
|
|
|
|
echo '<script>'
|
|
|
|
. 'alert("Failed to delete plugin file. Please try again.");'
|
|
|
|
. 'window.location.href = "/plupdate";'
|
|
|
|
. '</script>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|