xref: /dokuwiki/inc/actions.php (revision c346111a3d99754f72da068bb2e9518b4676d5a4)
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;
216b13307fSandi    global $QUERY;
226b13307fSandi    global $lang;
236b13307fSandi
2469cd1e27SAndreas Gohr    $preact = $ACT;
2569cd1e27SAndreas Gohr
26c2e830f2Schris    // give plugins an opportunity to process the action
2724bb549bSchris    $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT);
2824bb549bSchris    if ($evt->advise_before()) {
29c2e830f2Schris
30af182434Sandi        //sanitize $ACT
31af182434Sandi        $ACT = act_clean($ACT);
32af182434Sandi
33b8957367SBenjamin Gilbert        //check if searchword was given - else just show
340868021bSAndreas Gohr        $s = cleanID($QUERY);
350868021bSAndreas Gohr        if($ACT == 'search' && empty($s)){
36b8957367SBenjamin Gilbert            $ACT = 'show';
37b8957367SBenjamin Gilbert        }
38b8957367SBenjamin Gilbert
39b8957367SBenjamin Gilbert        //login stuff
401b2a85e8SAndreas Gohr        if(in_array($ACT,array('login','logout'))){
41b8957367SBenjamin Gilbert            $ACT = act_auth($ACT);
421b2a85e8SAndreas Gohr        }
43b8957367SBenjamin Gilbert
441380fc45SAndreas Gohr        //check if user is asking to (un)subscribe a page
455b75cd1fSAdrian Lang        if($ACT == 'subscribe') {
465b75cd1fSAdrian Lang            try {
471380fc45SAndreas Gohr                $ACT = act_subscription($ACT);
485b75cd1fSAdrian Lang            } catch (Exception $e) {
495b75cd1fSAdrian Lang                msg($e->getMessage(), -1);
505b75cd1fSAdrian Lang            }
515b75cd1fSAdrian Lang        }
5252b0dd67SGuy Brand
536b13307fSandi        //check permissions
546b13307fSandi        $ACT = act_permcheck($ACT);
556b13307fSandi
56b8957367SBenjamin Gilbert        //register
57b3510079SAndreas Gohr        if($ACT == 'register' && $_POST['save'] && register()){
58b8957367SBenjamin Gilbert            $ACT = 'login';
59b8957367SBenjamin Gilbert        }
606b13307fSandi
618b06d178Schris        if ($ACT == 'resendpwd' && act_resendpwd()) {
628b06d178Schris            $ACT = 'login';
638b06d178Schris        }
648b06d178Schris
658b06d178Schris        //update user profile
6625b2a98cSMichael Klier        if ($ACT == 'profile') {
6725b2a98cSMichael Klier            if(!$_SERVER['REMOTE_USER']) {
6825b2a98cSMichael Klier                $ACT = 'login';
6925b2a98cSMichael Klier            } else {
7025b2a98cSMichael Klier                if(updateprofile()) {
714cb79657SMatthias Grimm                    msg($lang['profchanged'],1);
724cb79657SMatthias Grimm                    $ACT = 'show';
738b06d178Schris                }
7425b2a98cSMichael Klier            }
7525b2a98cSMichael Klier        }
768b06d178Schris
771246e016SAndreas Gohr        //revert
781246e016SAndreas Gohr        if($ACT == 'revert'){
791246e016SAndreas Gohr            if(checkSecurityToken()){
801246e016SAndreas Gohr                $ACT = act_revert($ACT);
811246e016SAndreas Gohr            }else{
821246e016SAndreas Gohr                $ACT = 'show';
831246e016SAndreas Gohr            }
841246e016SAndreas Gohr        }
851246e016SAndreas Gohr
866b13307fSandi        //save
871b2a85e8SAndreas Gohr        if($ACT == 'save'){
881b2a85e8SAndreas Gohr            if(checkSecurityToken()){
896b13307fSandi                $ACT = act_save($ACT);
901b2a85e8SAndreas Gohr            }else{
911b2a85e8SAndreas Gohr                $ACT = 'show';
921b2a85e8SAndreas Gohr            }
931b2a85e8SAndreas Gohr        }
946b13307fSandi
95067c5d22SBen Coburn        //cancel conflicting edit
96067c5d22SBen Coburn        if($ACT == 'cancel')
97067c5d22SBen Coburn            $ACT = 'show';
98067c5d22SBen Coburn
99ee4c4a1bSAndreas Gohr        //draft deletion
100ee4c4a1bSAndreas Gohr        if($ACT == 'draftdel')
101ee4c4a1bSAndreas Gohr            $ACT = act_draftdel($ACT);
102ee4c4a1bSAndreas Gohr
103ee4c4a1bSAndreas Gohr        //draft saving on preview
104ee4c4a1bSAndreas Gohr        if($ACT == 'preview')
105ee4c4a1bSAndreas Gohr            $ACT = act_draftsave($ACT);
106ee4c4a1bSAndreas Gohr
1076b13307fSandi        //edit
108c9d5430bSAdrian Lang        if(in_array($ACT, array('edit', 'preview', 'recover'))) {
109af182434Sandi            $ACT = act_edit($ACT);
1106b13307fSandi        }else{
1116b13307fSandi            unlock($ID); //try to unlock
1126b13307fSandi        }
1136b13307fSandi
1146b13307fSandi        //handle export
115ac83b9d8Sandi        if(substr($ACT,0,7) == 'export_')
1166b13307fSandi            $ACT = act_export($ACT);
1176b13307fSandi
1186b13307fSandi        //display some infos
1196b13307fSandi        if($ACT == 'check'){
1206b13307fSandi            check();
1216b13307fSandi            $ACT = 'show';
1226b13307fSandi        }
1236b13307fSandi
124c19fe9c0Sandi        //handle admin tasks
125c19fe9c0Sandi        if($ACT == 'admin'){
12611e2ce22Schris            // retrieve admin plugin name from $_REQUEST['page']
127bb4866bdSchris            if (!empty($_REQUEST['page'])) {
12811e2ce22Schris                $pluginlist = plugin_list('admin');
12911e2ce22Schris                if (in_array($_REQUEST['page'], $pluginlist)) {
13011e2ce22Schris                    // attempt to load the plugin
13149eb6e38SAndreas Gohr                    if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== null)
13211e2ce22Schris                        $plugin->handle();
13311e2ce22Schris                }
13411e2ce22Schris            }
135c19fe9c0Sandi        }
1365f312bacSAndreas Gohr
1375f312bacSAndreas Gohr        // check permissions again - the action may have changed
1385f312bacSAndreas Gohr        $ACT = act_permcheck($ACT);
13924bb549bSchris    }  // end event ACTION_ACT_PREPROCESS default action
14024bb549bSchris    $evt->advise_after();
14124bb549bSchris    unset($evt);
142c19fe9c0Sandi
14346c0ed74SMichael Hamann    // when action 'show', the intial not 'show' and POST, do a redirect
14446c0ed74SMichael Hamann    if($ACT == 'show' && $preact != 'show' && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){
14569cd1e27SAndreas Gohr        act_redirect($ID,$preact);
14669cd1e27SAndreas Gohr    }
1475f312bacSAndreas Gohr
148*c346111aSAdrian Lang    global $INFO;
149*c346111aSAdrian Lang    global $conf;
150*c346111aSAdrian Lang    global $license;
151*c346111aSAdrian Lang
1526b13307fSandi    //call template FIXME: all needed vars available?
153f63a2007Schris    $headers[] = 'Content-Type: text/html; charset=utf-8';
154746855cfSBen Coburn    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
155f63a2007Schris
1565a892029SAndreas Gohr    include(template('main.php'));
157c19fe9c0Sandi    // output for the commands is now handled in inc/templates.php
158c19fe9c0Sandi    // in function tpl_content()
1596b13307fSandi}
1606b13307fSandi
161f63a2007Schrisfunction act_sendheaders($headers) {
162f63a2007Schris    foreach ($headers as $hdr) header($hdr);
163f63a2007Schris}
164f63a2007Schris
1656b13307fSandi/**
166af182434Sandi * Sanitize the action command
167af182434Sandi *
168af182434Sandi * Add all allowed commands here.
169af182434Sandi *
170af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
171af182434Sandi */
172af182434Sandifunction act_clean($act){
173af182434Sandi    global $lang;
17460e6b550SAndreas Gohr    global $conf;
175af182434Sandi
176ee4c4a1bSAndreas Gohr    // check if the action was given as array key
177ee4c4a1bSAndreas Gohr    if(is_array($act)){
178ee4c4a1bSAndreas Gohr        list($act) = array_keys($act);
179ee4c4a1bSAndreas Gohr    }
180ee4c4a1bSAndreas Gohr
181ac83b9d8Sandi    //remove all bad chars
182ac83b9d8Sandi    $act = strtolower($act);
1832d5ccb39SAndreas Gohr    $act = preg_replace('/[^1-9a-z_]+/','',$act);
184ac83b9d8Sandi
185ac83b9d8Sandi    if($act == 'export_html') $act = 'export_xhtml';
186cc2ae802SAndreas Gohr    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
187b146b32bSandi
188396c218fSAndreas Gohr    if($act === '') $act = 'show';
189396c218fSAndreas Gohr
190409d7af7SAndreas Gohr    // check if action is disabled
191409d7af7SAndreas Gohr    if(!actionOK($act)){
192409d7af7SAndreas Gohr        msg('Command disabled: '.htmlspecialchars($act),-1);
193409d7af7SAndreas Gohr        return 'show';
194409d7af7SAndreas Gohr    }
195409d7af7SAndreas Gohr
19660e6b550SAndreas Gohr    //disable all acl related commands if ACL is disabled
19760e6b550SAndreas Gohr    if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
1981246e016SAndreas Gohr                    'subscribe','unsubscribe','profile','revert',
19952b0dd67SGuy Brand                    'resendpwd','subscribens','unsubscribens',))){
20060e6b550SAndreas Gohr        msg('Command unavailable: '.htmlspecialchars($act),-1);
20160e6b550SAndreas Gohr        return 'show';
20260e6b550SAndreas Gohr    }
20360e6b550SAndreas Gohr
204067c5d22SBen Coburn    if(!in_array($act,array('login','logout','register','save','cancel','edit','draft',
205ac83b9d8Sandi                    'preview','search','show','check','index','revisions',
2061246e016SAndreas Gohr                    'diff','recent','backlink','admin','subscribe','revert',
2075a932e77SAdrian Lang                    'unsubscribe','profile','resendpwd','recover',
20852b0dd67SGuy Brand                    'draftdel','subscribens','unsubscribens',)) && substr($act,0,7) != 'export_' ) {
209ee4c4a1bSAndreas Gohr        msg('Command unknown: '.htmlspecialchars($act),-1);
210af182434Sandi        return 'show';
211af182434Sandi    }
212af182434Sandi    return $act;
213af182434Sandi}
214af182434Sandi
215af182434Sandi/**
2166b13307fSandi * Run permissionchecks
2176b13307fSandi *
2186b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2196b13307fSandi */
2206b13307fSandifunction act_permcheck($act){
221dbbc6aa7Sandi    global $INFO;
2225e199953Smatthiasgrimm    global $conf;
223dbbc6aa7Sandi
224ee4c4a1bSAndreas Gohr    if(in_array($act,array('save','preview','edit','recover'))){
2256b13307fSandi        if($INFO['exists']){
226bdbc16bfSandi            if($act == 'edit'){
227bdbc16bfSandi                //the edit function will check again and do a source show
228bdbc16bfSandi                //when no AUTH_EDIT available
229bdbc16bfSandi                $permneed = AUTH_READ;
230bdbc16bfSandi            }else{
2316b13307fSandi                $permneed = AUTH_EDIT;
232bdbc16bfSandi            }
2336b13307fSandi        }else{
2346b13307fSandi            $permneed = AUTH_CREATE;
2356b13307fSandi        }
236134b7bd9SAndreas Gohr    }elseif(in_array($act,array('login','search','recent','profile','index'))){
2376b13307fSandi        $permneed = AUTH_NONE;
2381246e016SAndreas Gohr    }elseif($act == 'revert'){
2391246e016SAndreas Gohr        $permneed = AUTH_ADMIN;
2401246e016SAndreas Gohr        if($INFO['ismanager']) $permneed = AUTH_EDIT;
2415e199953Smatthiasgrimm    }elseif($act == 'register'){
2425e199953Smatthiasgrimm        $permneed = AUTH_NONE;
243ebd3d9ceSchris    }elseif($act == 'resendpwd'){
244ebd3d9ceSchris        $permneed = AUTH_NONE;
245c19fe9c0Sandi    }elseif($act == 'admin'){
246f8cc712eSAndreas Gohr        if($INFO['ismanager']){
247f8cc712eSAndreas Gohr            // if the manager has the needed permissions for a certain admin
248f8cc712eSAndreas Gohr            // action is checked later
249f8cc712eSAndreas Gohr            $permneed = AUTH_READ;
250f8cc712eSAndreas Gohr        }else{
251c19fe9c0Sandi            $permneed = AUTH_ADMIN;
252f8cc712eSAndreas Gohr        }
2536b13307fSandi    }else{
2546b13307fSandi        $permneed = AUTH_READ;
2556b13307fSandi    }
256dbbc6aa7Sandi    if($INFO['perm'] >= $permneed) return $act;
257dbbc6aa7Sandi
2586b13307fSandi    return 'denied';
2596b13307fSandi}
2606b13307fSandi
2616b13307fSandi/**
262ee4c4a1bSAndreas Gohr * Handle 'draftdel'
263ee4c4a1bSAndreas Gohr *
264ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
265ee4c4a1bSAndreas Gohr */
266ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
267ee4c4a1bSAndreas Gohr    global $INFO;
268ee4c4a1bSAndreas Gohr    @unlink($INFO['draft']);
269ee4c4a1bSAndreas Gohr    $INFO['draft'] = null;
270ee4c4a1bSAndreas Gohr    return 'show';
271ee4c4a1bSAndreas Gohr}
272ee4c4a1bSAndreas Gohr
273ee4c4a1bSAndreas Gohr/**
274ee4c4a1bSAndreas Gohr * Saves a draft on preview
275ee4c4a1bSAndreas Gohr *
276ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
277ee4c4a1bSAndreas Gohr */
278ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
279ee4c4a1bSAndreas Gohr    global $INFO;
280ee4c4a1bSAndreas Gohr    global $ID;
281ee4c4a1bSAndreas Gohr    global $conf;
282ee4c4a1bSAndreas Gohr    if($conf['usedraft'] && $_POST['wikitext']){
283ee4c4a1bSAndreas Gohr        $draft = array('id'     => $ID,
284ee4c4a1bSAndreas Gohr                'prefix' => $_POST['prefix'],
285ee4c4a1bSAndreas Gohr                'text'   => $_POST['wikitext'],
286ee4c4a1bSAndreas Gohr                'suffix' => $_POST['suffix'],
287ee4c4a1bSAndreas Gohr                'date'   => $_POST['date'],
288ee4c4a1bSAndreas Gohr                'client' => $INFO['client'],
289ee4c4a1bSAndreas Gohr                );
290ee4c4a1bSAndreas Gohr        $cname = getCacheName($draft['client'].$ID,'.draft');
291ee4c4a1bSAndreas Gohr        if(io_saveFile($cname,serialize($draft))){
292ee4c4a1bSAndreas Gohr            $INFO['draft'] = $cname;
293ee4c4a1bSAndreas Gohr        }
294ee4c4a1bSAndreas Gohr    }
295ee4c4a1bSAndreas Gohr    return $act;
296ee4c4a1bSAndreas Gohr}
297ee4c4a1bSAndreas Gohr
298ee4c4a1bSAndreas Gohr/**
2996b13307fSandi * Handle 'save'
3006b13307fSandi *
3016b13307fSandi * Checks for spam and conflicts and saves the page.
3026b13307fSandi * Does a redirect to show the page afterwards or
3036b13307fSandi * returns a new action.
3046b13307fSandi *
3056b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3066b13307fSandi */
3076b13307fSandifunction act_save($act){
3086b13307fSandi    global $ID;
3096b13307fSandi    global $DATE;
3106b13307fSandi    global $PRE;
3116b13307fSandi    global $TEXT;
3126b13307fSandi    global $SUF;
3136b13307fSandi    global $SUM;
3145a932e77SAdrian Lang    global $lang;
3158d67c48aSAdrian Lang    global $INFO;
3166b13307fSandi
3176b13307fSandi    //spam check
3185a932e77SAdrian Lang    if(checkwordblock()) {
3195a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3205a932e77SAdrian Lang        return 'edit';
3215a932e77SAdrian Lang    }
3228d67c48aSAdrian Lang    //conflict check
3238d67c48aSAdrian Lang    if($DATE != 0 && $INFO['meta']['date']['modified'] > $DATE )
3246b13307fSandi        return 'conflict';
3256b13307fSandi
3266b13307fSandi    //save it
327b6912aeaSAndreas Gohr    saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con
3286b13307fSandi    //unlock it
3296b13307fSandi    unlock($ID);
3306b13307fSandi
331ee4c4a1bSAndreas Gohr    //delete draft
332ee4c4a1bSAndreas Gohr    act_draftdel($act);
33369cd1e27SAndreas Gohr    session_write_close();
334ee4c4a1bSAndreas Gohr
33569cd1e27SAndreas Gohr    // when done, show page
33669cd1e27SAndreas Gohr    return 'show';
33769cd1e27SAndreas Gohr}
338f951a474SAndreas Gohr
33914a122deSAndreas Gohr/**
3401246e016SAndreas Gohr * Revert to a certain revision
3411246e016SAndreas Gohr *
3421246e016SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3431246e016SAndreas Gohr */
3441246e016SAndreas Gohrfunction act_revert($act){
3451246e016SAndreas Gohr    global $ID;
3461246e016SAndreas Gohr    global $REV;
3471246e016SAndreas Gohr    global $lang;
348de4d479aSAdrian Lang    // FIXME $INFO['writable'] currently refers to the attic version
349de4d479aSAdrian Lang    // global $INFO;
350de4d479aSAdrian Lang    // if (!$INFO['writable']) {
351de4d479aSAdrian Lang    //     return 'show';
352de4d479aSAdrian Lang    // }
3531246e016SAndreas Gohr
3541246e016SAndreas Gohr    // when no revision is given, delete current one
3551246e016SAndreas Gohr    // FIXME this feature is not exposed in the GUI currently
3561246e016SAndreas Gohr    $text = '';
3571246e016SAndreas Gohr    $sum  = $lang['deleted'];
3581246e016SAndreas Gohr    if($REV){
3591246e016SAndreas Gohr        $text = rawWiki($ID,$REV);
3601246e016SAndreas Gohr        if(!$text) return 'show'; //something went wrong
3611246e016SAndreas Gohr        $sum  = $lang['restored'];
3621246e016SAndreas Gohr    }
3631246e016SAndreas Gohr
3641246e016SAndreas Gohr    // spam check
3655a932e77SAdrian Lang
3665a932e77SAdrian Lang    if (checkwordblock($text)) {
3675a932e77SAdrian Lang        msg($lang['wordblock'], -1);
3685a932e77SAdrian Lang        return 'edit';
3695a932e77SAdrian Lang    }
3701246e016SAndreas Gohr
3711246e016SAndreas Gohr    saveWikiText($ID,$text,$sum,false);
3721246e016SAndreas Gohr    msg($sum,1);
3731246e016SAndreas Gohr
3741246e016SAndreas Gohr    //delete any draft
3751246e016SAndreas Gohr    act_draftdel($act);
3761246e016SAndreas Gohr    session_write_close();
3771246e016SAndreas Gohr
3781246e016SAndreas Gohr    // when done, show current page
3791246e016SAndreas Gohr    $_SERVER['REQUEST_METHOD'] = 'post'; //should force a redirect
3801246e016SAndreas Gohr    $REV = '';
3811246e016SAndreas Gohr    return 'show';
3821246e016SAndreas Gohr}
3831246e016SAndreas Gohr
3841246e016SAndreas Gohr/**
38514a122deSAndreas Gohr * Do a redirect after receiving post data
38614a122deSAndreas Gohr *
38714a122deSAndreas Gohr * Tries to add the section id as hash mark after section editing
38814a122deSAndreas Gohr */
38969cd1e27SAndreas Gohrfunction act_redirect($id,$preact){
39069cd1e27SAndreas Gohr    global $PRE;
39169cd1e27SAndreas Gohr    global $TEXT;
392f951a474SAndreas Gohr
39369cd1e27SAndreas Gohr    $opts = array(
39469cd1e27SAndreas Gohr            'id'       => $id,
39569cd1e27SAndreas Gohr            'preact'   => $preact
39669cd1e27SAndreas Gohr            );
397c66972f2SAdrian Lang    //get section name when coming from section edit
398c66972f2SAdrian Lang    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
399c66972f2SAdrian Lang        $check = false; //Byref
400c66972f2SAdrian Lang        $opts['fragment'] = sectionID($match[0], $check);
401c66972f2SAdrian Lang    }
402c66972f2SAdrian Lang
40369cd1e27SAndreas Gohr    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
40469cd1e27SAndreas Gohr}
40569cd1e27SAndreas Gohr
40669cd1e27SAndreas Gohrfunction act_redirect_execute($opts){
40769cd1e27SAndreas Gohr    $go = wl($opts['id'],'',true);
408c66972f2SAdrian Lang    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
40969cd1e27SAndreas Gohr
4106b13307fSandi    //show it
411af2408d5SAndreas Gohr    send_redirect($go);
4126b13307fSandi}
4136b13307fSandi
4146b13307fSandi/**
415b8957367SBenjamin Gilbert * Handle 'login', 'logout'
4166b13307fSandi *
4176b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4186b13307fSandi */
4196b13307fSandifunction act_auth($act){
42008eda5bcSmatthiasgrimm    global $ID;
4217cace34dSAndreas Gohr    global $INFO;
42208eda5bcSmatthiasgrimm
4236b13307fSandi    //already logged in?
424c66972f2SAdrian Lang    if(isset($_SERVER['REMOTE_USER']) && $act=='login'){
425ca12ce46SAndreas Gohr        return 'show';
4262288dc06SGuy Brand    }
4276b13307fSandi
4286b13307fSandi    //handle logout
4296b13307fSandi    if($act=='logout'){
43008eda5bcSmatthiasgrimm        $lockedby = checklock($ID); //page still locked?
431424c3c4fSJohannes Buchner        if($lockedby == $_SERVER['REMOTE_USER'])
43208eda5bcSmatthiasgrimm            unlock($ID); //try to unlock
43308eda5bcSmatthiasgrimm
4347cace34dSAndreas Gohr        // do the logout stuff
4356b13307fSandi        auth_logoff();
4367cace34dSAndreas Gohr
4377cace34dSAndreas Gohr        // rebuild info array
4387cace34dSAndreas Gohr        $INFO = pageinfo();
4397cace34dSAndreas Gohr
440e16eccb7SGuy Brand        act_redirect($ID,'login');
4416b13307fSandi    }
4426b13307fSandi
4436b13307fSandi    return $act;
4446b13307fSandi}
4456b13307fSandi
4466b13307fSandi/**
44745a99335SAdrian Lang * Handle 'edit', 'preview', 'recover'
4486b13307fSandi *
4496b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
4506b13307fSandi */
4516b13307fSandifunction act_edit($act){
452cd409024Sjorda    global $ID;
453ee4c4a1bSAndreas Gohr    global $INFO;
454cd409024Sjorda
45545a99335SAdrian Lang    global $TEXT;
45645a99335SAdrian Lang    global $RANGE;
45745a99335SAdrian Lang    global $PRE;
45845a99335SAdrian Lang    global $SUF;
45945a99335SAdrian Lang    global $REV;
46045a99335SAdrian Lang    global $SUM;
46145a99335SAdrian Lang    global $lang;
46245a99335SAdrian Lang    global $DATE;
46345a99335SAdrian Lang
46445a99335SAdrian Lang    if (!isset($TEXT)) {
46545a99335SAdrian Lang        if ($INFO['exists']) {
46645a99335SAdrian Lang            if ($RANGE) {
46745a99335SAdrian Lang                list($PRE,$TEXT,$SUF) = rawWikiSlices($RANGE,$ID,$REV);
46845a99335SAdrian Lang            } else {
46945a99335SAdrian Lang                $TEXT = rawWiki($ID,$REV);
47045a99335SAdrian Lang            }
47145a99335SAdrian Lang        } else {
472fe17917eSAdrian Lang            $TEXT = pageTemplate($ID);
47345a99335SAdrian Lang        }
47445a99335SAdrian Lang    }
47545a99335SAdrian Lang
47645a99335SAdrian Lang    //set summary default
47745a99335SAdrian Lang    if(!$SUM){
47845a99335SAdrian Lang        if($REV){
47945a99335SAdrian Lang            $SUM = $lang['restored'];
48045a99335SAdrian Lang        }elseif(!$INFO['exists']){
48145a99335SAdrian Lang            $SUM = $lang['created'];
48245a99335SAdrian Lang        }
48345a99335SAdrian Lang    }
48445a99335SAdrian Lang
4858d67c48aSAdrian Lang    // Use the date of the newest revision, not of the revision we edit
4868d67c48aSAdrian Lang    // This is used for conflict detection
4878d67c48aSAdrian Lang    if(!$DATE) $DATE = $INFO['meta']['date']['modified'];
48845a99335SAdrian Lang
4896b13307fSandi    //check if locked by anyone - if not lock for my self
4906b13307fSandi    $lockedby = checklock($ID);
4916b13307fSandi    if($lockedby) return 'locked';
4926b13307fSandi
4936b13307fSandi    lock($ID);
4946b13307fSandi    return $act;
4956b13307fSandi}
4966b13307fSandi
4976b13307fSandi/**
498f6dad9fdSMichael Klier * Export a wiki page for various formats
499f6dad9fdSMichael Klier *
500f6dad9fdSMichael Klier * Triggers ACTION_EXPORT_POSTPROCESS
501f6dad9fdSMichael Klier *
502f6dad9fdSMichael Klier *  Event data:
503f6dad9fdSMichael Klier *    data['id']      -- page id
504f6dad9fdSMichael Klier *    data['mode']    -- requested export mode
505f6dad9fdSMichael Klier *    data['headers'] -- export headers
506f6dad9fdSMichael Klier *    data['output']  -- export output
5076b13307fSandi *
5086b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
509f6dad9fdSMichael Klier * @author Michael Klier <chi@chimeric.de>
5106b13307fSandi */
5116b13307fSandifunction act_export($act){
5126b13307fSandi    global $ID;
5136b13307fSandi    global $REV;
51485f8705cSAnika Henke    global $conf;
51585f8705cSAnika Henke    global $lang;
5166b13307fSandi
517f6dad9fdSMichael Klier    $pre = '';
518f6dad9fdSMichael Klier    $post = '';
519f6dad9fdSMichael Klier    $output = '';
520f6dad9fdSMichael Klier    $headers = array();
521cc2ae802SAndreas Gohr
522f6dad9fdSMichael Klier    // search engines: never cache exported docs! (Google only currently)
523f6dad9fdSMichael Klier    $headers['X-Robots-Tag'] = 'noindex';
524f6dad9fdSMichael Klier
525ac83b9d8Sandi    $mode = substr($act,7);
526f6dad9fdSMichael Klier    switch($mode) {
527f6dad9fdSMichael Klier        case 'raw':
5285adfc5afSAnika Henke            $headers['Content-Type'] = 'text/plain; charset=utf-8';
52966b23ce9SAndreas Gohr            $headers['Content-Disposition'] = 'attachment; filename='.noNS($ID).'.txt';
530f6dad9fdSMichael Klier            $output = rawWiki($ID,$REV);
531f6dad9fdSMichael Klier            break;
532f6dad9fdSMichael Klier        case 'xhtml':
533f6dad9fdSMichael Klier            $pre .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . DOKU_LF;
534f6dad9fdSMichael Klier            $pre .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . DOKU_LF;
535f6dad9fdSMichael Klier            $pre .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"' . DOKU_LF;
536f6dad9fdSMichael Klier            $pre .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF;
537f6dad9fdSMichael Klier            $pre .= '<head>' . DOKU_LF;
538f6dad9fdSMichael Klier            $pre .= '  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . DOKU_LF;
539f6dad9fdSMichael Klier            $pre .= '  <title>'.$ID.'</title>' . DOKU_LF;
540f6dad9fdSMichael Klier
541f6dad9fdSMichael Klier            // get metaheaders
542f6dad9fdSMichael Klier            ob_start();
543f6dad9fdSMichael Klier            tpl_metaheaders();
544f6dad9fdSMichael Klier            $pre .= ob_get_clean();
545f6dad9fdSMichael Klier
546f6dad9fdSMichael Klier            $pre .= '</head>' . DOKU_LF;
547f6dad9fdSMichael Klier            $pre .= '<body>' . DOKU_LF;
548f6dad9fdSMichael Klier            $pre .= '<div class="dokuwiki export">' . DOKU_LF;
549f6dad9fdSMichael Klier
550f6dad9fdSMichael Klier            // get toc
551f6dad9fdSMichael Klier            $pre .= tpl_toc(true);
552f6dad9fdSMichael Klier
553f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
554f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
555f6dad9fdSMichael Klier
556f6dad9fdSMichael Klier            $post .= '</div>' . DOKU_LF;
557f6dad9fdSMichael Klier            $post .= '</body>' . DOKU_LF;
558f6dad9fdSMichael Klier            $post .= '</html>' . DOKU_LF;
559f6dad9fdSMichael Klier            break;
560f6dad9fdSMichael Klier        case 'xhtmlbody':
561f6dad9fdSMichael Klier            $headers['Content-Type'] = 'text/html; charset=utf-8';
562f6dad9fdSMichael Klier            $output = p_wiki_xhtml($ID,$REV,false);
563f6dad9fdSMichael Klier            break;
564f6dad9fdSMichael Klier        default:
565f6dad9fdSMichael Klier            $output = p_cached_output(wikiFN($ID,$REV), $mode);
5669acedd40SAndreas Gohr            $headers = p_get_metadata($ID,"format $mode");
567f6dad9fdSMichael Klier            break;
568f6dad9fdSMichael Klier    }
569f6dad9fdSMichael Klier
570f6dad9fdSMichael Klier    // prepare event data
571f6dad9fdSMichael Klier    $data = array();
572f6dad9fdSMichael Klier    $data['id'] = $ID;
573f6dad9fdSMichael Klier    $data['mode'] = $mode;
574f6dad9fdSMichael Klier    $data['headers'] = $headers;
575f6dad9fdSMichael Klier    $data['output'] =& $output;
576f6dad9fdSMichael Klier
577f6dad9fdSMichael Klier    trigger_event('ACTION_EXPORT_POSTPROCESS', $data);
578f6dad9fdSMichael Klier
579f6dad9fdSMichael Klier    if(!empty($data['output'])){
580f6dad9fdSMichael Klier        if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){
58185767031SAndreas Gohr            header("$key: $val");
58285767031SAndreas Gohr        }
583f6dad9fdSMichael Klier        print $pre.$data['output'].$post;
5846b13307fSandi        exit;
5856b13307fSandi    }
5866b13307fSandi    return 'show';
5876b13307fSandi}
588340756e4Sandi
589b158d625SSteven Danz/**
5905b75cd1fSAdrian Lang * Handle page 'subscribe'
591b158d625SSteven Danz *
5925b75cd1fSAdrian Lang * Throws exception on error.
5935b75cd1fSAdrian Lang *
5945b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
595b158d625SSteven Danz */
5961380fc45SAndreas Gohrfunction act_subscription($act){
597056c2049SAndreas Gohr    global $lang;
598056c2049SAndreas Gohr    global $INFO;
599056c2049SAndreas Gohr    global $ID;
60052b0dd67SGuy Brand
6019fa341d0SAndreas Gohr    // subcriptions work for logged in users only
6029fa341d0SAndreas Gohr    if(!$_SERVER['REMOTE_USER']) return 'show';
6039fa341d0SAndreas Gohr
604056c2049SAndreas Gohr    // get and preprocess data.
6058881fcc9SAdrian Lang    $params = array();
6068881fcc9SAdrian Lang    foreach(array('target', 'style', 'action') as $param) {
607056c2049SAndreas Gohr        if (isset($_REQUEST["sub_$param"])) {
608056c2049SAndreas Gohr            $params[$param] = $_REQUEST["sub_$param"];
6098881fcc9SAdrian Lang        }
6108881fcc9SAdrian Lang    }
6118881fcc9SAdrian Lang
612056c2049SAndreas Gohr    // any action given? if not just return and show the subscription page
61366d2bed9SAdrian Lang    if(!$params['action'] || !checkSecurityToken()) return $act;
614056c2049SAndreas Gohr
6158881fcc9SAdrian Lang    // Handle POST data, may throw exception.
6168881fcc9SAdrian Lang    trigger_event('ACTION_HANDLE_SUBSCRIBE', $params, 'subscription_handle_post');
6178881fcc9SAdrian Lang
6188881fcc9SAdrian Lang    $target = $params['target'];
6198881fcc9SAdrian Lang    $style  = $params['style'];
6208881fcc9SAdrian Lang    $data   = $params['data'];
6218881fcc9SAdrian Lang    $action = $params['action'];
6228881fcc9SAdrian Lang
6238881fcc9SAdrian Lang    // Perform action.
6248881fcc9SAdrian Lang    if (!subscription_set($_SERVER['REMOTE_USER'], $target, $style, $data)) {
6258881fcc9SAdrian Lang        throw new Exception(sprintf($lang["subscr_{$action}_error"],
6268881fcc9SAdrian Lang                                    hsc($INFO['userinfo']['name']),
6278881fcc9SAdrian Lang                                    prettyprint_id($target)));
6288881fcc9SAdrian Lang    }
6298881fcc9SAdrian Lang    msg(sprintf($lang["subscr_{$action}_success"], hsc($INFO['userinfo']['name']),
6308881fcc9SAdrian Lang                prettyprint_id($target)), 1);
631cb3f9dbaSAdrian Lang    act_redirect($ID, $act);
632cb3f9dbaSAdrian Lang
633cb3f9dbaSAdrian Lang    // Assure that we have valid data if act_redirect somehow fails.
634cb3f9dbaSAdrian Lang    $INFO['subscribed'] = get_info_subscribed();
635cb3f9dbaSAdrian Lang    return 'show';
6368881fcc9SAdrian Lang}
6378881fcc9SAdrian Lang
6388881fcc9SAdrian Lang/**
6398881fcc9SAdrian Lang * Validate POST data
6408881fcc9SAdrian Lang *
6418881fcc9SAdrian Lang * Validates POST data for a subscribe or unsubscribe request. This is the
6428881fcc9SAdrian Lang * default action for the event ACTION_HANDLE_SUBSCRIBE.
6438881fcc9SAdrian Lang *
6448881fcc9SAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
6458881fcc9SAdrian Lang */
6467a9add1cSAdrian Langfunction subscription_handle_post(&$params) {
6478881fcc9SAdrian Lang    global $INFO;
6488881fcc9SAdrian Lang    global $lang;
6498881fcc9SAdrian Lang
6505b75cd1fSAdrian Lang    // Get and validate parameters.
6518881fcc9SAdrian Lang    if (!isset($params['target'])) {
65215741132SAndreas Gohr        throw new Exception('no subscription target given');
6535b75cd1fSAdrian Lang    }
6548881fcc9SAdrian Lang    $target = $params['target'];
6555b75cd1fSAdrian Lang    $valid_styles = array('every', 'digest');
6565b75cd1fSAdrian Lang    if (substr($target, -1, 1) === ':') {
6575b75cd1fSAdrian Lang        // Allow “list” subscribe style since the target is a namespace.
6585b75cd1fSAdrian Lang        $valid_styles[] = 'list';
6595b75cd1fSAdrian Lang    }
6608881fcc9SAdrian Lang    $style  = valid_input_set('style', $valid_styles, $params,
66115741132SAndreas Gohr                              'invalid subscription style given');
6628881fcc9SAdrian Lang    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
66315741132SAndreas Gohr                              $params, 'invalid subscription action given');
664613964ecSGuy Brand
6655b75cd1fSAdrian Lang    // Check other conditions.
6665b75cd1fSAdrian Lang    if ($action === 'subscribe') {
6675b75cd1fSAdrian Lang        if ($INFO['userinfo']['mail'] === '') {
6685b75cd1fSAdrian Lang            throw new Exception($lang['subscr_subscribe_noaddress']);
66952b0dd67SGuy Brand        }
6705b75cd1fSAdrian Lang    } elseif ($action === 'unsubscribe') {
6715b75cd1fSAdrian Lang        $is = false;
6725b75cd1fSAdrian Lang        foreach($INFO['subscribed'] as $subscr) {
6735b75cd1fSAdrian Lang            if ($subscr['target'] === $target) {
6745b75cd1fSAdrian Lang                $is = true;
67552b0dd67SGuy Brand            }
67652b0dd67SGuy Brand        }
6775b75cd1fSAdrian Lang        if ($is === false) {
67815741132SAndreas Gohr            throw new Exception(sprintf($lang['subscr_not_subscribed'],
67915741132SAndreas Gohr                                        $_SERVER['REMOTE_USER'],
6805b75cd1fSAdrian Lang                                        prettyprint_id($target)));
6815b75cd1fSAdrian Lang        }
6825b75cd1fSAdrian Lang        // subscription_set deletes a subscription if style = null.
6835b75cd1fSAdrian Lang        $style = null;
68452b0dd67SGuy Brand    }
68552b0dd67SGuy Brand
6868881fcc9SAdrian Lang    $data = in_array($style, array('list', 'digest')) ? time() : null;
6878881fcc9SAdrian Lang    $params = compact('target', 'style', 'data', 'action');
68852b0dd67SGuy Brand}
68952b0dd67SGuy Brand
690340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
691