1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\EventHandler;
5use dokuwiki\Extension\Event;
6use dokuwiki\Subscriptions\SubscriberManager;
7
8/**
9 * DokuWiki Plugin quicksubscribe (Action Component)
10 *
11 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
12 * @author  Andreas Gohr <gohr@cosmocode.de>
13 */
14class action_plugin_quicksubscribe extends ActionPlugin
15{
16    /** @inheritdoc */
17    public function register(EventHandler $controller)
18    {
19        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
20    }
21
22    /**
23     * Handle subscription/unsubscription AJAX events
24     *
25     * @param Event $event
26     * @param $param
27     */
28    public function handle_ajax_call_unknown(Event $event, $param)
29    {
30        if ($event->data != 'plugin_quicksubscribe') return;
31        $event->preventDefault();
32        $event->stopPropagation();
33
34        global $INPUT;
35
36        $ns = cleanID($INPUT->str('ns')) . ':'; // we only handle namespaces
37        $do = $INPUT->str('do');
38
39        $ok = false;
40        $msg = '';
41
42        $sub = new SubscriberManager();
43
44        if ($do == 'subscribe') {
45            // new subscriptions
46            try {
47                $ok = $sub->add($ns, $_SERVER['REMOTE_USER'], 'list');
48            } catch (\Exception $ignored) {
49                $ok = false;
50            }
51            if ($ok) {
52                $msg = sprintf($this->getLang('sub_succ'), prettyprint_id($ns));
53            } else {
54                $msg = sprintf($this->getLang('sub_fail'), prettyprint_id($ns));
55            }
56        } elseif ($do == 'unsubscribe') {
57            // subscription removal
58            $ok = $sub->remove($ns, $_SERVER['REMOTE_USER']);
59            if ($ok) {
60                $msg = sprintf($this->getLang('unsub_succ'), prettyprint_id($ns));
61            } else {
62                $msg = sprintf($this->getLang('unsub_fail'), prettyprint_id($ns));
63            }
64        }
65
66        if (!$ok) http_status(400);
67        echo '<p>' . $msg . '</p>';
68    }
69}
70