diff --git a/src/controllers/folderController.php b/src/controllers/folderController.php index a63d916..aa87c8b 100644 --- a/src/controllers/folderController.php +++ b/src/controllers/folderController.php @@ -4,7 +4,8 @@ require_once __DIR__ . '/../../config/config.php'; require_once PROJECT_ROOT . '/src/models/FolderModel.php'; -class FolderController { +class FolderController +{ /** * @OA\Post( * path="/api/folder/createFolder.php", @@ -45,22 +46,23 @@ class FolderController { * * @return void Outputs a JSON response. */ - public function createFolder(): void { + public function createFolder(): void + { header('Content-Type: application/json'); - + // Ensure user is authenticated. if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) { http_response_code(401); echo json_encode(["error" => "Unauthorized"]); exit; } - + // Ensure the request method is POST. if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['error' => 'Invalid request method.']); exit; } - + // CSRF check. $headersArr = array_change_key_case(getallheaders(), CASE_LOWER); $receivedToken = $headersArr['x-csrf-token'] ?? ''; @@ -69,7 +71,7 @@ class FolderController { echo json_encode(['error' => 'Invalid CSRF token.']); exit; } - + // Check permissions. $username = $_SESSION['username'] ?? ''; $userPermissions = loadUserPermissions($username); @@ -77,36 +79,36 @@ class FolderController { echo json_encode(["error" => "Read-only users are not allowed to create folders."]); exit; } - + // Get and decode JSON input. $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['folderName'])) { echo json_encode(['error' => 'Folder name not provided.']); exit; } - + $folderName = trim($input['folderName']); $parent = isset($input['parent']) ? trim($input['parent']) : ""; - + // Basic sanitation for folderName. if (!preg_match(REGEX_FOLDER_NAME, $folderName)) { echo json_encode(['error' => 'Invalid folder name.']); exit; } - + // Optionally sanitize the parent. if ($parent && !preg_match(REGEX_FOLDER_NAME, $parent)) { echo json_encode(['error' => 'Invalid parent folder name.']); exit; } - + // Delegate to FolderModel. $result = FolderModel::createFolder($folderName, $parent); echo json_encode($result); exit; } - /** + /** * @OA\Post( * path="/api/folder/deleteFolder.php", * summary="Delete an empty folder", @@ -145,22 +147,23 @@ class FolderController { * * @return void Outputs a JSON response. */ - public function deleteFolder(): void { + public function deleteFolder(): void + { header('Content-Type: application/json'); - + // Ensure user is authenticated. if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) { http_response_code(401); echo json_encode(["error" => "Unauthorized"]); exit; } - + // Ensure the request is a POST. if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(["error" => "Invalid request method."]); exit; } - + // CSRF Protection. $headersArr = array_change_key_case(getallheaders(), CASE_LOWER); $receivedToken = isset($headersArr['x-csrf-token']) ? trim($headersArr['x-csrf-token']) : ''; @@ -169,7 +172,7 @@ class FolderController { echo json_encode(["error" => "Invalid CSRF token."]); exit; } - + // Check user permissions. $username = $_SESSION['username'] ?? ''; $userPermissions = loadUserPermissions($username); @@ -177,28 +180,28 @@ class FolderController { echo json_encode(["error" => "Read-only users are not allowed to delete folders."]); exit; } - + // Get and decode JSON input. $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['folder'])) { echo json_encode(["error" => "Folder name not provided."]); exit; } - + $folder = trim($input['folder']); // Prevent deletion of the root folder. if (strtolower($folder) === 'root') { echo json_encode(["error" => "Cannot delete root folder."]); exit; } - + // Delegate to the model. $result = FolderModel::deleteFolder($folder); echo json_encode($result); exit; } - /** + /** * @OA\Post( * path="/api/folder/renameFolder.php", * summary="Rename a folder", @@ -238,22 +241,23 @@ class FolderController { * * @return void Outputs a JSON response. */ - public function renameFolder(): void { + public function renameFolder(): void + { header('Content-Type: application/json'); - + // Ensure user is authenticated. if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) { http_response_code(401); echo json_encode(["error" => "Unauthorized"]); exit; } - + // Ensure the request method is POST. if ($_SERVER['REQUEST_METHOD'] !== 'POST') { echo json_encode(['error' => 'Invalid request method.']); exit; } - + // CSRF Protection. $headersArr = array_change_key_case(getallheaders(), CASE_LOWER); $receivedToken = isset($headersArr['x-csrf-token']) ? trim($headersArr['x-csrf-token']) : ''; @@ -262,7 +266,7 @@ class FolderController { echo json_encode(["error" => "Invalid CSRF token."]); exit; } - + // Check that the user is not read-only. $username = $_SESSION['username'] ?? ''; $userPermissions = loadUserPermissions($username); @@ -270,23 +274,23 @@ class FolderController { echo json_encode(["error" => "Read-only users are not allowed to rename folders."]); exit; } - + // Get JSON input. $input = json_decode(file_get_contents('php://input'), true); if (!isset($input['oldFolder']) || !isset($input['newFolder'])) { echo json_encode(['error' => 'Required folder names not provided.']); exit; } - + $oldFolder = trim($input['oldFolder']); $newFolder = trim($input['newFolder']); - + // Validate folder names. if (!preg_match(REGEX_FOLDER_NAME, $oldFolder) || !preg_match(REGEX_FOLDER_NAME, $newFolder)) { echo json_encode(['error' => 'Invalid folder name(s).']); exit; } - + // Delegate to the model. $result = FolderModel::renameFolder($oldFolder, $newFolder); echo json_encode($result); @@ -329,23 +333,24 @@ class FolderController { * * @return void Outputs JSON response. */ - public function getFolderList(): void { + public function getFolderList(): void + { header('Content-Type: application/json'); - + // Ensure user is authenticated. if (!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) { http_response_code(401); echo json_encode(["error" => "Unauthorized"]); exit; } - + // Optionally, you might add further input validation if necessary. $folderList = FolderModel::getFolderList(); echo json_encode($folderList); exit; } - /** + /** * @OA\Get( * path="/api/folder/shareFolder.php", * summary="Display a shared folder", @@ -396,7 +401,8 @@ class FolderController { * * @return void Outputs HTML content. */ - public function shareFolder(): void { + public function shareFolder(): void + { // Retrieve GET parameters. $token = filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING); $providedPass = filter_input(INPUT_GET, 'pass', FILTER_SANITIZE_STRING); @@ -404,52 +410,83 @@ class FolderController { if ($page === false || $page < 1) { $page = 1; } - + if (empty($token)) { http_response_code(400); header('Content-Type: application/json'); echo json_encode(["error" => "Missing token."]); exit; } - + // Delegate to the model. $data = FolderModel::getSharedFolderData($token, $providedPass, $page); - + // If a password is needed, output an HTML form. if (isset($data['needs_password']) && $data['needs_password'] === true) { header("Content-Type: text/html; charset=utf-8"); - ?> +?> + Enter Password + -
-

Folder Protected

-

This folder is protected by a password. Please enter the password to view its contents.

-
- - - - -
-
+
+

Folder Protected

+

This folder is protected by a password. Please enter the password to view its contents.

+
+ + + + +
+
+ - $data['error']]); exit; } - + // Extract data for the HTML view. $folderName = $data['folder']; $files = $data['files']; $currentPage = $data['currentPage']; $totalPages = $data['totalPages']; - function formatBytes($bytes) { + function formatBytes($bytes) + { if ($bytes < 1024) { return $bytes . " B"; } elseif ($bytes < 1024 * 1024) { @@ -475,187 +513,297 @@ class FolderController { return round($bytes / (1024 * 1024 * 1024), 2) . " GB"; } } - + // Build the HTML view. header("Content-Type: text/html; charset=utf-8"); ?> + Shared Folder: <?php echo htmlspecialchars($folderName, ENT_QUOTES, 'UTF-8'); ?> + -
-

Shared Folder:

-
-
- - - - -
- -

This folder is empty.

- - - - - - - - - - - - - - - - -
FilenameSize
- - - - -
- +
+

Shared Folder:

- - - - - - - - - + + function toggleViewMode() { + if (viewMode === 'list') { + viewMode = 'gallery'; + document.getElementById("listViewContainer").style.display = "none"; + renderGalleryView(); + document.getElementById("galleryViewContainer").style.display = "block"; + document.getElementById("toggleBtn").textContent = "Switch to List View"; + } else { + viewMode = 'list'; + document.getElementById("galleryViewContainer").style.display = "none"; + document.getElementById("listViewContainer").style.display = "block"; + document.getElementById("toggleBtn").textContent = "Switch to Gallery View"; + } + } + + - "Unauthorized"]); exit; } - + // Check that the user is not read-only. $username = $_SESSION['username'] ?? ''; $userPermissions = loadUserPermissions($username); @@ -721,7 +870,7 @@ class FolderController { echo json_encode(["error" => "Read-only users are not allowed to create share folders."]); exit; } - + // Retrieve and decode POST input. $input = json_decode(file_get_contents("php://input"), true); if (!$input || !isset($input['folder'])) { @@ -729,12 +878,12 @@ class FolderController { echo json_encode(["error" => "Invalid input."]); exit; } - + $folder = trim($input['folder']); $expirationMinutes = isset($input['expirationMinutes']) ? intval($input['expirationMinutes']) : 60; $password = isset($input['password']) ? $input['password'] : ""; $allowUpload = isset($input['allowUpload']) ? intval($input['allowUpload']) : 0; - + // Delegate to the model. $result = FolderModel::createShareFolderLink($folder, $expirationMinutes, $password, $allowUpload); echo json_encode($result); @@ -785,18 +934,19 @@ class FolderController { * * @return void Outputs the file with proper headers. */ - public function downloadSharedFile(): void { + public function downloadSharedFile(): void + { // Retrieve and sanitize GET parameters. $token = filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING); $file = filter_input(INPUT_GET, 'file', FILTER_SANITIZE_STRING); - + if (empty($token) || empty($file)) { http_response_code(400); header('Content-Type: application/json'); echo json_encode(["error" => "Missing token or file parameter."]); exit; } - + // Delegate to the model. $result = FolderModel::getSharedFileInfo($token, $file); if (isset($result['error'])) { @@ -805,14 +955,14 @@ class FolderController { echo json_encode(["error" => $result['error']]); exit; } - + $realFilePath = $result['realFilePath']; $mimeType = $result['mimeType']; - + // Serve the file. header("Content-Type: " . $mimeType); $ext = strtolower(pathinfo($realFilePath, PATHINFO_EXTENSION)); - if (in_array($ext, ['jpg','jpeg','png','gif','bmp','webp','svg','ico'])) { + if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'ico'])) { header('Content-Disposition: inline; filename="' . basename($realFilePath) . '"'); } else { header('Content-Disposition: attachment; filename="' . basename($realFilePath) . '"'); @@ -863,7 +1013,8 @@ class FolderController { * * @return void Redirects upon successful upload or outputs JSON errors. */ - public function uploadToSharedFolder(): void { + public function uploadToSharedFolder(): void + { // Ensure request is POST. if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); @@ -871,7 +1022,7 @@ class FolderController { echo json_encode(["error" => "Method not allowed."]); exit; } - + // Ensure the share token is provided. if (empty($_POST['token'])) { http_response_code(400); @@ -880,7 +1031,7 @@ class FolderController { exit; } $token = trim($_POST['token']); - + // Delegate the upload to the model. if (!isset($_FILES['fileToUpload'])) { http_response_code(400); @@ -889,7 +1040,7 @@ class FolderController { exit; } $fileUpload = $_FILES['fileToUpload']; - + $result = FolderModel::uploadToSharedFolder($token, $fileUpload); if (isset($result['error'])) { http_response_code(400); @@ -897,13 +1048,13 @@ class FolderController { echo json_encode($result); exit; } - + // Optionally, set a flash message in session. $_SESSION['upload_message'] = "File uploaded successfully."; - + // Redirect back to the shared folder view. $redirectUrl = "/api/folder/shareFolder.php?token=" . urlencode($token); header("Location: " . $redirectUrl); exit; } -} \ No newline at end of file +}