Add api and usage page
This commit is contained in:
parent
e96329ac03
commit
f4830694d4
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
36
api-usage.html
Normal file
36
api-usage.html
Normal file
@ -0,0 +1,36 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>API Usage</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="api-container">
|
||||
<h2>API Usage</h2>
|
||||
<div class="text">
|
||||
|
||||
<p>Parameters</p>
|
||||
<ol>
|
||||
<li>Binary: <code>binary</code></li>
|
||||
<li>Decimal: <code>decimal</code></li>
|
||||
<li>Hexadecimal: <code>hexadecimal</code></li>
|
||||
</ol>
|
||||
|
||||
<p>Usage and Response</p>
|
||||
<ol class="no-dots">
|
||||
<li>GET request to <code>https://dev.nevets.tech/calculator/hexdecbin/api.php?(parameter)=(value)</code></li>
|
||||
<li>API responds with a json string similar to below</li>
|
||||
<pre>
|
||||
{
|
||||
"binary":1101011,
|
||||
"decimal":107,
|
||||
"hexadecimal":"6B"
|
||||
}
|
||||
</pre>
|
||||
<li><b>Reminder: </b>Only use one parameter, more than one will return a 422 error!</li>
|
||||
</ol>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
34
api.php
Normal file
34
api.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
include "./conversions.php";
|
||||
|
||||
header("Content-Type: application/json");
|
||||
|
||||
$binary = $decimal = $hexadecimal = 0;
|
||||
|
||||
$response = array("binary" => &$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);
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
function hex_to_decimal(string $hex) : int {
|
||||
$hex = sanitize_string_input($hex);
|
||||
$chars = array(0 => 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);
|
||||
}
|
40
index.php
40
index.php
@ -9,38 +9,40 @@ include "./conversions.php";
|
||||
</head>
|
||||
<body>
|
||||
<div class="inputs">
|
||||
<form action="index.php" method="get">
|
||||
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="get">
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
$bin = 0;
|
||||
$dec = 0;
|
||||
$hex = 0;
|
||||
$bin = $dec = $hex = 0;
|
||||
$new = false;
|
||||
|
||||
if (!isset($_GET['binary']) ||
|
||||
!isset($_GET['decimal']) ||
|
||||
!isset($_GET['hexadecimal'])) {
|
||||
// Check if GET params are set
|
||||
if (!isset($_GET['binary']) || !isset($_GET['decimal']) || !isset($_GET['hexadecimal'])) {
|
||||
$new = true;
|
||||
}
|
||||
|
||||
if (!isset($_SESSION['bin']) ||
|
||||
!isset($_SESSION['dec']) ||
|
||||
!isset($_SESSION['hex'])) {
|
||||
$_SESSION['bin'] = 0;
|
||||
$_SESSION['dec'] = 0;
|
||||
$_SESSION['hex'] = 0;
|
||||
if (!isset($_SESSION['bin']) || !isset($_SESSION['dec']) || !isset($_SESSION['hex'])) {
|
||||
$_SESSION['bin'] = $_SESSION['dec'] = $_SESSION['hex'] = 0;
|
||||
}
|
||||
|
||||
if (!$new) {
|
||||
$last_bin = $_SESSION['bin'];
|
||||
$last_dec = $_SESSION['dec'];
|
||||
$last_hex = $_SESSION['hex'];
|
||||
$last_bin = $_SESSION['bin'];
|
||||
$last_dec = $_SESSION['dec'];
|
||||
$last_hex = $_SESSION['hex'];
|
||||
|
||||
if (!$new) {
|
||||
$bin = $_GET['binary'];
|
||||
$dec = $_GET['decimal'];
|
||||
$hex = $_GET['hexadecimal'];
|
||||
}
|
||||
|
||||
// Check if it's a pasted url (all params are passed and aren't 0)
|
||||
if (isset($_GET['binary']) && isset($_GET['decimal']) && isset($_GET['hexadecimal']) &&
|
||||
$_GET['binary'] != 0 && $_GET['decimal'] != 0 && $_GET['hexadecimal'] != 0 &&
|
||||
$bin == $last_bin && $dec == $last_dec && $hex == $last_hex) {
|
||||
$new = true;
|
||||
}
|
||||
|
||||
if (!$new) {
|
||||
if ($bin == 0 && $dec == 0) {
|
||||
$bin = hex_to_binary($hex);
|
||||
$_SESSION['bin'] = $bin;
|
||||
@ -80,9 +82,7 @@ include "./conversions.php";
|
||||
$hex = binary_to_hex($bin);
|
||||
$_SESSION['hex'] = $hex;
|
||||
} else {
|
||||
$bin = 0;
|
||||
$dec = 0;
|
||||
$hex = 0;
|
||||
$bin = $dec = $hex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
39
style.css
39
style.css
@ -18,6 +18,23 @@
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.api-container {
|
||||
width: 75%;
|
||||
margin: 25px auto auto;
|
||||
border: 2px solid black;
|
||||
border-radius: 50px;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.text {
|
||||
margin-left: 10%;
|
||||
line-height: 150%;
|
||||
}
|
||||
|
||||
.no-dots {
|
||||
list-style-type: none;
|
||||
}
|
||||
|
||||
div {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
@ -33,3 +50,25 @@ input {
|
||||
width: 25%;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
h2 {
|
||||
text-align: center;
|
||||
margin: auto;
|
||||
width: 50%
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: monospace, Serif;
|
||||
background-color: #1d1d2d;
|
||||
padding: 2px 5px;
|
||||
border-radius: 5px;
|
||||
width: auto;
|
||||
}
|
||||
|
||||
pre {
|
||||
font-family: monospace, Serif;
|
||||
background-color: #1d1d2d;
|
||||
padding: 2px 5px;
|
||||
border-radius: 5px;
|
||||
width: 43%;
|
||||
}
|
Loading…
Reference in New Issue
Block a user