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', the intial not 'show' and POST, do a redirect 133 if($ACT == 'show' && $preact != '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 * Export a wiki page for various formats 396 * 397 * Triggers ACTION_EXPORT_POSTPROCESS 398 * 399 * Event data: 400 * data['id'] -- page id 401 * data['mode'] -- requested export mode 402 * data['headers'] -- export headers 403 * data['output'] -- export output 404 * 405 * @author Andreas Gohr <andi@splitbrain.org> 406 * @author Michael Klier <chi@chimeric.de> 407 */ 408function act_export($act){ 409 global $ID; 410 global $REV; 411 global $conf; 412 global $lang; 413 414 $pre = ''; 415 $post = ''; 416 $output = ''; 417 $headers = array(); 418 419 // search engines: never cache exported docs! (Google only currently) 420 $headers['X-Robots-Tag'] = 'noindex'; 421 422 $mode = substr($act,7); 423 switch($mode) { 424 case 'raw': 425 $headers['Content-Type'] = 'text/plain; charse=utf-8'; 426 $output = rawWiki($ID,$REV); 427 break; 428 case 'xhtml': 429 $pre .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' . DOKU_LF; 430 $pre .= ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . DOKU_LF; 431 $pre .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$conf['lang'].'"' . DOKU_LF; 432 $pre .= ' lang="'.$conf['lang'].'" dir="'.$lang['direction'].'">' . DOKU_LF; 433 $pre .= '<head>' . DOKU_LF; 434 $pre .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . DOKU_LF; 435 $pre .= ' <title>'.$ID.'</title>' . DOKU_LF; 436 437 // get metaheaders 438 ob_start(); 439 tpl_metaheaders(); 440 $pre .= ob_get_clean(); 441 442 $pre .= '</head>' . DOKU_LF; 443 $pre .= '<body>' . DOKU_LF; 444 $pre .= '<div class="dokuwiki export">' . DOKU_LF; 445 446 // get toc 447 $pre .= tpl_toc(true); 448 449 $headers['Content-Type'] = 'text/html; charset=utf-8'; 450 $output = p_wiki_xhtml($ID,$REV,false); 451 452 $post .= '</div>' . DOKU_LF; 453 $post .= '</body>' . DOKU_LF; 454 $post .= '</html>' . DOKU_LF; 455 break; 456 case 'xhtmlbody': 457 $headers['Content-Type'] = 'text/html; charset=utf-8'; 458 $output = p_wiki_xhtml($ID,$REV,false); 459 break; 460 default: 461 $headers = p_get_metadata($ID,"format $mode"); 462 $output = p_cached_output(wikiFN($ID,$REV), $mode); 463 break; 464 } 465 466 // prepare event data 467 $data = array(); 468 $data['id'] = $ID; 469 $data['mode'] = $mode; 470 $data['headers'] = $headers; 471 $data['output'] =& $output; 472 473 trigger_event('ACTION_EXPORT_POSTPROCESS', $data); 474 475 if(!empty($data['output'])){ 476 if(is_array($data['headers'])) foreach($data['headers'] as $key => $val){ 477 header("$key: $val"); 478 } 479 print $pre.$data['output'].$post; 480 exit; 481 } 482 return 'show'; 483} 484 485/** 486 * Handle page 'subscribe', 'unsubscribe' 487 * 488 * @author Steven Danz <steven-danz@kc.rr.com> 489 * @todo localize 490 */ 491function act_subscription($act){ 492 global $ID; 493 global $INFO; 494 global $lang; 495 496 $file=metaFN($ID,'.mlist'); 497 if ($act=='subscribe' && !$INFO['subscribed']){ 498 if ($INFO['userinfo']['mail']){ 499 if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 500 $INFO['subscribed'] = true; 501 msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 502 } else { 503 msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 504 } 505 } else { 506 msg($lang['subscribe_noaddress']); 507 } 508 } elseif ($act=='unsubscribe' && $INFO['subscribed']){ 509 if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 510 $INFO['subscribed'] = false; 511 msg(sprintf($lang[$act.'_success'], $INFO['userinfo']['name'], $ID),1); 512 } else { 513 msg(sprintf($lang[$act.'_error'], $INFO['userinfo']['name'], $ID),1); 514 } 515 } 516 517 return 'show'; 518} 519 520/** 521 * Handle namespace 'subscribe', 'unsubscribe' 522 * 523 */ 524function act_subscriptionns($act){ 525 global $ID; 526 global $INFO; 527 global $lang; 528 529 if(!getNS($ID)) { 530 $file = metaFN(getNS($ID),'.mlist'); 531 $ns = "root"; 532 } else { 533 $file = metaFN(getNS($ID),'/.mlist'); 534 $ns = getNS($ID); 535 } 536 537 // reuse strings used to display the status of the subscribe action 538 $act_msg = rtrim($act, 'ns'); 539 540 if ($act=='subscribens' && !$INFO['subscribedns']){ 541 if ($INFO['userinfo']['mail']){ 542 if (io_saveFile($file,$_SERVER['REMOTE_USER']."\n",true)) { 543 $INFO['subscribedns'] = true; 544 msg(sprintf($lang[$act_msg.'_success'], $INFO['userinfo']['name'], $ns),1); 545 } else { 546 msg(sprintf($lang[$act_msg.'_error'], $INFO['userinfo']['name'], $ns),1); 547 } 548 } else { 549 msg($lang['subscribe_noaddress']); 550 } 551 } elseif ($act=='unsubscribens' && $INFO['subscribedns']){ 552 if (io_deleteFromFile($file,$_SERVER['REMOTE_USER']."\n")) { 553 $INFO['subscribedns'] = false; 554 msg(sprintf($lang[$act_msg.'_success'], $INFO['userinfo']['name'], $ns),1); 555 } else { 556 msg(sprintf($lang[$act_msg.'_error'], $INFO['userinfo']['name'], $ns),1); 557 } 558 } 559 560 return 'show'; 561} 562 563//Setup VIM: ex: et ts=2 enc=utf-8 : 564