You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

182 lines
5.0 KiB

  1. <?php
  2. namespace dokuwiki\Action;
  3. use dokuwiki\Action\Exception\ActionAbort;
  4. use dokuwiki\Action\Exception\ActionDisabledException;
  5. use dokuwiki\Subscriptions\SubscriberManager;
  6. use dokuwiki\Extension\Event;
  7. use dokuwiki\Ui;
  8. use Exception;
  9. /**
  10. * Class Subscribe
  11. *
  12. * E-Mail subscription handling
  13. *
  14. * @package dokuwiki\Action
  15. */
  16. class Subscribe extends AbstractUserAction
  17. {
  18. /** @inheritdoc */
  19. public function minimumPermission()
  20. {
  21. return AUTH_READ;
  22. }
  23. /** @inheritdoc */
  24. public function checkPreconditions()
  25. {
  26. parent::checkPreconditions();
  27. global $conf;
  28. if (isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException();
  29. }
  30. /** @inheritdoc */
  31. public function preProcess()
  32. {
  33. try {
  34. $this->handleSubscribeData();
  35. } catch (ActionAbort $e) {
  36. throw $e;
  37. } catch (Exception $e) {
  38. msg($e->getMessage(), -1);
  39. }
  40. }
  41. /** @inheritdoc */
  42. public function tplContent()
  43. {
  44. (new Ui\Subscribe())->show();
  45. }
  46. /**
  47. * Handle page 'subscribe'
  48. *
  49. * @author Adrian Lang <lang@cosmocode.de>
  50. * @throws Exception if (un)subscribing fails
  51. * @throws ActionAbort when (un)subscribing worked
  52. */
  53. protected function handleSubscribeData()
  54. {
  55. global $lang;
  56. global $INFO;
  57. global $INPUT;
  58. // get and preprocess data.
  59. $params = [];
  60. foreach (['target', 'style', 'action'] as $param) {
  61. if ($INPUT->has("sub_$param")) {
  62. $params[$param] = $INPUT->str("sub_$param");
  63. }
  64. }
  65. // any action given? if not just return and show the subscription page
  66. if (empty($params['action']) || !checkSecurityToken()) return;
  67. // Handle POST data, may throw exception.
  68. Event::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, [$this, 'handlePostData']);
  69. $target = $params['target'];
  70. $style = $params['style'];
  71. $action = $params['action'];
  72. // Perform action.
  73. $subManager = new SubscriberManager();
  74. if ($action === 'unsubscribe') {
  75. $ok = $subManager->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
  76. } else {
  77. $ok = $subManager->add($target, $INPUT->server->str('REMOTE_USER'), $style);
  78. }
  79. if ($ok) {
  80. msg(
  81. sprintf(
  82. $lang["subscr_{$action}_success"],
  83. hsc($INFO['userinfo']['name']),
  84. prettyprint_id($target)
  85. ),
  86. 1
  87. );
  88. throw new ActionAbort('redirect');
  89. }
  90. throw new Exception(
  91. sprintf(
  92. $lang["subscr_{$action}_error"],
  93. hsc($INFO['userinfo']['name']),
  94. prettyprint_id($target)
  95. )
  96. );
  97. }
  98. /**
  99. * Validate POST data
  100. *
  101. * Validates POST data for a subscribe or unsubscribe request. This is the
  102. * default action for the event ACTION_HANDLE_SUBSCRIBE.
  103. *
  104. * @author Adrian Lang <lang@cosmocode.de>
  105. *
  106. * @param array &$params the parameters: target, style and action
  107. * @throws Exception
  108. */
  109. public function handlePostData(&$params)
  110. {
  111. global $INFO;
  112. global $lang;
  113. global $INPUT;
  114. // Get and validate parameters.
  115. if (!isset($params['target'])) {
  116. throw new Exception('no subscription target given');
  117. }
  118. $target = $params['target'];
  119. $valid_styles = ['every', 'digest'];
  120. if (str_ends_with($target, ':')) {
  121. // Allow “list” subscribe style since the target is a namespace.
  122. $valid_styles[] = 'list';
  123. }
  124. $style = valid_input_set(
  125. 'style',
  126. $valid_styles,
  127. $params,
  128. 'invalid subscription style given'
  129. );
  130. $action = valid_input_set(
  131. 'action',
  132. ['subscribe', 'unsubscribe'],
  133. $params,
  134. 'invalid subscription action given'
  135. );
  136. // Check other conditions.
  137. if ($action === 'subscribe') {
  138. if ($INFO['userinfo']['mail'] === '') {
  139. throw new Exception($lang['subscr_subscribe_noaddress']);
  140. }
  141. } elseif ($action === 'unsubscribe') {
  142. $is = false;
  143. foreach ($INFO['subscribed'] as $subscr) {
  144. if ($subscr['target'] === $target) {
  145. $is = true;
  146. }
  147. }
  148. if ($is === false) {
  149. throw new Exception(
  150. sprintf(
  151. $lang['subscr_not_subscribed'],
  152. $INPUT->server->str('REMOTE_USER'),
  153. prettyprint_id($target)
  154. )
  155. );
  156. }
  157. // subscription_set deletes a subscription if style = null.
  158. $style = null;
  159. }
  160. $params = ['target' => $target, 'style' => $style, 'action' => $action];
  161. }
  162. }