Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
require_once __DIR__ . '/Database.php';
require_once __DIR__ . '/Session.php';
require_once __DIR__ . '/Flash.php';
require_once __DIR__ . '/User.php';
require_once __DIR__ . '/Party.php';
require_once __DIR__ . '/Question.php';
require_once __DIR__ . '/PartyPosition.php';
function h(string $value): string {
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}
function redirect(string $path): void {
if (strpos($path, 'http') !== 0) {
$path = $path[0] === '/' ? $path : '/' . $path;
$path = BASE_URL . $path;
}
header('Location: ' . $path);
exit;
}
function require_login(): void {
$user = User::getCurrent();
if (!$user) {
redirect('index.php');
}
}
function require_role(string $role): void {
require_login();
$user = User::getCurrent();
if ($user->getRole() !== $role) {
redirect('index.php');
}
}
function save_logo(array $file): string {
if (empty($file['name']) || $file['error'] === UPLOAD_ERR_NO_FILE) {
throw new RuntimeException('Geen bestand geΓΌpload.');
}
if ($file['error'] !== UPLOAD_ERR_OK) {
throw new RuntimeException('Upload mislukt.');
}
if ($file['size'] > 2 * 1024 * 1024) {
throw new RuntimeException('Bestand te groot (max 2MB).');
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
$allowed = ['image/png' => 'png', 'image/jpeg' => 'jpg', 'image/jpg' => 'jpg'];
if (!isset($allowed[$mime])) {
throw new RuntimeException('Alleen PNG en JPG zijn toegestaan.');
}
if (!is_dir(UPLOAD_DIR)) {
@mkdir(UPLOAD_DIR, 0775, true);
}
$name = bin2hex(random_bytes(8)) . '.' . $allowed[$mime];
$dest = UPLOAD_DIR . DIRECTORY_SEPARATOR . $name;
if (!move_uploaded_file($file['tmp_name'], $dest)) {
throw new RuntimeException('Kon bestand niet opslaan.');
}
return 'assets/uploads/' . $name;
}
function count_table(string $table): int {
$pdo = Database::getInstance()->getConnection();
return (int) $pdo->query("SELECT COUNT(*) FROM {$table}")->fetchColumn();
}