1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Action\Exception\FatalException;
6use dokuwiki\Sitemap\Mapper;
7use dokuwiki\Utf8\PhpString;
8
9/**
10 * Class Sitemap
11 *
12 * Generate an XML sitemap for search engines. Do not confuse with Index
13 *
14 * @package dokuwiki\Action
15 */
16class Sitemap extends AbstractAction
17{
18    /** @inheritdoc */
19    public function minimumPermission()
20    {
21        return AUTH_NONE;
22    }
23
24    /**
25     * Handle sitemap delivery
26     *
27     * @author Michael Hamann <michael@content-space.de>
28     * @throws FatalException
29     * @inheritdoc
30     */
31    public function preProcess()
32    {
33        global $conf;
34
35        if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
36            throw new FatalException('Sitemap generation is disabled', 404);
37        }
38
39        $sitemap = Mapper::getFilePath();
40        if (Mapper::sitemapIsCompressed()) {
41            $mime = 'application/x-gzip';
42        } else {
43            $mime = 'application/xml; charset=utf-8';
44        }
45
46        // Check if sitemap file exists, otherwise create it
47        if (!is_readable($sitemap)) {
48            Mapper::generate();
49        }
50
51        if (is_readable($sitemap)) {
52            // Send headers
53            header('Content-Type: ' . $mime);
54            header('Content-Disposition: attachment; filename=' . PhpString::basename($sitemap));
55
56            http_conditionalRequest(filemtime($sitemap));
57
58            // Send file
59            //use x-sendfile header to pass the delivery to compatible webservers
60            http_sendfile($sitemap);
61
62            readfile($sitemap);
63            exit;
64        }
65
66        throw new FatalException('Could not read the sitemap file - bad permissions?');
67    }
68}
69