From c2fb61afaa1f75ddfa7c603d25a10195027c2777 Mon Sep 17 00:00:00 2001 From: Lncvrt Date: Sun, 2 Mar 2025 13:11:20 -0700 Subject: [PATCH] Small API Improvements --- webroot/api/changeDisplayname.php | 10 ++++------ webroot/api/changePassword.php | 7 +++---- webroot/api/changeUsername.php | 7 +++---- webroot/api/checkAPIKey.php | 5 +---- webroot/api/dashboard/generateAPIKey.php | 5 +---- webroot/api/dashboard/generateConfig.php | 2 +- webroot/api/dashboard/getAPIKey.php | 5 +---- webroot/api/dashboard/getAnnoucement.php | 2 +- webroot/api/dashboard/getAnnouncements.php | 2 +- webroot/api/dashboard/setPreferences.php | 18 +++++++----------- webroot/api/deleteAllFiles.php | 5 ++--- webroot/api/filePreferences.php | 15 ++++++++------- webroot/api/getFileInfo.php | 7 ++----- webroot/api/getFilesUploaded.php | 5 +---- webroot/api/getUserInfo.php | 7 ++----- webroot/api/loginAccount.php | 4 +--- webroot/api/registerAccount.php | 10 ++++------ webroot/api/uploadFile.php | 2 +- 18 files changed, 44 insertions(+), 74 deletions(-) diff --git a/webroot/api/changeDisplayname.php b/webroot/api/changeDisplayname.php index 1ad97c2..38066b1 100644 --- a/webroot/api/changeDisplayname.php +++ b/webroot/api/changeDisplayname.php @@ -2,10 +2,10 @@ require '../config/config.php'; require '../incl/main.php'; -$old_displayname = isset($_POST['olddisplayname']) ? $_POST['olddisplayname'] : ''; -$new_displayname = isset($_POST['newdisplayname']) ? $_POST['newdisplayname'] : ''; -$username = isset($_POST['username']) ? $_POST['username'] : ''; -$password = isset($_POST['password']) ? $_POST['password'] : ''; +$old_displayname = $_POST['olddisplayname'] ?? ''; +$new_displayname = $_POST['newdisplayname'] ?? ''; +$username = $_POST['username'] ?? ''; +$password = $_POST['password'] ??''; if (empty($username) || empty($old_displayname) || empty($new_displayname) || empty($password)) { http_response_code(400); @@ -17,7 +17,6 @@ if (strlen($new_displayname) < 3 || strlen($new_displayname) > 32) { http_response_code(400); header('Content-Type: application/json'); die(json_encode(['success' => 'false', 'responsae' => 'New Display Name must be between 3 and 32 characters'])); - exit; } $conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbname); @@ -56,7 +55,6 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode(['success' => 'false', 'response' => 'User not found'])); } - $stmt->close(); $conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/changePassword.php b/webroot/api/changePassword.php index 2f106c9..f399d76 100644 --- a/webroot/api/changePassword.php +++ b/webroot/api/changePassword.php @@ -2,9 +2,9 @@ require '../config/config.php'; require '../incl/main.php'; -$username = isset($_POST['username']) ? $_POST['username'] : ''; -$old_password = isset($_POST['oldpassword']) ? $_POST['oldpassword'] : ''; -$new_password = isset($_POST['newpassword']) ? $_POST['newpassword'] : ''; +$username = $_POST['username'] ?? ''; +$old_password = $_POST['oldpassword'] ?? ''; +$new_password = $_POST['newpassword'] ?? ''; if (empty($username) || empty($old_password) || empty($new_password)) { http_response_code(400); @@ -57,7 +57,6 @@ if ($result->num_rows > 0) { http_response_code(404); die(json_encode(['success' => 'false', 'response' => 'User not found'])); } - $stmt->close(); $conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/changeUsername.php b/webroot/api/changeUsername.php index cab7707..a67703c 100644 --- a/webroot/api/changeUsername.php +++ b/webroot/api/changeUsername.php @@ -2,9 +2,9 @@ require '../config/config.php'; require '../incl/main.php'; -$old_username = isset($_POST['oldusername']) ? $_POST['oldusername'] : ''; -$new_username = isset($_POST['newusername']) ? $_POST['newusername'] : ''; -$password = isset($_POST['password']) ? $_POST['password'] : ''; +$old_username = $_POST['oldusername'] ?? ''; +$new_username = $_POST['newusername'] ?? ''; +$password = $_POST['password'] ?? ''; if (empty($old_username) || empty($new_username) || empty($password)) { http_response_code(400); @@ -54,7 +54,6 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode(['success' => 'false', 'response' => 'User not found'])); } - $stmt->close(); $conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/checkAPIKey.php b/webroot/api/checkAPIKey.php index 8e79da0..a971190 100644 --- a/webroot/api/checkAPIKey.php +++ b/webroot/api/checkAPIKey.php @@ -14,7 +14,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$apikey = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; +$apikey = $_SERVER['HTTP_KEY'] ?? ''; if (isset($apikey) && empty($apikey)) { $response = [ @@ -47,7 +47,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - -$stmt->close(); -$conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/dashboard/generateAPIKey.php b/webroot/api/dashboard/generateAPIKey.php index c44f9a6..3018d28 100644 --- a/webroot/api/dashboard/generateAPIKey.php +++ b/webroot/api/dashboard/generateAPIKey.php @@ -23,7 +23,7 @@ function generateRandomString($length) { return $randomString; } -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : ''; +$session = $_COOKIE['session'] ?? ''; if (isset($session) && empty($session)) { $response = [ @@ -64,7 +64,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - -$stmt->close(); -$conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/dashboard/generateConfig.php b/webroot/api/dashboard/generateConfig.php index b75e4e1..f06dab6 100644 --- a/webroot/api/dashboard/generateConfig.php +++ b/webroot/api/dashboard/generateConfig.php @@ -15,7 +15,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : ''; +$session = $_COOKIE['session'] ?? ''; $stmt = $conn->prepare("SELECT * FROM users WHERE session = ?"); $stmt->bind_param("s", $session); diff --git a/webroot/api/dashboard/getAPIKey.php b/webroot/api/dashboard/getAPIKey.php index 978d95a..d2e26ed 100644 --- a/webroot/api/dashboard/getAPIKey.php +++ b/webroot/api/dashboard/getAPIKey.php @@ -14,7 +14,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : ''; +$session = $_COOKIE['session'] ?? ''; if (isset($session) && empty($session)) { $response = [ @@ -51,7 +51,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - -$stmt->close(); -$conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/dashboard/getAnnoucement.php b/webroot/api/dashboard/getAnnoucement.php index 8a2ea55..99e0fb8 100644 --- a/webroot/api/dashboard/getAnnoucement.php +++ b/webroot/api/dashboard/getAnnoucement.php @@ -14,7 +14,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : ''; +$session = $_COOKIE['session'] ?? ''; if (isset($session) && empty($session)) { $response = [ diff --git a/webroot/api/dashboard/getAnnouncements.php b/webroot/api/dashboard/getAnnouncements.php index d03fe40..fb301f3 100644 --- a/webroot/api/dashboard/getAnnouncements.php +++ b/webroot/api/dashboard/getAnnouncements.php @@ -14,7 +14,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$key = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; +$key = $_SERVER['HTTP_KEY'] ?? ''; $stmt = $conn->prepare("SELECT * FROM users WHERE api_key = ?"); $stmt->bind_param("s", $key); diff --git a/webroot/api/dashboard/setPreferences.php b/webroot/api/dashboard/setPreferences.php index 4d5cd68..5f2a6ff 100644 --- a/webroot/api/dashboard/setPreferences.php +++ b/webroot/api/dashboard/setPreferences.php @@ -15,12 +15,12 @@ if ($conn->connect_error) { die(json_encode($response)); } -$apiKey = isset($_POST['key']) ? $_POST['key'] : null; -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : null; -$country = isset($_POST['country']) ? $_POST['country'] : null; -$timezone = isset($_POST['timezone']) ? $_POST['timezone'] : null; -$dateformat = isset($_POST['dateformat']) ? $_POST['dateformat'] : null; -$timeformat = isset($_POST['timeformat']) ? $_POST['timeformat'] : null; +$apiKey = $_POST['key'] ?? null; +$session = $_COOKIE['session'] ?? null; +$country = $_POST['country'] ?? null; +$timezone = $_POST['timezone'] ?? null; +$dateformat = $_POST['dateformat'] ?? null; +$timeformat = $_POST['timeformat'] ?? null; $query = "UPDATE users SET "; $params = []; @@ -31,7 +31,6 @@ if ($country !== null) { if (!in_array($country, $countries)) { $country = null; die('{"success": false, "response": "Invalid country" }'); - return; } $query .= "country = ?, "; @@ -141,7 +140,4 @@ if ($stmt->execute()) { header('Content-Type: application/json'); die(json_encode($response)); } -$stmt->close(); - -$stmt->close(); -$conn->close(); \ No newline at end of file +?> \ No newline at end of file diff --git a/webroot/api/deleteAllFiles.php b/webroot/api/deleteAllFiles.php index ac31eb7..03fe7a6 100644 --- a/webroot/api/deleteAllFiles.php +++ b/webroot/api/deleteAllFiles.php @@ -18,8 +18,8 @@ if ($conn->connect_error) { die(json_encode($response)); } -$key = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : ''; +$key = $_SERVER['HTTP_KEY'] ?? ''; +$session = $_COOKIE['session'] ?? ''; if (!empty($key)) { $sql = "SELECT uid FROM users WHERE api_key = ?"; @@ -88,5 +88,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); echo json_encode($response); - ?> \ No newline at end of file diff --git a/webroot/api/filePreferences.php b/webroot/api/filePreferences.php index 83ef44d..3677517 100644 --- a/webroot/api/filePreferences.php +++ b/webroot/api/filePreferences.php @@ -15,12 +15,12 @@ if ($conn->connect_error) { die(json_encode($response)); } -$apiKey = isset($_POST['key']) ? $_POST['key'] : null; -$session = isset($_COOKIE['session']) ? $_COOKIE['session'] : null; -$password = null;//isset($_POST['password']) ? $_POST['password'] : null; -$spoiler = isset($_POST['spoiler']) ? $_POST['spoiler'] : null; -$filename = isset($_POST['filename']) ? $_POST['filename'] : null; -$fileId = isset($_POST['fileId']) ? $_POST['fileId'] : null; +$apiKey = $_POST['key'] ?? null; +$session = $_COOKIE['session'] ?? null; +$password = null;//$_POST['password'] ?? null; +$spoiler = $_POST['spoiler'] ?? null; +$filename = $_POST['filename'] ?? null; +$fileId = $_POST['fileId'] ?? null; $query = "UPDATE uploads SET "; $params = []; @@ -86,4 +86,5 @@ if ($password !== null || $filename !== null) { } header('Content-Type: application/json'); -die(json_encode($response)); \ No newline at end of file +die(json_encode($response)); +?> \ No newline at end of file diff --git a/webroot/api/getFileInfo.php b/webroot/api/getFileInfo.php index 92c3780..fd412a0 100644 --- a/webroot/api/getFileInfo.php +++ b/webroot/api/getFileInfo.php @@ -15,8 +15,8 @@ if ($conn->connect_error) { die(json_encode($response)); } -$apikey = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; -$fileid = isset($_SERVER['HTTP_FILEID']) ? $_SERVER['HTTP_FILEID'] : ''; +$apikey = $_SERVER['HTTP_KEY'] ?? ''; +$fileid = $_SERVER['HTTP_FILEID'] ?? ''; if (empty($apikey) || empty($fileid)) { $response = [ @@ -82,7 +82,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - -$stmt->close(); -$conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/getFilesUploaded.php b/webroot/api/getFilesUploaded.php index c8bb8e3..33b11f4 100644 --- a/webroot/api/getFilesUploaded.php +++ b/webroot/api/getFilesUploaded.php @@ -14,7 +14,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$apikey = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; +$apikey = $_SERVER['HTTP_KEY'] ?? ''; $stmt = $conn->prepare("SELECT uid FROM users WHERE api_key = ?"); $stmt->bind_param("s", $apikey); @@ -60,7 +60,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - -$stmt->close(); -$conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/getUserInfo.php b/webroot/api/getUserInfo.php index d734518..f6fb426 100644 --- a/webroot/api/getUserInfo.php +++ b/webroot/api/getUserInfo.php @@ -15,8 +15,8 @@ if ($conn->connect_error) { die(json_encode($response)); } -$apikey = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; -$uid = isset($_SERVER['HTTP_UID']) ? $_SERVER['HTTP_UID'] : ''; +$apikey = $_SERVER['HTTP_KEY'] ?? ''; +$uid = $_SERVER['HTTP_UID'] ?? ''; if (empty($apikey)) { $response = [ @@ -89,7 +89,4 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - -$stmt->close(); -$conn->close(); ?> \ No newline at end of file diff --git a/webroot/api/loginAccount.php b/webroot/api/loginAccount.php index 14e6a97..e73f2dc 100644 --- a/webroot/api/loginAccount.php +++ b/webroot/api/loginAccount.php @@ -4,7 +4,7 @@ require '../incl/main.php'; $ip = $_SERVER['REMOTE_ADDR']; -$token = isset($_POST['captcha_token']) ? $_POST['captcha_token'] : ''; +$token = $_POST['captcha_token'] ?? ''; $url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'; @@ -130,8 +130,6 @@ if ($result->num_rows > 0) { header('Content-Type: application/json'); die(json_encode($response)); } - $stmt->close(); $conn->close(); - ?> diff --git a/webroot/api/registerAccount.php b/webroot/api/registerAccount.php index 81e53ff..f824969 100644 --- a/webroot/api/registerAccount.php +++ b/webroot/api/registerAccount.php @@ -4,7 +4,7 @@ require '../incl/main.php'; $ip = $_SERVER['REMOTE_ADDR']; -$token = isset($_POST['captcha_token']) ? $_POST['captcha_token'] : ''; +$token = $_POST['captcha_token'] ?? ''; $url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'; @@ -62,10 +62,10 @@ if ($conn->connect_error) { die(json_encode($response)); } -$request_username = isset($_POST['username']) ? $_POST['username'] : ''; +$request_username = $_POST['username'] ?? ''; $request_displayname = (isset($_POST['displayname']) && !empty($_POST['displayname'])) ? $_POST['displayname'] : $request_username; -$request_password = isset($_POST['password']) ? $_POST['password'] : ''; -$request_email = isset($_POST['email']) ? $_POST['email'] : ''; +$request_password = $_POST['password'] ?? ''; +$request_email = $_POST['email'] ?? ''; if (empty($request_username) || empty($request_password) || empty($request_email)) { $response = [ @@ -235,8 +235,6 @@ if ($result->num_rows > 0) { http_response_code(200); echo json_encode($response); } - $stmt->close(); $conn->close(); - ?> \ No newline at end of file diff --git a/webroot/api/uploadFile.php b/webroot/api/uploadFile.php index 58e6b9c..1775ee4 100644 --- a/webroot/api/uploadFile.php +++ b/webroot/api/uploadFile.php @@ -27,7 +27,7 @@ if ($conn->connect_error) { die(json_encode($response)); } -$key = isset($_SERVER['HTTP_KEY']) ? $_SERVER['HTTP_KEY'] : ''; +$key = $_SERVER['HTTP_KEY'] ?? ''; if ($_COOKIE['session']) { $sql = "SELECT uid FROM users WHERE session = ?";