xref: /dokuwiki/inc/actions.php (revision 42ea7f447f39fbc2f79eaaec31f8c10ede59c5d0)
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?
203f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
204746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
205f63a2007Schris
2065a892029SAndreas Gohr    include(template('main.php'));
207c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
208c19fe9c0Sandi    // in function tpl_content()
2096b13307fSandi}
2106b13307fSandi
211c8b076b1SMichael Hamann/**
212c8b076b1SMichael Hamann * Send the given headers using header()
213c8b076b1SMichael Hamann *
214c8b076b1SMichael Hamann * @param array $headers The headers that shall be sent
215c8b076b1SMichael Hamann */
216f63a2007Schrisfunction act_sendheaders($headers) {
217f63a2007Schris    foreach ($headers as $hdr) header($hdr);
218f63a2007Schris}
219f63a2007Schris
2206b13307fSandi/**
221af182434Sandi * Sanitize the action command
222af182434Sandi *
223af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
224*42ea7f44SGerrit Uitslag *
225*42ea7f44SGerrit Uitslag * @param array|string $act
226*42ea7f44SGerrit Uitslag * @return string
227af182434Sandi */
228af182434Sandifunction act_clean($act){
229ee4c4a1bSAndreas Gohr    // check if the action was given as array key
230ee4c4a1bSAndreas Gohr    if(is_array($act)){
231ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
232ee4c4a1bSAndreas Gohr    }
233ee4c4a1bSAndreas Gohr
234ac83b9d8Sandi    //remove all bad chars
235ac83b9d8Sandi    $act = strtolower($act);
2362d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
237ac83b9d8Sandi
238ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
239cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
240b146b32bSandi
241396c218fSAndreas Gohr    if($act === '') $act = 'show';
24262baad0fSMartin Doucha    return $act;
24362baad0fSMartin Doucha}
24462baad0fSMartin Doucha
24562baad0fSMartin Doucha/**
24662baad0fSMartin Doucha * Sanitize and validate action commands.
24762baad0fSMartin Doucha *
24862baad0fSMartin Doucha * Add all allowed commands here.
24962baad0fSMartin Doucha *
25062baad0fSMartin Doucha * @author Andreas Gohr <andi@splitbrain.org>
251*42ea7f44SGerrit Uitslag *
252*42ea7f44SGerrit Uitslag * @param array|string $act
253*42ea7f44SGerrit Uitslag * @return string
25462baad0fSMartin Doucha */
25562baad0fSMartin Douchafunction act_validate($act) {
256daf0cdbaSMartin Doucha    global $conf;
257daf0cdbaSMartin Doucha    global $INFO;
258daf0cdbaSMartin Doucha
25962baad0fSMartin Doucha    $act = act_clean($act);
260396c218fSAndreas Gohr
261409d7af7SAndreas Gohr    // check if action is disabled
262409d7af7SAndreas Gohr    if(!actionOK($act)){
263409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
264409d7af7SAndreas Gohr        return 'show';
265409d7af7SAndreas Gohr    }
266409d7af7SAndreas Gohr
26760e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
26860e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2691246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
2702a7abf2dSChristopher Smith                    'resendpwd','profile_delete'))){
27160e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
27260e6b550SAndreas Gohr        return 'show';
27360e6b550SAndreas Gohr    }
27460e6b550SAndreas Gohr
275c828a5d6SAndreas Gohr    //is there really a draft?
276c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
277c828a5d6SAndreas Gohr
278067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
279ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2801246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2812a7abf2dSChristopher Smith                    'unsubscribe','profile','profile_delete','resendpwd','recover',
282d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
283ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
284af182434Sandi        return 'show';
285af182434Sandi    }
286af182434Sandi    return $act;
287af182434Sandi}
288af182434Sandi
289af182434Sandi/**
2906b13307fSandi * Run permissionchecks
2916b13307fSandi *
2926b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
293*42ea7f44SGerrit Uitslag *
294*42ea7f44SGerrit Uitslag * @param string $act action command
295*42ea7f44SGerrit Uitslag * @return string action command
2966b13307fSandi */
2976b13307fSandifunction act_permcheck($act){
298dbbc6aa7Sandi    global $INFO;
299dbbc6aa7Sandi
300ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
3016b13307fSandi        if($INFO['exists']){
302bdbc16bfSandi            if($act == 'edit'){
303bdbc16bfSandi                //the edit function will check again and do a source show
304bdbc16bfSandi                //when no AUTH_EDIT available
305bdbc16bfSandi                $permneed = AUTH_READ;
306bdbc16bfSandi            }else{
3076b13307fSandi                $permneed = AUTH_EDIT;
308bdbc16bfSandi            }
3096b13307fSandi        }else{
3106b13307fSandi            $permneed = AUTH_CREATE;
3116b13307fSandi        }
3122a7abf2dSChristopher Smith    }elseif(in_array($act,array('login','search','recent','profile','profile_delete','index', 'sitemap'))){
3136b13307fSandi        $permneed = AUTH_NONE;
3141246e016SAndreas Gohr    }elseif($act == 'revert'){
3151246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
3161246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
3175e199953Smatthiasgrimm    }elseif($act == 'register'){
3185e199953Smatthiasgrimm        $permneed = AUTH_NONE;
319ebd3d9ceSchris    }elseif($act == 'resendpwd'){
320ebd3d9ceSchris        $permneed = AUTH_NONE;
321c19fe9c0Sandi    }elseif($act == 'admin'){
322f8cc712eSAndreas Gohr        if($INFO['ismanager']){
323f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
324f8cc712eSAndreas Gohr            // action is checked later
325f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
326f8cc712eSAndreas Gohr        }else{
327c19fe9c0Sandi            $permneed = AUTH_ADMIN;
328f8cc712eSAndreas Gohr        }
3296b13307fSandi    }else{
3306b13307fSandi        $permneed = AUTH_READ;
3316b13307fSandi    }
332dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
333dbbc6aa7Sandi
3346b13307fSandi    return 'denied';
3356b13307fSandi}
3366b13307fSandi
3376b13307fSandi/**
338ee4c4a1bSAndreas Gohr * Handle 'draftdel'
339ee4c4a1bSAndreas Gohr *
340ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
341*42ea7f44SGerrit Uitslag *
342*42ea7f44SGerrit Uitslag * @param string $act action command
343*42ea7f44SGerrit Uitslag * @return string action command
344ee4c4a1bSAndreas Gohr */
345ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
346ee4c4a1bSAndreas Gohr    global $INFO;
347ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
348ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
349ee4c4a1bSAndreas Gohr    return 'show';
350ee4c4a1bSAndreas Gohr}
351ee4c4a1bSAndreas Gohr
352ee4c4a1bSAndreas Gohr/**
353ee4c4a1bSAndreas Gohr * Saves a draft on preview
354ee4c4a1bSAndreas Gohr *
355ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
356*42ea7f44SGerrit Uitslag *
357*42ea7f44SGerrit Uitslag * @param string $act action command
358*42ea7f44SGerrit Uitslag * @return string action command
359ee4c4a1bSAndreas Gohr */
360ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
361ee4c4a1bSAndreas Gohr    global $INFO;
362ee4c4a1bSAndreas Gohr    global $ID;
36390f1b7bdSTom N Harris    global $INPUT;
364ee4c4a1bSAndreas Gohr    global $conf;
36590f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
366ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
36790f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
36890f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
36990f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
37090f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
371ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
372ee4c4a1bSAndreas Gohr                );
373ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
374ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
375ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
376ee4c4a1bSAndreas Gohr        }
377ee4c4a1bSAndreas Gohr    }
378ee4c4a1bSAndreas Gohr    return $act;
379ee4c4a1bSAndreas Gohr}
380ee4c4a1bSAndreas Gohr
381ee4c4a1bSAndreas Gohr/**
3826b13307fSandi * Handle 'save'
3836b13307fSandi *
3846b13307fSandi * Checks for spam and conflicts and saves the page.
3856b13307fSandi * Does a redirect to show the page afterwards or
3866b13307fSandi * returns a new action.
3876b13307fSandi *
3886b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
389*42ea7f44SGerrit Uitslag *
390*42ea7f44SGerrit Uitslag * @param string $act action command
391*42ea7f44SGerrit Uitslag * @return string action command
3926b13307fSandi */
3936b13307fSandifunction act_save($act){
3946b13307fSandi    global $ID;
3956b13307fSandi    global $DATE;
3966b13307fSandi    global $PRE;
3976b13307fSandi    global $TEXT;
3986b13307fSandi    global $SUF;
3996b13307fSandi    global $SUM;
4005a932e77SAdrian Lang    global $lang;
4018d67c48aSAdrian Lang    global $INFO;
40290f1b7bdSTom N Harris    global $INPUT;
4036b13307fSandi
4046b13307fSandi    //spam check
4055a932e77SAdrian Lang    if(checkwordblock()) {
4065a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4075a932e77SAdrian Lang        return 'edit';
4085a932e77SAdrian Lang    }
4098d67c48aSAdrian Lang    //conflict check
4108d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
4116b13307fSandi        return 'conflict';
4126b13307fSandi
4136b13307fSandi    //save it
414e0c26282SGerrit Uitslag    saveWikiText($ID,con($PRE,$TEXT,$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con
4156b13307fSandi    //unlock it
4166b13307fSandi    unlock($ID);
4176b13307fSandi
418ee4c4a1bSAndreas Gohr    //delete draft
419ee4c4a1bSAndreas Gohr    act_draftdel($act);
42069cd1e27SAndreas Gohr    session_write_close();
421ee4c4a1bSAndreas Gohr
42269cd1e27SAndreas Gohr    // when done, show page
42369cd1e27SAndreas Gohr    return 'show';
42469cd1e27SAndreas Gohr}
425f951a474SAndreas Gohr
42614a122deSAndreas Gohr/**
4271246e016SAndreas Gohr * Revert to a certain revision
4281246e016SAndreas Gohr *
4291246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
430*42ea7f44SGerrit Uitslag *
431*42ea7f44SGerrit Uitslag * @param string $act action command
432*42ea7f44SGerrit Uitslag * @return string action command
4331246e016SAndreas Gohr */
4341246e016SAndreas Gohrfunction act_revert($act){
4351246e016SAndreas Gohr    global $ID;
4361246e016SAndreas Gohr    global $REV;
4371246e016SAndreas Gohr    global $lang;
438585bf44eSChristopher Smith    /* @var Input $INPUT */
439585bf44eSChristopher Smith    global $INPUT;
440de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
441de4d479aSAdrian Lang    // global $INFO;
442de4d479aSAdrian Lang    // if (!$INFO['writable']) {
443de4d479aSAdrian Lang    //     return 'show';
444de4d479aSAdrian Lang    // }
4451246e016SAndreas Gohr
4461246e016SAndreas Gohr    // when no revision is given, delete current one
4471246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
4481246e016SAndreas Gohr    $text = '';
4491246e016SAndreas Gohr    $sum  = $lang['deleted'];
4501246e016SAndreas Gohr    if($REV){
4511246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
4521246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
453d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
4541246e016SAndreas Gohr    }
4551246e016SAndreas Gohr
4561246e016SAndreas Gohr    // spam check
4575a932e77SAdrian Lang
4585a932e77SAdrian Lang    if (checkwordblock($text)) {
4595a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4605a932e77SAdrian Lang        return 'edit';
4615a932e77SAdrian Lang    }
4621246e016SAndreas Gohr
4631246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
4641246e016SAndreas Gohr    msg($sum,1);
4651246e016SAndreas Gohr
4661246e016SAndreas Gohr    //delete any draft
4671246e016SAndreas Gohr    act_draftdel($act);
4681246e016SAndreas Gohr    session_write_close();
4691246e016SAndreas Gohr
4701246e016SAndreas Gohr    // when done, show current page
471585bf44eSChristopher Smith    $INPUT->server->set('REQUEST_METHOD','post'); //should force a redirect
4721246e016SAndreas Gohr    $REV = '';
4731246e016SAndreas Gohr    return 'show';
4741246e016SAndreas Gohr}
4751246e016SAndreas Gohr
4761246e016SAndreas Gohr/**
47714a122deSAndreas Gohr * Do a redirect after receiving post data
47814a122deSAndreas Gohr *
47914a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
480*42ea7f44SGerrit Uitslag *
481*42ea7f44SGerrit Uitslag * @param string $id page id
482*42ea7f44SGerrit Uitslag * @param string $preact action command before redirect
48314a122deSAndreas Gohr */
48469cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
48569cd1e27SAndreas Gohr    global $PRE;
48669cd1e27SAndreas Gohr    global $TEXT;
487f951a474SAndreas Gohr
48869cd1e27SAndreas Gohr    $opts = array(
48969cd1e27SAndreas Gohr            'id'       => $id,
49069cd1e27SAndreas Gohr            'preact'   => $preact
49169cd1e27SAndreas Gohr            );
492c66972f2SAdrian Lang    //get section name when coming from section edit
493c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
494c66972f2SAdrian Lang        $check = false; //Byref
495c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
496c66972f2SAdrian Lang    }
497c66972f2SAdrian Lang
49869cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
49969cd1e27SAndreas Gohr}
50069cd1e27SAndreas Gohr
501c8b076b1SMichael Hamann/**
502c8b076b1SMichael Hamann * Execute the redirect
503c8b076b1SMichael Hamann *
504*42ea7f44SGerrit Uitslag * @param array $opts id and fragment for the redirect and the preact
505c8b076b1SMichael Hamann */
50669cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
50769cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
508c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
50969cd1e27SAndreas Gohr
5106b13307fSandi    //show it
511af2408d5SAndreas Gohr    send_redirect($go);
5126b13307fSandi}
5136b13307fSandi
5146b13307fSandi/**
515b8957367SBenjamin Gilbert * Handle 'login', 'logout'
5166b13307fSandi *
5176b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
518*42ea7f44SGerrit Uitslag *
519*42ea7f44SGerrit Uitslag * @param string $act action command
520*42ea7f44SGerrit Uitslag * @return string action command
5216b13307fSandi */
5226b13307fSandifunction act_auth($act){
52308eda5bcSmatthiasgrimm    global $ID;
5247cace34dSAndreas Gohr    global $INFO;
525585bf44eSChristopher Smith    /* @var Input $INPUT */
526585bf44eSChristopher Smith    global $INPUT;
52708eda5bcSmatthiasgrimm
5286b13307fSandi    //already logged in?
529585bf44eSChristopher Smith    if($INPUT->server->has('REMOTE_USER') && $act=='login'){
530ca12ce46SAndreas Gohr        return 'show';
5312288dc06SGuy Brand    }
5326b13307fSandi
5336b13307fSandi    //handle logout
5346b13307fSandi    if($act=='logout'){
53508eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
536585bf44eSChristopher Smith        if($lockedby == $INPUT->server->str('REMOTE_USER')){
53708eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
538585bf44eSChristopher Smith        }
53908eda5bcSmatthiasgrimm
5407cace34dSAndreas Gohr        // do the logout stuff
5416b13307fSandi        auth_logoff();
5427cace34dSAndreas Gohr
5437cace34dSAndreas Gohr        // rebuild info array
5447cace34dSAndreas Gohr        $INFO = pageinfo();
5457cace34dSAndreas Gohr
546e16eccb7SGuy Brand        act_redirect($ID,'login');
5476b13307fSandi    }
5486b13307fSandi
5496b13307fSandi    return $act;
5506b13307fSandi}
5516b13307fSandi
5526b13307fSandi/**
55345a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
5546b13307fSandi *
5556b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
556*42ea7f44SGerrit Uitslag *
557*42ea7f44SGerrit Uitslag * @param string $act action command
558*42ea7f44SGerrit Uitslag * @return string action command
5596b13307fSandi */
5606b13307fSandifunction act_edit($act){
561cd409024Sjorda    global $ID;
562ee4c4a1bSAndreas Gohr    global $INFO;
563cd409024Sjorda
56445a99335SAdrian Lang    global $TEXT;
56545a99335SAdrian Lang    global $RANGE;
56645a99335SAdrian Lang    global $PRE;
56745a99335SAdrian Lang    global $SUF;
56845a99335SAdrian Lang    global $REV;
56945a99335SAdrian Lang    global $SUM;
57045a99335SAdrian Lang    global $lang;
57145a99335SAdrian Lang    global $DATE;
57245a99335SAdrian Lang
57345a99335SAdrian Lang    if (!isset($TEXT)) {
57445a99335SAdrian Lang        if ($INFO['exists']) {
57545a99335SAdrian Lang            if ($RANGE) {
57645a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
57745a99335SAdrian Lang            } else {
57845a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
57945a99335SAdrian Lang            }
58045a99335SAdrian Lang        } else {
581fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
58245a99335SAdrian Lang        }
58345a99335SAdrian Lang    }
58445a99335SAdrian Lang
58545a99335SAdrian Lang    //set summary default
58645a99335SAdrian Lang    if(!$SUM){
58745a99335SAdrian Lang        if($REV){
5887656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
58945a99335SAdrian Lang        }elseif(!$INFO['exists']){
59045a99335SAdrian Lang            $SUM = $lang['created'];
59145a99335SAdrian Lang        }
59245a99335SAdrian Lang    }
59345a99335SAdrian Lang
5948d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5958d67c48aSAdrian Lang    // This is used for conflict detection
59678035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
59745a99335SAdrian Lang
5986b13307fSandi    //check if locked by anyone - if not lock for my self
59931bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
60031bc8f11SMichael Hamann    if ($INFO['writable']) {
6016b13307fSandi        $lockedby = checklock($ID);
6026b13307fSandi        if($lockedby) return 'locked';
6036b13307fSandi
6046b13307fSandi        lock($ID);
60531bc8f11SMichael Hamann    }
60631bc8f11SMichael Hamann
6076b13307fSandi    return $act;
6086b13307fSandi}
6096b13307fSandi
6106b13307fSandi/**
611f6dad9fdSMichael Klier * Export a wiki page for various formats
612f6dad9fdSMichael Klier *
613f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
614f6dad9fdSMichael Klier *
615f6dad9fdSMichael Klier *  Event data:
616f6dad9fdSMichael Klier *    data['id']      -- page id
617f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
618f6dad9fdSMichael Klier *    data['headers'] -- export headers
619f6dad9fdSMichael Klier *    data['output']  -- export output
6206b13307fSandi *
6216b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
622f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
623*42ea7f44SGerrit Uitslag *
624*42ea7f44SGerrit Uitslag * @param string $act action command
625*42ea7f44SGerrit Uitslag * @return string action command
6266b13307fSandi */
6276b13307fSandifunction act_export($act){
6286b13307fSandi    global $ID;
6296b13307fSandi    global $REV;
63085f8705cSAnika Henke    global $conf;
63185f8705cSAnika Henke    global $lang;
6326b13307fSandi
633f6dad9fdSMichael Klier    $pre = '';
634f6dad9fdSMichael Klier    $post = '';
635f6dad9fdSMichael Klier    $headers = array();
636cc2ae802SAndreas Gohr
637f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
638f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
639f6dad9fdSMichael Klier
640ac83b9d8Sandi    $mode = substr($act,7);
641f6dad9fdSMichael Klier    switch($mode) {
642f6dad9fdSMichael Klier        case 'raw':
6435adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
64466b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
645f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
646f6dad9fdSMichael Klier            break;
647f6dad9fdSMichael Klier        case 'xhtml':
648c8839c22SAnika Henke            $pre .= '<!DOCTYPE html>' . DOKU_LF;
649c8839c22SAnika Henke            $pre .= '<html lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
650f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
651c8839c22SAnika Henke            $pre .= '  <meta charset="utf-8" />' . DOKU_LF;
652f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
653f6dad9fdSMichael Klier
654f6dad9fdSMichael Klier            // get metaheaders
655f6dad9fdSMichael Klier            ob_start();
656f6dad9fdSMichael Klier            tpl_metaheaders();
657f6dad9fdSMichael Klier            $pre .= ob_get_clean();
658f6dad9fdSMichael Klier
659f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
660f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
661f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
662f6dad9fdSMichael Klier
663f6dad9fdSMichael Klier            // get toc
664f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
665f6dad9fdSMichael Klier
666f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
667f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
668f6dad9fdSMichael Klier
669f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
670f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
671f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
672f6dad9fdSMichael Klier            break;
673f6dad9fdSMichael Klier        case 'xhtmlbody':
674f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
675f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
676f6dad9fdSMichael Klier            break;
677f6dad9fdSMichael Klier        default:
678f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
6799acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
680f6dad9fdSMichael Klier            break;
681f6dad9fdSMichael Klier    }
682f6dad9fdSMichael Klier
683f6dad9fdSMichael Klier    // prepare event data
684f6dad9fdSMichael Klier    $data = array();
685f6dad9fdSMichael Klier    $data['id'] = $ID;
686f6dad9fdSMichael Klier    $data['mode'] = $mode;
687f6dad9fdSMichael Klier    $data['headers'] = $headers;
688f6dad9fdSMichael Klier    $data['output'] =& $output;
689f6dad9fdSMichael Klier
690f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
691f6dad9fdSMichael Klier
692f6dad9fdSMichael Klier    if(!empty($data['output'])){
693f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
69485767031SAndreas Gohr            header("$key: $val");
69585767031SAndreas Gohr        }
696f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6976b13307fSandi        exit;
6986b13307fSandi    }
6996b13307fSandi    return 'show';
7006b13307fSandi}
701340756e4Sandi
702b158d625SSteven Danz/**
703c4f79b71SMichael Hamann * Handle sitemap delivery
704c4f79b71SMichael Hamann *
705c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
706*42ea7f44SGerrit Uitslag *
707*42ea7f44SGerrit Uitslag * @param string $act action command
708c4f79b71SMichael Hamann */
709c4f79b71SMichael Hamannfunction act_sitemap($act) {
710c4f79b71SMichael Hamann    global $conf;
711c4f79b71SMichael Hamann
712eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
7139d2e1be6SAndreas Gohr        http_status(404);
714c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
715c4f79b71SMichael Hamann        exit;
716c4f79b71SMichael Hamann    }
717c4f79b71SMichael Hamann
718eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
71965f6e7d6SMichael Hamann    if (Sitemapper::sitemapIsCompressed()) {
720c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
721c4f79b71SMichael Hamann    }else{
722c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
723c4f79b71SMichael Hamann    }
724c4f79b71SMichael Hamann
725c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
726c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
7272897eb23SMichael Hamann        Sitemapper::generate();
728c4f79b71SMichael Hamann    }
729c4f79b71SMichael Hamann
730c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
731c4f79b71SMichael Hamann        // Send headers
732c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
7333009a773SAndreas Gohr        header('Content-Disposition: attachment; filename='.utf8_basename($sitemap));
734c4f79b71SMichael Hamann
735eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
736eae17177SMichael Hamann
737c4f79b71SMichael Hamann        // Send file
738c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
73940e0b444SDominik Eckelmann        http_sendfile($sitemap);
740c4f79b71SMichael Hamann
741eae17177SMichael Hamann        readfile($sitemap);
742c4f79b71SMichael Hamann        exit;
743c4f79b71SMichael Hamann    }
744c4f79b71SMichael Hamann
7459d2e1be6SAndreas Gohr    http_status(500);
746eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
747c4f79b71SMichael Hamann    exit;
748c4f79b71SMichael Hamann}
749c4f79b71SMichael Hamann
750c4f79b71SMichael Hamann/**
7515b75cd1fSAdrian Lang * Handle page 'subscribe'
752b158d625SSteven Danz *
7535b75cd1fSAdrian Lang * Throws exception on error.
7545b75cd1fSAdrian Lang *
7555b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
756*42ea7f44SGerrit Uitslag *
757*42ea7f44SGerrit Uitslag * @param string $act action command
758*42ea7f44SGerrit Uitslag * @return string action command
759*42ea7f44SGerrit Uitslag * @throws Exception if (un)subscribing fails
760b158d625SSteven Danz */
7611380fc45SAndreas Gohrfunction act_subscription($act){
762056c2049SAndreas Gohr    global $lang;
763056c2049SAndreas Gohr    global $INFO;
764056c2049SAndreas Gohr    global $ID;
765585bf44eSChristopher Smith    /* @var Input $INPUT */
76690f1b7bdSTom N Harris    global $INPUT;
76752b0dd67SGuy Brand
7689fa341d0SAndreas Gohr    // subcriptions work for logged in users only
769585bf44eSChristopher Smith    if(!$INPUT->server->str('REMOTE_USER')) return 'show';
7709fa341d0SAndreas Gohr
771056c2049SAndreas Gohr    // get and preprocess data.
7728881fcc9SAdrian Lang    $params = array();
7738881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
77490f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
77590f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
7768881fcc9SAdrian Lang        }
7778881fcc9SAdrian Lang    }
7788881fcc9SAdrian Lang
779056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
7800e80bb5eSChristopher Smith    if(empty($params['action']) || !checkSecurityToken()) return $act;
781056c2049SAndreas Gohr
7828881fcc9SAdrian Lang    // Handle POST data, may throw exception.
7838881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7848881fcc9SAdrian Lang
7858881fcc9SAdrian Lang    $target = $params['target'];
7868881fcc9SAdrian Lang    $style  = $params['style'];
7878881fcc9SAdrian Lang    $action = $params['action'];
7888881fcc9SAdrian Lang
7898881fcc9SAdrian Lang    // Perform action.
790a0519fdaSAndreas Gohr    $sub = new Subscription();
791a0519fdaSAndreas Gohr    if($action == 'unsubscribe'){
792585bf44eSChristopher Smith        $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
793a0519fdaSAndreas Gohr    }else{
794585bf44eSChristopher Smith        $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
795a0519fdaSAndreas Gohr    }
796a0519fdaSAndreas Gohr
797a0519fdaSAndreas Gohr    if($ok) {
798a0519fdaSAndreas Gohr        msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
799a0519fdaSAndreas Gohr                    prettyprint_id($target)), 1);
800a0519fdaSAndreas Gohr        act_redirect($ID, $act);
801a0519fdaSAndreas Gohr    } else {
8028881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
8038881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
8048881fcc9SAdrian Lang                                    prettyprint_id($target)));
8058881fcc9SAdrian Lang    }
806cb3f9dbaSAdrian Lang
807cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
808a0519fdaSAndreas Gohr    $INFO['subscribed'] = $sub->user_subscription();
809cb3f9dbaSAdrian Lang    return 'show';
8108881fcc9SAdrian Lang}
8118881fcc9SAdrian Lang
8128881fcc9SAdrian Lang/**
8138881fcc9SAdrian Lang * Validate POST data
8148881fcc9SAdrian Lang *
8158881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
8168881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
8178881fcc9SAdrian Lang *
8188881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
819*42ea7f44SGerrit Uitslag *
820*42ea7f44SGerrit Uitslag * @param array &$params the parameters: target, style and action
821*42ea7f44SGerrit Uitslag * @throws Exception
8228881fcc9SAdrian Lang */
8237a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
8248881fcc9SAdrian Lang    global $INFO;
8258881fcc9SAdrian Lang    global $lang;
826585bf44eSChristopher Smith    /* @var Input $INPUT */
827585bf44eSChristopher Smith    global $INPUT;
8288881fcc9SAdrian Lang
8295b75cd1fSAdrian Lang    // Get and validate parameters.
8308881fcc9SAdrian Lang    if (!isset($params['target'])) {
83115741132SAndreas Gohr        throw new Exception('no subscription target given');
8325b75cd1fSAdrian Lang    }
8338881fcc9SAdrian Lang    $target = $params['target'];
8345b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
8355b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
8365b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
8375b75cd1fSAdrian Lang        $valid_styles[] = 'list';
8385b75cd1fSAdrian Lang    }
8398881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
84015741132SAndreas Gohr                              'invalid subscription style given');
8418881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
84215741132SAndreas Gohr                              $params, 'invalid subscription action given');
843613964ecSGuy Brand
8445b75cd1fSAdrian Lang    // Check other conditions.
8455b75cd1fSAdrian Lang    if ($action === 'subscribe') {
8465b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
8475b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
84852b0dd67SGuy Brand        }
8495b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
8505b75cd1fSAdrian Lang        $is = false;
8515b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
8525b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
8535b75cd1fSAdrian Lang                $is = true;
85452b0dd67SGuy Brand            }
85552b0dd67SGuy Brand        }
8565b75cd1fSAdrian Lang        if ($is === false) {
85715741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
858585bf44eSChristopher Smith                                        $INPUT->server->str('REMOTE_USER'),
8595b75cd1fSAdrian Lang                                        prettyprint_id($target)));
8605b75cd1fSAdrian Lang        }
8615b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
8625b75cd1fSAdrian Lang        $style = null;
86352b0dd67SGuy Brand    }
86452b0dd67SGuy Brand
86516c665d9SAndreas Gohr    $params = compact('target', 'style', 'action');
86652b0dd67SGuy Brand}
86752b0dd67SGuy Brand
868e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
869