HexDecBinCalc/api.php

35 lines
1.2 KiB
PHP

<?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);