xref: /dokuwiki/inc/actions.php (revision d5a9514cb7286254b1dcf65ba764234e199b8b7f)
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;
236b13307fSandi    global $lang;
2485dcda20SRobin Getz    global $conf;
256b13307fSandi
2669cd1e27SAndreas Gohr    $preact = $ACT;
2769cd1e27SAndreas Gohr
28c2e830f2Schris    // give plugins an opportunity to process the action
2924bb549bSchris    $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT);
3024bb549bSchris    if ($evt->advise_before()) {
31c2e830f2Schris
32af182434Sandi        //sanitize $ACT
33af182434Sandi        $ACT = act_clean($ACT);
34af182434Sandi
35b8957367SBenjamin Gilbert        //check if searchword was given - else just show
360868021bSAndreas Gohr        $s = cleanID($QUERY);
370868021bSAndreas Gohr        if($ACT == 'search' && empty($s)){
38b8957367SBenjamin Gilbert            $ACT = 'show';
39b8957367SBenjamin Gilbert        }
40b8957367SBenjamin Gilbert
41b8957367SBenjamin Gilbert        //login stuff
421b2a85e8SAndreas Gohr        if(in_array($ACT,array('login','logout'))){
43b8957367SBenjamin Gilbert            $ACT = act_auth($ACT);
441b2a85e8SAndreas Gohr        }
45b8957367SBenjamin Gilbert
461380fc45SAndreas Gohr        //check if user is asking to (un)subscribe a page
475b75cd1fSAdrian Lang        if($ACT == 'subscribe') {
485b75cd1fSAdrian Lang            try {
491380fc45SAndreas Gohr                $ACT = act_subscription($ACT);
505b75cd1fSAdrian Lang            } catch (Exception $e) {
515b75cd1fSAdrian Lang                msg($e->getMessage(), -1);
525b75cd1fSAdrian Lang            }
535b75cd1fSAdrian Lang        }
5452b0dd67SGuy Brand
554064e2d3SRobin Getz        //display some infos
564064e2d3SRobin Getz        if($ACT == 'check'){
574064e2d3SRobin Getz            check();
584064e2d3SRobin Getz            $ACT = 'show';
594064e2d3SRobin Getz        }
604064e2d3SRobin Getz
616b13307fSandi        //check permissions
626b13307fSandi        $ACT = act_permcheck($ACT);
636b13307fSandi
64c4f79b71SMichael Hamann        //sitemap
65eae17177SMichael Hamann        if ($ACT == 'sitemap'){
66c4f79b71SMichael Hamann            $ACT = act_sitemap($ACT);
67eae17177SMichael Hamann        }
68c4f79b71SMichael Hamann
69b8957367SBenjamin Gilbert        //register
70b3510079SAndreas Gohr        if($ACT == 'register' && $_POST['save'] && register()){
71b8957367SBenjamin Gilbert            $ACT = 'login';
72b8957367SBenjamin Gilbert        }
736b13307fSandi
748b06d178Schris        if ($ACT == 'resendpwd' && act_resendpwd()) {
758b06d178Schris            $ACT = 'login';
768b06d178Schris        }
778b06d178Schris
788b06d178Schris        //update user profile
7925b2a98cSMichael Klier        if ($ACT == 'profile') {
8025b2a98cSMichael Klier            if(!$_SERVER['REMOTE_USER']) {
8125b2a98cSMichael Klier                $ACT = 'login';
8225b2a98cSMichael Klier            } else {
8325b2a98cSMichael Klier                if(updateprofile()) {
844cb79657SMatthias Grimm                    msg($lang['profchanged'],1);
854cb79657SMatthias Grimm                    $ACT = 'show';
868b06d178Schris                }
8725b2a98cSMichael Klier            }
8825b2a98cSMichael Klier        }
898b06d178Schris
901246e016SAndreas Gohr        //revert
911246e016SAndreas Gohr        if($ACT == 'revert'){
921246e016SAndreas Gohr            if(checkSecurityToken()){
931246e016SAndreas Gohr                $ACT = act_revert($ACT);
941246e016SAndreas Gohr            }else{
951246e016SAndreas Gohr                $ACT = 'show';
961246e016SAndreas Gohr            }
971246e016SAndreas Gohr        }
981246e016SAndreas Gohr
996b13307fSandi        //save
1001b2a85e8SAndreas Gohr        if($ACT == 'save'){
1011b2a85e8SAndreas Gohr            if(checkSecurityToken()){
1026b13307fSandi                $ACT = act_save($ACT);
1031b2a85e8SAndreas Gohr            }else{
1048071beaaSAndreas Gohr                $ACT = 'preview';
1051b2a85e8SAndreas Gohr            }
1061b2a85e8SAndreas Gohr        }
1076b13307fSandi
108067c5d22SBen Coburn        //cancel conflicting edit
109067c5d22SBen Coburn        if($ACT == 'cancel')
110067c5d22SBen Coburn            $ACT = 'show';
111067c5d22SBen Coburn
112ee4c4a1bSAndreas Gohr        //draft deletion
113ee4c4a1bSAndreas Gohr        if($ACT == 'draftdel')
114ee4c4a1bSAndreas Gohr            $ACT = act_draftdel($ACT);
115ee4c4a1bSAndreas Gohr
116ee4c4a1bSAndreas Gohr        //draft saving on preview
117ee4c4a1bSAndreas Gohr        if($ACT == 'preview')
118ee4c4a1bSAndreas Gohr            $ACT = act_draftsave($ACT);
119ee4c4a1bSAndreas Gohr
1206b13307fSandi        //edit
121c9d5430bSAdrian Lang        if(in_array($ACT, array('edit', 'preview', 'recover'))) {
122af182434Sandi            $ACT = act_edit($ACT);
1236b13307fSandi        }else{
1246b13307fSandi            unlock($ID); //try to unlock
1256b13307fSandi        }
1266b13307fSandi
1276b13307fSandi        //handle export
128ac83b9d8Sandi        if(substr($ACT,0,7) == 'export_')
1296b13307fSandi            $ACT = act_export($ACT);
1306b13307fSandi
131c19fe9c0Sandi        //handle admin tasks
132c19fe9c0Sandi        if($ACT == 'admin'){
13311e2ce22Schris            // retrieve admin plugin name from $_REQUEST['page']
134bb4866bdSchris            if (!empty($_REQUEST['page'])) {
13511e2ce22Schris                $pluginlist = plugin_list('admin');
13611e2ce22Schris                if (in_array($_REQUEST['page'], $pluginlist)) {
13711e2ce22Schris                    // attempt to load the plugin
13824ea6500SAndreas Gohr                    if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== null){
13924ea6500SAndreas Gohr                        if($plugin->forAdminOnly() && !$INFO['isadmin']){
14024ea6500SAndreas Gohr                            // a manager tried to load a plugin that's for admins only
14124ea6500SAndreas Gohr                            unset($_REQUEST['page']);
14224ea6500SAndreas Gohr                            msg('For admins only',-1);
14324ea6500SAndreas Gohr                        }else{
14411e2ce22Schris                            $plugin->handle();
14511e2ce22Schris                        }
14611e2ce22Schris                    }
147c19fe9c0Sandi                }
14824ea6500SAndreas Gohr            }
14924ea6500SAndreas Gohr        }
1505f312bacSAndreas Gohr
1515f312bacSAndreas Gohr        // check permissions again - the action may have changed
1525f312bacSAndreas Gohr        $ACT = act_permcheck($ACT);
15324bb549bSchris    }  // end event ACTION_ACT_PREPROCESS default action
15424bb549bSchris    $evt->advise_after();
15585dcda20SRobin Getz    // Make sure plugs can handle 'denied'
15685dcda20SRobin Getz    if($conf['send404'] && $ACT == 'denied') {
15785dcda20SRobin Getz        header('HTTP/1.0 403 Forbidden');
15885dcda20SRobin Getz    }
15924bb549bSchris    unset($evt);
160c19fe9c0Sandi
16146c0ed74SMichael Hamann    // when action 'show', the intial not 'show' and POST, do a redirect
16246c0ed74SMichael Hamann    if($ACT == 'show' && $preact != 'show' && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
16369cd1e27SAndreas Gohr        act_redirect($ID,$preact);
16469cd1e27SAndreas Gohr    }
1655f312bacSAndreas Gohr
166c346111aSAdrian Lang    global $INFO;
167c346111aSAdrian Lang    global $conf;
168c346111aSAdrian Lang    global $license;
169c346111aSAdrian Lang
1706b13307fSandi    //call template FIXME: all needed vars available?
171f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
172746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
173f63a2007Schris
1745a892029SAndreas Gohr    include(template('main.php'));
175c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
176c19fe9c0Sandi    // in function tpl_content()
1776b13307fSandi}
1786b13307fSandi
179f63a2007Schrisfunction act_sendheaders($headers) {
180f63a2007Schris    foreach ($headers as $hdr) header($hdr);
181f63a2007Schris}
182f63a2007Schris
1836b13307fSandi/**
184af182434Sandi * Sanitize the action command
185af182434Sandi *
186af182434Sandi * Add all allowed commands here.
187af182434Sandi *
188af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
189af182434Sandi */
190af182434Sandifunction act_clean($act){
191af182434Sandi    global $lang;
19260e6b550SAndreas Gohr    global $conf;
193c828a5d6SAndreas Gohr    global $INFO;
194af182434Sandi
195ee4c4a1bSAndreas Gohr    // check if the action was given as array key
196ee4c4a1bSAndreas Gohr    if(is_array($act)){
197ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
198ee4c4a1bSAndreas Gohr    }
199ee4c4a1bSAndreas Gohr
200ac83b9d8Sandi    //remove all bad chars
201ac83b9d8Sandi    $act = strtolower($act);
2022d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
203ac83b9d8Sandi
204ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
205cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
206b146b32bSandi
207396c218fSAndreas Gohr    if($act === '') $act = 'show';
208396c218fSAndreas Gohr
209409d7af7SAndreas Gohr    // check if action is disabled
210409d7af7SAndreas Gohr    if(!actionOK($act)){
211409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
212409d7af7SAndreas Gohr        return 'show';
213409d7af7SAndreas Gohr    }
214409d7af7SAndreas Gohr
21560e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
21660e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2171246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
218*d5a9514cSAdrian Lang                    'resendpwd'))){
21960e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
22060e6b550SAndreas Gohr        return 'show';
22160e6b550SAndreas Gohr    }
22260e6b550SAndreas Gohr
223c828a5d6SAndreas Gohr    //is there really a draft?
224c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
225c828a5d6SAndreas Gohr
226067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
227ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2281246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2295a932e77SAdrian Lang                    'unsubscribe','profile','resendpwd','recover',
230*d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
231ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
232af182434Sandi        return 'show';
233af182434Sandi    }
234af182434Sandi    return $act;
235af182434Sandi}
236af182434Sandi
237af182434Sandi/**
2386b13307fSandi * Run permissionchecks
2396b13307fSandi *
2406b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2416b13307fSandi */
2426b13307fSandifunction act_permcheck($act){
243dbbc6aa7Sandi    global $INFO;
2445e199953Smatthiasgrimm    global $conf;
245dbbc6aa7Sandi
246ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2476b13307fSandi        if($INFO['exists']){
248bdbc16bfSandi            if($act == 'edit'){
249bdbc16bfSandi                //the edit function will check again and do a source show
250bdbc16bfSandi                //when no AUTH_EDIT available
251bdbc16bfSandi                $permneed = AUTH_READ;
252bdbc16bfSandi            }else{
2536b13307fSandi                $permneed = AUTH_EDIT;
254bdbc16bfSandi            }
2556b13307fSandi        }else{
2566b13307fSandi            $permneed = AUTH_CREATE;
2576b13307fSandi        }
258c4f79b71SMichael Hamann    }elseif(in_array($act,array('login','search','recent','profile','index', 'sitemap'))){
2596b13307fSandi        $permneed = AUTH_NONE;
2601246e016SAndreas Gohr    }elseif($act == 'revert'){
2611246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
2621246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
2635e199953Smatthiasgrimm    }elseif($act == 'register'){
2645e199953Smatthiasgrimm        $permneed = AUTH_NONE;
265ebd3d9ceSchris    }elseif($act == 'resendpwd'){
266ebd3d9ceSchris        $permneed = AUTH_NONE;
267c19fe9c0Sandi    }elseif($act == 'admin'){
268f8cc712eSAndreas Gohr        if($INFO['ismanager']){
269f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
270f8cc712eSAndreas Gohr            // action is checked later
271f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
272f8cc712eSAndreas Gohr        }else{
273c19fe9c0Sandi            $permneed = AUTH_ADMIN;
274f8cc712eSAndreas Gohr        }
2756b13307fSandi    }else{
2766b13307fSandi        $permneed = AUTH_READ;
2776b13307fSandi    }
278dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
279dbbc6aa7Sandi
2806b13307fSandi    return 'denied';
2816b13307fSandi}
2826b13307fSandi
2836b13307fSandi/**
284ee4c4a1bSAndreas Gohr * Handle 'draftdel'
285ee4c4a1bSAndreas Gohr *
286ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
287ee4c4a1bSAndreas Gohr */
288ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
289ee4c4a1bSAndreas Gohr    global $INFO;
290ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
291ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
292ee4c4a1bSAndreas Gohr    return 'show';
293ee4c4a1bSAndreas Gohr}
294ee4c4a1bSAndreas Gohr
295ee4c4a1bSAndreas Gohr/**
296ee4c4a1bSAndreas Gohr * Saves a draft on preview
297ee4c4a1bSAndreas Gohr *
298ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
299ee4c4a1bSAndreas Gohr */
300ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
301ee4c4a1bSAndreas Gohr    global $INFO;
302ee4c4a1bSAndreas Gohr    global $ID;
303ee4c4a1bSAndreas Gohr    global $conf;
304ee4c4a1bSAndreas Gohr    if($conf['usedraft'] && $_POST['wikitext']){
305ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
30603f008cdSMichael Hamann                'prefix' => substr($_POST['prefix'], 0, -1),
307ee4c4a1bSAndreas Gohr                'text'   => $_POST['wikitext'],
308ee4c4a1bSAndreas Gohr                'suffix' => $_POST['suffix'],
30903f008cdSMichael Hamann                'date'   => (int) $_POST['date'],
310ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
311ee4c4a1bSAndreas Gohr                );
312ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
313ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
314ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
315ee4c4a1bSAndreas Gohr        }
316ee4c4a1bSAndreas Gohr    }
317ee4c4a1bSAndreas Gohr    return $act;
318ee4c4a1bSAndreas Gohr}
319ee4c4a1bSAndreas Gohr
320ee4c4a1bSAndreas Gohr/**
3216b13307fSandi * Handle 'save'
3226b13307fSandi *
3236b13307fSandi * Checks for spam and conflicts and saves the page.
3246b13307fSandi * Does a redirect to show the page afterwards or
3256b13307fSandi * returns a new action.
3266b13307fSandi *
3276b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3286b13307fSandi */
3296b13307fSandifunction act_save($act){
3306b13307fSandi    global $ID;
3316b13307fSandi    global $DATE;
3326b13307fSandi    global $PRE;
3336b13307fSandi    global $TEXT;
3346b13307fSandi    global $SUF;
3356b13307fSandi    global $SUM;
3365a932e77SAdrian Lang    global $lang;
3378d67c48aSAdrian Lang    global $INFO;
3386b13307fSandi
3396b13307fSandi    //spam check
3405a932e77SAdrian Lang    if(checkwordblock()) {
3415a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3425a932e77SAdrian Lang        return 'edit';
3435a932e77SAdrian Lang    }
3448d67c48aSAdrian Lang    //conflict check
3458d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3466b13307fSandi        return 'conflict';
3476b13307fSandi
3486b13307fSandi    //save it
349b6912aeaSAndreas Gohr    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con
3506b13307fSandi    //unlock it
3516b13307fSandi    unlock($ID);
3526b13307fSandi
353ee4c4a1bSAndreas Gohr    //delete draft
354ee4c4a1bSAndreas Gohr    act_draftdel($act);
35569cd1e27SAndreas Gohr    session_write_close();
356ee4c4a1bSAndreas Gohr
35769cd1e27SAndreas Gohr    // when done, show page
35869cd1e27SAndreas Gohr    return 'show';
35969cd1e27SAndreas Gohr}
360f951a474SAndreas Gohr
36114a122deSAndreas Gohr/**
3621246e016SAndreas Gohr * Revert to a certain revision
3631246e016SAndreas Gohr *
3641246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3651246e016SAndreas Gohr */
3661246e016SAndreas Gohrfunction act_revert($act){
3671246e016SAndreas Gohr    global $ID;
3681246e016SAndreas Gohr    global $REV;
3691246e016SAndreas Gohr    global $lang;
370de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
371de4d479aSAdrian Lang    // global $INFO;
372de4d479aSAdrian Lang    // if (!$INFO['writable']) {
373de4d479aSAdrian Lang    //     return 'show';
374de4d479aSAdrian Lang    // }
3751246e016SAndreas Gohr
3761246e016SAndreas Gohr    // when no revision is given, delete current one
3771246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
3781246e016SAndreas Gohr    $text = '';
3791246e016SAndreas Gohr    $sum  = $lang['deleted'];
3801246e016SAndreas Gohr    if($REV){
3811246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
3821246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
3831246e016SAndreas Gohr        $sum  = $lang['restored'];
3841246e016SAndreas Gohr    }
3851246e016SAndreas Gohr
3861246e016SAndreas Gohr    // spam check
3875a932e77SAdrian Lang
3885a932e77SAdrian Lang    if (checkwordblock($text)) {
3895a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3905a932e77SAdrian Lang        return 'edit';
3915a932e77SAdrian Lang    }
3921246e016SAndreas Gohr
3931246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
3941246e016SAndreas Gohr    msg($sum,1);
3951246e016SAndreas Gohr
3961246e016SAndreas Gohr    //delete any draft
3971246e016SAndreas Gohr    act_draftdel($act);
3981246e016SAndreas Gohr    session_write_close();
3991246e016SAndreas Gohr
4001246e016SAndreas Gohr    // when done, show current page
4011246e016SAndreas Gohr    $_SERVER['REQUEST_METHOD'] = 'post'; //should force a redirect
4021246e016SAndreas Gohr    $REV = '';
4031246e016SAndreas Gohr    return 'show';
4041246e016SAndreas Gohr}
4051246e016SAndreas Gohr
4061246e016SAndreas Gohr/**
40714a122deSAndreas Gohr * Do a redirect after receiving post data
40814a122deSAndreas Gohr *
40914a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
41014a122deSAndreas Gohr */
41169cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
41269cd1e27SAndreas Gohr    global $PRE;
41369cd1e27SAndreas Gohr    global $TEXT;
414f951a474SAndreas Gohr
41569cd1e27SAndreas Gohr    $opts = array(
41669cd1e27SAndreas Gohr            'id'       => $id,
41769cd1e27SAndreas Gohr            'preact'   => $preact
41869cd1e27SAndreas Gohr            );
419c66972f2SAdrian Lang    //get section name when coming from section edit
420c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
421c66972f2SAdrian Lang        $check = false; //Byref
422c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
423c66972f2SAdrian Lang    }
424c66972f2SAdrian Lang
42569cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
42669cd1e27SAndreas Gohr}
42769cd1e27SAndreas Gohr
42869cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
42969cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
430c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
43169cd1e27SAndreas Gohr
4326b13307fSandi    //show it
433af2408d5SAndreas Gohr    send_redirect($go);
4346b13307fSandi}
4356b13307fSandi
4366b13307fSandi/**
437b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4386b13307fSandi *
4396b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4406b13307fSandi */
4416b13307fSandifunction act_auth($act){
44208eda5bcSmatthiasgrimm    global $ID;
4437cace34dSAndreas Gohr    global $INFO;
44408eda5bcSmatthiasgrimm
4456b13307fSandi    //already logged in?
446c66972f2SAdrian Lang    if(isset($_SERVER['REMOTE_USER']) && $act=='login'){
447ca12ce46SAndreas Gohr        return 'show';
4482288dc06SGuy Brand    }
4496b13307fSandi
4506b13307fSandi    //handle logout
4516b13307fSandi    if($act=='logout'){
45208eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
453424c3c4fSJohannes Buchner        if($lockedby == $_SERVER['REMOTE_USER'])
45408eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
45508eda5bcSmatthiasgrimm
4567cace34dSAndreas Gohr        // do the logout stuff
4576b13307fSandi        auth_logoff();
4587cace34dSAndreas Gohr
4597cace34dSAndreas Gohr        // rebuild info array
4607cace34dSAndreas Gohr        $INFO = pageinfo();
4617cace34dSAndreas Gohr
462e16eccb7SGuy Brand        act_redirect($ID,'login');
4636b13307fSandi    }
4646b13307fSandi
4656b13307fSandi    return $act;
4666b13307fSandi}
4676b13307fSandi
4686b13307fSandi/**
46945a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
4706b13307fSandi *
4716b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4726b13307fSandi */
4736b13307fSandifunction act_edit($act){
474cd409024Sjorda    global $ID;
475ee4c4a1bSAndreas Gohr    global $INFO;
476cd409024Sjorda
47745a99335SAdrian Lang    global $TEXT;
47845a99335SAdrian Lang    global $RANGE;
47945a99335SAdrian Lang    global $PRE;
48045a99335SAdrian Lang    global $SUF;
48145a99335SAdrian Lang    global $REV;
48245a99335SAdrian Lang    global $SUM;
48345a99335SAdrian Lang    global $lang;
48445a99335SAdrian Lang    global $DATE;
48545a99335SAdrian Lang
48645a99335SAdrian Lang    if (!isset($TEXT)) {
48745a99335SAdrian Lang        if ($INFO['exists']) {
48845a99335SAdrian Lang            if ($RANGE) {
48945a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
49045a99335SAdrian Lang            } else {
49145a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
49245a99335SAdrian Lang            }
49345a99335SAdrian Lang        } else {
494fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
49545a99335SAdrian Lang        }
49645a99335SAdrian Lang    }
49745a99335SAdrian Lang
49845a99335SAdrian Lang    //set summary default
49945a99335SAdrian Lang    if(!$SUM){
50045a99335SAdrian Lang        if($REV){
50145a99335SAdrian Lang            $SUM = $lang['restored'];
50245a99335SAdrian Lang        }elseif(!$INFO['exists']){
50345a99335SAdrian Lang            $SUM = $lang['created'];
50445a99335SAdrian Lang        }
50545a99335SAdrian Lang    }
50645a99335SAdrian Lang
5078d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5088d67c48aSAdrian Lang    // This is used for conflict detection
5098d67c48aSAdrian Lang    if(!$DATE) $DATE = $INFO['meta']['date']['modified'];
51045a99335SAdrian Lang
5116b13307fSandi    //check if locked by anyone - if not lock for my self
51231bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
51331bc8f11SMichael Hamann    if ($INFO['writable']) {
5146b13307fSandi        $lockedby = checklock($ID);
5156b13307fSandi        if($lockedby) return 'locked';
5166b13307fSandi
5176b13307fSandi        lock($ID);
51831bc8f11SMichael Hamann    }
51931bc8f11SMichael Hamann
5206b13307fSandi    return $act;
5216b13307fSandi}
5226b13307fSandi
5236b13307fSandi/**
524f6dad9fdSMichael Klier * Export a wiki page for various formats
525f6dad9fdSMichael Klier *
526f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
527f6dad9fdSMichael Klier *
528f6dad9fdSMichael Klier *  Event data:
529f6dad9fdSMichael Klier *    data['id']      -- page id
530f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
531f6dad9fdSMichael Klier *    data['headers'] -- export headers
532f6dad9fdSMichael Klier *    data['output']  -- export output
5336b13307fSandi *
5346b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
535f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5366b13307fSandi */
5376b13307fSandifunction act_export($act){
5386b13307fSandi    global $ID;
5396b13307fSandi    global $REV;
54085f8705cSAnika Henke    global $conf;
54185f8705cSAnika Henke    global $lang;
5426b13307fSandi
543f6dad9fdSMichael Klier    $pre = '';
544f6dad9fdSMichael Klier    $post = '';
545f6dad9fdSMichael Klier    $output = '';
546f6dad9fdSMichael Klier    $headers = array();
547cc2ae802SAndreas Gohr
548f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
549f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
550f6dad9fdSMichael Klier
551ac83b9d8Sandi    $mode = substr($act,7);
552f6dad9fdSMichael Klier    switch($mode) {
553f6dad9fdSMichael Klier        case 'raw':
5545adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
55566b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
556f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
557f6dad9fdSMichael Klier            break;
558f6dad9fdSMichael Klier        case 'xhtml':
559f6dad9fdSMichael Klier            $pre .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . DOKU_LF;
560f6dad9fdSMichael Klier            $pre .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . DOKU_LF;
561f6dad9fdSMichael Klier            $pre .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"' . DOKU_LF;
562f6dad9fdSMichael Klier            $pre .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
563f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
564f6dad9fdSMichael Klier            $pre .= '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . DOKU_LF;
565f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
566f6dad9fdSMichael Klier
567f6dad9fdSMichael Klier            // get metaheaders
568f6dad9fdSMichael Klier            ob_start();
569f6dad9fdSMichael Klier            tpl_metaheaders();
570f6dad9fdSMichael Klier            $pre .= ob_get_clean();
571f6dad9fdSMichael Klier
572f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
573f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
574f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
575f6dad9fdSMichael Klier
576f6dad9fdSMichael Klier            // get toc
577f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
578f6dad9fdSMichael Klier
579f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
580f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
581f6dad9fdSMichael Klier
582f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
583f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
584f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
585f6dad9fdSMichael Klier            break;
586f6dad9fdSMichael Klier        case 'xhtmlbody':
587f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
588f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
589f6dad9fdSMichael Klier            break;
590f6dad9fdSMichael Klier        default:
591f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
5929acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
593f6dad9fdSMichael Klier            break;
594f6dad9fdSMichael Klier    }
595f6dad9fdSMichael Klier
596f6dad9fdSMichael Klier    // prepare event data
597f6dad9fdSMichael Klier    $data = array();
598f6dad9fdSMichael Klier    $data['id'] = $ID;
599f6dad9fdSMichael Klier    $data['mode'] = $mode;
600f6dad9fdSMichael Klier    $data['headers'] = $headers;
601f6dad9fdSMichael Klier    $data['output'] =& $output;
602f6dad9fdSMichael Klier
603f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
604f6dad9fdSMichael Klier
605f6dad9fdSMichael Klier    if(!empty($data['output'])){
606f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
60785767031SAndreas Gohr            header("$key: $val");
60885767031SAndreas Gohr        }
609f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6106b13307fSandi        exit;
6116b13307fSandi    }
6126b13307fSandi    return 'show';
6136b13307fSandi}
614340756e4Sandi
615b158d625SSteven Danz/**
616c4f79b71SMichael Hamann * Handle sitemap delivery
617c4f79b71SMichael Hamann *
618c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
619c4f79b71SMichael Hamann */
620c4f79b71SMichael Hamannfunction act_sitemap($act) {
621c4f79b71SMichael Hamann    global $conf;
622c4f79b71SMichael Hamann
623eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
624c4f79b71SMichael Hamann        header("HTTP/1.0 404 Not Found");
625c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
626c4f79b71SMichael Hamann        exit;
627c4f79b71SMichael Hamann    }
628c4f79b71SMichael Hamann
629eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
630eae17177SMichael Hamann    if(strrchr($sitemap, '.') === '.gz'){
631c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
632c4f79b71SMichael Hamann    }else{
633c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
634c4f79b71SMichael Hamann    }
635c4f79b71SMichael Hamann
636c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
637c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
6382897eb23SMichael Hamann        Sitemapper::generate();
639c4f79b71SMichael Hamann    }
640c4f79b71SMichael Hamann
641c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
642c4f79b71SMichael Hamann        // Send headers
643c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
6444c36bf82SGuillaume Turri        header('Content-Disposition: attachment; filename='.basename($sitemap));
645c4f79b71SMichael Hamann
646eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
647eae17177SMichael Hamann
648c4f79b71SMichael Hamann        // Send file
649c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
650c4f79b71SMichael Hamann        if (http_sendfile($sitemap)) exit;
651c4f79b71SMichael Hamann
652eae17177SMichael Hamann        readfile($sitemap);
653c4f79b71SMichael Hamann        exit;
654c4f79b71SMichael Hamann    }
655c4f79b71SMichael Hamann
656c4f79b71SMichael Hamann    header("HTTP/1.0 500 Internal Server Error");
657eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
658c4f79b71SMichael Hamann    exit;
659c4f79b71SMichael Hamann}
660c4f79b71SMichael Hamann
661c4f79b71SMichael Hamann/**
6625b75cd1fSAdrian Lang * Handle page 'subscribe'
663b158d625SSteven Danz *
6645b75cd1fSAdrian Lang * Throws exception on error.
6655b75cd1fSAdrian Lang *
6665b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
667b158d625SSteven Danz */
6681380fc45SAndreas Gohrfunction act_subscription($act){
669056c2049SAndreas Gohr    global $lang;
670056c2049SAndreas Gohr    global $INFO;
671056c2049SAndreas Gohr    global $ID;
67252b0dd67SGuy Brand
6739fa341d0SAndreas Gohr    // subcriptions work for logged in users only
6749fa341d0SAndreas Gohr    if(!$_SERVER['REMOTE_USER']) return 'show';
6759fa341d0SAndreas Gohr
676056c2049SAndreas Gohr    // get and preprocess data.
6778881fcc9SAdrian Lang    $params = array();
6788881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
679056c2049SAndreas Gohr        if (isset($_REQUEST["sub_$param"])) {
680056c2049SAndreas Gohr            $params[$param] = $_REQUEST["sub_$param"];
6818881fcc9SAdrian Lang        }
6828881fcc9SAdrian Lang    }
6838881fcc9SAdrian Lang
684056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
68566d2bed9SAdrian Lang    if(!$params['action'] || !checkSecurityToken()) return $act;
686056c2049SAndreas Gohr
6878881fcc9SAdrian Lang    // Handle POST data, may throw exception.
6888881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
6898881fcc9SAdrian Lang
6908881fcc9SAdrian Lang    $target = $params['target'];
6918881fcc9SAdrian Lang    $style  = $params['style'];
6928881fcc9SAdrian Lang    $data   = $params['data'];
6938881fcc9SAdrian Lang    $action = $params['action'];
6948881fcc9SAdrian Lang
6958881fcc9SAdrian Lang    // Perform action.
6968881fcc9SAdrian Lang    if (!subscription_set($_SERVER['REMOTE_USER'], $target, $style, $data)) {
6978881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
6988881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
6998881fcc9SAdrian Lang                                    prettyprint_id($target)));
7008881fcc9SAdrian Lang    }
7018881fcc9SAdrian Lang    msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
7028881fcc9SAdrian Lang                prettyprint_id($target)), 1);
703cb3f9dbaSAdrian Lang    act_redirect($ID, $act);
704cb3f9dbaSAdrian Lang
705cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
706cb3f9dbaSAdrian Lang    $INFO['subscribed'] = get_info_subscribed();
707cb3f9dbaSAdrian Lang    return 'show';
7088881fcc9SAdrian Lang}
7098881fcc9SAdrian Lang
7108881fcc9SAdrian Lang/**
7118881fcc9SAdrian Lang * Validate POST data
7128881fcc9SAdrian Lang *
7138881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
7148881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
7158881fcc9SAdrian Lang *
7168881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
7178881fcc9SAdrian Lang */
7187a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
7198881fcc9SAdrian Lang    global $INFO;
7208881fcc9SAdrian Lang    global $lang;
7218881fcc9SAdrian Lang
7225b75cd1fSAdrian Lang    // Get and validate parameters.
7238881fcc9SAdrian Lang    if (!isset($params['target'])) {
72415741132SAndreas Gohr        throw new Exception('no subscription target given');
7255b75cd1fSAdrian Lang    }
7268881fcc9SAdrian Lang    $target = $params['target'];
7275b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
7285b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
7295b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
7305b75cd1fSAdrian Lang        $valid_styles[] = 'list';
7315b75cd1fSAdrian Lang    }
7328881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
73315741132SAndreas Gohr                              'invalid subscription style given');
7348881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
73515741132SAndreas Gohr                              $params, 'invalid subscription action given');
736613964ecSGuy Brand
7375b75cd1fSAdrian Lang    // Check other conditions.
7385b75cd1fSAdrian Lang    if ($action === 'subscribe') {
7395b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
7405b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
74152b0dd67SGuy Brand        }
7425b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
7435b75cd1fSAdrian Lang        $is = false;
7445b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
7455b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
7465b75cd1fSAdrian Lang                $is = true;
74752b0dd67SGuy Brand            }
74852b0dd67SGuy Brand        }
7495b75cd1fSAdrian Lang        if ($is === false) {
75015741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
75115741132SAndreas Gohr                                        $_SERVER['REMOTE_USER'],
7525b75cd1fSAdrian Lang                                        prettyprint_id($target)));
7535b75cd1fSAdrian Lang        }
7545b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
7555b75cd1fSAdrian Lang        $style = null;
75652b0dd67SGuy Brand    }
75752b0dd67SGuy Brand
7588881fcc9SAdrian Lang    $data = in_array($style, array('list', 'digest')) ? time() : null;
7598881fcc9SAdrian Lang    $params = compact('target', 'style', 'data', 'action');
76052b0dd67SGuy Brand}
76152b0dd67SGuy Brand
762e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
763