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 97652610a2Sandi //who's the editor 98652610a2Sandi if($REV){ 99652610a2Sandi $revinfo = getRevisionInfo($ID,$REV); 100652610a2Sandi }else{ 101652610a2Sandi $revinfo = getRevisionInfo($ID,$info['lastmod']); 102652610a2Sandi } 103652610a2Sandi $info['ip'] = $revinfo['ip']; 104652610a2Sandi $info['user'] = $revinfo['user']; 105652610a2Sandi $info['sum'] = $revinfo['sum']; 106b6912aeaSAndreas Gohr $info['minor'] = $revinfo['minor']; 10759f257aeSchris 10888f522e9Sandi if($revinfo['user']){ 10988f522e9Sandi $info['editor'] = $revinfo['user']; 11088f522e9Sandi }else{ 11188f522e9Sandi $info['editor'] = $revinfo['ip']; 11288f522e9Sandi } 113652610a2Sandi 114ee4c4a1bSAndreas Gohr // draft 115ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID,'.draft'); 116ee4c4a1bSAndreas Gohr if(@file_exists($draft)){ 117ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))){ 118ee4c4a1bSAndreas Gohr // remove stale draft 119ee4c4a1bSAndreas Gohr @unlink($draft); 120ee4c4a1bSAndreas Gohr }else{ 121ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 122ee4c4a1bSAndreas Gohr } 123ee4c4a1bSAndreas Gohr } 124ee4c4a1bSAndreas Gohr 125f3f0262cSandi return $info; 126f3f0262cSandi} 127f3f0262cSandi 128f3f0262cSandi/** 1292684e50aSAndreas Gohr * Build an string of URL parameters 1302684e50aSAndreas Gohr * 1312684e50aSAndreas Gohr * @author Andreas Gohr 1322684e50aSAndreas Gohr */ 133b174aeaeSchrisfunction buildURLparams($params, $sep='&'){ 1342684e50aSAndreas Gohr $url = ''; 1352684e50aSAndreas Gohr $amp = false; 1362684e50aSAndreas Gohr foreach($params as $key => $val){ 137b174aeaeSchris if($amp) $url .= $sep; 1382684e50aSAndreas Gohr 1392684e50aSAndreas Gohr $url .= $key.'='; 140b6c6979fSAndreas Gohr $url .= rawurlencode($val); 1412684e50aSAndreas Gohr $amp = true; 1422684e50aSAndreas Gohr } 1432684e50aSAndreas Gohr return $url; 1442684e50aSAndreas Gohr} 1452684e50aSAndreas Gohr 1462684e50aSAndreas Gohr/** 1472684e50aSAndreas Gohr * Build an string of html tag attributes 1482684e50aSAndreas Gohr * 1492684e50aSAndreas Gohr * @author Andreas Gohr 1502684e50aSAndreas Gohr */ 1512684e50aSAndreas Gohrfunction buildAttributes($params){ 1522684e50aSAndreas Gohr $url = ''; 1532684e50aSAndreas Gohr foreach($params as $key => $val){ 1542684e50aSAndreas Gohr $url .= $key.'="'; 1552684e50aSAndreas Gohr $url .= htmlspecialchars ($val); 1562684e50aSAndreas Gohr $url .= '" '; 1572684e50aSAndreas Gohr } 1582684e50aSAndreas Gohr return $url; 1592684e50aSAndreas Gohr} 1602684e50aSAndreas Gohr 1612684e50aSAndreas Gohr 1622684e50aSAndreas Gohr/** 1630396becbSandi * print a message 1640396becbSandi * 1650396becbSandi * If HTTP headers were not sent yet the message is added 1660396becbSandi * to the global message array else it's printed directly 1670396becbSandi * using html_msgarea() 1680396becbSandi * 169f3f0262cSandi * 170f3f0262cSandi * Levels can be: 171f3f0262cSandi * 172f3f0262cSandi * -1 error 173f3f0262cSandi * 0 info 174f3f0262cSandi * 1 success 17515fae107Sandi * 17615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1770396becbSandi * @see html_msgarea 178f3f0262cSandi */ 1790d58d74eSAndreas Gohrfunction msg($message,$lvl=0,$line='',$file=''){ 180f3f0262cSandi global $MSG; 181f3f0262cSandi $errors[-1] = 'error'; 182f3f0262cSandi $errors[0] = 'info'; 183f3f0262cSandi $errors[1] = 'success'; 184f3f0262cSandi 1850d58d74eSAndreas Gohr if($line || $file) $message.=' ['.basename($file).':'.$line.']'; 1860d58d74eSAndreas Gohr 187cc20ad51Sandi if(!headers_sent()){ 188f3f0262cSandi if(!isset($MSG)) $MSG = array(); 189f3f0262cSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 1900396becbSandi }else{ 1910396becbSandi $MSG = array(); 1920396becbSandi $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); 193f62ea8a1Sandi if(function_exists('html_msgarea')){ 1940396becbSandi html_msgarea(); 195f62ea8a1Sandi }else{ 196f62ea8a1Sandi print "ERROR($lvl) $message"; 197f62ea8a1Sandi } 1980396becbSandi } 199f3f0262cSandi} 200f3f0262cSandi 201f3f0262cSandi/** 20215fae107Sandi * This builds the breadcrumb trail and returns it as array 20315fae107Sandi * 20415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 205f3f0262cSandi */ 206f3f0262cSandifunction breadcrumbs(){ 2078746e727Sandi // we prepare the breadcrumbs early for quick session closing 2088746e727Sandi static $crumbs = null; 2098746e727Sandi if($crumbs != null) return $crumbs; 2108746e727Sandi 211f3f0262cSandi global $ID; 212f3f0262cSandi global $ACT; 213f3f0262cSandi global $conf; 214f3f0262cSandi $crumbs = $_SESSION[$conf['title']]['bc']; 215f3f0262cSandi 216f3f0262cSandi //first visit? 217f3f0262cSandi if (!is_array($crumbs)){ 218f3f0262cSandi $crumbs = array(); 219f3f0262cSandi } 220f3f0262cSandi //we only save on show and existing wiki documents 221a77f5846Sjan $file = wikiFN($ID); 222a77f5846Sjan if($ACT != 'show' || !@file_exists($file)){ 223f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 224f3f0262cSandi return $crumbs; 225f3f0262cSandi } 226a77f5846Sjan 227a77f5846Sjan // page names 228a77f5846Sjan $name = noNS($ID); 229a77f5846Sjan if ($conf['useheading']) { 230a77f5846Sjan // get page title 231bb0a59d4Sjan $title = p_get_first_heading($ID); 232a77f5846Sjan if ($title) { 233a77f5846Sjan $name = $title; 234a77f5846Sjan } 235a77f5846Sjan } 236a77f5846Sjan 237f3f0262cSandi //remove ID from array 238a77f5846Sjan if (isset($crumbs[$ID])) { 239a77f5846Sjan unset($crumbs[$ID]); 240f3f0262cSandi } 241f3f0262cSandi 242f3f0262cSandi //add to array 243a77f5846Sjan $crumbs[$ID] = $name; 244f3f0262cSandi //reduce size 245f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']){ 246f3f0262cSandi array_shift($crumbs); 247f3f0262cSandi } 248f3f0262cSandi //save to session 249f3f0262cSandi $_SESSION[$conf['title']]['bc'] = $crumbs; 250f3f0262cSandi return $crumbs; 251f3f0262cSandi} 252f3f0262cSandi 253f3f0262cSandi/** 25415fae107Sandi * Filter for page IDs 25515fae107Sandi * 256f3f0262cSandi * This is run on a ID before it is outputted somewhere 257f3f0262cSandi * currently used to replace the colon with something else 258f3f0262cSandi * on Windows systems and to have proper URL encoding 25915fae107Sandi * 26049c713a3Sandi * Urlencoding is ommitted when the second parameter is false 26149c713a3Sandi * 26215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 263f3f0262cSandi */ 26449c713a3Sandifunction idfilter($id,$ue=true){ 265f3f0262cSandi global $conf; 266f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']){ 267f3f0262cSandi $id = strtr($id,':','/'); 268f3f0262cSandi }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 269f3f0262cSandi $conf['userewrite']) { 270f3f0262cSandi $id = strtr($id,':',';'); 271f3f0262cSandi } 27249c713a3Sandi if($ue){ 273b6c6979fSAndreas Gohr $id = rawurlencode($id); 274f3f0262cSandi $id = str_replace('%3A',':',$id); //keep as colon 275f3f0262cSandi $id = str_replace('%2F','/',$id); //keep as slash 27649c713a3Sandi } 277f3f0262cSandi return $id; 278f3f0262cSandi} 279f3f0262cSandi 280f3f0262cSandi/** 281ed7b5f09Sandi * This builds a link to a wikipage 28215fae107Sandi * 2836c7843b5Sandi * It handles URL rewriting and adds additional parameter if 2846c7843b5Sandi * given in $more 2856c7843b5Sandi * 28615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 287f3f0262cSandi */ 288b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&'){ 289f3f0262cSandi global $conf; 2906de3759aSAndreas Gohr if(is_array($more)){ 291b174aeaeSchris $more = buildURLparams($more,$sep); 2926de3759aSAndreas Gohr }else{ 293b174aeaeSchris $more = str_replace(',',$sep,$more); 2946de3759aSAndreas Gohr } 295f3f0262cSandi 296f3f0262cSandi $id = idfilter($id); 297ed7b5f09Sandi if($abs){ 298ed7b5f09Sandi $xlink = DOKU_URL; 299ed7b5f09Sandi }else{ 300ed7b5f09Sandi $xlink = DOKU_BASE; 301ed7b5f09Sandi } 302f3f0262cSandi 3036c7843b5Sandi if($conf['userewrite'] == 2){ 3046c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 3056c7843b5Sandi if($more) $xlink .= '?'.$more; 3066c7843b5Sandi }elseif($conf['userewrite']){ 307f3f0262cSandi $xlink .= $id; 308f3f0262cSandi if($more) $xlink .= '?'.$more; 3096c7843b5Sandi }else{ 3106c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 311b174aeaeSchris if($more) $xlink .= $sep.$more; 312f3f0262cSandi } 313f3f0262cSandi 314f3f0262cSandi return $xlink; 315f3f0262cSandi} 316f3f0262cSandi 317f3f0262cSandi/** 318f5c2808fSBen Coburn * This builds a link to an alternate page format 319f5c2808fSBen Coburn * 320f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 321f5c2808fSBen Coburn * 322f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 323f5c2808fSBen Coburn */ 324f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&'){ 325f5c2808fSBen Coburn global $conf; 326f5c2808fSBen Coburn if(is_array($more)){ 327f5c2808fSBen Coburn $more = buildURLparams($more,$sep); 328f5c2808fSBen Coburn }else{ 329f5c2808fSBen Coburn $more = str_replace(',',$sep,$more); 330f5c2808fSBen Coburn } 331f5c2808fSBen Coburn 332f5c2808fSBen Coburn $format = rawurlencode($format); 333f5c2808fSBen Coburn $id = idfilter($id); 334f5c2808fSBen Coburn if($abs){ 335f5c2808fSBen Coburn $xlink = DOKU_URL; 336f5c2808fSBen Coburn }else{ 337f5c2808fSBen Coburn $xlink = DOKU_BASE; 338f5c2808fSBen Coburn } 339f5c2808fSBen Coburn 340f5c2808fSBen Coburn if($conf['userewrite'] == 2){ 341f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 342f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 343f5c2808fSBen Coburn }elseif($conf['userewrite'] == 1){ 344f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 345f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 346f5c2808fSBen Coburn }else{ 347f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 348f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 349f5c2808fSBen Coburn } 350f5c2808fSBen Coburn 351f5c2808fSBen Coburn return $xlink; 352f5c2808fSBen Coburn} 353f5c2808fSBen Coburn 354f5c2808fSBen Coburn/** 3556de3759aSAndreas Gohr * Build a link to a media file 3566de3759aSAndreas Gohr * 3576de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 3586de3759aSAndreas Gohr */ 359b174aeaeSchrisfunction ml($id='',$more='',$direct=true,$sep='&'){ 3606de3759aSAndreas Gohr global $conf; 3616de3759aSAndreas Gohr if(is_array($more)){ 362b174aeaeSchris $more = buildURLparams($more,$sep); 3636de3759aSAndreas Gohr }else{ 364b174aeaeSchris $more = str_replace(',',$sep,$more); 3656de3759aSAndreas Gohr } 3666de3759aSAndreas Gohr 3676de3759aSAndreas Gohr $xlink = DOKU_BASE; 3686de3759aSAndreas Gohr 3696de3759aSAndreas Gohr // external URLs are always direct without rewriting 3706de3759aSAndreas Gohr if(preg_match('#^(https?|ftp)://#i',$id)){ 3716de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 3726de3759aSAndreas Gohr if($more){ 3736de3759aSAndreas Gohr $xlink .= '?'.$more; 374b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 3756de3759aSAndreas Gohr }else{ 376b6c6979fSAndreas Gohr $xlink .= '?media='.rawurlencode($id); 3776de3759aSAndreas Gohr } 3786de3759aSAndreas Gohr return $xlink; 3796de3759aSAndreas Gohr } 3806de3759aSAndreas Gohr 3816de3759aSAndreas Gohr $id = idfilter($id); 3826de3759aSAndreas Gohr 3836de3759aSAndreas Gohr // decide on scriptname 3846de3759aSAndreas Gohr if($direct){ 3856de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 3866de3759aSAndreas Gohr $script = '_media'; 3876de3759aSAndreas Gohr }else{ 3886de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 3896de3759aSAndreas Gohr } 3906de3759aSAndreas Gohr }else{ 3916de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 3926de3759aSAndreas Gohr $script = '_detail'; 3936de3759aSAndreas Gohr }else{ 3946de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 3956de3759aSAndreas Gohr } 3966de3759aSAndreas Gohr } 3976de3759aSAndreas Gohr 3986de3759aSAndreas Gohr // build URL based on rewrite mode 3996de3759aSAndreas Gohr if($conf['userewrite']){ 4006de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 4016de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 4026de3759aSAndreas Gohr }else{ 4036de3759aSAndreas Gohr if($more){ 404a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 405b174aeaeSchris $xlink .= $sep.'media='.$id; 4066de3759aSAndreas Gohr }else{ 407a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 4086de3759aSAndreas Gohr } 4096de3759aSAndreas Gohr } 4106de3759aSAndreas Gohr 4116de3759aSAndreas Gohr return $xlink; 4126de3759aSAndreas Gohr} 4136de3759aSAndreas Gohr 4146de3759aSAndreas Gohr 4156de3759aSAndreas Gohr 4166de3759aSAndreas Gohr/** 417f3f0262cSandi * Just builds a link to a script 41815fae107Sandi * 419ed7b5f09Sandi * @todo maybe obsolete 42015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 421f3f0262cSandi */ 422f3f0262cSandifunction script($script='doku.php'){ 423ed7b5f09Sandi# $link = getBaseURL(); 424ed7b5f09Sandi# $link .= $script; 425ed7b5f09Sandi# return $link; 426ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 427f3f0262cSandi} 428f3f0262cSandi 429f3f0262cSandi/** 43015fae107Sandi * Spamcheck against wordlist 43115fae107Sandi * 432f3f0262cSandi * Checks the wikitext against a list of blocked expressions 433f3f0262cSandi * returns true if the text contains any bad words 43415fae107Sandi * 43515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 436f3f0262cSandi */ 437f3f0262cSandifunction checkwordblock(){ 438f3f0262cSandi global $TEXT; 439f3f0262cSandi global $conf; 440f3f0262cSandi 441f3f0262cSandi if(!$conf['usewordblock']) return false; 442f3f0262cSandi 443b9ac8716Schris $wordblocks = getWordblocks(); 4443e2965d7Sandi //how many lines to read at once (to work around some PCRE limits) 4453e2965d7Sandi if(version_compare(phpversion(),'4.3.0','<')){ 4463e2965d7Sandi //old versions of PCRE define a maximum of parenthesises even if no 4473e2965d7Sandi //backreferences are used - the maximum is 99 4483e2965d7Sandi //this is very bad performancewise and may even be too high still 4493e2965d7Sandi $chunksize = 40; 4503e2965d7Sandi }else{ 451703f6fdeSandi //read file in chunks of 600 - this should work around the 4523e2965d7Sandi //MAX_PATTERN_SIZE in modern PCRE 453444b87a5SAndreas Gohr $chunksize = 400; 4543e2965d7Sandi } 455b9ac8716Schris while($blocks = array_splice($wordblocks,0,$chunksize)){ 456f3f0262cSandi $re = array(); 457f3f0262cSandi #build regexp from blocks 458f3f0262cSandi foreach($blocks as $block){ 459f3f0262cSandi $block = preg_replace('/#.*$/','',$block); 460f3f0262cSandi $block = trim($block); 461f3f0262cSandi if(empty($block)) continue; 462f3f0262cSandi $re[] = $block; 463f3f0262cSandi } 464b9ac8716Schris if(preg_match('#('.join('|',$re).')#si',$TEXT, $match=array())) { 465b9ac8716Schris return true; 466b9ac8716Schris } 467703f6fdeSandi } 468f3f0262cSandi return false; 469f3f0262cSandi} 470f3f0262cSandi 471f3f0262cSandi/** 47215fae107Sandi * Return the IP of the client 47315fae107Sandi * 4746d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 47515fae107Sandi * 4766d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 4776d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 4786d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 4796d8affe6SAndreas Gohr * headers 4806d8affe6SAndreas Gohr * 4816d8affe6SAndreas Gohr * @param boolean $single If set only a single IP is returned 48215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 483f3f0262cSandi */ 4846d8affe6SAndreas Gohrfunction clientIP($single=false){ 4856d8affe6SAndreas Gohr $ip = array(); 4866d8affe6SAndreas Gohr $ip[] = $_SERVER['REMOTE_ADDR']; 4876d8affe6SAndreas Gohr if($_SERVER['HTTP_X_FORWARDED_FOR']) 4886d8affe6SAndreas Gohr $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])); 4896d8affe6SAndreas Gohr if($_SERVER['HTTP_X_REAL_IP']) 4906d8affe6SAndreas Gohr $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP'])); 4916d8affe6SAndreas Gohr 4926d8affe6SAndreas Gohr // remove any non-IP stuff 4936d8affe6SAndreas Gohr $cnt = count($ip); 4946d8affe6SAndreas Gohr for($i=0; $i<$cnt; $i++){ 4956d8affe6SAndreas Gohr $ip[$i] = preg_replace('/[^0-9\.]+/','',$ip[$i]); 4966d8affe6SAndreas Gohr if(!preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/',$ip[$i])) $ip[$i] = ''; 4976d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 498f3f0262cSandi } 4996d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 5006d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 5016d8affe6SAndreas Gohr 5026d8affe6SAndreas Gohr if(!$single) return join(',',$ip); 5036d8affe6SAndreas Gohr 5046d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 5056d8affe6SAndreas Gohr $ip = array_reverse($ip); 5066d8affe6SAndreas Gohr foreach($ip as $i){ 5076d8affe6SAndreas Gohr if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){ 5086d8affe6SAndreas Gohr continue; 5096d8affe6SAndreas Gohr }else{ 5106d8affe6SAndreas Gohr return $i; 5116d8affe6SAndreas Gohr } 5126d8affe6SAndreas Gohr } 5136d8affe6SAndreas Gohr // still here? just use the first (last) address 5146d8affe6SAndreas Gohr return $ip[0]; 515f3f0262cSandi} 516f3f0262cSandi 517f3f0262cSandi/** 51815fae107Sandi * Checks if a given page is currently locked. 51915fae107Sandi * 520f3f0262cSandi * removes stale lockfiles 52115fae107Sandi * 52215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 523f3f0262cSandi */ 524f3f0262cSandifunction checklock($id){ 525f3f0262cSandi global $conf; 526f3f0262cSandi $lock = wikiFN($id).'.lock'; 527f3f0262cSandi 528f3f0262cSandi //no lockfile 529f3f0262cSandi if(!@file_exists($lock)) return false; 530f3f0262cSandi 531f3f0262cSandi //lockfile expired 532f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']){ 533f3f0262cSandi unlink($lock); 534f3f0262cSandi return false; 535f3f0262cSandi } 536f3f0262cSandi 537f3f0262cSandi //my own lock 538f3f0262cSandi $ip = io_readFile($lock); 539f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 540f3f0262cSandi return false; 541f3f0262cSandi } 542f3f0262cSandi 543f3f0262cSandi return $ip; 544f3f0262cSandi} 545f3f0262cSandi 546f3f0262cSandi/** 54715fae107Sandi * Lock a page for editing 54815fae107Sandi * 54915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 550f3f0262cSandi */ 551f3f0262cSandifunction lock($id){ 552f3f0262cSandi $lock = wikiFN($id).'.lock'; 553f3f0262cSandi if($_SERVER['REMOTE_USER']){ 554f3f0262cSandi io_saveFile($lock,$_SERVER['REMOTE_USER']); 555f3f0262cSandi }else{ 556f3f0262cSandi io_saveFile($lock,clientIP()); 557f3f0262cSandi } 558f3f0262cSandi} 559f3f0262cSandi 560f3f0262cSandi/** 56115fae107Sandi * Unlock a page if it was locked by the user 562f3f0262cSandi * 56315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 56415fae107Sandi * @return bool true if a lock was removed 565f3f0262cSandi */ 566f3f0262cSandifunction unlock($id){ 567f3f0262cSandi $lock = wikiFN($id).'.lock'; 568f3f0262cSandi if(@file_exists($lock)){ 569f3f0262cSandi $ip = io_readFile($lock); 570f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 571f3f0262cSandi @unlink($lock); 572f3f0262cSandi return true; 573f3f0262cSandi } 574f3f0262cSandi } 575f3f0262cSandi return false; 576f3f0262cSandi} 577f3f0262cSandi 578f3f0262cSandi/** 579f3f0262cSandi * convert line ending to unix format 580f3f0262cSandi * 58115fae107Sandi * @see formText() for 2crlf conversion 58215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 583f3f0262cSandi */ 584f3f0262cSandifunction cleanText($text){ 585f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 586f3f0262cSandi return $text; 587f3f0262cSandi} 588f3f0262cSandi 589f3f0262cSandi/** 590f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 591f3f0262cSandi * It also converts line endings to Windows format which is 592f3f0262cSandi * pseudo standard for webforms. 593f3f0262cSandi * 59415fae107Sandi * @see cleanText() for 2unix conversion 59515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 596f3f0262cSandi */ 597f3f0262cSandifunction formText($text){ 598f3f0262cSandi $text = preg_replace("/\012/","\015\012",$text); 599f3f0262cSandi return htmlspecialchars($text); 600f3f0262cSandi} 601f3f0262cSandi 602f3f0262cSandi/** 60315fae107Sandi * Returns the specified local text in raw format 60415fae107Sandi * 60515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 606f3f0262cSandi */ 607f3f0262cSandifunction rawLocale($id){ 608f3f0262cSandi return io_readFile(localeFN($id)); 609f3f0262cSandi} 610f3f0262cSandi 611f3f0262cSandi/** 612f3f0262cSandi * Returns the raw WikiText 61315fae107Sandi * 61415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 615f3f0262cSandi */ 616f3f0262cSandifunction rawWiki($id,$rev=''){ 617f3f0262cSandi return io_readFile(wikiFN($id,$rev)); 618f3f0262cSandi} 619f3f0262cSandi 620f3f0262cSandi/** 6217146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 6227146cee2SAndreas Gohr * 6237146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6247146cee2SAndreas Gohr */ 6257146cee2SAndreas Gohrfunction pageTemplate($id){ 626a15ce62dSEsther Brunner global $conf; 627a15ce62dSEsther Brunner global $INFO; 628a15ce62dSEsther Brunner $tpl = io_readFile(dirname(wikiFN($id)).'/_template.txt'); 629a15ce62dSEsther Brunner $tpl = str_replace('@ID@',$id,$tpl); 630a15ce62dSEsther Brunner $tpl = str_replace('@NS@',getNS($id),$tpl); 631a15ce62dSEsther Brunner $tpl = str_replace('@PAGE@',strtr(noNS($id),'_',' '),$tpl); 632a15ce62dSEsther Brunner $tpl = str_replace('@USER@',$_SERVER['REMOTE_USER'],$tpl); 633a15ce62dSEsther Brunner $tpl = str_replace('@NAME@',$INFO['userinfo']['name'],$tpl); 634a15ce62dSEsther Brunner $tpl = str_replace('@MAIL@',$INFO['userinfo']['mail'],$tpl); 635a15ce62dSEsther Brunner $tpl = str_replace('@DATE@',date($conf['dformat']),$tpl); 636a15ce62dSEsther Brunner return $tpl; 6377146cee2SAndreas Gohr} 6387146cee2SAndreas Gohr 6397146cee2SAndreas Gohr 6407146cee2SAndreas Gohr/** 64115fae107Sandi * Returns the raw Wiki Text in three slices. 64215fae107Sandi * 64315fae107Sandi * The range parameter needs to have the form "from-to" 64415cfe303Sandi * and gives the range of the section in bytes - no 64515cfe303Sandi * UTF-8 awareness is needed. 646f3f0262cSandi * The returned order is prefix, section and suffix. 64715fae107Sandi * 64815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 649f3f0262cSandi */ 650f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){ 651f3f0262cSandi list($from,$to) = split('-',$range,2); 652f3f0262cSandi $text = io_readFile(wikiFN($id,$rev)); 653f3f0262cSandi if(!$from) $from = 0; 654c3d8e19bSandi if(!$to) $to = strlen($text)+1; 655f3f0262cSandi 65615cfe303Sandi $slices[0] = substr($text,0,$from-1); 65715cfe303Sandi $slices[1] = substr($text,$from-1,$to-$from); 65815cfe303Sandi $slices[2] = substr($text,$to); 659f3f0262cSandi 660f3f0262cSandi return $slices; 661f3f0262cSandi} 662f3f0262cSandi 663f3f0262cSandi/** 66415fae107Sandi * Joins wiki text slices 66515fae107Sandi * 666f3f0262cSandi * function to join the text slices with correct lineendings again. 667f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 668f3f0262cSandi * lines between sections if needed (used on saving). 66915fae107Sandi * 67015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 671f3f0262cSandi */ 672f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){ 673f3f0262cSandi 674f3f0262cSandi if($pretty){ 675f3f0262cSandi if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 676f3f0262cSandi if($suf && substr($text,-1) != "\n") $text .= "\n"; 677f3f0262cSandi } 678f3f0262cSandi 679f3f0262cSandi if($pre) $pre .= "\n"; 680f3f0262cSandi if($suf) $text .= "\n"; 681f3f0262cSandi return $pre.$text.$suf; 682f3f0262cSandi} 683f3f0262cSandi 684f3f0262cSandi/** 68515fae107Sandi * print debug messages 68615fae107Sandi * 687f3f0262cSandi * little function to print the content of a var 68815fae107Sandi * 68915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 690f3f0262cSandi */ 691f3f0262cSandifunction dbg($msg,$hidden=false){ 692f3f0262cSandi (!$hidden) ? print '<pre class="dbg">' : print "<!--\n"; 693f3f0262cSandi print_r($msg); 694f3f0262cSandi (!$hidden) ? print '</pre>' : print "\n-->"; 695f3f0262cSandi} 696f3f0262cSandi 697f3f0262cSandi/** 698f3f0262cSandi * Add's an entry to the changelog 69915fae107Sandi * 70015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 701f3f0262cSandi */ 702b6912aeaSAndreas Gohrfunction addLogEntry($date,$id,$summary='',$minor=false){ 703f3f0262cSandi global $conf; 704c1049928Sandi 705c1049928Sandi if(!@is_writable($conf['changelog'])){ 706c1049928Sandi msg($conf['changelog'].' is not writable!',-1); 707c1049928Sandi return; 708c1049928Sandi } 709c1049928Sandi 710652610a2Sandi if(!$date) $date = time(); //use current time if none supplied 711f3f0262cSandi $remote = $_SERVER['REMOTE_ADDR']; 712f3f0262cSandi $user = $_SERVER['REMOTE_USER']; 713f3f0262cSandi 714b6912aeaSAndreas Gohr if($conf['useacl'] && $user && $minor){ 715b6912aeaSAndreas Gohr $summary = '*'.$summary; 716b6912aeaSAndreas Gohr }else{ 717b6912aeaSAndreas Gohr $summary = ' '.$summary; 718b6912aeaSAndreas Gohr } 719b6912aeaSAndreas Gohr 720f3f0262cSandi $logline = join("\t",array($date,$remote,$id,$user,$summary))."\n"; 721dbb00abcSEsther Brunner io_saveFile($conf['changelog'],$logline,true); 722f3f0262cSandi} 723f3f0262cSandi 724f3f0262cSandi/** 725b6912aeaSAndreas Gohr * Checks an summary entry if it was a minor edit 726b6912aeaSAndreas Gohr * 727b6912aeaSAndreas Gohr * The summary is cleaned of the marker char 728b6912aeaSAndreas Gohr * 729b6912aeaSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 730b6912aeaSAndreas Gohr */ 731b6912aeaSAndreas Gohrfunction isMinor(&$summary){ 732b6912aeaSAndreas Gohr if(substr($summary,0,1) == '*'){ 733b6912aeaSAndreas Gohr $summary = substr($summary,1); 734b6912aeaSAndreas Gohr return true; 735b6912aeaSAndreas Gohr } 736b6912aeaSAndreas Gohr $summary = trim($summary); 737b6912aeaSAndreas Gohr return false; 738b6912aeaSAndreas Gohr} 739b6912aeaSAndreas Gohr 740b6912aeaSAndreas Gohr/** 741d437bcc4SAndreas Gohr * Internal function used by getRecents 742d437bcc4SAndreas Gohr * 743d437bcc4SAndreas Gohr * don't call directly 744d437bcc4SAndreas Gohr * 745d437bcc4SAndreas Gohr * @see getRecents() 746d437bcc4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 747d437bcc4SAndreas Gohr */ 748b6912aeaSAndreas Gohrfunction _handleRecent($line,$ns,$flags){ 749d437bcc4SAndreas Gohr static $seen = array(); //caches seen pages and skip them 750d437bcc4SAndreas Gohr if(empty($line)) return false; //skip empty lines 751d437bcc4SAndreas Gohr 752d437bcc4SAndreas Gohr // split the line into parts 753d437bcc4SAndreas Gohr list($dt,$ip,$id,$usr,$sum) = explode("\t",$line); 754d437bcc4SAndreas Gohr 755d437bcc4SAndreas Gohr // skip seen ones 756d437bcc4SAndreas Gohr if($seen[$id]) return false; 757b6912aeaSAndreas Gohr $recent = array(); 758b6912aeaSAndreas Gohr 759b6912aeaSAndreas Gohr // check minors 760b6912aeaSAndreas Gohr if(isMinor($sum)){ 761b6912aeaSAndreas Gohr // skip minors 762b6912aeaSAndreas Gohr if($flags & RECENTS_SKIP_MINORS) return false; 763b6912aeaSAndreas Gohr $recent['minor'] = true; 764b6912aeaSAndreas Gohr }else{ 765b6912aeaSAndreas Gohr $recent['minor'] = false; 766b6912aeaSAndreas Gohr } 767d437bcc4SAndreas Gohr 768d437bcc4SAndreas Gohr // remember in seen to skip additional sights 769d437bcc4SAndreas Gohr $seen[$id] = 1; 770d437bcc4SAndreas Gohr 7710dc92c6fSAndreas Gohr // check if it's a hidden page 7720dc92c6fSAndreas Gohr if(isHiddenPage($id)) return false; 7730dc92c6fSAndreas Gohr 774d437bcc4SAndreas Gohr // filter namespace 775d437bcc4SAndreas Gohr if (($ns) && (strpos($id,$ns.':') !== 0)) return false; 776d437bcc4SAndreas Gohr 777d437bcc4SAndreas Gohr // exclude subnamespaces 778b6912aeaSAndreas Gohr if (($flags & RECENTS_SKIP_SUBSPACES) && (getNS($id) != $ns)) return false; 779d437bcc4SAndreas Gohr 780ae56bfb6SAndreas Gohr // check ACL 781ae56bfb6SAndreas Gohr if (auth_quickaclcheck($id) < AUTH_READ) return false; 782ae56bfb6SAndreas Gohr 783d437bcc4SAndreas Gohr // check existance 784d437bcc4SAndreas Gohr if(!@file_exists(wikiFN($id))){ 785b6912aeaSAndreas Gohr if($flags & RECENTS_SKIP_DELETED){ 786d437bcc4SAndreas Gohr return false; 787d437bcc4SAndreas Gohr }else{ 788d437bcc4SAndreas Gohr $recent['del'] = true; 789d437bcc4SAndreas Gohr } 790d437bcc4SAndreas Gohr }else{ 791d437bcc4SAndreas Gohr $recent['del'] = false; 792d437bcc4SAndreas Gohr } 793d437bcc4SAndreas Gohr 794d437bcc4SAndreas Gohr $recent['id'] = $id; 795d437bcc4SAndreas Gohr $recent['date'] = $dt; 796d437bcc4SAndreas Gohr $recent['ip'] = $ip; 797d437bcc4SAndreas Gohr $recent['user'] = $usr; 798d437bcc4SAndreas Gohr $recent['sum'] = $sum; 799d437bcc4SAndreas Gohr 800d437bcc4SAndreas Gohr return $recent; 801d437bcc4SAndreas Gohr} 802d437bcc4SAndreas Gohr 803b6912aeaSAndreas Gohr 804d437bcc4SAndreas Gohr/** 805f3f0262cSandi * returns an array of recently changed files using the 806f3f0262cSandi * changelog 807d437bcc4SAndreas Gohr * 808b6912aeaSAndreas Gohr * The following constants can be used to control which changes are 809b6912aeaSAndreas Gohr * included. Add them together as needed. 810b6912aeaSAndreas Gohr * 811b6912aeaSAndreas Gohr * RECENTS_SKIP_DELETED - don't include deleted pages 812b6912aeaSAndreas Gohr * RECENTS_SKIP_MINORS - don't include minor changes 813b6912aeaSAndreas Gohr * RECENTS_SKIP_SUBSPACES - don't include subspaces 814b6912aeaSAndreas Gohr * 815d437bcc4SAndreas Gohr * @param int $first number of first entry returned (for paginating 816d437bcc4SAndreas Gohr * @param int $num return $num entries 817d437bcc4SAndreas Gohr * @param string $ns restrict to given namespace 818b6912aeaSAndreas Gohr * @param bool $flags see above 81915fae107Sandi * 82015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 821f3f0262cSandi */ 822b6912aeaSAndreas Gohrfunction getRecents($first,$num,$ns='',$flags=0){ 823f3f0262cSandi global $conf; 824f3f0262cSandi $recent = array(); 825d437bcc4SAndreas Gohr $count = 0; 8265749f1ceSmatthiasgrimm 8275749f1ceSmatthiasgrimm if(!$num) 8285749f1ceSmatthiasgrimm return $recent; 829f3f0262cSandi 830c1049928Sandi if(!@is_readable($conf['changelog'])){ 831c1049928Sandi msg($conf['changelog'].' is not readable',-1); 832c1049928Sandi return $recent; 833c1049928Sandi } 834c1049928Sandi 835d437bcc4SAndreas Gohr $fh = fopen($conf['changelog'],'r'); 836d437bcc4SAndreas Gohr $buf = ''; 837d437bcc4SAndreas Gohr $csz = 4096; //chunksize 838d437bcc4SAndreas Gohr fseek($fh,0,SEEK_END); // jump to the end 839d437bcc4SAndreas Gohr $pos = ftell($fh); // position pointer 840f3f0262cSandi 841d437bcc4SAndreas Gohr // now read backwards into buffer 842d437bcc4SAndreas Gohr while($pos > 0){ 843d437bcc4SAndreas Gohr $pos -= $csz; // seek to previous chunk... 844f6c1156dSWolfgang Ocker if($pos < 0) { // ...or rest of file 845f6c1156dSWolfgang Ocker $csz += $pos; 846f6c1156dSWolfgang Ocker $pos = 0; 847f6c1156dSWolfgang Ocker } 848f6c1156dSWolfgang Ocker 849d437bcc4SAndreas Gohr fseek($fh,$pos); 850dbb00abcSEsther Brunner 851d437bcc4SAndreas Gohr $buf = fread($fh,$csz).$buf; // prepend to buffer 8528f1d587cSEsther Brunner 853d437bcc4SAndreas Gohr $lines = explode("\n",$buf); // split buffer into lines 8545749f1ceSmatthiasgrimm 855d437bcc4SAndreas Gohr if($pos > 0){ 856d437bcc4SAndreas Gohr $buf = array_shift($lines); // first one may be still incomplete 857f3f0262cSandi } 858d437bcc4SAndreas Gohr 859d437bcc4SAndreas Gohr $cnt = count($lines); 860d437bcc4SAndreas Gohr if(!$cnt) continue; // no lines yet 861d437bcc4SAndreas Gohr 862d437bcc4SAndreas Gohr // handle lines 863d437bcc4SAndreas Gohr for($i = $cnt-1; $i >= 0; $i--){ 864b6912aeaSAndreas Gohr $rec = _handleRecent($lines[$i],$ns,$flags); 865d437bcc4SAndreas Gohr if($rec !== false){ 866d437bcc4SAndreas Gohr if(--$first >= 0) continue; // skip first entries 867d437bcc4SAndreas Gohr $recent[] = $rec; 868d437bcc4SAndreas Gohr $count++; 869d437bcc4SAndreas Gohr 870d437bcc4SAndreas Gohr // break while when we have enough entries 871d437bcc4SAndreas Gohr if($count >= $num){ 872d437bcc4SAndreas Gohr $pos = 0; // will break the while loop 873d437bcc4SAndreas Gohr break; // will break the for loop 874f3f0262cSandi } 875f3f0262cSandi } 876d437bcc4SAndreas Gohr } 877d437bcc4SAndreas Gohr }// end of while 878d437bcc4SAndreas Gohr 879d437bcc4SAndreas Gohr fclose($fh); 880f3f0262cSandi return $recent; 881f3f0262cSandi} 882f3f0262cSandi 883f3f0262cSandi/** 88451815db0SYann * Compare the logline $a to the timestamp $b 88551815db0SYann * @author Yann Hamon <yann.hamon@mandragor.org> 88651815db0SYann * @return integer 0 if the logline has timestamp $b, <0 if the timestam 88751815db0SYann * of $a is greater than $b, >0 else. 88851815db0SYann */ 88951815db0SYannfunction hasTimestamp($a, $b) 89051815db0SYann{ 89151815db0SYann if (strpos($a, $b) === 0) 89251815db0SYann return 0; 89351815db0SYann else 89451815db0SYann return strcmp ($a, $b); 89551815db0SYann} 89651815db0SYann 89751815db0SYann/** 89851815db0SYann * performs a dichotomic search on an array using 89951815db0SYann * a custom compare function 90051815db0SYann * 90151815db0SYann * @author Yann Hamon <yann.hamon@mandragor.org> 90251815db0SYann */ 90351815db0SYannfunction array_dichotomic_search($ar, $value, $compareFunc) { 90451815db0SYann $value = trim($value); 90551815db0SYann if (!$ar || !$value || !$compareFunc) return (null); 90651815db0SYann $len = count($ar); 90751815db0SYann 90851815db0SYann $l = 0; 90951815db0SYann $r = $len-1; 91051815db0SYann 91151815db0SYann do { 91251815db0SYann $i = floor(($l+$r)/2); 91351815db0SYann if ($compareFunc($ar[$i], $value)<0) 91451815db0SYann $l = $i+1; 91551815db0SYann else 91651815db0SYann $r = $i-1; 91751815db0SYann } while ($compareFunc($ar[$i], $value)!=0 && $l<=$r); 91851815db0SYann 91951815db0SYann if ($compareFunc($ar[$i], $value)==0) 92051815db0SYann return $i; 92151815db0SYann else 92251815db0SYann return -1; 92351815db0SYann} 92451815db0SYann 92551815db0SYann/** 926652610a2Sandi * gets additonal informations for a certain pagerevison 927652610a2Sandi * from the changelog 928652610a2Sandi * 929652610a2Sandi * @author Andreas Gohr <andi@splitbrain.org> 93051815db0SYann * @author Yann Hamon <yann.hamon@mandragor.org> 931652610a2Sandi */ 932652610a2Sandifunction getRevisionInfo($id,$rev){ 933652610a2Sandi global $conf; 934258641c6Sandi 935258641c6Sandi if(!$rev) return(null); 936258641c6Sandi 937c1049928Sandi $info = array(); 938c1049928Sandi if(!@is_readable($conf['changelog'])){ 939c1049928Sandi msg($conf['changelog'].' is not readable',-1); 940c1049928Sandi return $recent; 941c1049928Sandi } 942652610a2Sandi $loglines = file($conf['changelog']); 94351815db0SYann 94451815db0SYann // Search for a line with a matching timestamp 94551815db0SYann $index = array_dichotomic_search ($loglines, $rev, hasTimestamp); 94651815db0SYann if ($index == -1) 94751815db0SYann return; 94851815db0SYann 94951815db0SYann // The following code is necessary when there is more than 95051815db0SYann // one line with one same timestamp 95151815db0SYann $loglines_matching = array(); 95251815db0SYann $loglines_matching[] = $loglines[$index]; 953*2e2fda08SYann for ($i=$index-1;$i>=0 && hasTimestamp($loglines[$i], $rev) == 0; $i--) 95451815db0SYann $loglines_matching[] = $loglines[$i]; 955*2e2fda08SYann $logsize = count($loglines); 956*2e2fda08SYann for ($i=$index+1;$i<$logsize && hasTimestamp($loglines[$i], $rev) == 0; $i++) 95751815db0SYann $loglines_matching[] = $loglines[$i]; 95851815db0SYann 95951815db0SYann // Match only lines concerning the document $id 96051815db0SYann $loglines_matching = preg_grep("/$rev\t\d+\.\d+\.\d+\.\d+\t$id\t/",$loglines_matching); 96151815db0SYann 96251815db0SYann $loglines_matching = array_reverse($loglines_matching); //reverse sort on timestamp 96351815db0SYann $line = split("\t",$loglines_matching[0]); 96451815db0SYann 965652610a2Sandi $info['date'] = $line[0]; 966652610a2Sandi $info['ip'] = $line[1]; 967652610a2Sandi $info['user'] = $line[3]; 968652610a2Sandi $info['sum'] = $line[4]; 969b6912aeaSAndreas Gohr $info['minor'] = isMinor($info['sum']); 970652610a2Sandi return $info; 971652610a2Sandi} 972652610a2Sandi 97351815db0SYann 974652610a2Sandi/** 975f3f0262cSandi * Saves a wikitext by calling io_saveFile 97615fae107Sandi * 97715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 978f3f0262cSandi */ 979b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){ 980f3f0262cSandi global $conf; 981f3f0262cSandi global $lang; 982f3f0262cSandi // ignore if no changes were made 983f3f0262cSandi if($text == rawWiki($id,'')){ 984f3f0262cSandi return; 985f3f0262cSandi } 986f3f0262cSandi 987f3f0262cSandi $file = wikiFN($id); 988f3f0262cSandi $old = saveOldRevision($id); 989f3f0262cSandi 990f3f0262cSandi if (empty($text)){ 991e1f3d9e1SEsther Brunner // remove empty file 992f3f0262cSandi @unlink($file); 993e1f3d9e1SEsther Brunner // remove any meta info 994e1f3d9e1SEsther Brunner $mfiles = metaFiles($id); 995e1f3d9e1SEsther Brunner foreach ($mfiles as $mfile) { 996e1f3d9e1SEsther Brunner if (file_exists($mfile)) @unlink($mfile); 997b158d625SSteven Danz } 998f3f0262cSandi $del = true; 9993ce054b3Sandi // autoset summary on deletion 10003ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 1001f864871eSAndreas Gohr // unlock early 1002f864871eSAndreas Gohr unlock($id); 100353d6ccfeSandi // remove empty namespaces 100453d6ccfeSandi io_sweepNS($id); 1005f3f0262cSandi }else{ 1006f3f0262cSandi // save file (datadir is created in io_saveFile) 1007f3f0262cSandi io_saveFile($file,$text); 1008f3f0262cSandi $del = false; 1009f3f0262cSandi } 1010f3f0262cSandi 1011b6912aeaSAndreas Gohr addLogEntry(@filemtime($file),$id,$summary,$minor); 101226a0801fSAndreas Gohr // send notify mails 101390033e9dSAndreas Gohr notify($id,'admin',$old,$summary,$minor); 101490033e9dSAndreas Gohr notify($id,'subscribers',$old,$summary,$minor); 1015f3f0262cSandi 1016f3f0262cSandi //purge cache on add by updating the purgefile 1017f3f0262cSandi if($conf['purgeonadd'] && (!$old || $del)){ 101898407a7aSandi io_saveFile($conf['cachedir'].'/purgefile',time()); 1019f3f0262cSandi } 1020f3f0262cSandi} 1021f3f0262cSandi 1022f3f0262cSandi/** 1023f3f0262cSandi * moves the current version to the attic and returns its 1024f3f0262cSandi * revision date 102515fae107Sandi * 102615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1027f3f0262cSandi */ 1028f3f0262cSandifunction saveOldRevision($id){ 1029f3f0262cSandi global $conf; 1030f3f0262cSandi $oldf = wikiFN($id); 1031f3f0262cSandi if(!@file_exists($oldf)) return ''; 1032f3f0262cSandi $date = filemtime($oldf); 1033f3f0262cSandi $newf = wikiFN($id,$date); 1034f3f0262cSandi if(substr($newf,-3)=='.gz'){ 1035f3f0262cSandi io_saveFile($newf,rawWiki($id)); 1036f3f0262cSandi }else{ 1037f3f0262cSandi io_makeFileDir($newf); 1038f3f0262cSandi copy($oldf, $newf); 1039f3f0262cSandi } 1040f3f0262cSandi return $date; 1041f3f0262cSandi} 1042f3f0262cSandi 1043f3f0262cSandi/** 104426a0801fSAndreas Gohr * Sends a notify mail on page change 104526a0801fSAndreas Gohr * 104626a0801fSAndreas Gohr * @param string $id The changed page 104726a0801fSAndreas Gohr * @param string $who Who to notify (admin|subscribers) 104826a0801fSAndreas Gohr * @param int $rev Old page revision 104926a0801fSAndreas Gohr * @param string $summary What changed 105090033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 105115fae107Sandi * 105215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1053f3f0262cSandi */ 105490033e9dSAndreas Gohrfunction notify($id,$who,$rev='',$summary='',$minor=false){ 1055f3f0262cSandi global $lang; 1056f3f0262cSandi global $conf; 1057b158d625SSteven Danz 105826a0801fSAndreas Gohr // decide if there is something to do 105926a0801fSAndreas Gohr if($who == 'admin'){ 106026a0801fSAndreas Gohr if(empty($conf['notify'])) return; //notify enabled? 1061f3f0262cSandi $text = rawLocale('mailtext'); 106226a0801fSAndreas Gohr $to = $conf['notify']; 106326a0801fSAndreas Gohr $bcc = ''; 106426a0801fSAndreas Gohr }elseif($who == 'subscribers'){ 106526a0801fSAndreas Gohr if(!$conf['subscribers']) return; //subscribers enabled? 106690033e9dSAndreas Gohr if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors 106726a0801fSAndreas Gohr $bcc = subscriber_addresslist($id); 106826a0801fSAndreas Gohr if(empty($bcc)) return; 106926a0801fSAndreas Gohr $to = ''; 107026a0801fSAndreas Gohr $text = rawLocale('subscribermail'); 107126a0801fSAndreas Gohr }else{ 107226a0801fSAndreas Gohr return; //just to be safe 107326a0801fSAndreas Gohr } 107426a0801fSAndreas Gohr 1075f3f0262cSandi $text = str_replace('@DATE@',date($conf['dformat']),$text); 1076f3f0262cSandi $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 1077f3f0262cSandi $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text); 1078f3f0262cSandi $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text); 1079ed7b5f09Sandi $text = str_replace('@NEWPAGE@',wl($id,'',true),$text); 108026a0801fSAndreas Gohr $text = str_replace('@PAGE@',$id,$text); 108126a0801fSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 1082ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 1083f3f0262cSandi $text = str_replace('@SUMMARY@',$summary,$text); 10847a82afdcSandi $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 1085f3f0262cSandi 1086f3f0262cSandi if($rev){ 1087f3f0262cSandi $subject = $lang['mail_changed'].' '.$id; 1088ed7b5f09Sandi $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text); 1089ccdfa6c0SAndreas Gohr require_once(DOKU_INC.'inc/DifferenceEngine.php'); 1090f3f0262cSandi $df = new Diff(split("\n",rawWiki($id,$rev)), 1091f3f0262cSandi split("\n",rawWiki($id))); 1092f3f0262cSandi $dformat = new UnifiedDiffFormatter(); 1093f3f0262cSandi $diff = $dformat->format($df); 1094f3f0262cSandi }else{ 1095f3f0262cSandi $subject=$lang['mail_newpage'].' '.$id; 1096f3f0262cSandi $text = str_replace('@OLDPAGE@','none',$text); 1097f3f0262cSandi $diff = rawWiki($id); 1098f3f0262cSandi } 1099f3f0262cSandi $text = str_replace('@DIFF@',$diff,$text); 1100241f3a36Sandi $subject = '['.$conf['title'].'] '.$subject; 1101f3f0262cSandi 110226a0801fSAndreas Gohr mail_send($to,$subject,$text,$conf['mailfrom'],'',$bcc); 1103f3f0262cSandi} 1104f3f0262cSandi 110515fae107Sandi/** 110615fae107Sandi * Return a list of available page revisons 110715fae107Sandi * 110815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 110915fae107Sandi */ 1110f3f0262cSandifunction getRevisions($id){ 1111f3f0262cSandi $revd = dirname(wikiFN($id,'foo')); 1112f3f0262cSandi $revs = array(); 1113f3f0262cSandi $clid = cleanID($id); 1114f3f0262cSandi if(strrpos($clid,':')) $clid = substr($clid,strrpos($clid,':')+1); //remove path 1115493a6929SKobaYY $clid = utf8_encodeFN($clid); 1116f3f0262cSandi 1117f3f0262cSandi if (is_dir($revd) && $dh = opendir($revd)) { 1118f3f0262cSandi while (($file = readdir($dh)) !== false) { 1119f3f0262cSandi if (is_dir($revd.'/'.$file)) continue; 1120f3f0262cSandi if (preg_match('/^'.$clid.'\.(\d+)\.txt(\.gz)?$/',$file,$match)){ 1121f3f0262cSandi $revs[]=$match[1]; 1122f3f0262cSandi } 1123f3f0262cSandi } 1124f3f0262cSandi closedir($dh); 1125f3f0262cSandi } 1126f3f0262cSandi rsort($revs); 1127f3f0262cSandi return $revs; 1128f3f0262cSandi} 1129f3f0262cSandi 1130f3f0262cSandi/** 1131f3f0262cSandi * extracts the query from a google referer 113215fae107Sandi * 11336b13307fSandi * @todo should be more generic and support yahoo et al 113415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1135f3f0262cSandi */ 1136f3f0262cSandifunction getGoogleQuery(){ 1137f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 11385c3f206fSandi if(!$url) return ''; 1139f3f0262cSandi 1140f3f0262cSandi if(!preg_match("#google\.#i",$url['host'])) return ''; 1141f3f0262cSandi $query = array(); 1142f3f0262cSandi parse_str($url['query'],$query); 1143f3f0262cSandi 1144f3f0262cSandi return $query['q']; 1145f3f0262cSandi} 1146f3f0262cSandi 1147f3f0262cSandi/** 114815fae107Sandi * Try to set correct locale 114915fae107Sandi * 1150095bfd5cSandi * @deprecated No longer used 115115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1152f3f0262cSandi */ 1153f3f0262cSandifunction setCorrectLocale(){ 1154f3f0262cSandi global $conf; 1155f3f0262cSandi global $lang; 1156f3f0262cSandi 1157f3f0262cSandi $enc = strtoupper($lang['encoding']); 1158f3f0262cSandi foreach ($lang['locales'] as $loc){ 1159f3f0262cSandi //try locale 1160f3f0262cSandi if(@setlocale(LC_ALL,$loc)) return; 1161f3f0262cSandi //try loceale with encoding 1162f3f0262cSandi if(@setlocale(LC_ALL,"$loc.$enc")) return; 1163f3f0262cSandi } 1164f3f0262cSandi //still here? try to set from environment 1165f3f0262cSandi @setlocale(LC_ALL,""); 1166f3f0262cSandi} 1167f3f0262cSandi 1168f3f0262cSandi/** 1169f3f0262cSandi * Return the human readable size of a file 1170f3f0262cSandi * 1171f3f0262cSandi * @param int $size A file size 1172f3f0262cSandi * @param int $dec A number of decimal places 1173f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1174f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1175f3f0262cSandi * @version 1.0.0 1176f3f0262cSandi */ 1177f31d5b73Sandifunction filesize_h($size, $dec = 1){ 1178f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1179f3f0262cSandi $count = count($sizes); 1180f3f0262cSandi $i = 0; 1181f3f0262cSandi 1182f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 1183f3f0262cSandi $size /= 1024; 1184f3f0262cSandi $i++; 1185f3f0262cSandi } 1186f3f0262cSandi 1187f3f0262cSandi return round($size, $dec) . ' ' . $sizes[$i]; 1188f3f0262cSandi} 1189f3f0262cSandi 119015fae107Sandi/** 119100a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 119200a7b5adSEsther Brunner * 119300a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 119400a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 119500a7b5adSEsther Brunner */ 119600a7b5adSEsther Brunnerfunction obfuscate($email) { 119700a7b5adSEsther Brunner global $conf; 119800a7b5adSEsther Brunner 119900a7b5adSEsther Brunner switch ($conf['mailguard']) { 120000a7b5adSEsther Brunner case 'visible' : 120100a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 120200a7b5adSEsther Brunner return strtr($email, $obfuscate); 120300a7b5adSEsther Brunner 120400a7b5adSEsther Brunner case 'hex' : 120500a7b5adSEsther Brunner $encode = ''; 120600a7b5adSEsther Brunner for ($x=0; $x < strlen($email); $x++) $encode .= '&#x' . bin2hex($email{$x}).';'; 120700a7b5adSEsther Brunner return $encode; 120800a7b5adSEsther Brunner 120900a7b5adSEsther Brunner case 'none' : 121000a7b5adSEsther Brunner default : 121100a7b5adSEsther Brunner return $email; 121200a7b5adSEsther Brunner } 121300a7b5adSEsther Brunner} 121400a7b5adSEsther Brunner 121500a7b5adSEsther Brunner/** 1216dc57ef04Sandi * Return DokuWikis version 121715fae107Sandi * 121815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 121915fae107Sandi */ 1220f31d5b73Sandifunction getVersion(){ 1221f31d5b73Sandi //import version string 1222f31d5b73Sandi if(@file_exists('VERSION')){ 1223f31d5b73Sandi //official release 12240647ce3bSAndreas Gohr return 'Release '.trim(io_readfile(DOKU_INC.'/VERSION')); 1225f31d5b73Sandi }elseif(is_dir('_darcs')){ 1226f31d5b73Sandi //darcs checkout 1227f31d5b73Sandi $inv = file('_darcs/inventory'); 1228ae41559bSAndreas Gohr $inv = preg_grep('#\*\*\d{14}[\]$]#',$inv); 1229f31d5b73Sandi $cur = array_pop($inv); 1230f31d5b73Sandi preg_match('#\*\*(\d{4})(\d{2})(\d{2})#',$cur,$matches); 1231f31d5b73Sandi return 'Darcs '.$matches[1].'-'.$matches[2].'-'.$matches[3]; 1232f31d5b73Sandi }else{ 1233f31d5b73Sandi return 'snapshot?'; 1234f31d5b73Sandi } 1235f31d5b73Sandi} 1236f31d5b73Sandi 1237f31d5b73Sandi/** 1238f31d5b73Sandi * Run a few sanity checks 1239f31d5b73Sandi * 1240f31d5b73Sandi * @author Andreas Gohr <andi@splitbrain.org> 1241f31d5b73Sandi */ 1242f3f0262cSandifunction check(){ 1243f3f0262cSandi global $conf; 1244f3f0262cSandi global $INFO; 1245f3f0262cSandi 1246f31d5b73Sandi msg('DokuWiki version: '.getVersion(),1); 1247f31d5b73Sandi 124849022a38Sandi if(version_compare(phpversion(),'4.3.0','<')){ 124949022a38Sandi msg('Your PHP version is too old ('.phpversion().' vs. 4.3.+ recommended)',-1); 125049022a38Sandi }elseif(version_compare(phpversion(),'4.3.10','<')){ 125149022a38Sandi msg('Consider upgrading PHP to 4.3.10 or higher for security reasons (your version: '.phpversion().')',0); 125249022a38Sandi }else{ 125349022a38Sandi msg('PHP version '.phpversion(),1); 125449022a38Sandi } 125549022a38Sandi 1256f3f0262cSandi if(is_writable($conf['changelog'])){ 1257f3f0262cSandi msg('Changelog is writable',1); 1258f3f0262cSandi }else{ 1259f3f0262cSandi msg('Changelog is not writable',-1); 1260f3f0262cSandi } 1261f3f0262cSandi 1262f3f0262cSandi if(is_writable($conf['datadir'])){ 1263f3f0262cSandi msg('Datadir is writable',1); 1264f3f0262cSandi }else{ 1265f3f0262cSandi msg('Datadir is not writable',-1); 1266f3f0262cSandi } 1267f3f0262cSandi 1268f3f0262cSandi if(is_writable($conf['olddir'])){ 1269f3f0262cSandi msg('Attic is writable',1); 1270f3f0262cSandi }else{ 1271f3f0262cSandi msg('Attic is not writable',-1); 1272f3f0262cSandi } 1273f3f0262cSandi 1274f3f0262cSandi if(is_writable($conf['mediadir'])){ 1275f3f0262cSandi msg('Mediadir is writable',1); 1276f3f0262cSandi }else{ 1277f3f0262cSandi msg('Mediadir is not writable',-1); 1278f3f0262cSandi } 1279f3f0262cSandi 128098407a7aSandi if(is_writable($conf['cachedir'])){ 128198407a7aSandi msg('Cachedir is writable',1); 128298407a7aSandi }else{ 128398407a7aSandi msg('Cachedir is not writable',-1); 128498407a7aSandi } 128598407a7aSandi 1286e7cb32dcSAndreas Gohr if(is_writable(DOKU_CONF.'users.auth.php')){ 12878c4f28e8Sjan msg('conf/users.auth.php is writable',1); 1288f3f0262cSandi }else{ 12898c4f28e8Sjan msg('conf/users.auth.php is not writable',0); 1290f3f0262cSandi } 129193a9e835Sandi 129293a9e835Sandi if(function_exists('mb_strpos')){ 129393a9e835Sandi if(defined('UTF8_NOMBSTRING')){ 129493a9e835Sandi msg('mb_string extension is available but will not be used',0); 129593a9e835Sandi }else{ 129693a9e835Sandi msg('mb_string extension is available and will be used',1); 129793a9e835Sandi } 129893a9e835Sandi }else{ 129993a9e835Sandi msg('mb_string extension not available - PHP only replacements will be used',0); 130093a9e835Sandi } 1301f42d1c75SAndreas Gohr 1302f42d1c75SAndreas Gohr if($conf['allowdebug']){ 1303f42d1c75SAndreas Gohr msg('Debugging support is enabled. If you don\'t need it you should set $conf[\'allowdebug\'] = 0',-1); 1304f42d1c75SAndreas Gohr }else{ 1305f42d1c75SAndreas Gohr msg('Debugging support is disabled',1); 1306f42d1c75SAndreas Gohr } 1307f3f0262cSandi 1308f3f0262cSandi msg('Your current permission for this page is '.$INFO['perm'],0); 1309f3f0262cSandi 1310f3f0262cSandi if(is_writable($INFO['filepath'])){ 1311f3f0262cSandi msg('The current page is writable by the webserver',0); 1312f3f0262cSandi }else{ 1313f3f0262cSandi msg('The current page is not writable by the webserver',0); 1314f3f0262cSandi } 1315f3f0262cSandi 1316f3f0262cSandi if($INFO['writable']){ 1317f3f0262cSandi msg('The current page is writable by you',0); 1318f3f0262cSandi }else{ 1319f3f0262cSandi msg('The current page is not writable you',0); 1320f3f0262cSandi } 1321f3f0262cSandi} 1322340756e4Sandi 1323b158d625SSteven Danz/** 1324b158d625SSteven Danz * Let us know if a user is tracking a page 1325b158d625SSteven Danz * 13261380fc45SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1327b158d625SSteven Danz */ 13281380fc45SAndreas Gohrfunction is_subscribed($id,$uid){ 13291380fc45SAndreas Gohr $file=metaFN($id,'.mlist'); 13301380fc45SAndreas Gohr if (@file_exists($file)) { 1331b158d625SSteven Danz $mlist = file($file); 13321380fc45SAndreas Gohr $pos = array_search($uid."\n",$mlist); 13331380fc45SAndreas Gohr return is_int($pos); 1334b158d625SSteven Danz } 13351380fc45SAndreas Gohr 1336b158d625SSteven Danz return false; 1337b158d625SSteven Danz} 1338340756e4Sandi 1339f9eb5648Ssteven-danz/** 1340f9eb5648Ssteven-danz * Return a string with the email addresses of all the 1341f9eb5648Ssteven-danz * users subscribed to a page 1342f9eb5648Ssteven-danz * 134326a0801fSAndreas Gohr * @author Steven Danz <steven-danz@kc.rr.com> 1344f9eb5648Ssteven-danz */ 1345f9eb5648Ssteven-danzfunction subscriber_addresslist($id){ 1346f9eb5648Ssteven-danz global $conf; 1347cd52f92dSchris global $auth; 1348f9eb5648Ssteven-danz 1349f9eb5648Ssteven-danz $emails = ''; 1350f9eb5648Ssteven-danz 135126a0801fSAndreas Gohr if (!$conf['subscribers']) return; 135226a0801fSAndreas Gohr 1353f9eb5648Ssteven-danz $mlist = array(); 1354f9eb5648Ssteven-danz $file=metaFN($id,'.mlist'); 1355f9eb5648Ssteven-danz if (file_exists($file)) { 1356f9eb5648Ssteven-danz $mlist = file($file); 1357f9eb5648Ssteven-danz } 1358f9eb5648Ssteven-danz if(count($mlist) > 0) { 1359f9eb5648Ssteven-danz foreach ($mlist as $who) { 1360f9eb5648Ssteven-danz $who = rtrim($who); 1361cd52f92dSchris $info = $auth->getUserData($who); 1362f9eb5648Ssteven-danz $level = auth_aclcheck($id,$who,$info['grps']); 1363f9eb5648Ssteven-danz if ($level >= AUTH_READ) { 1364f9eb5648Ssteven-danz if (strcasecmp($info['mail'],$conf['notify']) != 0) { 1365f9eb5648Ssteven-danz if (empty($emails)) { 1366f9eb5648Ssteven-danz $emails = $info['mail']; 1367f9eb5648Ssteven-danz } else { 1368f9eb5648Ssteven-danz $emails = "$emails,".$info['mail']; 1369f9eb5648Ssteven-danz } 1370f9eb5648Ssteven-danz } 1371f9eb5648Ssteven-danz } 1372f9eb5648Ssteven-danz } 1373f9eb5648Ssteven-danz } 1374f9eb5648Ssteven-danz 1375f9eb5648Ssteven-danz return $emails; 1376f9eb5648Ssteven-danz} 1377f9eb5648Ssteven-danz 137889541d4bSAndreas Gohr/** 137989541d4bSAndreas Gohr * Removes quoting backslashes 138089541d4bSAndreas Gohr * 138189541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 138289541d4bSAndreas Gohr */ 138389541d4bSAndreas Gohrfunction unslash($string,$char="'"){ 138489541d4bSAndreas Gohr return str_replace('\\'.$char,$char,$string); 138589541d4bSAndreas Gohr} 138689541d4bSAndreas Gohr 1387340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 1388