3 namespace dokuwiki\Action
;
5 use dokuwiki\Action\Exception\ActionAbort
;
6 use dokuwiki\Action\Exception\ActionDisabledException
;
7 use dokuwiki\Subscriptions\SubscriberManager
;
8 use dokuwiki\Extension\Event
;
15 * E-Mail subscription handling
17 * @package dokuwiki\Action
19 class Subscribe
extends AbstractUserAction
22 public function minimumPermission()
28 public function checkPreconditions()
30 parent
::checkPreconditions();
33 if (isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
37 public function preProcess()
40 $this->handleSubscribeData();
41 } catch (ActionAbort
$e) {
43 } catch (Exception
$e) {
44 msg($e->getMessage(), -1);
49 public function tplContent()
51 (new Ui\
Subscribe())->show();
55 * Handle page 'subscribe'
57 * @author Adrian Lang <lang@cosmocode.de>
58 * @throws Exception if (un)subscribing fails
59 * @throws ActionAbort when (un)subscribing worked
61 protected function handleSubscribeData()
67 // get and preprocess data.
69 foreach (['target', 'style', 'action'] as $param) {
70 if ($INPUT->has("sub_$param")) {
71 $params[$param] = $INPUT->str("sub_$param");
75 // any action given? if not just return and show the subscription page
76 if (empty($params['action']) ||
!checkSecurityToken()) return;
78 // Handle POST data, may throw exception.
79 Event
::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, [$this, 'handlePostData']);
81 $target = $params['target'];
82 $style = $params['style'];
83 $action = $params['action'];
86 $subManager = new SubscriberManager();
87 if ($action === 'unsubscribe') {
88 $ok = $subManager->remove($target, $INPUT->server
->str('REMOTE_USER'), $style);
90 $ok = $subManager->add($target, $INPUT->server
->str('REMOTE_USER'), $style);
96 $lang["subscr_{$action}_success"],
97 hsc($INFO['userinfo']['name']),
98 prettyprint_id($target)
102 throw new ActionAbort('redirect');
107 $lang["subscr_{$action}_error"],
108 hsc($INFO['userinfo']['name']),
109 prettyprint_id($target)
117 * Validates POST data for a subscribe or unsubscribe request. This is the
118 * default action for the event ACTION_HANDLE_SUBSCRIBE.
120 * @author Adrian Lang <lang@cosmocode.de>
122 * @param array &$params the parameters: target, style and action
125 public function handlePostData(&$params)
131 // Get and validate parameters.
132 if (!isset($params['target'])) {
133 throw new Exception('no subscription target given');
135 $target = $params['target'];
136 $valid_styles = ['every', 'digest'];
137 if (str_ends_with($target, ':')) {
138 // Allow “list” subscribe style since the target is a namespace.
139 $valid_styles[] = 'list';
141 $style = valid_input_set(
145 'invalid subscription style given'
147 $action = valid_input_set(
149 ['subscribe', 'unsubscribe'],
151 'invalid subscription action given'
154 // Check other conditions.
155 if ($action === 'subscribe') {
156 if ($INFO['userinfo']['mail'] === '') {
157 throw new Exception($lang['subscr_subscribe_noaddress']);
159 } elseif ($action === 'unsubscribe') {
161 foreach ($INFO['subscribed'] as $subscr) {
162 if ($subscr['target'] === $target) {
169 $lang['subscr_not_subscribed'],
170 $INPUT->server
->str('REMOTE_USER'),
171 prettyprint_id($target)
175 // subscription_set deletes a subscription if style = null.
179 $params = ['target' => $target, 'style' => $style, 'action' => $action];