fetchOne( "SELECT id, username, email, is_admin FROM users WHERE id = ?", [$_SESSION['user_id']] ); } // Login utente function login($username, $password) { $db = getDB(); $user = $db->fetchOne( "SELECT id, username, password, email, is_admin FROM users WHERE username = ?", [$username] ); if ($user && password_verify($password, $user['password'])) { $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; $_SESSION['is_admin'] = $user['is_admin']; $_SESSION['login_time'] = time(); return true; } return false; } // Logout utente function logout() { session_unset(); session_destroy(); session_start(); } // Richiedi autenticazione (redirect a login se non loggato) function requireLogin() { if (!isLoggedIn()) { header('Location: login.php'); exit; } } // Richiedi privilegi admin function requireAdmin() { requireLogin(); if (!isAdmin()) { header('Location: index.php?error=access_denied'); exit; } } // Genera un messaggio flash function setFlashMessage($message, $type = 'info') { $_SESSION['flash_message'] = $message; $_SESSION['flash_type'] = $type; } // Ottieni e pulisci il messaggio flash function getFlashMessage() { if (isset($_SESSION['flash_message'])) { $message = [ 'text' => $_SESSION['flash_message'], 'type' => $_SESSION['flash_type'] ?? 'info' ]; unset($_SESSION['flash_message']); unset($_SESSION['flash_type']); return $message; } return null; } // Sanitizza input function sanitize($input) { return htmlspecialchars(strip_tags(trim($input)), ENT_QUOTES, 'UTF-8'); } // Formatta data function formatDate($date) { if (empty($date)) return '-'; return date('d/m/Y', strtotime($date)); } // Formatta data e ora function formatDateTime($datetime) { if (empty($datetime)) return '-'; return date('d/m/Y H:i', strtotime($datetime)); } /** * Registra un'attività nel log * * @param string $action_type Tipo di azione (es: 'login', 'create', 'update', 'delete', 'assign', 'return') * @param string $action_description Descrizione dell'azione * @param string $entity_type Tipo di entità (es: 'territory', 'assignment', 'user', 'config') * @param int $entity_id ID dell'entità coinvolta */ function logActivity($action_type, $action_description, $entity_type = null, $entity_id = null) { if (!isLoggedIn()) { return false; } $db = getDB(); // Ottieni informazioni sull'utente e sulla richiesta $user_id = $_SESSION['user_id']; $username = $_SESSION['username']; $ip_address = $_SERVER['REMOTE_ADDR'] ?? null; $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? null; try { $db->execute( "INSERT INTO activity_logs (user_id, username, action_type, action_description, entity_type, entity_id, ip_address, user_agent) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [$user_id, $username, $action_type, $action_description, $entity_type, $entity_id, $ip_address, $user_agent] ); return true; } catch (Exception $e) { error_log("Errore nel log dell'attività: " . $e->getMessage()); return false; } } /** * Ottieni i log delle attività con filtri e paginazione * * @param array $filters Array di filtri (user_id, action_type, entity_type, date_from, date_to) * @param int $page Numero pagina * @param int $per_page Elementi per pagina * @return array Array con 'logs' e 'total' */ function getActivityLogs($filters = [], $page = 1, $per_page = 50) { $db = getDB(); $where = []; $params = []; if (!empty($filters['user_id'])) { $where[] = "user_id = ?"; $params[] = $filters['user_id']; } if (!empty($filters['action_type'])) { $where[] = "action_type = ?"; $params[] = $filters['action_type']; } if (!empty($filters['entity_type'])) { $where[] = "entity_type = ?"; $params[] = $filters['entity_type']; } if (!empty($filters['date_from'])) { $where[] = "DATE(created_at) >= ?"; $params[] = $filters['date_from']; } if (!empty($filters['date_to'])) { $where[] = "DATE(created_at) <= ?"; $params[] = $filters['date_to']; } if (!empty($filters['search'])) { $where[] = "(action_description LIKE ? OR username LIKE ?)"; $search_term = '%' . $filters['search'] . '%'; $params[] = $search_term; $params[] = $search_term; } $where_clause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : ''; // Conta totale $total = $db->fetchOne( "SELECT COUNT(*) as count FROM activity_logs $where_clause", $params )['count']; // Ottieni log con paginazione $offset = ($page - 1) * $per_page; $logs = $db->fetchAll( "SELECT * FROM activity_logs $where_clause ORDER BY created_at DESC LIMIT ? OFFSET ?", array_merge($params, [$per_page, $offset]) ); return [ 'logs' => $logs, 'total' => $total, 'pages' => ceil($total / $per_page), 'current_page' => $page ]; }