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; 12*b24e9c4aSSatoshi Saharause dokuwiki\File\PageFile; 1366f4cdd4SSatoshi Saharause dokuwiki\Logger; 14704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 1575d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 16e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 17e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 180c3a5702SAndreas Gohr 19f3f0262cSandi/** 20d5197206Schris * Wrapper around htmlspecialchars() 21d5197206Schris * 22d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 23d5197206Schris * @see htmlspecialchars() 24140cfbcdSGerrit Uitslag * 25140cfbcdSGerrit Uitslag * @param string $string the string being converted 26140cfbcdSGerrit Uitslag * @return string converted string 27d5197206Schris */ 28d5197206Schrisfunction hsc($string) { 29d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 30d5197206Schris} 31d5197206Schris 32d5197206Schris/** 335b571377SAndreas Gohr * Checks if the given input is blank 345b571377SAndreas Gohr * 355b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 365b571377SAndreas Gohr * 3767234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 3867234204SAndreas Gohr * with a NULL value without warning. 3967234204SAndreas Gohr * 4067234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 4167234204SAndreas Gohr * 4267234204SAndreas Gohr * (isset($foo) && !blank($foo)) 4367234204SAndreas Gohr * (!isset($foo) || blank($foo)) 4467234204SAndreas Gohr * 455b571377SAndreas Gohr * @param $in 465b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 475b571377SAndreas Gohr * @return bool 485b571377SAndreas Gohr */ 495b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 505b571377SAndreas Gohr if(is_null($in)) return true; 515b571377SAndreas Gohr if(is_array($in)) return empty($in); 525b571377SAndreas Gohr if($in === "\0") return true; 535b571377SAndreas Gohr if($trim && trim($in) === '') return true; 545b571377SAndreas Gohr if(strlen($in) > 0) return false; 555b571377SAndreas Gohr return empty($in); 565b571377SAndreas Gohr} 575b571377SAndreas Gohr 585b571377SAndreas Gohr/** 59d5197206Schris * print a newline terminated string 60d5197206Schris * 61d5197206Schris * You can give an indention as optional parameter 62d5197206Schris * 63d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 64140cfbcdSGerrit Uitslag * 65140cfbcdSGerrit Uitslag * @param string $string line of text 66140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 67d5197206Schris */ 6825ec097bSChris Smithfunction ptln($string, $indent = 0) { 6925ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 7002b0b681SAndreas Gohr} 7102b0b681SAndreas Gohr 7202b0b681SAndreas Gohr/** 7302b0b681SAndreas Gohr * strips control characters (<32) from the given string 7402b0b681SAndreas Gohr * 7502b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 76140cfbcdSGerrit Uitslag * 7742ea7f44SGerrit Uitslag * @param string $string being stripped 78140cfbcdSGerrit Uitslag * @return string 7902b0b681SAndreas Gohr */ 8002b0b681SAndreas Gohrfunction stripctl($string) { 8102b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 82d5197206Schris} 83d5197206Schris 84d5197206Schris/** 85634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 86634d7150SAndreas Gohr * 87634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 88634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 89634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 9042ea7f44SGerrit Uitslag * 91634d7150SAndreas Gohr * @return string 92634d7150SAndreas Gohr */ 93634d7150SAndreas Gohrfunction getSecurityToken() { 94585bf44eSChristopher Smith /** @var Input $INPUT */ 95585bf44eSChristopher Smith global $INPUT; 963680e2cdSAndreas Gohr 973680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 983680e2cdSAndreas Gohr $session = session_id(); 993680e2cdSAndreas Gohr 1003680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1013680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 102c3cc6e05SAndreas Gohr return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 103634d7150SAndreas Gohr} 104634d7150SAndreas Gohr 105634d7150SAndreas Gohr/** 106634d7150SAndreas Gohr * Check the secret CSRF token 107140cfbcdSGerrit Uitslag * 108140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 109140cfbcdSGerrit Uitslag * @return bool success if the token matched 110634d7150SAndreas Gohr */ 111634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 112585bf44eSChristopher Smith /** @var Input $INPUT */ 1137d01a0eaSTom N Harris global $INPUT; 114585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 115df97eaacSAndreas Gohr 1167d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 117634d7150SAndreas Gohr if(getSecurityToken() != $token) { 118634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 119634d7150SAndreas Gohr return false; 120634d7150SAndreas Gohr } 121634d7150SAndreas Gohr return true; 122634d7150SAndreas Gohr} 123634d7150SAndreas Gohr 124634d7150SAndreas Gohr/** 125634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 126634d7150SAndreas Gohr * 127634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 128140cfbcdSGerrit Uitslag * 129140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 13042ea7f44SGerrit Uitslag * @return string html of hidden form field 131634d7150SAndreas Gohr */ 132634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1332404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1343272d797SAndreas Gohr if($print) echo $ret; 135634d7150SAndreas Gohr return $ret; 136634d7150SAndreas Gohr} 137634d7150SAndreas Gohr 138634d7150SAndreas Gohr/** 1391015a57dSChristopher Smith * Determine basic information for a request of $id 14015fae107Sandi * 14115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1427e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 143140cfbcdSGerrit Uitslag * 144140cfbcdSGerrit Uitslag * @param string $id pageid 145140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 146140cfbcdSGerrit Uitslag * @return array with info for a request of $id 147140cfbcdSGerrit Uitslag * 148f3f0262cSandi */ 1491015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 150f3f0262cSandi global $USERINFO; 151585bf44eSChristopher Smith /* @var Input $INPUT */ 152585bf44eSChristopher Smith global $INPUT; 1536afe8dcaSchris 154c66972f2SAdrian Lang // set info about manager/admin status. 15559bc3b48SGerrit Uitslag $info = array(); 156c66972f2SAdrian Lang $info['isadmin'] = false; 157c66972f2SAdrian Lang $info['ismanager'] = false; 158585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 159f3f0262cSandi $info['userinfo'] = $USERINFO; 1601015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 161585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 16217ee7f66SAndreas Gohr 163f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 164f8cc712eSAndreas Gohr $info['isadmin'] = true; 165f8cc712eSAndreas Gohr $info['ismanager'] = true; 166f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 167f8cc712eSAndreas Gohr $info['ismanager'] = true; 168f8cc712eSAndreas Gohr } 169f8cc712eSAndreas Gohr 17017ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 17117ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 172585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 17317ee7f66SAndreas Gohr } 174ee4c4a1bSAndreas Gohr 175f3f0262cSandi } else { 1761015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 177ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 178f3f0262cSandi } 179f3f0262cSandi 1801015a57dSChristopher Smith $info['namespace'] = getNS($id); 1811015a57dSChristopher Smith 1821015a57dSChristopher Smith // mobile detection 1831015a57dSChristopher Smith if ($htmlClient) { 1841015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1851015a57dSChristopher Smith } 1861015a57dSChristopher Smith 1871015a57dSChristopher Smith return $info; 1881015a57dSChristopher Smith } 1891015a57dSChristopher Smith 1901015a57dSChristopher Smith/** 1911015a57dSChristopher Smith * Return info about the current document as associative 1921015a57dSChristopher Smith * array. 1931015a57dSChristopher Smith * 1941015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 195140cfbcdSGerrit Uitslag * 196140cfbcdSGerrit Uitslag * @return array with info about current document 1971015a57dSChristopher Smith */ 1981015a57dSChristopher Smithfunction pageinfo() { 1991015a57dSChristopher Smith global $ID; 2001015a57dSChristopher Smith global $REV; 2011015a57dSChristopher Smith global $RANGE; 2021015a57dSChristopher Smith global $lang; 203585bf44eSChristopher Smith /* @var Input $INPUT */ 204585bf44eSChristopher Smith global $INPUT; 2051015a57dSChristopher Smith 2061015a57dSChristopher Smith $info = basicinfo($ID); 2071015a57dSChristopher Smith 2081015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2091015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2101015a57dSChristopher Smith $info['id'] = $ID; 2111015a57dSChristopher Smith $info['rev'] = $REV; 2121015a57dSChristopher Smith 21375d66495SMichael Große $subManager = new SubscriberManager(); 21475d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2157e87a794SChristopher Smith 216f3f0262cSandi $info['locked'] = checklock($ID); 217317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 21879e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 21901c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2205ec96136SSatoshi Sahara 2212ca9d91cSBen Coburn if ($REV) { 2222ca9d91cSBen Coburn //check if current revision was meant 22301c9a118SAndreas Gohr if ($info['exists'] && ($info['currentrev'] == $REV)) { 2242ca9d91cSBen Coburn $REV = ''; 2257b3a6803SAndreas Gohr } elseif ($RANGE) { 2267b3a6803SAndreas Gohr //section editing does not work with old revisions! 2277b3a6803SAndreas Gohr $REV = ''; 2287b3a6803SAndreas Gohr $RANGE = ''; 2297b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2302ca9d91cSBen Coburn } else { 2312ca9d91cSBen Coburn //really use old revision 232317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 23379e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 234f3f0262cSandi } 235f3f0262cSandi } 236c112d578Sandi $info['rev'] = $REV; 237f3f0262cSandi if ($info['exists']) { 238252acce3SSatoshi Sahara $info['writable'] = (is_writable($info['filepath']) && $info['perm'] >= AUTH_EDIT); 239f3f0262cSandi } else { 240f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 241f3f0262cSandi } 24250e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 243f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 244f3f0262cSandi 24571726d78SBen Coburn //load page meta data 24671726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 24771726d78SBen Coburn 248652610a2Sandi //who's the editor 249047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 250652610a2Sandi if ($REV) { 251f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 252652610a2Sandi } else { 2530e80bb5eSChristopher Smith if (!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 254aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 255aa27cf05SAndreas Gohr } else { 256f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 257cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 258cd00a034SBen Coburn if ($revinfo !== false) { 259cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 260cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 261cd00a034SBen Coburn } 262cd00a034SBen Coburn } 263cd00a034SBen Coburn } 264cd00a034SBen Coburn //and check for an external edit 265cd00a034SBen Coburn if ($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 266cd00a034SBen Coburn // cached changelog line no longer valid 267cd00a034SBen Coburn $revinfo = false; 268cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 269cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 270652610a2Sandi } 271bb4866bdSchris 2720a444b5aSPhy if ($revinfo !== false) { 273652610a2Sandi $info['ip'] = $revinfo['ip']; 274652610a2Sandi $info['user'] = $revinfo['user']; 275652610a2Sandi $info['sum'] = $revinfo['sum']; 27671726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 277ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 27859f257aeSchris 279252acce3SSatoshi Sahara $info['editor'] = $revinfo['user'] ?: $revinfo['ip']; 2800a444b5aSPhy } else { 2810a444b5aSPhy $info['ip'] = null; 2820a444b5aSPhy $info['user'] = null; 2830a444b5aSPhy $info['sum'] = null; 2840a444b5aSPhy $info['editor'] = null; 2850a444b5aSPhy } 286652610a2Sandi 287ee4c4a1bSAndreas Gohr // draft 2880aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 2890aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 2900aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 291ee4c4a1bSAndreas Gohr } 292ee4c4a1bSAndreas Gohr 2931015a57dSChristopher Smith return $info; 2941015a57dSChristopher Smith} 2951015a57dSChristopher Smith 2961015a57dSChristopher Smith/** 2970c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 2980c39d46cSMichael Große */ 2990c39d46cSMichael Großefunction jsinfo() { 3000c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3010c39d46cSMichael Große 3020c39d46cSMichael Große if (!is_array($JSINFO)) { 3030c39d46cSMichael Große $JSINFO = []; 3040c39d46cSMichael Große } 3050c39d46cSMichael Große //export minimal info to JS, plugins can add more 3060c39d46cSMichael Große $JSINFO['id'] = $ID; 30768491db9SPhy $JSINFO['namespace'] = isset($INFO) ? (string) $INFO['namespace'] : ''; 3080c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3090c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3100c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3110c39d46cSMichael Große} 3120c39d46cSMichael Große 3130c39d46cSMichael Große/** 3141015a57dSChristopher Smith * Return information about the current media item as an associative array. 315140cfbcdSGerrit Uitslag * 316140cfbcdSGerrit Uitslag * @return array with info about current media item 3171015a57dSChristopher Smith */ 3181015a57dSChristopher Smithfunction mediainfo() { 3191015a57dSChristopher Smith global $NS; 3201015a57dSChristopher Smith global $IMG; 3211015a57dSChristopher Smith 3221015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3231015a57dSChristopher Smith $info['image'] = $IMG; 3241c548ebeSAndreas Gohr 325f3f0262cSandi return $info; 326f3f0262cSandi} 327f3f0262cSandi 328f3f0262cSandi/** 3292684e50aSAndreas Gohr * Build an string of URL parameters 3302684e50aSAndreas Gohr * 3312684e50aSAndreas Gohr * @author Andreas Gohr 332140cfbcdSGerrit Uitslag * 333140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 334140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 335140cfbcdSGerrit Uitslag * @return string query string 3362684e50aSAndreas Gohr */ 337b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3382684e50aSAndreas Gohr $url = ''; 3392684e50aSAndreas Gohr $amp = false; 3402684e50aSAndreas Gohr foreach($params as $key => $val) { 341b174aeaeSchris if($amp) $url .= $sep; 3422684e50aSAndreas Gohr 34385e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3443a50618cSgweissbach $url .= rawurlencode((string) $val); 3452684e50aSAndreas Gohr $amp = true; 3462684e50aSAndreas Gohr } 3472684e50aSAndreas Gohr return $url; 3482684e50aSAndreas Gohr} 3492684e50aSAndreas Gohr 3502684e50aSAndreas Gohr/** 3512684e50aSAndreas Gohr * Build an string of html tag attributes 3522684e50aSAndreas Gohr * 3537bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3547bff22c0SAndreas Gohr * 3552684e50aSAndreas Gohr * @author Andreas Gohr 356140cfbcdSGerrit Uitslag * 357140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 358246d3337SMichael Große * @param bool $skipEmptyStrings skip empty string values? 359140cfbcdSGerrit Uitslag * @return string 3602684e50aSAndreas Gohr */ 361246d3337SMichael Großefunction buildAttributes($params, $skipEmptyStrings = false) { 3622684e50aSAndreas Gohr $url = ''; 3639063ec14SAdrian Lang $white = false; 3642684e50aSAndreas Gohr foreach($params as $key => $val) { 3652401f18dSSyntaxseed if($key[0] == '_') continue; 366246d3337SMichael Große if($val === '' && $skipEmptyStrings) continue; 3679063ec14SAdrian Lang if($white) $url .= ' '; 3687bff22c0SAndreas Gohr 3692684e50aSAndreas Gohr $url .= $key.'="'; 3702684e50aSAndreas Gohr $url .= htmlspecialchars($val); 3712684e50aSAndreas Gohr $url .= '"'; 3729063ec14SAdrian Lang $white = true; 3732684e50aSAndreas Gohr } 3742684e50aSAndreas Gohr return $url; 3752684e50aSAndreas Gohr} 3762684e50aSAndreas Gohr 3772684e50aSAndreas Gohr/** 37815fae107Sandi * This builds the breadcrumb trail and returns it as array 37915fae107Sandi * 38015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 381140cfbcdSGerrit Uitslag * 382e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 383f3f0262cSandi */ 384f3f0262cSandifunction breadcrumbs() { 3858746e727Sandi // we prepare the breadcrumbs early for quick session closing 3868746e727Sandi static $crumbs = null; 3878746e727Sandi if($crumbs != null) return $crumbs; 3888746e727Sandi 389f3f0262cSandi global $ID; 390f3f0262cSandi global $ACT; 391f3f0262cSandi global $conf; 3920ea5ebb4SB_S666 global $INFO; 393f3f0262cSandi 394f3f0262cSandi //first visit? 395c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 3965603d3c1SHenry Pan //we only save on show and existing visible readable wiki documents 397a77f5846Sjan $file = wikiFN($ID); 3985603d3c1SHenry Pan if($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) { 399e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 400f3f0262cSandi return $crumbs; 401f3f0262cSandi } 402a77f5846Sjan 403a77f5846Sjan // page names 4041a84a0f3SAnika Henke $name = noNSorNS($ID); 405fe9ec250SChris Smith if(useHeading('navigation')) { 406a77f5846Sjan // get page title 40767c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 408a77f5846Sjan if($title) { 409a77f5846Sjan $name = $title; 410a77f5846Sjan } 411a77f5846Sjan } 412a77f5846Sjan 413f3f0262cSandi //remove ID from array 414a77f5846Sjan if(isset($crumbs[$ID])) { 415a77f5846Sjan unset($crumbs[$ID]); 416f3f0262cSandi } 417f3f0262cSandi 418f3f0262cSandi //add to array 419a77f5846Sjan $crumbs[$ID] = $name; 420f3f0262cSandi //reduce size 421f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 422f3f0262cSandi array_shift($crumbs); 423f3f0262cSandi } 424f3f0262cSandi //save to session 425e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 426f3f0262cSandi return $crumbs; 427f3f0262cSandi} 428f3f0262cSandi 429f3f0262cSandi/** 43015fae107Sandi * Filter for page IDs 43115fae107Sandi * 432f3f0262cSandi * This is run on a ID before it is outputted somewhere 433f3f0262cSandi * currently used to replace the colon with something else 434907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 435907f24f7SAndreas Gohr * 436907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 437907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 438907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 43915fae107Sandi * 44049c713a3Sandi * Urlencoding is ommitted when the second parameter is false 44149c713a3Sandi * 44215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 443140cfbcdSGerrit Uitslag * 444140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 445140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 446140cfbcdSGerrit Uitslag * @return string 447f3f0262cSandi */ 44849c713a3Sandifunction idfilter($id, $ue = true) { 449f3f0262cSandi global $conf; 450585bf44eSChristopher Smith /* @var Input $INPUT */ 451585bf44eSChristopher Smith global $INPUT; 452585bf44eSChristopher Smith 453f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 454f3f0262cSandi $id = strtr($id, ':', '/'); 455f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 45658bedc8aSborekb $conf['userewrite'] && 457585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4583272d797SAndreas Gohr ) { 459f3f0262cSandi $id = strtr($id, ':', ';'); 460f3f0262cSandi } 46149c713a3Sandi if($ue) { 462b6c6979fSAndreas Gohr $id = rawurlencode($id); 463f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 464edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 465f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 46649c713a3Sandi } 467f3f0262cSandi return $id; 468f3f0262cSandi} 469f3f0262cSandi 470f3f0262cSandi/** 471ed7b5f09Sandi * This builds a link to a wikipage 47215fae107Sandi * 4734bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4746c7843b5Sandi * 47515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4764bc480e5SAndreas Gohr * 4774bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4784bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4794bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4804bc480e5SAndreas Gohr * @param string $separator parameter separator 4814bc480e5SAndreas Gohr * @return string 482f3f0262cSandi */ 48316f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 484f3f0262cSandi global $conf; 48516f15a81SDominik Eckelmann if(is_array($urlParameters)) { 4864bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 48764159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 48864159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 48964159a61SAndreas Gohr } 49016f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4916de3759aSAndreas Gohr } else { 49216f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4936de3759aSAndreas Gohr } 49416f15a81SDominik Eckelmann if($id === '') { 49516f15a81SDominik Eckelmann $id = $conf['start']; 49616f15a81SDominik Eckelmann } 497f3f0262cSandi $id = idfilter($id); 49816f15a81SDominik Eckelmann if($absolute) { 499ed7b5f09Sandi $xlink = DOKU_URL; 500ed7b5f09Sandi } else { 501ed7b5f09Sandi $xlink = DOKU_BASE; 502ed7b5f09Sandi } 503f3f0262cSandi 5046c7843b5Sandi if($conf['userewrite'] == 2) { 5056c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 50616f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5076c7843b5Sandi } elseif($conf['userewrite']) { 508f3f0262cSandi $xlink .= $id; 50916f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 51040b5fb5bSPhy } elseif($id !== '') { 5116c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 51216f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 513bce3726dSAndreas Gohr } else { 514bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 51516f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 516f3f0262cSandi } 517f3f0262cSandi 518f3f0262cSandi return $xlink; 519f3f0262cSandi} 520f3f0262cSandi 521f3f0262cSandi/** 522f5c2808fSBen Coburn * This builds a link to an alternate page format 523f5c2808fSBen Coburn * 524f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 525f5c2808fSBen Coburn * 526f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5274bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5284bc480e5SAndreas Gohr * @param string $format the export renderer to use 5294bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5304bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5314bc480e5SAndreas Gohr * @param string $sep parameter separator 5324bc480e5SAndreas Gohr * @return string 533f5c2808fSBen Coburn */ 5344bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 535f5c2808fSBen Coburn global $conf; 5364bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5374bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 538f5c2808fSBen Coburn } else { 5394bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 540f5c2808fSBen Coburn } 541f5c2808fSBen Coburn 542f5c2808fSBen Coburn $format = rawurlencode($format); 543f5c2808fSBen Coburn $id = idfilter($id); 544f5c2808fSBen Coburn if($abs) { 545f5c2808fSBen Coburn $xlink = DOKU_URL; 546f5c2808fSBen Coburn } else { 547f5c2808fSBen Coburn $xlink = DOKU_BASE; 548f5c2808fSBen Coburn } 549f5c2808fSBen Coburn 550f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 551f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5524bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 553f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 554f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5554bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 556f5c2808fSBen Coburn } else { 557f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5584bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 559f5c2808fSBen Coburn } 560f5c2808fSBen Coburn 561f5c2808fSBen Coburn return $xlink; 562f5c2808fSBen Coburn} 563f5c2808fSBen Coburn 564f5c2808fSBen Coburn/** 5656de3759aSAndreas Gohr * Build a link to a media file 5666de3759aSAndreas Gohr * 5676de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5688c08db0aSAndreas Gohr * 5698c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5708c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5718c08db0aSAndreas Gohr * 5723272d797SAndreas Gohr * @param string $id the media file id or URL 5733272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5743272d797SAndreas Gohr * @param bool $direct link to detail page if false 5753272d797SAndreas Gohr * @param string $sep URL parameter separator 5763272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5773272d797SAndreas Gohr * @return string 5786de3759aSAndreas Gohr */ 57955b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5806de3759aSAndreas Gohr global $conf; 581b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 582826d2766SKlap-in if(!$isexternalimage) { 583826d2766SKlap-in $id = cleanID($id); 584826d2766SKlap-in } 585826d2766SKlap-in 5866de3759aSAndreas Gohr if(is_array($more)) { 5870f4e0092SChristopher Smith // add token for resized images 588357c9a39SDamien Regad $w = isset($more['w']) ? $more['w'] : null; 589357c9a39SDamien Regad $h = isset($more['h']) ? $more['h'] : null; 59098fe1ac9SDamien Regad if($w || $h || $isexternalimage){ 591357c9a39SDamien Regad $more['tok'] = media_get_token($id, $w, $h); 5920f4e0092SChristopher Smith } 5938c08db0aSAndreas Gohr // strip defaults for shorter URLs 5948c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 595443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 596443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 5978c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 59878b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 599b174aeaeSchris $more = buildURLparams($more, $sep); 6006de3759aSAndreas Gohr } else { 6015e7db1e2SChristopher Smith $matches = array(); 602cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6035e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6045e7db1e2SChristopher Smith foreach ($matches as $match){ 6055e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6065e7db1e2SChristopher Smith } 607cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 608cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6095e7db1e2SChristopher Smith } 6108c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6118c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 612b174aeaeSchris $more = str_replace(',', $sep, $more); 6136de3759aSAndreas Gohr } 6146de3759aSAndreas Gohr 61555b2b31bSAndreas Gohr if($abs) { 61655b2b31bSAndreas Gohr $xlink = DOKU_URL; 61755b2b31bSAndreas Gohr } else { 6186de3759aSAndreas Gohr $xlink = DOKU_BASE; 61955b2b31bSAndreas Gohr } 6206de3759aSAndreas Gohr 6216de3759aSAndreas Gohr // external URLs are always direct without rewriting 622826d2766SKlap-in if($isexternalimage) { 6236de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 624cc036f74SKlap-in $xlink .= '?'.$more; 625b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6266de3759aSAndreas Gohr return $xlink; 6276de3759aSAndreas Gohr } 6286de3759aSAndreas Gohr 6296de3759aSAndreas Gohr $id = idfilter($id); 6306de3759aSAndreas Gohr 6316de3759aSAndreas Gohr // decide on scriptname 6326de3759aSAndreas Gohr if($direct) { 6336de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6346de3759aSAndreas Gohr $script = '_media'; 6356de3759aSAndreas Gohr } else { 6366de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6376de3759aSAndreas Gohr } 6386de3759aSAndreas Gohr } else { 6396de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6406de3759aSAndreas Gohr $script = '_detail'; 6416de3759aSAndreas Gohr } else { 6426de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6436de3759aSAndreas Gohr } 6446de3759aSAndreas Gohr } 6456de3759aSAndreas Gohr 6466de3759aSAndreas Gohr // build URL based on rewrite mode 6476de3759aSAndreas Gohr if($conf['userewrite']) { 6486de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6496de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6506de3759aSAndreas Gohr } else { 6516de3759aSAndreas Gohr if($more) { 652a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 653b174aeaeSchris $xlink .= $sep.'media='.$id; 6546de3759aSAndreas Gohr } else { 655a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6566de3759aSAndreas Gohr } 6576de3759aSAndreas Gohr } 6586de3759aSAndreas Gohr 6596de3759aSAndreas Gohr return $xlink; 6606de3759aSAndreas Gohr} 6616de3759aSAndreas Gohr 6626de3759aSAndreas Gohr/** 66325ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 66415fae107Sandi * 66525ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 66625ca5b17SAndreas Gohr * 66715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 668140cfbcdSGerrit Uitslag * 669140cfbcdSGerrit Uitslag * @return string 670f3f0262cSandi */ 67125ca5b17SAndreas Gohrfunction script() { 672ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 673f3f0262cSandi} 674f3f0262cSandi 675f3f0262cSandi/** 67615fae107Sandi * Spamcheck against wordlist 67715fae107Sandi * 678f3f0262cSandi * Checks the wikitext against a list of blocked expressions 679f3f0262cSandi * returns true if the text contains any bad words 68015fae107Sandi * 681e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 682e403cc58SMichael Klier * 683e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 684e403cc58SMichael Klier * and gain information about the user who was blocked. 685e403cc58SMichael Klier * 686e403cc58SMichael Klier * Event data: 687e403cc58SMichael Klier * data['matches'] - array of matches 688e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 689e403cc58SMichael Klier * [ip] - ip address 690e403cc58SMichael Klier * [user] - username (if logged in) 691e403cc58SMichael Klier * [mail] - mail address (if logged in) 692e403cc58SMichael Klier * [name] - real name (if logged in) 693e403cc58SMichael Klier * 69415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6956dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 696140cfbcdSGerrit Uitslag * 6976dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 6986dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 699f3f0262cSandi */ 7006dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 701f3f0262cSandi global $TEXT; 7026dffa0e0SAndreas Gohr global $PRE; 7036dffa0e0SAndreas Gohr global $SUF; 704e0086ca2SAndreas Gohr global $SUM; 705f3f0262cSandi global $conf; 706e403cc58SMichael Klier global $INFO; 707585bf44eSChristopher Smith /* @var Input $INPUT */ 708585bf44eSChristopher Smith global $INPUT; 709f3f0262cSandi 710f3f0262cSandi if(!$conf['usewordblock']) return false; 711f3f0262cSandi 712e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7136dffa0e0SAndreas Gohr 714041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 71564159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 71664159a61SAndreas Gohr $text = preg_replace( 71764159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 71864159a61SAndreas Gohr '\1http://\2 \2\3', 71964159a61SAndreas Gohr $text 72064159a61SAndreas Gohr ); 72164159a61SAndreas Gohr // phpcs:enable 722041d1964SAndreas Gohr 723b9ac8716Schris $wordblocks = getWordblocks(); 7243e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 7253e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 7263e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 7273e2965d7Sandi // backreferences are used - the maximum is 99 7283e2965d7Sandi // this is very bad performancewise and may even be too high still 7293e2965d7Sandi $chunksize = 40; 7303e2965d7Sandi } else { 731a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7323e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 733a51d08efSAndreas Gohr $chunksize = 200; 7343e2965d7Sandi } 735b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 736f3f0262cSandi $re = array(); 73749eb6e38SAndreas Gohr // build regexp from blocks 738f3f0262cSandi foreach($blocks as $block) { 739f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 740f3f0262cSandi $block = trim($block); 741f3f0262cSandi if(empty($block)) continue; 742f3f0262cSandi $re[] = $block; 743f3f0262cSandi } 744e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 745e403cc58SMichael Klier // prepare event data 74659bc3b48SGerrit Uitslag $data = array(); 747e403cc58SMichael Klier $data['matches'] = $matches; 748585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 749585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 750585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 751e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 752e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 753e403cc58SMichael Klier } 754bad6fc0dSAndreas Gohr $callback = function () { 755bad6fc0dSAndreas Gohr return true; 756bad6fc0dSAndreas Gohr }; 757cbb44eabSAndreas Gohr return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 758b9ac8716Schris } 759703f6fdeSandi } 760f3f0262cSandi return false; 761f3f0262cSandi} 762f3f0262cSandi 763f3f0262cSandi/** 76415fae107Sandi * Return the IP of the client 76515fae107Sandi * 7666d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 76715fae107Sandi * 7686d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 7696d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 7706d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 7716d8affe6SAndreas Gohr * headers 7726d8affe6SAndreas Gohr * 77315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 774140cfbcdSGerrit Uitslag * 7753272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 7763272d797SAndreas Gohr * @return string 777f3f0262cSandi */ 7786d8affe6SAndreas Gohrfunction clientIP($single = false) { 779585bf44eSChristopher Smith /* @var Input $INPUT */ 780925105e8SPhy global $INPUT, $conf; 781585bf44eSChristopher Smith 7826d8affe6SAndreas Gohr $ip = array(); 783585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 784585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 785585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 786585bf44eSChristopher Smith } 787585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 788585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 789585bf44eSChristopher Smith } 7906d8affe6SAndreas Gohr 791dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 792dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 793dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 794dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 795dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 796dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 797dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 798dc14c6d1SGuy Brand $IPv6Address = 799dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 800dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 801dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 802dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 803dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 804dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 805dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 806dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 807dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 808dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 809dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 810dc14c6d1SGuy Brand 8116d8affe6SAndreas Gohr // remove any non-IP stuff 8126d8affe6SAndreas Gohr $cnt = count($ip); 8134ff28443Schris $match = array(); 8146d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 815dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 8164ff28443Schris $ip[$i] = $match[0]; 8174ff28443Schris } else { 8184ff28443Schris $ip[$i] = ''; 8194ff28443Schris } 8206d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 821f3f0262cSandi } 8226d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 823056bf31fSDamien Regad if(empty($ip) || !$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8246d8affe6SAndreas Gohr 8256d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 8266d8affe6SAndreas Gohr 827925105e8SPhy // skip trusted local addresses 8286d8affe6SAndreas Gohr foreach($ip as $i) { 829925105e8SPhy if(!empty($conf['trustedproxy']) && preg_match('/'.$conf['trustedproxy'].'/', $i)) { 8306d8affe6SAndreas Gohr continue; 8316d8affe6SAndreas Gohr } else { 8326d8affe6SAndreas Gohr return $i; 8336d8affe6SAndreas Gohr } 8346d8affe6SAndreas Gohr } 835925105e8SPhy 836925105e8SPhy // still here? just use the last address 837925105e8SPhy // this case all ips in the list are trusted 838925105e8SPhy return $ip[count($ip)-1]; 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 10168cbc5ee8SAndreas Gohr if(!\dokuwiki\Utf8\Clean::isUtf8($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 1085e1d9dcc8SAndreas Gohr $evt = new 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@', 1149a3db0ab0SSimon Lees '@!CURNS@', 1150a3db0ab0SSimon Lees '@!!CURNS@', 1151a3db0ab0SSimon Lees '@!CURNS!@', 115226ece5a7SAndreas Gohr '@FILE@', 115326ece5a7SAndreas Gohr '@!FILE@', 115426ece5a7SAndreas Gohr '@!FILE!@', 115526ece5a7SAndreas Gohr '@PAGE@', 115626ece5a7SAndreas Gohr '@!PAGE@', 115726ece5a7SAndreas Gohr '@!!PAGE@', 115826ece5a7SAndreas Gohr '@!PAGE!@', 115926ece5a7SAndreas Gohr '@USER@', 116026ece5a7SAndreas Gohr '@NAME@', 116126ece5a7SAndreas Gohr '@MAIL@', 116226ece5a7SAndreas Gohr '@DATE@', 116326ece5a7SAndreas Gohr ), 116426ece5a7SAndreas Gohr array( 116526ece5a7SAndreas Gohr $id, 116626ece5a7SAndreas Gohr getNS($id), 11678a7bcf66SShota Miyazaki curNS($id), 1168c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst(curNS($id)), 1169c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords(curNS($id)), 1170c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper(curNS($id)), 117126ece5a7SAndreas Gohr $file, 11728cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($file), 11738cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($file), 117426ece5a7SAndreas Gohr $page, 11758cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($page), 11768cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords($page), 11778cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($page), 1178585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11793e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 11803e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 118126ece5a7SAndreas Gohr $conf['dformat'], 11823272d797SAndreas Gohr ), $tpl 11833272d797SAndreas Gohr ); 118426ece5a7SAndreas Gohr 11857d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1186bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1187bad6fc0dSAndreas Gohr '/%./', 1188bad6fc0dSAndreas Gohr function ($m) { 1189bad6fc0dSAndreas Gohr return strftime($m[0]); 1190bad6fc0dSAndreas Gohr }, 1191bad6fc0dSAndreas Gohr $tpl 1192bad6fc0dSAndreas Gohr ); 1193d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1194a15ce62dSEsther Brunner return $tpl; 11957146cee2SAndreas Gohr} 11967146cee2SAndreas Gohr 11977146cee2SAndreas Gohr/** 119815fae107Sandi * Returns the raw Wiki Text in three slices. 119915fae107Sandi * 120015fae107Sandi * The range parameter needs to have the form "from-to" 120115cfe303Sandi * and gives the range of the section in bytes - no 120215cfe303Sandi * UTF-8 awareness is needed. 1203f3f0262cSandi * The returned order is prefix, section and suffix. 120415fae107Sandi * 120515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1206140cfbcdSGerrit Uitslag * 1207140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1208140cfbcdSGerrit Uitslag * @param string $id page id 1209140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 121042ea7f44SGerrit Uitslag * @return string[] with three slices 1211f3f0262cSandi */ 1212f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1213cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1214f3f0262cSandi 121580fcb268SAdrian Lang // Parse range 121680fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 121780fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 121880fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 121980fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 122080fcb268SAdrian Lang 122159bc3b48SGerrit Uitslag $slices = array(); 122280fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 122380fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 122415cfe303Sandi $slices[2] = substr($text, $to); 1225f3f0262cSandi return $slices; 1226f3f0262cSandi} 1227f3f0262cSandi 1228f3f0262cSandi/** 122915fae107Sandi * Joins wiki text slices 123015fae107Sandi * 123180fcb268SAdrian Lang * function to join the text slices. 1232f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1233f3f0262cSandi * lines between sections if needed (used on saving). 123415fae107Sandi * 123515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1236140cfbcdSGerrit Uitslag * 1237140cfbcdSGerrit Uitslag * @param string $pre prefix 1238140cfbcdSGerrit Uitslag * @param string $text text in the middle 1239140cfbcdSGerrit Uitslag * @param string $suf suffix 1240140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1241140cfbcdSGerrit Uitslag * @return string 1242f3f0262cSandi */ 1243f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1244f3f0262cSandi if($pretty) { 124580fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12463272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12473272d797SAndreas Gohr ) { 124880fcb268SAdrian Lang $pre .= "\n"; 124980fcb268SAdrian Lang } 125080fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12513272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12523272d797SAndreas Gohr ) { 125380fcb268SAdrian Lang $text .= "\n"; 125480fcb268SAdrian Lang } 1255f3f0262cSandi } 1256f3f0262cSandi 1257f3f0262cSandi return $pre.$text.$suf; 1258f3f0262cSandi} 1259f3f0262cSandi 1260f3f0262cSandi/** 1261b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1262b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1263b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1264b24d9195SAndreas Gohr * 1265b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1266b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1267b24d9195SAndreas Gohr * 1268b24d9195SAndreas Gohr * @param string $id the page ID 1269*b24e9c4aSSatoshi Sahara * @deprecated YYYY-MM-DD 1270b24d9195SAndreas Gohr */ 1271b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 1272*b24e9c4aSSatoshi Sahara //dbg_deprecated(\dokuwiki\File\PageFile::class .'::detectExternalEdit()'); 1273*b24e9c4aSSatoshi Sahara (new PageFile($id))->detectExternalEdit(); 1274b24d9195SAndreas Gohr} 1275b24d9195SAndreas Gohr 1276b24d9195SAndreas Gohr/** 1277a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1278a701424fSBen Coburn * Also directs changelog and attic updates. 127915fae107Sandi * 128015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 128171726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1282140cfbcdSGerrit Uitslag * 1283140cfbcdSGerrit Uitslag * @param string $id page id 1284140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1285140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1286140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1287f3f0262cSandi */ 1288b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1289585bf44eSChristopher Smith 1290*b24e9c4aSSatoshi Sahara // get COMMON_WIKIPAGE_SAVE event data 1291*b24e9c4aSSatoshi Sahara $data = (new PageFile($id))->saveWikiText($text, $summary, $minor); 1292ac3ed4afSGerrit Uitslag 129326a0801fSAndreas Gohr // send notify mails 1294d5824ab9SSatoshi Sahara notify($id, 'admin', $data['oldRevision'], $data['summary'], $minor, $data['newRevision']); 1295d5824ab9SSatoshi Sahara notify($id, 'subscribers', $data['oldRevision'], $data['summary'], $minor, $data['newRevision']); 1296f3f0262cSandi 12972eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1298fe9ec250SChris Smith if (useHeading('content')) { 129907ff0babSMichael Hamann $pages = ft_backlinks($id, true); 13002eccbdaaSGina Haeussge foreach ($pages as $page) { 13010db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 13022eccbdaaSGina Haeussge $cache->removeCache(); 13032eccbdaaSGina Haeussge } 13042eccbdaaSGina Haeussge } 1305f3f0262cSandi} 1306f3f0262cSandi 1307f3f0262cSandi/** 1308d5824ab9SSatoshi Sahara * moves the current version to the attic and returns its revision date 130915fae107Sandi * 131015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1311140cfbcdSGerrit Uitslag * 1312140cfbcdSGerrit Uitslag * @param string $id page id 1313140cfbcdSGerrit Uitslag * @return int|string revision timestamp 1314*b24e9c4aSSatoshi Sahara * @deprecated YYYY-MM-DD 1315f3f0262cSandi */ 1316f3f0262cSandifunction saveOldRevision($id) { 1317*b24e9c4aSSatoshi Sahara //dbg_deprecated(\dokuwiki\File\PageFile::class .'::saveOldRevision()'); 1318*b24e9c4aSSatoshi Sahara return (new PageFile($id))->saveOldRevision(); 1319f3f0262cSandi} 1320f3f0262cSandi 1321f3f0262cSandi/** 1322fde10de4SAdrian Lang * Sends a notify mail on page change or registration 132326a0801fSAndreas Gohr * 132426a0801fSAndreas Gohr * @param string $id The changed page 1325fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 13263272d797SAndreas Gohr * @param int|string $rev Old page revision 132726a0801fSAndreas Gohr * @param string $summary What changed 132890033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 132942ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 133083734cddSPhy * @param int|string $current_rev New page revision 13313272d797SAndreas Gohr * @return bool 1332140cfbcdSGerrit Uitslag * 133315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1334f3f0262cSandi */ 133583734cddSPhyfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array(), $current_rev = false) { 1336f3f0262cSandi global $conf; 1337585bf44eSChristopher Smith /* @var Input $INPUT */ 1338585bf44eSChristopher Smith global $INPUT; 1339b158d625SSteven Danz 13406df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 134126a0801fSAndreas Gohr if($who == 'admin') { 13423272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 13432ed38036SAndreas Gohr $tpl = 'mailtext'; 134426a0801fSAndreas Gohr $to = $conf['notify']; 134526a0801fSAndreas Gohr } elseif($who == 'subscribers') { 134684c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1347585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 13480bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 1349cbb44eabSAndreas Gohr Event::createAndTrigger( 13503272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1351c8cc4053SAndreas Gohr array(new SubscriberManager(), 'notifyAddresses') 13523272d797SAndreas Gohr ); 13532ed38036SAndreas Gohr $to = $data['addresslist']; 13542ed38036SAndreas Gohr if(empty($to)) return false; 13552ed38036SAndreas Gohr $tpl = 'subscr_single'; 135626a0801fSAndreas Gohr } else { 13573272d797SAndreas Gohr return false; //just to be safe 135826a0801fSAndreas Gohr } 135926a0801fSAndreas Gohr 13606df843eeSAndreas Gohr // prepare content 1361704a815fSMichael Große $subscription = new PageSubscriptionSender(); 136283734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1363f3f0262cSandi} 13642ed38036SAndreas Gohr 136515fae107Sandi/** 136671f7bde7SAndreas Gohr * extracts the query from a search engine referrer 136715fae107Sandi * 136815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 136971f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1370140cfbcdSGerrit Uitslag * 1371140cfbcdSGerrit Uitslag * @return array|string 1372f3f0262cSandi */ 1373f3f0262cSandifunction getGoogleQuery() { 1374585bf44eSChristopher Smith /* @var Input $INPUT */ 1375585bf44eSChristopher Smith global $INPUT; 1376585bf44eSChristopher Smith 1377585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1378c66972f2SAdrian Lang return ''; 1379c66972f2SAdrian Lang } 1380585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1381f3f0262cSandi 1382079b3ac1SAndreas Gohr // only handle common SEs 1383079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1384e4d8a516SKazutaka Miyasaka 1385079b3ac1SAndreas Gohr $query = array(); 1386f3f0262cSandi parse_str($url['query'], $query); 1387e4d8a516SKazutaka Miyasaka 1388c66972f2SAdrian Lang $q = ''; 1389079b3ac1SAndreas Gohr if(isset($query['q'])){ 1390079b3ac1SAndreas Gohr $q = $query['q']; 1391079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1392079b3ac1SAndreas Gohr $q = $query['p']; 1393079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1394079b3ac1SAndreas Gohr $q = $query['query']; 1395079b3ac1SAndreas Gohr } 1396079b3ac1SAndreas Gohr $q = trim($q); 1397f3f0262cSandi 1398079b3ac1SAndreas Gohr if(!$q) return ''; 1399c7dc833bSPhy // ignore if query includes a full URL 1400c7dc833bSPhy if(strpos($q, '//') !== false) return ''; 14016531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1402f93b3b50SAndreas Gohr return $q; 1403f3f0262cSandi} 1404f3f0262cSandi 1405f3f0262cSandi/** 1406f3f0262cSandi * Return the human readable size of a file 1407f3f0262cSandi * 1408f3f0262cSandi * @param int $size A file size 1409f3f0262cSandi * @param int $dec A number of decimal places 141074160ca1SGerrit Uitslag * @return string human readable size 1411140cfbcdSGerrit Uitslag * 1412f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1413f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1414f3f0262cSandi * @version 1.0.0 1415f3f0262cSandi */ 1416f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1417f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1418f3f0262cSandi $count = count($sizes); 1419f3f0262cSandi $i = 0; 1420f3f0262cSandi 1421f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1422f3f0262cSandi $size /= 1024; 1423f3f0262cSandi $i++; 1424f3f0262cSandi } 1425f3f0262cSandi 1426ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1427f3f0262cSandi} 1428f3f0262cSandi 142915fae107Sandi/** 1430c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1431c57e365eSAndreas Gohr * 1432c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1433140cfbcdSGerrit Uitslag * 1434140cfbcdSGerrit Uitslag * @param int $dt timestamp 1435140cfbcdSGerrit Uitslag * @return string 1436c57e365eSAndreas Gohr */ 1437c57e365eSAndreas Gohrfunction datetime_h($dt) { 1438c57e365eSAndreas Gohr global $lang; 1439c57e365eSAndreas Gohr 1440c57e365eSAndreas Gohr $ago = time() - $dt; 1441c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1442c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1443c57e365eSAndreas Gohr } 1444c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1445c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1446c57e365eSAndreas Gohr } 1447c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1448c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1449c57e365eSAndreas Gohr } 1450c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1451c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1452c57e365eSAndreas Gohr } 1453c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1454c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1455c57e365eSAndreas Gohr } 1456c57e365eSAndreas Gohr if($ago > 60 * 2) { 1457c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1458c57e365eSAndreas Gohr } 1459c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1460c57e365eSAndreas Gohr} 1461c57e365eSAndreas Gohr 1462c57e365eSAndreas Gohr/** 1463f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1464f2263577SAndreas Gohr * 1465f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1466f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1467f2263577SAndreas Gohr * 1468f2263577SAndreas Gohr * @see datetime_h 1469f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1470140cfbcdSGerrit Uitslag * 1471140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1472140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1473140cfbcdSGerrit Uitslag * @return string 1474f2263577SAndreas Gohr */ 1475f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1476f2263577SAndreas Gohr global $conf; 1477f2263577SAndreas Gohr 1478f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1479f2263577SAndreas Gohr $dt = (int) $dt; 1480f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1481f2263577SAndreas Gohr 1482f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1483f2263577SAndreas Gohr return strftime($format, $dt); 1484f2263577SAndreas Gohr} 1485f2263577SAndreas Gohr 1486f2263577SAndreas Gohr/** 1487c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1488c4f79b71SMichael Hamann * 1489c4f79b71SMichael Hamann * @author <ungu at terong dot com> 149059752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1491140cfbcdSGerrit Uitslag * 14927e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 14933272d797SAndreas Gohr * @return string 1494c4f79b71SMichael Hamann */ 1495c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1496c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1497c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1498c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1499c4f79b71SMichael Hamann $date_mod .= $time_zone; 1500c4f79b71SMichael Hamann return $date_mod; 1501c4f79b71SMichael Hamann} 1502c4f79b71SMichael Hamann 1503c4f79b71SMichael Hamann/** 150400a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 150500a7b5adSEsther Brunner * 150600a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 150700a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1508140cfbcdSGerrit Uitslag * 1509140cfbcdSGerrit Uitslag * @param string $email email address 1510140cfbcdSGerrit Uitslag * @return string 151100a7b5adSEsther Brunner */ 151200a7b5adSEsther Brunnerfunction obfuscate($email) { 151300a7b5adSEsther Brunner global $conf; 151400a7b5adSEsther Brunner 151500a7b5adSEsther Brunner switch($conf['mailguard']) { 151600a7b5adSEsther Brunner case 'visible' : 151700a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 151800a7b5adSEsther Brunner return strtr($email, $obfuscate); 151900a7b5adSEsther Brunner 152000a7b5adSEsther Brunner case 'hex' : 1521c1ec88ceSAndreas Gohr return \dokuwiki\Utf8\Conversion::toHtml($email, true); 152200a7b5adSEsther Brunner 152300a7b5adSEsther Brunner case 'none' : 152400a7b5adSEsther Brunner default : 152500a7b5adSEsther Brunner return $email; 152600a7b5adSEsther Brunner } 152700a7b5adSEsther Brunner} 152800a7b5adSEsther Brunner 152900a7b5adSEsther Brunner/** 153089541d4bSAndreas Gohr * Removes quoting backslashes 153189541d4bSAndreas Gohr * 153289541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1533140cfbcdSGerrit Uitslag * 1534140cfbcdSGerrit Uitslag * @param string $string 1535140cfbcdSGerrit Uitslag * @param string $char backslashed character 1536140cfbcdSGerrit Uitslag * @return string 153789541d4bSAndreas Gohr */ 153889541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 153989541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 154089541d4bSAndreas Gohr} 154189541d4bSAndreas Gohr 154273038c47SAndreas Gohr/** 154373038c47SAndreas Gohr * Convert php.ini shorthands to byte 154473038c47SAndreas Gohr * 1545a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1546140cfbcdSGerrit Uitslag * 1547a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1548a81f3d99SAndreas Gohr * 1549a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1550a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1551a81f3d99SAndreas Gohr * @return int 155273038c47SAndreas Gohr */ 1553a81f3d99SAndreas Gohrfunction php_to_byte($value) { 1554f5c0c80bSAndreas Gohr switch (strtoupper(substr($value,-1))) { 155573038c47SAndreas Gohr case 'G': 1556a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024 * 1024; 155773038c47SAndreas Gohr break; 155873038c47SAndreas Gohr case 'M': 1559a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024; 1560a81f3d99SAndreas Gohr break; 156173038c47SAndreas Gohr case 'K': 1562a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024; 156373038c47SAndreas Gohr break; 15649eeeb775SAndreas Gohr default: 1565a81f3d99SAndreas Gohr $ret = intval($value); 156649cbd23eSOtto Vainio break; 156773038c47SAndreas Gohr } 156873038c47SAndreas Gohr return $ret; 156973038c47SAndreas Gohr} 157073038c47SAndreas Gohr 1571546d3a99SAndreas Gohr/** 1572546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1573140cfbcdSGerrit Uitslag * 1574140cfbcdSGerrit Uitslag * @param string $string 1575140cfbcdSGerrit Uitslag * @return string 1576546d3a99SAndreas Gohr */ 1577546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1578546d3a99SAndreas Gohr return preg_quote($string, '/'); 1579546d3a99SAndreas Gohr} 158073038c47SAndreas Gohr 1581bd2f6c2fSAndreas Gohr/** 1582bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1583bd2f6c2fSAndreas Gohr * 1584c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1585bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1586bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1587bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1588bd2f6c2fSAndreas Gohr * 1589bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1590bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1591bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1592bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1593bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 15943272d797SAndreas Gohr * @return string 1595bd2f6c2fSAndreas Gohr */ 1596a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 15978cbc5ee8SAndreas Gohr $max = $max - \dokuwiki\Utf8\PhpString::strlen($keep); 1598bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 15998cbc5ee8SAndreas Gohr $len = \dokuwiki\Utf8\PhpString::strlen($short); 1600bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1601bd2f6c2fSAndreas Gohr $half = floor($max / 2); 16026ce3e5f8SAndreas Gohr return $keep . 16036ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, 0, $half - 1) . 16046ce3e5f8SAndreas Gohr $char . 16056ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, $len - $half); 1606bd2f6c2fSAndreas Gohr} 1607bd2f6c2fSAndreas Gohr 1608dc58b6f4SAndy Webber/** 1609dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1610dc58b6f4SAndy Webber * in page footer and recent changes pages 1611dc58b6f4SAndy Webber * 1612b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 161315f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1614c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 161515f3bc49SGerrit Uitslag * 1616dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1617dc58b6f4SAndy Webber */ 161815f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1619cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1620dc58b6f4SAndy Webber} 1621dc58b6f4SAndy Webber 162260a396c8SGerrit Uitslag/** 162360a396c8SGerrit Uitslag * Returns users realname w/o link 162460a396c8SGerrit Uitslag * 1625f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 162615f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1627c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 162860a396c8SGerrit Uitslag * 162960a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 163060a396c8SGerrit Uitslag */ 1631cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 163260a396c8SGerrit Uitslag global $conf, $INFO; 1633e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 163460a396c8SGerrit Uitslag global $auth; 163530f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 163630f6ec4bSGerrit Uitslag global $INPUT; 163760a396c8SGerrit Uitslag 163860a396c8SGerrit Uitslag // prepare initial event data 163960a396c8SGerrit Uitslag $data = array( 164060a396c8SGerrit Uitslag 'username' => $username, // the unique user name 164160a396c8SGerrit Uitslag 'name' => '', 164260a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 164360a396c8SGerrit Uitslag 'target' => '', 164460a396c8SGerrit Uitslag 'pre' => '', 164560a396c8SGerrit Uitslag 'suf' => '', 164660a396c8SGerrit Uitslag 'style' => '', 164760a396c8SGerrit Uitslag 'more' => '', 164860a396c8SGerrit Uitslag 'url' => '', 164960a396c8SGerrit Uitslag 'title' => '', 165060a396c8SGerrit Uitslag 'class' => '' 165160a396c8SGerrit Uitslag ), 16524d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 165315f3bc49SGerrit Uitslag 'textonly' => $textonly 165460a396c8SGerrit Uitslag ); 165562c8004eSGerrit Uitslag if($username === null) { 165630f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 165715f3bc49SGerrit Uitslag if($textonly){ 165815f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 165915f3bc49SGerrit Uitslag }else { 166064159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 166164159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 166260a396c8SGerrit Uitslag } 166315f3bc49SGerrit Uitslag } 166460a396c8SGerrit Uitslag 1665e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 166660a396c8SGerrit Uitslag if($evt->advise_before(true)) { 166760a396c8SGerrit Uitslag if(empty($data['name'])) { 166860a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 166965833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1670dc58b6f4SAndy Webber switch($conf['showuseras']) { 1671dc58b6f4SAndy Webber case 'username': 16727f081821SGerrit Uitslag case 'username_link': 167315f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 167460a396c8SGerrit Uitslag break; 1675dc58b6f4SAndy Webber case 'email': 1676dc58b6f4SAndy Webber case 'email_link': 167760a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 167860a396c8SGerrit Uitslag break; 1679dc58b6f4SAndy Webber } 168065833968SGerrit Uitslag } else { 168165833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 168260a396c8SGerrit Uitslag } 168360a396c8SGerrit Uitslag } 16847f081821SGerrit Uitslag 16857f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 16867f081821SGerrit Uitslag static $xhtml_renderer = null; 16877f081821SGerrit Uitslag 168815f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 16897f081821SGerrit Uitslag 16907f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 169160a396c8SGerrit Uitslag if(!isset($info)) { 169260a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 169360a396c8SGerrit Uitslag } 169460a396c8SGerrit Uitslag if(isset($info) && $info) { 16957f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 169660a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1697dc58b6f4SAndy Webber } else { 16987f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 16997f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 17007f081821SGerrit Uitslag } 17017f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 17027f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 17037f081821SGerrit Uitslag } 17047f081821SGerrit Uitslag $shortcut = 'user'; 1705533772e1SGerrit Uitslag $exists = null; 17066496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 17072a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 17086496c33fSGerrit Uitslag if($exists !== null) { 17096496c33fSGerrit Uitslag if($exists) { 17106496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 17116496c33fSGerrit Uitslag } else { 17126496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 17136496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 17146496c33fSGerrit Uitslag } 17156496c33fSGerrit Uitslag } 1716dc58b6f4SAndy Webber } 1717dc58b6f4SAndy Webber } else { 171815f3bc49SGerrit Uitslag $data['textonly'] = true; 1719dc58b6f4SAndy Webber } 172060a396c8SGerrit Uitslag 172160a396c8SGerrit Uitslag } else { 172215f3bc49SGerrit Uitslag $data['textonly'] = true; 172360a396c8SGerrit Uitslag } 172460a396c8SGerrit Uitslag } 172560a396c8SGerrit Uitslag 172615f3bc49SGerrit Uitslag if($data['textonly']) { 17274d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 172860a396c8SGerrit Uitslag } else { 172960a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 173060a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 173160a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 173260a396c8SGerrit Uitslag } 17334d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 173460a396c8SGerrit Uitslag } 173560a396c8SGerrit Uitslag } 173660a396c8SGerrit Uitslag $evt->advise_after(); 173760a396c8SGerrit Uitslag unset($evt); 173860a396c8SGerrit Uitslag 17394d5fc927SGerrit Uitslag return $data['userlink']; 1740066fee30SAndreas Gohr} 1741066fee30SAndreas Gohr 1742066fee30SAndreas Gohr/** 1743066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1744066fee30SAndreas Gohr * When no image exists, returns an empty string 1745066fee30SAndreas Gohr * 1746066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1747140cfbcdSGerrit Uitslag * 1748066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 17493272d797SAndreas Gohr * @return string 1750066fee30SAndreas Gohr */ 1751066fee30SAndreas Gohrfunction license_img($type) { 1752066fee30SAndreas Gohr global $license; 1753066fee30SAndreas Gohr global $conf; 1754066fee30SAndreas Gohr if(!$conf['license']) return ''; 1755066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1756066fee30SAndreas Gohr $try = array(); 1757066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1758066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1759066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1760066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1761066fee30SAndreas Gohr } 1762066fee30SAndreas Gohr foreach($try as $src) { 176379e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1764066fee30SAndreas Gohr } 1765066fee30SAndreas Gohr return ''; 1766dc58b6f4SAndy Webber} 1767dc58b6f4SAndy Webber 176813c08e2fSMichael Klier/** 176913c08e2fSMichael Klier * Checks if the given amount of memory is available 177013c08e2fSMichael Klier * 177113c08e2fSMichael Klier * If the memory_get_usage() function is not available the 177213c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 177313c08e2fSMichael Klier * 177413c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 177513c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 17763272d797SAndreas Gohr * 17773272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1778140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 17793272d797SAndreas Gohr * @return bool 178013c08e2fSMichael Klier */ 178113c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 178213c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 178313c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1784985d6187SElenchus if($limit == -1) return true; // unlimited 178513c08e2fSMichael Klier 178613c08e2fSMichael Klier // parse limit to bytes 178713c08e2fSMichael Klier $limit = php_to_byte($limit); 178813c08e2fSMichael Klier 178913c08e2fSMichael Klier // get used memory if possible 179013c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 179113c08e2fSMichael Klier $used = memory_get_usage(); 179249eb6e38SAndreas Gohr } else { 179349eb6e38SAndreas Gohr $used = $bytes; 179413c08e2fSMichael Klier } 179513c08e2fSMichael Klier 179613c08e2fSMichael Klier if($used + $mem > $limit) { 179713c08e2fSMichael Klier return false; 179813c08e2fSMichael Klier } 179913c08e2fSMichael Klier 180013c08e2fSMichael Klier return true; 180113c08e2fSMichael Klier} 180213c08e2fSMichael Klier 1803af2408d5SAndreas Gohr/** 1804af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1805af2408d5SAndreas Gohr * 1806af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1807af2408d5SAndreas Gohr * 1808af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1809af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1810140cfbcdSGerrit Uitslag * 1811140cfbcdSGerrit Uitslag * @param string $url url being directed to 1812af2408d5SAndreas Gohr */ 1813af2408d5SAndreas Gohrfunction send_redirect($url) { 181498ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 181598ca30d2SAndreas Gohr 1816585bf44eSChristopher Smith /* @var Input $INPUT */ 1817585bf44eSChristopher Smith global $INPUT; 1818585bf44eSChristopher Smith 18190181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 18200181f021SAndreas Gohr global $MSG; 18210181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 18220181f021SAndreas Gohr //reopen session, store data and close session again 18230181f021SAndreas Gohr @session_start(); 18240181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 18250181f021SAndreas Gohr } 18260181f021SAndreas Gohr 1827d4869846SAndreas Gohr // always close the session 1828d4869846SAndreas Gohr session_write_close(); 1829d4869846SAndreas Gohr 1830af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1831585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1832585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1833585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 18343272d797SAndreas Gohr $matches[1] < 6 18353272d797SAndreas Gohr ) { 1836af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1837af2408d5SAndreas Gohr } else { 1838af2408d5SAndreas Gohr header('Location: '.$url); 1839af2408d5SAndreas Gohr } 184081781cb6SAndreas Gohr 1841572dc222SLarsDW223 // no exits during unit tests 184227c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 184327c0c399SAndreas Gohr // pass info about the redirect back to the test suite 184427c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 184527c0c399SAndreas Gohr if($testRequest !== null) { 184627c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 184727c0c399SAndreas Gohr } 1848572dc222SLarsDW223 return; 1849572dc222SLarsDW223 } 185027c0c399SAndreas Gohr 1851af2408d5SAndreas Gohr exit; 1852af2408d5SAndreas Gohr} 1853af2408d5SAndreas Gohr 18545b75cd1fSAdrian Lang/** 18555b75cd1fSAdrian Lang * Validate a value using a set of valid values 18565b75cd1fSAdrian Lang * 18575b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 18585b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 18595b75cd1fSAdrian Lang * default is specified, throws an exception. 18605b75cd1fSAdrian Lang * 18615b75cd1fSAdrian Lang * @param string $param The name of the parameter 18625b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 18635b75cd1fSAdrian Lang * be marked by the key “default”. 18645b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 18655b75cd1fSAdrian Lang * or $_GET) 18665b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 18675b75cd1fSAdrian Lang * 18683272d797SAndreas Gohr * @throws Exception 18693272d797SAndreas Gohr * @return mixed 18705b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 18715b75cd1fSAdrian Lang */ 18725b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 18735b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 18745b75cd1fSAdrian Lang return $array[$param]; 18755b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 18765b75cd1fSAdrian Lang return $valid_values['default']; 18775b75cd1fSAdrian Lang } else { 18785b75cd1fSAdrian Lang throw new Exception($exc); 18795b75cd1fSAdrian Lang } 18805b75cd1fSAdrian Lang} 18815b75cd1fSAdrian Lang 188263703ba5SAndreas Gohr/** 188363703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1884646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 1885140cfbcdSGerrit Uitslag * 1886140cfbcdSGerrit Uitslag * @param string $pref preference key 1887b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 1888140cfbcdSGerrit Uitslag * @return string preference value 188963703ba5SAndreas Gohr */ 1890554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1891646a531aSChristopher Smith $enc_pref = urlencode($pref); 189206c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1893554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 189463703ba5SAndreas Gohr $cnt = count($parts); 18951c3eca7dSPhy 18961c3eca7dSPhy // due to #2721 there might be duplicate entries, 18971c3eca7dSPhy // so we read from the end 18981c3eca7dSPhy for($i = $cnt-2; $i >= 0; $i -= 2) { 1899646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1900646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1901554a8c9fSAdrian Lang } 1902554a8c9fSAdrian Lang } 1903554a8c9fSAdrian Lang } 1904554a8c9fSAdrian Lang return $default; 1905554a8c9fSAdrian Lang} 1906554a8c9fSAdrian Lang 19073c94d07bSAnika Henke/** 19083c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 190936ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 19103a970889SAnika Henke * Remove it by setting $val to false 1911140cfbcdSGerrit Uitslag * 1912140cfbcdSGerrit Uitslag * @param string $pref preference key 1913140cfbcdSGerrit Uitslag * @param string $val preference value 19143c94d07bSAnika Henke */ 19153c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 19163c94d07bSAnika Henke global $conf; 19173c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 19183c94d07bSAnika Henke $cookieVal = ''; 19193c94d07bSAnika Henke 19201c3eca7dSPhy if($orig !== false && ($orig !== $val)) { 19213c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 19223c94d07bSAnika Henke $cnt = count($parts); 192336ec377eSChristopher Smith // urlencode $pref for the comparison 192436ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 19251c3eca7dSPhy $seen = false; 19263c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 192736ec377eSChristopher Smith if ($parts[$i] == $enc_pref) { 19281c3eca7dSPhy if (!$seen){ 19293a970889SAnika Henke if ($val !== false) { 193036ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 19313a970889SAnika Henke } else { 19323a970889SAnika Henke unset($parts[$i]); 19333a970889SAnika Henke unset($parts[$i + 1]); 19343a970889SAnika Henke } 19351c3eca7dSPhy $seen = true; 19361c3eca7dSPhy } else { 19371c3eca7dSPhy // no break because we want to remove duplicate entries 19381c3eca7dSPhy unset($parts[$i]); 19391c3eca7dSPhy unset($parts[$i + 1]); 19401c3eca7dSPhy } 19413c94d07bSAnika Henke } 19423c94d07bSAnika Henke } 19433c94d07bSAnika Henke $cookieVal = implode('#', $parts); 19441c3eca7dSPhy } else if ($orig === false && $val !== false) { 1945c10f256aSDamien Regad $cookieVal = (isset($_COOKIE['DOKU_PREFS']) ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 194664159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 19473c94d07bSAnika Henke } 19483c94d07bSAnika Henke 194975e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 19505833995aSPhy if(defined('DOKU_UNITTEST')) { 19515833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 19525833995aSPhy }else{ 195375e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 19543c94d07bSAnika Henke } 19553c94d07bSAnika Henke} 19563c94d07bSAnika Henke 1957f8fb2d18SAndreas Gohr/** 1958f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 1959f8fb2d18SAndreas Gohr * 196042ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 1961f8fb2d18SAndreas Gohr */ 1962f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 1963f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 1964f8fb2d18SAndreas Gohr} 1965f8fb2d18SAndreas Gohr 19663c27983bSAndreas Gohr/** 196771de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 19683c27983bSAndreas Gohr * 19693c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 19703c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 19713c27983bSAndreas Gohr * files are embedded. 19723c27983bSAndreas Gohr * 197371de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 197471de5572SAndreas Gohr * 19753c27983bSAndreas Gohr * @param string $file full path to the SVG file 19763c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 197771de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 19783c27983bSAndreas Gohr */ 19794cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 19803c27983bSAndreas Gohr $file = trim($file); 19813c27983bSAndreas Gohr if($file === '') return false; 19823c27983bSAndreas Gohr if(!file_exists($file)) return false; 19833c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 19843c27983bSAndreas Gohr if(!is_readable($file)) return false; 19853c27983bSAndreas Gohr $content = file_get_contents($file); 19860849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 19870849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 19880849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 19890849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 19903c27983bSAndreas Gohr $content = trim($content); 19913c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 199271de5572SAndreas Gohr return $content; 19933c27983bSAndreas Gohr} 19943c27983bSAndreas Gohr 1995e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1996