More adjusted API tests
[dokuwiki.git] / inc / Action / Subscribe.php
blob640bb42d66e005d2b2fcf32b595efae3c84e0784
1 <?php
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;
9 use dokuwiki\Ui;
10 use Exception;
12 /**
13 * Class Subscribe
15 * E-Mail subscription handling
17 * @package dokuwiki\Action
19 class Subscribe extends AbstractUserAction
21 /** @inheritdoc */
22 public function minimumPermission()
24 return AUTH_READ;
27 /** @inheritdoc */
28 public function checkPreconditions()
30 parent::checkPreconditions();
32 global $conf;
33 if (isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
36 /** @inheritdoc */
37 public function preProcess()
39 try {
40 $this->handleSubscribeData();
41 } catch (ActionAbort $e) {
42 throw $e;
43 } catch (Exception $e) {
44 msg($e->getMessage(), -1);
48 /** @inheritdoc */
49 public function tplContent()
51 (new Ui\Subscribe())->show();
54 /**
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()
63 global $lang;
64 global $INFO;
65 global $INPUT;
67 // get and preprocess data.
68 $params = [];
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'];
85 // Perform action.
86 $subManager = new SubscriberManager();
87 if ($action === 'unsubscribe') {
88 $ok = $subManager->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
89 } else {
90 $ok = $subManager->add($target, $INPUT->server->str('REMOTE_USER'), $style);
93 if ($ok) {
94 msg(
95 sprintf(
96 $lang["subscr_{$action}_success"],
97 hsc($INFO['userinfo']['name']),
98 prettyprint_id($target)
102 throw new ActionAbort('redirect');
105 throw new Exception(
106 sprintf(
107 $lang["subscr_{$action}_error"],
108 hsc($INFO['userinfo']['name']),
109 prettyprint_id($target)
115 * Validate POST data
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
123 * @throws Exception
125 public function handlePostData(&$params)
127 global $INFO;
128 global $lang;
129 global $INPUT;
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(
142 'style',
143 $valid_styles,
144 $params,
145 'invalid subscription style given'
147 $action = valid_input_set(
148 'action',
149 ['subscribe', 'unsubscribe'],
150 $params,
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') {
160 $is = false;
161 foreach ($INFO['subscribed'] as $subscr) {
162 if ($subscr['target'] === $target) {
163 $is = true;
166 if ($is === false) {
167 throw new Exception(
168 sprintf(
169 $lang['subscr_not_subscribed'],
170 $INPUT->server->str('REMOTE_USER'),
171 prettyprint_id($target)
175 // subscription_set deletes a subscription if style = null.
176 $style = null;
179 $params = ['target' => $target, 'style' => $style, 'action' => $action];