Completed
Push — actionrefactor ( 6e4bf0 )
by Andreas
04:36
created

Sitemap::preProcess()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 9
nop 0
dl 0
loc 36
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace dokuwiki\Action;
4
5
use dokuwiki\Action\Exception\FatalException;
6
7
/**
8
 * Class Sitemap
9
 *
10
 * Generate an XML sitemap for search engines. Do not confuse with Index
11
 *
12
 * @package dokuwiki\Action
13
 */
14
class Sitemap extends AbstractAction {
15
16
    /** @inheritdoc */
17
    function minimumPermission() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

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