xref: /dokuwiki/inc/Action/Subscribe.php (revision 272271fce369848a156cb5cad81376522f939bbf)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
5*272271fcSAndreas Gohruse dokuwiki\Action\Exception\ActionAbort;
6480336a3SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException;
7480336a3SAndreas Gohr
8ab583a1bSAndreas Gohr/**
9ab583a1bSAndreas Gohr * Class Subscribe
10ab583a1bSAndreas Gohr *
11ab583a1bSAndreas Gohr * E-Mail subscription handling
12ab583a1bSAndreas Gohr *
13ab583a1bSAndreas Gohr * @package dokuwiki\Action
14ab583a1bSAndreas Gohr */
1564ab5140SAndreas Gohrclass Subscribe extends AbstractUserAction {
1664ab5140SAndreas Gohr
1764ab5140SAndreas Gohr    /** @inheritdoc */
18ec701221SAndreas Gohr    public function minimumPermission() {
1964ab5140SAndreas Gohr        return AUTH_READ;
2064ab5140SAndreas Gohr    }
2164ab5140SAndreas Gohr
2264ab5140SAndreas Gohr    /** @inheritdoc */
23480336a3SAndreas Gohr    public function checkPermissions() {
24480336a3SAndreas Gohr        parent::checkPermissions();
25480336a3SAndreas Gohr
26480336a3SAndreas Gohr        global $conf;
27480336a3SAndreas Gohr        if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
28480336a3SAndreas Gohr    }
29480336a3SAndreas Gohr
30480336a3SAndreas Gohr    /** @inheritdoc */
3164ab5140SAndreas Gohr    public function preProcess() {
3264ab5140SAndreas Gohr        try {
33*272271fcSAndreas Gohr            $this->handleSubscribeData();
34*272271fcSAndreas Gohr        } catch(ActionAbort $e) {
35*272271fcSAndreas Gohr            throw $e;
3664ab5140SAndreas Gohr        } catch(\Exception $e) {
3764ab5140SAndreas Gohr            msg($e->getMessage(), -1);
3864ab5140SAndreas Gohr        }
3964ab5140SAndreas Gohr    }
4064ab5140SAndreas Gohr
4164ab5140SAndreas Gohr    /** @inheritdoc */
4264ab5140SAndreas Gohr    public function tplContent() {
4364ab5140SAndreas Gohr        tpl_subscribe();
4464ab5140SAndreas Gohr    }
4564ab5140SAndreas Gohr
4664ab5140SAndreas Gohr    /**
4764ab5140SAndreas Gohr     * Handle page 'subscribe'
4864ab5140SAndreas Gohr     *
4964ab5140SAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
5064ab5140SAndreas Gohr     * @throws \Exception if (un)subscribing fails
51*272271fcSAndreas Gohr     * @throws ActionAbort when (un)subscribing worked
5264ab5140SAndreas Gohr     */
5364ab5140SAndreas Gohr    protected function handleSubscribeData() {
5464ab5140SAndreas Gohr        global $lang;
5564ab5140SAndreas Gohr        global $INFO;
5664ab5140SAndreas Gohr        global $INPUT;
5764ab5140SAndreas Gohr
5864ab5140SAndreas Gohr        // get and preprocess data.
5964ab5140SAndreas Gohr        $params = array();
6064ab5140SAndreas Gohr        foreach(array('target', 'style', 'action') as $param) {
6164ab5140SAndreas Gohr            if($INPUT->has("sub_$param")) {
6264ab5140SAndreas Gohr                $params[$param] = $INPUT->str("sub_$param");
6364ab5140SAndreas Gohr            }
6464ab5140SAndreas Gohr        }
6564ab5140SAndreas Gohr
6664ab5140SAndreas Gohr        // any action given? if not just return and show the subscription page
67*272271fcSAndreas Gohr        if(empty($params['action']) || !checkSecurityToken()) return;
6864ab5140SAndreas Gohr
6964ab5140SAndreas Gohr        // Handle POST data, may throw exception.
70*272271fcSAndreas Gohr        trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, array($this, 'handlePostData'));
7164ab5140SAndreas Gohr
7264ab5140SAndreas Gohr        $target = $params['target'];
7364ab5140SAndreas Gohr        $style = $params['style'];
7464ab5140SAndreas Gohr        $action = $params['action'];
7564ab5140SAndreas Gohr
7664ab5140SAndreas Gohr        // Perform action.
7764ab5140SAndreas Gohr        $sub = new \Subscription();
7864ab5140SAndreas Gohr        if($action == 'unsubscribe') {
7964ab5140SAndreas Gohr            $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
8064ab5140SAndreas Gohr        } else {
8164ab5140SAndreas Gohr            $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
8264ab5140SAndreas Gohr        }
8364ab5140SAndreas Gohr
8464ab5140SAndreas Gohr        if($ok) {
8564ab5140SAndreas Gohr            msg(
8664ab5140SAndreas Gohr                sprintf(
8764ab5140SAndreas Gohr                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
8864ab5140SAndreas Gohr                    prettyprint_id($target)
8964ab5140SAndreas Gohr                ), 1
9064ab5140SAndreas Gohr            );
91*272271fcSAndreas Gohr            throw new ActionAbort('redirect');
9264ab5140SAndreas Gohr        } else {
9364ab5140SAndreas Gohr            throw new \Exception(
9464ab5140SAndreas Gohr                sprintf(
9564ab5140SAndreas Gohr                    $lang["subscr_{$action}_error"],
9664ab5140SAndreas Gohr                    hsc($INFO['userinfo']['name']),
9764ab5140SAndreas Gohr                    prettyprint_id($target)
9864ab5140SAndreas Gohr                )
9964ab5140SAndreas Gohr            );
10064ab5140SAndreas Gohr        }
101*272271fcSAndreas Gohr    }
10264ab5140SAndreas Gohr
103*272271fcSAndreas Gohr    /**
104*272271fcSAndreas Gohr     * Validate POST data
105*272271fcSAndreas Gohr     *
106*272271fcSAndreas Gohr     * Validates POST data for a subscribe or unsubscribe request. This is the
107*272271fcSAndreas Gohr     * default action for the event ACTION_HANDLE_SUBSCRIBE.
108*272271fcSAndreas Gohr     *
109*272271fcSAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
110*272271fcSAndreas Gohr     *
111*272271fcSAndreas Gohr     * @param array &$params the parameters: target, style and action
112*272271fcSAndreas Gohr     * @throws \Exception
113*272271fcSAndreas Gohr     */
114*272271fcSAndreas Gohr    public function handlePostData(&$params) {
115*272271fcSAndreas Gohr        global $INFO;
116*272271fcSAndreas Gohr        global $lang;
117*272271fcSAndreas Gohr        global $INPUT;
118*272271fcSAndreas Gohr
119*272271fcSAndreas Gohr        // Get and validate parameters.
120*272271fcSAndreas Gohr        if(!isset($params['target'])) {
121*272271fcSAndreas Gohr            throw new \Exception('no subscription target given');
122*272271fcSAndreas Gohr        }
123*272271fcSAndreas Gohr        $target = $params['target'];
124*272271fcSAndreas Gohr        $valid_styles = array('every', 'digest');
125*272271fcSAndreas Gohr        if(substr($target, -1, 1) === ':') {
126*272271fcSAndreas Gohr            // Allow “list” subscribe style since the target is a namespace.
127*272271fcSAndreas Gohr            $valid_styles[] = 'list';
128*272271fcSAndreas Gohr        }
129*272271fcSAndreas Gohr        $style = valid_input_set(
130*272271fcSAndreas Gohr            'style', $valid_styles, $params,
131*272271fcSAndreas Gohr            'invalid subscription style given'
132*272271fcSAndreas Gohr        );
133*272271fcSAndreas Gohr        $action = valid_input_set(
134*272271fcSAndreas Gohr            'action', array('subscribe', 'unsubscribe'),
135*272271fcSAndreas Gohr            $params, 'invalid subscription action given'
136*272271fcSAndreas Gohr        );
137*272271fcSAndreas Gohr
138*272271fcSAndreas Gohr        // Check other conditions.
139*272271fcSAndreas Gohr        if($action === 'subscribe') {
140*272271fcSAndreas Gohr            if($INFO['userinfo']['mail'] === '') {
141*272271fcSAndreas Gohr                throw new \Exception($lang['subscr_subscribe_noaddress']);
142*272271fcSAndreas Gohr            }
143*272271fcSAndreas Gohr        } elseif($action === 'unsubscribe') {
144*272271fcSAndreas Gohr            $is = false;
145*272271fcSAndreas Gohr            foreach($INFO['subscribed'] as $subscr) {
146*272271fcSAndreas Gohr                if($subscr['target'] === $target) {
147*272271fcSAndreas Gohr                    $is = true;
148*272271fcSAndreas Gohr                }
149*272271fcSAndreas Gohr            }
150*272271fcSAndreas Gohr            if($is === false) {
151*272271fcSAndreas Gohr                throw new \Exception(
152*272271fcSAndreas Gohr                    sprintf(
153*272271fcSAndreas Gohr                        $lang['subscr_not_subscribed'],
154*272271fcSAndreas Gohr                        $INPUT->server->str('REMOTE_USER'),
155*272271fcSAndreas Gohr                        prettyprint_id($target)
156*272271fcSAndreas Gohr                    )
157*272271fcSAndreas Gohr                );
158*272271fcSAndreas Gohr            }
159*272271fcSAndreas Gohr            // subscription_set deletes a subscription if style = null.
160*272271fcSAndreas Gohr            $style = null;
161*272271fcSAndreas Gohr        }
162*272271fcSAndreas Gohr
163*272271fcSAndreas Gohr        $params = compact('target', 'style', 'action');
16464ab5140SAndreas Gohr    }
16564ab5140SAndreas Gohr
16664ab5140SAndreas Gohr}
167