164ab5140SAndreas Gohr<?php 264ab5140SAndreas Gohr 364ab5140SAndreas Gohrnamespace dokuwiki\Action; 464ab5140SAndreas Gohr 5272271fcSAndreas Gohruse dokuwiki\Action\Exception\ActionAbort; 6480336a3SAndreas Gohruse dokuwiki\Action\Exception\ActionDisabledException; 7*cbb44eabSAndreas Gohruse dokuwiki\Extension\Event; 8480336a3SAndreas Gohr 9ab583a1bSAndreas Gohr/** 10ab583a1bSAndreas Gohr * Class Subscribe 11ab583a1bSAndreas Gohr * 12ab583a1bSAndreas Gohr * E-Mail subscription handling 13ab583a1bSAndreas Gohr * 14ab583a1bSAndreas Gohr * @package dokuwiki\Action 15ab583a1bSAndreas Gohr */ 1664ab5140SAndreas Gohrclass Subscribe extends AbstractUserAction { 1764ab5140SAndreas Gohr 1864ab5140SAndreas Gohr /** @inheritdoc */ 19ec701221SAndreas Gohr public function minimumPermission() { 2064ab5140SAndreas Gohr return AUTH_READ; 2164ab5140SAndreas Gohr } 2264ab5140SAndreas Gohr 2364ab5140SAndreas Gohr /** @inheritdoc */ 24b2c9cd19SAndreas Gohr public function checkPreconditions() { 25b2c9cd19SAndreas Gohr parent::checkPreconditions(); 26480336a3SAndreas Gohr 27480336a3SAndreas Gohr global $conf; 28480336a3SAndreas Gohr if(isset($conf['subscribers']) && !$conf['subscribers']) throw new ActionDisabledException(); 29480336a3SAndreas Gohr } 30480336a3SAndreas Gohr 31480336a3SAndreas Gohr /** @inheritdoc */ 3264ab5140SAndreas Gohr public function preProcess() { 3364ab5140SAndreas Gohr try { 34272271fcSAndreas Gohr $this->handleSubscribeData(); 35272271fcSAndreas Gohr } catch(ActionAbort $e) { 36272271fcSAndreas Gohr throw $e; 3764ab5140SAndreas Gohr } catch(\Exception $e) { 3864ab5140SAndreas Gohr msg($e->getMessage(), -1); 3964ab5140SAndreas Gohr } 4064ab5140SAndreas Gohr } 4164ab5140SAndreas Gohr 4264ab5140SAndreas Gohr /** @inheritdoc */ 4364ab5140SAndreas Gohr public function tplContent() { 4464ab5140SAndreas Gohr tpl_subscribe(); 4564ab5140SAndreas Gohr } 4664ab5140SAndreas Gohr 4764ab5140SAndreas Gohr /** 4864ab5140SAndreas Gohr * Handle page 'subscribe' 4964ab5140SAndreas Gohr * 5064ab5140SAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 5164ab5140SAndreas Gohr * @throws \Exception if (un)subscribing fails 52272271fcSAndreas Gohr * @throws ActionAbort when (un)subscribing worked 5364ab5140SAndreas Gohr */ 5464ab5140SAndreas Gohr protected function handleSubscribeData() { 5564ab5140SAndreas Gohr global $lang; 5664ab5140SAndreas Gohr global $INFO; 5764ab5140SAndreas Gohr global $INPUT; 5864ab5140SAndreas Gohr 5964ab5140SAndreas Gohr // get and preprocess data. 6064ab5140SAndreas Gohr $params = array(); 6164ab5140SAndreas Gohr foreach(array('target', 'style', 'action') as $param) { 6264ab5140SAndreas Gohr if($INPUT->has("sub_$param")) { 6364ab5140SAndreas Gohr $params[$param] = $INPUT->str("sub_$param"); 6464ab5140SAndreas Gohr } 6564ab5140SAndreas Gohr } 6664ab5140SAndreas Gohr 6764ab5140SAndreas Gohr // any action given? if not just return and show the subscription page 68272271fcSAndreas Gohr if(empty($params['action']) || !checkSecurityToken()) return; 6964ab5140SAndreas Gohr 7064ab5140SAndreas Gohr // Handle POST data, may throw exception. 71*cbb44eabSAndreas Gohr Event::createAndTrigger('ACTION_HANDLE_SUBSCRIBE', $params, array($this, 'handlePostData')); 7264ab5140SAndreas Gohr 7364ab5140SAndreas Gohr $target = $params['target']; 7464ab5140SAndreas Gohr $style = $params['style']; 7564ab5140SAndreas Gohr $action = $params['action']; 7664ab5140SAndreas Gohr 7764ab5140SAndreas Gohr // Perform action. 7864ab5140SAndreas Gohr $sub = new \Subscription(); 7964ab5140SAndreas Gohr if($action == 'unsubscribe') { 8064ab5140SAndreas Gohr $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style); 8164ab5140SAndreas Gohr } else { 8264ab5140SAndreas Gohr $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style); 8364ab5140SAndreas Gohr } 8464ab5140SAndreas Gohr 8564ab5140SAndreas Gohr if($ok) { 8664ab5140SAndreas Gohr msg( 8764ab5140SAndreas Gohr sprintf( 8864ab5140SAndreas Gohr $lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']), 8964ab5140SAndreas Gohr prettyprint_id($target) 9064ab5140SAndreas Gohr ), 1 9164ab5140SAndreas Gohr ); 92272271fcSAndreas Gohr throw new ActionAbort('redirect'); 9364ab5140SAndreas Gohr } else { 9464ab5140SAndreas Gohr throw new \Exception( 9564ab5140SAndreas Gohr sprintf( 9664ab5140SAndreas Gohr $lang["subscr_{$action}_error"], 9764ab5140SAndreas Gohr hsc($INFO['userinfo']['name']), 9864ab5140SAndreas Gohr prettyprint_id($target) 9964ab5140SAndreas Gohr ) 10064ab5140SAndreas Gohr ); 10164ab5140SAndreas Gohr } 102272271fcSAndreas Gohr } 10364ab5140SAndreas Gohr 104272271fcSAndreas Gohr /** 105272271fcSAndreas Gohr * Validate POST data 106272271fcSAndreas Gohr * 107272271fcSAndreas Gohr * Validates POST data for a subscribe or unsubscribe request. This is the 108272271fcSAndreas Gohr * default action for the event ACTION_HANDLE_SUBSCRIBE. 109272271fcSAndreas Gohr * 110272271fcSAndreas Gohr * @author Adrian Lang <lang@cosmocode.de> 111272271fcSAndreas Gohr * 112272271fcSAndreas Gohr * @param array &$params the parameters: target, style and action 113272271fcSAndreas Gohr * @throws \Exception 114272271fcSAndreas Gohr */ 115272271fcSAndreas Gohr public function handlePostData(&$params) { 116272271fcSAndreas Gohr global $INFO; 117272271fcSAndreas Gohr global $lang; 118272271fcSAndreas Gohr global $INPUT; 119272271fcSAndreas Gohr 120272271fcSAndreas Gohr // Get and validate parameters. 121272271fcSAndreas Gohr if(!isset($params['target'])) { 122272271fcSAndreas Gohr throw new \Exception('no subscription target given'); 123272271fcSAndreas Gohr } 124272271fcSAndreas Gohr $target = $params['target']; 125272271fcSAndreas Gohr $valid_styles = array('every', 'digest'); 126272271fcSAndreas Gohr if(substr($target, -1, 1) === ':') { 127272271fcSAndreas Gohr // Allow “list” subscribe style since the target is a namespace. 128272271fcSAndreas Gohr $valid_styles[] = 'list'; 129272271fcSAndreas Gohr } 130272271fcSAndreas Gohr $style = valid_input_set( 131272271fcSAndreas Gohr 'style', $valid_styles, $params, 132272271fcSAndreas Gohr 'invalid subscription style given' 133272271fcSAndreas Gohr ); 134272271fcSAndreas Gohr $action = valid_input_set( 135272271fcSAndreas Gohr 'action', array('subscribe', 'unsubscribe'), 136272271fcSAndreas Gohr $params, 'invalid subscription action given' 137272271fcSAndreas Gohr ); 138272271fcSAndreas Gohr 139272271fcSAndreas Gohr // Check other conditions. 140272271fcSAndreas Gohr if($action === 'subscribe') { 141272271fcSAndreas Gohr if($INFO['userinfo']['mail'] === '') { 142272271fcSAndreas Gohr throw new \Exception($lang['subscr_subscribe_noaddress']); 143272271fcSAndreas Gohr } 144272271fcSAndreas Gohr } elseif($action === 'unsubscribe') { 145272271fcSAndreas Gohr $is = false; 146272271fcSAndreas Gohr foreach($INFO['subscribed'] as $subscr) { 147272271fcSAndreas Gohr if($subscr['target'] === $target) { 148272271fcSAndreas Gohr $is = true; 149272271fcSAndreas Gohr } 150272271fcSAndreas Gohr } 151272271fcSAndreas Gohr if($is === false) { 152272271fcSAndreas Gohr throw new \Exception( 153272271fcSAndreas Gohr sprintf( 154272271fcSAndreas Gohr $lang['subscr_not_subscribed'], 155272271fcSAndreas Gohr $INPUT->server->str('REMOTE_USER'), 156272271fcSAndreas Gohr prettyprint_id($target) 157272271fcSAndreas Gohr ) 158272271fcSAndreas Gohr ); 159272271fcSAndreas Gohr } 160272271fcSAndreas Gohr // subscription_set deletes a subscription if style = null. 161272271fcSAndreas Gohr $style = null; 162272271fcSAndreas Gohr } 163272271fcSAndreas Gohr 164272271fcSAndreas Gohr $params = compact('target', 'style', 'action'); 16564ab5140SAndreas Gohr } 16664ab5140SAndreas Gohr 16764ab5140SAndreas Gohr} 168