PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ] ); } catch (PDOException $e) { error_log("Database connection failed: " . $e->getMessage()); http_response_code(503); exit; } // Template rendering helper function render(string $template, array $vars = []) { extract($vars, EXTR_SKIP); include __DIR__ . '/templates/' . $template . '.php'; exit; } // Helper: SELECT wrapper function pdoSelect(PDO $pdo, string $query, array $params): array { $stmt = $pdo->prepare($query); $stmt->execute($params); return $stmt->fetchAll(); } // Prepare host & wildcard domain list $host = strtolower($host); $parts = explode('.', $host); $names = [$host]; while (count($parts) > 2) { array_shift($parts); $names[] = '*.' . implode('.', $parts); } $placeholders = implode(',', array_fill(0, count($names), '?')); // Check forwards table $sql = ' SELECT Destination, Framed, Tittle, Description, Metatags, Enabled, Source FROM forwards WHERE Source IN (' . $placeholders . ') AND Enabled = 1 '; try { $rows = pdoSelect($pdo, $sql, $names); } catch (PDOException $e) { error_log("Database query failed: " . $e->getMessage()); http_response_code(503); exit; } // check if we have rows if (count($rows) === 0) { checkOfflineRecords($pdo, $host, $names, $placeholders); exit; } $forwardDest = ''; $forwardDestRaw = ''; $isFrameRedirect = 0; $pageTitle = ''; $metaDescription = ''; $metaKeys = ''; $isEnabled = 1; $found = count($names) + 1; foreach ($rows as $row) { $src = $row['Source']; $pos = array_search($src, $names, true); if ($pos !== false && $pos < $found) { $found = $pos; $forwardDest = $row['Destination']; $forwardDestRaw = $forwardDest; $isFrameRedirect = (int)$row['Framed']; $pageTitle = $row['Tittle']; $metaDescription = $row['Description']; $metaKeys = $row['Metatags']; $isEnabled = $row['Enabled']; } } if ($found < count($names) + 1) { // Append query path if (!$queryString && strlen($_SERVER['REQUEST_URI'] ?? '') > 1) { $forwardDest .= $_SERVER['REQUEST_URI']; } // Redirect: not framed if ($isFrameRedirect != 1) { // Redirect raw if ($isFrameRedirect == 4) { header("Location: $forwardDestRaw"); exit; } // 301 redirect if ($isFrameRedirect == 3) { header('HTTP/1.1 301 Moved Permanently'); } header("Location: $forwardDest"); exit; } // Framed redirect → use template header('Content-Type: text/html; charset=utf-8'); render('frame-redirect', [ 'pageTitle' => $pageTitle ?? '', 'metaDescription' => $metaDescription ?? '', 'metaKeys' => $metaKeys ?? '', 'forwardDest' => $forwardDest ?? '' ]); } // Check offline records function checkOfflineRecords($pdo, $host, $names, $placeholders) { $sql = ' SELECT name, offlinevalue FROM records WHERE login IS NOT NULL AND name IN (' . $placeholders . ') AND type = "DA_OFF" '; try { $rows = pdoSelect($pdo, $sql, $names); } catch (PDOException $e) { error_log("Database query failed: " . $e->getMessage()); http_response_code(503); exit; } $offlineValue = ''; $found = count($names) + 1; foreach ($rows as $row) { $src = $row['name']; $pos = array_search($src, $names, true); if ($pos !== false && $pos < $found) { $found = $pos; $offlineValue = $row['offlinevalue']; } } if ($found < count($names) + 1) { if (strlen($offlineValue) > 0) { $url = preg_match('/^(https?|ftp|mailto):/i', $offlineValue) ? $offlineValue : 'http://' . $offlineValue; header("Location: $url"); exit; } // Show offline template render('offline', [ 'host' => $host ?? '' ]); } // Default fallback header('Location: http://www.topdns.com'); }