1<?php 2/** 3 * DokuWiki Actions 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9 if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 10 require_once(DOKU_INC.'inc/template.php'); 11 12 13/** 14 * Call the needed action handlers 15 * 16 * @author Andreas Gohr <andi@splitbrain.org> 17 * @triggers ACTION_ACT_PREPROCESS 18 * @triggers ACTION_HEADERS_SEND 19 */ 20function act_dispatch(){ 21 global $INFO; 22 global $ACT; 23 global $ID; 24 global $QUERY; 25 global $lang; 26 global $conf; 27 28 $preact = $ACT; 29 30 // give plugins an opportunity to process the action 31 $evt = new Doku_Event('ACTION_ACT_PREPROCESS',$ACT); 32 if ($evt->advise_before()) { 33 34 //sanitize $ACT 35 $ACT = act_clean($ACT); 36 37 //check if searchword was given - else just show 38 $s = cleanID($QUERY); 39 if($ACT == 'search' && empty($s)){ 40 $ACT = 'show'; 41 } 42 43 //login stuff 44 if(in_array($ACT,array('login','logout'))){ 45 $ACT = act_auth($ACT); 46 } 47 48 //check if user is asking to (un)subscribe a page 49 if($ACT == 'subscribe' || $ACT == 'unsubscribe') 50 $ACT = act_subscription($ACT); 51 52 //check if user is asking to (un)subscribe a namespace 53 if($ACT == 'subscribens' || $ACT == 'unsubscribens') 54 $ACT = act_subscriptionns($ACT); 55 56 //check permissions 57 $ACT = act_permcheck($ACT); 58 59 //register 60 $nil = array(); 61 if($ACT == 'register' && $_POST['save'] && register()){ 62 $ACT = 'login'; 63 } 64 65 if ($ACT == 'resendpwd' && act_resendpwd()) { 66 $ACT = 'login'; 67 } 68 69 //update user profile 70 if (($ACT == 'profile') && updateprofile()) { 71 msg($lang['profchanged'],1); 72 $ACT = 'show'; 73 } 74 75 //save 76 if($ACT == 'save'){ 77 if(checkSecurityToken()){ 78 $ACT = act_save($ACT); 79 }else{ 80 $ACT = 'show'; 81 } 82 } 83 84 //cancel conflicting edit 85 if($ACT == 'cancel') 86 $ACT = 'show'; 87 88 //draft deletion 89 if($ACT == 'draftdel') 90 $ACT = act_draftdel($ACT); 91 92 //draft saving on preview 93 if($ACT == 'preview') 94 $ACT = act_draftsave($ACT); 95 96 //edit 97 if(($ACT == 'edit' || $ACT == 'preview') && $INFO['editable']){ 98 $ACT = act_edit($ACT); 99 }else{ 100 unlock($ID); //try to unlock 101 } 102 103 //handle export 104 if(substr($ACT,0,7) == 'export_') 105 $ACT = act_export($ACT); 106 107 //display some infos 108 if($ACT == 'check'){ 109 check(); 110 $ACT = 'show'; 111 } 112 113 //handle admin tasks 114 if($ACT == 'admin'){ 115 // retrieve admin plugin name from $_REQUEST['page'] 116 if (!empty($_REQUEST['page'])) { 117 $pluginlist = plugin_list('admin'); 118 if (in_array($_REQUEST['page'], $pluginlist)) { 119 // attempt to load the plugin 120 if ($plugin =& plugin_load('admin',$_REQUEST['page']) !== NULL) 121 $plugin->handle(); 122 } 123 } 124 } 125 126 // check permissions again - the action may have changed 127 $ACT = act_permcheck($ACT); 128 } // end event ACTION_ACT_PREPROCESS default action 129 $evt->advise_after(); 130 unset($evt); 131 132 // when action 'show' and POST, do a redirect 133 if($ACT == 'show' && strtolower($_SERVER['REQUEST_METHOD']) == 'post'){ 134 act_redirect($ID,$preact); 135 } 136 137 //call template FIXME: all needed vars available? 138 $headers[] = 'Content-Type: text/html; charset=utf-8'; 139 trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders'); 140 141 include(template('main.php')); 142 // output for the commands is now handled in inc/templates.php 143 // in function tpl_content() 144} 145 146function act_sendheaders($headers) { 147 foreach ($headers as $hdr) header($hdr); 148} 149 150/** 151 * Sanitize the action command 152 * 153 * Add all allowed commands here. 154 * 155 * @author Andreas Gohr <andi@splitbrain.org> 156 */ 157function act_clean($act){ 158 global $lang; 159 global $conf; 160 161 // check if the action was given as array key 162 if(is_array($act)){ 163 list($act) = array_keys($act); 164 } 165 166 //remove all bad chars 167 $act = strtolower($act); 168 $act = preg_replace('/[^1-9a-z_]+/','',$act); 169 170 if($act == 'export_html') $act = 'export_xhtml'; 171 if($act == 'export_htmlbody') $act = 'export_xhtmlbody'; 172 173 // check if action is disabled 174 if(!actionOK($act)){ 175 msg('Command disabled: '.htmlspecialchars($act),-1); 176 return 'show'; 177 } 178 179 //disable all acl related commands if ACL is disabled 180 if(!$conf['useacl'] && in_array($act,array('login','logout','register','admin', 181 'subscribe','unsubscribe','profile', 182 'resendpwd','subscribens','unsubscribens',))){ 183 msg('Command unavailable: '.htmlspecialchars($act),-1); 184 return 'show'; 185 } 186 187 if(!in_array($act,array('login','logout','register','save','cancel','edit','draft', 188 'preview','search','show','check','index','revisions', 189 'diff','recent','backlink','admin','subscribe', 190 'unsubscribe','profile','resendpwd','recover','wordblock', 191 'draftdel','subscribens','unsubscribens',)) && substr($act,0,7) != 'export_' ) { 192 msg('Command unknown: '.htmlspecialchars($act),-1); 193 return 'show'; 194 } 195 return $act; 196} 197 198/** 199 * Run permissionchecks 200 * 201 * @author Andreas Gohr <andi@splitbrain.org> 202 */ 203function act_permcheck($act){ 204 global $INFO; 205 global $conf; 206 207 if(in_array($act,array('save','preview','edit','recover'))){ 208 if($INFO['exists']){ 209 if($act == 'edit'){ 210 //the edit function will check again and do a source show 211 //when no AUTH_EDIT available 212 $permneed = AUTH_READ; 213 }else{ 214 $permneed = AUTH_EDIT; 215 } 216 }else{ 217 $permneed = AUTH_CREATE; 218 } 219 }elseif(in_array($act,array('login','search','recent','profile'))){ 220 $permneed = AUTH_NONE; 221 }elseif($act == 'register'){ 222 $permneed = AUTH_NONE; 223 }elseif($act == 'resendpwd'){ 224 $permneed = AUTH_NONE; 225 }elseif($act == 'admin'){ 226 if($INFO['ismanager']){ 227 // if the manager has the needed permissions for a certain admin 228 // action is checked later 229 $permneed = AUTH_READ; 230 }else{ 231 $permneed = AUTH_ADMIN; 232 } 233 }else{ 234 $permneed = AUTH_READ; 235 } 236 if($INFO['perm'] >= $permneed) return $act; 237 238 return 'denied'; 239} 240 241/** 242 * Handle 'draftdel' 243 * 244 * Deletes the draft for the current page and user 245 */ 246function act_draftdel($act){ 247 global $INFO; 248 @unlink($INFO['draft']); 249 $INFO['draft'] = null; 250 return 'show'; 251} 252 253/** 254 * Saves a draft on preview 255 * 256 * @todo this currently duplicates code from ajax.php :-/ 257 */ 258function act_draftsave($act){ 259 global $INFO; 260 global $ID; 261 global $conf; 262 if($conf['usedraft'] && $_POST['wikitext']){ 263 $draft = array('id' => $ID, 264 'prefix' => $_POST['prefix'], 265 'text' => $_POST['wikitext'], 266 'suffix' => $_POST['suffix'], 267 'date' => $_POST['date'], 268 'client' => $INFO['client'], 269 ); 270 $cname = getCacheName($draft['client'].$ID,'.draft'); 271 if(io_saveFile($cname,serialize($draft))){ 272 $INFO['draft'] = $cname; 273 } 274 } 275 return $act; 276} 277 278/** 279 * Handle 'save' 280 * 281 * Checks for spam and conflicts and saves the page. 282 * Does a redirect to show the page afterwards or 283 * returns a new action. 284 * 285 * @author Andreas Gohr <andi@splitbrain.org> 286 */ 287function act_save($act){ 288 global $ID; 289 global $DATE; 290 global $PRE; 291 global $TEXT; 292 global $SUF; 293 global $SUM; 294 295 //spam check 296 if(checkwordblock()) 297 return 'wordblock'; 298 //conflict check //FIXME use INFO 299 if($DATE != 0 && @filemtime(wikiFN($ID)) > $DATE ) 300 return 'conflict'; 301 302 //save it 303 saveWikiText($ID,con($PRE,$TEXT,$SUF,1),$SUM,$_REQUEST['minor']); //use pretty mode for con 304 //unlock it 305 unlock($ID); 306 307 //delete draft 308 act_draftdel($act); 309 session_write_close(); 310 311 // when done, show page 312 return 'show'; 313} 314 315function act_redirect($id,$preact){ 316 global $PRE; 317 global $TEXT; 318 319 //get section name when coming from section edit 320 if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){ 321 #FIXME duplicates code from xhtml renderer 322 $title = $match[0]; 323 $title = str_replace(':','',cleanID($title)); 324 $title = ltrim($title,'0123456789._-'); 325 if(empty($title)) $title='section'; 326 } 327 328 $opts = array( 329 'id' => $id, 330 'fragment' => $title, 331 'preact' => $preact 332 ); 333 trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute'); 334} 335 336function act_redirect_execute($opts){ 337 $go = wl($opts['id'],'',true); 338 if($opts['fragment']) $go .= '#'.$opts['fragment']; 339 340 //show it 341 header("Location: $go"); 342 exit(); 343} 344 345/** 346 * Handle 'login', 'logout' 347 * 348 * @author Andreas Gohr <andi@splitbrain.org> 349 */ 350function act_auth($act){ 351 global $ID; 352 global $INFO; 353 354 //already logged in? 355 if($_SERVER['REMOTE_USER'] && $act=='login'){ 356 header("Location: ".wl($ID,'',true)); 357 exit; 358 } 359 360 //handle logout 361 if($act=='logout'){ 362 $lockedby = checklock($ID); //page still locked? 363 if($lockedby == $_SERVER['REMOTE_USER']) 364 unlock($ID); //try to unlock 365 366 // do the logout stuff 367 auth_logoff(); 368 369 // rebuild info array 370 $INFO = pageinfo(); 371 372 return 'login'; 373 } 374 375 return $act; 376} 377 378/** 379 * Handle 'edit', 'preview' 380 * 381 * @author Andreas Gohr <andi@splitbrain.org> 382 */ 383function act_edit($act){ 384 global $ID; 385 global $INFO; 386 387 //check if locked by anyone - if not lock for my self 388 $lockedby = checklock($ID); 389 if($lockedby) return 'locked'; 390 391 lock($ID); 392 return $act; 393} 394 395/** 396 * Handle 'edit', 'preview' 397 * 398 * @author Andreas Gohr <andi@splitbrain.org> 399 */ 400function act_export($act){ 401 global $ID; 402 global $REV; 403 404 // search engines: never cache exported docs! (Google only currently) 405 header('X-Robots-Tag: noindex'); 406 407 // no renderer for this 408 if($act == 'export_raw'){ 409 header('Content-Type: text/plain; charset=utf-8'); 410 print rawWiki($ID,$REV); 411 exit; 412 } 413 414 // html export #FIXME what about the template's style? 415 if($act == 'export_xhtml'){ 416 global $conf; 417 global $lang; 418 header('Content-Type: text/html; charset=utf-8'); 419 ptln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'); 420 ptln(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'); 421 ptln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"'); 422 ptln(' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">'); 423 ptln('<head>'); 424 ptln(' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); 425 ptln(' <title>'.$ID.'</title>'); 426 tpl_metaheaders(); 427 ptln('</head>'); 428 ptln('<body>'); 429 ptln('<div class="dokuwiki export">'); 430 $html = p_wiki_xhtml($ID,$REV,false); 431 tpl_toc(); 432 echo $html; 433 ptln('</div>'); 434 ptln('</body>'); 435 ptln('</html>'); 436 exit; 437 } 438 439 // html body only 440 if($act == 'export_xhtmlbody'){ 441 $html = p_wiki_xhtml($ID,$REV,false); 442 tpl_toc(); 443 echo $html; 444 exit; 445 } 446 447 // try to run renderer 448 $mode = substr($act,7); 449 $text = p_cached_output(wikiFN($ID,$REV), $mode); 450 $headers = p_get_metadata($ID,"format $mode"); 451 if(!is_null($text)){ 452 if(is_array($headers)) foreach($headers as $key => $val){ 453 header("$key: $val"); 454 } 455 print $text; 456 exit; 457 } 458 459 return 'show'; 460} 461 462/** 463 * Handle page 'subscribe', 'unsubscribe' 464 * 465 * @author Steven Danz <steven-danz@kc.rr.com> 466 * @todo localize 467 */ 468function act_subscription($act){ 469 global $ID; 470 global $INFO; 471 global $lang; 472 473 $file=metaFN($ID,'.mlist'); 474 if ($act=='subscribe' && !$INFO['subscribed']){ 475 if ($INFO['userinfo']['mail']){ 476 if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 477 $INFO['subscribed'] = true; 478 msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 479 } else { 480 msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 481 } 482 } else { 483 msg($lang['subscribe_noaddress']); 484 } 485 } elseif ($act=='unsubscribe' && $INFO['subscribed']){ 486 if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 487 $INFO['subscribed'] = false; 488 msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 489 } else { 490 msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 491 } 492 } 493 494 return 'show'; 495} 496 497/** 498 * Handle namespace 'subscribe', 'unsubscribe' 499 * 500 */ 501function act_subscriptionns($act){ 502 global $ID; 503 global $INFO; 504 global $lang; 505 506 if(!getNS($ID)) { 507 $file = metaFN(getNS($ID),'.mlist'); 508 $ns = "root"; 509 } else { 510 $file = metaFN(getNS($ID),'/.mlist'); 511 $ns = getNS($ID); 512 } 513 514 // reuse strings used to display the status of the subscribe action 515 $act_msg = rtrim($act, 'ns'); 516 517 if ($act=='subscribens' && !$INFO['subscribedns']){ 518 if ($INFO['userinfo']['mail']){ 519 if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 520 $INFO['subscribedns'] = true; 521 msg(sprintf($lang[$act_msg.'_success'], $INFO['userinfo']['name'], $ns),1); 522 } else { 523 msg(sprintf($lang[$act_msg.'_error'], $INFO['userinfo']['name'], $ns),1); 524 } 525 } else { 526 msg($lang['subscribe_noaddress']); 527 } 528 } elseif ($act=='unsubscribens' && $INFO['subscribedns']){ 529 if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 530 $INFO['subscribedns'] = false; 531 msg(sprintf($lang[$act_msg.'_success'], $INFO['userinfo']['name'], $ns),1); 532 } else { 533 msg(sprintf($lang[$act_msg.'_error'], $INFO['userinfo']['name'], $ns),1); 534 } 535 } 536 537 return 'show'; 538} 539 540//Setup VIM: ex: et ts=2 enc=utf-8 : 541