SESSION_TIMEOUT) { session_destroy(); return false; } $_SESSION['last_activity'] = time(); return true; } /** * Generate CSRF token */ function generateCSRFToken() { if (!isset($_SESSION[CSRF_TOKEN_NAME])) { $_SESSION[CSRF_TOKEN_NAME] = bin2hex(random_bytes(32)); } return $_SESSION[CSRF_TOKEN_NAME]; } /** * Verify CSRF token */ function verifyCSRFToken($token) { return isset($_SESSION[CSRF_TOKEN_NAME]) && hash_equals($_SESSION[CSRF_TOKEN_NAME], $token); } /** * Sanitize input */ function sanitizeInput($input) { return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8'); } /** * Log message to file */ function logMessage($level, $message) { $timestamp = date('Y-m-d H:i:s'); $logEntry = "[$timestamp] [$level] $message" . PHP_EOL; // Try to write to system log first if (is_writable(dirname(LOG_FILE))) { file_put_contents(LOG_FILE, $logEntry, FILE_APPEND | LOCK_EX); } // Also write to UI log $uiLogFile = UI_LOG_DIR . '/ui-enrollment.log'; if (!is_dir(UI_LOG_DIR)) { mkdir(UI_LOG_DIR, 0755, true); } file_put_contents($uiLogFile, $logEntry, FILE_APPEND | LOCK_EX); } /** * Check if running as root/admin */ function isRunningAsRoot() { return posix_getuid() === 0; } /** * Execute system command safely */ function executeCommand($command, &$output = null, &$returnCode = null) { $descriptorspec = [ 0 => ['pipe', 'r'], // stdin 1 => ['pipe', 'w'], // stdout 2 => ['pipe', 'w'] // stderr ]; $process = proc_open($command, $descriptorspec, $pipes); if (is_resource($process)) { fclose($pipes[0]); $stdout = stream_get_contents($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[1]); fclose($pipes[2]); $returnCode = proc_close($process); $output = trim($stdout . $stderr); return $returnCode === 0; } return false; } /** * Get the correct asset path based on current directory structure */ function getAssetPath($asset) { // Determine if we're in the public directory or main directory $currentDir = dirname($_SERVER['SCRIPT_FILENAME']); $publicDir = realpath(__DIR__ . '/../public'); if ($currentDir === $publicDir) { // We're in the public directory, use relative paths return '../assets/' . ltrim($asset, '/'); } else { // We're in the main directory, use direct paths return 'assets/' . ltrim($asset, '/'); } } /** * Get base URL for the application */ function getBaseUrl() { $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; $host = $_SERVER['HTTP_HOST']; $scriptName = $_SERVER['SCRIPT_NAME']; // Remove the script filename to get the base path $basePath = dirname($scriptName); if ($basePath === '/') { $basePath = ''; } return $protocol . '://' . $host . $basePath; } ?>