1<?php
2/*
3 * Copyright (c) 2008-2015 Mark C. Prins <mprins@users.sf.net>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
18 */
19
20/**
21 * DokuWiki Plugin openlayersmap (Admin Component).
22 * This component purges the cached tiles and maps.
23 *
24 * @author Mark Prins
25 */
26class admin_plugin_openlayersmap_purge extends DokuWiki_Admin_Plugin {
27    /**
28     * (non-PHPdoc)
29     * @see DokuWiki_Admin_Plugin::getMenuSort()
30     */
31    public function getMenuSort(): int {
32        return 800;
33    }
34
35    public function getMenuIcon(): string {
36        $plugin = $this->getPluginName();
37        return DOKU_PLUGIN . $plugin . '/admin/purge.svg';
38    }
39
40    /**
41     * (non-PHPdoc)
42     * @see DokuWiki_Admin_Plugin::handle()
43     */
44    public function handle(): void {
45        global $conf;
46        if(!isset($_REQUEST['continue']) || !checkSecurityToken()) {
47            return;
48        }
49        if(isset($_REQUEST['purgetiles'])) {
50            $path = $conf['cachedir'] . '/olmaptiles';
51            if($this->rrmdir($path)) {
52                msg($this->getLang('admin_purged_tiles'), 0);
53            }
54        }
55        if(isset($_REQUEST['purgemaps'])) {
56            $path = $conf['mediadir'] . '/olmapmaps';
57            if($this->rrmdir($path)) {
58                msg($this->getLang('admin_purged_maps'), 0);
59            }
60        }
61    }
62
63    /**
64     * Recursively delete the directory.
65     * @param string $sDir directory path
66     * @return boolean true when succesful
67     */
68    private function rrmdir(string $sDir): bool {
69        if(is_dir($sDir)) {
70            dbglog($sDir, 'admin_plugin_openlayersmap_purge::rrmdir: recursively removing path: ');
71            $sDir = rtrim($sDir, '/');
72            $oDir = dir($sDir);
73            while(($sFile = $oDir->read()) !== false) {
74                if($sFile !== '.' && $sFile !== '..') {
75                    (!is_link("$sDir/$sFile") && is_dir("$sDir/$sFile")) ?
76                        $this->rrmdir("$sDir/$sFile") : unlink("$sDir/$sFile");
77                }
78            }
79            $oDir->close();
80            rmdir($sDir);
81            return true;
82        }
83        return false;
84    }
85
86    /**
87     * (non-PHPdoc)
88     * @see DokuWiki_Admin_Plugin::html()
89     */
90    public function html(): void {
91        echo $this->locale_xhtml('admin_intro');
92        $form = new Doku_Form(array('id' => 'olmap_purgeform', 'method' => 'post'));
93        $form->addHidden('continue', 'go');
94
95        $form->startFieldset($this->getLang('admin_tiles'));
96        $form->addElement('<p>');
97        $form->addElement(
98            '<input id="purgetiles" name="purgetiles" type="checkbox" value="1" class="checkbox" />'
99        );
100        $form->addElement(
101            '<label for="purgetiles" class="label">' . $this->getLang('admin_purge_tiles')
102            . '</label>'
103        );
104        $form->addElement('</p>');
105        $form->endFieldset();
106
107        $form->startFieldset($this->getLang('admin_maps'));
108        $form->addElement('<p>');
109        $form->addElement('<input id="purgemaps" name="purgemaps" type="checkbox" value="1" class="checkbox" />');
110        $form->addElement(
111            '<label for="purgemaps" class="label">' . $this->getLang('admin_purge_maps') . '</label>'
112        );
113        $form->addElement('</p>');
114        $form->endFieldset();
115
116        $form->addElement(
117            form_makeButton(
118                'submit', 'admin', $this->getLang('admin_submit'),
119                array('accesskey' => 'p', 'title' => $this->getLang('admin_submit'))
120            )
121        );
122        $form->printForm();
123    }
124}
125