From a98bafed63f9322d54422861521c0c3c5d110d9a Mon Sep 17 00:00:00 2001 From: RaindropsSys Date: Sun, 14 May 2023 14:02:50 +0200 Subject: Updated 7 files and added 4 files (automated) --- pages/api/money/create.php | 99 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 pages/api/money/create.php (limited to 'pages/api/money/create.php') diff --git a/pages/api/money/create.php b/pages/api/money/create.php new file mode 100644 index 0000000..ff06a37 --- /dev/null +++ b/pages/api/money/create.php @@ -0,0 +1,99 @@ + 0) { + $myId = $fronters[0]["id"]; +} else { + $myId = "zdtsg"; +} + +$accounts = array_map(function ($i) { + $name = substr($i, 0, -5); + $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/money/" . $i), true); + $data["_name"] = $name; + return $data; +}, array_values(array_filter(scandir($_SERVER['DOCUMENT_ROOT'] . "/includes/data/money"), function ($i) { return !str_starts_with($i, "."); }))); +$rate = (float)trim(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/exchange.txt")); + +$obj = [ + "success" => false, + "error" => null +]; + +if ($_SERVER["REQUEST_METHOD"] !== "POST") { + $obj["success"] = false; + $obj["error"] = "INVALID_METHOD"; + die(json_encode($obj, JSON_PRETTY_PRINT)); +} + +if (!isset($data["amount"]) || !isset($data["id"]) || !isset($data["description"])) { + $obj["success"] = false; + $obj["error"] = "MISSING_OPERAND"; + die(json_encode($obj, JSON_PRETTY_PRINT)); +} + +$account = array_values(array_filter($accounts, function ($i) use ($data) { return $i["_name"] === $data["id"]; }))[0] ?? null; + +if (!isset($account)) { + $obj["success"] = false; + $obj["error"] = "ACCOUNT_NOT_FOUND"; + die(json_encode($obj, JSON_PRETTY_PRINT)); +} + +if (!(isset($data["amount"]) && is_numeric($data["amount"]) && (float)$data["amount"] < 9999 && (float)$data["amount"] > -9999)) { + $obj["success"] = false; + $obj["error"] = "INVALID_AMOUNT"; + die(json_encode($obj, JSON_PRETTY_PRINT)); +} +if (!isset($data["description"])) $data["description"] = ""; + +if (strlen($data["description"]) > 150) { + $obj["success"] = false; + $obj["error"] = "DESCRIPTION_TOO_LONG"; + die(json_encode($obj, JSON_PRETTY_PRINT)); +} + +if ($data["amount"] === 0) { + $obj["success"] = false; + $obj["error"] = "AMOUNT_IS_ZERO"; + die(json_encode($obj, JSON_PRETTY_PRINT)); +} + +$ntfy = $GLOBALS["ColdHazeApp"]["ntfy"]; +file_get_contents('https://' . $ntfy["server"] . '/' . $ntfy["topic"], false, stream_context_create([ + 'http' => [ + 'method' => 'POST', + 'header' => + "Content-Type: text/plain\r\n" . + "Title: " . formatPonypush((getMember($myId)["display_name"] ?? getMember($myId)["name"]) . " created a transaction to " . $account["name"] . " (" . ucfirst($account["owner"]) . ")") . "\r\n" . + "Tags: bits\r\n" . + "Authorization: Basic " . base64_encode($ntfy["user"] . ":" . $ntfy["password"]), + 'content' => formatPonypush(($account["currency"] === "gbp" ? "£" : "€") . abs((float)$_GET["amount"]) . " were " . ((float)$_GET["amount"] >= 0 ? "added" : "removed") . " just now" . (trim($_GET["description"]) !== "" ? ": " . $_GET["description"] : "")) + ] +])); + +array_unshift($account["transactions"], [ + "author" => $myId, + "description" => $_GET["description"], + "amount" => (float)$_GET["amount"], + "date" => date('c') +]); + +$name = $account["_name"]; +unset($account["_name"]); + +file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/money/" . $name . ".json", json_encode($account, JSON_PRETTY_PRINT)); + +$obj["success"] = true; +die(json_encode($obj, JSON_PRETTY_PRINT)); \ No newline at end of file -- cgit