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; 12b24e9c4aSSatoshi 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) { 29f7711f2bSAndreas Gohr return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, '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 171a58fcbbcSAndreas Gohr if(empty($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.'="'; 370f7711f2bSAndreas Gohr $url .= hsc($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 453bf8f8509SAndreas Gohr $id = (string) $id; 454bf8f8509SAndreas Gohr 455f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 456f3f0262cSandi $id = strtr($id, ':', '/'); 457f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 45858bedc8aSborekb $conf['userewrite'] && 459585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4603272d797SAndreas Gohr ) { 461f3f0262cSandi $id = strtr($id, ':', ';'); 462f3f0262cSandi } 46349c713a3Sandi if($ue) { 464b6c6979fSAndreas Gohr $id = rawurlencode($id); 465f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 466edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 467f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 46849c713a3Sandi } 469f3f0262cSandi return $id; 470f3f0262cSandi} 471f3f0262cSandi 472f3f0262cSandi/** 473ed7b5f09Sandi * This builds a link to a wikipage 47415fae107Sandi * 4754bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4766c7843b5Sandi * 47715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4784bc480e5SAndreas Gohr * 4794bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4804bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4814bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4824bc480e5SAndreas Gohr * @param string $separator parameter separator 4834bc480e5SAndreas Gohr * @return string 484f3f0262cSandi */ 48516f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 486f3f0262cSandi global $conf; 48716f15a81SDominik Eckelmann if(is_array($urlParameters)) { 4884bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 48964159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 49064159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 49164159a61SAndreas Gohr } 49216f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4936de3759aSAndreas Gohr } else { 49416f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4956de3759aSAndreas Gohr } 49616f15a81SDominik Eckelmann if($id === '') { 49716f15a81SDominik Eckelmann $id = $conf['start']; 49816f15a81SDominik Eckelmann } 499f3f0262cSandi $id = idfilter($id); 50016f15a81SDominik Eckelmann if($absolute) { 501ed7b5f09Sandi $xlink = DOKU_URL; 502ed7b5f09Sandi } else { 503ed7b5f09Sandi $xlink = DOKU_BASE; 504ed7b5f09Sandi } 505f3f0262cSandi 5066c7843b5Sandi if($conf['userewrite'] == 2) { 5076c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 50816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5096c7843b5Sandi } elseif($conf['userewrite']) { 510f3f0262cSandi $xlink .= $id; 51116f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 51240b5fb5bSPhy } elseif($id !== '') { 5136c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 51416f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 515bce3726dSAndreas Gohr } else { 516bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 51716f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 518f3f0262cSandi } 519f3f0262cSandi 520f3f0262cSandi return $xlink; 521f3f0262cSandi} 522f3f0262cSandi 523f3f0262cSandi/** 524f5c2808fSBen Coburn * This builds a link to an alternate page format 525f5c2808fSBen Coburn * 526f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 527f5c2808fSBen Coburn * 528f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5294bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5304bc480e5SAndreas Gohr * @param string $format the export renderer to use 5314bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5324bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5334bc480e5SAndreas Gohr * @param string $sep parameter separator 5344bc480e5SAndreas Gohr * @return string 535f5c2808fSBen Coburn */ 5364bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 537f5c2808fSBen Coburn global $conf; 5384bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5394bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 540f5c2808fSBen Coburn } else { 5414bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 542f5c2808fSBen Coburn } 543f5c2808fSBen Coburn 544f5c2808fSBen Coburn $format = rawurlencode($format); 545f5c2808fSBen Coburn $id = idfilter($id); 546f5c2808fSBen Coburn if($abs) { 547f5c2808fSBen Coburn $xlink = DOKU_URL; 548f5c2808fSBen Coburn } else { 549f5c2808fSBen Coburn $xlink = DOKU_BASE; 550f5c2808fSBen Coburn } 551f5c2808fSBen Coburn 552f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 553f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5544bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 555f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 556f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5574bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 558f5c2808fSBen Coburn } else { 559f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5604bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 561f5c2808fSBen Coburn } 562f5c2808fSBen Coburn 563f5c2808fSBen Coburn return $xlink; 564f5c2808fSBen Coburn} 565f5c2808fSBen Coburn 566f5c2808fSBen Coburn/** 5676de3759aSAndreas Gohr * Build a link to a media file 5686de3759aSAndreas Gohr * 5696de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5708c08db0aSAndreas Gohr * 5718c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5728c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5738c08db0aSAndreas Gohr * 5743272d797SAndreas Gohr * @param string $id the media file id or URL 5753272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5763272d797SAndreas Gohr * @param bool $direct link to detail page if false 5773272d797SAndreas Gohr * @param string $sep URL parameter separator 5783272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5793272d797SAndreas Gohr * @return string 5806de3759aSAndreas Gohr */ 58155b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5826de3759aSAndreas Gohr global $conf; 583b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 584826d2766SKlap-in if(!$isexternalimage) { 585826d2766SKlap-in $id = cleanID($id); 586826d2766SKlap-in } 587826d2766SKlap-in 5886de3759aSAndreas Gohr if(is_array($more)) { 5890f4e0092SChristopher Smith // add token for resized images 590357c9a39SDamien Regad $w = isset($more['w']) ? $more['w'] : null; 591357c9a39SDamien Regad $h = isset($more['h']) ? $more['h'] : null; 59298fe1ac9SDamien Regad if($w || $h || $isexternalimage){ 593357c9a39SDamien Regad $more['tok'] = media_get_token($id, $w, $h); 5940f4e0092SChristopher Smith } 5958c08db0aSAndreas Gohr // strip defaults for shorter URLs 5968c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 597443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 598443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 5998c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 60078b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 601b174aeaeSchris $more = buildURLparams($more, $sep); 6026de3759aSAndreas Gohr } else { 6035e7db1e2SChristopher Smith $matches = array(); 604cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6055e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6065e7db1e2SChristopher Smith foreach ($matches as $match){ 6075e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6085e7db1e2SChristopher Smith } 609cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 610cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6115e7db1e2SChristopher Smith } 6128c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6138c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 614b174aeaeSchris $more = str_replace(',', $sep, $more); 6156de3759aSAndreas Gohr } 6166de3759aSAndreas Gohr 61755b2b31bSAndreas Gohr if($abs) { 61855b2b31bSAndreas Gohr $xlink = DOKU_URL; 61955b2b31bSAndreas Gohr } else { 6206de3759aSAndreas Gohr $xlink = DOKU_BASE; 62155b2b31bSAndreas Gohr } 6226de3759aSAndreas Gohr 6236de3759aSAndreas Gohr // external URLs are always direct without rewriting 624826d2766SKlap-in if($isexternalimage) { 6256de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 626cc036f74SKlap-in $xlink .= '?'.$more; 627b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6286de3759aSAndreas Gohr return $xlink; 6296de3759aSAndreas Gohr } 6306de3759aSAndreas Gohr 6316de3759aSAndreas Gohr $id = idfilter($id); 6326de3759aSAndreas Gohr 6336de3759aSAndreas Gohr // decide on scriptname 6346de3759aSAndreas Gohr if($direct) { 6356de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6366de3759aSAndreas Gohr $script = '_media'; 6376de3759aSAndreas Gohr } else { 6386de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6396de3759aSAndreas Gohr } 6406de3759aSAndreas Gohr } else { 6416de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6426de3759aSAndreas Gohr $script = '_detail'; 6436de3759aSAndreas Gohr } else { 6446de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6456de3759aSAndreas Gohr } 6466de3759aSAndreas Gohr } 6476de3759aSAndreas Gohr 6486de3759aSAndreas Gohr // build URL based on rewrite mode 6496de3759aSAndreas Gohr if($conf['userewrite']) { 6506de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6516de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6526de3759aSAndreas Gohr } else { 6536de3759aSAndreas Gohr if($more) { 654a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 655b174aeaeSchris $xlink .= $sep.'media='.$id; 6566de3759aSAndreas Gohr } else { 657a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6586de3759aSAndreas Gohr } 6596de3759aSAndreas Gohr } 6606de3759aSAndreas Gohr 6616de3759aSAndreas Gohr return $xlink; 6626de3759aSAndreas Gohr} 6636de3759aSAndreas Gohr 6646de3759aSAndreas Gohr/** 66525ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 66615fae107Sandi * 66725ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 66825ca5b17SAndreas Gohr * 66915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 670140cfbcdSGerrit Uitslag * 671140cfbcdSGerrit Uitslag * @return string 672f3f0262cSandi */ 67325ca5b17SAndreas Gohrfunction script() { 674ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 675f3f0262cSandi} 676f3f0262cSandi 677f3f0262cSandi/** 67815fae107Sandi * Spamcheck against wordlist 67915fae107Sandi * 680f3f0262cSandi * Checks the wikitext against a list of blocked expressions 681f3f0262cSandi * returns true if the text contains any bad words 68215fae107Sandi * 683e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 684e403cc58SMichael Klier * 685e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 686e403cc58SMichael Klier * and gain information about the user who was blocked. 687e403cc58SMichael Klier * 688e403cc58SMichael Klier * Event data: 689e403cc58SMichael Klier * data['matches'] - array of matches 690e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 691e403cc58SMichael Klier * [ip] - ip address 692e403cc58SMichael Klier * [user] - username (if logged in) 693e403cc58SMichael Klier * [mail] - mail address (if logged in) 694e403cc58SMichael Klier * [name] - real name (if logged in) 695e403cc58SMichael Klier * 69615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6976dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 698140cfbcdSGerrit Uitslag * 6996dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 7006dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 701f3f0262cSandi */ 7026dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 703f3f0262cSandi global $TEXT; 7046dffa0e0SAndreas Gohr global $PRE; 7056dffa0e0SAndreas Gohr global $SUF; 706e0086ca2SAndreas Gohr global $SUM; 707f3f0262cSandi global $conf; 708e403cc58SMichael Klier global $INFO; 709585bf44eSChristopher Smith /* @var Input $INPUT */ 710585bf44eSChristopher Smith global $INPUT; 711f3f0262cSandi 712f3f0262cSandi if(!$conf['usewordblock']) return false; 713f3f0262cSandi 714e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7156dffa0e0SAndreas Gohr 716041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 71764159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 71864159a61SAndreas Gohr $text = preg_replace( 71964159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 72064159a61SAndreas Gohr '\1http://\2 \2\3', 72164159a61SAndreas Gohr $text 72264159a61SAndreas Gohr ); 72364159a61SAndreas Gohr // phpcs:enable 724041d1964SAndreas Gohr 725b9ac8716Schris $wordblocks = getWordblocks(); 7263e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 7273e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 7283e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 7293e2965d7Sandi // backreferences are used - the maximum is 99 7303e2965d7Sandi // this is very bad performancewise and may even be too high still 7313e2965d7Sandi $chunksize = 40; 7323e2965d7Sandi } else { 733a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7343e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 735a51d08efSAndreas Gohr $chunksize = 200; 7363e2965d7Sandi } 737b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 738f3f0262cSandi $re = array(); 73949eb6e38SAndreas Gohr // build regexp from blocks 740f3f0262cSandi foreach($blocks as $block) { 741f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 742f3f0262cSandi $block = trim($block); 743f3f0262cSandi if(empty($block)) continue; 744f3f0262cSandi $re[] = $block; 745f3f0262cSandi } 746e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 747e403cc58SMichael Klier // prepare event data 74859bc3b48SGerrit Uitslag $data = array(); 749e403cc58SMichael Klier $data['matches'] = $matches; 750585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 751585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 752585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 753e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 754e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 755e403cc58SMichael Klier } 756bad6fc0dSAndreas Gohr $callback = function () { 757bad6fc0dSAndreas Gohr return true; 758bad6fc0dSAndreas Gohr }; 759cbb44eabSAndreas Gohr return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 760b9ac8716Schris } 761703f6fdeSandi } 762f3f0262cSandi return false; 763f3f0262cSandi} 764f3f0262cSandi 765f3f0262cSandi/** 766a7580321SZebra North * Return the IP of the client. 76715fae107Sandi * 768a7580321SZebra North * The IP is sourced from, in order of preference: 76915fae107Sandi * 770a7580321SZebra North * - The X-Real-IP header if $conf[realip] is true. 771a7580321SZebra North * - The X-Forwarded-For header if all the proxies are trusted by $conf[trustedproxy]. 772a7580321SZebra North * - The TCP/IP connection remote address. 773a7580321SZebra North * - 0.0.0.0 if all else fails. 7746d8affe6SAndreas Gohr * 775a7580321SZebra North * The 'realip' config value should only be set to true if the X-Real-IP header 776a7580321SZebra North * is being added by the web server, otherwise it may be spoofed by the client. 777a7580321SZebra North * 778a7580321SZebra North * The 'trustedproxy' setting must not allow any IP, otherwise the X-Forwarded-For 779a7580321SZebra North * may be spoofed by the client. 780a7580321SZebra North * 781a7580321SZebra North * @author Zebra North <mrzebra@mrzebra.co.uk> 782140cfbcdSGerrit Uitslag * 783*608cdefcSZebra North * @param bool $single If set only a single IP is returned. 784*608cdefcSZebra North * 785a7580321SZebra North * @return string Returns an IP address if 'single' is true, or a comma-separated list 786a7580321SZebra North * of IP addresses otherwise. 787f3f0262cSandi */ 7886d8affe6SAndreas Gohrfunction clientIP($single = false) { 789585bf44eSChristopher Smith /* @var Input $INPUT */ 790925105e8SPhy global $INPUT, $conf; 791585bf44eSChristopher Smith 792a7580321SZebra North // IPs in order of most to least preferred. 793a7580321SZebra North $ips = []; 794a7580321SZebra North 795a7580321SZebra North // Use the X-Real-IP header if it is enabled by the configuration. 796a7580321SZebra North if (!empty($conf['realip']) && $INPUT->server->str('HTTP_X_REAL_IP')) { 797a7580321SZebra North $ips[] = $INPUT->server->str('HTTP_X_REAL_IP'); 798585bf44eSChristopher Smith } 7996d8affe6SAndreas Gohr 800*608cdefcSZebra North // Add the X-Forwarded-For addresses if all proxies are trusted. 801*608cdefcSZebra North $ips = array_merge($ips, dokuwiki\forwardedFor()); 802925105e8SPhy 803a7580321SZebra North // Add the TCP/IP connection endpoint. 804*608cdefcSZebra North $ips[] = $INPUT->server->str('REMOTE_ADDR'); 805a7580321SZebra North 806a7580321SZebra North // Remove invalid IPs. 807a7580321SZebra North $ips = array_filter($ips, function ($ip) { return filter_var($ip, FILTER_VALIDATE_IP); }); 808a7580321SZebra North 809a7580321SZebra North // Remove duplicated IPs. 810a7580321SZebra North $ips = array_values(array_unique($ips)); 811a7580321SZebra North 812a7580321SZebra North // Add a fallback if for some reason there were no valid IPs. 813a7580321SZebra North if (!$ips) { 814a7580321SZebra North $ips[] = '0.0.0.0'; 815a7580321SZebra North } 816a7580321SZebra North 817a7580321SZebra North // Return the first IP in single mode, or all the IPs. 818a7580321SZebra North return $single ? $ips[0] : join(',', $ips); 819f3f0262cSandi} 820f3f0262cSandi 821f3f0262cSandi/** 8221c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8231c548ebeSAndreas Gohr * 8241c548ebeSAndreas Gohr * Adapted from the example code at url below 8251c548ebeSAndreas Gohr * 8261c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 827140cfbcdSGerrit Uitslag * 82864159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 829140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8301c548ebeSAndreas Gohr */ 8311c548ebeSAndreas Gohrfunction clientismobile() { 832585bf44eSChristopher Smith /* @var Input $INPUT */ 833585bf44eSChristopher Smith global $INPUT; 8341c548ebeSAndreas Gohr 835585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8361c548ebeSAndreas Gohr 837585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8381c548ebeSAndreas Gohr 839585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8401c548ebeSAndreas Gohr 84164159a61SAndreas Gohr $uamatches = join( 84264159a61SAndreas Gohr '|', 84364159a61SAndreas Gohr [ 84464159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 84564159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 84664159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 84764159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 84864159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 84964159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 85064159a61SAndreas Gohr '\d\d\di', 'moto' 85164159a61SAndreas Gohr ] 85264159a61SAndreas Gohr ); 8531c548ebeSAndreas Gohr 854585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8551c548ebeSAndreas Gohr 8561c548ebeSAndreas Gohr return false; 8571c548ebeSAndreas Gohr} 8581c548ebeSAndreas Gohr 8591c548ebeSAndreas Gohr/** 8606efc45a2SDmitry Katsubo * check if a given link is interwiki link 8616efc45a2SDmitry Katsubo * 8626efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8636efc45a2SDmitry Katsubo * @return bool 8646efc45a2SDmitry Katsubo */ 8656efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 8666efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 8676efc45a2SDmitry Katsubo return false; 8686efc45a2SDmitry Katsubo} 8696efc45a2SDmitry Katsubo 8706efc45a2SDmitry Katsubo/** 87163211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 87263211f61SGlen Harris * 87322ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 87422ef1e32SAndreas Gohr * 87563211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 876140cfbcdSGerrit Uitslag * 8773272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 8783272d797SAndreas Gohr * @return string a comma separated list of hostnames 87963211f61SGlen Harris */ 88063211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 88122ef1e32SAndreas Gohr global $conf; 88222ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 88322ef1e32SAndreas Gohr 88463211f61SGlen Harris $hosts = array(); 88563211f61SGlen Harris $ips = explode(',', $ips); 886551a720fSMichael Klier 887551a720fSMichael Klier if(is_array($ips)) { 8883886270dSAndreas Gohr foreach($ips as $ip) { 889551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 89063211f61SGlen Harris } 891551a720fSMichael Klier return join(',', $hosts); 892551a720fSMichael Klier } else { 893551a720fSMichael Klier return gethostbyaddr(trim($ips)); 894551a720fSMichael Klier } 89563211f61SGlen Harris} 89663211f61SGlen Harris 89763211f61SGlen Harris/** 89815fae107Sandi * Checks if a given page is currently locked. 89915fae107Sandi * 900f3f0262cSandi * removes stale lockfiles 90115fae107Sandi * 90215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 903140cfbcdSGerrit Uitslag * 904140cfbcdSGerrit Uitslag * @param string $id page id 905140cfbcdSGerrit Uitslag * @return bool page is locked? 906f3f0262cSandi */ 907f3f0262cSandifunction checklock($id) { 908f3f0262cSandi global $conf; 909585bf44eSChristopher Smith /* @var Input $INPUT */ 910585bf44eSChristopher Smith global $INPUT; 911585bf44eSChristopher Smith 912c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 913f3f0262cSandi 914f3f0262cSandi //no lockfile 91579e79377SAndreas Gohr if(!file_exists($lock)) return false; 916f3f0262cSandi 917f3f0262cSandi //lockfile expired 918f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 919d8186216SBen Coburn @unlink($lock); 920f3f0262cSandi return false; 921f3f0262cSandi } 922f3f0262cSandi 923f3f0262cSandi //my own lock 9246d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 925c0dd3914SAdaKaleh if($ip == $INPUT->server->str('REMOTE_USER') || (session_id() && $session == session_id())) { 926f3f0262cSandi return false; 927f3f0262cSandi } 928f3f0262cSandi 929f3f0262cSandi return $ip; 930f3f0262cSandi} 931f3f0262cSandi 932f3f0262cSandi/** 93315fae107Sandi * Lock a page for editing 93415fae107Sandi * 93515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 936140cfbcdSGerrit Uitslag * 937140cfbcdSGerrit Uitslag * @param string $id page id to lock 938f3f0262cSandi */ 939f3f0262cSandifunction lock($id) { 940544ed901SDaniel Calviño Sánchez global $conf; 941585bf44eSChristopher Smith /* @var Input $INPUT */ 942585bf44eSChristopher Smith global $INPUT; 943544ed901SDaniel Calviño Sánchez 944544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 945544ed901SDaniel Calviño Sánchez return; 946544ed901SDaniel Calviño Sánchez } 947544ed901SDaniel Calviño Sánchez 948c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 949585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 950585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 951f3f0262cSandi } else { 95285fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 953f3f0262cSandi } 954f3f0262cSandi} 955f3f0262cSandi 956f3f0262cSandi/** 95715fae107Sandi * Unlock a page if it was locked by the user 958f3f0262cSandi * 95915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 960140cfbcdSGerrit Uitslag * 9613272d797SAndreas Gohr * @param string $id page id to unlock 96215fae107Sandi * @return bool true if a lock was removed 963f3f0262cSandi */ 964f3f0262cSandifunction unlock($id) { 965585bf44eSChristopher Smith /* @var Input $INPUT */ 966585bf44eSChristopher Smith global $INPUT; 967585bf44eSChristopher Smith 968c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 96979e79377SAndreas Gohr if(file_exists($lock)) { 9706d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 971c0dd3914SAdaKaleh if($ip == $INPUT->server->str('REMOTE_USER') || $session == session_id()) { 972f3f0262cSandi @unlink($lock); 973f3f0262cSandi return true; 974f3f0262cSandi } 975f3f0262cSandi } 976f3f0262cSandi return false; 977f3f0262cSandi} 978f3f0262cSandi 979f3f0262cSandi/** 980f3f0262cSandi * convert line ending to unix format 981f3f0262cSandi * 9826db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 9836db7468bSAndreas Gohr * 98415fae107Sandi * @see formText() for 2crlf conversion 98515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 986140cfbcdSGerrit Uitslag * 987140cfbcdSGerrit Uitslag * @param string $text 988140cfbcdSGerrit Uitslag * @return string 989f3f0262cSandi */ 990f3f0262cSandifunction cleanText($text) { 991f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 9926db7468bSAndreas Gohr 9936db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 9946db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 9956db7468bSAndreas Gohr // but might actually fix the problem in many cases 9968cbc5ee8SAndreas Gohr if(!\dokuwiki\Utf8\Clean::isUtf8($text)) $text = utf8_encode($text); 9976db7468bSAndreas Gohr 998f3f0262cSandi return $text; 999f3f0262cSandi} 1000f3f0262cSandi 1001f3f0262cSandi/** 1002f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1003f3f0262cSandi * It also converts line endings to Windows format which is 1004f3f0262cSandi * pseudo standard for webforms. 1005f3f0262cSandi * 100615fae107Sandi * @see cleanText() for 2unix conversion 100715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1008140cfbcdSGerrit Uitslag * 1009140cfbcdSGerrit Uitslag * @param string $text 1010140cfbcdSGerrit Uitslag * @return string 1011f3f0262cSandi */ 1012f3f0262cSandifunction formText($text) { 10135b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 1014f3f0262cSandi return htmlspecialchars($text); 1015f3f0262cSandi} 1016f3f0262cSandi 1017f3f0262cSandi/** 101815fae107Sandi * Returns the specified local text in raw format 101915fae107Sandi * 102015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1021140cfbcdSGerrit Uitslag * 1022140cfbcdSGerrit Uitslag * @param string $id page id 1023140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1024140cfbcdSGerrit Uitslag * @return string 1025f3f0262cSandi */ 10262adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10272adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1028f3f0262cSandi} 1029f3f0262cSandi 1030f3f0262cSandi/** 1031f3f0262cSandi * Returns the raw WikiText 103215fae107Sandi * 103315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1034140cfbcdSGerrit Uitslag * 1035140cfbcdSGerrit Uitslag * @param string $id page id 1036e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1037140cfbcdSGerrit Uitslag * @return string 1038f3f0262cSandi */ 1039f3f0262cSandifunction rawWiki($id, $rev = '') { 1040cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1041f3f0262cSandi} 1042f3f0262cSandi 1043f3f0262cSandi/** 10447146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10457146cee2SAndreas Gohr * 10467b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10477146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1048140cfbcdSGerrit Uitslag * 1049140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1050140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10517146cee2SAndreas Gohr */ 1052fe17917eSAdrian Langfunction pageTemplate($id) { 1053a15ce62dSEsther Brunner global $conf; 1054e29549feSAndreas Gohr 1055fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1056e29549feSAndreas Gohr 10577b84afa2SAndreas Gohr // prepare initial event data 10587b84afa2SAndreas Gohr $data = array( 10597b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10607b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10617b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10627b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10637b84afa2SAndreas Gohr ); 10647b84afa2SAndreas Gohr 1065e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_PAGETPL_LOAD', $data); 10667b84afa2SAndreas Gohr if($evt->advise_before(true)) { 10677b84afa2SAndreas Gohr // the before event might have loaded the content already 10687b84afa2SAndreas Gohr if(empty($data['tpl'])) { 10697b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10707b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1071fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 107279e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 10737b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1074e29549feSAndreas Gohr } else { 1075e29549feSAndreas Gohr // search upper namespaces for templates 1076e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1077e29549feSAndreas Gohr while(strlen($path) >= $len) { 107879e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 10797b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1080e29549feSAndreas Gohr break; 1081e29549feSAndreas Gohr } 1082e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1083e29549feSAndreas Gohr } 1084e29549feSAndreas Gohr } 10857b84afa2SAndreas Gohr } 10867b84afa2SAndreas Gohr // load the content 10873d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 10887b84afa2SAndreas Gohr } 1089a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 10907b84afa2SAndreas Gohr } 10917b84afa2SAndreas Gohr $evt->advise_after(); 10927b84afa2SAndreas Gohr unset($evt); 10937b84afa2SAndreas Gohr 1094fe17917eSAdrian Lang return $data['tpl']; 10952b1223ecSAdrian Lang} 10962b1223ecSAdrian Lang 10972b1223ecSAdrian Lang/** 10982b1223ecSAdrian Lang * Performs common page template replacements 10997b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11002b1223ecSAdrian Lang * 11012b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1102140cfbcdSGerrit Uitslag * 1103140cfbcdSGerrit Uitslag * @param array $data array with event data 1104140cfbcdSGerrit Uitslag * @return string 11052b1223ecSAdrian Lang */ 1106d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11073272d797SAndreas Gohr /** 11083272d797SAndreas Gohr * @var string $id the id of the page to be created 11093272d797SAndreas Gohr * @var string $tpl the text used as template 11103272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11113272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11123272d797SAndreas Gohr */ 1113fe17917eSAdrian Lang extract($data); 1114fe17917eSAdrian Lang 1115b856f7dfSAdrian Lang global $USERINFO; 1116bce53b1fSAdrian Lang global $conf; 1117585bf44eSChristopher Smith /* @var Input $INPUT */ 1118585bf44eSChristopher Smith global $INPUT; 1119e29549feSAndreas Gohr 1120e29549feSAndreas Gohr // replace placeholders 112126ece5a7SAndreas Gohr $file = noNS($id); 112237c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 112326ece5a7SAndreas Gohr 11243272d797SAndreas Gohr $tpl = str_replace( 11253272d797SAndreas Gohr array( 112626ece5a7SAndreas Gohr '@ID@', 112726ece5a7SAndreas Gohr '@NS@', 11288a7bcf66SShota Miyazaki '@CURNS@', 1129a3db0ab0SSimon Lees '@!CURNS@', 1130a3db0ab0SSimon Lees '@!!CURNS@', 1131a3db0ab0SSimon Lees '@!CURNS!@', 113226ece5a7SAndreas Gohr '@FILE@', 113326ece5a7SAndreas Gohr '@!FILE@', 113426ece5a7SAndreas Gohr '@!FILE!@', 113526ece5a7SAndreas Gohr '@PAGE@', 113626ece5a7SAndreas Gohr '@!PAGE@', 113726ece5a7SAndreas Gohr '@!!PAGE@', 113826ece5a7SAndreas Gohr '@!PAGE!@', 113926ece5a7SAndreas Gohr '@USER@', 114026ece5a7SAndreas Gohr '@NAME@', 114126ece5a7SAndreas Gohr '@MAIL@', 114226ece5a7SAndreas Gohr '@DATE@', 114326ece5a7SAndreas Gohr ), 114426ece5a7SAndreas Gohr array( 114526ece5a7SAndreas Gohr $id, 114626ece5a7SAndreas Gohr getNS($id), 11478a7bcf66SShota Miyazaki curNS($id), 1148c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst(curNS($id)), 1149c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords(curNS($id)), 1150c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper(curNS($id)), 115126ece5a7SAndreas Gohr $file, 11528cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($file), 11538cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($file), 115426ece5a7SAndreas Gohr $page, 11558cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($page), 11568cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords($page), 11578cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($page), 1158585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11593e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 11603e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 116126ece5a7SAndreas Gohr $conf['dformat'], 11623272d797SAndreas Gohr ), $tpl 11633272d797SAndreas Gohr ); 116426ece5a7SAndreas Gohr 11657d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1166bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1167bad6fc0dSAndreas Gohr '/%./', 1168bad6fc0dSAndreas Gohr function ($m) { 116910f359adSAndreas Gohr return dformat(null, $m[0]); 1170bad6fc0dSAndreas Gohr }, 1171bad6fc0dSAndreas Gohr $tpl 1172bad6fc0dSAndreas Gohr ); 1173d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1174a15ce62dSEsther Brunner return $tpl; 11757146cee2SAndreas Gohr} 11767146cee2SAndreas Gohr 11777146cee2SAndreas Gohr/** 117815fae107Sandi * Returns the raw Wiki Text in three slices. 117915fae107Sandi * 118015fae107Sandi * The range parameter needs to have the form "from-to" 118115cfe303Sandi * and gives the range of the section in bytes - no 118215cfe303Sandi * UTF-8 awareness is needed. 1183f3f0262cSandi * The returned order is prefix, section and suffix. 118415fae107Sandi * 118515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1186140cfbcdSGerrit Uitslag * 1187140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1188140cfbcdSGerrit Uitslag * @param string $id page id 1189140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 119042ea7f44SGerrit Uitslag * @return string[] with three slices 1191f3f0262cSandi */ 1192f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1193cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1194f3f0262cSandi 119580fcb268SAdrian Lang // Parse range 119680fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 119780fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 119880fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 119980fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 120080fcb268SAdrian Lang 120159bc3b48SGerrit Uitslag $slices = array(); 120280fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 120380fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 120415cfe303Sandi $slices[2] = substr($text, $to); 1205f3f0262cSandi return $slices; 1206f3f0262cSandi} 1207f3f0262cSandi 1208f3f0262cSandi/** 120915fae107Sandi * Joins wiki text slices 121015fae107Sandi * 121180fcb268SAdrian Lang * function to join the text slices. 1212f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1213f3f0262cSandi * lines between sections if needed (used on saving). 121415fae107Sandi * 121515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1216140cfbcdSGerrit Uitslag * 1217140cfbcdSGerrit Uitslag * @param string $pre prefix 1218140cfbcdSGerrit Uitslag * @param string $text text in the middle 1219140cfbcdSGerrit Uitslag * @param string $suf suffix 1220140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1221140cfbcdSGerrit Uitslag * @return string 1222f3f0262cSandi */ 1223f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1224f3f0262cSandi if($pretty) { 122580fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12263272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12273272d797SAndreas Gohr ) { 122880fcb268SAdrian Lang $pre .= "\n"; 122980fcb268SAdrian Lang } 123080fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12313272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12323272d797SAndreas Gohr ) { 123380fcb268SAdrian Lang $text .= "\n"; 123480fcb268SAdrian Lang } 1235f3f0262cSandi } 1236f3f0262cSandi 1237f3f0262cSandi return $pre.$text.$suf; 1238f3f0262cSandi} 1239f3f0262cSandi 1240f3f0262cSandi/** 1241b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1242b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1243b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1244b24d9195SAndreas Gohr * 1245b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1246b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1247b24d9195SAndreas Gohr * 1248b24d9195SAndreas Gohr * @param string $id the page ID 124969f9b481SSatoshi Sahara * @deprecated 2021-11-28 1250b24d9195SAndreas Gohr */ 1251b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 125279a2d784SGerrit Uitslag dbg_deprecated(PageFile::class .'::detectExternalEdit()'); 1253b24e9c4aSSatoshi Sahara (new PageFile($id))->detectExternalEdit(); 1254b24d9195SAndreas Gohr} 1255b24d9195SAndreas Gohr 1256b24d9195SAndreas Gohr/** 1257a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1258a701424fSBen Coburn * Also directs changelog and attic updates. 125915fae107Sandi * 126015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 126171726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1262140cfbcdSGerrit Uitslag * 1263140cfbcdSGerrit Uitslag * @param string $id page id 1264140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1265140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1266140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1267f3f0262cSandi */ 1268b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1269585bf44eSChristopher Smith 1270b24e9c4aSSatoshi Sahara // get COMMON_WIKIPAGE_SAVE event data 1271b24e9c4aSSatoshi Sahara $data = (new PageFile($id))->saveWikiText($text, $summary, $minor); 1272ac3ed4afSGerrit Uitslag 127326a0801fSAndreas Gohr // send notify mails 12743b813d43SSatoshi Sahara list('oldRevision' => $rev, 'newRevision' => $new_rev, 'summary' => $summary) = $data; 12753b813d43SSatoshi Sahara notify($id, 'admin', $rev, $summary, $minor, $new_rev); 12763b813d43SSatoshi Sahara notify($id, 'subscribers', $rev, $summary, $minor, $new_rev); 1277f3f0262cSandi 12782eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1279fe9ec250SChris Smith if (useHeading('content')) { 128007ff0babSMichael Hamann $pages = ft_backlinks($id, true); 12812eccbdaaSGina Haeussge foreach ($pages as $page) { 12820db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 12832eccbdaaSGina Haeussge $cache->removeCache(); 12842eccbdaaSGina Haeussge } 12852eccbdaaSGina Haeussge } 1286f3f0262cSandi} 1287f3f0262cSandi 1288f3f0262cSandi/** 1289d5824ab9SSatoshi Sahara * moves the current version to the attic and returns its revision date 129015fae107Sandi * 129115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1292140cfbcdSGerrit Uitslag * 1293140cfbcdSGerrit Uitslag * @param string $id page id 1294140cfbcdSGerrit Uitslag * @return int|string revision timestamp 129569f9b481SSatoshi Sahara * @deprecated 2021-11-28 1296f3f0262cSandi */ 1297f3f0262cSandifunction saveOldRevision($id) { 129879a2d784SGerrit Uitslag dbg_deprecated(PageFile::class .'::saveOldRevision()'); 1299b24e9c4aSSatoshi Sahara return (new PageFile($id))->saveOldRevision(); 1300f3f0262cSandi} 1301f3f0262cSandi 1302f3f0262cSandi/** 1303fde10de4SAdrian Lang * Sends a notify mail on page change or registration 130426a0801fSAndreas Gohr * 130526a0801fSAndreas Gohr * @param string $id The changed page 1306fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 13073272d797SAndreas Gohr * @param int|string $rev Old page revision 130826a0801fSAndreas Gohr * @param string $summary What changed 130990033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 131042ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 131183734cddSPhy * @param int|string $current_rev New page revision 13123272d797SAndreas Gohr * @return bool 1313140cfbcdSGerrit Uitslag * 131415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1315f3f0262cSandi */ 131683734cddSPhyfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array(), $current_rev = false) { 1317f3f0262cSandi global $conf; 1318585bf44eSChristopher Smith /* @var Input $INPUT */ 1319585bf44eSChristopher Smith global $INPUT; 1320b158d625SSteven Danz 13216df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 132226a0801fSAndreas Gohr if ($who == 'admin') { 13233272d797SAndreas Gohr if (empty($conf['notify'])) return false; //notify enabled? 13242ed38036SAndreas Gohr $tpl = 'mailtext'; 132526a0801fSAndreas Gohr $to = $conf['notify']; 132626a0801fSAndreas Gohr } elseif ($who == 'subscribers') { 132784c1127cSAndreas Gohr if (!actionOK('subscribe')) return false; //subscribers enabled? 1328585bf44eSChristopher Smith if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 13290bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 1330cbb44eabSAndreas Gohr Event::createAndTrigger( 13313272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1332c8cc4053SAndreas Gohr array(new SubscriberManager(), 'notifyAddresses') 13333272d797SAndreas Gohr ); 13342ed38036SAndreas Gohr $to = $data['addresslist']; 13352ed38036SAndreas Gohr if (empty($to)) return false; 13362ed38036SAndreas Gohr $tpl = 'subscr_single'; 133726a0801fSAndreas Gohr } else { 13383272d797SAndreas Gohr return false; //just to be safe 133926a0801fSAndreas Gohr } 134026a0801fSAndreas Gohr 13416df843eeSAndreas Gohr // prepare content 1342704a815fSMichael Große $subscription = new PageSubscriptionSender(); 134383734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1344f3f0262cSandi} 13452ed38036SAndreas Gohr 134615fae107Sandi/** 134771f7bde7SAndreas Gohr * extracts the query from a search engine referrer 134815fae107Sandi * 134915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 135071f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1351140cfbcdSGerrit Uitslag * 1352140cfbcdSGerrit Uitslag * @return array|string 1353f3f0262cSandi */ 1354f3f0262cSandifunction getGoogleQuery() { 1355585bf44eSChristopher Smith /* @var Input $INPUT */ 1356585bf44eSChristopher Smith global $INPUT; 1357585bf44eSChristopher Smith 1358585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1359c66972f2SAdrian Lang return ''; 1360c66972f2SAdrian Lang } 1361585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1362f3f0262cSandi 1363079b3ac1SAndreas Gohr // only handle common SEs 1364079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1365e4d8a516SKazutaka Miyasaka 1366079b3ac1SAndreas Gohr $query = array(); 1367f3f0262cSandi parse_str($url['query'], $query); 1368e4d8a516SKazutaka Miyasaka 1369c66972f2SAdrian Lang $q = ''; 1370079b3ac1SAndreas Gohr if(isset($query['q'])){ 1371079b3ac1SAndreas Gohr $q = $query['q']; 1372079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1373079b3ac1SAndreas Gohr $q = $query['p']; 1374079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1375079b3ac1SAndreas Gohr $q = $query['query']; 1376079b3ac1SAndreas Gohr } 1377079b3ac1SAndreas Gohr $q = trim($q); 1378f3f0262cSandi 1379079b3ac1SAndreas Gohr if(!$q) return ''; 1380c7dc833bSPhy // ignore if query includes a full URL 1381c7dc833bSPhy if(strpos($q, '//') !== false) return ''; 13826531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1383f93b3b50SAndreas Gohr return $q; 1384f3f0262cSandi} 1385f3f0262cSandi 1386f3f0262cSandi/** 1387f3f0262cSandi * Return the human readable size of a file 1388f3f0262cSandi * 1389f3f0262cSandi * @param int $size A file size 1390f3f0262cSandi * @param int $dec A number of decimal places 139174160ca1SGerrit Uitslag * @return string human readable size 1392140cfbcdSGerrit Uitslag * 1393f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1394f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1395f3f0262cSandi * @version 1.0.0 1396f3f0262cSandi */ 1397f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1398f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1399f3f0262cSandi $count = count($sizes); 1400f3f0262cSandi $i = 0; 1401f3f0262cSandi 1402f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1403f3f0262cSandi $size /= 1024; 1404f3f0262cSandi $i++; 1405f3f0262cSandi } 1406f3f0262cSandi 1407ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1408f3f0262cSandi} 1409f3f0262cSandi 141015fae107Sandi/** 1411c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1412c57e365eSAndreas Gohr * 1413c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1414140cfbcdSGerrit Uitslag * 1415140cfbcdSGerrit Uitslag * @param int $dt timestamp 1416140cfbcdSGerrit Uitslag * @return string 1417c57e365eSAndreas Gohr */ 1418c57e365eSAndreas Gohrfunction datetime_h($dt) { 1419c57e365eSAndreas Gohr global $lang; 1420c57e365eSAndreas Gohr 1421c57e365eSAndreas Gohr $ago = time() - $dt; 1422c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1423c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1424c57e365eSAndreas Gohr } 1425c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1426c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1427c57e365eSAndreas Gohr } 1428c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1429c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1430c57e365eSAndreas Gohr } 1431c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1432c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1433c57e365eSAndreas Gohr } 1434c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1435c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1436c57e365eSAndreas Gohr } 1437c57e365eSAndreas Gohr if($ago > 60 * 2) { 1438c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1439c57e365eSAndreas Gohr } 1440c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1441c57e365eSAndreas Gohr} 1442c57e365eSAndreas Gohr 1443c57e365eSAndreas Gohr/** 1444f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1445f2263577SAndreas Gohr * 1446f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1447f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1448f2263577SAndreas Gohr * 1449f2263577SAndreas Gohr * @see datetime_h 1450f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1451140cfbcdSGerrit Uitslag * 1452140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1453140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1454140cfbcdSGerrit Uitslag * @return string 1455f2263577SAndreas Gohr */ 1456f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1457f2263577SAndreas Gohr global $conf; 1458f2263577SAndreas Gohr 1459f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1460f2263577SAndreas Gohr $dt = (int) $dt; 1461f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1462f2263577SAndreas Gohr 1463f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1464f2263577SAndreas Gohr return strftime($format, $dt); 1465f2263577SAndreas Gohr} 1466f2263577SAndreas Gohr 1467f2263577SAndreas Gohr/** 1468c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1469c4f79b71SMichael Hamann * 1470c4f79b71SMichael Hamann * @author <ungu at terong dot com> 147159752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1472140cfbcdSGerrit Uitslag * 14737e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 14743272d797SAndreas Gohr * @return string 1475c4f79b71SMichael Hamann */ 1476c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1477c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1478c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1479c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1480c4f79b71SMichael Hamann $date_mod .= $time_zone; 1481c4f79b71SMichael Hamann return $date_mod; 1482c4f79b71SMichael Hamann} 1483c4f79b71SMichael Hamann 1484c4f79b71SMichael Hamann/** 148500a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 148600a7b5adSEsther Brunner * 148700a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 148800a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1489140cfbcdSGerrit Uitslag * 1490140cfbcdSGerrit Uitslag * @param string $email email address 1491140cfbcdSGerrit Uitslag * @return string 149200a7b5adSEsther Brunner */ 149300a7b5adSEsther Brunnerfunction obfuscate($email) { 149400a7b5adSEsther Brunner global $conf; 149500a7b5adSEsther Brunner 149600a7b5adSEsther Brunner switch($conf['mailguard']) { 149700a7b5adSEsther Brunner case 'visible' : 149800a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 149900a7b5adSEsther Brunner return strtr($email, $obfuscate); 150000a7b5adSEsther Brunner 150100a7b5adSEsther Brunner case 'hex' : 1502c1ec88ceSAndreas Gohr return \dokuwiki\Utf8\Conversion::toHtml($email, true); 150300a7b5adSEsther Brunner 150400a7b5adSEsther Brunner case 'none' : 150500a7b5adSEsther Brunner default : 150600a7b5adSEsther Brunner return $email; 150700a7b5adSEsther Brunner } 150800a7b5adSEsther Brunner} 150900a7b5adSEsther Brunner 151000a7b5adSEsther Brunner/** 151189541d4bSAndreas Gohr * Removes quoting backslashes 151289541d4bSAndreas Gohr * 151389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1514140cfbcdSGerrit Uitslag * 1515140cfbcdSGerrit Uitslag * @param string $string 1516140cfbcdSGerrit Uitslag * @param string $char backslashed character 1517140cfbcdSGerrit Uitslag * @return string 151889541d4bSAndreas Gohr */ 151989541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 152089541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 152189541d4bSAndreas Gohr} 152289541d4bSAndreas Gohr 152373038c47SAndreas Gohr/** 152473038c47SAndreas Gohr * Convert php.ini shorthands to byte 152573038c47SAndreas Gohr * 1526a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1527140cfbcdSGerrit Uitslag * 1528a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1529a81f3d99SAndreas Gohr * 1530a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1531a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1532a81f3d99SAndreas Gohr * @return int 153373038c47SAndreas Gohr */ 1534a81f3d99SAndreas Gohrfunction php_to_byte($value) { 1535f5c0c80bSAndreas Gohr switch (strtoupper(substr($value,-1))) { 153673038c47SAndreas Gohr case 'G': 1537a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024 * 1024; 153873038c47SAndreas Gohr break; 153973038c47SAndreas Gohr case 'M': 1540a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024; 1541a81f3d99SAndreas Gohr break; 154273038c47SAndreas Gohr case 'K': 1543a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024; 154473038c47SAndreas Gohr break; 15459eeeb775SAndreas Gohr default: 1546a81f3d99SAndreas Gohr $ret = intval($value); 154749cbd23eSOtto Vainio break; 154873038c47SAndreas Gohr } 154973038c47SAndreas Gohr return $ret; 155073038c47SAndreas Gohr} 155173038c47SAndreas Gohr 1552546d3a99SAndreas Gohr/** 1553546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1554140cfbcdSGerrit Uitslag * 1555140cfbcdSGerrit Uitslag * @param string $string 1556140cfbcdSGerrit Uitslag * @return string 1557546d3a99SAndreas Gohr */ 1558546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1559546d3a99SAndreas Gohr return preg_quote($string, '/'); 1560546d3a99SAndreas Gohr} 156173038c47SAndreas Gohr 1562bd2f6c2fSAndreas Gohr/** 1563bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1564bd2f6c2fSAndreas Gohr * 1565c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1566bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1567bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1568bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1569bd2f6c2fSAndreas Gohr * 1570bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1571bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1572bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1573bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1574bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 15753272d797SAndreas Gohr * @return string 1576bd2f6c2fSAndreas Gohr */ 1577a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 15788cbc5ee8SAndreas Gohr $max = $max - \dokuwiki\Utf8\PhpString::strlen($keep); 1579bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 15808cbc5ee8SAndreas Gohr $len = \dokuwiki\Utf8\PhpString::strlen($short); 1581bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1582bd2f6c2fSAndreas Gohr $half = floor($max / 2); 15836ce3e5f8SAndreas Gohr return $keep . 15846ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, 0, $half - 1) . 15856ce3e5f8SAndreas Gohr $char . 15866ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, $len - $half); 1587bd2f6c2fSAndreas Gohr} 1588bd2f6c2fSAndreas Gohr 1589dc58b6f4SAndy Webber/** 1590dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1591dc58b6f4SAndy Webber * in page footer and recent changes pages 1592dc58b6f4SAndy Webber * 1593b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 159415f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1595c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 159615f3bc49SGerrit Uitslag * 1597dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1598dc58b6f4SAndy Webber */ 159915f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1600cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1601dc58b6f4SAndy Webber} 1602dc58b6f4SAndy Webber 160360a396c8SGerrit Uitslag/** 160460a396c8SGerrit Uitslag * Returns users realname w/o link 160560a396c8SGerrit Uitslag * 1606f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 160715f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1608c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 160960a396c8SGerrit Uitslag * 161060a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 161160a396c8SGerrit Uitslag */ 1612cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 161360a396c8SGerrit Uitslag global $conf, $INFO; 1614e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 161560a396c8SGerrit Uitslag global $auth; 161630f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 161730f6ec4bSGerrit Uitslag global $INPUT; 161860a396c8SGerrit Uitslag 161960a396c8SGerrit Uitslag // prepare initial event data 162060a396c8SGerrit Uitslag $data = array( 162160a396c8SGerrit Uitslag 'username' => $username, // the unique user name 162260a396c8SGerrit Uitslag 'name' => '', 162360a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 162460a396c8SGerrit Uitslag 'target' => '', 162560a396c8SGerrit Uitslag 'pre' => '', 162660a396c8SGerrit Uitslag 'suf' => '', 162760a396c8SGerrit Uitslag 'style' => '', 162860a396c8SGerrit Uitslag 'more' => '', 162960a396c8SGerrit Uitslag 'url' => '', 163060a396c8SGerrit Uitslag 'title' => '', 163160a396c8SGerrit Uitslag 'class' => '' 163260a396c8SGerrit Uitslag ), 16334d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 163415f3bc49SGerrit Uitslag 'textonly' => $textonly 163560a396c8SGerrit Uitslag ); 163662c8004eSGerrit Uitslag if($username === null) { 163730f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 163815f3bc49SGerrit Uitslag if($textonly){ 163915f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 164015f3bc49SGerrit Uitslag }else { 164164159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 164264159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 164360a396c8SGerrit Uitslag } 164415f3bc49SGerrit Uitslag } 164560a396c8SGerrit Uitslag 1646e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 164760a396c8SGerrit Uitslag if($evt->advise_before(true)) { 164860a396c8SGerrit Uitslag if(empty($data['name'])) { 164960a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 165065833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1651dc58b6f4SAndy Webber switch($conf['showuseras']) { 1652dc58b6f4SAndy Webber case 'username': 16537f081821SGerrit Uitslag case 'username_link': 165415f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 165560a396c8SGerrit Uitslag break; 1656dc58b6f4SAndy Webber case 'email': 1657dc58b6f4SAndy Webber case 'email_link': 165860a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 165960a396c8SGerrit Uitslag break; 1660dc58b6f4SAndy Webber } 166165833968SGerrit Uitslag } else { 166265833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 166360a396c8SGerrit Uitslag } 166460a396c8SGerrit Uitslag } 16657f081821SGerrit Uitslag 16667f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 16677f081821SGerrit Uitslag static $xhtml_renderer = null; 16687f081821SGerrit Uitslag 166915f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 16707f081821SGerrit Uitslag 16717f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 167260a396c8SGerrit Uitslag if(!isset($info)) { 167360a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 167460a396c8SGerrit Uitslag } 167560a396c8SGerrit Uitslag if(isset($info) && $info) { 16767f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 167760a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1678dc58b6f4SAndy Webber } else { 16797f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 16807f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 16817f081821SGerrit Uitslag } 16827f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 16837f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 16847f081821SGerrit Uitslag } 16857f081821SGerrit Uitslag $shortcut = 'user'; 1686533772e1SGerrit Uitslag $exists = null; 16876496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 16882a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 16896496c33fSGerrit Uitslag if($exists !== null) { 16906496c33fSGerrit Uitslag if($exists) { 16916496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 16926496c33fSGerrit Uitslag } else { 16936496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 16946496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 16956496c33fSGerrit Uitslag } 16966496c33fSGerrit Uitslag } 1697dc58b6f4SAndy Webber } 1698dc58b6f4SAndy Webber } else { 169915f3bc49SGerrit Uitslag $data['textonly'] = true; 1700dc58b6f4SAndy Webber } 170160a396c8SGerrit Uitslag 170260a396c8SGerrit Uitslag } else { 170315f3bc49SGerrit Uitslag $data['textonly'] = true; 170460a396c8SGerrit Uitslag } 170560a396c8SGerrit Uitslag } 170660a396c8SGerrit Uitslag 170715f3bc49SGerrit Uitslag if($data['textonly']) { 17084d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 170960a396c8SGerrit Uitslag } else { 171060a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 171160a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 171260a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 171360a396c8SGerrit Uitslag } 17144d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 171560a396c8SGerrit Uitslag } 171660a396c8SGerrit Uitslag } 171760a396c8SGerrit Uitslag $evt->advise_after(); 171860a396c8SGerrit Uitslag unset($evt); 171960a396c8SGerrit Uitslag 17204d5fc927SGerrit Uitslag return $data['userlink']; 1721066fee30SAndreas Gohr} 1722066fee30SAndreas Gohr 1723066fee30SAndreas Gohr/** 1724066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1725066fee30SAndreas Gohr * When no image exists, returns an empty string 1726066fee30SAndreas Gohr * 1727066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1728140cfbcdSGerrit Uitslag * 1729066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 17303272d797SAndreas Gohr * @return string 1731066fee30SAndreas Gohr */ 1732066fee30SAndreas Gohrfunction license_img($type) { 1733066fee30SAndreas Gohr global $license; 1734066fee30SAndreas Gohr global $conf; 1735066fee30SAndreas Gohr if(!$conf['license']) return ''; 1736066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1737066fee30SAndreas Gohr $try = array(); 1738066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1739066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1740066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1741066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1742066fee30SAndreas Gohr } 1743066fee30SAndreas Gohr foreach($try as $src) { 174479e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1745066fee30SAndreas Gohr } 1746066fee30SAndreas Gohr return ''; 1747dc58b6f4SAndy Webber} 1748dc58b6f4SAndy Webber 174913c08e2fSMichael Klier/** 175013c08e2fSMichael Klier * Checks if the given amount of memory is available 175113c08e2fSMichael Klier * 175213c08e2fSMichael Klier * If the memory_get_usage() function is not available the 175313c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 175413c08e2fSMichael Klier * 175513c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 175613c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 17573272d797SAndreas Gohr * 17583272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1759140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 17603272d797SAndreas Gohr * @return bool 176113c08e2fSMichael Klier */ 176213c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 176313c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 176413c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1765985d6187SElenchus if($limit == -1) return true; // unlimited 176613c08e2fSMichael Klier 176713c08e2fSMichael Klier // parse limit to bytes 176813c08e2fSMichael Klier $limit = php_to_byte($limit); 176913c08e2fSMichael Klier 177013c08e2fSMichael Klier // get used memory if possible 177113c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 177213c08e2fSMichael Klier $used = memory_get_usage(); 177349eb6e38SAndreas Gohr } else { 177449eb6e38SAndreas Gohr $used = $bytes; 177513c08e2fSMichael Klier } 177613c08e2fSMichael Klier 177713c08e2fSMichael Klier if($used + $mem > $limit) { 177813c08e2fSMichael Klier return false; 177913c08e2fSMichael Klier } 178013c08e2fSMichael Klier 178113c08e2fSMichael Klier return true; 178213c08e2fSMichael Klier} 178313c08e2fSMichael Klier 1784af2408d5SAndreas Gohr/** 1785af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1786af2408d5SAndreas Gohr * 1787af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1788af2408d5SAndreas Gohr * 1789af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1790af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1791140cfbcdSGerrit Uitslag * 1792140cfbcdSGerrit Uitslag * @param string $url url being directed to 1793af2408d5SAndreas Gohr */ 1794af2408d5SAndreas Gohrfunction send_redirect($url) { 179598ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 179698ca30d2SAndreas Gohr 1797585bf44eSChristopher Smith /* @var Input $INPUT */ 1798585bf44eSChristopher Smith global $INPUT; 1799585bf44eSChristopher Smith 18000181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 18010181f021SAndreas Gohr global $MSG; 18020181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 18030181f021SAndreas Gohr //reopen session, store data and close session again 18040181f021SAndreas Gohr @session_start(); 18050181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 18060181f021SAndreas Gohr } 18070181f021SAndreas Gohr 1808d4869846SAndreas Gohr // always close the session 1809d4869846SAndreas Gohr session_write_close(); 1810d4869846SAndreas Gohr 1811af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1812585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1813585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1814585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 18153272d797SAndreas Gohr $matches[1] < 6 18163272d797SAndreas Gohr ) { 1817af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1818af2408d5SAndreas Gohr } else { 1819af2408d5SAndreas Gohr header('Location: '.$url); 1820af2408d5SAndreas Gohr } 182181781cb6SAndreas Gohr 1822572dc222SLarsDW223 // no exits during unit tests 182327c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 182427c0c399SAndreas Gohr // pass info about the redirect back to the test suite 182527c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 182627c0c399SAndreas Gohr if($testRequest !== null) { 182727c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 182827c0c399SAndreas Gohr } 1829572dc222SLarsDW223 return; 1830572dc222SLarsDW223 } 183127c0c399SAndreas Gohr 1832af2408d5SAndreas Gohr exit; 1833af2408d5SAndreas Gohr} 1834af2408d5SAndreas Gohr 18355b75cd1fSAdrian Lang/** 18365b75cd1fSAdrian Lang * Validate a value using a set of valid values 18375b75cd1fSAdrian Lang * 18385b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 18395b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 18405b75cd1fSAdrian Lang * default is specified, throws an exception. 18415b75cd1fSAdrian Lang * 18425b75cd1fSAdrian Lang * @param string $param The name of the parameter 18435b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 18445b75cd1fSAdrian Lang * be marked by the key “default”. 18455b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 18465b75cd1fSAdrian Lang * or $_GET) 18475b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 18485b75cd1fSAdrian Lang * 18493272d797SAndreas Gohr * @throws Exception 18503272d797SAndreas Gohr * @return mixed 18515b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 18525b75cd1fSAdrian Lang */ 18535b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 18545b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 18555b75cd1fSAdrian Lang return $array[$param]; 18565b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 18575b75cd1fSAdrian Lang return $valid_values['default']; 18585b75cd1fSAdrian Lang } else { 18595b75cd1fSAdrian Lang throw new Exception($exc); 18605b75cd1fSAdrian Lang } 18615b75cd1fSAdrian Lang} 18625b75cd1fSAdrian Lang 186363703ba5SAndreas Gohr/** 186463703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1865646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 1866140cfbcdSGerrit Uitslag * 1867140cfbcdSGerrit Uitslag * @param string $pref preference key 1868b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 1869140cfbcdSGerrit Uitslag * @return string preference value 187063703ba5SAndreas Gohr */ 1871554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1872646a531aSChristopher Smith $enc_pref = urlencode($pref); 187306c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1874554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 187563703ba5SAndreas Gohr $cnt = count($parts); 18761c3eca7dSPhy 18771c3eca7dSPhy // due to #2721 there might be duplicate entries, 18781c3eca7dSPhy // so we read from the end 18791c3eca7dSPhy for($i = $cnt-2; $i >= 0; $i -= 2) { 1880646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1881646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1882554a8c9fSAdrian Lang } 1883554a8c9fSAdrian Lang } 1884554a8c9fSAdrian Lang } 1885554a8c9fSAdrian Lang return $default; 1886554a8c9fSAdrian Lang} 1887554a8c9fSAdrian Lang 18883c94d07bSAnika Henke/** 18893c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 189036ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 18913a970889SAnika Henke * Remove it by setting $val to false 1892140cfbcdSGerrit Uitslag * 1893140cfbcdSGerrit Uitslag * @param string $pref preference key 1894140cfbcdSGerrit Uitslag * @param string $val preference value 18953c94d07bSAnika Henke */ 18963c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 18973c94d07bSAnika Henke global $conf; 18983c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 18993c94d07bSAnika Henke $cookieVal = ''; 19003c94d07bSAnika Henke 19011c3eca7dSPhy if($orig !== false && ($orig !== $val)) { 19023c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 19033c94d07bSAnika Henke $cnt = count($parts); 190436ec377eSChristopher Smith // urlencode $pref for the comparison 190536ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 19061c3eca7dSPhy $seen = false; 19073c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 190836ec377eSChristopher Smith if ($parts[$i] == $enc_pref) { 19091c3eca7dSPhy if (!$seen){ 19103a970889SAnika Henke if ($val !== false) { 1911bf8f8509SAndreas Gohr $parts[$i + 1] = rawurlencode($val ?? ''); 19123a970889SAnika Henke } else { 19133a970889SAnika Henke unset($parts[$i]); 19143a970889SAnika Henke unset($parts[$i + 1]); 19153a970889SAnika Henke } 19161c3eca7dSPhy $seen = true; 19171c3eca7dSPhy } else { 19181c3eca7dSPhy // no break because we want to remove duplicate entries 19191c3eca7dSPhy unset($parts[$i]); 19201c3eca7dSPhy unset($parts[$i + 1]); 19211c3eca7dSPhy } 19223c94d07bSAnika Henke } 19233c94d07bSAnika Henke } 19243c94d07bSAnika Henke $cookieVal = implode('#', $parts); 19251c3eca7dSPhy } else if ($orig === false && $val !== false) { 1926c10f256aSDamien Regad $cookieVal = (isset($_COOKIE['DOKU_PREFS']) ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 192764159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 19283c94d07bSAnika Henke } 19293c94d07bSAnika Henke 193075e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 19315833995aSPhy if(defined('DOKU_UNITTEST')) { 19325833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 19335833995aSPhy }else{ 193475e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 19353c94d07bSAnika Henke } 19363c94d07bSAnika Henke} 19373c94d07bSAnika Henke 1938f8fb2d18SAndreas Gohr/** 1939f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 1940f8fb2d18SAndreas Gohr * 194142ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 1942f8fb2d18SAndreas Gohr */ 1943f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 1944f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 1945f8fb2d18SAndreas Gohr} 1946f8fb2d18SAndreas Gohr 19473c27983bSAndreas Gohr/** 194871de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 19493c27983bSAndreas Gohr * 19503c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 19513c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 19523c27983bSAndreas Gohr * files are embedded. 19533c27983bSAndreas Gohr * 195471de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 195571de5572SAndreas Gohr * 19563c27983bSAndreas Gohr * @param string $file full path to the SVG file 19573c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 195871de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 19593c27983bSAndreas Gohr */ 19604cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 19613c27983bSAndreas Gohr $file = trim($file); 19623c27983bSAndreas Gohr if($file === '') return false; 19633c27983bSAndreas Gohr if(!file_exists($file)) return false; 19643c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 19653c27983bSAndreas Gohr if(!is_readable($file)) return false; 19663c27983bSAndreas Gohr $content = file_get_contents($file); 19670849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 19680849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 19690849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 19700849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 19713c27983bSAndreas Gohr $content = trim($content); 19723c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 197371de5572SAndreas Gohr return $content; 19743c27983bSAndreas Gohr} 19753c27983bSAndreas Gohr 1976e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1977