xref: /dokuwiki/inc/Action/Subscribe.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
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    {
5173022918SAndreas 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(
96*dccd6b2bSAndreas Gohr                    $lang["subscr_{$action}_success"],
97*dccd6b2bSAndreas Gohr                    hsc($INFO['userinfo']['name']),
9864ab5140SAndreas Gohr                    prettyprint_id($target)
99*dccd6b2bSAndreas Gohr                ),
100*dccd6b2bSAndreas Gohr                1
10164ab5140SAndreas Gohr            );
102272271fcSAndreas Gohr            throw new ActionAbort('redirect');
103820934dcSAndreas Gohr        }
104820934dcSAndreas Gohr
10579a2d784SGerrit Uitslag        throw new Exception(
10664ab5140SAndreas Gohr            sprintf(
10764ab5140SAndreas Gohr                $lang["subscr_{$action}_error"],
10864ab5140SAndreas Gohr                hsc($INFO['userinfo']['name']),
10964ab5140SAndreas Gohr                prettyprint_id($target)
11064ab5140SAndreas Gohr            )
11164ab5140SAndreas Gohr        );
11264ab5140SAndreas Gohr    }
11364ab5140SAndreas Gohr
114272271fcSAndreas Gohr    /**
115272271fcSAndreas Gohr     * Validate POST data
116272271fcSAndreas Gohr     *
117272271fcSAndreas Gohr     * Validates POST data for a subscribe or unsubscribe request. This is the
118272271fcSAndreas Gohr     * default action for the event ACTION_HANDLE_SUBSCRIBE.
119272271fcSAndreas Gohr     *
120272271fcSAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
121272271fcSAndreas Gohr     *
122272271fcSAndreas Gohr     * @param array &$params the parameters: target, style and action
12379a2d784SGerrit Uitslag     * @throws Exception
124272271fcSAndreas Gohr     */
125848cb786SSatoshi Sahara    public function handlePostData(&$params)
126848cb786SSatoshi Sahara    {
127272271fcSAndreas Gohr        global $INFO;
128272271fcSAndreas Gohr        global $lang;
129272271fcSAndreas Gohr        global $INPUT;
130272271fcSAndreas Gohr
131272271fcSAndreas Gohr        // Get and validate parameters.
132272271fcSAndreas Gohr        if (!isset($params['target'])) {
13379a2d784SGerrit Uitslag            throw new Exception('no subscription target given');
134272271fcSAndreas Gohr        }
135272271fcSAndreas Gohr        $target = $params['target'];
1366723156fSAndreas Gohr        $valid_styles = ['every', 'digest'];
137272271fcSAndreas Gohr        if (substr($target, -1, 1) === ':') {
138272271fcSAndreas Gohr            // Allow “list” subscribe style since the target is a namespace.
139272271fcSAndreas Gohr            $valid_styles[] = 'list';
140272271fcSAndreas Gohr        }
141272271fcSAndreas Gohr        $style = valid_input_set(
142*dccd6b2bSAndreas Gohr            'style',
143*dccd6b2bSAndreas Gohr            $valid_styles,
144*dccd6b2bSAndreas Gohr            $params,
145272271fcSAndreas Gohr            'invalid subscription style given'
146272271fcSAndreas Gohr        );
147272271fcSAndreas Gohr        $action = valid_input_set(
148*dccd6b2bSAndreas Gohr            'action',
149*dccd6b2bSAndreas Gohr            ['subscribe', 'unsubscribe'],
150*dccd6b2bSAndreas Gohr            $params,
151*dccd6b2bSAndreas Gohr            'invalid subscription action given'
152272271fcSAndreas Gohr        );
153272271fcSAndreas Gohr
154272271fcSAndreas Gohr        // Check other conditions.
155272271fcSAndreas Gohr        if ($action === 'subscribe') {
156272271fcSAndreas Gohr            if ($INFO['userinfo']['mail'] === '') {
15779a2d784SGerrit Uitslag                throw new Exception($lang['subscr_subscribe_noaddress']);
158272271fcSAndreas Gohr            }
159272271fcSAndreas Gohr        } elseif ($action === 'unsubscribe') {
160272271fcSAndreas Gohr            $is = false;
161272271fcSAndreas Gohr            foreach ($INFO['subscribed'] as $subscr) {
162272271fcSAndreas Gohr                if ($subscr['target'] === $target) {
163272271fcSAndreas Gohr                    $is = true;
164272271fcSAndreas Gohr                }
165272271fcSAndreas Gohr            }
166272271fcSAndreas Gohr            if ($is === false) {
16779a2d784SGerrit Uitslag                throw new Exception(
168272271fcSAndreas Gohr                    sprintf(
169272271fcSAndreas Gohr                        $lang['subscr_not_subscribed'],
170272271fcSAndreas Gohr                        $INPUT->server->str('REMOTE_USER'),
171272271fcSAndreas Gohr                        prettyprint_id($target)
172272271fcSAndreas Gohr                    )
173272271fcSAndreas Gohr                );
174272271fcSAndreas Gohr            }
175272271fcSAndreas Gohr            // subscription_set deletes a subscription if style = null.
176272271fcSAndreas Gohr            $style = null;
177272271fcSAndreas Gohr        }
178272271fcSAndreas Gohr
1796723156fSAndreas Gohr        $params = ['target' => $target, 'style' => $style, 'action' => $action];
18064ab5140SAndreas Gohr    }
18164ab5140SAndreas Gohr}
182