first commit
This commit is contained in:
+124
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
function base_path(string $path = ''): string
|
||||
{
|
||||
$base = dirname(__DIR__);
|
||||
|
||||
if ($path === '') {
|
||||
return $base;
|
||||
}
|
||||
|
||||
return $base . '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
function storage_path(string $path = ''): string
|
||||
{
|
||||
return base_path('storage/' . ltrim($path, '/'));
|
||||
}
|
||||
|
||||
function e(mixed $value): string
|
||||
{
|
||||
return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
}
|
||||
|
||||
function redirect(string $path): never
|
||||
{
|
||||
header('Location: ' . $path);
|
||||
exit;
|
||||
}
|
||||
|
||||
function request_path(): string
|
||||
{
|
||||
$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
|
||||
$path = is_string($path) ? $path : '/';
|
||||
|
||||
if ($path !== '/') {
|
||||
$path = rtrim($path, '/');
|
||||
}
|
||||
|
||||
return $path === '' ? '/' : $path;
|
||||
}
|
||||
|
||||
function flash(string $type, string $message): void
|
||||
{
|
||||
$_SESSION['_flash'][] = [
|
||||
'type' => $type,
|
||||
'message' => $message,
|
||||
];
|
||||
}
|
||||
|
||||
function pull_flashes(): array
|
||||
{
|
||||
$flashes = $_SESSION['_flash'] ?? [];
|
||||
unset($_SESSION['_flash']);
|
||||
|
||||
return is_array($flashes) ? $flashes : [];
|
||||
}
|
||||
|
||||
function csrf_token(): string
|
||||
{
|
||||
if (empty($_SESSION['_csrf'])) {
|
||||
$_SESSION['_csrf'] = bin2hex(random_bytes(32));
|
||||
}
|
||||
|
||||
return (string) $_SESSION['_csrf'];
|
||||
}
|
||||
|
||||
function csrf_field(): string
|
||||
{
|
||||
return '<input type="hidden" name="_token" value="' . e(csrf_token()) . '">';
|
||||
}
|
||||
|
||||
function verify_csrf(?string $token): bool
|
||||
{
|
||||
if (!is_string($token) || $token === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return hash_equals(csrf_token(), $token);
|
||||
}
|
||||
|
||||
function is_active_path(string $path): bool
|
||||
{
|
||||
return request_path() === $path;
|
||||
}
|
||||
|
||||
function format_points(float $value): string
|
||||
{
|
||||
$rounded = round($value, 1);
|
||||
|
||||
if (abs($rounded - round($rounded)) < 0.05) {
|
||||
return (string) (int) round($rounded);
|
||||
}
|
||||
|
||||
return number_format($rounded, 1, ',', '.');
|
||||
}
|
||||
|
||||
function normalize_username(string $username): string
|
||||
{
|
||||
return strtolower(trim($username));
|
||||
}
|
||||
|
||||
function today(): string
|
||||
{
|
||||
return date('Y-m-d');
|
||||
}
|
||||
|
||||
function decode_json_file(string $path, array $fallback = []): array
|
||||
{
|
||||
if (!is_file($path)) {
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
$decoded = json_decode((string) file_get_contents($path), true);
|
||||
|
||||
return is_array($decoded) ? $decoded : $fallback;
|
||||
}
|
||||
|
||||
function encode_payload(array $payload): string
|
||||
{
|
||||
return base64_encode((string) json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user