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        return AUTH_NONE;
21    }
22
23    /**
24     * Handle sitemap delivery
25     *
26     * @author Michael Hamann <michael@content-space.de>
27     * @throws FatalException
28     * @inheritdoc
29     */
30    public function preProcess() {
31        global $conf;
32
33        if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
34            throw new FatalException('Sitemap generation is disabled', 404);
35        }
36
37        $sitemap = Mapper::getFilePath();
38        if(Mapper::sitemapIsCompressed()) {
39            $mime = 'application/x-gzip';
40        } else {
41            $mime = 'application/xml; charset=utf-8';
42        }
43
44        // Check if sitemap file exists, otherwise create it
45        if(!is_readable($sitemap)) {
46            Mapper::generate();
47        }
48
49        if(is_readable($sitemap)) {
50            // Send headers
51            header('Content-Type: ' . $mime);
52            header('Content-Disposition: attachment; filename=' . PhpString::basename($sitemap));
53
54            http_conditionalRequest(filemtime($sitemap));
55
56            // Send file
57            //use x-sendfile header to pass the delivery to compatible webservers
58            http_sendfile($sitemap);
59
60            readfile($sitemap);
61            exit;
62        }
63
64        throw new FatalException('Could not read the sitemap file - bad permissions?');
65    }
66
67}
68