From f3456ead674046aa71ccc65ee35d64655441fd10 Mon Sep 17 00:00:00 2001 From: Marcus Hill Date: Sun, 5 Feb 2023 15:07:41 +0000 Subject: [PATCH] API - Add PHP logging when API queries fail because of SQL errors - Add user agent to logging - Enhance asset update endpoint --- api/v1/assets/asset_model.php | 129 ++++++++++++++++++++++++++++++ api/v1/assets/create.php | 89 +-------------------- api/v1/assets/delete.php | 2 +- api/v1/assets/update.php | 96 ++-------------------- api/v1/contacts/contact_model.php | 117 ++++++++++++++++++++++++--- api/v1/contacts/create.php | 4 +- api/v1/contacts/delete.php | 4 +- api/v1/contacts/update.php | 12 ++- api/v1/create_output.php | 7 +- api/v1/delete_output.php | 7 +- api/v1/read_output.php | 8 +- api/v1/update_output.php | 9 ++- api/v1/validate_api_key.php | 21 ++--- 13 files changed, 290 insertions(+), 215 deletions(-) create mode 100644 api/v1/assets/asset_model.php diff --git a/api/v1/assets/asset_model.php b/api/v1/assets/asset_model.php new file mode 100644 index 00000000..ffbfa6df --- /dev/null +++ b/api/v1/assets/asset_model.php @@ -0,0 +1,129 @@ + 0) { else { $return_arr['success'] = "False"; $return_arr['message'] = "Auth success but delete query failed. Ensure ALL required variables are provided and database schema is up-to-date. Most likely cause: asset/client/company ID mismatch."; + + // Log any database/schema related errors to the PHP Error log + if (mysqli_error($mysqli)) { + error_log("API Database Error: " . mysqli_error($mysqli)); + } } echo json_encode($return_arr); -exit(); \ No newline at end of file +exit(); diff --git a/api/v1/read_output.php b/api/v1/read_output.php index 42c6cde9..97162ace 100644 --- a/api/v1/read_output.php +++ b/api/v1/read_output.php @@ -21,6 +21,12 @@ if ($sql && mysqli_num_rows($sql) > 0) { else { $return_arr['success'] = "False"; $return_arr['message'] = "No resource (for this client and company) with the specified parameter(s)."; + + // Log any database/schema related errors to the PHP Error log + if (mysqli_error($mysqli)) { + error_log("API Database Error: " . mysqli_error($mysqli)); + } + echo json_encode($return_arr); exit(); -} \ No newline at end of file +} diff --git a/api/v1/update_output.php b/api/v1/update_output.php index 59e7c13c..da985b54 100644 --- a/api/v1/update_output.php +++ b/api/v1/update_output.php @@ -16,8 +16,13 @@ if (isset($update_count) && is_numeric($update_count) && $update_count > 0) { // Query returned false: something went wrong, or it was declined due to required variables missing else { $return_arr['success'] = "False"; - $return_arr['message'] = "Auth success but update query failed/returned no results. Ensure ALL required variables are provided and database schema is up-to-date. Most likely cause: non-existent module ID (contact ID/ticket ID/etc)"; + $return_arr['message'] = "Auth success but update query failed/returned no results. Ensure ALL required variables are provided and database schema is up-to-date. Most likely cause: non-existent module ID (i.e. bad contact ID/ticket ID/etc)."; + + // Log any database/schema related errors to the PHP Error log + if (mysqli_error($mysqli)) { + error_log("API Database Error: " . mysqli_error($mysqli)); + } } echo json_encode($return_arr); -exit(); \ No newline at end of file +exit(); diff --git a/api/v1/validate_api_key.php b/api/v1/validate_api_key.php index 6fa07065..e2f29590 100644 --- a/api/v1/validate_api_key.php +++ b/api/v1/validate_api_key.php @@ -16,9 +16,8 @@ header('Content-Type: application/json'); // POST data $_POST = json_decode(file_get_contents('php://input'), true); -// Get user IP +// Get IP & UA $ip = strip_tags(mysqli_real_escape_string($mysqli, getIP())); -// Get user agent $user_agent = strip_tags(mysqli_real_escape_string($mysqli, $_SERVER['HTTP_USER_AGENT'])); // Setup return array @@ -81,10 +80,10 @@ if (isset($api_key)) { header(WORDING_UNAUTHORIZED); echo json_encode($return_arr); exit(); - } - // Success - else { + } else { + + // SUCCESS // Set client ID, company ID & key name $row = mysqli_fetch_array($sql); @@ -95,21 +94,17 @@ if (isset($api_key)) { // Set limit & offset for queries if (isset($_GET['limit'])) { $limit = intval($_GET['limit']); - } - elseif (isset($_POST['limit'])) { + } elseif (isset($_POST['limit'])) { $limit = intval($_POST['limit']); - } - else { + } else { $limit = 50; } if (isset($_GET['offset'])) { $offset = intval($_GET['offset']); - } - elseif (isset($_POST['offset'])) { + } elseif (isset($_POST['offset'])) { $offset = intval($_POST['offset']); - } - else { + } else { $offset = 0; }