This repository has been archived on 2024-08-28. You can view files and clone it, but cannot push or open issues or pull requests.
satori-services/public/exrate.php

84 lines
3.3 KiB
PHP
Raw Normal View History

2022-07-04 00:07:38 +00:00
<?php
$config = parse_ini_file(__DIR__ . '/../config/flashii.ini');
try {
$pdo = new PDO($config['exrate-dsn'], $config['exrate-user'], $config['exrate-pass'], [
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::MYSQL_ATTR_INIT_COMMAND => "
SET SESSION
sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION',
time_zone = '+00:00';
",
]);
} catch(PDOException $ex) {
die((string)$ex);
}
define('EXRATE_INTER', 'EUR');
define('EXRATE_COMMON', [
'EUR', 'AUD', 'GBP', 'CAD', 'USD', 'JPY', 'PLN', 'SGD', 'RUB', 'ILS',
]);
$from = strtoupper((string)filter_input(INPUT_GET, 'from', FILTER_SANITIZE_STRING));
$to = strtoupper((string)filter_input(INPUT_GET, 'to', FILTER_SANITIZE_STRING));
$amount = (string)(filter_input(INPUT_GET, 'amount', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION) ?? '1');
if((!empty($to) && strlen($to) !== 3) || strlen($from) !== 3) {
http_response_code(400);
die('Invalid currency specified.');
}
$needsRefresh = $pdo->prepare('SELECT MAX(`rate_stored`) <= NOW() - INTERVAL 1 DAY FROM `exchange-rates` LIMIT 1');
$needsRefresh = $needsRefresh->execute() ? $needsRefresh->fetchColumn() : 1;
if($needsRefresh !== 0) {
$data = json_decode(file_get_contents('https://api.exchangerate.host/latest?base=' . EXRATE_INTER), true);
if($data !== null) {
$pdo->exec('TRUNCATE `exchange-rates`;');
$insertCurrency = $pdo->prepare('REPLACE INTO `exchange-rates` (`rate_from`, `rate_to`, `rate_value`) VALUES (:from, :to, :value)');
$insertCurrency->bindValue('from', $data['base']);
foreach($data['rates'] as $currency => $rate) {
$insertCurrency->bindValue('to', $currency);
$insertCurrency->bindValue('value', $rate);
$insertCurrency->execute();
}
}
}
$result = new stdClass;
$result->from = $from;
$result->to = $to;
$result->amount = (float)$amount;
if($from === $to) {
$result->result = $result->amount;
} else {
$convertCurrency = $pdo->prepare('SELECT (SELECT (:amount / `rate_value`) FROM `exchange-rates` WHERE `rate_from` = \'' . EXRATE_INTER . '\' AND `rate_to` = :from) * `rate_value` FROM `exchange-rates` WHERE `rate_from` = \'' . EXRATE_INTER . '\' AND `rate_to` = :to;');
$convertCurrency->bindValue('from', $from);
$convertCurrency->bindValue('amount', $amount);
if(empty($to)) {
$result->results = [];
foreach(EXRATE_COMMON as $commonCurrency) {
if($commonCurrency === $from)
continue;
$result->results[] = $current = new stdClass;
$current->to = $commonCurrency;
$convertCurrency->bindValue('to', $commonCurrency);
$convertCurrency->execute();
$current->result = $convertCurrency->fetchColumn() ?? 0;
}
} else {
$convertCurrency->bindValue('to', $to);
$convertCurrency->execute();
$result->result = $convertCurrency->fetchColumn() ?? 0;
}
}
http_response_code(200);
header('Content-Type: application/json');
echo json_encode($result);