xref: /dokuwiki/inc/Action/Subscribe.php (revision b2c9cd19ff3733a632c8d415256d3096765343f7)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
5272271fcSAndreas 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 */
23*b2c9cd19SAndreas Gohr    public function checkPreconditions() {
24*b2c9cd19SAndreas Gohr        parent::checkPreconditions();
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 {
33272271fcSAndreas Gohr            $this->handleSubscribeData();
34272271fcSAndreas Gohr        } catch(ActionAbort $e) {
35272271fcSAndreas 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
51272271fcSAndreas 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
67272271fcSAndreas Gohr        if(empty($params['action']) || !checkSecurityToken()) return;
6864ab5140SAndreas Gohr
6964ab5140SAndreas Gohr        // Handle POST data, may throw exception.
70272271fcSAndreas 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            );
91272271fcSAndreas 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        }
101272271fcSAndreas Gohr    }
10264ab5140SAndreas Gohr
103272271fcSAndreas Gohr    /**
104272271fcSAndreas Gohr     * Validate POST data
105272271fcSAndreas Gohr     *
106272271fcSAndreas Gohr     * Validates POST data for a subscribe or unsubscribe request. This is the
107272271fcSAndreas Gohr     * default action for the event ACTION_HANDLE_SUBSCRIBE.
108272271fcSAndreas Gohr     *
109272271fcSAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
110272271fcSAndreas Gohr     *
111272271fcSAndreas Gohr     * @param array &$params the parameters: target, style and action
112272271fcSAndreas Gohr     * @throws \Exception
113272271fcSAndreas Gohr     */
114272271fcSAndreas Gohr    public function handlePostData(&$params) {
115272271fcSAndreas Gohr        global $INFO;
116272271fcSAndreas Gohr        global $lang;
117272271fcSAndreas Gohr        global $INPUT;
118272271fcSAndreas Gohr
119272271fcSAndreas Gohr        // Get and validate parameters.
120272271fcSAndreas Gohr        if(!isset($params['target'])) {
121272271fcSAndreas Gohr            throw new \Exception('no subscription target given');
122272271fcSAndreas Gohr        }
123272271fcSAndreas Gohr        $target = $params['target'];
124272271fcSAndreas Gohr        $valid_styles = array('every', 'digest');
125272271fcSAndreas Gohr        if(substr($target, -1, 1) === ':') {
126272271fcSAndreas Gohr            // Allow “list” subscribe style since the target is a namespace.
127272271fcSAndreas Gohr            $valid_styles[] = 'list';
128272271fcSAndreas Gohr        }
129272271fcSAndreas Gohr        $style = valid_input_set(
130272271fcSAndreas Gohr            'style', $valid_styles, $params,
131272271fcSAndreas Gohr            'invalid subscription style given'
132272271fcSAndreas Gohr        );
133272271fcSAndreas Gohr        $action = valid_input_set(
134272271fcSAndreas Gohr            'action', array('subscribe', 'unsubscribe'),
135272271fcSAndreas Gohr            $params, 'invalid subscription action given'
136272271fcSAndreas Gohr        );
137272271fcSAndreas Gohr
138272271fcSAndreas Gohr        // Check other conditions.
139272271fcSAndreas Gohr        if($action === 'subscribe') {
140272271fcSAndreas Gohr            if($INFO['userinfo']['mail'] === '') {
141272271fcSAndreas Gohr                throw new \Exception($lang['subscr_subscribe_noaddress']);
142272271fcSAndreas Gohr            }
143272271fcSAndreas Gohr        } elseif($action === 'unsubscribe') {
144272271fcSAndreas Gohr            $is = false;
145272271fcSAndreas Gohr            foreach($INFO['subscribed'] as $subscr) {
146272271fcSAndreas Gohr                if($subscr['target'] === $target) {
147272271fcSAndreas Gohr                    $is = true;
148272271fcSAndreas Gohr                }
149272271fcSAndreas Gohr            }
150272271fcSAndreas Gohr            if($is === false) {
151272271fcSAndreas Gohr                throw new \Exception(
152272271fcSAndreas Gohr                    sprintf(
153272271fcSAndreas Gohr                        $lang['subscr_not_subscribed'],
154272271fcSAndreas Gohr                        $INPUT->server->str('REMOTE_USER'),
155272271fcSAndreas Gohr                        prettyprint_id($target)
156272271fcSAndreas Gohr                    )
157272271fcSAndreas Gohr                );
158272271fcSAndreas Gohr            }
159272271fcSAndreas Gohr            // subscription_set deletes a subscription if style = null.
160272271fcSAndreas Gohr            $style = null;
161272271fcSAndreas Gohr        }
162272271fcSAndreas Gohr
163272271fcSAndreas Gohr        $params = compact('target', 'style', 'action');
16464ab5140SAndreas Gohr    }
16564ab5140SAndreas Gohr
16664ab5140SAndreas Gohr}
167