xref: /dokuwiki/inc/actions.php (revision 1cc82e5c76ae7fcd646e448404afdc0fd458bf55)
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
116b13307fSandi/**
126b13307fSandi * Call the needed action handlers
136b13307fSandi *
146b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
15c9570649SAndreas Gohr * @triggers ACTION_ACT_PREPROCESS
16c9570649SAndreas Gohr * @triggers ACTION_HEADERS_SEND
176b13307fSandi */
186b13307fSandifunction act_dispatch(){
196b13307fSandi    global $ACT;
206b13307fSandi    global $ID;
2124ea6500SAndreas Gohr    global $INFO;
226b13307fSandi    global $QUERY;
23585bf44eSChristopher Smith    /* @var Input $INPUT */
2490f1b7bdSTom N Harris    global $INPUT;
256b13307fSandi    global $lang;
2685dcda20SRobin Getz    global $conf;
276b13307fSandi
2869cd1e27SAndreas Gohr    $preact = $ACT;
2969cd1e27SAndreas Gohr
30c2e830f2Schris    // give plugins an opportunity to process the action
3124bb549bSchris    $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT);
3224bb549bSchris    if ($evt->advise_before()) {
33c2e830f2Schris
34af182434Sandi        //sanitize $ACT
3562baad0fSMartin Doucha        $ACT = act_validate($ACT);
36af182434Sandi
37b8957367SBenjamin Gilbert        //check if searchword was given - else just show
380868021bSAndreas Gohr        $s = cleanID($QUERY);
390868021bSAndreas Gohr        if($ACT == 'search' && empty($s)){
40b8957367SBenjamin Gilbert            $ACT = 'show';
41b8957367SBenjamin Gilbert        }
42b8957367SBenjamin Gilbert
43b8957367SBenjamin Gilbert        //login stuff
441b2a85e8SAndreas Gohr        if(in_array($ACT,array('login','logout'))){
45b8957367SBenjamin Gilbert            $ACT = act_auth($ACT);
461b2a85e8SAndreas Gohr        }
47b8957367SBenjamin Gilbert
481380fc45SAndreas Gohr        //check if user is asking to (un)subscribe a page
495b75cd1fSAdrian Lang        if($ACT == 'subscribe') {
505b75cd1fSAdrian Lang            try {
511380fc45SAndreas Gohr                $ACT = act_subscription($ACT);
525b75cd1fSAdrian Lang            } catch (Exception $e) {
535b75cd1fSAdrian Lang                msg($e->getMessage(), -1);
545b75cd1fSAdrian Lang            }
555b75cd1fSAdrian Lang        }
5652b0dd67SGuy Brand
575381a7eeSElan Ruusamäe        //display some info
584064e2d3SRobin Getz        if($ACT == 'check'){
594064e2d3SRobin Getz            check();
604064e2d3SRobin Getz            $ACT = 'show';
614064e2d3SRobin Getz        }
624064e2d3SRobin Getz
636b13307fSandi        //check permissions
646b13307fSandi        $ACT = act_permcheck($ACT);
656b13307fSandi
66c4f79b71SMichael Hamann        //sitemap
67eae17177SMichael Hamann        if ($ACT == 'sitemap'){
68c8b076b1SMichael Hamann            act_sitemap($ACT);
69eae17177SMichael Hamann        }
70c4f79b71SMichael Hamann
713c94d07bSAnika Henke        //recent changes
723c94d07bSAnika Henke        if ($ACT == 'recent'){
733c94d07bSAnika Henke            $show_changes = $INPUT->str('show_changes');
743c94d07bSAnika Henke            if (!empty($show_changes)) {
753c94d07bSAnika Henke                set_doku_pref('show_changes', $show_changes);
763c94d07bSAnika Henke            }
773c94d07bSAnika Henke        }
783c94d07bSAnika Henke
793c94d07bSAnika Henke        //diff
803c94d07bSAnika Henke        if ($ACT == 'diff'){
813c94d07bSAnika Henke            $difftype = $INPUT->str('difftype');
823c94d07bSAnika Henke            if (!empty($difftype)) {
833c94d07bSAnika Henke                set_doku_pref('difftype', $difftype);
843c94d07bSAnika Henke            }
853c94d07bSAnika Henke        }
863c94d07bSAnika Henke
87b8957367SBenjamin Gilbert        //register
88eea0f0d0SAndreas Gohr        if($ACT == 'register' && $INPUT->post->bool('save') && register()){
89b8957367SBenjamin Gilbert            $ACT = 'login';
90b8957367SBenjamin Gilbert        }
916b13307fSandi
928b06d178Schris        if ($ACT == 'resendpwd' && act_resendpwd()) {
938b06d178Schris            $ACT = 'login';
948b06d178Schris        }
958b06d178Schris
962a7abf2dSChristopher Smith        // user profile changes
972a7abf2dSChristopher Smith        if (in_array($ACT, array('profile','profile_delete'))) {
98585bf44eSChristopher Smith            if(!$INPUT->server->str('REMOTE_USER')) {
9925b2a98cSMichael Klier                $ACT = 'login';
10025b2a98cSMichael Klier            } else {
1012a7abf2dSChristopher Smith                switch ($ACT) {
1022a7abf2dSChristopher Smith                    case 'profile' :
10325b2a98cSMichael Klier                        if(updateprofile()) {
1044cb79657SMatthias Grimm                            msg($lang['profchanged'],1);
1054cb79657SMatthias Grimm                            $ACT = 'show';
1068b06d178Schris                        }
1072a7abf2dSChristopher Smith                        break;
1082a7abf2dSChristopher Smith                    case 'profile_delete' :
1092a7abf2dSChristopher Smith                        if(auth_deleteprofile()){
1102a7abf2dSChristopher Smith                            msg($lang['profdeleted'],1);
1112a7abf2dSChristopher Smith                            $ACT = 'show';
1122a7abf2dSChristopher Smith                        } else {
1132a7abf2dSChristopher Smith                            $ACT = 'profile';
1142a7abf2dSChristopher Smith                        }
1152a7abf2dSChristopher Smith                        break;
1162a7abf2dSChristopher Smith                }
11725b2a98cSMichael Klier            }
11825b2a98cSMichael Klier        }
1198b06d178Schris
1201246e016SAndreas Gohr        //revert
1211246e016SAndreas Gohr        if($ACT == 'revert'){
1221246e016SAndreas Gohr            if(checkSecurityToken()){
1231246e016SAndreas Gohr                $ACT = act_revert($ACT);
1241246e016SAndreas Gohr            }else{
1251246e016SAndreas Gohr                $ACT = 'show';
1261246e016SAndreas Gohr            }
1271246e016SAndreas Gohr        }
1281246e016SAndreas Gohr
1296b13307fSandi        //save
1301b2a85e8SAndreas Gohr        if($ACT == 'save'){
1311b2a85e8SAndreas Gohr            if(checkSecurityToken()){
1326b13307fSandi                $ACT = act_save($ACT);
1331b2a85e8SAndreas Gohr            }else{
1348071beaaSAndreas Gohr                $ACT = 'preview';
1351b2a85e8SAndreas Gohr            }
1361b2a85e8SAndreas Gohr        }
1376b13307fSandi
138067c5d22SBen Coburn        //cancel conflicting edit
139067c5d22SBen Coburn        if($ACT == 'cancel')
140067c5d22SBen Coburn            $ACT = 'show';
141067c5d22SBen Coburn
142ee4c4a1bSAndreas Gohr        //draft deletion
143ee4c4a1bSAndreas Gohr        if($ACT == 'draftdel')
144ee4c4a1bSAndreas Gohr            $ACT = act_draftdel($ACT);
145ee4c4a1bSAndreas Gohr
146ee4c4a1bSAndreas Gohr        //draft saving on preview
147ee4c4a1bSAndreas Gohr        if($ACT == 'preview')
148ee4c4a1bSAndreas Gohr            $ACT = act_draftsave($ACT);
149ee4c4a1bSAndreas Gohr
1506b13307fSandi        //edit
151c9d5430bSAdrian Lang        if(in_array($ACT, array('edit', 'preview', 'recover'))) {
152af182434Sandi            $ACT = act_edit($ACT);
1536b13307fSandi        }else{
1546b13307fSandi            unlock($ID); //try to unlock
1556b13307fSandi        }
1566b13307fSandi
1576b13307fSandi        //handle export
158ac83b9d8Sandi        if(substr($ACT,0,7) == 'export_')
1596b13307fSandi            $ACT = act_export($ACT);
1606b13307fSandi
161c19fe9c0Sandi        //handle admin tasks
162c19fe9c0Sandi        if($ACT == 'admin'){
16311e2ce22Schris            // retrieve admin plugin name from $_REQUEST['page']
16490f1b7bdSTom N Harris            if (($page = $INPUT->str('page', '', true)) != '') {
16511e2ce22Schris                $pluginlist = plugin_list('admin');
16690f1b7bdSTom N Harris                if (in_array($page, $pluginlist)) {
16711e2ce22Schris                    // attempt to load the plugin
1685da403f1SGerrit Uitslag
1695da403f1SGerrit Uitslag                    if (($plugin = plugin_load('admin',$page)) !== null){
170c8b076b1SMichael Hamann                        /** @var DokuWiki_Admin_Plugin $plugin */
17124ea6500SAndreas Gohr                        if($plugin->forAdminOnly() && !$INFO['isadmin']){
17224ea6500SAndreas Gohr                            // a manager tried to load a plugin that's for admins only
17390f1b7bdSTom N Harris                            $INPUT->remove('page');
17424ea6500SAndreas Gohr                            msg('For admins only',-1);
17524ea6500SAndreas Gohr                        }else{
17611e2ce22Schris                            $plugin->handle();
17711e2ce22Schris                        }
17811e2ce22Schris                    }
179c19fe9c0Sandi                }
18024ea6500SAndreas Gohr            }
18124ea6500SAndreas Gohr        }
1825f312bacSAndreas Gohr
1835f312bacSAndreas Gohr        // check permissions again - the action may have changed
1845f312bacSAndreas Gohr        $ACT = act_permcheck($ACT);
18524bb549bSchris    }  // end event ACTION_ACT_PREPROCESS default action
18624bb549bSchris    $evt->advise_after();
18785dcda20SRobin Getz    // Make sure plugs can handle 'denied'
18885dcda20SRobin Getz    if($conf['send404'] && $ACT == 'denied') {
1899d2e1be6SAndreas Gohr        http_status(403);
19085dcda20SRobin Getz    }
19124bb549bSchris    unset($evt);
192c19fe9c0Sandi
19346c0ed74SMichael Hamann    // when action 'show', the intial not 'show' and POST, do a redirect
194585bf44eSChristopher Smith    if($ACT == 'show' && $preact != 'show' && strtolower($INPUT->server->str('REQUEST_METHOD')) == 'post'){
19569cd1e27SAndreas Gohr        act_redirect($ID,$preact);
19669cd1e27SAndreas Gohr    }
1975f312bacSAndreas Gohr
198c346111aSAdrian Lang    global $INFO;
199c346111aSAdrian Lang    global $conf;
200c346111aSAdrian Lang    global $license;
201c346111aSAdrian Lang
2026b13307fSandi    //call template FIXME: all needed vars available?
203*1cc82e5cSGerrit Uitslag    $headers = array();
204f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
205746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
206f63a2007Schris
2075a892029SAndreas Gohr    include(template('main.php'));
208c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
209c19fe9c0Sandi    // in function tpl_content()
2106b13307fSandi}
2116b13307fSandi
212c8b076b1SMichael Hamann/**
213c8b076b1SMichael Hamann * Send the given headers using header()
214c8b076b1SMichael Hamann *
215c8b076b1SMichael Hamann * @param array $headers The headers that shall be sent
216c8b076b1SMichael Hamann */
217f63a2007Schrisfunction act_sendheaders($headers) {
218f63a2007Schris    foreach ($headers as $hdr) header($hdr);
219f63a2007Schris}
220f63a2007Schris
2216b13307fSandi/**
222af182434Sandi * Sanitize the action command
223af182434Sandi *
224af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
22542ea7f44SGerrit Uitslag *
22642ea7f44SGerrit Uitslag * @param array|string $act
22742ea7f44SGerrit Uitslag * @return string
228af182434Sandi */
229af182434Sandifunction act_clean($act){
230ee4c4a1bSAndreas Gohr    // check if the action was given as array key
231ee4c4a1bSAndreas Gohr    if(is_array($act)){
232ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
233ee4c4a1bSAndreas Gohr    }
234ee4c4a1bSAndreas Gohr
235ac83b9d8Sandi    //remove all bad chars
236ac83b9d8Sandi    $act = strtolower($act);
2372d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
238ac83b9d8Sandi
239ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
240cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
241b146b32bSandi
242396c218fSAndreas Gohr    if($act === '') $act = 'show';
24362baad0fSMartin Doucha    return $act;
24462baad0fSMartin Doucha}
24562baad0fSMartin Doucha
24662baad0fSMartin Doucha/**
24762baad0fSMartin Doucha * Sanitize and validate action commands.
24862baad0fSMartin Doucha *
24962baad0fSMartin Doucha * Add all allowed commands here.
25062baad0fSMartin Doucha *
25162baad0fSMartin Doucha * @author Andreas Gohr <andi@splitbrain.org>
25242ea7f44SGerrit Uitslag *
25342ea7f44SGerrit Uitslag * @param array|string $act
25442ea7f44SGerrit Uitslag * @return string
25562baad0fSMartin Doucha */
25662baad0fSMartin Douchafunction act_validate($act) {
257daf0cdbaSMartin Doucha    global $conf;
258daf0cdbaSMartin Doucha    global $INFO;
259daf0cdbaSMartin Doucha
26062baad0fSMartin Doucha    $act = act_clean($act);
261396c218fSAndreas Gohr
262409d7af7SAndreas Gohr    // check if action is disabled
263409d7af7SAndreas Gohr    if(!actionOK($act)){
264409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
265409d7af7SAndreas Gohr        return 'show';
266409d7af7SAndreas Gohr    }
267409d7af7SAndreas Gohr
26860e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
26960e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2701246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
2712a7abf2dSChristopher Smith                    'resendpwd','profile_delete'))){
27260e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
27360e6b550SAndreas Gohr        return 'show';
27460e6b550SAndreas Gohr    }
27560e6b550SAndreas Gohr
276c828a5d6SAndreas Gohr    //is there really a draft?
277c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
278c828a5d6SAndreas Gohr
279067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
280ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2811246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2822a7abf2dSChristopher Smith                    'unsubscribe','profile','profile_delete','resendpwd','recover',
283d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
284ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
285af182434Sandi        return 'show';
286af182434Sandi    }
287af182434Sandi    return $act;
288af182434Sandi}
289af182434Sandi
290af182434Sandi/**
2916b13307fSandi * Run permissionchecks
2926b13307fSandi *
2936b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
29442ea7f44SGerrit Uitslag *
29542ea7f44SGerrit Uitslag * @param string $act action command
29642ea7f44SGerrit Uitslag * @return string action command
2976b13307fSandi */
2986b13307fSandifunction act_permcheck($act){
299dbbc6aa7Sandi    global $INFO;
300dbbc6aa7Sandi
301ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
3026b13307fSandi        if($INFO['exists']){
303bdbc16bfSandi            if($act == 'edit'){
304bdbc16bfSandi                //the edit function will check again and do a source show
305bdbc16bfSandi                //when no AUTH_EDIT available
306bdbc16bfSandi                $permneed = AUTH_READ;
307bdbc16bfSandi            }else{
3086b13307fSandi                $permneed = AUTH_EDIT;
309bdbc16bfSandi            }
3106b13307fSandi        }else{
3116b13307fSandi            $permneed = AUTH_CREATE;
3126b13307fSandi        }
3132a7abf2dSChristopher Smith    }elseif(in_array($act,array('login','search','recent','profile','profile_delete','index', 'sitemap'))){
3146b13307fSandi        $permneed = AUTH_NONE;
3151246e016SAndreas Gohr    }elseif($act == 'revert'){
3161246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
3171246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
3185e199953Smatthiasgrimm    }elseif($act == 'register'){
3195e199953Smatthiasgrimm        $permneed = AUTH_NONE;
320ebd3d9ceSchris    }elseif($act == 'resendpwd'){
321ebd3d9ceSchris        $permneed = AUTH_NONE;
322c19fe9c0Sandi    }elseif($act == 'admin'){
323f8cc712eSAndreas Gohr        if($INFO['ismanager']){
324f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
325f8cc712eSAndreas Gohr            // action is checked later
326f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
327f8cc712eSAndreas Gohr        }else{
328c19fe9c0Sandi            $permneed = AUTH_ADMIN;
329f8cc712eSAndreas Gohr        }
3306b13307fSandi    }else{
3316b13307fSandi        $permneed = AUTH_READ;
3326b13307fSandi    }
333dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
334dbbc6aa7Sandi
3356b13307fSandi    return 'denied';
3366b13307fSandi}
3376b13307fSandi
3386b13307fSandi/**
339ee4c4a1bSAndreas Gohr * Handle 'draftdel'
340ee4c4a1bSAndreas Gohr *
341ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
34242ea7f44SGerrit Uitslag *
34342ea7f44SGerrit Uitslag * @param string $act action command
34442ea7f44SGerrit Uitslag * @return string action command
345ee4c4a1bSAndreas Gohr */
346ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
347ee4c4a1bSAndreas Gohr    global $INFO;
348ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
349ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
350ee4c4a1bSAndreas Gohr    return 'show';
351ee4c4a1bSAndreas Gohr}
352ee4c4a1bSAndreas Gohr
353ee4c4a1bSAndreas Gohr/**
354ee4c4a1bSAndreas Gohr * Saves a draft on preview
355ee4c4a1bSAndreas Gohr *
356ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
35742ea7f44SGerrit Uitslag *
35842ea7f44SGerrit Uitslag * @param string $act action command
35942ea7f44SGerrit Uitslag * @return string action command
360ee4c4a1bSAndreas Gohr */
361ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
362ee4c4a1bSAndreas Gohr    global $INFO;
363ee4c4a1bSAndreas Gohr    global $ID;
36490f1b7bdSTom N Harris    global $INPUT;
365ee4c4a1bSAndreas Gohr    global $conf;
36690f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
367ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
36890f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
36990f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
37090f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
37190f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
372ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
373ee4c4a1bSAndreas Gohr                );
374ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
375ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
376ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
377ee4c4a1bSAndreas Gohr        }
378ee4c4a1bSAndreas Gohr    }
379ee4c4a1bSAndreas Gohr    return $act;
380ee4c4a1bSAndreas Gohr}
381ee4c4a1bSAndreas Gohr
382ee4c4a1bSAndreas Gohr/**
3836b13307fSandi * Handle 'save'
3846b13307fSandi *
3856b13307fSandi * Checks for spam and conflicts and saves the page.
3866b13307fSandi * Does a redirect to show the page afterwards or
3876b13307fSandi * returns a new action.
3886b13307fSandi *
3896b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
39042ea7f44SGerrit Uitslag *
39142ea7f44SGerrit Uitslag * @param string $act action command
39242ea7f44SGerrit Uitslag * @return string action command
3936b13307fSandi */
3946b13307fSandifunction act_save($act){
3956b13307fSandi    global $ID;
3966b13307fSandi    global $DATE;
3976b13307fSandi    global $PRE;
3986b13307fSandi    global $TEXT;
3996b13307fSandi    global $SUF;
4006b13307fSandi    global $SUM;
4015a932e77SAdrian Lang    global $lang;
4028d67c48aSAdrian Lang    global $INFO;
40390f1b7bdSTom N Harris    global $INPUT;
4046b13307fSandi
4056b13307fSandi    //spam check
4065a932e77SAdrian Lang    if(checkwordblock()) {
4075a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4085a932e77SAdrian Lang        return 'edit';
4095a932e77SAdrian Lang    }
4108d67c48aSAdrian Lang    //conflict check
4118d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
4126b13307fSandi        return 'conflict';
4136b13307fSandi
4146b13307fSandi    //save it
415e0c26282SGerrit Uitslag    saveWikiText($ID,con($PRE,$TEXT,$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con
4166b13307fSandi    //unlock it
4176b13307fSandi    unlock($ID);
4186b13307fSandi
419ee4c4a1bSAndreas Gohr    //delete draft
420ee4c4a1bSAndreas Gohr    act_draftdel($act);
42169cd1e27SAndreas Gohr    session_write_close();
422ee4c4a1bSAndreas Gohr
42369cd1e27SAndreas Gohr    // when done, show page
42469cd1e27SAndreas Gohr    return 'show';
42569cd1e27SAndreas Gohr}
426f951a474SAndreas Gohr
42714a122deSAndreas Gohr/**
4281246e016SAndreas Gohr * Revert to a certain revision
4291246e016SAndreas Gohr *
4301246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
43142ea7f44SGerrit Uitslag *
43242ea7f44SGerrit Uitslag * @param string $act action command
43342ea7f44SGerrit Uitslag * @return string action command
4341246e016SAndreas Gohr */
4351246e016SAndreas Gohrfunction act_revert($act){
4361246e016SAndreas Gohr    global $ID;
4371246e016SAndreas Gohr    global $REV;
4381246e016SAndreas Gohr    global $lang;
439585bf44eSChristopher Smith    /* @var Input $INPUT */
440585bf44eSChristopher Smith    global $INPUT;
441de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
442de4d479aSAdrian Lang    // global $INFO;
443de4d479aSAdrian Lang    // if (!$INFO['writable']) {
444de4d479aSAdrian Lang    //     return 'show';
445de4d479aSAdrian Lang    // }
4461246e016SAndreas Gohr
4471246e016SAndreas Gohr    // when no revision is given, delete current one
4481246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
4491246e016SAndreas Gohr    $text = '';
4501246e016SAndreas Gohr    $sum  = $lang['deleted'];
4511246e016SAndreas Gohr    if($REV){
4521246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
4531246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
454d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
4551246e016SAndreas Gohr    }
4561246e016SAndreas Gohr
4571246e016SAndreas Gohr    // spam check
4585a932e77SAdrian Lang
4595a932e77SAdrian Lang    if (checkwordblock($text)) {
4605a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4615a932e77SAdrian Lang        return 'edit';
4625a932e77SAdrian Lang    }
4631246e016SAndreas Gohr
4641246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
4651246e016SAndreas Gohr    msg($sum,1);
4661246e016SAndreas Gohr
4671246e016SAndreas Gohr    //delete any draft
4681246e016SAndreas Gohr    act_draftdel($act);
4691246e016SAndreas Gohr    session_write_close();
4701246e016SAndreas Gohr
4711246e016SAndreas Gohr    // when done, show current page
472585bf44eSChristopher Smith    $INPUT->server->set('REQUEST_METHOD','post'); //should force a redirect
4731246e016SAndreas Gohr    $REV = '';
4741246e016SAndreas Gohr    return 'show';
4751246e016SAndreas Gohr}
4761246e016SAndreas Gohr
4771246e016SAndreas Gohr/**
47814a122deSAndreas Gohr * Do a redirect after receiving post data
47914a122deSAndreas Gohr *
48014a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
48142ea7f44SGerrit Uitslag *
48242ea7f44SGerrit Uitslag * @param string $id page id
48342ea7f44SGerrit Uitslag * @param string $preact action command before redirect
48414a122deSAndreas Gohr */
48569cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
48669cd1e27SAndreas Gohr    global $PRE;
48769cd1e27SAndreas Gohr    global $TEXT;
488f951a474SAndreas Gohr
48969cd1e27SAndreas Gohr    $opts = array(
49069cd1e27SAndreas Gohr            'id'       => $id,
49169cd1e27SAndreas Gohr            'preact'   => $preact
49269cd1e27SAndreas Gohr            );
493c66972f2SAdrian Lang    //get section name when coming from section edit
494c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
495c66972f2SAdrian Lang        $check = false; //Byref
496c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
497c66972f2SAdrian Lang    }
498c66972f2SAdrian Lang
49969cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
50069cd1e27SAndreas Gohr}
50169cd1e27SAndreas Gohr
502c8b076b1SMichael Hamann/**
503c8b076b1SMichael Hamann * Execute the redirect
504c8b076b1SMichael Hamann *
50542ea7f44SGerrit Uitslag * @param array $opts id and fragment for the redirect and the preact
506c8b076b1SMichael Hamann */
50769cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
50869cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
509c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
51069cd1e27SAndreas Gohr
5116b13307fSandi    //show it
512af2408d5SAndreas Gohr    send_redirect($go);
5136b13307fSandi}
5146b13307fSandi
5156b13307fSandi/**
516b8957367SBenjamin Gilbert * Handle 'login', 'logout'
5176b13307fSandi *
5186b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
51942ea7f44SGerrit Uitslag *
52042ea7f44SGerrit Uitslag * @param string $act action command
52142ea7f44SGerrit Uitslag * @return string action command
5226b13307fSandi */
5236b13307fSandifunction act_auth($act){
52408eda5bcSmatthiasgrimm    global $ID;
5257cace34dSAndreas Gohr    global $INFO;
526585bf44eSChristopher Smith    /* @var Input $INPUT */
527585bf44eSChristopher Smith    global $INPUT;
52808eda5bcSmatthiasgrimm
5296b13307fSandi    //already logged in?
530585bf44eSChristopher Smith    if($INPUT->server->has('REMOTE_USER') && $act=='login'){
531ca12ce46SAndreas Gohr        return 'show';
5322288dc06SGuy Brand    }
5336b13307fSandi
5346b13307fSandi    //handle logout
5356b13307fSandi    if($act=='logout'){
53608eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
537585bf44eSChristopher Smith        if($lockedby == $INPUT->server->str('REMOTE_USER')){
53808eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
539585bf44eSChristopher Smith        }
54008eda5bcSmatthiasgrimm
5417cace34dSAndreas Gohr        // do the logout stuff
5426b13307fSandi        auth_logoff();
5437cace34dSAndreas Gohr
5447cace34dSAndreas Gohr        // rebuild info array
5457cace34dSAndreas Gohr        $INFO = pageinfo();
5467cace34dSAndreas Gohr
547e16eccb7SGuy Brand        act_redirect($ID,'login');
5486b13307fSandi    }
5496b13307fSandi
5506b13307fSandi    return $act;
5516b13307fSandi}
5526b13307fSandi
5536b13307fSandi/**
55445a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
5556b13307fSandi *
5566b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
55742ea7f44SGerrit Uitslag *
55842ea7f44SGerrit Uitslag * @param string $act action command
55942ea7f44SGerrit Uitslag * @return string action command
5606b13307fSandi */
5616b13307fSandifunction act_edit($act){
562cd409024Sjorda    global $ID;
563ee4c4a1bSAndreas Gohr    global $INFO;
564cd409024Sjorda
56545a99335SAdrian Lang    global $TEXT;
56645a99335SAdrian Lang    global $RANGE;
56745a99335SAdrian Lang    global $PRE;
56845a99335SAdrian Lang    global $SUF;
56945a99335SAdrian Lang    global $REV;
57045a99335SAdrian Lang    global $SUM;
57145a99335SAdrian Lang    global $lang;
57245a99335SAdrian Lang    global $DATE;
57345a99335SAdrian Lang
57445a99335SAdrian Lang    if (!isset($TEXT)) {
57545a99335SAdrian Lang        if ($INFO['exists']) {
57645a99335SAdrian Lang            if ($RANGE) {
57745a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
57845a99335SAdrian Lang            } else {
57945a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
58045a99335SAdrian Lang            }
58145a99335SAdrian Lang        } else {
582fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
58345a99335SAdrian Lang        }
58445a99335SAdrian Lang    }
58545a99335SAdrian Lang
58645a99335SAdrian Lang    //set summary default
58745a99335SAdrian Lang    if(!$SUM){
58845a99335SAdrian Lang        if($REV){
5897656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
59045a99335SAdrian Lang        }elseif(!$INFO['exists']){
59145a99335SAdrian Lang            $SUM = $lang['created'];
59245a99335SAdrian Lang        }
59345a99335SAdrian Lang    }
59445a99335SAdrian Lang
5958d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5968d67c48aSAdrian Lang    // This is used for conflict detection
59778035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
59845a99335SAdrian Lang
5996b13307fSandi    //check if locked by anyone - if not lock for my self
60031bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
60131bc8f11SMichael Hamann    if ($INFO['writable']) {
6026b13307fSandi        $lockedby = checklock($ID);
6036b13307fSandi        if($lockedby) return 'locked';
6046b13307fSandi
6056b13307fSandi        lock($ID);
60631bc8f11SMichael Hamann    }
60731bc8f11SMichael Hamann
6086b13307fSandi    return $act;
6096b13307fSandi}
6106b13307fSandi
6116b13307fSandi/**
612f6dad9fdSMichael Klier * Export a wiki page for various formats
613f6dad9fdSMichael Klier *
614f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
615f6dad9fdSMichael Klier *
616f6dad9fdSMichael Klier *  Event data:
617f6dad9fdSMichael Klier *    data['id']      -- page id
618f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
619f6dad9fdSMichael Klier *    data['headers'] -- export headers
620f6dad9fdSMichael Klier *    data['output']  -- export output
6216b13307fSandi *
6226b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
623f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
62442ea7f44SGerrit Uitslag *
62542ea7f44SGerrit Uitslag * @param string $act action command
62642ea7f44SGerrit Uitslag * @return string action command
6276b13307fSandi */
6286b13307fSandifunction act_export($act){
6296b13307fSandi    global $ID;
6306b13307fSandi    global $REV;
63185f8705cSAnika Henke    global $conf;
63285f8705cSAnika Henke    global $lang;
6336b13307fSandi
634f6dad9fdSMichael Klier    $pre = '';
635f6dad9fdSMichael Klier    $post = '';
636f6dad9fdSMichael Klier    $headers = array();
637cc2ae802SAndreas Gohr
638f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
639f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
640f6dad9fdSMichael Klier
641ac83b9d8Sandi    $mode = substr($act,7);
642f6dad9fdSMichael Klier    switch($mode) {
643f6dad9fdSMichael Klier        case 'raw':
6445adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
64566b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
646f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
647f6dad9fdSMichael Klier            break;
648f6dad9fdSMichael Klier        case 'xhtml':
649c8839c22SAnika Henke            $pre .= '<!DOCTYPE html>' . DOKU_LF;
650c8839c22SAnika Henke            $pre .= '<html lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
651f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
652c8839c22SAnika Henke            $pre .= '  <meta charset="utf-8" />' . DOKU_LF;
653f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
654f6dad9fdSMichael Klier
655f6dad9fdSMichael Klier            // get metaheaders
656f6dad9fdSMichael Klier            ob_start();
657f6dad9fdSMichael Klier            tpl_metaheaders();
658f6dad9fdSMichael Klier            $pre .= ob_get_clean();
659f6dad9fdSMichael Klier
660f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
661f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
662f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
663f6dad9fdSMichael Klier
664f6dad9fdSMichael Klier            // get toc
665f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
666f6dad9fdSMichael Klier
667f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
668f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
669f6dad9fdSMichael Klier
670f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
671f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
672f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
673f6dad9fdSMichael Klier            break;
674f6dad9fdSMichael Klier        case 'xhtmlbody':
675f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
676f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
677f6dad9fdSMichael Klier            break;
678f6dad9fdSMichael Klier        default:
679f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
6809acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
681f6dad9fdSMichael Klier            break;
682f6dad9fdSMichael Klier    }
683f6dad9fdSMichael Klier
684f6dad9fdSMichael Klier    // prepare event data
685f6dad9fdSMichael Klier    $data = array();
686f6dad9fdSMichael Klier    $data['id'] = $ID;
687f6dad9fdSMichael Klier    $data['mode'] = $mode;
688f6dad9fdSMichael Klier    $data['headers'] = $headers;
689f6dad9fdSMichael Klier    $data['output'] =& $output;
690f6dad9fdSMichael Klier
691f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
692f6dad9fdSMichael Klier
693f6dad9fdSMichael Klier    if(!empty($data['output'])){
694f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
69585767031SAndreas Gohr            header("$key: $val");
69685767031SAndreas Gohr        }
697f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6986b13307fSandi        exit;
6996b13307fSandi    }
7006b13307fSandi    return 'show';
7016b13307fSandi}
702340756e4Sandi
703b158d625SSteven Danz/**
704c4f79b71SMichael Hamann * Handle sitemap delivery
705c4f79b71SMichael Hamann *
706c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
70742ea7f44SGerrit Uitslag *
70842ea7f44SGerrit Uitslag * @param string $act action command
709c4f79b71SMichael Hamann */
710c4f79b71SMichael Hamannfunction act_sitemap($act) {
711c4f79b71SMichael Hamann    global $conf;
712c4f79b71SMichael Hamann
713eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
7149d2e1be6SAndreas Gohr        http_status(404);
715c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
716c4f79b71SMichael Hamann        exit;
717c4f79b71SMichael Hamann    }
718c4f79b71SMichael Hamann
719eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
72065f6e7d6SMichael Hamann    if (Sitemapper::sitemapIsCompressed()) {
721c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
722c4f79b71SMichael Hamann    }else{
723c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
724c4f79b71SMichael Hamann    }
725c4f79b71SMichael Hamann
726c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
727c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
7282897eb23SMichael Hamann        Sitemapper::generate();
729c4f79b71SMichael Hamann    }
730c4f79b71SMichael Hamann
731c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
732c4f79b71SMichael Hamann        // Send headers
733c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
7343009a773SAndreas Gohr        header('Content-Disposition: attachment; filename='.utf8_basename($sitemap));
735c4f79b71SMichael Hamann
736eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
737eae17177SMichael Hamann
738c4f79b71SMichael Hamann        // Send file
739c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
74040e0b444SDominik Eckelmann        http_sendfile($sitemap);
741c4f79b71SMichael Hamann
742eae17177SMichael Hamann        readfile($sitemap);
743c4f79b71SMichael Hamann        exit;
744c4f79b71SMichael Hamann    }
745c4f79b71SMichael Hamann
7469d2e1be6SAndreas Gohr    http_status(500);
747eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
748c4f79b71SMichael Hamann    exit;
749c4f79b71SMichael Hamann}
750c4f79b71SMichael Hamann
751c4f79b71SMichael Hamann/**
7525b75cd1fSAdrian Lang * Handle page 'subscribe'
753b158d625SSteven Danz *
7545b75cd1fSAdrian Lang * Throws exception on error.
7555b75cd1fSAdrian Lang *
7565b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
75742ea7f44SGerrit Uitslag *
75842ea7f44SGerrit Uitslag * @param string $act action command
75942ea7f44SGerrit Uitslag * @return string action command
76042ea7f44SGerrit Uitslag * @throws Exception if (un)subscribing fails
761b158d625SSteven Danz */
7621380fc45SAndreas Gohrfunction act_subscription($act){
763056c2049SAndreas Gohr    global $lang;
764056c2049SAndreas Gohr    global $INFO;
765056c2049SAndreas Gohr    global $ID;
766585bf44eSChristopher Smith    /* @var Input $INPUT */
76790f1b7bdSTom N Harris    global $INPUT;
76852b0dd67SGuy Brand
7699fa341d0SAndreas Gohr    // subcriptions work for logged in users only
770585bf44eSChristopher Smith    if(!$INPUT->server->str('REMOTE_USER')) return 'show';
7719fa341d0SAndreas Gohr
772056c2049SAndreas Gohr    // get and preprocess data.
7738881fcc9SAdrian Lang    $params = array();
7748881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
77590f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
77690f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
7778881fcc9SAdrian Lang        }
7788881fcc9SAdrian Lang    }
7798881fcc9SAdrian Lang
780056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
7810e80bb5eSChristopher Smith    if(empty($params['action']) || !checkSecurityToken()) return $act;
782056c2049SAndreas Gohr
7838881fcc9SAdrian Lang    // Handle POST data, may throw exception.
7848881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7858881fcc9SAdrian Lang
7868881fcc9SAdrian Lang    $target = $params['target'];
7878881fcc9SAdrian Lang    $style  = $params['style'];
7888881fcc9SAdrian Lang    $action = $params['action'];
7898881fcc9SAdrian Lang
7908881fcc9SAdrian Lang    // Perform action.
791a0519fdaSAndreas Gohr    $sub = new Subscription();
792a0519fdaSAndreas Gohr    if($action == 'unsubscribe'){
793585bf44eSChristopher Smith        $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
794a0519fdaSAndreas Gohr    }else{
795585bf44eSChristopher Smith        $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
796a0519fdaSAndreas Gohr    }
797a0519fdaSAndreas Gohr
798a0519fdaSAndreas Gohr    if($ok) {
799a0519fdaSAndreas Gohr        msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
800a0519fdaSAndreas Gohr                    prettyprint_id($target)), 1);
801a0519fdaSAndreas Gohr        act_redirect($ID, $act);
802a0519fdaSAndreas Gohr    } else {
8038881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
8048881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
8058881fcc9SAdrian Lang                                    prettyprint_id($target)));
8068881fcc9SAdrian Lang    }
807cb3f9dbaSAdrian Lang
808cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
809a0519fdaSAndreas Gohr    $INFO['subscribed'] = $sub->user_subscription();
810cb3f9dbaSAdrian Lang    return 'show';
8118881fcc9SAdrian Lang}
8128881fcc9SAdrian Lang
8138881fcc9SAdrian Lang/**
8148881fcc9SAdrian Lang * Validate POST data
8158881fcc9SAdrian Lang *
8168881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
8178881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
8188881fcc9SAdrian Lang *
8198881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
82042ea7f44SGerrit Uitslag *
82142ea7f44SGerrit Uitslag * @param array &$params the parameters: target, style and action
82242ea7f44SGerrit Uitslag * @throws Exception
8238881fcc9SAdrian Lang */
8247a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
8258881fcc9SAdrian Lang    global $INFO;
8268881fcc9SAdrian Lang    global $lang;
827585bf44eSChristopher Smith    /* @var Input $INPUT */
828585bf44eSChristopher Smith    global $INPUT;
8298881fcc9SAdrian Lang
8305b75cd1fSAdrian Lang    // Get and validate parameters.
8318881fcc9SAdrian Lang    if (!isset($params['target'])) {
83215741132SAndreas Gohr        throw new Exception('no subscription target given');
8335b75cd1fSAdrian Lang    }
8348881fcc9SAdrian Lang    $target = $params['target'];
8355b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
8365b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
8375b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
8385b75cd1fSAdrian Lang        $valid_styles[] = 'list';
8395b75cd1fSAdrian Lang    }
8408881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
84115741132SAndreas Gohr                              'invalid subscription style given');
8428881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
84315741132SAndreas Gohr                              $params, 'invalid subscription action given');
844613964ecSGuy Brand
8455b75cd1fSAdrian Lang    // Check other conditions.
8465b75cd1fSAdrian Lang    if ($action === 'subscribe') {
8475b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
8485b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
84952b0dd67SGuy Brand        }
8505b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
8515b75cd1fSAdrian Lang        $is = false;
8525b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
8535b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
8545b75cd1fSAdrian Lang                $is = true;
85552b0dd67SGuy Brand            }
85652b0dd67SGuy Brand        }
8575b75cd1fSAdrian Lang        if ($is === false) {
85815741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
859585bf44eSChristopher Smith                                        $INPUT->server->str('REMOTE_USER'),
8605b75cd1fSAdrian Lang                                        prettyprint_id($target)));
8615b75cd1fSAdrian Lang        }
8625b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
8635b75cd1fSAdrian Lang        $style = null;
86452b0dd67SGuy Brand    }
86552b0dd67SGuy Brand
86616c665d9SAndreas Gohr    $params = compact('target', 'style', 'action');
86752b0dd67SGuy Brand}
86852b0dd67SGuy Brand
869e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
870