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['user'] = $_SERVER['REMOTE_USER']; 30f3f0262cSandi $info['userinfo'] = $USERINFO; 31f3f0262cSandi $info['perm'] = auth_quickaclcheck($ID); 32*1380fc45SAndreas Gohr $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']); 33f3f0262cSandi }else{ 34f3f0262cSandi $info['user'] = ''; 35f3f0262cSandi $info['perm'] = auth_aclcheck($ID,'',null); 36*1380fc45SAndreas Gohr $info['subscribed'] = false; 37f3f0262cSandi } 38f3f0262cSandi 39f3f0262cSandi $info['namespace'] = getNS($ID); 40f3f0262cSandi $info['locked'] = checklock($ID); 41f3f0262cSandi $info['filepath'] = realpath(wikiFN($ID,$REV)); 42f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 43f3f0262cSandi if($REV && !$info['exists']){ 44f3f0262cSandi //check if current revision was meant 45f3f0262cSandi $cur = wikiFN($ID); 46f3f0262cSandi if(@file_exists($cur) && (@filemtime($cur) == $REV)){ 47f3f0262cSandi $info['filepath'] = realpath($cur); 48f3f0262cSandi $info['exists'] = true; 49f3f0262cSandi $REV = ''; 50f3f0262cSandi } 51f3f0262cSandi } 52c112d578Sandi $info['rev'] = $REV; 53f3f0262cSandi if($info['exists']){ 54f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 55f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 56f3f0262cSandi }else{ 57f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 58f3f0262cSandi } 59f3f0262cSandi $info['editable'] = ($info['writable'] && empty($info['lock'])); 60f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 61f3f0262cSandi 62652610a2Sandi //who's the editor 63652610a2Sandi if($REV){ 64652610a2Sandi $revinfo = getRevisionInfo($ID,$REV); 65652610a2Sandi }else{ 66652610a2Sandi $revinfo = getRevisionInfo($ID,$info['lastmod']); 67652610a2Sandi } 68652610a2Sandi $info['ip'] = $revinfo['ip']; 69652610a2Sandi $info['user'] = $revinfo['user']; 70652610a2Sandi $info['sum'] = $revinfo['sum']; 71652610a2Sandi $info['editor'] = $revinfo['ip']; 7288f522e9Sandi if($revinfo['user']){ 7388f522e9Sandi $info['editor'] = $revinfo['user']; 7488f522e9Sandi }else{ 7588f522e9Sandi $info['editor'] = $revinfo['ip']; 7688f522e9Sandi } 77652610a2Sandi 78f3f0262cSandi return $info; 79f3f0262cSandi} 80f3f0262cSandi 81f3f0262cSandi/** 822684e50aSAndreas Gohr * Build an string of URL parameters 832684e50aSAndreas Gohr * 842684e50aSAndreas Gohr * @author Andreas Gohr 852684e50aSAndreas Gohr */ 862684e50aSAndreas Gohrfunction buildURLparams($params){ 872684e50aSAndreas Gohr $url = ''; 882684e50aSAndreas Gohr $amp = false; 892684e50aSAndreas Gohr foreach($params as $key => $val){ 902684e50aSAndreas Gohr if($amp) $url .= '&'; 912684e50aSAndreas Gohr 922684e50aSAndreas Gohr $url .= $key.'='; 932684e50aSAndreas Gohr $url .= urlencode($val); 942684e50aSAndreas Gohr $amp = true; 952684e50aSAndreas Gohr } 962684e50aSAndreas Gohr return $url; 972684e50aSAndreas Gohr} 982684e50aSAndreas Gohr 992684e50aSAndreas Gohr/** 1002684e50aSAndreas Gohr * Build an string of html tag attributes 1012684e50aSAndreas Gohr * 1022684e50aSAndreas Gohr * @author Andreas Gohr 1032684e50aSAndreas Gohr */ 1042684e50aSAndreas Gohrfunction buildAttributes($params){ 1052684e50aSAndreas Gohr $url = ''; 1062684e50aSAndreas Gohr foreach($params as $key => $val){ 1072684e50aSAndreas Gohr $url .= $key.'="'; 1082684e50aSAndreas Gohr $url .= htmlspecialchars ($val); 1092684e50aSAndreas Gohr $url .= '" '; 1102684e50aSAndreas Gohr } 1112684e50aSAndreas Gohr return $url; 1122684e50aSAndreas Gohr} 1132684e50aSAndreas Gohr 1142684e50aSAndreas Gohr 1152684e50aSAndreas Gohr/** 1160396becbSandi * print a message 1170396becbSandi * 1180396becbSandi * If HTTP headers were not sent yet the message is added 1190396becbSandi * to the global message array else it's printed directly 1200396becbSandi * using html_msgarea() 1210396becbSandi * 122f3f0262cSandi * 123f3f0262cSandi * Levels can be: 124f3f0262cSandi * 125f3f0262cSandi * -1 error 126f3f0262cSandi * 0 info 127f3f0262cSandi * 1 success 12815fae107Sandi * 12915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1300396becbSandi * @see html_msgarea 131f3f0262cSandi */ 132f3f0262cSandifunction msg($message,$lvl=0){ 133f3f0262cSandi global $MSG; 134f3f0262cSandi $errors[-1] = 'error'; 135f3f0262cSandi $errors[0] = 'info'; 136f3f0262cSandi $errors[1] = 'success'; 137f3f0262cSandi 138cc20ad51Sandi if(!headers_sent()){ 139f3f0262cSandi if(!isset($MSG)) $MSG = array(); 140f3f0262cSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 1410396becbSandi }else{ 1420396becbSandi $MSG = array(); 1430396becbSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 144f62ea8a1Sandi if(function_exists('html_msgarea')){ 1450396becbSandi html_msgarea(); 146f62ea8a1Sandi }else{ 147f62ea8a1Sandi print "ERROR($lvl) $message"; 148f62ea8a1Sandi } 1490396becbSandi } 150f3f0262cSandi} 151f3f0262cSandi 152f3f0262cSandi/** 15315fae107Sandi * This builds the breadcrumb trail and returns it as array 15415fae107Sandi * 15515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 156f3f0262cSandi */ 157f3f0262cSandifunction breadcrumbs(){ 1588746e727Sandi // we prepare the breadcrumbs early for quick session closing 1598746e727Sandi static $crumbs = null; 1608746e727Sandi if($crumbs != null) return $crumbs; 1618746e727Sandi 162f3f0262cSandi global $ID; 163f3f0262cSandi global $ACT; 164f3f0262cSandi global $conf; 165f3f0262cSandi $crumbs = $_SESSION[$conf['title']]['bc']; 166f3f0262cSandi 167f3f0262cSandi //first visit? 168f3f0262cSandi if (!is_array($crumbs)){ 169f3f0262cSandi $crumbs = array(); 170f3f0262cSandi } 171f3f0262cSandi //we only save on show and existing wiki documents 172a77f5846Sjan $file = wikiFN($ID); 173a77f5846Sjan if($ACT != 'show' || !@file_exists($file)){ 174f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 175f3f0262cSandi return $crumbs; 176f3f0262cSandi } 177a77f5846Sjan 178a77f5846Sjan // page names 179a77f5846Sjan $name = noNS($ID); 180a77f5846Sjan if ($conf['useheading']) { 181a77f5846Sjan // get page title 182bb0a59d4Sjan $title = p_get_first_heading($ID); 183a77f5846Sjan if ($title) { 184a77f5846Sjan $name = $title; 185a77f5846Sjan } 186a77f5846Sjan } 187a77f5846Sjan 188f3f0262cSandi //remove ID from array 189a77f5846Sjan if (isset($crumbs[$ID])) { 190a77f5846Sjan unset($crumbs[$ID]); 191f3f0262cSandi } 192f3f0262cSandi 193f3f0262cSandi //add to array 194a77f5846Sjan $crumbs[$ID] = $name; 195f3f0262cSandi //reduce size 196f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']){ 197f3f0262cSandi array_shift($crumbs); 198f3f0262cSandi } 199f3f0262cSandi //save to session 200f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 201f3f0262cSandi return $crumbs; 202f3f0262cSandi} 203f3f0262cSandi 204f3f0262cSandi/** 20515fae107Sandi * Filter for page IDs 20615fae107Sandi * 207f3f0262cSandi * This is run on a ID before it is outputted somewhere 208f3f0262cSandi * currently used to replace the colon with something else 209f3f0262cSandi * on Windows systems and to have proper URL encoding 21015fae107Sandi * 21149c713a3Sandi * Urlencoding is ommitted when the second parameter is false 21249c713a3Sandi * 21315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 214f3f0262cSandi */ 21549c713a3Sandifunction idfilter($id,$ue=true){ 216f3f0262cSandi global $conf; 217f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']){ 218f3f0262cSandi $id = strtr($id,':','/'); 219f3f0262cSandi }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 220f3f0262cSandi $conf['userewrite']) { 221f3f0262cSandi $id = strtr($id,':',';'); 222f3f0262cSandi } 22349c713a3Sandi if($ue){ 224f3f0262cSandi $id = urlencode($id); 225f3f0262cSandi $id = str_replace('%3A',':',$id); //keep as colon 226f3f0262cSandi $id = str_replace('%2F','/',$id); //keep as slash 22749c713a3Sandi } 228f3f0262cSandi return $id; 229f3f0262cSandi} 230f3f0262cSandi 231f3f0262cSandi/** 232ed7b5f09Sandi * This builds a link to a wikipage 23315fae107Sandi * 2346c7843b5Sandi * It handles URL rewriting and adds additional parameter if 2356c7843b5Sandi * given in $more 2366c7843b5Sandi * 23715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 238f3f0262cSandi */ 239ed7b5f09Sandifunction wl($id='',$more='',$abs=false){ 240f3f0262cSandi global $conf; 241f3f0262cSandi $more = str_replace(',','&',$more); 242f3f0262cSandi 243f3f0262cSandi $id = idfilter($id); 244ed7b5f09Sandi if($abs){ 245ed7b5f09Sandi $xlink = DOKU_URL; 246ed7b5f09Sandi }else{ 247ed7b5f09Sandi $xlink = DOKU_BASE; 248ed7b5f09Sandi } 249f3f0262cSandi 2506c7843b5Sandi if($conf['userewrite'] == 2){ 2516c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 2526c7843b5Sandi if($more) $xlink .= '?'.$more; 2536c7843b5Sandi }elseif($conf['userewrite']){ 254f3f0262cSandi $xlink .= $id; 255f3f0262cSandi if($more) $xlink .= '?'.$more; 2566c7843b5Sandi }else{ 2576c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 2586c7843b5Sandi if($more) $xlink .= '&'.$more; 259f3f0262cSandi } 260f3f0262cSandi 261f3f0262cSandi return $xlink; 262f3f0262cSandi} 263f3f0262cSandi 264f3f0262cSandi/** 265f3f0262cSandi * Just builds a link to a script 26615fae107Sandi * 267ed7b5f09Sandi * @todo maybe obsolete 26815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 269f3f0262cSandi */ 270f3f0262cSandifunction script($script='doku.php'){ 271ed7b5f09Sandi# $link = getBaseURL(); 272ed7b5f09Sandi# $link .= $script; 273ed7b5f09Sandi# return $link; 274ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 275f3f0262cSandi} 276f3f0262cSandi 277f3f0262cSandi/** 27815fae107Sandi * Spamcheck against wordlist 27915fae107Sandi * 280f3f0262cSandi * Checks the wikitext against a list of blocked expressions 281f3f0262cSandi * returns true if the text contains any bad words 28215fae107Sandi * 28315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 284f3f0262cSandi */ 285f3f0262cSandifunction checkwordblock(){ 286f3f0262cSandi global $TEXT; 287f3f0262cSandi global $conf; 288f3f0262cSandi 289f3f0262cSandi if(!$conf['usewordblock']) return false; 290f3f0262cSandi 291e7cb32dcSAndreas Gohr $blockfile = file(DOKU_CONF.'wordblock.conf'); 2923e2965d7Sandi //how many lines to read at once (to work around some PCRE limits) 2933e2965d7Sandi if(version_compare(phpversion(),'4.3.0','<')){ 2943e2965d7Sandi //old versions of PCRE define a maximum of parenthesises even if no 2953e2965d7Sandi //backreferences are used - the maximum is 99 2963e2965d7Sandi //this is very bad performancewise and may even be too high still 2973e2965d7Sandi $chunksize = 40; 2983e2965d7Sandi }else{ 299703f6fdeSandi //read file in chunks of 600 - this should work around the 3003e2965d7Sandi //MAX_PATTERN_SIZE in modern PCRE 3013e2965d7Sandi $chunksize = 600; 3023e2965d7Sandi } 3033e2965d7Sandi while($blocks = array_splice($blockfile,0,$chunksize)){ 304f3f0262cSandi $re = array(); 305f3f0262cSandi #build regexp from blocks 306f3f0262cSandi foreach($blocks as $block){ 307f3f0262cSandi $block = preg_replace('/#.*$/','',$block); 308f3f0262cSandi $block = trim($block); 309f3f0262cSandi if(empty($block)) continue; 310f3f0262cSandi $re[] = $block; 311f3f0262cSandi } 312f3f0262cSandi if(preg_match('#('.join('|',$re).')#si',$TEXT)) return true; 313703f6fdeSandi } 314f3f0262cSandi return false; 315f3f0262cSandi} 316f3f0262cSandi 317f3f0262cSandi/** 31815fae107Sandi * Return the IP of the client 31915fae107Sandi * 32015fae107Sandi * Honours X-Forwarded-For Proxy Headers 32115fae107Sandi * 32215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 323f3f0262cSandi */ 324f3f0262cSandifunction clientIP(){ 325f3f0262cSandi $my = $_SERVER['REMOTE_ADDR']; 326f3f0262cSandi if($_SERVER['HTTP_X_FORWARDED_FOR']){ 327f3f0262cSandi $my .= ' ('.$_SERVER['HTTP_X_FORWARDED_FOR'].')'; 328f3f0262cSandi } 329f3f0262cSandi return $my; 330f3f0262cSandi} 331f3f0262cSandi 332f3f0262cSandi/** 33315fae107Sandi * Checks if a given page is currently locked. 33415fae107Sandi * 335f3f0262cSandi * removes stale lockfiles 33615fae107Sandi * 33715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 338f3f0262cSandi */ 339f3f0262cSandifunction checklock($id){ 340f3f0262cSandi global $conf; 341f3f0262cSandi $lock = wikiFN($id).'.lock'; 342f3f0262cSandi 343f3f0262cSandi //no lockfile 344f3f0262cSandi if(!@file_exists($lock)) return false; 345f3f0262cSandi 346f3f0262cSandi //lockfile expired 347f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']){ 348f3f0262cSandi unlink($lock); 349f3f0262cSandi return false; 350f3f0262cSandi } 351f3f0262cSandi 352f3f0262cSandi //my own lock 353f3f0262cSandi $ip = io_readFile($lock); 354f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 355f3f0262cSandi return false; 356f3f0262cSandi } 357f3f0262cSandi 358f3f0262cSandi return $ip; 359f3f0262cSandi} 360f3f0262cSandi 361f3f0262cSandi/** 36215fae107Sandi * Lock a page for editing 36315fae107Sandi * 36415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 365f3f0262cSandi */ 366f3f0262cSandifunction lock($id){ 367f3f0262cSandi $lock = wikiFN($id).'.lock'; 368f3f0262cSandi if($_SERVER['REMOTE_USER']){ 369f3f0262cSandi io_saveFile($lock,$_SERVER['REMOTE_USER']); 370f3f0262cSandi }else{ 371f3f0262cSandi io_saveFile($lock,clientIP()); 372f3f0262cSandi } 373f3f0262cSandi} 374f3f0262cSandi 375f3f0262cSandi/** 37615fae107Sandi * Unlock a page if it was locked by the user 377f3f0262cSandi * 37815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 37915fae107Sandi * @return bool true if a lock was removed 380f3f0262cSandi */ 381f3f0262cSandifunction unlock($id){ 382f3f0262cSandi $lock = wikiFN($id).'.lock'; 383f3f0262cSandi if(@file_exists($lock)){ 384f3f0262cSandi $ip = io_readFile($lock); 385f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 386f3f0262cSandi @unlink($lock); 387f3f0262cSandi return true; 388f3f0262cSandi } 389f3f0262cSandi } 390f3f0262cSandi return false; 391f3f0262cSandi} 392f3f0262cSandi 393f3f0262cSandi/** 394f3f0262cSandi * convert line ending to unix format 395f3f0262cSandi * 39615fae107Sandi * @see formText() for 2crlf conversion 39715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 398f3f0262cSandi */ 399f3f0262cSandifunction cleanText($text){ 400f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 401f3f0262cSandi return $text; 402f3f0262cSandi} 403f3f0262cSandi 404f3f0262cSandi/** 405f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 406f3f0262cSandi * It also converts line endings to Windows format which is 407f3f0262cSandi * pseudo standard for webforms. 408f3f0262cSandi * 40915fae107Sandi * @see cleanText() for 2unix conversion 41015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 411f3f0262cSandi */ 412f3f0262cSandifunction formText($text){ 413f3f0262cSandi $text = preg_replace("/\012/","\015\012",$text); 414f3f0262cSandi return htmlspecialchars($text); 415f3f0262cSandi} 416f3f0262cSandi 417f3f0262cSandi/** 41815fae107Sandi * Returns the specified local text in raw format 41915fae107Sandi * 42015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 421f3f0262cSandi */ 422f3f0262cSandifunction rawLocale($id){ 423f3f0262cSandi return io_readFile(localeFN($id)); 424f3f0262cSandi} 425f3f0262cSandi 426f3f0262cSandi/** 427f3f0262cSandi * Returns the raw WikiText 42815fae107Sandi * 42915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 430f3f0262cSandi */ 431f3f0262cSandifunction rawWiki($id,$rev=''){ 432f3f0262cSandi return io_readFile(wikiFN($id,$rev)); 433f3f0262cSandi} 434f3f0262cSandi 435f3f0262cSandi/** 4367146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 4377146cee2SAndreas Gohr * 4387146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 4397146cee2SAndreas Gohr */ 4407146cee2SAndreas Gohrfunction pageTemplate($id){ 4417146cee2SAndreas Gohr return io_readFile(dirname(wikiFN($id)).'/_template.txt'); 4427146cee2SAndreas Gohr} 4437146cee2SAndreas Gohr 4447146cee2SAndreas Gohr 4457146cee2SAndreas Gohr/** 44615fae107Sandi * Returns the raw Wiki Text in three slices. 44715fae107Sandi * 44815fae107Sandi * The range parameter needs to have the form "from-to" 44915cfe303Sandi * and gives the range of the section in bytes - no 45015cfe303Sandi * UTF-8 awareness is needed. 451f3f0262cSandi * The returned order is prefix, section and suffix. 45215fae107Sandi * 45315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 454f3f0262cSandi */ 455f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){ 456f3f0262cSandi list($from,$to) = split('-',$range,2); 457f3f0262cSandi $text = io_readFile(wikiFN($id,$rev)); 458f3f0262cSandi if(!$from) $from = 0; 459c3d8e19bSandi if(!$to) $to = strlen($text)+1; 460f3f0262cSandi 46115cfe303Sandi $slices[0] = substr($text,0,$from-1); 46215cfe303Sandi $slices[1] = substr($text,$from-1,$to-$from); 46315cfe303Sandi $slices[2] = substr($text,$to); 464f3f0262cSandi 465f3f0262cSandi return $slices; 466f3f0262cSandi} 467f3f0262cSandi 468f3f0262cSandi/** 46915fae107Sandi * Joins wiki text slices 47015fae107Sandi * 471f3f0262cSandi * function to join the text slices with correct lineendings again. 472f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 473f3f0262cSandi * lines between sections if needed (used on saving). 47415fae107Sandi * 47515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 476f3f0262cSandi */ 477f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){ 478f3f0262cSandi 479f3f0262cSandi if($pretty){ 480f3f0262cSandi if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 481f3f0262cSandi if($suf && substr($text,-1) != "\n") $text .= "\n"; 482f3f0262cSandi } 483f3f0262cSandi 484f3f0262cSandi if($pre) $pre .= "\n"; 485f3f0262cSandi if($suf) $text .= "\n"; 486f3f0262cSandi return $pre.$text.$suf; 487f3f0262cSandi} 488f3f0262cSandi 489f3f0262cSandi/** 49015fae107Sandi * print debug messages 49115fae107Sandi * 492f3f0262cSandi * little function to print the content of a var 49315fae107Sandi * 49415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 495f3f0262cSandi */ 496f3f0262cSandifunction dbg($msg,$hidden=false){ 497f3f0262cSandi (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 498f3f0262cSandi print_r($msg); 499f3f0262cSandi (!$hidden) ? print '</pre>' : print "\n-->"; 500f3f0262cSandi} 501f3f0262cSandi 502f3f0262cSandi/** 503f3f0262cSandi * Add's an entry to the changelog 50415fae107Sandi * 50515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 506f3f0262cSandi */ 507652610a2Sandifunction addLogEntry($date,$id,$summary=""){ 508f3f0262cSandi global $conf; 509c1049928Sandi $id = cleanID($id);//FIXME not needed anymore? 510c1049928Sandi 511c1049928Sandi if(!@is_writable($conf['changelog'])){ 512c1049928Sandi msg($conf['changelog'].' is not writable!',-1); 513c1049928Sandi return; 514c1049928Sandi } 515c1049928Sandi 516652610a2Sandi if(!$date) $date = time(); //use current time if none supplied 517f3f0262cSandi $remote = $_SERVER['REMOTE_ADDR']; 518f3f0262cSandi $user = $_SERVER['REMOTE_USER']; 519f3f0262cSandi 520f3f0262cSandi $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n"; 521f3f0262cSandi 522c1049928Sandi //FIXME: use adjusted io_saveFile instead 523f3f0262cSandi $fh = fopen($conf['changelog'],'a'); 524f3f0262cSandi if($fh){ 525f3f0262cSandi fwrite($fh,$logline); 526f3f0262cSandi fclose($fh); 527f3f0262cSandi } 528f3f0262cSandi} 529f3f0262cSandi 530f3f0262cSandi/** 531f3f0262cSandi * returns an array of recently changed files using the 532f3f0262cSandi * changelog 5335749f1ceSmatthiasgrimm * first : first entry in array returned 534a39955b0Smatthiasgrimm * num : return 'num' entries 53515fae107Sandi * 53615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 537f3f0262cSandi */ 5385749f1ceSmatthiasgrimmfunction getRecents($first,$num,$incdel=false){ 539f3f0262cSandi global $conf; 540f3f0262cSandi $recent = array(); 5415749f1ceSmatthiasgrimm $names = array(); 5425749f1ceSmatthiasgrimm 5435749f1ceSmatthiasgrimm if(!$num) 5445749f1ceSmatthiasgrimm return $recent; 545f3f0262cSandi 546c1049928Sandi if(!@is_readable($conf['changelog'])){ 547c1049928Sandi msg($conf['changelog'].' is not readable',-1); 548c1049928Sandi return $recent; 549c1049928Sandi } 550c1049928Sandi 551f3f0262cSandi $loglines = file($conf['changelog']); 552f3f0262cSandi rsort($loglines); //reverse sort on timestamp 553f3f0262cSandi 554f3f0262cSandi foreach ($loglines as $line){ 555f3f0262cSandi $line = rtrim($line); //remove newline 556f3f0262cSandi if(empty($line)) continue; //skip empty lines 557f3f0262cSandi $info = split("\t",$line); //split into parts 558f3f0262cSandi //add id if not in yet and file still exists and is allowed to read 5595749f1ceSmatthiasgrimm if(!$names[$info[2]] && 560f3f0262cSandi (@file_exists(wikiFN($info[2])) || $incdel) && 561f3f0262cSandi (auth_quickaclcheck($info[2]) >= AUTH_READ) 562f3f0262cSandi ){ 5635749f1ceSmatthiasgrimm $names[$info[2]] = 1; 5645749f1ceSmatthiasgrimm if(--$first >= 0) continue; /* skip "first" entries */ 5655749f1ceSmatthiasgrimm 566f3f0262cSandi $recent[$info[2]]['date'] = $info[0]; 567f3f0262cSandi $recent[$info[2]]['ip'] = $info[1]; 568f3f0262cSandi $recent[$info[2]]['user'] = $info[3]; 569f3f0262cSandi $recent[$info[2]]['sum'] = $info[4]; 570f3f0262cSandi $recent[$info[2]]['del'] = !@file_exists(wikiFN($info[2])); 571f3f0262cSandi } 5725749f1ceSmatthiasgrimm if(count($recent) >= $num){ 573f3f0262cSandi break; //finish if enough items found 574f3f0262cSandi } 575f3f0262cSandi } 576f3f0262cSandi return $recent; 577f3f0262cSandi} 578f3f0262cSandi 579f3f0262cSandi/** 580652610a2Sandi * gets additonal informations for a certain pagerevison 581652610a2Sandi * from the changelog 582652610a2Sandi * 583652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org> 584652610a2Sandi */ 585652610a2Sandifunction getRevisionInfo($id,$rev){ 586652610a2Sandi global $conf; 587258641c6Sandi 588258641c6Sandi if(!$rev) return(null); 589258641c6Sandi 590c1049928Sandi $info = array(); 591c1049928Sandi if(!@is_readable($conf['changelog'])){ 592c1049928Sandi msg($conf['changelog'].' is not readable',-1); 593c1049928Sandi return $recent; 594c1049928Sandi } 595652610a2Sandi $loglines = file($conf['changelog']); 596652610a2Sandi $loglines = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines); 597dc42ff59Sandi $loglines = array_reverse($loglines); //reverse sort on timestamp (shouldn't be needed) 598652610a2Sandi $line = split("\t",$loglines[0]); 599652610a2Sandi $info['date'] = $line[0]; 600652610a2Sandi $info['ip'] = $line[1]; 601652610a2Sandi $info['user'] = $line[3]; 602652610a2Sandi $info['sum'] = $line[4]; 603652610a2Sandi return $info; 604652610a2Sandi} 605652610a2Sandi 606652610a2Sandi/** 607f3f0262cSandi * Saves a wikitext by calling io_saveFile 60815fae107Sandi * 60915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 610f3f0262cSandi */ 611f3f0262cSandifunction saveWikiText($id,$text,$summary){ 612f3f0262cSandi global $conf; 613f3f0262cSandi global $lang; 614f3f0262cSandi umask($conf['umask']); 615f3f0262cSandi // ignore if no changes were made 616f3f0262cSandi if($text == rawWiki($id,'')){ 617f3f0262cSandi return; 618f3f0262cSandi } 619f3f0262cSandi 620f3f0262cSandi $file = wikiFN($id); 621f3f0262cSandi $old = saveOldRevision($id); 622f3f0262cSandi 623f3f0262cSandi if (empty($text)){ 624f3f0262cSandi // remove empty files 625f3f0262cSandi @unlink($file); 626b158d625SSteven Danz $mfile=wikiMN($id); 627b158d625SSteven Danz if (file_exists($mfile)) { 628b158d625SSteven Danz @unlink($mfile); 629b158d625SSteven Danz } 630f3f0262cSandi $del = true; 6313ce054b3Sandi //autoset summary on deletion 6323ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 63353d6ccfeSandi //remove empty namespaces 63453d6ccfeSandi io_sweepNS($id); 635f3f0262cSandi }else{ 636f3f0262cSandi // save file (datadir is created in io_saveFile) 637f3f0262cSandi io_saveFile($file,$text); 638f3f0262cSandi $del = false; 639f3f0262cSandi } 640f3f0262cSandi 641652610a2Sandi addLogEntry(@filemtime($file),$id,$summary); 642f3f0262cSandi notify($id,$old,$summary); 643f3f0262cSandi 644f3f0262cSandi //purge cache on add by updating the purgefile 645f3f0262cSandi if($conf['purgeonadd'] && (!$old || $del)){ 64698407a7aSandi io_saveFile($conf['cachedir'].'/purgefile',time()); 647f3f0262cSandi } 648f3f0262cSandi} 649f3f0262cSandi 650f3f0262cSandi/** 651f3f0262cSandi * moves the current version to the attic and returns its 652f3f0262cSandi * revision date 65315fae107Sandi * 65415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 655f3f0262cSandi */ 656f3f0262cSandifunction saveOldRevision($id){ 657f3f0262cSandi global $conf; 658f3f0262cSandi umask($conf['umask']); 659f3f0262cSandi $oldf = wikiFN($id); 660f3f0262cSandi if(!@file_exists($oldf)) return ''; 661f3f0262cSandi $date = filemtime($oldf); 662f3f0262cSandi $newf = wikiFN($id,$date); 663f3f0262cSandi if(substr($newf,-3)=='.gz'){ 664f3f0262cSandi io_saveFile($newf,rawWiki($id)); 665f3f0262cSandi }else{ 666f3f0262cSandi io_makeFileDir($newf); 667f3f0262cSandi copy($oldf, $newf); 668f3f0262cSandi } 669f3f0262cSandi return $date; 670f3f0262cSandi} 671f3f0262cSandi 672f3f0262cSandi/** 673f3f0262cSandi * Sends a notify mail to the wikiadmin when a page was 674f3f0262cSandi * changed 67515fae107Sandi * 67615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 677f3f0262cSandi */ 678f3f0262cSandifunction notify($id,$rev="",$summary=""){ 679f3f0262cSandi global $lang; 680f3f0262cSandi global $conf; 681f3f0262cSandi $hdrs =''; 682b158d625SSteven Danz 683b158d625SSteven Danz 684b158d625SSteven Danz if(empty($conf['notify']) && count($mlist) == 0) return; //notify enabled? 685f3f0262cSandi 686f3f0262cSandi $text = rawLocale('mailtext'); 687f3f0262cSandi $text = str_replace('@DATE@',date($conf['dformat']),$text); 688f3f0262cSandi $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 689f3f0262cSandi $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 690f3f0262cSandi $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 691ed7b5f09Sandi $text = str_replace('@NEWPAGE@',wl($id,'',true),$text); 692ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 693f3f0262cSandi $text = str_replace('@SUMMARY@',$summary,$text); 6947a82afdcSandi $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 695f3f0262cSandi 696f3f0262cSandi if($rev){ 697f3f0262cSandi $subject = $lang['mail_changed'].' '.$id; 698ed7b5f09Sandi $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text); 699f3f0262cSandi require_once("inc/DifferenceEngine.php"); 700f3f0262cSandi $df = new Diff(split("\n",rawWiki($id,$rev)), 701f3f0262cSandi split("\n",rawWiki($id))); 702f3f0262cSandi $dformat = new UnifiedDiffFormatter(); 703f3f0262cSandi $diff = $dformat->format($df); 704f3f0262cSandi }else{ 705f3f0262cSandi $subject=$lang['mail_newpage'].' '.$id; 706f3f0262cSandi $text = str_replace('@OLDPAGE@','none',$text); 707f3f0262cSandi $diff = rawWiki($id); 708f3f0262cSandi } 709f3f0262cSandi $text = str_replace('@DIFF@',$diff,$text); 710241f3a36Sandi $subject = '['.$conf['title'].'] '.$subject; 711f3f0262cSandi 712*1380fc45SAndreas Gohr 713*1380fc45SAndreas Gohr // FIXME move this to its own function 714*1380fc45SAndreas Gohr $mlist = array(); 715*1380fc45SAndreas Gohr $file=metaFN($id,'.mlist'); 716*1380fc45SAndreas Gohr if (file_exists($file)) { 717*1380fc45SAndreas Gohr $mlist = file($file); 718*1380fc45SAndreas Gohr } 719*1380fc45SAndreas Gohr 720b158d625SSteven Danz $bcc = ''; 721b158d625SSteven Danz if(count($mlist) > 0) { 722b158d625SSteven Danz foreach ($mlist as $who) { 723b158d625SSteven Danz $who = rtrim($who); 724b158d625SSteven Danz $info = auth_getUserData($who); 725b158d625SSteven Danz $level = auth_aclcheck($id,$who,$info['grps']); 726b158d625SSteven Danz if ($level >= AUTH_READ) { 727b158d625SSteven Danz if (strcasecmp($info['mail'],$conf['notify']) != 0) { 728b158d625SSteven Danz if (empty($bcc)) { 729b158d625SSteven Danz $bcc = $info['mail']; 730b158d625SSteven Danz } else { 731b158d625SSteven Danz $bcc = "$bcc,".$info['mail']; 732b158d625SSteven Danz } 733b158d625SSteven Danz } 734b158d625SSteven Danz } 735b158d625SSteven Danz } 736b158d625SSteven Danz } 737b158d625SSteven Danz 738b158d625SSteven Danz mail_send($conf['notify'],$subject,$text,$conf['mailfrom'],'',$bcc); 739f3f0262cSandi} 740f3f0262cSandi 74115fae107Sandi/** 74215fae107Sandi * Return a list of available page revisons 74315fae107Sandi * 74415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 74515fae107Sandi */ 746f3f0262cSandifunction getRevisions($id){ 747f3f0262cSandi $revd = dirname(wikiFN($id,'foo')); 748f3f0262cSandi $revs = array(); 749f3f0262cSandi $clid = cleanID($id); 750f3f0262cSandi if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path 751493a6929SKobaYY $clid = utf8_encodeFN($clid); 752f3f0262cSandi 753f3f0262cSandi if (is_dir($revd) && $dh = opendir($revd)) { 754f3f0262cSandi while (($file = readdir($dh)) !== false) { 755f3f0262cSandi if (is_dir($revd.'/'.$file)) continue; 756f3f0262cSandi if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){ 757f3f0262cSandi $revs[]=$match[1]; 758f3f0262cSandi } 759f3f0262cSandi } 760f3f0262cSandi closedir($dh); 761f3f0262cSandi } 762f3f0262cSandi rsort($revs); 763f3f0262cSandi return $revs; 764f3f0262cSandi} 765f3f0262cSandi 766f3f0262cSandi/** 767f3f0262cSandi * extracts the query from a google referer 76815fae107Sandi * 7696b13307fSandi * @todo should be more generic and support yahoo et al 77015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 771f3f0262cSandi */ 772f3f0262cSandifunction getGoogleQuery(){ 773f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 7745c3f206fSandi if(!$url) return ''; 775f3f0262cSandi 776f3f0262cSandi if(!preg_match("#google\.#i",$url['host'])) return ''; 777f3f0262cSandi $query = array(); 778f3f0262cSandi parse_str($url['query'],$query); 779f3f0262cSandi 780f3f0262cSandi return $query['q']; 781f3f0262cSandi} 782f3f0262cSandi 783f3f0262cSandi/** 78415fae107Sandi * Try to set correct locale 78515fae107Sandi * 786095bfd5cSandi * @deprecated No longer used 78715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 788f3f0262cSandi */ 789f3f0262cSandifunction setCorrectLocale(){ 790f3f0262cSandi global $conf; 791f3f0262cSandi global $lang; 792f3f0262cSandi 793f3f0262cSandi $enc = strtoupper($lang['encoding']); 794f3f0262cSandi foreach ($lang['locales'] as $loc){ 795f3f0262cSandi //try locale 796f3f0262cSandi if(@setlocale(LC_ALL,$loc)) return; 797f3f0262cSandi //try loceale with encoding 798f3f0262cSandi if(@setlocale(LC_ALL,"$loc.$enc")) return; 799f3f0262cSandi } 800f3f0262cSandi //still here? try to set from environment 801f3f0262cSandi @setlocale(LC_ALL,""); 802f3f0262cSandi} 803f3f0262cSandi 804f3f0262cSandi/** 805f3f0262cSandi * Return the human readable size of a file 806f3f0262cSandi * 807f3f0262cSandi * @param int $size A file size 808f3f0262cSandi * @param int $dec A number of decimal places 809f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 810f3f0262cSandi * @author Aidan Lister <aidan@php.net> 811f3f0262cSandi * @version 1.0.0 812f3f0262cSandi */ 813f31d5b73Sandifunction filesize_h($size, $dec = 1){ 814f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 815f3f0262cSandi $count = count($sizes); 816f3f0262cSandi $i = 0; 817f3f0262cSandi 818f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 819f3f0262cSandi $size /= 1024; 820f3f0262cSandi $i++; 821f3f0262cSandi } 822f3f0262cSandi 823f3f0262cSandi return round($size, $dec) . ' ' . $sizes[$i]; 824f3f0262cSandi} 825f3f0262cSandi 82615fae107Sandi/** 827dc57ef04Sandi * Return DokuWikis version 82815fae107Sandi * 82915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 83015fae107Sandi */ 831f31d5b73Sandifunction getVersion(){ 832f31d5b73Sandi //import version string 833f31d5b73Sandi if(@file_exists('VERSION')){ 834f31d5b73Sandi //official release 835ec052310Sandi return 'Release '.trim(io_readfile('VERSION')); 836f31d5b73Sandi }elseif(is_dir('_darcs')){ 837f31d5b73Sandi //darcs checkout 838f31d5b73Sandi $inv = file('_darcs/inventory'); 839f31d5b73Sandi $inv = preg_grep('#andi@splitbrain\.org\*\*\d{14}#',$inv); 840f31d5b73Sandi $cur = array_pop($inv); 841f31d5b73Sandi preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 842f31d5b73Sandi return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 843f31d5b73Sandi }else{ 844f31d5b73Sandi return 'snapshot?'; 845f31d5b73Sandi } 846f31d5b73Sandi} 847f31d5b73Sandi 848f31d5b73Sandi/** 849f31d5b73Sandi * Run a few sanity checks 850f31d5b73Sandi * 851f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org> 852f31d5b73Sandi */ 853f3f0262cSandifunction check(){ 854f3f0262cSandi global $conf; 855f3f0262cSandi global $INFO; 856f3f0262cSandi 857f31d5b73Sandi msg('DokuWiki version: '.getVersion(),1); 858f31d5b73Sandi 85949022a38Sandi if(version_compare(phpversion(),'4.3.0','<')){ 86049022a38Sandi msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 86149022a38Sandi }elseif(version_compare(phpversion(),'4.3.10','<')){ 86249022a38Sandi msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 86349022a38Sandi }else{ 86449022a38Sandi msg('PHP version '.phpversion(),1); 86549022a38Sandi } 86649022a38Sandi 867f3f0262cSandi if(is_writable($conf['changelog'])){ 868f3f0262cSandi msg('Changelog is writable',1); 869f3f0262cSandi }else{ 870f3f0262cSandi msg('Changelog is not writable',-1); 871f3f0262cSandi } 872f3f0262cSandi 873f3f0262cSandi if(is_writable($conf['datadir'])){ 874f3f0262cSandi msg('Datadir is writable',1); 875f3f0262cSandi }else{ 876f3f0262cSandi msg('Datadir is not writable',-1); 877f3f0262cSandi } 878f3f0262cSandi 879f3f0262cSandi if(is_writable($conf['olddir'])){ 880f3f0262cSandi msg('Attic is writable',1); 881f3f0262cSandi }else{ 882f3f0262cSandi msg('Attic is not writable',-1); 883f3f0262cSandi } 884f3f0262cSandi 885f3f0262cSandi if(is_writable($conf['mediadir'])){ 886f3f0262cSandi msg('Mediadir is writable',1); 887f3f0262cSandi }else{ 888f3f0262cSandi msg('Mediadir is not writable',-1); 889f3f0262cSandi } 890f3f0262cSandi 89198407a7aSandi if(is_writable($conf['cachedir'])){ 89298407a7aSandi msg('Cachedir is writable',1); 89398407a7aSandi }else{ 89498407a7aSandi msg('Cachedir is not writable',-1); 89598407a7aSandi } 89698407a7aSandi 897e7cb32dcSAndreas Gohr if(is_writable(DOKU_CONF.'users.auth.php')){ 8988c4f28e8Sjan msg('conf/users.auth.php is writable',1); 899f3f0262cSandi }else{ 9008c4f28e8Sjan msg('conf/users.auth.php is not writable',0); 901f3f0262cSandi } 90293a9e835Sandi 90393a9e835Sandi if(function_exists('mb_strpos')){ 90493a9e835Sandi if(defined('UTF8_NOMBSTRING')){ 90593a9e835Sandi msg('mb_string extension is available but will not be used',0); 90693a9e835Sandi }else{ 90793a9e835Sandi msg('mb_string extension is available and will be used',1); 90893a9e835Sandi } 90993a9e835Sandi }else{ 91093a9e835Sandi msg('mb_string extension not available - PHP only replacements will be used',0); 91193a9e835Sandi } 912f3f0262cSandi 913f3f0262cSandi msg('Your current permission for this page is '.$INFO['perm'],0); 914f3f0262cSandi 915f3f0262cSandi if(is_writable($INFO['filepath'])){ 916f3f0262cSandi msg('The current page is writable by the webserver',0); 917f3f0262cSandi }else{ 918f3f0262cSandi msg('The current page is not writable by the webserver',0); 919f3f0262cSandi } 920f3f0262cSandi 921f3f0262cSandi if($INFO['writable']){ 922f3f0262cSandi msg('The current page is writable by you',0); 923f3f0262cSandi }else{ 924f3f0262cSandi msg('The current page is not writable you',0); 925f3f0262cSandi } 926f3f0262cSandi} 927340756e4Sandi 928b158d625SSteven Danz/** 929b158d625SSteven Danz * Let us know if a user is tracking a page 930b158d625SSteven Danz * 931*1380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 932b158d625SSteven Danz */ 933*1380fc45SAndreas Gohrfunction is_subscribed($id,$uid){ 934*1380fc45SAndreas Gohr $file=metaFN($id,'.mlist'); 935*1380fc45SAndreas Gohr if (@file_exists($file)) { 936b158d625SSteven Danz $mlist = file($file); 937*1380fc45SAndreas Gohr $pos = array_search($uid."\n",$mlist); 938*1380fc45SAndreas Gohr return is_int($pos); 939b158d625SSteven Danz } 940*1380fc45SAndreas Gohr 941b158d625SSteven Danz return false; 942b158d625SSteven Danz} 943340756e4Sandi 944340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 945