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 90db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 100db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 110c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 120c3a5702SAndreas Gohr 13f3f0262cSandi/** 14b6912aeaSAndreas Gohr * These constants are used with the recents function 15b6912aeaSAndreas Gohr */ 16b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED', 2); 17b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS', 4); 18b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES', 8); 190b926329SKate Arzamastsevadefine('RECENTS_MEDIA_CHANGES', 16); 200b926329SKate Arzamastsevadefine('RECENTS_MEDIA_PAGES_MIXED', 32); 21b6912aeaSAndreas Gohr 22b6912aeaSAndreas Gohr/** 23d5197206Schris * Wrapper around htmlspecialchars() 24d5197206Schris * 25d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 26d5197206Schris * @see htmlspecialchars() 27140cfbcdSGerrit Uitslag * 28140cfbcdSGerrit Uitslag * @param string $string the string being converted 29140cfbcdSGerrit Uitslag * @return string converted string 30d5197206Schris */ 31d5197206Schrisfunction hsc($string) { 32d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 33d5197206Schris} 34d5197206Schris 35d5197206Schris/** 365b571377SAndreas Gohr * Checks if the given input is blank 375b571377SAndreas Gohr * 385b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 395b571377SAndreas Gohr * 4067234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 4167234204SAndreas Gohr * with a NULL value without warning. 4267234204SAndreas Gohr * 4367234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 4467234204SAndreas Gohr * 4567234204SAndreas Gohr * (isset($foo) && !blank($foo)) 4667234204SAndreas Gohr * (!isset($foo) || blank($foo)) 4767234204SAndreas Gohr * 485b571377SAndreas Gohr * @param $in 495b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 505b571377SAndreas Gohr * @return bool 515b571377SAndreas Gohr */ 525b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 535b571377SAndreas Gohr if(is_null($in)) return true; 545b571377SAndreas Gohr if(is_array($in)) return empty($in); 555b571377SAndreas Gohr if($in === "\0") return true; 565b571377SAndreas Gohr if($trim && trim($in) === '') return true; 575b571377SAndreas Gohr if(strlen($in) > 0) return false; 585b571377SAndreas Gohr return empty($in); 595b571377SAndreas Gohr} 605b571377SAndreas Gohr 615b571377SAndreas Gohr/** 62d5197206Schris * print a newline terminated string 63d5197206Schris * 64d5197206Schris * You can give an indention as optional parameter 65d5197206Schris * 66d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 67140cfbcdSGerrit Uitslag * 68140cfbcdSGerrit Uitslag * @param string $string line of text 69140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 70d5197206Schris */ 7125ec097bSChris Smithfunction ptln($string, $indent = 0) { 7225ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 7302b0b681SAndreas Gohr} 7402b0b681SAndreas Gohr 7502b0b681SAndreas Gohr/** 7602b0b681SAndreas Gohr * strips control characters (<32) from the given string 7702b0b681SAndreas Gohr * 7802b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 79140cfbcdSGerrit Uitslag * 8042ea7f44SGerrit Uitslag * @param string $string being stripped 81140cfbcdSGerrit Uitslag * @return string 8202b0b681SAndreas Gohr */ 8302b0b681SAndreas Gohrfunction stripctl($string) { 8402b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 85d5197206Schris} 86d5197206Schris 87d5197206Schris/** 88634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 89634d7150SAndreas Gohr * 90634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 91634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 92634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 9342ea7f44SGerrit Uitslag * 94634d7150SAndreas Gohr * @return string 95634d7150SAndreas Gohr */ 96634d7150SAndreas Gohrfunction getSecurityToken() { 97585bf44eSChristopher Smith /** @var Input $INPUT */ 98585bf44eSChristopher Smith global $INPUT; 993680e2cdSAndreas Gohr 1003680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 1013680e2cdSAndreas Gohr $session = session_id(); 1023680e2cdSAndreas Gohr 1033680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1043680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 105*c3cc6e05SAndreas Gohr return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 106634d7150SAndreas Gohr} 107634d7150SAndreas Gohr 108634d7150SAndreas Gohr/** 109634d7150SAndreas Gohr * Check the secret CSRF token 110140cfbcdSGerrit Uitslag * 111140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 112140cfbcdSGerrit Uitslag * @return bool success if the token matched 113634d7150SAndreas Gohr */ 114634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 115585bf44eSChristopher Smith /** @var Input $INPUT */ 1167d01a0eaSTom N Harris global $INPUT; 117585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 118df97eaacSAndreas Gohr 1197d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 120634d7150SAndreas Gohr if(getSecurityToken() != $token) { 121634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 122634d7150SAndreas Gohr return false; 123634d7150SAndreas Gohr } 124634d7150SAndreas Gohr return true; 125634d7150SAndreas Gohr} 126634d7150SAndreas Gohr 127634d7150SAndreas Gohr/** 128634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 129634d7150SAndreas Gohr * 130634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 131140cfbcdSGerrit Uitslag * 132140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 13342ea7f44SGerrit Uitslag * @return string html of hidden form field 134634d7150SAndreas Gohr */ 135634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1362404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1373272d797SAndreas Gohr if($print) echo $ret; 138634d7150SAndreas Gohr return $ret; 139634d7150SAndreas Gohr} 140634d7150SAndreas Gohr 141634d7150SAndreas Gohr/** 1421015a57dSChristopher Smith * Determine basic information for a request of $id 14315fae107Sandi * 14415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1457e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 146140cfbcdSGerrit Uitslag * 147140cfbcdSGerrit Uitslag * @param string $id pageid 148140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 149140cfbcdSGerrit Uitslag * @return array with info for a request of $id 150140cfbcdSGerrit Uitslag * 151f3f0262cSandi */ 1521015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 153f3f0262cSandi global $USERINFO; 154585bf44eSChristopher Smith /* @var Input $INPUT */ 155585bf44eSChristopher Smith global $INPUT; 1566afe8dcaSchris 157c66972f2SAdrian Lang // set info about manager/admin status. 15859bc3b48SGerrit Uitslag $info = array(); 159c66972f2SAdrian Lang $info['isadmin'] = false; 160c66972f2SAdrian Lang $info['ismanager'] = false; 161585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 162f3f0262cSandi $info['userinfo'] = $USERINFO; 1631015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 164585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 16517ee7f66SAndreas Gohr 166f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 167f8cc712eSAndreas Gohr $info['isadmin'] = true; 168f8cc712eSAndreas Gohr $info['ismanager'] = true; 169f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 170f8cc712eSAndreas Gohr $info['ismanager'] = true; 171f8cc712eSAndreas Gohr } 172f8cc712eSAndreas Gohr 17317ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 17417ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 175585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 17617ee7f66SAndreas Gohr } 177ee4c4a1bSAndreas Gohr 178f3f0262cSandi } else { 1791015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 180ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 181f3f0262cSandi } 182f3f0262cSandi 1831015a57dSChristopher Smith $info['namespace'] = getNS($id); 1841015a57dSChristopher Smith 1851015a57dSChristopher Smith // mobile detection 1861015a57dSChristopher Smith if ($htmlClient) { 1871015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1881015a57dSChristopher Smith } 1891015a57dSChristopher Smith 1901015a57dSChristopher Smith return $info; 1911015a57dSChristopher Smith } 1921015a57dSChristopher Smith 1931015a57dSChristopher Smith/** 1941015a57dSChristopher Smith * Return info about the current document as associative 1951015a57dSChristopher Smith * array. 1961015a57dSChristopher Smith * 1971015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 198140cfbcdSGerrit Uitslag * 199140cfbcdSGerrit Uitslag * @return array with info about current document 2001015a57dSChristopher Smith */ 2011015a57dSChristopher Smithfunction pageinfo() { 2021015a57dSChristopher Smith global $ID; 2031015a57dSChristopher Smith global $REV; 2041015a57dSChristopher Smith global $RANGE; 2051015a57dSChristopher Smith global $lang; 206585bf44eSChristopher Smith /* @var Input $INPUT */ 207585bf44eSChristopher Smith global $INPUT; 2081015a57dSChristopher Smith 2091015a57dSChristopher Smith $info = basicinfo($ID); 2101015a57dSChristopher Smith 2111015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2121015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2131015a57dSChristopher Smith $info['id'] = $ID; 2141015a57dSChristopher Smith $info['rev'] = $REV; 2151015a57dSChristopher Smith 216585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 2177e87a794SChristopher Smith $sub = new Subscription(); 2187e87a794SChristopher Smith $info['subscribed'] = $sub->user_subscription(); 2197e87a794SChristopher Smith } else { 2207e87a794SChristopher Smith $info['subscribed'] = false; 2217e87a794SChristopher Smith } 2227e87a794SChristopher Smith 223f3f0262cSandi $info['locked'] = checklock($ID); 224317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 22579e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 22601c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2272ca9d91cSBen Coburn if($REV) { 2282ca9d91cSBen Coburn //check if current revision was meant 22901c9a118SAndreas Gohr if($info['exists'] && ($info['currentrev'] == $REV)) { 2302ca9d91cSBen Coburn $REV = ''; 2317b3a6803SAndreas Gohr } elseif($RANGE) { 2327b3a6803SAndreas Gohr //section editing does not work with old revisions! 2337b3a6803SAndreas Gohr $REV = ''; 2347b3a6803SAndreas Gohr $RANGE = ''; 2357b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2362ca9d91cSBen Coburn } else { 2372ca9d91cSBen Coburn //really use old revision 238317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 23979e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 240f3f0262cSandi } 241f3f0262cSandi } 242c112d578Sandi $info['rev'] = $REV; 243f3f0262cSandi if($info['exists']) { 244f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 245f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 246f3f0262cSandi } else { 247f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 248f3f0262cSandi } 24950e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 250f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 251f3f0262cSandi 25271726d78SBen Coburn //load page meta data 25371726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 25471726d78SBen Coburn 255652610a2Sandi //who's the editor 256047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 257652610a2Sandi if($REV) { 258f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 259652610a2Sandi } else { 2600e80bb5eSChristopher Smith if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 261aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 262aa27cf05SAndreas Gohr } else { 263f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 264cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 265cd00a034SBen Coburn if($revinfo !== false) { 266cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 267cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 268cd00a034SBen Coburn } 269cd00a034SBen Coburn } 270cd00a034SBen Coburn } 271cd00a034SBen Coburn //and check for an external edit 272cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 273cd00a034SBen Coburn // cached changelog line no longer valid 274cd00a034SBen Coburn $revinfo = false; 275cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 276cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 277652610a2Sandi } 278bb4866bdSchris 279652610a2Sandi $info['ip'] = $revinfo['ip']; 280652610a2Sandi $info['user'] = $revinfo['user']; 281652610a2Sandi $info['sum'] = $revinfo['sum']; 28271726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 283ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 28459f257aeSchris 28588f522e9Sandi if($revinfo['user']) { 28688f522e9Sandi $info['editor'] = $revinfo['user']; 28788f522e9Sandi } else { 28888f522e9Sandi $info['editor'] = $revinfo['ip']; 28988f522e9Sandi } 290652610a2Sandi 291ee4c4a1bSAndreas Gohr // draft 2920aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 2930aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 2940aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 295ee4c4a1bSAndreas Gohr } 296ee4c4a1bSAndreas Gohr 2971015a57dSChristopher Smith return $info; 2981015a57dSChristopher Smith} 2991015a57dSChristopher Smith 3001015a57dSChristopher Smith/** 3010c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3020c39d46cSMichael Große */ 3030c39d46cSMichael Großefunction jsinfo() { 3040c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3050c39d46cSMichael Große 3060c39d46cSMichael Große if (!is_array($JSINFO)) { 3070c39d46cSMichael Große $JSINFO = []; 3080c39d46cSMichael Große } 3090c39d46cSMichael Große //export minimal info to JS, plugins can add more 3100c39d46cSMichael Große $JSINFO['id'] = $ID; 3110c39d46cSMichael Große $JSINFO['namespace'] = (string) $INFO['namespace']; 3120c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3130c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3140c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3150c39d46cSMichael Große} 3160c39d46cSMichael Große 3170c39d46cSMichael Große/** 3181015a57dSChristopher Smith * Return information about the current media item as an associative array. 319140cfbcdSGerrit Uitslag * 320140cfbcdSGerrit Uitslag * @return array with info about current media item 3211015a57dSChristopher Smith */ 3221015a57dSChristopher Smithfunction mediainfo(){ 3231015a57dSChristopher Smith global $NS; 3241015a57dSChristopher Smith global $IMG; 3251015a57dSChristopher Smith 3261015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3271015a57dSChristopher Smith $info['image'] = $IMG; 3281c548ebeSAndreas Gohr 329f3f0262cSandi return $info; 330f3f0262cSandi} 331f3f0262cSandi 332f3f0262cSandi/** 3332684e50aSAndreas Gohr * Build an string of URL parameters 3342684e50aSAndreas Gohr * 3352684e50aSAndreas Gohr * @author Andreas Gohr 336140cfbcdSGerrit Uitslag * 337140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 338140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 339140cfbcdSGerrit Uitslag * @return string query string 3402684e50aSAndreas Gohr */ 341b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3422684e50aSAndreas Gohr $url = ''; 3432684e50aSAndreas Gohr $amp = false; 3442684e50aSAndreas Gohr foreach($params as $key => $val) { 345b174aeaeSchris if($amp) $url .= $sep; 3462684e50aSAndreas Gohr 34785e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3483a50618cSgweissbach $url .= rawurlencode((string) $val); 3492684e50aSAndreas Gohr $amp = true; 3502684e50aSAndreas Gohr } 3512684e50aSAndreas Gohr return $url; 3522684e50aSAndreas Gohr} 3532684e50aSAndreas Gohr 3542684e50aSAndreas Gohr/** 3552684e50aSAndreas Gohr * Build an string of html tag attributes 3562684e50aSAndreas Gohr * 3577bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3587bff22c0SAndreas Gohr * 3592684e50aSAndreas Gohr * @author Andreas Gohr 360140cfbcdSGerrit Uitslag * 361140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 362140cfbcdSGerrit Uitslag * @param bool $skipempty skip empty string values? 363140cfbcdSGerrit Uitslag * @return string 3642684e50aSAndreas Gohr */ 3654b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 3662684e50aSAndreas Gohr $url = ''; 3679063ec14SAdrian Lang $white = false; 3682684e50aSAndreas Gohr foreach($params as $key => $val) { 3697bff22c0SAndreas Gohr if($key{0} == '_') continue; 370b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 3719063ec14SAdrian Lang if($white) $url .= ' '; 3727bff22c0SAndreas Gohr 3732684e50aSAndreas Gohr $url .= $key.'="'; 3742684e50aSAndreas Gohr $url .= htmlspecialchars($val); 3752684e50aSAndreas Gohr $url .= '"'; 3769063ec14SAdrian Lang $white = true; 3772684e50aSAndreas Gohr } 3782684e50aSAndreas Gohr return $url; 3792684e50aSAndreas Gohr} 3802684e50aSAndreas Gohr 3812684e50aSAndreas Gohr/** 38215fae107Sandi * This builds the breadcrumb trail and returns it as array 38315fae107Sandi * 38415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 385140cfbcdSGerrit Uitslag * 386e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 387f3f0262cSandi */ 388f3f0262cSandifunction breadcrumbs() { 3898746e727Sandi // we prepare the breadcrumbs early for quick session closing 3908746e727Sandi static $crumbs = null; 3918746e727Sandi if($crumbs != null) return $crumbs; 3928746e727Sandi 393f3f0262cSandi global $ID; 394f3f0262cSandi global $ACT; 395f3f0262cSandi global $conf; 396f3f0262cSandi 397f3f0262cSandi //first visit? 398c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 3994d1fee4cSB_S666 //we only save on show and existing visible wiki documents 400a77f5846Sjan $file = wikiFN($ID); 4014d1fee4cSB_S666 if($ACT != 'show' || isHiddenPage($ID) || !file_exists($file)) { 402e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 403f3f0262cSandi return $crumbs; 404f3f0262cSandi } 405a77f5846Sjan 406a77f5846Sjan // page names 4071a84a0f3SAnika Henke $name = noNSorNS($ID); 408fe9ec250SChris Smith if(useHeading('navigation')) { 409a77f5846Sjan // get page title 41067c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 411a77f5846Sjan if($title) { 412a77f5846Sjan $name = $title; 413a77f5846Sjan } 414a77f5846Sjan } 415a77f5846Sjan 416f3f0262cSandi //remove ID from array 417a77f5846Sjan if(isset($crumbs[$ID])) { 418a77f5846Sjan unset($crumbs[$ID]); 419f3f0262cSandi } 420f3f0262cSandi 421f3f0262cSandi //add to array 422a77f5846Sjan $crumbs[$ID] = $name; 423f3f0262cSandi //reduce size 424f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 425f3f0262cSandi array_shift($crumbs); 426f3f0262cSandi } 427f3f0262cSandi //save to session 428e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 429f3f0262cSandi return $crumbs; 430f3f0262cSandi} 431f3f0262cSandi 432f3f0262cSandi/** 43315fae107Sandi * Filter for page IDs 43415fae107Sandi * 435f3f0262cSandi * This is run on a ID before it is outputted somewhere 436f3f0262cSandi * currently used to replace the colon with something else 437907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 438907f24f7SAndreas Gohr * 439907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 440907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 441907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 44215fae107Sandi * 44349c713a3Sandi * Urlencoding is ommitted when the second parameter is false 44449c713a3Sandi * 44515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 446140cfbcdSGerrit Uitslag * 447140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 448140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 449140cfbcdSGerrit Uitslag * @return string 450f3f0262cSandi */ 45149c713a3Sandifunction idfilter($id, $ue = true) { 452f3f0262cSandi global $conf; 453585bf44eSChristopher Smith /* @var Input $INPUT */ 454585bf44eSChristopher Smith global $INPUT; 455585bf44eSChristopher Smith 456f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 457f3f0262cSandi $id = strtr($id, ':', '/'); 458f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 45958bedc8aSborekb $conf['userewrite'] && 460585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4613272d797SAndreas Gohr ) { 462f3f0262cSandi $id = strtr($id, ':', ';'); 463f3f0262cSandi } 46449c713a3Sandi if($ue) { 465b6c6979fSAndreas Gohr $id = rawurlencode($id); 466f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 467edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 468f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 46949c713a3Sandi } 470f3f0262cSandi return $id; 471f3f0262cSandi} 472f3f0262cSandi 473f3f0262cSandi/** 474ed7b5f09Sandi * This builds a link to a wikipage 47515fae107Sandi * 4764bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4776c7843b5Sandi * 47815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4794bc480e5SAndreas Gohr * 4804bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4814bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4824bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4834bc480e5SAndreas Gohr * @param string $separator parameter separator 4844bc480e5SAndreas Gohr * @return string 485f3f0262cSandi */ 48616f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 487f3f0262cSandi global $conf; 48816f15a81SDominik Eckelmann if(is_array($urlParameters)) { 4894bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 49064159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 49164159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 49264159a61SAndreas Gohr } 49316f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4946de3759aSAndreas Gohr } else { 49516f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4966de3759aSAndreas Gohr } 49716f15a81SDominik Eckelmann if($id === '') { 49816f15a81SDominik Eckelmann $id = $conf['start']; 49916f15a81SDominik Eckelmann } 500f3f0262cSandi $id = idfilter($id); 50116f15a81SDominik Eckelmann if($absolute) { 502ed7b5f09Sandi $xlink = DOKU_URL; 503ed7b5f09Sandi } else { 504ed7b5f09Sandi $xlink = DOKU_BASE; 505ed7b5f09Sandi } 506f3f0262cSandi 5076c7843b5Sandi if($conf['userewrite'] == 2) { 5086c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 50916f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5106c7843b5Sandi } elseif($conf['userewrite']) { 511f3f0262cSandi $xlink .= $id; 51216f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 513bce3726dSAndreas Gohr } elseif($id) { 5146c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 51516f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 516bce3726dSAndreas Gohr } else { 517bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 51816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 519f3f0262cSandi } 520f3f0262cSandi 521f3f0262cSandi return $xlink; 522f3f0262cSandi} 523f3f0262cSandi 524f3f0262cSandi/** 525f5c2808fSBen Coburn * This builds a link to an alternate page format 526f5c2808fSBen Coburn * 527f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 528f5c2808fSBen Coburn * 529f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5304bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5314bc480e5SAndreas Gohr * @param string $format the export renderer to use 5324bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5334bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5344bc480e5SAndreas Gohr * @param string $sep parameter separator 5354bc480e5SAndreas Gohr * @return string 536f5c2808fSBen Coburn */ 5374bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 538f5c2808fSBen Coburn global $conf; 5394bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5404bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 541f5c2808fSBen Coburn } else { 5424bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 543f5c2808fSBen Coburn } 544f5c2808fSBen Coburn 545f5c2808fSBen Coburn $format = rawurlencode($format); 546f5c2808fSBen Coburn $id = idfilter($id); 547f5c2808fSBen Coburn if($abs) { 548f5c2808fSBen Coburn $xlink = DOKU_URL; 549f5c2808fSBen Coburn } else { 550f5c2808fSBen Coburn $xlink = DOKU_BASE; 551f5c2808fSBen Coburn } 552f5c2808fSBen Coburn 553f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 554f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5554bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 556f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 557f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5584bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 559f5c2808fSBen Coburn } else { 560f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5614bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 562f5c2808fSBen Coburn } 563f5c2808fSBen Coburn 564f5c2808fSBen Coburn return $xlink; 565f5c2808fSBen Coburn} 566f5c2808fSBen Coburn 567f5c2808fSBen Coburn/** 5686de3759aSAndreas Gohr * Build a link to a media file 5696de3759aSAndreas Gohr * 5706de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5718c08db0aSAndreas Gohr * 5728c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5738c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5748c08db0aSAndreas Gohr * 5753272d797SAndreas Gohr * @param string $id the media file id or URL 5763272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5773272d797SAndreas Gohr * @param bool $direct link to detail page if false 5783272d797SAndreas Gohr * @param string $sep URL parameter separator 5793272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5803272d797SAndreas Gohr * @return string 5816de3759aSAndreas Gohr */ 58255b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5836de3759aSAndreas Gohr global $conf; 584b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 585826d2766SKlap-in if(!$isexternalimage) { 586826d2766SKlap-in $id = cleanID($id); 587826d2766SKlap-in } 588826d2766SKlap-in 5896de3759aSAndreas Gohr if(is_array($more)) { 5900f4e0092SChristopher Smith // add token for resized images 591443e135dSChristopher Smith if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){ 5920f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 5930f4e0092SChristopher Smith } 5948c08db0aSAndreas Gohr // strip defaults for shorter URLs 5958c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 596443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 597443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 5988c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 59978b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 600b174aeaeSchris $more = buildURLparams($more, $sep); 6016de3759aSAndreas Gohr } else { 6025e7db1e2SChristopher Smith $matches = array(); 603cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6045e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6055e7db1e2SChristopher Smith foreach ($matches as $match){ 6065e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6075e7db1e2SChristopher Smith } 608cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 609cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6105e7db1e2SChristopher Smith } 6118c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6128c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 613b174aeaeSchris $more = str_replace(',', $sep, $more); 6146de3759aSAndreas Gohr } 6156de3759aSAndreas Gohr 61655b2b31bSAndreas Gohr if($abs) { 61755b2b31bSAndreas Gohr $xlink = DOKU_URL; 61855b2b31bSAndreas Gohr } else { 6196de3759aSAndreas Gohr $xlink = DOKU_BASE; 62055b2b31bSAndreas Gohr } 6216de3759aSAndreas Gohr 6226de3759aSAndreas Gohr // external URLs are always direct without rewriting 623826d2766SKlap-in if($isexternalimage) { 6246de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 625cc036f74SKlap-in $xlink .= '?'.$more; 626b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6276de3759aSAndreas Gohr return $xlink; 6286de3759aSAndreas Gohr } 6296de3759aSAndreas Gohr 6306de3759aSAndreas Gohr $id = idfilter($id); 6316de3759aSAndreas Gohr 6326de3759aSAndreas Gohr // decide on scriptname 6336de3759aSAndreas Gohr if($direct) { 6346de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6356de3759aSAndreas Gohr $script = '_media'; 6366de3759aSAndreas Gohr } else { 6376de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6386de3759aSAndreas Gohr } 6396de3759aSAndreas Gohr } else { 6406de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6416de3759aSAndreas Gohr $script = '_detail'; 6426de3759aSAndreas Gohr } else { 6436de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6446de3759aSAndreas Gohr } 6456de3759aSAndreas Gohr } 6466de3759aSAndreas Gohr 6476de3759aSAndreas Gohr // build URL based on rewrite mode 6486de3759aSAndreas Gohr if($conf['userewrite']) { 6496de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6506de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6516de3759aSAndreas Gohr } else { 6526de3759aSAndreas Gohr if($more) { 653a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 654b174aeaeSchris $xlink .= $sep.'media='.$id; 6556de3759aSAndreas Gohr } else { 656a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6576de3759aSAndreas Gohr } 6586de3759aSAndreas Gohr } 6596de3759aSAndreas Gohr 6606de3759aSAndreas Gohr return $xlink; 6616de3759aSAndreas Gohr} 6626de3759aSAndreas Gohr 6636de3759aSAndreas Gohr/** 66425ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 66515fae107Sandi * 66625ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 66725ca5b17SAndreas Gohr * 66815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 669140cfbcdSGerrit Uitslag * 670140cfbcdSGerrit Uitslag * @return string 671f3f0262cSandi */ 67225ca5b17SAndreas Gohrfunction script() { 673ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 674f3f0262cSandi} 675f3f0262cSandi 676f3f0262cSandi/** 67715fae107Sandi * Spamcheck against wordlist 67815fae107Sandi * 679f3f0262cSandi * Checks the wikitext against a list of blocked expressions 680f3f0262cSandi * returns true if the text contains any bad words 68115fae107Sandi * 682e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 683e403cc58SMichael Klier * 684e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 685e403cc58SMichael Klier * and gain information about the user who was blocked. 686e403cc58SMichael Klier * 687e403cc58SMichael Klier * Event data: 688e403cc58SMichael Klier * data['matches'] - array of matches 689e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 690e403cc58SMichael Klier * [ip] - ip address 691e403cc58SMichael Klier * [user] - username (if logged in) 692e403cc58SMichael Klier * [mail] - mail address (if logged in) 693e403cc58SMichael Klier * [name] - real name (if logged in) 694e403cc58SMichael Klier * 69515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6966dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 697140cfbcdSGerrit Uitslag * 6986dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 6996dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 700f3f0262cSandi */ 7016dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 702f3f0262cSandi global $TEXT; 7036dffa0e0SAndreas Gohr global $PRE; 7046dffa0e0SAndreas Gohr global $SUF; 705e0086ca2SAndreas Gohr global $SUM; 706f3f0262cSandi global $conf; 707e403cc58SMichael Klier global $INFO; 708585bf44eSChristopher Smith /* @var Input $INPUT */ 709585bf44eSChristopher Smith global $INPUT; 710f3f0262cSandi 711f3f0262cSandi if(!$conf['usewordblock']) return false; 712f3f0262cSandi 713e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7146dffa0e0SAndreas Gohr 715041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 71664159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 71764159a61SAndreas Gohr $text = preg_replace( 71864159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 71964159a61SAndreas Gohr '\1http://\2 \2\3', 72064159a61SAndreas Gohr $text 72164159a61SAndreas Gohr ); 72264159a61SAndreas Gohr // phpcs:enable 723041d1964SAndreas Gohr 724b9ac8716Schris $wordblocks = getWordblocks(); 7253e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 7263e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 7273e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 7283e2965d7Sandi // backreferences are used - the maximum is 99 7293e2965d7Sandi // this is very bad performancewise and may even be too high still 7303e2965d7Sandi $chunksize = 40; 7313e2965d7Sandi } else { 732a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7333e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 734a51d08efSAndreas Gohr $chunksize = 200; 7353e2965d7Sandi } 736b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 737f3f0262cSandi $re = array(); 73849eb6e38SAndreas Gohr // build regexp from blocks 739f3f0262cSandi foreach($blocks as $block) { 740f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 741f3f0262cSandi $block = trim($block); 742f3f0262cSandi if(empty($block)) continue; 743f3f0262cSandi $re[] = $block; 744f3f0262cSandi } 745e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 746e403cc58SMichael Klier // prepare event data 74759bc3b48SGerrit Uitslag $data = array(); 748e403cc58SMichael Klier $data['matches'] = $matches; 749585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 750585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 751585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 752e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 753e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 754e403cc58SMichael Klier } 755bad6fc0dSAndreas Gohr $callback = function () { 756bad6fc0dSAndreas Gohr return true; 757bad6fc0dSAndreas Gohr }; 758e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 759b9ac8716Schris } 760703f6fdeSandi } 761f3f0262cSandi return false; 762f3f0262cSandi} 763f3f0262cSandi 764f3f0262cSandi/** 76515fae107Sandi * Return the IP of the client 76615fae107Sandi * 7676d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 76815fae107Sandi * 7696d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 7706d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 7716d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 7726d8affe6SAndreas Gohr * headers 7736d8affe6SAndreas Gohr * 77415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 775140cfbcdSGerrit Uitslag * 7763272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 7773272d797SAndreas Gohr * @return string 778f3f0262cSandi */ 7796d8affe6SAndreas Gohrfunction clientIP($single = false) { 780585bf44eSChristopher Smith /* @var Input $INPUT */ 781585bf44eSChristopher Smith global $INPUT; 782585bf44eSChristopher Smith 7836d8affe6SAndreas Gohr $ip = array(); 784585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 785585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 786585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 787585bf44eSChristopher Smith } 788585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 789585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 790585bf44eSChristopher Smith } 7916d8affe6SAndreas Gohr 792dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 793dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 794dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 795dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 796dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 797dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 798dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 799dc14c6d1SGuy Brand $IPv6Address = 800dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 801dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 802dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 803dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 804dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 805dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 806dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 807dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 808dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 809dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 810dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 811dc14c6d1SGuy Brand 8126d8affe6SAndreas Gohr // remove any non-IP stuff 8136d8affe6SAndreas Gohr $cnt = count($ip); 8144ff28443Schris $match = array(); 8156d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 816dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 8174ff28443Schris $ip[$i] = $match[0]; 8184ff28443Schris } else { 8194ff28443Schris $ip[$i] = ''; 8204ff28443Schris } 8216d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 822f3f0262cSandi } 8236d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 8246d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8256d8affe6SAndreas Gohr 8266d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 8276d8affe6SAndreas Gohr 8286d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 8296d8affe6SAndreas Gohr $ip = array_reverse($ip); 8306d8affe6SAndreas Gohr foreach($ip as $i) { 8312343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 8326d8affe6SAndreas Gohr continue; 8336d8affe6SAndreas Gohr } else { 8346d8affe6SAndreas Gohr return $i; 8356d8affe6SAndreas Gohr } 8366d8affe6SAndreas Gohr } 8376d8affe6SAndreas Gohr // still here? just use the first (last) address 8386d8affe6SAndreas Gohr return $ip[0]; 839f3f0262cSandi} 840f3f0262cSandi 841f3f0262cSandi/** 8421c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8431c548ebeSAndreas Gohr * 8441c548ebeSAndreas Gohr * Adapted from the example code at url below 8451c548ebeSAndreas Gohr * 8461c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 847140cfbcdSGerrit Uitslag * 84864159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 849140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8501c548ebeSAndreas Gohr */ 8511c548ebeSAndreas Gohrfunction clientismobile() { 852585bf44eSChristopher Smith /* @var Input $INPUT */ 853585bf44eSChristopher Smith global $INPUT; 8541c548ebeSAndreas Gohr 855585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8561c548ebeSAndreas Gohr 857585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8581c548ebeSAndreas Gohr 859585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8601c548ebeSAndreas Gohr 86164159a61SAndreas Gohr $uamatches = join( 86264159a61SAndreas Gohr '|', 86364159a61SAndreas Gohr [ 86464159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 86564159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 86664159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 86764159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 86864159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 86964159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 87064159a61SAndreas Gohr '\d\d\di', 'moto' 87164159a61SAndreas Gohr ] 87264159a61SAndreas Gohr ); 8731c548ebeSAndreas Gohr 874585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8751c548ebeSAndreas Gohr 8761c548ebeSAndreas Gohr return false; 8771c548ebeSAndreas Gohr} 8781c548ebeSAndreas Gohr 8791c548ebeSAndreas Gohr/** 8806efc45a2SDmitry Katsubo * check if a given link is interwiki link 8816efc45a2SDmitry Katsubo * 8826efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8836efc45a2SDmitry Katsubo * @return bool 8846efc45a2SDmitry Katsubo */ 8856efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 8866efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 8876efc45a2SDmitry Katsubo return false; 8886efc45a2SDmitry Katsubo} 8896efc45a2SDmitry Katsubo 8906efc45a2SDmitry Katsubo/** 89163211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 89263211f61SGlen Harris * 89322ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 89422ef1e32SAndreas Gohr * 89563211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 896140cfbcdSGerrit Uitslag * 8973272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 8983272d797SAndreas Gohr * @return string a comma separated list of hostnames 89963211f61SGlen Harris */ 90063211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 90122ef1e32SAndreas Gohr global $conf; 90222ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 90322ef1e32SAndreas Gohr 90463211f61SGlen Harris $hosts = array(); 90563211f61SGlen Harris $ips = explode(',', $ips); 906551a720fSMichael Klier 907551a720fSMichael Klier if(is_array($ips)) { 9083886270dSAndreas Gohr foreach($ips as $ip) { 909551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 91063211f61SGlen Harris } 911551a720fSMichael Klier return join(',', $hosts); 912551a720fSMichael Klier } else { 913551a720fSMichael Klier return gethostbyaddr(trim($ips)); 914551a720fSMichael Klier } 91563211f61SGlen Harris} 91663211f61SGlen Harris 91763211f61SGlen Harris/** 91815fae107Sandi * Checks if a given page is currently locked. 91915fae107Sandi * 920f3f0262cSandi * removes stale lockfiles 92115fae107Sandi * 92215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 923140cfbcdSGerrit Uitslag * 924140cfbcdSGerrit Uitslag * @param string $id page id 925140cfbcdSGerrit Uitslag * @return bool page is locked? 926f3f0262cSandi */ 927f3f0262cSandifunction checklock($id) { 928f3f0262cSandi global $conf; 929585bf44eSChristopher Smith /* @var Input $INPUT */ 930585bf44eSChristopher Smith global $INPUT; 931585bf44eSChristopher Smith 932c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 933f3f0262cSandi 934f3f0262cSandi //no lockfile 93579e79377SAndreas Gohr if(!file_exists($lock)) return false; 936f3f0262cSandi 937f3f0262cSandi //lockfile expired 938f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 939d8186216SBen Coburn @unlink($lock); 940f3f0262cSandi return false; 941f3f0262cSandi } 942f3f0262cSandi 943f3f0262cSandi //my own lock 9446d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 9450712fefaSAndreas Gohr if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) { 946f3f0262cSandi return false; 947f3f0262cSandi } 948f3f0262cSandi 949f3f0262cSandi return $ip; 950f3f0262cSandi} 951f3f0262cSandi 952f3f0262cSandi/** 95315fae107Sandi * Lock a page for editing 95415fae107Sandi * 95515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 956140cfbcdSGerrit Uitslag * 957140cfbcdSGerrit Uitslag * @param string $id page id to lock 958f3f0262cSandi */ 959f3f0262cSandifunction lock($id) { 960544ed901SDaniel Calviño Sánchez global $conf; 961585bf44eSChristopher Smith /* @var Input $INPUT */ 962585bf44eSChristopher Smith global $INPUT; 963544ed901SDaniel Calviño Sánchez 964544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 965544ed901SDaniel Calviño Sánchez return; 966544ed901SDaniel Calviño Sánchez } 967544ed901SDaniel Calviño Sánchez 968c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 969585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 970585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 971f3f0262cSandi } else { 97285fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 973f3f0262cSandi } 974f3f0262cSandi} 975f3f0262cSandi 976f3f0262cSandi/** 97715fae107Sandi * Unlock a page if it was locked by the user 978f3f0262cSandi * 97915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 980140cfbcdSGerrit Uitslag * 9813272d797SAndreas Gohr * @param string $id page id to unlock 98215fae107Sandi * @return bool true if a lock was removed 983f3f0262cSandi */ 984f3f0262cSandifunction unlock($id) { 985585bf44eSChristopher Smith /* @var Input $INPUT */ 986585bf44eSChristopher Smith global $INPUT; 987585bf44eSChristopher Smith 988c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 98979e79377SAndreas Gohr if(file_exists($lock)) { 9906d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 991585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 992f3f0262cSandi @unlink($lock); 993f3f0262cSandi return true; 994f3f0262cSandi } 995f3f0262cSandi } 996f3f0262cSandi return false; 997f3f0262cSandi} 998f3f0262cSandi 999f3f0262cSandi/** 1000f3f0262cSandi * convert line ending to unix format 1001f3f0262cSandi * 10026db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 10036db7468bSAndreas Gohr * 100415fae107Sandi * @see formText() for 2crlf conversion 100515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1006140cfbcdSGerrit Uitslag * 1007140cfbcdSGerrit Uitslag * @param string $text 1008140cfbcdSGerrit Uitslag * @return string 1009f3f0262cSandi */ 1010f3f0262cSandifunction cleanText($text) { 1011f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10126db7468bSAndreas Gohr 10136db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10146db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10156db7468bSAndreas Gohr // but might actually fix the problem in many cases 10166db7468bSAndreas Gohr if(!utf8_check($text)) $text = utf8_encode($text); 10176db7468bSAndreas Gohr 1018f3f0262cSandi return $text; 1019f3f0262cSandi} 1020f3f0262cSandi 1021f3f0262cSandi/** 1022f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1023f3f0262cSandi * It also converts line endings to Windows format which is 1024f3f0262cSandi * pseudo standard for webforms. 1025f3f0262cSandi * 102615fae107Sandi * @see cleanText() for 2unix conversion 102715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1028140cfbcdSGerrit Uitslag * 1029140cfbcdSGerrit Uitslag * @param string $text 1030140cfbcdSGerrit Uitslag * @return string 1031f3f0262cSandi */ 1032f3f0262cSandifunction formText($text) { 10335b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 1034f3f0262cSandi return htmlspecialchars($text); 1035f3f0262cSandi} 1036f3f0262cSandi 1037f3f0262cSandi/** 103815fae107Sandi * Returns the specified local text in raw format 103915fae107Sandi * 104015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1041140cfbcdSGerrit Uitslag * 1042140cfbcdSGerrit Uitslag * @param string $id page id 1043140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1044140cfbcdSGerrit Uitslag * @return string 1045f3f0262cSandi */ 10462adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10472adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1048f3f0262cSandi} 1049f3f0262cSandi 1050f3f0262cSandi/** 1051f3f0262cSandi * Returns the raw WikiText 105215fae107Sandi * 105315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1054140cfbcdSGerrit Uitslag * 1055140cfbcdSGerrit Uitslag * @param string $id page id 1056e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1057140cfbcdSGerrit Uitslag * @return string 1058f3f0262cSandi */ 1059f3f0262cSandifunction rawWiki($id, $rev = '') { 1060cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1061f3f0262cSandi} 1062f3f0262cSandi 1063f3f0262cSandi/** 10647146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10657146cee2SAndreas Gohr * 10667b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10677146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1068140cfbcdSGerrit Uitslag * 1069140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1070140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10717146cee2SAndreas Gohr */ 1072fe17917eSAdrian Langfunction pageTemplate($id) { 1073a15ce62dSEsther Brunner global $conf; 1074e29549feSAndreas Gohr 1075fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1076e29549feSAndreas Gohr 10777b84afa2SAndreas Gohr // prepare initial event data 10787b84afa2SAndreas Gohr $data = array( 10797b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10807b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10817b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10827b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10837b84afa2SAndreas Gohr ); 10847b84afa2SAndreas Gohr 10857b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 10867b84afa2SAndreas Gohr if($evt->advise_before(true)) { 10877b84afa2SAndreas Gohr // the before event might have loaded the content already 10887b84afa2SAndreas Gohr if(empty($data['tpl'])) { 10897b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10907b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1091fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 109279e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 10937b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1094e29549feSAndreas Gohr } else { 1095e29549feSAndreas Gohr // search upper namespaces for templates 1096e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1097e29549feSAndreas Gohr while(strlen($path) >= $len) { 109879e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 10997b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1100e29549feSAndreas Gohr break; 1101e29549feSAndreas Gohr } 1102e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1103e29549feSAndreas Gohr } 1104e29549feSAndreas Gohr } 11057b84afa2SAndreas Gohr } 11067b84afa2SAndreas Gohr // load the content 11073d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 11087b84afa2SAndreas Gohr } 1109a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 11107b84afa2SAndreas Gohr } 11117b84afa2SAndreas Gohr $evt->advise_after(); 11127b84afa2SAndreas Gohr unset($evt); 11137b84afa2SAndreas Gohr 1114fe17917eSAdrian Lang return $data['tpl']; 11152b1223ecSAdrian Lang} 11162b1223ecSAdrian Lang 11172b1223ecSAdrian Lang/** 11182b1223ecSAdrian Lang * Performs common page template replacements 11197b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11202b1223ecSAdrian Lang * 11212b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1122140cfbcdSGerrit Uitslag * 1123140cfbcdSGerrit Uitslag * @param array $data array with event data 1124140cfbcdSGerrit Uitslag * @return string 11252b1223ecSAdrian Lang */ 1126d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11273272d797SAndreas Gohr /** 11283272d797SAndreas Gohr * @var string $id the id of the page to be created 11293272d797SAndreas Gohr * @var string $tpl the text used as template 11303272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11313272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11323272d797SAndreas Gohr */ 1133fe17917eSAdrian Lang extract($data); 1134fe17917eSAdrian Lang 1135b856f7dfSAdrian Lang global $USERINFO; 1136bce53b1fSAdrian Lang global $conf; 1137585bf44eSChristopher Smith /* @var Input $INPUT */ 1138585bf44eSChristopher Smith global $INPUT; 1139e29549feSAndreas Gohr 1140e29549feSAndreas Gohr // replace placeholders 114126ece5a7SAndreas Gohr $file = noNS($id); 114237c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 114326ece5a7SAndreas Gohr 11443272d797SAndreas Gohr $tpl = str_replace( 11453272d797SAndreas Gohr array( 114626ece5a7SAndreas Gohr '@ID@', 114726ece5a7SAndreas Gohr '@NS@', 11488a7bcf66SShota Miyazaki '@CURNS@', 114926ece5a7SAndreas Gohr '@FILE@', 115026ece5a7SAndreas Gohr '@!FILE@', 115126ece5a7SAndreas Gohr '@!FILE!@', 115226ece5a7SAndreas Gohr '@PAGE@', 115326ece5a7SAndreas Gohr '@!PAGE@', 115426ece5a7SAndreas Gohr '@!!PAGE@', 115526ece5a7SAndreas Gohr '@!PAGE!@', 115626ece5a7SAndreas Gohr '@USER@', 115726ece5a7SAndreas Gohr '@NAME@', 115826ece5a7SAndreas Gohr '@MAIL@', 115926ece5a7SAndreas Gohr '@DATE@', 116026ece5a7SAndreas Gohr ), 116126ece5a7SAndreas Gohr array( 116226ece5a7SAndreas Gohr $id, 116326ece5a7SAndreas Gohr getNS($id), 11648a7bcf66SShota Miyazaki curNS($id), 116526ece5a7SAndreas Gohr $file, 116626ece5a7SAndreas Gohr utf8_ucfirst($file), 116726ece5a7SAndreas Gohr utf8_strtoupper($file), 116826ece5a7SAndreas Gohr $page, 116926ece5a7SAndreas Gohr utf8_ucfirst($page), 117026ece5a7SAndreas Gohr utf8_ucwords($page), 117126ece5a7SAndreas Gohr utf8_strtoupper($page), 1172585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 1173b856f7dfSAdrian Lang $USERINFO['name'], 1174b856f7dfSAdrian Lang $USERINFO['mail'], 117526ece5a7SAndreas Gohr $conf['dformat'], 11763272d797SAndreas Gohr ), $tpl 11773272d797SAndreas Gohr ); 117826ece5a7SAndreas Gohr 11797d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1180bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1181bad6fc0dSAndreas Gohr '/%./', 1182bad6fc0dSAndreas Gohr function ($m) { 1183bad6fc0dSAndreas Gohr return strftime($m[0]); 1184bad6fc0dSAndreas Gohr }, 1185bad6fc0dSAndreas Gohr $tpl 1186bad6fc0dSAndreas Gohr ); 1187d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1188a15ce62dSEsther Brunner return $tpl; 11897146cee2SAndreas Gohr} 11907146cee2SAndreas Gohr 11917146cee2SAndreas Gohr/** 119215fae107Sandi * Returns the raw Wiki Text in three slices. 119315fae107Sandi * 119415fae107Sandi * The range parameter needs to have the form "from-to" 119515cfe303Sandi * and gives the range of the section in bytes - no 119615cfe303Sandi * UTF-8 awareness is needed. 1197f3f0262cSandi * The returned order is prefix, section and suffix. 119815fae107Sandi * 119915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1200140cfbcdSGerrit Uitslag * 1201140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1202140cfbcdSGerrit Uitslag * @param string $id page id 1203140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 120442ea7f44SGerrit Uitslag * @return string[] with three slices 1205f3f0262cSandi */ 1206f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1207cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1208f3f0262cSandi 120980fcb268SAdrian Lang // Parse range 121080fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 121180fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 121280fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 121380fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 121480fcb268SAdrian Lang 121559bc3b48SGerrit Uitslag $slices = array(); 121680fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 121780fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 121815cfe303Sandi $slices[2] = substr($text, $to); 1219f3f0262cSandi return $slices; 1220f3f0262cSandi} 1221f3f0262cSandi 1222f3f0262cSandi/** 122315fae107Sandi * Joins wiki text slices 122415fae107Sandi * 122580fcb268SAdrian Lang * function to join the text slices. 1226f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1227f3f0262cSandi * lines between sections if needed (used on saving). 122815fae107Sandi * 122915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1230140cfbcdSGerrit Uitslag * 1231140cfbcdSGerrit Uitslag * @param string $pre prefix 1232140cfbcdSGerrit Uitslag * @param string $text text in the middle 1233140cfbcdSGerrit Uitslag * @param string $suf suffix 1234140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1235140cfbcdSGerrit Uitslag * @return string 1236f3f0262cSandi */ 1237f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1238f3f0262cSandi if($pretty) { 123980fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12403272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12413272d797SAndreas Gohr ) { 124280fcb268SAdrian Lang $pre .= "\n"; 124380fcb268SAdrian Lang } 124480fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12453272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12463272d797SAndreas Gohr ) { 124780fcb268SAdrian Lang $text .= "\n"; 124880fcb268SAdrian Lang } 1249f3f0262cSandi } 1250f3f0262cSandi 1251f3f0262cSandi return $pre.$text.$suf; 1252f3f0262cSandi} 1253f3f0262cSandi 1254f3f0262cSandi/** 1255b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1256b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1257b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1258b24d9195SAndreas Gohr * 1259b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1260b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1261b24d9195SAndreas Gohr * 1262b24d9195SAndreas Gohr * @param string $id the page ID 1263b24d9195SAndreas Gohr */ 1264b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 1265b24d9195SAndreas Gohr global $lang; 1266b24d9195SAndreas Gohr 12678c7319beSGerrit Uitslag $fileLastMod = wikiFN($id); 12688c7319beSGerrit Uitslag $lastMod = @filemtime($fileLastMod); // from page 1269b24d9195SAndreas Gohr $pagelog = new PageChangeLog($id, 1024); 12708c7319beSGerrit Uitslag $lastRev = $pagelog->getRevisions(-1, 1); // from changelog 12718c7319beSGerrit Uitslag $lastRev = (int) (empty($lastRev) ? 0 : $lastRev[0]); 1272b24d9195SAndreas Gohr 12738c7319beSGerrit Uitslag if(!file_exists(wikiFN($id, $lastMod)) && file_exists($fileLastMod) && $lastMod >= $lastRev) { 1274b24d9195SAndreas Gohr // add old revision to the attic if missing 1275b24d9195SAndreas Gohr saveOldRevision($id); 1276b24d9195SAndreas Gohr // add a changelog entry if this edit came from outside dokuwiki 12778c7319beSGerrit Uitslag if($lastMod > $lastRev) { 12788c7319beSGerrit Uitslag $fileLastRev = wikiFN($id, $lastRev); 12798c7319beSGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($lastRev); 12803c48b1d0SGerrit Uitslag if(empty($lastRev) || !file_exists($fileLastRev) || $revinfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 12814b5aebc1SGerrit Uitslag $filesize_old = 0; 12824b5aebc1SGerrit Uitslag } else { 12838c7319beSGerrit Uitslag $filesize_old = io_getSizeFile($fileLastRev); 12844b5aebc1SGerrit Uitslag } 12858c7319beSGerrit Uitslag $filesize_new = filesize($fileLastMod); 12862966355bSGerrit Uitslag $sizechange = $filesize_new - $filesize_old; 12872966355bSGerrit Uitslag 128864159a61SAndreas Gohr addLogEntry( 128964159a61SAndreas Gohr $lastMod, 129064159a61SAndreas Gohr $id, 129164159a61SAndreas Gohr DOKU_CHANGE_TYPE_EDIT, 129264159a61SAndreas Gohr $lang['external_edit'], 129364159a61SAndreas Gohr '', 129464159a61SAndreas Gohr array('ExternalEdit' => true), 129564159a61SAndreas Gohr $sizechange 129664159a61SAndreas Gohr ); 1297b24d9195SAndreas Gohr // remove soon to be stale instructions 12980db5771eSMichael Große $cache = new CacheInstructions($id, $fileLastMod); 1299b24d9195SAndreas Gohr $cache->removeCache(); 1300b24d9195SAndreas Gohr } 1301b24d9195SAndreas Gohr } 1302b24d9195SAndreas Gohr} 1303b24d9195SAndreas Gohr 1304b24d9195SAndreas Gohr/** 1305a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1306a701424fSBen Coburn * Also directs changelog and attic updates. 130715fae107Sandi * 130815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 130971726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1310140cfbcdSGerrit Uitslag * 1311140cfbcdSGerrit Uitslag * @param string $id page id 1312140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1313140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1314140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1315f3f0262cSandi */ 1316b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1317a701424fSBen Coburn /* Note to developers: 1318a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1319a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1320a701424fSBen Coburn after any changes. External edits change the wiki page 1321a701424fSBen Coburn directly without using php or dokuwiki. 1322a701424fSBen Coburn */ 1323f3f0262cSandi global $conf; 1324f3f0262cSandi global $lang; 132571726d78SBen Coburn global $REV; 1326585bf44eSChristopher Smith /* @var Input $INPUT */ 1327585bf44eSChristopher Smith global $INPUT; 1328585bf44eSChristopher Smith 1329b24d9195SAndreas Gohr // prepare data for event 1330b24d9195SAndreas Gohr $svdta = array(); 1331b24d9195SAndreas Gohr $svdta['id'] = $id; 1332b24d9195SAndreas Gohr $svdta['file'] = wikiFN($id); 1333b24d9195SAndreas Gohr $svdta['revertFrom'] = $REV; 1334b24d9195SAndreas Gohr $svdta['oldRevision'] = @filemtime($svdta['file']); 1335b24d9195SAndreas Gohr $svdta['newRevision'] = 0; 1336b24d9195SAndreas Gohr $svdta['newContent'] = $text; 1337b24d9195SAndreas Gohr $svdta['oldContent'] = rawWiki($id); 1338b24d9195SAndreas Gohr $svdta['summary'] = $summary; 1339b24d9195SAndreas Gohr $svdta['contentChanged'] = ($svdta['newContent'] != $svdta['oldContent']); 1340b24d9195SAndreas Gohr $svdta['changeInfo'] = ''; 1341b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_EDIT; 13422966355bSGerrit Uitslag $svdta['sizechange'] = null; 1343b24d9195SAndreas Gohr 1344b24d9195SAndreas Gohr // select changelog line type 1345b24d9195SAndreas Gohr if($REV) { 1346b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_REVERT; 1347b24d9195SAndreas Gohr $svdta['changeInfo'] = $REV; 1348b24d9195SAndreas Gohr } else if(!file_exists($svdta['file'])) { 1349b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_CREATE; 1350b24d9195SAndreas Gohr } else if(trim($text) == '') { 1351b24d9195SAndreas Gohr // empty or whitespace only content deletes 1352b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_DELETE; 1353b24d9195SAndreas Gohr // autoset summary on deletion 1354655ddc1dSGerrit Uitslag if(blank($svdta['summary'])) { 1355655ddc1dSGerrit Uitslag $svdta['summary'] = $lang['deleted']; 1356655ddc1dSGerrit Uitslag } 1357b24d9195SAndreas Gohr } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 1358b24d9195SAndreas Gohr //minor edits only for logged in users 1359b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_MINOR_EDIT; 1360f3f0262cSandi } 1361f3f0262cSandi 1362b24d9195SAndreas Gohr $event = new Doku_Event('COMMON_WIKIPAGE_SAVE', $svdta); 1363b24d9195SAndreas Gohr if(!$event->advise_before()) return; 1364f3f0262cSandi 1365b24d9195SAndreas Gohr // if the content has not been changed, no save happens (plugins may override this) 1366b24d9195SAndreas Gohr if(!$svdta['contentChanged']) return; 1367b24d9195SAndreas Gohr 1368b24d9195SAndreas Gohr detectExternalEdit($id); 1369f3f0262cSandi 13704b5aebc1SGerrit Uitslag if( 13714b5aebc1SGerrit Uitslag $svdta['changeType'] == DOKU_CHANGE_TYPE_CREATE || 13724b5aebc1SGerrit Uitslag ($svdta['changeType'] == DOKU_CHANGE_TYPE_REVERT && !file_exists($svdta['file'])) 13734b5aebc1SGerrit Uitslag ) { 1374ac3ed4afSGerrit Uitslag $filesize_old = 0; 1375ac3ed4afSGerrit Uitslag } else { 13762966355bSGerrit Uitslag $filesize_old = filesize($svdta['file']); 1377ac3ed4afSGerrit Uitslag } 1378b24d9195SAndreas Gohr if($svdta['changeType'] == DOKU_CHANGE_TYPE_DELETE) { 137930725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 1380b24d9195SAndreas Gohr $data = array(array($svdta['file'], '', false), getNS($id), noNS($id), false); 138130725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1382e45b34cdSBen Coburn // pre-save deleted revision 1383b24d9195SAndreas Gohr @touch($svdta['file']); 138446844156SBen Coburn clearstatcache(); 13852d69eb44SMichael Hamann $svdta['newRevision'] = saveOldRevision($id); 1386e1f3d9e1SEsther Brunner // remove empty file 1387b24d9195SAndreas Gohr @unlink($svdta['file']); 1388ac3ed4afSGerrit Uitslag $filesize_new = 0; 138964159a61SAndreas Gohr // don't remove old meta info as it should be saved, plugins can use 139064159a61SAndreas Gohr // IO_WIKIPAGE_WRITE for removing their metadata... 1391c5f92742SMichael Hamann // purge non-persistant meta data 13923d1f9ec3SMichael Klier p_purge_metadata($id); 139353d6ccfeSandi // remove empty namespaces 1394cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1395cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1396f3f0262cSandi } else { 1397cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 139833d979e7SMichael Große io_writeWikiPage($svdta['file'], $svdta['newContent'], $id); 139946844156SBen Coburn // pre-save the revision, to keep the attic in sync 1400b24d9195SAndreas Gohr $svdta['newRevision'] = saveOldRevision($id); 14012966355bSGerrit Uitslag $filesize_new = filesize($svdta['file']); 1402f3f0262cSandi } 14032966355bSGerrit Uitslag $svdta['sizechange'] = $filesize_new - $filesize_old; 1404f3f0262cSandi 1405b24d9195SAndreas Gohr $event->advise_after(); 140671726d78SBen Coburn 140764159a61SAndreas Gohr addLogEntry( 140864159a61SAndreas Gohr $svdta['newRevision'], 140964159a61SAndreas Gohr $svdta['id'], 141064159a61SAndreas Gohr $svdta['changeType'], 141164159a61SAndreas Gohr $svdta['summary'], 141264159a61SAndreas Gohr $svdta['changeInfo'], 141364159a61SAndreas Gohr null, 141464159a61SAndreas Gohr $svdta['sizechange'] 141564159a61SAndreas Gohr ); 1416ac3ed4afSGerrit Uitslag 141726a0801fSAndreas Gohr // send notify mails 1418b24d9195SAndreas Gohr notify($svdta['id'], 'admin', $svdta['oldRevision'], $svdta['summary'], $minor); 1419b24d9195SAndreas Gohr notify($svdta['id'], 'subscribers', $svdta['oldRevision'], $svdta['summary'], $minor); 1420f3f0262cSandi 1421ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 142298407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 14232eccbdaaSGina Haeussge 14242eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1425fe9ec250SChris Smith if(useHeading('content')) { 142607ff0babSMichael Hamann $pages = ft_backlinks($id, true); 14272eccbdaaSGina Haeussge foreach($pages as $page) { 14280db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 14292eccbdaaSGina Haeussge $cache->removeCache(); 14302eccbdaaSGina Haeussge } 14312eccbdaaSGina Haeussge } 1432f3f0262cSandi} 1433f3f0262cSandi 1434f3f0262cSandi/** 1435f3f0262cSandi * moves the current version to the attic and returns its 1436f3f0262cSandi * revision date 143715fae107Sandi * 143815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1439140cfbcdSGerrit Uitslag * 1440140cfbcdSGerrit Uitslag * @param string $id page id 1441140cfbcdSGerrit Uitslag * @return int|string revision timestamp 1442f3f0262cSandi */ 1443f3f0262cSandifunction saveOldRevision($id) { 1444f3f0262cSandi $oldf = wikiFN($id); 144579e79377SAndreas Gohr if(!file_exists($oldf)) return ''; 1446f3f0262cSandi $date = filemtime($oldf); 1447f3f0262cSandi $newf = wikiFN($id, $date); 1448cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1449f3f0262cSandi return $date; 1450f3f0262cSandi} 1451f3f0262cSandi 1452f3f0262cSandi/** 1453fde10de4SAdrian Lang * Sends a notify mail on page change or registration 145426a0801fSAndreas Gohr * 145526a0801fSAndreas Gohr * @param string $id The changed page 1456fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 14573272d797SAndreas Gohr * @param int|string $rev Old page revision 145826a0801fSAndreas Gohr * @param string $summary What changed 145990033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 146042ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 14613272d797SAndreas Gohr * @return bool 1462140cfbcdSGerrit Uitslag * 146315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1464f3f0262cSandi */ 146502a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1466f3f0262cSandi global $conf; 1467585bf44eSChristopher Smith /* @var Input $INPUT */ 1468585bf44eSChristopher Smith global $INPUT; 1469b158d625SSteven Danz 14706df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 147126a0801fSAndreas Gohr if($who == 'admin') { 14723272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 14732ed38036SAndreas Gohr $tpl = 'mailtext'; 147426a0801fSAndreas Gohr $to = $conf['notify']; 147526a0801fSAndreas Gohr } elseif($who == 'subscribers') { 147684c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1477585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 14780bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 14793272d797SAndreas Gohr trigger_event( 14803272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1481835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 14823272d797SAndreas Gohr ); 14832ed38036SAndreas Gohr $to = $data['addresslist']; 14842ed38036SAndreas Gohr if(empty($to)) return false; 14852ed38036SAndreas Gohr $tpl = 'subscr_single'; 148626a0801fSAndreas Gohr } else { 14873272d797SAndreas Gohr return false; //just to be safe 148826a0801fSAndreas Gohr } 148926a0801fSAndreas Gohr 14906df843eeSAndreas Gohr // prepare content 14912ed38036SAndreas Gohr $subscription = new Subscription(); 14922ed38036SAndreas Gohr return $subscription->send_diff($to, $tpl, $id, $rev, $summary); 1493f3f0262cSandi} 14942ed38036SAndreas Gohr 149515fae107Sandi/** 149671f7bde7SAndreas Gohr * extracts the query from a search engine referrer 149715fae107Sandi * 149815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 149971f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1500140cfbcdSGerrit Uitslag * 1501140cfbcdSGerrit Uitslag * @return array|string 1502f3f0262cSandi */ 1503f3f0262cSandifunction getGoogleQuery() { 1504585bf44eSChristopher Smith /* @var Input $INPUT */ 1505585bf44eSChristopher Smith global $INPUT; 1506585bf44eSChristopher Smith 1507585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1508c66972f2SAdrian Lang return ''; 1509c66972f2SAdrian Lang } 1510585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1511f3f0262cSandi 1512079b3ac1SAndreas Gohr // only handle common SEs 1513079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1514e4d8a516SKazutaka Miyasaka 1515079b3ac1SAndreas Gohr $query = array(); 1516e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1517e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1518e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1519f3f0262cSandi parse_str($url['query'], $query); 1520e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1521e4d8a516SKazutaka Miyasaka 1522c66972f2SAdrian Lang $q = ''; 1523079b3ac1SAndreas Gohr if(isset($query['q'])){ 1524079b3ac1SAndreas Gohr $q = $query['q']; 1525079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1526079b3ac1SAndreas Gohr $q = $query['p']; 1527079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1528079b3ac1SAndreas Gohr $q = $query['query']; 1529079b3ac1SAndreas Gohr } 1530079b3ac1SAndreas Gohr $q = trim($q); 1531f3f0262cSandi 1532079b3ac1SAndreas Gohr if(!$q) return ''; 15336531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1534f93b3b50SAndreas Gohr return $q; 1535f3f0262cSandi} 1536f3f0262cSandi 1537f3f0262cSandi/** 1538f3f0262cSandi * Return the human readable size of a file 1539f3f0262cSandi * 1540f3f0262cSandi * @param int $size A file size 1541f3f0262cSandi * @param int $dec A number of decimal places 154274160ca1SGerrit Uitslag * @return string human readable size 1543140cfbcdSGerrit Uitslag * 1544f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1545f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1546f3f0262cSandi * @version 1.0.0 1547f3f0262cSandi */ 1548f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1549f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1550f3f0262cSandi $count = count($sizes); 1551f3f0262cSandi $i = 0; 1552f3f0262cSandi 1553f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1554f3f0262cSandi $size /= 1024; 1555f3f0262cSandi $i++; 1556f3f0262cSandi } 1557f3f0262cSandi 1558ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1559f3f0262cSandi} 1560f3f0262cSandi 156115fae107Sandi/** 1562c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1563c57e365eSAndreas Gohr * 1564c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1565140cfbcdSGerrit Uitslag * 1566140cfbcdSGerrit Uitslag * @param int $dt timestamp 1567140cfbcdSGerrit Uitslag * @return string 1568c57e365eSAndreas Gohr */ 1569c57e365eSAndreas Gohrfunction datetime_h($dt) { 1570c57e365eSAndreas Gohr global $lang; 1571c57e365eSAndreas Gohr 1572c57e365eSAndreas Gohr $ago = time() - $dt; 1573c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1574c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1575c57e365eSAndreas Gohr } 1576c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1577c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1578c57e365eSAndreas Gohr } 1579c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1580c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1581c57e365eSAndreas Gohr } 1582c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1583c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1584c57e365eSAndreas Gohr } 1585c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1586c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1587c57e365eSAndreas Gohr } 1588c57e365eSAndreas Gohr if($ago > 60 * 2) { 1589c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1590c57e365eSAndreas Gohr } 1591c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1592c57e365eSAndreas Gohr} 1593c57e365eSAndreas Gohr 1594c57e365eSAndreas Gohr/** 1595f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1596f2263577SAndreas Gohr * 1597f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1598f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1599f2263577SAndreas Gohr * 1600f2263577SAndreas Gohr * @see datetime_h 1601f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1602140cfbcdSGerrit Uitslag * 1603140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1604140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1605140cfbcdSGerrit Uitslag * @return string 1606f2263577SAndreas Gohr */ 1607f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1608f2263577SAndreas Gohr global $conf; 1609f2263577SAndreas Gohr 1610f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1611f2263577SAndreas Gohr $dt = (int) $dt; 1612f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1613f2263577SAndreas Gohr 1614f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1615f2263577SAndreas Gohr return strftime($format, $dt); 1616f2263577SAndreas Gohr} 1617f2263577SAndreas Gohr 1618f2263577SAndreas Gohr/** 1619c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1620c4f79b71SMichael Hamann * 1621c4f79b71SMichael Hamann * @author <ungu at terong dot com> 162259752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1623140cfbcdSGerrit Uitslag * 16247e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 16253272d797SAndreas Gohr * @return string 1626c4f79b71SMichael Hamann */ 1627c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1628c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1629c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1630c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1631c4f79b71SMichael Hamann $date_mod .= $time_zone; 1632c4f79b71SMichael Hamann return $date_mod; 1633c4f79b71SMichael Hamann} 1634c4f79b71SMichael Hamann 1635c4f79b71SMichael Hamann/** 163600a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 163700a7b5adSEsther Brunner * 163800a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 163900a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1640140cfbcdSGerrit Uitslag * 1641140cfbcdSGerrit Uitslag * @param string $email email address 1642140cfbcdSGerrit Uitslag * @return string 164300a7b5adSEsther Brunner */ 164400a7b5adSEsther Brunnerfunction obfuscate($email) { 164500a7b5adSEsther Brunner global $conf; 164600a7b5adSEsther Brunner 164700a7b5adSEsther Brunner switch($conf['mailguard']) { 164800a7b5adSEsther Brunner case 'visible' : 164900a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 165000a7b5adSEsther Brunner return strtr($email, $obfuscate); 165100a7b5adSEsther Brunner 165200a7b5adSEsther Brunner case 'hex' : 165300a7b5adSEsther Brunner $encode = ''; 165449eb6e38SAndreas Gohr $len = strlen($email); 165549eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 165649eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 165749eb6e38SAndreas Gohr } 165800a7b5adSEsther Brunner return $encode; 165900a7b5adSEsther Brunner 166000a7b5adSEsther Brunner case 'none' : 166100a7b5adSEsther Brunner default : 166200a7b5adSEsther Brunner return $email; 166300a7b5adSEsther Brunner } 166400a7b5adSEsther Brunner} 166500a7b5adSEsther Brunner 166600a7b5adSEsther Brunner/** 166789541d4bSAndreas Gohr * Removes quoting backslashes 166889541d4bSAndreas Gohr * 166989541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1670140cfbcdSGerrit Uitslag * 1671140cfbcdSGerrit Uitslag * @param string $string 1672140cfbcdSGerrit Uitslag * @param string $char backslashed character 1673140cfbcdSGerrit Uitslag * @return string 167489541d4bSAndreas Gohr */ 167589541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 167689541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 167789541d4bSAndreas Gohr} 167889541d4bSAndreas Gohr 167973038c47SAndreas Gohr/** 168073038c47SAndreas Gohr * Convert php.ini shorthands to byte 168173038c47SAndreas Gohr * 168273038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 168359752844SAnders Sandblad * @link http://php.net/manual/en/ini.core.php#79564 1684140cfbcdSGerrit Uitslag * 1685140cfbcdSGerrit Uitslag * @param string $v shorthands 1686140cfbcdSGerrit Uitslag * @return int|string 168773038c47SAndreas Gohr */ 168873038c47SAndreas Gohrfunction php_to_byte($v) { 168973038c47SAndreas Gohr $l = substr($v, -1); 169073038c47SAndreas Gohr $ret = substr($v, 0, -1); 169173038c47SAndreas Gohr switch(strtoupper($l)) { 169274160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 169373038c47SAndreas Gohr case 'P': 169473038c47SAndreas Gohr $ret *= 1024; 169574160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 169673038c47SAndreas Gohr case 'T': 169773038c47SAndreas Gohr $ret *= 1024; 169874160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 169973038c47SAndreas Gohr case 'G': 170073038c47SAndreas Gohr $ret *= 1024; 170174160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 170273038c47SAndreas Gohr case 'M': 170373038c47SAndreas Gohr $ret *= 1024; 1704f168548cSGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 170573038c47SAndreas Gohr case 'K': 170673038c47SAndreas Gohr $ret *= 1024; 170773038c47SAndreas Gohr break; 170849cbd23eSOtto Vainio default; 170949cbd23eSOtto Vainio $ret *= 10; 171049cbd23eSOtto Vainio break; 171173038c47SAndreas Gohr } 171273038c47SAndreas Gohr return $ret; 171373038c47SAndreas Gohr} 171473038c47SAndreas Gohr 1715546d3a99SAndreas Gohr/** 1716546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1717140cfbcdSGerrit Uitslag * 1718140cfbcdSGerrit Uitslag * @param string $string 1719140cfbcdSGerrit Uitslag * @return string 1720546d3a99SAndreas Gohr */ 1721546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1722546d3a99SAndreas Gohr return preg_quote($string, '/'); 1723546d3a99SAndreas Gohr} 172473038c47SAndreas Gohr 1725bd2f6c2fSAndreas Gohr/** 1726bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1727bd2f6c2fSAndreas Gohr * 1728c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1729bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1730bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1731bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1732bd2f6c2fSAndreas Gohr * 1733bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1734bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1735bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1736bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1737bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 17383272d797SAndreas Gohr * @return string 1739bd2f6c2fSAndreas Gohr */ 1740a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 1741bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1742bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1743bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1744bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1745bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1746bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1747bd2f6c2fSAndreas Gohr} 1748bd2f6c2fSAndreas Gohr 1749dc58b6f4SAndy Webber/** 1750dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1751dc58b6f4SAndy Webber * in page footer and recent changes pages 1752dc58b6f4SAndy Webber * 1753b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 175415f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1755c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 175615f3bc49SGerrit Uitslag * 1757dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1758dc58b6f4SAndy Webber */ 175915f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1760cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1761dc58b6f4SAndy Webber} 1762dc58b6f4SAndy Webber 176360a396c8SGerrit Uitslag/** 176460a396c8SGerrit Uitslag * Returns users realname w/o link 176560a396c8SGerrit Uitslag * 1766f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 176715f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1768c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 176960a396c8SGerrit Uitslag * 177060a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 177160a396c8SGerrit Uitslag */ 1772cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 177360a396c8SGerrit Uitslag global $conf, $INFO; 177460a396c8SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 177560a396c8SGerrit Uitslag global $auth; 177630f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 177730f6ec4bSGerrit Uitslag global $INPUT; 177860a396c8SGerrit Uitslag 177960a396c8SGerrit Uitslag // prepare initial event data 178060a396c8SGerrit Uitslag $data = array( 178160a396c8SGerrit Uitslag 'username' => $username, // the unique user name 178260a396c8SGerrit Uitslag 'name' => '', 178360a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 178460a396c8SGerrit Uitslag 'target' => '', 178560a396c8SGerrit Uitslag 'pre' => '', 178660a396c8SGerrit Uitslag 'suf' => '', 178760a396c8SGerrit Uitslag 'style' => '', 178860a396c8SGerrit Uitslag 'more' => '', 178960a396c8SGerrit Uitslag 'url' => '', 179060a396c8SGerrit Uitslag 'title' => '', 179160a396c8SGerrit Uitslag 'class' => '' 179260a396c8SGerrit Uitslag ), 17934d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 179415f3bc49SGerrit Uitslag 'textonly' => $textonly 179560a396c8SGerrit Uitslag ); 179662c8004eSGerrit Uitslag if($username === null) { 179730f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 179815f3bc49SGerrit Uitslag if($textonly){ 179915f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 180015f3bc49SGerrit Uitslag }else { 180164159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 180264159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 180360a396c8SGerrit Uitslag } 180415f3bc49SGerrit Uitslag } 180560a396c8SGerrit Uitslag 180660a396c8SGerrit Uitslag $evt = new Doku_Event('COMMON_USER_LINK', $data); 180760a396c8SGerrit Uitslag if($evt->advise_before(true)) { 180860a396c8SGerrit Uitslag if(empty($data['name'])) { 180960a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 181065833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1811dc58b6f4SAndy Webber switch($conf['showuseras']) { 1812dc58b6f4SAndy Webber case 'username': 18137f081821SGerrit Uitslag case 'username_link': 181415f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 181560a396c8SGerrit Uitslag break; 1816dc58b6f4SAndy Webber case 'email': 1817dc58b6f4SAndy Webber case 'email_link': 181860a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 181960a396c8SGerrit Uitslag break; 1820dc58b6f4SAndy Webber } 182165833968SGerrit Uitslag } else { 182265833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 182360a396c8SGerrit Uitslag } 182460a396c8SGerrit Uitslag } 18257f081821SGerrit Uitslag 18267f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 18277f081821SGerrit Uitslag static $xhtml_renderer = null; 18287f081821SGerrit Uitslag 182915f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 18307f081821SGerrit Uitslag 18317f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 183260a396c8SGerrit Uitslag if(!isset($info)) { 183360a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 183460a396c8SGerrit Uitslag } 183560a396c8SGerrit Uitslag if(isset($info) && $info) { 18367f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 183760a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1838dc58b6f4SAndy Webber } else { 18397f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 18407f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 18417f081821SGerrit Uitslag } 18427f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 18437f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 18447f081821SGerrit Uitslag } 18457f081821SGerrit Uitslag $shortcut = 'user'; 1846533772e1SGerrit Uitslag $exists = null; 18476496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 18482a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 18496496c33fSGerrit Uitslag if($exists !== null) { 18506496c33fSGerrit Uitslag if($exists) { 18516496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 18526496c33fSGerrit Uitslag } else { 18536496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 18546496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 18556496c33fSGerrit Uitslag } 18566496c33fSGerrit Uitslag } 1857dc58b6f4SAndy Webber } 1858dc58b6f4SAndy Webber } else { 185915f3bc49SGerrit Uitslag $data['textonly'] = true; 1860dc58b6f4SAndy Webber } 186160a396c8SGerrit Uitslag 186260a396c8SGerrit Uitslag } else { 186315f3bc49SGerrit Uitslag $data['textonly'] = true; 186460a396c8SGerrit Uitslag } 186560a396c8SGerrit Uitslag } 186660a396c8SGerrit Uitslag 186715f3bc49SGerrit Uitslag if($data['textonly']) { 18684d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 186960a396c8SGerrit Uitslag } else { 187060a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 187160a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 187260a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 187360a396c8SGerrit Uitslag } 18744d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 187560a396c8SGerrit Uitslag } 187660a396c8SGerrit Uitslag } 187760a396c8SGerrit Uitslag $evt->advise_after(); 187860a396c8SGerrit Uitslag unset($evt); 187960a396c8SGerrit Uitslag 18804d5fc927SGerrit Uitslag return $data['userlink']; 1881066fee30SAndreas Gohr} 1882066fee30SAndreas Gohr 1883066fee30SAndreas Gohr/** 1884066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1885066fee30SAndreas Gohr * When no image exists, returns an empty string 1886066fee30SAndreas Gohr * 1887066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1888140cfbcdSGerrit Uitslag * 1889066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 18903272d797SAndreas Gohr * @return string 1891066fee30SAndreas Gohr */ 1892066fee30SAndreas Gohrfunction license_img($type) { 1893066fee30SAndreas Gohr global $license; 1894066fee30SAndreas Gohr global $conf; 1895066fee30SAndreas Gohr if(!$conf['license']) return ''; 1896066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1897066fee30SAndreas Gohr $try = array(); 1898066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1899066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1900066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1901066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1902066fee30SAndreas Gohr } 1903066fee30SAndreas Gohr foreach($try as $src) { 190479e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1905066fee30SAndreas Gohr } 1906066fee30SAndreas Gohr return ''; 1907dc58b6f4SAndy Webber} 1908dc58b6f4SAndy Webber 190913c08e2fSMichael Klier/** 191013c08e2fSMichael Klier * Checks if the given amount of memory is available 191113c08e2fSMichael Klier * 191213c08e2fSMichael Klier * If the memory_get_usage() function is not available the 191313c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 191413c08e2fSMichael Klier * 191513c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 191613c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 19173272d797SAndreas Gohr * 19183272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1919140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 19203272d797SAndreas Gohr * @return bool 192113c08e2fSMichael Klier */ 192213c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 192313c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 192413c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1925985d6187SElenchus if($limit == -1) return true; // unlimited 192613c08e2fSMichael Klier 192713c08e2fSMichael Klier // parse limit to bytes 192813c08e2fSMichael Klier $limit = php_to_byte($limit); 192913c08e2fSMichael Klier 193013c08e2fSMichael Klier // get used memory if possible 193113c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 193213c08e2fSMichael Klier $used = memory_get_usage(); 193349eb6e38SAndreas Gohr } else { 193449eb6e38SAndreas Gohr $used = $bytes; 193513c08e2fSMichael Klier } 193613c08e2fSMichael Klier 193713c08e2fSMichael Klier if($used + $mem > $limit) { 193813c08e2fSMichael Klier return false; 193913c08e2fSMichael Klier } 194013c08e2fSMichael Klier 194113c08e2fSMichael Klier return true; 194213c08e2fSMichael Klier} 194313c08e2fSMichael Klier 1944af2408d5SAndreas Gohr/** 1945af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1946af2408d5SAndreas Gohr * 1947af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1948af2408d5SAndreas Gohr * 1949af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1950af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1951140cfbcdSGerrit Uitslag * 1952140cfbcdSGerrit Uitslag * @param string $url url being directed to 1953af2408d5SAndreas Gohr */ 1954af2408d5SAndreas Gohrfunction send_redirect($url) { 195598ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 195698ca30d2SAndreas Gohr 1957585bf44eSChristopher Smith /* @var Input $INPUT */ 1958585bf44eSChristopher Smith global $INPUT; 1959585bf44eSChristopher Smith 19600181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 19610181f021SAndreas Gohr global $MSG; 19620181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 19630181f021SAndreas Gohr //reopen session, store data and close session again 19640181f021SAndreas Gohr @session_start(); 19650181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 19660181f021SAndreas Gohr } 19670181f021SAndreas Gohr 1968d4869846SAndreas Gohr // always close the session 1969d4869846SAndreas Gohr session_write_close(); 1970d4869846SAndreas Gohr 1971af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1972585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1973585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1974585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 19753272d797SAndreas Gohr $matches[1] < 6 19763272d797SAndreas Gohr ) { 1977af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1978af2408d5SAndreas Gohr } else { 1979af2408d5SAndreas Gohr header('Location: '.$url); 1980af2408d5SAndreas Gohr } 198181781cb6SAndreas Gohr 1982572dc222SLarsDW223 // no exits during unit tests 198327c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 198427c0c399SAndreas Gohr // pass info about the redirect back to the test suite 198527c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 198627c0c399SAndreas Gohr if($testRequest !== null) { 198727c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 198827c0c399SAndreas Gohr } 1989572dc222SLarsDW223 return; 1990572dc222SLarsDW223 } 199127c0c399SAndreas Gohr 1992af2408d5SAndreas Gohr exit; 1993af2408d5SAndreas Gohr} 1994af2408d5SAndreas Gohr 19955b75cd1fSAdrian Lang/** 19965b75cd1fSAdrian Lang * Validate a value using a set of valid values 19975b75cd1fSAdrian Lang * 19985b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 19995b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 20005b75cd1fSAdrian Lang * default is specified, throws an exception. 20015b75cd1fSAdrian Lang * 20025b75cd1fSAdrian Lang * @param string $param The name of the parameter 20035b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 20045b75cd1fSAdrian Lang * be marked by the key “default”. 20055b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 20065b75cd1fSAdrian Lang * or $_GET) 20075b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 20085b75cd1fSAdrian Lang * 20093272d797SAndreas Gohr * @throws Exception 20103272d797SAndreas Gohr * @return mixed 20115b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 20125b75cd1fSAdrian Lang */ 20135b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 20145b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 20155b75cd1fSAdrian Lang return $array[$param]; 20165b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 20175b75cd1fSAdrian Lang return $valid_values['default']; 20185b75cd1fSAdrian Lang } else { 20195b75cd1fSAdrian Lang throw new Exception($exc); 20205b75cd1fSAdrian Lang } 20215b75cd1fSAdrian Lang} 20225b75cd1fSAdrian Lang 202363703ba5SAndreas Gohr/** 202463703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 2025646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 2026140cfbcdSGerrit Uitslag * 2027140cfbcdSGerrit Uitslag * @param string $pref preference key 2028b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 2029140cfbcdSGerrit Uitslag * @return string preference value 203063703ba5SAndreas Gohr */ 2031554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 2032646a531aSChristopher Smith $enc_pref = urlencode($pref); 203306c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 2034554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 203563703ba5SAndreas Gohr $cnt = count($parts); 203663703ba5SAndreas Gohr for($i = 0; $i < $cnt; $i += 2) { 2037646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 2038646a531aSChristopher Smith return urldecode($parts[$i + 1]); 2039554a8c9fSAdrian Lang } 2040554a8c9fSAdrian Lang } 2041554a8c9fSAdrian Lang } 2042554a8c9fSAdrian Lang return $default; 2043554a8c9fSAdrian Lang} 2044554a8c9fSAdrian Lang 20453c94d07bSAnika Henke/** 20463c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 204736ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 20483a970889SAnika Henke * Remove it by setting $val to false 2049140cfbcdSGerrit Uitslag * 2050140cfbcdSGerrit Uitslag * @param string $pref preference key 2051140cfbcdSGerrit Uitslag * @param string $val preference value 20523c94d07bSAnika Henke */ 20533c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 20543c94d07bSAnika Henke global $conf; 20553c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 20563c94d07bSAnika Henke $cookieVal = ''; 20573c94d07bSAnika Henke 20583c94d07bSAnika Henke if($orig && ($orig != $val)) { 20593c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 20603c94d07bSAnika Henke $cnt = count($parts); 206136ec377eSChristopher Smith // urlencode $pref for the comparison 206236ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 20633c94d07bSAnika Henke for($i = 0; $i < $cnt; $i += 2) { 206436ec377eSChristopher Smith if($parts[$i] == $enc_pref) { 20653a970889SAnika Henke if ($val !== false) { 206636ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 20673a970889SAnika Henke } else { 20683a970889SAnika Henke unset($parts[$i]); 20693a970889SAnika Henke unset($parts[$i + 1]); 20703a970889SAnika Henke } 207150f261f7SMichael Hamann break; 20723c94d07bSAnika Henke } 20733c94d07bSAnika Henke } 20743c94d07bSAnika Henke $cookieVal = implode('#', $parts); 20753a970889SAnika Henke } else if (!$orig && $val !== false) { 207664159a61SAndreas Gohr $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : ''). 207764159a61SAndreas Gohr rawurlencode($pref).'#'.rawurlencode($val); 20783c94d07bSAnika Henke } 20793c94d07bSAnika Henke 20803c94d07bSAnika Henke if (!empty($cookieVal)) { 208175e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 208275e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 20833c94d07bSAnika Henke } 20843c94d07bSAnika Henke} 20853c94d07bSAnika Henke 2086f8fb2d18SAndreas Gohr/** 2087f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 2088f8fb2d18SAndreas Gohr * 208942ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 2090f8fb2d18SAndreas Gohr */ 2091f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 2092f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 2093f8fb2d18SAndreas Gohr} 2094f8fb2d18SAndreas Gohr 20953c27983bSAndreas Gohr/** 209671de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 20973c27983bSAndreas Gohr * 20983c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 20993c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 21003c27983bSAndreas Gohr * files are embedded. 21013c27983bSAndreas Gohr * 210271de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 210371de5572SAndreas Gohr * 21043c27983bSAndreas Gohr * @param string $file full path to the SVG file 21053c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 210671de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 21073c27983bSAndreas Gohr */ 21084cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 21093c27983bSAndreas Gohr $file = trim($file); 21103c27983bSAndreas Gohr if($file === '') return false; 21113c27983bSAndreas Gohr if(!file_exists($file)) return false; 21123c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 21133c27983bSAndreas Gohr if(!is_readable($file)) return false; 21143c27983bSAndreas Gohr $content = file_get_contents($file); 21150849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 21160849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 21170849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 21180849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 21193c27983bSAndreas Gohr $content = trim($content); 21203c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 212171de5572SAndreas Gohr return $content; 21223c27983bSAndreas Gohr} 21233c27983bSAndreas Gohr 2124e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 2125