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