<?php
session_start();

$host = 'localhost';
$dbname = 'exam_donate';
$user = 'root';
$pass = 'root'; // Укажите свой пароль

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Ошибка подключения: " . $e->getMessage());
}

function h($str) {
    return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
}

function isLoggedIn() {
    return isset($_SESSION['user_id']);
}

function getAllProjects($pdo) {
    $stmt = $pdo->query("SELECT * FROM projects WHERE is_active = 1 ORDER BY (goal_amount - current_amount) / goal_amount ASC, days_left ASC");
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

function getProgressPercent($current, $goal) {
    if($goal <= 0) return 0;
    $percent = ($current / $goal) * 100;
    return min(100, round($percent, 1));
}
?>