1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Common DokuWiki functions 415fae107Sandi * 515fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 715fae107Sandi */ 815fae107Sandi 9ed7b5f09Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 10e7cb32dcSAndreas Gohr require_once(DOKU_CONF.'dokuwiki.php'); 11ed7b5f09Sandi require_once(DOKU_INC.'inc/io.php'); 12ed7b5f09Sandi require_once(DOKU_INC.'inc/utf8.php'); 13ed7b5f09Sandi require_once(DOKU_INC.'inc/mail.php'); 14c112d578Sandi require_once(DOKU_INC.'inc/parserutils.php'); 15f3f0262cSandi 16f3f0262cSandi/** 1715fae107Sandi * Return info about the current document as associative 18f3f0262cSandi * array. 1915fae107Sandi * 2015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 21f3f0262cSandi */ 22f3f0262cSandifunction pageinfo(){ 23f3f0262cSandi global $ID; 24f3f0262cSandi global $REV; 25f3f0262cSandi global $USERINFO; 26f3f0262cSandi global $conf; 27f3f0262cSandi 28f3f0262cSandi if($_SERVER['REMOTE_USER']){ 29f3f0262cSandi $info['userinfo'] = $USERINFO; 30f3f0262cSandi $info['perm'] = auth_quickaclcheck($ID); 311380fc45SAndreas Gohr $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']); 32f3f0262cSandi }else{ 33f3f0262cSandi $info['perm'] = auth_aclcheck($ID,'',null); 341380fc45SAndreas Gohr $info['subscribed'] = false; 35f3f0262cSandi } 36f3f0262cSandi 37f3f0262cSandi $info['namespace'] = getNS($ID); 38f3f0262cSandi $info['locked'] = checklock($ID); 39f3f0262cSandi $info['filepath'] = realpath(wikiFN($ID,$REV)); 40f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 41f3f0262cSandi if($REV && !$info['exists']){ 42f3f0262cSandi //check if current revision was meant 43f3f0262cSandi $cur = wikiFN($ID); 44f3f0262cSandi if(@file_exists($cur) && (@filemtime($cur) == $REV)){ 45f3f0262cSandi $info['filepath'] = realpath($cur); 46f3f0262cSandi $info['exists'] = true; 47f3f0262cSandi $REV = ''; 48f3f0262cSandi } 49f3f0262cSandi } 50c112d578Sandi $info['rev'] = $REV; 51f3f0262cSandi if($info['exists']){ 52f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 53f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 54f3f0262cSandi }else{ 55f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 56f3f0262cSandi } 57f3f0262cSandi $info['editable'] = ($info['writable'] && empty($info['lock'])); 58f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 59f3f0262cSandi 60652610a2Sandi //who's the editor 61652610a2Sandi if($REV){ 62652610a2Sandi $revinfo = getRevisionInfo($ID,$REV); 63652610a2Sandi }else{ 64652610a2Sandi $revinfo = getRevisionInfo($ID,$info['lastmod']); 65652610a2Sandi } 66652610a2Sandi $info['ip'] = $revinfo['ip']; 67652610a2Sandi $info['user'] = $revinfo['user']; 68652610a2Sandi $info['sum'] = $revinfo['sum']; 69*59f257aeSchris 7088f522e9Sandi if($revinfo['user']){ 7188f522e9Sandi $info['editor'] = $revinfo['user']; 7288f522e9Sandi }else{ 7388f522e9Sandi $info['editor'] = $revinfo['ip']; 7488f522e9Sandi } 75652610a2Sandi 76f3f0262cSandi return $info; 77f3f0262cSandi} 78f3f0262cSandi 79f3f0262cSandi/** 802684e50aSAndreas Gohr * Build an string of URL parameters 812684e50aSAndreas Gohr * 822684e50aSAndreas Gohr * @author Andreas Gohr 832684e50aSAndreas Gohr */ 842684e50aSAndreas Gohrfunction buildURLparams($params){ 852684e50aSAndreas Gohr $url = ''; 862684e50aSAndreas Gohr $amp = false; 872684e50aSAndreas Gohr foreach($params as $key => $val){ 882684e50aSAndreas Gohr if($amp) $url .= '&'; 892684e50aSAndreas Gohr 902684e50aSAndreas Gohr $url .= $key.'='; 912684e50aSAndreas Gohr $url .= urlencode($val); 922684e50aSAndreas Gohr $amp = true; 932684e50aSAndreas Gohr } 942684e50aSAndreas Gohr return $url; 952684e50aSAndreas Gohr} 962684e50aSAndreas Gohr 972684e50aSAndreas Gohr/** 982684e50aSAndreas Gohr * Build an string of html tag attributes 992684e50aSAndreas Gohr * 1002684e50aSAndreas Gohr * @author Andreas Gohr 1012684e50aSAndreas Gohr */ 1022684e50aSAndreas Gohrfunction buildAttributes($params){ 1032684e50aSAndreas Gohr $url = ''; 1042684e50aSAndreas Gohr foreach($params as $key => $val){ 1052684e50aSAndreas Gohr $url .= $key.'="'; 1062684e50aSAndreas Gohr $url .= htmlspecialchars ($val); 1072684e50aSAndreas Gohr $url .= '" '; 1082684e50aSAndreas Gohr } 1092684e50aSAndreas Gohr return $url; 1102684e50aSAndreas Gohr} 1112684e50aSAndreas Gohr 1122684e50aSAndreas Gohr 1132684e50aSAndreas Gohr/** 1140396becbSandi * print a message 1150396becbSandi * 1160396becbSandi * If HTTP headers were not sent yet the message is added 1170396becbSandi * to the global message array else it's printed directly 1180396becbSandi * using html_msgarea() 1190396becbSandi * 120f3f0262cSandi * 121f3f0262cSandi * Levels can be: 122f3f0262cSandi * 123f3f0262cSandi * -1 error 124f3f0262cSandi * 0 info 125f3f0262cSandi * 1 success 12615fae107Sandi * 12715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1280396becbSandi * @see html_msgarea 129f3f0262cSandi */ 130f3f0262cSandifunction msg($message,$lvl=0){ 131f3f0262cSandi global $MSG; 132f3f0262cSandi $errors[-1] = 'error'; 133f3f0262cSandi $errors[0] = 'info'; 134f3f0262cSandi $errors[1] = 'success'; 135f3f0262cSandi 136cc20ad51Sandi if(!headers_sent()){ 137f3f0262cSandi if(!isset($MSG)) $MSG = array(); 138f3f0262cSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 1390396becbSandi }else{ 1400396becbSandi $MSG = array(); 1410396becbSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 142f62ea8a1Sandi if(function_exists('html_msgarea')){ 1430396becbSandi html_msgarea(); 144f62ea8a1Sandi }else{ 145f62ea8a1Sandi print "ERROR($lvl) $message"; 146f62ea8a1Sandi } 1470396becbSandi } 148f3f0262cSandi} 149f3f0262cSandi 150f3f0262cSandi/** 15115fae107Sandi * This builds the breadcrumb trail and returns it as array 15215fae107Sandi * 15315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 154f3f0262cSandi */ 155f3f0262cSandifunction breadcrumbs(){ 1568746e727Sandi // we prepare the breadcrumbs early for quick session closing 1578746e727Sandi static $crumbs = null; 1588746e727Sandi if($crumbs != null) return $crumbs; 1598746e727Sandi 160f3f0262cSandi global $ID; 161f3f0262cSandi global $ACT; 162f3f0262cSandi global $conf; 163f3f0262cSandi $crumbs = $_SESSION[$conf['title']]['bc']; 164f3f0262cSandi 165f3f0262cSandi //first visit? 166f3f0262cSandi if (!is_array($crumbs)){ 167f3f0262cSandi $crumbs = array(); 168f3f0262cSandi } 169f3f0262cSandi //we only save on show and existing wiki documents 170a77f5846Sjan $file = wikiFN($ID); 171a77f5846Sjan if($ACT != 'show' || !@file_exists($file)){ 172f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 173f3f0262cSandi return $crumbs; 174f3f0262cSandi } 175a77f5846Sjan 176a77f5846Sjan // page names 177a77f5846Sjan $name = noNS($ID); 178a77f5846Sjan if ($conf['useheading']) { 179a77f5846Sjan // get page title 180bb0a59d4Sjan $title = p_get_first_heading($ID); 181a77f5846Sjan if ($title) { 182a77f5846Sjan $name = $title; 183a77f5846Sjan } 184a77f5846Sjan } 185a77f5846Sjan 186f3f0262cSandi //remove ID from array 187a77f5846Sjan if (isset($crumbs[$ID])) { 188a77f5846Sjan unset($crumbs[$ID]); 189f3f0262cSandi } 190f3f0262cSandi 191f3f0262cSandi //add to array 192a77f5846Sjan $crumbs[$ID] = $name; 193f3f0262cSandi //reduce size 194f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']){ 195f3f0262cSandi array_shift($crumbs); 196f3f0262cSandi } 197f3f0262cSandi //save to session 198f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 199f3f0262cSandi return $crumbs; 200f3f0262cSandi} 201f3f0262cSandi 202f3f0262cSandi/** 20315fae107Sandi * Filter for page IDs 20415fae107Sandi * 205f3f0262cSandi * This is run on a ID before it is outputted somewhere 206f3f0262cSandi * currently used to replace the colon with something else 207f3f0262cSandi * on Windows systems and to have proper URL encoding 20815fae107Sandi * 20949c713a3Sandi * Urlencoding is ommitted when the second parameter is false 21049c713a3Sandi * 21115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 212f3f0262cSandi */ 21349c713a3Sandifunction idfilter($id,$ue=true){ 214f3f0262cSandi global $conf; 215f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']){ 216f3f0262cSandi $id = strtr($id,':','/'); 217f3f0262cSandi }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 218f3f0262cSandi $conf['userewrite']) { 219f3f0262cSandi $id = strtr($id,':',';'); 220f3f0262cSandi } 22149c713a3Sandi if($ue){ 222f3f0262cSandi $id = urlencode($id); 223f3f0262cSandi $id = str_replace('%3A',':',$id); //keep as colon 224f3f0262cSandi $id = str_replace('%2F','/',$id); //keep as slash 22549c713a3Sandi } 226f3f0262cSandi return $id; 227f3f0262cSandi} 228f3f0262cSandi 229f3f0262cSandi/** 230ed7b5f09Sandi * This builds a link to a wikipage 23115fae107Sandi * 2326c7843b5Sandi * It handles URL rewriting and adds additional parameter if 2336c7843b5Sandi * given in $more 2346c7843b5Sandi * 23515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 236f3f0262cSandi */ 237ed7b5f09Sandifunction wl($id='',$more='',$abs=false){ 238f3f0262cSandi global $conf; 2396de3759aSAndreas Gohr if(is_array($more)){ 2406de3759aSAndreas Gohr $more = buildURLparams($more); 2416de3759aSAndreas Gohr }else{ 242f3f0262cSandi $more = str_replace(',','&',$more); 2436de3759aSAndreas Gohr } 244f3f0262cSandi 245f3f0262cSandi $id = idfilter($id); 246ed7b5f09Sandi if($abs){ 247ed7b5f09Sandi $xlink = DOKU_URL; 248ed7b5f09Sandi }else{ 249ed7b5f09Sandi $xlink = DOKU_BASE; 250ed7b5f09Sandi } 251f3f0262cSandi 2526c7843b5Sandi if($conf['userewrite'] == 2){ 2536c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 2546c7843b5Sandi if($more) $xlink .= '?'.$more; 2556c7843b5Sandi }elseif($conf['userewrite']){ 256f3f0262cSandi $xlink .= $id; 257f3f0262cSandi if($more) $xlink .= '?'.$more; 2586c7843b5Sandi }else{ 2596c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 2606c7843b5Sandi if($more) $xlink .= '&'.$more; 261f3f0262cSandi } 262f3f0262cSandi 263f3f0262cSandi return $xlink; 264f3f0262cSandi} 265f3f0262cSandi 266f3f0262cSandi/** 2676de3759aSAndreas Gohr * Build a link to a media file 2686de3759aSAndreas Gohr * 2696de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 2706de3759aSAndreas Gohr */ 2716de3759aSAndreas Gohrfunction ml($id='',$more='',$direct=true){ 2726de3759aSAndreas Gohr global $conf; 2736de3759aSAndreas Gohr if(is_array($more)){ 2746de3759aSAndreas Gohr $more = buildURLparams($more); 2756de3759aSAndreas Gohr }else{ 2766de3759aSAndreas Gohr $more = str_replace(',','&',$more); 2776de3759aSAndreas Gohr } 2786de3759aSAndreas Gohr 2796de3759aSAndreas Gohr $xlink = DOKU_BASE; 2806de3759aSAndreas Gohr 2816de3759aSAndreas Gohr // external URLs are always direct without rewriting 2826de3759aSAndreas Gohr if(preg_match('#^(https?|ftp)://#i',$id)){ 2836de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 2846de3759aSAndreas Gohr if($more){ 2856de3759aSAndreas Gohr $xlink .= '?'.$more; 28648665d38SAndreas Gohr $xlink .= '&media='.urlencode($id); 2876de3759aSAndreas Gohr }else{ 28848665d38SAndreas Gohr $xlink .= '?media='.urlencode($id); 2896de3759aSAndreas Gohr } 2906de3759aSAndreas Gohr return $xlink; 2916de3759aSAndreas Gohr } 2926de3759aSAndreas Gohr 2936de3759aSAndreas Gohr $id = idfilter($id); 2946de3759aSAndreas Gohr 2956de3759aSAndreas Gohr // decide on scriptname 2966de3759aSAndreas Gohr if($direct){ 2976de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 2986de3759aSAndreas Gohr $script = '_media'; 2996de3759aSAndreas Gohr }else{ 3006de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 3016de3759aSAndreas Gohr } 3026de3759aSAndreas Gohr }else{ 3036de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 3046de3759aSAndreas Gohr $script = '_detail'; 3056de3759aSAndreas Gohr }else{ 3066de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 3076de3759aSAndreas Gohr } 3086de3759aSAndreas Gohr } 3096de3759aSAndreas Gohr 3106de3759aSAndreas Gohr // build URL based on rewrite mode 3116de3759aSAndreas Gohr if($conf['userewrite']){ 3126de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 3136de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 3146de3759aSAndreas Gohr }else{ 3156de3759aSAndreas Gohr if($more){ 316a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 3176de3759aSAndreas Gohr $xlink .= '&media='.$id; 3186de3759aSAndreas Gohr }else{ 319a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 3206de3759aSAndreas Gohr } 3216de3759aSAndreas Gohr } 3226de3759aSAndreas Gohr 3236de3759aSAndreas Gohr return $xlink; 3246de3759aSAndreas Gohr} 3256de3759aSAndreas Gohr 3266de3759aSAndreas Gohr 3276de3759aSAndreas Gohr 3286de3759aSAndreas Gohr/** 329f3f0262cSandi * Just builds a link to a script 33015fae107Sandi * 331ed7b5f09Sandi * @todo maybe obsolete 33215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 333f3f0262cSandi */ 334f3f0262cSandifunction script($script='doku.php'){ 335ed7b5f09Sandi# $link = getBaseURL(); 336ed7b5f09Sandi# $link .= $script; 337ed7b5f09Sandi# return $link; 338ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 339f3f0262cSandi} 340f3f0262cSandi 341f3f0262cSandi/** 34215fae107Sandi * Spamcheck against wordlist 34315fae107Sandi * 344f3f0262cSandi * Checks the wikitext against a list of blocked expressions 345f3f0262cSandi * returns true if the text contains any bad words 34615fae107Sandi * 34715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 348f3f0262cSandi */ 349f3f0262cSandifunction checkwordblock(){ 350f3f0262cSandi global $TEXT; 351f3f0262cSandi global $conf; 352f3f0262cSandi 353f3f0262cSandi if(!$conf['usewordblock']) return false; 354f3f0262cSandi 355e7cb32dcSAndreas Gohr $blockfile = file(DOKU_CONF.'wordblock.conf'); 3563e2965d7Sandi //how many lines to read at once (to work around some PCRE limits) 3573e2965d7Sandi if(version_compare(phpversion(),'4.3.0','<')){ 3583e2965d7Sandi //old versions of PCRE define a maximum of parenthesises even if no 3593e2965d7Sandi //backreferences are used - the maximum is 99 3603e2965d7Sandi //this is very bad performancewise and may even be too high still 3613e2965d7Sandi $chunksize = 40; 3623e2965d7Sandi }else{ 363703f6fdeSandi //read file in chunks of 600 - this should work around the 3643e2965d7Sandi //MAX_PATTERN_SIZE in modern PCRE 3653e2965d7Sandi $chunksize = 600; 3663e2965d7Sandi } 3673e2965d7Sandi while($blocks = array_splice($blockfile,0,$chunksize)){ 368f3f0262cSandi $re = array(); 369f3f0262cSandi #build regexp from blocks 370f3f0262cSandi foreach($blocks as $block){ 371f3f0262cSandi $block = preg_replace('/#.*$/','',$block); 372f3f0262cSandi $block = trim($block); 373f3f0262cSandi if(empty($block)) continue; 374f3f0262cSandi $re[] = $block; 375f3f0262cSandi } 376f3f0262cSandi if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true; 377703f6fdeSandi } 378f3f0262cSandi return false; 379f3f0262cSandi} 380f3f0262cSandi 381f3f0262cSandi/** 38215fae107Sandi * Return the IP of the client 38315fae107Sandi * 38415fae107Sandi * Honours X-Forwarded-For Proxy Headers 38515fae107Sandi * 38615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 387f3f0262cSandi */ 388f3f0262cSandifunction clientIP(){ 389f3f0262cSandi $my = $_SERVER['REMOTE_ADDR']; 390f3f0262cSandi if($_SERVER['HTTP_X_FORWARDED_FOR']){ 391f3f0262cSandi $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')'; 392f3f0262cSandi } 393f3f0262cSandi return $my; 394f3f0262cSandi} 395f3f0262cSandi 396f3f0262cSandi/** 39715fae107Sandi * Checks if a given page is currently locked. 39815fae107Sandi * 399f3f0262cSandi * removes stale lockfiles 40015fae107Sandi * 40115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 402f3f0262cSandi */ 403f3f0262cSandifunction checklock($id){ 404f3f0262cSandi global $conf; 405f3f0262cSandi $lock = wikiFN($id).'.lock'; 406f3f0262cSandi 407f3f0262cSandi //no lockfile 408f3f0262cSandi if(!@file_exists($lock)) return false; 409f3f0262cSandi 410f3f0262cSandi //lockfile expired 411f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']){ 412f3f0262cSandi unlink($lock); 413f3f0262cSandi return false; 414f3f0262cSandi } 415f3f0262cSandi 416f3f0262cSandi //my own lock 417f3f0262cSandi $ip = io_readFile($lock); 418f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 419f3f0262cSandi return false; 420f3f0262cSandi } 421f3f0262cSandi 422f3f0262cSandi return $ip; 423f3f0262cSandi} 424f3f0262cSandi 425f3f0262cSandi/** 42615fae107Sandi * Lock a page for editing 42715fae107Sandi * 42815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 429f3f0262cSandi */ 430f3f0262cSandifunction lock($id){ 431f3f0262cSandi $lock = wikiFN($id).'.lock'; 432f3f0262cSandi if($_SERVER['REMOTE_USER']){ 433f3f0262cSandi io_saveFile($lock,$_SERVER['REMOTE_USER']); 434f3f0262cSandi }else{ 435f3f0262cSandi io_saveFile($lock,clientIP()); 436f3f0262cSandi } 437f3f0262cSandi} 438f3f0262cSandi 439f3f0262cSandi/** 44015fae107Sandi * Unlock a page if it was locked by the user 441f3f0262cSandi * 44215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 44315fae107Sandi * @return bool true if a lock was removed 444f3f0262cSandi */ 445f3f0262cSandifunction unlock($id){ 446f3f0262cSandi $lock = wikiFN($id).'.lock'; 447f3f0262cSandi if(@file_exists($lock)){ 448f3f0262cSandi $ip = io_readFile($lock); 449f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 450f3f0262cSandi @unlink($lock); 451f3f0262cSandi return true; 452f3f0262cSandi } 453f3f0262cSandi } 454f3f0262cSandi return false; 455f3f0262cSandi} 456f3f0262cSandi 457f3f0262cSandi/** 458f3f0262cSandi * convert line ending to unix format 459f3f0262cSandi * 46015fae107Sandi * @see formText() for 2crlf conversion 46115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 462f3f0262cSandi */ 463f3f0262cSandifunction cleanText($text){ 464f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 465f3f0262cSandi return $text; 466f3f0262cSandi} 467f3f0262cSandi 468f3f0262cSandi/** 469f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 470f3f0262cSandi * It also converts line endings to Windows format which is 471f3f0262cSandi * pseudo standard for webforms. 472f3f0262cSandi * 47315fae107Sandi * @see cleanText() for 2unix conversion 47415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 475f3f0262cSandi */ 476f3f0262cSandifunction formText($text){ 477f3f0262cSandi $text = preg_replace("/\012/","\015\012",$text); 478f3f0262cSandi return htmlspecialchars($text); 479f3f0262cSandi} 480f3f0262cSandi 481f3f0262cSandi/** 48215fae107Sandi * Returns the specified local text in raw format 48315fae107Sandi * 48415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 485f3f0262cSandi */ 486f3f0262cSandifunction rawLocale($id){ 487f3f0262cSandi return io_readFile(localeFN($id)); 488f3f0262cSandi} 489f3f0262cSandi 490f3f0262cSandi/** 491f3f0262cSandi * Returns the raw WikiText 49215fae107Sandi * 49315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 494f3f0262cSandi */ 495f3f0262cSandifunction rawWiki($id,$rev=''){ 496f3f0262cSandi return io_readFile(wikiFN($id,$rev)); 497f3f0262cSandi} 498f3f0262cSandi 499f3f0262cSandi/** 5007146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 5017146cee2SAndreas Gohr * 5027146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 5037146cee2SAndreas Gohr */ 5047146cee2SAndreas Gohrfunction pageTemplate($id){ 505a15ce62dSEsther Brunner global $conf; 506a15ce62dSEsther Brunner global $INFO; 507a15ce62dSEsther Brunner $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt'); 508a15ce62dSEsther Brunner $tpl = str_replace('@ID@',$id,$tpl); 509a15ce62dSEsther Brunner $tpl = str_replace('@NS@',getNS($id),$tpl); 510a15ce62dSEsther Brunner $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl); 511a15ce62dSEsther Brunner $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl); 512a15ce62dSEsther Brunner $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl); 513a15ce62dSEsther Brunner $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl); 514a15ce62dSEsther Brunner $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl); 515a15ce62dSEsther Brunner return $tpl; 5167146cee2SAndreas Gohr} 5177146cee2SAndreas Gohr 5187146cee2SAndreas Gohr 5197146cee2SAndreas Gohr/** 52015fae107Sandi * Returns the raw Wiki Text in three slices. 52115fae107Sandi * 52215fae107Sandi * The range parameter needs to have the form "from-to" 52315cfe303Sandi * and gives the range of the section in bytes - no 52415cfe303Sandi * UTF-8 awareness is needed. 525f3f0262cSandi * The returned order is prefix, section and suffix. 52615fae107Sandi * 52715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 528f3f0262cSandi */ 529f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){ 530f3f0262cSandi list($from,$to) = split('-',$range,2); 531f3f0262cSandi $text = io_readFile(wikiFN($id,$rev)); 532f3f0262cSandi if(!$from) $from = 0; 533c3d8e19bSandi if(!$to) $to = strlen($text)+1; 534f3f0262cSandi 53515cfe303Sandi $slices[0] = substr($text,0,$from-1); 53615cfe303Sandi $slices[1] = substr($text,$from-1,$to-$from); 53715cfe303Sandi $slices[2] = substr($text,$to); 538f3f0262cSandi 539f3f0262cSandi return $slices; 540f3f0262cSandi} 541f3f0262cSandi 542f3f0262cSandi/** 54315fae107Sandi * Joins wiki text slices 54415fae107Sandi * 545f3f0262cSandi * function to join the text slices with correct lineendings again. 546f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 547f3f0262cSandi * lines between sections if needed (used on saving). 54815fae107Sandi * 54915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 550f3f0262cSandi */ 551f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){ 552f3f0262cSandi 553f3f0262cSandi if($pretty){ 554f3f0262cSandi if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 555f3f0262cSandi if($suf && substr($text,-1) != "\n") $text .= "\n"; 556f3f0262cSandi } 557f3f0262cSandi 558f3f0262cSandi if($pre) $pre .= "\n"; 559f3f0262cSandi if($suf) $text .= "\n"; 560f3f0262cSandi return $pre.$text.$suf; 561f3f0262cSandi} 562f3f0262cSandi 563f3f0262cSandi/** 56415fae107Sandi * print debug messages 56515fae107Sandi * 566f3f0262cSandi * little function to print the content of a var 56715fae107Sandi * 56815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 569f3f0262cSandi */ 570f3f0262cSandifunction dbg($msg,$hidden=false){ 571f3f0262cSandi (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 572f3f0262cSandi print_r($msg); 573f3f0262cSandi (!$hidden) ? print '</pre>' : print "\n-->"; 574f3f0262cSandi} 575f3f0262cSandi 576f3f0262cSandi/** 577f3f0262cSandi * Add's an entry to the changelog 57815fae107Sandi * 57915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 580f3f0262cSandi */ 581652610a2Sandifunction addLogEntry($date,$id,$summary=""){ 582f3f0262cSandi global $conf; 583c1049928Sandi 584c1049928Sandi if(!@is_writable($conf['changelog'])){ 585c1049928Sandi msg($conf['changelog'].' is not writable!',-1); 586c1049928Sandi return; 587c1049928Sandi } 588c1049928Sandi 589652610a2Sandi if(!$date) $date = time(); //use current time if none supplied 590f3f0262cSandi $remote = $_SERVER['REMOTE_ADDR']; 591f3f0262cSandi $user = $_SERVER['REMOTE_USER']; 592f3f0262cSandi 593f3f0262cSandi $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n"; 594dbb00abcSEsther Brunner io_saveFile($conf['changelog'],$logline,true); 595f3f0262cSandi} 596f3f0262cSandi 597f3f0262cSandi/** 598f3f0262cSandi * returns an array of recently changed files using the 599f3f0262cSandi * changelog 6005749f1ceSmatthiasgrimm * first : first entry in array returned 601a39955b0Smatthiasgrimm * num : return 'num' entries 60215fae107Sandi * 60315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 604f3f0262cSandi */ 6058f1d587cSEsther Brunnerfunction getRecents($first,$num,$incdel=false,$ns='',$subNS=true){ 606f3f0262cSandi global $conf; 607f3f0262cSandi $recent = array(); 6085749f1ceSmatthiasgrimm $names = array(); 6095749f1ceSmatthiasgrimm 6105749f1ceSmatthiasgrimm if(!$num) 6115749f1ceSmatthiasgrimm return $recent; 612f3f0262cSandi 613c1049928Sandi if(!@is_readable($conf['changelog'])){ 614c1049928Sandi msg($conf['changelog'].' is not readable',-1); 615c1049928Sandi return $recent; 616c1049928Sandi } 617c1049928Sandi 618f3f0262cSandi $loglines = file($conf['changelog']); 619f3f0262cSandi rsort($loglines); //reverse sort on timestamp 620f3f0262cSandi 621f3f0262cSandi foreach ($loglines as $line){ 622f3f0262cSandi $line = rtrim($line); //remove newline 623f3f0262cSandi if(empty($line)) continue; //skip empty lines 624f3f0262cSandi $info = split("\t",$line); //split into parts 625f3f0262cSandi //add id if not in yet and file still exists and is allowed to read 6265749f1ceSmatthiasgrimm if(!$names[$info[2]] && 627f3f0262cSandi (@file_exists(wikiFN($info[2])) || $incdel) && 628f3f0262cSandi (auth_quickaclcheck($info[2]) >= AUTH_READ) 629f3f0262cSandi ){ 630dbb00abcSEsther Brunner // filter namespace 631dbb00abcSEsther Brunner if (($ns) && (strpos($info[2],$ns.':') !== 0)) continue; 632dbb00abcSEsther Brunner 6338f1d587cSEsther Brunner // exclude subnamespaces 6348f1d587cSEsther Brunner if ((!$subNS) && (getNS($info[2]) != $ns)) continue; 6358f1d587cSEsther Brunner 6365749f1ceSmatthiasgrimm $names[$info[2]] = 1; 6375749f1ceSmatthiasgrimm if(--$first >= 0) continue; /* skip "first" entries */ 6385749f1ceSmatthiasgrimm 639f3f0262cSandi $recent[$info[2]]['date'] = $info[0]; 640f3f0262cSandi $recent[$info[2]]['ip'] = $info[1]; 641f3f0262cSandi $recent[$info[2]]['user'] = $info[3]; 642f3f0262cSandi $recent[$info[2]]['sum'] = $info[4]; 643f3f0262cSandi $recent[$info[2]]['del'] = !@file_exists(wikiFN($info[2])); 644f3f0262cSandi } 6455749f1ceSmatthiasgrimm if(count($recent) >= $num){ 646f3f0262cSandi break; //finish if enough items found 647f3f0262cSandi } 648f3f0262cSandi } 649f3f0262cSandi return $recent; 650f3f0262cSandi} 651f3f0262cSandi 652f3f0262cSandi/** 653652610a2Sandi * gets additonal informations for a certain pagerevison 654652610a2Sandi * from the changelog 655652610a2Sandi * 656652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org> 657652610a2Sandi */ 658652610a2Sandifunction getRevisionInfo($id,$rev){ 659652610a2Sandi global $conf; 660258641c6Sandi 661258641c6Sandi if(!$rev) return(null); 662258641c6Sandi 663c1049928Sandi $info = array(); 664c1049928Sandi if(!@is_readable($conf['changelog'])){ 665c1049928Sandi msg($conf['changelog'].' is not readable',-1); 666c1049928Sandi return $recent; 667c1049928Sandi } 668652610a2Sandi $loglines = file($conf['changelog']); 669652610a2Sandi $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines); 670dc42ff59Sandi $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed) 671652610a2Sandi $line = split("\t",$loglines[0]); 672652610a2Sandi $info['date'] = $line[0]; 673652610a2Sandi $info['ip'] = $line[1]; 674652610a2Sandi $info['user'] = $line[3]; 675652610a2Sandi $info['sum'] = $line[4]; 676652610a2Sandi return $info; 677652610a2Sandi} 678652610a2Sandi 679652610a2Sandi/** 680f3f0262cSandi * Saves a wikitext by calling io_saveFile 68115fae107Sandi * 68215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 683f3f0262cSandi */ 684f3f0262cSandifunction saveWikiText($id,$text,$summary){ 685f3f0262cSandi global $conf; 686f3f0262cSandi global $lang; 687f3f0262cSandi umask($conf['umask']); 688f3f0262cSandi // ignore if no changes were made 689f3f0262cSandi if($text == rawWiki($id,'')){ 690f3f0262cSandi return; 691f3f0262cSandi } 692f3f0262cSandi 693f3f0262cSandi $file = wikiFN($id); 694f3f0262cSandi $old = saveOldRevision($id); 695f3f0262cSandi 696f3f0262cSandi if (empty($text)){ 697e1f3d9e1SEsther Brunner // remove empty file 698f3f0262cSandi @unlink($file); 699e1f3d9e1SEsther Brunner // remove any meta info 700e1f3d9e1SEsther Brunner $mfiles = metaFiles($id); 701e1f3d9e1SEsther Brunner foreach ($mfiles as $mfile) { 702e1f3d9e1SEsther Brunner if (file_exists($mfile)) @unlink($mfile); 703b158d625SSteven Danz } 704f3f0262cSandi $del = true; 7053ce054b3Sandi //autoset summary on deletion 7063ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 70753d6ccfeSandi //remove empty namespaces 70853d6ccfeSandi io_sweepNS($id); 709f3f0262cSandi }else{ 710f3f0262cSandi // save file (datadir is created in io_saveFile) 711f3f0262cSandi io_saveFile($file,$text); 712f3f0262cSandi $del = false; 713f3f0262cSandi } 714f3f0262cSandi 715652610a2Sandi addLogEntry(@filemtime($file),$id,$summary); 71626a0801fSAndreas Gohr // send notify mails 71726a0801fSAndreas Gohr notify($id,'admin',$old,$summary); 71826a0801fSAndreas Gohr notify($id,'subscribers',$old,$summary); 719f3f0262cSandi 720f3f0262cSandi //purge cache on add by updating the purgefile 721f3f0262cSandi if($conf['purgeonadd'] && (!$old || $del)){ 72298407a7aSandi io_saveFile($conf['cachedir'].'/purgefile',time()); 723f3f0262cSandi } 724f3f0262cSandi} 725f3f0262cSandi 726f3f0262cSandi/** 727f3f0262cSandi * moves the current version to the attic and returns its 728f3f0262cSandi * revision date 72915fae107Sandi * 73015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 731f3f0262cSandi */ 732f3f0262cSandifunction saveOldRevision($id){ 733f3f0262cSandi global $conf; 734f3f0262cSandi umask($conf['umask']); 735f3f0262cSandi $oldf = wikiFN($id); 736f3f0262cSandi if(!@file_exists($oldf)) return ''; 737f3f0262cSandi $date = filemtime($oldf); 738f3f0262cSandi $newf = wikiFN($id,$date); 739f3f0262cSandi if(substr($newf,-3)=='.gz'){ 740f3f0262cSandi io_saveFile($newf,rawWiki($id)); 741f3f0262cSandi }else{ 742f3f0262cSandi io_makeFileDir($newf); 743f3f0262cSandi copy($oldf, $newf); 744f3f0262cSandi } 745f3f0262cSandi return $date; 746f3f0262cSandi} 747f3f0262cSandi 748f3f0262cSandi/** 74926a0801fSAndreas Gohr * Sends a notify mail on page change 75026a0801fSAndreas Gohr * 75126a0801fSAndreas Gohr * @param string $id The changed page 75226a0801fSAndreas Gohr * @param string $who Who to notify (admin|subscribers) 75326a0801fSAndreas Gohr * @param int $rev Old page revision 75426a0801fSAndreas Gohr * @param string $summary What changed 75515fae107Sandi * 75615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 757f3f0262cSandi */ 75826a0801fSAndreas Gohrfunction notify($id,$who,$rev='',$summary=''){ 759f3f0262cSandi global $lang; 760f3f0262cSandi global $conf; 761b158d625SSteven Danz 76226a0801fSAndreas Gohr // decide if there is something to do 76326a0801fSAndreas Gohr if($who == 'admin'){ 76426a0801fSAndreas Gohr if(empty($conf['notify'])) return; //notify enabled? 765f3f0262cSandi $text = rawLocale('mailtext'); 76626a0801fSAndreas Gohr $to = $conf['notify']; 76726a0801fSAndreas Gohr $bcc = ''; 76826a0801fSAndreas Gohr }elseif($who == 'subscribers'){ 76926a0801fSAndreas Gohr if(!$conf['subscribers']) return; //subscribers enabled? 77026a0801fSAndreas Gohr $bcc = subscriber_addresslist($id); 77126a0801fSAndreas Gohr if(empty($bcc)) return; 77226a0801fSAndreas Gohr $to = ''; 77326a0801fSAndreas Gohr $text = rawLocale('subscribermail'); 77426a0801fSAndreas Gohr }else{ 77526a0801fSAndreas Gohr return; //just to be safe 77626a0801fSAndreas Gohr } 77726a0801fSAndreas Gohr 778f3f0262cSandi $text = str_replace('@DATE@',date($conf['dformat']),$text); 779f3f0262cSandi $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 780f3f0262cSandi $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 781f3f0262cSandi $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 782ed7b5f09Sandi $text = str_replace('@NEWPAGE@',wl($id,'',true),$text); 78326a0801fSAndreas Gohr $text = str_replace('@PAGE@',$id,$text); 78426a0801fSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 785ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 786f3f0262cSandi $text = str_replace('@SUMMARY@',$summary,$text); 7877a82afdcSandi $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 788f3f0262cSandi 789f3f0262cSandi if($rev){ 790f3f0262cSandi $subject = $lang['mail_changed'].' '.$id; 791ed7b5f09Sandi $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text); 792f3f0262cSandi require_once("inc/DifferenceEngine.php"); 793f3f0262cSandi $df = new Diff(split("\n",rawWiki($id,$rev)), 794f3f0262cSandi split("\n",rawWiki($id))); 795f3f0262cSandi $dformat = new UnifiedDiffFormatter(); 796f3f0262cSandi $diff = $dformat->format($df); 797f3f0262cSandi }else{ 798f3f0262cSandi $subject=$lang['mail_newpage'].' '.$id; 799f3f0262cSandi $text = str_replace('@OLDPAGE@','none',$text); 800f3f0262cSandi $diff = rawWiki($id); 801f3f0262cSandi } 802f3f0262cSandi $text = str_replace('@DIFF@',$diff,$text); 803241f3a36Sandi $subject = '['.$conf['title'].'] '.$subject; 804f3f0262cSandi 80526a0801fSAndreas Gohr mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc); 806f3f0262cSandi} 807f3f0262cSandi 80815fae107Sandi/** 80915fae107Sandi * Return a list of available page revisons 81015fae107Sandi * 81115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 81215fae107Sandi */ 813f3f0262cSandifunction getRevisions($id){ 814f3f0262cSandi $revd = dirname(wikiFN($id,'foo')); 815f3f0262cSandi $revs = array(); 816f3f0262cSandi $clid = cleanID($id); 817f3f0262cSandi if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path 818493a6929SKobaYY $clid = utf8_encodeFN($clid); 819f3f0262cSandi 820f3f0262cSandi if (is_dir($revd) && $dh = opendir($revd)) { 821f3f0262cSandi while (($file = readdir($dh)) !== false) { 822f3f0262cSandi if (is_dir($revd.'/'.$file)) continue; 823f3f0262cSandi if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){ 824f3f0262cSandi $revs[]=$match[1]; 825f3f0262cSandi } 826f3f0262cSandi } 827f3f0262cSandi closedir($dh); 828f3f0262cSandi } 829f3f0262cSandi rsort($revs); 830f3f0262cSandi return $revs; 831f3f0262cSandi} 832f3f0262cSandi 833f3f0262cSandi/** 834f3f0262cSandi * extracts the query from a google referer 83515fae107Sandi * 8366b13307fSandi * @todo should be more generic and support yahoo et al 83715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 838f3f0262cSandi */ 839f3f0262cSandifunction getGoogleQuery(){ 840f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 8415c3f206fSandi if(!$url) return ''; 842f3f0262cSandi 843f3f0262cSandi if(!preg_match("#google\.#i",$url['host'])) return ''; 844f3f0262cSandi $query = array(); 845f3f0262cSandi parse_str($url['query'],$query); 846f3f0262cSandi 847f3f0262cSandi return $query['q']; 848f3f0262cSandi} 849f3f0262cSandi 850f3f0262cSandi/** 85115fae107Sandi * Try to set correct locale 85215fae107Sandi * 853095bfd5cSandi * @deprecated No longer used 85415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 855f3f0262cSandi */ 856f3f0262cSandifunction setCorrectLocale(){ 857f3f0262cSandi global $conf; 858f3f0262cSandi global $lang; 859f3f0262cSandi 860f3f0262cSandi $enc = strtoupper($lang['encoding']); 861f3f0262cSandi foreach ($lang['locales'] as $loc){ 862f3f0262cSandi //try locale 863f3f0262cSandi if(@setlocale(LC_ALL,$loc)) return; 864f3f0262cSandi //try loceale with encoding 865f3f0262cSandi if(@setlocale(LC_ALL,"$loc.$enc")) return; 866f3f0262cSandi } 867f3f0262cSandi //still here? try to set from environment 868f3f0262cSandi @setlocale(LC_ALL,""); 869f3f0262cSandi} 870f3f0262cSandi 871f3f0262cSandi/** 872f3f0262cSandi * Return the human readable size of a file 873f3f0262cSandi * 874f3f0262cSandi * @param int $size A file size 875f3f0262cSandi * @param int $dec A number of decimal places 876f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 877f3f0262cSandi * @author Aidan Lister <aidan@php.net> 878f3f0262cSandi * @version 1.0.0 879f3f0262cSandi */ 880f31d5b73Sandifunction filesize_h($size, $dec = 1){ 881f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 882f3f0262cSandi $count = count($sizes); 883f3f0262cSandi $i = 0; 884f3f0262cSandi 885f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 886f3f0262cSandi $size /= 1024; 887f3f0262cSandi $i++; 888f3f0262cSandi } 889f3f0262cSandi 890f3f0262cSandi return round($size, $dec) . ' ' . $sizes[$i]; 891f3f0262cSandi} 892f3f0262cSandi 89315fae107Sandi/** 89400a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 89500a7b5adSEsther Brunner * 89600a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 89700a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 89800a7b5adSEsther Brunner */ 89900a7b5adSEsther Brunnerfunction obfuscate($email) { 90000a7b5adSEsther Brunner global $conf; 90100a7b5adSEsther Brunner 90200a7b5adSEsther Brunner switch ($conf['mailguard']) { 90300a7b5adSEsther Brunner case 'visible' : 90400a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 90500a7b5adSEsther Brunner return strtr($email, $obfuscate); 90600a7b5adSEsther Brunner 90700a7b5adSEsther Brunner case 'hex' : 90800a7b5adSEsther Brunner $encode = ''; 90900a7b5adSEsther Brunner for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';'; 91000a7b5adSEsther Brunner return $encode; 91100a7b5adSEsther Brunner 91200a7b5adSEsther Brunner case 'none' : 91300a7b5adSEsther Brunner default : 91400a7b5adSEsther Brunner return $email; 91500a7b5adSEsther Brunner } 91600a7b5adSEsther Brunner} 91700a7b5adSEsther Brunner 91800a7b5adSEsther Brunner/** 919dc57ef04Sandi * Return DokuWikis version 92015fae107Sandi * 92115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 92215fae107Sandi */ 923f31d5b73Sandifunction getVersion(){ 924f31d5b73Sandi //import version string 925f31d5b73Sandi if(@file_exists('VERSION')){ 926f31d5b73Sandi //official release 9270647ce3bSAndreas Gohr return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION')); 928f31d5b73Sandi }elseif(is_dir('_darcs')){ 929f31d5b73Sandi //darcs checkout 930f31d5b73Sandi $inv = file('_darcs/inventory'); 931f31d5b73Sandi $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv); 932f31d5b73Sandi $cur = array_pop($inv); 933f31d5b73Sandi preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 934f31d5b73Sandi return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 935f31d5b73Sandi }else{ 936f31d5b73Sandi return 'snapshot?'; 937f31d5b73Sandi } 938f31d5b73Sandi} 939f31d5b73Sandi 940f31d5b73Sandi/** 941f31d5b73Sandi * Run a few sanity checks 942f31d5b73Sandi * 943f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org> 944f31d5b73Sandi */ 945f3f0262cSandifunction check(){ 946f3f0262cSandi global $conf; 947f3f0262cSandi global $INFO; 948f3f0262cSandi 949f31d5b73Sandi msg('DokuWiki version: '.getVersion(),1); 950f31d5b73Sandi 95149022a38Sandi if(version_compare(phpversion(),'4.3.0','<')){ 95249022a38Sandi msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 95349022a38Sandi }elseif(version_compare(phpversion(),'4.3.10','<')){ 95449022a38Sandi msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 95549022a38Sandi }else{ 95649022a38Sandi msg('PHP version '.phpversion(),1); 95749022a38Sandi } 95849022a38Sandi 959f3f0262cSandi if(is_writable($conf['changelog'])){ 960f3f0262cSandi msg('Changelog is writable',1); 961f3f0262cSandi }else{ 962f3f0262cSandi msg('Changelog is not writable',-1); 963f3f0262cSandi } 964f3f0262cSandi 965f3f0262cSandi if(is_writable($conf['datadir'])){ 966f3f0262cSandi msg('Datadir is writable',1); 967f3f0262cSandi }else{ 968f3f0262cSandi msg('Datadir is not writable',-1); 969f3f0262cSandi } 970f3f0262cSandi 971f3f0262cSandi if(is_writable($conf['olddir'])){ 972f3f0262cSandi msg('Attic is writable',1); 973f3f0262cSandi }else{ 974f3f0262cSandi msg('Attic is not writable',-1); 975f3f0262cSandi } 976f3f0262cSandi 977f3f0262cSandi if(is_writable($conf['mediadir'])){ 978f3f0262cSandi msg('Mediadir is writable',1); 979f3f0262cSandi }else{ 980f3f0262cSandi msg('Mediadir is not writable',-1); 981f3f0262cSandi } 982f3f0262cSandi 98398407a7aSandi if(is_writable($conf['cachedir'])){ 98498407a7aSandi msg('Cachedir is writable',1); 98598407a7aSandi }else{ 98698407a7aSandi msg('Cachedir is not writable',-1); 98798407a7aSandi } 98898407a7aSandi 989e7cb32dcSAndreas Gohr if(is_writable(DOKU_CONF.'users.auth.php')){ 9908c4f28e8Sjan msg('conf/users.auth.php is writable',1); 991f3f0262cSandi }else{ 9928c4f28e8Sjan msg('conf/users.auth.php is not writable',0); 993f3f0262cSandi } 99493a9e835Sandi 99593a9e835Sandi if(function_exists('mb_strpos')){ 99693a9e835Sandi if(defined('UTF8_NOMBSTRING')){ 99793a9e835Sandi msg('mb_string extension is available but will not be used',0); 99893a9e835Sandi }else{ 99993a9e835Sandi msg('mb_string extension is available and will be used',1); 100093a9e835Sandi } 100193a9e835Sandi }else{ 100293a9e835Sandi msg('mb_string extension not available - PHP only replacements will be used',0); 100393a9e835Sandi } 1004f3f0262cSandi 1005f3f0262cSandi msg('Your current permission for this page is '.$INFO['perm'],0); 1006f3f0262cSandi 1007f3f0262cSandi if(is_writable($INFO['filepath'])){ 1008f3f0262cSandi msg('The current page is writable by the webserver',0); 1009f3f0262cSandi }else{ 1010f3f0262cSandi msg('The current page is not writable by the webserver',0); 1011f3f0262cSandi } 1012f3f0262cSandi 1013f3f0262cSandi if($INFO['writable']){ 1014f3f0262cSandi msg('The current page is writable by you',0); 1015f3f0262cSandi }else{ 1016f3f0262cSandi msg('The current page is not writable you',0); 1017f3f0262cSandi } 1018f3f0262cSandi} 1019340756e4Sandi 1020b158d625SSteven Danz/** 1021b158d625SSteven Danz * Let us know if a user is tracking a page 1022b158d625SSteven Danz * 10231380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1024b158d625SSteven Danz */ 10251380fc45SAndreas Gohrfunction is_subscribed($id,$uid){ 10261380fc45SAndreas Gohr $file=metaFN($id,'.mlist'); 10271380fc45SAndreas Gohr if (@file_exists($file)) { 1028b158d625SSteven Danz $mlist = file($file); 10291380fc45SAndreas Gohr $pos = array_search($uid."\n",$mlist); 10301380fc45SAndreas Gohr return is_int($pos); 1031b158d625SSteven Danz } 10321380fc45SAndreas Gohr 1033b158d625SSteven Danz return false; 1034b158d625SSteven Danz} 1035340756e4Sandi 1036f9eb5648Ssteven-danz/** 1037f9eb5648Ssteven-danz * Return a string with the email addresses of all the 1038f9eb5648Ssteven-danz * users subscribed to a page 1039f9eb5648Ssteven-danz * 104026a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com> 1041f9eb5648Ssteven-danz */ 1042f9eb5648Ssteven-danzfunction subscriber_addresslist($id){ 1043f9eb5648Ssteven-danz global $conf; 1044f9eb5648Ssteven-danz 1045f9eb5648Ssteven-danz $emails = ''; 1046f9eb5648Ssteven-danz 104726a0801fSAndreas Gohr if (!$conf['subscribers']) return; 104826a0801fSAndreas Gohr 1049f9eb5648Ssteven-danz $mlist = array(); 1050f9eb5648Ssteven-danz $file=metaFN($id,'.mlist'); 1051f9eb5648Ssteven-danz if (file_exists($file)) { 1052f9eb5648Ssteven-danz $mlist = file($file); 1053f9eb5648Ssteven-danz } 1054f9eb5648Ssteven-danz if(count($mlist) > 0) { 1055f9eb5648Ssteven-danz foreach ($mlist as $who) { 1056f9eb5648Ssteven-danz $who = rtrim($who); 1057f9eb5648Ssteven-danz $info = auth_getUserData($who); 1058f9eb5648Ssteven-danz $level = auth_aclcheck($id,$who,$info['grps']); 1059f9eb5648Ssteven-danz if ($level >= AUTH_READ) { 1060f9eb5648Ssteven-danz if (strcasecmp($info['mail'],$conf['notify']) != 0) { 1061f9eb5648Ssteven-danz if (empty($emails)) { 1062f9eb5648Ssteven-danz $emails = $info['mail']; 1063f9eb5648Ssteven-danz } else { 1064f9eb5648Ssteven-danz $emails = "$emails,".$info['mail']; 1065f9eb5648Ssteven-danz } 1066f9eb5648Ssteven-danz } 1067f9eb5648Ssteven-danz } 1068f9eb5648Ssteven-danz } 1069f9eb5648Ssteven-danz } 1070f9eb5648Ssteven-danz 1071f9eb5648Ssteven-danz return $emails; 1072f9eb5648Ssteven-danz} 1073f9eb5648Ssteven-danz 107489541d4bSAndreas Gohr/** 107589541d4bSAndreas Gohr * Removes quoting backslashes 107689541d4bSAndreas Gohr * 107789541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 107889541d4bSAndreas Gohr */ 107989541d4bSAndreas Gohrfunction unslash($string,$char="'"){ 108089541d4bSAndreas Gohr return str_replace('\\'.$char,$char,$string); 108189541d4bSAndreas Gohr} 108289541d4bSAndreas Gohr 1083340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 1084