Wallos v0.9
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
|
||||
if (isset($_GET['action']) && $_GET['action'] == "add") {
|
||||
$currencyName = "Currency";
|
||||
$currencySymbol = "$";
|
||||
$currencyCode = "CODE";
|
||||
$currencyRate = 1;
|
||||
$sqlInsert = "INSERT INTO currencies (name, symbol, code, rate) VALUES (:name, :symbol, :code, :rate)";
|
||||
$stmtInsert = $db->prepare($sqlInsert);
|
||||
$stmtInsert->bindParam(':name', $currencyName, SQLITE3_TEXT);
|
||||
$stmtInsert->bindParam(':symbol', $currencySymbol, SQLITE3_TEXT);
|
||||
$stmtInsert->bindParam(':code', $currencyCode, SQLITE3_TEXT);
|
||||
$stmtInsert->bindParam(':rate', $currencyRate, SQLITE3_TEXT);
|
||||
$resultInsert = $stmtInsert->execute();
|
||||
|
||||
if ($resultInsert) {
|
||||
$currencyId = $db->lastInsertRowID();
|
||||
echo $currencyId;
|
||||
} else {
|
||||
echo "Error adding currency entry.";
|
||||
}
|
||||
} else if (isset($_GET['action']) && $_GET['action'] == "edit") {
|
||||
if (isset($_GET['currencyId']) && $_GET['currencyId'] != "" && isset($_GET['name']) && $_GET['name'] != "" && isset($_GET['symbol']) && $_GET['symbol'] != "") {
|
||||
$currencyId = $_GET['currencyId'];
|
||||
$name = $_GET['name'];
|
||||
$symbol = $_GET['symbol'];
|
||||
$code = $_GET['code'];
|
||||
$sql = "UPDATE currencies SET name = :name, symbol = :symbol, code = :code WHERE id = :currencyId";
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->bindParam(':name', $name, SQLITE3_TEXT);
|
||||
$stmt->bindParam(':symbol', $symbol, SQLITE3_TEXT);
|
||||
$stmt->bindParam(':code', $code, SQLITE3_TEXT);
|
||||
$stmt->bindParam(':currencyId', $currencyId, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
|
||||
if ($result) {
|
||||
echo json_encode(["success" => true]);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Failed to store Currency on the Database"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Some fields are missing"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
} else if (isset($_GET['action']) && $_GET['action'] == "delete") {
|
||||
if (isset($_GET['currencyId']) && $_GET['currencyId'] != "") {
|
||||
$query = "SELECT main_currency FROM user WHERE id = 1";
|
||||
$stmt = $db->prepare($query);
|
||||
$result = $stmt->execute();
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
$mainCurrencyId = $row['main_currency'];
|
||||
|
||||
$currencyId = $_GET['currencyId'];
|
||||
$checkQuery = "SELECT COUNT(*) FROM subscriptions WHERE currency_id = :currencyId";
|
||||
$checkStmt = $db->prepare($checkQuery);
|
||||
$checkStmt->bindParam(':currencyId', $currencyId, SQLITE3_INTEGER);
|
||||
$checkResult = $checkStmt->execute();
|
||||
$row = $checkResult->fetchArray();
|
||||
$count = $row[0];
|
||||
|
||||
if ($count > 0) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Currency is in use in subscriptions and can't be deleted."
|
||||
];
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
} else {
|
||||
if ($currencyId == $mainCurrencyId) {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Currency is set as main currency and can't be deleted."
|
||||
];
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
} else {
|
||||
$sql = "DELETE FROM currencies WHERE id = :currencyId";
|
||||
$stmt = $db->prepare($sql);
|
||||
$stmt->bindParam(':currencyId', $currencyId, SQLITE3_INTEGER);
|
||||
$result = $stmt->execute();
|
||||
if ($result) {
|
||||
echo json_encode(["success" => true]);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Failed to remove currency from the Database"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Some fields are missing."
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
} else {
|
||||
echo "Error";
|
||||
}
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Your session expired. Please login again"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
session_start();
|
||||
|
||||
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true) {
|
||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||
$newApiKey = isset($_POST["api_key"]) ? $_POST["api_key"] : "";
|
||||
$removeOldKey = "DELETE FROM fixer";
|
||||
$db->exec($removeOldKey);
|
||||
$testKeyUrl = "http://data.fixer.io/api/latest?access_key=$newApiKey";
|
||||
$response = file_get_contents($testKeyUrl);
|
||||
$apiData = json_decode($response, true);
|
||||
if ($apiData['success'] && $apiData['success'] == 1) {
|
||||
if (!empty($newApiKey)) {
|
||||
$insertNewKey = "INSERT INTO fixer (api_key) VALUES (:api_key)";
|
||||
$stmt = $db->prepare($insertNewKey);
|
||||
$stmt->bindParam(":api_key", $newApiKey, SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
if ($result) {
|
||||
echo json_encode(["success" => true]);
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Failed to store API Key on the Database"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
} else {
|
||||
echo json_encode(["success" => true]);
|
||||
}
|
||||
} else {
|
||||
$response = [
|
||||
"success" => false,
|
||||
"message" => "Invalid API Key"
|
||||
];
|
||||
echo json_encode($response);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
require_once '../../includes/connect_endpoint.php';
|
||||
|
||||
$shouldUpdate = true;
|
||||
$query = "SELECT date FROM last_exchange_update";
|
||||
$result = $db->querySingle($query);
|
||||
|
||||
if ($result) {
|
||||
$lastUpdateDate = new DateTime($result);
|
||||
$currentDate = new DateTime();
|
||||
$lastUpdateDateString = $lastUpdateDate->format('Y-m-d');
|
||||
$currentDateString = $currentDate->format('Y-m-d');
|
||||
$shouldUpdate = $lastUpdateDateString < $currentDateString;
|
||||
}
|
||||
|
||||
if (!$shouldUpdate) {
|
||||
echo "Rates are current, no need to update.";
|
||||
exit;
|
||||
}
|
||||
|
||||
$query = "SELECT api_key FROM fixer";
|
||||
$result = $db->query($query);
|
||||
|
||||
if ($result) {
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
|
||||
if ($row) {
|
||||
$apiKey = $row['api_key'];
|
||||
|
||||
$codes = "";
|
||||
$query = "SELECT id, name, symbol, code FROM currencies";
|
||||
$result = $db->query($query);
|
||||
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
|
||||
$codes .= $row['code'].",";
|
||||
}
|
||||
$codes = rtrim($codes, ',');
|
||||
$query = "SELECT u.main_currency, c.code FROM user u LEFT JOIN currencies c ON u.main_currency = c.id WHERE u.id = 1";
|
||||
$stmt = $db->prepare($query);
|
||||
$result = $stmt->execute();
|
||||
$row = $result->fetchArray(SQLITE3_ASSOC);
|
||||
$mainCurrencyCode = $row['code'];
|
||||
$mainCurrencyId = $row['main_currency'];
|
||||
|
||||
$api_url = "http://data.fixer.io/api/latest?access_key=". $apiKey . "&base=EUR&symbols=" . $codes;
|
||||
$response = file_get_contents($api_url);
|
||||
$apiData = json_decode($response, true);
|
||||
|
||||
$mainCurrencyToEUR = $apiData['rates'][$mainCurrencyCode];
|
||||
|
||||
if ($apiData !== null && isset($apiData['rates'])) {
|
||||
foreach ($apiData['rates'] as $currencyCode => $rate) {
|
||||
if ($currencyCode === $mainCurrencyCode) {
|
||||
$exchangeRate = 1.0;
|
||||
} else {
|
||||
$exchangeRate = $rate / $mainCurrencyToEUR;
|
||||
}
|
||||
$updateQuery = "UPDATE currencies SET rate = :rate WHERE code = :code";
|
||||
$updateStmt = $db->prepare($updateQuery);
|
||||
$updateStmt->bindParam(':rate', $exchangeRate, SQLITE3_TEXT);
|
||||
$updateStmt->bindParam(':code', $currencyCode, SQLITE3_TEXT);
|
||||
$updateResult = $updateStmt->execute();
|
||||
|
||||
if (!$updateResult) {
|
||||
echo "Error updating rate for currency: $currencyCode";
|
||||
}
|
||||
}
|
||||
$currentDate = new DateTime();
|
||||
$formattedDate = $currentDate->format('Y-m-d');
|
||||
|
||||
$deleteQuery = "DELETE FROM last_exchange_update";
|
||||
$deleteStmt = $db->prepare($deleteQuery);
|
||||
$deleteResult = $deleteStmt->execute();
|
||||
|
||||
$query = "INSERT INTO last_exchange_update (date) VALUES (:formattedDate)";
|
||||
$stmt = $db->prepare($query);
|
||||
$stmt->bindParam(':formattedDate', $formattedDate, SQLITE3_TEXT);
|
||||
$result = $stmt->execute();
|
||||
|
||||
$db->close();
|
||||
echo "Rates updated successfully!";
|
||||
}
|
||||
} else {
|
||||
echo "Exchange rates update skipped. No fixer.io api key provided";
|
||||
$apiKey = null;
|
||||
}
|
||||
} else {
|
||||
echo "Exchange rates update skipped. No fixer.io api key provided";
|
||||
$apiKey = null;
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user