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