xref: /dokuwiki/inc/actions.php (revision c8839c220c49633ea45ce5d0e4be1f411f66ad6c)
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
3462baad0fSMartin 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){
190ee4c4a1bSAndreas Gohr    // check if the action was given as array key
191ee4c4a1bSAndreas Gohr    if(is_array($act)){
192ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
193ee4c4a1bSAndreas Gohr    }
194ee4c4a1bSAndreas Gohr
195ac83b9d8Sandi    //remove all bad chars
196ac83b9d8Sandi    $act = strtolower($act);
1972d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
198ac83b9d8Sandi
199ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
200cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
201b146b32bSandi
202396c218fSAndreas Gohr    if($act === '') $act = 'show';
20362baad0fSMartin Doucha    return $act;
20462baad0fSMartin Doucha}
20562baad0fSMartin Doucha
20662baad0fSMartin Doucha/**
20762baad0fSMartin Doucha * Sanitize and validate action commands.
20862baad0fSMartin Doucha *
20962baad0fSMartin Doucha * Add all allowed commands here.
21062baad0fSMartin Doucha *
21162baad0fSMartin Doucha * @author Andreas Gohr <andi@splitbrain.org>
21262baad0fSMartin Doucha */
21362baad0fSMartin Douchafunction act_validate($act) {
214daf0cdbaSMartin Doucha    global $conf;
215daf0cdbaSMartin Doucha    global $INFO;
216daf0cdbaSMartin Doucha
21762baad0fSMartin Doucha    $act = act_clean($act);
218396c218fSAndreas Gohr
219409d7af7SAndreas Gohr    // check if action is disabled
220409d7af7SAndreas Gohr    if(!actionOK($act)){
221409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
222409d7af7SAndreas Gohr        return 'show';
223409d7af7SAndreas Gohr    }
224409d7af7SAndreas Gohr
22560e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
22660e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2271246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
228d5a9514cSAdrian Lang                    'resendpwd'))){
22960e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
23060e6b550SAndreas Gohr        return 'show';
23160e6b550SAndreas Gohr    }
23260e6b550SAndreas Gohr
233c828a5d6SAndreas Gohr    //is there really a draft?
234c828a5d6SAndreas Gohr    if($act == 'draft' && !file_exists($INFO['draft'])) return 'edit';
235c828a5d6SAndreas Gohr
236067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
237ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2381246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2395a932e77SAdrian Lang                    'unsubscribe','profile','resendpwd','recover',
240d5a9514cSAdrian Lang                    'draftdel','sitemap','media')) && substr($act,0,7) != 'export_' ) {
241ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
242af182434Sandi        return 'show';
243af182434Sandi    }
244af182434Sandi    return $act;
245af182434Sandi}
246af182434Sandi
247af182434Sandi/**
2486b13307fSandi * Run permissionchecks
2496b13307fSandi *
2506b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2516b13307fSandi */
2526b13307fSandifunction act_permcheck($act){
253dbbc6aa7Sandi    global $INFO;
2545e199953Smatthiasgrimm    global $conf;
255dbbc6aa7Sandi
256ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2576b13307fSandi        if($INFO['exists']){
258bdbc16bfSandi            if($act == 'edit'){
259bdbc16bfSandi                //the edit function will check again and do a source show
260bdbc16bfSandi                //when no AUTH_EDIT available
261bdbc16bfSandi                $permneed = AUTH_READ;
262bdbc16bfSandi            }else{
2636b13307fSandi                $permneed = AUTH_EDIT;
264bdbc16bfSandi            }
2656b13307fSandi        }else{
2666b13307fSandi            $permneed = AUTH_CREATE;
2676b13307fSandi        }
268c4f79b71SMichael Hamann    }elseif(in_array($act,array('login','search','recent','profile','index', 'sitemap'))){
2696b13307fSandi        $permneed = AUTH_NONE;
2701246e016SAndreas Gohr    }elseif($act == 'revert'){
2711246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
2721246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
2735e199953Smatthiasgrimm    }elseif($act == 'register'){
2745e199953Smatthiasgrimm        $permneed = AUTH_NONE;
275ebd3d9ceSchris    }elseif($act == 'resendpwd'){
276ebd3d9ceSchris        $permneed = AUTH_NONE;
277c19fe9c0Sandi    }elseif($act == 'admin'){
278f8cc712eSAndreas Gohr        if($INFO['ismanager']){
279f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
280f8cc712eSAndreas Gohr            // action is checked later
281f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
282f8cc712eSAndreas Gohr        }else{
283c19fe9c0Sandi            $permneed = AUTH_ADMIN;
284f8cc712eSAndreas Gohr        }
2856b13307fSandi    }else{
2866b13307fSandi        $permneed = AUTH_READ;
2876b13307fSandi    }
288dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
289dbbc6aa7Sandi
2906b13307fSandi    return 'denied';
2916b13307fSandi}
2926b13307fSandi
2936b13307fSandi/**
294ee4c4a1bSAndreas Gohr * Handle 'draftdel'
295ee4c4a1bSAndreas Gohr *
296ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
297ee4c4a1bSAndreas Gohr */
298ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
299ee4c4a1bSAndreas Gohr    global $INFO;
300ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
301ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
302ee4c4a1bSAndreas Gohr    return 'show';
303ee4c4a1bSAndreas Gohr}
304ee4c4a1bSAndreas Gohr
305ee4c4a1bSAndreas Gohr/**
306ee4c4a1bSAndreas Gohr * Saves a draft on preview
307ee4c4a1bSAndreas Gohr *
308ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
309ee4c4a1bSAndreas Gohr */
310ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
311ee4c4a1bSAndreas Gohr    global $INFO;
312ee4c4a1bSAndreas Gohr    global $ID;
31390f1b7bdSTom N Harris    global $INPUT;
314ee4c4a1bSAndreas Gohr    global $conf;
31590f1b7bdSTom N Harris    if($conf['usedraft'] && $INPUT->post->has('wikitext')) {
316ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
31790f1b7bdSTom N Harris                'prefix' => substr($INPUT->post->str('prefix'), 0, -1),
31890f1b7bdSTom N Harris                'text'   => $INPUT->post->str('wikitext'),
31990f1b7bdSTom N Harris                'suffix' => $INPUT->post->str('suffix'),
32090f1b7bdSTom N Harris                'date'   => $INPUT->post->int('date'),
321ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
322ee4c4a1bSAndreas Gohr                );
323ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
324ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
325ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
326ee4c4a1bSAndreas Gohr        }
327ee4c4a1bSAndreas Gohr    }
328ee4c4a1bSAndreas Gohr    return $act;
329ee4c4a1bSAndreas Gohr}
330ee4c4a1bSAndreas Gohr
331ee4c4a1bSAndreas Gohr/**
3326b13307fSandi * Handle 'save'
3336b13307fSandi *
3346b13307fSandi * Checks for spam and conflicts and saves the page.
3356b13307fSandi * Does a redirect to show the page afterwards or
3366b13307fSandi * returns a new action.
3376b13307fSandi *
3386b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3396b13307fSandi */
3406b13307fSandifunction act_save($act){
3416b13307fSandi    global $ID;
3426b13307fSandi    global $DATE;
3436b13307fSandi    global $PRE;
3446b13307fSandi    global $TEXT;
3456b13307fSandi    global $SUF;
3466b13307fSandi    global $SUM;
3475a932e77SAdrian Lang    global $lang;
3488d67c48aSAdrian Lang    global $INFO;
34990f1b7bdSTom N Harris    global $INPUT;
3506b13307fSandi
3516b13307fSandi    //spam check
3525a932e77SAdrian Lang    if(checkwordblock()) {
3535a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3545a932e77SAdrian Lang        return 'edit';
3555a932e77SAdrian Lang    }
3568d67c48aSAdrian Lang    //conflict check
3578d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3586b13307fSandi        return 'conflict';
3596b13307fSandi
3606b13307fSandi    //save it
36190f1b7bdSTom N Harris    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$INPUT->bool('minor')); //use pretty mode for con
3626b13307fSandi    //unlock it
3636b13307fSandi    unlock($ID);
3646b13307fSandi
365ee4c4a1bSAndreas Gohr    //delete draft
366ee4c4a1bSAndreas Gohr    act_draftdel($act);
36769cd1e27SAndreas Gohr    session_write_close();
368ee4c4a1bSAndreas Gohr
36969cd1e27SAndreas Gohr    // when done, show page
37069cd1e27SAndreas Gohr    return 'show';
37169cd1e27SAndreas Gohr}
372f951a474SAndreas Gohr
37314a122deSAndreas Gohr/**
3741246e016SAndreas Gohr * Revert to a certain revision
3751246e016SAndreas Gohr *
3761246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3771246e016SAndreas Gohr */
3781246e016SAndreas Gohrfunction act_revert($act){
3791246e016SAndreas Gohr    global $ID;
3801246e016SAndreas Gohr    global $REV;
3811246e016SAndreas Gohr    global $lang;
382de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
383de4d479aSAdrian Lang    // global $INFO;
384de4d479aSAdrian Lang    // if (!$INFO['writable']) {
385de4d479aSAdrian Lang    //     return 'show';
386de4d479aSAdrian Lang    // }
3871246e016SAndreas Gohr
3881246e016SAndreas Gohr    // when no revision is given, delete current one
3891246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
3901246e016SAndreas Gohr    $text = '';
3911246e016SAndreas Gohr    $sum  = $lang['deleted'];
3921246e016SAndreas Gohr    if($REV){
3931246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
3941246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
395d6b9c7bfSlupo49        $sum = sprintf($lang['restored'], dformat($REV));
3961246e016SAndreas Gohr    }
3971246e016SAndreas Gohr
3981246e016SAndreas Gohr    // spam check
3995a932e77SAdrian Lang
4005a932e77SAdrian Lang    if (checkwordblock($text)) {
4015a932e77SAdrian Lang        msg($lang['wordblock'], -1);
4025a932e77SAdrian Lang        return 'edit';
4035a932e77SAdrian Lang    }
4041246e016SAndreas Gohr
4051246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
4061246e016SAndreas Gohr    msg($sum,1);
4071246e016SAndreas Gohr
4081246e016SAndreas Gohr    //delete any draft
4091246e016SAndreas Gohr    act_draftdel($act);
4101246e016SAndreas Gohr    session_write_close();
4111246e016SAndreas Gohr
4121246e016SAndreas Gohr    // when done, show current page
4131246e016SAndreas Gohr    $_SERVER['REQUEST_METHOD'] = 'post'; //should force a redirect
4141246e016SAndreas Gohr    $REV = '';
4151246e016SAndreas Gohr    return 'show';
4161246e016SAndreas Gohr}
4171246e016SAndreas Gohr
4181246e016SAndreas Gohr/**
41914a122deSAndreas Gohr * Do a redirect after receiving post data
42014a122deSAndreas Gohr *
42114a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
42214a122deSAndreas Gohr */
42369cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
42469cd1e27SAndreas Gohr    global $PRE;
42569cd1e27SAndreas Gohr    global $TEXT;
426f951a474SAndreas Gohr
42769cd1e27SAndreas Gohr    $opts = array(
42869cd1e27SAndreas Gohr            'id'       => $id,
42969cd1e27SAndreas Gohr            'preact'   => $preact
43069cd1e27SAndreas Gohr            );
431c66972f2SAdrian Lang    //get section name when coming from section edit
432c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
433c66972f2SAdrian Lang        $check = false; //Byref
434c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
435c66972f2SAdrian Lang    }
436c66972f2SAdrian Lang
43769cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
43869cd1e27SAndreas Gohr}
43969cd1e27SAndreas Gohr
44069cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
44169cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
442c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
44369cd1e27SAndreas Gohr
4446b13307fSandi    //show it
445af2408d5SAndreas Gohr    send_redirect($go);
4466b13307fSandi}
4476b13307fSandi
4486b13307fSandi/**
449b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4506b13307fSandi *
4516b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4526b13307fSandi */
4536b13307fSandifunction act_auth($act){
45408eda5bcSmatthiasgrimm    global $ID;
4557cace34dSAndreas Gohr    global $INFO;
45608eda5bcSmatthiasgrimm
4576b13307fSandi    //already logged in?
458c66972f2SAdrian Lang    if(isset($_SERVER['REMOTE_USER']) && $act=='login'){
459ca12ce46SAndreas Gohr        return 'show';
4602288dc06SGuy Brand    }
4616b13307fSandi
4626b13307fSandi    //handle logout
4636b13307fSandi    if($act=='logout'){
46408eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
465424c3c4fSJohannes Buchner        if($lockedby == $_SERVER['REMOTE_USER'])
46608eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
46708eda5bcSmatthiasgrimm
4687cace34dSAndreas Gohr        // do the logout stuff
4696b13307fSandi        auth_logoff();
4707cace34dSAndreas Gohr
4717cace34dSAndreas Gohr        // rebuild info array
4727cace34dSAndreas Gohr        $INFO = pageinfo();
4737cace34dSAndreas Gohr
474e16eccb7SGuy Brand        act_redirect($ID,'login');
4756b13307fSandi    }
4766b13307fSandi
4776b13307fSandi    return $act;
4786b13307fSandi}
4796b13307fSandi
4806b13307fSandi/**
48145a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
4826b13307fSandi *
4836b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4846b13307fSandi */
4856b13307fSandifunction act_edit($act){
486cd409024Sjorda    global $ID;
487ee4c4a1bSAndreas Gohr    global $INFO;
488cd409024Sjorda
48945a99335SAdrian Lang    global $TEXT;
49045a99335SAdrian Lang    global $RANGE;
49145a99335SAdrian Lang    global $PRE;
49245a99335SAdrian Lang    global $SUF;
49345a99335SAdrian Lang    global $REV;
49445a99335SAdrian Lang    global $SUM;
49545a99335SAdrian Lang    global $lang;
49645a99335SAdrian Lang    global $DATE;
49745a99335SAdrian Lang
49845a99335SAdrian Lang    if (!isset($TEXT)) {
49945a99335SAdrian Lang        if ($INFO['exists']) {
50045a99335SAdrian Lang            if ($RANGE) {
50145a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
50245a99335SAdrian Lang            } else {
50345a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
50445a99335SAdrian Lang            }
50545a99335SAdrian Lang        } else {
506fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
50745a99335SAdrian Lang        }
50845a99335SAdrian Lang    }
50945a99335SAdrian Lang
51045a99335SAdrian Lang    //set summary default
51145a99335SAdrian Lang    if(!$SUM){
51245a99335SAdrian Lang        if($REV){
5137656ee3bSlupo49            $SUM = sprintf($lang['restored'], dformat($REV));
51445a99335SAdrian Lang        }elseif(!$INFO['exists']){
51545a99335SAdrian Lang            $SUM = $lang['created'];
51645a99335SAdrian Lang        }
51745a99335SAdrian Lang    }
51845a99335SAdrian Lang
5198d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
5208d67c48aSAdrian Lang    // This is used for conflict detection
52178035fe8SAndreas Gohr    if(!$DATE) $DATE = @filemtime(wikiFN($ID));
52245a99335SAdrian Lang
5236b13307fSandi    //check if locked by anyone - if not lock for my self
52431bc8f11SMichael Hamann    //do not lock when the user can't edit anyway
52531bc8f11SMichael Hamann    if ($INFO['writable']) {
5266b13307fSandi        $lockedby = checklock($ID);
5276b13307fSandi        if($lockedby) return 'locked';
5286b13307fSandi
5296b13307fSandi        lock($ID);
53031bc8f11SMichael Hamann    }
53131bc8f11SMichael Hamann
5326b13307fSandi    return $act;
5336b13307fSandi}
5346b13307fSandi
5356b13307fSandi/**
536f6dad9fdSMichael Klier * Export a wiki page for various formats
537f6dad9fdSMichael Klier *
538f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
539f6dad9fdSMichael Klier *
540f6dad9fdSMichael Klier *  Event data:
541f6dad9fdSMichael Klier *    data['id']      -- page id
542f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
543f6dad9fdSMichael Klier *    data['headers'] -- export headers
544f6dad9fdSMichael Klier *    data['output']  -- export output
5456b13307fSandi *
5466b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
547f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5486b13307fSandi */
5496b13307fSandifunction act_export($act){
5506b13307fSandi    global $ID;
5516b13307fSandi    global $REV;
55285f8705cSAnika Henke    global $conf;
55385f8705cSAnika Henke    global $lang;
5546b13307fSandi
555f6dad9fdSMichael Klier    $pre = '';
556f6dad9fdSMichael Klier    $post = '';
557f6dad9fdSMichael Klier    $output = '';
558f6dad9fdSMichael Klier    $headers = array();
559cc2ae802SAndreas Gohr
560f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
561f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
562f6dad9fdSMichael Klier
563ac83b9d8Sandi    $mode = substr($act,7);
564f6dad9fdSMichael Klier    switch($mode) {
565f6dad9fdSMichael Klier        case 'raw':
5665adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
56766b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
568f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
569f6dad9fdSMichael Klier            break;
570f6dad9fdSMichael Klier        case 'xhtml':
571*c8839c22SAnika Henke            $pre .= '<!DOCTYPE html>' . DOKU_LF;
572*c8839c22SAnika Henke            $pre .= '<html lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
573f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
574*c8839c22SAnika Henke            $pre .= '  <meta charset="utf-8" />' . DOKU_LF;
575f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
576f6dad9fdSMichael Klier
577f6dad9fdSMichael Klier            // get metaheaders
578f6dad9fdSMichael Klier            ob_start();
579f6dad9fdSMichael Klier            tpl_metaheaders();
580f6dad9fdSMichael Klier            $pre .= ob_get_clean();
581f6dad9fdSMichael Klier
582f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
583f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
584f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
585f6dad9fdSMichael Klier
586f6dad9fdSMichael Klier            // get toc
587f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
588f6dad9fdSMichael Klier
589f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
590f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
591f6dad9fdSMichael Klier
592f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
593f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
594f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
595f6dad9fdSMichael Klier            break;
596f6dad9fdSMichael Klier        case 'xhtmlbody':
597f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
598f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
599f6dad9fdSMichael Klier            break;
600f6dad9fdSMichael Klier        default:
601f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
6029acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
603f6dad9fdSMichael Klier            break;
604f6dad9fdSMichael Klier    }
605f6dad9fdSMichael Klier
606f6dad9fdSMichael Klier    // prepare event data
607f6dad9fdSMichael Klier    $data = array();
608f6dad9fdSMichael Klier    $data['id'] = $ID;
609f6dad9fdSMichael Klier    $data['mode'] = $mode;
610f6dad9fdSMichael Klier    $data['headers'] = $headers;
611f6dad9fdSMichael Klier    $data['output'] =& $output;
612f6dad9fdSMichael Klier
613f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
614f6dad9fdSMichael Klier
615f6dad9fdSMichael Klier    if(!empty($data['output'])){
616f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
61785767031SAndreas Gohr            header("$key: $val");
61885767031SAndreas Gohr        }
619f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
6206b13307fSandi        exit;
6216b13307fSandi    }
6226b13307fSandi    return 'show';
6236b13307fSandi}
624340756e4Sandi
625b158d625SSteven Danz/**
626c4f79b71SMichael Hamann * Handle sitemap delivery
627c4f79b71SMichael Hamann *
628c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
629c4f79b71SMichael Hamann */
630c4f79b71SMichael Hamannfunction act_sitemap($act) {
631c4f79b71SMichael Hamann    global $conf;
632c4f79b71SMichael Hamann
633eae17177SMichael Hamann    if ($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
634c4f79b71SMichael Hamann        header("HTTP/1.0 404 Not Found");
635c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
636c4f79b71SMichael Hamann        exit;
637c4f79b71SMichael Hamann    }
638c4f79b71SMichael Hamann
639eae17177SMichael Hamann    $sitemap = Sitemapper::getFilePath();
640eae17177SMichael Hamann    if(strrchr($sitemap, '.') === '.gz'){
641c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
642c4f79b71SMichael Hamann    }else{
643c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
644c4f79b71SMichael Hamann    }
645c4f79b71SMichael Hamann
646c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
647c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
6482897eb23SMichael Hamann        Sitemapper::generate();
649c4f79b71SMichael Hamann    }
650c4f79b71SMichael Hamann
651c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
652c4f79b71SMichael Hamann        // Send headers
653c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
6544c36bf82SGuillaume Turri        header('Content-Disposition: attachment; filename='.basename($sitemap));
655c4f79b71SMichael Hamann
656eae17177SMichael Hamann        http_conditionalRequest(filemtime($sitemap));
657eae17177SMichael Hamann
658c4f79b71SMichael Hamann        // Send file
659c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
660c4f79b71SMichael Hamann        if (http_sendfile($sitemap)) exit;
661c4f79b71SMichael Hamann
662eae17177SMichael Hamann        readfile($sitemap);
663c4f79b71SMichael Hamann        exit;
664c4f79b71SMichael Hamann    }
665c4f79b71SMichael Hamann
666c4f79b71SMichael Hamann    header("HTTP/1.0 500 Internal Server Error");
667eae17177SMichael Hamann    print "Could not read the sitemap file - bad permissions?";
668c4f79b71SMichael Hamann    exit;
669c4f79b71SMichael Hamann}
670c4f79b71SMichael Hamann
671c4f79b71SMichael Hamann/**
6725b75cd1fSAdrian Lang * Handle page 'subscribe'
673b158d625SSteven Danz *
6745b75cd1fSAdrian Lang * Throws exception on error.
6755b75cd1fSAdrian Lang *
6765b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
677b158d625SSteven Danz */
6781380fc45SAndreas Gohrfunction act_subscription($act){
679056c2049SAndreas Gohr    global $lang;
680056c2049SAndreas Gohr    global $INFO;
681056c2049SAndreas Gohr    global $ID;
68290f1b7bdSTom N Harris    global $INPUT;
68352b0dd67SGuy Brand
6849fa341d0SAndreas Gohr    // subcriptions work for logged in users only
6859fa341d0SAndreas Gohr    if(!$_SERVER['REMOTE_USER']) return 'show';
6869fa341d0SAndreas Gohr
687056c2049SAndreas Gohr    // get and preprocess data.
6888881fcc9SAdrian Lang    $params = array();
6898881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
69090f1b7bdSTom N Harris        if ($INPUT->has("sub_$param")) {
69190f1b7bdSTom N Harris            $params[$param] = $INPUT->str("sub_$param");
6928881fcc9SAdrian Lang        }
6938881fcc9SAdrian Lang    }
6948881fcc9SAdrian Lang
695056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
69666d2bed9SAdrian Lang    if(!$params['action'] || !checkSecurityToken()) return $act;
697056c2049SAndreas Gohr
6988881fcc9SAdrian Lang    // Handle POST data, may throw exception.
6998881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
7008881fcc9SAdrian Lang
7018881fcc9SAdrian Lang    $target = $params['target'];
7028881fcc9SAdrian Lang    $style  = $params['style'];
7038881fcc9SAdrian Lang    $data   = $params['data'];
7048881fcc9SAdrian Lang    $action = $params['action'];
7058881fcc9SAdrian Lang
7068881fcc9SAdrian Lang    // Perform action.
7078881fcc9SAdrian Lang    if (!subscription_set($_SERVER['REMOTE_USER'], $target, $style, $data)) {
7088881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
7098881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
7108881fcc9SAdrian Lang                                    prettyprint_id($target)));
7118881fcc9SAdrian Lang    }
7128881fcc9SAdrian Lang    msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
7138881fcc9SAdrian Lang                prettyprint_id($target)), 1);
714cb3f9dbaSAdrian Lang    act_redirect($ID, $act);
715cb3f9dbaSAdrian Lang
716cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
717cb3f9dbaSAdrian Lang    $INFO['subscribed'] = get_info_subscribed();
718cb3f9dbaSAdrian Lang    return 'show';
7198881fcc9SAdrian Lang}
7208881fcc9SAdrian Lang
7218881fcc9SAdrian Lang/**
7228881fcc9SAdrian Lang * Validate POST data
7238881fcc9SAdrian Lang *
7248881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
7258881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
7268881fcc9SAdrian Lang *
7278881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
7288881fcc9SAdrian Lang */
7297a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
7308881fcc9SAdrian Lang    global $INFO;
7318881fcc9SAdrian Lang    global $lang;
7328881fcc9SAdrian Lang
7335b75cd1fSAdrian Lang    // Get and validate parameters.
7348881fcc9SAdrian Lang    if (!isset($params['target'])) {
73515741132SAndreas Gohr        throw new Exception('no subscription target given');
7365b75cd1fSAdrian Lang    }
7378881fcc9SAdrian Lang    $target = $params['target'];
7385b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
7395b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
7405b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
7415b75cd1fSAdrian Lang        $valid_styles[] = 'list';
7425b75cd1fSAdrian Lang    }
7438881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
74415741132SAndreas Gohr                              'invalid subscription style given');
7458881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
74615741132SAndreas Gohr                              $params, 'invalid subscription action given');
747613964ecSGuy Brand
7485b75cd1fSAdrian Lang    // Check other conditions.
7495b75cd1fSAdrian Lang    if ($action === 'subscribe') {
7505b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
7515b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
75252b0dd67SGuy Brand        }
7535b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
7545b75cd1fSAdrian Lang        $is = false;
7555b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
7565b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
7575b75cd1fSAdrian Lang                $is = true;
75852b0dd67SGuy Brand            }
75952b0dd67SGuy Brand        }
7605b75cd1fSAdrian Lang        if ($is === false) {
76115741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
76215741132SAndreas Gohr                                        $_SERVER['REMOTE_USER'],
7635b75cd1fSAdrian Lang                                        prettyprint_id($target)));
7645b75cd1fSAdrian Lang        }
7655b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
7665b75cd1fSAdrian Lang        $style = null;
76752b0dd67SGuy Brand    }
76852b0dd67SGuy Brand
7698881fcc9SAdrian Lang    $data = in_array($style, array('list', 'digest')) ? time() : null;
7708881fcc9SAdrian Lang    $params = compact('target', 'style', 'data', 'action');
77152b0dd67SGuy Brand}
77252b0dd67SGuy Brand
773e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
774