SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Controllers/SeoController.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Controllers;
  15.  
  16. use Sprout\Helpers\Register;
  17. use Sprout\Helpers\Sprout;
  18.  
  19.  
  20. /**
  21.  * Handler for SEO functions, including generation of robots.txt and XML sitemap
  22.  */
  23. class SeoController extends Controller
  24. {
  25.  
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30.  
  31.  
  32. /**
  33.   * Output a dynamic robots.txt file which points to the sitemap; for use on production sites
  34.   * @return void Outputs plain text after setting the appropriate content-type header
  35.   */
  36. public function robots()
  37. {
  38. header('Content-type: text/plain; charset=UTF-8');
  39.  
  40. // Load additional robots.txt rules, if found
  41. if (file_exists(DOCROOT . 'config/additional_robots.txt')) {
  42. echo trim(file_get_contents(DOCROOT . 'config/additional_robots.txt')), PHP_EOL, PHP_EOL;
  43. }
  44.  
  45. // Include sidemap rule
  46. echo '# Dynamic sitemap', PHP_EOL;
  47. echo 'Sitemap: ', Sprout::absRoot(), 'seo/xmlSitemap', PHP_EOL;
  48. }
  49.  
  50.  
  51. /**
  52.   * Robots.txt which denies all; for use on dev/QA sites
  53.   * @return void Outputs plain text after setting the appropriate content-type header
  54.   */
  55. public function robotsDeny() {
  56. header('Content-type: text/plain; charset=UTF-8');
  57.  
  58. echo 'User-agent: *', PHP_EOL;
  59. echo 'Disallow: /', PHP_EOL;
  60. }
  61.  
  62.  
  63. /**
  64.   * Output a dynamic XML sitemap
  65.   * @return void Outputs XML directly
  66.   */
  67. public function xmlSitemap()
  68. {
  69. $gens = Register::getSitemapGens();
  70.  
  71. $this->header();
  72. foreach ($gens as $class_name) {
  73. $inst = Sprout::instance($class_name, ['Sprout\\Helpers\\SitemapGen']);
  74. $inst->generate();
  75. }
  76. $this->footer();
  77. }
  78.  
  79.  
  80. /**
  81.   * Echo the XML header for the sitemap
  82.   * @return void Outputs XML after setting the appropriate content-type header
  83.   */
  84. private function header()
  85. {
  86. header('Content-type: text/xml; charset=UTF-8');
  87. echo '<?xml version="1.0" encoding="utf-8"?>';
  88. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ' .
  89. 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' .
  90. 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
  91. echo PHP_EOL;
  92. }
  93.  
  94.  
  95. /**
  96.   * Echo the XML footer for the sitemap
  97.   * @return void Outputs XML directly
  98.   */
  99. private function footer()
  100. {
  101. echo '</urlset>';
  102. echo PHP_EOL;
  103. }
  104.  
  105. }
  106.