xref: /dokuwiki/inc/actions.php (revision b814e978ddb3bb9f05058fb684e2394dd2bf8eb5)
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>
224af182434Sandi */
225af182434Sandifunction act_clean($act){
226ee4c4a1bSAndreas Gohr    // check if the action was given as array key
227ee4c4a1bSAndreas Gohr    if(is_array($act)){
228ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
229ee4c4a1bSAndreas Gohr    }
230ee4c4a1bSAndreas Gohr
231ac83b9d8Sandi    //remove all bad chars
232ac83b9d8Sandi    $act = strtolower($act);
2332d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
234ac83b9d8Sandi
235ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
236cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
237b146b32bSandi
238396c218fSAndreas Gohr    if($act === '') $act = 'show';
23962baad0fSMartin Doucha    return $act;
24062baad0fSMartin Doucha}
24162baad0fSMartin Doucha
24262baad0fSMartin Doucha/**
24362baad0fSMartin Doucha * Sanitize and validate action commands.
24462baad0fSMartin Doucha *
24562baad0fSMartin Doucha * Add all allowed commands here.
24662baad0fSMartin Doucha *
24762baad0fSMartin Doucha * @author Andreas Gohr <andi@splitbrain.org>
24862baad0fSMartin Doucha */
24962baad0fSMartin Douchafunction act_validate($act) {
250daf0cdbaSMartin Doucha    global $conf;
251daf0cdbaSMartin Doucha    global $INFO;
252daf0cdbaSMartin Doucha
25362baad0fSMartin Doucha    $act = act_clean($act);
254396c218fSAndreas Gohr
255409d7af7SAndreas Gohr    // check if action is disabled
256409d7af7SAndreas Gohr    if(!actionOK($act)){
257409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
258409d7af7SAndreas Gohr        return 'show';
259409d7af7SAndreas Gohr    }
260409d7af7SAndreas Gohr
26160e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
26260e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2631246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
2642a7abf2dSChristopher Smith                    'resendpwd','profile_delete'))){
26560e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
26660e6b550SAndreas Gohr        return 'show';
26760e6b550SAndreas Gohr    }
26860e6b550SAndreas Gohr
269c828a5d6SAndreas Gohr    //is there really a draft?
270c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
271c828a5d6SAndreas Gohr
272067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
273ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2741246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2752a7abf2dSChristopher Smith                    'unsubscribe','profile','profile_delete','resendpwd','recover',
276d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
277ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
278af182434Sandi        return 'show';
279af182434Sandi    }
280af182434Sandi    return $act;
281af182434Sandi}
282af182434Sandi
283af182434Sandi/**
2846b13307fSandi * Run permissionchecks
2856b13307fSandi *
2866b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2876b13307fSandi */
2886b13307fSandifunction act_permcheck($act){
289dbbc6aa7Sandi    global $INFO;
2905e199953Smatthiasgrimm    global $conf;
291dbbc6aa7Sandi
292ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2936b13307fSandi        if($INFO['exists']){
294bdbc16bfSandi            if($act == 'edit'){
295bdbc16bfSandi                //the edit function will check again and do a source show
296bdbc16bfSandi                //when no AUTH_EDIT available
297bdbc16bfSandi                $permneed = AUTH_READ;
298bdbc16bfSandi            }else{
2996b13307fSandi                $permneed = AUTH_EDIT;
300bdbc16bfSandi            }
3016b13307fSandi        }else{
3026b13307fSandi            $permneed = AUTH_CREATE;
3036b13307fSandi        }
3042a7abf2dSChristopher Smith    }elseif(in_array($act,array('login','search','recent','profile','profile_delete','index', 'sitemap'))){
3056b13307fSandi        $permneed = AUTH_NONE;
3061246e016SAndreas Gohr    }elseif($act == 'revert'){
3071246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
3081246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
3095e199953Smatthiasgrimm    }elseif($act == 'register'){
3105e199953Smatthiasgrimm        $permneed = AUTH_NONE;
311ebd3d9ceSchris    }elseif($act == 'resendpwd'){
312ebd3d9ceSchris        $permneed = AUTH_NONE;
313c19fe9c0Sandi    }elseif($act == 'admin'){
314f8cc712eSAndreas Gohr        if($INFO['ismanager']){
315f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
316f8cc712eSAndreas Gohr            // action is checked later
317f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
318f8cc712eSAndreas Gohr        }else{
319c19fe9c0Sandi            $permneed = AUTH_ADMIN;
320f8cc712eSAndreas Gohr        }
3216b13307fSandi    }else{
3226b13307fSandi        $permneed = AUTH_READ;
3236b13307fSandi    }
324dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
325dbbc6aa7Sandi
3266b13307fSandi    return 'denied';
3276b13307fSandi}
3286b13307fSandi
3296b13307fSandi/**
330ee4c4a1bSAndreas Gohr * Handle 'draftdel'
331ee4c4a1bSAndreas Gohr *
332ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
333ee4c4a1bSAndreas Gohr */
334ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
335ee4c4a1bSAndreas Gohr    global $INFO;
336ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
337ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
338ee4c4a1bSAndreas Gohr    return 'show';
339ee4c4a1bSAndreas Gohr}
340ee4c4a1bSAndreas Gohr
341ee4c4a1bSAndreas Gohr/**
342ee4c4a1bSAndreas Gohr * Saves a draft on preview
343ee4c4a1bSAndreas Gohr *
344ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
345ee4c4a1bSAndreas Gohr */
346ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
347ee4c4a1bSAndreas Gohr    global $INFO;
348ee4c4a1bSAndreas Gohr    global $ID;
34990f1b7bdSTom N Harris    global $INPUT;
350ee4c4a1bSAndreas Gohr    global $conf;
35190f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
352ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
35390f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
35490f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
35590f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
35690f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
357ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
358ee4c4a1bSAndreas Gohr                );
359ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
360ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
361ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
362ee4c4a1bSAndreas Gohr        }
363ee4c4a1bSAndreas Gohr    }
364ee4c4a1bSAndreas Gohr    return $act;
365ee4c4a1bSAndreas Gohr}
366ee4c4a1bSAndreas Gohr
367ee4c4a1bSAndreas Gohr/**
3686b13307fSandi * Handle 'save'
3696b13307fSandi *
3706b13307fSandi * Checks for spam and conflicts and saves the page.
3716b13307fSandi * Does a redirect to show the page afterwards or
3726b13307fSandi * returns a new action.
3736b13307fSandi *
3746b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3756b13307fSandi */
3766b13307fSandifunction act_save($act){
3776b13307fSandi    global $ID;
3786b13307fSandi    global $DATE;
3796b13307fSandi    global $PRE;
3806b13307fSandi    global $TEXT;
3816b13307fSandi    global $SUF;
3826b13307fSandi    global $SUM;
3835a932e77SAdrian Lang    global $lang;
3848d67c48aSAdrian Lang    global $INFO;
38590f1b7bdSTom N Harris    global $INPUT;
3866b13307fSandi
3876b13307fSandi    //spam check
3885a932e77SAdrian Lang    if(checkwordblock()) {
3895a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3905a932e77SAdrian Lang        return 'edit';
3915a932e77SAdrian Lang    }
3928d67c48aSAdrian Lang    //conflict check
3938d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3946b13307fSandi        return 'conflict';
3956b13307fSandi
3966b13307fSandi    //save it
39790f1b7bdSTom N Harris    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$INPUT->bool('minor')); //use pretty mode for con
3986b13307fSandi    //unlock it
3996b13307fSandi    unlock($ID);
4006b13307fSandi
401ee4c4a1bSAndreas Gohr    //delete draft
402ee4c4a1bSAndreas Gohr    act_draftdel($act);
40369cd1e27SAndreas Gohr    session_write_close();
404ee4c4a1bSAndreas Gohr
40569cd1e27SAndreas Gohr    // when done, show page
40669cd1e27SAndreas Gohr    return 'show';
40769cd1e27SAndreas Gohr}
408f951a474SAndreas Gohr
40914a122deSAndreas Gohr/**
4101246e016SAndreas Gohr * Revert to a certain revision
4111246e016SAndreas Gohr *
4121246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
4131246e016SAndreas Gohr */
4141246e016SAndreas Gohrfunction act_revert($act){
4151246e016SAndreas Gohr    global $ID;
4161246e016SAndreas Gohr    global $REV;
4171246e016SAndreas Gohr    global $lang;
418585bf44eSChristopher Smith    /* @var Input $INPUT */
419585bf44eSChristopher Smith    global $INPUT;
420de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
421de4d479aSAdrian Lang    // global $INFO;
422de4d479aSAdrian Lang    // if (!$INFO['writable']) {
423de4d479aSAdrian Lang    //     return 'show';
424de4d479aSAdrian Lang    // }
4251246e016SAndreas Gohr
4261246e016SAndreas Gohr    // when no revision is given, delete current one
4271246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
4281246e016SAndreas Gohr    $text = '';
4291246e016SAndreas Gohr    $sum  = $lang['deleted'];
4301246e016SAndreas Gohr    if($REV){
4311246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
4321246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
433d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
4341246e016SAndreas Gohr    }
4351246e016SAndreas Gohr
4361246e016SAndreas Gohr    // spam check
4375a932e77SAdrian Lang
4385a932e77SAdrian Lang    if (checkwordblock($text)) {
4395a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4405a932e77SAdrian Lang        return 'edit';
4415a932e77SAdrian Lang    }
4421246e016SAndreas Gohr
4431246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
4441246e016SAndreas Gohr    msg($sum,1);
4451246e016SAndreas Gohr
4461246e016SAndreas Gohr    //delete any draft
4471246e016SAndreas Gohr    act_draftdel($act);
4481246e016SAndreas Gohr    session_write_close();
4491246e016SAndreas Gohr
4501246e016SAndreas Gohr    // when done, show current page
451585bf44eSChristopher Smith    $INPUT->server->set('REQUEST_METHOD','post'); //should force a redirect
4521246e016SAndreas Gohr    $REV = '';
4531246e016SAndreas Gohr    return 'show';
4541246e016SAndreas Gohr}
4551246e016SAndreas Gohr
4561246e016SAndreas Gohr/**
45714a122deSAndreas Gohr * Do a redirect after receiving post data
45814a122deSAndreas Gohr *
45914a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
46014a122deSAndreas Gohr */
46169cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
46269cd1e27SAndreas Gohr    global $PRE;
46369cd1e27SAndreas Gohr    global $TEXT;
464f951a474SAndreas Gohr
46569cd1e27SAndreas Gohr    $opts = array(
46669cd1e27SAndreas Gohr            'id'       => $id,
46769cd1e27SAndreas Gohr            'preact'   => $preact
46869cd1e27SAndreas Gohr            );
469c66972f2SAdrian Lang    //get section name when coming from section edit
470c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
471c66972f2SAdrian Lang        $check = false; //Byref
472c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
473c66972f2SAdrian Lang    }
474c66972f2SAdrian Lang
47569cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
47669cd1e27SAndreas Gohr}
47769cd1e27SAndreas Gohr
478c8b076b1SMichael Hamann/**
479c8b076b1SMichael Hamann * Execute the redirect
480c8b076b1SMichael Hamann *
481c8b076b1SMichael Hamann * @param array $opts id and fragment for the redirect
482c8b076b1SMichael Hamann */
48369cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
48469cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
485c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
48669cd1e27SAndreas Gohr
4876b13307fSandi    //show it
488af2408d5SAndreas Gohr    send_redirect($go);
4896b13307fSandi}
4906b13307fSandi
4916b13307fSandi/**
492b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4936b13307fSandi *
4946b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4956b13307fSandi */
4966b13307fSandifunction act_auth($act){
49708eda5bcSmatthiasgrimm    global $ID;
4987cace34dSAndreas Gohr    global $INFO;
499585bf44eSChristopher Smith    /* @var Input $INPUT */
500585bf44eSChristopher Smith    global $INPUT;
50108eda5bcSmatthiasgrimm
5026b13307fSandi    //already logged in?
503585bf44eSChristopher Smith    if($INPUT->server->has('REMOTE_USER') && $act=='login'){
504ca12ce46SAndreas Gohr        return 'show';
5052288dc06SGuy Brand    }
5066b13307fSandi
5076b13307fSandi    //handle logout
5086b13307fSandi    if($act=='logout'){
50908eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
510585bf44eSChristopher Smith        if($lockedby == $INPUT->server->str('REMOTE_USER')){
51108eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
512585bf44eSChristopher Smith        }
51308eda5bcSmatthiasgrimm
5147cace34dSAndreas Gohr        // do the logout stuff
5156b13307fSandi        auth_logoff();
5167cace34dSAndreas Gohr
5177cace34dSAndreas Gohr        // rebuild info array
5187cace34dSAndreas Gohr        $INFO = pageinfo();
5197cace34dSAndreas Gohr
520e16eccb7SGuy Brand        act_redirect($ID,'login');
5216b13307fSandi    }
5226b13307fSandi
5236b13307fSandi    return $act;
5246b13307fSandi}
5256b13307fSandi
5266b13307fSandi/**
52745a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
5286b13307fSandi *
5296b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
5306b13307fSandi */
5316b13307fSandifunction act_edit($act){
532cd409024Sjorda    global $ID;
533ee4c4a1bSAndreas Gohr    global $INFO;
534cd409024Sjorda
53545a99335SAdrian Lang    global $TEXT;
53645a99335SAdrian Lang    global $RANGE;
53745a99335SAdrian Lang    global $PRE;
53845a99335SAdrian Lang    global $SUF;
53945a99335SAdrian Lang    global $REV;
54045a99335SAdrian Lang    global $SUM;
54145a99335SAdrian Lang    global $lang;
54245a99335SAdrian Lang    global $DATE;
54345a99335SAdrian Lang
54445a99335SAdrian Lang    if (!isset($TEXT)) {
54545a99335SAdrian Lang        if ($INFO['exists']) {
54645a99335SAdrian Lang            if ($RANGE) {
54745a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
54845a99335SAdrian Lang            } else {
54945a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
55045a99335SAdrian Lang            }
55145a99335SAdrian Lang        } else {
552fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
55345a99335SAdrian Lang        }
55445a99335SAdrian Lang    }
55545a99335SAdrian Lang
55645a99335SAdrian Lang    //set summary default
55745a99335SAdrian Lang    if(!$SUM){
55845a99335SAdrian Lang        if($REV){
5597656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
56045a99335SAdrian Lang        }elseif(!$INFO['exists']){
56145a99335SAdrian Lang            $SUM = $lang['created'];
56245a99335SAdrian Lang        }
56345a99335SAdrian Lang    }
56445a99335SAdrian Lang
5658d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5668d67c48aSAdrian Lang    // This is used for conflict detection
56778035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
56845a99335SAdrian Lang
5696b13307fSandi    //check if locked by anyone - if not lock for my self
57031bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
57131bc8f11SMichael Hamann    if ($INFO['writable']) {
5726b13307fSandi        $lockedby = checklock($ID);
5736b13307fSandi        if($lockedby) return 'locked';
5746b13307fSandi
5756b13307fSandi        lock($ID);
57631bc8f11SMichael Hamann    }
57731bc8f11SMichael Hamann
5786b13307fSandi    return $act;
5796b13307fSandi}
5806b13307fSandi
5816b13307fSandi/**
582f6dad9fdSMichael Klier * Export a wiki page for various formats
583f6dad9fdSMichael Klier *
584f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
585f6dad9fdSMichael Klier *
586f6dad9fdSMichael Klier *  Event data:
587f6dad9fdSMichael Klier *    data['id']      -- page id
588f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
589f6dad9fdSMichael Klier *    data['headers'] -- export headers
590f6dad9fdSMichael Klier *    data['output']  -- export output
5916b13307fSandi *
5926b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
593f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5946b13307fSandi */
5956b13307fSandifunction act_export($act){
5966b13307fSandi    global $ID;
5976b13307fSandi    global $REV;
59885f8705cSAnika Henke    global $conf;
59985f8705cSAnika Henke    global $lang;
6006b13307fSandi
601f6dad9fdSMichael Klier    $pre = '';
602f6dad9fdSMichael Klier    $post = '';
603f6dad9fdSMichael Klier    $output = '';
604f6dad9fdSMichael Klier    $headers = array();
605cc2ae802SAndreas Gohr
606f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
607f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
608f6dad9fdSMichael Klier
609ac83b9d8Sandi    $mode = substr($act,7);
610f6dad9fdSMichael Klier    switch($mode) {
611f6dad9fdSMichael Klier        case 'raw':
6125adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
61366b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
614f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
615f6dad9fdSMichael Klier            break;
616f6dad9fdSMichael Klier        case 'xhtml':
617c8839c22SAnika Henke            $pre .= '<!DOCTYPE html>' . DOKU_LF;
618c8839c22SAnika Henke            $pre .= '<html lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
619f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
620c8839c22SAnika Henke            $pre .= '  <meta charset="utf-8" />' . DOKU_LF;
621f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
622f6dad9fdSMichael Klier
623f6dad9fdSMichael Klier            // get metaheaders
624f6dad9fdSMichael Klier            ob_start();
625f6dad9fdSMichael Klier            tpl_metaheaders();
626f6dad9fdSMichael Klier            $pre .= ob_get_clean();
627f6dad9fdSMichael Klier
628f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
629f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
630f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
631f6dad9fdSMichael Klier
632f6dad9fdSMichael Klier            // get toc
633f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
634f6dad9fdSMichael Klier
635f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
636f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
637f6dad9fdSMichael Klier
638f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
639f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
640f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
641f6dad9fdSMichael Klier            break;
642f6dad9fdSMichael Klier        case 'xhtmlbody':
643f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
644f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
645f6dad9fdSMichael Klier            break;
646f6dad9fdSMichael Klier        default:
647*b814e978SGerrit Uitslag            $output = p_cached_output(wikiFN($ID,$REV), $mode, $ID);
6489acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
649f6dad9fdSMichael Klier            break;
650f6dad9fdSMichael Klier    }
651f6dad9fdSMichael Klier
652f6dad9fdSMichael Klier    // prepare event data
653f6dad9fdSMichael Klier    $data = array();
654f6dad9fdSMichael Klier    $data['id'] = $ID;
655f6dad9fdSMichael Klier    $data['mode'] = $mode;
656f6dad9fdSMichael Klier    $data['headers'] = $headers;
657f6dad9fdSMichael Klier    $data['output'] =& $output;
658f6dad9fdSMichael Klier
659f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
660f6dad9fdSMichael Klier
661f6dad9fdSMichael Klier    if(!empty($data['output'])){
662f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
66385767031SAndreas Gohr            header("$key: $val");
66485767031SAndreas Gohr        }
665f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6666b13307fSandi        exit;
6676b13307fSandi    }
6686b13307fSandi    return 'show';
6696b13307fSandi}
670340756e4Sandi
671b158d625SSteven Danz/**
672c4f79b71SMichael Hamann * Handle sitemap delivery
673c4f79b71SMichael Hamann *
674c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
675c4f79b71SMichael Hamann */
676c4f79b71SMichael Hamannfunction act_sitemap($act) {
677c4f79b71SMichael Hamann    global $conf;
678c4f79b71SMichael Hamann
679eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
6809d2e1be6SAndreas Gohr        http_status(404);
681c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
682c4f79b71SMichael Hamann        exit;
683c4f79b71SMichael Hamann    }
684c4f79b71SMichael Hamann
685eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
68665f6e7d6SMichael Hamann    if (Sitemapper::sitemapIsCompressed()) {
687c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
688c4f79b71SMichael Hamann    }else{
689c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
690c4f79b71SMichael Hamann    }
691c4f79b71SMichael Hamann
692c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
693c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
6942897eb23SMichael Hamann        Sitemapper::generate();
695c4f79b71SMichael Hamann    }
696c4f79b71SMichael Hamann
697c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
698c4f79b71SMichael Hamann        // Send headers
699c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
7003009a773SAndreas Gohr        header('Content-Disposition: attachment; filename='.utf8_basename($sitemap));
701c4f79b71SMichael Hamann
702eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
703eae17177SMichael Hamann
704c4f79b71SMichael Hamann        // Send file
705c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
70640e0b444SDominik Eckelmann        http_sendfile($sitemap);
707c4f79b71SMichael Hamann
708eae17177SMichael Hamann        readfile($sitemap);
709c4f79b71SMichael Hamann        exit;
710c4f79b71SMichael Hamann    }
711c4f79b71SMichael Hamann
7129d2e1be6SAndreas Gohr    http_status(500);
713eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
714c4f79b71SMichael Hamann    exit;
715c4f79b71SMichael Hamann}
716c4f79b71SMichael Hamann
717c4f79b71SMichael Hamann/**
7185b75cd1fSAdrian Lang * Handle page 'subscribe'
719b158d625SSteven Danz *
7205b75cd1fSAdrian Lang * Throws exception on error.
7215b75cd1fSAdrian Lang *
7225b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
723b158d625SSteven Danz */
7241380fc45SAndreas Gohrfunction act_subscription($act){
725056c2049SAndreas Gohr    global $lang;
726056c2049SAndreas Gohr    global $INFO;
727056c2049SAndreas Gohr    global $ID;
728585bf44eSChristopher Smith    /* @var Input $INPUT */
72990f1b7bdSTom N Harris    global $INPUT;
73052b0dd67SGuy Brand
7319fa341d0SAndreas Gohr    // subcriptions work for logged in users only
732585bf44eSChristopher Smith    if(!$INPUT->server->str('REMOTE_USER')) return 'show';
7339fa341d0SAndreas Gohr
734056c2049SAndreas Gohr    // get and preprocess data.
7358881fcc9SAdrian Lang    $params = array();
7368881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
73790f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
73890f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
7398881fcc9SAdrian Lang        }
7408881fcc9SAdrian Lang    }
7418881fcc9SAdrian Lang
742056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
7430e80bb5eSChristopher Smith    if(empty($params['action']) || !checkSecurityToken()) return $act;
744056c2049SAndreas Gohr
7458881fcc9SAdrian Lang    // Handle POST data, may throw exception.
7468881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7478881fcc9SAdrian Lang
7488881fcc9SAdrian Lang    $target = $params['target'];
7498881fcc9SAdrian Lang    $style  = $params['style'];
7508881fcc9SAdrian Lang    $action = $params['action'];
7518881fcc9SAdrian Lang
7528881fcc9SAdrian Lang    // Perform action.
753a0519fdaSAndreas Gohr    $sub = new Subscription();
754a0519fdaSAndreas Gohr    if($action == 'unsubscribe'){
755585bf44eSChristopher Smith        $ok = $sub->remove($target, $INPUT->server->str('REMOTE_USER'), $style);
756a0519fdaSAndreas Gohr    }else{
757585bf44eSChristopher Smith        $ok = $sub->add($target, $INPUT->server->str('REMOTE_USER'), $style);
758a0519fdaSAndreas Gohr    }
759a0519fdaSAndreas Gohr
760a0519fdaSAndreas Gohr    if($ok) {
761a0519fdaSAndreas Gohr        msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
762a0519fdaSAndreas Gohr                    prettyprint_id($target)), 1);
763a0519fdaSAndreas Gohr        act_redirect($ID, $act);
764a0519fdaSAndreas Gohr    } else {
7658881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
7668881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
7678881fcc9SAdrian Lang                                    prettyprint_id($target)));
7688881fcc9SAdrian Lang    }
769cb3f9dbaSAdrian Lang
770cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
771a0519fdaSAndreas Gohr    $INFO['subscribed'] = $sub->user_subscription();
772cb3f9dbaSAdrian Lang    return 'show';
7738881fcc9SAdrian Lang}
7748881fcc9SAdrian Lang
7758881fcc9SAdrian Lang/**
7768881fcc9SAdrian Lang * Validate POST data
7778881fcc9SAdrian Lang *
7788881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
7798881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
7808881fcc9SAdrian Lang *
7818881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
7828881fcc9SAdrian Lang */
7837a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
7848881fcc9SAdrian Lang    global $INFO;
7858881fcc9SAdrian Lang    global $lang;
786585bf44eSChristopher Smith    /* @var Input $INPUT */
787585bf44eSChristopher Smith    global $INPUT;
7888881fcc9SAdrian Lang
7895b75cd1fSAdrian Lang    // Get and validate parameters.
7908881fcc9SAdrian Lang    if (!isset($params['target'])) {
79115741132SAndreas Gohr        throw new Exception('no subscription target given');
7925b75cd1fSAdrian Lang    }
7938881fcc9SAdrian Lang    $target = $params['target'];
7945b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
7955b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
7965b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
7975b75cd1fSAdrian Lang        $valid_styles[] = 'list';
7985b75cd1fSAdrian Lang    }
7998881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
80015741132SAndreas Gohr                              'invalid subscription style given');
8018881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
80215741132SAndreas Gohr                              $params, 'invalid subscription action given');
803613964ecSGuy Brand
8045b75cd1fSAdrian Lang    // Check other conditions.
8055b75cd1fSAdrian Lang    if ($action === 'subscribe') {
8065b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
8075b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
80852b0dd67SGuy Brand        }
8095b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
8105b75cd1fSAdrian Lang        $is = false;
8115b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
8125b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
8135b75cd1fSAdrian Lang                $is = true;
81452b0dd67SGuy Brand            }
81552b0dd67SGuy Brand        }
8165b75cd1fSAdrian Lang        if ($is === false) {
81715741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
818585bf44eSChristopher Smith                                        $INPUT->server->str('REMOTE_USER'),
8195b75cd1fSAdrian Lang                                        prettyprint_id($target)));
8205b75cd1fSAdrian Lang        }
8215b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
8225b75cd1fSAdrian Lang        $style = null;
82352b0dd67SGuy Brand    }
82452b0dd67SGuy Brand
82516c665d9SAndreas Gohr    $params = compact('target', 'style', 'action');
82652b0dd67SGuy Brand}
82752b0dd67SGuy Brand
828e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
829