Completed
Push — actionrefactor ( 480336...d77352 )
by Andreas
03:58
created

Subscribe::checkPermissions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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