1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Common DokuWiki functions 415fae107Sandi * 515fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 715fae107Sandi */ 815fae107Sandi 90db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 100db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 110c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 12b24e9c4aSSatoshi Saharause dokuwiki\File\PageFile; 1366f4cdd4SSatoshi Saharause dokuwiki\Logger; 14704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 1575d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 16e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 17e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 180c3a5702SAndreas Gohr 19f3f0262cSandi/** 20d5197206Schris * Wrapper around htmlspecialchars() 21d5197206Schris * 22d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 23d5197206Schris * @see htmlspecialchars() 24140cfbcdSGerrit Uitslag * 25140cfbcdSGerrit Uitslag * @param string $string the string being converted 26140cfbcdSGerrit Uitslag * @return string converted string 27d5197206Schris */ 28d5197206Schrisfunction hsc($string) { 29f7711f2bSAndreas Gohr return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); 30d5197206Schris} 31d5197206Schris 32d5197206Schris/** 3312dd3cbcSAndreas Gohr * A safer explode for fixed length lists 3412dd3cbcSAndreas Gohr * 3512dd3cbcSAndreas Gohr * This works just like explode(), but will always return the wanted number of elements. 3612dd3cbcSAndreas Gohr * If the $input string does not contain enough elements, the missing elements will be 3712dd3cbcSAndreas Gohr * filled up with the $default value. If the input string contains more elements, the last 3812dd3cbcSAndreas Gohr * one will NOT be split up and will still contain $separator 3912dd3cbcSAndreas Gohr * 4012dd3cbcSAndreas Gohr * @param string $separator The boundary string 4112dd3cbcSAndreas Gohr * @param string $string The input string 4212dd3cbcSAndreas Gohr * @param int $limit The number of expected elements 4312dd3cbcSAndreas Gohr * @param mixed $default The value to use when filling up missing elements 4412dd3cbcSAndreas Gohr * @see explode 4512dd3cbcSAndreas Gohr * @return array 4612dd3cbcSAndreas Gohr */ 4712dd3cbcSAndreas Gohrfunction sexplode($separator, $string, $limit, $default = null) 4812dd3cbcSAndreas Gohr{ 4912dd3cbcSAndreas Gohr return array_pad(explode($separator, $string, $limit), $limit, $default); 5012dd3cbcSAndreas Gohr} 5112dd3cbcSAndreas Gohr 5212dd3cbcSAndreas Gohr/** 535b571377SAndreas Gohr * Checks if the given input is blank 545b571377SAndreas Gohr * 555b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 565b571377SAndreas Gohr * 5767234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 5867234204SAndreas Gohr * with a NULL value without warning. 5967234204SAndreas Gohr * 6067234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 6167234204SAndreas Gohr * 6267234204SAndreas Gohr * (isset($foo) && !blank($foo)) 6367234204SAndreas Gohr * (!isset($foo) || blank($foo)) 6467234204SAndreas Gohr * 655b571377SAndreas Gohr * @param $in 665b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 675b571377SAndreas Gohr * @return bool 685b571377SAndreas Gohr */ 695b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 705b571377SAndreas Gohr if(is_null($in)) return true; 715b571377SAndreas Gohr if(is_array($in)) return empty($in); 725b571377SAndreas Gohr if($in === "\0") return true; 735b571377SAndreas Gohr if($trim && trim($in) === '') return true; 745b571377SAndreas Gohr if(strlen($in) > 0) return false; 755b571377SAndreas Gohr return empty($in); 765b571377SAndreas Gohr} 775b571377SAndreas Gohr 785b571377SAndreas Gohr/** 79d5197206Schris * print a newline terminated string 80d5197206Schris * 81d5197206Schris * You can give an indention as optional parameter 82d5197206Schris * 83d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 84140cfbcdSGerrit Uitslag * 85140cfbcdSGerrit Uitslag * @param string $string line of text 86140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 87d5197206Schris */ 8825ec097bSChris Smithfunction ptln($string, $indent = 0) { 8925ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 9002b0b681SAndreas Gohr} 9102b0b681SAndreas Gohr 9202b0b681SAndreas Gohr/** 9302b0b681SAndreas Gohr * strips control characters (<32) from the given string 9402b0b681SAndreas Gohr * 9502b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 96140cfbcdSGerrit Uitslag * 9742ea7f44SGerrit Uitslag * @param string $string being stripped 98140cfbcdSGerrit Uitslag * @return string 9902b0b681SAndreas Gohr */ 10002b0b681SAndreas Gohrfunction stripctl($string) { 10102b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 102d5197206Schris} 103d5197206Schris 104d5197206Schris/** 105634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 106634d7150SAndreas Gohr * 107634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 108634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 109634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 11042ea7f44SGerrit Uitslag * 111634d7150SAndreas Gohr * @return string 112634d7150SAndreas Gohr */ 113634d7150SAndreas Gohrfunction getSecurityToken() { 114585bf44eSChristopher Smith /** @var Input $INPUT */ 115585bf44eSChristopher Smith global $INPUT; 1163680e2cdSAndreas Gohr 1173680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 1183680e2cdSAndreas Gohr $session = session_id(); 1193680e2cdSAndreas Gohr 1203680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1213680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 122c3cc6e05SAndreas Gohr return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 123634d7150SAndreas Gohr} 124634d7150SAndreas Gohr 125634d7150SAndreas Gohr/** 126634d7150SAndreas Gohr * Check the secret CSRF token 127140cfbcdSGerrit Uitslag * 128140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 129140cfbcdSGerrit Uitslag * @return bool success if the token matched 130634d7150SAndreas Gohr */ 131634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 132585bf44eSChristopher Smith /** @var Input $INPUT */ 1337d01a0eaSTom N Harris global $INPUT; 134585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 135df97eaacSAndreas Gohr 1367d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 137634d7150SAndreas Gohr if(getSecurityToken() != $token) { 138634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 139634d7150SAndreas Gohr return false; 140634d7150SAndreas Gohr } 141634d7150SAndreas Gohr return true; 142634d7150SAndreas Gohr} 143634d7150SAndreas Gohr 144634d7150SAndreas Gohr/** 145634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 146634d7150SAndreas Gohr * 147634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 148140cfbcdSGerrit Uitslag * 149140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 15042ea7f44SGerrit Uitslag * @return string html of hidden form field 151634d7150SAndreas Gohr */ 152634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1532404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1543272d797SAndreas Gohr if($print) echo $ret; 155634d7150SAndreas Gohr return $ret; 156634d7150SAndreas Gohr} 157634d7150SAndreas Gohr 158634d7150SAndreas Gohr/** 1591015a57dSChristopher Smith * Determine basic information for a request of $id 16015fae107Sandi * 16115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1627e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 163140cfbcdSGerrit Uitslag * 164140cfbcdSGerrit Uitslag * @param string $id pageid 165140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 166140cfbcdSGerrit Uitslag * @return array with info for a request of $id 167140cfbcdSGerrit Uitslag * 168f3f0262cSandi */ 1691015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 170f3f0262cSandi global $USERINFO; 171585bf44eSChristopher Smith /* @var Input $INPUT */ 172585bf44eSChristopher Smith global $INPUT; 1736afe8dcaSchris 174c66972f2SAdrian Lang // set info about manager/admin status. 17559bc3b48SGerrit Uitslag $info = array(); 176c66972f2SAdrian Lang $info['isadmin'] = false; 177c66972f2SAdrian Lang $info['ismanager'] = false; 178585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 179f3f0262cSandi $info['userinfo'] = $USERINFO; 1801015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 181585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 18217ee7f66SAndreas Gohr 183f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 184f8cc712eSAndreas Gohr $info['isadmin'] = true; 185f8cc712eSAndreas Gohr $info['ismanager'] = true; 186f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 187f8cc712eSAndreas Gohr $info['ismanager'] = true; 188f8cc712eSAndreas Gohr } 189f8cc712eSAndreas Gohr 19017ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 191a58fcbbcSAndreas Gohr if(empty($info['userinfo']['name'])) { 192585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 19317ee7f66SAndreas Gohr } 194ee4c4a1bSAndreas Gohr 195f3f0262cSandi } else { 1961015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 197ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 198f3f0262cSandi } 199f3f0262cSandi 2001015a57dSChristopher Smith $info['namespace'] = getNS($id); 2011015a57dSChristopher Smith 2021015a57dSChristopher Smith // mobile detection 2031015a57dSChristopher Smith if ($htmlClient) { 2041015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 2051015a57dSChristopher Smith } 2061015a57dSChristopher Smith 2071015a57dSChristopher Smith return $info; 2081015a57dSChristopher Smith } 2091015a57dSChristopher Smith 2101015a57dSChristopher Smith/** 2111015a57dSChristopher Smith * Return info about the current document as associative 2121015a57dSChristopher Smith * array. 2131015a57dSChristopher Smith * 2141015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 215140cfbcdSGerrit Uitslag * 216140cfbcdSGerrit Uitslag * @return array with info about current document 2171015a57dSChristopher Smith */ 2181015a57dSChristopher Smithfunction pageinfo() { 2191015a57dSChristopher Smith global $ID; 2201015a57dSChristopher Smith global $REV; 2211015a57dSChristopher Smith global $RANGE; 2221015a57dSChristopher Smith global $lang; 223585bf44eSChristopher Smith /* @var Input $INPUT */ 224585bf44eSChristopher Smith global $INPUT; 2251015a57dSChristopher Smith 2261015a57dSChristopher Smith $info = basicinfo($ID); 2271015a57dSChristopher Smith 2281015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2291015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2301015a57dSChristopher Smith $info['id'] = $ID; 2311015a57dSChristopher Smith $info['rev'] = $REV; 2321015a57dSChristopher Smith 23375d66495SMichael Große $subManager = new SubscriberManager(); 23475d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2357e87a794SChristopher Smith 236f3f0262cSandi $info['locked'] = checklock($ID); 237317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 23879e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 23901c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2405ec96136SSatoshi Sahara 2412ca9d91cSBen Coburn if ($REV) { 2422ca9d91cSBen Coburn //check if current revision was meant 24301c9a118SAndreas Gohr if ($info['exists'] && ($info['currentrev'] == $REV)) { 2442ca9d91cSBen Coburn $REV = ''; 2457b3a6803SAndreas Gohr } elseif ($RANGE) { 2467b3a6803SAndreas Gohr //section editing does not work with old revisions! 2477b3a6803SAndreas Gohr $REV = ''; 2487b3a6803SAndreas Gohr $RANGE = ''; 2497b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2502ca9d91cSBen Coburn } else { 2512ca9d91cSBen Coburn //really use old revision 252317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 25379e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 254f3f0262cSandi } 255f3f0262cSandi } 256c112d578Sandi $info['rev'] = $REV; 257f3f0262cSandi if ($info['exists']) { 258252acce3SSatoshi Sahara $info['writable'] = (is_writable($info['filepath']) && $info['perm'] >= AUTH_EDIT); 259f3f0262cSandi } else { 260f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 261f3f0262cSandi } 26250e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 263f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 264f3f0262cSandi 26571726d78SBen Coburn //load page meta data 26671726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 26771726d78SBen Coburn 268652610a2Sandi //who's the editor 269047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 270652610a2Sandi if ($REV) { 271f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 272652610a2Sandi } else { 2730e80bb5eSChristopher Smith if (!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 274aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 275aa27cf05SAndreas Gohr } else { 276f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 277cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 278cd00a034SBen Coburn if ($revinfo !== false) { 279cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 280cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 281cd00a034SBen Coburn } 282cd00a034SBen Coburn } 283cd00a034SBen Coburn } 284cd00a034SBen Coburn //and check for an external edit 285cd00a034SBen Coburn if ($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 286cd00a034SBen Coburn // cached changelog line no longer valid 287cd00a034SBen Coburn $revinfo = false; 288cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 289cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 290652610a2Sandi } 291bb4866bdSchris 2920a444b5aSPhy if ($revinfo !== false) { 293652610a2Sandi $info['ip'] = $revinfo['ip']; 294652610a2Sandi $info['user'] = $revinfo['user']; 295652610a2Sandi $info['sum'] = $revinfo['sum']; 29671726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 297ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 29859f257aeSchris 299252acce3SSatoshi Sahara $info['editor'] = $revinfo['user'] ?: $revinfo['ip']; 3000a444b5aSPhy } else { 3010a444b5aSPhy $info['ip'] = null; 3020a444b5aSPhy $info['user'] = null; 3030a444b5aSPhy $info['sum'] = null; 3040a444b5aSPhy $info['editor'] = null; 3050a444b5aSPhy } 306652610a2Sandi 307ee4c4a1bSAndreas Gohr // draft 3080aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 3090aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 3100aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 311ee4c4a1bSAndreas Gohr } 312ee4c4a1bSAndreas Gohr 3131015a57dSChristopher Smith return $info; 3141015a57dSChristopher Smith} 3151015a57dSChristopher Smith 3161015a57dSChristopher Smith/** 3170c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3180c39d46cSMichael Große */ 3190c39d46cSMichael Großefunction jsinfo() { 3200c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3210c39d46cSMichael Große 3220c39d46cSMichael Große if (!is_array($JSINFO)) { 3230c39d46cSMichael Große $JSINFO = []; 3240c39d46cSMichael Große } 3250c39d46cSMichael Große //export minimal info to JS, plugins can add more 3260c39d46cSMichael Große $JSINFO['id'] = $ID; 32768491db9SPhy $JSINFO['namespace'] = isset($INFO) ? (string) $INFO['namespace'] : ''; 3280c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3290c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3300c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3310c39d46cSMichael Große} 3320c39d46cSMichael Große 3330c39d46cSMichael Große/** 3341015a57dSChristopher Smith * Return information about the current media item as an associative array. 335140cfbcdSGerrit Uitslag * 336140cfbcdSGerrit Uitslag * @return array with info about current media item 3371015a57dSChristopher Smith */ 3381015a57dSChristopher Smithfunction mediainfo() { 3391015a57dSChristopher Smith global $NS; 3401015a57dSChristopher Smith global $IMG; 3411015a57dSChristopher Smith 3421015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3431015a57dSChristopher Smith $info['image'] = $IMG; 3441c548ebeSAndreas Gohr 345f3f0262cSandi return $info; 346f3f0262cSandi} 347f3f0262cSandi 348f3f0262cSandi/** 3492684e50aSAndreas Gohr * Build an string of URL parameters 3502684e50aSAndreas Gohr * 3512684e50aSAndreas Gohr * @author Andreas Gohr 352140cfbcdSGerrit Uitslag * 353140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 354140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 355140cfbcdSGerrit Uitslag * @return string query string 3562684e50aSAndreas Gohr */ 357b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3582684e50aSAndreas Gohr $url = ''; 3592684e50aSAndreas Gohr $amp = false; 3602684e50aSAndreas Gohr foreach($params as $key => $val) { 361b174aeaeSchris if($amp) $url .= $sep; 3622684e50aSAndreas Gohr 36385e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3643a50618cSgweissbach $url .= rawurlencode((string) $val); 3652684e50aSAndreas Gohr $amp = true; 3662684e50aSAndreas Gohr } 3672684e50aSAndreas Gohr return $url; 3682684e50aSAndreas Gohr} 3692684e50aSAndreas Gohr 3702684e50aSAndreas Gohr/** 3712684e50aSAndreas Gohr * Build an string of html tag attributes 3722684e50aSAndreas Gohr * 3737bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3747bff22c0SAndreas Gohr * 3752684e50aSAndreas Gohr * @author Andreas Gohr 376140cfbcdSGerrit Uitslag * 377140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 378246d3337SMichael Große * @param bool $skipEmptyStrings skip empty string values? 379140cfbcdSGerrit Uitslag * @return string 3802684e50aSAndreas Gohr */ 381246d3337SMichael Großefunction buildAttributes($params, $skipEmptyStrings = false) { 3822684e50aSAndreas Gohr $url = ''; 3839063ec14SAdrian Lang $white = false; 3842684e50aSAndreas Gohr foreach($params as $key => $val) { 3852401f18dSSyntaxseed if($key[0] == '_') continue; 386246d3337SMichael Große if($val === '' && $skipEmptyStrings) continue; 3879063ec14SAdrian Lang if($white) $url .= ' '; 3887bff22c0SAndreas Gohr 3892684e50aSAndreas Gohr $url .= $key.'="'; 390f7711f2bSAndreas Gohr $url .= hsc($val); 3912684e50aSAndreas Gohr $url .= '"'; 3929063ec14SAdrian Lang $white = true; 3932684e50aSAndreas Gohr } 3942684e50aSAndreas Gohr return $url; 3952684e50aSAndreas Gohr} 3962684e50aSAndreas Gohr 3972684e50aSAndreas Gohr/** 39815fae107Sandi * This builds the breadcrumb trail and returns it as array 39915fae107Sandi * 40015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 401140cfbcdSGerrit Uitslag * 402e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 403f3f0262cSandi */ 404f3f0262cSandifunction breadcrumbs() { 4058746e727Sandi // we prepare the breadcrumbs early for quick session closing 4068746e727Sandi static $crumbs = null; 4078746e727Sandi if($crumbs != null) return $crumbs; 4088746e727Sandi 409f3f0262cSandi global $ID; 410f3f0262cSandi global $ACT; 411f3f0262cSandi global $conf; 4120ea5ebb4SB_S666 global $INFO; 413f3f0262cSandi 414f3f0262cSandi //first visit? 415c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 4165603d3c1SHenry Pan //we only save on show and existing visible readable wiki documents 417a77f5846Sjan $file = wikiFN($ID); 4185603d3c1SHenry Pan if($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) { 419e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 420f3f0262cSandi return $crumbs; 421f3f0262cSandi } 422a77f5846Sjan 423a77f5846Sjan // page names 4241a84a0f3SAnika Henke $name = noNSorNS($ID); 425fe9ec250SChris Smith if(useHeading('navigation')) { 426a77f5846Sjan // get page title 42767c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 428a77f5846Sjan if($title) { 429a77f5846Sjan $name = $title; 430a77f5846Sjan } 431a77f5846Sjan } 432a77f5846Sjan 433f3f0262cSandi //remove ID from array 434a77f5846Sjan if(isset($crumbs[$ID])) { 435a77f5846Sjan unset($crumbs[$ID]); 436f3f0262cSandi } 437f3f0262cSandi 438f3f0262cSandi //add to array 439a77f5846Sjan $crumbs[$ID] = $name; 440f3f0262cSandi //reduce size 441f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 442f3f0262cSandi array_shift($crumbs); 443f3f0262cSandi } 444f3f0262cSandi //save to session 445e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 446f3f0262cSandi return $crumbs; 447f3f0262cSandi} 448f3f0262cSandi 449f3f0262cSandi/** 45015fae107Sandi * Filter for page IDs 45115fae107Sandi * 452f3f0262cSandi * This is run on a ID before it is outputted somewhere 453f3f0262cSandi * currently used to replace the colon with something else 454907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 455907f24f7SAndreas Gohr * 456977aa967SAndreas Gohr * See discussions at https://github.com/dokuwiki/dokuwiki/pull/84 and 457977aa967SAndreas Gohr * https://github.com/dokuwiki/dokuwiki/pull/173 why we use a whitelist of 458907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 45915fae107Sandi * 46049c713a3Sandi * Urlencoding is ommitted when the second parameter is false 46149c713a3Sandi * 46215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 463140cfbcdSGerrit Uitslag * 464140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 465140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 466140cfbcdSGerrit Uitslag * @return string 467f3f0262cSandi */ 46849c713a3Sandifunction idfilter($id, $ue = true) { 469f3f0262cSandi global $conf; 470585bf44eSChristopher Smith /* @var Input $INPUT */ 471585bf44eSChristopher Smith global $INPUT; 472585bf44eSChristopher Smith 473bf8f8509SAndreas Gohr $id = (string) $id; 474bf8f8509SAndreas Gohr 475f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 476f3f0262cSandi $id = strtr($id, ':', '/'); 477f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 47858bedc8aSborekb $conf['userewrite'] && 479585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4803272d797SAndreas Gohr ) { 481f3f0262cSandi $id = strtr($id, ':', ';'); 482f3f0262cSandi } 48349c713a3Sandi if($ue) { 484b6c6979fSAndreas Gohr $id = rawurlencode($id); 485f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 486edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 487f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 48849c713a3Sandi } 489f3f0262cSandi return $id; 490f3f0262cSandi} 491f3f0262cSandi 492f3f0262cSandi/** 493ed7b5f09Sandi * This builds a link to a wikipage 49415fae107Sandi * 4954bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4966c7843b5Sandi * 49715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4984bc480e5SAndreas Gohr * 4994bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5004bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5014bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 5024bc480e5SAndreas Gohr * @param string $separator parameter separator 5034bc480e5SAndreas Gohr * @return string 504f3f0262cSandi */ 50516f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 506f3f0262cSandi global $conf; 50716f15a81SDominik Eckelmann if(is_array($urlParameters)) { 5084bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 50964159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 51064159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 51164159a61SAndreas Gohr } 51216f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 5136de3759aSAndreas Gohr } else { 51416f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 5156de3759aSAndreas Gohr } 51616f15a81SDominik Eckelmann if($id === '') { 51716f15a81SDominik Eckelmann $id = $conf['start']; 51816f15a81SDominik Eckelmann } 519f3f0262cSandi $id = idfilter($id); 52016f15a81SDominik Eckelmann if($absolute) { 521ed7b5f09Sandi $xlink = DOKU_URL; 522ed7b5f09Sandi } else { 523ed7b5f09Sandi $xlink = DOKU_BASE; 524ed7b5f09Sandi } 525f3f0262cSandi 5266c7843b5Sandi if($conf['userewrite'] == 2) { 5276c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 52816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5296c7843b5Sandi } elseif($conf['userewrite']) { 530f3f0262cSandi $xlink .= $id; 53116f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 53240b5fb5bSPhy } elseif($id !== '') { 5336c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 53416f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 535bce3726dSAndreas Gohr } else { 536bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 53716f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 538f3f0262cSandi } 539f3f0262cSandi 540f3f0262cSandi return $xlink; 541f3f0262cSandi} 542f3f0262cSandi 543f3f0262cSandi/** 544f5c2808fSBen Coburn * This builds a link to an alternate page format 545f5c2808fSBen Coburn * 546f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 547f5c2808fSBen Coburn * 548f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5494bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5504bc480e5SAndreas Gohr * @param string $format the export renderer to use 5514bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5524bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5534bc480e5SAndreas Gohr * @param string $sep parameter separator 5544bc480e5SAndreas Gohr * @return string 555f5c2808fSBen Coburn */ 5564bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 557f5c2808fSBen Coburn global $conf; 5584bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5594bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 560f5c2808fSBen Coburn } else { 5614bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 562f5c2808fSBen Coburn } 563f5c2808fSBen Coburn 564f5c2808fSBen Coburn $format = rawurlencode($format); 565f5c2808fSBen Coburn $id = idfilter($id); 566f5c2808fSBen Coburn if($abs) { 567f5c2808fSBen Coburn $xlink = DOKU_URL; 568f5c2808fSBen Coburn } else { 569f5c2808fSBen Coburn $xlink = DOKU_BASE; 570f5c2808fSBen Coburn } 571f5c2808fSBen Coburn 572f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 573f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5744bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 575f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 576f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5774bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 578f5c2808fSBen Coburn } else { 579f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5804bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 581f5c2808fSBen Coburn } 582f5c2808fSBen Coburn 583f5c2808fSBen Coburn return $xlink; 584f5c2808fSBen Coburn} 585f5c2808fSBen Coburn 586f5c2808fSBen Coburn/** 5876de3759aSAndreas Gohr * Build a link to a media file 5886de3759aSAndreas Gohr * 5896de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5908c08db0aSAndreas Gohr * 5918c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5928c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5938c08db0aSAndreas Gohr * 5943272d797SAndreas Gohr * @param string $id the media file id or URL 5953272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5963272d797SAndreas Gohr * @param bool $direct link to detail page if false 5973272d797SAndreas Gohr * @param string $sep URL parameter separator 5983272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5993272d797SAndreas Gohr * @return string 6006de3759aSAndreas Gohr */ 60155b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 6026de3759aSAndreas Gohr global $conf; 603b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 604826d2766SKlap-in if(!$isexternalimage) { 605826d2766SKlap-in $id = cleanID($id); 606826d2766SKlap-in } 607826d2766SKlap-in 6086de3759aSAndreas Gohr if(is_array($more)) { 6090f4e0092SChristopher Smith // add token for resized images 610357c9a39SDamien Regad $w = isset($more['w']) ? $more['w'] : null; 611357c9a39SDamien Regad $h = isset($more['h']) ? $more['h'] : null; 61298fe1ac9SDamien Regad if($w || $h || $isexternalimage){ 613357c9a39SDamien Regad $more['tok'] = media_get_token($id, $w, $h); 6140f4e0092SChristopher Smith } 6158c08db0aSAndreas Gohr // strip defaults for shorter URLs 6168c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 617443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 618443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 6198c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 62078b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 621b174aeaeSchris $more = buildURLparams($more, $sep); 6226de3759aSAndreas Gohr } else { 6235e7db1e2SChristopher Smith $matches = array(); 624cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6255e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6265e7db1e2SChristopher Smith foreach ($matches as $match){ 6275e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6285e7db1e2SChristopher Smith } 629cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 630cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6315e7db1e2SChristopher Smith } 6328c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6338c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 634b174aeaeSchris $more = str_replace(',', $sep, $more); 6356de3759aSAndreas Gohr } 6366de3759aSAndreas Gohr 63755b2b31bSAndreas Gohr if($abs) { 63855b2b31bSAndreas Gohr $xlink = DOKU_URL; 63955b2b31bSAndreas Gohr } else { 6406de3759aSAndreas Gohr $xlink = DOKU_BASE; 64155b2b31bSAndreas Gohr } 6426de3759aSAndreas Gohr 6436de3759aSAndreas Gohr // external URLs are always direct without rewriting 644826d2766SKlap-in if($isexternalimage) { 6456de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 646cc036f74SKlap-in $xlink .= '?'.$more; 647b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6486de3759aSAndreas Gohr return $xlink; 6496de3759aSAndreas Gohr } 6506de3759aSAndreas Gohr 6516de3759aSAndreas Gohr $id = idfilter($id); 6526de3759aSAndreas Gohr 6536de3759aSAndreas Gohr // decide on scriptname 6546de3759aSAndreas Gohr if($direct) { 6556de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6566de3759aSAndreas Gohr $script = '_media'; 6576de3759aSAndreas Gohr } else { 6586de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6596de3759aSAndreas Gohr } 6606de3759aSAndreas Gohr } else { 6616de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6626de3759aSAndreas Gohr $script = '_detail'; 6636de3759aSAndreas Gohr } else { 6646de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6656de3759aSAndreas Gohr } 6666de3759aSAndreas Gohr } 6676de3759aSAndreas Gohr 6686de3759aSAndreas Gohr // build URL based on rewrite mode 6696de3759aSAndreas Gohr if($conf['userewrite']) { 6706de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6716de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6726de3759aSAndreas Gohr } else { 6736de3759aSAndreas Gohr if($more) { 674a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 675b174aeaeSchris $xlink .= $sep.'media='.$id; 6766de3759aSAndreas Gohr } else { 677a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6786de3759aSAndreas Gohr } 6796de3759aSAndreas Gohr } 6806de3759aSAndreas Gohr 6816de3759aSAndreas Gohr return $xlink; 6826de3759aSAndreas Gohr} 6836de3759aSAndreas Gohr 6846de3759aSAndreas Gohr/** 68525ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 68615fae107Sandi * 68725ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 68825ca5b17SAndreas Gohr * 68915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 690140cfbcdSGerrit Uitslag * 691140cfbcdSGerrit Uitslag * @return string 692f3f0262cSandi */ 69325ca5b17SAndreas Gohrfunction script() { 694ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 695f3f0262cSandi} 696f3f0262cSandi 697f3f0262cSandi/** 69815fae107Sandi * Spamcheck against wordlist 69915fae107Sandi * 700f3f0262cSandi * Checks the wikitext against a list of blocked expressions 701f3f0262cSandi * returns true if the text contains any bad words 70215fae107Sandi * 703e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 704e403cc58SMichael Klier * 705e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 706e403cc58SMichael Klier * and gain information about the user who was blocked. 707e403cc58SMichael Klier * 708e403cc58SMichael Klier * Event data: 709e403cc58SMichael Klier * data['matches'] - array of matches 710e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 711e403cc58SMichael Klier * [ip] - ip address 712e403cc58SMichael Klier * [user] - username (if logged in) 713e403cc58SMichael Klier * [mail] - mail address (if logged in) 714e403cc58SMichael Klier * [name] - real name (if logged in) 715e403cc58SMichael Klier * 71615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 7176dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 718140cfbcdSGerrit Uitslag * 7196dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 7206dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 721f3f0262cSandi */ 7226dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 723f3f0262cSandi global $TEXT; 7246dffa0e0SAndreas Gohr global $PRE; 7256dffa0e0SAndreas Gohr global $SUF; 726e0086ca2SAndreas Gohr global $SUM; 727f3f0262cSandi global $conf; 728e403cc58SMichael Klier global $INFO; 729585bf44eSChristopher Smith /* @var Input $INPUT */ 730585bf44eSChristopher Smith global $INPUT; 731f3f0262cSandi 732f3f0262cSandi if(!$conf['usewordblock']) return false; 733f3f0262cSandi 734e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7356dffa0e0SAndreas Gohr 736041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 73764159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 73864159a61SAndreas Gohr $text = preg_replace( 73964159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 74064159a61SAndreas Gohr '\1http://\2 \2\3', 74164159a61SAndreas Gohr $text 74264159a61SAndreas Gohr ); 74364159a61SAndreas Gohr // phpcs:enable 744041d1964SAndreas Gohr 745b9ac8716Schris $wordblocks = getWordblocks(); 746a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7473e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 748a51d08efSAndreas Gohr $chunksize = 200; 749*64259528SAndreas Gohr 750b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 751f3f0262cSandi $re = array(); 75249eb6e38SAndreas Gohr // build regexp from blocks 753f3f0262cSandi foreach($blocks as $block) { 754f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 755f3f0262cSandi $block = trim($block); 756f3f0262cSandi if(empty($block)) continue; 757f3f0262cSandi $re[] = $block; 758f3f0262cSandi } 759e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 760e403cc58SMichael Klier // prepare event data 76159bc3b48SGerrit Uitslag $data = array(); 762e403cc58SMichael Klier $data['matches'] = $matches; 763585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 764585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 765585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 766e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 767e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 768e403cc58SMichael Klier } 769bad6fc0dSAndreas Gohr $callback = function () { 770bad6fc0dSAndreas Gohr return true; 771bad6fc0dSAndreas Gohr }; 772cbb44eabSAndreas Gohr return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 773b9ac8716Schris } 774703f6fdeSandi } 775f3f0262cSandi return false; 776f3f0262cSandi} 777f3f0262cSandi 778f3f0262cSandi/** 77915fae107Sandi * Return the IP of the client 78015fae107Sandi * 7816d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 78215fae107Sandi * 7836d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 7846d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 7856d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 7866d8affe6SAndreas Gohr * headers 7876d8affe6SAndreas Gohr * 78815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 789140cfbcdSGerrit Uitslag * 7903272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 7913272d797SAndreas Gohr * @return string 792f3f0262cSandi */ 7936d8affe6SAndreas Gohrfunction clientIP($single = false) { 794585bf44eSChristopher Smith /* @var Input $INPUT */ 795925105e8SPhy global $INPUT, $conf; 796585bf44eSChristopher Smith 7976d8affe6SAndreas Gohr $ip = array(); 798585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 799585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 800585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 801585bf44eSChristopher Smith } 802585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 803585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 804585bf44eSChristopher Smith } 8056d8affe6SAndreas Gohr 8066d8affe6SAndreas Gohr // remove any non-IP stuff 8076d8affe6SAndreas Gohr $cnt = count($ip); 8086d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 8090a5f08e5SAdaKaleh if(filter_var($ip[$i], FILTER_VALIDATE_IP) === false) { 8100a5f08e5SAdaKaleh unset($ip[$i]); 8114ff28443Schris } 812f3f0262cSandi } 8136d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 814056bf31fSDamien Regad if(empty($ip) || !$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8156d8affe6SAndreas Gohr 8166d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 8176d8affe6SAndreas Gohr 818925105e8SPhy // skip trusted local addresses 8196d8affe6SAndreas Gohr foreach($ip as $i) { 820925105e8SPhy if(!empty($conf['trustedproxy']) && preg_match('/'.$conf['trustedproxy'].'/', $i)) { 8216d8affe6SAndreas Gohr continue; 8226d8affe6SAndreas Gohr } else { 8236d8affe6SAndreas Gohr return $i; 8246d8affe6SAndreas Gohr } 8256d8affe6SAndreas Gohr } 826925105e8SPhy 827925105e8SPhy // still here? just use the last address 828925105e8SPhy // this case all ips in the list are trusted 829925105e8SPhy return $ip[count($ip)-1]; 830f3f0262cSandi} 831f3f0262cSandi 832f3f0262cSandi/** 8331c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8341c548ebeSAndreas Gohr * 8351c548ebeSAndreas Gohr * Adapted from the example code at url below 8361c548ebeSAndreas Gohr * 8371c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 838140cfbcdSGerrit Uitslag * 83964159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 840140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8411c548ebeSAndreas Gohr */ 8421c548ebeSAndreas Gohrfunction clientismobile() { 843585bf44eSChristopher Smith /* @var Input $INPUT */ 844585bf44eSChristopher Smith global $INPUT; 8451c548ebeSAndreas Gohr 846585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8471c548ebeSAndreas Gohr 848585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8491c548ebeSAndreas Gohr 850585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8511c548ebeSAndreas Gohr 85264159a61SAndreas Gohr $uamatches = join( 85364159a61SAndreas Gohr '|', 85464159a61SAndreas Gohr [ 85564159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 85664159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 85764159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 85864159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 85964159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 86064159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 86164159a61SAndreas Gohr '\d\d\di', 'moto' 86264159a61SAndreas Gohr ] 86364159a61SAndreas Gohr ); 8641c548ebeSAndreas Gohr 865585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8661c548ebeSAndreas Gohr 8671c548ebeSAndreas Gohr return false; 8681c548ebeSAndreas Gohr} 8691c548ebeSAndreas Gohr 8701c548ebeSAndreas Gohr/** 8716efc45a2SDmitry Katsubo * check if a given link is interwiki link 8726efc45a2SDmitry Katsubo * 8736efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8746efc45a2SDmitry Katsubo * @return bool 8756efc45a2SDmitry Katsubo */ 8766efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 8776efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 8786efc45a2SDmitry Katsubo return false; 8796efc45a2SDmitry Katsubo} 8806efc45a2SDmitry Katsubo 8816efc45a2SDmitry Katsubo/** 88263211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 88363211f61SGlen Harris * 88422ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 88522ef1e32SAndreas Gohr * 88663211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 887140cfbcdSGerrit Uitslag * 8883272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 8893272d797SAndreas Gohr * @return string a comma separated list of hostnames 89063211f61SGlen Harris */ 89163211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 89222ef1e32SAndreas Gohr global $conf; 89322ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 89422ef1e32SAndreas Gohr 89563211f61SGlen Harris $hosts = array(); 89663211f61SGlen Harris $ips = explode(',', $ips); 897551a720fSMichael Klier 898551a720fSMichael Klier if(is_array($ips)) { 8993886270dSAndreas Gohr foreach($ips as $ip) { 900551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 90163211f61SGlen Harris } 902551a720fSMichael Klier return join(',', $hosts); 903551a720fSMichael Klier } else { 904551a720fSMichael Klier return gethostbyaddr(trim($ips)); 905551a720fSMichael Klier } 90663211f61SGlen Harris} 90763211f61SGlen Harris 90863211f61SGlen Harris/** 90915fae107Sandi * Checks if a given page is currently locked. 91015fae107Sandi * 911f3f0262cSandi * removes stale lockfiles 91215fae107Sandi * 91315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 914140cfbcdSGerrit Uitslag * 915140cfbcdSGerrit Uitslag * @param string $id page id 916140cfbcdSGerrit Uitslag * @return bool page is locked? 917f3f0262cSandi */ 918f3f0262cSandifunction checklock($id) { 919f3f0262cSandi global $conf; 920585bf44eSChristopher Smith /* @var Input $INPUT */ 921585bf44eSChristopher Smith global $INPUT; 922585bf44eSChristopher Smith 923c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 924f3f0262cSandi 925f3f0262cSandi //no lockfile 92679e79377SAndreas Gohr if(!file_exists($lock)) return false; 927f3f0262cSandi 928f3f0262cSandi //lockfile expired 929f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 930d8186216SBen Coburn @unlink($lock); 931f3f0262cSandi return false; 932f3f0262cSandi } 933f3f0262cSandi 934f3f0262cSandi //my own lock 9356d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 936c0dd3914SAdaKaleh if($ip == $INPUT->server->str('REMOTE_USER') || (session_id() && $session == session_id())) { 937f3f0262cSandi return false; 938f3f0262cSandi } 939f3f0262cSandi 940f3f0262cSandi return $ip; 941f3f0262cSandi} 942f3f0262cSandi 943f3f0262cSandi/** 94415fae107Sandi * Lock a page for editing 94515fae107Sandi * 94615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 947140cfbcdSGerrit Uitslag * 948140cfbcdSGerrit Uitslag * @param string $id page id to lock 949f3f0262cSandi */ 950f3f0262cSandifunction lock($id) { 951544ed901SDaniel Calviño Sánchez global $conf; 952585bf44eSChristopher Smith /* @var Input $INPUT */ 953585bf44eSChristopher Smith global $INPUT; 954544ed901SDaniel Calviño Sánchez 955544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 956544ed901SDaniel Calviño Sánchez return; 957544ed901SDaniel Calviño Sánchez } 958544ed901SDaniel Calviño Sánchez 959c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 960585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 961585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 962f3f0262cSandi } else { 96385fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 964f3f0262cSandi } 965f3f0262cSandi} 966f3f0262cSandi 967f3f0262cSandi/** 96815fae107Sandi * Unlock a page if it was locked by the user 969f3f0262cSandi * 97015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 971140cfbcdSGerrit Uitslag * 9723272d797SAndreas Gohr * @param string $id page id to unlock 97315fae107Sandi * @return bool true if a lock was removed 974f3f0262cSandi */ 975f3f0262cSandifunction unlock($id) { 976585bf44eSChristopher Smith /* @var Input $INPUT */ 977585bf44eSChristopher Smith global $INPUT; 978585bf44eSChristopher Smith 979c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 98079e79377SAndreas Gohr if(file_exists($lock)) { 9816d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 982c0dd3914SAdaKaleh if($ip == $INPUT->server->str('REMOTE_USER') || $session == session_id()) { 983f3f0262cSandi @unlink($lock); 984f3f0262cSandi return true; 985f3f0262cSandi } 986f3f0262cSandi } 987f3f0262cSandi return false; 988f3f0262cSandi} 989f3f0262cSandi 990f3f0262cSandi/** 991f3f0262cSandi * convert line ending to unix format 992f3f0262cSandi * 9936db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 9946db7468bSAndreas Gohr * 99515fae107Sandi * @see formText() for 2crlf conversion 99615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 997140cfbcdSGerrit Uitslag * 998140cfbcdSGerrit Uitslag * @param string $text 999140cfbcdSGerrit Uitslag * @return string 1000f3f0262cSandi */ 1001f3f0262cSandifunction cleanText($text) { 1002f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10036db7468bSAndreas Gohr 10046db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10056db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10066db7468bSAndreas Gohr // but might actually fix the problem in many cases 10078cbc5ee8SAndreas Gohr if(!\dokuwiki\Utf8\Clean::isUtf8($text)) $text = utf8_encode($text); 10086db7468bSAndreas Gohr 1009f3f0262cSandi return $text; 1010f3f0262cSandi} 1011f3f0262cSandi 1012f3f0262cSandi/** 1013f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1014f3f0262cSandi * It also converts line endings to Windows format which is 1015f3f0262cSandi * pseudo standard for webforms. 1016f3f0262cSandi * 101715fae107Sandi * @see cleanText() for 2unix conversion 101815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1019140cfbcdSGerrit Uitslag * 1020140cfbcdSGerrit Uitslag * @param string $text 1021140cfbcdSGerrit Uitslag * @return string 1022f3f0262cSandi */ 1023f3f0262cSandifunction formText($text) { 1024a46a37efSAndreas Gohr $text = str_replace("\012", "\015\012", $text ?? ''); 1025f3f0262cSandi return htmlspecialchars($text); 1026f3f0262cSandi} 1027f3f0262cSandi 1028f3f0262cSandi/** 102915fae107Sandi * Returns the specified local text in raw format 103015fae107Sandi * 103115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1032140cfbcdSGerrit Uitslag * 1033140cfbcdSGerrit Uitslag * @param string $id page id 1034140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1035140cfbcdSGerrit Uitslag * @return string 1036f3f0262cSandi */ 10372adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10382adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1039f3f0262cSandi} 1040f3f0262cSandi 1041f3f0262cSandi/** 1042f3f0262cSandi * Returns the raw WikiText 104315fae107Sandi * 104415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1045140cfbcdSGerrit Uitslag * 1046140cfbcdSGerrit Uitslag * @param string $id page id 1047e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1048140cfbcdSGerrit Uitslag * @return string 1049f3f0262cSandi */ 1050f3f0262cSandifunction rawWiki($id, $rev = '') { 1051cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1052f3f0262cSandi} 1053f3f0262cSandi 1054f3f0262cSandi/** 10557146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10567146cee2SAndreas Gohr * 10577b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10587146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1059140cfbcdSGerrit Uitslag * 1060140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1061140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10627146cee2SAndreas Gohr */ 1063fe17917eSAdrian Langfunction pageTemplate($id) { 1064a15ce62dSEsther Brunner global $conf; 1065e29549feSAndreas Gohr 1066fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1067e29549feSAndreas Gohr 10687b84afa2SAndreas Gohr // prepare initial event data 10697b84afa2SAndreas Gohr $data = array( 10707b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10717b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10727b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10737b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10747b84afa2SAndreas Gohr ); 10757b84afa2SAndreas Gohr 1076e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_PAGETPL_LOAD', $data); 10777b84afa2SAndreas Gohr if($evt->advise_before(true)) { 10787b84afa2SAndreas Gohr // the before event might have loaded the content already 10797b84afa2SAndreas Gohr if(empty($data['tpl'])) { 10807b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10817b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1082fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 108379e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 10847b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1085e29549feSAndreas Gohr } else { 1086e29549feSAndreas Gohr // search upper namespaces for templates 1087e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1088e29549feSAndreas Gohr while(strlen($path) >= $len) { 108979e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 10907b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1091e29549feSAndreas Gohr break; 1092e29549feSAndreas Gohr } 1093e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1094e29549feSAndreas Gohr } 1095e29549feSAndreas Gohr } 10967b84afa2SAndreas Gohr } 10977b84afa2SAndreas Gohr // load the content 10983d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 10997b84afa2SAndreas Gohr } 1100a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 11017b84afa2SAndreas Gohr } 11027b84afa2SAndreas Gohr $evt->advise_after(); 11037b84afa2SAndreas Gohr unset($evt); 11047b84afa2SAndreas Gohr 1105fe17917eSAdrian Lang return $data['tpl']; 11062b1223ecSAdrian Lang} 11072b1223ecSAdrian Lang 11082b1223ecSAdrian Lang/** 11092b1223ecSAdrian Lang * Performs common page template replacements 11107b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11112b1223ecSAdrian Lang * 11122b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1113140cfbcdSGerrit Uitslag * 1114140cfbcdSGerrit Uitslag * @param array $data array with event data 1115140cfbcdSGerrit Uitslag * @return string 11162b1223ecSAdrian Lang */ 1117d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11183272d797SAndreas Gohr /** 11193272d797SAndreas Gohr * @var string $id the id of the page to be created 11203272d797SAndreas Gohr * @var string $tpl the text used as template 11213272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11223272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11233272d797SAndreas Gohr */ 1124fe17917eSAdrian Lang extract($data); 1125fe17917eSAdrian Lang 1126b856f7dfSAdrian Lang global $USERINFO; 1127bce53b1fSAdrian Lang global $conf; 1128585bf44eSChristopher Smith /* @var Input $INPUT */ 1129585bf44eSChristopher Smith global $INPUT; 1130e29549feSAndreas Gohr 1131e29549feSAndreas Gohr // replace placeholders 113226ece5a7SAndreas Gohr $file = noNS($id); 113337c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 113426ece5a7SAndreas Gohr 11353272d797SAndreas Gohr $tpl = str_replace( 11363272d797SAndreas Gohr array( 113726ece5a7SAndreas Gohr '@ID@', 113826ece5a7SAndreas Gohr '@NS@', 11398a7bcf66SShota Miyazaki '@CURNS@', 1140a3db0ab0SSimon Lees '@!CURNS@', 1141a3db0ab0SSimon Lees '@!!CURNS@', 1142a3db0ab0SSimon Lees '@!CURNS!@', 114326ece5a7SAndreas Gohr '@FILE@', 114426ece5a7SAndreas Gohr '@!FILE@', 114526ece5a7SAndreas Gohr '@!FILE!@', 114626ece5a7SAndreas Gohr '@PAGE@', 114726ece5a7SAndreas Gohr '@!PAGE@', 114826ece5a7SAndreas Gohr '@!!PAGE@', 114926ece5a7SAndreas Gohr '@!PAGE!@', 115026ece5a7SAndreas Gohr '@USER@', 115126ece5a7SAndreas Gohr '@NAME@', 115226ece5a7SAndreas Gohr '@MAIL@', 115326ece5a7SAndreas Gohr '@DATE@', 115426ece5a7SAndreas Gohr ), 115526ece5a7SAndreas Gohr array( 115626ece5a7SAndreas Gohr $id, 115726ece5a7SAndreas Gohr getNS($id), 11588a7bcf66SShota Miyazaki curNS($id), 1159c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst(curNS($id)), 1160c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords(curNS($id)), 1161c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper(curNS($id)), 116226ece5a7SAndreas Gohr $file, 11638cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($file), 11648cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($file), 116526ece5a7SAndreas Gohr $page, 11668cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($page), 11678cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords($page), 11688cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($page), 1169585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11703e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 11713e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 117226ece5a7SAndreas Gohr $conf['dformat'], 11733272d797SAndreas Gohr ), $tpl 11743272d797SAndreas Gohr ); 117526ece5a7SAndreas Gohr 11767d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1177bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1178bad6fc0dSAndreas Gohr '/%./', 1179bad6fc0dSAndreas Gohr function ($m) { 118010f359adSAndreas Gohr return dformat(null, $m[0]); 1181bad6fc0dSAndreas Gohr }, 1182bad6fc0dSAndreas Gohr $tpl 1183bad6fc0dSAndreas Gohr ); 1184d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1185a15ce62dSEsther Brunner return $tpl; 11867146cee2SAndreas Gohr} 11877146cee2SAndreas Gohr 11887146cee2SAndreas Gohr/** 118915fae107Sandi * Returns the raw Wiki Text in three slices. 119015fae107Sandi * 119115fae107Sandi * The range parameter needs to have the form "from-to" 119215cfe303Sandi * and gives the range of the section in bytes - no 119315cfe303Sandi * UTF-8 awareness is needed. 1194f3f0262cSandi * The returned order is prefix, section and suffix. 119515fae107Sandi * 119615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1197140cfbcdSGerrit Uitslag * 1198140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1199140cfbcdSGerrit Uitslag * @param string $id page id 1200140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 120142ea7f44SGerrit Uitslag * @return string[] with three slices 1202f3f0262cSandi */ 1203f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1204cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1205f3f0262cSandi 120680fcb268SAdrian Lang // Parse range 1207ec34bb30SAndreas Gohr list($from, $to) = sexplode('-', $range, 2); 120880fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 120980fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 121080fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 121180fcb268SAdrian Lang 121259bc3b48SGerrit Uitslag $slices = array(); 121380fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 121480fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 121515cfe303Sandi $slices[2] = substr($text, $to); 1216f3f0262cSandi return $slices; 1217f3f0262cSandi} 1218f3f0262cSandi 1219f3f0262cSandi/** 122015fae107Sandi * Joins wiki text slices 122115fae107Sandi * 122280fcb268SAdrian Lang * function to join the text slices. 1223f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1224f3f0262cSandi * lines between sections if needed (used on saving). 122515fae107Sandi * 122615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1227140cfbcdSGerrit Uitslag * 1228140cfbcdSGerrit Uitslag * @param string $pre prefix 1229140cfbcdSGerrit Uitslag * @param string $text text in the middle 1230140cfbcdSGerrit Uitslag * @param string $suf suffix 1231140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1232140cfbcdSGerrit Uitslag * @return string 1233f3f0262cSandi */ 1234f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1235f3f0262cSandi if($pretty) { 123680fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12373272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12383272d797SAndreas Gohr ) { 123980fcb268SAdrian Lang $pre .= "\n"; 124080fcb268SAdrian Lang } 124180fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12423272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12433272d797SAndreas Gohr ) { 124480fcb268SAdrian Lang $text .= "\n"; 124580fcb268SAdrian Lang } 1246f3f0262cSandi } 1247f3f0262cSandi 1248f3f0262cSandi return $pre.$text.$suf; 1249f3f0262cSandi} 1250f3f0262cSandi 1251f3f0262cSandi/** 1252b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1253b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1254b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1255b24d9195SAndreas Gohr * 1256b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1257b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1258b24d9195SAndreas Gohr * 1259b24d9195SAndreas Gohr * @param string $id the page ID 126069f9b481SSatoshi Sahara * @deprecated 2021-11-28 1261b24d9195SAndreas Gohr */ 1262b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 126379a2d784SGerrit Uitslag dbg_deprecated(PageFile::class .'::detectExternalEdit()'); 1264b24e9c4aSSatoshi Sahara (new PageFile($id))->detectExternalEdit(); 1265b24d9195SAndreas Gohr} 1266b24d9195SAndreas Gohr 1267b24d9195SAndreas Gohr/** 1268a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1269a701424fSBen Coburn * Also directs changelog and attic updates. 127015fae107Sandi * 127115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 127271726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1273140cfbcdSGerrit Uitslag * 1274140cfbcdSGerrit Uitslag * @param string $id page id 1275140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1276140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1277140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1278f3f0262cSandi */ 1279b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1280585bf44eSChristopher Smith 1281b24e9c4aSSatoshi Sahara // get COMMON_WIKIPAGE_SAVE event data 1282b24e9c4aSSatoshi Sahara $data = (new PageFile($id))->saveWikiText($text, $summary, $minor); 1283a577fbc2SAndreas Gohr if(!$data) return; // save was cancelled (for no changes or by a plugin) 1284ac3ed4afSGerrit Uitslag 128526a0801fSAndreas Gohr // send notify mails 12863b813d43SSatoshi Sahara list('oldRevision' => $rev, 'newRevision' => $new_rev, 'summary' => $summary) = $data; 12873b813d43SSatoshi Sahara notify($id, 'admin', $rev, $summary, $minor, $new_rev); 12883b813d43SSatoshi Sahara notify($id, 'subscribers', $rev, $summary, $minor, $new_rev); 1289f3f0262cSandi 12902eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1291fe9ec250SChris Smith if (useHeading('content')) { 129207ff0babSMichael Hamann $pages = ft_backlinks($id, true); 12932eccbdaaSGina Haeussge foreach ($pages as $page) { 12940db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 12952eccbdaaSGina Haeussge $cache->removeCache(); 12962eccbdaaSGina Haeussge } 12972eccbdaaSGina Haeussge } 1298f3f0262cSandi} 1299f3f0262cSandi 1300f3f0262cSandi/** 1301d5824ab9SSatoshi Sahara * moves the current version to the attic and returns its revision date 130215fae107Sandi * 130315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1304140cfbcdSGerrit Uitslag * 1305140cfbcdSGerrit Uitslag * @param string $id page id 1306140cfbcdSGerrit Uitslag * @return int|string revision timestamp 130769f9b481SSatoshi Sahara * @deprecated 2021-11-28 1308f3f0262cSandi */ 1309f3f0262cSandifunction saveOldRevision($id) { 131079a2d784SGerrit Uitslag dbg_deprecated(PageFile::class .'::saveOldRevision()'); 1311b24e9c4aSSatoshi Sahara return (new PageFile($id))->saveOldRevision(); 1312f3f0262cSandi} 1313f3f0262cSandi 1314f3f0262cSandi/** 1315fde10de4SAdrian Lang * Sends a notify mail on page change or registration 131626a0801fSAndreas Gohr * 131726a0801fSAndreas Gohr * @param string $id The changed page 1318fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 13193272d797SAndreas Gohr * @param int|string $rev Old page revision 132026a0801fSAndreas Gohr * @param string $summary What changed 132190033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 132242ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 132383734cddSPhy * @param int|string $current_rev New page revision 13243272d797SAndreas Gohr * @return bool 1325140cfbcdSGerrit Uitslag * 132615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1327f3f0262cSandi */ 132883734cddSPhyfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array(), $current_rev = false) { 1329f3f0262cSandi global $conf; 1330585bf44eSChristopher Smith /* @var Input $INPUT */ 1331585bf44eSChristopher Smith global $INPUT; 1332b158d625SSteven Danz 13336df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 133426a0801fSAndreas Gohr if ($who == 'admin') { 13353272d797SAndreas Gohr if (empty($conf['notify'])) return false; //notify enabled? 13362ed38036SAndreas Gohr $tpl = 'mailtext'; 133726a0801fSAndreas Gohr $to = $conf['notify']; 133826a0801fSAndreas Gohr } elseif ($who == 'subscribers') { 133984c1127cSAndreas Gohr if (!actionOK('subscribe')) return false; //subscribers enabled? 1340585bf44eSChristopher Smith if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 13410bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 1342cbb44eabSAndreas Gohr Event::createAndTrigger( 13433272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1344c8cc4053SAndreas Gohr array(new SubscriberManager(), 'notifyAddresses') 13453272d797SAndreas Gohr ); 13462ed38036SAndreas Gohr $to = $data['addresslist']; 13472ed38036SAndreas Gohr if (empty($to)) return false; 13482ed38036SAndreas Gohr $tpl = 'subscr_single'; 134926a0801fSAndreas Gohr } else { 13503272d797SAndreas Gohr return false; //just to be safe 135126a0801fSAndreas Gohr } 135226a0801fSAndreas Gohr 13536df843eeSAndreas Gohr // prepare content 1354704a815fSMichael Große $subscription = new PageSubscriptionSender(); 135583734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1356f3f0262cSandi} 13572ed38036SAndreas Gohr 135815fae107Sandi/** 135971f7bde7SAndreas Gohr * extracts the query from a search engine referrer 136015fae107Sandi * 136115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 136271f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1363140cfbcdSGerrit Uitslag * 1364140cfbcdSGerrit Uitslag * @return array|string 1365f3f0262cSandi */ 1366f3f0262cSandifunction getGoogleQuery() { 1367585bf44eSChristopher Smith /* @var Input $INPUT */ 1368585bf44eSChristopher Smith global $INPUT; 1369585bf44eSChristopher Smith 1370585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1371c66972f2SAdrian Lang return ''; 1372c66972f2SAdrian Lang } 1373585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1374f3f0262cSandi 1375079b3ac1SAndreas Gohr // only handle common SEs 1376c7875401SJyoti S if(!array_key_exists('host', $url)) return ''; 1377079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1378e4d8a516SKazutaka Miyasaka 1379079b3ac1SAndreas Gohr $query = array(); 1380181adffeSJulian Jeggle if(!array_key_exists('query', $url)) return ''; 1381f3f0262cSandi parse_str($url['query'], $query); 1382e4d8a516SKazutaka Miyasaka 1383c66972f2SAdrian Lang $q = ''; 1384079b3ac1SAndreas Gohr if(isset($query['q'])){ 1385079b3ac1SAndreas Gohr $q = $query['q']; 1386079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1387079b3ac1SAndreas Gohr $q = $query['p']; 1388079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1389079b3ac1SAndreas Gohr $q = $query['query']; 1390079b3ac1SAndreas Gohr } 1391079b3ac1SAndreas Gohr $q = trim($q); 1392f3f0262cSandi 1393079b3ac1SAndreas Gohr if(!$q) return ''; 1394c7dc833bSPhy // ignore if query includes a full URL 1395c7dc833bSPhy if(strpos($q, '//') !== false) return ''; 13966531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1397f93b3b50SAndreas Gohr return $q; 1398f3f0262cSandi} 1399f3f0262cSandi 1400f3f0262cSandi/** 1401f3f0262cSandi * Return the human readable size of a file 1402f3f0262cSandi * 1403f3f0262cSandi * @param int $size A file size 1404f3f0262cSandi * @param int $dec A number of decimal places 140574160ca1SGerrit Uitslag * @return string human readable size 1406140cfbcdSGerrit Uitslag * 1407f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1408f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1409f3f0262cSandi * @version 1.0.0 1410f3f0262cSandi */ 1411f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1412f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1413f3f0262cSandi $count = count($sizes); 1414f3f0262cSandi $i = 0; 1415f3f0262cSandi 1416f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1417f3f0262cSandi $size /= 1024; 1418f3f0262cSandi $i++; 1419f3f0262cSandi } 1420f3f0262cSandi 1421ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1422f3f0262cSandi} 1423f3f0262cSandi 142415fae107Sandi/** 1425c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1426c57e365eSAndreas Gohr * 1427c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1428140cfbcdSGerrit Uitslag * 1429140cfbcdSGerrit Uitslag * @param int $dt timestamp 1430140cfbcdSGerrit Uitslag * @return string 1431c57e365eSAndreas Gohr */ 1432c57e365eSAndreas Gohrfunction datetime_h($dt) { 1433c57e365eSAndreas Gohr global $lang; 1434c57e365eSAndreas Gohr 1435c57e365eSAndreas Gohr $ago = time() - $dt; 1436c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1437c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1438c57e365eSAndreas Gohr } 1439c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1440c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1441c57e365eSAndreas Gohr } 1442c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1443c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1444c57e365eSAndreas Gohr } 1445c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1446c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1447c57e365eSAndreas Gohr } 1448c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1449c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1450c57e365eSAndreas Gohr } 1451c57e365eSAndreas Gohr if($ago > 60 * 2) { 1452c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1453c57e365eSAndreas Gohr } 1454c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1455c57e365eSAndreas Gohr} 1456c57e365eSAndreas Gohr 1457c57e365eSAndreas Gohr/** 1458f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1459f2263577SAndreas Gohr * 1460f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1461f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1462f2263577SAndreas Gohr * 1463f2263577SAndreas Gohr * @see datetime_h 1464f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1465140cfbcdSGerrit Uitslag * 1466140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1467140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1468140cfbcdSGerrit Uitslag * @return string 1469f2263577SAndreas Gohr */ 1470f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1471f2263577SAndreas Gohr global $conf; 1472f2263577SAndreas Gohr 1473f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1474f2263577SAndreas Gohr $dt = (int) $dt; 1475f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1476f2263577SAndreas Gohr 1477f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1478f2263577SAndreas Gohr return strftime($format, $dt); 1479f2263577SAndreas Gohr} 1480f2263577SAndreas Gohr 1481f2263577SAndreas Gohr/** 1482c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1483c4f79b71SMichael Hamann * 1484c4f79b71SMichael Hamann * @author <ungu at terong dot com> 148559752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1486140cfbcdSGerrit Uitslag * 14877e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 14883272d797SAndreas Gohr * @return string 1489c4f79b71SMichael Hamann */ 1490c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1491c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1492c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1493c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1494c4f79b71SMichael Hamann $date_mod .= $time_zone; 1495c4f79b71SMichael Hamann return $date_mod; 1496c4f79b71SMichael Hamann} 1497c4f79b71SMichael Hamann 1498c4f79b71SMichael Hamann/** 149900a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 150000a7b5adSEsther Brunner * 150100a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 150200a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1503140cfbcdSGerrit Uitslag * 1504140cfbcdSGerrit Uitslag * @param string $email email address 1505140cfbcdSGerrit Uitslag * @return string 150600a7b5adSEsther Brunner */ 150700a7b5adSEsther Brunnerfunction obfuscate($email) { 150800a7b5adSEsther Brunner global $conf; 150900a7b5adSEsther Brunner 151000a7b5adSEsther Brunner switch($conf['mailguard']) { 151100a7b5adSEsther Brunner case 'visible' : 151200a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 151300a7b5adSEsther Brunner return strtr($email, $obfuscate); 151400a7b5adSEsther Brunner 151500a7b5adSEsther Brunner case 'hex' : 1516c1ec88ceSAndreas Gohr return \dokuwiki\Utf8\Conversion::toHtml($email, true); 151700a7b5adSEsther Brunner 151800a7b5adSEsther Brunner case 'none' : 151900a7b5adSEsther Brunner default : 152000a7b5adSEsther Brunner return $email; 152100a7b5adSEsther Brunner } 152200a7b5adSEsther Brunner} 152300a7b5adSEsther Brunner 152400a7b5adSEsther Brunner/** 152589541d4bSAndreas Gohr * Removes quoting backslashes 152689541d4bSAndreas Gohr * 152789541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1528140cfbcdSGerrit Uitslag * 1529140cfbcdSGerrit Uitslag * @param string $string 1530140cfbcdSGerrit Uitslag * @param string $char backslashed character 1531140cfbcdSGerrit Uitslag * @return string 153289541d4bSAndreas Gohr */ 153389541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 153489541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 153589541d4bSAndreas Gohr} 153689541d4bSAndreas Gohr 153773038c47SAndreas Gohr/** 153873038c47SAndreas Gohr * Convert php.ini shorthands to byte 153973038c47SAndreas Gohr * 1540a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1541140cfbcdSGerrit Uitslag * 1542a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1543a81f3d99SAndreas Gohr * 1544a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1545a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1546a81f3d99SAndreas Gohr * @return int 154773038c47SAndreas Gohr */ 1548a81f3d99SAndreas Gohrfunction php_to_byte($value) { 1549f5c0c80bSAndreas Gohr switch (strtoupper(substr($value,-1))) { 155073038c47SAndreas Gohr case 'G': 1551a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024 * 1024; 155273038c47SAndreas Gohr break; 155373038c47SAndreas Gohr case 'M': 1554a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024; 1555a81f3d99SAndreas Gohr break; 155673038c47SAndreas Gohr case 'K': 1557a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024; 155873038c47SAndreas Gohr break; 15599eeeb775SAndreas Gohr default: 1560a81f3d99SAndreas Gohr $ret = intval($value); 156149cbd23eSOtto Vainio break; 156273038c47SAndreas Gohr } 156373038c47SAndreas Gohr return $ret; 156473038c47SAndreas Gohr} 156573038c47SAndreas Gohr 1566546d3a99SAndreas Gohr/** 1567546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1568140cfbcdSGerrit Uitslag * 1569140cfbcdSGerrit Uitslag * @param string $string 1570140cfbcdSGerrit Uitslag * @return string 1571546d3a99SAndreas Gohr */ 1572546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1573546d3a99SAndreas Gohr return preg_quote($string, '/'); 1574546d3a99SAndreas Gohr} 157573038c47SAndreas Gohr 1576bd2f6c2fSAndreas Gohr/** 1577bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1578bd2f6c2fSAndreas Gohr * 1579c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1580bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1581bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1582bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1583bd2f6c2fSAndreas Gohr * 1584bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1585bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1586bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1587bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1588bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 15893272d797SAndreas Gohr * @return string 1590bd2f6c2fSAndreas Gohr */ 1591a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 15928cbc5ee8SAndreas Gohr $max = $max - \dokuwiki\Utf8\PhpString::strlen($keep); 1593bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 15948cbc5ee8SAndreas Gohr $len = \dokuwiki\Utf8\PhpString::strlen($short); 1595bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1596bd2f6c2fSAndreas Gohr $half = floor($max / 2); 15976ce3e5f8SAndreas Gohr return $keep . 15986ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, 0, $half - 1) . 15996ce3e5f8SAndreas Gohr $char . 16006ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, $len - $half); 1601bd2f6c2fSAndreas Gohr} 1602bd2f6c2fSAndreas Gohr 1603dc58b6f4SAndy Webber/** 1604dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1605dc58b6f4SAndy Webber * in page footer and recent changes pages 1606dc58b6f4SAndy Webber * 1607b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 160815f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1609c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 161015f3bc49SGerrit Uitslag * 1611dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1612dc58b6f4SAndy Webber */ 161315f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1614cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1615dc58b6f4SAndy Webber} 1616dc58b6f4SAndy Webber 161760a396c8SGerrit Uitslag/** 161860a396c8SGerrit Uitslag * Returns users realname w/o link 161960a396c8SGerrit Uitslag * 1620f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 162115f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1622c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 162360a396c8SGerrit Uitslag * 162460a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 162560a396c8SGerrit Uitslag */ 1626cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 162760a396c8SGerrit Uitslag global $conf, $INFO; 1628e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 162960a396c8SGerrit Uitslag global $auth; 163030f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 163130f6ec4bSGerrit Uitslag global $INPUT; 163260a396c8SGerrit Uitslag 163360a396c8SGerrit Uitslag // prepare initial event data 163460a396c8SGerrit Uitslag $data = array( 163560a396c8SGerrit Uitslag 'username' => $username, // the unique user name 163660a396c8SGerrit Uitslag 'name' => '', 163760a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 163860a396c8SGerrit Uitslag 'target' => '', 163960a396c8SGerrit Uitslag 'pre' => '', 164060a396c8SGerrit Uitslag 'suf' => '', 164160a396c8SGerrit Uitslag 'style' => '', 164260a396c8SGerrit Uitslag 'more' => '', 164360a396c8SGerrit Uitslag 'url' => '', 164460a396c8SGerrit Uitslag 'title' => '', 164560a396c8SGerrit Uitslag 'class' => '' 164660a396c8SGerrit Uitslag ), 16474d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 164815f3bc49SGerrit Uitslag 'textonly' => $textonly 164960a396c8SGerrit Uitslag ); 165062c8004eSGerrit Uitslag if($username === null) { 165130f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 165215f3bc49SGerrit Uitslag if($textonly){ 165315f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 165415f3bc49SGerrit Uitslag }else { 165564159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 165664159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 165760a396c8SGerrit Uitslag } 165815f3bc49SGerrit Uitslag } 165960a396c8SGerrit Uitslag 1660e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 166160a396c8SGerrit Uitslag if($evt->advise_before(true)) { 166260a396c8SGerrit Uitslag if(empty($data['name'])) { 166360a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 166465833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1665dc58b6f4SAndy Webber switch($conf['showuseras']) { 1666dc58b6f4SAndy Webber case 'username': 16677f081821SGerrit Uitslag case 'username_link': 166815f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 166960a396c8SGerrit Uitslag break; 1670dc58b6f4SAndy Webber case 'email': 1671dc58b6f4SAndy Webber case 'email_link': 167260a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 167360a396c8SGerrit Uitslag break; 1674dc58b6f4SAndy Webber } 167565833968SGerrit Uitslag } else { 167665833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 167760a396c8SGerrit Uitslag } 167860a396c8SGerrit Uitslag } 16797f081821SGerrit Uitslag 16807f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 16817f081821SGerrit Uitslag static $xhtml_renderer = null; 16827f081821SGerrit Uitslag 168315f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 16847f081821SGerrit Uitslag 16857f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 168660a396c8SGerrit Uitslag if(!isset($info)) { 168760a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 168860a396c8SGerrit Uitslag } 168960a396c8SGerrit Uitslag if(isset($info) && $info) { 16907f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 169160a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1692dc58b6f4SAndy Webber } else { 16937f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 16947f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 16957f081821SGerrit Uitslag } 16967f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 16977f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 16987f081821SGerrit Uitslag } 16997f081821SGerrit Uitslag $shortcut = 'user'; 1700533772e1SGerrit Uitslag $exists = null; 17016496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 17022a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 17036496c33fSGerrit Uitslag if($exists !== null) { 17046496c33fSGerrit Uitslag if($exists) { 17056496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 17066496c33fSGerrit Uitslag } else { 17076496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 17086496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 17096496c33fSGerrit Uitslag } 17106496c33fSGerrit Uitslag } 1711dc58b6f4SAndy Webber } 1712dc58b6f4SAndy Webber } else { 171315f3bc49SGerrit Uitslag $data['textonly'] = true; 1714dc58b6f4SAndy Webber } 171560a396c8SGerrit Uitslag 171660a396c8SGerrit Uitslag } else { 171715f3bc49SGerrit Uitslag $data['textonly'] = true; 171860a396c8SGerrit Uitslag } 171960a396c8SGerrit Uitslag } 172060a396c8SGerrit Uitslag 172115f3bc49SGerrit Uitslag if($data['textonly']) { 17224d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 172360a396c8SGerrit Uitslag } else { 172460a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 172560a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 172660a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 172760a396c8SGerrit Uitslag } 17284d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 172960a396c8SGerrit Uitslag } 173060a396c8SGerrit Uitslag } 173160a396c8SGerrit Uitslag $evt->advise_after(); 173260a396c8SGerrit Uitslag unset($evt); 173360a396c8SGerrit Uitslag 17344d5fc927SGerrit Uitslag return $data['userlink']; 1735066fee30SAndreas Gohr} 1736066fee30SAndreas Gohr 1737066fee30SAndreas Gohr/** 1738066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1739066fee30SAndreas Gohr * When no image exists, returns an empty string 1740066fee30SAndreas Gohr * 1741066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1742140cfbcdSGerrit Uitslag * 1743066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 17443272d797SAndreas Gohr * @return string 1745066fee30SAndreas Gohr */ 1746066fee30SAndreas Gohrfunction license_img($type) { 1747066fee30SAndreas Gohr global $license; 1748066fee30SAndreas Gohr global $conf; 1749066fee30SAndreas Gohr if(!$conf['license']) return ''; 1750066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1751066fee30SAndreas Gohr $try = array(); 1752066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1753066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1754066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1755066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1756066fee30SAndreas Gohr } 1757066fee30SAndreas Gohr foreach($try as $src) { 175879e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1759066fee30SAndreas Gohr } 1760066fee30SAndreas Gohr return ''; 1761dc58b6f4SAndy Webber} 1762dc58b6f4SAndy Webber 176313c08e2fSMichael Klier/** 176413c08e2fSMichael Klier * Checks if the given amount of memory is available 176513c08e2fSMichael Klier * 176613c08e2fSMichael Klier * If the memory_get_usage() function is not available the 176713c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 176813c08e2fSMichael Klier * 176913c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 177013c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 17713272d797SAndreas Gohr * 17723272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1773140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 17743272d797SAndreas Gohr * @return bool 177513c08e2fSMichael Klier */ 177613c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 177713c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 177813c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1779985d6187SElenchus if($limit == -1) return true; // unlimited 178013c08e2fSMichael Klier 178113c08e2fSMichael Klier // parse limit to bytes 178213c08e2fSMichael Klier $limit = php_to_byte($limit); 178313c08e2fSMichael Klier 178413c08e2fSMichael Klier // get used memory if possible 178513c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 178613c08e2fSMichael Klier $used = memory_get_usage(); 178749eb6e38SAndreas Gohr } else { 178849eb6e38SAndreas Gohr $used = $bytes; 178913c08e2fSMichael Klier } 179013c08e2fSMichael Klier 179113c08e2fSMichael Klier if($used + $mem > $limit) { 179213c08e2fSMichael Klier return false; 179313c08e2fSMichael Klier } 179413c08e2fSMichael Klier 179513c08e2fSMichael Klier return true; 179613c08e2fSMichael Klier} 179713c08e2fSMichael Klier 1798af2408d5SAndreas Gohr/** 1799af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1800af2408d5SAndreas Gohr * 1801af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1802af2408d5SAndreas Gohr * 1803af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1804af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1805140cfbcdSGerrit Uitslag * 1806140cfbcdSGerrit Uitslag * @param string $url url being directed to 1807af2408d5SAndreas Gohr */ 1808af2408d5SAndreas Gohrfunction send_redirect($url) { 180998ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 181098ca30d2SAndreas Gohr 1811585bf44eSChristopher Smith /* @var Input $INPUT */ 1812585bf44eSChristopher Smith global $INPUT; 1813585bf44eSChristopher Smith 18140181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 18150181f021SAndreas Gohr global $MSG; 18160181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 18170181f021SAndreas Gohr //reopen session, store data and close session again 18180181f021SAndreas Gohr @session_start(); 18190181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 18200181f021SAndreas Gohr } 18210181f021SAndreas Gohr 1822d4869846SAndreas Gohr // always close the session 1823d4869846SAndreas Gohr session_write_close(); 1824d4869846SAndreas Gohr 1825af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1826585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1827585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1828585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 18293272d797SAndreas Gohr $matches[1] < 6 18303272d797SAndreas Gohr ) { 1831af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1832af2408d5SAndreas Gohr } else { 1833af2408d5SAndreas Gohr header('Location: '.$url); 1834af2408d5SAndreas Gohr } 183581781cb6SAndreas Gohr 1836572dc222SLarsDW223 // no exits during unit tests 183727c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 183827c0c399SAndreas Gohr // pass info about the redirect back to the test suite 183927c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 184027c0c399SAndreas Gohr if($testRequest !== null) { 184127c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 184227c0c399SAndreas Gohr } 1843572dc222SLarsDW223 return; 1844572dc222SLarsDW223 } 184527c0c399SAndreas Gohr 1846af2408d5SAndreas Gohr exit; 1847af2408d5SAndreas Gohr} 1848af2408d5SAndreas Gohr 18495b75cd1fSAdrian Lang/** 18505b75cd1fSAdrian Lang * Validate a value using a set of valid values 18515b75cd1fSAdrian Lang * 18525b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 18535b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 18545b75cd1fSAdrian Lang * default is specified, throws an exception. 18555b75cd1fSAdrian Lang * 18565b75cd1fSAdrian Lang * @param string $param The name of the parameter 18575b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 18585b75cd1fSAdrian Lang * be marked by the key “default”. 18595b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 18605b75cd1fSAdrian Lang * or $_GET) 18615b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 18625b75cd1fSAdrian Lang * 18633272d797SAndreas Gohr * @throws Exception 18643272d797SAndreas Gohr * @return mixed 18655b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 18665b75cd1fSAdrian Lang */ 18675b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 18685b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 18695b75cd1fSAdrian Lang return $array[$param]; 18705b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 18715b75cd1fSAdrian Lang return $valid_values['default']; 18725b75cd1fSAdrian Lang } else { 18735b75cd1fSAdrian Lang throw new Exception($exc); 18745b75cd1fSAdrian Lang } 18755b75cd1fSAdrian Lang} 18765b75cd1fSAdrian Lang 187763703ba5SAndreas Gohr/** 187863703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1879646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 1880140cfbcdSGerrit Uitslag * 1881140cfbcdSGerrit Uitslag * @param string $pref preference key 1882b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 1883140cfbcdSGerrit Uitslag * @return string preference value 188463703ba5SAndreas Gohr */ 1885554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1886646a531aSChristopher Smith $enc_pref = urlencode($pref); 188706c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1888554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 188963703ba5SAndreas Gohr $cnt = count($parts); 18901c3eca7dSPhy 18911c3eca7dSPhy // due to #2721 there might be duplicate entries, 18921c3eca7dSPhy // so we read from the end 18931c3eca7dSPhy for($i = $cnt-2; $i >= 0; $i -= 2) { 1894646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1895646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1896554a8c9fSAdrian Lang } 1897554a8c9fSAdrian Lang } 1898554a8c9fSAdrian Lang } 1899554a8c9fSAdrian Lang return $default; 1900554a8c9fSAdrian Lang} 1901554a8c9fSAdrian Lang 19023c94d07bSAnika Henke/** 19033c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 190436ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 19053a970889SAnika Henke * Remove it by setting $val to false 1906140cfbcdSGerrit Uitslag * 1907140cfbcdSGerrit Uitslag * @param string $pref preference key 1908140cfbcdSGerrit Uitslag * @param string $val preference value 19093c94d07bSAnika Henke */ 19103c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 19113c94d07bSAnika Henke global $conf; 19123c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 19133c94d07bSAnika Henke $cookieVal = ''; 19143c94d07bSAnika Henke 19151c3eca7dSPhy if($orig !== false && ($orig !== $val)) { 19163c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 19173c94d07bSAnika Henke $cnt = count($parts); 191836ec377eSChristopher Smith // urlencode $pref for the comparison 191936ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 19201c3eca7dSPhy $seen = false; 19213c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 192236ec377eSChristopher Smith if ($parts[$i] == $enc_pref) { 19231c3eca7dSPhy if (!$seen){ 19243a970889SAnika Henke if ($val !== false) { 1925bf8f8509SAndreas Gohr $parts[$i + 1] = rawurlencode($val ?? ''); 19263a970889SAnika Henke } else { 19273a970889SAnika Henke unset($parts[$i]); 19283a970889SAnika Henke unset($parts[$i + 1]); 19293a970889SAnika Henke } 19301c3eca7dSPhy $seen = true; 19311c3eca7dSPhy } else { 19321c3eca7dSPhy // no break because we want to remove duplicate entries 19331c3eca7dSPhy unset($parts[$i]); 19341c3eca7dSPhy unset($parts[$i + 1]); 19351c3eca7dSPhy } 19363c94d07bSAnika Henke } 19373c94d07bSAnika Henke } 19383c94d07bSAnika Henke $cookieVal = implode('#', $parts); 19391c3eca7dSPhy } else if ($orig === false && $val !== false) { 1940c10f256aSDamien Regad $cookieVal = (isset($_COOKIE['DOKU_PREFS']) ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 194164159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 19423c94d07bSAnika Henke } 19433c94d07bSAnika Henke 194475e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 19455833995aSPhy if(defined('DOKU_UNITTEST')) { 19465833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 19475833995aSPhy }else{ 1948bf8392ebSAndreas Gohr setcookie('DOKU_PREFS', $cookieVal, [ 1949bf8392ebSAndreas Gohr 'expires' => time() + 365 * 24 * 3600, 1950bf8392ebSAndreas Gohr 'path' => $cookieDir, 1951bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1952bf8392ebSAndreas Gohr 'samesite' => 'Lax' 1953bf8392ebSAndreas Gohr ]); 19543c94d07bSAnika Henke } 19553c94d07bSAnika Henke} 19563c94d07bSAnika Henke 1957f8fb2d18SAndreas Gohr/** 1958f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 1959f8fb2d18SAndreas Gohr * 196042ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 1961f8fb2d18SAndreas Gohr */ 1962f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 1963f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 1964f8fb2d18SAndreas Gohr} 1965f8fb2d18SAndreas Gohr 19663c27983bSAndreas Gohr/** 196771de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 19683c27983bSAndreas Gohr * 19693c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 19703c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 19713c27983bSAndreas Gohr * files are embedded. 19723c27983bSAndreas Gohr * 197371de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 197471de5572SAndreas Gohr * 19753c27983bSAndreas Gohr * @param string $file full path to the SVG file 19763c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 197771de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 19783c27983bSAndreas Gohr */ 19794cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 19803c27983bSAndreas Gohr $file = trim($file); 19813c27983bSAndreas Gohr if($file === '') return false; 19823c27983bSAndreas Gohr if(!file_exists($file)) return false; 19833c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 19843c27983bSAndreas Gohr if(!is_readable($file)) return false; 19853c27983bSAndreas Gohr $content = file_get_contents($file); 19860849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 19870849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 19880849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 19890849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 19903c27983bSAndreas Gohr $content = trim($content); 19913c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 199271de5572SAndreas Gohr return $content; 19933c27983bSAndreas Gohr} 19943c27983bSAndreas Gohr 1995e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1996