1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<?php
namespace dokuwiki\Action;
use dokuwiki\Action\Exception\ActionAbort;
use dokuwiki\Action\Exception\ActionDisabledException;
use dokuwiki\Subscriptions\SubscriberManager;
use dokuwiki\Extension\Event;
use dokuwiki\Ui;
use Exception;
/**
* 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()
{
(new Ui\Subscribe)->show();
}
/**
* 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.
Event::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, array($this, 'handlePostData'));
$target = $params['target'];
$style = $params['style'];
$action = $params['action'];
// Perform action.
$subManager = new SubscriberManager();
if ($action === 'unsubscribe') {
$ok = $subManager->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
} else {
$ok = $subManager->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');
}
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');
}
}
|