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