From f4830694d41166e2932e706d6ba991e0eab3231d Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Fri, 4 Nov 2022 00:03:33 -0400 Subject: [PATCH] Add api and usage page --- .idea/vcs.xml | 6 ++++++ api-usage.html | 36 ++++++++++++++++++++++++++++++++++++ api.php | 34 ++++++++++++++++++++++++++++++++++ conversions.php | 22 ++++++++++++++++++++++ index.php | 40 ++++++++++++++++++++-------------------- style.css | 39 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 157 insertions(+), 20 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 api-usage.html create mode 100644 api.php diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api-usage.html b/api-usage.html new file mode 100644 index 0000000..ee4d7a0 --- /dev/null +++ b/api-usage.html @@ -0,0 +1,36 @@ + + + + + API Usage + + + +
+

API Usage

+
+ +

Parameters

+
    +
  1. Binary: binary
  2. +
  3. Decimal: decimal
  4. +
  5. Hexadecimal: hexadecimal
  6. +
+ +

Usage and Response

+
    +
  1. GET request to https://dev.nevets.tech/calculator/hexdecbin/api.php?(parameter)=(value)
  2. +
  3. API responds with a json string similar to below
  4. +
    +          {
    +            "binary":1101011,
    +            "decimal":107,
    +            "hexadecimal":"6B"
    +          }
    +        
    +
  5. Reminder: Only use one parameter, more than one will return a 422 error!
  6. +
+
+
+ + \ No newline at end of file diff --git a/api.php b/api.php new file mode 100644 index 0000000..acaf94e --- /dev/null +++ b/api.php @@ -0,0 +1,34 @@ + &$binary, "decimal" => &$decimal, "hexadecimal" => &$hexadecimal); + +if (isset($_GET['binary']) && !isset($_GET['decimal']) && !isset($_GET['hexadecimal'])) { + $binary = sanitize_int_input($_GET['binary']); + $decimal = binary_to_decimal($binary); + $hexadecimal = binary_to_hex($binary); +} + +if (!isset($_GET['binary']) && isset($_GET['decimal']) && !isset($_GET['hexadecimal'])) { + $decimal = sanitize_int_input($_GET['decimal']); + $binary = decimal_to_binary($decimal); + $hexadecimal = decimal_to_hex($decimal); +} + +if (!isset($_GET['binary']) && !isset($_GET['decimal']) && isset($_GET['hexadecimal'])) { + $hexadecimal = sanitize_string_input($_GET['hexadecimal']); + $binary = hex_to_binary($hexadecimal); + $decimal = hex_to_decimal($hexadecimal); +} + +if ((!isset($_GET['binary']) && !isset($_GET['decimal']) && !isset($_GET['hexadecimal']) || + (isset($_GET['binary']) && isset($_GET['decimal']) && isset($_GET['hexadecimal'])))) { + http_response_code(422); + exit(); +} + +echo json_encode($response); diff --git a/conversions.php b/conversions.php index 86b3b95..7a3ebad 100644 --- a/conversions.php +++ b/conversions.php @@ -1,6 +1,7 @@ 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, "A" => 10, "B" => 11, "C" => 12, "D" => 13, "E" => 14, "F" => 15); $decimal = 0; @@ -13,11 +14,13 @@ function hex_to_decimal(string $hex) : int { } function hex_to_binary(string $hex) : int { + $hex = sanitize_string_input($hex); $decimal = hex_to_decimal($hex); return decimal_to_binary($decimal); } function decimal_to_hex(int $decimal) : string { + $decimal = sanitize_int_input($decimal); $chars = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => "A", 11 => "B", 12 => "C", 13 => "D", 14 => "E", 15 => "F"); $remainder = $decimal; @@ -38,6 +41,7 @@ function decimal_to_hex(int $decimal) : string { } function decimal_to_binary(int $decimal) : int { + $decimal = sanitize_int_input($decimal); $remainder = $decimal; $greatest_base_2 = 0; $binary_str = ""; @@ -58,6 +62,7 @@ function decimal_to_binary(int $decimal) : int { } function binary_to_decimal(int $binary) : int { + $binary = sanitize_int_input($binary); $digits = str_split(strrev(strval($binary))); $decimal = 0; for ($i = 0; $i < sizeof($digits); $i++) { @@ -70,6 +75,23 @@ function binary_to_decimal(int $binary) : int { } function binary_to_hex(int $binary) : string { + $binary = sanitize_int_input($binary); $decimal = binary_to_decimal($binary); return decimal_to_hex($decimal); +} + +function sanitize_int_input(int $input) : int { + $input = trim($input); + $input = stripslashes($input); + $input = htmlspecialchars($input); + + return intval($input); +} + +function sanitize_string_input(string $input) : string { + $input = trim($input); + $input = stripslashes($input); + $input = htmlspecialchars($input); + + return strval($input); } \ No newline at end of file diff --git a/index.php b/index.php index b87de5f..ec323de 100644 --- a/index.php +++ b/index.php @@ -9,38 +9,40 @@ include "./conversions.php";
-
+