1<?php
2
3/**
4 * Plugin BatchEdit
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10require_once(DOKU_PLUGIN . 'batchedit/admin.php');
11require_once(DOKU_PLUGIN . 'batchedit/config.php');
12require_once(DOKU_PLUGIN . 'batchedit/server.php');
13
14class action_plugin_batchedit extends DokuWiki_Action_Plugin {
15
16    const YEAR_IN_SECONDS = 31536000;
17
18    /**
19     * Register callbacks
20     */
21    public function register(Doku_Event_Handler $controller) {
22        if ($this->isBatchEditAjax()) {
23            $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'onAjaxCallUnknown');
24        }
25
26        if (!$this->isBatchEdit()) {
27            return;
28        }
29
30        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, 'onBeforeHeadersSend');
31        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'onBeforeMetaheaderOutput');
32    }
33
34    /**
35     *
36     */
37    public function onAjaxCallUnknown($event, $param) {
38        if ($event->data == 'batchedit') {
39            $event->preventDefault();
40            $event->stopPropagation();
41
42            $server = new BatcheditServer($this);
43
44            $server->handle();
45        }
46    }
47
48    /**
49     *
50     */
51    public function onBeforeHeadersSend($event, $param) {
52        $admin = admin_plugin_batchedit::getInstance();
53
54        if ($admin == NULL) {
55            return;
56        }
57
58        // FIXME: Before PHP 7.3 there is no official way to set SameSite attribiute with setcookie().
59        // Use header() function instead until PHP 7.2 is still supported.
60        // setcookie(BatcheditConfig::COOKIE, $admin->getConfig()->serialize(), time() + self::YEAR_IN_SECONDS);
61        header('Set-Cookie: ' . BatcheditConfig::COOKIE . '=' . urlencode($admin->getConfig()->serialize()) .
62                '; SameSite=Strict; Max-Age=' . self::YEAR_IN_SECONDS);
63    }
64
65    /**
66     *
67     */
68    public function onBeforeMetaheaderOutput($event, $param) {
69        $this->addTemplateHeaderInclude($event, 'interface.css');
70        $this->addTemplateHeaderInclude($event, 'server.js');
71        $this->addTemplateHeaderInclude($event, 'interface.js');
72        $this->addTemplateHeaderInclude($event, 'js.cookie.js');
73    }
74
75    /**
76     *
77     */
78    private function isBatchEditAjax() {
79        return !empty($_REQUEST['call']) && $_REQUEST['call'] == 'batchedit';
80    }
81
82    /**
83     *
84     */
85    private function isBatchEdit() {
86        return !empty($_REQUEST['do']) && $_REQUEST['do'] == 'admin' &&
87                !empty($_REQUEST['page']) && $_REQUEST['page'] == 'batchedit';
88    }
89
90    /**
91     *
92     */
93    private function addTemplateHeaderInclude($event, $fileName) {
94        $type = '';
95        $fileName = DOKU_BASE . 'lib/plugins/batchedit/' . $fileName;
96
97        switch (pathinfo($fileName, PATHINFO_EXTENSION)) {
98            case 'css':
99                $type = 'link';
100                $data = array('type' => 'text/css', 'rel' => 'stylesheet', 'href' => $fileName);
101                break;
102
103            case 'js':
104                $type = 'script';
105                $data = array('type' => 'text/javascript', 'charset' => 'utf-8', 'src' => $fileName, '_data' => '', 'defer' => 'defer');
106                break;
107        }
108
109        if ($type != '') {
110            $event->data[$type][] = $data;
111        }
112    }
113}
114