|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Pico robots plugin - add a robots.txt and sitemap.xml to your website |
| 5 | + * |
| 6 | + * PicoRobots is a simple plugin that add a `robots.txt` and `sitemap.xml` to |
| 7 | + * your website. Both the robots exclusion protocol (`robots.txt`) and the |
| 8 | + * Sitemaps protocol (`sitemap.xml`) are used to communicate with web crawlers |
| 9 | + * and other web robots. `robots.txt` informs the web robot about which areas |
| 10 | + * of your website should not be processed or scanned. `sitemap.xml` allows |
| 11 | + * web robots to crawl your website more intelligently. `sitemap.xml` is a URL |
| 12 | + * inclusion protocol and complements `robots.txt`, a URL exclusion protocol. |
| 13 | + * |
| 14 | + * @author Daniel Rudolf |
| 15 | + * @link http://picocms.org |
| 16 | + * @license http://opensource.org/licenses/MIT The MIT License |
| 17 | + * @version 1.0.0 |
| 18 | + */ |
| 19 | +class PicoRobots extends AbstractPicoPlugin |
| 20 | +{ |
| 21 | + /** |
| 22 | + * This plugin uses Pico's API version 2 as of Pico 2.0 |
| 23 | + * |
| 24 | + * @var int |
| 25 | + */ |
| 26 | + const API_VERSION = 2; |
| 27 | + |
| 28 | + /** |
| 29 | + * List of robots exclusion rules |
| 30 | + * |
| 31 | + * @see PicoRobots::getRobots() |
| 32 | + * @var array[]|null |
| 33 | + */ |
| 34 | + protected $robots; |
| 35 | + |
| 36 | + /** |
| 37 | + * List of sitemap records |
| 38 | + * |
| 39 | + * @see PicoRobots::getSitemap() |
| 40 | + * @var array[]|null |
| 41 | + */ |
| 42 | + protected $sitemap; |
| 43 | + |
| 44 | + /** |
| 45 | + * Disables this plugin if neither robots.txt nor sitemap.xml is requested |
| 46 | + * |
| 47 | + * @see DummyPlugin::onRequestUrl() |
| 48 | + */ |
| 49 | + public function onRequestUrl(&$requestUrl) |
| 50 | + { |
| 51 | + if (!in_array($requestUrl, array('robots.txt', 'sitemap.xml'), true)) { |
| 52 | + $this->setEnabled(false); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets a page's last modification time and its default sitemap status |
| 58 | + * |
| 59 | + * @see DummyPlugin::onSinglePageLoaded() |
| 60 | + */ |
| 61 | + public function onSinglePageLoaded(array &$pageData) |
| 62 | + { |
| 63 | + if (($this->getRequestUrl() === 'sitemap.xml') && $pageData['id']) { |
| 64 | + $fileName = $this->getConfig('content_dir') . $pageData['id'] . $this->getConfig('content_ext'); |
| 65 | + if (file_exists($fileName) && !isset($pageData['modificationTime'])) { |
| 66 | + $pageData['modificationTime'] = filemtime($fileName); |
| 67 | + } |
| 68 | + |
| 69 | + if (!$pageData['meta']['sitemap'] && ($pageData['meta']['sitemap'] !== false)) { |
| 70 | + $pageData['meta']['sitemap'] = true; |
| 71 | + |
| 72 | + if (preg_match('/(?:^|\/)_/', $pageData['id'])) { |
| 73 | + $pageData['meta']['sitemap'] = false; |
| 74 | + } else { |
| 75 | + $robots = explode(',', $pageData['meta']['robots']); |
| 76 | + $robots = array_map('strtolower', $robots); |
| 77 | + if (in_array('noindex', $robots)) { |
| 78 | + $pageData['meta']['sitemap'] = false; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Tells Pico to serve the robots.txt resp. sitemap.xml |
| 87 | + * |
| 88 | + * You can overwrite the plugin's default templates for `robots.txt` and |
| 89 | + * `sitemap.xml` by simply adding a `robots.twig` resp. `sitemap.twig` to |
| 90 | + * your theme. |
| 91 | + * |
| 92 | + * @see DummyPlugin::onPageRendering() |
| 93 | + */ |
| 94 | + public function onPageRendering(&$twigTemplate, array &$twigVariables) |
| 95 | + { |
| 96 | + if ($this->getRequestUrl() === 'robots.txt') { |
| 97 | + header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); |
| 98 | + header('Content-Type: text/plain; charset=utf-8'); |
| 99 | + $twigTemplate = 'robots.twig'; |
| 100 | + |
| 101 | + $twigVariables['robots'] = $this->getRobots(); |
| 102 | + } |
| 103 | + |
| 104 | + if ($this->getRequestUrl() === 'sitemap.xml') { |
| 105 | + header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK'); |
| 106 | + header('Content-Type: application/xml; charset=utf-8'); |
| 107 | + $twigTemplate = 'sitemap.twig'; |
| 108 | + |
| 109 | + $twigVariables['sitemap'] = $this->getSitemap(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Returns the structured contents of robots.txt |
| 115 | + * |
| 116 | + * This method triggers the `onRobots` event when the contents of |
| 117 | + * `robots.txt` weren't assembled yet. |
| 118 | + * |
| 119 | + * @return array[] list of robots exclusion rules |
| 120 | + */ |
| 121 | + public function getRobots() |
| 122 | + { |
| 123 | + if ($this->robots === null) { |
| 124 | + $this->robots = array(); |
| 125 | + |
| 126 | + $robotsConfig = $this->getPluginConfig('robots', array()); |
| 127 | + foreach ($robotsConfig as $rule) { |
| 128 | + $userAgents = !empty($rule['user_agents']) ? (array) $rule['user_agents'] : array(); |
| 129 | + $disallow = !empty($rule['disallow']) ? (array) $rule['disallow'] : array(); |
| 130 | + $allow = !empty($rule['allow']) ? (array) $rule['allow'] : array(); |
| 131 | + |
| 132 | + $this->robots[] = array( |
| 133 | + 'user_agents' => $userAgents ?: array('*'), |
| 134 | + 'disallow' => $disallow ?: (!$allow ? array('*') : array()), |
| 135 | + 'allow' => $allow |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + $this->triggerEvent('onRobots', array(&$this->robots)); |
| 140 | + } |
| 141 | + |
| 142 | + return $this->robots; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Returns the structure contents of sitemap.xml |
| 147 | + * |
| 148 | + * This method triggers the `onSitemap` event when the contents of |
| 149 | + * `sitemap.xml` weren't assembled yet. |
| 150 | + * |
| 151 | + * @return array[] list of sitemap records |
| 152 | + */ |
| 153 | + public function getSitemap() |
| 154 | + { |
| 155 | + if ($this->sitemap === null) { |
| 156 | + $changeFrequencies = array('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'); |
| 157 | + $this->sitemap = array(); |
| 158 | + |
| 159 | + $pages = $this->getPages(); |
| 160 | + foreach ($pages as $pageData) { |
| 161 | + if (!empty($pageData['meta']['sitemap'])) { |
| 162 | + $modificationTime = null; |
| 163 | + if (isset($pageData['meta']['sitemap']['lastmod'])) { |
| 164 | + $modificationTime = $pageData['meta']['sitemap']['lastmod'] ?: null; |
| 165 | + |
| 166 | + if ($modificationTime && !is_int($modificationTime)) { |
| 167 | + $modificationTime = strtotime($modificationTime) ?: null; |
| 168 | + } |
| 169 | + } elseif (!empty($pageData['modificationTime'])) { |
| 170 | + $modificationTime = $pageData['modificationTime']; |
| 171 | + } |
| 172 | + |
| 173 | + $changeFrequency = null; |
| 174 | + if (!empty($pageData['meta']['sitemap']['changefreq'])) { |
| 175 | + $changeFrequency = $pageData['meta']['sitemap']['changefreq']; |
| 176 | + } |
| 177 | + |
| 178 | + $priority = null; |
| 179 | + if (isset($pageData['meta']['sitemap']['priority'])) { |
| 180 | + $priority = (float) $pageData['meta']['sitemap']['priority']; |
| 181 | + } |
| 182 | + |
| 183 | + $this->sitemap[] = array( |
| 184 | + 'url' => $pageData['url'], |
| 185 | + 'modificationTime' => $modificationTime, |
| 186 | + 'changeFrequency' => in_array($changeFrequency, $changeFrequencies) ? $changeFrequency : null, |
| 187 | + 'priority' => ($priority !== null) ? min(max(round($priority, 1), 0), 1) : null |
| 188 | + ); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + $sitemapConfig = $this->getPluginConfig('sitemap', array()); |
| 193 | + foreach ($sitemapConfig as $record) { |
| 194 | + if (!empty($record['url'])) { |
| 195 | + $modificationTime = !empty($record['lastmod']) ? $record['lastmod'] : null; |
| 196 | + $changeFrequency = !empty($record['changefreq']) ? $record['changefreq'] : null; |
| 197 | + $priority = isset($record['priority']) ? (float) $record['priority'] : null; |
| 198 | + |
| 199 | + if ($modificationTime && !is_int($modificationTime)) { |
| 200 | + $modificationTime = strtotime($modificationTime) ?: null; |
| 201 | + } |
| 202 | + |
| 203 | + $this->sitemap[] = array( |
| 204 | + 'url' => $this->substituteUrl($record['url']), |
| 205 | + 'modificationTime' => $modificationTime, |
| 206 | + 'changeFrequency' => in_array($changeFrequency, $changeFrequencies) ? $changeFrequency : null, |
| 207 | + 'priority' => ($priority !== null) ? min(max(round($priority, 1), 0), 1) : null |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + $this->triggerEvent('onSitemap', array(&$this->sitemap)); |
| 213 | + } |
| 214 | + |
| 215 | + return $this->sitemap; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Registers the Sitemap meta header |
| 220 | + * |
| 221 | + * @see DummyPlugin::onMetaHeaders() |
| 222 | + */ |
| 223 | + public function onMetaHeaders(array &$headers) |
| 224 | + { |
| 225 | + $headers['Sitemap'] = 'sitemap'; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Adds the plugin's theme dir to Twig's template loader |
| 230 | + * |
| 231 | + * @see DummyPlugin::onTwigRegistered() |
| 232 | + */ |
| 233 | + public function onTwigRegistered(Twig_Environment &$twig) |
| 234 | + { |
| 235 | + $twig->getLoader()->addPath(__DIR__ . '/theme'); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Substitutes the placeholders %base_url% and %theme_url% in URLs |
| 240 | + * |
| 241 | + * @param string $url URL with (or without) placeholders |
| 242 | + * |
| 243 | + * @return string substituted URL |
| 244 | + */ |
| 245 | + protected function substituteUrl($url) |
| 246 | + { |
| 247 | + $variables = array( |
| 248 | + '%base_url%?' => $this->getBaseUrl() . (!$this->isUrlRewritingEnabled() ? '?' : ''), |
| 249 | + '%base_url%' => rtrim($this->getBaseUrl(), '/'), |
| 250 | + '%theme_url%' => $this->getBaseThemeUrl() . $this->getConfig('theme') |
| 251 | + ); |
| 252 | + |
| 253 | + return str_replace(array_keys($variables), $variables, $url); |
| 254 | + } |
| 255 | +} |
0 commit comments