0) { if (!empty($id)) { foreach ($data as $item) { if ((isset($item['id']) && (string)$item['id'] === (string)$id) || (isset($item['slug']) && (string)$item['slug'] === (string)$id)) { $article = $item; break; } } } // Nếu không truyền ID cụ thể, lấy bài viết mới nhất đầu danh sách if (!$article && !empty($data[0])) { $article = $data[0]; $id = $article['id'] ?? ''; } } } // Đọc nội dung file HTML gốc if (!file_exists($htmlFile)) { http_response_code(404); echo "Không tìm thấy file giao diện {$pageFilename}"; exit(); } $html = file_get_contents($htmlFile); // Nếu tìm thấy bài viết, tiến hành trích xuất dữ liệu động if ($article) { // 1. Tiêu đề bài viết $rawTitle = trim($article['title'] ?? 'GK WINTRON'); $safeTitle = htmlspecialchars($rawTitle, ENT_QUOTES, 'UTF-8'); if ($isVideos) { $pageTitle = $safeTitle . ' - GK WINTRON VIDEOS'; } else if ($isNews) { $pageTitle = $safeTitle . ' - GK WINTRON'; } else { $pageTitle = $safeTitle . ' - GK WINTRON TUYỂN SINH'; } // 2. Mô tả / Tóm tắt bài viết $rawDesc = trim($article['excerpt'] ?? $article['sapo'] ?? $article['description'] ?? ''); if (empty($rawDesc) && !empty($article['paragraphs']) && is_array($article['paragraphs'])) { foreach ($article['paragraphs'] as $p) { $cleanP = trim(strip_tags($p)); if (!empty($cleanP)) { $rawDesc = mb_substr($cleanP, 0, 220, 'UTF-8') . '...'; break; } } } if (empty($rawDesc) && !empty($article['content'])) { $cleanTxt = trim(strip_tags($article['content'])); $rawDesc = mb_substr($cleanTxt, 0, 220, 'UTF-8') . '...'; } if (empty($rawDesc)) { if ($isVideos) { $rawDesc = 'Xem video sự kiện, lễ ký kết và các hoạt động đào tạo hàng không quốc tế GK Wintron & CAC Canada.'; } else if ($isNews) { $rawDesc = 'Tin tức đào tạo và kết nối nguồn nhân lực hàng không toàn cầu.'; } else { $rawDesc = 'Thông tin tuyển sinh các khóa đào tạo phi công, tiếp viên, kỹ thuật máy bay.'; } } $safeDesc = htmlspecialchars($rawDesc, ENT_QUOTES, 'UTF-8'); // 3. Xử lý ảnh đại diện (Preview Image) của bài viết $rawImg = trim($article['image'] ?? $article['thumbnail'] ?? ''); // Tự động tìm thumbnail YouTube nếu là video if (empty($rawImg) && !empty($article['videoUrl'])) { if (preg_match('/(?:youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=|shorts\/)([\w-]{11})/i', $article['videoUrl'], $ytm)) { $rawImg = 'https://img.youtube.com/vi/' . $ytm[1] . '/hqdefault.jpg'; } } // Tự động tìm ảnh đầu tiên trong nội dung bài viết nếu chưa có ảnh đại diện riêng if (empty($rawImg) && !empty($article['content'])) { if (preg_match('/]+src=["\']([^"\']+)["\']/i', $article['content'], $mImg)) { $rawImg = trim($mImg[1]); } } if (empty($rawImg) && !empty($article['paragraphs']) && is_array($article['paragraphs'])) { foreach ($article['paragraphs'] as $p) { if (preg_match('/]+src=["\']([^"\']+)["\']/i', $p, $mImg)) { $rawImg = trim($mImg[1]); break; } } } $imgUrl = ''; if (!empty($rawImg)) { // Trường hợp 3.1: Ảnh lưu dạng base64 từ trình upload máy tính if (strpos($rawImg, 'data:image') === 0) { $cacheDir = $baseDir . '/asset/luutrutintuc'; if (!is_dir($cacheDir)) { @mkdir($cacheDir, 0777, true); } $imgHash = substr(md5($rawImg), 0, 10); $cacheFilename = 'share_' . ($id ? $id . '_' : '') . $imgHash . '.jpg'; $cachePath = $cacheDir . '/' . $cacheFilename; if (!file_exists($cachePath) || filesize($cachePath) === 0) { $parts = explode(',', $rawImg, 2); if (count($parts) > 1) { $binaryData = base64_decode($parts[1]); if ($binaryData !== false) { @file_put_contents($cachePath, $binaryData); } } } if (file_exists($cachePath) && filesize($cachePath) > 0) { $imgUrl = $baseUrl . '/asset/luutrutintuc/' . $cacheFilename . '?v=' . filemtime($cachePath); } } // Trường hợp 3.2: URL tuyệt đối (bắt đầu bằng http:// hoặc https://) elseif (preg_match('/^https?:\/\//i', $rawImg)) { $imgUrl = $rawImg; } // Trường hợp 3.3: Đường dẫn tương đối trong dự án (ví dụ asset/... hoặc media/...) else { $cleanRel = preg_replace('/^\.?\//', '', $rawImg); $localFile = $baseDir . '/' . $cleanRel; $vParam = file_exists($localFile) ? ('?v=' . filemtime($localFile)) : '?v=2026'; $imgUrl = $baseUrl . '/' . $cleanRel . $vParam; } } // Nếu không có ảnh bài viết, dùng ảnh mặc định preview.jpg if (empty($imgUrl)) { $imgUrl = $baseUrl . '/preview.jpg?v=2026'; } // Đường link bài viết đầy đủ $shareUrl = $baseUrl . '/' . $pageFilename . ($id ? ('?id=' . urlencode($id)) : ''); // Tự động ép văng sang Google Chrome trên Android nếu người dùng thật bấm từ Zalo, Messenger, LINE, WhatsApp $rawUa = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; $isCrawlerBot = preg_match('/(facebookexternalhit|Facebot|Twitterbot|TelegramBot|LinkedInBot|Pinterest|Googlebot|bingbot|Baiduspider)/i', $rawUa); $isAndroidInApp = (stripos($rawUa, 'Android') !== false) && preg_match('/(Zalo|Messenger|FB_IAB|FB4A|FBIOS|Line\/|WhatsApp|Instagram|TikTok|MicroMessenger|WebView)/i', $rawUa); if ($isAndroidInApp && !$isCrawlerBot) { $cleanShareUrl = preg_replace('/^https?:\/\//i', '', $shareUrl); $chromeIntent = 'intent://' . $cleanShareUrl . '#Intent;scheme=https;package=com.android.chrome;end'; header('Location: ' . $chromeIntent, true, 302); header('Cache-Control: no-cache, no-store, must-revalidate'); exit(); } // 4. Thay thế chính xác toàn bộ thẻ meta Open Graph trong HTML // Thay thế $html = preg_replace('/<title>.*?<\/title>/is', '<title>' . $pageTitle . '', $html, 1); // Thay thế meta description $html = preg_replace('//i', '', $html, 1); // Thay thế og:title & twitter:title $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); // Thay thế og:description & twitter:description $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); // Thay thế og:url $html = preg_replace('//i', '', $html, 1); // Thay thế ảnh xem trước: og:image, og:image:secure_url, image_src, itemprop="image", twitter:image $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); $html = preg_replace('//i', '', $html, 1); } // Trả về trang HTML hoàn chỉnh cho trình duyệt hoặc robot mạng xã hội header('Content-Type: text/html; charset=utf-8'); header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0'); echo $html; exit();