54 lines
1.2 KiB
PHP
54 lines
1.2 KiB
PHP
<?php
|
|
|
|
function auth_get_client_ip(): string
|
|
{
|
|
$forwarded = $_SERVER["HTTP_X_FORWARDED_FOR"] ?? "";
|
|
if ($forwarded) {
|
|
$parts = explode(",", $forwarded);
|
|
return trim($parts[0]);
|
|
}
|
|
return $_SERVER["REMOTE_ADDR"] ?? "unknown";
|
|
}
|
|
|
|
function auth_read_json(): array
|
|
{
|
|
$raw = file_get_contents("php://input");
|
|
if (!$raw) {
|
|
return [];
|
|
}
|
|
$decoded = json_decode($raw, true);
|
|
return is_array($decoded) ? $decoded : [];
|
|
}
|
|
|
|
function auth_json_response(int $status, array $payload): void
|
|
{
|
|
http_response_code($status);
|
|
header("Content-Type: application/json; charset=utf-8");
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
function auth_get_site_key(array $data): string
|
|
{
|
|
$siteKey = $data["siteKey"] ?? $_GET["siteKey"] ?? "";
|
|
return is_string($siteKey) ? trim($siteKey) : "";
|
|
}
|
|
|
|
function auth_is_valid_login(string $login): bool
|
|
{
|
|
$len = mb_strlen($login, "UTF-8");
|
|
return $len >= 3 && $len <= 64;
|
|
}
|
|
|
|
function auth_is_valid_password(string $password): bool
|
|
{
|
|
$len = mb_strlen($password, "UTF-8");
|
|
return $len >= 8 && $len <= 128;
|
|
}
|
|
|
|
function auth_is_valid_site_key(string $siteKey): bool
|
|
{
|
|
$len = mb_strlen($siteKey, "UTF-8");
|
|
return $len >= 3 && $len <= 255;
|
|
}
|