WebOrbiton
v1.0.0.8

Publisium

62 lines · 2.5 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/includes/config.php';
  6. require_once __DIR__ . '/includes/database.php';
  7. require_once __DIR__ . '/includes/site-front.php';
  8. ​
  9. $settings = SiteFront::settings();
  10. ​
  11. if ($settings['sitemap_rss_enabled'] !== '1') {
  12. http_response_code(404);
  13. exit;
  14. }
  15. ​
  16. $db = Database::site();
  17. $baseUrl = rtrim(Config::get('APP_URL', ''), '/') . Config::get('APP_BASE_PATH', '');
  18. ​
  19. $articles = $db->query("SELECT id, slug, updated_at FROM articles WHERE status = 'published' ORDER BY updated_at DESC")->fetchAll();
  20. $pages = $db->query("SELECT slug, updated_at FROM pages WHERE status = 'published'")->fetchAll();
  21. $categories = $db->query('SELECT slug FROM categories')->fetchAll();
  22. ​
  23. header('Content-Type: application/xml; charset=utf-8');
  24. ​
  25. echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  26. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  27. ​
  28. echo '<url><loc>' . htmlspecialchars(rtrim((string) Config::get('APP_URL', ''), '/') . SiteFront::homeUrl('')) . '</loc></url>' . "\n";
  29. ​
  30. foreach ($categories as $category) {
  31. echo '<url><loc>' . htmlspecialchars($baseUrl . '/category.php?slug=' . urlencode($category['slug'])) . '</loc></url>' . "\n";
  32. }
  33. ​
  34. foreach ($pages as $page) {
  35. echo '<url><loc>' . htmlspecialchars(rtrim(Config::get('APP_URL', ''), '/') . SiteFront::pageUrl((string) $page['slug'])) . '</loc>';
  36. echo '<lastmod>' . htmlspecialchars(date('c', strtotime($page['updated_at']))) . '</lastmod></url>' . "\n";
  37. }
  38. ​
  39. foreach ($articles as $article) {
  40. echo '<url><loc>' . htmlspecialchars(rtrim(Config::get('APP_URL', ''), '/') . SiteFront::articleUrl($article['slug'])) . '</loc>';
  41. echo '<lastmod>' . htmlspecialchars(date('c', strtotime($article['updated_at']))) . '</lastmod></url>' . "\n";
  42. }
  43. ​
  44. if (Languages::enabled()) {
  45. $origin = rtrim(Config::get('APP_URL', ''), '/');
  46. ​
  47. foreach (Languages::activeCodes() as $languageCode) {
  48. echo '<url><loc>' . htmlspecialchars($origin . Languages::homeUrl($languageCode)) . '</loc></url>' . "\n";
  49. ​
  50. $translatedArticleIds = Languages::translatedIds('article', $languageCode);
  51. foreach ($articles as $article) {
  52. if (!in_array((int) $article['id'], $translatedArticleIds, true)) {
  53. continue;
  54. }
  55. echo '<url><loc>' . htmlspecialchars($origin . SiteFront::articleUrl($article['slug'], $languageCode)) . '</loc>';
  56. echo '<lastmod>' . htmlspecialchars(date('c', strtotime($article['updated_at']))) . '</lastmod></url>' . "\n";
  57. }
  58. }
  59. }
  60. ​
  61. echo '</urlset>';
  62. ​