From ff741c223e99e0912830f346ae007819c5fd073c Mon Sep 17 00:00:00 2001 From: Marcus Hill Date: Sun, 1 Jan 2023 16:00:07 +0000 Subject: [PATCH] API code style tidy --- api/v1/assets/create.php | 154 +++++++++++------------ api/v1/assets/delete.php | 28 ++--- api/v1/assets/read.php | 40 +++--- api/v1/assets/update.php | 200 +++++++++++++++--------------- api/v1/certificates/read.php | 28 ++--- api/v1/clients/read.php | 22 ++-- api/v1/contacts/contact_model.php | 12 +- api/v1/contacts/create.php | 36 +++--- api/v1/contacts/delete.php | 26 ++-- api/v1/contacts/read.php | 14 +-- api/v1/contacts/update.php | 26 ++-- api/v1/create_output.php | 20 +-- api/v1/delete_output.php | 14 +-- api/v1/domains/read.php | 28 ++--- api/v1/networks/read.php | 28 ++--- api/v1/read_output.php | 28 ++--- api/v1/require_get_method.php | 16 +-- api/v1/require_post_method.php | 16 +-- api/v1/software/read.php | 40 +++--- api/v1/tickets/read.php | 16 +-- api/v1/update_output.php | 14 +-- api/v1/validate_api_key.php | 116 ++++++++--------- 22 files changed, 461 insertions(+), 461 deletions(-) diff --git a/api/v1/assets/create.php b/api/v1/assets/create.php index 1d79b843..7a63d58b 100644 --- a/api/v1/assets/create.php +++ b/api/v1/assets/create.php @@ -1,107 +1,107 @@ $insert_id - ]; +if (isset($insert_id) && is_numeric($insert_id)) { + // Insert successful + $return_arr['success'] = "True"; + $return_arr['count'] = '1'; + $return_arr['data'][] = [ + 'insert_id' => $insert_id + ]; } // 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 insert query failed, ensure ALL required variables are provided (and aren't duplicates where applicable) and database schema is up-to-date. Turn on error logging and look for 'undefined index'."; +else { + $return_arr['success'] = "False"; + $return_arr['message'] = "Auth success but insert query failed, ensure ALL required variables are provided (and aren't duplicates where applicable) and database schema is up-to-date. Turn on error logging and look for 'undefined index'."; } echo json_encode($return_arr); diff --git a/api/v1/delete_output.php b/api/v1/delete_output.php index d1ffe44c..c8139d92 100644 --- a/api/v1/delete_output.php +++ b/api/v1/delete_output.php @@ -7,16 +7,16 @@ */ // Check if delete query was successful -if(isset($delete_count) && is_numeric($delete_count) && $delete_count > 0){ - // Delete was successful - $return_arr['success'] = "True"; - $return_arr['count'] = $delete_count; +if (isset($delete_count) && is_numeric($delete_count) && $delete_count > 0) { + // Delete was successful + $return_arr['success'] = "True"; + $return_arr['count'] = $delete_count; } // Delete 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 delete query failed. Ensure ALL required variables are provided and database schema is up-to-date. Most likely cause: asset/client/company ID mismatch."; +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."; } echo json_encode($return_arr); diff --git a/api/v1/domains/read.php b/api/v1/domains/read.php index 423dc778..f0cf2b7a 100644 --- a/api/v1/domains/read.php +++ b/api/v1/domains/read.php @@ -1,30 +1,30 @@ 0){ - $return_arr['success'] = "True"; - $return_arr['count'] = mysqli_num_rows($sql); +if ($sql && mysqli_num_rows($sql) > 0) { + $return_arr['success'] = "True"; + $return_arr['count'] = mysqli_num_rows($sql); - $row = array(); - while($row = mysqli_fetch_array($sql)){ - $return_arr['data'][] = $row; - } + $row = array(); + while ($row = mysqli_fetch_array($sql)) { + $return_arr['data'][] = $row; + } - echo json_encode($return_arr); - exit(); + echo json_encode($return_arr); + exit(); } -else{ - $return_arr['success'] = "False"; - $return_arr['message'] = "No resource (for this client and company) with the specified parameter(s)."; - echo json_encode($return_arr); - exit(); +else { + $return_arr['success'] = "False"; + $return_arr['message'] = "No resource (for this client and company) with the specified parameter(s)."; + echo json_encode($return_arr); + exit(); } \ No newline at end of file diff --git a/api/v1/require_get_method.php b/api/v1/require_get_method.php index 4667bed7..4ce0b1f9 100644 --- a/api/v1/require_get_method.php +++ b/api/v1/require_get_method.php @@ -1,13 +1,13 @@ 0){ - // Insert successful - $return_arr['success'] = "True"; - $return_arr['count'] = $update_count; +if (isset($update_count) && is_numeric($update_count) && $update_count > 0) { + // Insert successful + $return_arr['success'] = "True"; + $return_arr['count'] = $update_count; } // 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)"; +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)"; } echo json_encode($return_arr); diff --git a/api/v1/validate_api_key.php b/api/v1/validate_api_key.php index a026e42b..5b85055e 100644 --- a/api/v1/validate_api_key.php +++ b/api/v1/validate_api_key.php @@ -7,8 +7,8 @@ */ // Includes -include( __DIR__ . '../../../functions.php'); -include(__DIR__ . "../../../config.php"); +require_once( __DIR__ . '../../../functions.php'); +require_once(__DIR__ . "../../../config.php"); // JSON header header('Content-Type: application/json'); @@ -17,9 +17,9 @@ header('Content-Type: application/json'); $_POST = json_decode(file_get_contents('php://input'), true); // Get user IP -$ip = strip_tags(mysqli_real_escape_string($mysqli,get_ip())); +$ip = strip_tags(mysqli_real_escape_string($mysqli, get_ip())); // Get user agent -$user_agent = strip_tags(mysqli_real_escape_string($mysqli,$_SERVER['HTTP_USER_AGENT'])); +$user_agent = strip_tags(mysqli_real_escape_string($mysqli, $_SERVER['HTTP_USER_AGENT'])); // Setup return array $return_arr = array(); @@ -43,75 +43,75 @@ DEFINE("WORDING_UNAUTHORIZED", "HTTP/1.1 401 Unauthorized"); */ // Decline methods other than GET/POST -if($_SERVER['REQUEST_METHOD'] !== "GET" && $_SERVER['REQUEST_METHOD'] !== "POST"){ - header("HTTP/1.1 405 Method Not Allowed"); - var_dump($_SERVER['REQUEST_METHOD']); - exit(); +if ($_SERVER['REQUEST_METHOD'] !== "GET" && $_SERVER['REQUEST_METHOD'] !== "POST") { + header("HTTP/1.1 405 Method Not Allowed"); + var_dump($_SERVER['REQUEST_METHOD']); + exit(); } // Check API key is provided -if(!isset($_GET['api_key']) && !isset($_POST['api_key'])){ - header(WORDING_UNAUTHORIZED); - exit(); +if (!isset($_GET['api_key']) && !isset($_POST['api_key'])) { + header(WORDING_UNAUTHORIZED); + exit(); } // Set API key variable -if(isset($_GET['api_key'])){ - $api_key = $_GET['api_key']; +if (isset($_GET['api_key'])) { + $api_key = $_GET['api_key']; } -if(isset($_POST['api_key'])){ - $api_key = $_POST['api_key']; +if (isset($_POST['api_key'])) { + $api_key = $_POST['api_key']; } // Validate API key -if(isset($api_key)){ - $api_key = mysqli_real_escape_string($mysqli,$api_key); +if (isset($api_key)) { + $api_key = mysqli_real_escape_string($mysqli, $api_key); - $sql = mysqli_query($mysqli,"SELECT * FROM api_keys WHERE api_key_secret = '$api_key' AND api_key_expire > NOW() LIMIT 1"); + $sql = mysqli_query($mysqli, "SELECT * FROM api_keys WHERE api_key_secret = '$api_key' AND api_key_expire > NOW() LIMIT 1"); - // Failed - if(mysqli_num_rows($sql) !== 1){ - // Invalid Key - header(WORDING_UNAUTHORIZED); - mysqli_query($mysqli,"INSERT INTO logs SET log_type = 'API', log_action = 'Failed', log_description = 'Incorrect or expired Key', log_ip = '$ip', log_user_agent = '$user_agent', log_created_at = NOW()"); + // Failed + if (mysqli_num_rows($sql) !== 1) { + // Invalid Key + header(WORDING_UNAUTHORIZED); + mysqli_query($mysqli, "INSERT INTO logs SET log_type = 'API', log_action = 'Failed', log_description = 'Incorrect or expired Key', log_ip = '$ip', log_user_agent = '$user_agent', log_created_at = NOW()"); - $return_arr['success'] = "False"; - $return_arr['message'] = "API Key authentication failure or expired."; + $return_arr['success'] = "False"; + $return_arr['message'] = "API Key authentication failure or expired."; - header(WORDING_UNAUTHORIZED); - echo json_encode($return_arr); - exit(); - } - - // Success - else{ - - // Set client ID, company ID & key name - $row = mysqli_fetch_array($sql); - $api_key_name = $row['api_key_name']; - $client_id = $row['api_key_client_id']; - $company_id = $row['company_id']; - - // Set limit & offset for queries - if(isset($_GET['limit'])){ - $limit = intval($_GET['limit']); - } - elseif(isset($_POST['limit'])){ - $limit = intval($_POST['limit']); - } - else{ - $limit = 50; + header(WORDING_UNAUTHORIZED); + echo json_encode($return_arr); + exit(); } - if(isset($_GET['offset'])){ - $offset = intval($_GET['offset']); - } - elseif(isset($_POST['offset'])){ - $offset = intval($_POST['offset']); - } - else{ - $offset = 0; - } + // Success + else { - } + // Set client ID, company ID & key name + $row = mysqli_fetch_array($sql); + $api_key_name = $row['api_key_name']; + $client_id = $row['api_key_client_id']; + $company_id = $row['company_id']; + + // Set limit & offset for queries + if (isset($_GET['limit'])) { + $limit = intval($_GET['limit']); + } + elseif (isset($_POST['limit'])) { + $limit = intval($_POST['limit']); + } + else { + $limit = 50; + } + + if (isset($_GET['offset'])) { + $offset = intval($_GET['offset']); + } + elseif (isset($_POST['offset'])) { + $offset = intval($_POST['offset']); + } + else { + $offset = 0; + } + + } } \ No newline at end of file