<?php
// Без пробелов перед PHP тегом!

// Защита session_start()
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$dbhost = "localhost";
$dbuser = "admin_root"; 
$dbname = "admin_main6"; 
$dbpass = "kVT3,(BhbQ%1";

// Инициализация переменных
$active1 = '';
$active2 = '';

$root = $_SERVER['DOCUMENT_ROOT'];
$url = $_SERVER['REQUEST_URI'];
$site = $_SERVER['HTTP_HOST'];

$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
mysqli_query($db, "SET NAMES utf8");

if (!$db) {
    printf("<center>Невозможно подключиться к базе данных: %s<br></center>", mysqli_connect_error());
    exit;
}

// Получаем контент
$sql = "SELECT * FROM main ORDER BY id DESC";
$result = $db->query($sql);
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $title = $row['title'];
        $descrip = $row['descrip'];
        $counter = base64_decode($row['counter']);
        $butto = $row['butto'];
    }
}

if (isset($_GET['id'])) {
    $aidi = $_GET['id'];
    $sql = "SELECT * FROM post WHERE id='$aidi'";
    $result = $db->query($sql);
    if ($result && $result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $id = $row['id'];
            $title = $row['title'];
            $descrip = $row['descrip'];
            $name = $row['name'];
            $img = $row['img'];
            $text = $row['text'];
            $data = $row['data'];
        }
    }
} else {
    $aidi = "1";
}

if (isset($_GET['page'])) {
    $ids = $_GET['page'];
    if ($ids == '1') {
        $active1 = 'active';
        $title .= " стр 1";
        $descrip .= " стр 1";
    } elseif ($ids == '2') {
        $active2 = 'active';
        $title .= " стр 2";
        $descrip .= " стр 2";
    }
}

// Определение поисковиков
$searchEngines = ['duckduckgo.com'];
$searchEngineBots = [
    'Googlebot', 'Bingbot', 'Slurp', 'DuckDuckBot', 'Baiduspider', 'YandexBot',
    'Sogou', 'Exabot', 'facebookexternalhit', 'facebot', 'ia_archiver'
];

$referrer = $_SERVER['HTTP_REFERER'] ?? '';
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';

$isFromSearchEngine = false;

foreach ($searchEngines as $searchEngine) {
    if (strpos($referrer, $searchEngine) !== false) {
        $isFromSearchEngine = true;
        break;
    }
}

if (!$isFromSearchEngine) {
    $good = 0;
    $butto = "#";
    echo "<style>.wb_button{display:none!important;} .hmenu li:last-child { display: none; }</style>";
} else {
    $good = 1;
}

// Last-Modified заголовок
$LastModified_unix = filemtime($_SERVER['SCRIPT_FILENAME']);
$LastModified = gmdate("D, d M Y H:i:s \G\M\T", $LastModified_unix);

$IfModifiedSince = $_SERVER['HTTP_IF_MODIFIED_SINCE'] ?? '';

if ($IfModifiedSince && strtotime($IfModifiedSince) >= $LastModified_unix) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
    exit;
}

header('Last-Modified: ' . $LastModified);

