1<? 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 require_once("conf/dokuwiki.php"); 10 require_once("inc/io.php"); 11 require_once('inc/utf8.php'); 12 13 //set up error reporting to sane values 14 error_reporting(E_ALL ^ E_NOTICE); 15 16 //make session rewrites XHTML compliant 17 ini_set('arg_separator.output', '&'); 18 19 //init session 20 session_name("DokuWiki"); 21 session_start(); 22 23 //kill magic quotes 24 if (get_magic_quotes_gpc()) { 25 if (!empty($_GET)) remove_magic_quotes($_GET); 26 if (!empty($_POST)) remove_magic_quotes($_POST); 27 if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE); 28 if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST); 29 if (!empty($_SESSION)) remove_magic_quotes($_SESSION); 30 ini_set('magic_quotes_gpc', 0); 31 } 32 set_magic_quotes_runtime(0); 33 ini_set('magic_quotes_sybase',0); 34 35 //disable gzip if not available 36 if($conf['usegzip'] && !function_exists('gzopen')){ 37 $conf['usegzip'] = 0; 38 } 39 40/** 41 * remove magic quotes recursivly 42 * 43 * @author Andreas Gohr <andi@splitbrain.org> 44 */ 45function remove_magic_quotes(&$array) { 46 foreach (array_keys($array) as $key) { 47 if (is_array($array[$key])) { 48 remove_magic_quotes($array[$key]); 49 }else { 50 $array[$key] = stripslashes($array[$key]); 51 } 52 } 53} 54 55/** 56 * Returns the full absolute URL to the directory where 57 * DokuWiki is installed in (includes a trailing slash) 58 * 59 * @author Andreas Gohr <andi@splitbrain.org> 60 */ 61function getBaseURL($abs=false){ 62 global $conf; 63 //if canonical url enabled always return absolute 64 if($conf['canonical']) $abs = true; 65 66 //relative URLs are easy 67 if(!$abs){ 68 $dir = dirname($_SERVER['PHP_SELF']).'/'; 69 $dir = preg_replace('#//#','/',$dir); 70 $dir = preg_replace('#\/$#','/',$dir); #bugfix for weird WIN behaviour 71 return $dir; 72 } 73 74 $port = ':'.$_SERVER['SERVER_PORT']; 75 //remove port from hostheader as sent by IE 76 $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']); 77 78 // see if HTTPS is enabled - apache leaves this empty when not available, 79 // IIS sets it to 'off', 'false' and 'disabled' are just guessing 80 if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){ 81 $proto = 'http://'; 82 if ($_SERVER['SERVER_PORT'] == '80') { 83 $port=''; 84 } 85 }else{ 86 $proto = 'https://'; 87 if ($_SERVER['SERVER_PORT'] == '443') { 88 $port=''; 89 } 90 } 91 $dir = (dirname($_SERVER['PHP_SELF'])).'/'; 92 $dir = preg_replace('#//#','/',$dir); 93 $dir = preg_replace('#\/$#','/',$dir); #bugfix for weird WIN behaviour 94 95 return $proto.$host.$port.$dir; 96} 97 98/** 99 * Return info about the current document as associative 100 * array. 101 * 102 * @author Andreas Gohr <andi@splitbrain.org> 103 */ 104function pageinfo(){ 105 global $ID; 106 global $REV; 107 global $USERINFO; 108 global $conf; 109 110 if($_SERVER['REMOTE_USER']){ 111 $info['user'] = $_SERVER['REMOTE_USER']; 112 $info['userinfo'] = $USERINFO; 113 $info['perm'] = auth_quickaclcheck($ID); 114 }else{ 115 $info['user'] = ''; 116 $info['perm'] = auth_aclcheck($ID,'',null); 117 } 118 119 $info['namespace'] = getNS($ID); 120 $info['locked'] = checklock($ID); 121 $info['filepath'] = realpath(wikiFN($ID,$REV)); 122 $info['exists'] = @file_exists($info['filepath']); 123 if($REV && !$info['exists']){ 124 //check if current revision was meant 125 $cur = wikiFN($ID); 126 if(@file_exists($cur) && (@filemtime($cur) == $REV)){ 127 $info['filepath'] = realpath($cur); 128 $info['exists'] = true; 129 $REV = ''; 130 } 131 } 132 if($info['exists']){ 133 $info['writable'] = (is_writable($info['filepath']) && 134 ($info['perm'] >= AUTH_EDIT)); 135 }else{ 136 $info['writable'] = ($info['perm'] >= AUTH_CREATE); 137 } 138 $info['editable'] = ($info['writable'] && empty($info['lock'])); 139 $info['lastmod'] = @filemtime($info['filepath']); 140 141 return $info; 142} 143 144/** 145 * print a message 146 * 147 * If HTTP headers were not sent yet the message is added 148 * to the global message array else it's printed directly 149 * using html_msgarea() 150 * 151 * 152 * Levels can be: 153 * 154 * -1 error 155 * 0 info 156 * 1 success 157 * 158 * @author Andreas Gohr <andi@splitbrain.org> 159 * @see html_msgarea 160 */ 161function msg($message,$lvl=0){ 162 global $MSG; 163 $errors[-1] = 'error'; 164 $errors[0] = 'info'; 165 $errors[1] = 'success'; 166 167 if(!headers_sent){ 168 if(!isset($MSG)) $MSG = array(); 169 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 170 }else{ 171 $MSG = array(); 172 $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 173 html_msgarea(); 174 } 175} 176 177/** 178 * This builds the breadcrumb trail and returns it as array 179 * 180 * @author Andreas Gohr <andi@splitbrain.org> 181 */ 182function breadcrumbs(){ 183 global $ID; 184 global $ACT; 185 global $conf; 186 $crumbs = $_SESSION[$conf['title']]['bc']; 187 188 //first visit? 189 if (!is_array($crumbs)){ 190 $crumbs = array(); 191 } 192 //we only save on show and existing wiki documents 193 if($ACT != 'show' || !@file_exists(wikiFN($ID))){ 194 $_SESSION[$conf['title']]['bc'] = $crumbs; 195 return $crumbs; 196 } 197 //remove ID from array 198 $pos = array_search($ID,$crumbs); 199 if($pos !== false && $pos !== null){ 200 array_splice($crumbs,$pos,1); 201 } 202 203 //add to array 204 $crumbs[] =$ID; 205 //reduce size 206 while(count($crumbs) > $conf['breadcrumbs']){ 207 array_shift($crumbs); 208 } 209 //save to session 210 $_SESSION[$conf['title']]['bc'] = $crumbs; 211 return $crumbs; 212} 213 214/** 215 * Filter for page IDs 216 * 217 * This is run on a ID before it is outputted somewhere 218 * currently used to replace the colon with something else 219 * on Windows systems and to have proper URL encoding 220 * 221 * Urlencoding is ommitted when the second parameter is false 222 * 223 * @author Andreas Gohr <andi@splitbrain.org> 224 */ 225function idfilter($id,$ue=true){ 226 global $conf; 227 if ($conf['useslash'] && $conf['userewrite']){ 228 $id = strtr($id,':','/'); 229 }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 230 $conf['userewrite']) { 231 $id = strtr($id,':',';'); 232 } 233 if($ue){ 234 $id = urlencode($id); 235 $id = str_replace('%3A',':',$id); //keep as colon 236 $id = str_replace('%2F','/',$id); //keep as slash 237 } 238 return $id; 239} 240 241/** 242 * This builds a link to a wikipage (using getBaseURL) 243 * 244 * @author Andreas Gohr <andi@splitbrain.org> 245 */ 246function wl($id='',$more='',$script='doku.php',$canonical=false){ 247 global $conf; 248 $more = str_replace(',','&',$more); 249 250 $id = idfilter($id); 251 $xlink = getBaseURL($canonical); 252 253 if(!$conf['userewrite']){ 254 $xlink .= $script; 255 $xlink .= '?id='.$id; 256 if($more) $xlink .= '&'.$more; 257 }else{ 258 $xlink .= $id; 259 if($more) $xlink .= '?'.$more; 260 } 261 262 return $xlink; 263} 264 265/** 266 * Just builds a link to a script 267 * 268 * @author Andreas Gohr <andi@splitbrain.org> 269 */ 270function script($script='doku.php'){ 271 $link = getBaseURL(); 272 $link .= $script; 273 return $link; 274} 275 276/** 277 * Return namespacepart of a wiki ID 278 * 279 * @author Andreas Gohr <andi@splitbrain.org> 280 */ 281function getNS($id){ 282 if(strpos($id,':')!==false){ 283 return substr($id,0,strrpos($id,':')); 284 } 285 return false; 286} 287 288/** 289 * Returns the ID without the namespace 290 * 291 * @author Andreas Gohr <andi@splitbrain.org> 292 */ 293function noNS($id){ 294 return preg_replace('/.*:/','',$id); 295} 296 297/** 298 * Spamcheck against wordlist 299 * 300 * Checks the wikitext against a list of blocked expressions 301 * returns true if the text contains any bad words 302 * 303 * @author Andreas Gohr <andi@splitbrain.org> 304 */ 305function checkwordblock(){ 306 global $TEXT; 307 global $conf; 308 309 if(!$conf['usewordblock']) return false; 310 311 $blocks = file('conf/wordblock.conf'); 312 $re = array(); 313 #build regexp from blocks 314 foreach($blocks as $block){ 315 $block = preg_replace('/#.*$/','',$block); 316 $block = trim($block); 317 if(empty($block)) continue; 318 $re[] = $block; 319 } 320 if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true; 321 return false; 322} 323 324/** 325 * Return the IP of the client 326 * 327 * Honours X-Forwarded-For Proxy Headers 328 * 329 * @author Andreas Gohr <andi@splitbrain.org> 330 */ 331function clientIP(){ 332 $my = $_SERVER['REMOTE_ADDR']; 333 if($_SERVER['HTTP_X_FORWARDED_FOR']){ 334 $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')'; 335 } 336 return $my; 337} 338 339/** 340 * Checks if a given page is currently locked. 341 * 342 * removes stale lockfiles 343 * 344 * @author Andreas Gohr <andi@splitbrain.org> 345 */ 346function checklock($id){ 347 global $conf; 348 $lock = wikiFN($id).'.lock'; 349 350 //no lockfile 351 if(!@file_exists($lock)) return false; 352 353 //lockfile expired 354 if((time() - filemtime($lock)) > $conf['locktime']){ 355 unlink($lock); 356 return false; 357 } 358 359 //my own lock 360 $ip = io_readFile($lock); 361 if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 362 return false; 363 } 364 365 return $ip; 366} 367 368/** 369 * Lock a page for editing 370 * 371 * @author Andreas Gohr <andi@splitbrain.org> 372 */ 373function lock($id){ 374 $lock = wikiFN($id).'.lock'; 375 if($_SERVER['REMOTE_USER']){ 376 io_saveFile($lock,$_SERVER['REMOTE_USER']); 377 }else{ 378 io_saveFile($lock,clientIP()); 379 } 380} 381 382/** 383 * Unlock a page if it was locked by the user 384 * 385 * @author Andreas Gohr <andi@splitbrain.org> 386 * @return bool true if a lock was removed 387 */ 388function unlock($id){ 389 $lock = wikiFN($id).'.lock'; 390 if(@file_exists($lock)){ 391 $ip = io_readFile($lock); 392 if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 393 @unlink($lock); 394 return true; 395 } 396 } 397 return false; 398} 399 400/** 401 * Remove unwanted chars from ID 402 * 403 * Cleans a given ID to only use allowed characters. Accented characters are 404 * converted to unaccented ones 405 * 406 * @author Andreas Gohr <andi@splitbrain.org> 407 */ 408function cleanID($id){ 409 global $conf; 410 global $lang; 411 $id = trim($id); 412 $id = utf8_strtolower($id); 413 414 //alternative namespace seperator 415 $id = strtr($id,';',':'); 416 if($conf['useslash']) $id = strtr($id,'/',':'); 417 418 //FIXME use config to ask for deaccenting 419 $id = utf8_deaccent($id,-1); 420 421 //remove specials (only ascii specials are removed) 422 $id = preg_replace('#[ !"§$%&()\[\]{}\\?`\'\#~*+=,<>\|^°@µ¹²³¼½¬]#u','_',$id); 423 424/* DELETEME legacy code 425 if(!$conf['localnames']){ 426 if($lang['encoding'] == 'iso-8859-15'){ 427 // replace accented chars with unaccented ones 428 // this may look strange on your terminal - just don't touch 429 $id = strtr( 430 strtr($id, 431 'ÀÁÂÃÅÇÈÉÊËÌÍÎÏÑÒÓÔÕØÙÚÛÝàáâãåçèéêëìíîïñòóôõøùúûýÿ', 432 'szszyaaaaaceeeeiiiinooooouuuyaaaaaceeeeiiiinooooouuuyy'), 433 array('Þ' => 'th', 'þ' => 'th', 'Ð' => 'dh', 'ð' => 'dh', 'ß' => 'ss', 434 '' => 'oe', '' => 'oe', 'Æ' => 'ae', 'æ' => 'ae', 'µ' => 'u', 435 'ü' => 'ue', 'ö' => 'oe', 'ä' => 'ae', 'Ü' => 'ue', 'Ö' => 'ö', 436 'Ä' => 'ae')); 437 } 438 $WORD = 'a-z'; 439 }else{ 440 $WORD = '\w'; 441 } 442 //special chars left will be converted to _ 443 $id = preg_replace('#[^'.$WORD.'0-9:\-\.]#','_',$id); 444*/ 445 446 //clean up 447 $id = preg_replace('#__#','_',$id); 448 $id = preg_replace('#:+#',':',$id); 449 $id = trim($id,':._-'); 450 $id = preg_replace('#:[:\._\-]+#',':',$id); 451 452 return($id); 453} 454 455/** 456 * returns the full path to the datafile specified by ID and 457 * optional revision 458 * 459 * The filename is URL encoded to protect Unicode chars 460 * 461 * @author Andreas Gohr <andi@splitbrain.org> 462 */ 463function wikiFN($id,$rev=''){ 464 global $conf; 465 $id = cleanID($id); 466 $id = str_replace(':','/',$id); 467 if(empty($rev)){ 468 $fn = $conf['datadir'].'/'.$id.'.txt'; 469 }else{ 470 $fn = $conf['olddir'].'/'.$id.'.'.$rev.'.txt'; 471 if($conf['usegzip'] && !@file_exists($fn)){ 472 //return gzip if enabled and plaintext doesn't exist 473 $fn .= '.gz'; 474 } 475 } 476 $fn = utf8_encodeFN($fn); 477 return $fn; 478} 479 480/** 481 * Returns the full filepath to a localized textfile if local 482 * version isn't found the english one is returned 483 * 484 * @author Andreas Gohr <andi@splitbrain.org> 485 */ 486function localeFN($id){ 487 global $conf; 488 $file = './lang/'.$conf['lang'].'/'.$id.'.txt'; 489 if(!@file_exists($file)){ 490 //fall back to english 491 $file = './lang/en/'.$id.'.txt'; 492 } 493 return cleanText($file); 494} 495 496/** 497 * convert line ending to unix format 498 * 499 * @see formText() for 2crlf conversion 500 * @author Andreas Gohr <andi@splitbrain.org> 501 */ 502function cleanText($text){ 503 $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 504 return $text; 505} 506 507/** 508 * Prepares text for print in Webforms by encoding special chars. 509 * It also converts line endings to Windows format which is 510 * pseudo standard for webforms. 511 * 512 * @see cleanText() for 2unix conversion 513 * @author Andreas Gohr <andi@splitbrain.org> 514 */ 515function formText($text){ 516 $text = preg_replace("/\012/","\015\012",$text); 517 return htmlspecialchars($text); 518} 519 520/** 521 * Returns the specified local text in parsed format 522 * 523 * @author Andreas Gohr <andi@splitbrain.org> 524 */ 525function parsedLocale($id){ 526 //disable section editing 527 global $parser; 528 $se = $parser['secedit']; 529 $parser['secedit'] = false; 530 //fetch parsed locale 531 $html = io_cacheParse(localeFN($id)); 532 //reset section editing 533 $parser['secedit'] = $se; 534 return $html; 535} 536 537/** 538 * Returns the specified local text in raw format 539 * 540 * @author Andreas Gohr <andi@splitbrain.org> 541 */ 542function rawLocale($id){ 543 return io_readFile(localeFN($id)); 544} 545 546 547/** 548 * Returns the parsed Wikitext for the given id and revision. 549 * 550 * If $excuse is true an explanation is returned if the file 551 * wasn't found 552 * 553 * @author Andreas Gohr <andi@splitbrain.org> 554 */ 555function parsedWiki($id,$rev='',$excuse=true){ 556 $file = wikiFN($id,$rev); 557 $ret = ''; 558 559 //ensure $id is in global $ID (needed for parsing) 560 global $ID; 561 $ID = $id; 562 563 if($rev){ 564 if(@file_exists($file)){ 565 $ret = parse(io_readFile($file)); 566 }elseif($excuse){ 567 $ret = parsedLocale('norev'); 568 } 569 }else{ 570 if(@file_exists($file)){ 571 $ret = io_cacheParse($file); 572 }elseif($excuse){ 573 $ret = parsedLocale('newpage'); 574 } 575 } 576 return $ret; 577} 578 579/** 580 * Returns the raw WikiText 581 * 582 * @author Andreas Gohr <andi@splitbrain.org> 583 */ 584function rawWiki($id,$rev=''){ 585 return io_readFile(wikiFN($id,$rev)); 586} 587 588/** 589 * Returns the raw Wiki Text in three slices. 590 * 591 * The range parameter needs to have the form "from-to" 592 * and gives the range of the section. 593 * The returned order is prefix, section and suffix. 594 * 595 * @author Andreas Gohr <andi@splitbrain.org> 596 */ 597function rawWikiSlices($range,$id,$rev=''){ 598 list($from,$to) = split('-',$range,2); 599 $text = io_readFile(wikiFN($id,$rev)); 600 $text = split("\n",$text); 601 if(!$from) $from = 0; 602 if(!$to) $to = count($text); 603 604 $slices[0] = join("\n",array_slice($text,0,$from)); 605 $slices[1] = join("\n",array_slice($text,$from,$to + 1 - $from)); 606 $slices[2] = join("\n",array_slice($text,$to+1)); 607 608 return $slices; 609} 610 611/** 612 * Joins wiki text slices 613 * 614 * function to join the text slices with correct lineendings again. 615 * When the pretty parameter is set to true it adds additional empty 616 * lines between sections if needed (used on saving). 617 * 618 * @author Andreas Gohr <andi@splitbrain.org> 619 */ 620function con($pre,$text,$suf,$pretty=false){ 621 622 if($pretty){ 623 if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 624 if($suf && substr($text,-1) != "\n") $text .= "\n"; 625 } 626 627 if($pre) $pre .= "\n"; 628 if($suf) $text .= "\n"; 629 return $pre.$text.$suf; 630} 631 632/** 633 * print debug messages 634 * 635 * little function to print the content of a var 636 * 637 * @author Andreas Gohr <andi@splitbrain.org> 638 */ 639function dbg($msg,$hidden=false){ 640 (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 641 print_r($msg); 642 (!$hidden) ? print '</pre>' : print "\n-->"; 643} 644 645/** 646 * Add's an entry to the changelog 647 * 648 * @author Andreas Gohr <andi@splitbrain.org> 649 */ 650function addLogEntry($id,$summary=""){ 651 global $conf; 652 $id = cleanID($id); 653 $date = time(); 654 $remote = $_SERVER['REMOTE_ADDR']; 655 $user = $_SERVER['REMOTE_USER']; 656 657 $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n"; 658 659 $fh = fopen($conf['changelog'],'a'); 660 if($fh){ 661 fwrite($fh,$logline); 662 fclose($fh); 663 } 664} 665 666/** 667 * returns an array of recently changed files using the 668 * changelog 669 * 670 * @author Andreas Gohr <andi@splitbrain.org> 671 */ 672function getRecents($num=0,$incdel=false){ 673 global $conf; 674 $recent = array(); 675 if(!$num) $num = $conf['recent']; 676 677 $loglines = file($conf['changelog']); 678 rsort($loglines); //reverse sort on timestamp 679 680 foreach ($loglines as $line){ 681 $line = rtrim($line); //remove newline 682 if(empty($line)) continue; //skip empty lines 683 $info = split("\t",$line); //split into parts 684 //add id if not in yet and file still exists and is allowed to read 685 if(!$recent[$info[2]] && 686 (@file_exists(wikiFN($info[2])) || $incdel) && 687 (auth_quickaclcheck($info[2]) >= AUTH_READ) 688 ){ 689 $recent[$info[2]]['date'] = $info[0]; 690 $recent[$info[2]]['ip'] = $info[1]; 691 $recent[$info[2]]['user'] = $info[3]; 692 $recent[$info[2]]['sum'] = $info[4]; 693 $recent[$info[2]]['del'] = !@file_exists(wikiFN($info[2])); 694 } 695 if(count($recent) >= $num){ 696 break; //finish if enough items found 697 } 698 } 699 return $recent; 700} 701 702/** 703 * Saves a wikitext by calling io_saveFile 704 * 705 * @author Andreas Gohr <andi@splitbrain.org> 706 */ 707function saveWikiText($id,$text,$summary){ 708 global $conf; 709 global $lang; 710 umask($conf['umask']); 711 // ignore if no changes were made 712 if($text == rawWiki($id,'')){ 713 return; 714 } 715 716 $file = wikiFN($id); 717 $old = saveOldRevision($id); 718 719 if (empty($text)){ 720 // remove empty files 721 @unlink($file); 722 $del = true; 723 $summary = $lang['deleted']; //autoset summary on deletion 724 }else{ 725 // save file (datadir is created in io_saveFile) 726 io_saveFile($file,$text); 727 $del = false; 728 } 729 730 addLogEntry($id,$summary); 731 notify($id,$old,$summary); 732 733 //purge cache on add by updating the purgefile 734 if($conf['purgeonadd'] && (!$old || $del)){ 735 io_saveFile($conf['datadir'].'/.cache/purgefile',time()); 736 } 737} 738 739/** 740 * moves the current version to the attic and returns its 741 * revision date 742 * 743 * @author Andreas Gohr <andi@splitbrain.org> 744 */ 745function saveOldRevision($id){ 746 global $conf; 747 umask($conf['umask']); 748 $oldf = wikiFN($id); 749 if(!@file_exists($oldf)) return ''; 750 $date = filemtime($oldf); 751 $newf = wikiFN($id,$date); 752 if(substr($newf,-3)=='.gz'){ 753 io_saveFile($newf,rawWiki($id)); 754 }else{ 755 io_makeFileDir($newf); 756 copy($oldf, $newf); 757 } 758 return $date; 759} 760 761/** 762 * Sends a notify mail to the wikiadmin when a page was 763 * changed 764 * 765 * @author Andreas Gohr <andi@splitbrain.org> 766 */ 767function notify($id,$rev="",$summary=""){ 768 global $lang; 769 global $conf; 770 $hdrs =''; 771 if(empty($conf['notify'])) return; //notify enabled? 772 773 $text = rawLocale('mailtext'); 774 $text = str_replace('@DATE@',date($conf['dformat']),$text); 775 $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 776 $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 777 $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 778 $text = str_replace('@NEWPAGE@',wl($id,'','',true),$text); 779 $text = str_replace('@DOKUWIKIURL@',getBaseURL(true),$text); 780 $text = str_replace('@SUMMARY@',$summary,$text); 781 782 if($rev){ 783 $subject = $lang['mail_changed'].' '.$id; 784 $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",'',true),$text); 785 require_once("inc/DifferenceEngine.php"); 786 $df = new Diff(split("\n",rawWiki($id,$rev)), 787 split("\n",rawWiki($id))); 788 $dformat = new UnifiedDiffFormatter(); 789 $diff = $dformat->format($df); 790 }else{ 791 $subject=$lang['mail_newpage'].' '.$id; 792 $text = str_replace('@OLDPAGE@','none',$text); 793 $diff = rawWiki($id); 794 } 795 $text = str_replace('@DIFF@',$diff,$text); 796 797 if (!empty($conf['mailfrom'])) { 798 $hdrs = 'From: '.$conf['mailfrom']."\n"; 799 } 800 @mail($conf['notify'],$subject,$text,$hdrs); 801} 802 803/** 804 * Return a list of available page revisons 805 * 806 * @author Andreas Gohr <andi@splitbrain.org> 807 */ 808function getRevisions($id){ 809 $revd = dirname(wikiFN($id,'foo')); 810 $revs = array(); 811 $clid = cleanID($id); 812 if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path 813 814 if (is_dir($revd) && $dh = opendir($revd)) { 815 while (($file = readdir($dh)) !== false) { 816 if (is_dir($revd.'/'.$file)) continue; 817 if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){ 818 $revs[]=$match[1]; 819 } 820 } 821 closedir($dh); 822 } 823 rsort($revs); 824 return $revs; 825} 826 827/** 828 * downloads a file from the net and saves it to the given location 829 * 830 * @author Andreas Gohr <andi@splitbrain.org> 831 */ 832function download($url,$file){ 833 $fp = @fopen($url,"rb"); 834 if(!$fp) return false; 835 836 while(!feof($fp)){ 837 $cont.= fread($fp,1024); 838 } 839 fclose($fp); 840 841 $fp2 = @fopen($file,"w"); 842 if(!$fp2) return false; 843 fwrite($fp2,$cont); 844 fclose($fp2); 845 return true; 846} 847 848/** 849 * extracts the query from a google referer 850 * 851 * @author Andreas Gohr <andi@splitbrain.org> 852 */ 853function getGoogleQuery(){ 854 $url = parse_url($_SERVER['HTTP_REFERER']); 855 856 if(!preg_match("#google\.#i",$url['host'])) return ''; 857 $query = array(); 858 parse_str($url['query'],$query); 859 860 return $query['q']; 861} 862 863/** 864 * Try to set correct locale 865 * 866 * @author Andreas Gohr <andi@splitbrain.org> 867 */ 868function setCorrectLocale(){ 869 global $conf; 870 global $lang; 871 872 $enc = strtoupper($lang['encoding']); 873 foreach ($lang['locales'] as $loc){ 874 //try locale 875 if(@setlocale(LC_ALL,$loc)) return; 876 //try loceale with encoding 877 if(@setlocale(LC_ALL,"$loc.$enc")) return; 878 } 879 //still here? try to set from environment 880 @setlocale(LC_ALL,""); 881} 882 883/** 884 * Return the human readable size of a file 885 * 886 * @param int $size A file size 887 * @param int $dec A number of decimal places 888 * @author Martin Benjamin <b.martin@cybernet.ch> 889 * @author Aidan Lister <aidan@php.net> 890 * @version 1.0.0 891 */ 892function filesize_h($size, $dec = 1) 893{ 894 $sizes = array('B', 'KB', 'MB', 'GB'); 895 $count = count($sizes); 896 $i = 0; 897 898 while ($size >= 1024 && ($i < $count - 1)) { 899 $size /= 1024; 900 $i++; 901 } 902 903 return round($size, $dec) . ' ' . $sizes[$i]; 904} 905 906/** 907 * Run a few sanity checks 908 * 909 * @author Andreas Gohr <andi@splitbrain.org> 910 */ 911function check(){ 912 global $conf; 913 global $INFO; 914 915 if(version_compare(phpversion(),'4.3.0','<')){ 916 msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 917 }elseif(version_compare(phpversion(),'4.3.10','<')){ 918 msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 919 }else{ 920 msg('PHP version '.phpversion(),1); 921 } 922 923 if(is_writable($conf['changelog'])){ 924 msg('Changelog is writable',1); 925 }else{ 926 msg('Changelog is not writable',-1); 927 } 928 929 if(is_writable($conf['datadir'])){ 930 msg('Datadir is writable',1); 931 }else{ 932 msg('Datadir is not writable',-1); 933 } 934 935 if(is_writable($conf['olddir'])){ 936 msg('Attic is writable',1); 937 }else{ 938 msg('Attic is not writable',-1); 939 } 940 941 if(is_writable($conf['mediadir'])){ 942 msg('Mediadir is writable',1); 943 }else{ 944 msg('Mediadir is not writable',-1); 945 } 946 947 if(is_writable('conf/users.auth')){ 948 msg('conf/users.auth is writable',1); 949 }else{ 950 msg('conf/users.auth is not writable',0); 951 } 952 953 if(function_exists('mb_strpos')){ 954 if(defined('UTF8_NOMBSTRING')){ 955 msg('mb_string extension is available but will not be used',0); 956 }else{ 957 msg('mb_string extension is available and will be used',1); 958 } 959 }else{ 960 msg('mb_string extension not available - PHP only replacements will be used',0); 961 } 962 963 msg('Your current permission for this page is '.$INFO['perm'],0); 964 965 if(is_writable($INFO['filepath'])){ 966 msg('The current page is writable by the webserver',0); 967 }else{ 968 msg('The current page is not writable by the webserver',0); 969 } 970 971 if($INFO['writable']){ 972 msg('The current page is writable by you',0); 973 }else{ 974 msg('The current page is not writable you',0); 975 } 976} 977?> 978