xref: /dokuwiki/inc/Action/Subscribe.php (revision 73022918a947abda7eee4d7d2302ffd28fdb78e0)
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;
9848cb786SSatoshi Saharause dokuwiki\Ui;
1079a2d784SGerrit Uitslaguse Exception;
11480336a3SAndreas Gohr
12ab583a1bSAndreas Gohr/**
13ab583a1bSAndreas Gohr * Class Subscribe
14ab583a1bSAndreas Gohr *
15ab583a1bSAndreas Gohr * E-Mail subscription handling
16ab583a1bSAndreas Gohr *
17ab583a1bSAndreas Gohr * @package dokuwiki\Action
18ab583a1bSAndreas Gohr */
19848cb786SSatoshi Saharaclass Subscribe extends AbstractUserAction
20848cb786SSatoshi Sahara{
2164ab5140SAndreas Gohr    /** @inheritdoc */
22848cb786SSatoshi Sahara    public function minimumPermission()
23848cb786SSatoshi Sahara    {
2464ab5140SAndreas Gohr        return AUTH_READ;
2564ab5140SAndreas Gohr    }
2664ab5140SAndreas Gohr
2764ab5140SAndreas Gohr    /** @inheritdoc */
28848cb786SSatoshi Sahara    public function checkPreconditions()
29848cb786SSatoshi Sahara    {
30b2c9cd19SAndreas Gohr        parent::checkPreconditions();
31480336a3SAndreas Gohr
32480336a3SAndreas Gohr        global $conf;
33480336a3SAndreas Gohr        if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
34480336a3SAndreas Gohr    }
35480336a3SAndreas Gohr
36480336a3SAndreas Gohr    /** @inheritdoc */
37848cb786SSatoshi Sahara    public function preProcess()
38848cb786SSatoshi Sahara    {
3964ab5140SAndreas Gohr        try {
40272271fcSAndreas Gohr            $this->handleSubscribeData();
41272271fcSAndreas Gohr        } catch (ActionAbort $e) {
42272271fcSAndreas Gohr            throw $e;
4379a2d784SGerrit Uitslag        } catch (Exception $e) {
4464ab5140SAndreas Gohr            msg($e->getMessage(), -1);
4564ab5140SAndreas Gohr        }
4664ab5140SAndreas Gohr    }
4764ab5140SAndreas Gohr
4864ab5140SAndreas Gohr    /** @inheritdoc */
49848cb786SSatoshi Sahara    public function tplContent()
50848cb786SSatoshi Sahara    {
51*73022918SAndreas Gohr        (new Ui\Subscribe())->show();
5264ab5140SAndreas Gohr    }
5364ab5140SAndreas Gohr
5464ab5140SAndreas Gohr    /**
5564ab5140SAndreas Gohr     * Handle page 'subscribe'
5664ab5140SAndreas Gohr     *
5764ab5140SAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
5879a2d784SGerrit Uitslag     * @throws Exception if (un)subscribing fails
59272271fcSAndreas Gohr     * @throws ActionAbort when (un)subscribing worked
6064ab5140SAndreas Gohr     */
61848cb786SSatoshi Sahara    protected function handleSubscribeData()
62848cb786SSatoshi Sahara    {
6364ab5140SAndreas Gohr        global $lang;
6464ab5140SAndreas Gohr        global $INFO;
6564ab5140SAndreas Gohr        global $INPUT;
6664ab5140SAndreas Gohr
6764ab5140SAndreas Gohr        // get and preprocess data.
686723156fSAndreas Gohr        $params = [];
696723156fSAndreas Gohr        foreach (['target', 'style', 'action'] as $param) {
7064ab5140SAndreas Gohr            if ($INPUT->has("sub_$param")) {
7164ab5140SAndreas Gohr                $params[$param] = $INPUT->str("sub_$param");
7264ab5140SAndreas Gohr            }
7364ab5140SAndreas Gohr        }
7464ab5140SAndreas Gohr
7564ab5140SAndreas Gohr        // any action given? if not just return and show the subscription page
76272271fcSAndreas Gohr        if (empty($params['action']) || !checkSecurityToken()) return;
7764ab5140SAndreas Gohr
7864ab5140SAndreas Gohr        // Handle POST data, may throw exception.
796723156fSAndreas Gohr        Event::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, [$this, 'handlePostData']);
8064ab5140SAndreas Gohr
8164ab5140SAndreas Gohr        $target = $params['target'];
8264ab5140SAndreas Gohr        $style = $params['style'];
8364ab5140SAndreas Gohr        $action = $params['action'];
8464ab5140SAndreas Gohr
8564ab5140SAndreas Gohr        // Perform action.
8675d66495SMichael Große        $subManager = new SubscriberManager();
87820934dcSAndreas Gohr        if ($action === 'unsubscribe') {
8875d66495SMichael Große            $ok = $subManager->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
8964ab5140SAndreas Gohr        } else {
9075d66495SMichael Große            $ok = $subManager->add($target, $INPUT->server->str('REMOTE_USER'), $style);
9164ab5140SAndreas Gohr        }
9264ab5140SAndreas Gohr
9364ab5140SAndreas Gohr        if ($ok) {
9464ab5140SAndreas Gohr            msg(
9564ab5140SAndreas Gohr                sprintf(
9664ab5140SAndreas Gohr                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
9764ab5140SAndreas Gohr                    prettyprint_id($target)
9864ab5140SAndreas Gohr                ), 1
9964ab5140SAndreas Gohr            );
100272271fcSAndreas Gohr            throw new ActionAbort('redirect');
101820934dcSAndreas Gohr        }
102820934dcSAndreas Gohr
10379a2d784SGerrit Uitslag        throw new Exception(
10464ab5140SAndreas Gohr            sprintf(
10564ab5140SAndreas Gohr                $lang["subscr_{$action}_error"],
10664ab5140SAndreas Gohr                hsc($INFO['userinfo']['name']),
10764ab5140SAndreas Gohr                prettyprint_id($target)
10864ab5140SAndreas Gohr            )
10964ab5140SAndreas Gohr        );
11064ab5140SAndreas Gohr    }
11164ab5140SAndreas Gohr
112272271fcSAndreas Gohr    /**
113272271fcSAndreas Gohr     * Validate POST data
114272271fcSAndreas Gohr     *
115272271fcSAndreas Gohr     * Validates POST data for a subscribe or unsubscribe request. This is the
116272271fcSAndreas Gohr     * default action for the event ACTION_HANDLE_SUBSCRIBE.
117272271fcSAndreas Gohr     *
118272271fcSAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
119272271fcSAndreas Gohr     *
120272271fcSAndreas Gohr     * @param array &$params the parameters: target, style and action
12179a2d784SGerrit Uitslag     * @throws Exception
122272271fcSAndreas Gohr     */
123848cb786SSatoshi Sahara    public function handlePostData(&$params)
124848cb786SSatoshi Sahara    {
125272271fcSAndreas Gohr        global $INFO;
126272271fcSAndreas Gohr        global $lang;
127272271fcSAndreas Gohr        global $INPUT;
128272271fcSAndreas Gohr
129272271fcSAndreas Gohr        // Get and validate parameters.
130272271fcSAndreas Gohr        if (!isset($params['target'])) {
13179a2d784SGerrit Uitslag            throw new Exception('no subscription target given');
132272271fcSAndreas Gohr        }
133272271fcSAndreas Gohr        $target = $params['target'];
1346723156fSAndreas Gohr        $valid_styles = ['every', 'digest'];
135272271fcSAndreas Gohr        if (substr($target, -1, 1) === ':') {
136272271fcSAndreas Gohr            // Allow “list” subscribe style since the target is a namespace.
137272271fcSAndreas Gohr            $valid_styles[] = 'list';
138272271fcSAndreas Gohr        }
139272271fcSAndreas Gohr        $style = valid_input_set(
140272271fcSAndreas Gohr            'style', $valid_styles, $params,
141272271fcSAndreas Gohr            'invalid subscription style given'
142272271fcSAndreas Gohr        );
143272271fcSAndreas Gohr        $action = valid_input_set(
1446723156fSAndreas Gohr            'action', ['subscribe', 'unsubscribe'],
145272271fcSAndreas Gohr            $params, 'invalid subscription action given'
146272271fcSAndreas Gohr        );
147272271fcSAndreas Gohr
148272271fcSAndreas Gohr        // Check other conditions.
149272271fcSAndreas Gohr        if ($action === 'subscribe') {
150272271fcSAndreas Gohr            if ($INFO['userinfo']['mail'] === '') {
15179a2d784SGerrit Uitslag                throw new Exception($lang['subscr_subscribe_noaddress']);
152272271fcSAndreas Gohr            }
153272271fcSAndreas Gohr        } elseif ($action === 'unsubscribe') {
154272271fcSAndreas Gohr            $is = false;
155272271fcSAndreas Gohr            foreach ($INFO['subscribed'] as $subscr) {
156272271fcSAndreas Gohr                if ($subscr['target'] === $target) {
157272271fcSAndreas Gohr                    $is = true;
158272271fcSAndreas Gohr                }
159272271fcSAndreas Gohr            }
160272271fcSAndreas Gohr            if ($is === false) {
16179a2d784SGerrit Uitslag                throw new Exception(
162272271fcSAndreas Gohr                    sprintf(
163272271fcSAndreas Gohr                        $lang['subscr_not_subscribed'],
164272271fcSAndreas Gohr                        $INPUT->server->str('REMOTE_USER'),
165272271fcSAndreas Gohr                        prettyprint_id($target)
166272271fcSAndreas Gohr                    )
167272271fcSAndreas Gohr                );
168272271fcSAndreas Gohr            }
169272271fcSAndreas Gohr            // subscription_set deletes a subscription if style = null.
170272271fcSAndreas Gohr            $style = null;
171272271fcSAndreas Gohr        }
172272271fcSAndreas Gohr
1736723156fSAndreas Gohr        $params = ['target' => $target, 'style' => $style, 'action' => $action];
17464ab5140SAndreas Gohr    }
17564ab5140SAndreas Gohr}
176