xref: /dokuwiki/inc/actions.php (revision c4f79b71351dd0d96f19f7c5629888d85a814c72)
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 $INFO;
206b13307fSandi    global $ACT;
216b13307fSandi    global $ID;
226b13307fSandi    global $QUERY;
236b13307fSandi    global $lang;
246b13307fSandi    global $conf;
25066fee30SAndreas Gohr    global $license;
266b13307fSandi
2769cd1e27SAndreas Gohr    $preact = $ACT;
2869cd1e27SAndreas Gohr
29c2e830f2Schris    // give plugins an opportunity to process the action
3024bb549bSchris    $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT);
3124bb549bSchris    if ($evt->advise_before()) {
32c2e830f2Schris
33af182434Sandi        //sanitize $ACT
34af182434Sandi        $ACT = act_clean($ACT);
35af182434Sandi
36b8957367SBenjamin Gilbert        //check if searchword was given - else just show
370868021bSAndreas Gohr        $s = cleanID($QUERY);
380868021bSAndreas Gohr        if($ACT == 'search' && empty($s)){
39b8957367SBenjamin Gilbert            $ACT = 'show';
40b8957367SBenjamin Gilbert        }
41b8957367SBenjamin Gilbert
42b8957367SBenjamin Gilbert        //login stuff
431b2a85e8SAndreas Gohr        if(in_array($ACT,array('login','logout'))){
44b8957367SBenjamin Gilbert            $ACT = act_auth($ACT);
451b2a85e8SAndreas Gohr        }
46b8957367SBenjamin Gilbert
471380fc45SAndreas Gohr        //check if user is asking to (un)subscribe a page
485b75cd1fSAdrian Lang        if($ACT == 'subscribe') {
495b75cd1fSAdrian Lang            try {
501380fc45SAndreas Gohr                $ACT = act_subscription($ACT);
515b75cd1fSAdrian Lang            } catch (Exception $e) {
525b75cd1fSAdrian Lang                msg($e->getMessage(), -1);
535b75cd1fSAdrian Lang            }
545b75cd1fSAdrian Lang        }
5552b0dd67SGuy Brand
566b13307fSandi        //check permissions
576b13307fSandi        $ACT = act_permcheck($ACT);
586b13307fSandi
59*c4f79b71SMichael Hamann        //sitemap
60*c4f79b71SMichael Hamann        if ($ACT == 'sitemap')
61*c4f79b71SMichael Hamann            $ACT = act_sitemap($ACT);
62*c4f79b71SMichael Hamann
63b8957367SBenjamin Gilbert        //register
64c9570649SAndreas Gohr        $nil = array();
65b3510079SAndreas Gohr        if($ACT == 'register' && $_POST['save'] && register()){
66b8957367SBenjamin Gilbert            $ACT = 'login';
67b8957367SBenjamin Gilbert        }
686b13307fSandi
698b06d178Schris        if ($ACT == 'resendpwd' && act_resendpwd()) {
708b06d178Schris            $ACT = 'login';
718b06d178Schris        }
728b06d178Schris
738b06d178Schris        //update user profile
7425b2a98cSMichael Klier        if ($ACT == 'profile') {
7525b2a98cSMichael Klier            if(!$_SERVER['REMOTE_USER']) {
7625b2a98cSMichael Klier                $ACT = 'login';
7725b2a98cSMichael Klier            } else {
7825b2a98cSMichael Klier                if(updateprofile()) {
794cb79657SMatthias Grimm                    msg($lang['profchanged'],1);
804cb79657SMatthias Grimm                    $ACT = 'show';
818b06d178Schris                }
8225b2a98cSMichael Klier            }
8325b2a98cSMichael Klier        }
848b06d178Schris
851246e016SAndreas Gohr        //revert
861246e016SAndreas Gohr        if($ACT == 'revert'){
871246e016SAndreas Gohr            if(checkSecurityToken()){
881246e016SAndreas Gohr                $ACT = act_revert($ACT);
891246e016SAndreas Gohr            }else{
901246e016SAndreas Gohr                $ACT = 'show';
911246e016SAndreas Gohr            }
921246e016SAndreas Gohr        }
931246e016SAndreas Gohr
946b13307fSandi        //save
951b2a85e8SAndreas Gohr        if($ACT == 'save'){
961b2a85e8SAndreas Gohr            if(checkSecurityToken()){
976b13307fSandi                $ACT = act_save($ACT);
981b2a85e8SAndreas Gohr            }else{
991b2a85e8SAndreas Gohr                $ACT = 'show';
1001b2a85e8SAndreas Gohr            }
1011b2a85e8SAndreas Gohr        }
1026b13307fSandi
103067c5d22SBen Coburn        //cancel conflicting edit
104067c5d22SBen Coburn        if($ACT == 'cancel')
105067c5d22SBen Coburn            $ACT = 'show';
106067c5d22SBen Coburn
107ee4c4a1bSAndreas Gohr        //draft deletion
108ee4c4a1bSAndreas Gohr        if($ACT == 'draftdel')
109ee4c4a1bSAndreas Gohr            $ACT = act_draftdel($ACT);
110ee4c4a1bSAndreas Gohr
111ee4c4a1bSAndreas Gohr        //draft saving on preview
112ee4c4a1bSAndreas Gohr        if($ACT == 'preview')
113ee4c4a1bSAndreas Gohr            $ACT = act_draftsave($ACT);
114ee4c4a1bSAndreas Gohr
1156b13307fSandi        //edit
116c9d5430bSAdrian Lang        if(in_array($ACT, array('edit', 'preview', 'recover'))) {
117af182434Sandi            $ACT = act_edit($ACT);
1186b13307fSandi        }else{
1196b13307fSandi            unlock($ID); //try to unlock
1206b13307fSandi        }
1216b13307fSandi
1226b13307fSandi        //handle export
123ac83b9d8Sandi        if(substr($ACT,0,7) == 'export_')
1246b13307fSandi            $ACT = act_export($ACT);
1256b13307fSandi
1266b13307fSandi        //display some infos
1276b13307fSandi        if($ACT == 'check'){
1286b13307fSandi            check();
1296b13307fSandi            $ACT = 'show';
1306b13307fSandi        }
1316b13307fSandi
132c19fe9c0Sandi        //handle admin tasks
133c19fe9c0Sandi        if($ACT == 'admin'){
13411e2ce22Schris            // retrieve admin plugin name from $_REQUEST['page']
135bb4866bdSchris            if (!empty($_REQUEST['page'])) {
13611e2ce22Schris                $pluginlist = plugin_list('admin');
13711e2ce22Schris                if (in_array($_REQUEST['page'], $pluginlist)) {
13811e2ce22Schris                    // attempt to load the plugin
13949eb6e38SAndreas Gohr                    if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== null)
14011e2ce22Schris                        $plugin->handle();
14111e2ce22Schris                }
14211e2ce22Schris            }
143c19fe9c0Sandi        }
1445f312bacSAndreas Gohr
1455f312bacSAndreas Gohr        // check permissions again - the action may have changed
1465f312bacSAndreas Gohr        $ACT = act_permcheck($ACT);
14724bb549bSchris    }  // end event ACTION_ACT_PREPROCESS default action
14824bb549bSchris    $evt->advise_after();
14924bb549bSchris    unset($evt);
150c19fe9c0Sandi
15146c0ed74SMichael Hamann    // when action 'show', the intial not 'show' and POST, do a redirect
15246c0ed74SMichael Hamann    if($ACT == 'show' && $preact != 'show' && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
15369cd1e27SAndreas Gohr        act_redirect($ID,$preact);
15469cd1e27SAndreas Gohr    }
1555f312bacSAndreas Gohr
1566b13307fSandi    //call template FIXME: all needed vars available?
157f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
158746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
159f63a2007Schris
1605a892029SAndreas Gohr    include(template('main.php'));
161c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
162c19fe9c0Sandi    // in function tpl_content()
1636b13307fSandi}
1646b13307fSandi
165f63a2007Schrisfunction act_sendheaders($headers) {
166f63a2007Schris    foreach ($headers as $hdr) header($hdr);
167f63a2007Schris}
168f63a2007Schris
1696b13307fSandi/**
170af182434Sandi * Sanitize the action command
171af182434Sandi *
172af182434Sandi * Add all allowed commands here.
173af182434Sandi *
174af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
175af182434Sandi */
176af182434Sandifunction act_clean($act){
177af182434Sandi    global $lang;
17860e6b550SAndreas Gohr    global $conf;
179af182434Sandi
180ee4c4a1bSAndreas Gohr    // check if the action was given as array key
181ee4c4a1bSAndreas Gohr    if(is_array($act)){
182ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
183ee4c4a1bSAndreas Gohr    }
184ee4c4a1bSAndreas Gohr
185ac83b9d8Sandi    //remove all bad chars
186ac83b9d8Sandi    $act = strtolower($act);
1872d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
188ac83b9d8Sandi
189ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
190cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
191b146b32bSandi
192396c218fSAndreas Gohr    if($act === '') $act = 'show';
193396c218fSAndreas Gohr
194409d7af7SAndreas Gohr    // check if action is disabled
195409d7af7SAndreas Gohr    if(!actionOK($act)){
196409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
197409d7af7SAndreas Gohr        return 'show';
198409d7af7SAndreas Gohr    }
199409d7af7SAndreas Gohr
20060e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
20160e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
2021246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
20352b0dd67SGuy Brand                    'resendpwd','subscribens','unsubscribens',))){
20460e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
20560e6b550SAndreas Gohr        return 'show';
20660e6b550SAndreas Gohr    }
20760e6b550SAndreas Gohr
208067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
209ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2101246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2115a932e77SAdrian Lang                    'unsubscribe','profile','resendpwd','recover',
212*c4f79b71SMichael Hamann                    'draftdel','subscribens','unsubscribens','sitemap')) && substr($act,0,7) != 'export_' ) {
213ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
214af182434Sandi        return 'show';
215af182434Sandi    }
216af182434Sandi    return $act;
217af182434Sandi}
218af182434Sandi
219af182434Sandi/**
2206b13307fSandi * Run permissionchecks
2216b13307fSandi *
2226b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2236b13307fSandi */
2246b13307fSandifunction act_permcheck($act){
225dbbc6aa7Sandi    global $INFO;
2265e199953Smatthiasgrimm    global $conf;
227dbbc6aa7Sandi
228ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2296b13307fSandi        if($INFO['exists']){
230bdbc16bfSandi            if($act == 'edit'){
231bdbc16bfSandi                //the edit function will check again and do a source show
232bdbc16bfSandi                //when no AUTH_EDIT available
233bdbc16bfSandi                $permneed = AUTH_READ;
234bdbc16bfSandi            }else{
2356b13307fSandi                $permneed = AUTH_EDIT;
236bdbc16bfSandi            }
2376b13307fSandi        }else{
2386b13307fSandi            $permneed = AUTH_CREATE;
2396b13307fSandi        }
240*c4f79b71SMichael Hamann    }elseif(in_array($act,array('login','search','recent','profile','index', 'sitemap'))){
241*c4f79b71SMichael Hamann    }elseif(in_array($act,array('login','search','recent','profile','sitemap'))){
2426b13307fSandi        $permneed = AUTH_NONE;
2431246e016SAndreas Gohr    }elseif($act == 'revert'){
2441246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
2451246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
2465e199953Smatthiasgrimm    }elseif($act == 'register'){
2475e199953Smatthiasgrimm        $permneed = AUTH_NONE;
248ebd3d9ceSchris    }elseif($act == 'resendpwd'){
249ebd3d9ceSchris        $permneed = AUTH_NONE;
250c19fe9c0Sandi    }elseif($act == 'admin'){
251f8cc712eSAndreas Gohr        if($INFO['ismanager']){
252f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
253f8cc712eSAndreas Gohr            // action is checked later
254f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
255f8cc712eSAndreas Gohr        }else{
256c19fe9c0Sandi            $permneed = AUTH_ADMIN;
257f8cc712eSAndreas Gohr        }
2586b13307fSandi    }else{
2596b13307fSandi        $permneed = AUTH_READ;
2606b13307fSandi    }
261dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
262dbbc6aa7Sandi
2636b13307fSandi    return 'denied';
2646b13307fSandi}
2656b13307fSandi
2666b13307fSandi/**
267ee4c4a1bSAndreas Gohr * Handle 'draftdel'
268ee4c4a1bSAndreas Gohr *
269ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
270ee4c4a1bSAndreas Gohr */
271ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
272ee4c4a1bSAndreas Gohr    global $INFO;
273ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
274ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
275ee4c4a1bSAndreas Gohr    return 'show';
276ee4c4a1bSAndreas Gohr}
277ee4c4a1bSAndreas Gohr
278ee4c4a1bSAndreas Gohr/**
279ee4c4a1bSAndreas Gohr * Saves a draft on preview
280ee4c4a1bSAndreas Gohr *
281ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
282ee4c4a1bSAndreas Gohr */
283ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
284ee4c4a1bSAndreas Gohr    global $INFO;
285ee4c4a1bSAndreas Gohr    global $ID;
286ee4c4a1bSAndreas Gohr    global $conf;
287ee4c4a1bSAndreas Gohr    if($conf['usedraft'] && $_POST['wikitext']){
288ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
289ee4c4a1bSAndreas Gohr                'prefix' => $_POST['prefix'],
290ee4c4a1bSAndreas Gohr                'text'   => $_POST['wikitext'],
291ee4c4a1bSAndreas Gohr                'suffix' => $_POST['suffix'],
292ee4c4a1bSAndreas Gohr                'date'   => $_POST['date'],
293ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
294ee4c4a1bSAndreas Gohr                );
295ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
296ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
297ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
298ee4c4a1bSAndreas Gohr        }
299ee4c4a1bSAndreas Gohr    }
300ee4c4a1bSAndreas Gohr    return $act;
301ee4c4a1bSAndreas Gohr}
302ee4c4a1bSAndreas Gohr
303ee4c4a1bSAndreas Gohr/**
3046b13307fSandi * Handle 'save'
3056b13307fSandi *
3066b13307fSandi * Checks for spam and conflicts and saves the page.
3076b13307fSandi * Does a redirect to show the page afterwards or
3086b13307fSandi * returns a new action.
3096b13307fSandi *
3106b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3116b13307fSandi */
3126b13307fSandifunction act_save($act){
3136b13307fSandi    global $ID;
3146b13307fSandi    global $DATE;
3156b13307fSandi    global $PRE;
3166b13307fSandi    global $TEXT;
3176b13307fSandi    global $SUF;
3186b13307fSandi    global $SUM;
3195a932e77SAdrian Lang    global $lang;
3208d67c48aSAdrian Lang    global $INFO;
3216b13307fSandi
3226b13307fSandi    //spam check
3235a932e77SAdrian Lang    if(checkwordblock()) {
3245a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3255a932e77SAdrian Lang        return 'edit';
3265a932e77SAdrian Lang    }
3278d67c48aSAdrian Lang    //conflict check
3288d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3296b13307fSandi        return 'conflict';
3306b13307fSandi
3316b13307fSandi    //save it
332b6912aeaSAndreas Gohr    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con
3336b13307fSandi    //unlock it
3346b13307fSandi    unlock($ID);
3356b13307fSandi
336ee4c4a1bSAndreas Gohr    //delete draft
337ee4c4a1bSAndreas Gohr    act_draftdel($act);
33869cd1e27SAndreas Gohr    session_write_close();
339ee4c4a1bSAndreas Gohr
34069cd1e27SAndreas Gohr    // when done, show page
34169cd1e27SAndreas Gohr    return 'show';
34269cd1e27SAndreas Gohr}
343f951a474SAndreas Gohr
34414a122deSAndreas Gohr/**
3451246e016SAndreas Gohr * Revert to a certain revision
3461246e016SAndreas Gohr *
3471246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3481246e016SAndreas Gohr */
3491246e016SAndreas Gohrfunction act_revert($act){
3501246e016SAndreas Gohr    global $ID;
3511246e016SAndreas Gohr    global $REV;
3521246e016SAndreas Gohr    global $lang;
353de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
354de4d479aSAdrian Lang    // global $INFO;
355de4d479aSAdrian Lang    // if (!$INFO['writable']) {
356de4d479aSAdrian Lang    //     return 'show';
357de4d479aSAdrian Lang    // }
3581246e016SAndreas Gohr
3591246e016SAndreas Gohr    // when no revision is given, delete current one
3601246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
3611246e016SAndreas Gohr    $text = '';
3621246e016SAndreas Gohr    $sum  = $lang['deleted'];
3631246e016SAndreas Gohr    if($REV){
3641246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
3651246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
3661246e016SAndreas Gohr        $sum  = $lang['restored'];
3671246e016SAndreas Gohr    }
3681246e016SAndreas Gohr
3691246e016SAndreas Gohr    // spam check
3705a932e77SAdrian Lang
3715a932e77SAdrian Lang    if (checkwordblock($text)) {
3725a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3735a932e77SAdrian Lang        return 'edit';
3745a932e77SAdrian Lang    }
3751246e016SAndreas Gohr
3761246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
3771246e016SAndreas Gohr    msg($sum,1);
3781246e016SAndreas Gohr
3791246e016SAndreas Gohr    //delete any draft
3801246e016SAndreas Gohr    act_draftdel($act);
3811246e016SAndreas Gohr    session_write_close();
3821246e016SAndreas Gohr
3831246e016SAndreas Gohr    // when done, show current page
3841246e016SAndreas Gohr    $_SERVER['REQUEST_METHOD'] = 'post'; //should force a redirect
3851246e016SAndreas Gohr    $REV = '';
3861246e016SAndreas Gohr    return 'show';
3871246e016SAndreas Gohr}
3881246e016SAndreas Gohr
3891246e016SAndreas Gohr/**
39014a122deSAndreas Gohr * Do a redirect after receiving post data
39114a122deSAndreas Gohr *
39214a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
39314a122deSAndreas Gohr */
39469cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
39569cd1e27SAndreas Gohr    global $PRE;
39669cd1e27SAndreas Gohr    global $TEXT;
397f951a474SAndreas Gohr
39869cd1e27SAndreas Gohr    $opts = array(
39969cd1e27SAndreas Gohr            'id'       => $id,
40069cd1e27SAndreas Gohr            'preact'   => $preact
40169cd1e27SAndreas Gohr            );
402c66972f2SAdrian Lang    //get section name when coming from section edit
403c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
404c66972f2SAdrian Lang        $check = false; //Byref
405c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
406c66972f2SAdrian Lang    }
407c66972f2SAdrian Lang
40869cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
40969cd1e27SAndreas Gohr}
41069cd1e27SAndreas Gohr
41169cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
41269cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
413c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
41469cd1e27SAndreas Gohr
4156b13307fSandi    //show it
416af2408d5SAndreas Gohr    send_redirect($go);
4176b13307fSandi}
4186b13307fSandi
4196b13307fSandi/**
420b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4216b13307fSandi *
4226b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4236b13307fSandi */
4246b13307fSandifunction act_auth($act){
42508eda5bcSmatthiasgrimm    global $ID;
4267cace34dSAndreas Gohr    global $INFO;
42708eda5bcSmatthiasgrimm
4286b13307fSandi    //already logged in?
429c66972f2SAdrian Lang    if(isset($_SERVER['REMOTE_USER']) && $act=='login'){
430ca12ce46SAndreas Gohr        return 'show';
4312288dc06SGuy Brand    }
4326b13307fSandi
4336b13307fSandi    //handle logout
4346b13307fSandi    if($act=='logout'){
43508eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
436424c3c4fSJohannes Buchner        if($lockedby == $_SERVER['REMOTE_USER'])
43708eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
43808eda5bcSmatthiasgrimm
4397cace34dSAndreas Gohr        // do the logout stuff
4406b13307fSandi        auth_logoff();
4417cace34dSAndreas Gohr
4427cace34dSAndreas Gohr        // rebuild info array
4437cace34dSAndreas Gohr        $INFO = pageinfo();
4447cace34dSAndreas Gohr
445e16eccb7SGuy Brand        act_redirect($ID,'login');
4466b13307fSandi    }
4476b13307fSandi
4486b13307fSandi    return $act;
4496b13307fSandi}
4506b13307fSandi
4516b13307fSandi/**
45245a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
4536b13307fSandi *
4546b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4556b13307fSandi */
4566b13307fSandifunction act_edit($act){
457cd409024Sjorda    global $ID;
458ee4c4a1bSAndreas Gohr    global $INFO;
459cd409024Sjorda
46045a99335SAdrian Lang    global $TEXT;
46145a99335SAdrian Lang    global $RANGE;
46245a99335SAdrian Lang    global $PRE;
46345a99335SAdrian Lang    global $SUF;
46445a99335SAdrian Lang    global $REV;
46545a99335SAdrian Lang    global $SUM;
46645a99335SAdrian Lang    global $lang;
46745a99335SAdrian Lang    global $DATE;
46845a99335SAdrian Lang
46945a99335SAdrian Lang    if (!isset($TEXT)) {
47045a99335SAdrian Lang        if ($INFO['exists']) {
47145a99335SAdrian Lang            if ($RANGE) {
47245a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
47345a99335SAdrian Lang            } else {
47445a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
47545a99335SAdrian Lang            }
47645a99335SAdrian Lang        } else {
477fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
47845a99335SAdrian Lang        }
47945a99335SAdrian Lang    }
48045a99335SAdrian Lang
48145a99335SAdrian Lang    //set summary default
48245a99335SAdrian Lang    if(!$SUM){
48345a99335SAdrian Lang        if($REV){
48445a99335SAdrian Lang            $SUM = $lang['restored'];
48545a99335SAdrian Lang        }elseif(!$INFO['exists']){
48645a99335SAdrian Lang            $SUM = $lang['created'];
48745a99335SAdrian Lang        }
48845a99335SAdrian Lang    }
48945a99335SAdrian Lang
4908d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
4918d67c48aSAdrian Lang    // This is used for conflict detection
4928d67c48aSAdrian Lang    if(!$DATE) $DATE = $INFO['meta']['date']['modified'];
49345a99335SAdrian Lang
4946b13307fSandi    //check if locked by anyone - if not lock for my self
4956b13307fSandi    $lockedby = checklock($ID);
4966b13307fSandi    if($lockedby) return 'locked';
4976b13307fSandi
4986b13307fSandi    lock($ID);
4996b13307fSandi    return $act;
5006b13307fSandi}
5016b13307fSandi
5026b13307fSandi/**
503f6dad9fdSMichael Klier * Export a wiki page for various formats
504f6dad9fdSMichael Klier *
505f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
506f6dad9fdSMichael Klier *
507f6dad9fdSMichael Klier *  Event data:
508f6dad9fdSMichael Klier *    data['id']      -- page id
509f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
510f6dad9fdSMichael Klier *    data['headers'] -- export headers
511f6dad9fdSMichael Klier *    data['output']  -- export output
5126b13307fSandi *
5136b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
514f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5156b13307fSandi */
5166b13307fSandifunction act_export($act){
5176b13307fSandi    global $ID;
5186b13307fSandi    global $REV;
51985f8705cSAnika Henke    global $conf;
52085f8705cSAnika Henke    global $lang;
5216b13307fSandi
522f6dad9fdSMichael Klier    $pre = '';
523f6dad9fdSMichael Klier    $post = '';
524f6dad9fdSMichael Klier    $output = '';
525f6dad9fdSMichael Klier    $headers = array();
526cc2ae802SAndreas Gohr
527f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
528f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
529f6dad9fdSMichael Klier
530ac83b9d8Sandi    $mode = substr($act,7);
531f6dad9fdSMichael Klier    switch($mode) {
532f6dad9fdSMichael Klier        case 'raw':
5335adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
53466b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
535f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
536f6dad9fdSMichael Klier            break;
537f6dad9fdSMichael Klier        case 'xhtml':
538f6dad9fdSMichael Klier            $pre .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . DOKU_LF;
539f6dad9fdSMichael Klier            $pre .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . DOKU_LF;
540f6dad9fdSMichael Klier            $pre .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"' . DOKU_LF;
541f6dad9fdSMichael Klier            $pre .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
542f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
543f6dad9fdSMichael Klier            $pre .= '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . DOKU_LF;
544f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
545f6dad9fdSMichael Klier
546f6dad9fdSMichael Klier            // get metaheaders
547f6dad9fdSMichael Klier            ob_start();
548f6dad9fdSMichael Klier            tpl_metaheaders();
549f6dad9fdSMichael Klier            $pre .= ob_get_clean();
550f6dad9fdSMichael Klier
551f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
552f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
553f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
554f6dad9fdSMichael Klier
555f6dad9fdSMichael Klier            // get toc
556f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
557f6dad9fdSMichael Klier
558f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
559f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
560f6dad9fdSMichael Klier
561f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
562f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
563f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
564f6dad9fdSMichael Klier            break;
565f6dad9fdSMichael Klier        case 'xhtmlbody':
566f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
567f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
568f6dad9fdSMichael Klier            break;
569f6dad9fdSMichael Klier        default:
570f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
5719acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
572f6dad9fdSMichael Klier            break;
573f6dad9fdSMichael Klier    }
574f6dad9fdSMichael Klier
575f6dad9fdSMichael Klier    // prepare event data
576f6dad9fdSMichael Klier    $data = array();
577f6dad9fdSMichael Klier    $data['id'] = $ID;
578f6dad9fdSMichael Klier    $data['mode'] = $mode;
579f6dad9fdSMichael Klier    $data['headers'] = $headers;
580f6dad9fdSMichael Klier    $data['output'] =& $output;
581f6dad9fdSMichael Klier
582f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
583f6dad9fdSMichael Klier
584f6dad9fdSMichael Klier    if(!empty($data['output'])){
585f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
58685767031SAndreas Gohr            header("$key: $val");
58785767031SAndreas Gohr        }
588f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
5896b13307fSandi        exit;
5906b13307fSandi    }
5916b13307fSandi    return 'show';
5926b13307fSandi}
593340756e4Sandi
594b158d625SSteven Danz/**
595*c4f79b71SMichael Hamann * Handle sitemap delivery
596*c4f79b71SMichael Hamann *
597*c4f79b71SMichael Hamann * @author Michael Hamann <michael@content-space.de>
598*c4f79b71SMichael Hamann */
599*c4f79b71SMichael Hamannfunction act_sitemap($act) {
600*c4f79b71SMichael Hamann    global $conf;
601*c4f79b71SMichael Hamann
602*c4f79b71SMichael Hamann    if (!$conf['sitemap']) {
603*c4f79b71SMichael Hamann        header("HTTP/1.0 404 Not Found");
604*c4f79b71SMichael Hamann        print "Sitemap generation is disabled.";
605*c4f79b71SMichael Hamann        exit;
606*c4f79b71SMichael Hamann    }
607*c4f79b71SMichael Hamann
608*c4f79b71SMichael Hamann    $sitemap = $conf['cachedir'].'/sitemap.xml';
609*c4f79b71SMichael Hamann    if($conf['compression'] == 'bz2' || $conf['compression'] == 'gz'){
610*c4f79b71SMichael Hamann        $mime = 'application/x-gzip';
611*c4f79b71SMichael Hamann        $sitemap .= '.gz';
612*c4f79b71SMichael Hamann    } else {
613*c4f79b71SMichael Hamann        $mime = 'application/xml; charset=utf-8';
614*c4f79b71SMichael Hamann    }
615*c4f79b71SMichael Hamann
616*c4f79b71SMichael Hamann    // Check if sitemap file exists, otherwise create it
617*c4f79b71SMichael Hamann    if (!is_readable($sitemap)) {
618*c4f79b71SMichael Hamann        require_once DOKU_INC.'inc/sitemap.php';
619*c4f79b71SMichael Hamann        sitemapGenerate();
620*c4f79b71SMichael Hamann    }
621*c4f79b71SMichael Hamann
622*c4f79b71SMichael Hamann    if (is_readable($sitemap)) {
623*c4f79b71SMichael Hamann        // Send headers
624*c4f79b71SMichael Hamann        header('Content-Type: '.$mime);
625*c4f79b71SMichael Hamann
626*c4f79b71SMichael Hamann        // Send file
627*c4f79b71SMichael Hamann        //use x-sendfile header to pass the delivery to compatible webservers
628*c4f79b71SMichael Hamann        if (http_sendfile($sitemap)) exit;
629*c4f79b71SMichael Hamann
630*c4f79b71SMichael Hamann        $fp = @fopen($sitemap,"rb");
631*c4f79b71SMichael Hamann        if($fp){
632*c4f79b71SMichael Hamann            http_rangeRequest($fp,filesize($sitemap),$mime);
633*c4f79b71SMichael Hamann            exit;
634*c4f79b71SMichael Hamann        }
635*c4f79b71SMichael Hamann    }
636*c4f79b71SMichael Hamann
637*c4f79b71SMichael Hamann    header("HTTP/1.0 500 Internal Server Error");
638*c4f79b71SMichael Hamann    print "Could not read $sitemap - bad permissions?";
639*c4f79b71SMichael Hamann    exit;
640*c4f79b71SMichael Hamann}
641*c4f79b71SMichael Hamann
642*c4f79b71SMichael Hamann/**
6435b75cd1fSAdrian Lang * Handle page 'subscribe'
644b158d625SSteven Danz *
6455b75cd1fSAdrian Lang * Throws exception on error.
6465b75cd1fSAdrian Lang *
6475b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
648b158d625SSteven Danz */
6491380fc45SAndreas Gohrfunction act_subscription($act){
650056c2049SAndreas Gohr    global $lang;
651056c2049SAndreas Gohr    global $INFO;
652056c2049SAndreas Gohr    global $ID;
65352b0dd67SGuy Brand
6549fa341d0SAndreas Gohr    // subcriptions work for logged in users only
6559fa341d0SAndreas Gohr    if(!$_SERVER['REMOTE_USER']) return 'show';
6569fa341d0SAndreas Gohr
657056c2049SAndreas Gohr    // get and preprocess data.
6588881fcc9SAdrian Lang    $params = array();
6598881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
660056c2049SAndreas Gohr        if (isset($_REQUEST["sub_$param"])) {
661056c2049SAndreas Gohr            $params[$param] = $_REQUEST["sub_$param"];
6628881fcc9SAdrian Lang        }
6638881fcc9SAdrian Lang    }
6648881fcc9SAdrian Lang
665056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
66666d2bed9SAdrian Lang    if(!$params['action'] || !checkSecurityToken()) return $act;
667056c2049SAndreas Gohr
6688881fcc9SAdrian Lang    // Handle POST data, may throw exception.
6698881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
6708881fcc9SAdrian Lang
6718881fcc9SAdrian Lang    $target = $params['target'];
6728881fcc9SAdrian Lang    $style  = $params['style'];
6738881fcc9SAdrian Lang    $data   = $params['data'];
6748881fcc9SAdrian Lang    $action = $params['action'];
6758881fcc9SAdrian Lang
6768881fcc9SAdrian Lang    // Perform action.
6778881fcc9SAdrian Lang    if (!subscription_set($_SERVER['REMOTE_USER'], $target, $style, $data)) {
6788881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
6798881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
6808881fcc9SAdrian Lang                                    prettyprint_id($target)));
6818881fcc9SAdrian Lang    }
6828881fcc9SAdrian Lang    msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
6838881fcc9SAdrian Lang                prettyprint_id($target)), 1);
684cb3f9dbaSAdrian Lang    act_redirect($ID, $act);
685cb3f9dbaSAdrian Lang
686cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
687cb3f9dbaSAdrian Lang    $INFO['subscribed'] = get_info_subscribed();
688cb3f9dbaSAdrian Lang    return 'show';
6898881fcc9SAdrian Lang}
6908881fcc9SAdrian Lang
6918881fcc9SAdrian Lang/**
6928881fcc9SAdrian Lang * Validate POST data
6938881fcc9SAdrian Lang *
6948881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
6958881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
6968881fcc9SAdrian Lang *
6978881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
6988881fcc9SAdrian Lang */
6997a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
7008881fcc9SAdrian Lang    global $INFO;
7018881fcc9SAdrian Lang    global $lang;
7028881fcc9SAdrian Lang
7035b75cd1fSAdrian Lang    // Get and validate parameters.
7048881fcc9SAdrian Lang    if (!isset($params['target'])) {
70515741132SAndreas Gohr        throw new Exception('no subscription target given');
7065b75cd1fSAdrian Lang    }
7078881fcc9SAdrian Lang    $target = $params['target'];
7085b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
7095b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
7105b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
7115b75cd1fSAdrian Lang        $valid_styles[] = 'list';
7125b75cd1fSAdrian Lang    }
7138881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
71415741132SAndreas Gohr                              'invalid subscription style given');
7158881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
71615741132SAndreas Gohr                              $params, 'invalid subscription action given');
717613964ecSGuy Brand
7185b75cd1fSAdrian Lang    // Check other conditions.
7195b75cd1fSAdrian Lang    if ($action === 'subscribe') {
7205b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
7215b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
72252b0dd67SGuy Brand        }
7235b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
7245b75cd1fSAdrian Lang        $is = false;
7255b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
7265b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
7275b75cd1fSAdrian Lang                $is = true;
72852b0dd67SGuy Brand            }
72952b0dd67SGuy Brand        }
7305b75cd1fSAdrian Lang        if ($is === false) {
73115741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
73215741132SAndreas Gohr                                        $_SERVER['REMOTE_USER'],
7335b75cd1fSAdrian Lang                                        prettyprint_id($target)));
7345b75cd1fSAdrian Lang        }
7355b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
7365b75cd1fSAdrian Lang        $style = null;
73752b0dd67SGuy Brand    }
73852b0dd67SGuy Brand
7398881fcc9SAdrian Lang    $data = in_array($style, array('list', 'digest')) ? time() : null;
7408881fcc9SAdrian Lang    $params = compact('target', 'style', 'data', 'action');
74152b0dd67SGuy Brand}
74252b0dd67SGuy Brand
743340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
744