diff --git a/api/actions/profile.php b/api/actions/profile.php new file mode 100644 index 0000000..a29d3c3 --- /dev/null +++ b/api/actions/profile.php @@ -0,0 +1,25 @@ +has("accounts", ["publicid" => $VARS["id"]])) { + sendJsonResp($Strings->get("Could not find a matching account.", false), "ERROR"); +} + +$profile = $database->get("accounts", ["publicid", "name", "username", "type", "verified"], ["publicid" => $VARS["id"]]); + +// Make sure the name field always has something useful +if (empty($profile["name"])) { + $profile["name"] = $profile["username"]; +} + +$profile["verified"] = $profile["verified"] == 1; + +exitWithJson([ + "status" => "OK", + "profile" => $profile +]); diff --git a/api/actions/send.php b/api/actions/send.php new file mode 100644 index 0000000..cce3b70 --- /dev/null +++ b/api/actions/send.php @@ -0,0 +1,41 @@ +get("accounts", ["[>]authkeys" => "accountid"], ["accounts.accountid", "balance", "type"], ["authkeys.key" => $VARS["key"]]); + +if (!$database->has("accounts", ["publicid" => $VARS["to"]])) { + sendJsonResp($Strings->get("Could not find the receiving account.", false), "ERROR"); +} + +$receiver = $database->get("accounts", ["accountid", "balance", "type"], ["publicid" => $VARS["to"]]); + +$amount = $VARS["amount"] * 1.0; + +if ($amount > $sender["balance"]) { + sendJsonResp($Strings->get("Insufficient funds.", false), "ERROR"); +} + +if (($sender["type"] === 1 && $receiver["type"] === 2) || ($sender["type"] === 2 && $receiver["type"] === 3)) { + $database->action(function ($database) { + global $VARS, $sender, $receiver, $amount; + + $database->update("accounts", ["balance[+]" => $amount], ["accountid" => $receiver["accountid"]]); + $database->update("accounts", ["balance[-]" => $amount], ["accountid" => $sender["accountid"]]); + + $database->insert("transactions", [ + "amount" => $amount, + "fromid" => $sender["accountid"], + "toid" => $receiver["accountid"], + "datetime" => date("Y-m-d H:i:s") + ]); + }); + + sendJsonResp(); +} else { + sendJsonResp($Strings->get("Money cannot be sent from receivers to givers.", false), "ERROR"); +} \ No newline at end of file diff --git a/api/apisettings.php b/api/apisettings.php index 86d0a76..25977e9 100644 --- a/api/apisettings.php +++ b/api/apisettings.php @@ -58,5 +58,12 @@ $APIS = [ "to" => "/[0-9a-z]+/", "amount" => "/[0-9]{1,4}(\.[0-9]{2})?/" ] + ], + "getprofile" => [ + "load" => "profile.php", + "vars" => [ + "key" => $keyregex, + "id" => "/[0-9a-z]+/" + ] ] ]; diff --git a/database.mwb b/database.mwb index 5af4679..d081387 100644 Binary files a/database.mwb and b/database.mwb differ