xref: /dokuwiki/inc/actions.php (revision e5802cb76bfc6a2db174f805c78e339cc79a226a)
16b13307fSandi<?php
26b13307fSandi/**
36b13307fSandi * DokuWiki Actions
46b13307fSandi *
56b13307fSandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
66b13307fSandi * @author     Andreas Gohr <andi@splitbrain.org>
76b13307fSandi */
86b13307fSandi
9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.');
10af182434Sandi
11*e5802cb7SAndreas Gohr
12*e5802cb7SAndreas Gohrfunction act_dispatch(){
13*e5802cb7SAndreas Gohr    $router = \dokuwiki\ActionRouter::getInstance(); // is this needed here or could we delegate it to tpl_content() later?
14*e5802cb7SAndreas Gohr
15*e5802cb7SAndreas Gohr
16*e5802cb7SAndreas Gohr
17*e5802cb7SAndreas Gohr    //call template FIXME: all needed vars available?
18*e5802cb7SAndreas Gohr    $headers[] = 'Content-Type: text/html; charset=utf-8';
19*e5802cb7SAndreas Gohr    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
20*e5802cb7SAndreas Gohr
21*e5802cb7SAndreas Gohr    // clear internal variables
22*e5802cb7SAndreas Gohr    unset($router);
23*e5802cb7SAndreas Gohr    unset($headers);
24*e5802cb7SAndreas Gohr    // make all globals available to the template
25*e5802cb7SAndreas Gohr    extract($GLOBALS);
26*e5802cb7SAndreas Gohr
27*e5802cb7SAndreas Gohr    include(template('main.php'));
28*e5802cb7SAndreas Gohr    // output for the commands is now handled in inc/templates.php
29*e5802cb7SAndreas Gohr    // in function tpl_content()
30*e5802cb7SAndreas Gohr}
31*e5802cb7SAndreas Gohr
326b13307fSandi/**
336b13307fSandi * Call the needed action handlers
346b13307fSandi *
356b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
36c9570649SAndreas Gohr * @triggers ACTION_ACT_PREPROCESS
37c9570649SAndreas Gohr * @triggers ACTION_HEADERS_SEND
386b13307fSandi */
39*e5802cb7SAndreas Gohrfunction XXX_act_dispatch(){
406b13307fSandi    global $ACT;
416b13307fSandi    global $ID;
4224ea6500SAndreas Gohr    global $INFO;
436b13307fSandi    global $QUERY;
44585bf44eSChristopher Smith    /* @var Input $INPUT */
4590f1b7bdSTom N Harris    global $INPUT;
466b13307fSandi    global $lang;
4785dcda20SRobin Getz    global $conf;
486b13307fSandi
4969cd1e27SAndreas Gohr    $preact = $ACT;
5069cd1e27SAndreas Gohr
51c2e830f2Schris    // give plugins an opportunity to process the action
5224bb549bSchris    $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT);
53844aec66SMichael Große
54844aec66SMichael Große    $headers = array();
5524bb549bSchris    if ($evt->advise_before()) {
56c2e830f2Schris
57af182434Sandi        //sanitize $ACT
5862baad0fSMartin Doucha        $ACT = act_validate($ACT);
59af182434Sandi
60b8957367SBenjamin Gilbert        //check if searchword was given - else just show
610868021bSAndreas Gohr        $s = cleanID($QUERY);
620868021bSAndreas Gohr        if($ACT == 'search' && empty($s)){
63b8957367SBenjamin Gilbert            $ACT = 'show';
64b8957367SBenjamin Gilbert        }
65b8957367SBenjamin Gilbert
66b8957367SBenjamin Gilbert        //login stuff
671b2a85e8SAndreas Gohr        if(in_array($ACT,array('login','logout'))){
68b8957367SBenjamin Gilbert            $ACT = act_auth($ACT);
691b2a85e8SAndreas Gohr        }
70b8957367SBenjamin Gilbert
711380fc45SAndreas Gohr        //check if user is asking to (un)subscribe a page
725b75cd1fSAdrian Lang        if($ACT == 'subscribe') {
735b75cd1fSAdrian Lang            try {
741380fc45SAndreas Gohr                $ACT = act_subscription($ACT);
755b75cd1fSAdrian Lang            } catch (Exception $e) {
765b75cd1fSAdrian Lang                msg($e->getMessage(), -1);
775b75cd1fSAdrian Lang            }
785b75cd1fSAdrian Lang        }
7952b0dd67SGuy Brand
805381a7eeSElan Ruusamäe        //display some info
814064e2d3SRobin Getz        if($ACT == 'check'){
824064e2d3SRobin Getz            check();
834064e2d3SRobin Getz            $ACT = 'show';
844064e2d3SRobin Getz        }
854064e2d3SRobin Getz
866b13307fSandi        //check permissions
876b13307fSandi        $ACT = act_permcheck($ACT);
886b13307fSandi
89c4f79b71SMichael Hamann        //sitemap
90eae17177SMichael Hamann        if ($ACT == 'sitemap'){
91c8b076b1SMichael Hamann            act_sitemap($ACT);
92eae17177SMichael Hamann        }
93c4f79b71SMichael Hamann
943c94d07bSAnika Henke        //recent changes
953c94d07bSAnika Henke        if ($ACT == 'recent'){
963c94d07bSAnika Henke            $show_changes = $INPUT->str('show_changes');
973c94d07bSAnika Henke            if (!empty($show_changes)) {
983c94d07bSAnika Henke                set_doku_pref('show_changes', $show_changes);
993c94d07bSAnika Henke            }
1003c94d07bSAnika Henke        }
1013c94d07bSAnika Henke
1023c94d07bSAnika Henke        //diff
1033c94d07bSAnika Henke        if ($ACT == 'diff'){
1043c94d07bSAnika Henke            $difftype = $INPUT->str('difftype');
1053c94d07bSAnika Henke            if (!empty($difftype)) {
1063c94d07bSAnika Henke                set_doku_pref('difftype', $difftype);
1073c94d07bSAnika Henke            }
1083c94d07bSAnika Henke        }
1093c94d07bSAnika Henke
110b8957367SBenjamin Gilbert        //register
111eea0f0d0SAndreas Gohr        if($ACT == 'register' && $INPUT->post->bool('save') && register()){
112b8957367SBenjamin Gilbert            $ACT = 'login';
113b8957367SBenjamin Gilbert        }
1146b13307fSandi
1158b06d178Schris        if ($ACT == 'resendpwd' && act_resendpwd()) {
1168b06d178Schris            $ACT = 'login';
1178b06d178Schris        }
1188b06d178Schris
1192a7abf2dSChristopher Smith        // user profile changes
1202a7abf2dSChristopher Smith        if (in_array($ACT, array('profile','profile_delete'))) {
121585bf44eSChristopher Smith            if(!$INPUT->server->str('REMOTE_USER')) {
12225b2a98cSMichael Klier                $ACT = 'login';
12325b2a98cSMichael Klier            } else {
1242a7abf2dSChristopher Smith                switch ($ACT) {
1252a7abf2dSChristopher Smith                    case 'profile' :
12625b2a98cSMichael Klier                        if(updateprofile()) {
1274cb79657SMatthias Grimm                            msg($lang['profchanged'],1);
1284cb79657SMatthias Grimm                            $ACT = 'show';
1298b06d178Schris                        }
1302a7abf2dSChristopher Smith                        break;
1312a7abf2dSChristopher Smith                    case 'profile_delete' :
1322a7abf2dSChristopher Smith                        if(auth_deleteprofile()){
1332a7abf2dSChristopher Smith                            msg($lang['profdeleted'],1);
1342a7abf2dSChristopher Smith                            $ACT = 'show';
1352a7abf2dSChristopher Smith                        } else {
1362a7abf2dSChristopher Smith                            $ACT = 'profile';
1372a7abf2dSChristopher Smith                        }
1382a7abf2dSChristopher Smith                        break;
1392a7abf2dSChristopher Smith                }
14025b2a98cSMichael Klier            }
14125b2a98cSMichael Klier        }
1428b06d178Schris
1431246e016SAndreas Gohr        //revert
1441246e016SAndreas Gohr        if($ACT == 'revert'){
1451246e016SAndreas Gohr            if(checkSecurityToken()){
1461246e016SAndreas Gohr                $ACT = act_revert($ACT);
1471246e016SAndreas Gohr            }else{
1481246e016SAndreas Gohr                $ACT = 'show';
1491246e016SAndreas Gohr            }
1501246e016SAndreas Gohr        }
1511246e016SAndreas Gohr
1526b13307fSandi        //save
1531b2a85e8SAndreas Gohr        if($ACT == 'save'){
1541b2a85e8SAndreas Gohr            if(checkSecurityToken()){
1556b13307fSandi                $ACT = act_save($ACT);
1561b2a85e8SAndreas Gohr            }else{
1578071beaaSAndreas Gohr                $ACT = 'preview';
1581b2a85e8SAndreas Gohr            }
1591b2a85e8SAndreas Gohr        }
1606b13307fSandi
161067c5d22SBen Coburn        //cancel conflicting edit
162067c5d22SBen Coburn        if($ACT == 'cancel')
163067c5d22SBen Coburn            $ACT = 'show';
164067c5d22SBen Coburn
165ee4c4a1bSAndreas Gohr        //draft deletion
166ee4c4a1bSAndreas Gohr        if($ACT == 'draftdel')
167ee4c4a1bSAndreas Gohr            $ACT = act_draftdel($ACT);
168ee4c4a1bSAndreas Gohr
169ee4c4a1bSAndreas Gohr        //draft saving on preview
170844aec66SMichael Große        if($ACT == 'preview') {
171844aec66SMichael Große            $headers[] = "X-XSS-Protection: 0";
172ee4c4a1bSAndreas Gohr            $ACT = act_draftsave($ACT);
173844aec66SMichael Große        }
174ee4c4a1bSAndreas Gohr
1756b13307fSandi        //edit
176c9d5430bSAdrian Lang        if(in_array($ACT, array('edit', 'preview', 'recover'))) {
177af182434Sandi            $ACT = act_edit($ACT);
1786b13307fSandi        }else{
1796b13307fSandi            unlock($ID); //try to unlock
1806b13307fSandi        }
1816b13307fSandi
1826b13307fSandi        //handle export
183ac83b9d8Sandi        if(substr($ACT,0,7) == 'export_')
1846b13307fSandi            $ACT = act_export($ACT);
1856b13307fSandi
186c19fe9c0Sandi        //handle admin tasks
187c19fe9c0Sandi        if($ACT == 'admin'){
18811e2ce22Schris            // retrieve admin plugin name from $_REQUEST['page']
18990f1b7bdSTom N Harris            if (($page = $INPUT->str('page', '', true)) != '') {
190a61966c5SChristopher Smith                /** @var $plugin DokuWiki_Admin_Plugin */
191a61966c5SChristopher Smith                if ($plugin = plugin_getRequestAdminPlugin()){
19211e2ce22Schris                    $plugin->handle();
19311e2ce22Schris                }
19411e2ce22Schris            }
195c19fe9c0Sandi        }
1965f312bacSAndreas Gohr
1975f312bacSAndreas Gohr        // check permissions again - the action may have changed
1985f312bacSAndreas Gohr        $ACT = act_permcheck($ACT);
19924bb549bSchris    }  // end event ACTION_ACT_PREPROCESS default action
20024bb549bSchris    $evt->advise_after();
20185dcda20SRobin Getz    // Make sure plugs can handle 'denied'
20285dcda20SRobin Getz    if($conf['send404'] && $ACT == 'denied') {
2039d2e1be6SAndreas Gohr        http_status(403);
20485dcda20SRobin Getz    }
20524bb549bSchris    unset($evt);
206c19fe9c0Sandi
20746c0ed74SMichael Hamann    // when action 'show', the intial not 'show' and POST, do a redirect
208585bf44eSChristopher Smith    if($ACT == 'show' && $preact != 'show' && strtolower($INPUT->server->str('REQUEST_METHOD')) == 'post'){
20969cd1e27SAndreas Gohr        act_redirect($ID,$preact);
21069cd1e27SAndreas Gohr    }
2115f312bacSAndreas Gohr
212c346111aSAdrian Lang    global $INFO;
213c346111aSAdrian Lang    global $conf;
214c346111aSAdrian Lang    global $license;
215c346111aSAdrian Lang
2166b13307fSandi    //call template FIXME: all needed vars available?
217f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
218746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
219f63a2007Schris
2205a892029SAndreas Gohr    include(template('main.php'));
221c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
222c19fe9c0Sandi    // in function tpl_content()
2236b13307fSandi}
2246b13307fSandi
225c8b076b1SMichael Hamann/**
226c8b076b1SMichael Hamann * Send the given headers using header()
227c8b076b1SMichael Hamann *
228c8b076b1SMichael Hamann * @param array $headers The headers that shall be sent
229c8b076b1SMichael Hamann */
230f63a2007Schrisfunction act_sendheaders($headers) {
231f63a2007Schris    foreach ($headers as $hdr) header($hdr);
232f63a2007Schris}
233f63a2007Schris
2346b13307fSandi/**
235af182434Sandi * Sanitize the action command
236af182434Sandi *
237af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
23842ea7f44SGerrit Uitslag *
23942ea7f44SGerrit Uitslag * @param array|string $act
24042ea7f44SGerrit Uitslag * @return string
241af182434Sandi */
242af182434Sandifunction act_clean($act){
243ee4c4a1bSAndreas Gohr    // check if the action was given as array key
244ee4c4a1bSAndreas Gohr    if(is_array($act)){
245ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
246ee4c4a1bSAndreas Gohr    }
247ee4c4a1bSAndreas Gohr
248ac83b9d8Sandi    //remove all bad chars
249ac83b9d8Sandi    $act = strtolower($act);
2502d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
251ac83b9d8Sandi
252ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
253cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
254b146b32bSandi
255396c218fSAndreas Gohr    if($act === '') $act = 'show';
25662baad0fSMartin Doucha    return $act;
25762baad0fSMartin Doucha}
25862baad0fSMartin Doucha
25962baad0fSMartin Doucha/**
26062baad0fSMartin Doucha * Sanitize and validate action commands.
26162baad0fSMartin Doucha *
26262baad0fSMartin Doucha * Add all allowed commands here.
26362baad0fSMartin Doucha *
26462baad0fSMartin Doucha * @author Andreas Gohr <andi@splitbrain.org>
26542ea7f44SGerrit Uitslag *
26642ea7f44SGerrit Uitslag * @param array|string $act
26742ea7f44SGerrit Uitslag * @return string
26862baad0fSMartin Doucha */
26962baad0fSMartin Douchafunction act_validate($act) {
270daf0cdbaSMartin Doucha    global $conf;
271daf0cdbaSMartin Doucha    global $INFO;
272daf0cdbaSMartin Doucha
27362baad0fSMartin Doucha    $act = act_clean($act);
274396c218fSAndreas Gohr
275409d7af7SAndreas Gohr    // check if action is disabled
276409d7af7SAndreas Gohr    if(!actionOK($act)){
277409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
278409d7af7SAndreas Gohr        return 'show';
279409d7af7SAndreas Gohr    }
280409d7af7SAndreas Gohr
28160e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
28260e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2831246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
2842a7abf2dSChristopher Smith                    'resendpwd','profile_delete'))){
28560e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
28660e6b550SAndreas Gohr        return 'show';
28760e6b550SAndreas Gohr    }
28860e6b550SAndreas Gohr
289c828a5d6SAndreas Gohr    //is there really a draft?
290c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
291c828a5d6SAndreas Gohr
292067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
293ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2941246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2952a7abf2dSChristopher Smith                    'unsubscribe','profile','profile_delete','resendpwd','recover',
296d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
297ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
298af182434Sandi        return 'show';
299af182434Sandi    }
300af182434Sandi    return $act;
301af182434Sandi}
302af182434Sandi
303af182434Sandi/**
3046b13307fSandi * Run permissionchecks
3056b13307fSandi *
3066b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
30742ea7f44SGerrit Uitslag *
30842ea7f44SGerrit Uitslag * @param string $act action command
30942ea7f44SGerrit Uitslag * @return string action command
3106b13307fSandi */
3116b13307fSandifunction act_permcheck($act){
312dbbc6aa7Sandi    global $INFO;
313dbbc6aa7Sandi
314ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
3156b13307fSandi        if($INFO['exists']){
316bdbc16bfSandi            if($act == 'edit'){
317bdbc16bfSandi                //the edit function will check again and do a source show
318bdbc16bfSandi                //when no AUTH_EDIT available
319bdbc16bfSandi                $permneed = AUTH_READ;
320bdbc16bfSandi            }else{
3216b13307fSandi                $permneed = AUTH_EDIT;
322bdbc16bfSandi            }
3236b13307fSandi        }else{
3246b13307fSandi            $permneed = AUTH_CREATE;
3256b13307fSandi        }
3262a7abf2dSChristopher Smith    }elseif(in_array($act,array('login','search','recent','profile','profile_delete','index', 'sitemap'))){
3276b13307fSandi        $permneed = AUTH_NONE;
3281246e016SAndreas Gohr    }elseif($act == 'revert'){
3291246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
3301246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
3315e199953Smatthiasgrimm    }elseif($act == 'register'){
3325e199953Smatthiasgrimm        $permneed = AUTH_NONE;
333ebd3d9ceSchris    }elseif($act == 'resendpwd'){
334ebd3d9ceSchris        $permneed = AUTH_NONE;
335c19fe9c0Sandi    }elseif($act == 'admin'){
336f8cc712eSAndreas Gohr        if($INFO['ismanager']){
337f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
338f8cc712eSAndreas Gohr            // action is checked later
339f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
340f8cc712eSAndreas Gohr        }else{
341c19fe9c0Sandi            $permneed = AUTH_ADMIN;
342f8cc712eSAndreas Gohr        }
3436b13307fSandi    }else{
3446b13307fSandi        $permneed = AUTH_READ;
3456b13307fSandi    }
346dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
347dbbc6aa7Sandi
3486b13307fSandi    return 'denied';
3496b13307fSandi}
3506b13307fSandi
3516b13307fSandi/**
352ee4c4a1bSAndreas Gohr * Handle 'draftdel'
353ee4c4a1bSAndreas Gohr *
354ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
35542ea7f44SGerrit Uitslag *
35642ea7f44SGerrit Uitslag * @param string $act action command
35742ea7f44SGerrit Uitslag * @return string action command
358ee4c4a1bSAndreas Gohr */
359ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
360ee4c4a1bSAndreas Gohr    global $INFO;
361ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
362ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
363ee4c4a1bSAndreas Gohr    return 'show';
364ee4c4a1bSAndreas Gohr}
365ee4c4a1bSAndreas Gohr
366ee4c4a1bSAndreas Gohr/**
367ee4c4a1bSAndreas Gohr * Saves a draft on preview
368ee4c4a1bSAndreas Gohr *
369ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
37042ea7f44SGerrit Uitslag *
37142ea7f44SGerrit Uitslag * @param string $act action command
37242ea7f44SGerrit Uitslag * @return string action command
373ee4c4a1bSAndreas Gohr */
374ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
375ee4c4a1bSAndreas Gohr    global $INFO;
376ee4c4a1bSAndreas Gohr    global $ID;
37790f1b7bdSTom N Harris    global $INPUT;
378ee4c4a1bSAndreas Gohr    global $conf;
37990f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
380ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
38190f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
38290f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
38390f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
38490f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
385ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
386ee4c4a1bSAndreas Gohr                );
387ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
388ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
389ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
390ee4c4a1bSAndreas Gohr        }
391ee4c4a1bSAndreas Gohr    }
392ee4c4a1bSAndreas Gohr    return $act;
393ee4c4a1bSAndreas Gohr}
394ee4c4a1bSAndreas Gohr
395ee4c4a1bSAndreas Gohr/**
3966b13307fSandi * Handle 'save'
3976b13307fSandi *
3986b13307fSandi * Checks for spam and conflicts and saves the page.
3996b13307fSandi * Does a redirect to show the page afterwards or
4006b13307fSandi * returns a new action.
4016b13307fSandi *
4026b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
40342ea7f44SGerrit Uitslag *
40442ea7f44SGerrit Uitslag * @param string $act action command
40542ea7f44SGerrit Uitslag * @return string action command
4066b13307fSandi */
4076b13307fSandifunction act_save($act){
4086b13307fSandi    global $ID;
4096b13307fSandi    global $DATE;
4106b13307fSandi    global $PRE;
4116b13307fSandi    global $TEXT;
4126b13307fSandi    global $SUF;
4136b13307fSandi    global $SUM;
4145a932e77SAdrian Lang    global $lang;
4158d67c48aSAdrian Lang    global $INFO;
41690f1b7bdSTom N Harris    global $INPUT;
4176b13307fSandi
4186b13307fSandi    //spam check
4195a932e77SAdrian Lang    if(checkwordblock()) {
4205a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4215a932e77SAdrian Lang        return 'edit';
4225a932e77SAdrian Lang    }
4238d67c48aSAdrian Lang    //conflict check
4248d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
4256b13307fSandi        return 'conflict';
4266b13307fSandi
4276b13307fSandi    //save it
428e0c26282SGerrit Uitslag    saveWikiText($ID,con($PRE,$TEXT,$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con
4296b13307fSandi    //unlock it
4306b13307fSandi    unlock($ID);
4316b13307fSandi
432ee4c4a1bSAndreas Gohr    //delete draft
433ee4c4a1bSAndreas Gohr    act_draftdel($act);
43469cd1e27SAndreas Gohr    session_write_close();
435ee4c4a1bSAndreas Gohr
43669cd1e27SAndreas Gohr    // when done, show page
43769cd1e27SAndreas Gohr    return 'show';
43869cd1e27SAndreas Gohr}
439f951a474SAndreas Gohr
44014a122deSAndreas Gohr/**
4411246e016SAndreas Gohr * Revert to a certain revision
4421246e016SAndreas Gohr *
4431246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
44442ea7f44SGerrit Uitslag *
44542ea7f44SGerrit Uitslag * @param string $act action command
44642ea7f44SGerrit Uitslag * @return string action command
4471246e016SAndreas Gohr */
4481246e016SAndreas Gohrfunction act_revert($act){
4491246e016SAndreas Gohr    global $ID;
4501246e016SAndreas Gohr    global $REV;
4511246e016SAndreas Gohr    global $lang;
452585bf44eSChristopher Smith    /* @var Input $INPUT */
453585bf44eSChristopher Smith    global $INPUT;
454de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
455de4d479aSAdrian Lang    // global $INFO;
456de4d479aSAdrian Lang    // if (!$INFO['writable']) {
457de4d479aSAdrian Lang    //     return 'show';
458de4d479aSAdrian Lang    // }
4591246e016SAndreas Gohr
4601246e016SAndreas Gohr    // when no revision is given, delete current one
4611246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
4621246e016SAndreas Gohr    $text = '';
4631246e016SAndreas Gohr    $sum  = $lang['deleted'];
4641246e016SAndreas Gohr    if($REV){
4651246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
4661246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
467d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
4681246e016SAndreas Gohr    }
4691246e016SAndreas Gohr
4701246e016SAndreas Gohr    // spam check
4715a932e77SAdrian Lang
4725a932e77SAdrian Lang    if (checkwordblock($text)) {
4735a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4745a932e77SAdrian Lang        return 'edit';
4755a932e77SAdrian Lang    }
4761246e016SAndreas Gohr
4771246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
4781246e016SAndreas Gohr    msg($sum,1);
4791246e016SAndreas Gohr
4801246e016SAndreas Gohr    //delete any draft
4811246e016SAndreas Gohr    act_draftdel($act);
4821246e016SAndreas Gohr    session_write_close();
4831246e016SAndreas Gohr
4841246e016SAndreas Gohr    // when done, show current page
485585bf44eSChristopher Smith    $INPUT->server->set('REQUEST_METHOD','post'); //should force a redirect
4861246e016SAndreas Gohr    $REV = '';
4871246e016SAndreas Gohr    return 'show';
4881246e016SAndreas Gohr}
4891246e016SAndreas Gohr
4901246e016SAndreas Gohr/**
49114a122deSAndreas Gohr * Do a redirect after receiving post data
49214a122deSAndreas Gohr *
49314a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
49442ea7f44SGerrit Uitslag *
49542ea7f44SGerrit Uitslag * @param string $id page id
49642ea7f44SGerrit Uitslag * @param string $preact action command before redirect
49714a122deSAndreas Gohr */
49869cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
49969cd1e27SAndreas Gohr    global $PRE;
50069cd1e27SAndreas Gohr    global $TEXT;
501f951a474SAndreas Gohr
50269cd1e27SAndreas Gohr    $opts = array(
50369cd1e27SAndreas Gohr            'id'       => $id,
50469cd1e27SAndreas Gohr            'preact'   => $preact
50569cd1e27SAndreas Gohr            );
506c66972f2SAdrian Lang    //get section name when coming from section edit
507c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
508c66972f2SAdrian Lang        $check = false; //Byref
509c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
510c66972f2SAdrian Lang    }
511c66972f2SAdrian Lang
51269cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
51369cd1e27SAndreas Gohr}
51469cd1e27SAndreas Gohr
515c8b076b1SMichael Hamann/**
516c8b076b1SMichael Hamann * Execute the redirect
517c8b076b1SMichael Hamann *
51842ea7f44SGerrit Uitslag * @param array $opts id and fragment for the redirect and the preact
519c8b076b1SMichael Hamann */
52069cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
52169cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
522c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
52369cd1e27SAndreas Gohr
5246b13307fSandi    //show it
525af2408d5SAndreas Gohr    send_redirect($go);
5266b13307fSandi}
5276b13307fSandi
5286b13307fSandi/**
529b8957367SBenjamin Gilbert * Handle 'login', 'logout'
5306b13307fSandi *
5316b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
53242ea7f44SGerrit Uitslag *
53342ea7f44SGerrit Uitslag * @param string $act action command
53442ea7f44SGerrit Uitslag * @return string action command
5356b13307fSandi */
5366b13307fSandifunction act_auth($act){
53708eda5bcSmatthiasgrimm    global $ID;
5387cace34dSAndreas Gohr    global $INFO;
539585bf44eSChristopher Smith    /* @var Input $INPUT */
540585bf44eSChristopher Smith    global $INPUT;
54108eda5bcSmatthiasgrimm
5426b13307fSandi    //already logged in?
543585bf44eSChristopher Smith    if($INPUT->server->has('REMOTE_USER') && $act=='login'){
544ca12ce46SAndreas Gohr        return 'show';
5452288dc06SGuy Brand    }
5466b13307fSandi
5476b13307fSandi    //handle logout
5486b13307fSandi    if($act=='logout'){
54908eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
550585bf44eSChristopher Smith        if($lockedby == $INPUT->server->str('REMOTE_USER')){
55108eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
552585bf44eSChristopher Smith        }
55308eda5bcSmatthiasgrimm
5547cace34dSAndreas Gohr        // do the logout stuff
5556b13307fSandi        auth_logoff();
5567cace34dSAndreas Gohr
5577cace34dSAndreas Gohr        // rebuild info array
5587cace34dSAndreas Gohr        $INFO = pageinfo();
5597cace34dSAndreas Gohr
560e16eccb7SGuy Brand        act_redirect($ID,'login');
5616b13307fSandi    }
5626b13307fSandi
5636b13307fSandi    return $act;
5646b13307fSandi}
5656b13307fSandi
5666b13307fSandi/**
56745a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
5686b13307fSandi *
5696b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
57042ea7f44SGerrit Uitslag *
57142ea7f44SGerrit Uitslag * @param string $act action command
57242ea7f44SGerrit Uitslag * @return string action command
5736b13307fSandi */
5746b13307fSandifunction act_edit($act){
575cd409024Sjorda    global $ID;
576ee4c4a1bSAndreas Gohr    global $INFO;
577cd409024Sjorda
57845a99335SAdrian Lang    global $TEXT;
57945a99335SAdrian Lang    global $RANGE;
58045a99335SAdrian Lang    global $PRE;
58145a99335SAdrian Lang    global $SUF;
58245a99335SAdrian Lang    global $REV;
58345a99335SAdrian Lang    global $SUM;
58445a99335SAdrian Lang    global $lang;
58545a99335SAdrian Lang    global $DATE;
58645a99335SAdrian Lang
58745a99335SAdrian Lang    if (!isset($TEXT)) {
58845a99335SAdrian Lang        if ($INFO['exists']) {
58945a99335SAdrian Lang            if ($RANGE) {
59045a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
59145a99335SAdrian Lang            } else {
59245a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
59345a99335SAdrian Lang            }
59445a99335SAdrian Lang        } else {
595fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
59645a99335SAdrian Lang        }
59745a99335SAdrian Lang    }
59845a99335SAdrian Lang
59945a99335SAdrian Lang    //set summary default
60045a99335SAdrian Lang    if(!$SUM){
60145a99335SAdrian Lang        if($REV){
6027656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
60345a99335SAdrian Lang        }elseif(!$INFO['exists']){
60445a99335SAdrian Lang            $SUM = $lang['created'];
60545a99335SAdrian Lang        }
60645a99335SAdrian Lang    }
60745a99335SAdrian Lang
6088d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
6098d67c48aSAdrian Lang    // This is used for conflict detection
61078035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
61145a99335SAdrian Lang
6126b13307fSandi    //check if locked by anyone - if not lock for my self
61331bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
61431bc8f11SMichael Hamann    if ($INFO['writable']) {
6156b13307fSandi        $lockedby = checklock($ID);
6166b13307fSandi        if($lockedby) return 'locked';
6176b13307fSandi
6186b13307fSandi        lock($ID);
61931bc8f11SMichael Hamann    }
62031bc8f11SMichael Hamann
6216b13307fSandi    return $act;
6226b13307fSandi}
6236b13307fSandi
6246b13307fSandi/**
625f6dad9fdSMichael Klier * Export a wiki page for various formats
626f6dad9fdSMichael Klier *
627f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
628f6dad9fdSMichael Klier *
629f6dad9fdSMichael Klier *  Event data:
630f6dad9fdSMichael Klier *    data['id']      -- page id
631f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
632f6dad9fdSMichael Klier *    data['headers'] -- export headers
633f6dad9fdSMichael Klier *    data['output']  -- export output
6346b13307fSandi *
6356b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
636f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
63742ea7f44SGerrit Uitslag *
63842ea7f44SGerrit Uitslag * @param string $act action command
63942ea7f44SGerrit Uitslag * @return string action command
6406b13307fSandi */
6416b13307fSandifunction act_export($act){
6426b13307fSandi    global $ID;
6436b13307fSandi    global $REV;
64485f8705cSAnika Henke    global $conf;
64585f8705cSAnika Henke    global $lang;
6466b13307fSandi
647f6dad9fdSMichael Klier    $pre = '';
648f6dad9fdSMichael Klier    $post = '';
649f6dad9fdSMichael Klier    $headers = array();
650cc2ae802SAndreas Gohr
651f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
652f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
653f6dad9fdSMichael Klier
654ac83b9d8Sandi    $mode = substr($act,7);
655f6dad9fdSMichael Klier    switch($mode) {
656f6dad9fdSMichael Klier        case 'raw':
6575adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
65866b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
659f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
660f6dad9fdSMichael Klier            break;
661f6dad9fdSMichael Klier        case 'xhtml':
662c8839c22SAnika Henke            $pre .= '<!DOCTYPE html>' . DOKU_LF;
663c8839c22SAnika Henke            $pre .= '<html lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
664f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
665c8839c22SAnika Henke            $pre .= '  <meta charset="utf-8" />' . DOKU_LF;
666f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
667f6dad9fdSMichael Klier
668f6dad9fdSMichael Klier            // get metaheaders
669f6dad9fdSMichael Klier            ob_start();
670f6dad9fdSMichael Klier            tpl_metaheaders();
671f6dad9fdSMichael Klier            $pre .= ob_get_clean();
672f6dad9fdSMichael Klier
673f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
674f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
675f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
676f6dad9fdSMichael Klier
677f6dad9fdSMichael Klier            // get toc
678f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
679f6dad9fdSMichael Klier
680f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
681f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
682f6dad9fdSMichael Klier
683f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
684f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
685f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
686f6dad9fdSMichael Klier            break;
687f6dad9fdSMichael Klier        case 'xhtmlbody':
688f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
689f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
690f6dad9fdSMichael Klier            break;
691f6dad9fdSMichael Klier        default:
692b814e978SGerrit Uitslag            $output = p_cached_output(wikiFN($ID,$REV), $mode, $ID);
6939acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
694f6dad9fdSMichael Klier            break;
695f6dad9fdSMichael Klier    }
696f6dad9fdSMichael Klier
697f6dad9fdSMichael Klier    // prepare event data
698f6dad9fdSMichael Klier    $data = array();
699f6dad9fdSMichael Klier    $data['id'] = $ID;
700f6dad9fdSMichael Klier    $data['mode'] = $mode;
701f6dad9fdSMichael Klier    $data['headers'] = $headers;
702f6dad9fdSMichael Klier    $data['output'] =& $output;
703f6dad9fdSMichael Klier
704f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
705f6dad9fdSMichael Klier
706f6dad9fdSMichael Klier    if(!empty($data['output'])){
707f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
70885767031SAndreas Gohr            header("$key: $val");
70985767031SAndreas Gohr        }
710f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
7116b13307fSandi        exit;
7126b13307fSandi    }
7136b13307fSandi    return 'show';
7146b13307fSandi}
715340756e4Sandi
716b158d625SSteven Danz/**
717c4f79b71SMichael Hamann * Handle sitemap delivery
718c4f79b71SMichael Hamann *
719c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
72042ea7f44SGerrit Uitslag *
72142ea7f44SGerrit Uitslag * @param string $act action command
722c4f79b71SMichael Hamann */
723c4f79b71SMichael Hamannfunction act_sitemap($act) {
724c4f79b71SMichael Hamann    global $conf;
725c4f79b71SMichael Hamann
726eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
7279d2e1be6SAndreas Gohr        http_status(404);
728c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
729c4f79b71SMichael Hamann        exit;
730c4f79b71SMichael Hamann    }
731c4f79b71SMichael Hamann
732eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
73365f6e7d6SMichael Hamann    if (Sitemapper::sitemapIsCompressed()) {
734c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
735c4f79b71SMichael Hamann    }else{
736c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
737c4f79b71SMichael Hamann    }
738c4f79b71SMichael Hamann
739c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
740c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
7412897eb23SMichael Hamann        Sitemapper::generate();
742c4f79b71SMichael Hamann    }
743c4f79b71SMichael Hamann
744c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
745c4f79b71SMichael Hamann        // Send headers
746c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
7473009a773SAndreas Gohr        header('Content-Disposition: attachment; filename='.utf8_basename($sitemap));
748c4f79b71SMichael Hamann
749eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
750eae17177SMichael Hamann
751c4f79b71SMichael Hamann        // Send file
752c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
75340e0b444SDominik Eckelmann        http_sendfile($sitemap);
754c4f79b71SMichael Hamann
755eae17177SMichael Hamann        readfile($sitemap);
756c4f79b71SMichael Hamann        exit;
757c4f79b71SMichael Hamann    }
758c4f79b71SMichael Hamann
7599d2e1be6SAndreas Gohr    http_status(500);
760eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
761c4f79b71SMichael Hamann    exit;
762c4f79b71SMichael Hamann}
763c4f79b71SMichael Hamann
764c4f79b71SMichael Hamann/**
7655b75cd1fSAdrian Lang * Handle page 'subscribe'
766b158d625SSteven Danz *
7675b75cd1fSAdrian Lang * Throws exception on error.
7685b75cd1fSAdrian Lang *
7695b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
77042ea7f44SGerrit Uitslag *
77142ea7f44SGerrit Uitslag * @param string $act action command
77242ea7f44SGerrit Uitslag * @return string action command
77342ea7f44SGerrit Uitslag * @throws Exception if (un)subscribing fails
774b158d625SSteven Danz */
7751380fc45SAndreas Gohrfunction act_subscription($act){
776056c2049SAndreas Gohr    global $lang;
777056c2049SAndreas Gohr    global $INFO;
778056c2049SAndreas Gohr    global $ID;
779585bf44eSChristopher Smith    /* @var Input $INPUT */
78090f1b7bdSTom N Harris    global $INPUT;
78152b0dd67SGuy Brand
7829fa341d0SAndreas Gohr    // subcriptions work for logged in users only
783585bf44eSChristopher Smith    if(!$INPUT->server->str('REMOTE_USER')) return 'show';
7849fa341d0SAndreas Gohr
785056c2049SAndreas Gohr    // get and preprocess data.
7868881fcc9SAdrian Lang    $params = array();
7878881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
78890f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
78990f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
7908881fcc9SAdrian Lang        }
7918881fcc9SAdrian Lang    }
7928881fcc9SAdrian Lang
793056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
7940e80bb5eSChristopher Smith    if(empty($params['action']) || !checkSecurityToken()) return $act;
795056c2049SAndreas Gohr
7968881fcc9SAdrian Lang    // Handle POST data, may throw exception.
7978881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7988881fcc9SAdrian Lang
7998881fcc9SAdrian Lang    $target = $params['target'];
8008881fcc9SAdrian Lang    $style  = $params['style'];
8018881fcc9SAdrian Lang    $action = $params['action'];
8028881fcc9SAdrian Lang
8038881fcc9SAdrian Lang    // Perform action.
804a0519fdaSAndreas Gohr    $sub = new Subscription();
805a0519fdaSAndreas Gohr    if($action == 'unsubscribe'){
806585bf44eSChristopher Smith        $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
807a0519fdaSAndreas Gohr    }else{
808585bf44eSChristopher Smith        $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
809a0519fdaSAndreas Gohr    }
810a0519fdaSAndreas Gohr
811a0519fdaSAndreas Gohr    if($ok) {
812a0519fdaSAndreas Gohr        msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
813a0519fdaSAndreas Gohr                    prettyprint_id($target)), 1);
814a0519fdaSAndreas Gohr        act_redirect($ID, $act);
815a0519fdaSAndreas Gohr    } else {
8168881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
8178881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
8188881fcc9SAdrian Lang                                    prettyprint_id($target)));
8198881fcc9SAdrian Lang    }
820cb3f9dbaSAdrian Lang
821cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
822a0519fdaSAndreas Gohr    $INFO['subscribed'] = $sub->user_subscription();
823cb3f9dbaSAdrian Lang    return 'show';
8248881fcc9SAdrian Lang}
8258881fcc9SAdrian Lang
8268881fcc9SAdrian Lang/**
8278881fcc9SAdrian Lang * Validate POST data
8288881fcc9SAdrian Lang *
8298881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
8308881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
8318881fcc9SAdrian Lang *
8328881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
83342ea7f44SGerrit Uitslag *
83442ea7f44SGerrit Uitslag * @param array &$params the parameters: target, style and action
83542ea7f44SGerrit Uitslag * @throws Exception
8368881fcc9SAdrian Lang */
8377a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
8388881fcc9SAdrian Lang    global $INFO;
8398881fcc9SAdrian Lang    global $lang;
840585bf44eSChristopher Smith    /* @var Input $INPUT */
841585bf44eSChristopher Smith    global $INPUT;
8428881fcc9SAdrian Lang
8435b75cd1fSAdrian Lang    // Get and validate parameters.
8448881fcc9SAdrian Lang    if (!isset($params['target'])) {
84515741132SAndreas Gohr        throw new Exception('no subscription target given');
8465b75cd1fSAdrian Lang    }
8478881fcc9SAdrian Lang    $target = $params['target'];
8485b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
8495b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
8505b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
8515b75cd1fSAdrian Lang        $valid_styles[] = 'list';
8525b75cd1fSAdrian Lang    }
8538881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
85415741132SAndreas Gohr                              'invalid subscription style given');
8558881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
85615741132SAndreas Gohr                              $params, 'invalid subscription action given');
857613964ecSGuy Brand
8585b75cd1fSAdrian Lang    // Check other conditions.
8595b75cd1fSAdrian Lang    if ($action === 'subscribe') {
8605b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
8615b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
86252b0dd67SGuy Brand        }
8635b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
8645b75cd1fSAdrian Lang        $is = false;
8655b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
8665b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
8675b75cd1fSAdrian Lang                $is = true;
86852b0dd67SGuy Brand            }
86952b0dd67SGuy Brand        }
8705b75cd1fSAdrian Lang        if ($is === false) {
87115741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
872585bf44eSChristopher Smith                                        $INPUT->server->str('REMOTE_USER'),
8735b75cd1fSAdrian Lang                                        prettyprint_id($target)));
8745b75cd1fSAdrian Lang        }
8755b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
8765b75cd1fSAdrian Lang        $style = null;
87752b0dd67SGuy Brand    }
87852b0dd67SGuy Brand
87916c665d9SAndreas Gohr    $params = compact('target', 'style', 'action');
88052b0dd67SGuy Brand}
88152b0dd67SGuy Brand
882e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
883