'/', 'games.index' => '/media/games', 'movies.index' => '/media/movies', 'tvshows.index' => '/media/tv-shows', 'music.index' => '/media/music', 'auth.login' => '/login', 'auth.logout' => '/logout' ]; $path = $routes[$name] ?? '/'; // Replace parameters in path foreach ($params as $key => $value) { $path = str_replace('{' . $key . '}', $value, $path); } return $path; } /** * Get CSRF token for forms */ function csrf_token(): string { if (isset($_SESSION['auth']) && $_SESSION['auth'] instanceof \App\Services\AuthService) { return $_SESSION['auth']->generateCSRFToken(); } return ''; } /** * Get current authenticated user */ function current_user(): ?array { if (isset($_SESSION['auth']) && $_SESSION['auth'] instanceof \App\Services\AuthService) { return $_SESSION['auth']->getCurrentUser(); } return null; } /** * Check if current user is authenticated */ function is_logged_in(): bool { if (isset($_SESSION['auth']) && $_SESSION['auth'] instanceof \App\Services\AuthService) { return $_SESSION['auth']->isLoggedIn(); } return false; } /** * Check if current user is admin */ function is_admin(): bool { if (isset($_SESSION['auth']) && $_SESSION['auth'] instanceof \App\Services\AuthService) { return $_SESSION['auth']->isAdmin(); } return false; } /** * Format bytes to human readable format */ function format_bytes(int $bytes, int $precision = 2): string { $units = ['B', 'KB', 'MB', 'GB', 'TB']; $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= (1 << (10 * $pow)); return round($bytes, $precision) . ' ' . $units[$pow]; } /** * Format duration in seconds to human readable format */ function format_duration(int $seconds): string { $hours = floor($seconds / 3600); $minutes = floor(($seconds % 3600) / 60); $seconds = $seconds % 60; if ($hours > 0) { return sprintf('%dh %dm', $hours, $minutes); } elseif ($minutes > 0) { return sprintf('%dm %ds', $minutes, $seconds); } else { return sprintf('%ds', $seconds); } } /** * Generate a random string */ function generate_random_string(int $length = 32): string { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[random_int(0, $charactersLength - 1)]; } return $randomString; } /** * Check if file exists and is readable */ function file_exists_and_readable(string $path): bool { return file_exists($path) && is_readable($path); } /** * Get file extension from path */ function get_file_extension(string $path): string { return strtolower(pathinfo($path, PATHINFO_EXTENSION)); } /** * Check if string is valid JSON */ function is_json(string $string): bool { json_decode($string); return json_last_error() === JSON_ERROR_NONE; } /** * Convert array to object recursively */ function array_to_object(array $array): object { return json_decode(json_encode($array), false); } /** * Convert object to array recursively */ function object_to_array(object $object): array { return json_decode(json_encode($object), true); }