2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
namespace openvk\ServiceAPI;
|
|
|
|
|
2024-03-30 13:59:10 +03:00
|
|
|
use openvk\Web\Models\Entities\APIToken;
|
2022-08-20 21:07:54 +03:00
|
|
|
use openvk\Web\Models\Entities\User;
|
2024-03-30 13:59:10 +03:00
|
|
|
use openvk\Web\Models\Repositories\APITokens;
|
2022-08-20 21:07:54 +03:00
|
|
|
use openvk\Web\Models\Repositories\Applications;
|
2024-03-30 13:59:10 +03:00
|
|
|
use WhichBrowser;
|
2022-08-20 21:07:54 +03:00
|
|
|
|
|
|
|
class Apps implements Handler
|
|
|
|
{
|
|
|
|
private $user;
|
|
|
|
private $apps;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
public function __construct(?User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
2025-01-31 18:20:13 +03:00
|
|
|
$this->apps = new Applications();
|
2022-08-20 21:07:54 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUserInfo(callable $resolve, callable $reject): void
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
$hexId = dechex($this->user->getId());
|
|
|
|
$sign = hash_hmac("sha512/224", $hexId, CHANDLER_ROOT_CONF["security"]["secret"], true);
|
|
|
|
$marketingId = $hexId . "_" . base64_encode($sign);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
$resolve([
|
|
|
|
"id" => $this->user->getId(),
|
|
|
|
"marketing_id" => $marketingId,
|
|
|
|
"name" => [
|
|
|
|
"first" => $this->user->getFirstName(),
|
|
|
|
"last" => $this->user->getLastName(),
|
|
|
|
"full" => $this->user->getFullName(),
|
|
|
|
],
|
|
|
|
"ava" => $this->user->getAvatarUrl(),
|
|
|
|
]);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function updatePermission(int $app, string $perm, string $state, callable $resolve, callable $reject): void
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
$app = $this->apps->get($app);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$app || !$app->isEnabled()) {
|
2022-08-20 21:07:54 +03:00
|
|
|
$reject("No application with this id found");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
if (!$app->setPermission($this->user, $perm, $state == "yes")) {
|
2022-08-20 21:07:54 +03:00
|
|
|
$reject("Invalid permission $perm");
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
$resolve(1);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function pay(int $appId, float $amount, callable $resolve, callable $reject): void
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
$app = $this->apps->get($appId);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$app || !$app->isEnabled()) {
|
2022-08-20 21:07:54 +03:00
|
|
|
$reject("No application with this id found");
|
|
|
|
return;
|
|
|
|
}
|
2022-11-14 17:22:23 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($amount < 0) {
|
2022-11-14 17:22:23 +03:00
|
|
|
$reject(552, "Payment amount is invalid");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
$coinsLeft = $this->user->getCoins() - $amount;
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($coinsLeft < 0) {
|
2022-08-20 21:07:54 +03:00
|
|
|
$reject(41, "Not enough money");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
$this->user->setCoins($coinsLeft);
|
|
|
|
$this->user->save();
|
|
|
|
$app->addCoins($amount);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
$t = time();
|
|
|
|
$resolve($t . "," . hash_hmac("whirlpool", "$appId:$amount:$t", CHANDLER_ROOT_CONF["security"]["secret"]));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function withdrawFunds(int $appId, callable $resolve, callable $reject): void
|
2022-08-20 21:07:54 +03:00
|
|
|
{
|
|
|
|
$app = $this->apps->get($appId);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$app) {
|
2022-08-20 21:07:54 +03:00
|
|
|
$reject("No application with this id found");
|
|
|
|
return;
|
2025-01-31 18:20:13 +03:00
|
|
|
} elseif ($app->getOwner()->getId() != $this->user->getId()) {
|
2022-08-20 21:07:54 +03:00
|
|
|
$reject("You don't have rights to edit this app");
|
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2022-08-20 21:07:54 +03:00
|
|
|
$coins = $app->getBalance();
|
|
|
|
$app->withdrawCoins();
|
|
|
|
$resolve($coins);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getRegularToken(string $clientName, bool $acceptsStale, callable $resolve, callable $reject): void
|
2024-03-30 13:59:10 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
$token = null;
|
2024-03-30 13:59:10 +03:00
|
|
|
$stale = true;
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($acceptsStale) {
|
|
|
|
$token = (new APITokens())->getStaleByUser($this->user->getId(), $clientName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($token)) {
|
2024-03-30 13:59:10 +03:00
|
|
|
$stale = false;
|
2025-01-31 18:20:13 +03:00
|
|
|
$token = new APIToken();
|
2024-03-30 13:59:10 +03:00
|
|
|
$token->setUser($this->user);
|
|
|
|
$token->setPlatform($clientName ?? (new WhichBrowser\Parser(getallheaders()))->toString());
|
|
|
|
$token->save();
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2024-03-30 13:59:10 +03:00
|
|
|
$resolve([
|
|
|
|
'is_stale' => $stale,
|
|
|
|
'token' => $token->getFormattedToken(),
|
|
|
|
]);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|