Source of file Robots.php
Size: 3,817 Bytes - Last Modified: 2021-12-23T10:04:04+00:00
/var/www/docs.ssmods.com/process/src/code/controller/Robots.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 | <?php /** * Provides robots.txt functionality */ class Robots extends Controller { /** * Determines if this is a public site * * @return boolean flag indicating if this robots is for a public site */ protected function isPublic() { return Director::isLive(); } /** * Generates the response containing the robots.txt content * * @return SS_HTTPResponse */ public function index() { $siteConfig = SiteConfig::current_site_config(); if(class_exists('Translatable')) { $hostlocale = $siteConfig->getLocaleFromHost($_SERVER['HTTP_HOST']); Translatable::set_current_locale($hostlocale); i18n::set_locale($hostlocale); } //check of empty, if so, generate and save if (!$siteConfig->RobotsText) { $siteConfig->RobotsText = $this->generateText(); if ($this->isPublic()) { $siteConfig->write(); } } $text = $siteConfig->RobotsText; $response = new SS_HTTPResponse($text, 200); $response->addHeader("Content-Type", "text/plain; charset=\"utf-8\""); return $response; } protected function generateText() { $text = ""; $text .= $this->renderSitemap(); $text .= "User-agent: *\n"; $text .= $this->renderDisallow(); $text .= $this->renderAllow(); return $text; } /** * Renders the sitemap link reference * * @return string */ protected function renderSitemap() { // No sitemap if not public if (!$this->isPublic()) { return ''; } // Check if sitemap is configured $sitemap = Robots::config()->sitemap; if (empty($sitemap)) { return ''; } // Skip sitemap if not available if (!class_exists('GoogleSitemap') && !Director::fileExists($sitemap)) { return ''; } // Report the sitemap location return sprintf("Sitemap: %s\n", Director::absoluteURL($sitemap)); } /** * Renders the list of disallowed pages * * @return string */ protected function renderDisallow() { // List only disallowed urls $text = ''; foreach ($this->disallowedUrls() as $url) { $text .= sprintf("Disallow: %s\n", $url); } return $text; } /** * Renders the list of allowed pages, if any * * @return string */ protected function renderAllow() { $text = ''; foreach ($this->allowedUrls() as $url) { $text .= sprintf("Allow: %s\n", $url); } return $text; } /** * Returns an array of disallowed URLs * * @return array */ protected function disallowedUrls() { // If not public, disallow all if (!$this->isPublic()) { return ["/"]; } // Get configured disallowed urls $urls = (array) Robots::config()->disallowed_urls; // Add all pages where ShowInSearch is false if (Robots::config()->disallow_unsearchable) { $unsearchablePages = SiteTree::get() ->where('"SiteTree"."ShowInSearch" = 0'); foreach ($unsearchablePages as $page) { $link = $page->Link(); // Don't disallow home page if ($link !== '/') { $urls[] = $link; } } } return array_unique($urls); } /** * Returns an array of allowed URLs * * @return array */ protected function allowedUrls() { return (array) Robots::config()->allowed_urls; } } |