mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-13 23:56:46 +02:00
193 lines
4.6 KiB
PHP
193 lines
4.6 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);
|
|
}
|
|
|
|
/**
|
|
* Check if a media type is visible to the current user
|
|
*/
|
|
function is_media_type_visible(string $mediaType): bool
|
|
{
|
|
// Get database connection
|
|
$pdo = \App\Database\Database::getInstance();
|
|
|
|
// Get media visibility setting
|
|
$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = :key LIMIT 1");
|
|
$stmt->execute(['key' => "media_visibility_{$mediaType}"]);
|
|
$visibility = $stmt->fetchColumn() ?: 'authenticated'; // Default to authenticated only
|
|
|
|
// Check user authentication status
|
|
$isLoggedIn = is_logged_in();
|
|
|
|
switch ($visibility) {
|
|
case 'public':
|
|
return true; // Visible to everyone
|
|
case 'authenticated':
|
|
return $isLoggedIn; // Visible only to authenticated users
|
|
case 'hidden':
|
|
return false; // Hidden from all users
|
|
default:
|
|
return $isLoggedIn; // Default to authenticated only
|
|
}
|
|
}
|