Subscribe.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 = array();
  60. foreach (array('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, array($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"], hsc($INFO['userinfo']['name']),
  83. prettyprint_id($target)
  84. ), 1
  85. );
  86. throw new ActionAbort('redirect');
  87. }
  88. throw new Exception(
  89. sprintf(
  90. $lang["subscr_{$action}_error"],
  91. hsc($INFO['userinfo']['name']),
  92. prettyprint_id($target)
  93. )
  94. );
  95. }
  96. /**
  97. * Validate POST data
  98. *
  99. * Validates POST data for a subscribe or unsubscribe request. This is the
  100. * default action for the event ACTION_HANDLE_SUBSCRIBE.
  101. *
  102. * @author Adrian Lang <lang@cosmocode.de>
  103. *
  104. * @param array &$params the parameters: target, style and action
  105. * @throws Exception
  106. */
  107. public function handlePostData(&$params)
  108. {
  109. global $INFO;
  110. global $lang;
  111. global $INPUT;
  112. // Get and validate parameters.
  113. if (!isset($params['target'])) {
  114. throw new Exception('no subscription target given');
  115. }
  116. $target = $params['target'];
  117. $valid_styles = array('every', 'digest');
  118. if (substr($target, -1, 1) === ':') {
  119. // Allow “list” subscribe style since the target is a namespace.
  120. $valid_styles[] = 'list';
  121. }
  122. $style = valid_input_set(
  123. 'style', $valid_styles, $params,
  124. 'invalid subscription style given'
  125. );
  126. $action = valid_input_set(
  127. 'action', array('subscribe', 'unsubscribe'),
  128. $params, 'invalid subscription action given'
  129. );
  130. // Check other conditions.
  131. if ($action === 'subscribe') {
  132. if ($INFO['userinfo']['mail'] === '') {
  133. throw new Exception($lang['subscr_subscribe_noaddress']);
  134. }
  135. } elseif ($action === 'unsubscribe') {
  136. $is = false;
  137. foreach ($INFO['subscribed'] as $subscr) {
  138. if ($subscr['target'] === $target) {
  139. $is = true;
  140. }
  141. }
  142. if ($is === false) {
  143. throw new Exception(
  144. sprintf(
  145. $lang['subscr_not_subscribed'],
  146. $INPUT->server->str('REMOTE_USER'),
  147. prettyprint_id($target)
  148. )
  149. );
  150. }
  151. // subscription_set deletes a subscription if style = null.
  152. $style = null;
  153. }
  154. $params = compact('target', 'style', 'action');
  155. }
  156. }