xref: /dokuwiki/inc/actions.php (revision 62baad0f61a13ec01791a8cdc8a7dbbd78f6a567)
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;
2390f1b7bdSTom 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
34*62baad0fSMartin Doucha        $ACT = act_validate($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']
13590f1b7bdSTom N Harris            if (($page = $INPUT->str('page', '', true)) != '') {
13611e2ce22Schris                $pluginlist = plugin_list('admin');
13790f1b7bdSTom N Harris                if (in_array($page, $pluginlist)) {
13811e2ce22Schris                    // attempt to load the plugin
13990f1b7bdSTom 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
14290f1b7bdSTom 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 * @author Andreas Gohr <andi@splitbrain.org>
188af182434Sandi */
189af182434Sandifunction act_clean($act){
190af182434Sandi    global $lang;
19160e6b550SAndreas Gohr    global $conf;
192c828a5d6SAndreas Gohr    global $INFO;
193af182434Sandi
194ee4c4a1bSAndreas Gohr    // check if the action was given as array key
195ee4c4a1bSAndreas Gohr    if(is_array($act)){
196ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
197ee4c4a1bSAndreas Gohr    }
198ee4c4a1bSAndreas Gohr
199ac83b9d8Sandi    //remove all bad chars
200ac83b9d8Sandi    $act = strtolower($act);
2012d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
202ac83b9d8Sandi
203ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
204cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
205b146b32bSandi
206396c218fSAndreas Gohr    if($act === '') $act = 'show';
207*62baad0fSMartin Doucha    return $act;
208*62baad0fSMartin Doucha}
209*62baad0fSMartin Doucha
210*62baad0fSMartin Doucha/**
211*62baad0fSMartin Doucha * Sanitize and validate action commands.
212*62baad0fSMartin Doucha *
213*62baad0fSMartin Doucha * Add all allowed commands here.
214*62baad0fSMartin Doucha *
215*62baad0fSMartin Doucha * @author Andreas Gohr <andi@splitbrain.org>
216*62baad0fSMartin Doucha */
217*62baad0fSMartin Douchafunction act_validate($act) {
218*62baad0fSMartin Doucha    $act = act_clean($act);
219396c218fSAndreas Gohr
220409d7af7SAndreas Gohr    // check if action is disabled
221409d7af7SAndreas Gohr    if(!actionOK($act)){
222409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
223409d7af7SAndreas Gohr        return 'show';
224409d7af7SAndreas Gohr    }
225409d7af7SAndreas Gohr
22660e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
22760e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2281246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
229d5a9514cSAdrian Lang                    'resendpwd'))){
23060e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
23160e6b550SAndreas Gohr        return 'show';
23260e6b550SAndreas Gohr    }
23360e6b550SAndreas Gohr
234c828a5d6SAndreas Gohr    //is there really a draft?
235c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
236c828a5d6SAndreas Gohr
237067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
238ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2391246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2405a932e77SAdrian Lang                    'unsubscribe','profile','resendpwd','recover',
241d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
242ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
243af182434Sandi        return 'show';
244af182434Sandi    }
245af182434Sandi    return $act;
246af182434Sandi}
247af182434Sandi
248af182434Sandi/**
2496b13307fSandi * Run permissionchecks
2506b13307fSandi *
2516b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2526b13307fSandi */
2536b13307fSandifunction act_permcheck($act){
254dbbc6aa7Sandi    global $INFO;
2555e199953Smatthiasgrimm    global $conf;
256dbbc6aa7Sandi
257ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2586b13307fSandi        if($INFO['exists']){
259bdbc16bfSandi            if($act == 'edit'){
260bdbc16bfSandi                //the edit function will check again and do a source show
261bdbc16bfSandi                //when no AUTH_EDIT available
262bdbc16bfSandi                $permneed = AUTH_READ;
263bdbc16bfSandi            }else{
2646b13307fSandi                $permneed = AUTH_EDIT;
265bdbc16bfSandi            }
2666b13307fSandi        }else{
2676b13307fSandi            $permneed = AUTH_CREATE;
2686b13307fSandi        }
269c4f79b71SMichael Hamann    }elseif(in_array($act,array('login','search','recent','profile','index', 'sitemap'))){
2706b13307fSandi        $permneed = AUTH_NONE;
2711246e016SAndreas Gohr    }elseif($act == 'revert'){
2721246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
2731246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
2745e199953Smatthiasgrimm    }elseif($act == 'register'){
2755e199953Smatthiasgrimm        $permneed = AUTH_NONE;
276ebd3d9ceSchris    }elseif($act == 'resendpwd'){
277ebd3d9ceSchris        $permneed = AUTH_NONE;
278c19fe9c0Sandi    }elseif($act == 'admin'){
279f8cc712eSAndreas Gohr        if($INFO['ismanager']){
280f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
281f8cc712eSAndreas Gohr            // action is checked later
282f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
283f8cc712eSAndreas Gohr        }else{
284c19fe9c0Sandi            $permneed = AUTH_ADMIN;
285f8cc712eSAndreas Gohr        }
2866b13307fSandi    }else{
2876b13307fSandi        $permneed = AUTH_READ;
2886b13307fSandi    }
289dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
290dbbc6aa7Sandi
2916b13307fSandi    return 'denied';
2926b13307fSandi}
2936b13307fSandi
2946b13307fSandi/**
295ee4c4a1bSAndreas Gohr * Handle 'draftdel'
296ee4c4a1bSAndreas Gohr *
297ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
298ee4c4a1bSAndreas Gohr */
299ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
300ee4c4a1bSAndreas Gohr    global $INFO;
301ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
302ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
303ee4c4a1bSAndreas Gohr    return 'show';
304ee4c4a1bSAndreas Gohr}
305ee4c4a1bSAndreas Gohr
306ee4c4a1bSAndreas Gohr/**
307ee4c4a1bSAndreas Gohr * Saves a draft on preview
308ee4c4a1bSAndreas Gohr *
309ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
310ee4c4a1bSAndreas Gohr */
311ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
312ee4c4a1bSAndreas Gohr    global $INFO;
313ee4c4a1bSAndreas Gohr    global $ID;
31490f1b7bdSTom N Harris    global $INPUT;
315ee4c4a1bSAndreas Gohr    global $conf;
31690f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
317ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
31890f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
31990f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
32090f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
32190f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
322ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
323ee4c4a1bSAndreas Gohr                );
324ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
325ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
326ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
327ee4c4a1bSAndreas Gohr        }
328ee4c4a1bSAndreas Gohr    }
329ee4c4a1bSAndreas Gohr    return $act;
330ee4c4a1bSAndreas Gohr}
331ee4c4a1bSAndreas Gohr
332ee4c4a1bSAndreas Gohr/**
3336b13307fSandi * Handle 'save'
3346b13307fSandi *
3356b13307fSandi * Checks for spam and conflicts and saves the page.
3366b13307fSandi * Does a redirect to show the page afterwards or
3376b13307fSandi * returns a new action.
3386b13307fSandi *
3396b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3406b13307fSandi */
3416b13307fSandifunction act_save($act){
3426b13307fSandi    global $ID;
3436b13307fSandi    global $DATE;
3446b13307fSandi    global $PRE;
3456b13307fSandi    global $TEXT;
3466b13307fSandi    global $SUF;
3476b13307fSandi    global $SUM;
3485a932e77SAdrian Lang    global $lang;
3498d67c48aSAdrian Lang    global $INFO;
35090f1b7bdSTom N Harris    global $INPUT;
3516b13307fSandi
3526b13307fSandi    //spam check
3535a932e77SAdrian Lang    if(checkwordblock()) {
3545a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3555a932e77SAdrian Lang        return 'edit';
3565a932e77SAdrian Lang    }
3578d67c48aSAdrian Lang    //conflict check
3588d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3596b13307fSandi        return 'conflict';
3606b13307fSandi
3616b13307fSandi    //save it
36290f1b7bdSTom N Harris    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$INPUT->bool('minor')); //use pretty mode for con
3636b13307fSandi    //unlock it
3646b13307fSandi    unlock($ID);
3656b13307fSandi
366ee4c4a1bSAndreas Gohr    //delete draft
367ee4c4a1bSAndreas Gohr    act_draftdel($act);
36869cd1e27SAndreas Gohr    session_write_close();
369ee4c4a1bSAndreas Gohr
37069cd1e27SAndreas Gohr    // when done, show page
37169cd1e27SAndreas Gohr    return 'show';
37269cd1e27SAndreas Gohr}
373f951a474SAndreas Gohr
37414a122deSAndreas Gohr/**
3751246e016SAndreas Gohr * Revert to a certain revision
3761246e016SAndreas Gohr *
3771246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3781246e016SAndreas Gohr */
3791246e016SAndreas Gohrfunction act_revert($act){
3801246e016SAndreas Gohr    global $ID;
3811246e016SAndreas Gohr    global $REV;
3821246e016SAndreas Gohr    global $lang;
383de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
384de4d479aSAdrian Lang    // global $INFO;
385de4d479aSAdrian Lang    // if (!$INFO['writable']) {
386de4d479aSAdrian Lang    //     return 'show';
387de4d479aSAdrian Lang    // }
3881246e016SAndreas Gohr
3891246e016SAndreas Gohr    // when no revision is given, delete current one
3901246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
3911246e016SAndreas Gohr    $text = '';
3921246e016SAndreas Gohr    $sum  = $lang['deleted'];
3931246e016SAndreas Gohr    if($REV){
3941246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
3951246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
396d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
3971246e016SAndreas Gohr    }
3981246e016SAndreas Gohr
3991246e016SAndreas Gohr    // spam check
4005a932e77SAdrian Lang
4015a932e77SAdrian Lang    if (checkwordblock($text)) {
4025a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4035a932e77SAdrian Lang        return 'edit';
4045a932e77SAdrian Lang    }
4051246e016SAndreas Gohr
4061246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
4071246e016SAndreas Gohr    msg($sum,1);
4081246e016SAndreas Gohr
4091246e016SAndreas Gohr    //delete any draft
4101246e016SAndreas Gohr    act_draftdel($act);
4111246e016SAndreas Gohr    session_write_close();
4121246e016SAndreas Gohr
4131246e016SAndreas Gohr    // when done, show current page
4141246e016SAndreas Gohr    $_SERVER['REQUEST_METHOD'] = 'post'; //should force a redirect
4151246e016SAndreas Gohr    $REV = '';
4161246e016SAndreas Gohr    return 'show';
4171246e016SAndreas Gohr}
4181246e016SAndreas Gohr
4191246e016SAndreas Gohr/**
42014a122deSAndreas Gohr * Do a redirect after receiving post data
42114a122deSAndreas Gohr *
42214a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
42314a122deSAndreas Gohr */
42469cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
42569cd1e27SAndreas Gohr    global $PRE;
42669cd1e27SAndreas Gohr    global $TEXT;
427f951a474SAndreas Gohr
42869cd1e27SAndreas Gohr    $opts = array(
42969cd1e27SAndreas Gohr            'id'       => $id,
43069cd1e27SAndreas Gohr            'preact'   => $preact
43169cd1e27SAndreas Gohr            );
432c66972f2SAdrian Lang    //get section name when coming from section edit
433c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
434c66972f2SAdrian Lang        $check = false; //Byref
435c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
436c66972f2SAdrian Lang    }
437c66972f2SAdrian Lang
43869cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
43969cd1e27SAndreas Gohr}
44069cd1e27SAndreas Gohr
44169cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
44269cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
443c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
44469cd1e27SAndreas Gohr
4456b13307fSandi    //show it
446af2408d5SAndreas Gohr    send_redirect($go);
4476b13307fSandi}
4486b13307fSandi
4496b13307fSandi/**
450b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4516b13307fSandi *
4526b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4536b13307fSandi */
4546b13307fSandifunction act_auth($act){
45508eda5bcSmatthiasgrimm    global $ID;
4567cace34dSAndreas Gohr    global $INFO;
45708eda5bcSmatthiasgrimm
4586b13307fSandi    //already logged in?
459c66972f2SAdrian Lang    if(isset($_SERVER['REMOTE_USER']) && $act=='login'){
460ca12ce46SAndreas Gohr        return 'show';
4612288dc06SGuy Brand    }
4626b13307fSandi
4636b13307fSandi    //handle logout
4646b13307fSandi    if($act=='logout'){
46508eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
466424c3c4fSJohannes Buchner        if($lockedby == $_SERVER['REMOTE_USER'])
46708eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
46808eda5bcSmatthiasgrimm
4697cace34dSAndreas Gohr        // do the logout stuff
4706b13307fSandi        auth_logoff();
4717cace34dSAndreas Gohr
4727cace34dSAndreas Gohr        // rebuild info array
4737cace34dSAndreas Gohr        $INFO = pageinfo();
4747cace34dSAndreas Gohr
475e16eccb7SGuy Brand        act_redirect($ID,'login');
4766b13307fSandi    }
4776b13307fSandi
4786b13307fSandi    return $act;
4796b13307fSandi}
4806b13307fSandi
4816b13307fSandi/**
48245a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
4836b13307fSandi *
4846b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4856b13307fSandi */
4866b13307fSandifunction act_edit($act){
487cd409024Sjorda    global $ID;
488ee4c4a1bSAndreas Gohr    global $INFO;
489cd409024Sjorda
49045a99335SAdrian Lang    global $TEXT;
49145a99335SAdrian Lang    global $RANGE;
49245a99335SAdrian Lang    global $PRE;
49345a99335SAdrian Lang    global $SUF;
49445a99335SAdrian Lang    global $REV;
49545a99335SAdrian Lang    global $SUM;
49645a99335SAdrian Lang    global $lang;
49745a99335SAdrian Lang    global $DATE;
49845a99335SAdrian Lang
49945a99335SAdrian Lang    if (!isset($TEXT)) {
50045a99335SAdrian Lang        if ($INFO['exists']) {
50145a99335SAdrian Lang            if ($RANGE) {
50245a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
50345a99335SAdrian Lang            } else {
50445a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
50545a99335SAdrian Lang            }
50645a99335SAdrian Lang        } else {
507fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
50845a99335SAdrian Lang        }
50945a99335SAdrian Lang    }
51045a99335SAdrian Lang
51145a99335SAdrian Lang    //set summary default
51245a99335SAdrian Lang    if(!$SUM){
51345a99335SAdrian Lang        if($REV){
5147656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
51545a99335SAdrian Lang        }elseif(!$INFO['exists']){
51645a99335SAdrian Lang            $SUM = $lang['created'];
51745a99335SAdrian Lang        }
51845a99335SAdrian Lang    }
51945a99335SAdrian Lang
5208d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5218d67c48aSAdrian Lang    // This is used for conflict detection
52278035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
52345a99335SAdrian Lang
5246b13307fSandi    //check if locked by anyone - if not lock for my self
52531bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
52631bc8f11SMichael Hamann    if ($INFO['writable']) {
5276b13307fSandi        $lockedby = checklock($ID);
5286b13307fSandi        if($lockedby) return 'locked';
5296b13307fSandi
5306b13307fSandi        lock($ID);
53131bc8f11SMichael Hamann    }
53231bc8f11SMichael Hamann
5336b13307fSandi    return $act;
5346b13307fSandi}
5356b13307fSandi
5366b13307fSandi/**
537f6dad9fdSMichael Klier * Export a wiki page for various formats
538f6dad9fdSMichael Klier *
539f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
540f6dad9fdSMichael Klier *
541f6dad9fdSMichael Klier *  Event data:
542f6dad9fdSMichael Klier *    data['id']      -- page id
543f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
544f6dad9fdSMichael Klier *    data['headers'] -- export headers
545f6dad9fdSMichael Klier *    data['output']  -- export output
5466b13307fSandi *
5476b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
548f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5496b13307fSandi */
5506b13307fSandifunction act_export($act){
5516b13307fSandi    global $ID;
5526b13307fSandi    global $REV;
55385f8705cSAnika Henke    global $conf;
55485f8705cSAnika Henke    global $lang;
5556b13307fSandi
556f6dad9fdSMichael Klier    $pre = '';
557f6dad9fdSMichael Klier    $post = '';
558f6dad9fdSMichael Klier    $output = '';
559f6dad9fdSMichael Klier    $headers = array();
560cc2ae802SAndreas Gohr
561f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
562f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
563f6dad9fdSMichael Klier
564ac83b9d8Sandi    $mode = substr($act,7);
565f6dad9fdSMichael Klier    switch($mode) {
566f6dad9fdSMichael Klier        case 'raw':
5675adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
56866b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
569f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
570f6dad9fdSMichael Klier            break;
571f6dad9fdSMichael Klier        case 'xhtml':
572f6dad9fdSMichael Klier            $pre .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . DOKU_LF;
573f6dad9fdSMichael Klier            $pre .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . DOKU_LF;
574f6dad9fdSMichael Klier            $pre .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"' . DOKU_LF;
575f6dad9fdSMichael Klier            $pre .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
576f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
577f6dad9fdSMichael Klier            $pre .= '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . DOKU_LF;
578f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
579f6dad9fdSMichael Klier
580f6dad9fdSMichael Klier            // get metaheaders
581f6dad9fdSMichael Klier            ob_start();
582f6dad9fdSMichael Klier            tpl_metaheaders();
583f6dad9fdSMichael Klier            $pre .= ob_get_clean();
584f6dad9fdSMichael Klier
585f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
586f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
587f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
588f6dad9fdSMichael Klier
589f6dad9fdSMichael Klier            // get toc
590f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
591f6dad9fdSMichael Klier
592f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
593f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
594f6dad9fdSMichael Klier
595f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
596f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
597f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
598f6dad9fdSMichael Klier            break;
599f6dad9fdSMichael Klier        case 'xhtmlbody':
600f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
601f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
602f6dad9fdSMichael Klier            break;
603f6dad9fdSMichael Klier        default:
604f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
6059acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
606f6dad9fdSMichael Klier            break;
607f6dad9fdSMichael Klier    }
608f6dad9fdSMichael Klier
609f6dad9fdSMichael Klier    // prepare event data
610f6dad9fdSMichael Klier    $data = array();
611f6dad9fdSMichael Klier    $data['id'] = $ID;
612f6dad9fdSMichael Klier    $data['mode'] = $mode;
613f6dad9fdSMichael Klier    $data['headers'] = $headers;
614f6dad9fdSMichael Klier    $data['output'] =& $output;
615f6dad9fdSMichael Klier
616f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
617f6dad9fdSMichael Klier
618f6dad9fdSMichael Klier    if(!empty($data['output'])){
619f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
62085767031SAndreas Gohr            header("$key: $val");
62185767031SAndreas Gohr        }
622f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6236b13307fSandi        exit;
6246b13307fSandi    }
6256b13307fSandi    return 'show';
6266b13307fSandi}
627340756e4Sandi
628b158d625SSteven Danz/**
629c4f79b71SMichael Hamann * Handle sitemap delivery
630c4f79b71SMichael Hamann *
631c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
632c4f79b71SMichael Hamann */
633c4f79b71SMichael Hamannfunction act_sitemap($act) {
634c4f79b71SMichael Hamann    global $conf;
635c4f79b71SMichael Hamann
636eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
637c4f79b71SMichael Hamann        header("HTTP/1.0 404 Not Found");
638c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
639c4f79b71SMichael Hamann        exit;
640c4f79b71SMichael Hamann    }
641c4f79b71SMichael Hamann
642eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
643eae17177SMichael Hamann    if(strrchr($sitemap, '.') === '.gz'){
644c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
645c4f79b71SMichael Hamann    }else{
646c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
647c4f79b71SMichael Hamann    }
648c4f79b71SMichael Hamann
649c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
650c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
6512897eb23SMichael Hamann        Sitemapper::generate();
652c4f79b71SMichael Hamann    }
653c4f79b71SMichael Hamann
654c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
655c4f79b71SMichael Hamann        // Send headers
656c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
6574c36bf82SGuillaume Turri        header('Content-Disposition: attachment; filename='.basename($sitemap));
658c4f79b71SMichael Hamann
659eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
660eae17177SMichael Hamann
661c4f79b71SMichael Hamann        // Send file
662c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
663c4f79b71SMichael Hamann        if (http_sendfile($sitemap)) exit;
664c4f79b71SMichael Hamann
665eae17177SMichael Hamann        readfile($sitemap);
666c4f79b71SMichael Hamann        exit;
667c4f79b71SMichael Hamann    }
668c4f79b71SMichael Hamann
669c4f79b71SMichael Hamann    header("HTTP/1.0 500 Internal Server Error");
670eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
671c4f79b71SMichael Hamann    exit;
672c4f79b71SMichael Hamann}
673c4f79b71SMichael Hamann
674c4f79b71SMichael Hamann/**
6755b75cd1fSAdrian Lang * Handle page 'subscribe'
676b158d625SSteven Danz *
6775b75cd1fSAdrian Lang * Throws exception on error.
6785b75cd1fSAdrian Lang *
6795b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
680b158d625SSteven Danz */
6811380fc45SAndreas Gohrfunction act_subscription($act){
682056c2049SAndreas Gohr    global $lang;
683056c2049SAndreas Gohr    global $INFO;
684056c2049SAndreas Gohr    global $ID;
68590f1b7bdSTom N Harris    global $INPUT;
68652b0dd67SGuy Brand
6879fa341d0SAndreas Gohr    // subcriptions work for logged in users only
6889fa341d0SAndreas Gohr    if(!$_SERVER['REMOTE_USER']) return 'show';
6899fa341d0SAndreas Gohr
690056c2049SAndreas Gohr    // get and preprocess data.
6918881fcc9SAdrian Lang    $params = array();
6928881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
69390f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
69490f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
6958881fcc9SAdrian Lang        }
6968881fcc9SAdrian Lang    }
6978881fcc9SAdrian Lang
698056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
69966d2bed9SAdrian Lang    if(!$params['action'] || !checkSecurityToken()) return $act;
700056c2049SAndreas Gohr
7018881fcc9SAdrian Lang    // Handle POST data, may throw exception.
7028881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7038881fcc9SAdrian Lang
7048881fcc9SAdrian Lang    $target = $params['target'];
7058881fcc9SAdrian Lang    $style  = $params['style'];
7068881fcc9SAdrian Lang    $data   = $params['data'];
7078881fcc9SAdrian Lang    $action = $params['action'];
7088881fcc9SAdrian Lang
7098881fcc9SAdrian Lang    // Perform action.
7108881fcc9SAdrian Lang    if (!subscription_set($_SERVER['REMOTE_USER'], $target, $style, $data)) {
7118881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
7128881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
7138881fcc9SAdrian Lang                                    prettyprint_id($target)));
7148881fcc9SAdrian Lang    }
7158881fcc9SAdrian Lang    msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
7168881fcc9SAdrian Lang                prettyprint_id($target)), 1);
717cb3f9dbaSAdrian Lang    act_redirect($ID, $act);
718cb3f9dbaSAdrian Lang
719cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
720cb3f9dbaSAdrian Lang    $INFO['subscribed'] = get_info_subscribed();
721cb3f9dbaSAdrian Lang    return 'show';
7228881fcc9SAdrian Lang}
7238881fcc9SAdrian Lang
7248881fcc9SAdrian Lang/**
7258881fcc9SAdrian Lang * Validate POST data
7268881fcc9SAdrian Lang *
7278881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
7288881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
7298881fcc9SAdrian Lang *
7308881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
7318881fcc9SAdrian Lang */
7327a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
7338881fcc9SAdrian Lang    global $INFO;
7348881fcc9SAdrian Lang    global $lang;
7358881fcc9SAdrian Lang
7365b75cd1fSAdrian Lang    // Get and validate parameters.
7378881fcc9SAdrian Lang    if (!isset($params['target'])) {
73815741132SAndreas Gohr        throw new Exception('no subscription target given');
7395b75cd1fSAdrian Lang    }
7408881fcc9SAdrian Lang    $target = $params['target'];
7415b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
7425b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
7435b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
7445b75cd1fSAdrian Lang        $valid_styles[] = 'list';
7455b75cd1fSAdrian Lang    }
7468881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
74715741132SAndreas Gohr                              'invalid subscription style given');
7488881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
74915741132SAndreas Gohr                              $params, 'invalid subscription action given');
750613964ecSGuy Brand
7515b75cd1fSAdrian Lang    // Check other conditions.
7525b75cd1fSAdrian Lang    if ($action === 'subscribe') {
7535b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
7545b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
75552b0dd67SGuy Brand        }
7565b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
7575b75cd1fSAdrian Lang        $is = false;
7585b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
7595b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
7605b75cd1fSAdrian Lang                $is = true;
76152b0dd67SGuy Brand            }
76252b0dd67SGuy Brand        }
7635b75cd1fSAdrian Lang        if ($is === false) {
76415741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
76515741132SAndreas Gohr                                        $_SERVER['REMOTE_USER'],
7665b75cd1fSAdrian Lang                                        prettyprint_id($target)));
7675b75cd1fSAdrian Lang        }
7685b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
7695b75cd1fSAdrian Lang        $style = null;
77052b0dd67SGuy Brand    }
77152b0dd67SGuy Brand
7728881fcc9SAdrian Lang    $data = in_array($style, array('list', 'digest')) ? time() : null;
7738881fcc9SAdrian Lang    $params = compact('target', 'style', 'data', 'action');
77452b0dd67SGuy Brand}
77552b0dd67SGuy Brand
776e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
777