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