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