revise backend

This commit is contained in:
Michal Sedlák
2026-10-18 15:55:34 +02:00
parent dc452059f3
commit 99b1c96f4b
23 changed files with 681 additions and 720 deletions
+24 -37
View File
@@ -1,20 +1,7 @@
<?php
session_start();
if(!isset($_SESSION['uname'])){
header("location: ../../index.php");
session_destroy();
exit;
}
// Check if user has permission to upload/modify
if(!isset($_SESSION["upPer"]) || $_SESSION["upPer"] != true) {
die("You don't have permission to extract archives.");
}
// Include error reporting for debugging
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/../../includes/auth.php';
requireLogin('../../index.php');
requirePermission('upPer', 'You do not have permission to perform this operation.');
require_once __DIR__ . '/../../config/config.php';
use phpseclib3\Net\SFTP;
@@ -23,21 +10,21 @@ $sftp = initializeSFTP();
// Process extraction request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['archive'])) {
$archivePath = $_POST['archive'];
$dirPath = remoteDirname($archivePath);
$archivePath = requireRemotePath($_POST['archive'], $defaultPath);
$directoryPath = remoteDirname($archivePath);
$archiveName = basename($archivePath);
$extension = pathinfo($archiveName, PATHINFO_EXTENSION);
// Create a temporary local directory for extraction
$tempDir = sys_get_temp_dir() . '/sftp_extract_' . time();
if (!mkdir($tempDir, 0777, true)) {
$temporaryDirectory = sys_get_temp_dir() . '/sftp_extract_' . time();
if (!mkdir($temporaryDirectory, 0777, true)) {
die("Failed to create temporary directory");
}
// Download the archive to the temp directory
$localArchivePath = $tempDir . '/' . $archiveName;
$localArchivePath = $temporaryDirectory . '/' . $archiveName;
if (!$sftp->get($archivePath, $localArchivePath)) {
rmdir($tempDir);
rmdir($temporaryDirectory);
die("Failed to download the archive");
}
@@ -48,11 +35,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['archive'])) {
// Determine the extraction method based on file extension
switch (strtolower($extension)) {
case 'zip':
$extractionSuccess = extractZip($localArchivePath, $tempDir);
$extractionSuccess = extractZip($localArchivePath, $temporaryDirectory);
break;
case 'rar':
$extractionSuccess = extractRar($localArchivePath, $tempDir);
$extractionSuccess = extractRar($localArchivePath, $temporaryDirectory);
break;
case 'tar':
@@ -60,7 +47,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['archive'])) {
case 'bz2':
case 'xz':
case '7z':
$extractionSuccess = extractArchive($localArchivePath, $tempDir);
$extractionSuccess = extractArchive($localArchivePath, $temporaryDirectory);
break;
default:
@@ -69,20 +56,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['archive'])) {
if ($extractionSuccess) {
// Upload extracted files back to the server
uploadExtractedFiles($sftp, $tempDir, $dirPath);
uploadExtractedFiles($sftp, $temporaryDirectory, $directoryPath);
// Clean up temporary directory
deleteDirectory($tempDir);
deleteDirectory($temporaryDirectory);
// Redirect back to the file listing
header("Location: index.php?path=" . urlencode($dirPath));
header("Location: index.php?path=" . urlencode($directoryPath));
exit;
} else {
die("Failed to extract the archive");
}
} catch (Exception $e) {
deleteDirectory($tempDir);
die("Error during extraction: " . $e->getMessage());
} catch (Exception $exception) {
deleteDirectory($temporaryDirectory);
die("Error during extraction: " . $exception->getMessage());
}
} else {
header("Location: index.php");
@@ -140,7 +127,7 @@ function extractArchive($archivePath, $destination) {
$command = '';
// Change to the destination directory
$currentDir = getcwd();
$originalDirectory = getcwd();
chdir($destination);
if (PHP_OS_FAMILY === 'Windows') {
@@ -189,20 +176,20 @@ function extractArchive($archivePath, $destination) {
exec($command, $output, $returnCode);
// Change back to the original directory
chdir($currentDir);
chdir($originalDirectory);
return $returnCode === 0;
}
// Upload extracted files back to the SFTP server
function uploadExtractedFiles($sftp, $localDir, $remoteDir) {
$items = scandir($localDir);
function uploadExtractedFiles($sftp, $localDirectory, $remoteDirectory) {
$items = scandir($localDirectory);
foreach ($items as $item) {
if ($item === '.' || $item === '..') continue;
$localPath = $localDir . '/' . $item;
$remotePath = $remoteDir . '/' . $item;
$localPath = $localDirectory . '/' . $item;
$remotePath = $remoteDirectory . '/' . $item;
if (is_dir($localPath)) {
// Create the directory on the remote server