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 return 'show'; 357 } 358 359 //handle logout 360 if($act=='logout'){ 361 $lockedby = checklock($ID); //page still locked? 362 if($lockedby == $_SERVER['REMOTE_USER']) 363 unlock($ID); //try to unlock 364 365 // do the logout stuff 366 auth_logoff(); 367 368 // rebuild info array 369 $INFO = pageinfo(); 370 371 return 'login'; 372 } 373 374 return $act; 375} 376 377/** 378 * Handle 'edit', 'preview' 379 * 380 * @author Andreas Gohr <andi@splitbrain.org> 381 */ 382function act_edit($act){ 383 global $ID; 384 global $INFO; 385 386 //check if locked by anyone - if not lock for my self 387 $lockedby = checklock($ID); 388 if($lockedby) return 'locked'; 389 390 lock($ID); 391 return $act; 392} 393 394/** 395 * Handle 'edit', 'preview' 396 * 397 * @author Andreas Gohr <andi@splitbrain.org> 398 */ 399function act_export($act){ 400 global $ID; 401 global $REV; 402 403 // search engines: never cache exported docs! (Google only currently) 404 header('X-Robots-Tag: noindex'); 405 406 // no renderer for this 407 if($act == 'export_raw'){ 408 header('Content-Type: text/plain; charset=utf-8'); 409 print rawWiki($ID,$REV); 410 exit; 411 } 412 413 // html export #FIXME what about the template's style? 414 if($act == 'export_xhtml'){ 415 global $conf; 416 global $lang; 417 header('Content-Type: text/html; charset=utf-8'); 418 ptln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'); 419 ptln(' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'); 420 ptln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"'); 421 ptln(' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">'); 422 ptln('<head>'); 423 ptln(' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); 424 ptln(' <title>'.$ID.'</title>'); 425 tpl_metaheaders(); 426 ptln('</head>'); 427 ptln('<body>'); 428 ptln('<div class="dokuwiki export">'); 429 $html = p_wiki_xhtml($ID,$REV,false); 430 tpl_toc(); 431 echo $html; 432 ptln('</div>'); 433 ptln('</body>'); 434 ptln('</html>'); 435 exit; 436 } 437 438 // html body only 439 if($act == 'export_xhtmlbody'){ 440 $html = p_wiki_xhtml($ID,$REV,false); 441 tpl_toc(); 442 echo $html; 443 exit; 444 } 445 446 // try to run renderer 447 $mode = substr($act,7); 448 $text = p_cached_output(wikiFN($ID,$REV), $mode); 449 $headers = p_get_metadata($ID,"format $mode"); 450 if(!is_null($text)){ 451 if(is_array($headers)) foreach($headers as $key => $val){ 452 header("$key: $val"); 453 } 454 print $text; 455 exit; 456 } 457 458 return 'show'; 459} 460 461/** 462 * Handle page 'subscribe', 'unsubscribe' 463 * 464 * @author Steven Danz <steven-danz@kc.rr.com> 465 * @todo localize 466 */ 467function act_subscription($act){ 468 global $ID; 469 global $INFO; 470 global $lang; 471 472 $file=metaFN($ID,'.mlist'); 473 if ($act=='subscribe' && !$INFO['subscribed']){ 474 if ($INFO['userinfo']['mail']){ 475 if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 476 $INFO['subscribed'] = true; 477 msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 478 } else { 479 msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 480 } 481 } else { 482 msg($lang['subscribe_noaddress']); 483 } 484 } elseif ($act=='unsubscribe' && $INFO['subscribed']){ 485 if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 486 $INFO['subscribed'] = false; 487 msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 488 } else { 489 msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 490 } 491 } 492 493 return 'show'; 494} 495 496/** 497 * Handle namespace 'subscribe', 'unsubscribe' 498 * 499 */ 500function act_subscriptionns($act){ 501 global $ID; 502 global $INFO; 503 global $lang; 504 505 if(!getNS($ID)) { 506 $file = metaFN(getNS($ID),'.mlist'); 507 $ns = "root"; 508 } else { 509 $file = metaFN(getNS($ID),'/.mlist'); 510 $ns = getNS($ID); 511 } 512 513 // reuse strings used to display the status of the subscribe action 514 $act_msg = rtrim($act, 'ns'); 515 516 if ($act=='subscribens' && !$INFO['subscribedns']){ 517 if ($INFO['userinfo']['mail']){ 518 if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 519 $INFO['subscribedns'] = true; 520 msg(sprintf($lang[$act_msg.'_success'], $INFO['userinfo']['name'], $ns),1); 521 } else { 522 msg(sprintf($lang[$act_msg.'_error'], $INFO['userinfo']['name'], $ns),1); 523 } 524 } else { 525 msg($lang['subscribe_noaddress']); 526 } 527 } elseif ($act=='unsubscribens' && $INFO['subscribedns']){ 528 if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 529 $INFO['subscribedns'] = false; 530 msg(sprintf($lang[$act_msg.'_success'], $INFO['userinfo']['name'], $ns),1); 531 } else { 532 msg(sprintf($lang[$act_msg.'_error'], $INFO['userinfo']['name'], $ns),1); 533 } 534 } 535 536 return 'show'; 537} 538 539//Setup VIM: ex: et ts=2 enc=utf-8 : 540