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