diff --git a/auth.php b/auth.php index 28c6d60..ca2403b 100644 --- a/auth.php +++ b/auth.php @@ -99,10 +99,13 @@ if ($userRole !== false) { // Generate a secure random token. $token = bin2hex(random_bytes(32)); $expiry = time() + (30 * 24 * 60 * 60); // 30 days + // Load existing persistent tokens. $persistentTokens = []; if (file_exists($persistentTokensFile)) { - $persistentTokens = json_decode(file_get_contents($persistentTokensFile), true); + $encryptedContent = file_get_contents($persistentTokensFile); + $decryptedContent = decryptData($encryptedContent, $encryptionKey); + $persistentTokens = json_decode($decryptedContent, true); if (!is_array($persistentTokens)) { $persistentTokens = []; } @@ -110,9 +113,10 @@ if ($userRole !== false) { // Save token along with username and expiry. $persistentTokens[$token] = [ "username" => $username, - "expiry" => $expiry + "expiry" => $expiry ]; - file_put_contents($persistentTokensFile, json_encode($persistentTokens, JSON_PRETTY_PRINT)); + $encryptedContent = encryptData(json_encode($persistentTokens, JSON_PRETTY_PRINT), $encryptionKey); + file_put_contents($persistentTokensFile, $encryptedContent, LOCK_EX); // Set the cookie. (Assuming $secure is defined in config.php.) setcookie('remember_me_token', $token, $expiry, '/', '', $secure, true); } diff --git a/config.php b/config.php index c9af650..4fc1a01 100644 --- a/config.php +++ b/config.php @@ -1,6 +1,57 @@ = time()) { - // Token is valid; auto-authenticate the user. - $_SESSION["authenticated"] = true; - $_SESSION["username"] = $tokenData["username"]; - // Optionally, set admin status if stored in token data: - // $_SESSION["isAdmin"] = $tokenData["isAdmin"]; - } else { - // Token expired; remove it and clear the cookie. - unset($persistentTokens[$_COOKIE['remember_me_token']]); - file_put_contents($persistentTokensFile, json_encode($persistentTokens, JSON_PRETTY_PRINT)); - setcookie('remember_me_token', '', time() - 3600, '/', '', $secure, true); - } + $encryptedContent = file_get_contents($persistentTokensFile); + $decryptedContent = decryptData($encryptedContent, $encryptionKey); + $persistentTokens = json_decode($decryptedContent, true); + if (!is_array($persistentTokens)) { + $persistentTokens = []; + } + } + if (is_array($persistentTokens) && isset($persistentTokens[$_COOKIE['remember_me_token']])) { + $tokenData = $persistentTokens[$_COOKIE['remember_me_token']]; + if ($tokenData['expiry'] >= time()) { + // Token is valid; auto-authenticate the user. + $_SESSION["authenticated"] = true; + $_SESSION["username"] = $tokenData["username"]; + // Optionally, set admin status if stored in token data: + // $_SESSION["isAdmin"] = $tokenData["isAdmin"]; + } else { + // Token expired; remove it and clear the cookie. + unset($persistentTokens[$_COOKIE['remember_me_token']]); + $newEncryptedContent = encryptData(json_encode($persistentTokens, JSON_PRETTY_PRINT), $encryptionKey); + file_put_contents($persistentTokensFile, $newEncryptedContent, LOCK_EX); + setcookie('remember_me_token', '', time() - 3600, '/', '', $secure, true); } } } @@ -64,15 +123,4 @@ if (strpos(BASE_URL, 'yourwebsite') !== false) { } define('SHARE_URL', getenv('SHARE_URL') ? getenv('SHARE_URL') : $defaultShareUrl); - -define('UPLOAD_DIR', '/var/www/uploads/'); -define('TIMEZONE', 'America/New_York'); -define('DATE_TIME_FORMAT', 'm/d/y h:iA'); -define('TOTAL_UPLOAD_SIZE', '5G'); -define('USERS_DIR', '/var/www/users/'); -define('USERS_FILE', 'users.txt'); -define('META_DIR','/var/www/metadata/'); -define('META_FILE','file_metadata.json'); -define('TRASH_DIR', UPLOAD_DIR . 'trash/'); -date_default_timezone_set(TIMEZONE); ?> \ No newline at end of file diff --git a/logout.php b/logout.php index 9c0a251..a210e21 100644 --- a/logout.php +++ b/logout.php @@ -1,17 +1,36 @@ "Logged out"]); diff --git a/networkUtils.js b/networkUtils.js index c3e579d..90f42fe 100644 --- a/networkUtils.js +++ b/networkUtils.js @@ -1,4 +1,3 @@ -// networkUtils.js export function sendRequest(url, method = "GET", data = null) { console.log("Sending request to:", url, "with method:", method); const options = { @@ -24,9 +23,11 @@ export function sendRequest(url, method = "GET", data = null) { throw new Error(`HTTP error ${response.status}: ${text}`); }); } + // Clone the response so we can safely fall back if JSON parsing fails. + const clonedResponse = response.clone(); return response.json().catch(() => { console.warn("Response is not JSON, returning as text"); - return response.text(); + return clonedResponse.text(); }); }); } \ No newline at end of file