xref: /dokuwiki/inc/Action/Subscribe.php (revision 64ab5140f7b1c996873fbfe9bab26d9202fbb773)
1<?php
2/**
3 * Created by IntelliJ IDEA.
4 * User: andi
5 * Date: 2/10/17
6 * Time: 12:31 PM
7 */
8
9namespace dokuwiki\Action;
10
11class Subscribe extends AbstractUserAction {
12
13    /** @inheritdoc */
14    function minimumPermission() {
15        return AUTH_READ;
16    }
17
18    /** @inheritdoc */
19    public function preProcess() {
20        $act = $this->actionname;
21        try {
22            $act = $this->handleSubscribeData();
23        } catch(\Exception $e) {
24            msg($e->getMessage(), -1);
25        }
26        return $act;
27    }
28
29    /** @inheritdoc */
30    public function tplContent() {
31        tpl_subscribe();
32    }
33
34    /**
35     * Handle page 'subscribe'
36     *
37     * Throws exception on error.
38     *
39     * @author Adrian Lang <lang@cosmocode.de>
40     *
41     * @return string action command
42     * @throws \Exception if (un)subscribing fails
43     */
44    protected function handleSubscribeData() {
45        global $lang;
46        global $INFO;
47        global $ID;
48        global $INPUT;
49
50        // get and preprocess data.
51        $params = array();
52        foreach(array('target', 'style', 'action') as $param) {
53            if($INPUT->has("sub_$param")) {
54                $params[$param] = $INPUT->str("sub_$param");
55            }
56        }
57
58        // any action given? if not just return and show the subscription page
59        if(empty($params['action']) || !checkSecurityToken()) return $this->actionname;
60
61        // Handle POST data, may throw exception.
62        trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
63
64        $target = $params['target'];
65        $style = $params['style'];
66        $action = $params['action'];
67
68        // Perform action.
69        $sub = new \Subscription();
70        if($action == 'unsubscribe') {
71            $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
72        } else {
73            $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
74        }
75
76        if($ok) {
77            msg(
78                sprintf(
79                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
80                    prettyprint_id($target)
81                ), 1
82            );
83            act_redirect($ID, $this->actionname);
84        } else {
85            throw new \Exception(
86                sprintf(
87                    $lang["subscr_{$action}_error"],
88                    hsc($INFO['userinfo']['name']),
89                    prettyprint_id($target)
90                )
91            );
92        }
93
94        // Assure that we have valid data if act_redirect somehow fails. should never be reached
95        $INFO['subscribed'] = $sub->user_subscription();
96        return 'show';
97    }
98
99}
100