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 26*c2e830f2Schris // give plugins an opportunity to process the action 27*c2e830f2Schris $evt = new event('ACTION_DISPATCH',$ACT); 28*c2e830f2Schris $evt->trigger(); 29*c2e830f2Schris if ($evt->_default) { 30*c2e830f2Schris 31af182434Sandi //sanitize $ACT 32af182434Sandi $ACT = act_clean($ACT); 33af182434Sandi 34b8957367SBenjamin Gilbert //check if searchword was given - else just show 350868021bSAndreas Gohr $s = cleanID($QUERY); 360868021bSAndreas Gohr if($ACT == 'search' && empty($s)){ 37b8957367SBenjamin Gilbert $ACT = 'show'; 38b8957367SBenjamin Gilbert } 39b8957367SBenjamin Gilbert 40b8957367SBenjamin Gilbert //login stuff 41b8957367SBenjamin Gilbert if(in_array($ACT,array('login','logout'))) 42b8957367SBenjamin Gilbert $ACT = act_auth($ACT); 43b8957367SBenjamin Gilbert 441380fc45SAndreas Gohr //check if user is asking to (un)subscribe a page 451380fc45SAndreas Gohr if($ACT == 'subscribe' || $ACT == 'unsubscribe') 461380fc45SAndreas Gohr $ACT = act_subscription($ACT); 47b158d625SSteven Danz 486b13307fSandi //check permissions 496b13307fSandi $ACT = act_permcheck($ACT); 506b13307fSandi 51b8957367SBenjamin Gilbert //register 52b8957367SBenjamin Gilbert if($ACT == 'register' && register()){ 53b8957367SBenjamin Gilbert $ACT = 'login'; 54b8957367SBenjamin Gilbert } 556b13307fSandi 568b06d178Schris if ($ACT == 'resendpwd' && act_resendpwd()) { 578b06d178Schris $ACT = 'login'; 588b06d178Schris } 598b06d178Schris 608b06d178Schris //update user profile 618b06d178Schris if (($ACT == 'profile') && updateprofile()) { 624cb79657SMatthias Grimm msg($lang['profchanged'],1); 634cb79657SMatthias Grimm $ACT = 'show'; 648b06d178Schris } 658b06d178Schris 666b13307fSandi //save 676b13307fSandi if($ACT == 'save') 686b13307fSandi $ACT = act_save($ACT); 696b13307fSandi 70ee4c4a1bSAndreas Gohr //draft deletion 71ee4c4a1bSAndreas Gohr if($ACT == 'draftdel') 72ee4c4a1bSAndreas Gohr $ACT = act_draftdel($ACT); 73ee4c4a1bSAndreas Gohr 74ee4c4a1bSAndreas Gohr //draft saving on preview 75ee4c4a1bSAndreas Gohr if($ACT == 'preview') 76ee4c4a1bSAndreas Gohr $ACT = act_draftsave($ACT); 77ee4c4a1bSAndreas Gohr 786b13307fSandi //edit 79b146b32bSandi if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){ 80af182434Sandi $ACT = act_edit($ACT); 816b13307fSandi }else{ 826b13307fSandi unlock($ID); //try to unlock 836b13307fSandi } 846b13307fSandi 856b13307fSandi //handle export 86ac83b9d8Sandi if(substr($ACT,0,7) == 'export_') 876b13307fSandi $ACT = act_export($ACT); 886b13307fSandi 896b13307fSandi //display some infos 906b13307fSandi if($ACT == 'check'){ 916b13307fSandi check(); 926b13307fSandi $ACT = 'show'; 936b13307fSandi } 946b13307fSandi 95c19fe9c0Sandi //handle admin tasks 96c19fe9c0Sandi if($ACT == 'admin'){ 9711e2ce22Schris // retrieve admin plugin name from $_REQUEST['page'] 9811e2ce22Schris if ($_REQUEST['page']) { 9911e2ce22Schris $pluginlist = plugin_list('admin'); 10011e2ce22Schris if (in_array($_REQUEST['page'], $pluginlist)) { 10111e2ce22Schris // attempt to load the plugin 10211e2ce22Schris if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== NULL) 10311e2ce22Schris $plugin->handle(); 10411e2ce22Schris } 10511e2ce22Schris } 106c19fe9c0Sandi } 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 127ee4c4a1bSAndreas Gohr // check if the action was given as array key 128ee4c4a1bSAndreas Gohr if(is_array($act)){ 129ee4c4a1bSAndreas Gohr list($act) = array_keys($act); 130ee4c4a1bSAndreas Gohr } 131ee4c4a1bSAndreas 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'; 136ee4c4a1bSAndreas Gohr if($act == $lang['btn_recover']) $act = 'recover'; 137ee4c4a1bSAndreas Gohr if($act == $lang['btn_draftdel']) $act = 'draftdel'; 138ee4c4a1bSAndreas 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 155ee4c4a1bSAndreas 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', 158ee4c4a1bSAndreas Gohr 'unsubscribe','profile','resendpwd','recover', 159ee4c4a1bSAndreas Gohr 'draftdel',)) && substr($act,0,7) != 'export_' ) { 160ee4c4a1bSAndreas 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 175ee4c4a1bSAndreas 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/** 206ee4c4a1bSAndreas Gohr * Handle 'draftdel' 207ee4c4a1bSAndreas Gohr * 208ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user 209ee4c4a1bSAndreas Gohr */ 210ee4c4a1bSAndreas Gohrfunction act_draftdel($act){ 211ee4c4a1bSAndreas Gohr global $INFO; 212ee4c4a1bSAndreas Gohr @unlink($INFO['draft']); 213ee4c4a1bSAndreas Gohr $INFO['draft'] = null; 214ee4c4a1bSAndreas Gohr return 'show'; 215ee4c4a1bSAndreas Gohr} 216ee4c4a1bSAndreas Gohr 217ee4c4a1bSAndreas Gohr/** 218ee4c4a1bSAndreas Gohr * Saves a draft on preview 219ee4c4a1bSAndreas Gohr * 220ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/ 221ee4c4a1bSAndreas Gohr */ 222ee4c4a1bSAndreas Gohrfunction act_draftsave($act){ 223ee4c4a1bSAndreas Gohr global $INFO; 224ee4c4a1bSAndreas Gohr global $ID; 225ee4c4a1bSAndreas Gohr global $conf; 226ee4c4a1bSAndreas Gohr if($conf['usedraft'] && $_POST['wikitext']){ 227ee4c4a1bSAndreas Gohr $draft = array('id' => $ID, 228ee4c4a1bSAndreas Gohr 'prefix' => $_POST['prefix'], 229ee4c4a1bSAndreas Gohr 'text' => $_POST['wikitext'], 230ee4c4a1bSAndreas Gohr 'suffix' => $_POST['suffix'], 231ee4c4a1bSAndreas Gohr 'date' => $_POST['date'], 232ee4c4a1bSAndreas Gohr 'client' => $INFO['client'], 233ee4c4a1bSAndreas Gohr ); 234ee4c4a1bSAndreas Gohr $cname = getCacheName($draft['client'].$ID,'.draft'); 235ee4c4a1bSAndreas Gohr if(io_saveFile($cname,serialize($draft))){ 236ee4c4a1bSAndreas Gohr $INFO['draft'] = $cname; 237ee4c4a1bSAndreas Gohr } 238ee4c4a1bSAndreas Gohr } 239ee4c4a1bSAndreas Gohr return $act; 240ee4c4a1bSAndreas Gohr} 241ee4c4a1bSAndreas Gohr 242ee4c4a1bSAndreas 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 271ee4c4a1bSAndreas Gohr //delete draft 272ee4c4a1bSAndreas Gohr act_draftdel($act); 273ee4c4a1bSAndreas 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; 318ee4c4a1bSAndreas 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 :