Files
Epicnabbo-Catalogus-Updated…/Updated_Cms/app/Services/FindRetrosService.php
T
Remco 7b9849c159 🆙 More fixes 🆙
2026-01-19 20:43:46 +01:00

94 lines
2.2 KiB
PHP

<?php
namespace App\Services;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Cache;
/* Credits to Kani for this */
class FindRetrosService
{
/**
* The findretros verification uri.
*/
public const FIND_RETROS_VERIFY_URI = '%s/validate.php?user=%s&ip=%s';
/**
* The findretros redirect uri.
*/
public const FIND_RETROS_REDIRECT_URI = '%s/servers/%s/vote?minimal=1&return=1';
public const FIND_RETROS_CACHE_KEY = 'voted.%s';
/**
* The guzzle client instance
*/
protected Client $client;
/**
* Initialise Find Retros Service
*/
public function __construct()
{
$this->client = new Client(['verify' => false]);
}
/**
* Check the user has voted.
*/
public function checkHasVoted(): bool
{
if (! config('habbo.findretros.enabled')) {
return true;
}
$ip = request()->ip();
$cacheKey = sprintf(self::FIND_RETROS_CACHE_KEY, is_scalar($ip) ? (string) $ip : '');
if ($ip === '127.0.0.1') {
return true;
}
if (request()->has('novote')) {
return true;
}
if (Cache::has($cacheKey)) {
return true;
}
$api = config('habbo.findretros.api');
$name = config('habbo.findretros.name');
$uri = sprintf(self::FIND_RETROS_VERIFY_URI,
is_scalar($api) ? (string) $api : '',
is_scalar($name) ? (string) $name : '',
is_scalar($ip) ? (string) $ip : ''
);
$request = $this->client->get($uri);
$response = $request->getBody()->getContents();
if (in_array($response, ['1', '2'])) {
Cache::put($cacheKey, true, now()->addMinutes(30));
return true;
}
return false;
}
/**
* Retrieve the find retros redirect url.
*/
public function getRedirectUri(): string
{
$api = config('habbo.findretros.api');
$name = config('habbo.findretros.name');
return sprintf(self::FIND_RETROS_REDIRECT_URI,
is_scalar($api) ? (string) $api : '',
is_scalar($name) ? (string) $name : ''
);
}
}