164ab5140SAndreas Gohr<?php 264ab5140SAndreas Gohr 364ab5140SAndreas Gohrnamespace dokuwiki\Action; 464ab5140SAndreas Gohr 5272271fcSAndreas Gohruse dokuwiki\Action\Exception\ActionAbort; 6480336a3SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException; 775d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 8cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 9*848cb786SSatoshi Saharause dokuwiki\Ui; 10480336a3SAndreas Gohr 11ab583a1bSAndreas Gohr/** 12ab583a1bSAndreas Gohr * Class Subscribe 13ab583a1bSAndreas Gohr * 14ab583a1bSAndreas Gohr * E-Mail subscription handling 15ab583a1bSAndreas Gohr * 16ab583a1bSAndreas Gohr * @package dokuwiki\Action 17ab583a1bSAndreas Gohr */ 18*848cb786SSatoshi Saharaclass Subscribe extends AbstractUserAction 19*848cb786SSatoshi Sahara{ 2064ab5140SAndreas Gohr /** @inheritdoc */ 21*848cb786SSatoshi Sahara public function minimumPermission() 22*848cb786SSatoshi Sahara { 2364ab5140SAndreas Gohr return AUTH_READ; 2464ab5140SAndreas Gohr } 2564ab5140SAndreas Gohr 2664ab5140SAndreas Gohr /** @inheritdoc */ 27*848cb786SSatoshi Sahara public function checkPreconditions() 28*848cb786SSatoshi Sahara { 29b2c9cd19SAndreas Gohr parent::checkPreconditions(); 30480336a3SAndreas Gohr 31480336a3SAndreas Gohr global $conf; 32480336a3SAndreas Gohr if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException(); 33480336a3SAndreas Gohr } 34480336a3SAndreas Gohr 35480336a3SAndreas Gohr /** @inheritdoc */ 36*848cb786SSatoshi Sahara public function preProcess() 37*848cb786SSatoshi Sahara { 3864ab5140SAndreas Gohr try { 39272271fcSAndreas Gohr $this->handleSubscribeData(); 40272271fcSAndreas Gohr } catch (ActionAbort $e) { 41272271fcSAndreas Gohr throw $e; 4264ab5140SAndreas Gohr } catch (\Exception $e) { 4364ab5140SAndreas Gohr msg($e->getMessage(), -1); 4464ab5140SAndreas Gohr } 4564ab5140SAndreas Gohr } 4664ab5140SAndreas Gohr 4764ab5140SAndreas Gohr /** @inheritdoc */ 48*848cb786SSatoshi Sahara public function tplContent() 49*848cb786SSatoshi Sahara { 50*848cb786SSatoshi Sahara (new Ui\Subscribe)->show(); 5164ab5140SAndreas Gohr } 5264ab5140SAndreas Gohr 5364ab5140SAndreas Gohr /** 5464ab5140SAndreas Gohr * Handle page 'subscribe' 5564ab5140SAndreas Gohr * 5664ab5140SAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 5764ab5140SAndreas Gohr * @throws \Exception if (un)subscribing fails 58272271fcSAndreas Gohr * @throws ActionAbort when (un)subscribing worked 5964ab5140SAndreas Gohr */ 60*848cb786SSatoshi Sahara protected function handleSubscribeData() 61*848cb786SSatoshi Sahara { 6264ab5140SAndreas Gohr global $lang; 6364ab5140SAndreas Gohr global $INFO; 6464ab5140SAndreas Gohr global $INPUT; 6564ab5140SAndreas Gohr 6664ab5140SAndreas Gohr // get and preprocess data. 6764ab5140SAndreas Gohr $params = array(); 6864ab5140SAndreas Gohr foreach (array('target', 'style', 'action') as $param) { 6964ab5140SAndreas Gohr if ($INPUT->has("sub_$param")) { 7064ab5140SAndreas Gohr $params[$param] = $INPUT->str("sub_$param"); 7164ab5140SAndreas Gohr } 7264ab5140SAndreas Gohr } 7364ab5140SAndreas Gohr 7464ab5140SAndreas Gohr // any action given? if not just return and show the subscription page 75272271fcSAndreas Gohr if (empty($params['action']) || !checkSecurityToken()) return; 7664ab5140SAndreas Gohr 7764ab5140SAndreas Gohr // Handle POST data, may throw exception. 78cbb44eabSAndreas Gohr Event::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, array($this, 'handlePostData')); 7964ab5140SAndreas Gohr 8064ab5140SAndreas Gohr $target = $params['target']; 8164ab5140SAndreas Gohr $style = $params['style']; 8264ab5140SAndreas Gohr $action = $params['action']; 8364ab5140SAndreas Gohr 8464ab5140SAndreas Gohr // Perform action. 8575d66495SMichael Große $subManager = new SubscriberManager(); 86820934dcSAndreas Gohr if ($action === 'unsubscribe') { 8775d66495SMichael Große $ok = $subManager->remove($target, $INPUT->server->str('REMOTE_USER'), $style); 8864ab5140SAndreas Gohr } else { 8975d66495SMichael Große $ok = $subManager->add($target, $INPUT->server->str('REMOTE_USER'), $style); 9064ab5140SAndreas Gohr } 9164ab5140SAndreas Gohr 9264ab5140SAndreas Gohr if ($ok) { 9364ab5140SAndreas Gohr msg( 9464ab5140SAndreas Gohr sprintf( 9564ab5140SAndreas Gohr $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']), 9664ab5140SAndreas Gohr prettyprint_id($target) 9764ab5140SAndreas Gohr ), 1 9864ab5140SAndreas Gohr ); 99272271fcSAndreas Gohr throw new ActionAbort('redirect'); 100820934dcSAndreas Gohr } 101820934dcSAndreas Gohr 10264ab5140SAndreas Gohr throw new \Exception( 10364ab5140SAndreas Gohr sprintf( 10464ab5140SAndreas Gohr $lang["subscr_{$action}_error"], 10564ab5140SAndreas Gohr hsc($INFO['userinfo']['name']), 10664ab5140SAndreas Gohr prettyprint_id($target) 10764ab5140SAndreas Gohr ) 10864ab5140SAndreas Gohr ); 10964ab5140SAndreas Gohr } 11064ab5140SAndreas Gohr 111272271fcSAndreas Gohr /** 112272271fcSAndreas Gohr * Validate POST data 113272271fcSAndreas Gohr * 114272271fcSAndreas Gohr * Validates POST data for a subscribe or unsubscribe request. This is the 115272271fcSAndreas Gohr * default action for the event ACTION_HANDLE_SUBSCRIBE. 116272271fcSAndreas Gohr * 117272271fcSAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 118272271fcSAndreas Gohr * 119272271fcSAndreas Gohr * @param array &$params the parameters: target, style and action 120272271fcSAndreas Gohr * @throws \Exception 121272271fcSAndreas Gohr */ 122*848cb786SSatoshi Sahara public function handlePostData(&$params) 123*848cb786SSatoshi Sahara { 124272271fcSAndreas Gohr global $INFO; 125272271fcSAndreas Gohr global $lang; 126272271fcSAndreas Gohr global $INPUT; 127272271fcSAndreas Gohr 128272271fcSAndreas Gohr // Get and validate parameters. 129272271fcSAndreas Gohr if (!isset($params['target'])) { 130272271fcSAndreas Gohr throw new \Exception('no subscription target given'); 131272271fcSAndreas Gohr } 132272271fcSAndreas Gohr $target = $params['target']; 133272271fcSAndreas Gohr $valid_styles = array('every', 'digest'); 134272271fcSAndreas Gohr if (substr($target, -1, 1) === ':') { 135272271fcSAndreas Gohr // Allow “list” subscribe style since the target is a namespace. 136272271fcSAndreas Gohr $valid_styles[] = 'list'; 137272271fcSAndreas Gohr } 138272271fcSAndreas Gohr $style = valid_input_set( 139272271fcSAndreas Gohr 'style', $valid_styles, $params, 140272271fcSAndreas Gohr 'invalid subscription style given' 141272271fcSAndreas Gohr ); 142272271fcSAndreas Gohr $action = valid_input_set( 143272271fcSAndreas Gohr 'action', array('subscribe', 'unsubscribe'), 144272271fcSAndreas Gohr $params, 'invalid subscription action given' 145272271fcSAndreas Gohr ); 146272271fcSAndreas Gohr 147272271fcSAndreas Gohr // Check other conditions. 148272271fcSAndreas Gohr if ($action === 'subscribe') { 149272271fcSAndreas Gohr if ($INFO['userinfo']['mail'] === '') { 150272271fcSAndreas Gohr throw new \Exception($lang['subscr_subscribe_noaddress']); 151272271fcSAndreas Gohr } 152272271fcSAndreas Gohr } elseif ($action === 'unsubscribe') { 153272271fcSAndreas Gohr $is = false; 154272271fcSAndreas Gohr foreach ($INFO['subscribed'] as $subscr) { 155272271fcSAndreas Gohr if ($subscr['target'] === $target) { 156272271fcSAndreas Gohr $is = true; 157272271fcSAndreas Gohr } 158272271fcSAndreas Gohr } 159272271fcSAndreas Gohr if ($is === false) { 160272271fcSAndreas Gohr throw new \Exception( 161272271fcSAndreas Gohr sprintf( 162272271fcSAndreas Gohr $lang['subscr_not_subscribed'], 163272271fcSAndreas Gohr $INPUT->server->str('REMOTE_USER'), 164272271fcSAndreas Gohr prettyprint_id($target) 165272271fcSAndreas Gohr ) 166272271fcSAndreas Gohr ); 167272271fcSAndreas Gohr } 168272271fcSAndreas Gohr // subscription_set deletes a subscription if style = null. 169272271fcSAndreas Gohr $style = null; 170272271fcSAndreas Gohr } 171272271fcSAndreas Gohr 172272271fcSAndreas Gohr $params = compact('target', 'style', 'action'); 17364ab5140SAndreas Gohr } 17464ab5140SAndreas Gohr 17564ab5140SAndreas Gohr} 176