xref: /dokuwiki/inc/Action/Sitemap.php (revision 64ab5140f7b1c996873fbfe9bab26d9202fbb773)
1*64ab5140SAndreas Gohr<?php
2*64ab5140SAndreas Gohr/**
3*64ab5140SAndreas Gohr * Created by IntelliJ IDEA.
4*64ab5140SAndreas Gohr * User: andi
5*64ab5140SAndreas Gohr * Date: 2/10/17
6*64ab5140SAndreas Gohr * Time: 3:18 PM
7*64ab5140SAndreas Gohr */
8*64ab5140SAndreas Gohr
9*64ab5140SAndreas Gohrnamespace dokuwiki\Action;
10*64ab5140SAndreas Gohr
11*64ab5140SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
12*64ab5140SAndreas Gohr
13*64ab5140SAndreas Gohrclass Sitemap extends AbstractAction {
14*64ab5140SAndreas Gohr
15*64ab5140SAndreas Gohr    /** @inheritdoc */
16*64ab5140SAndreas Gohr    function minimumPermission() {
17*64ab5140SAndreas Gohr        return AUTH_NONE;
18*64ab5140SAndreas Gohr    }
19*64ab5140SAndreas Gohr
20*64ab5140SAndreas Gohr    /**
21*64ab5140SAndreas Gohr     * Handle sitemap delivery
22*64ab5140SAndreas Gohr     *
23*64ab5140SAndreas Gohr     * @author Michael Hamann <michael@content-space.de>
24*64ab5140SAndreas Gohr     * @throws FatalException
25*64ab5140SAndreas Gohr     * @inheritdoc
26*64ab5140SAndreas Gohr     */
27*64ab5140SAndreas Gohr    public function preProcess() {
28*64ab5140SAndreas Gohr        global $conf;
29*64ab5140SAndreas Gohr
30*64ab5140SAndreas Gohr        if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
31*64ab5140SAndreas Gohr            throw new FatalException(404, 'Sitemap generation is disabled');
32*64ab5140SAndreas Gohr        }
33*64ab5140SAndreas Gohr
34*64ab5140SAndreas Gohr        $sitemap = \Sitemapper::getFilePath();
35*64ab5140SAndreas Gohr        if (\Sitemapper::sitemapIsCompressed()) {
36*64ab5140SAndreas Gohr            $mime = 'application/x-gzip';
37*64ab5140SAndreas Gohr        }else{
38*64ab5140SAndreas Gohr            $mime = 'application/xml; charset=utf-8';
39*64ab5140SAndreas Gohr        }
40*64ab5140SAndreas Gohr
41*64ab5140SAndreas Gohr        // Check if sitemap file exists, otherwise create it
42*64ab5140SAndreas Gohr        if (!is_readable($sitemap)) {
43*64ab5140SAndreas Gohr            \Sitemapper::generate();
44*64ab5140SAndreas Gohr        }
45*64ab5140SAndreas Gohr
46*64ab5140SAndreas Gohr        if (is_readable($sitemap)) {
47*64ab5140SAndreas Gohr            // Send headers
48*64ab5140SAndreas Gohr            header('Content-Type: '.$mime);
49*64ab5140SAndreas Gohr            header('Content-Disposition: attachment; filename='.utf8_basename($sitemap));
50*64ab5140SAndreas Gohr
51*64ab5140SAndreas Gohr            http_conditionalRequest(filemtime($sitemap));
52*64ab5140SAndreas Gohr
53*64ab5140SAndreas Gohr            // Send file
54*64ab5140SAndreas Gohr            //use x-sendfile header to pass the delivery to compatible webservers
55*64ab5140SAndreas Gohr            http_sendfile($sitemap);
56*64ab5140SAndreas Gohr
57*64ab5140SAndreas Gohr            readfile($sitemap);
58*64ab5140SAndreas Gohr            exit;
59*64ab5140SAndreas Gohr        }
60*64ab5140SAndreas Gohr
61*64ab5140SAndreas Gohr        throw new FatalException(500, 'Could not read the sitemap file - bad permissions?');
62*64ab5140SAndreas Gohr    }
63*64ab5140SAndreas Gohr
64*64ab5140SAndreas Gohr}
65