xref: /dokuwiki/inc/actions.php (revision ee4c4a1b5a5840c1b9d2d8c74b3f4298dd52928b)
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
96b13307fSandi  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
106b13307fSandi  require_once(DOKU_INC.'inc/template.php');
116b13307fSandi
12af182434Sandi
136b13307fSandi/**
146b13307fSandi * Call the needed action handlers
156b13307fSandi *
166b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
176b13307fSandi */
186b13307fSandifunction act_dispatch(){
196b13307fSandi  global $INFO;
206b13307fSandi  global $ACT;
216b13307fSandi  global $ID;
226b13307fSandi  global $QUERY;
236b13307fSandi  global $lang;
246b13307fSandi  global $conf;
256b13307fSandi
26af182434Sandi  //sanitize $ACT
27af182434Sandi  $ACT = act_clean($ACT);
28af182434Sandi
29b8957367SBenjamin Gilbert  //check if searchword was given - else just show
300868021bSAndreas Gohr  $s = cleanID($QUERY);
310868021bSAndreas Gohr  if($ACT == 'search' && empty($s)){
32b8957367SBenjamin Gilbert    $ACT = 'show';
33b8957367SBenjamin Gilbert  }
34b8957367SBenjamin Gilbert
35b8957367SBenjamin Gilbert  //login stuff
36b8957367SBenjamin Gilbert  if(in_array($ACT,array('login','logout')))
37b8957367SBenjamin Gilbert    $ACT = act_auth($ACT);
38b8957367SBenjamin Gilbert
391380fc45SAndreas Gohr  //check if user is asking to (un)subscribe a page
401380fc45SAndreas Gohr  if($ACT == 'subscribe' || $ACT == 'unsubscribe')
411380fc45SAndreas Gohr    $ACT = act_subscription($ACT);
42b158d625SSteven Danz
436b13307fSandi  //check permissions
446b13307fSandi  $ACT = act_permcheck($ACT);
456b13307fSandi
46b8957367SBenjamin Gilbert  //register
47b8957367SBenjamin Gilbert  if($ACT == 'register' && register()){
48b8957367SBenjamin Gilbert    $ACT = 'login';
49b8957367SBenjamin Gilbert  }
506b13307fSandi
518b06d178Schris  if ($ACT == 'resendpwd' && act_resendpwd()) {
528b06d178Schris    $ACT = 'login';
538b06d178Schris  }
548b06d178Schris
558b06d178Schris  //update user profile
568b06d178Schris  if (($ACT == 'profile') && updateprofile()) {
574cb79657SMatthias Grimm    msg($lang['profchanged'],1);
584cb79657SMatthias Grimm    $ACT = 'show';
598b06d178Schris  }
608b06d178Schris
616b13307fSandi  //save
626b13307fSandi  if($ACT == 'save')
636b13307fSandi    $ACT = act_save($ACT);
646b13307fSandi
65*ee4c4a1bSAndreas Gohr  //draft deletion
66*ee4c4a1bSAndreas Gohr  if($ACT == 'draftdel')
67*ee4c4a1bSAndreas Gohr    $ACT = act_draftdel($ACT);
68*ee4c4a1bSAndreas Gohr
69*ee4c4a1bSAndreas Gohr  //draft saving on preview
70*ee4c4a1bSAndreas Gohr  if($ACT == 'preview')
71*ee4c4a1bSAndreas Gohr    $ACT = act_draftsave($ACT);
72*ee4c4a1bSAndreas Gohr
736b13307fSandi  //edit
74b146b32bSandi  if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){
75af182434Sandi    $ACT = act_edit($ACT);
766b13307fSandi  }else{
776b13307fSandi    unlock($ID); //try to unlock
786b13307fSandi  }
796b13307fSandi
806b13307fSandi  //handle export
81ac83b9d8Sandi  if(substr($ACT,0,7) == 'export_')
826b13307fSandi    $ACT = act_export($ACT);
836b13307fSandi
846b13307fSandi  //display some infos
856b13307fSandi  if($ACT == 'check'){
866b13307fSandi    check();
876b13307fSandi    $ACT = 'show';
886b13307fSandi  }
896b13307fSandi
90c19fe9c0Sandi  //handle admin tasks
91c19fe9c0Sandi  if($ACT == 'admin'){
9211e2ce22Schris    // retrieve admin plugin name from $_REQUEST['page']
9311e2ce22Schris    if ($_REQUEST['page']) {
9411e2ce22Schris        $pluginlist = plugin_list('admin');
9511e2ce22Schris        if (in_array($_REQUEST['page'], $pluginlist)) {
9611e2ce22Schris          // attempt to load the plugin
9711e2ce22Schris          if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== NULL)
9811e2ce22Schris              $plugin->handle();
9911e2ce22Schris        }
10011e2ce22Schris    }
10111e2ce22Schris/*
102c19fe9c0Sandi        if($_REQUEST['page'] == 'acl'){
103c19fe9c0Sandi            require_once(DOKU_INC.'inc/admin_acl.php');
104c19fe9c0Sandi            admin_acl_handler();
105c19fe9c0Sandi    }
10611e2ce22Schris*/
107c19fe9c0Sandi  }
108c19fe9c0Sandi
1096b13307fSandi  //call template FIXME: all needed vars available?
1106b13307fSandi  header('Content-Type: text/html; charset=utf-8');
1115a892029SAndreas Gohr  include(template('main.php'));
112c19fe9c0Sandi  // output for the commands is now handled in inc/templates.php
113c19fe9c0Sandi  // in function tpl_content()
1146b13307fSandi}
1156b13307fSandi
1166b13307fSandi/**
117af182434Sandi * Sanitize the action command
118af182434Sandi *
119af182434Sandi * Add all allowed commands here.
120af182434Sandi *
121af182434Sandi * @author Andreas Gohr <andi@splitbrain.org>
122af182434Sandi */
123af182434Sandifunction act_clean($act){
124af182434Sandi  global $lang;
12560e6b550SAndreas Gohr  global $conf;
126af182434Sandi
127*ee4c4a1bSAndreas Gohr  // check if the action was given as array key
128*ee4c4a1bSAndreas Gohr  if(is_array($act)){
129*ee4c4a1bSAndreas Gohr    list($act) = array_keys($act);
130*ee4c4a1bSAndreas Gohr  }
131*ee4c4a1bSAndreas Gohr
132cf81b04aSandi  //handle localized buttons
133cf81b04aSandi  if($act == $lang['btn_save'])     $act = 'save';
134cf81b04aSandi  if($act == $lang['btn_preview'])  $act = 'preview';
135cf81b04aSandi  if($act == $lang['btn_cancel'])   $act = 'show';
136*ee4c4a1bSAndreas Gohr  if($act == $lang['btn_recover'])  $act = 'recover';
137*ee4c4a1bSAndreas Gohr  if($act == $lang['btn_draftdel']) $act = 'draftdel';
138*ee4c4a1bSAndreas Gohr
139cf81b04aSandi
140ac83b9d8Sandi  //remove all bad chars
141ac83b9d8Sandi  $act = strtolower($act);
142ac83b9d8Sandi  $act = preg_replace('/[^a-z_]+/','',$act);
143ac83b9d8Sandi
144ac83b9d8Sandi  if($act == 'export_html') $act = 'export_xhtml';
145cc2ae802SAndreas Gohr  if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
146b146b32bSandi
14760e6b550SAndreas Gohr  //disable all acl related commands if ACL is disabled
14860e6b550SAndreas Gohr  if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin',
14960e6b550SAndreas Gohr                                             'subscribe','unsubscribe','profile',
15060e6b550SAndreas Gohr                                             'resendpwd',))){
15160e6b550SAndreas Gohr    msg('Command unavailable: '.htmlspecialchars($act),-1);
15260e6b550SAndreas Gohr    return 'show';
15360e6b550SAndreas Gohr  }
15460e6b550SAndreas Gohr
155*ee4c4a1bSAndreas Gohr  if(!in_array($act,array('login','logout','register','save','edit','draft',
156ac83b9d8Sandi                          'preview','search','show','check','index','revisions',
1571380fc45SAndreas Gohr                          'diff','recent','backlink','admin','subscribe',
158*ee4c4a1bSAndreas Gohr                          'unsubscribe','profile','resendpwd','recover',
159*ee4c4a1bSAndreas Gohr                          'draftdel',)) && substr($act,0,7) != 'export_' ) {
160*ee4c4a1bSAndreas Gohr    msg('Command unknown: '.htmlspecialchars($act),-1);
161af182434Sandi    return 'show';
162af182434Sandi  }
163af182434Sandi  return $act;
164af182434Sandi}
165af182434Sandi
166af182434Sandi/**
1676b13307fSandi * Run permissionchecks
1686b13307fSandi *
1696b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
1706b13307fSandi */
1716b13307fSandifunction act_permcheck($act){
172dbbc6aa7Sandi  global $INFO;
1735e199953Smatthiasgrimm  global $conf;
174dbbc6aa7Sandi
175*ee4c4a1bSAndreas Gohr  if(in_array($act,array('save','preview','edit','recover'))){
1766b13307fSandi    if($INFO['exists']){
177bdbc16bfSandi      if($act == 'edit'){
178bdbc16bfSandi        //the edit function will check again and do a source show
179bdbc16bfSandi        //when no AUTH_EDIT available
180bdbc16bfSandi        $permneed = AUTH_READ;
181bdbc16bfSandi      }else{
1826b13307fSandi        $permneed = AUTH_EDIT;
183bdbc16bfSandi      }
1846b13307fSandi    }else{
1856b13307fSandi      $permneed = AUTH_CREATE;
1866b13307fSandi    }
1878b06d178Schris  }elseif(in_array($act,array('login','search','recent','profile'))){
1886b13307fSandi    $permneed = AUTH_NONE;
1895e199953Smatthiasgrimm  }elseif($act == 'register'){
190e1fcbe1eSandi    if ($conf['openregister']){
1915e199953Smatthiasgrimm      $permneed = AUTH_NONE;
192e1fcbe1eSandi    }else{
193e1fcbe1eSandi      $permneed = AUTH_ADMIN;
194e1fcbe1eSandi    }
195c19fe9c0Sandi  }elseif($act == 'admin'){
196c19fe9c0Sandi    $permneed = AUTH_ADMIN;
1976b13307fSandi  }else{
1986b13307fSandi    $permneed = AUTH_READ;
1996b13307fSandi  }
200dbbc6aa7Sandi  if($INFO['perm'] >= $permneed) return $act;
201dbbc6aa7Sandi
2026b13307fSandi  return 'denied';
2036b13307fSandi}
2046b13307fSandi
2056b13307fSandi/**
206*ee4c4a1bSAndreas Gohr * Handle 'draftdel'
207*ee4c4a1bSAndreas Gohr *
208*ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user
209*ee4c4a1bSAndreas Gohr */
210*ee4c4a1bSAndreas Gohrfunction act_draftdel($act){
211*ee4c4a1bSAndreas Gohr  global $INFO;
212*ee4c4a1bSAndreas Gohr  @unlink($INFO['draft']);
213*ee4c4a1bSAndreas Gohr  $INFO['draft'] = null;
214*ee4c4a1bSAndreas Gohr  return 'show';
215*ee4c4a1bSAndreas Gohr}
216*ee4c4a1bSAndreas Gohr
217*ee4c4a1bSAndreas Gohr/**
218*ee4c4a1bSAndreas Gohr * Saves a draft on preview
219*ee4c4a1bSAndreas Gohr *
220*ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/
221*ee4c4a1bSAndreas Gohr */
222*ee4c4a1bSAndreas Gohrfunction act_draftsave($act){
223*ee4c4a1bSAndreas Gohr  global $INFO;
224*ee4c4a1bSAndreas Gohr  global $ID;
225*ee4c4a1bSAndreas Gohr  global $conf;
226*ee4c4a1bSAndreas Gohr  if($conf['usedraft'] && $_POST['wikitext']){
227*ee4c4a1bSAndreas Gohr    $draft = array('id'     => $ID,
228*ee4c4a1bSAndreas Gohr                   'prefix' => $_POST['prefix'],
229*ee4c4a1bSAndreas Gohr                   'text'   => $_POST['wikitext'],
230*ee4c4a1bSAndreas Gohr                   'suffix' => $_POST['suffix'],
231*ee4c4a1bSAndreas Gohr                   'date'   => $_POST['date'],
232*ee4c4a1bSAndreas Gohr                   'client' => $INFO['client'],
233*ee4c4a1bSAndreas Gohr                  );
234*ee4c4a1bSAndreas Gohr    $cname = getCacheName($draft['client'].$ID,'.draft');
235*ee4c4a1bSAndreas Gohr    if(io_saveFile($cname,serialize($draft))){
236*ee4c4a1bSAndreas Gohr      $INFO['draft'] = $cname;
237*ee4c4a1bSAndreas Gohr    }
238*ee4c4a1bSAndreas Gohr  }
239*ee4c4a1bSAndreas Gohr  return $act;
240*ee4c4a1bSAndreas Gohr}
241*ee4c4a1bSAndreas Gohr
242*ee4c4a1bSAndreas Gohr/**
2436b13307fSandi * Handle 'save'
2446b13307fSandi *
2456b13307fSandi * Checks for spam and conflicts and saves the page.
2466b13307fSandi * Does a redirect to show the page afterwards or
2476b13307fSandi * returns a new action.
2486b13307fSandi *
2496b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2506b13307fSandi */
2516b13307fSandifunction act_save($act){
2526b13307fSandi  global $ID;
2536b13307fSandi  global $DATE;
2546b13307fSandi  global $PRE;
2556b13307fSandi  global $TEXT;
2566b13307fSandi  global $SUF;
2576b13307fSandi  global $SUM;
2586b13307fSandi
2596b13307fSandi  //spam check
2606b13307fSandi  if(checkwordblock())
2616b13307fSandi    return 'wordblock';
2626b13307fSandi  //conflict check //FIXME use INFO
2636b13307fSandi  if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE )
2646b13307fSandi    return 'conflict';
2656b13307fSandi
2666b13307fSandi  //save it
267b6912aeaSAndreas Gohr  saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con
2686b13307fSandi  //unlock it
2696b13307fSandi  unlock($ID);
2706b13307fSandi
271*ee4c4a1bSAndreas Gohr  //delete draft
272*ee4c4a1bSAndreas Gohr  act_draftdel($act);
273*ee4c4a1bSAndreas Gohr
2746b13307fSandi  //show it
2756b13307fSandi  session_write_close();
2766b13307fSandi  header("Location: ".wl($ID,'',true));
2776b13307fSandi  exit();
2786b13307fSandi}
2796b13307fSandi
2806b13307fSandi/**
281b8957367SBenjamin Gilbert * Handle 'login', 'logout'
2826b13307fSandi *
2836b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
2846b13307fSandi */
2856b13307fSandifunction act_auth($act){
28608eda5bcSmatthiasgrimm  global $ID;
2877cace34dSAndreas Gohr  global $INFO;
28808eda5bcSmatthiasgrimm
2896b13307fSandi  //already logged in?
2906b13307fSandi  if($_SERVER['REMOTE_USER'] && $act=='login')
2916b13307fSandi    return 'show';
2926b13307fSandi
2936b13307fSandi  //handle logout
2946b13307fSandi  if($act=='logout'){
29508eda5bcSmatthiasgrimm    $lockedby = checklock($ID); //page still locked?
296424c3c4fSJohannes Buchner    if($lockedby == $_SERVER['REMOTE_USER'])
29708eda5bcSmatthiasgrimm      unlock($ID); //try to unlock
29808eda5bcSmatthiasgrimm
2997cace34dSAndreas Gohr    // do the logout stuff
3006b13307fSandi    auth_logoff();
3017cace34dSAndreas Gohr
3027cace34dSAndreas Gohr    // rebuild info array
3037cace34dSAndreas Gohr    $INFO = pageinfo();
3047cace34dSAndreas Gohr
3056b13307fSandi    return 'login';
3066b13307fSandi  }
3076b13307fSandi
3086b13307fSandi  return $act;
3096b13307fSandi}
3106b13307fSandi
3116b13307fSandi/**
3126b13307fSandi * Handle 'edit', 'preview'
3136b13307fSandi *
3146b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3156b13307fSandi */
3166b13307fSandifunction act_edit($act){
317cd409024Sjorda  global $ID;
318*ee4c4a1bSAndreas Gohr  global $INFO;
319cd409024Sjorda
3206b13307fSandi  //check if locked by anyone - if not lock for my self
3216b13307fSandi  $lockedby = checklock($ID);
3226b13307fSandi  if($lockedby) return 'locked';
3236b13307fSandi
3246b13307fSandi  lock($ID);
3256b13307fSandi  return $act;
3266b13307fSandi}
3276b13307fSandi
3286b13307fSandi/**
3296b13307fSandi * Handle 'edit', 'preview'
3306b13307fSandi *
3316b13307fSandi * @author Andreas Gohr <andi@splitbrain.org>
3326b13307fSandi */
3336b13307fSandifunction act_export($act){
3346b13307fSandi  global $ID;
3356b13307fSandi  global $REV;
3366b13307fSandi
337ac83b9d8Sandi  // no renderer for this
338ac83b9d8Sandi  if($act == 'export_raw'){
339ac83b9d8Sandi    header('Content-Type: text/plain; charset=utf-8');
340ac83b9d8Sandi    print rawWiki($ID,$REV);
341ac83b9d8Sandi    exit;
342ac83b9d8Sandi  }
343ac83b9d8Sandi
344ac83b9d8Sandi  // html export #FIXME what about the template's style?
345ac83b9d8Sandi  if($act == 'export_xhtml'){
34685f8705cSAnika Henke    global $conf;
34785f8705cSAnika Henke    global $lang;
3486b13307fSandi    header('Content-Type: text/html; charset=utf-8');
34985f8705cSAnika Henke    ptln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"');
35085f8705cSAnika Henke    ptln(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
35185f8705cSAnika Henke    ptln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"');
35285f8705cSAnika Henke    ptln(' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">');
3536b13307fSandi    ptln('<head>');
35485f8705cSAnika Henke    ptln('  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
35585f8705cSAnika Henke    ptln('  <title>'.$ID.'</title>');
3566b13307fSandi    tpl_metaheaders();
3576b13307fSandi    ptln('</head>');
3586b13307fSandi    ptln('<body>');
3592c5c3308SAndreas Gohr    ptln('<div class="dokuwiki export">');
360ac83b9d8Sandi    print p_wiki_xhtml($ID,$REV,false);
361c771e9edSAnika Henke    ptln('</div>');
3626b13307fSandi    ptln('</body>');
3636b13307fSandi    ptln('</html>');
3646b13307fSandi    exit;
3656b13307fSandi  }
3666b13307fSandi
367cc2ae802SAndreas Gohr  // html body only
368cc2ae802SAndreas Gohr  if($act == 'export_xhtmlbody'){
369cc2ae802SAndreas Gohr    print p_wiki_xhtml($ID,$REV,false);
370cc2ae802SAndreas Gohr    exit;
371cc2ae802SAndreas Gohr  }
372cc2ae802SAndreas Gohr
373ac83b9d8Sandi  // try to run renderer #FIXME use cached instructions
374ac83b9d8Sandi  $mode = substr($act,7);
3759dc2c2afSandi  $text = p_render($mode,p_get_instructions(rawWiki($ID,$REV)),$info);
376ac83b9d8Sandi  if(!is_null($text)){
377ac83b9d8Sandi    print $text;
3786b13307fSandi    exit;
3796b13307fSandi  }
3806b13307fSandi
381ac83b9d8Sandi
382ac83b9d8Sandi
3836b13307fSandi  return 'show';
3846b13307fSandi}
385340756e4Sandi
386b158d625SSteven Danz/**
3871380fc45SAndreas Gohr * Handle 'subscribe', 'unsubscribe'
388b158d625SSteven Danz *
389b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
3901380fc45SAndreas Gohr * @todo   localize
391b158d625SSteven Danz */
3921380fc45SAndreas Gohrfunction act_subscription($act){
393b158d625SSteven Danz  global $ID;
394b158d625SSteven Danz  global $INFO;
395f9eb5648Ssteven-danz  global $lang;
396b158d625SSteven Danz
3971380fc45SAndreas Gohr  $file=metaFN($ID,'.mlist');
3981380fc45SAndreas Gohr  if ($act=='subscribe' && !$INFO['subscribed']){
399b158d625SSteven Danz    if ($INFO['userinfo']['mail']){
4001380fc45SAndreas Gohr      if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) {
4011380fc45SAndreas Gohr        $INFO['subscribed'] = true;
402f9eb5648Ssteven-danz        msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1);
403b158d625SSteven Danz      } else {
404f9eb5648Ssteven-danz        msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1);
405b158d625SSteven Danz      }
406b158d625SSteven Danz    } else {
407f9eb5648Ssteven-danz      msg($lang['subscribe_noaddress']);
408b158d625SSteven Danz    }
4091380fc45SAndreas Gohr  } elseif ($act=='unsubscribe' && $INFO['subscribed']){
410b158d625SSteven Danz    if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) {
4111380fc45SAndreas Gohr      $INFO['subscribed'] = false;
412f9eb5648Ssteven-danz      msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1);
413b158d625SSteven Danz    } else {
414f9eb5648Ssteven-danz      msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1);
415b158d625SSteven Danz    }
416b158d625SSteven Danz  }
417b158d625SSteven Danz
418b158d625SSteven Danz  return 'show';
419b158d625SSteven Danz}
420b158d625SSteven Danz
421340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
422