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