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> 17c9570649SAndreas Gohr * @triggers ACTION_ACT_PREPROCESS 18c9570649SAndreas Gohr * @triggers ACTION_REGISTER 19c9570649SAndreas Gohr * @triggers ACTION_HEADERS_SEND 206b13307fSandi */ 216b13307fSandifunction act_dispatch(){ 226b13307fSandi global $INFO; 236b13307fSandi global $ACT; 246b13307fSandi global $ID; 256b13307fSandi global $QUERY; 266b13307fSandi global $lang; 276b13307fSandi global $conf; 286b13307fSandi 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 43b8957367SBenjamin Gilbert if(in_array($ACT,array('login','logout'))) 44b8957367SBenjamin Gilbert $ACT = act_auth($ACT); 45b8957367SBenjamin Gilbert 461380fc45SAndreas Gohr //check if user is asking to (un)subscribe a page 471380fc45SAndreas Gohr if($ACT == 'subscribe' || $ACT == 'unsubscribe') 481380fc45SAndreas Gohr $ACT = act_subscription($ACT); 49b158d625SSteven Danz 506b13307fSandi //check permissions 516b13307fSandi $ACT = act_permcheck($ACT); 526b13307fSandi 53b8957367SBenjamin Gilbert //register 54c9570649SAndreas Gohr $nil = array(); 55c9570649SAndreas Gohr if($ACT == 'register' && $_POST['save'] && trigger_event('ACTION_REGISTER', $nil, 'register')){ 56b8957367SBenjamin Gilbert $ACT = 'login'; 57b8957367SBenjamin Gilbert } 586b13307fSandi 598b06d178Schris if ($ACT == 'resendpwd' && act_resendpwd()) { 608b06d178Schris $ACT = 'login'; 618b06d178Schris } 628b06d178Schris 638b06d178Schris //update user profile 648b06d178Schris if (($ACT == 'profile') && updateprofile()) { 654cb79657SMatthias Grimm msg($lang['profchanged'],1); 664cb79657SMatthias Grimm $ACT = 'show'; 678b06d178Schris } 688b06d178Schris 696b13307fSandi //save 706b13307fSandi if($ACT == 'save') 716b13307fSandi $ACT = act_save($ACT); 726b13307fSandi 73067c5d22SBen Coburn //cancel conflicting edit 74067c5d22SBen Coburn if($ACT == 'cancel') 75067c5d22SBen Coburn $ACT = 'show'; 76067c5d22SBen Coburn 77ee4c4a1bSAndreas Gohr //draft deletion 78ee4c4a1bSAndreas Gohr if($ACT == 'draftdel') 79ee4c4a1bSAndreas Gohr $ACT = act_draftdel($ACT); 80ee4c4a1bSAndreas Gohr 81ee4c4a1bSAndreas Gohr //draft saving on preview 82ee4c4a1bSAndreas Gohr if($ACT == 'preview') 83ee4c4a1bSAndreas Gohr $ACT = act_draftsave($ACT); 84ee4c4a1bSAndreas Gohr 856b13307fSandi //edit 86b146b32bSandi if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){ 87af182434Sandi $ACT = act_edit($ACT); 886b13307fSandi }else{ 896b13307fSandi unlock($ID); //try to unlock 906b13307fSandi } 916b13307fSandi 926b13307fSandi //handle export 93ac83b9d8Sandi if(substr($ACT,0,7) == 'export_') 946b13307fSandi $ACT = act_export($ACT); 956b13307fSandi 966b13307fSandi //display some infos 976b13307fSandi if($ACT == 'check'){ 986b13307fSandi check(); 996b13307fSandi $ACT = 'show'; 1006b13307fSandi } 1016b13307fSandi 102c19fe9c0Sandi //handle admin tasks 103c19fe9c0Sandi if($ACT == 'admin'){ 10411e2ce22Schris // retrieve admin plugin name from $_REQUEST['page'] 105bb4866bdSchris if (!empty($_REQUEST['page'])) { 10611e2ce22Schris $pluginlist = plugin_list('admin'); 10711e2ce22Schris if (in_array($_REQUEST['page'], $pluginlist)) { 10811e2ce22Schris // attempt to load the plugin 10911e2ce22Schris if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== NULL) 11011e2ce22Schris $plugin->handle(); 11111e2ce22Schris } 11211e2ce22Schris } 113c19fe9c0Sandi } 1145f312bacSAndreas Gohr 1155f312bacSAndreas Gohr // check permissions again - the action may have changed 1165f312bacSAndreas Gohr $ACT = act_permcheck($ACT); 11724bb549bSchris } // end event ACTION_ACT_PREPROCESS default action 11824bb549bSchris $evt->advise_after(); 11924bb549bSchris unset($evt); 120c19fe9c0Sandi 1215f312bacSAndreas Gohr 1226b13307fSandi //call template FIXME: all needed vars available? 123f63a2007Schris $headers[] = 'Content-Type: text/html; charset=utf-8'; 124746855cfSBen Coburn trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders'); 125f63a2007Schris 1265a892029SAndreas Gohr include(template('main.php')); 127c19fe9c0Sandi // output for the commands is now handled in inc/templates.php 128c19fe9c0Sandi // in function tpl_content() 1296b13307fSandi} 1306b13307fSandi 131f63a2007Schrisfunction act_sendheaders($headers) { 132f63a2007Schris foreach ($headers as $hdr) header($hdr); 133f63a2007Schris} 134f63a2007Schris 1356b13307fSandi/** 136af182434Sandi * Sanitize the action command 137af182434Sandi * 138af182434Sandi * Add all allowed commands here. 139af182434Sandi * 140af182434Sandi * @author Andreas Gohr <andi@splitbrain.org> 141af182434Sandi */ 142af182434Sandifunction act_clean($act){ 143af182434Sandi global $lang; 14460e6b550SAndreas Gohr global $conf; 145af182434Sandi 146ee4c4a1bSAndreas Gohr // check if the action was given as array key 147ee4c4a1bSAndreas Gohr if(is_array($act)){ 148ee4c4a1bSAndreas Gohr list($act) = array_keys($act); 149ee4c4a1bSAndreas Gohr } 150ee4c4a1bSAndreas Gohr 151ac83b9d8Sandi //remove all bad chars 152ac83b9d8Sandi $act = strtolower($act); 153ac83b9d8Sandi $act = preg_replace('/[^a-z_]+/','',$act); 154ac83b9d8Sandi 155ac83b9d8Sandi if($act == 'export_html') $act = 'export_xhtml'; 156cc2ae802SAndreas Gohr if($act == 'export_htmlbody') $act = 'export_xhtmlbody'; 157b146b32bSandi 158409d7af7SAndreas Gohr // check if action is disabled 159409d7af7SAndreas Gohr if(!actionOK($act)){ 160409d7af7SAndreas Gohr msg('Command disabled: '.htmlspecialchars($act),-1); 161409d7af7SAndreas Gohr return 'show'; 162409d7af7SAndreas Gohr } 163409d7af7SAndreas Gohr 16460e6b550SAndreas Gohr //disable all acl related commands if ACL is disabled 16560e6b550SAndreas Gohr if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin', 16660e6b550SAndreas Gohr 'subscribe','unsubscribe','profile', 16760e6b550SAndreas Gohr 'resendpwd',))){ 16860e6b550SAndreas Gohr msg('Command unavailable: '.htmlspecialchars($act),-1); 16960e6b550SAndreas Gohr return 'show'; 17060e6b550SAndreas Gohr } 17160e6b550SAndreas Gohr 172067c5d22SBen Coburn if(!in_array($act,array('login','logout','register','save','cancel','edit','draft', 173ac83b9d8Sandi 'preview','search','show','check','index','revisions', 1741380fc45SAndreas Gohr 'diff','recent','backlink','admin','subscribe', 17518829381SAndreas Gohr 'unsubscribe','profile','resendpwd','recover','wordblock', 176ee4c4a1bSAndreas Gohr 'draftdel',)) && substr($act,0,7) != 'export_' ) { 177ee4c4a1bSAndreas Gohr msg('Command unknown: '.htmlspecialchars($act),-1); 178af182434Sandi return 'show'; 179af182434Sandi } 180af182434Sandi return $act; 181af182434Sandi} 182af182434Sandi 183af182434Sandi/** 1846b13307fSandi * Run permissionchecks 1856b13307fSandi * 1866b13307fSandi * @author Andreas Gohr <andi@splitbrain.org> 1876b13307fSandi */ 1886b13307fSandifunction act_permcheck($act){ 189dbbc6aa7Sandi global $INFO; 1905e199953Smatthiasgrimm global $conf; 191dbbc6aa7Sandi 192ee4c4a1bSAndreas Gohr if(in_array($act,array('save','preview','edit','recover'))){ 1936b13307fSandi if($INFO['exists']){ 194bdbc16bfSandi if($act == 'edit'){ 195bdbc16bfSandi //the edit function will check again and do a source show 196bdbc16bfSandi //when no AUTH_EDIT available 197bdbc16bfSandi $permneed = AUTH_READ; 198bdbc16bfSandi }else{ 1996b13307fSandi $permneed = AUTH_EDIT; 200bdbc16bfSandi } 2016b13307fSandi }else{ 2026b13307fSandi $permneed = AUTH_CREATE; 2036b13307fSandi } 2048b06d178Schris }elseif(in_array($act,array('login','search','recent','profile'))){ 2056b13307fSandi $permneed = AUTH_NONE; 2065e199953Smatthiasgrimm }elseif($act == 'register'){ 2075e199953Smatthiasgrimm $permneed = AUTH_NONE; 208ebd3d9ceSchris }elseif($act == 'resendpwd'){ 209ebd3d9ceSchris $permneed = AUTH_NONE; 210c19fe9c0Sandi }elseif($act == 'admin'){ 211*f8cc712eSAndreas Gohr if($INFO['ismanager']){ 212*f8cc712eSAndreas Gohr // if the manager has the needed permissions for a certain admin 213*f8cc712eSAndreas Gohr // action is checked later 214*f8cc712eSAndreas Gohr $permneed = AUTH_READ; 215*f8cc712eSAndreas Gohr }else{ 216c19fe9c0Sandi $permneed = AUTH_ADMIN; 217*f8cc712eSAndreas Gohr } 2186b13307fSandi }else{ 2196b13307fSandi $permneed = AUTH_READ; 2206b13307fSandi } 221dbbc6aa7Sandi if($INFO['perm'] >= $permneed) return $act; 222dbbc6aa7Sandi 2236b13307fSandi return 'denied'; 2246b13307fSandi} 2256b13307fSandi 2266b13307fSandi/** 227ee4c4a1bSAndreas Gohr * Handle 'draftdel' 228ee4c4a1bSAndreas Gohr * 229ee4c4a1bSAndreas Gohr * Deletes the draft for the current page and user 230ee4c4a1bSAndreas Gohr */ 231ee4c4a1bSAndreas Gohrfunction act_draftdel($act){ 232ee4c4a1bSAndreas Gohr global $INFO; 233ee4c4a1bSAndreas Gohr @unlink($INFO['draft']); 234ee4c4a1bSAndreas Gohr $INFO['draft'] = null; 235ee4c4a1bSAndreas Gohr return 'show'; 236ee4c4a1bSAndreas Gohr} 237ee4c4a1bSAndreas Gohr 238ee4c4a1bSAndreas Gohr/** 239ee4c4a1bSAndreas Gohr * Saves a draft on preview 240ee4c4a1bSAndreas Gohr * 241ee4c4a1bSAndreas Gohr * @todo this currently duplicates code from ajax.php :-/ 242ee4c4a1bSAndreas Gohr */ 243ee4c4a1bSAndreas Gohrfunction act_draftsave($act){ 244ee4c4a1bSAndreas Gohr global $INFO; 245ee4c4a1bSAndreas Gohr global $ID; 246ee4c4a1bSAndreas Gohr global $conf; 247ee4c4a1bSAndreas Gohr if($conf['usedraft'] && $_POST['wikitext']){ 248ee4c4a1bSAndreas Gohr $draft = array('id' => $ID, 249ee4c4a1bSAndreas Gohr 'prefix' => $_POST['prefix'], 250ee4c4a1bSAndreas Gohr 'text' => $_POST['wikitext'], 251ee4c4a1bSAndreas Gohr 'suffix' => $_POST['suffix'], 252ee4c4a1bSAndreas Gohr 'date' => $_POST['date'], 253ee4c4a1bSAndreas Gohr 'client' => $INFO['client'], 254ee4c4a1bSAndreas Gohr ); 255ee4c4a1bSAndreas Gohr $cname = getCacheName($draft['client'].$ID,'.draft'); 256ee4c4a1bSAndreas Gohr if(io_saveFile($cname,serialize($draft))){ 257ee4c4a1bSAndreas Gohr $INFO['draft'] = $cname; 258ee4c4a1bSAndreas Gohr } 259ee4c4a1bSAndreas Gohr } 260ee4c4a1bSAndreas Gohr return $act; 261ee4c4a1bSAndreas Gohr} 262ee4c4a1bSAndreas Gohr 263ee4c4a1bSAndreas Gohr/** 2646b13307fSandi * Handle 'save' 2656b13307fSandi * 2666b13307fSandi * Checks for spam and conflicts and saves the page. 2676b13307fSandi * Does a redirect to show the page afterwards or 2686b13307fSandi * returns a new action. 2696b13307fSandi * 2706b13307fSandi * @author Andreas Gohr <andi@splitbrain.org> 2716b13307fSandi */ 2726b13307fSandifunction act_save($act){ 2736b13307fSandi global $ID; 2746b13307fSandi global $DATE; 2756b13307fSandi global $PRE; 2766b13307fSandi global $TEXT; 2776b13307fSandi global $SUF; 2786b13307fSandi global $SUM; 2796b13307fSandi 2806b13307fSandi //spam check 2816b13307fSandi if(checkwordblock()) 2826b13307fSandi return 'wordblock'; 2836b13307fSandi //conflict check //FIXME use INFO 2846b13307fSandi if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE ) 2856b13307fSandi return 'conflict'; 2866b13307fSandi 2876b13307fSandi //save it 288b6912aeaSAndreas Gohr saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con 2896b13307fSandi //unlock it 2906b13307fSandi unlock($ID); 2916b13307fSandi 292ee4c4a1bSAndreas Gohr //delete draft 293ee4c4a1bSAndreas Gohr act_draftdel($act); 294ee4c4a1bSAndreas Gohr 2956b13307fSandi //show it 2966b13307fSandi session_write_close(); 2976b13307fSandi header("Location: ".wl($ID,'',true)); 2986b13307fSandi exit(); 2996b13307fSandi} 3006b13307fSandi 3016b13307fSandi/** 302b8957367SBenjamin Gilbert * Handle 'login', 'logout' 3036b13307fSandi * 3046b13307fSandi * @author Andreas Gohr <andi@splitbrain.org> 3056b13307fSandi */ 3066b13307fSandifunction act_auth($act){ 30708eda5bcSmatthiasgrimm global $ID; 3087cace34dSAndreas Gohr global $INFO; 30908eda5bcSmatthiasgrimm 3106b13307fSandi //already logged in? 3116b13307fSandi if($_SERVER['REMOTE_USER'] && $act=='login') 3126b13307fSandi return 'show'; 3136b13307fSandi 3146b13307fSandi //handle logout 3156b13307fSandi if($act=='logout'){ 31608eda5bcSmatthiasgrimm $lockedby = checklock($ID); //page still locked? 317424c3c4fSJohannes Buchner if($lockedby == $_SERVER['REMOTE_USER']) 31808eda5bcSmatthiasgrimm unlock($ID); //try to unlock 31908eda5bcSmatthiasgrimm 3207cace34dSAndreas Gohr // do the logout stuff 3216b13307fSandi auth_logoff(); 3227cace34dSAndreas Gohr 3237cace34dSAndreas Gohr // rebuild info array 3247cace34dSAndreas Gohr $INFO = pageinfo(); 3257cace34dSAndreas Gohr 3266b13307fSandi return 'login'; 3276b13307fSandi } 3286b13307fSandi 3296b13307fSandi return $act; 3306b13307fSandi} 3316b13307fSandi 3326b13307fSandi/** 3336b13307fSandi * Handle 'edit', 'preview' 3346b13307fSandi * 3356b13307fSandi * @author Andreas Gohr <andi@splitbrain.org> 3366b13307fSandi */ 3376b13307fSandifunction act_edit($act){ 338cd409024Sjorda global $ID; 339ee4c4a1bSAndreas Gohr global $INFO; 340cd409024Sjorda 3416b13307fSandi //check if locked by anyone - if not lock for my self 3426b13307fSandi $lockedby = checklock($ID); 3436b13307fSandi if($lockedby) return 'locked'; 3446b13307fSandi 3456b13307fSandi lock($ID); 3466b13307fSandi return $act; 3476b13307fSandi} 3486b13307fSandi 3496b13307fSandi/** 3506b13307fSandi * Handle 'edit', 'preview' 3516b13307fSandi * 3526b13307fSandi * @author Andreas Gohr <andi@splitbrain.org> 3536b13307fSandi */ 3546b13307fSandifunction act_export($act){ 3556b13307fSandi global $ID; 3566b13307fSandi global $REV; 3576b13307fSandi 358ac83b9d8Sandi // no renderer for this 359ac83b9d8Sandi if($act == 'export_raw'){ 360ac83b9d8Sandi header('Content-Type: text/plain; charset=utf-8'); 361ac83b9d8Sandi print rawWiki($ID,$REV); 362ac83b9d8Sandi exit; 363ac83b9d8Sandi } 364ac83b9d8Sandi 365ac83b9d8Sandi // html export #FIXME what about the template's style? 366ac83b9d8Sandi if($act == 'export_xhtml'){ 36785f8705cSAnika Henke global $conf; 36885f8705cSAnika Henke global $lang; 3696b13307fSandi header('Content-Type: text/html; charset=utf-8'); 37085f8705cSAnika Henke ptln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'); 37185f8705cSAnika Henke ptln(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'); 37285f8705cSAnika Henke ptln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"'); 37385f8705cSAnika Henke ptln(' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">'); 3746b13307fSandi ptln('<head>'); 37585f8705cSAnika Henke ptln(' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); 37685f8705cSAnika Henke ptln(' <title>'.$ID.'</title>'); 3776b13307fSandi tpl_metaheaders(); 3786b13307fSandi ptln('</head>'); 3796b13307fSandi ptln('<body>'); 3802c5c3308SAndreas Gohr ptln('<div class="dokuwiki export">'); 381ac83b9d8Sandi print p_wiki_xhtml($ID,$REV,false); 382c771e9edSAnika Henke ptln('</div>'); 3836b13307fSandi ptln('</body>'); 3846b13307fSandi ptln('</html>'); 3856b13307fSandi exit; 3866b13307fSandi } 3876b13307fSandi 388cc2ae802SAndreas Gohr // html body only 389cc2ae802SAndreas Gohr if($act == 'export_xhtmlbody'){ 390cc2ae802SAndreas Gohr print p_wiki_xhtml($ID,$REV,false); 391cc2ae802SAndreas Gohr exit; 392cc2ae802SAndreas Gohr } 393cc2ae802SAndreas Gohr 394ac83b9d8Sandi // try to run renderer #FIXME use cached instructions 395ac83b9d8Sandi $mode = substr($act,7); 3969dc2c2afSandi $text = p_render($mode,p_get_instructions(rawWiki($ID,$REV)),$info); 397ac83b9d8Sandi if(!is_null($text)){ 398ac83b9d8Sandi print $text; 3996b13307fSandi exit; 4006b13307fSandi } 4016b13307fSandi 402ac83b9d8Sandi 403ac83b9d8Sandi 4046b13307fSandi return 'show'; 4056b13307fSandi} 406340756e4Sandi 407b158d625SSteven Danz/** 4081380fc45SAndreas Gohr * Handle 'subscribe', 'unsubscribe' 409b158d625SSteven Danz * 410b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 4111380fc45SAndreas Gohr * @todo localize 412b158d625SSteven Danz */ 4131380fc45SAndreas Gohrfunction act_subscription($act){ 414b158d625SSteven Danz global $ID; 415b158d625SSteven Danz global $INFO; 416f9eb5648Ssteven-danz global $lang; 417b158d625SSteven Danz 4181380fc45SAndreas Gohr $file=metaFN($ID,'.mlist'); 4191380fc45SAndreas Gohr if ($act=='subscribe' && !$INFO['subscribed']){ 420b158d625SSteven Danz if ($INFO['userinfo']['mail']){ 4211380fc45SAndreas Gohr if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 4221380fc45SAndreas Gohr $INFO['subscribed'] = true; 423f9eb5648Ssteven-danz msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 424b158d625SSteven Danz } else { 425f9eb5648Ssteven-danz msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 426b158d625SSteven Danz } 427b158d625SSteven Danz } else { 428f9eb5648Ssteven-danz msg($lang['subscribe_noaddress']); 429b158d625SSteven Danz } 4301380fc45SAndreas Gohr } elseif ($act=='unsubscribe' && $INFO['subscribed']){ 431b158d625SSteven Danz if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 4321380fc45SAndreas Gohr $INFO['subscribed'] = false; 433f9eb5648Ssteven-danz msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 434b158d625SSteven Danz } else { 435f9eb5648Ssteven-danz msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 436b158d625SSteven Danz } 437b158d625SSteven Danz } 438b158d625SSteven Danz 439b158d625SSteven Danz return 'show'; 440b158d625SSteven Danz} 441b158d625SSteven Danz 442340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 443