xref: /dokuwiki/inc/Action/Subscribe.php (revision 480336a332edb3421967e8a704976494c582e76c)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
5*480336a3SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException;
6*480336a3SAndreas Gohr
7ab583a1bSAndreas Gohr/**
8ab583a1bSAndreas Gohr * Class Subscribe
9ab583a1bSAndreas Gohr *
10ab583a1bSAndreas Gohr * E-Mail subscription handling
11ab583a1bSAndreas Gohr *
12ab583a1bSAndreas Gohr * @package dokuwiki\Action
13ab583a1bSAndreas Gohr */
1464ab5140SAndreas Gohrclass Subscribe extends AbstractUserAction {
1564ab5140SAndreas Gohr
1664ab5140SAndreas Gohr    /** @inheritdoc */
17ec701221SAndreas Gohr    public function minimumPermission() {
1864ab5140SAndreas Gohr        return AUTH_READ;
1964ab5140SAndreas Gohr    }
2064ab5140SAndreas Gohr
2164ab5140SAndreas Gohr    /** @inheritdoc */
22*480336a3SAndreas Gohr    public function checkPermissions() {
23*480336a3SAndreas Gohr        parent::checkPermissions();
24*480336a3SAndreas Gohr
25*480336a3SAndreas Gohr        global $conf;
26*480336a3SAndreas Gohr        if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
27*480336a3SAndreas Gohr    }
28*480336a3SAndreas Gohr
29*480336a3SAndreas Gohr    /** @inheritdoc */
3064ab5140SAndreas Gohr    public function preProcess() {
3164ab5140SAndreas Gohr        $act = $this->actionname;
3264ab5140SAndreas Gohr        try {
3364ab5140SAndreas Gohr            $act = $this->handleSubscribeData();
3464ab5140SAndreas Gohr        } catch(\Exception $e) {
3564ab5140SAndreas Gohr            msg($e->getMessage(), -1);
3664ab5140SAndreas Gohr        }
3764ab5140SAndreas Gohr        return $act;
3864ab5140SAndreas Gohr    }
3964ab5140SAndreas Gohr
4064ab5140SAndreas Gohr    /** @inheritdoc */
4164ab5140SAndreas Gohr    public function tplContent() {
4264ab5140SAndreas Gohr        tpl_subscribe();
4364ab5140SAndreas Gohr    }
4464ab5140SAndreas Gohr
4564ab5140SAndreas Gohr    /**
4664ab5140SAndreas Gohr     * Handle page 'subscribe'
4764ab5140SAndreas Gohr     *
4864ab5140SAndreas Gohr     * Throws exception on error.
4964ab5140SAndreas Gohr     *
5064ab5140SAndreas Gohr     * @author Adrian Lang <lang@cosmocode.de>
5164ab5140SAndreas Gohr     *
5264ab5140SAndreas Gohr     * @return string action command
5364ab5140SAndreas Gohr     * @throws \Exception if (un)subscribing fails
5464ab5140SAndreas Gohr     */
5564ab5140SAndreas Gohr    protected function handleSubscribeData() {
5664ab5140SAndreas Gohr        global $lang;
5764ab5140SAndreas Gohr        global $INFO;
5864ab5140SAndreas Gohr        global $ID;
5964ab5140SAndreas Gohr        global $INPUT;
6064ab5140SAndreas Gohr
6164ab5140SAndreas Gohr        // get and preprocess data.
6264ab5140SAndreas Gohr        $params = array();
6364ab5140SAndreas Gohr        foreach(array('target', 'style', 'action') as $param) {
6464ab5140SAndreas Gohr            if($INPUT->has("sub_$param")) {
6564ab5140SAndreas Gohr                $params[$param] = $INPUT->str("sub_$param");
6664ab5140SAndreas Gohr            }
6764ab5140SAndreas Gohr        }
6864ab5140SAndreas Gohr
6964ab5140SAndreas Gohr        // any action given? if not just return and show the subscription page
7064ab5140SAndreas Gohr        if(empty($params['action']) || !checkSecurityToken()) return $this->actionname;
7164ab5140SAndreas Gohr
7264ab5140SAndreas Gohr        // Handle POST data, may throw exception.
7364ab5140SAndreas Gohr        trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7464ab5140SAndreas Gohr
7564ab5140SAndreas Gohr        $target = $params['target'];
7664ab5140SAndreas Gohr        $style = $params['style'];
7764ab5140SAndreas Gohr        $action = $params['action'];
7864ab5140SAndreas Gohr
7964ab5140SAndreas Gohr        // Perform action.
8064ab5140SAndreas Gohr        $sub = new \Subscription();
8164ab5140SAndreas Gohr        if($action == 'unsubscribe') {
8264ab5140SAndreas Gohr            $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
8364ab5140SAndreas Gohr        } else {
8464ab5140SAndreas Gohr            $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
8564ab5140SAndreas Gohr        }
8664ab5140SAndreas Gohr
8764ab5140SAndreas Gohr        if($ok) {
8864ab5140SAndreas Gohr            msg(
8964ab5140SAndreas Gohr                sprintf(
9064ab5140SAndreas Gohr                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
9164ab5140SAndreas Gohr                    prettyprint_id($target)
9264ab5140SAndreas Gohr                ), 1
9364ab5140SAndreas Gohr            );
9464ab5140SAndreas Gohr            act_redirect($ID, $this->actionname);
9564ab5140SAndreas Gohr        } else {
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
10564ab5140SAndreas Gohr        // Assure that we have valid data if act_redirect somehow fails. should never be reached
10664ab5140SAndreas Gohr        $INFO['subscribed'] = $sub->user_subscription();
10764ab5140SAndreas Gohr        return 'show';
10864ab5140SAndreas Gohr    }
10964ab5140SAndreas Gohr
11064ab5140SAndreas Gohr}
111