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