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 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10ed7b5f09Sandirequire_once(DOKU_INC.'inc/io.php'); 117d559c7fSBen Coburnrequire_once(DOKU_INC.'inc/changelog.php'); 12ed7b5f09Sandirequire_once(DOKU_INC.'inc/utf8.php'); 13ed7b5f09Sandirequire_once(DOKU_INC.'inc/mail.php'); 14c112d578Sandirequire_once(DOKU_INC.'inc/parserutils.php'); 15c29dc6e4SAndreas Gohrrequire_once(DOKU_INC.'inc/infoutils.php'); 165b75cd1fSAdrian Langrequire_once DOKU_INC.'inc/subscription.php'; 17f3f0262cSandi 18f3f0262cSandi/** 19b6912aeaSAndreas Gohr * These constants are used with the recents function 20b6912aeaSAndreas Gohr */ 21b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED',2); 22b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS',4); 23b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES',8); 2499c8d7f2Smichaeldefine('RECENTS_MEDIA_CHANGES',16); 25b6912aeaSAndreas Gohr 26b6912aeaSAndreas Gohr/** 27d5197206Schris * Wrapper around htmlspecialchars() 28d5197206Schris * 29d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 30d5197206Schris * @see htmlspecialchars() 31d5197206Schris */ 32d5197206Schrisfunction hsc($string){ 33d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 34d5197206Schris} 35d5197206Schris 36d5197206Schris/** 37d5197206Schris * print a newline terminated string 38d5197206Schris * 39d5197206Schris * You can give an indention as optional parameter 40d5197206Schris * 41d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 42d5197206Schris */ 4325ec097bSChris Smithfunction ptln($string,$indent=0){ 4425ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 4502b0b681SAndreas Gohr} 4602b0b681SAndreas Gohr 4702b0b681SAndreas Gohr/** 4802b0b681SAndreas Gohr * strips control characters (<32) from the given string 4902b0b681SAndreas Gohr * 5002b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 5102b0b681SAndreas Gohr */ 5202b0b681SAndreas Gohrfunction stripctl($string){ 5302b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s','',$string); 54d5197206Schris} 55d5197206Schris 56d5197206Schris/** 57634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 58634d7150SAndreas Gohr * 59634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 60634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 61634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 62634d7150SAndreas Gohr * @return string 63634d7150SAndreas Gohr */ 64634d7150SAndreas Gohrfunction getSecurityToken(){ 65634d7150SAndreas Gohr return md5(auth_cookiesalt().session_id()); 66634d7150SAndreas Gohr} 67634d7150SAndreas Gohr 68634d7150SAndreas Gohr/** 69634d7150SAndreas Gohr * Check the secret CSRF token 70634d7150SAndreas Gohr */ 71634d7150SAndreas Gohrfunction checkSecurityToken($token=null){ 72df97eaacSAndreas Gohr if(!$_SERVER['REMOTE_USER']) return true; // no logged in user, no need for a check 73df97eaacSAndreas Gohr 74634d7150SAndreas Gohr if(is_null($token)) $token = $_REQUEST['sectok']; 75634d7150SAndreas Gohr if(getSecurityToken() != $token){ 76634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.',-1); 77634d7150SAndreas Gohr return false; 78634d7150SAndreas Gohr } 79634d7150SAndreas Gohr return true; 80634d7150SAndreas Gohr} 81634d7150SAndreas Gohr 82634d7150SAndreas Gohr/** 83634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 84634d7150SAndreas Gohr * 85634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 86634d7150SAndreas Gohr */ 87634d7150SAndreas Gohrfunction formSecurityToken($print=true){ 882404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 89634d7150SAndreas Gohr if($print){ 90634d7150SAndreas Gohr echo $ret; 91634d7150SAndreas Gohr }else{ 92634d7150SAndreas Gohr return $ret; 93634d7150SAndreas Gohr } 94634d7150SAndreas Gohr} 95634d7150SAndreas Gohr 96634d7150SAndreas Gohr/** 9715fae107Sandi * Return info about the current document as associative 98f3f0262cSandi * array. 9915fae107Sandi * 10015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 101f3f0262cSandi */ 102f3f0262cSandifunction pageinfo(){ 103f3f0262cSandi global $ID; 104f3f0262cSandi global $REV; 1057b3a6803SAndreas Gohr global $RANGE; 106f3f0262cSandi global $USERINFO; 107f3f0262cSandi global $conf; 1087b3a6803SAndreas Gohr global $lang; 109f3f0262cSandi 1106afe8dcaSchris // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 1116afe8dcaSchris // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 1126afe8dcaSchris $info['id'] = $ID; 1136afe8dcaSchris $info['rev'] = $REV; 1146afe8dcaSchris 115c66972f2SAdrian Lang // set info about manager/admin status. 116c66972f2SAdrian Lang $info['isadmin'] = false; 117c66972f2SAdrian Lang $info['ismanager'] = false; 118c66972f2SAdrian Lang if(isset($_SERVER['REMOTE_USER'])){ 119f3f0262cSandi $info['userinfo'] = $USERINFO; 120f3f0262cSandi $info['perm'] = auth_quickaclcheck($ID); 1215b75cd1fSAdrian Lang $info['subscribed'] = get_info_subscribed(); 122ee4c4a1bSAndreas Gohr $info['client'] = $_SERVER['REMOTE_USER']; 12317ee7f66SAndreas Gohr 124f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN){ 125f8cc712eSAndreas Gohr $info['isadmin'] = true; 126f8cc712eSAndreas Gohr $info['ismanager'] = true; 127f8cc712eSAndreas Gohr }elseif(auth_ismanager()){ 128f8cc712eSAndreas Gohr $info['ismanager'] = true; 129f8cc712eSAndreas Gohr } 130f8cc712eSAndreas Gohr 13117ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 13217ee7f66SAndreas Gohr if(!$info['userinfo']['name']){ 13317ee7f66SAndreas Gohr $info['userinfo']['name'] = $_SERVER['REMOTE_USER']; 13417ee7f66SAndreas Gohr } 135ee4c4a1bSAndreas Gohr 136f3f0262cSandi }else{ 137f3f0262cSandi $info['perm'] = auth_aclcheck($ID,'',null); 1381380fc45SAndreas Gohr $info['subscribed'] = false; 139ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 140f3f0262cSandi } 141f3f0262cSandi 142f3f0262cSandi $info['namespace'] = getNS($ID); 143f3f0262cSandi $info['locked'] = checklock($ID); 14400976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID)); 1452ca9d91cSBen Coburn $info['exists'] = @file_exists($info['filepath']); 1462ca9d91cSBen Coburn if($REV){ 1472ca9d91cSBen Coburn //check if current revision was meant 1482ca9d91cSBen Coburn if($info['exists'] && (@filemtime($info['filepath'])==$REV)){ 1492ca9d91cSBen Coburn $REV = ''; 1507b3a6803SAndreas Gohr }elseif($RANGE){ 1517b3a6803SAndreas Gohr //section editing does not work with old revisions! 1527b3a6803SAndreas Gohr $REV = ''; 1537b3a6803SAndreas Gohr $RANGE = ''; 1547b3a6803SAndreas Gohr msg($lang['nosecedit'],0); 1552ca9d91cSBen Coburn }else{ 1562ca9d91cSBen Coburn //really use old revision 15700976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID,$REV)); 158f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 159f3f0262cSandi } 160f3f0262cSandi } 161c112d578Sandi $info['rev'] = $REV; 162f3f0262cSandi if($info['exists']){ 163f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 164f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 165f3f0262cSandi }else{ 166f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 167f3f0262cSandi } 168f3f0262cSandi $info['editable'] = ($info['writable'] && empty($info['lock'])); 169f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 170f3f0262cSandi 17171726d78SBen Coburn //load page meta data 17271726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 17371726d78SBen Coburn 174652610a2Sandi //who's the editor 175652610a2Sandi if($REV){ 17671726d78SBen Coburn $revinfo = getRevisionInfo($ID, $REV, 1024); 177652610a2Sandi }else{ 178aa27cf05SAndreas Gohr if (is_array($info['meta']['last_change'])) { 179aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 180aa27cf05SAndreas Gohr } else { 181cd00a034SBen Coburn $revinfo = getRevisionInfo($ID, $info['lastmod'], 1024); 182cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 183cd00a034SBen Coburn if ($revinfo!==false) { 184cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 185cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 186cd00a034SBen Coburn } 187cd00a034SBen Coburn } 188cd00a034SBen Coburn } 189cd00a034SBen Coburn //and check for an external edit 190cd00a034SBen Coburn if($revinfo!==false && $revinfo['date']!=$info['lastmod']){ 191cd00a034SBen Coburn // cached changelog line no longer valid 192cd00a034SBen Coburn $revinfo = false; 193cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 194cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 195652610a2Sandi } 196bb4866bdSchris 197652610a2Sandi $info['ip'] = $revinfo['ip']; 198652610a2Sandi $info['user'] = $revinfo['user']; 199652610a2Sandi $info['sum'] = $revinfo['sum']; 20071726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 201ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 20259f257aeSchris 20388f522e9Sandi if($revinfo['user']){ 20488f522e9Sandi $info['editor'] = $revinfo['user']; 20588f522e9Sandi }else{ 20688f522e9Sandi $info['editor'] = $revinfo['ip']; 20788f522e9Sandi } 208652610a2Sandi 209ee4c4a1bSAndreas Gohr // draft 210ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID,'.draft'); 211ee4c4a1bSAndreas Gohr if(@file_exists($draft)){ 212ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))){ 213ee4c4a1bSAndreas Gohr // remove stale draft 214ee4c4a1bSAndreas Gohr @unlink($draft); 215ee4c4a1bSAndreas Gohr }else{ 216ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 217ee4c4a1bSAndreas Gohr } 218ee4c4a1bSAndreas Gohr } 219ee4c4a1bSAndreas Gohr 2201c548ebeSAndreas Gohr // mobile detection 2211c548ebeSAndreas Gohr $info['ismobile'] = clientismobile(); 2221c548ebeSAndreas Gohr 223f3f0262cSandi return $info; 224f3f0262cSandi} 225f3f0262cSandi 226f3f0262cSandi/** 2272684e50aSAndreas Gohr * Build an string of URL parameters 2282684e50aSAndreas Gohr * 2292684e50aSAndreas Gohr * @author Andreas Gohr 2302684e50aSAndreas Gohr */ 231b174aeaeSchrisfunction buildURLparams($params, $sep='&'){ 2322684e50aSAndreas Gohr $url = ''; 2332684e50aSAndreas Gohr $amp = false; 2342684e50aSAndreas Gohr foreach($params as $key => $val){ 235b174aeaeSchris if($amp) $url .= $sep; 2362684e50aSAndreas Gohr 2372684e50aSAndreas Gohr $url .= $key.'='; 2383a50618cSgweissbach $url .= rawurlencode((string)$val); 2392684e50aSAndreas Gohr $amp = true; 2402684e50aSAndreas Gohr } 2412684e50aSAndreas Gohr return $url; 2422684e50aSAndreas Gohr} 2432684e50aSAndreas Gohr 2442684e50aSAndreas Gohr/** 2452684e50aSAndreas Gohr * Build an string of html tag attributes 2462684e50aSAndreas Gohr * 2477bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 2487bff22c0SAndreas Gohr * 2492684e50aSAndreas Gohr * @author Andreas Gohr 2502684e50aSAndreas Gohr */ 2514b030ce7SAndreas Gohrfunction buildAttributes($params,$skipempty=false){ 2522684e50aSAndreas Gohr $url = ''; 2532684e50aSAndreas Gohr foreach($params as $key => $val){ 2547bff22c0SAndreas Gohr if($key{0} == '_') continue; 255b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 2567bff22c0SAndreas Gohr 2572684e50aSAndreas Gohr $url .= $key.'="'; 2582684e50aSAndreas Gohr $url .= htmlspecialchars ($val); 2592684e50aSAndreas Gohr $url .= '" '; 2602684e50aSAndreas Gohr } 2612684e50aSAndreas Gohr return $url; 2622684e50aSAndreas Gohr} 2632684e50aSAndreas Gohr 2642684e50aSAndreas Gohr 2652684e50aSAndreas Gohr/** 26615fae107Sandi * This builds the breadcrumb trail and returns it as array 26715fae107Sandi * 26815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 269f3f0262cSandi */ 270f3f0262cSandifunction breadcrumbs(){ 2718746e727Sandi // we prepare the breadcrumbs early for quick session closing 2728746e727Sandi static $crumbs = null; 2738746e727Sandi if($crumbs != null) return $crumbs; 2748746e727Sandi 275f3f0262cSandi global $ID; 276f3f0262cSandi global $ACT; 277f3f0262cSandi global $conf; 278f3f0262cSandi 279f3f0262cSandi //first visit? 280c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 281f3f0262cSandi //we only save on show and existing wiki documents 282a77f5846Sjan $file = wikiFN($ID); 283a77f5846Sjan if($ACT != 'show' || !@file_exists($file)){ 284e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 285f3f0262cSandi return $crumbs; 286f3f0262cSandi } 287a77f5846Sjan 288a77f5846Sjan // page names 2891a84a0f3SAnika Henke $name = noNSorNS($ID); 290fe9ec250SChris Smith if (useHeading('navigation')) { 291a77f5846Sjan // get page title 292955cd091SChris Smith $title = p_get_first_heading($ID,true); 293a77f5846Sjan if ($title) { 294a77f5846Sjan $name = $title; 295a77f5846Sjan } 296a77f5846Sjan } 297a77f5846Sjan 298f3f0262cSandi //remove ID from array 299a77f5846Sjan if (isset($crumbs[$ID])) { 300a77f5846Sjan unset($crumbs[$ID]); 301f3f0262cSandi } 302f3f0262cSandi 303f3f0262cSandi //add to array 304a77f5846Sjan $crumbs[$ID] = $name; 305f3f0262cSandi //reduce size 306f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']){ 307f3f0262cSandi array_shift($crumbs); 308f3f0262cSandi } 309f3f0262cSandi //save to session 310e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 311f3f0262cSandi return $crumbs; 312f3f0262cSandi} 313f3f0262cSandi 314f3f0262cSandi/** 31515fae107Sandi * Filter for page IDs 31615fae107Sandi * 317f3f0262cSandi * This is run on a ID before it is outputted somewhere 318f3f0262cSandi * currently used to replace the colon with something else 319f3f0262cSandi * on Windows systems and to have proper URL encoding 32015fae107Sandi * 32149c713a3Sandi * Urlencoding is ommitted when the second parameter is false 32249c713a3Sandi * 32315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 324f3f0262cSandi */ 32549c713a3Sandifunction idfilter($id,$ue=true){ 326f3f0262cSandi global $conf; 327f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']){ 328f3f0262cSandi $id = strtr($id,':','/'); 329f3f0262cSandi }elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 330f3f0262cSandi $conf['userewrite']) { 331f3f0262cSandi $id = strtr($id,':',';'); 332f3f0262cSandi } 33349c713a3Sandi if($ue){ 334b6c6979fSAndreas Gohr $id = rawurlencode($id); 335f3f0262cSandi $id = str_replace('%3A',':',$id); //keep as colon 336f3f0262cSandi $id = str_replace('%2F','/',$id); //keep as slash 33749c713a3Sandi } 338f3f0262cSandi return $id; 339f3f0262cSandi} 340f3f0262cSandi 341f3f0262cSandi/** 342ed7b5f09Sandi * This builds a link to a wikipage 34315fae107Sandi * 3446c7843b5Sandi * It handles URL rewriting and adds additional parameter if 3456c7843b5Sandi * given in $more 3466c7843b5Sandi * 34715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 348f3f0262cSandi */ 349b174aeaeSchrisfunction wl($id='',$more='',$abs=false,$sep='&'){ 350f3f0262cSandi global $conf; 3516de3759aSAndreas Gohr if(is_array($more)){ 352b174aeaeSchris $more = buildURLparams($more,$sep); 3536de3759aSAndreas Gohr }else{ 354b174aeaeSchris $more = str_replace(',',$sep,$more); 3556de3759aSAndreas Gohr } 356f3f0262cSandi 357f3f0262cSandi $id = idfilter($id); 358ed7b5f09Sandi if($abs){ 359ed7b5f09Sandi $xlink = DOKU_URL; 360ed7b5f09Sandi }else{ 361ed7b5f09Sandi $xlink = DOKU_BASE; 362ed7b5f09Sandi } 363f3f0262cSandi 3646c7843b5Sandi if($conf['userewrite'] == 2){ 3656c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 3666c7843b5Sandi if($more) $xlink .= '?'.$more; 3676c7843b5Sandi }elseif($conf['userewrite']){ 368f3f0262cSandi $xlink .= $id; 369f3f0262cSandi if($more) $xlink .= '?'.$more; 370bce3726dSAndreas Gohr }elseif($id){ 3716c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 372b174aeaeSchris if($more) $xlink .= $sep.$more; 373bce3726dSAndreas Gohr }else{ 374bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 375bce3726dSAndreas Gohr if($more) $xlink .= '?'.$more; 376f3f0262cSandi } 377f3f0262cSandi 378f3f0262cSandi return $xlink; 379f3f0262cSandi} 380f3f0262cSandi 381f3f0262cSandi/** 382f5c2808fSBen Coburn * This builds a link to an alternate page format 383f5c2808fSBen Coburn * 384f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 385f5c2808fSBen Coburn * 386f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 387f5c2808fSBen Coburn */ 388f5c2808fSBen Coburnfunction exportlink($id='',$format='raw',$more='',$abs=false,$sep='&'){ 389f5c2808fSBen Coburn global $conf; 390f5c2808fSBen Coburn if(is_array($more)){ 391f5c2808fSBen Coburn $more = buildURLparams($more,$sep); 392f5c2808fSBen Coburn }else{ 393f5c2808fSBen Coburn $more = str_replace(',',$sep,$more); 394f5c2808fSBen Coburn } 395f5c2808fSBen Coburn 396f5c2808fSBen Coburn $format = rawurlencode($format); 397f5c2808fSBen Coburn $id = idfilter($id); 398f5c2808fSBen Coburn if($abs){ 399f5c2808fSBen Coburn $xlink = DOKU_URL; 400f5c2808fSBen Coburn }else{ 401f5c2808fSBen Coburn $xlink = DOKU_BASE; 402f5c2808fSBen Coburn } 403f5c2808fSBen Coburn 404f5c2808fSBen Coburn if($conf['userewrite'] == 2){ 405f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 406f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 407f5c2808fSBen Coburn }elseif($conf['userewrite'] == 1){ 408f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 409f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 410f5c2808fSBen Coburn }else{ 411f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 412f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 413f5c2808fSBen Coburn } 414f5c2808fSBen Coburn 415f5c2808fSBen Coburn return $xlink; 416f5c2808fSBen Coburn} 417f5c2808fSBen Coburn 418f5c2808fSBen Coburn/** 4196de3759aSAndreas Gohr * Build a link to a media file 4206de3759aSAndreas Gohr * 4216de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 4228c08db0aSAndreas Gohr * 4238c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 4248c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 4258c08db0aSAndreas Gohr * 4268c08db0aSAndreas Gohr * @param string $id - the media file id or URL 4278c08db0aSAndreas Gohr * @param mixed $more - string or array with additional parameters 4288c08db0aSAndreas Gohr * @param boolean $direct - link to detail page if false 4298c08db0aSAndreas Gohr * @param string $sep - URL parameter separator 4308c08db0aSAndreas Gohr * @param boolean $abs - Create an absolute URL 4316de3759aSAndreas Gohr */ 43255b2b31bSAndreas Gohrfunction ml($id='',$more='',$direct=true,$sep='&',$abs=false){ 4336de3759aSAndreas Gohr global $conf; 4346de3759aSAndreas Gohr if(is_array($more)){ 4358c08db0aSAndreas Gohr // strip defaults for shorter URLs 4368c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 4378c08db0aSAndreas Gohr if(!$more['w']) unset($more['w']); 4388c08db0aSAndreas Gohr if(!$more['h']) unset($more['h']); 4398c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 440b174aeaeSchris $more = buildURLparams($more,$sep); 4416de3759aSAndreas Gohr }else{ 4428c08db0aSAndreas Gohr $more = str_replace('cache=cache','',$more); //skip default 4438c08db0aSAndreas Gohr $more = str_replace(',,',',',$more); 444b174aeaeSchris $more = str_replace(',',$sep,$more); 4456de3759aSAndreas Gohr } 4466de3759aSAndreas Gohr 44755b2b31bSAndreas Gohr if($abs){ 44855b2b31bSAndreas Gohr $xlink = DOKU_URL; 44955b2b31bSAndreas Gohr }else{ 4506de3759aSAndreas Gohr $xlink = DOKU_BASE; 45155b2b31bSAndreas Gohr } 4526de3759aSAndreas Gohr 4536de3759aSAndreas Gohr // external URLs are always direct without rewriting 4546de3759aSAndreas Gohr if(preg_match('#^(https?|ftp)://#i',$id)){ 4556de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 45669d17d94SAndreas Gohr // add hash: 45769d17d94SAndreas Gohr $xlink .= '?hash='.substr(md5(auth_cookiesalt().$id),0,6); 4586de3759aSAndreas Gohr if($more){ 45969d17d94SAndreas Gohr $xlink .= $sep.$more; 460b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 4616de3759aSAndreas Gohr }else{ 46269d17d94SAndreas Gohr $xlink .= $sep.'media='.rawurlencode($id); 4636de3759aSAndreas Gohr } 4646de3759aSAndreas Gohr return $xlink; 4656de3759aSAndreas Gohr } 4666de3759aSAndreas Gohr 4676de3759aSAndreas Gohr $id = idfilter($id); 4686de3759aSAndreas Gohr 4696de3759aSAndreas Gohr // decide on scriptname 4706de3759aSAndreas Gohr if($direct){ 4716de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 4726de3759aSAndreas Gohr $script = '_media'; 4736de3759aSAndreas Gohr }else{ 4746de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 4756de3759aSAndreas Gohr } 4766de3759aSAndreas Gohr }else{ 4776de3759aSAndreas Gohr if($conf['userewrite'] == 1){ 4786de3759aSAndreas Gohr $script = '_detail'; 4796de3759aSAndreas Gohr }else{ 4806de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 4816de3759aSAndreas Gohr } 4826de3759aSAndreas Gohr } 4836de3759aSAndreas Gohr 4846de3759aSAndreas Gohr // build URL based on rewrite mode 4856de3759aSAndreas Gohr if($conf['userewrite']){ 4866de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 4876de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 4886de3759aSAndreas Gohr }else{ 4896de3759aSAndreas Gohr if($more){ 490a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 491b174aeaeSchris $xlink .= $sep.'media='.$id; 4926de3759aSAndreas Gohr }else{ 493a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 4946de3759aSAndreas Gohr } 4956de3759aSAndreas Gohr } 4966de3759aSAndreas Gohr 4976de3759aSAndreas Gohr return $xlink; 4986de3759aSAndreas Gohr} 4996de3759aSAndreas Gohr 5006de3759aSAndreas Gohr 5016de3759aSAndreas Gohr 5026de3759aSAndreas Gohr/** 503f3f0262cSandi * Just builds a link to a script 50415fae107Sandi * 505ed7b5f09Sandi * @todo maybe obsolete 50615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 507f3f0262cSandi */ 508f3f0262cSandifunction script($script='doku.php'){ 509ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 510f3f0262cSandi} 511f3f0262cSandi 512f3f0262cSandi/** 51315fae107Sandi * Spamcheck against wordlist 51415fae107Sandi * 515f3f0262cSandi * Checks the wikitext against a list of blocked expressions 516f3f0262cSandi * returns true if the text contains any bad words 51715fae107Sandi * 518e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 519e403cc58SMichael Klier * 520e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 521e403cc58SMichael Klier * and gain information about the user who was blocked. 522e403cc58SMichael Klier * 523e403cc58SMichael Klier * Event data: 524e403cc58SMichael Klier * data['matches'] - array of matches 525e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 526e403cc58SMichael Klier * [ip] - ip address 527e403cc58SMichael Klier * [user] - username (if logged in) 528e403cc58SMichael Klier * [mail] - mail address (if logged in) 529e403cc58SMichael Klier * [name] - real name (if logged in) 530e403cc58SMichael Klier * 53115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5326dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 5336dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 5346dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 535f3f0262cSandi */ 5366dffa0e0SAndreas Gohrfunction checkwordblock($text=''){ 537f3f0262cSandi global $TEXT; 5386dffa0e0SAndreas Gohr global $PRE; 5396dffa0e0SAndreas Gohr global $SUF; 540f3f0262cSandi global $conf; 541e403cc58SMichael Klier global $INFO; 542f3f0262cSandi 543f3f0262cSandi if(!$conf['usewordblock']) return false; 544f3f0262cSandi 5456dffa0e0SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF"; 5466dffa0e0SAndreas Gohr 547041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 5486dffa0e0SAndreas Gohr $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i','\1http://\2 \2\3',$text); 549041d1964SAndreas Gohr 550b9ac8716Schris $wordblocks = getWordblocks(); 5513e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 5523e2965d7Sandi if(version_compare(phpversion(),'4.3.0','<')){ 5533e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 5543e2965d7Sandi // backreferences are used - the maximum is 99 5553e2965d7Sandi // this is very bad performancewise and may even be too high still 5563e2965d7Sandi $chunksize = 40; 5573e2965d7Sandi }else{ 558a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 5593e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 560a51d08efSAndreas Gohr $chunksize = 200; 5613e2965d7Sandi } 562b9ac8716Schris while($blocks = array_splice($wordblocks,0,$chunksize)){ 563f3f0262cSandi $re = array(); 56449eb6e38SAndreas Gohr // build regexp from blocks 565f3f0262cSandi foreach($blocks as $block){ 566f3f0262cSandi $block = preg_replace('/#.*$/','',$block); 567f3f0262cSandi $block = trim($block); 568f3f0262cSandi if(empty($block)) continue; 569f3f0262cSandi $re[] = $block; 570f3f0262cSandi } 571e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|',$re).')#si',$text,$matches)) { 572e403cc58SMichael Klier // prepare event data 573e403cc58SMichael Klier $data['matches'] = $matches; 574e403cc58SMichael Klier $data['userinfo']['ip'] = $_SERVER['REMOTE_ADDR']; 575e403cc58SMichael Klier if($_SERVER['REMOTE_USER']) { 576e403cc58SMichael Klier $data['userinfo']['user'] = $_SERVER['REMOTE_USER']; 577e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 578e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 579e403cc58SMichael Klier } 580e403cc58SMichael Klier $callback = create_function('', 'return true;'); 581e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 582b9ac8716Schris } 583703f6fdeSandi } 584f3f0262cSandi return false; 585f3f0262cSandi} 586f3f0262cSandi 587f3f0262cSandi/** 58815fae107Sandi * Return the IP of the client 58915fae107Sandi * 5906d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 59115fae107Sandi * 5926d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 5936d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 5946d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 5956d8affe6SAndreas Gohr * headers 5966d8affe6SAndreas Gohr * 5976d8affe6SAndreas Gohr * @param boolean $single If set only a single IP is returned 59815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 599f3f0262cSandi */ 6006d8affe6SAndreas Gohrfunction clientIP($single=false){ 6016d8affe6SAndreas Gohr $ip = array(); 6026d8affe6SAndreas Gohr $ip[] = $_SERVER['REMOTE_ADDR']; 603bb4866bdSchris if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 6046d8affe6SAndreas Gohr $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])); 605bb4866bdSchris if(!empty($_SERVER['HTTP_X_REAL_IP'])) 6066d8affe6SAndreas Gohr $ip = array_merge($ip,explode(',',$_SERVER['HTTP_X_REAL_IP'])); 6076d8affe6SAndreas Gohr 608dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 609dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 610dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 611dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 612dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 613dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 614dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 615dc14c6d1SGuy Brand $IPv6Address = 616dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 617dc14c6d1SGuy Brand "(?:$h16:){6}$ls32" . 618dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32" . 619dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32" . 620dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32" . 621dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32" . 622dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32" . 623dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32" . 624dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16" . 625dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::" . 626dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 627dc14c6d1SGuy Brand 6286d8affe6SAndreas Gohr // remove any non-IP stuff 6296d8affe6SAndreas Gohr $cnt = count($ip); 6304ff28443Schris $match = array(); 6316d8affe6SAndreas Gohr for($i=0; $i<$cnt; $i++){ 632dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/",$ip[$i],$match) || preg_match("/^$IPv6Address$/",$ip[$i],$match)) { 6334ff28443Schris $ip[$i] = $match[0]; 6344ff28443Schris } else { 6354ff28443Schris $ip[$i] = ''; 6364ff28443Schris } 6376d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 638f3f0262cSandi } 6396d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 6406d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 6416d8affe6SAndreas Gohr 6426d8affe6SAndreas Gohr if(!$single) return join(',',$ip); 6436d8affe6SAndreas Gohr 6446d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 6456d8affe6SAndreas Gohr $ip = array_reverse($ip); 6466d8affe6SAndreas Gohr foreach($ip as $i){ 6476d8affe6SAndreas Gohr if(preg_match('/^(127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/',$i)){ 6486d8affe6SAndreas Gohr continue; 6496d8affe6SAndreas Gohr }else{ 6506d8affe6SAndreas Gohr return $i; 6516d8affe6SAndreas Gohr } 6526d8affe6SAndreas Gohr } 6536d8affe6SAndreas Gohr // still here? just use the first (last) address 6546d8affe6SAndreas Gohr return $ip[0]; 655f3f0262cSandi} 656f3f0262cSandi 657f3f0262cSandi/** 6581c548ebeSAndreas Gohr * Check if the browser is on a mobile device 6591c548ebeSAndreas Gohr * 6601c548ebeSAndreas Gohr * Adapted from the example code at url below 6611c548ebeSAndreas Gohr * 6621c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 6631c548ebeSAndreas Gohr */ 6641c548ebeSAndreas Gohrfunction clientismobile(){ 6651c548ebeSAndreas Gohr 6661c548ebeSAndreas Gohr if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) return true; 6671c548ebeSAndreas Gohr 6681c548ebeSAndreas Gohr if(preg_match('/wap\.|\.wap/i',$_SERVER['HTTP_ACCEPT'])) return true; 6691c548ebeSAndreas Gohr 6701c548ebeSAndreas Gohr if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; 6711c548ebeSAndreas Gohr 6721c548ebeSAndreas Gohr $uamatches = 'midp|j2me|avantg|docomo|novarra|palmos|palmsource|240x320|opwv|chtml|pda|windows ce|mmp\/|blackberry|mib\/|symbian|wireless|nokia|hand|mobi|phone|cdm|up\.b|audio|SIE\-|SEC\-|samsung|HTC|mot\-|mitsu|sagem|sony|alcatel|lg|erics|vx|NEC|philips|mmm|xx|panasonic|sharp|wap|sch|rover|pocket|benq|java|pt|pg|vox|amoi|bird|compal|kg|voda|sany|kdd|dbt|sendo|sgh|gradi|jb|\d\d\di|moto'; 6731c548ebeSAndreas Gohr 6741c548ebeSAndreas Gohr if(preg_match("/$uamatches/i",$_SERVER['HTTP_USER_AGENT'])) return true; 6751c548ebeSAndreas Gohr 6761c548ebeSAndreas Gohr return false; 6771c548ebeSAndreas Gohr} 6781c548ebeSAndreas Gohr 6791c548ebeSAndreas Gohr 6801c548ebeSAndreas Gohr/** 68163211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 68263211f61SGlen Harris * 68363211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 68463211f61SGlen Harris * @returns a comma separated list of hostnames 68563211f61SGlen Harris */ 68663211f61SGlen Harrisfunction gethostsbyaddrs($ips){ 68763211f61SGlen Harris $hosts = array(); 68863211f61SGlen Harris $ips = explode(',',$ips); 689551a720fSMichael Klier 690551a720fSMichael Klier if(is_array($ips)) { 6913886270dSAndreas Gohr foreach($ips as $ip){ 692551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 69363211f61SGlen Harris } 694551a720fSMichael Klier return join(',',$hosts); 695551a720fSMichael Klier } else { 696551a720fSMichael Klier return gethostbyaddr(trim($ips)); 697551a720fSMichael Klier } 69863211f61SGlen Harris} 69963211f61SGlen Harris 70063211f61SGlen Harris/** 70115fae107Sandi * Checks if a given page is currently locked. 70215fae107Sandi * 703f3f0262cSandi * removes stale lockfiles 70415fae107Sandi * 70515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 706f3f0262cSandi */ 707f3f0262cSandifunction checklock($id){ 708f3f0262cSandi global $conf; 709c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 710f3f0262cSandi 711f3f0262cSandi //no lockfile 712f3f0262cSandi if(!@file_exists($lock)) return false; 713f3f0262cSandi 714f3f0262cSandi //lockfile expired 715f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']){ 716d8186216SBen Coburn @unlink($lock); 717f3f0262cSandi return false; 718f3f0262cSandi } 719f3f0262cSandi 720f3f0262cSandi //my own lock 721f3f0262cSandi $ip = io_readFile($lock); 722f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 723f3f0262cSandi return false; 724f3f0262cSandi } 725f3f0262cSandi 726f3f0262cSandi return $ip; 727f3f0262cSandi} 728f3f0262cSandi 729f3f0262cSandi/** 73015fae107Sandi * Lock a page for editing 73115fae107Sandi * 73215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 733f3f0262cSandi */ 734f3f0262cSandifunction lock($id){ 735c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 736f3f0262cSandi if($_SERVER['REMOTE_USER']){ 737f3f0262cSandi io_saveFile($lock,$_SERVER['REMOTE_USER']); 738f3f0262cSandi }else{ 739f3f0262cSandi io_saveFile($lock,clientIP()); 740f3f0262cSandi } 741f3f0262cSandi} 742f3f0262cSandi 743f3f0262cSandi/** 74415fae107Sandi * Unlock a page if it was locked by the user 745f3f0262cSandi * 74615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 74715fae107Sandi * @return bool true if a lock was removed 748f3f0262cSandi */ 749f3f0262cSandifunction unlock($id){ 750c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 751f3f0262cSandi if(@file_exists($lock)){ 752f3f0262cSandi $ip = io_readFile($lock); 753f3f0262cSandi if( ($ip == clientIP()) || ($ip == $_SERVER['REMOTE_USER']) ){ 754f3f0262cSandi @unlink($lock); 755f3f0262cSandi return true; 756f3f0262cSandi } 757f3f0262cSandi } 758f3f0262cSandi return false; 759f3f0262cSandi} 760f3f0262cSandi 761f3f0262cSandi/** 762f3f0262cSandi * convert line ending to unix format 763f3f0262cSandi * 76415fae107Sandi * @see formText() for 2crlf conversion 76515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 766f3f0262cSandi */ 767f3f0262cSandifunction cleanText($text){ 768f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/","\012",$text); 769f3f0262cSandi return $text; 770f3f0262cSandi} 771f3f0262cSandi 772f3f0262cSandi/** 773f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 774f3f0262cSandi * It also converts line endings to Windows format which is 775f3f0262cSandi * pseudo standard for webforms. 776f3f0262cSandi * 77715fae107Sandi * @see cleanText() for 2unix conversion 77815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 779f3f0262cSandi */ 780f3f0262cSandifunction formText($text){ 7815b7d45a5SAndreas Gohr $text = str_replace("\012","\015\012",$text); 782f3f0262cSandi return htmlspecialchars($text); 783f3f0262cSandi} 784f3f0262cSandi 785f3f0262cSandi/** 78615fae107Sandi * Returns the specified local text in raw format 78715fae107Sandi * 78815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 789f3f0262cSandi */ 790f3f0262cSandifunction rawLocale($id){ 791f3f0262cSandi return io_readFile(localeFN($id)); 792f3f0262cSandi} 793f3f0262cSandi 794f3f0262cSandi/** 795f3f0262cSandi * Returns the raw WikiText 79615fae107Sandi * 79715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 798f3f0262cSandi */ 799f3f0262cSandifunction rawWiki($id,$rev=''){ 800cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 801f3f0262cSandi} 802f3f0262cSandi 803f3f0262cSandi/** 8047146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 8057146cee2SAndreas Gohr * 8067146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8077146cee2SAndreas Gohr */ 808b7d5a5f0SAndreas Gohrfunction pageTemplate($data){ 809b7d5a5f0SAndreas Gohr $id = $data[0]; 810a15ce62dSEsther Brunner global $conf; 811a15ce62dSEsther Brunner global $INFO; 812e29549feSAndreas Gohr 813e29549feSAndreas Gohr $path = dirname(wikiFN($id)); 814e29549feSAndreas Gohr 815e29549feSAndreas Gohr if(@file_exists($path.'/_template.txt')){ 816e29549feSAndreas Gohr $tpl = io_readFile($path.'/_template.txt'); 817e29549feSAndreas Gohr }else{ 818e29549feSAndreas Gohr // search upper namespaces for templates 819e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'],'/')); 820e29549feSAndreas Gohr while (strlen($path) >= $len){ 821e29549feSAndreas Gohr if(@file_exists($path.'/__template.txt')){ 822e29549feSAndreas Gohr $tpl = io_readFile($path.'/__template.txt'); 823e29549feSAndreas Gohr break; 824e29549feSAndreas Gohr } 825e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 826e29549feSAndreas Gohr } 827e29549feSAndreas Gohr } 828e29549feSAndreas Gohr if(!$tpl) return ''; 829e29549feSAndreas Gohr 830e29549feSAndreas Gohr // replace placeholders 83126ece5a7SAndreas Gohr $file = noNS($id); 83226ece5a7SAndreas Gohr $page = strtr($file,'_',' '); 83326ece5a7SAndreas Gohr 83426ece5a7SAndreas Gohr $tpl = str_replace(array( 83526ece5a7SAndreas Gohr '@ID@', 83626ece5a7SAndreas Gohr '@NS@', 83726ece5a7SAndreas Gohr '@FILE@', 83826ece5a7SAndreas Gohr '@!FILE@', 83926ece5a7SAndreas Gohr '@!FILE!@', 84026ece5a7SAndreas Gohr '@PAGE@', 84126ece5a7SAndreas Gohr '@!PAGE@', 84226ece5a7SAndreas Gohr '@!!PAGE@', 84326ece5a7SAndreas Gohr '@!PAGE!@', 84426ece5a7SAndreas Gohr '@USER@', 84526ece5a7SAndreas Gohr '@NAME@', 84626ece5a7SAndreas Gohr '@MAIL@', 84726ece5a7SAndreas Gohr '@DATE@', 84826ece5a7SAndreas Gohr ), 84926ece5a7SAndreas Gohr array( 85026ece5a7SAndreas Gohr $id, 85126ece5a7SAndreas Gohr getNS($id), 85226ece5a7SAndreas Gohr $file, 85326ece5a7SAndreas Gohr utf8_ucfirst($file), 85426ece5a7SAndreas Gohr utf8_strtoupper($file), 85526ece5a7SAndreas Gohr $page, 85626ece5a7SAndreas Gohr utf8_ucfirst($page), 85726ece5a7SAndreas Gohr utf8_ucwords($page), 85826ece5a7SAndreas Gohr utf8_strtoupper($page), 85926ece5a7SAndreas Gohr $_SERVER['REMOTE_USER'], 86026ece5a7SAndreas Gohr $INFO['userinfo']['name'], 86126ece5a7SAndreas Gohr $INFO['userinfo']['mail'], 86226ece5a7SAndreas Gohr $conf['dformat'], 86326ece5a7SAndreas Gohr ), $tpl); 86426ece5a7SAndreas Gohr 8657d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 8667d644fc8SAndreas Gohr $tpl = preg_replace_callback('/%./',create_function('$m','return strftime($m[0]);'),$tpl); 8677d644fc8SAndreas Gohr 868a15ce62dSEsther Brunner return $tpl; 8697146cee2SAndreas Gohr} 8707146cee2SAndreas Gohr 8717146cee2SAndreas Gohr 8727146cee2SAndreas Gohr/** 87315fae107Sandi * Returns the raw Wiki Text in three slices. 87415fae107Sandi * 87515fae107Sandi * The range parameter needs to have the form "from-to" 87615cfe303Sandi * and gives the range of the section in bytes - no 87715cfe303Sandi * UTF-8 awareness is needed. 878f3f0262cSandi * The returned order is prefix, section and suffix. 87915fae107Sandi * 88015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 881f3f0262cSandi */ 882f3f0262cSandifunction rawWikiSlices($range,$id,$rev=''){ 8834b7f9e70STom N Harris list($from,$to) = explode('-',$range,2); 884cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 885f3f0262cSandi if(!$from) $from = 0; 886c3d8e19bSandi if(!$to) $to = strlen($text)+1; 887f3f0262cSandi 88815cfe303Sandi $slices[0] = substr($text,0,$from-1); 88915cfe303Sandi $slices[1] = substr($text,$from-1,$to-$from); 89015cfe303Sandi $slices[2] = substr($text,$to); 891f3f0262cSandi 892f3f0262cSandi return $slices; 893f3f0262cSandi} 894f3f0262cSandi 895f3f0262cSandi/** 89615fae107Sandi * Joins wiki text slices 89715fae107Sandi * 898f3f0262cSandi * function to join the text slices with correct lineendings again. 899f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 900f3f0262cSandi * lines between sections if needed (used on saving). 90115fae107Sandi * 90215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 903f3f0262cSandi */ 904f3f0262cSandifunction con($pre,$text,$suf,$pretty=false){ 905f3f0262cSandi if($pretty){ 906f3f0262cSandi if($pre && substr($pre,-1) != "\n") $pre .= "\n"; 907f3f0262cSandi if($suf && substr($text,-1) != "\n") $text .= "\n"; 908f3f0262cSandi } 909f3f0262cSandi 9107e038d4eSAndreas Gohr // Avoid double newline above section when saving section edit 9117e038d4eSAndreas Gohr //if($pre) $pre .= "\n"; 912f3f0262cSandi if($suf) $text .= "\n"; 913f3f0262cSandi return $pre.$text.$suf; 914f3f0262cSandi} 915f3f0262cSandi 916f3f0262cSandi/** 917a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 918a701424fSBen Coburn * Also directs changelog and attic updates. 91915fae107Sandi * 92015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 92171726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 922f3f0262cSandi */ 923b6912aeaSAndreas Gohrfunction saveWikiText($id,$text,$summary,$minor=false){ 924a701424fSBen Coburn /* Note to developers: 925a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 926a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 927a701424fSBen Coburn after any changes. External edits change the wiki page 928a701424fSBen Coburn directly without using php or dokuwiki. 929a701424fSBen Coburn */ 930f3f0262cSandi global $conf; 931f3f0262cSandi global $lang; 93271726d78SBen Coburn global $REV; 933f3f0262cSandi // ignore if no changes were made 934f3f0262cSandi if($text == rawWiki($id,'')){ 935f3f0262cSandi return; 936f3f0262cSandi } 937f3f0262cSandi 938f3f0262cSandi $file = wikiFN($id); 939a701424fSBen Coburn $old = @filemtime($file); // from page 94071726d78SBen Coburn $wasRemoved = empty($text); 941d8186216SBen Coburn $wasCreated = !@file_exists($file); 94271726d78SBen Coburn $wasReverted = ($REV==true); 943e45b34cdSBen Coburn $newRev = false; 944a701424fSBen Coburn $oldRev = getRevisions($id, -1, 1, 1024); // from changelog 945a701424fSBen Coburn $oldRev = (int)(empty($oldRev)?0:$oldRev[0]); 946a701424fSBen Coburn if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old>=$oldRev) { 94746844156SBen Coburn // add old revision to the attic if missing 94846844156SBen Coburn saveOldRevision($id); 94946844156SBen Coburn // add a changelog entry if this edit came from outside dokuwiki 950a701424fSBen Coburn if ($old>$oldRev) { 951ebf1501fSBen Coburn addLogEntry($old, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=>true)); 95246844156SBen Coburn // remove soon to be stale instructions 95346844156SBen Coburn $cache = new cache_instructions($id, $file); 95446844156SBen Coburn $cache->removeCache(); 95546844156SBen Coburn } 95646844156SBen Coburn } 957f3f0262cSandi 95871726d78SBen Coburn if ($wasRemoved){ 95930725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 96030725328SGabriel Birke $data = array(array($file, '', false), getNS($id), noNS($id), false); 96130725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 962e45b34cdSBen Coburn // pre-save deleted revision 963e45b34cdSBen Coburn @touch($file); 96446844156SBen Coburn clearstatcache(); 965e45b34cdSBen Coburn $newRev = saveOldRevision($id); 966e1f3d9e1SEsther Brunner // remove empty file 967f3f0262cSandi @unlink($file); 96871726d78SBen Coburn // remove old meta info... 969e1f3d9e1SEsther Brunner $mfiles = metaFiles($id); 97071726d78SBen Coburn $changelog = metaFN($id, '.changes'); 9713d1f9ec3SMichael Klier $metadata = metaFN($id, '.meta'); 972e1f3d9e1SEsther Brunner foreach ($mfiles as $mfile) { 9733d1f9ec3SMichael Klier // but keep per-page changelog to preserve page history and keep meta data 9743d1f9ec3SMichael Klier if (@file_exists($mfile) && $mfile!==$changelog && $mfile!==$metadata) { @unlink($mfile); } 975b158d625SSteven Danz } 9763d1f9ec3SMichael Klier // purge meta data 9773d1f9ec3SMichael Klier p_purge_metadata($id); 978f3f0262cSandi $del = true; 9793ce054b3Sandi // autoset summary on deletion 9803ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 98153d6ccfeSandi // remove empty namespaces 982cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 983cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 984f3f0262cSandi }else{ 985cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 986cc7d0c94SBen Coburn io_writeWikiPage($file, $text, $id); 98746844156SBen Coburn // pre-save the revision, to keep the attic in sync 98846844156SBen Coburn $newRev = saveOldRevision($id); 989f3f0262cSandi $del = false; 990f3f0262cSandi } 991f3f0262cSandi 99271726d78SBen Coburn // select changelog line type 99371726d78SBen Coburn $extra = ''; 994ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_EDIT; 99571726d78SBen Coburn if ($wasReverted) { 996ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_REVERT; 99771726d78SBen Coburn $extra = $REV; 99871726d78SBen Coburn } 999ebf1501fSBen Coburn else if ($wasCreated) { $type = DOKU_CHANGE_TYPE_CREATE; } 1000ebf1501fSBen Coburn else if ($wasRemoved) { $type = DOKU_CHANGE_TYPE_DELETE; } 1001ebf1501fSBen Coburn else if ($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { $type = DOKU_CHANGE_TYPE_MINOR_EDIT; } //minor edits only for logged in users 100271726d78SBen Coburn 1003e45b34cdSBen Coburn addLogEntry($newRev, $id, $type, $summary, $extra); 100426a0801fSAndreas Gohr // send notify mails 100590033e9dSAndreas Gohr notify($id,'admin',$old,$summary,$minor); 100690033e9dSAndreas Gohr notify($id,'subscribers',$old,$summary,$minor); 1007f3f0262cSandi 1008ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 100998407a7aSandi io_saveFile($conf['cachedir'].'/purgefile',time()); 10102eccbdaaSGina Haeussge 10112eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1012fe9ec250SChris Smith if(useHeading('content')){ 10132eccbdaaSGina Haeussge require_once(DOKU_INC.'inc/fulltext.php'); 10142eccbdaaSGina Haeussge $pages = ft_backlinks($id); 10152eccbdaaSGina Haeussge foreach ($pages as $page) { 10162eccbdaaSGina Haeussge $cache = new cache_renderer($page, wikiFN($page), 'xhtml'); 10172eccbdaaSGina Haeussge $cache->removeCache(); 10182eccbdaaSGina Haeussge } 10192eccbdaaSGina Haeussge } 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); 1034cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1035f3f0262cSandi return $date; 1036f3f0262cSandi} 1037f3f0262cSandi 1038f3f0262cSandi/** 1039fde10de4SAdrian Lang * Sends a notify mail on page change or registration 104026a0801fSAndreas Gohr * 104126a0801fSAndreas Gohr * @param string $id The changed page 1042fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 104326a0801fSAndreas Gohr * @param int $rev Old page revision 104426a0801fSAndreas Gohr * @param string $summary What changed 104590033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 104602a498e7Schris * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 104715fae107Sandi * 104815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1049f3f0262cSandi */ 105002a498e7Schrisfunction notify($id,$who,$rev='',$summary='',$minor=false,$replace=array()){ 1051f3f0262cSandi global $lang; 1052f3f0262cSandi global $conf; 105330d7d718SMike Frysinger global $INFO; 1054b158d625SSteven Danz 105526a0801fSAndreas Gohr // decide if there is something to do 105626a0801fSAndreas Gohr if($who == 'admin'){ 105726a0801fSAndreas Gohr if(empty($conf['notify'])) return; //notify enabled? 1058f3f0262cSandi $text = rawLocale('mailtext'); 105926a0801fSAndreas Gohr $to = $conf['notify']; 106026a0801fSAndreas Gohr $bcc = ''; 106126a0801fSAndreas Gohr }elseif($who == 'subscribers'){ 106226a0801fSAndreas Gohr if(!$conf['subscribers']) return; //subscribers enabled? 106390033e9dSAndreas Gohr if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return; //skip minors 1064*8881fcc9SAdrian Lang $data = array('id' => $id, 'addresslist' => '', 'self' => false); 1065*8881fcc9SAdrian Lang trigger_event('COMMON_NOTIFY_ADDRESSLIST', $data, 1066*8881fcc9SAdrian Lang 'subscription_addresslist'); 1067*8881fcc9SAdrian Lang $bcc = $data['addresslist']; 106826a0801fSAndreas Gohr if(empty($bcc)) return; 106926a0801fSAndreas Gohr $to = ''; 10705b75cd1fSAdrian Lang $text = rawLocale('subscr_single'); 1071a06e4bdbSSebastian Harl }elseif($who == 'register'){ 1072a06e4bdbSSebastian Harl if(empty($conf['registernotify'])) return; 1073a06e4bdbSSebastian Harl $text = rawLocale('registermail'); 1074a06e4bdbSSebastian Harl $to = $conf['registernotify']; 1075a06e4bdbSSebastian Harl $bcc = ''; 107626a0801fSAndreas Gohr }else{ 107726a0801fSAndreas Gohr return; //just to be safe 107826a0801fSAndreas Gohr } 107926a0801fSAndreas Gohr 108063211f61SGlen Harris $ip = clientIP(); 1081f2263577SAndreas Gohr $text = str_replace('@DATE@',dformat(),$text); 1082f3f0262cSandi $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text); 108363211f61SGlen Harris $text = str_replace('@IPADDRESS@',$ip,$text); 108463211f61SGlen Harris $text = str_replace('@HOSTNAME@',gethostsbyaddrs($ip),$text); 1085c9321d91SAndreas Gohr $text = str_replace('@NEWPAGE@',wl($id,'',true,'&'),$text); 108626a0801fSAndreas Gohr $text = str_replace('@PAGE@',$id,$text); 108726a0801fSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 1088ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 1089f3f0262cSandi $text = str_replace('@SUMMARY@',$summary,$text); 10907a82afdcSandi $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text); 1091f3f0262cSandi 109202a498e7Schris foreach ($replace as $key => $substitution) { 109302a498e7Schris $text = str_replace('@'.strtoupper($key).'@',$substitution, $text); 109402a498e7Schris } 109502a498e7Schris 1096a06e4bdbSSebastian Harl if($who == 'register'){ 1097a06e4bdbSSebastian Harl $subject = $lang['mail_new_user'].' '.$summary; 1098a06e4bdbSSebastian Harl }elseif($rev){ 1099f3f0262cSandi $subject = $lang['mail_changed'].' '.$id; 1100c9321d91SAndreas Gohr $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true,'&'),$text); 1101ccdfa6c0SAndreas Gohr require_once(DOKU_INC.'inc/DifferenceEngine.php'); 11024b7f9e70STom N Harris $df = new Diff(explode("\n",rawWiki($id,$rev)), 11034b7f9e70STom N Harris explode("\n",rawWiki($id))); 1104f3f0262cSandi $dformat = new UnifiedDiffFormatter(); 1105f3f0262cSandi $diff = $dformat->format($df); 1106f3f0262cSandi }else{ 1107f3f0262cSandi $subject=$lang['mail_newpage'].' '.$id; 1108f3f0262cSandi $text = str_replace('@OLDPAGE@','none',$text); 1109f3f0262cSandi $diff = rawWiki($id); 1110f3f0262cSandi } 1111f3f0262cSandi $text = str_replace('@DIFF@',$diff,$text); 1112241f3a36Sandi $subject = '['.$conf['title'].'] '.$subject; 1113f3f0262cSandi 111430d7d718SMike Frysinger $from = $conf['mailfrom']; 111530d7d718SMike Frysinger $from = str_replace('@USER@',$_SERVER['REMOTE_USER'],$from); 111630d7d718SMike Frysinger $from = str_replace('@NAME@',$INFO['userinfo']['name'],$from); 111730d7d718SMike Frysinger $from = str_replace('@MAIL@',$INFO['userinfo']['mail'],$from); 111830d7d718SMike Frysinger 111930d7d718SMike Frysinger mail_send($to,$subject,$text,$from,'',$bcc); 1120f3f0262cSandi} 1121f3f0262cSandi 112215fae107Sandi/** 112371f7bde7SAndreas Gohr * extracts the query from a search engine referrer 112415fae107Sandi * 112515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 112671f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1127f3f0262cSandi */ 1128f3f0262cSandifunction getGoogleQuery(){ 1129c66972f2SAdrian Lang if (!isset($_SERVER['HTTP_REFERER'])) { 1130c66972f2SAdrian Lang return ''; 1131c66972f2SAdrian Lang } 1132f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 1133f3f0262cSandi 1134f3f0262cSandi $query = array(); 1135e4d8a516SKazutaka Miyasaka 1136e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1137e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1138e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1139f3f0262cSandi parse_str($url['query'],$query); 1140e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1141e4d8a516SKazutaka Miyasaka 1142c66972f2SAdrian Lang $q = ''; 114371f7bde7SAndreas Gohr if(isset($query['q'])) 1144f93b3b50SAndreas Gohr $q = $query['q']; // google, live/msn, aol, ask, altavista, alltheweb, gigablast 114571f7bde7SAndreas Gohr elseif(isset($query['p'])) 1146f93b3b50SAndreas Gohr $q = $query['p']; // yahoo 114771f7bde7SAndreas Gohr elseif(isset($query['query'])) 1148f93b3b50SAndreas Gohr $q = $query['query']; // lycos, netscape, clusty, hotbot 114971f7bde7SAndreas Gohr elseif(preg_match("#a9\.com#i",$url['host'])) // a9 1150f93b3b50SAndreas Gohr $q = urldecode(ltrim($url['path'],'/')); 1151f3f0262cSandi 1152c66972f2SAdrian Lang if($q === '') return ''; 11536531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/',$q,-1,PREG_SPLIT_NO_EMPTY); 1154f93b3b50SAndreas Gohr return $q; 1155f3f0262cSandi} 1156f3f0262cSandi 1157f3f0262cSandi/** 115815fae107Sandi * Try to set correct locale 115915fae107Sandi * 1160095bfd5cSandi * @deprecated No longer used 116115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1162f3f0262cSandi */ 1163f3f0262cSandifunction setCorrectLocale(){ 1164f3f0262cSandi global $conf; 1165f3f0262cSandi global $lang; 1166f3f0262cSandi 1167f3f0262cSandi $enc = strtoupper($lang['encoding']); 1168f3f0262cSandi foreach ($lang['locales'] as $loc){ 1169f3f0262cSandi //try locale 1170f3f0262cSandi if(@setlocale(LC_ALL,$loc)) return; 1171f3f0262cSandi //try loceale with encoding 1172f3f0262cSandi if(@setlocale(LC_ALL,"$loc.$enc")) return; 1173f3f0262cSandi } 1174f3f0262cSandi //still here? try to set from environment 1175f3f0262cSandi @setlocale(LC_ALL,""); 1176f3f0262cSandi} 1177f3f0262cSandi 1178f3f0262cSandi/** 1179f3f0262cSandi * Return the human readable size of a file 1180f3f0262cSandi * 1181f3f0262cSandi * @param int $size A file size 1182f3f0262cSandi * @param int $dec A number of decimal places 1183f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1184f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1185f3f0262cSandi * @version 1.0.0 1186f3f0262cSandi */ 1187f31d5b73Sandifunction filesize_h($size, $dec = 1){ 1188f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1189f3f0262cSandi $count = count($sizes); 1190f3f0262cSandi $i = 0; 1191f3f0262cSandi 1192f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 1193f3f0262cSandi $size /= 1024; 1194f3f0262cSandi $i++; 1195f3f0262cSandi } 1196f3f0262cSandi 1197f3f0262cSandi return round($size, $dec) . ' ' . $sizes[$i]; 1198f3f0262cSandi} 1199f3f0262cSandi 120015fae107Sandi/** 1201c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1202c57e365eSAndreas Gohr * 1203c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1204c57e365eSAndreas Gohr */ 1205c57e365eSAndreas Gohrfunction datetime_h($dt){ 1206c57e365eSAndreas Gohr global $lang; 1207c57e365eSAndreas Gohr 1208c57e365eSAndreas Gohr $ago = time() - $dt; 1209c57e365eSAndreas Gohr if($ago > 24*60*60*30*12*2){ 1210c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago/(24*60*60*30*12))); 1211c57e365eSAndreas Gohr } 1212c57e365eSAndreas Gohr if($ago > 24*60*60*30*2){ 1213c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago/(24*60*60*30))); 1214c57e365eSAndreas Gohr } 1215c57e365eSAndreas Gohr if($ago > 24*60*60*7*2){ 1216c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago/(24*60*60*7))); 1217c57e365eSAndreas Gohr } 1218c57e365eSAndreas Gohr if($ago > 24*60*60*2){ 1219c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago/(24*60*60))); 1220c57e365eSAndreas Gohr } 1221c57e365eSAndreas Gohr if($ago > 60*60*2){ 1222c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago/(60*60))); 1223c57e365eSAndreas Gohr } 1224c57e365eSAndreas Gohr if($ago > 60*2){ 1225c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago/(60))); 1226c57e365eSAndreas Gohr } 1227c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1228c57e365eSAndreas Gohr} 1229c57e365eSAndreas Gohr 1230c57e365eSAndreas Gohr/** 1231f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1232f2263577SAndreas Gohr * 1233f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1234f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1235f2263577SAndreas Gohr * 1236f2263577SAndreas Gohr * @see datetime_h 1237f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1238f2263577SAndreas Gohr */ 1239f2263577SAndreas Gohrfunction dformat($dt=null,$format=''){ 1240f2263577SAndreas Gohr global $conf; 1241f2263577SAndreas Gohr 1242f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1243f2263577SAndreas Gohr $dt = (int) $dt; 1244f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1245f2263577SAndreas Gohr 1246f2263577SAndreas Gohr $format = str_replace('%f',datetime_h($dt),$format); 1247f2263577SAndreas Gohr return strftime($format,$dt); 1248f2263577SAndreas Gohr} 1249f2263577SAndreas Gohr 1250f2263577SAndreas Gohr/** 125100a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 125200a7b5adSEsther Brunner * 125300a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 125400a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 125500a7b5adSEsther Brunner */ 125600a7b5adSEsther Brunnerfunction obfuscate($email) { 125700a7b5adSEsther Brunner global $conf; 125800a7b5adSEsther Brunner 125900a7b5adSEsther Brunner switch ($conf['mailguard']) { 126000a7b5adSEsther Brunner case 'visible' : 126100a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 126200a7b5adSEsther Brunner return strtr($email, $obfuscate); 126300a7b5adSEsther Brunner 126400a7b5adSEsther Brunner case 'hex' : 126500a7b5adSEsther Brunner $encode = ''; 126649eb6e38SAndreas Gohr $len = strlen($email); 126749eb6e38SAndreas Gohr for ($x=0; $x < $len; $x++){ 126849eb6e38SAndreas Gohr $encode .= '&#x' . bin2hex($email{$x}).';'; 126949eb6e38SAndreas Gohr } 127000a7b5adSEsther Brunner return $encode; 127100a7b5adSEsther Brunner 127200a7b5adSEsther Brunner case 'none' : 127300a7b5adSEsther Brunner default : 127400a7b5adSEsther Brunner return $email; 127500a7b5adSEsther Brunner } 127600a7b5adSEsther Brunner} 127700a7b5adSEsther Brunner 127800a7b5adSEsther Brunner/** 127989541d4bSAndreas Gohr * Removes quoting backslashes 128089541d4bSAndreas Gohr * 128189541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 128289541d4bSAndreas Gohr */ 128389541d4bSAndreas Gohrfunction unslash($string,$char="'"){ 128489541d4bSAndreas Gohr return str_replace('\\'.$char,$char,$string); 128589541d4bSAndreas Gohr} 128689541d4bSAndreas Gohr 128773038c47SAndreas Gohr/** 128873038c47SAndreas Gohr * Convert php.ini shorthands to byte 128973038c47SAndreas Gohr * 129073038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 129173038c47SAndreas Gohr * @link http://de3.php.net/manual/en/ini.core.php#79564 129273038c47SAndreas Gohr */ 129373038c47SAndreas Gohrfunction php_to_byte($v){ 129473038c47SAndreas Gohr $l = substr($v, -1); 129573038c47SAndreas Gohr $ret = substr($v, 0, -1); 129673038c47SAndreas Gohr switch(strtoupper($l)){ 129773038c47SAndreas Gohr case 'P': 129873038c47SAndreas Gohr $ret *= 1024; 129973038c47SAndreas Gohr case 'T': 130073038c47SAndreas Gohr $ret *= 1024; 130173038c47SAndreas Gohr case 'G': 130273038c47SAndreas Gohr $ret *= 1024; 130373038c47SAndreas Gohr case 'M': 130473038c47SAndreas Gohr $ret *= 1024; 130573038c47SAndreas Gohr case 'K': 130673038c47SAndreas Gohr $ret *= 1024; 130773038c47SAndreas Gohr break; 130873038c47SAndreas Gohr } 130973038c47SAndreas Gohr return $ret; 131073038c47SAndreas Gohr} 131173038c47SAndreas Gohr 1312546d3a99SAndreas Gohr/** 1313546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1314546d3a99SAndreas Gohr */ 1315546d3a99SAndreas Gohrfunction preg_quote_cb($string){ 1316546d3a99SAndreas Gohr return preg_quote($string,'/'); 1317546d3a99SAndreas Gohr} 131873038c47SAndreas Gohr 1319bd2f6c2fSAndreas Gohr/** 1320bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1321bd2f6c2fSAndreas Gohr * 1322c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1323bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1324bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1325bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1326bd2f6c2fSAndreas Gohr * 1327bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1328bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1329bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1330bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1331bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 1332bd2f6c2fSAndreas Gohr */ 1333a5d27328SAndreas Gohrfunction shorten($keep,$short,$max,$min=9,$char='…'){ 1334bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1335bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1336bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1337bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1338bd2f6c2fSAndreas Gohr $half = floor($max/2); 1339bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short,0,$half-1).$char.utf8_substr($short,$len-$half); 1340bd2f6c2fSAndreas Gohr} 1341bd2f6c2fSAndreas Gohr 1342dc58b6f4SAndy Webber/** 1343dc58b6f4SAndy Webber * Return the users realname or e-mail address for use 1344dc58b6f4SAndy Webber * in page footer and recent changes pages 1345dc58b6f4SAndy Webber * 1346dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1347dc58b6f4SAndy Webber */ 1348dc58b6f4SAndy Webberfunction editorinfo($username){ 1349dc58b6f4SAndy Webber global $conf; 1350dc58b6f4SAndy Webber global $auth; 1351dc58b6f4SAndy Webber 1352dc58b6f4SAndy Webber switch($conf['showuseras']){ 1353dc58b6f4SAndy Webber case 'username': 1354dc58b6f4SAndy Webber case 'email': 1355dc58b6f4SAndy Webber case 'email_link': 1356173d78c4SAndreas Gohr if($auth) $info = $auth->getUserData($username); 1357dc58b6f4SAndy Webber break; 1358dc58b6f4SAndy Webber default: 1359dc58b6f4SAndy Webber return hsc($username); 1360dc58b6f4SAndy Webber } 1361dc58b6f4SAndy Webber 1362dc58b6f4SAndy Webber if(isset($info) && $info) { 1363dc58b6f4SAndy Webber switch($conf['showuseras']){ 1364dc58b6f4SAndy Webber case 'username': 1365dc58b6f4SAndy Webber return hsc($info['name']); 1366dc58b6f4SAndy Webber case 'email': 1367dc58b6f4SAndy Webber return obfuscate($info['mail']); 1368dc58b6f4SAndy Webber case 'email_link': 1369dc58b6f4SAndy Webber $mail=obfuscate($info['mail']); 1370dc58b6f4SAndy Webber return '<a href="mailto:'.$mail.'">'.$mail.'</a>'; 1371dc58b6f4SAndy Webber default: 1372dc58b6f4SAndy Webber return hsc($username); 1373dc58b6f4SAndy Webber } 1374dc58b6f4SAndy Webber } else { 1375dc58b6f4SAndy Webber return hsc($username); 1376dc58b6f4SAndy Webber } 1377066fee30SAndreas Gohr} 1378066fee30SAndreas Gohr 1379066fee30SAndreas Gohr/** 1380066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1381066fee30SAndreas Gohr * When no image exists, returns an empty string 1382066fee30SAndreas Gohr * 1383066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1384066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 1385066fee30SAndreas Gohr */ 1386066fee30SAndreas Gohrfunction license_img($type){ 1387066fee30SAndreas Gohr global $license; 1388066fee30SAndreas Gohr global $conf; 1389066fee30SAndreas Gohr if(!$conf['license']) return ''; 1390066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1391066fee30SAndreas Gohr $lic = $license[$conf['license']]; 1392066fee30SAndreas Gohr $try = array(); 1393066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1394066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1395066fee30SAndreas Gohr if(substr($conf['license'],0,3) == 'cc-'){ 1396066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1397066fee30SAndreas Gohr } 1398066fee30SAndreas Gohr foreach($try as $src){ 1399066fee30SAndreas Gohr if(@file_exists(DOKU_INC.$src)) return $src; 1400066fee30SAndreas Gohr } 1401066fee30SAndreas Gohr return ''; 1402dc58b6f4SAndy Webber} 1403dc58b6f4SAndy Webber 140413c08e2fSMichael Klier/** 140513c08e2fSMichael Klier * Checks if the given amount of memory is available 140613c08e2fSMichael Klier * 140713c08e2fSMichael Klier * If the memory_get_usage() function is not available the 140813c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 140913c08e2fSMichael Klier * 141013c08e2fSMichael Klier * @param int $mem Size of memory you want to allocate in bytes 141113c08e2fSMichael Klier * @param int $used already allocated memory (see above) 141213c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 141313c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 141413c08e2fSMichael Klier */ 141513c08e2fSMichael Klierfunction is_mem_available($mem,$bytes=1048576){ 141613c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 141713c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 141813c08e2fSMichael Klier 141913c08e2fSMichael Klier // parse limit to bytes 142013c08e2fSMichael Klier $limit = php_to_byte($limit); 142113c08e2fSMichael Klier 142213c08e2fSMichael Klier // get used memory if possible 142313c08e2fSMichael Klier if(function_exists('memory_get_usage')){ 142413c08e2fSMichael Klier $used = memory_get_usage(); 142549eb6e38SAndreas Gohr }else{ 142649eb6e38SAndreas Gohr $used = $bytes; 142713c08e2fSMichael Klier } 142813c08e2fSMichael Klier 142913c08e2fSMichael Klier if($used+$mem > $limit){ 143013c08e2fSMichael Klier return false; 143113c08e2fSMichael Klier } 143213c08e2fSMichael Klier 143313c08e2fSMichael Klier return true; 143413c08e2fSMichael Klier} 143513c08e2fSMichael Klier 1436af2408d5SAndreas Gohr/** 1437af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1438af2408d5SAndreas Gohr * 1439af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1440af2408d5SAndreas Gohr * 1441af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1442af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1443af2408d5SAndreas Gohr */ 1444af2408d5SAndreas Gohrfunction send_redirect($url){ 1445d4869846SAndreas Gohr // always close the session 1446d4869846SAndreas Gohr session_write_close(); 1447d4869846SAndreas Gohr 1448af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1449af2408d5SAndreas Gohr if( isset($_SERVER['SERVER_SOFTWARE']) && isset($_SERVER['GATEWAY_INTERFACE']) && 1450af2408d5SAndreas Gohr (strpos($_SERVER['GATEWAY_INTERFACE'],'CGI') !== false) && 1451af2408d5SAndreas Gohr (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) && 1452af2408d5SAndreas Gohr $matches[1] < 6 ){ 1453af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1454af2408d5SAndreas Gohr }else{ 1455af2408d5SAndreas Gohr header('Location: '.$url); 1456af2408d5SAndreas Gohr } 1457af2408d5SAndreas Gohr exit; 1458af2408d5SAndreas Gohr} 1459af2408d5SAndreas Gohr 14605b75cd1fSAdrian Lang/** 14615b75cd1fSAdrian Lang * Validate a value using a set of valid values 14625b75cd1fSAdrian Lang * 14635b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 14645b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 14655b75cd1fSAdrian Lang * default is specified, throws an exception. 14665b75cd1fSAdrian Lang * 14675b75cd1fSAdrian Lang * @param string $param The name of the parameter 14685b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 14695b75cd1fSAdrian Lang * be marked by the key “default”. 14705b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 14715b75cd1fSAdrian Lang * or $_GET) 14725b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 14735b75cd1fSAdrian Lang * 14745b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 14755b75cd1fSAdrian Lang */ 14765b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 14775b75cd1fSAdrian Lang if (isset($array[$param]) && in_array($array[$param], $valid_values)) { 14785b75cd1fSAdrian Lang return $array[$param]; 14795b75cd1fSAdrian Lang } elseif (isset($valid_values['default'])) { 14805b75cd1fSAdrian Lang return $valid_values['default']; 14815b75cd1fSAdrian Lang } else { 14825b75cd1fSAdrian Lang throw new Exception($exc); 14835b75cd1fSAdrian Lang } 14845b75cd1fSAdrian Lang} 14855b75cd1fSAdrian Lang 14865b75cd1fSAdrian Lang//Setup VIM: ex: et ts=2 enc=utf-8 : 1487