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/** 17b6912aeaSAndreas Gohr * These constants are used with the recents function 18b6912aeaSAndreas Gohr */ 19b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED',2); 20b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS',4); 21b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES',8); 22b6912aeaSAndreas Gohr 23b6912aeaSAndreas Gohr/** 24d5197206Schris * Wrapper around htmlspecialchars() 25d5197206Schris * 26d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 27d5197206Schris * @see htmlspecialchars() 28d5197206Schris */ 29d5197206Schrisfunction hsc($string){ 30d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 31d5197206Schris} 32d5197206Schris 33d5197206Schris/** 34d5197206Schris * print a newline terminated string 35d5197206Schris * 36d5197206Schris * You can give an indention as optional parameter 37d5197206Schris * 38d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 39d5197206Schris */ 40d5197206Schrisfunction ptln($string,$intend=0){ 41d5197206Schris for($i=0; $i<$intend; $i++) print ' '; 42d5197206Schris print"$string\n"; 43d5197206Schris} 44d5197206Schris 45d5197206Schris/** 4615fae107Sandi * Return info about the current document as associative 47f3f0262cSandi * array. 4815fae107Sandi * 4915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 50f3f0262cSandi */ 51f3f0262cSandifunction pageinfo(){ 52f3f0262cSandi global $ID; 53f3f0262cSandi global $REV; 54f3f0262cSandi global $USERINFO; 55f3f0262cSandi global $conf; 56f3f0262cSandi 57f3f0262cSandi if($_SERVER['REMOTE_USER']){ 58f3f0262cSandi $info['userinfo'] = $USERINFO; 59f3f0262cSandi $info['perm'] = auth_quickaclcheck($ID); 601380fc45SAndreas Gohr $info['subscribed'] = is_subscribed($ID,$_SERVER['REMOTE_USER']); 61ee4c4a1bSAndreas Gohr $info['client'] = $_SERVER['REMOTE_USER']; 6217ee7f66SAndreas Gohr 6317ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 6417ee7f66SAndreas Gohr if(!$info['userinfo']['name']){ 6517ee7f66SAndreas Gohr $info['userinfo']['name'] = $_SERVER['REMOTE_USER']; 6617ee7f66SAndreas Gohr } 67ee4c4a1bSAndreas Gohr 68f3f0262cSandi }else{ 69f3f0262cSandi $info['perm'] = auth_aclcheck($ID,'',null); 701380fc45SAndreas Gohr $info['subscribed'] = false; 71ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 72f3f0262cSandi } 73f3f0262cSandi 74f3f0262cSandi $info['namespace'] = getNS($ID); 75f3f0262cSandi $info['locked'] = checklock($ID); 76f3f0262cSandi $info['filepath'] = realpath(wikiFN($ID,$REV)); 77f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 78f3f0262cSandi if($REV && !$info['exists']){ 79f3f0262cSandi //check if current revision was meant 80f3f0262cSandi $cur = wikiFN($ID); 81f3f0262cSandi if(@file_exists($cur) && (@filemtime($cur) == $REV)){ 82f3f0262cSandi $info['filepath'] = realpath($cur); 83f3f0262cSandi $info['exists'] = true; 84f3f0262cSandi $REV = ''; 85f3f0262cSandi } 86f3f0262cSandi } 87c112d578Sandi $info['rev'] = $REV; 88f3f0262cSandi if($info['exists']){ 89f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 90f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 91f3f0262cSandi }else{ 92f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 93f3f0262cSandi } 94f3f0262cSandi $info['editable'] = ($info['writable'] && empty($info['lock'])); 95f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 96f3f0262cSandi 9771726d78SBen Coburn //load page meta data 9871726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 9971726d78SBen Coburn 100652610a2Sandi //who's the editor 101652610a2Sandi if($REV){ 10271726d78SBen Coburn $revinfo = getRevisionInfo($ID, $REV, 1024); 103652610a2Sandi }else{ 10471726d78SBen Coburn $revinfo = $info['meta']['last_change']; 105652610a2Sandi } 106652610a2Sandi $info['ip'] = $revinfo['ip']; 107652610a2Sandi $info['user'] = $revinfo['user']; 108652610a2Sandi $info['sum'] = $revinfo['sum']; 10971726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 11071726d78SBen Coburn // Use $INFO['meta']['last_change']['type']==='e' in place of $info['minor']. 11159f257aeSchris 11288f522e9Sandi if($revinfo['user']){ 11388f522e9Sandi $info['editor'] = $revinfo['user']; 11488f522e9Sandi }else{ 11588f522e9Sandi $info['editor'] = $revinfo['ip']; 11688f522e9Sandi } 117652610a2Sandi 118ee4c4a1bSAndreas Gohr // draft 119ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID,'.draft'); 120ee4c4a1bSAndreas Gohr if(@file_exists($draft)){ 121ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))){ 122ee4c4a1bSAndreas Gohr // remove stale draft 123ee4c4a1bSAndreas Gohr @unlink($draft); 124ee4c4a1bSAndreas Gohr }else{ 125ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 126ee4c4a1bSAndreas Gohr } 127ee4c4a1bSAndreas Gohr } 128ee4c4a1bSAndreas Gohr 129f3f0262cSandi return $info; 130f3f0262cSandi} 131f3f0262cSandi 132f3f0262cSandi/** 1332684e50aSAndreas Gohr * Build an string of URL parameters 1342684e50aSAndreas Gohr * 1352684e50aSAndreas Gohr * @author Andreas Gohr 1362684e50aSAndreas Gohr */ 137b174aeaeSchrisfunction buildURLparams($params, $sep='&'){ 1382684e50aSAndreas Gohr $url = ''; 1392684e50aSAndreas Gohr $amp = false; 1402684e50aSAndreas Gohr foreach($params as $key => $val){ 141b174aeaeSchris if($amp) $url .= $sep; 1422684e50aSAndreas Gohr 1432684e50aSAndreas Gohr $url .= $key.'='; 144b6c6979fSAndreas Gohr $url .= rawurlencode($val); 1452684e50aSAndreas Gohr $amp = true; 1462684e50aSAndreas Gohr } 1472684e50aSAndreas Gohr return $url; 1482684e50aSAndreas Gohr} 1492684e50aSAndreas Gohr 1502684e50aSAndreas Gohr/** 1512684e50aSAndreas Gohr * Build an string of html tag attributes 1522684e50aSAndreas Gohr * 1532684e50aSAndreas Gohr * @author Andreas Gohr 1542684e50aSAndreas Gohr */ 1552684e50aSAndreas Gohrfunction buildAttributes($params){ 1562684e50aSAndreas Gohr $url = ''; 1572684e50aSAndreas Gohr foreach($params as $key => $val){ 1582684e50aSAndreas Gohr $url .= $key.'="'; 1592684e50aSAndreas Gohr $url .= htmlspecialchars ($val); 1602684e50aSAndreas Gohr $url .= '" '; 1612684e50aSAndreas Gohr } 1622684e50aSAndreas Gohr return $url; 1632684e50aSAndreas Gohr} 1642684e50aSAndreas Gohr 1652684e50aSAndreas Gohr 1662684e50aSAndreas Gohr/** 1670396becbSandi * print a message 1680396becbSandi * 1690396becbSandi * If HTTP headers were not sent yet the message is added 1700396becbSandi * to the global message array else it's printed directly 1710396becbSandi * using html_msgarea() 1720396becbSandi * 173f3f0262cSandi * 174f3f0262cSandi * Levels can be: 175f3f0262cSandi * 176f3f0262cSandi * -1 error 177f3f0262cSandi * 0 info 178f3f0262cSandi * 1 success 17915fae107Sandi * 18015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1810396becbSandi * @see html_msgarea 182f3f0262cSandi */ 1830d58d74eSAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){ 184f3f0262cSandi global $MSG; 185f3f0262cSandi $errors[-1] = 'error'; 186f3f0262cSandi $errors[0] = 'info'; 187f3f0262cSandi $errors[1] = 'success'; 188f3f0262cSandi 1890d58d74eSAndreas Gohr if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 1900d58d74eSAndreas Gohr 191cc20ad51Sandi if(!headers_sent()){ 192f3f0262cSandi if(!isset($MSG)) $MSG = array(); 193f3f0262cSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 1940396becbSandi }else{ 1950396becbSandi $MSG = array(); 1960396becbSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 197f62ea8a1Sandi if(function_exists('html_msgarea')){ 1980396becbSandi html_msgarea(); 199f62ea8a1Sandi }else{ 200f62ea8a1Sandi print "ERROR($lvl) $message"; 201f62ea8a1Sandi } 2020396becbSandi } 203f3f0262cSandi} 204f3f0262cSandi 205f3f0262cSandi/** 20615fae107Sandi * This builds the breadcrumb trail and returns it as array 20715fae107Sandi * 20815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 209f3f0262cSandi */ 210f3f0262cSandifunction breadcrumbs(){ 2118746e727Sandi // we prepare the breadcrumbs early for quick session closing 2128746e727Sandi static $crumbs = null; 2138746e727Sandi if($crumbs != null) return $crumbs; 2148746e727Sandi 215f3f0262cSandi global $ID; 216f3f0262cSandi global $ACT; 217f3f0262cSandi global $conf; 218f3f0262cSandi $crumbs = $_SESSION[$conf['title']]['bc']; 219f3f0262cSandi 220f3f0262cSandi //first visit? 221f3f0262cSandi if (!is_array($crumbs)){ 222f3f0262cSandi $crumbs = array(); 223f3f0262cSandi } 224f3f0262cSandi //we only save on show and existing wiki documents 225a77f5846Sjan $file = wikiFN($ID); 226a77f5846Sjan if($ACT != 'show' || !@file_exists($file)){ 227f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 228f3f0262cSandi return $crumbs; 229f3f0262cSandi } 230a77f5846Sjan 231a77f5846Sjan // page names 232a77f5846Sjan $name = noNS($ID); 233a77f5846Sjan if ($conf['useheading']) { 234a77f5846Sjan // get page title 235bb0a59d4Sjan $title = p_get_first_heading($ID); 236a77f5846Sjan if ($title) { 237a77f5846Sjan $name = $title; 238a77f5846Sjan } 239a77f5846Sjan } 240a77f5846Sjan 241f3f0262cSandi //remove ID from array 242a77f5846Sjan if (isset($crumbs[$ID])) { 243a77f5846Sjan unset($crumbs[$ID]); 244f3f0262cSandi } 245f3f0262cSandi 246f3f0262cSandi //add to array 247a77f5846Sjan $crumbs[$ID] = $name; 248f3f0262cSandi //reduce size 249f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']){ 250f3f0262cSandi array_shift($crumbs); 251f3f0262cSandi } 252f3f0262cSandi //save to session 253f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 254f3f0262cSandi return $crumbs; 255f3f0262cSandi} 256f3f0262cSandi 257f3f0262cSandi/** 25815fae107Sandi * Filter for page IDs 25915fae107Sandi * 260f3f0262cSandi * This is run on a ID before it is outputted somewhere 261f3f0262cSandi * currently used to replace the colon with something else 262f3f0262cSandi * on Windows systems and to have proper URL encoding 26315fae107Sandi * 26449c713a3Sandi * Urlencoding is ommitted when the second parameter is false 26549c713a3Sandi * 26615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 267f3f0262cSandi */ 26849c713a3Sandifunction idfilter($id,$ue=true){ 269f3f0262cSandi global $conf; 270f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']){ 271f3f0262cSandi $id = strtr($id,':','/'); 272f3f0262cSandi }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 273f3f0262cSandi $conf['userewrite']) { 274f3f0262cSandi $id = strtr($id,':',';'); 275f3f0262cSandi } 27649c713a3Sandi if($ue){ 277b6c6979fSAndreas Gohr $id = rawurlencode($id); 278f3f0262cSandi $id = str_replace('%3A',':',$id); //keep as colon 279f3f0262cSandi $id = str_replace('%2F','/',$id); //keep as slash 28049c713a3Sandi } 281f3f0262cSandi return $id; 282f3f0262cSandi} 283f3f0262cSandi 284f3f0262cSandi/** 285ed7b5f09Sandi * This builds a link to a wikipage 28615fae107Sandi * 2876c7843b5Sandi * It handles URL rewriting and adds additional parameter if 2886c7843b5Sandi * given in $more 2896c7843b5Sandi * 29015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 291f3f0262cSandi */ 292b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&'){ 293f3f0262cSandi global $conf; 2946de3759aSAndreas Gohr if(is_array($more)){ 295b174aeaeSchris $more = buildURLparams($more,$sep); 2966de3759aSAndreas Gohr }else{ 297b174aeaeSchris $more = str_replace(',',$sep,$more); 2986de3759aSAndreas Gohr } 299f3f0262cSandi 300f3f0262cSandi $id = idfilter($id); 301ed7b5f09Sandi if($abs){ 302ed7b5f09Sandi $xlink = DOKU_URL; 303ed7b5f09Sandi }else{ 304ed7b5f09Sandi $xlink = DOKU_BASE; 305ed7b5f09Sandi } 306f3f0262cSandi 3076c7843b5Sandi if($conf['userewrite'] == 2){ 3086c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 3096c7843b5Sandi if($more) $xlink .= '?'.$more; 3106c7843b5Sandi }elseif($conf['userewrite']){ 311f3f0262cSandi $xlink .= $id; 312f3f0262cSandi if($more) $xlink .= '?'.$more; 3136c7843b5Sandi }else{ 3146c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 315b174aeaeSchris if($more) $xlink .= $sep.$more; 316f3f0262cSandi } 317f3f0262cSandi 318f3f0262cSandi return $xlink; 319f3f0262cSandi} 320f3f0262cSandi 321f3f0262cSandi/** 322f5c2808fSBen Coburn * This builds a link to an alternate page format 323f5c2808fSBen Coburn * 324f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 325f5c2808fSBen Coburn * 326f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 327f5c2808fSBen Coburn */ 328f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&'){ 329f5c2808fSBen Coburn global $conf; 330f5c2808fSBen Coburn if(is_array($more)){ 331f5c2808fSBen Coburn $more = buildURLparams($more,$sep); 332f5c2808fSBen Coburn }else{ 333f5c2808fSBen Coburn $more = str_replace(',',$sep,$more); 334f5c2808fSBen Coburn } 335f5c2808fSBen Coburn 336f5c2808fSBen Coburn $format = rawurlencode($format); 337f5c2808fSBen Coburn $id = idfilter($id); 338f5c2808fSBen Coburn if($abs){ 339f5c2808fSBen Coburn $xlink = DOKU_URL; 340f5c2808fSBen Coburn }else{ 341f5c2808fSBen Coburn $xlink = DOKU_BASE; 342f5c2808fSBen Coburn } 343f5c2808fSBen Coburn 344f5c2808fSBen Coburn if($conf['userewrite'] == 2){ 345f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 346f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 347f5c2808fSBen Coburn }elseif($conf['userewrite'] == 1){ 348f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 349f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 350f5c2808fSBen Coburn }else{ 351f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 352f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 353f5c2808fSBen Coburn } 354f5c2808fSBen Coburn 355f5c2808fSBen Coburn return $xlink; 356f5c2808fSBen Coburn} 357f5c2808fSBen Coburn 358f5c2808fSBen Coburn/** 3596de3759aSAndreas Gohr * Build a link to a media file 3606de3759aSAndreas Gohr * 3616de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 3626de3759aSAndreas Gohr */ 363b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&'){ 3646de3759aSAndreas Gohr global $conf; 3656de3759aSAndreas Gohr if(is_array($more)){ 366b174aeaeSchris $more = buildURLparams($more,$sep); 3676de3759aSAndreas Gohr }else{ 368b174aeaeSchris $more = str_replace(',',$sep,$more); 3696de3759aSAndreas Gohr } 3706de3759aSAndreas Gohr 3716de3759aSAndreas Gohr $xlink = DOKU_BASE; 3726de3759aSAndreas Gohr 3736de3759aSAndreas Gohr // external URLs are always direct without rewriting 3746de3759aSAndreas Gohr if(preg_match('#^(https?|ftp)://#i',$id)){ 3756de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 3766de3759aSAndreas Gohr if($more){ 3776de3759aSAndreas Gohr $xlink .= '?'.$more; 378b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 3796de3759aSAndreas Gohr }else{ 380b6c6979fSAndreas Gohr $xlink .= '?media='.rawurlencode($id); 3816de3759aSAndreas Gohr } 3826de3759aSAndreas Gohr return $xlink; 3836de3759aSAndreas Gohr } 3846de3759aSAndreas Gohr 3856de3759aSAndreas Gohr $id = idfilter($id); 3866de3759aSAndreas Gohr 3876de3759aSAndreas Gohr // decide on scriptname 3886de3759aSAndreas Gohr if($direct){ 3896de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 3906de3759aSAndreas Gohr $script = '_media'; 3916de3759aSAndreas Gohr }else{ 3926de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 3936de3759aSAndreas Gohr } 3946de3759aSAndreas Gohr }else{ 3956de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 3966de3759aSAndreas Gohr $script = '_detail'; 3976de3759aSAndreas Gohr }else{ 3986de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 3996de3759aSAndreas Gohr } 4006de3759aSAndreas Gohr } 4016de3759aSAndreas Gohr 4026de3759aSAndreas Gohr // build URL based on rewrite mode 4036de3759aSAndreas Gohr if($conf['userewrite']){ 4046de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 4056de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 4066de3759aSAndreas Gohr }else{ 4076de3759aSAndreas Gohr if($more){ 408a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 409b174aeaeSchris $xlink .= $sep.'media='.$id; 4106de3759aSAndreas Gohr }else{ 411a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 4126de3759aSAndreas Gohr } 4136de3759aSAndreas Gohr } 4146de3759aSAndreas Gohr 4156de3759aSAndreas Gohr return $xlink; 4166de3759aSAndreas Gohr} 4176de3759aSAndreas Gohr 4186de3759aSAndreas Gohr 4196de3759aSAndreas Gohr 4206de3759aSAndreas Gohr/** 421f3f0262cSandi * Just builds a link to a script 42215fae107Sandi * 423ed7b5f09Sandi * @todo maybe obsolete 42415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 425f3f0262cSandi */ 426f3f0262cSandifunction script($script='doku.php'){ 427ed7b5f09Sandi# $link = getBaseURL(); 428ed7b5f09Sandi# $link .= $script; 429ed7b5f09Sandi# return $link; 430ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 431f3f0262cSandi} 432f3f0262cSandi 433f3f0262cSandi/** 43415fae107Sandi * Spamcheck against wordlist 43515fae107Sandi * 436f3f0262cSandi * Checks the wikitext against a list of blocked expressions 437f3f0262cSandi * returns true if the text contains any bad words 43815fae107Sandi * 43915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 440f3f0262cSandi */ 441f3f0262cSandifunction checkwordblock(){ 442f3f0262cSandi global $TEXT; 443f3f0262cSandi global $conf; 444f3f0262cSandi 445f3f0262cSandi if(!$conf['usewordblock']) return false; 446f3f0262cSandi 447b9ac8716Schris $wordblocks = getWordblocks(); 4483e2965d7Sandi //how many lines to read at once (to work around some PCRE limits) 4493e2965d7Sandi if(version_compare(phpversion(),'4.3.0','<')){ 4503e2965d7Sandi //old versions of PCRE define a maximum of parenthesises even if no 4513e2965d7Sandi //backreferences are used - the maximum is 99 4523e2965d7Sandi //this is very bad performancewise and may even be too high still 4533e2965d7Sandi $chunksize = 40; 4543e2965d7Sandi }else{ 455703f6fdeSandi //read file in chunks of 600 - this should work around the 4563e2965d7Sandi //MAX_PATTERN_SIZE in modern PCRE 457444b87a5SAndreas Gohr $chunksize = 400; 4583e2965d7Sandi } 459b9ac8716Schris while($blocks = array_splice($wordblocks,0,$chunksize)){ 460f3f0262cSandi $re = array(); 461f3f0262cSandi #build regexp from blocks 462f3f0262cSandi foreach($blocks as $block){ 463f3f0262cSandi $block = preg_replace('/#.*$/','',$block); 464f3f0262cSandi $block = trim($block); 465f3f0262cSandi if(empty($block)) continue; 466f3f0262cSandi $re[] = $block; 467f3f0262cSandi } 468b9ac8716Schris if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) { 469b9ac8716Schris return true; 470b9ac8716Schris } 471703f6fdeSandi } 472f3f0262cSandi return false; 473f3f0262cSandi} 474f3f0262cSandi 475f3f0262cSandi/** 47615fae107Sandi * Return the IP of the client 47715fae107Sandi * 4786d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 47915fae107Sandi * 4806d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 4816d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 4826d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 4836d8affe6SAndreas Gohr * headers 4846d8affe6SAndreas Gohr * 4856d8affe6SAndreas Gohr * @param boolean $single If set only a single IP is returned 48615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 487f3f0262cSandi */ 4886d8affe6SAndreas Gohrfunction clientIP($single=false){ 4896d8affe6SAndreas Gohr $ip = array(); 4906d8affe6SAndreas Gohr $ip[] = $_SERVER['REMOTE_ADDR']; 4916d8affe6SAndreas Gohr if($_SERVER['HTTP_X_FORWARDED_FOR']) 4926d8affe6SAndreas Gohr $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])); 4936d8affe6SAndreas Gohr if($_SERVER['HTTP_X_REAL_IP']) 4946d8affe6SAndreas Gohr $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP'])); 4956d8affe6SAndreas Gohr 4966d8affe6SAndreas Gohr // remove any non-IP stuff 4976d8affe6SAndreas Gohr $cnt = count($ip); 4984ff28443Schris $match = array(); 4996d8affe6SAndreas Gohr for($i=0; $i<$cnt; $i++){ 5004ff28443Schris if(preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i],$match)) { 5014ff28443Schris $ip[$i] = $match[0]; 5024ff28443Schris } else { 5034ff28443Schris $ip[$i] = ''; 5044ff28443Schris } 5056d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 506f3f0262cSandi } 5076d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 5086d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 5096d8affe6SAndreas Gohr 5106d8affe6SAndreas Gohr if(!$single) return join(',',$ip); 5116d8affe6SAndreas Gohr 5126d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 5136d8affe6SAndreas Gohr $ip = array_reverse($ip); 5146d8affe6SAndreas Gohr foreach($ip as $i){ 5156d8affe6SAndreas Gohr if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){ 5166d8affe6SAndreas Gohr continue; 5176d8affe6SAndreas Gohr }else{ 5186d8affe6SAndreas Gohr return $i; 5196d8affe6SAndreas Gohr } 5206d8affe6SAndreas Gohr } 5216d8affe6SAndreas Gohr // still here? just use the first (last) address 5226d8affe6SAndreas Gohr return $ip[0]; 523f3f0262cSandi} 524f3f0262cSandi 525f3f0262cSandi/** 52615fae107Sandi * Checks if a given page is currently locked. 52715fae107Sandi * 528f3f0262cSandi * removes stale lockfiles 52915fae107Sandi * 53015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 531f3f0262cSandi */ 532f3f0262cSandifunction checklock($id){ 533f3f0262cSandi global $conf; 534c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 535f3f0262cSandi 536f3f0262cSandi //no lockfile 537f3f0262cSandi if(!@file_exists($lock)) return false; 538f3f0262cSandi 539f3f0262cSandi //lockfile expired 540f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']){ 541d8186216SBen Coburn @unlink($lock); 542f3f0262cSandi return false; 543f3f0262cSandi } 544f3f0262cSandi 545f3f0262cSandi //my own lock 546f3f0262cSandi $ip = io_readFile($lock); 547f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 548f3f0262cSandi return false; 549f3f0262cSandi } 550f3f0262cSandi 551f3f0262cSandi return $ip; 552f3f0262cSandi} 553f3f0262cSandi 554f3f0262cSandi/** 55515fae107Sandi * Lock a page for editing 55615fae107Sandi * 55715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 558f3f0262cSandi */ 559f3f0262cSandifunction lock($id){ 560c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 561f3f0262cSandi if($_SERVER['REMOTE_USER']){ 562f3f0262cSandi io_saveFile($lock,$_SERVER['REMOTE_USER']); 563f3f0262cSandi }else{ 564f3f0262cSandi io_saveFile($lock,clientIP()); 565f3f0262cSandi } 566f3f0262cSandi} 567f3f0262cSandi 568f3f0262cSandi/** 56915fae107Sandi * Unlock a page if it was locked by the user 570f3f0262cSandi * 57115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 57215fae107Sandi * @return bool true if a lock was removed 573f3f0262cSandi */ 574f3f0262cSandifunction unlock($id){ 575c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 576f3f0262cSandi if(@file_exists($lock)){ 577f3f0262cSandi $ip = io_readFile($lock); 578f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 579f3f0262cSandi @unlink($lock); 580f3f0262cSandi return true; 581f3f0262cSandi } 582f3f0262cSandi } 583f3f0262cSandi return false; 584f3f0262cSandi} 585f3f0262cSandi 586f3f0262cSandi/** 587f3f0262cSandi * convert line ending to unix format 588f3f0262cSandi * 58915fae107Sandi * @see formText() for 2crlf conversion 59015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 591f3f0262cSandi */ 592f3f0262cSandifunction cleanText($text){ 593f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 594f3f0262cSandi return $text; 595f3f0262cSandi} 596f3f0262cSandi 597f3f0262cSandi/** 598f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 599f3f0262cSandi * It also converts line endings to Windows format which is 600f3f0262cSandi * pseudo standard for webforms. 601f3f0262cSandi * 60215fae107Sandi * @see cleanText() for 2unix conversion 60315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 604f3f0262cSandi */ 605f3f0262cSandifunction formText($text){ 606f3f0262cSandi $text = preg_replace("/\012/","\015\012",$text); 607f3f0262cSandi return htmlspecialchars($text); 608f3f0262cSandi} 609f3f0262cSandi 610f3f0262cSandi/** 61115fae107Sandi * Returns the specified local text in raw format 61215fae107Sandi * 61315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 614f3f0262cSandi */ 615f3f0262cSandifunction rawLocale($id){ 616f3f0262cSandi return io_readFile(localeFN($id)); 617f3f0262cSandi} 618f3f0262cSandi 619f3f0262cSandi/** 620f3f0262cSandi * Returns the raw WikiText 62115fae107Sandi * 62215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 623f3f0262cSandi */ 624f3f0262cSandifunction rawWiki($id,$rev=''){ 625cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 626f3f0262cSandi} 627f3f0262cSandi 628f3f0262cSandi/** 6297146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 6307146cee2SAndreas Gohr * 6317146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6327146cee2SAndreas Gohr */ 6337146cee2SAndreas Gohrfunction pageTemplate($id){ 634a15ce62dSEsther Brunner global $conf; 635a15ce62dSEsther Brunner global $INFO; 636a15ce62dSEsther Brunner $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt'); 637a15ce62dSEsther Brunner $tpl = str_replace('@ID@',$id,$tpl); 638a15ce62dSEsther Brunner $tpl = str_replace('@NS@',getNS($id),$tpl); 639a15ce62dSEsther Brunner $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl); 640a15ce62dSEsther Brunner $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl); 641a15ce62dSEsther Brunner $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl); 642a15ce62dSEsther Brunner $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl); 643a15ce62dSEsther Brunner $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl); 644a15ce62dSEsther Brunner return $tpl; 6457146cee2SAndreas Gohr} 6467146cee2SAndreas Gohr 6477146cee2SAndreas Gohr 6487146cee2SAndreas Gohr/** 64915fae107Sandi * Returns the raw Wiki Text in three slices. 65015fae107Sandi * 65115fae107Sandi * The range parameter needs to have the form "from-to" 65215cfe303Sandi * and gives the range of the section in bytes - no 65315cfe303Sandi * UTF-8 awareness is needed. 654f3f0262cSandi * The returned order is prefix, section and suffix. 65515fae107Sandi * 65615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 657f3f0262cSandi */ 658f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){ 659f3f0262cSandi list($from,$to) = split('-',$range,2); 660cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 661f3f0262cSandi if(!$from) $from = 0; 662c3d8e19bSandi if(!$to) $to = strlen($text)+1; 663f3f0262cSandi 66415cfe303Sandi $slices[0] = substr($text,0,$from-1); 66515cfe303Sandi $slices[1] = substr($text,$from-1,$to-$from); 66615cfe303Sandi $slices[2] = substr($text,$to); 667f3f0262cSandi 668f3f0262cSandi return $slices; 669f3f0262cSandi} 670f3f0262cSandi 671f3f0262cSandi/** 67215fae107Sandi * Joins wiki text slices 67315fae107Sandi * 674f3f0262cSandi * function to join the text slices with correct lineendings again. 675f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 676f3f0262cSandi * lines between sections if needed (used on saving). 67715fae107Sandi * 67815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 679f3f0262cSandi */ 680f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){ 681f3f0262cSandi 682f3f0262cSandi if($pretty){ 683f3f0262cSandi if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 684f3f0262cSandi if($suf && substr($text,-1) != "\n") $text .= "\n"; 685f3f0262cSandi } 686f3f0262cSandi 687f3f0262cSandi if($pre) $pre .= "\n"; 688f3f0262cSandi if($suf) $text .= "\n"; 689f3f0262cSandi return $pre.$text.$suf; 690f3f0262cSandi} 691f3f0262cSandi 692f3f0262cSandi/** 69315fae107Sandi * print debug messages 69415fae107Sandi * 695f3f0262cSandi * little function to print the content of a var 69615fae107Sandi * 69715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 698f3f0262cSandi */ 699f3f0262cSandifunction dbg($msg,$hidden=false){ 700f3f0262cSandi (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 701f3f0262cSandi print_r($msg); 702f3f0262cSandi (!$hidden) ? print '</pre>' : print "\n-->"; 703f3f0262cSandi} 704f3f0262cSandi 705f3f0262cSandi/** 70663cb5853SAndreas Gohr * Print info to a log file 70763cb5853SAndreas Gohr * 70863cb5853SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 70963cb5853SAndreas Gohr */ 71063cb5853SAndreas Gohrfunction dbglog($msg){ 71163cb5853SAndreas Gohr global $conf; 71263cb5853SAndreas Gohr $file = $conf['cachedir'].'/debug.log'; 71363cb5853SAndreas Gohr $fh = fopen($file,'a'); 71463cb5853SAndreas Gohr if($fh){ 71563cb5853SAndreas Gohr fwrite($fh,date('H:i:s ').$_SERVER['REMOTE_ADDR'].': '.$msg."\n"); 71663cb5853SAndreas Gohr fclose($fh); 71763cb5853SAndreas Gohr } 71863cb5853SAndreas Gohr} 71963cb5853SAndreas Gohr 72063cb5853SAndreas Gohr/** 72171726d78SBen Coburn * Add's an entry to the changelog and saves the metadata for the page 72215fae107Sandi * 72315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 72471726d78SBen Coburn * @author Esther Brunner <wikidesign@gmail.com> 72571726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 726f3f0262cSandi */ 72771726d78SBen Coburnfunction addLogEntry($date, $id, $type='E', $summary='', $extra=''){ 72871726d78SBen Coburn global $conf, $INFO; 729c1049928Sandi 73071726d78SBen Coburn $id = cleanid($id); 73171726d78SBen Coburn $file = wikiFN($id); 73271726d78SBen Coburn $created = @filectime($file); 73371726d78SBen Coburn $minor = ($type==='e'); 73471726d78SBen Coburn $wasRemoved = ($type==='D'); 735c1049928Sandi 736652610a2Sandi if(!$date) $date = time(); //use current time if none supplied 737f3f0262cSandi $remote = $_SERVER['REMOTE_ADDR']; 738f3f0262cSandi $user = $_SERVER['REMOTE_USER']; 739f3f0262cSandi 740*e45b34cdSBen Coburn $strip = array("\t", "\n"); 74171726d78SBen Coburn $logline = array( 74271726d78SBen Coburn 'date' => $date, 74371726d78SBen Coburn 'ip' => $remote, 744*e45b34cdSBen Coburn 'type' => str_replace($strip, '', $type), 74571726d78SBen Coburn 'id' => $id, 74671726d78SBen Coburn 'user' => $user, 747*e45b34cdSBen Coburn 'sum' => str_replace($strip, '', $summary), 748*e45b34cdSBen Coburn 'extra' => str_replace($strip, '', $extra) 74971726d78SBen Coburn ); 75071726d78SBen Coburn 75171726d78SBen Coburn // update metadata 75271726d78SBen Coburn if (!$wasRemoved) { 75371726d78SBen Coburn $meta = array(); 75471726d78SBen Coburn if (!$INFO['exists']){ // newly created 75571726d78SBen Coburn $meta['date']['created'] = $created; 75671726d78SBen Coburn if ($user) $meta['creator'] = $INFO['userinfo']['name']; 75771726d78SBen Coburn } elseif (!$minor) { // non-minor modification 75871726d78SBen Coburn $meta['date']['modified'] = $date; 75971726d78SBen Coburn if ($user) $meta['contributor'][$user] = $INFO['userinfo']['name']; 76071726d78SBen Coburn } 76171726d78SBen Coburn $meta['last_change'] = $logline; 76271726d78SBen Coburn p_set_metadata($id, $meta, true); 763b6912aeaSAndreas Gohr } 764b6912aeaSAndreas Gohr 76571726d78SBen Coburn // add changelog lines 76671726d78SBen Coburn $logline = implode("\t", $logline)."\n"; 76771726d78SBen Coburn io_saveFile(metaFN($id,'.changes'),$logline,true); //page changelog 76871726d78SBen Coburn io_saveFile($conf['changelog'],$logline,true); //global changelog cache 769b6912aeaSAndreas Gohr} 770b6912aeaSAndreas Gohr 771b6912aeaSAndreas Gohr/** 772d437bcc4SAndreas Gohr * Internal function used by getRecents 773d437bcc4SAndreas Gohr * 774d437bcc4SAndreas Gohr * don't call directly 775d437bcc4SAndreas Gohr * 776d437bcc4SAndreas Gohr * @see getRecents() 777d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 77871726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 779d437bcc4SAndreas Gohr */ 780b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){ 781d437bcc4SAndreas Gohr static $seen = array(); //caches seen pages and skip them 782d437bcc4SAndreas Gohr if(empty($line)) return false; //skip empty lines 783d437bcc4SAndreas Gohr 784d437bcc4SAndreas Gohr // split the line into parts 78571726d78SBen Coburn $recent = parseChangelogLine($line); 78671726d78SBen Coburn if ($recent===false) { return false; } 787d437bcc4SAndreas Gohr 788d437bcc4SAndreas Gohr // skip seen ones 78971726d78SBen Coburn if(isset($seen[$recent['id']])) return false; 790b6912aeaSAndreas Gohr 791b6912aeaSAndreas Gohr // skip minors 79271726d78SBen Coburn if($recent['type']==='e' && ($flags & RECENTS_SKIP_MINORS)) return false; 793d437bcc4SAndreas Gohr 794d437bcc4SAndreas Gohr // remember in seen to skip additional sights 79571726d78SBen Coburn $seen[$recent['id']] = 1; 796d437bcc4SAndreas Gohr 7970dc92c6fSAndreas Gohr // check if it's a hidden page 79871726d78SBen Coburn if(isHiddenPage($recent['id'])) return false; 7990dc92c6fSAndreas Gohr 800d437bcc4SAndreas Gohr // filter namespace 80171726d78SBen Coburn if (($ns) && (strpos($recent['id'],$ns.':') !== 0)) return false; 802d437bcc4SAndreas Gohr 803d437bcc4SAndreas Gohr // exclude subnamespaces 80471726d78SBen Coburn if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($recent['id']) != $ns)) return false; 805d437bcc4SAndreas Gohr 806ae56bfb6SAndreas Gohr // check ACL 80771726d78SBen Coburn if (auth_quickaclcheck($recent['id']) < AUTH_READ) return false; 808ae56bfb6SAndreas Gohr 809d437bcc4SAndreas Gohr // check existance 81071726d78SBen Coburn if((!@file_exists(wikiFN($recent['id']))) && ($flags & RECENTS_SKIP_DELETED)) return false; 811d437bcc4SAndreas Gohr 812d437bcc4SAndreas Gohr return $recent; 813d437bcc4SAndreas Gohr} 814d437bcc4SAndreas Gohr 815b6912aeaSAndreas Gohr 816d437bcc4SAndreas Gohr/** 817f3f0262cSandi * returns an array of recently changed files using the 818f3f0262cSandi * changelog 819d437bcc4SAndreas Gohr * 820b6912aeaSAndreas Gohr * The following constants can be used to control which changes are 821b6912aeaSAndreas Gohr * included. Add them together as needed. 822b6912aeaSAndreas Gohr * 823b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED - don't include deleted pages 824b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS - don't include minor changes 825b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces 826b6912aeaSAndreas Gohr * 827d437bcc4SAndreas Gohr * @param int $first number of first entry returned (for paginating 828d437bcc4SAndreas Gohr * @param int $num return $num entries 829d437bcc4SAndreas Gohr * @param string $ns restrict to given namespace 830b6912aeaSAndreas Gohr * @param bool $flags see above 83115fae107Sandi * 83271726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 833f3f0262cSandi */ 834b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){ 835f3f0262cSandi global $conf; 836f3f0262cSandi $recent = array(); 837d437bcc4SAndreas Gohr $count = 0; 8385749f1ceSmatthiasgrimm 8395749f1ceSmatthiasgrimm if(!$num) 8405749f1ceSmatthiasgrimm return $recent; 841f3f0262cSandi 84271726d78SBen Coburn // read all recent changes. (kept short) 84371726d78SBen Coburn $lines = file($conf['changelog']); 844d437bcc4SAndreas Gohr 845d437bcc4SAndreas Gohr // handle lines 84671726d78SBen Coburn for($i = count($lines)-1; $i >= 0; $i--){ 847b6912aeaSAndreas Gohr $rec = _handleRecent($lines[$i], $ns, $flags); 848d437bcc4SAndreas Gohr if($rec !== false) { 849d437bcc4SAndreas Gohr if(--$first >= 0) continue; // skip first entries 850d437bcc4SAndreas Gohr $recent[] = $rec; 851d437bcc4SAndreas Gohr $count++; 85271726d78SBen Coburn // break when we have enough entries 85371726d78SBen Coburn if($count >= $num){ break; } 85471726d78SBen Coburn } 85571726d78SBen Coburn } 856d437bcc4SAndreas Gohr 857f3f0262cSandi return $recent; 858f3f0262cSandi} 859f3f0262cSandi 860f3f0262cSandi/** 86171726d78SBen Coburn * parses a changelog line into it's components 86251815db0SYann * 863fb53bfe2SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 864652610a2Sandi */ 86571726d78SBen Coburnfunction parseChangelogLine($line) { 86671726d78SBen Coburn $tmp = explode("\t", $line); 86771726d78SBen Coburn if ($tmp!==false && count($tmp)>1) { 86871726d78SBen Coburn $info = array(); 86971726d78SBen Coburn $info['date'] = $tmp[0]; // unix timestamp 87071726d78SBen Coburn $info['ip'] = $tmp[1]; // IPv4 address (127.0.0.1) 87171726d78SBen Coburn $info['type'] = $tmp[2]; // log line type 87271726d78SBen Coburn $info['id'] = $tmp[3]; // page id 87371726d78SBen Coburn $info['user'] = $tmp[4]; // user name 87471726d78SBen Coburn $info['sum'] = $tmp[5]; // edit summary (or action reason) 87571726d78SBen Coburn $info['extra'] = rtrim($tmp[6], "\n"); // extra data (varies by line type) 87671726d78SBen Coburn return $info; 87771726d78SBen Coburn } else { return false; } 87871726d78SBen Coburn} 87971726d78SBen Coburn 88071726d78SBen Coburn/** 88171726d78SBen Coburn * Get the changelog information for a specific page id 88271726d78SBen Coburn * and revision (timestamp). Adjacent changelog lines 88371726d78SBen Coburn * are optimistically parsed and cached to speed up 88471726d78SBen Coburn * consecutive calls to getRevisionInfo. For large 88571726d78SBen Coburn * changelog files, only the chunk containing the 88671726d78SBen Coburn * requested changelog line is read. 88771726d78SBen Coburn * 88871726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 88971726d78SBen Coburn */ 89071726d78SBen Coburnfunction getRevisionInfo($id, $rev, $chunk_size=8192) { 89171726d78SBen Coburn global $cache_revinfo; 89271726d78SBen Coburn $cache =& $cache_revinfo; 89371726d78SBen Coburn if (!isset($cache[$id])) { $cache[$id] = array(); } 89471726d78SBen Coburn $rev = max($rev, 0); 895258641c6Sandi 896fb53bfe2SBen Coburn // check if it's already in the memory cache 89771726d78SBen Coburn if (isset($cache[$id]) && isset($cache[$id][$rev])) { 898fb53bfe2SBen Coburn return $cache[$id][$rev]; 899fb53bfe2SBen Coburn } 900fb53bfe2SBen Coburn 90171726d78SBen Coburn $file = metaFN($id, '.changes'); 902d8186216SBen Coburn if (!@file_exists($file)) { return false; } 90371726d78SBen Coburn if (filesize($file)<$chunk_size || $chunk_size==0) { 90471726d78SBen Coburn // read whole file 90571726d78SBen Coburn $lines = file($file); 90671726d78SBen Coburn if ($lines===false) { return false; } 907fb53bfe2SBen Coburn } else { 90871726d78SBen Coburn // read by chunk 90971726d78SBen Coburn $fp = fopen($file, 'rb'); // "file pointer" 91071726d78SBen Coburn if ($fp===false) { return false; } 91171726d78SBen Coburn $head = 0; 91271726d78SBen Coburn fseek($fp, 0, SEEK_END); 91371726d78SBen Coburn $tail = ftell($fp); 91471726d78SBen Coburn $finger = 0; 91571726d78SBen Coburn $finger_rev = 0; 91671726d78SBen Coburn 91771726d78SBen Coburn // find chunk 91871726d78SBen Coburn while ($tail-$head>$chunk_size) { 91971726d78SBen Coburn $finger = $head+floor(($tail-$head)/2.0); 92071726d78SBen Coburn fseek($fp, $finger); 92171726d78SBen Coburn fgets($fp); // slip the finger forward to a new line 92271726d78SBen Coburn $finger = ftell($fp); 92371726d78SBen Coburn $tmp = fgets($fp); // then read at that location 92471726d78SBen Coburn $tmp = parseChangelogLine($tmp); 92571726d78SBen Coburn $finger_rev = $tmp['date']; 92671726d78SBen Coburn if ($finger==$head || $finger==$tail) { break; } 92771726d78SBen Coburn if ($finger_rev>$rev) { 92871726d78SBen Coburn $tail = $finger; 92971726d78SBen Coburn } else { 93071726d78SBen Coburn $head = $finger; 93171726d78SBen Coburn } 93271726d78SBen Coburn } 93371726d78SBen Coburn 93471726d78SBen Coburn if ($tail-$head<1) { 93571726d78SBen Coburn // cound not find chunk, assume requested rev is missing 93671726d78SBen Coburn fclose($fp); 93771726d78SBen Coburn return false; 93871726d78SBen Coburn } 93971726d78SBen Coburn 94071726d78SBen Coburn // read chunk 94171726d78SBen Coburn $chunk = ''; 94271726d78SBen Coburn $chunk_size = max($tail-$head, 0); // found chunk size 94371726d78SBen Coburn $got = 0; 94471726d78SBen Coburn fseek($fp, $head); 94571726d78SBen Coburn while ($got<$chunk_size && !feof($fp)) { 94671726d78SBen Coburn $tmp = fread($fp, max($chunk_size-$got, 0)); 94771726d78SBen Coburn if ($tmp===false) { break; } //error state 94871726d78SBen Coburn $got += strlen($tmp); 94971726d78SBen Coburn $chunk .= $tmp; 95071726d78SBen Coburn } 95171726d78SBen Coburn $lines = explode("\n", $chunk); 95271726d78SBen Coburn array_pop($lines); // remove trailing newline 95371726d78SBen Coburn fclose($fp); 95471726d78SBen Coburn } 95571726d78SBen Coburn 95671726d78SBen Coburn // parse and cache changelog lines 95771726d78SBen Coburn foreach ($lines as $value) { 95871726d78SBen Coburn $tmp = parseChangelogLine($value); 95971726d78SBen Coburn if ($tmp!==false) { 96071726d78SBen Coburn $cache[$id][$tmp['date']] = $tmp; 96171726d78SBen Coburn } 96271726d78SBen Coburn } 96371726d78SBen Coburn if (!isset($cache[$id][$rev])) { return false; } 96471726d78SBen Coburn return $cache[$id][$rev]; 96571726d78SBen Coburn} 96671726d78SBen Coburn 96771726d78SBen Coburn/** 96871726d78SBen Coburn * Return a list of page revisions numbers 96971726d78SBen Coburn * Does not guarantee that the revision exists in the attic, 97071726d78SBen Coburn * only that a line with the date exists in the changelog. 97171726d78SBen Coburn * By default the current revision is skipped. 97271726d78SBen Coburn * 97371726d78SBen Coburn * id: the page of interest 97471726d78SBen Coburn * first: skip the first n changelog lines 97571726d78SBen Coburn * num: number of revisions to return 97671726d78SBen Coburn * 97771726d78SBen Coburn * The current revision is automatically skipped when the page exists. 97871726d78SBen Coburn * See $INFO['meta']['last_change'] for the current revision. 97971726d78SBen Coburn * 98071726d78SBen Coburn * For efficiency, the log lines are parsed and cached for later 98171726d78SBen Coburn * calls to getRevisionInfo. Large changelog files are read 98271726d78SBen Coburn * backwards in chunks untill the requested number of changelog 98371726d78SBen Coburn * lines are recieved. 98471726d78SBen Coburn * 98571726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 98671726d78SBen Coburn */ 98771726d78SBen Coburnfunction getRevisions($id, $first, $num, $chunk_size=8192) { 98871726d78SBen Coburn global $cache_revinfo; 98971726d78SBen Coburn $cache =& $cache_revinfo; 990fb53bfe2SBen Coburn if (!isset($cache[$id])) { $cache[$id] = array(); } 99171726d78SBen Coburn 99271726d78SBen Coburn $revs = array(); 99371726d78SBen Coburn $lines = array(); 99471726d78SBen Coburn $count = 0; 99571726d78SBen Coburn $file = metaFN($id, '.changes'); 99671726d78SBen Coburn $num = max($num, 0); 99771726d78SBen Coburn $chunk_size = max($chunk_size, 0); 99871726d78SBen Coburn if ($first<0) { $first = 0; } 999d8186216SBen Coburn else if (@file_exists(wikiFN($id))) { 100071726d78SBen Coburn // skip current revision if the page exists 100171726d78SBen Coburn $first = max($first+1, 0); 1002fb53bfe2SBen Coburn } 1003fb53bfe2SBen Coburn 1004d8186216SBen Coburn if (!@file_exists($file)) { return $revs; } 100571726d78SBen Coburn if (filesize($file)<$chunk_size || $chunk_size==0) { 100671726d78SBen Coburn // read whole file 100771726d78SBen Coburn $lines = file($file); 100871726d78SBen Coburn if ($lines===false) { return $revs; } 100971726d78SBen Coburn } else { 101071726d78SBen Coburn // read chunks backwards 101171726d78SBen Coburn $fp = fopen($file, 'rb'); // "file pointer" 101271726d78SBen Coburn if ($fp===false) { return $revs; } 101371726d78SBen Coburn fseek($fp, 0, SEEK_END); 101471726d78SBen Coburn $tail = ftell($fp); 101571726d78SBen Coburn 101671726d78SBen Coburn // chunk backwards 101771726d78SBen Coburn $finger = max($tail-$chunk_size, 0); 101871726d78SBen Coburn while ($count<$num+$first) { 101971726d78SBen Coburn fseek($fp, $finger); 102071726d78SBen Coburn if ($finger>0) { 102171726d78SBen Coburn fgets($fp); // slip the finger forward to a new line 102271726d78SBen Coburn $finger = ftell($fp); 1023652610a2Sandi } 1024652610a2Sandi 102571726d78SBen Coburn // read chunk 102671726d78SBen Coburn if ($tail<=$finger) { break; } 102771726d78SBen Coburn $chunk = ''; 102871726d78SBen Coburn $read_size = max($tail-$finger, 0); // found chunk size 102971726d78SBen Coburn $got = 0; 103071726d78SBen Coburn while ($got<$read_size && !feof($fp)) { 103171726d78SBen Coburn $tmp = fread($fp, max($read_size-$got, 0)); 103271726d78SBen Coburn if ($tmp===false) { break; } //error state 103371726d78SBen Coburn $got += strlen($tmp); 103471726d78SBen Coburn $chunk .= $tmp; 103571726d78SBen Coburn } 103671726d78SBen Coburn $tmp = explode("\n", $chunk); 103771726d78SBen Coburn array_pop($tmp); // remove trailing newline 103871726d78SBen Coburn 103971726d78SBen Coburn // combine with previous chunk 104071726d78SBen Coburn $count += count($tmp); 104171726d78SBen Coburn $lines = array_merge($tmp, $lines); 104271726d78SBen Coburn 104371726d78SBen Coburn // next chunk 104471726d78SBen Coburn if ($finger==0) { break; } // already read all the lines 104571726d78SBen Coburn else { 104671726d78SBen Coburn $tail = $finger; 104771726d78SBen Coburn $finger = max($tail-$chunk_size, 0); 104871726d78SBen Coburn } 104971726d78SBen Coburn } 105071726d78SBen Coburn fclose($fp); 105171726d78SBen Coburn } 105271726d78SBen Coburn 105371726d78SBen Coburn // skip parsing extra lines 105471726d78SBen Coburn $num = max(min(count($lines)-$first, $num), 0); 105571726d78SBen Coburn if ($first>0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$first-$num, 0), $num); } 105671726d78SBen Coburn else if ($first>0 && $num==0) { $lines = array_slice($lines, 0, max(count($lines)-$first, 0)); } 105771726d78SBen Coburn else if ($first==0 && $num>0) { $lines = array_slice($lines, max(count($lines)-$num, 0)); } 105871726d78SBen Coburn 105971726d78SBen Coburn // handle lines in reverse order 106071726d78SBen Coburn for ($i = count($lines)-1; $i >= 0; $i--) { 106171726d78SBen Coburn $tmp = parseChangelogLine($lines[$i]); 106271726d78SBen Coburn if ($tmp!==false) { 106371726d78SBen Coburn $cache[$id][$tmp['date']] = $tmp; 106471726d78SBen Coburn $revs[] = $tmp['date']; 106571726d78SBen Coburn } 106671726d78SBen Coburn } 106771726d78SBen Coburn 106871726d78SBen Coburn return $revs; 106971726d78SBen Coburn} 107051815db0SYann 1071652610a2Sandi/** 1072cc7d0c94SBen Coburn * Saves a wikitext by calling io_writeWikiPage 107315fae107Sandi * 107415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 107571726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1076f3f0262cSandi */ 1077b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){ 1078f3f0262cSandi global $conf; 1079f3f0262cSandi global $lang; 108071726d78SBen Coburn global $REV; 1081f3f0262cSandi // ignore if no changes were made 1082f3f0262cSandi if($text == rawWiki($id,'')){ 1083f3f0262cSandi return; 1084f3f0262cSandi } 1085f3f0262cSandi 1086f3f0262cSandi $file = wikiFN($id); 1087f3f0262cSandi $old = saveOldRevision($id); 108871726d78SBen Coburn $wasRemoved = empty($text); 1089d8186216SBen Coburn $wasCreated = !@file_exists($file); 109071726d78SBen Coburn $wasReverted = ($REV==true); 1091*e45b34cdSBen Coburn $newRev = false; 1092f3f0262cSandi 109371726d78SBen Coburn if ($wasRemoved){ 1094*e45b34cdSBen Coburn // pre-save deleted revision 1095*e45b34cdSBen Coburn @touch($file); 1096*e45b34cdSBen Coburn $newRev = saveOldRevision($id); 1097e1f3d9e1SEsther Brunner // remove empty file 1098f3f0262cSandi @unlink($file); 109971726d78SBen Coburn // remove old meta info... 1100e1f3d9e1SEsther Brunner $mfiles = metaFiles($id); 110171726d78SBen Coburn $changelog = metaFN($id, '.changes'); 1102e1f3d9e1SEsther Brunner foreach ($mfiles as $mfile) { 110371726d78SBen Coburn // but keep per-page changelog to preserve page history 1104d8186216SBen Coburn if (@file_exists($mfile) && $mfile!==$changelog) { @unlink($mfile); } 1105b158d625SSteven Danz } 1106f3f0262cSandi $del = true; 11073ce054b3Sandi // autoset summary on deletion 11083ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 110953d6ccfeSandi // remove empty namespaces 1110cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1111cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1112f3f0262cSandi }else{ 1113cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 1114cc7d0c94SBen Coburn io_writeWikiPage($file, $text, $id); 1115*e45b34cdSBen Coburn $newRev = @filemtime($file); 1116f3f0262cSandi $del = false; 1117f3f0262cSandi } 1118f3f0262cSandi 111971726d78SBen Coburn // select changelog line type 112071726d78SBen Coburn $extra = ''; 112171726d78SBen Coburn $type = 'E'; 112271726d78SBen Coburn if ($wasReverted) { 112371726d78SBen Coburn $type = 'R'; 112471726d78SBen Coburn $extra = $REV; 112571726d78SBen Coburn } 112671726d78SBen Coburn else if ($wasCreated) { $type = 'C'; } 112771726d78SBen Coburn else if ($wasRemoved) { $type = 'D'; } 112871726d78SBen Coburn else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = 'e'; } //minor edits only for logged in users 112971726d78SBen Coburn 1130*e45b34cdSBen Coburn addLogEntry($newRev, $id, $type, $summary, $extra); 113126a0801fSAndreas Gohr // send notify mails 113290033e9dSAndreas Gohr notify($id,'admin',$old,$summary,$minor); 113390033e9dSAndreas Gohr notify($id,'subscribers',$old,$summary,$minor); 1134f3f0262cSandi 1135f3f0262cSandi //purge cache on add by updating the purgefile 1136f3f0262cSandi if($conf['purgeonadd'] && (!$old || $del)){ 113798407a7aSandi io_saveFile($conf['cachedir'].'/purgefile',time()); 1138f3f0262cSandi } 1139f3f0262cSandi} 1140f3f0262cSandi 1141f3f0262cSandi/** 1142f3f0262cSandi * moves the current version to the attic and returns its 1143f3f0262cSandi * revision date 114415fae107Sandi * 114515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1146f3f0262cSandi */ 1147f3f0262cSandifunction saveOldRevision($id){ 1148f3f0262cSandi global $conf; 1149f3f0262cSandi $oldf = wikiFN($id); 1150f3f0262cSandi if(!@file_exists($oldf)) return ''; 1151f3f0262cSandi $date = filemtime($oldf); 1152f3f0262cSandi $newf = wikiFN($id,$date); 1153cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1154f3f0262cSandi return $date; 1155f3f0262cSandi} 1156f3f0262cSandi 1157f3f0262cSandi/** 115826a0801fSAndreas Gohr * Sends a notify mail on page change 115926a0801fSAndreas Gohr * 116026a0801fSAndreas Gohr * @param string $id The changed page 116126a0801fSAndreas Gohr * @param string $who Who to notify (admin|subscribers) 116226a0801fSAndreas Gohr * @param int $rev Old page revision 116326a0801fSAndreas Gohr * @param string $summary What changed 116490033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 116502a498e7Schris * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 116615fae107Sandi * 116715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1168f3f0262cSandi */ 116902a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ 1170f3f0262cSandi global $lang; 1171f3f0262cSandi global $conf; 1172b158d625SSteven Danz 117326a0801fSAndreas Gohr // decide if there is something to do 117426a0801fSAndreas Gohr if($who == 'admin'){ 117526a0801fSAndreas Gohr if(empty($conf['notify'])) return; //notify enabled? 1176f3f0262cSandi $text = rawLocale('mailtext'); 117726a0801fSAndreas Gohr $to = $conf['notify']; 117826a0801fSAndreas Gohr $bcc = ''; 117926a0801fSAndreas Gohr }elseif($who == 'subscribers'){ 118026a0801fSAndreas Gohr if(!$conf['subscribers']) return; //subscribers enabled? 118190033e9dSAndreas Gohr if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors 118226a0801fSAndreas Gohr $bcc = subscriber_addresslist($id); 118326a0801fSAndreas Gohr if(empty($bcc)) return; 118426a0801fSAndreas Gohr $to = ''; 118526a0801fSAndreas Gohr $text = rawLocale('subscribermail'); 1186a06e4bdbSSebastian Harl }elseif($who == 'register'){ 1187a06e4bdbSSebastian Harl if(empty($conf['registernotify'])) return; 1188a06e4bdbSSebastian Harl $text = rawLocale('registermail'); 1189a06e4bdbSSebastian Harl $to = $conf['registernotify']; 1190a06e4bdbSSebastian Harl $bcc = ''; 119126a0801fSAndreas Gohr }else{ 119226a0801fSAndreas Gohr return; //just to be safe 119326a0801fSAndreas Gohr } 119426a0801fSAndreas Gohr 1195f3f0262cSandi $text = str_replace('@DATE@',date($conf['dformat']),$text); 1196f3f0262cSandi $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 1197f3f0262cSandi $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 1198f3f0262cSandi $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 1199ed7b5f09Sandi $text = str_replace('@NEWPAGE@',wl($id,'',true),$text); 120026a0801fSAndreas Gohr $text = str_replace('@PAGE@',$id,$text); 120126a0801fSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 1202ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 1203f3f0262cSandi $text = str_replace('@SUMMARY@',$summary,$text); 12047a82afdcSandi $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 1205f3f0262cSandi 120602a498e7Schris foreach ($replace as $key => $substitution) { 120702a498e7Schris $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 120802a498e7Schris } 120902a498e7Schris 1210a06e4bdbSSebastian Harl if($who == 'register'){ 1211a06e4bdbSSebastian Harl $subject = $lang['mail_new_user'].' '.$summary; 1212a06e4bdbSSebastian Harl }elseif($rev){ 1213f3f0262cSandi $subject = $lang['mail_changed'].' '.$id; 1214ed7b5f09Sandi $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text); 1215ccdfa6c0SAndreas Gohr require_once(DOKU_INC.'inc/DifferenceEngine.php'); 1216f3f0262cSandi $df = new Diff(split("\n",rawWiki($id,$rev)), 1217f3f0262cSandi split("\n",rawWiki($id))); 1218f3f0262cSandi $dformat = new UnifiedDiffFormatter(); 1219f3f0262cSandi $diff = $dformat->format($df); 1220f3f0262cSandi }else{ 1221f3f0262cSandi $subject=$lang['mail_newpage'].' '.$id; 1222f3f0262cSandi $text = str_replace('@OLDPAGE@','none',$text); 1223f3f0262cSandi $diff = rawWiki($id); 1224f3f0262cSandi } 1225f3f0262cSandi $text = str_replace('@DIFF@',$diff,$text); 1226241f3a36Sandi $subject = '['.$conf['title'].'] '.$subject; 1227f3f0262cSandi 122826a0801fSAndreas Gohr mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc); 1229f3f0262cSandi} 1230f3f0262cSandi 123115fae107Sandi/** 1232f3f0262cSandi * extracts the query from a google referer 123315fae107Sandi * 12346b13307fSandi * @todo should be more generic and support yahoo et al 123515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1236f3f0262cSandi */ 1237f3f0262cSandifunction getGoogleQuery(){ 1238f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 12395c3f206fSandi if(!$url) return ''; 1240f3f0262cSandi 1241f3f0262cSandi if(!preg_match("#google\.#i",$url['host'])) return ''; 1242f3f0262cSandi $query = array(); 1243f3f0262cSandi parse_str($url['query'],$query); 1244f3f0262cSandi 1245f3f0262cSandi return $query['q']; 1246f3f0262cSandi} 1247f3f0262cSandi 1248f3f0262cSandi/** 124915fae107Sandi * Try to set correct locale 125015fae107Sandi * 1251095bfd5cSandi * @deprecated No longer used 125215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1253f3f0262cSandi */ 1254f3f0262cSandifunction setCorrectLocale(){ 1255f3f0262cSandi global $conf; 1256f3f0262cSandi global $lang; 1257f3f0262cSandi 1258f3f0262cSandi $enc = strtoupper($lang['encoding']); 1259f3f0262cSandi foreach ($lang['locales'] as $loc){ 1260f3f0262cSandi //try locale 1261f3f0262cSandi if(@setlocale(LC_ALL,$loc)) return; 1262f3f0262cSandi //try loceale with encoding 1263f3f0262cSandi if(@setlocale(LC_ALL,"$loc.$enc")) return; 1264f3f0262cSandi } 1265f3f0262cSandi //still here? try to set from environment 1266f3f0262cSandi @setlocale(LC_ALL,""); 1267f3f0262cSandi} 1268f3f0262cSandi 1269f3f0262cSandi/** 1270f3f0262cSandi * Return the human readable size of a file 1271f3f0262cSandi * 1272f3f0262cSandi * @param int $size A file size 1273f3f0262cSandi * @param int $dec A number of decimal places 1274f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1275f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1276f3f0262cSandi * @version 1.0.0 1277f3f0262cSandi */ 1278f31d5b73Sandifunction filesize_h($size, $dec = 1){ 1279f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1280f3f0262cSandi $count = count($sizes); 1281f3f0262cSandi $i = 0; 1282f3f0262cSandi 1283f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 1284f3f0262cSandi $size /= 1024; 1285f3f0262cSandi $i++; 1286f3f0262cSandi } 1287f3f0262cSandi 1288f3f0262cSandi return round($size, $dec) . ' ' . $sizes[$i]; 1289f3f0262cSandi} 1290f3f0262cSandi 129115fae107Sandi/** 129200a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 129300a7b5adSEsther Brunner * 129400a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 129500a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 129600a7b5adSEsther Brunner */ 129700a7b5adSEsther Brunnerfunction obfuscate($email) { 129800a7b5adSEsther Brunner global $conf; 129900a7b5adSEsther Brunner 130000a7b5adSEsther Brunner switch ($conf['mailguard']) { 130100a7b5adSEsther Brunner case 'visible' : 130200a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 130300a7b5adSEsther Brunner return strtr($email, $obfuscate); 130400a7b5adSEsther Brunner 130500a7b5adSEsther Brunner case 'hex' : 130600a7b5adSEsther Brunner $encode = ''; 130700a7b5adSEsther Brunner for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';'; 130800a7b5adSEsther Brunner return $encode; 130900a7b5adSEsther Brunner 131000a7b5adSEsther Brunner case 'none' : 131100a7b5adSEsther Brunner default : 131200a7b5adSEsther Brunner return $email; 131300a7b5adSEsther Brunner } 131400a7b5adSEsther Brunner} 131500a7b5adSEsther Brunner 131600a7b5adSEsther Brunner/** 1317dc57ef04Sandi * Return DokuWikis version 131815fae107Sandi * 131915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 132015fae107Sandi */ 1321f31d5b73Sandifunction getVersion(){ 1322f31d5b73Sandi //import version string 1323f31d5b73Sandi if(@file_exists('VERSION')){ 1324f31d5b73Sandi //official release 13250647ce3bSAndreas Gohr return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION')); 1326f31d5b73Sandi }elseif(is_dir('_darcs')){ 1327f31d5b73Sandi //darcs checkout 1328f31d5b73Sandi $inv = file('_darcs/inventory'); 1329ae41559bSAndreas Gohr $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv); 1330f31d5b73Sandi $cur = array_pop($inv); 1331f31d5b73Sandi preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 1332f31d5b73Sandi return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 1333f31d5b73Sandi }else{ 1334f31d5b73Sandi return 'snapshot?'; 1335f31d5b73Sandi } 1336f31d5b73Sandi} 1337f31d5b73Sandi 1338f31d5b73Sandi/** 1339f31d5b73Sandi * Run a few sanity checks 1340f31d5b73Sandi * 1341f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org> 1342f31d5b73Sandi */ 1343f3f0262cSandifunction check(){ 1344f3f0262cSandi global $conf; 1345f3f0262cSandi global $INFO; 1346f3f0262cSandi 1347f31d5b73Sandi msg('DokuWiki version: '.getVersion(),1); 1348f31d5b73Sandi 134949022a38Sandi if(version_compare(phpversion(),'4.3.0','<')){ 135049022a38Sandi msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 135149022a38Sandi }elseif(version_compare(phpversion(),'4.3.10','<')){ 135249022a38Sandi msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 135349022a38Sandi }else{ 135449022a38Sandi msg('PHP version '.phpversion(),1); 135549022a38Sandi } 135649022a38Sandi 1357f3f0262cSandi if(is_writable($conf['changelog'])){ 1358f3f0262cSandi msg('Changelog is writable',1); 1359f3f0262cSandi }else{ 1360d8186216SBen Coburn if (@file_exists($conf['changelog'])) { 1361f3f0262cSandi msg('Changelog is not writable',-1); 1362f3f0262cSandi } 136371726d78SBen Coburn } 136471726d78SBen Coburn 1365d8186216SBen Coburn if (isset($conf['changelog_old']) && @file_exists($conf['changelog_old'])) { 136671726d78SBen Coburn msg('Old changelog exists.', 0); 136771726d78SBen Coburn } 136871726d78SBen Coburn 1369d8186216SBen Coburn if (@file_exists($conf['changelog'].'_failed')) { 137071726d78SBen Coburn msg('Importing old changelog failed.', -1); 1371d8186216SBen Coburn } else if (@file_exists($conf['changelog'].'_importing')) { 137271726d78SBen Coburn msg('Importing old changelog now.', 0); 1373d8186216SBen Coburn } else if (@file_exists($conf['changelog'].'_import_ok')) { 137471726d78SBen Coburn msg('Old changelog imported.', 1); 1375*e45b34cdSBen Coburn if (!plugin_isdisabled('importoldchangelog')) { 1376*e45b34cdSBen Coburn msg('Importoldchangelog plugin not disabled after import.', -1); 1377*e45b34cdSBen Coburn } 137871726d78SBen Coburn } 1379f3f0262cSandi 1380f3f0262cSandi if(is_writable($conf['datadir'])){ 1381f3f0262cSandi msg('Datadir is writable',1); 1382f3f0262cSandi }else{ 1383f3f0262cSandi msg('Datadir is not writable',-1); 1384f3f0262cSandi } 1385f3f0262cSandi 1386f3f0262cSandi if(is_writable($conf['olddir'])){ 1387f3f0262cSandi msg('Attic is writable',1); 1388f3f0262cSandi }else{ 1389f3f0262cSandi msg('Attic is not writable',-1); 1390f3f0262cSandi } 1391f3f0262cSandi 1392f3f0262cSandi if(is_writable($conf['mediadir'])){ 1393f3f0262cSandi msg('Mediadir is writable',1); 1394f3f0262cSandi }else{ 1395f3f0262cSandi msg('Mediadir is not writable',-1); 1396f3f0262cSandi } 1397f3f0262cSandi 139898407a7aSandi if(is_writable($conf['cachedir'])){ 139998407a7aSandi msg('Cachedir is writable',1); 140098407a7aSandi }else{ 140198407a7aSandi msg('Cachedir is not writable',-1); 140298407a7aSandi } 140398407a7aSandi 14047de6c234SAndreas Gohr if(is_writable($conf['lockdir'])){ 14057de6c234SAndreas Gohr msg('Lockdir is writable',1); 14067de6c234SAndreas Gohr }else{ 14077de6c234SAndreas Gohr msg('Lockdir is not writable',-1); 14087de6c234SAndreas Gohr } 14097de6c234SAndreas Gohr 1410e7cb32dcSAndreas Gohr if(is_writable(DOKU_CONF.'users.auth.php')){ 14118c4f28e8Sjan msg('conf/users.auth.php is writable',1); 1412f3f0262cSandi }else{ 14138c4f28e8Sjan msg('conf/users.auth.php is not writable',0); 1414f3f0262cSandi } 141593a9e835Sandi 141693a9e835Sandi if(function_exists('mb_strpos')){ 141793a9e835Sandi if(defined('UTF8_NOMBSTRING')){ 141893a9e835Sandi msg('mb_string extension is available but will not be used',0); 141993a9e835Sandi }else{ 142093a9e835Sandi msg('mb_string extension is available and will be used',1); 142193a9e835Sandi } 142293a9e835Sandi }else{ 142393a9e835Sandi msg('mb_string extension not available - PHP only replacements will be used',0); 142493a9e835Sandi } 1425f42d1c75SAndreas Gohr 1426f42d1c75SAndreas Gohr if($conf['allowdebug']){ 1427f42d1c75SAndreas Gohr msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 1428f42d1c75SAndreas Gohr }else{ 1429f42d1c75SAndreas Gohr msg('Debugging support is disabled',1); 1430f42d1c75SAndreas Gohr } 1431f3f0262cSandi 1432f3f0262cSandi msg('Your current permission for this page is '.$INFO['perm'],0); 1433f3f0262cSandi 1434f3f0262cSandi if(is_writable($INFO['filepath'])){ 1435f3f0262cSandi msg('The current page is writable by the webserver',0); 1436f3f0262cSandi }else{ 1437f3f0262cSandi msg('The current page is not writable by the webserver',0); 1438f3f0262cSandi } 1439f3f0262cSandi 1440f3f0262cSandi if($INFO['writable']){ 1441f3f0262cSandi msg('The current page is writable by you',0); 1442f3f0262cSandi }else{ 1443f3f0262cSandi msg('The current page is not writable you',0); 1444f3f0262cSandi } 1445f3f0262cSandi} 1446340756e4Sandi 1447b158d625SSteven Danz/** 1448b158d625SSteven Danz * Let us know if a user is tracking a page 1449b158d625SSteven Danz * 14501380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1451b158d625SSteven Danz */ 14521380fc45SAndreas Gohrfunction is_subscribed($id,$uid){ 14531380fc45SAndreas Gohr $file=metaFN($id,'.mlist'); 14541380fc45SAndreas Gohr if (@file_exists($file)) { 1455b158d625SSteven Danz $mlist = file($file); 14561380fc45SAndreas Gohr $pos = array_search($uid."\n",$mlist); 14571380fc45SAndreas Gohr return is_int($pos); 1458b158d625SSteven Danz } 14591380fc45SAndreas Gohr 1460b158d625SSteven Danz return false; 1461b158d625SSteven Danz} 1462340756e4Sandi 1463f9eb5648Ssteven-danz/** 1464f9eb5648Ssteven-danz * Return a string with the email addresses of all the 1465f9eb5648Ssteven-danz * users subscribed to a page 1466f9eb5648Ssteven-danz * 146726a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com> 1468f9eb5648Ssteven-danz */ 1469f9eb5648Ssteven-danzfunction subscriber_addresslist($id){ 1470f9eb5648Ssteven-danz global $conf; 1471cd52f92dSchris global $auth; 1472f9eb5648Ssteven-danz 1473f9eb5648Ssteven-danz $emails = ''; 1474f9eb5648Ssteven-danz 147526a0801fSAndreas Gohr if (!$conf['subscribers']) return; 147626a0801fSAndreas Gohr 1477f9eb5648Ssteven-danz $mlist = array(); 1478f9eb5648Ssteven-danz $file=metaFN($id,'.mlist'); 1479d8186216SBen Coburn if (@file_exists($file)) { 1480f9eb5648Ssteven-danz $mlist = file($file); 1481f9eb5648Ssteven-danz } 1482f9eb5648Ssteven-danz if(count($mlist) > 0) { 1483f9eb5648Ssteven-danz foreach ($mlist as $who) { 1484f9eb5648Ssteven-danz $who = rtrim($who); 1485cd52f92dSchris $info = $auth->getUserData($who); 1486f9eb5648Ssteven-danz $level = auth_aclcheck($id,$who,$info['grps']); 1487f9eb5648Ssteven-danz if ($level >= AUTH_READ) { 1488f9eb5648Ssteven-danz if (strcasecmp($info['mail'],$conf['notify']) != 0) { 1489f9eb5648Ssteven-danz if (empty($emails)) { 1490f9eb5648Ssteven-danz $emails = $info['mail']; 1491f9eb5648Ssteven-danz } else { 1492f9eb5648Ssteven-danz $emails = "$emails,".$info['mail']; 1493f9eb5648Ssteven-danz } 1494f9eb5648Ssteven-danz } 1495f9eb5648Ssteven-danz } 1496f9eb5648Ssteven-danz } 1497f9eb5648Ssteven-danz } 1498f9eb5648Ssteven-danz 1499f9eb5648Ssteven-danz return $emails; 1500f9eb5648Ssteven-danz} 1501f9eb5648Ssteven-danz 150289541d4bSAndreas Gohr/** 150389541d4bSAndreas Gohr * Removes quoting backslashes 150489541d4bSAndreas Gohr * 150589541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 150689541d4bSAndreas Gohr */ 150789541d4bSAndreas Gohrfunction unslash($string,$char="'"){ 150889541d4bSAndreas Gohr return str_replace('\\'.$char,$char,$string); 150989541d4bSAndreas Gohr} 151089541d4bSAndreas Gohr 1511340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 1512