xref: /dokuwiki/inc/Action/Subscribe.php (revision 820934dc5dde4982117063a0296c135334fdc7a3)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
5272271fcSAndreas Gohruse dokuwiki\Action\Exception\ActionAbort;
6480336a3SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException;
775d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager;
8cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
9480336a3SAndreas Gohr
10ab583a1bSAndreas Gohr/**
11ab583a1bSAndreas Gohr * Class Subscribe
12ab583a1bSAndreas Gohr *
13ab583a1bSAndreas Gohr * E-Mail subscription handling
14ab583a1bSAndreas Gohr *
15ab583a1bSAndreas Gohr * @package dokuwiki\Action
16ab583a1bSAndreas Gohr */
1764ab5140SAndreas Gohrclass Subscribe extends AbstractUserAction {
1864ab5140SAndreas Gohr
1964ab5140SAndreas Gohr    /** @inheritdoc */
20ec701221SAndreas Gohr    public function minimumPermission() {
2164ab5140SAndreas Gohr        return AUTH_READ;
2264ab5140SAndreas Gohr    }
2364ab5140SAndreas Gohr
2464ab5140SAndreas Gohr    /** @inheritdoc */
25b2c9cd19SAndreas Gohr    public function checkPreconditions() {
26b2c9cd19SAndreas Gohr        parent::checkPreconditions();
27480336a3SAndreas Gohr
28480336a3SAndreas Gohr        global $conf;
29480336a3SAndreas Gohr        if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
30480336a3SAndreas Gohr    }
31480336a3SAndreas Gohr
32480336a3SAndreas Gohr    /** @inheritdoc */
3364ab5140SAndreas Gohr    public function preProcess() {
3464ab5140SAndreas Gohr        try {
35272271fcSAndreas Gohr            $this->handleSubscribeData();
36272271fcSAndreas Gohr        } catch(ActionAbort $e) {
37272271fcSAndreas Gohr            throw $e;
3864ab5140SAndreas Gohr        } catch(\Exception $e) {
3964ab5140SAndreas Gohr            msg($e->getMessage(), -1);
4064ab5140SAndreas Gohr        }
4164ab5140SAndreas Gohr    }
4264ab5140SAndreas Gohr
4364ab5140SAndreas Gohr    /** @inheritdoc */
4464ab5140SAndreas Gohr    public function tplContent() {
4564ab5140SAndreas Gohr        tpl_subscribe();
4664ab5140SAndreas Gohr    }
4764ab5140SAndreas Gohr
4864ab5140SAndreas Gohr    /**
4964ab5140SAndreas Gohr     * Handle page 'subscribe'
5064ab5140SAndreas Gohr     *
5164ab5140SAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
5264ab5140SAndreas Gohr     * @throws \Exception if (un)subscribing fails
53272271fcSAndreas Gohr     * @throws ActionAbort when (un)subscribing worked
5464ab5140SAndreas Gohr     */
5564ab5140SAndreas Gohr    protected function handleSubscribeData() {
5664ab5140SAndreas Gohr        global $lang;
5764ab5140SAndreas Gohr        global $INFO;
5864ab5140SAndreas Gohr        global $INPUT;
5964ab5140SAndreas Gohr
6064ab5140SAndreas Gohr        // get and preprocess data.
6164ab5140SAndreas Gohr        $params = array();
6264ab5140SAndreas Gohr        foreach(array('target', 'style', 'action') as $param) {
6364ab5140SAndreas Gohr            if($INPUT->has("sub_$param")) {
6464ab5140SAndreas Gohr                $params[$param] = $INPUT->str("sub_$param");
6564ab5140SAndreas Gohr            }
6664ab5140SAndreas Gohr        }
6764ab5140SAndreas Gohr
6864ab5140SAndreas Gohr        // any action given? if not just return and show the subscription page
69272271fcSAndreas Gohr        if(empty($params['action']) || !checkSecurityToken()) return;
7064ab5140SAndreas Gohr
7164ab5140SAndreas Gohr        // Handle POST data, may throw exception.
72cbb44eabSAndreas Gohr        Event::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, array($this, 'handlePostData'));
7364ab5140SAndreas Gohr
7464ab5140SAndreas Gohr        $target = $params['target'];
7564ab5140SAndreas Gohr        $style = $params['style'];
7664ab5140SAndreas Gohr        $action = $params['action'];
7764ab5140SAndreas Gohr
7864ab5140SAndreas Gohr        // Perform action.
7975d66495SMichael Große        $subManager = new SubscriberManager();
80*820934dcSAndreas Gohr        if($action === 'unsubscribe') {
8175d66495SMichael Große            $ok = $subManager->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
8264ab5140SAndreas Gohr        } else {
8375d66495SMichael Große            $ok = $subManager->add($target, $INPUT->server->str('REMOTE_USER'), $style);
8464ab5140SAndreas Gohr        }
8564ab5140SAndreas Gohr
8664ab5140SAndreas Gohr        if($ok) {
8764ab5140SAndreas Gohr            msg(
8864ab5140SAndreas Gohr                sprintf(
8964ab5140SAndreas Gohr                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
9064ab5140SAndreas Gohr                    prettyprint_id($target)
9164ab5140SAndreas Gohr                ), 1
9264ab5140SAndreas Gohr            );
93272271fcSAndreas Gohr            throw new ActionAbort('redirect');
94*820934dcSAndreas Gohr        }
95*820934dcSAndreas Gohr
9664ab5140SAndreas Gohr        throw new \Exception(
9764ab5140SAndreas Gohr            sprintf(
9864ab5140SAndreas Gohr                $lang["subscr_{$action}_error"],
9964ab5140SAndreas Gohr                hsc($INFO['userinfo']['name']),
10064ab5140SAndreas Gohr                prettyprint_id($target)
10164ab5140SAndreas Gohr            )
10264ab5140SAndreas Gohr        );
10364ab5140SAndreas Gohr    }
10464ab5140SAndreas Gohr
105272271fcSAndreas Gohr    /**
106272271fcSAndreas Gohr     * Validate POST data
107272271fcSAndreas Gohr     *
108272271fcSAndreas Gohr     * Validates POST data for a subscribe or unsubscribe request. This is the
109272271fcSAndreas Gohr     * default action for the event ACTION_HANDLE_SUBSCRIBE.
110272271fcSAndreas Gohr     *
111272271fcSAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
112272271fcSAndreas Gohr     *
113272271fcSAndreas Gohr     * @param array &$params the parameters: target, style and action
114272271fcSAndreas Gohr     * @throws \Exception
115272271fcSAndreas Gohr     */
116272271fcSAndreas Gohr    public function handlePostData(&$params) {
117272271fcSAndreas Gohr        global $INFO;
118272271fcSAndreas Gohr        global $lang;
119272271fcSAndreas Gohr        global $INPUT;
120272271fcSAndreas Gohr
121272271fcSAndreas Gohr        // Get and validate parameters.
122272271fcSAndreas Gohr        if(!isset($params['target'])) {
123272271fcSAndreas Gohr            throw new \Exception('no subscription target given');
124272271fcSAndreas Gohr        }
125272271fcSAndreas Gohr        $target = $params['target'];
126272271fcSAndreas Gohr        $valid_styles = array('every', 'digest');
127272271fcSAndreas Gohr        if(substr($target, -1, 1) === ':') {
128272271fcSAndreas Gohr            // Allow “list” subscribe style since the target is a namespace.
129272271fcSAndreas Gohr            $valid_styles[] = 'list';
130272271fcSAndreas Gohr        }
131272271fcSAndreas Gohr        $style = valid_input_set(
132272271fcSAndreas Gohr            'style', $valid_styles, $params,
133272271fcSAndreas Gohr            'invalid subscription style given'
134272271fcSAndreas Gohr        );
135272271fcSAndreas Gohr        $action = valid_input_set(
136272271fcSAndreas Gohr            'action', array('subscribe', 'unsubscribe'),
137272271fcSAndreas Gohr            $params, 'invalid subscription action given'
138272271fcSAndreas Gohr        );
139272271fcSAndreas Gohr
140272271fcSAndreas Gohr        // Check other conditions.
141272271fcSAndreas Gohr        if($action === 'subscribe') {
142272271fcSAndreas Gohr            if($INFO['userinfo']['mail'] === '') {
143272271fcSAndreas Gohr                throw new \Exception($lang['subscr_subscribe_noaddress']);
144272271fcSAndreas Gohr            }
145272271fcSAndreas Gohr        } elseif($action === 'unsubscribe') {
146272271fcSAndreas Gohr            $is = false;
147272271fcSAndreas Gohr            foreach($INFO['subscribed'] as $subscr) {
148272271fcSAndreas Gohr                if($subscr['target'] === $target) {
149272271fcSAndreas Gohr                    $is = true;
150272271fcSAndreas Gohr                }
151272271fcSAndreas Gohr            }
152272271fcSAndreas Gohr            if($is === false) {
153272271fcSAndreas Gohr                throw new \Exception(
154272271fcSAndreas Gohr                    sprintf(
155272271fcSAndreas Gohr                        $lang['subscr_not_subscribed'],
156272271fcSAndreas Gohr                        $INPUT->server->str('REMOTE_USER'),
157272271fcSAndreas Gohr                        prettyprint_id($target)
158272271fcSAndreas Gohr                    )
159272271fcSAndreas Gohr                );
160272271fcSAndreas Gohr            }
161272271fcSAndreas Gohr            // subscription_set deletes a subscription if style = null.
162272271fcSAndreas Gohr            $style = null;
163272271fcSAndreas Gohr        }
164272271fcSAndreas Gohr
165272271fcSAndreas Gohr        $params = compact('target', 'style', 'action');
16664ab5140SAndreas Gohr    }
16764ab5140SAndreas Gohr
16864ab5140SAndreas Gohr}
169