about summary refs log tree commit diff stats
path: root/wiki/inc/Action/Subscribe.php
diff options
context:
space:
mode:
authorahriman <ahriman@falte.red>2018-12-03 19:22:25 -0500
committerahriman <ahriman@falte.red>2018-12-03 19:22:25 -0500
commit0ae8cbf5c0b1a198b963490985b7738392ebcb97 (patch)
treeb2c77ae72c6b717e2b97492065196ac5ffb2d9e2 /wiki/inc/Action/Subscribe.php
parentf57f6cc5a2d159f90168d292437dc4bd8cd7f934 (diff)
downloadsite-0ae8cbf5c0b1a198b963490985b7738392ebcb97.tar.gz
installed dokuwiki, added to navbar, updated news
Diffstat (limited to 'wiki/inc/Action/Subscribe.php')
-rw-r--r--wiki/inc/Action/Subscribe.php166
1 files changed, 166 insertions, 0 deletions
diff --git a/wiki/inc/Action/Subscribe.php b/wiki/inc/Action/Subscribe.php
new file mode 100644
index 0000000..c165710
--- /dev/null
+++ b/wiki/inc/Action/Subscribe.php
@@ -0,0 +1,166 @@
+<?php
+
+namespace dokuwiki\Action;
+
+use dokuwiki\Action\Exception\ActionAbort;
+use dokuwiki\Action\Exception\ActionDisabledException;
+
+/**
+ * Class Subscribe
+ *
+ * E-Mail subscription handling
+ *
+ * @package dokuwiki\Action
+ */
+class Subscribe extends AbstractUserAction {
+
+    /** @inheritdoc */
+    public function minimumPermission() {
+        return AUTH_READ;
+    }
+
+    /** @inheritdoc */
+    public function checkPreconditions() {
+        parent::checkPreconditions();
+
+        global $conf;
+        if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
+    }
+
+    /** @inheritdoc */
+    public function preProcess() {
+        try {
+            $this->handleSubscribeData();
+        } catch(ActionAbort $e) {
+            throw $e;
+        } catch(\Exception $e) {
+            msg($e->getMessage(), -1);
+        }
+    }
+
+    /** @inheritdoc */
+    public function tplContent() {
+        tpl_subscribe();
+    }
+
+    /**
+     * Handle page 'subscribe'
+     *
+     * @author Adrian Lang <lang@cosmocode.de>
+     * @throws \Exception if (un)subscribing fails
+     * @throws ActionAbort when (un)subscribing worked
+     */
+    protected function handleSubscribeData() {
+        global $lang;
+        global $INFO;
+        global $INPUT;
+
+        // get and preprocess data.
+        $params = array();
+        foreach(array('target', 'style', 'action') as $param) {
+            if($INPUT->has("sub_$param")) {
+                $params[$param] = $INPUT->str("sub_$param");
+            }
+        }
+
+        // any action given? if not just return and show the subscription page
+        if(empty($params['action']) || !checkSecurityToken()) return;
+
+        // Handle POST data, may throw exception.
+        trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, array($this, 'handlePostData'));
+
+        $target = $params['target'];
+        $style = $params['style'];
+        $action = $params['action'];
+
+        // Perform action.
+        $sub = new \Subscription();
+        if($action == 'unsubscribe') {
+            $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
+        } else {
+            $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
+        }
+
+        if($ok) {
+            msg(
+                sprintf(
+                    $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
+                    prettyprint_id($target)
+                ), 1
+            );
+            throw new ActionAbort('redirect');
+        } else {
+            throw new \Exception(
+                sprintf(
+                    $lang["subscr_{$action}_error"],
+                    hsc($INFO['userinfo']['name']),
+                    prettyprint_id($target)
+                )
+            );
+        }
+    }
+
+    /**
+     * Validate POST data
+     *
+     * Validates POST data for a subscribe or unsubscribe request. This is the
+     * default action for the event ACTION_HANDLE_SUBSCRIBE.
+     *
+     * @author Adrian Lang <lang@cosmocode.de>
+     *
+     * @param array &$params the parameters: target, style and action
+     * @throws \Exception
+     */
+    public function handlePostData(&$params) {
+        global $INFO;
+        global $lang;
+        global $INPUT;
+
+        // Get and validate parameters.
+        if(!isset($params['target'])) {
+            throw new \Exception('no subscription target given');
+        }
+        $target = $params['target'];
+        $valid_styles = array('every', 'digest');
+        if(substr($target, -1, 1) === ':') {
+            // Allow “list” subscribe style since the target is a namespace.
+            $valid_styles[] = 'list';
+        }
+        $style = valid_input_set(
+            'style', $valid_styles, $params,
+            'invalid subscription style given'
+        );
+        $action = valid_input_set(
+            'action', array('subscribe', 'unsubscribe'),
+            $params, 'invalid subscription action given'
+        );
+
+        // Check other conditions.
+        if($action === 'subscribe') {
+            if($INFO['userinfo']['mail'] === '') {
+                throw new \Exception($lang['subscr_subscribe_noaddress']);
+            }
+        } elseif($action === 'unsubscribe') {
+            $is = false;
+            foreach($INFO['subscribed'] as $subscr) {
+                if($subscr['target'] === $target) {
+                    $is = true;
+                }
+            }
+            if($is === false) {
+                throw new \Exception(
+                    sprintf(
+                        $lang['subscr_not_subscribed'],
+                        $INPUT->server->str('REMOTE_USER'),
+                        prettyprint_id($target)
+                    )
+                );
+            }
+            // subscription_set deletes a subscription if style = null.
+            $style = null;
+        }
+
+        $params = compact('target', 'style', 'action');
+    }
+
+}