1<?php 2/** 3 * Common DokuWiki functions 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',realpath(dirname(__FILE__).'/../').'/'); 10 require_once(DOKU_CONF.'dokuwiki.php'); 11 require_once(DOKU_INC.'inc/io.php'); 12 require_once(DOKU_INC.'inc/changelog.php'); 13 require_once(DOKU_INC.'inc/utf8.php'); 14 require_once(DOKU_INC.'inc/mail.php'); 15 require_once(DOKU_INC.'inc/parserutils.php'); 16 17/** 18 * These constants are used with the recents function 19 */ 20define('RECENTS_SKIP_DELETED',2); 21define('RECENTS_SKIP_MINORS',4); 22define('RECENTS_SKIP_SUBSPACES',8); 23 24/** 25 * Wrapper around htmlspecialchars() 26 * 27 * @author Andreas Gohr <andi@splitbrain.org> 28 * @see htmlspecialchars() 29 */ 30function hsc($string){ 31 return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 32} 33 34/** 35 * print a newline terminated string 36 * 37 * You can give an indention as optional parameter 38 * 39 * @author Andreas Gohr <andi@splitbrain.org> 40 */ 41function ptln($string,$intend=0){ 42 for($i=0; $i<$intend; $i++) print ' '; 43 print"$string\n"; 44} 45 46/** 47 * Return info about the current document as associative 48 * array. 49 * 50 * @author Andreas Gohr <andi@splitbrain.org> 51 */ 52function pageinfo(){ 53 global $ID; 54 global $REV; 55 global $USERINFO; 56 global $conf; 57 58 if($_SERVER['REMOTE_USER']){ 59 $info['userinfo'] = $USERINFO; 60 $info['perm'] = auth_quickaclcheck($ID); 61 $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']); 62 $info['client'] = $_SERVER['REMOTE_USER']; 63 64 // if some outside auth were used only REMOTE_USER is set 65 if(!$info['userinfo']['name']){ 66 $info['userinfo']['name'] = $_SERVER['REMOTE_USER']; 67 } 68 69 }else{ 70 $info['perm'] = auth_aclcheck($ID,'',null); 71 $info['subscribed'] = false; 72 $info['client'] = clientIP(true); 73 } 74 75 $info['namespace'] = getNS($ID); 76 $info['locked'] = checklock($ID); 77 $info['filepath'] = realpath(wikiFN($ID,$REV)); 78 $info['exists'] = @file_exists($info['filepath']); 79 if($REV && !$info['exists']){ 80 //check if current revision was meant 81 $cur = wikiFN($ID); 82 if(@file_exists($cur) && (@filemtime($cur) == $REV)){ 83 $info['filepath'] = realpath($cur); 84 $info['exists'] = true; 85 $REV = ''; 86 } 87 } 88 $info['rev'] = $REV; 89 if($info['exists']){ 90 $info['writable'] = (is_writable($info['filepath']) && 91 ($info['perm'] >= AUTH_EDIT)); 92 }else{ 93 $info['writable'] = ($info['perm'] >= AUTH_CREATE); 94 } 95 $info['editable'] = ($info['writable'] && empty($info['lock'])); 96 $info['lastmod'] = @filemtime($info['filepath']); 97 98 //load page meta data 99 $info['meta'] = p_get_metadata($ID); 100 101 //who's the editor 102 if($REV){ 103 $revinfo = getRevisionInfo($ID, $REV, 1024); 104 }else{ 105 $revinfo = $info['meta']['last_change']; 106 } 107 $info['ip'] = $revinfo['ip']; 108 $info['user'] = $revinfo['user']; 109 $info['sum'] = $revinfo['sum']; 110 // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 111 // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor']. 112 113 if($revinfo['user']){ 114 $info['editor'] = $revinfo['user']; 115 }else{ 116 $info['editor'] = $revinfo['ip']; 117 } 118 119 // draft 120 $draft = getCacheName($info['client'].$ID,'.draft'); 121 if(@file_exists($draft)){ 122 if(@filemtime($draft) < @filemtime(wikiFN($ID))){ 123 // remove stale draft 124 @unlink($draft); 125 }else{ 126 $info['draft'] = $draft; 127 } 128 } 129 130 return $info; 131} 132 133/** 134 * Build an string of URL parameters 135 * 136 * @author Andreas Gohr 137 */ 138function buildURLparams($params, $sep='&'){ 139 $url = ''; 140 $amp = false; 141 foreach($params as $key => $val){ 142 if($amp) $url .= $sep; 143 144 $url .= $key.'='; 145 $url .= rawurlencode($val); 146 $amp = true; 147 } 148 return $url; 149} 150 151/** 152 * Build an string of html tag attributes 153 * 154 * @author Andreas Gohr 155 */ 156function buildAttributes($params){ 157 $url = ''; 158 foreach($params as $key => $val){ 159 $url .= $key.'="'; 160 $url .= htmlspecialchars ($val); 161 $url .= '" '; 162 } 163 return $url; 164} 165 166 167/** 168 * print a message 169 * 170 * If HTTP headers were not sent yet the message is added 171 * to the global message array else it's printed directly 172 * using html_msgarea() 173 * 174 * 175 * Levels can be: 176 * 177 * -1 error 178 * 0 info 179 * 1 success 180 * 181 * @author Andreas Gohr <andi@splitbrain.org> 182 * @see html_msgarea 183 */ 184function msg($message,$lvl=0,$line='',$file=''){ 185 global $MSG; 186 $errors[-1] = 'error'; 187 $errors[0] = 'info'; 188 $errors[1] = 'success'; 189 190 if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 191 192 if(!headers_sent()){ 193 if(!isset($MSG)) $MSG = array(); 194 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 195 }else{ 196 $MSG = array(); 197 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 198 if(function_exists('html_msgarea')){ 199 html_msgarea(); 200 }else{ 201 print "ERROR($lvl) $message"; 202 } 203 } 204} 205 206/** 207 * This builds the breadcrumb trail and returns it as array 208 * 209 * @author Andreas Gohr <andi@splitbrain.org> 210 */ 211function breadcrumbs(){ 212 // we prepare the breadcrumbs early for quick session closing 213 static $crumbs = null; 214 if($crumbs != null) return $crumbs; 215 216 global $ID; 217 global $ACT; 218 global $conf; 219 $crumbs = $_SESSION[$conf['title']]['bc']; 220 221 //first visit? 222 if (!is_array($crumbs)){ 223 $crumbs = array(); 224 } 225 //we only save on show and existing wiki documents 226 $file = wikiFN($ID); 227 if($ACT != 'show' || !@file_exists($file)){ 228 $_SESSION[$conf['title']]['bc'] = $crumbs; 229 return $crumbs; 230 } 231 232 // page names 233 $name = noNS($ID); 234 if ($conf['useheading']) { 235 // get page title 236 $title = p_get_first_heading($ID); 237 if ($title) { 238 $name = $title; 239 } 240 } 241 242 //remove ID from array 243 if (isset($crumbs[$ID])) { 244 unset($crumbs[$ID]); 245 } 246 247 //add to array 248 $crumbs[$ID] = $name; 249 //reduce size 250 while(count($crumbs) > $conf['breadcrumbs']){ 251 array_shift($crumbs); 252 } 253 //save to session 254 $_SESSION[$conf['title']]['bc'] = $crumbs; 255 return $crumbs; 256} 257 258/** 259 * Filter for page IDs 260 * 261 * This is run on a ID before it is outputted somewhere 262 * currently used to replace the colon with something else 263 * on Windows systems and to have proper URL encoding 264 * 265 * Urlencoding is ommitted when the second parameter is false 266 * 267 * @author Andreas Gohr <andi@splitbrain.org> 268 */ 269function idfilter($id,$ue=true){ 270 global $conf; 271 if ($conf['useslash'] && $conf['userewrite']){ 272 $id = strtr($id,':','/'); 273 }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 274 $conf['userewrite']) { 275 $id = strtr($id,':',';'); 276 } 277 if($ue){ 278 $id = rawurlencode($id); 279 $id = str_replace('%3A',':',$id); //keep as colon 280 $id = str_replace('%2F','/',$id); //keep as slash 281 } 282 return $id; 283} 284 285/** 286 * This builds a link to a wikipage 287 * 288 * It handles URL rewriting and adds additional parameter if 289 * given in $more 290 * 291 * @author Andreas Gohr <andi@splitbrain.org> 292 */ 293function wl($id='',$more='',$abs=false,$sep='&'){ 294 global $conf; 295 if(is_array($more)){ 296 $more = buildURLparams($more,$sep); 297 }else{ 298 $more = str_replace(',',$sep,$more); 299 } 300 301 $id = idfilter($id); 302 if($abs){ 303 $xlink = DOKU_URL; 304 }else{ 305 $xlink = DOKU_BASE; 306 } 307 308 if($conf['userewrite'] == 2){ 309 $xlink .= DOKU_SCRIPT.'/'.$id; 310 if($more) $xlink .= '?'.$more; 311 }elseif($conf['userewrite']){ 312 $xlink .= $id; 313 if($more) $xlink .= '?'.$more; 314 }else{ 315 $xlink .= DOKU_SCRIPT.'?id='.$id; 316 if($more) $xlink .= $sep.$more; 317 } 318 319 return $xlink; 320} 321 322/** 323 * This builds a link to an alternate page format 324 * 325 * Handles URL rewriting if enabled. Follows the style of wl(). 326 * 327 * @author Ben Coburn <btcoburn@silicodon.net> 328 */ 329function exportlink($id='',$format='raw',$more='',$abs=false,$sep='&'){ 330 global $conf; 331 if(is_array($more)){ 332 $more = buildURLparams($more,$sep); 333 }else{ 334 $more = str_replace(',',$sep,$more); 335 } 336 337 $format = rawurlencode($format); 338 $id = idfilter($id); 339 if($abs){ 340 $xlink = DOKU_URL; 341 }else{ 342 $xlink = DOKU_BASE; 343 } 344 345 if($conf['userewrite'] == 2){ 346 $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 347 if($more) $xlink .= $sep.$more; 348 }elseif($conf['userewrite'] == 1){ 349 $xlink .= '_export/'.$format.'/'.$id; 350 if($more) $xlink .= '?'.$more; 351 }else{ 352 $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 353 if($more) $xlink .= $sep.$more; 354 } 355 356 return $xlink; 357} 358 359/** 360 * Build a link to a media file 361 * 362 * Will return a link to the detail page if $direct is false 363 */ 364function ml($id='',$more='',$direct=true,$sep='&'){ 365 global $conf; 366 if(is_array($more)){ 367 $more = buildURLparams($more,$sep); 368 }else{ 369 $more = str_replace(',',$sep,$more); 370 } 371 372 $xlink = DOKU_BASE; 373 374 // external URLs are always direct without rewriting 375 if(preg_match('#^(https?|ftp)://#i',$id)){ 376 $xlink .= 'lib/exe/fetch.php'; 377 if($more){ 378 $xlink .= '?'.$more; 379 $xlink .= $sep.'media='.rawurlencode($id); 380 }else{ 381 $xlink .= '?media='.rawurlencode($id); 382 } 383 return $xlink; 384 } 385 386 $id = idfilter($id); 387 388 // decide on scriptname 389 if($direct){ 390 if($conf['userewrite'] == 1){ 391 $script = '_media'; 392 }else{ 393 $script = 'lib/exe/fetch.php'; 394 } 395 }else{ 396 if($conf['userewrite'] == 1){ 397 $script = '_detail'; 398 }else{ 399 $script = 'lib/exe/detail.php'; 400 } 401 } 402 403 // build URL based on rewrite mode 404 if($conf['userewrite']){ 405 $xlink .= $script.'/'.$id; 406 if($more) $xlink .= '?'.$more; 407 }else{ 408 if($more){ 409 $xlink .= $script.'?'.$more; 410 $xlink .= $sep.'media='.$id; 411 }else{ 412 $xlink .= $script.'?media='.$id; 413 } 414 } 415 416 return $xlink; 417} 418 419 420 421/** 422 * Just builds a link to a script 423 * 424 * @todo maybe obsolete 425 * @author Andreas Gohr <andi@splitbrain.org> 426 */ 427function script($script='doku.php'){ 428# $link = getBaseURL(); 429# $link .= $script; 430# return $link; 431 return DOKU_BASE.DOKU_SCRIPT; 432} 433 434/** 435 * Spamcheck against wordlist 436 * 437 * Checks the wikitext against a list of blocked expressions 438 * returns true if the text contains any bad words 439 * 440 * @author Andreas Gohr <andi@splitbrain.org> 441 */ 442function checkwordblock(){ 443 global $TEXT; 444 global $conf; 445 446 if(!$conf['usewordblock']) return false; 447 448 $wordblocks = getWordblocks(); 449 //how many lines to read at once (to work around some PCRE limits) 450 if(version_compare(phpversion(),'4.3.0','<')){ 451 //old versions of PCRE define a maximum of parenthesises even if no 452 //backreferences are used - the maximum is 99 453 //this is very bad performancewise and may even be too high still 454 $chunksize = 40; 455 }else{ 456 //read file in chunks of 600 - this should work around the 457 //MAX_PATTERN_SIZE in modern PCRE 458 $chunksize = 400; 459 } 460 while($blocks = array_splice($wordblocks,0,$chunksize)){ 461 $re = array(); 462 #build regexp from blocks 463 foreach($blocks as $block){ 464 $block = preg_replace('/#.*$/','',$block); 465 $block = trim($block); 466 if(empty($block)) continue; 467 $re[] = $block; 468 } 469 if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) { 470 return true; 471 } 472 } 473 return false; 474} 475 476/** 477 * Return the IP of the client 478 * 479 * Honours X-Forwarded-For and X-Real-IP Proxy Headers 480 * 481 * It returns a comma separated list of IPs if the above mentioned 482 * headers are set. If the single parameter is set, it tries to return 483 * a routable public address, prefering the ones suplied in the X 484 * headers 485 * 486 * @param boolean $single If set only a single IP is returned 487 * @author Andreas Gohr <andi@splitbrain.org> 488 */ 489function clientIP($single=false){ 490 $ip = array(); 491 $ip[] = $_SERVER['REMOTE_ADDR']; 492 if($_SERVER['HTTP_X_FORWARDED_FOR']) 493 $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])); 494 if($_SERVER['HTTP_X_REAL_IP']) 495 $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP'])); 496 497 // remove any non-IP stuff 498 $cnt = count($ip); 499 $match = array(); 500 for($i=0; $i<$cnt; $i++){ 501 if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) { 502 $ip[$i] = $match[0]; 503 } else { 504 $ip[$i] = ''; 505 } 506 if(empty($ip[$i])) unset($ip[$i]); 507 } 508 $ip = array_values(array_unique($ip)); 509 if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 510 511 if(!$single) return join(',',$ip); 512 513 // decide which IP to use, trying to avoid local addresses 514 $ip = array_reverse($ip); 515 foreach($ip as $i){ 516 if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){ 517 continue; 518 }else{ 519 return $i; 520 } 521 } 522 // still here? just use the first (last) address 523 return $ip[0]; 524} 525 526/** 527 * Checks if a given page is currently locked. 528 * 529 * removes stale lockfiles 530 * 531 * @author Andreas Gohr <andi@splitbrain.org> 532 */ 533function checklock($id){ 534 global $conf; 535 $lock = wikiLockFN($id); 536 537 //no lockfile 538 if(!@file_exists($lock)) return false; 539 540 //lockfile expired 541 if((time() - filemtime($lock)) > $conf['locktime']){ 542 @unlink($lock); 543 return false; 544 } 545 546 //my own lock 547 $ip = io_readFile($lock); 548 if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 549 return false; 550 } 551 552 return $ip; 553} 554 555/** 556 * Lock a page for editing 557 * 558 * @author Andreas Gohr <andi@splitbrain.org> 559 */ 560function lock($id){ 561 $lock = wikiLockFN($id); 562 if($_SERVER['REMOTE_USER']){ 563 io_saveFile($lock,$_SERVER['REMOTE_USER']); 564 }else{ 565 io_saveFile($lock,clientIP()); 566 } 567} 568 569/** 570 * Unlock a page if it was locked by the user 571 * 572 * @author Andreas Gohr <andi@splitbrain.org> 573 * @return bool true if a lock was removed 574 */ 575function unlock($id){ 576 $lock = wikiLockFN($id); 577 if(@file_exists($lock)){ 578 $ip = io_readFile($lock); 579 if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 580 @unlink($lock); 581 return true; 582 } 583 } 584 return false; 585} 586 587/** 588 * convert line ending to unix format 589 * 590 * @see formText() for 2crlf conversion 591 * @author Andreas Gohr <andi@splitbrain.org> 592 */ 593function cleanText($text){ 594 $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 595 return $text; 596} 597 598/** 599 * Prepares text for print in Webforms by encoding special chars. 600 * It also converts line endings to Windows format which is 601 * pseudo standard for webforms. 602 * 603 * @see cleanText() for 2unix conversion 604 * @author Andreas Gohr <andi@splitbrain.org> 605 */ 606function formText($text){ 607 $text = preg_replace("/\012/","\015\012",$text); 608 return htmlspecialchars($text); 609} 610 611/** 612 * Returns the specified local text in raw format 613 * 614 * @author Andreas Gohr <andi@splitbrain.org> 615 */ 616function rawLocale($id){ 617 return io_readFile(localeFN($id)); 618} 619 620/** 621 * Returns the raw WikiText 622 * 623 * @author Andreas Gohr <andi@splitbrain.org> 624 */ 625function rawWiki($id,$rev=''){ 626 return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 627} 628 629/** 630 * Returns the pagetemplate contents for the ID's namespace 631 * 632 * @author Andreas Gohr <andi@splitbrain.org> 633 */ 634function pageTemplate($data){ 635 $id = $data[0]; 636 global $conf; 637 global $INFO; 638 $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt'); 639 $tpl = str_replace('@ID@',$id,$tpl); 640 $tpl = str_replace('@NS@',getNS($id),$tpl); 641 $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl); 642 $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl); 643 $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl); 644 $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl); 645 $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl); 646 return $tpl; 647} 648 649 650/** 651 * Returns the raw Wiki Text in three slices. 652 * 653 * The range parameter needs to have the form "from-to" 654 * and gives the range of the section in bytes - no 655 * UTF-8 awareness is needed. 656 * The returned order is prefix, section and suffix. 657 * 658 * @author Andreas Gohr <andi@splitbrain.org> 659 */ 660function rawWikiSlices($range,$id,$rev=''){ 661 list($from,$to) = split('-',$range,2); 662 $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 663 if(!$from) $from = 0; 664 if(!$to) $to = strlen($text)+1; 665 666 $slices[0] = substr($text,0,$from-1); 667 $slices[1] = substr($text,$from-1,$to-$from); 668 $slices[2] = substr($text,$to); 669 670 return $slices; 671} 672 673/** 674 * Joins wiki text slices 675 * 676 * function to join the text slices with correct lineendings again. 677 * When the pretty parameter is set to true it adds additional empty 678 * lines between sections if needed (used on saving). 679 * 680 * @author Andreas Gohr <andi@splitbrain.org> 681 */ 682function con($pre,$text,$suf,$pretty=false){ 683 684 if($pretty){ 685 if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 686 if($suf && substr($text,-1) != "\n") $text .= "\n"; 687 } 688 689 if($pre) $pre .= "\n"; 690 if($suf) $text .= "\n"; 691 return $pre.$text.$suf; 692} 693 694/** 695 * print debug messages 696 * 697 * little function to print the content of a var 698 * 699 * @author Andreas Gohr <andi@splitbrain.org> 700 */ 701function dbg($msg,$hidden=false){ 702 (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 703 print_r($msg); 704 (!$hidden) ? print '</pre>' : print "\n-->"; 705} 706 707/** 708 * Print info to a log file 709 * 710 * @author Andreas Gohr <andi@splitbrain.org> 711 */ 712function dbglog($msg){ 713 global $conf; 714 $file = $conf['cachedir'].'/debug.log'; 715 $fh = fopen($file,'a'); 716 if($fh){ 717 fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 718 fclose($fh); 719 } 720} 721 722/** 723 * Saves a wikitext by calling io_writeWikiPage 724 * 725 * @author Andreas Gohr <andi@splitbrain.org> 726 * @author Ben Coburn <btcoburn@silicodon.net> 727 */ 728function saveWikiText($id,$text,$summary,$minor=false){ 729 global $conf; 730 global $lang; 731 global $REV; 732 // ignore if no changes were made 733 if($text == rawWiki($id,'')){ 734 return; 735 } 736 737 $file = wikiFN($id); 738 $old = saveOldRevision($id); 739 $wasRemoved = empty($text); 740 $wasCreated = !@file_exists($file); 741 $wasReverted = ($REV==true); 742 $newRev = false; 743 744 if ($wasRemoved){ 745 // pre-save deleted revision 746 @touch($file); 747 $newRev = saveOldRevision($id); 748 // remove empty file 749 @unlink($file); 750 // remove old meta info... 751 $mfiles = metaFiles($id); 752 $changelog = metaFN($id, '.changes'); 753 foreach ($mfiles as $mfile) { 754 // but keep per-page changelog to preserve page history 755 if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); } 756 } 757 $del = true; 758 // autoset summary on deletion 759 if(empty($summary)) $summary = $lang['deleted']; 760 // remove empty namespaces 761 io_sweepNS($id, 'datadir'); 762 io_sweepNS($id, 'mediadir'); 763 }else{ 764 // save file (namespace dir is created in io_writeWikiPage) 765 io_writeWikiPage($file, $text, $id); 766 $newRev = @filemtime($file); 767 $del = false; 768 } 769 770 // select changelog line type 771 $extra = ''; 772 $type = 'E'; 773 if ($wasReverted) { 774 $type = 'R'; 775 $extra = $REV; 776 } 777 else if ($wasCreated) { $type = 'C'; } 778 else if ($wasRemoved) { $type = 'D'; } 779 else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users 780 781 addLogEntry($newRev, $id, $type, $summary, $extra); 782 // send notify mails 783 notify($id,'admin',$old,$summary,$minor); 784 notify($id,'subscribers',$old,$summary,$minor); 785 786 //purge cache on add by updating the purgefile 787 if($conf['purgeonadd'] && (!$old || $del)){ 788 io_saveFile($conf['cachedir'].'/purgefile',time()); 789 } 790} 791 792/** 793 * moves the current version to the attic and returns its 794 * revision date 795 * 796 * @author Andreas Gohr <andi@splitbrain.org> 797 */ 798function saveOldRevision($id){ 799 global $conf; 800 $oldf = wikiFN($id); 801 if(!@file_exists($oldf)) return ''; 802 $date = filemtime($oldf); 803 $newf = wikiFN($id,$date); 804 io_writeWikiPage($newf, rawWiki($id), $id, $date); 805 return $date; 806} 807 808/** 809 * Sends a notify mail on page change 810 * 811 * @param string $id The changed page 812 * @param string $who Who to notify (admin|subscribers) 813 * @param int $rev Old page revision 814 * @param string $summary What changed 815 * @param boolean $minor Is this a minor edit? 816 * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 817 * 818 * @author Andreas Gohr <andi@splitbrain.org> 819 */ 820function notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ 821 global $lang; 822 global $conf; 823 824 // decide if there is something to do 825 if($who == 'admin'){ 826 if(empty($conf['notify'])) return; //notify enabled? 827 $text = rawLocale('mailtext'); 828 $to = $conf['notify']; 829 $bcc = ''; 830 }elseif($who == 'subscribers'){ 831 if(!$conf['subscribers']) return; //subscribers enabled? 832 if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors 833 $bcc = subscriber_addresslist($id); 834 if(empty($bcc)) return; 835 $to = ''; 836 $text = rawLocale('subscribermail'); 837 }elseif($who == 'register'){ 838 if(empty($conf['registernotify'])) return; 839 $text = rawLocale('registermail'); 840 $to = $conf['registernotify']; 841 $bcc = ''; 842 }else{ 843 return; //just to be safe 844 } 845 846 $text = str_replace('@DATE@',date($conf['dformat']),$text); 847 $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 848 $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 849 $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 850 $text = str_replace('@NEWPAGE@',wl($id,'',true),$text); 851 $text = str_replace('@PAGE@',$id,$text); 852 $text = str_replace('@TITLE@',$conf['title'],$text); 853 $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 854 $text = str_replace('@SUMMARY@',$summary,$text); 855 $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 856 857 foreach ($replace as $key => $substitution) { 858 $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 859 } 860 861 if($who == 'register'){ 862 $subject = $lang['mail_new_user'].' '.$summary; 863 }elseif($rev){ 864 $subject = $lang['mail_changed'].' '.$id; 865 $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text); 866 require_once(DOKU_INC.'inc/DifferenceEngine.php'); 867 $df = new Diff(split("\n",rawWiki($id,$rev)), 868 split("\n",rawWiki($id))); 869 $dformat = new UnifiedDiffFormatter(); 870 $diff = $dformat->format($df); 871 }else{ 872 $subject=$lang['mail_newpage'].' '.$id; 873 $text = str_replace('@OLDPAGE@','none',$text); 874 $diff = rawWiki($id); 875 } 876 $text = str_replace('@DIFF@',$diff,$text); 877 $subject = '['.$conf['title'].'] '.$subject; 878 879 mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc); 880} 881 882/** 883 * extracts the query from a google referer 884 * 885 * @todo should be more generic and support yahoo et al 886 * @author Andreas Gohr <andi@splitbrain.org> 887 */ 888function getGoogleQuery(){ 889 $url = parse_url($_SERVER['HTTP_REFERER']); 890 if(!$url) return ''; 891 892 if(!preg_match("#google\.#i",$url['host'])) return ''; 893 $query = array(); 894 parse_str($url['query'],$query); 895 896 return $query['q']; 897} 898 899/** 900 * Try to set correct locale 901 * 902 * @deprecated No longer used 903 * @author Andreas Gohr <andi@splitbrain.org> 904 */ 905function setCorrectLocale(){ 906 global $conf; 907 global $lang; 908 909 $enc = strtoupper($lang['encoding']); 910 foreach ($lang['locales'] as $loc){ 911 //try locale 912 if(@setlocale(LC_ALL,$loc)) return; 913 //try loceale with encoding 914 if(@setlocale(LC_ALL,"$loc.$enc")) return; 915 } 916 //still here? try to set from environment 917 @setlocale(LC_ALL,""); 918} 919 920/** 921 * Return the human readable size of a file 922 * 923 * @param int $size A file size 924 * @param int $dec A number of decimal places 925 * @author Martin Benjamin <b.martin@cybernet.ch> 926 * @author Aidan Lister <aidan@php.net> 927 * @version 1.0.0 928 */ 929function filesize_h($size, $dec = 1){ 930 $sizes = array('B', 'KB', 'MB', 'GB'); 931 $count = count($sizes); 932 $i = 0; 933 934 while ($size >= 1024 && ($i < $count - 1)) { 935 $size /= 1024; 936 $i++; 937 } 938 939 return round($size, $dec) . ' ' . $sizes[$i]; 940} 941 942/** 943 * return an obfuscated email address in line with $conf['mailguard'] setting 944 * 945 * @author Harry Fuecks <hfuecks@gmail.com> 946 * @author Christopher Smith <chris@jalakai.co.uk> 947 */ 948function obfuscate($email) { 949 global $conf; 950 951 switch ($conf['mailguard']) { 952 case 'visible' : 953 $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 954 return strtr($email, $obfuscate); 955 956 case 'hex' : 957 $encode = ''; 958 for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';'; 959 return $encode; 960 961 case 'none' : 962 default : 963 return $email; 964 } 965} 966 967/** 968 * Return DokuWikis version 969 * 970 * @author Andreas Gohr <andi@splitbrain.org> 971 */ 972function getVersion(){ 973 //import version string 974 if(@file_exists('VERSION')){ 975 //official release 976 return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION')); 977 }elseif(is_dir('_darcs')){ 978 //darcs checkout 979 $inv = file('_darcs/inventory'); 980 $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv); 981 $cur = array_pop($inv); 982 preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 983 return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 984 }else{ 985 return 'snapshot?'; 986 } 987} 988 989/** 990 * Run a few sanity checks 991 * 992 * @author Andreas Gohr <andi@splitbrain.org> 993 */ 994function check(){ 995 global $conf; 996 global $INFO; 997 998 msg('DokuWiki version: '.getVersion(),1); 999 1000 if(version_compare(phpversion(),'4.3.0','<')){ 1001 msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 1002 }elseif(version_compare(phpversion(),'4.3.10','<')){ 1003 msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 1004 }else{ 1005 msg('PHP version '.phpversion(),1); 1006 } 1007 1008 if(is_writable($conf['changelog'])){ 1009 msg('Changelog is writable',1); 1010 }else{ 1011 if (@file_exists($conf['changelog'])) { 1012 msg('Changelog is not writable',-1); 1013 } 1014 } 1015 1016 if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 1017 msg('Old changelog exists.', 0); 1018 } 1019 1020 if (@file_exists($conf['changelog'].'_failed')) { 1021 msg('Importing old changelog failed.', -1); 1022 } else if (@file_exists($conf['changelog'].'_importing')) { 1023 msg('Importing old changelog now.', 0); 1024 } else if (@file_exists($conf['changelog'].'_import_ok')) { 1025 msg('Old changelog imported.', 1); 1026 if (!plugin_isdisabled('importoldchangelog')) { 1027 msg('Importoldchangelog plugin not disabled after import.', -1); 1028 } 1029 } 1030 1031 if(is_writable($conf['datadir'])){ 1032 msg('Datadir is writable',1); 1033 }else{ 1034 msg('Datadir is not writable',-1); 1035 } 1036 1037 if(is_writable($conf['olddir'])){ 1038 msg('Attic is writable',1); 1039 }else{ 1040 msg('Attic is not writable',-1); 1041 } 1042 1043 if(is_writable($conf['mediadir'])){ 1044 msg('Mediadir is writable',1); 1045 }else{ 1046 msg('Mediadir is not writable',-1); 1047 } 1048 1049 if(is_writable($conf['cachedir'])){ 1050 msg('Cachedir is writable',1); 1051 }else{ 1052 msg('Cachedir is not writable',-1); 1053 } 1054 1055 if(is_writable($conf['lockdir'])){ 1056 msg('Lockdir is writable',1); 1057 }else{ 1058 msg('Lockdir is not writable',-1); 1059 } 1060 1061 if(is_writable(DOKU_CONF.'users.auth.php')){ 1062 msg('conf/users.auth.php is writable',1); 1063 }else{ 1064 msg('conf/users.auth.php is not writable',0); 1065 } 1066 1067 if(function_exists('mb_strpos')){ 1068 if(defined('UTF8_NOMBSTRING')){ 1069 msg('mb_string extension is available but will not be used',0); 1070 }else{ 1071 msg('mb_string extension is available and will be used',1); 1072 } 1073 }else{ 1074 msg('mb_string extension not available - PHP only replacements will be used',0); 1075 } 1076 1077 if($conf['allowdebug']){ 1078 msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 1079 }else{ 1080 msg('Debugging support is disabled',1); 1081 } 1082 1083 msg('Your current permission for this page is '.$INFO['perm'],0); 1084 1085 if(is_writable($INFO['filepath'])){ 1086 msg('The current page is writable by the webserver',0); 1087 }else{ 1088 msg('The current page is not writable by the webserver',0); 1089 } 1090 1091 if($INFO['writable']){ 1092 msg('The current page is writable by you',0); 1093 }else{ 1094 msg('The current page is not writable you',0); 1095 } 1096} 1097 1098/** 1099 * Let us know if a user is tracking a page 1100 * 1101 * @author Andreas Gohr <andi@splitbrain.org> 1102 */ 1103function is_subscribed($id,$uid){ 1104 $file=metaFN($id,'.mlist'); 1105 if (@file_exists($file)) { 1106 $mlist = file($file); 1107 $pos = array_search($uid."\n",$mlist); 1108 return is_int($pos); 1109 } 1110 1111 return false; 1112} 1113 1114/** 1115 * Return a string with the email addresses of all the 1116 * users subscribed to a page 1117 * 1118 * @author Steven Danz <steven-danz@kc.rr.com> 1119 */ 1120function subscriber_addresslist($id){ 1121 global $conf; 1122 global $auth; 1123 1124 $emails = ''; 1125 1126 if (!$conf['subscribers']) return; 1127 1128 $mlist = array(); 1129 $file=metaFN($id,'.mlist'); 1130 if (@file_exists($file)) { 1131 $mlist = file($file); 1132 } 1133 if(count($mlist) > 0) { 1134 foreach ($mlist as $who) { 1135 $who = rtrim($who); 1136 $info = $auth->getUserData($who); 1137 $level = auth_aclcheck($id,$who,$info['grps']); 1138 if ($level >= AUTH_READ) { 1139 if (strcasecmp($info['mail'],$conf['notify']) != 0) { 1140 if (empty($emails)) { 1141 $emails = $info['mail']; 1142 } else { 1143 $emails = "$emails,".$info['mail']; 1144 } 1145 } 1146 } 1147 } 1148 } 1149 1150 return $emails; 1151} 1152 1153/** 1154 * Removes quoting backslashes 1155 * 1156 * @author Andreas Gohr <andi@splitbrain.org> 1157 */ 1158function unslash($string,$char="'"){ 1159 return str_replace('\\'.$char,$char,$string); 1160} 1161 1162//Setup VIM: ex: et ts=2 enc=utf-8 : 1163