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 existance 625 if(!@file_exists(wikiFN($id))){ 626 if(!$incdel){ 627 return false; 628 }else{ 629 $recent = array(); 630 $recent['del'] = true; 631 } 632 }else{ 633 $recent = array(); 634 $recent['del'] = false; 635 } 636 637 $recent['id'] = $id; 638 $recent['date'] = $dt; 639 $recent['ip'] = $ip; 640 $recent['user'] = $usr; 641 $recent['sum'] = $sum; 642 643 return $recent; 644} 645 646/** 647 * returns an array of recently changed files using the 648 * changelog 649 * 650 * @param int $first number of first entry returned (for paginating 651 * @param int $num return $num entries 652 * @param bool $incdel include deleted pages? 653 * @param string $ns restrict to given namespace 654 * @param bool $subNS include subnamespaces 655 * 656 * @author Andreas Gohr <andi@splitbrain.org> 657 */ 658function getRecents($first,$num,$incdel=false,$ns='',$subNS=true){ 659 global $conf; 660 $recent = array(); 661 $count = 0; 662 663 if(!$num) 664 return $recent; 665 666 if(!@is_readable($conf['changelog'])){ 667 msg($conf['changelog'].' is not readable',-1); 668 return $recent; 669 } 670 671 $fh = fopen($conf['changelog'],'r'); 672 $buf = ''; 673 $csz = 4096; //chunksize 674 fseek($fh,0,SEEK_END); // jump to the end 675 $pos = ftell($fh); // position pointer 676 677 // now read backwards into buffer 678 while($pos > 0){ 679 $pos -= $csz; // seek to previous chunk... 680 if($pos < 0) $pos = 0; // ...or rest of file 681 fseek($fh,$pos); 682 683 $buf = fread($fh,$csz).$buf; // prepend to buffer 684 685 $lines = explode("\n",$buf); // split buffer into lines 686 687 if($pos > 0){ 688 $buf = array_shift($lines); // first one may be still incomplete 689 } 690 691 $cnt = count($lines); 692 if(!$cnt) continue; // no lines yet 693 694 // handle lines 695 for($i = $cnt-1; $i >= 0; $i--){ 696 $rec = _handleRecent($lines[$i],$incdel,$ns,$subNS); 697 if($rec !== false){ 698 if(--$first >= 0) continue; // skip first entries 699 $recent[] = $rec; 700 $count++; 701 702 // break while when we have enough entries 703 if($count >= $num){ 704 $pos = 0; // will break the while loop 705 break; // will break the for loop 706 } 707 } 708 } 709 }// end of while 710 711 fclose($fh); 712 return $recent; 713} 714 715/** 716 * gets additonal informations for a certain pagerevison 717 * from the changelog 718 * 719 * @author Andreas Gohr <andi@splitbrain.org> 720 */ 721function getRevisionInfo($id,$rev){ 722 global $conf; 723 724 if(!$rev) return(null); 725 726 $info = array(); 727 if(!@is_readable($conf['changelog'])){ 728 msg($conf['changelog'].' is not readable',-1); 729 return $recent; 730 } 731 $loglines = file($conf['changelog']); 732 $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines); 733 $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed) 734 $line = split("\t",$loglines[0]); 735 $info['date'] = $line[0]; 736 $info['ip'] = $line[1]; 737 $info['user'] = $line[3]; 738 $info['sum'] = $line[4]; 739 return $info; 740} 741 742/** 743 * Saves a wikitext by calling io_saveFile 744 * 745 * @author Andreas Gohr <andi@splitbrain.org> 746 */ 747function saveWikiText($id,$text,$summary){ 748 global $conf; 749 global $lang; 750 umask($conf['umask']); 751 // ignore if no changes were made 752 if($text == rawWiki($id,'')){ 753 return; 754 } 755 756 $file = wikiFN($id); 757 $old = saveOldRevision($id); 758 759 if (empty($text)){ 760 // remove empty file 761 @unlink($file); 762 // remove any meta info 763 $mfiles = metaFiles($id); 764 foreach ($mfiles as $mfile) { 765 if (file_exists($mfile)) @unlink($mfile); 766 } 767 $del = true; 768 //autoset summary on deletion 769 if(empty($summary)) $summary = $lang['deleted']; 770 //remove empty namespaces 771 io_sweepNS($id); 772 }else{ 773 // save file (datadir is created in io_saveFile) 774 io_saveFile($file,$text); 775 $del = false; 776 } 777 778 addLogEntry(@filemtime($file),$id,$summary); 779 // send notify mails 780 notify($id,'admin',$old,$summary); 781 notify($id,'subscribers',$old,$summary); 782 783 //purge cache on add by updating the purgefile 784 if($conf['purgeonadd'] && (!$old || $del)){ 785 io_saveFile($conf['cachedir'].'/purgefile',time()); 786 } 787} 788 789/** 790 * moves the current version to the attic and returns its 791 * revision date 792 * 793 * @author Andreas Gohr <andi@splitbrain.org> 794 */ 795function saveOldRevision($id){ 796 global $conf; 797 umask($conf['umask']); 798 $oldf = wikiFN($id); 799 if(!@file_exists($oldf)) return ''; 800 $date = filemtime($oldf); 801 $newf = wikiFN($id,$date); 802 if(substr($newf,-3)=='.gz'){ 803 io_saveFile($newf,rawWiki($id)); 804 }else{ 805 io_makeFileDir($newf); 806 copy($oldf, $newf); 807 } 808 return $date; 809} 810 811/** 812 * Sends a notify mail on page change 813 * 814 * @param string $id The changed page 815 * @param string $who Who to notify (admin|subscribers) 816 * @param int $rev Old page revision 817 * @param string $summary What changed 818 * 819 * @author Andreas Gohr <andi@splitbrain.org> 820 */ 821function notify($id,$who,$rev='',$summary=''){ 822 global $lang; 823 global $conf; 824 825 // decide if there is something to do 826 if($who == 'admin'){ 827 if(empty($conf['notify'])) return; //notify enabled? 828 $text = rawLocale('mailtext'); 829 $to = $conf['notify']; 830 $bcc = ''; 831 }elseif($who == 'subscribers'){ 832 if(!$conf['subscribers']) return; //subscribers enabled? 833 $bcc = subscriber_addresslist($id); 834 if(empty($bcc)) return; 835 $to = ''; 836 $text = rawLocale('subscribermail'); 837 }else{ 838 return; //just to be safe 839 } 840 841 $text = str_replace('@DATE@',date($conf['dformat']),$text); 842 $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 843 $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 844 $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 845 $text = str_replace('@NEWPAGE@',wl($id,'',true),$text); 846 $text = str_replace('@PAGE@',$id,$text); 847 $text = str_replace('@TITLE@',$conf['title'],$text); 848 $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 849 $text = str_replace('@SUMMARY@',$summary,$text); 850 $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 851 852 if($rev){ 853 $subject = $lang['mail_changed'].' '.$id; 854 $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text); 855 require_once("inc/DifferenceEngine.php"); 856 $df = new Diff(split("\n",rawWiki($id,$rev)), 857 split("\n",rawWiki($id))); 858 $dformat = new UnifiedDiffFormatter(); 859 $diff = $dformat->format($df); 860 }else{ 861 $subject=$lang['mail_newpage'].' '.$id; 862 $text = str_replace('@OLDPAGE@','none',$text); 863 $diff = rawWiki($id); 864 } 865 $text = str_replace('@DIFF@',$diff,$text); 866 $subject = '['.$conf['title'].'] '.$subject; 867 868 mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc); 869} 870 871/** 872 * Return a list of available page revisons 873 * 874 * @author Andreas Gohr <andi@splitbrain.org> 875 */ 876function getRevisions($id){ 877 $revd = dirname(wikiFN($id,'foo')); 878 $revs = array(); 879 $clid = cleanID($id); 880 if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path 881 $clid = utf8_encodeFN($clid); 882 883 if (is_dir($revd) && $dh = opendir($revd)) { 884 while (($file = readdir($dh)) !== false) { 885 if (is_dir($revd.'/'.$file)) continue; 886 if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){ 887 $revs[]=$match[1]; 888 } 889 } 890 closedir($dh); 891 } 892 rsort($revs); 893 return $revs; 894} 895 896/** 897 * extracts the query from a google referer 898 * 899 * @todo should be more generic and support yahoo et al 900 * @author Andreas Gohr <andi@splitbrain.org> 901 */ 902function getGoogleQuery(){ 903 $url = parse_url($_SERVER['HTTP_REFERER']); 904 if(!$url) return ''; 905 906 if(!preg_match("#google\.#i",$url['host'])) return ''; 907 $query = array(); 908 parse_str($url['query'],$query); 909 910 return $query['q']; 911} 912 913/** 914 * Try to set correct locale 915 * 916 * @deprecated No longer used 917 * @author Andreas Gohr <andi@splitbrain.org> 918 */ 919function setCorrectLocale(){ 920 global $conf; 921 global $lang; 922 923 $enc = strtoupper($lang['encoding']); 924 foreach ($lang['locales'] as $loc){ 925 //try locale 926 if(@setlocale(LC_ALL,$loc)) return; 927 //try loceale with encoding 928 if(@setlocale(LC_ALL,"$loc.$enc")) return; 929 } 930 //still here? try to set from environment 931 @setlocale(LC_ALL,""); 932} 933 934/** 935 * Return the human readable size of a file 936 * 937 * @param int $size A file size 938 * @param int $dec A number of decimal places 939 * @author Martin Benjamin <b.martin@cybernet.ch> 940 * @author Aidan Lister <aidan@php.net> 941 * @version 1.0.0 942 */ 943function filesize_h($size, $dec = 1){ 944 $sizes = array('B', 'KB', 'MB', 'GB'); 945 $count = count($sizes); 946 $i = 0; 947 948 while ($size >= 1024 && ($i < $count - 1)) { 949 $size /= 1024; 950 $i++; 951 } 952 953 return round($size, $dec) . ' ' . $sizes[$i]; 954} 955 956/** 957 * return an obfuscated email address in line with $conf['mailguard'] setting 958 * 959 * @author Harry Fuecks <hfuecks@gmail.com> 960 * @author Christopher Smith <chris@jalakai.co.uk> 961 */ 962function obfuscate($email) { 963 global $conf; 964 965 switch ($conf['mailguard']) { 966 case 'visible' : 967 $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 968 return strtr($email, $obfuscate); 969 970 case 'hex' : 971 $encode = ''; 972 for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';'; 973 return $encode; 974 975 case 'none' : 976 default : 977 return $email; 978 } 979} 980 981/** 982 * Return DokuWikis version 983 * 984 * @author Andreas Gohr <andi@splitbrain.org> 985 */ 986function getVersion(){ 987 //import version string 988 if(@file_exists('VERSION')){ 989 //official release 990 return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION')); 991 }elseif(is_dir('_darcs')){ 992 //darcs checkout 993 $inv = file('_darcs/inventory'); 994 $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv); 995 $cur = array_pop($inv); 996 preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 997 return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 998 }else{ 999 return 'snapshot?'; 1000 } 1001} 1002 1003/** 1004 * Run a few sanity checks 1005 * 1006 * @author Andreas Gohr <andi@splitbrain.org> 1007 */ 1008function check(){ 1009 global $conf; 1010 global $INFO; 1011 1012 msg('DokuWiki version: '.getVersion(),1); 1013 1014 if(version_compare(phpversion(),'4.3.0','<')){ 1015 msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 1016 }elseif(version_compare(phpversion(),'4.3.10','<')){ 1017 msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 1018 }else{ 1019 msg('PHP version '.phpversion(),1); 1020 } 1021 1022 if(is_writable($conf['changelog'])){ 1023 msg('Changelog is writable',1); 1024 }else{ 1025 msg('Changelog is not writable',-1); 1026 } 1027 1028 if(is_writable($conf['datadir'])){ 1029 msg('Datadir is writable',1); 1030 }else{ 1031 msg('Datadir is not writable',-1); 1032 } 1033 1034 if(is_writable($conf['olddir'])){ 1035 msg('Attic is writable',1); 1036 }else{ 1037 msg('Attic is not writable',-1); 1038 } 1039 1040 if(is_writable($conf['mediadir'])){ 1041 msg('Mediadir is writable',1); 1042 }else{ 1043 msg('Mediadir is not writable',-1); 1044 } 1045 1046 if(is_writable($conf['cachedir'])){ 1047 msg('Cachedir is writable',1); 1048 }else{ 1049 msg('Cachedir is not writable',-1); 1050 } 1051 1052 if(is_writable(DOKU_CONF.'users.auth.php')){ 1053 msg('conf/users.auth.php is writable',1); 1054 }else{ 1055 msg('conf/users.auth.php is not writable',0); 1056 } 1057 1058 if(function_exists('mb_strpos')){ 1059 if(defined('UTF8_NOMBSTRING')){ 1060 msg('mb_string extension is available but will not be used',0); 1061 }else{ 1062 msg('mb_string extension is available and will be used',1); 1063 } 1064 }else{ 1065 msg('mb_string extension not available - PHP only replacements will be used',0); 1066 } 1067 1068 msg('Your current permission for this page is '.$INFO['perm'],0); 1069 1070 if(is_writable($INFO['filepath'])){ 1071 msg('The current page is writable by the webserver',0); 1072 }else{ 1073 msg('The current page is not writable by the webserver',0); 1074 } 1075 1076 if($INFO['writable']){ 1077 msg('The current page is writable by you',0); 1078 }else{ 1079 msg('The current page is not writable you',0); 1080 } 1081} 1082 1083/** 1084 * Let us know if a user is tracking a page 1085 * 1086 * @author Andreas Gohr <andi@splitbrain.org> 1087 */ 1088function is_subscribed($id,$uid){ 1089 $file=metaFN($id,'.mlist'); 1090 if (@file_exists($file)) { 1091 $mlist = file($file); 1092 $pos = array_search($uid."\n",$mlist); 1093 return is_int($pos); 1094 } 1095 1096 return false; 1097} 1098 1099/** 1100 * Return a string with the email addresses of all the 1101 * users subscribed to a page 1102 * 1103 * @author Steven Danz <steven-danz@kc.rr.com> 1104 */ 1105function subscriber_addresslist($id){ 1106 global $conf; 1107 1108 $emails = ''; 1109 1110 if (!$conf['subscribers']) return; 1111 1112 $mlist = array(); 1113 $file=metaFN($id,'.mlist'); 1114 if (file_exists($file)) { 1115 $mlist = file($file); 1116 } 1117 if(count($mlist) > 0) { 1118 foreach ($mlist as $who) { 1119 $who = rtrim($who); 1120 $info = auth_getUserData($who); 1121 $level = auth_aclcheck($id,$who,$info['grps']); 1122 if ($level >= AUTH_READ) { 1123 if (strcasecmp($info['mail'],$conf['notify']) != 0) { 1124 if (empty($emails)) { 1125 $emails = $info['mail']; 1126 } else { 1127 $emails = "$emails,".$info['mail']; 1128 } 1129 } 1130 } 1131 } 1132 } 1133 1134 return $emails; 1135} 1136 1137/** 1138 * Removes quoting backslashes 1139 * 1140 * @author Andreas Gohr <andi@splitbrain.org> 1141 */ 1142function unslash($string,$char="'"){ 1143 return str_replace('\\'.$char,$char,$string); 1144} 1145 1146//Setup VIM: ex: et ts=2 enc=utf-8 : 1147