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 */ 824870174SAndreas Gohruse dokuwiki\PassHash; 924870174SAndreas Gohruse dokuwiki\Draft; 1024870174SAndreas Gohruse dokuwiki\Utf8\Clean; 1124870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1224870174SAndreas Gohruse dokuwiki\Utf8\Conversion; 130db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 140db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 150c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 16b24e9c4aSSatoshi Saharause dokuwiki\File\PageFile; 1766f4cdd4SSatoshi Saharause dokuwiki\Logger; 18704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 1975d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 20e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 21e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 220c3a5702SAndreas Gohr 23f3f0262cSandi/** 24d5197206Schris * Wrapper around htmlspecialchars() 25d5197206Schris * 26d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 27d5197206Schris * @see htmlspecialchars() 28140cfbcdSGerrit Uitslag * 29140cfbcdSGerrit Uitslag * @param string $string the string being converted 30140cfbcdSGerrit Uitslag * @return string converted string 31d5197206Schris */ 32d868eb89SAndreas Gohrfunction hsc($string) 33d868eb89SAndreas Gohr{ 34f7711f2bSAndreas Gohr return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); 35d5197206Schris} 36d5197206Schris 37d5197206Schris/** 3812dd3cbcSAndreas Gohr * A safer explode for fixed length lists 3912dd3cbcSAndreas Gohr * 4012dd3cbcSAndreas Gohr * This works just like explode(), but will always return the wanted number of elements. 4112dd3cbcSAndreas Gohr * If the $input string does not contain enough elements, the missing elements will be 4212dd3cbcSAndreas Gohr * filled up with the $default value. If the input string contains more elements, the last 4312dd3cbcSAndreas Gohr * one will NOT be split up and will still contain $separator 4412dd3cbcSAndreas Gohr * 4512dd3cbcSAndreas Gohr * @param string $separator The boundary string 4612dd3cbcSAndreas Gohr * @param string $string The input string 4712dd3cbcSAndreas Gohr * @param int $limit The number of expected elements 4812dd3cbcSAndreas Gohr * @param mixed $default The value to use when filling up missing elements 4912dd3cbcSAndreas Gohr * @see explode 5012dd3cbcSAndreas Gohr * @return array 5112dd3cbcSAndreas Gohr */ 5212dd3cbcSAndreas Gohrfunction sexplode($separator, $string, $limit, $default = null) 5312dd3cbcSAndreas Gohr{ 5412dd3cbcSAndreas Gohr return array_pad(explode($separator, $string, $limit), $limit, $default); 5512dd3cbcSAndreas Gohr} 5612dd3cbcSAndreas Gohr 5712dd3cbcSAndreas Gohr/** 585b571377SAndreas Gohr * Checks if the given input is blank 595b571377SAndreas Gohr * 605b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 615b571377SAndreas Gohr * 6267234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 6367234204SAndreas Gohr * with a NULL value without warning. 6467234204SAndreas Gohr * 6567234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 6667234204SAndreas Gohr * 6767234204SAndreas Gohr * (isset($foo) && !blank($foo)) 6867234204SAndreas Gohr * (!isset($foo) || blank($foo)) 6967234204SAndreas Gohr * 705b571377SAndreas Gohr * @param $in 715b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 725b571377SAndreas Gohr * @return bool 735b571377SAndreas Gohr */ 74d868eb89SAndreas Gohrfunction blank(&$in, $trim = false) 75d868eb89SAndreas Gohr{ 765b571377SAndreas Gohr if (is_null($in)) return true; 7724870174SAndreas Gohr if (is_array($in)) return $in === []; 785b571377SAndreas Gohr if ($in === "\0") return true; 795b571377SAndreas Gohr if ($trim && trim($in) === '') return true; 805b571377SAndreas Gohr if (strlen($in) > 0) return false; 815b571377SAndreas Gohr return empty($in); 825b571377SAndreas Gohr} 835b571377SAndreas Gohr 845b571377SAndreas Gohr/** 85d5197206Schris * print a newline terminated string 86d5197206Schris * 87d5197206Schris * You can give an indention as optional parameter 88d5197206Schris * 89d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 90140cfbcdSGerrit Uitslag * 91140cfbcdSGerrit Uitslag * @param string $string line of text 92140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 93d5197206Schris */ 94d868eb89SAndreas Gohrfunction ptln($string, $indent = 0) 95d868eb89SAndreas Gohr{ 9625ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 9702b0b681SAndreas Gohr} 9802b0b681SAndreas Gohr 9902b0b681SAndreas Gohr/** 10002b0b681SAndreas Gohr * strips control characters (<32) from the given string 10102b0b681SAndreas Gohr * 10202b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 103140cfbcdSGerrit Uitslag * 10442ea7f44SGerrit Uitslag * @param string $string being stripped 105140cfbcdSGerrit Uitslag * @return string 10602b0b681SAndreas Gohr */ 107d868eb89SAndreas Gohrfunction stripctl($string) 108d868eb89SAndreas Gohr{ 10902b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 110d5197206Schris} 111d5197206Schris 112d5197206Schris/** 113634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 114634d7150SAndreas Gohr * 115634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 116634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 117634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 11842ea7f44SGerrit Uitslag * 119634d7150SAndreas Gohr * @return string 120634d7150SAndreas Gohr */ 121d868eb89SAndreas Gohrfunction getSecurityToken() 122d868eb89SAndreas Gohr{ 123585bf44eSChristopher Smith /** @var Input $INPUT */ 124585bf44eSChristopher Smith global $INPUT; 1253680e2cdSAndreas Gohr 1263680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 1273680e2cdSAndreas Gohr $session = session_id(); 1283680e2cdSAndreas Gohr 1293680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1303680e2cdSAndreas Gohr if (trim($user) == '' || trim($session) == '') return ''; 13124870174SAndreas Gohr return PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 132634d7150SAndreas Gohr} 133634d7150SAndreas Gohr 134634d7150SAndreas Gohr/** 135634d7150SAndreas Gohr * Check the secret CSRF token 136140cfbcdSGerrit Uitslag * 137140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 138140cfbcdSGerrit Uitslag * @return bool success if the token matched 139634d7150SAndreas Gohr */ 140d868eb89SAndreas Gohrfunction checkSecurityToken($token = null) 141d868eb89SAndreas Gohr{ 142585bf44eSChristopher Smith /** @var Input $INPUT */ 1437d01a0eaSTom N Harris global $INPUT; 144585bf44eSChristopher Smith if (!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 145df97eaacSAndreas Gohr 1467d01a0eaSTom N Harris if (is_null($token)) $token = $INPUT->str('sectok'); 147634d7150SAndreas Gohr if (getSecurityToken() != $token) { 148634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 149634d7150SAndreas Gohr return false; 150634d7150SAndreas Gohr } 151634d7150SAndreas Gohr return true; 152634d7150SAndreas Gohr} 153634d7150SAndreas Gohr 154634d7150SAndreas Gohr/** 155634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 156634d7150SAndreas Gohr * 157634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 158140cfbcdSGerrit Uitslag * 159140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 16042ea7f44SGerrit Uitslag * @return string html of hidden form field 161634d7150SAndreas Gohr */ 162d868eb89SAndreas Gohrfunction formSecurityToken($print = true) 163d868eb89SAndreas Gohr{ 1642404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1653272d797SAndreas Gohr if ($print) echo $ret; 166634d7150SAndreas Gohr return $ret; 167634d7150SAndreas Gohr} 168634d7150SAndreas Gohr 169634d7150SAndreas Gohr/** 1701015a57dSChristopher Smith * Determine basic information for a request of $id 17115fae107Sandi * 17215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1737e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 174140cfbcdSGerrit Uitslag * 175140cfbcdSGerrit Uitslag * @param string $id pageid 176140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 177140cfbcdSGerrit Uitslag * @return array with info for a request of $id 178140cfbcdSGerrit Uitslag * 179f3f0262cSandi */ 180d868eb89SAndreas Gohrfunction basicinfo($id, $htmlClient = true) 181d868eb89SAndreas Gohr{ 182f3f0262cSandi global $USERINFO; 183585bf44eSChristopher Smith /* @var Input $INPUT */ 184585bf44eSChristopher Smith global $INPUT; 1856afe8dcaSchris 186c66972f2SAdrian Lang // set info about manager/admin status. 18724870174SAndreas Gohr $info = []; 188c66972f2SAdrian Lang $info['isadmin'] = false; 189c66972f2SAdrian Lang $info['ismanager'] = false; 190585bf44eSChristopher Smith if ($INPUT->server->has('REMOTE_USER')) { 191f3f0262cSandi $info['userinfo'] = $USERINFO; 1921015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 193585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 19417ee7f66SAndreas Gohr 195f8cc712eSAndreas Gohr if ($info['perm'] == AUTH_ADMIN) { 196f8cc712eSAndreas Gohr $info['isadmin'] = true; 197f8cc712eSAndreas Gohr $info['ismanager'] = true; 198f8cc712eSAndreas Gohr } elseif (auth_ismanager()) { 199f8cc712eSAndreas Gohr $info['ismanager'] = true; 200f8cc712eSAndreas Gohr } 201f8cc712eSAndreas Gohr 20217ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 203a58fcbbcSAndreas Gohr if (empty($info['userinfo']['name'])) { 204585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 20517ee7f66SAndreas Gohr } 206f3f0262cSandi } else { 2071015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 208ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 209f3f0262cSandi } 210f3f0262cSandi 2111015a57dSChristopher Smith $info['namespace'] = getNS($id); 2121015a57dSChristopher Smith 2131015a57dSChristopher Smith // mobile detection 2141015a57dSChristopher Smith if ($htmlClient) { 2151015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 2161015a57dSChristopher Smith } 2171015a57dSChristopher Smith 2181015a57dSChristopher Smith return $info; 2191015a57dSChristopher Smith } 2201015a57dSChristopher Smith 2211015a57dSChristopher Smith/** 2221015a57dSChristopher Smith * Return info about the current document as associative 2231015a57dSChristopher Smith * array. 2241015a57dSChristopher Smith * 2251015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 226140cfbcdSGerrit Uitslag * 227140cfbcdSGerrit Uitslag * @return array with info about current document 2281015a57dSChristopher Smith */ 229d868eb89SAndreas Gohrfunction pageinfo() 230d868eb89SAndreas Gohr{ 2311015a57dSChristopher Smith global $ID; 2321015a57dSChristopher Smith global $REV; 2331015a57dSChristopher Smith global $RANGE; 2341015a57dSChristopher Smith global $lang; 235585bf44eSChristopher Smith /* @var Input $INPUT */ 236585bf44eSChristopher Smith global $INPUT; 2371015a57dSChristopher Smith 2381015a57dSChristopher Smith $info = basicinfo($ID); 2391015a57dSChristopher Smith 2401015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2411015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2421015a57dSChristopher Smith $info['id'] = $ID; 2431015a57dSChristopher Smith $info['rev'] = $REV; 2441015a57dSChristopher Smith 24575d66495SMichael Große $subManager = new SubscriberManager(); 24675d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2477e87a794SChristopher Smith 248f3f0262cSandi $info['locked'] = checklock($ID); 249317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 25079e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 25101c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2525ec96136SSatoshi Sahara 2532ca9d91cSBen Coburn if ($REV) { 2542ca9d91cSBen Coburn //check if current revision was meant 25501c9a118SAndreas Gohr if ($info['exists'] && ($info['currentrev'] == $REV)) { 2562ca9d91cSBen Coburn $REV = ''; 2577b3a6803SAndreas Gohr } elseif ($RANGE) { 2587b3a6803SAndreas Gohr //section editing does not work with old revisions! 2597b3a6803SAndreas Gohr $REV = ''; 2607b3a6803SAndreas Gohr $RANGE = ''; 2617b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2622ca9d91cSBen Coburn } else { 2632ca9d91cSBen Coburn //really use old revision 264317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 26579e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 266f3f0262cSandi } 267f3f0262cSandi } 268c112d578Sandi $info['rev'] = $REV; 269f3f0262cSandi if ($info['exists']) { 270252acce3SSatoshi Sahara $info['writable'] = (is_writable($info['filepath']) && $info['perm'] >= AUTH_EDIT); 271f3f0262cSandi } else { 272f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 273f3f0262cSandi } 27450e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 275f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 276f3f0262cSandi 27771726d78SBen Coburn //load page meta data 27871726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 27971726d78SBen Coburn 280652610a2Sandi //who's the editor 281047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 282652610a2Sandi if ($REV) { 283f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 28424870174SAndreas Gohr } elseif (!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 285aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 286aa27cf05SAndreas Gohr } else { 287f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 288cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 289cd00a034SBen Coburn if ($revinfo !== false) { 290cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 29124870174SAndreas Gohr p_set_metadata($ID, ['last_change' => $revinfo]); 292cd00a034SBen Coburn } 293cd00a034SBen Coburn } 294cd00a034SBen Coburn //and check for an external edit 295cd00a034SBen Coburn if ($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 296cd00a034SBen Coburn // cached changelog line no longer valid 297cd00a034SBen Coburn $revinfo = false; 298cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 29924870174SAndreas Gohr p_set_metadata($ID, ['last_change' => $revinfo]); 300652610a2Sandi } 301bb4866bdSchris 3020a444b5aSPhy if ($revinfo !== false) { 303652610a2Sandi $info['ip'] = $revinfo['ip']; 304652610a2Sandi $info['user'] = $revinfo['user']; 305652610a2Sandi $info['sum'] = $revinfo['sum']; 30671726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 307ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 30859f257aeSchris 309252acce3SSatoshi Sahara $info['editor'] = $revinfo['user'] ?: $revinfo['ip']; 3100a444b5aSPhy } else { 3110a444b5aSPhy $info['ip'] = null; 3120a444b5aSPhy $info['user'] = null; 3130a444b5aSPhy $info['sum'] = null; 3140a444b5aSPhy $info['editor'] = null; 3150a444b5aSPhy } 316652610a2Sandi 317ee4c4a1bSAndreas Gohr // draft 31824870174SAndreas Gohr $draft = new Draft($ID, $info['client']); 3190aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 3200aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 321ee4c4a1bSAndreas Gohr } 322ee4c4a1bSAndreas Gohr 3231015a57dSChristopher Smith return $info; 3241015a57dSChristopher Smith} 3251015a57dSChristopher Smith 3261015a57dSChristopher Smith/** 3270c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3280c39d46cSMichael Große */ 329d868eb89SAndreas Gohrfunction jsinfo() 330d868eb89SAndreas Gohr{ 3310c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3320c39d46cSMichael Große 3330c39d46cSMichael Große if (!is_array($JSINFO)) { 3340c39d46cSMichael Große $JSINFO = []; 3350c39d46cSMichael Große } 3360c39d46cSMichael Große //export minimal info to JS, plugins can add more 3370c39d46cSMichael Große $JSINFO['id'] = $ID; 33868491db9SPhy $JSINFO['namespace'] = isset($INFO) ? (string) $INFO['namespace'] : ''; 3390c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3400c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3410c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3420c39d46cSMichael Große} 3430c39d46cSMichael Große 3440c39d46cSMichael Große/** 3451015a57dSChristopher Smith * Return information about the current media item as an associative array. 346140cfbcdSGerrit Uitslag * 347140cfbcdSGerrit Uitslag * @return array with info about current media item 3481015a57dSChristopher Smith */ 349d868eb89SAndreas Gohrfunction mediainfo() 350d868eb89SAndreas Gohr{ 3511015a57dSChristopher Smith global $NS; 3521015a57dSChristopher Smith global $IMG; 3531015a57dSChristopher Smith 3541015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3551015a57dSChristopher Smith $info['image'] = $IMG; 3561c548ebeSAndreas Gohr 357f3f0262cSandi return $info; 358f3f0262cSandi} 359f3f0262cSandi 360f3f0262cSandi/** 3612684e50aSAndreas Gohr * Build an string of URL parameters 3622684e50aSAndreas Gohr * 3632684e50aSAndreas Gohr * @author Andreas Gohr 364140cfbcdSGerrit Uitslag * 365140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 366140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 367140cfbcdSGerrit Uitslag * @return string query string 3682684e50aSAndreas Gohr */ 369d868eb89SAndreas Gohrfunction buildURLparams($params, $sep = '&') 370d868eb89SAndreas Gohr{ 3712684e50aSAndreas Gohr $url = ''; 3722684e50aSAndreas Gohr $amp = false; 3732684e50aSAndreas Gohr foreach ($params as $key => $val) { 374b174aeaeSchris if ($amp) $url .= $sep; 3752684e50aSAndreas Gohr 37685e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3773a50618cSgweissbach $url .= rawurlencode((string) $val); 3782684e50aSAndreas Gohr $amp = true; 3792684e50aSAndreas Gohr } 3802684e50aSAndreas Gohr return $url; 3812684e50aSAndreas Gohr} 3822684e50aSAndreas Gohr 3832684e50aSAndreas Gohr/** 3842684e50aSAndreas Gohr * Build an string of html tag attributes 3852684e50aSAndreas Gohr * 3867bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3877bff22c0SAndreas Gohr * 3882684e50aSAndreas Gohr * @author Andreas Gohr 389140cfbcdSGerrit Uitslag * 390140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 391246d3337SMichael Große * @param bool $skipEmptyStrings skip empty string values? 392140cfbcdSGerrit Uitslag * @return string 3932684e50aSAndreas Gohr */ 394d868eb89SAndreas Gohrfunction buildAttributes($params, $skipEmptyStrings = false) 395d868eb89SAndreas Gohr{ 3962684e50aSAndreas Gohr $url = ''; 3979063ec14SAdrian Lang $white = false; 3982684e50aSAndreas Gohr foreach ($params as $key => $val) { 3992401f18dSSyntaxseed if ($key[0] == '_') continue; 400246d3337SMichael Große if ($val === '' && $skipEmptyStrings) continue; 4019063ec14SAdrian Lang if ($white) $url .= ' '; 4027bff22c0SAndreas Gohr 4032684e50aSAndreas Gohr $url .= $key.'="'; 404f7711f2bSAndreas Gohr $url .= hsc($val); 4052684e50aSAndreas Gohr $url .= '"'; 4069063ec14SAdrian Lang $white = true; 4072684e50aSAndreas Gohr } 4082684e50aSAndreas Gohr return $url; 4092684e50aSAndreas Gohr} 4102684e50aSAndreas Gohr 4112684e50aSAndreas Gohr/** 41215fae107Sandi * This builds the breadcrumb trail and returns it as array 41315fae107Sandi * 41415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 415140cfbcdSGerrit Uitslag * 416e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 417f3f0262cSandi */ 418d868eb89SAndreas Gohrfunction breadcrumbs() 419d868eb89SAndreas Gohr{ 4208746e727Sandi // we prepare the breadcrumbs early for quick session closing 4218746e727Sandi static $crumbs = null; 4228746e727Sandi if ($crumbs != null) return $crumbs; 4238746e727Sandi 424f3f0262cSandi global $ID; 425f3f0262cSandi global $ACT; 426f3f0262cSandi global $conf; 4270ea5ebb4SB_S666 global $INFO; 428f3f0262cSandi 429f3f0262cSandi //first visit? 43024870174SAndreas Gohr $crumbs = $_SESSION[DOKU_COOKIE]['bc'] ?? []; 4315603d3c1SHenry Pan //we only save on show and existing visible readable wiki documents 432a77f5846Sjan $file = wikiFN($ID); 4335603d3c1SHenry Pan if ($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) { 434e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 435f3f0262cSandi return $crumbs; 436f3f0262cSandi } 437a77f5846Sjan 438a77f5846Sjan // page names 4391a84a0f3SAnika Henke $name = noNSorNS($ID); 440fe9ec250SChris Smith if (useHeading('navigation')) { 441a77f5846Sjan // get page title 44267c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 443a77f5846Sjan if ($title) { 444a77f5846Sjan $name = $title; 445a77f5846Sjan } 446a77f5846Sjan } 447a77f5846Sjan 448f3f0262cSandi //remove ID from array 449a77f5846Sjan if (isset($crumbs[$ID])) { 450a77f5846Sjan unset($crumbs[$ID]); 451f3f0262cSandi } 452f3f0262cSandi 453f3f0262cSandi //add to array 454a77f5846Sjan $crumbs[$ID] = $name; 455f3f0262cSandi //reduce size 456f3f0262cSandi while (count($crumbs) > $conf['breadcrumbs']) { 457f3f0262cSandi array_shift($crumbs); 458f3f0262cSandi } 459f3f0262cSandi //save to session 460e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 461f3f0262cSandi return $crumbs; 462f3f0262cSandi} 463f3f0262cSandi 464f3f0262cSandi/** 46515fae107Sandi * Filter for page IDs 46615fae107Sandi * 467f3f0262cSandi * This is run on a ID before it is outputted somewhere 468f3f0262cSandi * currently used to replace the colon with something else 469907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 470907f24f7SAndreas Gohr * 471977aa967SAndreas Gohr * See discussions at https://github.com/dokuwiki/dokuwiki/pull/84 and 472977aa967SAndreas Gohr * https://github.com/dokuwiki/dokuwiki/pull/173 why we use a whitelist of 473907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 47415fae107Sandi * 47549c713a3Sandi * Urlencoding is ommitted when the second parameter is false 47649c713a3Sandi * 47715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 478140cfbcdSGerrit Uitslag * 479140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 480140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 481140cfbcdSGerrit Uitslag * @return string 482f3f0262cSandi */ 483d868eb89SAndreas Gohrfunction idfilter($id, $ue = true) 484d868eb89SAndreas Gohr{ 485f3f0262cSandi global $conf; 486585bf44eSChristopher Smith /* @var Input $INPUT */ 487585bf44eSChristopher Smith global $INPUT; 488585bf44eSChristopher Smith 489bf8f8509SAndreas Gohr $id = (string) $id; 490bf8f8509SAndreas Gohr 491f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']) { 492f3f0262cSandi $id = strtr($id, ':', '/'); 493*7d34963bSAndreas Gohr } elseif ( 494*7d34963bSAndreas Gohr strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 49558bedc8aSborekb $conf['userewrite'] && 496585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4973272d797SAndreas Gohr ) { 498f3f0262cSandi $id = strtr($id, ':', ';'); 499f3f0262cSandi } 50049c713a3Sandi if ($ue) { 501b6c6979fSAndreas Gohr $id = rawurlencode($id); 502f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 503edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 504f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 50549c713a3Sandi } 506f3f0262cSandi return $id; 507f3f0262cSandi} 508f3f0262cSandi 509f3f0262cSandi/** 510ed7b5f09Sandi * This builds a link to a wikipage 51115fae107Sandi * 5124bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 5136c7843b5Sandi * 51415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5154bc480e5SAndreas Gohr * 5164bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5174bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5184bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 5194bc480e5SAndreas Gohr * @param string $separator parameter separator 5204bc480e5SAndreas Gohr * @return string 521f3f0262cSandi */ 522d868eb89SAndreas Gohrfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') 523d868eb89SAndreas Gohr{ 524f3f0262cSandi global $conf; 52516f15a81SDominik Eckelmann if (is_array($urlParameters)) { 5264bde2196Slisps if (isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 52764159a61SAndreas Gohr if (isset($urlParameters['at']) && $conf['date_at_format']) { 52864159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 52964159a61SAndreas Gohr } 53016f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 5316de3759aSAndreas Gohr } else { 53216f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 5336de3759aSAndreas Gohr } 53416f15a81SDominik Eckelmann if ($id === '') { 53516f15a81SDominik Eckelmann $id = $conf['start']; 53616f15a81SDominik Eckelmann } 537f3f0262cSandi $id = idfilter($id); 53816f15a81SDominik Eckelmann if ($absolute) { 539ed7b5f09Sandi $xlink = DOKU_URL; 540ed7b5f09Sandi } else { 541ed7b5f09Sandi $xlink = DOKU_BASE; 542ed7b5f09Sandi } 543f3f0262cSandi 5446c7843b5Sandi if ($conf['userewrite'] == 2) { 5456c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 54616f15a81SDominik Eckelmann if ($urlParameters) $xlink .= '?'.$urlParameters; 5476c7843b5Sandi } elseif ($conf['userewrite']) { 548f3f0262cSandi $xlink .= $id; 54916f15a81SDominik Eckelmann if ($urlParameters) $xlink .= '?'.$urlParameters; 55040b5fb5bSPhy } elseif ($id !== '') { 5516c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 55216f15a81SDominik Eckelmann if ($urlParameters) $xlink .= $separator.$urlParameters; 553bce3726dSAndreas Gohr } else { 554bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 55516f15a81SDominik Eckelmann if ($urlParameters) $xlink .= '?'.$urlParameters; 556f3f0262cSandi } 557f3f0262cSandi 558f3f0262cSandi return $xlink; 559f3f0262cSandi} 560f3f0262cSandi 561f3f0262cSandi/** 562f5c2808fSBen Coburn * This builds a link to an alternate page format 563f5c2808fSBen Coburn * 564f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 565f5c2808fSBen Coburn * 566f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5674bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5684bc480e5SAndreas Gohr * @param string $format the export renderer to use 5694bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5704bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5714bc480e5SAndreas Gohr * @param string $sep parameter separator 5724bc480e5SAndreas Gohr * @return string 573f5c2808fSBen Coburn */ 574d868eb89SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') 575d868eb89SAndreas Gohr{ 576f5c2808fSBen Coburn global $conf; 5774bc480e5SAndreas Gohr if (is_array($urlParameters)) { 5784bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 579f5c2808fSBen Coburn } else { 5804bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 581f5c2808fSBen Coburn } 582f5c2808fSBen Coburn 583f5c2808fSBen Coburn $format = rawurlencode($format); 584f5c2808fSBen Coburn $id = idfilter($id); 585f5c2808fSBen Coburn if ($abs) { 586f5c2808fSBen Coburn $xlink = DOKU_URL; 587f5c2808fSBen Coburn } else { 588f5c2808fSBen Coburn $xlink = DOKU_BASE; 589f5c2808fSBen Coburn } 590f5c2808fSBen Coburn 591f5c2808fSBen Coburn if ($conf['userewrite'] == 2) { 592f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5934bc480e5SAndreas Gohr if ($urlParameters) $xlink .= $sep.$urlParameters; 594f5c2808fSBen Coburn } elseif ($conf['userewrite'] == 1) { 595f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5964bc480e5SAndreas Gohr if ($urlParameters) $xlink .= '?'.$urlParameters; 597f5c2808fSBen Coburn } else { 598f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5994bc480e5SAndreas Gohr if ($urlParameters) $xlink .= $sep.$urlParameters; 600f5c2808fSBen Coburn } 601f5c2808fSBen Coburn 602f5c2808fSBen Coburn return $xlink; 603f5c2808fSBen Coburn} 604f5c2808fSBen Coburn 605f5c2808fSBen Coburn/** 6066de3759aSAndreas Gohr * Build a link to a media file 6076de3759aSAndreas Gohr * 6086de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 6098c08db0aSAndreas Gohr * 6108c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 6118c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 6128c08db0aSAndreas Gohr * 6133272d797SAndreas Gohr * @param string $id the media file id or URL 6143272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 6153272d797SAndreas Gohr * @param bool $direct link to detail page if false 6163272d797SAndreas Gohr * @param string $sep URL parameter separator 6173272d797SAndreas Gohr * @param bool $abs Create an absolute URL 6183272d797SAndreas Gohr * @return string 6196de3759aSAndreas Gohr */ 620d868eb89SAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) 621d868eb89SAndreas Gohr{ 6226de3759aSAndreas Gohr global $conf; 623b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 624826d2766SKlap-in if (!$isexternalimage) { 625826d2766SKlap-in $id = cleanID($id); 626826d2766SKlap-in } 627826d2766SKlap-in 6286de3759aSAndreas Gohr if (is_array($more)) { 6290f4e0092SChristopher Smith // add token for resized images 63024870174SAndreas Gohr $w = $more['w'] ?? null; 63124870174SAndreas Gohr $h = $more['h'] ?? null; 63298fe1ac9SDamien Regad if ($w || $h || $isexternalimage) { 633357c9a39SDamien Regad $more['tok'] = media_get_token($id, $w, $h); 6340f4e0092SChristopher Smith } 6358c08db0aSAndreas Gohr // strip defaults for shorter URLs 6368c08db0aSAndreas Gohr if (isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 637443e135dSChristopher Smith if (empty($more['w'])) unset($more['w']); 638443e135dSChristopher Smith if (empty($more['h'])) unset($more['h']); 6398c08db0aSAndreas Gohr if (isset($more['id']) && $direct) unset($more['id']); 64078b874e6Slisps if (isset($more['rev']) && !$more['rev']) unset($more['rev']); 641b174aeaeSchris $more = buildURLparams($more, $sep); 6426de3759aSAndreas Gohr } else { 64324870174SAndreas Gohr $matches = []; 644cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/', $more, $matches, PREG_SET_ORDER) || $isexternalimage) { 64524870174SAndreas Gohr $resize = ['w'=>0, 'h'=>0]; 6465e7db1e2SChristopher Smith foreach ($matches as $match) { 6475e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6485e7db1e2SChristopher Smith } 649cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 650cc036f74SKlap-in $more .= 'tok='.media_get_token($id, $resize['w'], $resize['h']); 6515e7db1e2SChristopher Smith } 6528c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6538c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 654b174aeaeSchris $more = str_replace(',', $sep, $more); 6556de3759aSAndreas Gohr } 6566de3759aSAndreas Gohr 65755b2b31bSAndreas Gohr if ($abs) { 65855b2b31bSAndreas Gohr $xlink = DOKU_URL; 65955b2b31bSAndreas Gohr } else { 6606de3759aSAndreas Gohr $xlink = DOKU_BASE; 66155b2b31bSAndreas Gohr } 6626de3759aSAndreas Gohr 6636de3759aSAndreas Gohr // external URLs are always direct without rewriting 664826d2766SKlap-in if ($isexternalimage) { 6656de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 666cc036f74SKlap-in $xlink .= '?'.$more; 667b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6686de3759aSAndreas Gohr return $xlink; 6696de3759aSAndreas Gohr } 6706de3759aSAndreas Gohr 6716de3759aSAndreas Gohr $id = idfilter($id); 6726de3759aSAndreas Gohr 6736de3759aSAndreas Gohr // decide on scriptname 6746de3759aSAndreas Gohr if ($direct) { 6756de3759aSAndreas Gohr if ($conf['userewrite'] == 1) { 6766de3759aSAndreas Gohr $script = '_media'; 6776de3759aSAndreas Gohr } else { 6786de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6796de3759aSAndreas Gohr } 68024870174SAndreas Gohr } elseif ($conf['userewrite'] == 1) { 6816de3759aSAndreas Gohr $script = '_detail'; 6826de3759aSAndreas Gohr } else { 6836de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6846de3759aSAndreas Gohr } 6856de3759aSAndreas Gohr 6866de3759aSAndreas Gohr // build URL based on rewrite mode 6876de3759aSAndreas Gohr if ($conf['userewrite']) { 6886de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6896de3759aSAndreas Gohr if ($more) $xlink .= '?'.$more; 69024870174SAndreas Gohr } elseif ($more) { 691a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 692b174aeaeSchris $xlink .= $sep.'media='.$id; 6936de3759aSAndreas Gohr } else { 694a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6956de3759aSAndreas Gohr } 6966de3759aSAndreas Gohr 6976de3759aSAndreas Gohr return $xlink; 6986de3759aSAndreas Gohr} 6996de3759aSAndreas Gohr 7006de3759aSAndreas Gohr/** 70125ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 70215fae107Sandi * 70325ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 70425ca5b17SAndreas Gohr * 70515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 706140cfbcdSGerrit Uitslag * 707140cfbcdSGerrit Uitslag * @return string 708f3f0262cSandi */ 709d868eb89SAndreas Gohrfunction script() 710d868eb89SAndreas Gohr{ 711ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 712f3f0262cSandi} 713f3f0262cSandi 714f3f0262cSandi/** 71515fae107Sandi * Spamcheck against wordlist 71615fae107Sandi * 717f3f0262cSandi * Checks the wikitext against a list of blocked expressions 718f3f0262cSandi * returns true if the text contains any bad words 71915fae107Sandi * 720e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 721e403cc58SMichael Klier * 722e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 723e403cc58SMichael Klier * and gain information about the user who was blocked. 724e403cc58SMichael Klier * 725e403cc58SMichael Klier * Event data: 726e403cc58SMichael Klier * data['matches'] - array of matches 727e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 728e403cc58SMichael Klier * [ip] - ip address 729e403cc58SMichael Klier * [user] - username (if logged in) 730e403cc58SMichael Klier * [mail] - mail address (if logged in) 731e403cc58SMichael Klier * [name] - real name (if logged in) 732e403cc58SMichael Klier * 73315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 7346dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 735140cfbcdSGerrit Uitslag * 7366dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 7376dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 738f3f0262cSandi */ 739d868eb89SAndreas Gohrfunction checkwordblock($text = '') 740d868eb89SAndreas Gohr{ 741f3f0262cSandi global $TEXT; 7426dffa0e0SAndreas Gohr global $PRE; 7436dffa0e0SAndreas Gohr global $SUF; 744e0086ca2SAndreas Gohr global $SUM; 745f3f0262cSandi global $conf; 746e403cc58SMichael Klier global $INFO; 747585bf44eSChristopher Smith /* @var Input $INPUT */ 748585bf44eSChristopher Smith global $INPUT; 749f3f0262cSandi 750f3f0262cSandi if (!$conf['usewordblock']) return false; 751f3f0262cSandi 752e0086ca2SAndreas Gohr if (!$text) $text = "$PRE $TEXT $SUF $SUM"; 7536dffa0e0SAndreas Gohr 754041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 75564159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 75664159a61SAndreas Gohr $text = preg_replace( 75764159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 75864159a61SAndreas Gohr '\1http://\2 \2\3', 75964159a61SAndreas Gohr $text 76064159a61SAndreas Gohr ); 76164159a61SAndreas Gohr // phpcs:enable 762041d1964SAndreas Gohr 763b9ac8716Schris $wordblocks = getWordblocks(); 764a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7653e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 766a51d08efSAndreas Gohr $chunksize = 200; 76764259528SAndreas Gohr 768b9ac8716Schris while ($blocks = array_splice($wordblocks, 0, $chunksize)) { 76924870174SAndreas Gohr $re = []; 77049eb6e38SAndreas Gohr // build regexp from blocks 771f3f0262cSandi foreach ($blocks as $block) { 772f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 773f3f0262cSandi $block = trim($block); 774f3f0262cSandi if (empty($block)) continue; 775f3f0262cSandi $re[] = $block; 776f3f0262cSandi } 77724870174SAndreas Gohr if (count($re) && preg_match('#('.implode('|', $re).')#si', $text, $matches)) { 778e403cc58SMichael Klier // prepare event data 77924870174SAndreas Gohr $data = []; 780e403cc58SMichael Klier $data['matches'] = $matches; 781585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 782585bf44eSChristopher Smith if ($INPUT->server->str('REMOTE_USER')) { 783585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 784e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 785e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 786e403cc58SMichael Klier } 78724870174SAndreas Gohr $callback = static fn() => true; 788cbb44eabSAndreas Gohr return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 789b9ac8716Schris } 790703f6fdeSandi } 791f3f0262cSandi return false; 792f3f0262cSandi} 793f3f0262cSandi 794f3f0262cSandi/** 79515fae107Sandi * Return the IP of the client 79615fae107Sandi * 7976d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 79815fae107Sandi * 7996d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 8006d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 8016d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 8026d8affe6SAndreas Gohr * headers 8036d8affe6SAndreas Gohr * 80415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 805140cfbcdSGerrit Uitslag * 8063272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 8073272d797SAndreas Gohr * @return string 808f3f0262cSandi */ 809d868eb89SAndreas Gohrfunction clientIP($single = false) 810d868eb89SAndreas Gohr{ 811585bf44eSChristopher Smith /* @var Input $INPUT */ 812925105e8SPhy global $INPUT, $conf; 813585bf44eSChristopher Smith 81424870174SAndreas Gohr $ip = []; 815585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 816585bf44eSChristopher Smith if ($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 817585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 818585bf44eSChristopher Smith } 819585bf44eSChristopher Smith if ($INPUT->server->str('HTTP_X_REAL_IP')) { 820585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 821585bf44eSChristopher Smith } 8226d8affe6SAndreas Gohr 8236d8affe6SAndreas Gohr // remove any non-IP stuff 8246d8affe6SAndreas Gohr $cnt = count($ip); 8256d8affe6SAndreas Gohr for ($i = 0; $i < $cnt; $i++) { 8260a5f08e5SAdaKaleh if (filter_var($ip[$i], FILTER_VALIDATE_IP) === false) { 8270a5f08e5SAdaKaleh unset($ip[$i]); 8284ff28443Schris } 829f3f0262cSandi } 8306d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 83124870174SAndreas Gohr if ($ip === [] || !$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8326d8affe6SAndreas Gohr 83324870174SAndreas Gohr if (!$single) return implode(',', $ip); 8346d8affe6SAndreas Gohr 835925105e8SPhy // skip trusted local addresses 8366d8affe6SAndreas Gohr foreach ($ip as $i) { 837925105e8SPhy if (!empty($conf['trustedproxy']) && preg_match('/'.$conf['trustedproxy'].'/', $i)) { 8386d8affe6SAndreas Gohr continue; 8396d8affe6SAndreas Gohr } else { 8406d8affe6SAndreas Gohr return $i; 8416d8affe6SAndreas Gohr } 8426d8affe6SAndreas Gohr } 843925105e8SPhy 844925105e8SPhy // still here? just use the last address 845925105e8SPhy // this case all ips in the list are trusted 846925105e8SPhy return $ip[count($ip)-1]; 847f3f0262cSandi} 848f3f0262cSandi 849f3f0262cSandi/** 8501c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8511c548ebeSAndreas Gohr * 8521c548ebeSAndreas Gohr * Adapted from the example code at url below 8531c548ebeSAndreas Gohr * 8541c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 855140cfbcdSGerrit Uitslag * 85664159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 857140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8581c548ebeSAndreas Gohr */ 859d868eb89SAndreas Gohrfunction clientismobile() 860d868eb89SAndreas Gohr{ 861585bf44eSChristopher Smith /* @var Input $INPUT */ 862585bf44eSChristopher Smith global $INPUT; 8631c548ebeSAndreas Gohr 864585bf44eSChristopher Smith if ($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8651c548ebeSAndreas Gohr 866585bf44eSChristopher Smith if (preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8671c548ebeSAndreas Gohr 868585bf44eSChristopher Smith if (!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8691c548ebeSAndreas Gohr 87024870174SAndreas Gohr $uamatches = implode( 87164159a61SAndreas Gohr '|', 87264159a61SAndreas Gohr [ 87364159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 87464159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 87564159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 87664159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 87764159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 87864159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 87964159a61SAndreas Gohr '\d\d\di', 'moto' 88064159a61SAndreas Gohr ] 88164159a61SAndreas Gohr ); 8821c548ebeSAndreas Gohr 883585bf44eSChristopher Smith if (preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8841c548ebeSAndreas Gohr 8851c548ebeSAndreas Gohr return false; 8861c548ebeSAndreas Gohr} 8871c548ebeSAndreas Gohr 8881c548ebeSAndreas Gohr/** 8896efc45a2SDmitry Katsubo * check if a given link is interwiki link 8906efc45a2SDmitry Katsubo * 8916efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8926efc45a2SDmitry Katsubo * @return bool 8936efc45a2SDmitry Katsubo */ 894d868eb89SAndreas Gohrfunction link_isinterwiki($link) 895d868eb89SAndreas Gohr{ 8966efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u', $link)) return true; 8976efc45a2SDmitry Katsubo return false; 8986efc45a2SDmitry Katsubo} 8996efc45a2SDmitry Katsubo 9006efc45a2SDmitry Katsubo/** 90163211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 90263211f61SGlen Harris * 90322ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 90422ef1e32SAndreas Gohr * 90563211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 906140cfbcdSGerrit Uitslag * 9073272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 9083272d797SAndreas Gohr * @return string a comma separated list of hostnames 90963211f61SGlen Harris */ 910d868eb89SAndreas Gohrfunction gethostsbyaddrs($ips) 911d868eb89SAndreas Gohr{ 91222ef1e32SAndreas Gohr global $conf; 91322ef1e32SAndreas Gohr if (!$conf['dnslookups']) return $ips; 91422ef1e32SAndreas Gohr 91524870174SAndreas Gohr $hosts = []; 91663211f61SGlen Harris $ips = explode(',', $ips); 917551a720fSMichael Klier 918551a720fSMichael Klier if (is_array($ips)) { 9193886270dSAndreas Gohr foreach ($ips as $ip) { 920551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 92163211f61SGlen Harris } 92224870174SAndreas Gohr return implode(',', $hosts); 923551a720fSMichael Klier } else { 924551a720fSMichael Klier return gethostbyaddr(trim($ips)); 925551a720fSMichael Klier } 92663211f61SGlen Harris} 92763211f61SGlen Harris 92863211f61SGlen Harris/** 92915fae107Sandi * Checks if a given page is currently locked. 93015fae107Sandi * 931f3f0262cSandi * removes stale lockfiles 93215fae107Sandi * 93315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 934140cfbcdSGerrit Uitslag * 935140cfbcdSGerrit Uitslag * @param string $id page id 936140cfbcdSGerrit Uitslag * @return bool page is locked? 937f3f0262cSandi */ 938d868eb89SAndreas Gohrfunction checklock($id) 939d868eb89SAndreas Gohr{ 940f3f0262cSandi global $conf; 941585bf44eSChristopher Smith /* @var Input $INPUT */ 942585bf44eSChristopher Smith global $INPUT; 943585bf44eSChristopher Smith 944c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 945f3f0262cSandi 946f3f0262cSandi //no lockfile 94779e79377SAndreas Gohr if (!file_exists($lock)) return false; 948f3f0262cSandi 949f3f0262cSandi //lockfile expired 950f3f0262cSandi if ((time() - filemtime($lock)) > $conf['locktime']) { 951d8186216SBen Coburn @unlink($lock); 952f3f0262cSandi return false; 953f3f0262cSandi } 954f3f0262cSandi 955f3f0262cSandi //my own lock 95624870174SAndreas Gohr @[$ip, $session] = explode("\n", io_readFile($lock)); 95724870174SAndreas Gohr if ($ip == $INPUT->server->str('REMOTE_USER') || (session_id() && $session === session_id())) { 958f3f0262cSandi return false; 959f3f0262cSandi } 960f3f0262cSandi 961f3f0262cSandi return $ip; 962f3f0262cSandi} 963f3f0262cSandi 964f3f0262cSandi/** 96515fae107Sandi * Lock a page for editing 96615fae107Sandi * 96715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 968140cfbcdSGerrit Uitslag * 969140cfbcdSGerrit Uitslag * @param string $id page id to lock 970f3f0262cSandi */ 971d868eb89SAndreas Gohrfunction lock($id) 972d868eb89SAndreas Gohr{ 973544ed901SDaniel Calviño Sánchez global $conf; 974585bf44eSChristopher Smith /* @var Input $INPUT */ 975585bf44eSChristopher Smith global $INPUT; 976544ed901SDaniel Calviño Sánchez 977544ed901SDaniel Calviño Sánchez if ($conf['locktime'] == 0) { 978544ed901SDaniel Calviño Sánchez return; 979544ed901SDaniel Calviño Sánchez } 980544ed901SDaniel Calviño Sánchez 981c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 982585bf44eSChristopher Smith if ($INPUT->server->str('REMOTE_USER')) { 983585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 984f3f0262cSandi } else { 98585fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 986f3f0262cSandi } 987f3f0262cSandi} 988f3f0262cSandi 989f3f0262cSandi/** 99015fae107Sandi * Unlock a page if it was locked by the user 991f3f0262cSandi * 99215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 993140cfbcdSGerrit Uitslag * 9943272d797SAndreas Gohr * @param string $id page id to unlock 99515fae107Sandi * @return bool true if a lock was removed 996f3f0262cSandi */ 997d868eb89SAndreas Gohrfunction unlock($id) 998d868eb89SAndreas Gohr{ 999585bf44eSChristopher Smith /* @var Input $INPUT */ 1000585bf44eSChristopher Smith global $INPUT; 1001585bf44eSChristopher Smith 1002c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 100379e79377SAndreas Gohr if (file_exists($lock)) { 100424870174SAndreas Gohr @[$ip, $session] = explode("\n", io_readFile($lock)); 1005c0dd3914SAdaKaleh if ($ip == $INPUT->server->str('REMOTE_USER') || $session == session_id()) { 1006f3f0262cSandi @unlink($lock); 1007f3f0262cSandi return true; 1008f3f0262cSandi } 1009f3f0262cSandi } 1010f3f0262cSandi return false; 1011f3f0262cSandi} 1012f3f0262cSandi 1013f3f0262cSandi/** 1014f3f0262cSandi * convert line ending to unix format 1015f3f0262cSandi * 10166db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 10176db7468bSAndreas Gohr * 101815fae107Sandi * @see formText() for 2crlf conversion 101915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1020140cfbcdSGerrit Uitslag * 1021140cfbcdSGerrit Uitslag * @param string $text 1022140cfbcdSGerrit Uitslag * @return string 1023f3f0262cSandi */ 1024d868eb89SAndreas Gohrfunction cleanText($text) 1025d868eb89SAndreas Gohr{ 1026f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10276db7468bSAndreas Gohr 10286db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10296db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10306db7468bSAndreas Gohr // but might actually fix the problem in many cases 103124870174SAndreas Gohr if (!Clean::isUtf8($text)) $text = utf8_encode($text); 10326db7468bSAndreas Gohr 1033f3f0262cSandi return $text; 1034f3f0262cSandi} 1035f3f0262cSandi 1036f3f0262cSandi/** 1037f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1038f3f0262cSandi * It also converts line endings to Windows format which is 1039f3f0262cSandi * pseudo standard for webforms. 1040f3f0262cSandi * 104115fae107Sandi * @see cleanText() for 2unix conversion 104215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1043140cfbcdSGerrit Uitslag * 1044140cfbcdSGerrit Uitslag * @param string $text 1045140cfbcdSGerrit Uitslag * @return string 1046f3f0262cSandi */ 1047d868eb89SAndreas Gohrfunction formText($text) 1048d868eb89SAndreas Gohr{ 1049a46a37efSAndreas Gohr $text = str_replace("\012", "\015\012", $text ?? ''); 1050f3f0262cSandi return htmlspecialchars($text); 1051f3f0262cSandi} 1052f3f0262cSandi 1053f3f0262cSandi/** 105415fae107Sandi * Returns the specified local text in raw format 105515fae107Sandi * 105615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1057140cfbcdSGerrit Uitslag * 1058140cfbcdSGerrit Uitslag * @param string $id page id 1059140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1060140cfbcdSGerrit Uitslag * @return string 1061f3f0262cSandi */ 1062d868eb89SAndreas Gohrfunction rawLocale($id, $ext = 'txt') 1063d868eb89SAndreas Gohr{ 10642adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1065f3f0262cSandi} 1066f3f0262cSandi 1067f3f0262cSandi/** 1068f3f0262cSandi * Returns the raw WikiText 106915fae107Sandi * 107015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1071140cfbcdSGerrit Uitslag * 1072140cfbcdSGerrit Uitslag * @param string $id page id 1073e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1074140cfbcdSGerrit Uitslag * @return string 1075f3f0262cSandi */ 1076d868eb89SAndreas Gohrfunction rawWiki($id, $rev = '') 1077d868eb89SAndreas Gohr{ 1078cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1079f3f0262cSandi} 1080f3f0262cSandi 1081f3f0262cSandi/** 10827146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10837146cee2SAndreas Gohr * 10847b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10857146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1086140cfbcdSGerrit Uitslag * 1087140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1088140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10897146cee2SAndreas Gohr */ 1090d868eb89SAndreas Gohrfunction pageTemplate($id) 1091d868eb89SAndreas Gohr{ 1092a15ce62dSEsther Brunner global $conf; 1093e29549feSAndreas Gohr 1094fe17917eSAdrian Lang if (is_array($id)) $id = $id[0]; 1095e29549feSAndreas Gohr 10967b84afa2SAndreas Gohr // prepare initial event data 109724870174SAndreas Gohr $data = [ 10987b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10997b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 11007b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 110124870174SAndreas Gohr 'doreplace' => true, 110224870174SAndreas Gohr ]; 11037b84afa2SAndreas Gohr 1104e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_PAGETPL_LOAD', $data); 11057b84afa2SAndreas Gohr if ($evt->advise_before(true)) { 11067b84afa2SAndreas Gohr // the before event might have loaded the content already 11077b84afa2SAndreas Gohr if (empty($data['tpl'])) { 11087b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 11097b84afa2SAndreas Gohr if (empty($data['tplfile'])) { 1110fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 111179e79377SAndreas Gohr if (file_exists($path.'/_template.txt')) { 11127b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1113e29549feSAndreas Gohr } else { 1114e29549feSAndreas Gohr // search upper namespaces for templates 1115e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1116e29549feSAndreas Gohr while (strlen($path) >= $len) { 111779e79377SAndreas Gohr if (file_exists($path.'/__template.txt')) { 11187b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1119e29549feSAndreas Gohr break; 1120e29549feSAndreas Gohr } 1121e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1122e29549feSAndreas Gohr } 1123e29549feSAndreas Gohr } 11247b84afa2SAndreas Gohr } 11257b84afa2SAndreas Gohr // load the content 11263d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 11277b84afa2SAndreas Gohr } 1128a1bbd05bSMichael Hamann if ($data['doreplace']) parsePageTemplate($data); 11297b84afa2SAndreas Gohr } 11307b84afa2SAndreas Gohr $evt->advise_after(); 11317b84afa2SAndreas Gohr unset($evt); 11327b84afa2SAndreas Gohr 1133fe17917eSAdrian Lang return $data['tpl']; 11342b1223ecSAdrian Lang} 11352b1223ecSAdrian Lang 11362b1223ecSAdrian Lang/** 11372b1223ecSAdrian Lang * Performs common page template replacements 11387b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11392b1223ecSAdrian Lang * 11402b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1141140cfbcdSGerrit Uitslag * 1142140cfbcdSGerrit Uitslag * @param array $data array with event data 1143140cfbcdSGerrit Uitslag * @return string 11442b1223ecSAdrian Lang */ 1145d868eb89SAndreas Gohrfunction parsePageTemplate(&$data) 1146d868eb89SAndreas Gohr{ 11473272d797SAndreas Gohr /** 11483272d797SAndreas Gohr * @var string $id the id of the page to be created 11493272d797SAndreas Gohr * @var string $tpl the text used as template 11503272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11513272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11523272d797SAndreas Gohr */ 1153fe17917eSAdrian Lang extract($data); 1154fe17917eSAdrian Lang 1155b856f7dfSAdrian Lang global $USERINFO; 1156bce53b1fSAdrian Lang global $conf; 1157585bf44eSChristopher Smith /* @var Input $INPUT */ 1158585bf44eSChristopher Smith global $INPUT; 1159e29549feSAndreas Gohr 1160e29549feSAndreas Gohr // replace placeholders 116126ece5a7SAndreas Gohr $file = noNS($id); 116237c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 116326ece5a7SAndreas Gohr 11643272d797SAndreas Gohr $tpl = str_replace( 116524870174SAndreas Gohr [ 116626ece5a7SAndreas Gohr '@ID@', 116726ece5a7SAndreas Gohr '@NS@', 11688a7bcf66SShota Miyazaki '@CURNS@', 1169a3db0ab0SSimon Lees '@!CURNS@', 1170a3db0ab0SSimon Lees '@!!CURNS@', 1171a3db0ab0SSimon Lees '@!CURNS!@', 117226ece5a7SAndreas Gohr '@FILE@', 117326ece5a7SAndreas Gohr '@!FILE@', 117426ece5a7SAndreas Gohr '@!FILE!@', 117526ece5a7SAndreas Gohr '@PAGE@', 117626ece5a7SAndreas Gohr '@!PAGE@', 117726ece5a7SAndreas Gohr '@!!PAGE@', 117826ece5a7SAndreas Gohr '@!PAGE!@', 117926ece5a7SAndreas Gohr '@USER@', 118026ece5a7SAndreas Gohr '@NAME@', 118126ece5a7SAndreas Gohr '@MAIL@', 118224870174SAndreas Gohr '@DATE@' 118324870174SAndreas Gohr ], 118424870174SAndreas Gohr [ 118526ece5a7SAndreas Gohr $id, 118626ece5a7SAndreas Gohr getNS($id), 11878a7bcf66SShota Miyazaki curNS($id), 118824870174SAndreas Gohr PhpString::ucfirst(curNS($id)), 118924870174SAndreas Gohr PhpString::ucwords(curNS($id)), 119024870174SAndreas Gohr PhpString::strtoupper(curNS($id)), 119126ece5a7SAndreas Gohr $file, 119224870174SAndreas Gohr PhpString::ucfirst($file), 119324870174SAndreas Gohr PhpString::strtoupper($file), 119426ece5a7SAndreas Gohr $page, 119524870174SAndreas Gohr PhpString::ucfirst($page), 119624870174SAndreas Gohr PhpString::ucwords($page), 119724870174SAndreas Gohr PhpString::strtoupper($page), 1198585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11993e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 12003e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 120124870174SAndreas Gohr $conf['dformat'] 120224870174SAndreas Gohr ], 120324870174SAndreas Gohr $tpl 12043272d797SAndreas Gohr ); 120526ece5a7SAndreas Gohr 12067d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1207bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1208bad6fc0dSAndreas Gohr '/%./', 120924870174SAndreas Gohr static fn($m) => dformat(null, $m[0]), 1210bad6fc0dSAndreas Gohr $tpl 1211bad6fc0dSAndreas Gohr ); 1212d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1213a15ce62dSEsther Brunner return $tpl; 12147146cee2SAndreas Gohr} 12157146cee2SAndreas Gohr 12167146cee2SAndreas Gohr/** 121715fae107Sandi * Returns the raw Wiki Text in three slices. 121815fae107Sandi * 121915fae107Sandi * The range parameter needs to have the form "from-to" 122015cfe303Sandi * and gives the range of the section in bytes - no 122115cfe303Sandi * UTF-8 awareness is needed. 1222f3f0262cSandi * The returned order is prefix, section and suffix. 122315fae107Sandi * 122415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1225140cfbcdSGerrit Uitslag * 1226140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1227140cfbcdSGerrit Uitslag * @param string $id page id 1228140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 122942ea7f44SGerrit Uitslag * @return string[] with three slices 1230f3f0262cSandi */ 1231d868eb89SAndreas Gohrfunction rawWikiSlices($range, $id, $rev = '') 1232d868eb89SAndreas Gohr{ 1233cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1234f3f0262cSandi 123580fcb268SAdrian Lang // Parse range 123624870174SAndreas Gohr [$from, $to] = sexplode('-', $range, 2); 123780fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 123824870174SAndreas Gohr $from = $from ? $from - 1 : (0); 123924870174SAndreas Gohr $to = $to ? $to - 1 : (strlen($text)); 124080fcb268SAdrian Lang 124124870174SAndreas Gohr $slices = []; 124280fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 124380fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 124415cfe303Sandi $slices[2] = substr($text, $to); 1245f3f0262cSandi return $slices; 1246f3f0262cSandi} 1247f3f0262cSandi 1248f3f0262cSandi/** 124915fae107Sandi * Joins wiki text slices 125015fae107Sandi * 125180fcb268SAdrian Lang * function to join the text slices. 1252f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1253f3f0262cSandi * lines between sections if needed (used on saving). 125415fae107Sandi * 125515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1256140cfbcdSGerrit Uitslag * 1257140cfbcdSGerrit Uitslag * @param string $pre prefix 1258140cfbcdSGerrit Uitslag * @param string $text text in the middle 1259140cfbcdSGerrit Uitslag * @param string $suf suffix 1260140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1261140cfbcdSGerrit Uitslag * @return string 1262f3f0262cSandi */ 1263d868eb89SAndreas Gohrfunction con($pre, $text, $suf, $pretty = false) 1264d868eb89SAndreas Gohr{ 1265f3f0262cSandi if ($pretty) { 1266*7d34963bSAndreas Gohr if ( 1267*7d34963bSAndreas Gohr $pre !== '' && substr($pre, -1) !== "\n" && 12683272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12693272d797SAndreas Gohr ) { 127080fcb268SAdrian Lang $pre .= "\n"; 127180fcb268SAdrian Lang } 1272*7d34963bSAndreas Gohr if ( 1273*7d34963bSAndreas Gohr $suf !== '' && substr($text, -1) !== "\n" && 12743272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12753272d797SAndreas Gohr ) { 127680fcb268SAdrian Lang $text .= "\n"; 127780fcb268SAdrian Lang } 1278f3f0262cSandi } 1279f3f0262cSandi 1280f3f0262cSandi return $pre.$text.$suf; 1281f3f0262cSandi} 1282f3f0262cSandi 1283f3f0262cSandi/** 1284b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1285b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1286b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1287b24d9195SAndreas Gohr * 1288b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1289b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1290b24d9195SAndreas Gohr * 1291b24d9195SAndreas Gohr * @param string $id the page ID 129269f9b481SSatoshi Sahara * @deprecated 2021-11-28 1293b24d9195SAndreas Gohr */ 1294d868eb89SAndreas Gohrfunction detectExternalEdit($id) 1295d868eb89SAndreas Gohr{ 129679a2d784SGerrit Uitslag dbg_deprecated(PageFile::class .'::detectExternalEdit()'); 1297b24e9c4aSSatoshi Sahara (new PageFile($id))->detectExternalEdit(); 1298b24d9195SAndreas Gohr} 1299b24d9195SAndreas Gohr 1300b24d9195SAndreas Gohr/** 1301a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1302a701424fSBen Coburn * Also directs changelog and attic updates. 130315fae107Sandi * 130415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 130571726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1306140cfbcdSGerrit Uitslag * 1307140cfbcdSGerrit Uitslag * @param string $id page id 1308140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1309140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1310140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1311f3f0262cSandi */ 1312d868eb89SAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) 1313d868eb89SAndreas Gohr{ 1314585bf44eSChristopher Smith 1315b24e9c4aSSatoshi Sahara // get COMMON_WIKIPAGE_SAVE event data 1316b24e9c4aSSatoshi Sahara $data = (new PageFile($id))->saveWikiText($text, $summary, $minor); 1317a577fbc2SAndreas Gohr if (!$data) return; // save was cancelled (for no changes or by a plugin) 1318ac3ed4afSGerrit Uitslag 131926a0801fSAndreas Gohr // send notify mails 132024870174SAndreas Gohr ['oldRevision' => $rev, 'newRevision' => $new_rev, 'summary' => $summary] = $data; 13213b813d43SSatoshi Sahara notify($id, 'admin', $rev, $summary, $minor, $new_rev); 13223b813d43SSatoshi Sahara notify($id, 'subscribers', $rev, $summary, $minor, $new_rev); 1323f3f0262cSandi 13242eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1325fe9ec250SChris Smith if (useHeading('content')) { 132607ff0babSMichael Hamann $pages = ft_backlinks($id, true); 13272eccbdaaSGina Haeussge foreach ($pages as $page) { 13280db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 13292eccbdaaSGina Haeussge $cache->removeCache(); 13302eccbdaaSGina Haeussge } 13312eccbdaaSGina Haeussge } 1332f3f0262cSandi} 1333f3f0262cSandi 1334f3f0262cSandi/** 1335d5824ab9SSatoshi Sahara * moves the current version to the attic and returns its revision date 133615fae107Sandi * 133715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1338140cfbcdSGerrit Uitslag * 1339140cfbcdSGerrit Uitslag * @param string $id page id 1340140cfbcdSGerrit Uitslag * @return int|string revision timestamp 134169f9b481SSatoshi Sahara * @deprecated 2021-11-28 1342f3f0262cSandi */ 1343d868eb89SAndreas Gohrfunction saveOldRevision($id) 1344d868eb89SAndreas Gohr{ 134579a2d784SGerrit Uitslag dbg_deprecated(PageFile::class .'::saveOldRevision()'); 1346b24e9c4aSSatoshi Sahara return (new PageFile($id))->saveOldRevision(); 1347f3f0262cSandi} 1348f3f0262cSandi 1349f3f0262cSandi/** 1350fde10de4SAdrian Lang * Sends a notify mail on page change or registration 135126a0801fSAndreas Gohr * 135226a0801fSAndreas Gohr * @param string $id The changed page 1353fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 13543272d797SAndreas Gohr * @param int|string $rev Old page revision 135526a0801fSAndreas Gohr * @param string $summary What changed 135690033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 135742ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 135883734cddSPhy * @param int|string $current_rev New page revision 13593272d797SAndreas Gohr * @return bool 1360140cfbcdSGerrit Uitslag * 136115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1362f3f0262cSandi */ 1363d868eb89SAndreas Gohrfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = [], $current_rev = false) 1364d868eb89SAndreas Gohr{ 1365f3f0262cSandi global $conf; 1366585bf44eSChristopher Smith /* @var Input $INPUT */ 1367585bf44eSChristopher Smith global $INPUT; 1368b158d625SSteven Danz 13696df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 137026a0801fSAndreas Gohr if ($who == 'admin') { 13713272d797SAndreas Gohr if (empty($conf['notify'])) return false; //notify enabled? 13722ed38036SAndreas Gohr $tpl = 'mailtext'; 137326a0801fSAndreas Gohr $to = $conf['notify']; 137426a0801fSAndreas Gohr } elseif ($who == 'subscribers') { 137584c1127cSAndreas Gohr if (!actionOK('subscribe')) return false; //subscribers enabled? 1376585bf44eSChristopher Smith if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 137724870174SAndreas Gohr $data = ['id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace]; 1378cbb44eabSAndreas Gohr Event::createAndTrigger( 1379dccd6b2bSAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', 1380dccd6b2bSAndreas Gohr $data, 138124870174SAndreas Gohr [new SubscriberManager(), 'notifyAddresses'] 13823272d797SAndreas Gohr ); 13832ed38036SAndreas Gohr $to = $data['addresslist']; 13842ed38036SAndreas Gohr if (empty($to)) return false; 13852ed38036SAndreas Gohr $tpl = 'subscr_single'; 138626a0801fSAndreas Gohr } else { 13873272d797SAndreas Gohr return false; //just to be safe 138826a0801fSAndreas Gohr } 138926a0801fSAndreas Gohr 13906df843eeSAndreas Gohr // prepare content 1391704a815fSMichael Große $subscription = new PageSubscriptionSender(); 139283734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1393f3f0262cSandi} 13942ed38036SAndreas Gohr 139515fae107Sandi/** 139671f7bde7SAndreas Gohr * extracts the query from a search engine referrer 139715fae107Sandi * 139815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 139971f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1400140cfbcdSGerrit Uitslag * 1401140cfbcdSGerrit Uitslag * @return array|string 1402f3f0262cSandi */ 1403d868eb89SAndreas Gohrfunction getGoogleQuery() 1404d868eb89SAndreas Gohr{ 1405585bf44eSChristopher Smith /* @var Input $INPUT */ 1406585bf44eSChristopher Smith global $INPUT; 1407585bf44eSChristopher Smith 1408585bf44eSChristopher Smith if (!$INPUT->server->has('HTTP_REFERER')) { 1409c66972f2SAdrian Lang return ''; 1410c66972f2SAdrian Lang } 1411585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1412f3f0262cSandi 1413079b3ac1SAndreas Gohr // only handle common SEs 1414c7875401SJyoti S if (!array_key_exists('host', $url)) return ''; 1415079b3ac1SAndreas Gohr if (!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/', $url['host'])) return ''; 1416e4d8a516SKazutaka Miyasaka 141724870174SAndreas Gohr $query = []; 1418181adffeSJulian Jeggle if (!array_key_exists('query', $url)) return ''; 1419f3f0262cSandi parse_str($url['query'], $query); 1420e4d8a516SKazutaka Miyasaka 1421c66972f2SAdrian Lang $q = ''; 1422079b3ac1SAndreas Gohr if (isset($query['q'])) { 1423079b3ac1SAndreas Gohr $q = $query['q']; 1424079b3ac1SAndreas Gohr } elseif (isset($query['p'])) { 1425079b3ac1SAndreas Gohr $q = $query['p']; 1426079b3ac1SAndreas Gohr } elseif (isset($query['query'])) { 1427079b3ac1SAndreas Gohr $q = $query['query']; 1428079b3ac1SAndreas Gohr } 1429079b3ac1SAndreas Gohr $q = trim($q); 1430f3f0262cSandi 1431079b3ac1SAndreas Gohr if (!$q) return ''; 1432c7dc833bSPhy // ignore if query includes a full URL 1433c7dc833bSPhy if (strpos($q, '//') !== false) return ''; 14346531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1435f93b3b50SAndreas Gohr return $q; 1436f3f0262cSandi} 1437f3f0262cSandi 1438f3f0262cSandi/** 1439f3f0262cSandi * Return the human readable size of a file 1440f3f0262cSandi * 1441f3f0262cSandi * @param int $size A file size 1442f3f0262cSandi * @param int $dec A number of decimal places 144374160ca1SGerrit Uitslag * @return string human readable size 1444140cfbcdSGerrit Uitslag * 1445f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1446f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1447f3f0262cSandi * @version 1.0.0 1448f3f0262cSandi */ 1449d868eb89SAndreas Gohrfunction filesize_h($size, $dec = 1) 1450d868eb89SAndreas Gohr{ 145124870174SAndreas Gohr $sizes = ['B', 'KB', 'MB', 'GB']; 1452f3f0262cSandi $count = count($sizes); 1453f3f0262cSandi $i = 0; 1454f3f0262cSandi 1455f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 1456f3f0262cSandi $size /= 1024; 1457f3f0262cSandi $i++; 1458f3f0262cSandi } 1459f3f0262cSandi 1460ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1461f3f0262cSandi} 1462f3f0262cSandi 146315fae107Sandi/** 1464c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1465c57e365eSAndreas Gohr * 1466c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1467140cfbcdSGerrit Uitslag * 1468140cfbcdSGerrit Uitslag * @param int $dt timestamp 1469140cfbcdSGerrit Uitslag * @return string 1470c57e365eSAndreas Gohr */ 1471d868eb89SAndreas Gohrfunction datetime_h($dt) 1472d868eb89SAndreas Gohr{ 1473c57e365eSAndreas Gohr global $lang; 1474c57e365eSAndreas Gohr 1475c57e365eSAndreas Gohr $ago = time() - $dt; 1476c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1477c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1478c57e365eSAndreas Gohr } 1479c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 30 * 2) { 1480c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1481c57e365eSAndreas Gohr } 1482c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 7 * 2) { 1483c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1484c57e365eSAndreas Gohr } 1485c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 2) { 1486c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1487c57e365eSAndreas Gohr } 1488c57e365eSAndreas Gohr if ($ago > 60 * 60 * 2) { 1489c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1490c57e365eSAndreas Gohr } 1491c57e365eSAndreas Gohr if ($ago > 60 * 2) { 1492c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1493c57e365eSAndreas Gohr } 1494c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1495c57e365eSAndreas Gohr} 1496c57e365eSAndreas Gohr 1497c57e365eSAndreas Gohr/** 1498f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1499f2263577SAndreas Gohr * 1500f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1501f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1502f2263577SAndreas Gohr * 1503f2263577SAndreas Gohr * @see datetime_h 1504f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1505140cfbcdSGerrit Uitslag * 1506140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1507140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1508140cfbcdSGerrit Uitslag * @return string 1509f2263577SAndreas Gohr */ 1510d868eb89SAndreas Gohrfunction dformat($dt = null, $format = '') 1511d868eb89SAndreas Gohr{ 1512f2263577SAndreas Gohr global $conf; 1513f2263577SAndreas Gohr 1514f2263577SAndreas Gohr if (is_null($dt)) $dt = time(); 1515f2263577SAndreas Gohr $dt = (int) $dt; 1516f2263577SAndreas Gohr if (!$format) $format = $conf['dformat']; 1517f2263577SAndreas Gohr 1518f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1519f2263577SAndreas Gohr return strftime($format, $dt); 1520f2263577SAndreas Gohr} 1521f2263577SAndreas Gohr 1522f2263577SAndreas Gohr/** 1523c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1524c4f79b71SMichael Hamann * 1525c4f79b71SMichael Hamann * @author <ungu at terong dot com> 152659752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1527140cfbcdSGerrit Uitslag * 15287e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 15293272d797SAndreas Gohr * @return string 1530c4f79b71SMichael Hamann */ 1531d868eb89SAndreas Gohrfunction date_iso8601($int_date) 1532d868eb89SAndreas Gohr{ 1533c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1534c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1535c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1536c4f79b71SMichael Hamann $date_mod .= $time_zone; 1537c4f79b71SMichael Hamann return $date_mod; 1538c4f79b71SMichael Hamann} 1539c4f79b71SMichael Hamann 1540c4f79b71SMichael Hamann/** 154100a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 154200a7b5adSEsther Brunner * 154300a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 154400a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1545140cfbcdSGerrit Uitslag * 1546140cfbcdSGerrit Uitslag * @param string $email email address 1547140cfbcdSGerrit Uitslag * @return string 154800a7b5adSEsther Brunner */ 1549d868eb89SAndreas Gohrfunction obfuscate($email) 1550d868eb89SAndreas Gohr{ 155100a7b5adSEsther Brunner global $conf; 155200a7b5adSEsther Brunner 155300a7b5adSEsther Brunner switch ($conf['mailguard']) { 155400a7b5adSEsther Brunner case 'visible' : 155524870174SAndreas Gohr $obfuscate = ['@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ']; 155600a7b5adSEsther Brunner return strtr($email, $obfuscate); 155700a7b5adSEsther Brunner 155800a7b5adSEsther Brunner case 'hex' : 155924870174SAndreas Gohr return Conversion::toHtml($email, true); 156000a7b5adSEsther Brunner 156100a7b5adSEsther Brunner case 'none' : 156200a7b5adSEsther Brunner default : 156300a7b5adSEsther Brunner return $email; 156400a7b5adSEsther Brunner } 156500a7b5adSEsther Brunner} 156600a7b5adSEsther Brunner 156700a7b5adSEsther Brunner/** 156889541d4bSAndreas Gohr * Removes quoting backslashes 156989541d4bSAndreas Gohr * 157089541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1571140cfbcdSGerrit Uitslag * 1572140cfbcdSGerrit Uitslag * @param string $string 1573140cfbcdSGerrit Uitslag * @param string $char backslashed character 1574140cfbcdSGerrit Uitslag * @return string 157589541d4bSAndreas Gohr */ 1576d868eb89SAndreas Gohrfunction unslash($string, $char = "'") 1577d868eb89SAndreas Gohr{ 157889541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 157989541d4bSAndreas Gohr} 158089541d4bSAndreas Gohr 158173038c47SAndreas Gohr/** 158273038c47SAndreas Gohr * Convert php.ini shorthands to byte 158373038c47SAndreas Gohr * 1584a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1585140cfbcdSGerrit Uitslag * 1586a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1587a81f3d99SAndreas Gohr * 1588a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1589a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1590a81f3d99SAndreas Gohr * @return int 159173038c47SAndreas Gohr */ 1592d868eb89SAndreas Gohrfunction php_to_byte($value) 1593d868eb89SAndreas Gohr{ 1594f5c0c80bSAndreas Gohr switch (strtoupper(substr($value, -1))) { 159573038c47SAndreas Gohr case 'G': 159624870174SAndreas Gohr $ret = (int) substr($value, 0, -1) * 1024 * 1024 * 1024; 159773038c47SAndreas Gohr break; 159873038c47SAndreas Gohr case 'M': 159924870174SAndreas Gohr $ret = (int) substr($value, 0, -1) * 1024 * 1024; 1600a81f3d99SAndreas Gohr break; 160173038c47SAndreas Gohr case 'K': 160224870174SAndreas Gohr $ret = (int) substr($value, 0, -1) * 1024; 160373038c47SAndreas Gohr break; 16049eeeb775SAndreas Gohr default: 160524870174SAndreas Gohr $ret = (int) $value; 160649cbd23eSOtto Vainio break; 160773038c47SAndreas Gohr } 160873038c47SAndreas Gohr return $ret; 160973038c47SAndreas Gohr} 161073038c47SAndreas Gohr 1611546d3a99SAndreas Gohr/** 1612546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1613140cfbcdSGerrit Uitslag * 1614140cfbcdSGerrit Uitslag * @param string $string 1615140cfbcdSGerrit Uitslag * @return string 1616546d3a99SAndreas Gohr */ 1617d868eb89SAndreas Gohrfunction preg_quote_cb($string) 1618d868eb89SAndreas Gohr{ 1619546d3a99SAndreas Gohr return preg_quote($string, '/'); 1620546d3a99SAndreas Gohr} 162173038c47SAndreas Gohr 1622bd2f6c2fSAndreas Gohr/** 1623bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1624bd2f6c2fSAndreas Gohr * 1625c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1626bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1627bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1628bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1629bd2f6c2fSAndreas Gohr * 1630bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1631bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1632bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1633bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1634bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 16353272d797SAndreas Gohr * @return string 1636bd2f6c2fSAndreas Gohr */ 1637d868eb89SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') 1638d868eb89SAndreas Gohr{ 163924870174SAndreas Gohr $max -= PhpString::strlen($keep); 1640bd2f6c2fSAndreas Gohr if ($max < $min) return $keep; 164124870174SAndreas Gohr $len = PhpString::strlen($short); 1642bd2f6c2fSAndreas Gohr if ($len <= $max) return $keep.$short; 1643bd2f6c2fSAndreas Gohr $half = floor($max / 2); 16446ce3e5f8SAndreas Gohr return $keep . 164524870174SAndreas Gohr PhpString::substr($short, 0, $half - 1) . 16466ce3e5f8SAndreas Gohr $char . 164724870174SAndreas Gohr PhpString::substr($short, $len - $half); 1648bd2f6c2fSAndreas Gohr} 1649bd2f6c2fSAndreas Gohr 1650dc58b6f4SAndy Webber/** 1651dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1652dc58b6f4SAndy Webber * in page footer and recent changes pages 1653dc58b6f4SAndy Webber * 1654b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 165515f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1656c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 165715f3bc49SGerrit Uitslag * 1658dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1659dc58b6f4SAndy Webber */ 1660d868eb89SAndreas Gohrfunction editorinfo($username, $textonly = false) 1661d868eb89SAndreas Gohr{ 1662cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1663dc58b6f4SAndy Webber} 1664dc58b6f4SAndy Webber 166560a396c8SGerrit Uitslag/** 166660a396c8SGerrit Uitslag * Returns users realname w/o link 166760a396c8SGerrit Uitslag * 1668f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 166915f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1670c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 167160a396c8SGerrit Uitslag * 167260a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 167360a396c8SGerrit Uitslag */ 1674d868eb89SAndreas Gohrfunction userlink($username = null, $textonly = false) 1675d868eb89SAndreas Gohr{ 167660a396c8SGerrit Uitslag global $conf, $INFO; 1677e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 167860a396c8SGerrit Uitslag global $auth; 167930f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 168030f6ec4bSGerrit Uitslag global $INPUT; 168160a396c8SGerrit Uitslag 168260a396c8SGerrit Uitslag // prepare initial event data 168324870174SAndreas Gohr $data = [ 168460a396c8SGerrit Uitslag 'username' => $username, // the unique user name 168560a396c8SGerrit Uitslag 'name' => '', 168624870174SAndreas Gohr 'link' => [ 168724870174SAndreas Gohr //setting 'link' to false disables linking 168860a396c8SGerrit Uitslag 'target' => '', 168960a396c8SGerrit Uitslag 'pre' => '', 169060a396c8SGerrit Uitslag 'suf' => '', 169160a396c8SGerrit Uitslag 'style' => '', 169260a396c8SGerrit Uitslag 'more' => '', 169360a396c8SGerrit Uitslag 'url' => '', 169460a396c8SGerrit Uitslag 'title' => '', 169524870174SAndreas Gohr 'class' => '', 169624870174SAndreas Gohr ], 16974d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 169824870174SAndreas Gohr 'textonly' => $textonly, 169924870174SAndreas Gohr ]; 170062c8004eSGerrit Uitslag if ($username === null) { 170130f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 170215f3bc49SGerrit Uitslag if ($textonly) { 170315f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 170415f3bc49SGerrit Uitslag } else { 170564159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 170664159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 170760a396c8SGerrit Uitslag } 170815f3bc49SGerrit Uitslag } 170960a396c8SGerrit Uitslag 1710e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 171160a396c8SGerrit Uitslag if ($evt->advise_before(true)) { 171260a396c8SGerrit Uitslag if (empty($data['name'])) { 171360a396c8SGerrit Uitslag if ($auth) $info = $auth->getUserData($username); 171465833968SGerrit Uitslag if ($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1715dc58b6f4SAndy Webber switch ($conf['showuseras']) { 1716dc58b6f4SAndy Webber case 'username': 17177f081821SGerrit Uitslag case 'username_link': 171815f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 171960a396c8SGerrit Uitslag break; 1720dc58b6f4SAndy Webber case 'email': 1721dc58b6f4SAndy Webber case 'email_link': 172260a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 172360a396c8SGerrit Uitslag break; 1724dc58b6f4SAndy Webber } 172565833968SGerrit Uitslag } else { 172665833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 172760a396c8SGerrit Uitslag } 172860a396c8SGerrit Uitslag } 17297f081821SGerrit Uitslag 17307f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 17317f081821SGerrit Uitslag static $xhtml_renderer = null; 17327f081821SGerrit Uitslag 173315f3bc49SGerrit Uitslag if (!$data['textonly'] && empty($data['link']['url'])) { 173424870174SAndreas Gohr if (in_array($conf['showuseras'], ['email_link', 'username_link'])) { 173560a396c8SGerrit Uitslag if (!isset($info)) { 173660a396c8SGerrit Uitslag if ($auth) $info = $auth->getUserData($username); 173760a396c8SGerrit Uitslag } 173860a396c8SGerrit Uitslag if (isset($info) && $info) { 17397f081821SGerrit Uitslag if ($conf['showuseras'] == 'email_link') { 174060a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1741dc58b6f4SAndy Webber } else { 17427f081821SGerrit Uitslag if (is_null($xhtml_renderer)) { 17437f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 17447f081821SGerrit Uitslag } 17457f081821SGerrit Uitslag if (empty($xhtml_renderer->interwiki)) { 17467f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 17477f081821SGerrit Uitslag } 17487f081821SGerrit Uitslag $shortcut = 'user'; 1749533772e1SGerrit Uitslag $exists = null; 17506496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 17512a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 17526496c33fSGerrit Uitslag if ($exists !== null) { 17536496c33fSGerrit Uitslag if ($exists) { 17546496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 17556496c33fSGerrit Uitslag } else { 17566496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 17576496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 17586496c33fSGerrit Uitslag } 17596496c33fSGerrit Uitslag } 1760dc58b6f4SAndy Webber } 1761dc58b6f4SAndy Webber } else { 176215f3bc49SGerrit Uitslag $data['textonly'] = true; 1763dc58b6f4SAndy Webber } 176460a396c8SGerrit Uitslag } else { 176515f3bc49SGerrit Uitslag $data['textonly'] = true; 176660a396c8SGerrit Uitslag } 176760a396c8SGerrit Uitslag } 176860a396c8SGerrit Uitslag 176915f3bc49SGerrit Uitslag if ($data['textonly']) { 17704d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 177160a396c8SGerrit Uitslag } else { 177260a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 177360a396c8SGerrit Uitslag if (is_null($xhtml_renderer)) { 177460a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 177560a396c8SGerrit Uitslag } 17764d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 177760a396c8SGerrit Uitslag } 177860a396c8SGerrit Uitslag } 177960a396c8SGerrit Uitslag $evt->advise_after(); 178060a396c8SGerrit Uitslag unset($evt); 178160a396c8SGerrit Uitslag 17824d5fc927SGerrit Uitslag return $data['userlink']; 1783066fee30SAndreas Gohr} 1784066fee30SAndreas Gohr 1785066fee30SAndreas Gohr/** 1786066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1787066fee30SAndreas Gohr * When no image exists, returns an empty string 1788066fee30SAndreas Gohr * 1789066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1790140cfbcdSGerrit Uitslag * 1791066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 17923272d797SAndreas Gohr * @return string 1793066fee30SAndreas Gohr */ 1794d868eb89SAndreas Gohrfunction license_img($type) 1795d868eb89SAndreas Gohr{ 1796066fee30SAndreas Gohr global $license; 1797066fee30SAndreas Gohr global $conf; 1798066fee30SAndreas Gohr if (!$conf['license']) return ''; 1799066fee30SAndreas Gohr if (!is_array($license[$conf['license']])) return ''; 180024870174SAndreas Gohr $try = []; 1801066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1802066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1803066fee30SAndreas Gohr if (substr($conf['license'], 0, 3) == 'cc-') { 1804066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1805066fee30SAndreas Gohr } 1806066fee30SAndreas Gohr foreach ($try as $src) { 180779e79377SAndreas Gohr if (file_exists(DOKU_INC.$src)) return $src; 1808066fee30SAndreas Gohr } 1809066fee30SAndreas Gohr return ''; 1810dc58b6f4SAndy Webber} 1811dc58b6f4SAndy Webber 181213c08e2fSMichael Klier/** 181313c08e2fSMichael Klier * Checks if the given amount of memory is available 181413c08e2fSMichael Klier * 181513c08e2fSMichael Klier * If the memory_get_usage() function is not available the 181613c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 181713c08e2fSMichael Klier * 181813c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 181913c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 18203272d797SAndreas Gohr * 18213272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1822140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 18233272d797SAndreas Gohr * @return bool 182413c08e2fSMichael Klier */ 1825d868eb89SAndreas Gohrfunction is_mem_available($mem, $bytes = 1_048_576) 1826d868eb89SAndreas Gohr{ 182713c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 182813c08e2fSMichael Klier if (empty($limit)) return true; // no limit set! 1829985d6187SElenchus if ($limit == -1) return true; // unlimited 183013c08e2fSMichael Klier 183113c08e2fSMichael Klier // parse limit to bytes 183213c08e2fSMichael Klier $limit = php_to_byte($limit); 183313c08e2fSMichael Klier 183413c08e2fSMichael Klier // get used memory if possible 183513c08e2fSMichael Klier if (function_exists('memory_get_usage')) { 183613c08e2fSMichael Klier $used = memory_get_usage(); 183749eb6e38SAndreas Gohr } else { 183849eb6e38SAndreas Gohr $used = $bytes; 183913c08e2fSMichael Klier } 184013c08e2fSMichael Klier 184113c08e2fSMichael Klier if ($used + $mem > $limit) { 184213c08e2fSMichael Klier return false; 184313c08e2fSMichael Klier } 184413c08e2fSMichael Klier 184513c08e2fSMichael Klier return true; 184613c08e2fSMichael Klier} 184713c08e2fSMichael Klier 1848af2408d5SAndreas Gohr/** 1849af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1850af2408d5SAndreas Gohr * 1851af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1852af2408d5SAndreas Gohr * 1853af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1854af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1855140cfbcdSGerrit Uitslag * 1856140cfbcdSGerrit Uitslag * @param string $url url being directed to 1857af2408d5SAndreas Gohr */ 1858d868eb89SAndreas Gohrfunction send_redirect($url) 1859d868eb89SAndreas Gohr{ 186098ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 186198ca30d2SAndreas Gohr 1862585bf44eSChristopher Smith /* @var Input $INPUT */ 1863585bf44eSChristopher Smith global $INPUT; 1864585bf44eSChristopher Smith 18650181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 18660181f021SAndreas Gohr global $MSG; 18670181f021SAndreas Gohr if (isset($MSG) && count($MSG) && !defined('NOSESSION')) { 18680181f021SAndreas Gohr //reopen session, store data and close session again 18690181f021SAndreas Gohr @session_start(); 18700181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 18710181f021SAndreas Gohr } 18720181f021SAndreas Gohr 1873d4869846SAndreas Gohr // always close the session 1874d4869846SAndreas Gohr session_write_close(); 1875d4869846SAndreas Gohr 1876af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1877*7d34963bSAndreas Gohr if ( 1878*7d34963bSAndreas Gohr $INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1879585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1880585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 18813272d797SAndreas Gohr $matches[1] < 6 18823272d797SAndreas Gohr ) { 1883af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1884af2408d5SAndreas Gohr } else { 1885af2408d5SAndreas Gohr header('Location: '.$url); 1886af2408d5SAndreas Gohr } 188781781cb6SAndreas Gohr 1888572dc222SLarsDW223 // no exits during unit tests 188927c0c399SAndreas Gohr if (defined('DOKU_UNITTEST')) { 189027c0c399SAndreas Gohr // pass info about the redirect back to the test suite 189127c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 189227c0c399SAndreas Gohr if ($testRequest !== null) { 189327c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 189427c0c399SAndreas Gohr } 1895572dc222SLarsDW223 return; 1896572dc222SLarsDW223 } 189727c0c399SAndreas Gohr 1898af2408d5SAndreas Gohr exit; 1899af2408d5SAndreas Gohr} 1900af2408d5SAndreas Gohr 19015b75cd1fSAdrian Lang/** 19025b75cd1fSAdrian Lang * Validate a value using a set of valid values 19035b75cd1fSAdrian Lang * 19045b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 19055b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 19065b75cd1fSAdrian Lang * default is specified, throws an exception. 19075b75cd1fSAdrian Lang * 19085b75cd1fSAdrian Lang * @param string $param The name of the parameter 19095b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 19105b75cd1fSAdrian Lang * be marked by the key “default”. 19115b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 19125b75cd1fSAdrian Lang * or $_GET) 19135b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 19145b75cd1fSAdrian Lang * 19153272d797SAndreas Gohr * @throws Exception 19163272d797SAndreas Gohr * @return mixed 19175b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 19185b75cd1fSAdrian Lang */ 1919d868eb89SAndreas Gohrfunction valid_input_set($param, $valid_values, $array, $exc = '') 1920d868eb89SAndreas Gohr{ 19215b75cd1fSAdrian Lang if (isset($array[$param]) && in_array($array[$param], $valid_values)) { 19225b75cd1fSAdrian Lang return $array[$param]; 19235b75cd1fSAdrian Lang } elseif (isset($valid_values['default'])) { 19245b75cd1fSAdrian Lang return $valid_values['default']; 19255b75cd1fSAdrian Lang } else { 19265b75cd1fSAdrian Lang throw new Exception($exc); 19275b75cd1fSAdrian Lang } 19285b75cd1fSAdrian Lang} 19295b75cd1fSAdrian Lang 193063703ba5SAndreas Gohr/** 193163703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1932646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 1933140cfbcdSGerrit Uitslag * 1934140cfbcdSGerrit Uitslag * @param string $pref preference key 1935b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 1936140cfbcdSGerrit Uitslag * @return string preference value 193763703ba5SAndreas Gohr */ 1938d868eb89SAndreas Gohrfunction get_doku_pref($pref, $default) 1939d868eb89SAndreas Gohr{ 1940646a531aSChristopher Smith $enc_pref = urlencode($pref); 194106c9ee33SMarius van Witzenburg if (isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1942554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 194363703ba5SAndreas Gohr $cnt = count($parts); 19441c3eca7dSPhy 19451c3eca7dSPhy // due to #2721 there might be duplicate entries, 19461c3eca7dSPhy // so we read from the end 19471c3eca7dSPhy for ($i = $cnt-2; $i >= 0; $i -= 2) { 194824870174SAndreas Gohr if ($parts[$i] === $enc_pref) { 1949646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1950554a8c9fSAdrian Lang } 1951554a8c9fSAdrian Lang } 1952554a8c9fSAdrian Lang } 1953554a8c9fSAdrian Lang return $default; 1954554a8c9fSAdrian Lang} 1955554a8c9fSAdrian Lang 19563c94d07bSAnika Henke/** 19573c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 195836ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 19593a970889SAnika Henke * Remove it by setting $val to false 1960140cfbcdSGerrit Uitslag * 1961140cfbcdSGerrit Uitslag * @param string $pref preference key 1962140cfbcdSGerrit Uitslag * @param string $val preference value 19633c94d07bSAnika Henke */ 1964d868eb89SAndreas Gohrfunction set_doku_pref($pref, $val) 1965d868eb89SAndreas Gohr{ 19663c94d07bSAnika Henke global $conf; 19673c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 19683c94d07bSAnika Henke $cookieVal = ''; 19693c94d07bSAnika Henke 19701c3eca7dSPhy if ($orig !== false && ($orig !== $val)) { 19713c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 19723c94d07bSAnika Henke $cnt = count($parts); 197336ec377eSChristopher Smith // urlencode $pref for the comparison 197436ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 19751c3eca7dSPhy $seen = false; 19763c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 197724870174SAndreas Gohr if ($parts[$i] === $enc_pref) { 19781c3eca7dSPhy if (!$seen) { 19793a970889SAnika Henke if ($val !== false) { 1980bf8f8509SAndreas Gohr $parts[$i + 1] = rawurlencode($val ?? ''); 19813a970889SAnika Henke } else { 19823a970889SAnika Henke unset($parts[$i]); 19833a970889SAnika Henke unset($parts[$i + 1]); 19843a970889SAnika Henke } 19851c3eca7dSPhy $seen = true; 19861c3eca7dSPhy } else { 19871c3eca7dSPhy // no break because we want to remove duplicate entries 19881c3eca7dSPhy unset($parts[$i]); 19891c3eca7dSPhy unset($parts[$i + 1]); 19901c3eca7dSPhy } 19913c94d07bSAnika Henke } 19923c94d07bSAnika Henke } 19933c94d07bSAnika Henke $cookieVal = implode('#', $parts); 19941c3eca7dSPhy } elseif ($orig === false && $val !== false) { 1995c10f256aSDamien Regad $cookieVal = (isset($_COOKIE['DOKU_PREFS']) ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 199664159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 19973c94d07bSAnika Henke } 19983c94d07bSAnika Henke 199975e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 20005833995aSPhy if (defined('DOKU_UNITTEST')) { 20015833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 20025833995aSPhy } else { 2003bf8392ebSAndreas Gohr setcookie('DOKU_PREFS', $cookieVal, [ 2004bf8392ebSAndreas Gohr 'expires' => time() + 365 * 24 * 3600, 2005bf8392ebSAndreas Gohr 'path' => $cookieDir, 2006bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 2007bf8392ebSAndreas Gohr 'samesite' => 'Lax' 2008bf8392ebSAndreas Gohr ]); 20093c94d07bSAnika Henke } 20103c94d07bSAnika Henke} 20113c94d07bSAnika Henke 2012f8fb2d18SAndreas Gohr/** 2013f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 2014f8fb2d18SAndreas Gohr * 201542ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 2016f8fb2d18SAndreas Gohr */ 2017d868eb89SAndreas Gohrfunction stripsourcemaps(&$text) 2018d868eb89SAndreas Gohr{ 2019f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 2020f8fb2d18SAndreas Gohr} 2021f8fb2d18SAndreas Gohr 20223c27983bSAndreas Gohr/** 202371de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 20243c27983bSAndreas Gohr * 20253c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 20263c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 20273c27983bSAndreas Gohr * files are embedded. 20283c27983bSAndreas Gohr * 202971de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 203071de5572SAndreas Gohr * 20313c27983bSAndreas Gohr * @param string $file full path to the SVG file 20323c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 203371de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 20343c27983bSAndreas Gohr */ 2035d868eb89SAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) 2036d868eb89SAndreas Gohr{ 20373c27983bSAndreas Gohr $file = trim($file); 20383c27983bSAndreas Gohr if ($file === '') return false; 20393c27983bSAndreas Gohr if (!file_exists($file)) return false; 20403c27983bSAndreas Gohr if (filesize($file) > $maxsize) return false; 20413c27983bSAndreas Gohr if (!is_readable($file)) return false; 20423c27983bSAndreas Gohr $content = file_get_contents($file); 20430849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s', '', $content); // comments 20440849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 20450849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 20460849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 20473c27983bSAndreas Gohr $content = trim($content); 20483c27983bSAndreas Gohr if (substr($content, 0, 5) !== '<svg ') return false; 204971de5572SAndreas Gohr return $content; 20503c27983bSAndreas Gohr} 20513c27983bSAndreas Gohr 2052e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 2053