Files
MediaCollectorLibary/app/helpers.php
Lars Behrends 929ee43001 first commit
2025-10-17 13:29:28 +02:00

173 lines
3.8 KiB
PHP

<?php
/**
* Get the base URL for the application
*/
function base_url(): string
{
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
return $protocol . '://' . $host;
}
/**
* Get the path for a named route
*/
function path_for(string $name, array $params = []): string
{
// This would be implemented with Slim's URL generator in a real application
$routes = [
'home' => '/',
'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);
}