xref: /dokuwiki/inc/actions.php (revision 90f1b7bd60332450b32e4ec0b189ddb0ab11fdf8)
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;
23*90f1b7bdSTom N Harris    global $INPUT;
246b13307fSandi    global $lang;
2585dcda20SRobin Getz    global $conf;
266b13307fSandi
2769cd1e27SAndreas Gohr    $preact = $ACT;
2869cd1e27SAndreas Gohr
29c2e830f2Schris    // give plugins an opportunity to process the action
3024bb549bSchris    $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT);
3124bb549bSchris    if ($evt->advise_before()) {
32c2e830f2Schris
33af182434Sandi        //sanitize $ACT
34af182434Sandi        $ACT = act_clean($ACT);
35af182434Sandi
36b8957367SBenjamin Gilbert        //check if searchword was given - else just show
370868021bSAndreas Gohr        $s = cleanID($QUERY);
380868021bSAndreas Gohr        if($ACT == 'search' && empty($s)){
39b8957367SBenjamin Gilbert            $ACT = 'show';
40b8957367SBenjamin Gilbert        }
41b8957367SBenjamin Gilbert
42b8957367SBenjamin Gilbert        //login stuff
431b2a85e8SAndreas Gohr        if(in_array($ACT,array('login','logout'))){
44b8957367SBenjamin Gilbert            $ACT = act_auth($ACT);
451b2a85e8SAndreas Gohr        }
46b8957367SBenjamin Gilbert
471380fc45SAndreas Gohr        //check if user is asking to (un)subscribe a page
485b75cd1fSAdrian Lang        if($ACT == 'subscribe') {
495b75cd1fSAdrian Lang            try {
501380fc45SAndreas Gohr                $ACT = act_subscription($ACT);
515b75cd1fSAdrian Lang            } catch (Exception $e) {
525b75cd1fSAdrian Lang                msg($e->getMessage(), -1);
535b75cd1fSAdrian Lang            }
545b75cd1fSAdrian Lang        }
5552b0dd67SGuy Brand
564064e2d3SRobin Getz        //display some infos
574064e2d3SRobin Getz        if($ACT == 'check'){
584064e2d3SRobin Getz            check();
594064e2d3SRobin Getz            $ACT = 'show';
604064e2d3SRobin Getz        }
614064e2d3SRobin Getz
626b13307fSandi        //check permissions
636b13307fSandi        $ACT = act_permcheck($ACT);
646b13307fSandi
65c4f79b71SMichael Hamann        //sitemap
66eae17177SMichael Hamann        if ($ACT == 'sitemap'){
67c4f79b71SMichael Hamann            $ACT = act_sitemap($ACT);
68eae17177SMichael Hamann        }
69c4f79b71SMichael Hamann
70b8957367SBenjamin Gilbert        //register
71b3510079SAndreas Gohr        if($ACT == 'register' && $_POST['save'] && register()){
72b8957367SBenjamin Gilbert            $ACT = 'login';
73b8957367SBenjamin Gilbert        }
746b13307fSandi
758b06d178Schris        if ($ACT == 'resendpwd' && act_resendpwd()) {
768b06d178Schris            $ACT = 'login';
778b06d178Schris        }
788b06d178Schris
798b06d178Schris        //update user profile
8025b2a98cSMichael Klier        if ($ACT == 'profile') {
8125b2a98cSMichael Klier            if(!$_SERVER['REMOTE_USER']) {
8225b2a98cSMichael Klier                $ACT = 'login';
8325b2a98cSMichael Klier            } else {
8425b2a98cSMichael Klier                if(updateprofile()) {
854cb79657SMatthias Grimm                    msg($lang['profchanged'],1);
864cb79657SMatthias Grimm                    $ACT = 'show';
878b06d178Schris                }
8825b2a98cSMichael Klier            }
8925b2a98cSMichael Klier        }
908b06d178Schris
911246e016SAndreas Gohr        //revert
921246e016SAndreas Gohr        if($ACT == 'revert'){
931246e016SAndreas Gohr            if(checkSecurityToken()){
941246e016SAndreas Gohr                $ACT = act_revert($ACT);
951246e016SAndreas Gohr            }else{
961246e016SAndreas Gohr                $ACT = 'show';
971246e016SAndreas Gohr            }
981246e016SAndreas Gohr        }
991246e016SAndreas Gohr
1006b13307fSandi        //save
1011b2a85e8SAndreas Gohr        if($ACT == 'save'){
1021b2a85e8SAndreas Gohr            if(checkSecurityToken()){
1036b13307fSandi                $ACT = act_save($ACT);
1041b2a85e8SAndreas Gohr            }else{
1058071beaaSAndreas Gohr                $ACT = 'preview';
1061b2a85e8SAndreas Gohr            }
1071b2a85e8SAndreas Gohr        }
1086b13307fSandi
109067c5d22SBen Coburn        //cancel conflicting edit
110067c5d22SBen Coburn        if($ACT == 'cancel')
111067c5d22SBen Coburn            $ACT = 'show';
112067c5d22SBen Coburn
113ee4c4a1bSAndreas Gohr        //draft deletion
114ee4c4a1bSAndreas Gohr        if($ACT == 'draftdel')
115ee4c4a1bSAndreas Gohr            $ACT = act_draftdel($ACT);
116ee4c4a1bSAndreas Gohr
117ee4c4a1bSAndreas Gohr        //draft saving on preview
118ee4c4a1bSAndreas Gohr        if($ACT == 'preview')
119ee4c4a1bSAndreas Gohr            $ACT = act_draftsave($ACT);
120ee4c4a1bSAndreas Gohr
1216b13307fSandi        //edit
122c9d5430bSAdrian Lang        if(in_array($ACT, array('edit', 'preview', 'recover'))) {
123af182434Sandi            $ACT = act_edit($ACT);
1246b13307fSandi        }else{
1256b13307fSandi            unlock($ID); //try to unlock
1266b13307fSandi        }
1276b13307fSandi
1286b13307fSandi        //handle export
129ac83b9d8Sandi        if(substr($ACT,0,7) == 'export_')
1306b13307fSandi            $ACT = act_export($ACT);
1316b13307fSandi
132c19fe9c0Sandi        //handle admin tasks
133c19fe9c0Sandi        if($ACT == 'admin'){
13411e2ce22Schris            // retrieve admin plugin name from $_REQUEST['page']
135*90f1b7bdSTom N Harris            if (($page = $INPUT->str('page', '', true)) != '') {
13611e2ce22Schris                $pluginlist = plugin_list('admin');
137*90f1b7bdSTom N Harris                if (in_array($page, $pluginlist)) {
13811e2ce22Schris                    // attempt to load the plugin
139*90f1b7bdSTom N Harris                    if ($plugin =& plugin_load('admin',$page) !== null){
14024ea6500SAndreas Gohr                        if($plugin->forAdminOnly() && !$INFO['isadmin']){
14124ea6500SAndreas Gohr                            // a manager tried to load a plugin that's for admins only
142*90f1b7bdSTom N Harris                            $INPUT->remove('page');
14324ea6500SAndreas Gohr                            msg('For admins only',-1);
14424ea6500SAndreas Gohr                        }else{
14511e2ce22Schris                            $plugin->handle();
14611e2ce22Schris                        }
14711e2ce22Schris                    }
148c19fe9c0Sandi                }
14924ea6500SAndreas Gohr            }
15024ea6500SAndreas Gohr        }
1515f312bacSAndreas Gohr
1525f312bacSAndreas Gohr        // check permissions again - the action may have changed
1535f312bacSAndreas Gohr        $ACT = act_permcheck($ACT);
15424bb549bSchris    }  // end event ACTION_ACT_PREPROCESS default action
15524bb549bSchris    $evt->advise_after();
15685dcda20SRobin Getz    // Make sure plugs can handle 'denied'
15785dcda20SRobin Getz    if($conf['send404'] && $ACT == 'denied') {
15885dcda20SRobin Getz        header('HTTP/1.0 403 Forbidden');
15985dcda20SRobin Getz    }
16024bb549bSchris    unset($evt);
161c19fe9c0Sandi
16246c0ed74SMichael Hamann    // when action 'show', the intial not 'show' and POST, do a redirect
16346c0ed74SMichael Hamann    if($ACT == 'show' && $preact != 'show' && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
16469cd1e27SAndreas Gohr        act_redirect($ID,$preact);
16569cd1e27SAndreas Gohr    }
1665f312bacSAndreas Gohr
167c346111aSAdrian Lang    global $INFO;
168c346111aSAdrian Lang    global $conf;
169c346111aSAdrian Lang    global $license;
170c346111aSAdrian Lang
1716b13307fSandi    //call template FIXME: all needed vars available?
172f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
173746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
174f63a2007Schris
1755a892029SAndreas Gohr    include(template('main.php'));
176c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
177c19fe9c0Sandi    // in function tpl_content()
1786b13307fSandi}
1796b13307fSandi
180f63a2007Schrisfunction act_sendheaders($headers) {
181f63a2007Schris    foreach ($headers as $hdr) header($hdr);
182f63a2007Schris}
183f63a2007Schris
1846b13307fSandi/**
185af182434Sandi * Sanitize the action command
186af182434Sandi *
187af182434Sandi * Add all allowed commands here.
188af182434Sandi *
189af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
190af182434Sandi */
191af182434Sandifunction act_clean($act){
192af182434Sandi    global $lang;
19360e6b550SAndreas Gohr    global $conf;
194c828a5d6SAndreas Gohr    global $INFO;
195af182434Sandi
196ee4c4a1bSAndreas Gohr    // check if the action was given as array key
197ee4c4a1bSAndreas Gohr    if(is_array($act)){
198ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
199ee4c4a1bSAndreas Gohr    }
200ee4c4a1bSAndreas Gohr
201ac83b9d8Sandi    //remove all bad chars
202ac83b9d8Sandi    $act = strtolower($act);
2032d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
204ac83b9d8Sandi
205ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
206cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
207b146b32bSandi
208396c218fSAndreas Gohr    if($act === '') $act = 'show';
209396c218fSAndreas Gohr
210409d7af7SAndreas Gohr    // check if action is disabled
211409d7af7SAndreas Gohr    if(!actionOK($act)){
212409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
213409d7af7SAndreas Gohr        return 'show';
214409d7af7SAndreas Gohr    }
215409d7af7SAndreas Gohr
21660e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
21760e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2181246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
219d5a9514cSAdrian Lang                    'resendpwd'))){
22060e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
22160e6b550SAndreas Gohr        return 'show';
22260e6b550SAndreas Gohr    }
22360e6b550SAndreas Gohr
224c828a5d6SAndreas Gohr    //is there really a draft?
225c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
226c828a5d6SAndreas Gohr
227067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
228ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2291246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2305a932e77SAdrian Lang                    'unsubscribe','profile','resendpwd','recover',
231d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
232ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
233af182434Sandi        return 'show';
234af182434Sandi    }
235af182434Sandi    return $act;
236af182434Sandi}
237af182434Sandi
238af182434Sandi/**
2396b13307fSandi * Run permissionchecks
2406b13307fSandi *
2416b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2426b13307fSandi */
2436b13307fSandifunction act_permcheck($act){
244dbbc6aa7Sandi    global $INFO;
2455e199953Smatthiasgrimm    global $conf;
246dbbc6aa7Sandi
247ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2486b13307fSandi        if($INFO['exists']){
249bdbc16bfSandi            if($act == 'edit'){
250bdbc16bfSandi                //the edit function will check again and do a source show
251bdbc16bfSandi                //when no AUTH_EDIT available
252bdbc16bfSandi                $permneed = AUTH_READ;
253bdbc16bfSandi            }else{
2546b13307fSandi                $permneed = AUTH_EDIT;
255bdbc16bfSandi            }
2566b13307fSandi        }else{
2576b13307fSandi            $permneed = AUTH_CREATE;
2586b13307fSandi        }
259c4f79b71SMichael Hamann    }elseif(in_array($act,array('login','search','recent','profile','index', 'sitemap'))){
2606b13307fSandi        $permneed = AUTH_NONE;
2611246e016SAndreas Gohr    }elseif($act == 'revert'){
2621246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
2631246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
2645e199953Smatthiasgrimm    }elseif($act == 'register'){
2655e199953Smatthiasgrimm        $permneed = AUTH_NONE;
266ebd3d9ceSchris    }elseif($act == 'resendpwd'){
267ebd3d9ceSchris        $permneed = AUTH_NONE;
268c19fe9c0Sandi    }elseif($act == 'admin'){
269f8cc712eSAndreas Gohr        if($INFO['ismanager']){
270f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
271f8cc712eSAndreas Gohr            // action is checked later
272f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
273f8cc712eSAndreas Gohr        }else{
274c19fe9c0Sandi            $permneed = AUTH_ADMIN;
275f8cc712eSAndreas Gohr        }
2766b13307fSandi    }else{
2776b13307fSandi        $permneed = AUTH_READ;
2786b13307fSandi    }
279dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
280dbbc6aa7Sandi
2816b13307fSandi    return 'denied';
2826b13307fSandi}
2836b13307fSandi
2846b13307fSandi/**
285ee4c4a1bSAndreas Gohr * Handle 'draftdel'
286ee4c4a1bSAndreas Gohr *
287ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
288ee4c4a1bSAndreas Gohr */
289ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
290ee4c4a1bSAndreas Gohr    global $INFO;
291ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
292ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
293ee4c4a1bSAndreas Gohr    return 'show';
294ee4c4a1bSAndreas Gohr}
295ee4c4a1bSAndreas Gohr
296ee4c4a1bSAndreas Gohr/**
297ee4c4a1bSAndreas Gohr * Saves a draft on preview
298ee4c4a1bSAndreas Gohr *
299ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
300ee4c4a1bSAndreas Gohr */
301ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
302ee4c4a1bSAndreas Gohr    global $INFO;
303ee4c4a1bSAndreas Gohr    global $ID;
304*90f1b7bdSTom N Harris    global $INPUT;
305ee4c4a1bSAndreas Gohr    global $conf;
306*90f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
307ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
308*90f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
309*90f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
310*90f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
311*90f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
312ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
313ee4c4a1bSAndreas Gohr                );
314ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
315ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
316ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
317ee4c4a1bSAndreas Gohr        }
318ee4c4a1bSAndreas Gohr    }
319ee4c4a1bSAndreas Gohr    return $act;
320ee4c4a1bSAndreas Gohr}
321ee4c4a1bSAndreas Gohr
322ee4c4a1bSAndreas Gohr/**
3236b13307fSandi * Handle 'save'
3246b13307fSandi *
3256b13307fSandi * Checks for spam and conflicts and saves the page.
3266b13307fSandi * Does a redirect to show the page afterwards or
3276b13307fSandi * returns a new action.
3286b13307fSandi *
3296b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3306b13307fSandi */
3316b13307fSandifunction act_save($act){
3326b13307fSandi    global $ID;
3336b13307fSandi    global $DATE;
3346b13307fSandi    global $PRE;
3356b13307fSandi    global $TEXT;
3366b13307fSandi    global $SUF;
3376b13307fSandi    global $SUM;
3385a932e77SAdrian Lang    global $lang;
3398d67c48aSAdrian Lang    global $INFO;
340*90f1b7bdSTom N Harris    global $INPUT;
3416b13307fSandi
3426b13307fSandi    //spam check
3435a932e77SAdrian Lang    if(checkwordblock()) {
3445a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3455a932e77SAdrian Lang        return 'edit';
3465a932e77SAdrian Lang    }
3478d67c48aSAdrian Lang    //conflict check
3488d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3496b13307fSandi        return 'conflict';
3506b13307fSandi
3516b13307fSandi    //save it
352*90f1b7bdSTom N Harris    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$INPUT->bool('minor')); //use pretty mode for con
3536b13307fSandi    //unlock it
3546b13307fSandi    unlock($ID);
3556b13307fSandi
356ee4c4a1bSAndreas Gohr    //delete draft
357ee4c4a1bSAndreas Gohr    act_draftdel($act);
35869cd1e27SAndreas Gohr    session_write_close();
359ee4c4a1bSAndreas Gohr
36069cd1e27SAndreas Gohr    // when done, show page
36169cd1e27SAndreas Gohr    return 'show';
36269cd1e27SAndreas Gohr}
363f951a474SAndreas Gohr
36414a122deSAndreas Gohr/**
3651246e016SAndreas Gohr * Revert to a certain revision
3661246e016SAndreas Gohr *
3671246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3681246e016SAndreas Gohr */
3691246e016SAndreas Gohrfunction act_revert($act){
3701246e016SAndreas Gohr    global $ID;
3711246e016SAndreas Gohr    global $REV;
3721246e016SAndreas Gohr    global $lang;
373de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
374de4d479aSAdrian Lang    // global $INFO;
375de4d479aSAdrian Lang    // if (!$INFO['writable']) {
376de4d479aSAdrian Lang    //     return 'show';
377de4d479aSAdrian Lang    // }
3781246e016SAndreas Gohr
3791246e016SAndreas Gohr    // when no revision is given, delete current one
3801246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
3811246e016SAndreas Gohr    $text = '';
3821246e016SAndreas Gohr    $sum  = $lang['deleted'];
3831246e016SAndreas Gohr    if($REV){
3841246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
3851246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
386d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
3871246e016SAndreas Gohr    }
3881246e016SAndreas Gohr
3891246e016SAndreas Gohr    // spam check
3905a932e77SAdrian Lang
3915a932e77SAdrian Lang    if (checkwordblock($text)) {
3925a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3935a932e77SAdrian Lang        return 'edit';
3945a932e77SAdrian Lang    }
3951246e016SAndreas Gohr
3961246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
3971246e016SAndreas Gohr    msg($sum,1);
3981246e016SAndreas Gohr
3991246e016SAndreas Gohr    //delete any draft
4001246e016SAndreas Gohr    act_draftdel($act);
4011246e016SAndreas Gohr    session_write_close();
4021246e016SAndreas Gohr
4031246e016SAndreas Gohr    // when done, show current page
4041246e016SAndreas Gohr    $_SERVER['REQUEST_METHOD'] = 'post'; //should force a redirect
4051246e016SAndreas Gohr    $REV = '';
4061246e016SAndreas Gohr    return 'show';
4071246e016SAndreas Gohr}
4081246e016SAndreas Gohr
4091246e016SAndreas Gohr/**
41014a122deSAndreas Gohr * Do a redirect after receiving post data
41114a122deSAndreas Gohr *
41214a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
41314a122deSAndreas Gohr */
41469cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
41569cd1e27SAndreas Gohr    global $PRE;
41669cd1e27SAndreas Gohr    global $TEXT;
417f951a474SAndreas Gohr
41869cd1e27SAndreas Gohr    $opts = array(
41969cd1e27SAndreas Gohr            'id'       => $id,
42069cd1e27SAndreas Gohr            'preact'   => $preact
42169cd1e27SAndreas Gohr            );
422c66972f2SAdrian Lang    //get section name when coming from section edit
423c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
424c66972f2SAdrian Lang        $check = false; //Byref
425c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
426c66972f2SAdrian Lang    }
427c66972f2SAdrian Lang
42869cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
42969cd1e27SAndreas Gohr}
43069cd1e27SAndreas Gohr
43169cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
43269cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
433c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
43469cd1e27SAndreas Gohr
4356b13307fSandi    //show it
436af2408d5SAndreas Gohr    send_redirect($go);
4376b13307fSandi}
4386b13307fSandi
4396b13307fSandi/**
440b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4416b13307fSandi *
4426b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4436b13307fSandi */
4446b13307fSandifunction act_auth($act){
44508eda5bcSmatthiasgrimm    global $ID;
4467cace34dSAndreas Gohr    global $INFO;
44708eda5bcSmatthiasgrimm
4486b13307fSandi    //already logged in?
449c66972f2SAdrian Lang    if(isset($_SERVER['REMOTE_USER']) && $act=='login'){
450ca12ce46SAndreas Gohr        return 'show';
4512288dc06SGuy Brand    }
4526b13307fSandi
4536b13307fSandi    //handle logout
4546b13307fSandi    if($act=='logout'){
45508eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
456424c3c4fSJohannes Buchner        if($lockedby == $_SERVER['REMOTE_USER'])
45708eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
45808eda5bcSmatthiasgrimm
4597cace34dSAndreas Gohr        // do the logout stuff
4606b13307fSandi        auth_logoff();
4617cace34dSAndreas Gohr
4627cace34dSAndreas Gohr        // rebuild info array
4637cace34dSAndreas Gohr        $INFO = pageinfo();
4647cace34dSAndreas Gohr
465e16eccb7SGuy Brand        act_redirect($ID,'login');
4666b13307fSandi    }
4676b13307fSandi
4686b13307fSandi    return $act;
4696b13307fSandi}
4706b13307fSandi
4716b13307fSandi/**
47245a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
4736b13307fSandi *
4746b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4756b13307fSandi */
4766b13307fSandifunction act_edit($act){
477cd409024Sjorda    global $ID;
478ee4c4a1bSAndreas Gohr    global $INFO;
479cd409024Sjorda
48045a99335SAdrian Lang    global $TEXT;
48145a99335SAdrian Lang    global $RANGE;
48245a99335SAdrian Lang    global $PRE;
48345a99335SAdrian Lang    global $SUF;
48445a99335SAdrian Lang    global $REV;
48545a99335SAdrian Lang    global $SUM;
48645a99335SAdrian Lang    global $lang;
48745a99335SAdrian Lang    global $DATE;
48845a99335SAdrian Lang
48945a99335SAdrian Lang    if (!isset($TEXT)) {
49045a99335SAdrian Lang        if ($INFO['exists']) {
49145a99335SAdrian Lang            if ($RANGE) {
49245a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
49345a99335SAdrian Lang            } else {
49445a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
49545a99335SAdrian Lang            }
49645a99335SAdrian Lang        } else {
497fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
49845a99335SAdrian Lang        }
49945a99335SAdrian Lang    }
50045a99335SAdrian Lang
50145a99335SAdrian Lang    //set summary default
50245a99335SAdrian Lang    if(!$SUM){
50345a99335SAdrian Lang        if($REV){
5047656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
50545a99335SAdrian Lang        }elseif(!$INFO['exists']){
50645a99335SAdrian Lang            $SUM = $lang['created'];
50745a99335SAdrian Lang        }
50845a99335SAdrian Lang    }
50945a99335SAdrian Lang
5108d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5118d67c48aSAdrian Lang    // This is used for conflict detection
51278035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
51345a99335SAdrian Lang
5146b13307fSandi    //check if locked by anyone - if not lock for my self
51531bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
51631bc8f11SMichael Hamann    if ($INFO['writable']) {
5176b13307fSandi        $lockedby = checklock($ID);
5186b13307fSandi        if($lockedby) return 'locked';
5196b13307fSandi
5206b13307fSandi        lock($ID);
52131bc8f11SMichael Hamann    }
52231bc8f11SMichael Hamann
5236b13307fSandi    return $act;
5246b13307fSandi}
5256b13307fSandi
5266b13307fSandi/**
527f6dad9fdSMichael Klier * Export a wiki page for various formats
528f6dad9fdSMichael Klier *
529f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
530f6dad9fdSMichael Klier *
531f6dad9fdSMichael Klier *  Event data:
532f6dad9fdSMichael Klier *    data['id']      -- page id
533f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
534f6dad9fdSMichael Klier *    data['headers'] -- export headers
535f6dad9fdSMichael Klier *    data['output']  -- export output
5366b13307fSandi *
5376b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
538f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5396b13307fSandi */
5406b13307fSandifunction act_export($act){
5416b13307fSandi    global $ID;
5426b13307fSandi    global $REV;
54385f8705cSAnika Henke    global $conf;
54485f8705cSAnika Henke    global $lang;
5456b13307fSandi
546f6dad9fdSMichael Klier    $pre = '';
547f6dad9fdSMichael Klier    $post = '';
548f6dad9fdSMichael Klier    $output = '';
549f6dad9fdSMichael Klier    $headers = array();
550cc2ae802SAndreas Gohr
551f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
552f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
553f6dad9fdSMichael Klier
554ac83b9d8Sandi    $mode = substr($act,7);
555f6dad9fdSMichael Klier    switch($mode) {
556f6dad9fdSMichael Klier        case 'raw':
5575adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
55866b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
559f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
560f6dad9fdSMichael Klier            break;
561f6dad9fdSMichael Klier        case 'xhtml':
562f6dad9fdSMichael Klier            $pre .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . DOKU_LF;
563f6dad9fdSMichael Klier            $pre .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . DOKU_LF;
564f6dad9fdSMichael Klier            $pre .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"' . DOKU_LF;
565f6dad9fdSMichael Klier            $pre .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
566f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
567f6dad9fdSMichael Klier            $pre .= '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . DOKU_LF;
568f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
569f6dad9fdSMichael Klier
570f6dad9fdSMichael Klier            // get metaheaders
571f6dad9fdSMichael Klier            ob_start();
572f6dad9fdSMichael Klier            tpl_metaheaders();
573f6dad9fdSMichael Klier            $pre .= ob_get_clean();
574f6dad9fdSMichael Klier
575f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
576f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
577f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
578f6dad9fdSMichael Klier
579f6dad9fdSMichael Klier            // get toc
580f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
581f6dad9fdSMichael Klier
582f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
583f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
584f6dad9fdSMichael Klier
585f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
586f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
587f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
588f6dad9fdSMichael Klier            break;
589f6dad9fdSMichael Klier        case 'xhtmlbody':
590f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
591f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
592f6dad9fdSMichael Klier            break;
593f6dad9fdSMichael Klier        default:
594f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
5959acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
596f6dad9fdSMichael Klier            break;
597f6dad9fdSMichael Klier    }
598f6dad9fdSMichael Klier
599f6dad9fdSMichael Klier    // prepare event data
600f6dad9fdSMichael Klier    $data = array();
601f6dad9fdSMichael Klier    $data['id'] = $ID;
602f6dad9fdSMichael Klier    $data['mode'] = $mode;
603f6dad9fdSMichael Klier    $data['headers'] = $headers;
604f6dad9fdSMichael Klier    $data['output'] =& $output;
605f6dad9fdSMichael Klier
606f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
607f6dad9fdSMichael Klier
608f6dad9fdSMichael Klier    if(!empty($data['output'])){
609f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
61085767031SAndreas Gohr            header("$key: $val");
61185767031SAndreas Gohr        }
612f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6136b13307fSandi        exit;
6146b13307fSandi    }
6156b13307fSandi    return 'show';
6166b13307fSandi}
617340756e4Sandi
618b158d625SSteven Danz/**
619c4f79b71SMichael Hamann * Handle sitemap delivery
620c4f79b71SMichael Hamann *
621c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
622c4f79b71SMichael Hamann */
623c4f79b71SMichael Hamannfunction act_sitemap($act) {
624c4f79b71SMichael Hamann    global $conf;
625c4f79b71SMichael Hamann
626eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
627c4f79b71SMichael Hamann        header("HTTP/1.0 404 Not Found");
628c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
629c4f79b71SMichael Hamann        exit;
630c4f79b71SMichael Hamann    }
631c4f79b71SMichael Hamann
632eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
633eae17177SMichael Hamann    if(strrchr($sitemap, '.') === '.gz'){
634c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
635c4f79b71SMichael Hamann    }else{
636c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
637c4f79b71SMichael Hamann    }
638c4f79b71SMichael Hamann
639c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
640c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
6412897eb23SMichael Hamann        Sitemapper::generate();
642c4f79b71SMichael Hamann    }
643c4f79b71SMichael Hamann
644c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
645c4f79b71SMichael Hamann        // Send headers
646c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
6474c36bf82SGuillaume Turri        header('Content-Disposition: attachment; filename='.basename($sitemap));
648c4f79b71SMichael Hamann
649eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
650eae17177SMichael Hamann
651c4f79b71SMichael Hamann        // Send file
652c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
653c4f79b71SMichael Hamann        if (http_sendfile($sitemap)) exit;
654c4f79b71SMichael Hamann
655eae17177SMichael Hamann        readfile($sitemap);
656c4f79b71SMichael Hamann        exit;
657c4f79b71SMichael Hamann    }
658c4f79b71SMichael Hamann
659c4f79b71SMichael Hamann    header("HTTP/1.0 500 Internal Server Error");
660eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
661c4f79b71SMichael Hamann    exit;
662c4f79b71SMichael Hamann}
663c4f79b71SMichael Hamann
664c4f79b71SMichael Hamann/**
6655b75cd1fSAdrian Lang * Handle page 'subscribe'
666b158d625SSteven Danz *
6675b75cd1fSAdrian Lang * Throws exception on error.
6685b75cd1fSAdrian Lang *
6695b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
670b158d625SSteven Danz */
6711380fc45SAndreas Gohrfunction act_subscription($act){
672056c2049SAndreas Gohr    global $lang;
673056c2049SAndreas Gohr    global $INFO;
674056c2049SAndreas Gohr    global $ID;
675*90f1b7bdSTom N Harris    global $INPUT;
67652b0dd67SGuy Brand
6779fa341d0SAndreas Gohr    // subcriptions work for logged in users only
6789fa341d0SAndreas Gohr    if(!$_SERVER['REMOTE_USER']) return 'show';
6799fa341d0SAndreas Gohr
680056c2049SAndreas Gohr    // get and preprocess data.
6818881fcc9SAdrian Lang    $params = array();
6828881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
683*90f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
684*90f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
6858881fcc9SAdrian Lang        }
6868881fcc9SAdrian Lang    }
6878881fcc9SAdrian Lang
688056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
68966d2bed9SAdrian Lang    if(!$params['action'] || !checkSecurityToken()) return $act;
690056c2049SAndreas Gohr
6918881fcc9SAdrian Lang    // Handle POST data, may throw exception.
6928881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
6938881fcc9SAdrian Lang
6948881fcc9SAdrian Lang    $target = $params['target'];
6958881fcc9SAdrian Lang    $style  = $params['style'];
6968881fcc9SAdrian Lang    $data   = $params['data'];
6978881fcc9SAdrian Lang    $action = $params['action'];
6988881fcc9SAdrian Lang
6998881fcc9SAdrian Lang    // Perform action.
7008881fcc9SAdrian Lang    if (!subscription_set($_SERVER['REMOTE_USER'], $target, $style, $data)) {
7018881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
7028881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
7038881fcc9SAdrian Lang                                    prettyprint_id($target)));
7048881fcc9SAdrian Lang    }
7058881fcc9SAdrian Lang    msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
7068881fcc9SAdrian Lang                prettyprint_id($target)), 1);
707cb3f9dbaSAdrian Lang    act_redirect($ID, $act);
708cb3f9dbaSAdrian Lang
709cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
710cb3f9dbaSAdrian Lang    $INFO['subscribed'] = get_info_subscribed();
711cb3f9dbaSAdrian Lang    return 'show';
7128881fcc9SAdrian Lang}
7138881fcc9SAdrian Lang
7148881fcc9SAdrian Lang/**
7158881fcc9SAdrian Lang * Validate POST data
7168881fcc9SAdrian Lang *
7178881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
7188881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
7198881fcc9SAdrian Lang *
7208881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
7218881fcc9SAdrian Lang */
7227a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
7238881fcc9SAdrian Lang    global $INFO;
7248881fcc9SAdrian Lang    global $lang;
7258881fcc9SAdrian Lang
7265b75cd1fSAdrian Lang    // Get and validate parameters.
7278881fcc9SAdrian Lang    if (!isset($params['target'])) {
72815741132SAndreas Gohr        throw new Exception('no subscription target given');
7295b75cd1fSAdrian Lang    }
7308881fcc9SAdrian Lang    $target = $params['target'];
7315b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
7325b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
7335b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
7345b75cd1fSAdrian Lang        $valid_styles[] = 'list';
7355b75cd1fSAdrian Lang    }
7368881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
73715741132SAndreas Gohr                              'invalid subscription style given');
7388881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
73915741132SAndreas Gohr                              $params, 'invalid subscription action given');
740613964ecSGuy Brand
7415b75cd1fSAdrian Lang    // Check other conditions.
7425b75cd1fSAdrian Lang    if ($action === 'subscribe') {
7435b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
7445b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
74552b0dd67SGuy Brand        }
7465b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
7475b75cd1fSAdrian Lang        $is = false;
7485b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
7495b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
7505b75cd1fSAdrian Lang                $is = true;
75152b0dd67SGuy Brand            }
75252b0dd67SGuy Brand        }
7535b75cd1fSAdrian Lang        if ($is === false) {
75415741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
75515741132SAndreas Gohr                                        $_SERVER['REMOTE_USER'],
7565b75cd1fSAdrian Lang                                        prettyprint_id($target)));
7575b75cd1fSAdrian Lang        }
7585b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
7595b75cd1fSAdrian Lang        $style = null;
76052b0dd67SGuy Brand    }
76152b0dd67SGuy Brand
7628881fcc9SAdrian Lang    $data = in_array($style, array('list', 'digest')) ? time() : null;
7638881fcc9SAdrian Lang    $params = compact('target', 'style', 'data', 'action');
76452b0dd67SGuy Brand}
76552b0dd67SGuy Brand
766e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
767