1ed7b5f09Sandi<?php 2d4f83172SAndreas Gohr 315fae107Sandi/** 415fae107Sandi * Common DokuWiki functions 515fae107Sandi * 615fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 815fae107Sandi */ 915fae107Sandi 1024870174SAndreas Gohruse dokuwiki\PassHash; 1124870174SAndreas Gohruse dokuwiki\Draft; 123f108b37SAndreas Gohruse dokuwiki\PrefCookie; 1324870174SAndreas Gohruse dokuwiki\Utf8\Clean; 1424870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1524870174SAndreas Gohruse dokuwiki\Utf8\Conversion; 160db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 170c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 18b24e9c4aSSatoshi Saharause dokuwiki\File\PageFile; 19704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 2075d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 21e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 22e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 232aba9aedSAndreas Gohruse dokuwiki\Ip; 24*6734bb8cSAndreas Gohruse dokuwiki\Search\MetadataSearch; 250c3a5702SAndreas Gohr 268b19906eSAndreas Gohruse function PHP81_BC\strftime; 278b19906eSAndreas Gohr 28f3f0262cSandi/** 29d5197206Schris * Wrapper around htmlspecialchars() 30d5197206Schris * 318b19906eSAndreas Gohr * @param string $string the string being converted 328b19906eSAndreas Gohr * @return string converted string 33d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 34d5197206Schris * @see htmlspecialchars() 35140cfbcdSGerrit Uitslag * 36d5197206Schris */ 37d868eb89SAndreas Gohrfunction hsc($string) 38d868eb89SAndreas Gohr{ 39f7711f2bSAndreas Gohr return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8'); 40d5197206Schris} 41d5197206Schris 42d5197206Schris/** 4312dd3cbcSAndreas Gohr * A safer explode for fixed length lists 4412dd3cbcSAndreas Gohr * 4512dd3cbcSAndreas Gohr * This works just like explode(), but will always return the wanted number of elements. 4612dd3cbcSAndreas Gohr * If the $input string does not contain enough elements, the missing elements will be 4712dd3cbcSAndreas Gohr * filled up with the $default value. If the input string contains more elements, the last 4812dd3cbcSAndreas Gohr * one will NOT be split up and will still contain $separator 4912dd3cbcSAndreas Gohr * 5012dd3cbcSAndreas Gohr * @param string $separator The boundary string 5112dd3cbcSAndreas Gohr * @param string $string The input string 5212dd3cbcSAndreas Gohr * @param int $limit The number of expected elements 5312dd3cbcSAndreas Gohr * @param mixed $default The value to use when filling up missing elements 5412dd3cbcSAndreas Gohr * @return array 558b19906eSAndreas Gohr * @see explode 5612dd3cbcSAndreas Gohr */ 5712dd3cbcSAndreas Gohrfunction sexplode($separator, $string, $limit, $default = null) 5812dd3cbcSAndreas Gohr{ 5912dd3cbcSAndreas Gohr return array_pad(explode($separator, $string, $limit), $limit, $default); 60d5197206Schris} 61d5197206Schris 62d5197206Schris/** 635b571377SAndreas Gohr * Checks if the given input is blank 645b571377SAndreas Gohr * 655b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 665b571377SAndreas Gohr * 6767234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 6867234204SAndreas Gohr * with a NULL value without warning. 6967234204SAndreas Gohr * 7067234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 7167234204SAndreas Gohr * 7267234204SAndreas Gohr * (isset($foo) && !blank($foo)) 7367234204SAndreas Gohr * (!isset($foo) || blank($foo)) 7467234204SAndreas Gohr * 755b571377SAndreas Gohr * @param $in 765b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 775b571377SAndreas Gohr * @return bool 785b571377SAndreas Gohr */ 79d868eb89SAndreas Gohrfunction blank(&$in, $trim = false) 80d868eb89SAndreas Gohr{ 815b571377SAndreas Gohr if (is_null($in)) return true; 8224870174SAndreas Gohr if (is_array($in)) return $in === []; 835b571377SAndreas Gohr if ($in === "\0") return true; 845b571377SAndreas Gohr if ($trim && trim($in) === '') return true; 85093fe67eSAndreas Gohr if ((string) $in !== '') return false; 865b571377SAndreas Gohr return empty($in); 875b571377SAndreas Gohr} 885b571377SAndreas Gohr 895b571377SAndreas Gohr/** 9002b0b681SAndreas Gohr * strips control characters (<32) from the given string 9102b0b681SAndreas Gohr * 9242ea7f44SGerrit Uitslag * @param string $string being stripped 93140cfbcdSGerrit Uitslag * @return string 948b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 958b19906eSAndreas Gohr * 9602b0b681SAndreas Gohr */ 97d868eb89SAndreas Gohrfunction stripctl($string) 98d868eb89SAndreas Gohr{ 9902b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 100d5197206Schris} 101d5197206Schris 102d5197206Schris/** 103634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 104634d7150SAndreas Gohr * 1058b19906eSAndreas Gohr * @return string 106634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 107634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 10842ea7f44SGerrit Uitslag * 1098b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 110634d7150SAndreas Gohr */ 111d868eb89SAndreas Gohrfunction getSecurityToken() 112d868eb89SAndreas Gohr{ 113585bf44eSChristopher Smith /** @var Input $INPUT */ 114585bf44eSChristopher Smith global $INPUT; 1153680e2cdSAndreas Gohr 1163680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 1173680e2cdSAndreas Gohr $session = session_id(); 1183680e2cdSAndreas Gohr 1193680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1203680e2cdSAndreas Gohr if (trim($user) == '' || trim($session) == '') return ''; 12124870174SAndreas Gohr return PassHash::hmac('md5', $session . $user, auth_cookiesalt()); 122634d7150SAndreas Gohr} 123634d7150SAndreas Gohr 124634d7150SAndreas Gohr/** 125634d7150SAndreas Gohr * Check the secret CSRF token 126140cfbcdSGerrit Uitslag * 127140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 128140cfbcdSGerrit Uitslag * @return bool success if the token matched 129634d7150SAndreas Gohr */ 130d868eb89SAndreas Gohrfunction checkSecurityToken($token = null) 131d868eb89SAndreas Gohr{ 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 * 147140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 14842ea7f44SGerrit Uitslag * @return string html of hidden form field 1498b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1508b19906eSAndreas Gohr * 151634d7150SAndreas Gohr */ 152d868eb89SAndreas Gohrfunction formSecurityToken($print = true) 153d868eb89SAndreas Gohr{ 1542404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="' . getSecurityToken() . '" /></div>' . "\n"; 1553272d797SAndreas Gohr if ($print) echo $ret; 156634d7150SAndreas Gohr return $ret; 157634d7150SAndreas Gohr} 158634d7150SAndreas Gohr 159634d7150SAndreas Gohr/** 1601015a57dSChristopher Smith * Determine basic information for a request of $id 16115fae107Sandi * 162140cfbcdSGerrit Uitslag * @param string $id pageid 163140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 164140cfbcdSGerrit Uitslag * @return array with info for a request of $id 165140cfbcdSGerrit Uitslag * 1668b19906eSAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 1678b19906eSAndreas Gohr * 1688b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 169f3f0262cSandi */ 170d868eb89SAndreas Gohrfunction basicinfo($id, $htmlClient = true) 171d868eb89SAndreas Gohr{ 172f3f0262cSandi global $USERINFO; 173585bf44eSChristopher Smith /* @var Input $INPUT */ 174585bf44eSChristopher Smith global $INPUT; 1756afe8dcaSchris 176c66972f2SAdrian Lang // set info about manager/admin status. 17724870174SAndreas Gohr $info = []; 178c66972f2SAdrian Lang $info['isadmin'] = false; 179c66972f2SAdrian Lang $info['ismanager'] = false; 180585bf44eSChristopher Smith if ($INPUT->server->has('REMOTE_USER')) { 181f3f0262cSandi $info['userinfo'] = $USERINFO; 1821015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 183585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 18417ee7f66SAndreas Gohr 185f8cc712eSAndreas Gohr if ($info['perm'] == AUTH_ADMIN) { 186f8cc712eSAndreas Gohr $info['isadmin'] = true; 187f8cc712eSAndreas Gohr $info['ismanager'] = true; 188f8cc712eSAndreas Gohr } elseif (auth_ismanager()) { 189f8cc712eSAndreas Gohr $info['ismanager'] = true; 190f8cc712eSAndreas Gohr } 191f8cc712eSAndreas Gohr 19217ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 193a58fcbbcSAndreas Gohr if (empty($info['userinfo']['name'])) { 194585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 19517ee7f66SAndreas Gohr } 196f3f0262cSandi } else { 1971015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 198ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 199f3f0262cSandi } 200f3f0262cSandi 2011015a57dSChristopher Smith $info['namespace'] = getNS($id); 2021015a57dSChristopher Smith 2031015a57dSChristopher Smith // mobile detection 2041015a57dSChristopher Smith if ($htmlClient) { 2051015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 2061015a57dSChristopher Smith } 2071015a57dSChristopher Smith 2081015a57dSChristopher Smith return $info; 2091015a57dSChristopher Smith} 2101015a57dSChristopher Smith 2111015a57dSChristopher Smith/** 2121015a57dSChristopher Smith * Return info about the current document as associative 2131015a57dSChristopher Smith * array. 2141015a57dSChristopher Smith * 215140cfbcdSGerrit Uitslag * @return array with info about current document 2164dc42f7fSGerrit Uitslag * @throws Exception 2174dc42f7fSGerrit Uitslag * 2184dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 2191015a57dSChristopher Smith */ 220d868eb89SAndreas Gohrfunction pageinfo() 221d868eb89SAndreas Gohr{ 2221015a57dSChristopher Smith global $ID; 2231015a57dSChristopher Smith global $REV; 2241015a57dSChristopher Smith global $RANGE; 2251015a57dSChristopher Smith global $lang; 2261015a57dSChristopher Smith 2271015a57dSChristopher Smith $info = basicinfo($ID); 2281015a57dSChristopher Smith 2291015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2301015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2311015a57dSChristopher Smith $info['id'] = $ID; 2321015a57dSChristopher Smith $info['rev'] = $REV; 2331015a57dSChristopher Smith 23475d66495SMichael Große $subManager = new SubscriberManager(); 23575d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2367e87a794SChristopher Smith 237f3f0262cSandi $info['locked'] = checklock($ID); 238317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 23979e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 24001c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2415ec96136SSatoshi Sahara 2422ca9d91cSBen Coburn if ($REV) { 2432ca9d91cSBen Coburn //check if current revision was meant 24401c9a118SAndreas Gohr if ($info['exists'] && ($info['currentrev'] == $REV)) { 2452ca9d91cSBen Coburn $REV = ''; 2467b3a6803SAndreas Gohr } elseif ($RANGE) { 2477b3a6803SAndreas Gohr //section editing does not work with old revisions! 2487b3a6803SAndreas Gohr $REV = ''; 2497b3a6803SAndreas Gohr $RANGE = ''; 2507b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2512ca9d91cSBen Coburn } else { 2522ca9d91cSBen Coburn //really use old revision 253317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 25479e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 255f3f0262cSandi } 256f3f0262cSandi } 257c112d578Sandi $info['rev'] = $REV; 258f3f0262cSandi if ($info['exists']) { 259252acce3SSatoshi Sahara $info['writable'] = (is_writable($info['filepath']) && $info['perm'] >= AUTH_EDIT); 260f3f0262cSandi } else { 261f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 262f3f0262cSandi } 26350e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 264f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 265f3f0262cSandi 26671726d78SBen Coburn //load page meta data 26771726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 26871726d78SBen Coburn 269652610a2Sandi //who's the editor 270047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 271652610a2Sandi if ($REV) { 272f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 27324870174SAndreas Gohr } elseif (!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; 28024870174SAndreas Gohr p_set_metadata($ID, ['last_change' => $revinfo]); 281cd00a034SBen Coburn } 282cd00a034SBen Coburn } 283cd00a034SBen Coburn //and check for an external edit 284cd00a034SBen Coburn if ($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 285cd00a034SBen Coburn // cached changelog line no longer valid 286cd00a034SBen Coburn $revinfo = false; 287cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 28824870174SAndreas Gohr p_set_metadata($ID, ['last_change' => $revinfo]); 289652610a2Sandi } 290bb4866bdSchris 2910a444b5aSPhy if ($revinfo !== false) { 292652610a2Sandi $info['ip'] = $revinfo['ip']; 293652610a2Sandi $info['user'] = $revinfo['user']; 294652610a2Sandi $info['sum'] = $revinfo['sum']; 29571726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 296ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 29759f257aeSchris 298252acce3SSatoshi Sahara $info['editor'] = $revinfo['user'] ?: $revinfo['ip']; 2990a444b5aSPhy } else { 3000a444b5aSPhy $info['ip'] = null; 3010a444b5aSPhy $info['user'] = null; 3020a444b5aSPhy $info['sum'] = null; 3030a444b5aSPhy $info['editor'] = null; 3040a444b5aSPhy } 305652610a2Sandi 306ee4c4a1bSAndreas Gohr // draft 30724870174SAndreas Gohr $draft = new Draft($ID, $info['client']); 3080aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 3090aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 310ee4c4a1bSAndreas Gohr } 311ee4c4a1bSAndreas Gohr 3121015a57dSChristopher Smith return $info; 3131015a57dSChristopher Smith} 3141015a57dSChristopher Smith 3151015a57dSChristopher Smith/** 3160c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3170c39d46cSMichael Große */ 318d868eb89SAndreas Gohrfunction jsinfo() 319d868eb89SAndreas Gohr{ 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 */ 338d868eb89SAndreas Gohrfunction mediainfo() 339d868eb89SAndreas Gohr{ 3401015a57dSChristopher Smith global $NS; 3411015a57dSChristopher Smith global $IMG; 3421015a57dSChristopher Smith 3431015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3441015a57dSChristopher Smith $info['image'] = $IMG; 3451c548ebeSAndreas Gohr 346f3f0262cSandi return $info; 347f3f0262cSandi} 348f3f0262cSandi 349f3f0262cSandi/** 3502684e50aSAndreas Gohr * Build an string of URL parameters 3512684e50aSAndreas Gohr * 3526cc6a0d2SAndreas Gohr * @see http_build_query() 3536cc6a0d2SAndreas Gohr * @param array|object $params the data to encode 354140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 355140cfbcdSGerrit Uitslag * @return string query string 3568b19906eSAndreas Gohr * 3572684e50aSAndreas Gohr */ 358d868eb89SAndreas Gohrfunction buildURLparams($params, $sep = '&') 359d868eb89SAndreas Gohr{ 3606cc6a0d2SAndreas Gohr return http_build_query($params, '', $sep, PHP_QUERY_RFC3986); 3612684e50aSAndreas Gohr} 3622684e50aSAndreas Gohr 3632684e50aSAndreas Gohr/** 3642684e50aSAndreas Gohr * Build an string of html tag attributes 3652684e50aSAndreas Gohr * 3667bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3677bff22c0SAndreas Gohr * 368140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 369246d3337SMichael Große * @param bool $skipEmptyStrings skip empty string values? 370140cfbcdSGerrit Uitslag * @return string 3718b19906eSAndreas Gohr * @author Andreas Gohr 3728b19906eSAndreas Gohr * 3732684e50aSAndreas Gohr */ 374d868eb89SAndreas Gohrfunction buildAttributes($params, $skipEmptyStrings = false) 375d868eb89SAndreas Gohr{ 3762684e50aSAndreas Gohr $url = ''; 3779063ec14SAdrian Lang $white = false; 3782684e50aSAndreas Gohr foreach ($params as $key => $val) { 3792401f18dSSyntaxseed if ($key[0] == '_') continue; 380246d3337SMichael Große if ($val === '' && $skipEmptyStrings) continue; 3819063ec14SAdrian Lang if ($white) $url .= ' '; 3827bff22c0SAndreas Gohr 3832684e50aSAndreas Gohr $url .= $key . '="'; 384f7711f2bSAndreas Gohr $url .= hsc($val); 3852684e50aSAndreas Gohr $url .= '"'; 3869063ec14SAdrian Lang $white = true; 3872684e50aSAndreas Gohr } 3882684e50aSAndreas Gohr return $url; 3892684e50aSAndreas Gohr} 3902684e50aSAndreas Gohr 3912684e50aSAndreas Gohr/** 39215fae107Sandi * This builds the breadcrumb trail and returns it as array 39315fae107Sandi * 3948b19906eSAndreas Gohr * @return string[] with the data: array(pageid=>name, ... ) 39515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 396140cfbcdSGerrit Uitslag * 397f3f0262cSandi */ 398d868eb89SAndreas Gohrfunction breadcrumbs() 399d868eb89SAndreas Gohr{ 4008746e727Sandi // we prepare the breadcrumbs early for quick session closing 4018746e727Sandi static $crumbs = null; 4028746e727Sandi if ($crumbs != null) return $crumbs; 4038746e727Sandi 404f3f0262cSandi global $ID; 405f3f0262cSandi global $ACT; 406f3f0262cSandi global $conf; 4070ea5ebb4SB_S666 global $INFO; 408f3f0262cSandi 409f3f0262cSandi //first visit? 41024870174SAndreas Gohr $crumbs = $_SESSION[DOKU_COOKIE]['bc'] ?? []; 4115603d3c1SHenry Pan //we only save on show and existing visible readable wiki documents 412a77f5846Sjan $file = wikiFN($ID); 4135603d3c1SHenry Pan if ($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) { 414e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 415f3f0262cSandi return $crumbs; 416f3f0262cSandi } 417a77f5846Sjan 418a77f5846Sjan // page names 4191a84a0f3SAnika Henke $name = noNSorNS($ID); 420fe9ec250SChris Smith if (useHeading('navigation')) { 421a77f5846Sjan // get page title 42267c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 423a77f5846Sjan if ($title) { 424a77f5846Sjan $name = $title; 425a77f5846Sjan } 426a77f5846Sjan } 427a77f5846Sjan 428f3f0262cSandi //remove ID from array 429a77f5846Sjan if (isset($crumbs[$ID])) { 430a77f5846Sjan unset($crumbs[$ID]); 431f3f0262cSandi } 432f3f0262cSandi 433f3f0262cSandi //add to array 434a77f5846Sjan $crumbs[$ID] = $name; 435f3f0262cSandi //reduce size 436f3f0262cSandi while (count($crumbs) > $conf['breadcrumbs']) { 437f3f0262cSandi array_shift($crumbs); 438f3f0262cSandi } 439f3f0262cSandi //save to session 440e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 441f3f0262cSandi return $crumbs; 442f3f0262cSandi} 443f3f0262cSandi 444f3f0262cSandi/** 44515fae107Sandi * Filter for page IDs 44615fae107Sandi * 447f3f0262cSandi * This is run on a ID before it is outputted somewhere 448f3f0262cSandi * currently used to replace the colon with something else 449907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 450907f24f7SAndreas Gohr * 451977aa967SAndreas Gohr * See discussions at https://github.com/dokuwiki/dokuwiki/pull/84 and 452977aa967SAndreas Gohr * https://github.com/dokuwiki/dokuwiki/pull/173 why we use a whitelist of 453907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 45415fae107Sandi * 45549c713a3Sandi * Urlencoding is ommitted when the second parameter is false 45649c713a3Sandi * 457140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 458140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 459140cfbcdSGerrit Uitslag * @return string 4608b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 4618b19906eSAndreas Gohr * 462f3f0262cSandi */ 463d868eb89SAndreas Gohrfunction idfilter($id, $ue = true) 464d868eb89SAndreas Gohr{ 465f3f0262cSandi global $conf; 466585bf44eSChristopher Smith /* @var Input $INPUT */ 467585bf44eSChristopher Smith global $INPUT; 468585bf44eSChristopher Smith 469bf8f8509SAndreas Gohr $id = (string)$id; 470bf8f8509SAndreas Gohr 471f3f0262cSandi if ($conf['useslash'] && $conf['userewrite']) { 472f3f0262cSandi $id = strtr($id, ':', '/'); 4737d34963bSAndreas Gohr } elseif ( 4746c16a3a9Sfiwswe str_starts_with(strtoupper(PHP_OS), 'WIN') && 47558bedc8aSborekb $conf['userewrite'] && 476093fe67eSAndreas Gohr !str_contains($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') 4773272d797SAndreas Gohr ) { 478f3f0262cSandi $id = strtr($id, ':', ';'); 479f3f0262cSandi } 48049c713a3Sandi if ($ue) { 481b6c6979fSAndreas Gohr $id = rawurlencode($id); 482f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 483edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 484f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 48549c713a3Sandi } 486f3f0262cSandi return $id; 487f3f0262cSandi} 488f3f0262cSandi 489f3f0262cSandi/** 490ed7b5f09Sandi * This builds a link to a wikipage 49115fae107Sandi * 4924bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4936c7843b5Sandi * 4944bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4954bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4964bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4974bc480e5SAndreas Gohr * @param string $separator parameter separator 4984bc480e5SAndreas Gohr * @return string 4998b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 5008b19906eSAndreas Gohr * 501f3f0262cSandi */ 502d868eb89SAndreas Gohrfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') 503d868eb89SAndreas Gohr{ 504f3f0262cSandi global $conf; 50516f15a81SDominik Eckelmann if (is_array($urlParameters)) { 5064bde2196Slisps if (isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 50764159a61SAndreas Gohr if (isset($urlParameters['at']) && $conf['date_at_format']) { 50864159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 50964159a61SAndreas Gohr } 51016f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 5116de3759aSAndreas Gohr } else { 51216f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 5136de3759aSAndreas Gohr } 51416f15a81SDominik Eckelmann if ($id === '') { 51516f15a81SDominik Eckelmann $id = $conf['start']; 51616f15a81SDominik Eckelmann } 517f3f0262cSandi $id = idfilter($id); 51816f15a81SDominik Eckelmann if ($absolute) { 519ed7b5f09Sandi $xlink = DOKU_URL; 520ed7b5f09Sandi } else { 521ed7b5f09Sandi $xlink = DOKU_BASE; 522ed7b5f09Sandi } 523f3f0262cSandi 5246c7843b5Sandi if ($conf['userewrite'] == 2) { 5256c7843b5Sandi $xlink .= DOKU_SCRIPT . '/' . $id; 52616f15a81SDominik Eckelmann if ($urlParameters) $xlink .= '?' . $urlParameters; 5276c7843b5Sandi } elseif ($conf['userewrite']) { 528f3f0262cSandi $xlink .= $id; 52916f15a81SDominik Eckelmann if ($urlParameters) $xlink .= '?' . $urlParameters; 53040b5fb5bSPhy } elseif ($id !== '') { 5316c7843b5Sandi $xlink .= DOKU_SCRIPT . '?id=' . $id; 53216f15a81SDominik Eckelmann if ($urlParameters) $xlink .= $separator . $urlParameters; 533bce3726dSAndreas Gohr } else { 534bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 53516f15a81SDominik Eckelmann if ($urlParameters) $xlink .= '?' . $urlParameters; 536f3f0262cSandi } 537f3f0262cSandi 538f3f0262cSandi return $xlink; 539f3f0262cSandi} 540f3f0262cSandi 541f3f0262cSandi/** 542f5c2808fSBen Coburn * This builds a link to an alternate page format 543f5c2808fSBen Coburn * 544f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 545f5c2808fSBen Coburn * 5464bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5474bc480e5SAndreas Gohr * @param string $format the export renderer to use 5484bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5494bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5504bc480e5SAndreas Gohr * @param string $sep parameter separator 5514bc480e5SAndreas Gohr * @return string 5528b19906eSAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 553f5c2808fSBen Coburn */ 554d868eb89SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') 555d868eb89SAndreas Gohr{ 556f5c2808fSBen Coburn global $conf; 5574bc480e5SAndreas Gohr if (is_array($urlParameters)) { 5584bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 559f5c2808fSBen Coburn } else { 5604bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 561f5c2808fSBen Coburn } 562f5c2808fSBen Coburn 563f5c2808fSBen Coburn $format = rawurlencode($format); 564f5c2808fSBen Coburn $id = idfilter($id); 565f5c2808fSBen Coburn if ($abs) { 566f5c2808fSBen Coburn $xlink = DOKU_URL; 567f5c2808fSBen Coburn } else { 568f5c2808fSBen Coburn $xlink = DOKU_BASE; 569f5c2808fSBen Coburn } 570f5c2808fSBen Coburn 571f5c2808fSBen Coburn if ($conf['userewrite'] == 2) { 572f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT . '/' . $id . '?do=export_' . $format; 5734bc480e5SAndreas Gohr if ($urlParameters) $xlink .= $sep . $urlParameters; 574f5c2808fSBen Coburn } elseif ($conf['userewrite'] == 1) { 575f5c2808fSBen Coburn $xlink .= '_export/' . $format . '/' . $id; 5764bc480e5SAndreas Gohr if ($urlParameters) $xlink .= '?' . $urlParameters; 577f5c2808fSBen Coburn } else { 578f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT . '?do=export_' . $format . $sep . 'id=' . $id; 5794bc480e5SAndreas Gohr if ($urlParameters) $xlink .= $sep . $urlParameters; 580f5c2808fSBen Coburn } 581f5c2808fSBen Coburn 582f5c2808fSBen Coburn return $xlink; 583f5c2808fSBen Coburn} 584f5c2808fSBen Coburn 585f5c2808fSBen Coburn/** 5866de3759aSAndreas Gohr * Build a link to a media file 5876de3759aSAndreas Gohr * 5886de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5898c08db0aSAndreas Gohr * 5908c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5918c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5928c08db0aSAndreas Gohr * 5933272d797SAndreas Gohr * @param string $id the media file id or URL 5943272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5953272d797SAndreas Gohr * @param bool $direct link to detail page if false 5963272d797SAndreas Gohr * @param string $sep URL parameter separator 5973272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5983272d797SAndreas Gohr * @return string 5996de3759aSAndreas Gohr */ 600d868eb89SAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) 601d868eb89SAndreas Gohr{ 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 61024870174SAndreas Gohr $w = $more['w'] ?? null; 61124870174SAndreas Gohr $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 { 62324870174SAndreas Gohr $matches = []; 624cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/', $more, $matches, PREG_SET_ORDER) || $isexternalimage) { 62524870174SAndreas Gohr $resize = ['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 } 66024870174SAndreas Gohr } elseif ($conf['userewrite'] == 1) { 6616de3759aSAndreas Gohr $script = '_detail'; 6626de3759aSAndreas Gohr } else { 6636de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6646de3759aSAndreas Gohr } 6656de3759aSAndreas Gohr 6666de3759aSAndreas Gohr // build URL based on rewrite mode 6676de3759aSAndreas Gohr if ($conf['userewrite']) { 6686de3759aSAndreas Gohr $xlink .= $script . '/' . $id; 6696de3759aSAndreas Gohr if ($more) $xlink .= '?' . $more; 67024870174SAndreas Gohr } elseif ($more) { 671a99d3236SEsther Brunner $xlink .= $script . '?' . $more; 672b174aeaeSchris $xlink .= $sep . 'media=' . $id; 6736de3759aSAndreas Gohr } else { 674a99d3236SEsther Brunner $xlink .= $script . '?media=' . $id; 6756de3759aSAndreas Gohr } 6766de3759aSAndreas Gohr 6776de3759aSAndreas Gohr return $xlink; 6786de3759aSAndreas Gohr} 6796de3759aSAndreas Gohr 6806de3759aSAndreas Gohr/** 68125ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 68215fae107Sandi * 68325ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 68425ca5b17SAndreas Gohr * 6858b19906eSAndreas Gohr * @return string 68615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 687140cfbcdSGerrit Uitslag * 688f3f0262cSandi */ 689d868eb89SAndreas Gohrfunction script() 690d868eb89SAndreas Gohr{ 691ed7b5f09Sandi return DOKU_BASE . DOKU_SCRIPT; 692f3f0262cSandi} 693f3f0262cSandi 694f3f0262cSandi/** 69515fae107Sandi * Spamcheck against wordlist 69615fae107Sandi * 697f3f0262cSandi * Checks the wikitext against a list of blocked expressions 698f3f0262cSandi * returns true if the text contains any bad words 69915fae107Sandi * 700e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 701e403cc58SMichael Klier * 702e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 703e403cc58SMichael Klier * and gain information about the user who was blocked. 704e403cc58SMichael Klier * 705e403cc58SMichael Klier * Event data: 706e403cc58SMichael Klier * data['matches'] - array of matches 707e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 708e403cc58SMichael Klier * [ip] - ip address 709e403cc58SMichael Klier * [user] - username (if logged in) 710e403cc58SMichael Klier * [mail] - mail address (if logged in) 711e403cc58SMichael Klier * [name] - real name (if logged in) 712e403cc58SMichael Klier * 7138b19906eSAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 7148b19906eSAndreas Gohr * @return bool - true if a spam word was found 71515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 7166dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 717140cfbcdSGerrit Uitslag * 718f3f0262cSandi */ 719d868eb89SAndreas Gohrfunction checkwordblock($text = '') 720d868eb89SAndreas Gohr{ 721f3f0262cSandi global $TEXT; 7226dffa0e0SAndreas Gohr global $PRE; 7236dffa0e0SAndreas Gohr global $SUF; 724e0086ca2SAndreas Gohr global $SUM; 725f3f0262cSandi global $conf; 726e403cc58SMichael Klier global $INFO; 727585bf44eSChristopher Smith /* @var Input $INPUT */ 728585bf44eSChristopher Smith global $INPUT; 729f3f0262cSandi 730f3f0262cSandi if (!$conf['usewordblock']) return false; 731f3f0262cSandi 732e0086ca2SAndreas Gohr if (!$text) $text = "$PRE $TEXT $SUF $SUM"; 7336dffa0e0SAndreas Gohr 734041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 73564159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 73664159a61SAndreas Gohr $text = preg_replace( 73764159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 73864159a61SAndreas Gohr '\1http://\2 \2\3', 73964159a61SAndreas Gohr $text 74064159a61SAndreas Gohr ); 74164159a61SAndreas Gohr // phpcs:enable 742041d1964SAndreas Gohr 743b9ac8716Schris $wordblocks = getWordblocks(); 744a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7453e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 746a51d08efSAndreas Gohr $chunksize = 200; 74764259528SAndreas Gohr 748b9ac8716Schris while ($blocks = array_splice($wordblocks, 0, $chunksize)) { 74924870174SAndreas Gohr $re = []; 75049eb6e38SAndreas Gohr // build regexp from blocks 751f3f0262cSandi foreach ($blocks as $block) { 752f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 753f3f0262cSandi $block = trim($block); 754f3f0262cSandi if (empty($block)) continue; 755f3f0262cSandi $re[] = $block; 756f3f0262cSandi } 75724870174SAndreas Gohr if (count($re) && preg_match('#(' . implode('|', $re) . ')#si', $text, $matches)) { 758e403cc58SMichael Klier // prepare event data 75924870174SAndreas Gohr $data = []; 760e403cc58SMichael Klier $data['matches'] = $matches; 761585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 762585bf44eSChristopher Smith if ($INPUT->server->str('REMOTE_USER')) { 763585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 764e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 765e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 766e403cc58SMichael Klier } 76724870174SAndreas Gohr $callback = static fn() => true; 768cbb44eabSAndreas Gohr return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 769b9ac8716Schris } 770703f6fdeSandi } 771f3f0262cSandi return false; 772f3f0262cSandi} 773f3f0262cSandi 774f3f0262cSandi/** 775a7580321SZebra North * Return the IP of the client. 77615fae107Sandi * 777a7580321SZebra North * The IP is sourced from, in order of preference: 77815fae107Sandi * 779a7580321SZebra North * - The X-Real-IP header if $conf[realip] is true. 780d5dd5d1bSAndreas Gohr * - The X-Forwarded-For header if all the proxies are trusted by $conf[trustedproxies]. 781a7580321SZebra North * - The TCP/IP connection remote address. 782a7580321SZebra North * - 0.0.0.0 if all else fails. 7836d8affe6SAndreas Gohr * 784a7580321SZebra North * The 'realip' config value should only be set to true if the X-Real-IP header 785a7580321SZebra North * is being added by the web server, otherwise it may be spoofed by the client. 786140cfbcdSGerrit Uitslag * 787d5dd5d1bSAndreas Gohr * The 'trustedproxies' setting must not allow any IP, otherwise the X-Forwarded-For 788a7580321SZebra North * may be spoofed by the client. 789a7580321SZebra North * 790608cdefcSZebra North * @param bool $single If set only a single IP is returned. 791608cdefcSZebra North * 792a7580321SZebra North * @return string Returns an IP address if 'single' is true, or a comma-separated list 793a7580321SZebra North * of IP addresses otherwise. 7942f828abfSAndreas Gohr * @author Zebra North <mrzebra@mrzebra.co.uk> 7952f828abfSAndreas Gohr * 796f3f0262cSandi */ 7972f828abfSAndreas Gohrfunction clientIP($single = false) 7982f828abfSAndreas Gohr{ 799a7580321SZebra North // Return the first IP in single mode, or all the IPs. 80098b599a6Ssplitbrain return $single ? Ip::clientIp() : implode(',', Ip::clientIps()); 801f3f0262cSandi} 802f3f0262cSandi 803f3f0262cSandi/** 8041c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8051c548ebeSAndreas Gohr * 8061c548ebeSAndreas Gohr * Adapted from the example code at url below 8071c548ebeSAndreas Gohr * 8081c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 809140cfbcdSGerrit Uitslag * 81064159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 811140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8121c548ebeSAndreas Gohr */ 813d868eb89SAndreas Gohrfunction clientismobile() 814d868eb89SAndreas Gohr{ 815585bf44eSChristopher Smith /* @var Input $INPUT */ 816585bf44eSChristopher Smith global $INPUT; 8171c548ebeSAndreas Gohr 818585bf44eSChristopher Smith if ($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8191c548ebeSAndreas Gohr 820585bf44eSChristopher Smith if (preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8211c548ebeSAndreas Gohr 822585bf44eSChristopher Smith if (!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8231c548ebeSAndreas Gohr 82424870174SAndreas Gohr $uamatches = implode( 82564159a61SAndreas Gohr '|', 82664159a61SAndreas Gohr [ 82764159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 82864159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 82964159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 83064159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 83164159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 83264159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 83364159a61SAndreas Gohr '\d\d\di', 'moto' 83464159a61SAndreas Gohr ] 83564159a61SAndreas Gohr ); 8361c548ebeSAndreas Gohr 837585bf44eSChristopher Smith if (preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8381c548ebeSAndreas Gohr 8391c548ebeSAndreas Gohr return false; 8401c548ebeSAndreas Gohr} 8411c548ebeSAndreas Gohr 8421c548ebeSAndreas Gohr/** 8436efc45a2SDmitry Katsubo * check if a given link is interwiki link 8446efc45a2SDmitry Katsubo * 8456efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8466efc45a2SDmitry Katsubo * @return bool 8476efc45a2SDmitry Katsubo */ 848d868eb89SAndreas Gohrfunction link_isinterwiki($link) 849d868eb89SAndreas Gohr{ 8506efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u', $link)) return true; 8516efc45a2SDmitry Katsubo return false; 8526efc45a2SDmitry Katsubo} 8536efc45a2SDmitry Katsubo 8546efc45a2SDmitry Katsubo/** 85563211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 85663211f61SGlen Harris * 85722ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 85822ef1e32SAndreas Gohr * 8593272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 8603272d797SAndreas Gohr * @return string a comma separated list of hostnames 8618b19906eSAndreas Gohr * @author Glen Harris <astfgl@iamnota.org> 8628b19906eSAndreas Gohr * 86363211f61SGlen Harris */ 864d868eb89SAndreas Gohrfunction gethostsbyaddrs($ips) 865d868eb89SAndreas Gohr{ 86622ef1e32SAndreas Gohr global $conf; 86722ef1e32SAndreas Gohr if (!$conf['dnslookups']) return $ips; 86822ef1e32SAndreas Gohr 86924870174SAndreas Gohr $hosts = []; 87063211f61SGlen Harris $ips = explode(',', $ips); 871551a720fSMichael Klier 872551a720fSMichael Klier if (is_array($ips)) { 8733886270dSAndreas Gohr foreach ($ips as $ip) { 874551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 87563211f61SGlen Harris } 87624870174SAndreas Gohr return implode(',', $hosts); 877551a720fSMichael Klier } else { 878551a720fSMichael Klier return gethostbyaddr(trim($ips)); 879551a720fSMichael Klier } 88063211f61SGlen Harris} 88163211f61SGlen Harris 88263211f61SGlen Harris/** 88315fae107Sandi * Checks if a given page is currently locked. 88415fae107Sandi * 885f3f0262cSandi * removes stale lockfiles 88615fae107Sandi * 887140cfbcdSGerrit Uitslag * @param string $id page id 888140cfbcdSGerrit Uitslag * @return bool page is locked? 8898b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8908b19906eSAndreas Gohr * 891f3f0262cSandi */ 892d868eb89SAndreas Gohrfunction checklock($id) 893d868eb89SAndreas Gohr{ 894f3f0262cSandi global $conf; 895585bf44eSChristopher Smith /* @var Input $INPUT */ 896585bf44eSChristopher Smith global $INPUT; 897585bf44eSChristopher Smith 898c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 899f3f0262cSandi 900f3f0262cSandi //no lockfile 90179e79377SAndreas Gohr if (!file_exists($lock)) return false; 902f3f0262cSandi 903f3f0262cSandi //lockfile expired 904f3f0262cSandi if ((time() - filemtime($lock)) > $conf['locktime']) { 905d8186216SBen Coburn @unlink($lock); 906f3f0262cSandi return false; 907f3f0262cSandi } 908f3f0262cSandi 909f3f0262cSandi //my own lock 9105f21556dSDamien Regad [$ip, $session] = sexplode("\n", io_readFile($lock), 2); 91124870174SAndreas Gohr if ($ip == $INPUT->server->str('REMOTE_USER') || (session_id() && $session === session_id())) { 912f3f0262cSandi return false; 913f3f0262cSandi } 914f3f0262cSandi 915f3f0262cSandi return $ip; 916f3f0262cSandi} 917f3f0262cSandi 918f3f0262cSandi/** 91915fae107Sandi * Lock a page for editing 92015fae107Sandi * 9218b19906eSAndreas Gohr * @param string $id page id to lock 92215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 923140cfbcdSGerrit Uitslag * 924f3f0262cSandi */ 925d868eb89SAndreas Gohrfunction lock($id) 926d868eb89SAndreas Gohr{ 927544ed901SDaniel Calviño Sánchez global $conf; 928585bf44eSChristopher Smith /* @var Input $INPUT */ 929585bf44eSChristopher Smith global $INPUT; 930544ed901SDaniel Calviño Sánchez 931544ed901SDaniel Calviño Sánchez if ($conf['locktime'] == 0) { 932544ed901SDaniel Calviño Sánchez return; 933544ed901SDaniel Calviño Sánchez } 934544ed901SDaniel Calviño Sánchez 935c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 936585bf44eSChristopher Smith if ($INPUT->server->str('REMOTE_USER')) { 937585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 938f3f0262cSandi } else { 93985fef7e2SAndreas Gohr io_saveFile($lock, clientIP() . "\n" . session_id()); 940f3f0262cSandi } 941f3f0262cSandi} 942f3f0262cSandi 943f3f0262cSandi/** 94415fae107Sandi * Unlock a page if it was locked by the user 945f3f0262cSandi * 9463272d797SAndreas Gohr * @param string $id page id to unlock 94715fae107Sandi * @return bool true if a lock was removed 9488b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9498b19906eSAndreas Gohr * 950f3f0262cSandi */ 951d868eb89SAndreas Gohrfunction unlock($id) 952d868eb89SAndreas Gohr{ 953585bf44eSChristopher Smith /* @var Input $INPUT */ 954585bf44eSChristopher Smith global $INPUT; 955585bf44eSChristopher Smith 956c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 95779e79377SAndreas Gohr if (file_exists($lock)) { 95824870174SAndreas Gohr @[$ip, $session] = explode("\n", io_readFile($lock)); 959c0dd3914SAdaKaleh if ($ip == $INPUT->server->str('REMOTE_USER') || $session == session_id()) { 960f3f0262cSandi @unlink($lock); 961f3f0262cSandi return true; 962f3f0262cSandi } 963f3f0262cSandi } 964f3f0262cSandi return false; 965f3f0262cSandi} 966f3f0262cSandi 967f3f0262cSandi/** 968f3f0262cSandi * convert line ending to unix format 969f3f0262cSandi * 9706db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 9716db7468bSAndreas Gohr * 9728b19906eSAndreas Gohr * @param string $text 9738b19906eSAndreas Gohr * @return string 97415fae107Sandi * @see formText() for 2crlf conversion 97515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 976140cfbcdSGerrit Uitslag * 977f3f0262cSandi */ 978d868eb89SAndreas Gohrfunction cleanText($text) 979d868eb89SAndreas Gohr{ 980f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 9816db7468bSAndreas Gohr 9826db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 9836db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 9846db7468bSAndreas Gohr // but might actually fix the problem in many cases 98553c68e5cSAndreas Gohr if (!Clean::isUtf8($text)) $text = Conversion::fromLatin1($text); 9866db7468bSAndreas Gohr 987f3f0262cSandi return $text; 988f3f0262cSandi} 989f3f0262cSandi 990f3f0262cSandi/** 991f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 992f3f0262cSandi * It also converts line endings to Windows format which is 993f3f0262cSandi * pseudo standard for webforms. 994f3f0262cSandi * 9958b19906eSAndreas Gohr * @param string $text 9968b19906eSAndreas Gohr * @return string 99715fae107Sandi * @see cleanText() for 2unix conversion 99815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 999140cfbcdSGerrit Uitslag * 1000f3f0262cSandi */ 1001d868eb89SAndreas Gohrfunction formText($text) 1002d868eb89SAndreas Gohr{ 1003a46a37efSAndreas Gohr $text = str_replace("\012", "\015\012", $text ?? ''); 1004f3f0262cSandi return htmlspecialchars($text); 1005f3f0262cSandi} 1006f3f0262cSandi 1007f3f0262cSandi/** 100815fae107Sandi * Returns the specified local text in raw format 100915fae107Sandi * 1010140cfbcdSGerrit Uitslag * @param string $id page id 1011140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1012140cfbcdSGerrit Uitslag * @return string 10138b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10148b19906eSAndreas Gohr * 1015f3f0262cSandi */ 1016d868eb89SAndreas Gohrfunction rawLocale($id, $ext = 'txt') 1017d868eb89SAndreas Gohr{ 10182adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1019f3f0262cSandi} 1020f3f0262cSandi 1021f3f0262cSandi/** 1022f3f0262cSandi * Returns the raw WikiText 102315fae107Sandi * 1024140cfbcdSGerrit Uitslag * @param string $id page id 1025e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1026140cfbcdSGerrit Uitslag * @return string 10278b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10288b19906eSAndreas Gohr * 1029f3f0262cSandi */ 1030d868eb89SAndreas Gohrfunction rawWiki($id, $rev = '') 1031d868eb89SAndreas Gohr{ 1032cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1033f3f0262cSandi} 1034f3f0262cSandi 1035f3f0262cSandi/** 10367146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10377146cee2SAndreas Gohr * 10387b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 1039140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1040140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10418b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10428b19906eSAndreas Gohr * 10437146cee2SAndreas Gohr */ 1044d868eb89SAndreas Gohrfunction pageTemplate($id) 1045d868eb89SAndreas Gohr{ 1046a15ce62dSEsther Brunner global $conf; 1047e29549feSAndreas Gohr 1048fe17917eSAdrian Lang if (is_array($id)) $id = $id[0]; 1049e29549feSAndreas Gohr 10507b84afa2SAndreas Gohr // prepare initial event data 105124870174SAndreas Gohr $data = [ 10527b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10537b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10547b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 105524870174SAndreas Gohr 'doreplace' => true, 105624870174SAndreas Gohr ]; 10577b84afa2SAndreas Gohr 1058e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_PAGETPL_LOAD', $data); 10597b84afa2SAndreas Gohr if ($evt->advise_before(true)) { 10607b84afa2SAndreas Gohr // the before event might have loaded the content already 10617b84afa2SAndreas Gohr if (empty($data['tpl'])) { 10627b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10637b84afa2SAndreas Gohr if (empty($data['tplfile'])) { 1064fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 106579e79377SAndreas Gohr if (file_exists($path . '/_template.txt')) { 10667b84afa2SAndreas Gohr $data['tplfile'] = $path . '/_template.txt'; 1067e29549feSAndreas Gohr } else { 1068e29549feSAndreas Gohr // search upper namespaces for templates 1069e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1070e29549feSAndreas Gohr while (strlen($path) >= $len) { 107179e79377SAndreas Gohr if (file_exists($path . '/__template.txt')) { 10727b84afa2SAndreas Gohr $data['tplfile'] = $path . '/__template.txt'; 1073e29549feSAndreas Gohr break; 1074e29549feSAndreas Gohr } 1075e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1076e29549feSAndreas Gohr } 1077e29549feSAndreas Gohr } 10787b84afa2SAndreas Gohr } 10797b84afa2SAndreas Gohr // load the content 10803d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 10817b84afa2SAndreas Gohr } 1082a1bbd05bSMichael Hamann if ($data['doreplace']) parsePageTemplate($data); 10837b84afa2SAndreas Gohr } 10847b84afa2SAndreas Gohr $evt->advise_after(); 10857b84afa2SAndreas Gohr unset($evt); 10867b84afa2SAndreas Gohr 1087fe17917eSAdrian Lang return $data['tpl']; 10882b1223ecSAdrian Lang} 10892b1223ecSAdrian Lang 10902b1223ecSAdrian Lang/** 10912b1223ecSAdrian Lang * Performs common page template replacements 10927b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 10932b1223ecSAdrian Lang * 1094140cfbcdSGerrit Uitslag * @param array $data array with event data 1095140cfbcdSGerrit Uitslag * @return string 10968b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 10978b19906eSAndreas Gohr * 10982b1223ecSAdrian Lang */ 1099d868eb89SAndreas Gohrfunction parsePageTemplate(&$data) 1100d868eb89SAndreas Gohr{ 11013272d797SAndreas Gohr /** 11023272d797SAndreas Gohr * @var string $id the id of the page to be created 11033272d797SAndreas Gohr * @var string $tpl the text used as template 11043272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11053272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11063272d797SAndreas Gohr */ 1107fe17917eSAdrian Lang extract($data); 1108fe17917eSAdrian Lang 1109b856f7dfSAdrian Lang global $USERINFO; 1110bce53b1fSAdrian Lang global $conf; 1111585bf44eSChristopher Smith /* @var Input $INPUT */ 1112585bf44eSChristopher Smith global $INPUT; 1113e29549feSAndreas Gohr 1114e29549feSAndreas Gohr // replace placeholders 111526ece5a7SAndreas Gohr $file = noNS($id); 111637c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 111726ece5a7SAndreas Gohr 11183272d797SAndreas Gohr $tpl = str_replace( 111924870174SAndreas Gohr [ 112026ece5a7SAndreas Gohr '@ID@', 112126ece5a7SAndreas Gohr '@NS@', 11228a7bcf66SShota Miyazaki '@CURNS@', 1123a3db0ab0SSimon Lees '@!CURNS@', 1124a3db0ab0SSimon Lees '@!!CURNS@', 1125a3db0ab0SSimon Lees '@!CURNS!@', 112626ece5a7SAndreas Gohr '@FILE@', 112726ece5a7SAndreas Gohr '@!FILE@', 112826ece5a7SAndreas Gohr '@!FILE!@', 112926ece5a7SAndreas Gohr '@PAGE@', 113026ece5a7SAndreas Gohr '@!PAGE@', 113126ece5a7SAndreas Gohr '@!!PAGE@', 113226ece5a7SAndreas Gohr '@!PAGE!@', 113326ece5a7SAndreas Gohr '@USER@', 113426ece5a7SAndreas Gohr '@NAME@', 113526ece5a7SAndreas Gohr '@MAIL@', 113624870174SAndreas Gohr '@DATE@' 113724870174SAndreas Gohr ], 113824870174SAndreas Gohr [ 113926ece5a7SAndreas Gohr $id, 114026ece5a7SAndreas Gohr getNS($id), 11418a7bcf66SShota Miyazaki curNS($id), 114224870174SAndreas Gohr PhpString::ucfirst(curNS($id)), 114324870174SAndreas Gohr PhpString::ucwords(curNS($id)), 114424870174SAndreas Gohr PhpString::strtoupper(curNS($id)), 114526ece5a7SAndreas Gohr $file, 114624870174SAndreas Gohr PhpString::ucfirst($file), 114724870174SAndreas Gohr PhpString::strtoupper($file), 114826ece5a7SAndreas Gohr $page, 114924870174SAndreas Gohr PhpString::ucfirst($page), 115024870174SAndreas Gohr PhpString::ucwords($page), 115124870174SAndreas Gohr PhpString::strtoupper($page), 1152585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11533e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 11543e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 115524870174SAndreas Gohr $conf['dformat'] 115624870174SAndreas Gohr ], 115724870174SAndreas Gohr $tpl 11583272d797SAndreas Gohr ); 115926ece5a7SAndreas Gohr 11607d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1161bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1162bad6fc0dSAndreas Gohr '/%./', 116324870174SAndreas Gohr static fn($m) => dformat(null, $m[0]), 1164bad6fc0dSAndreas Gohr $tpl 1165bad6fc0dSAndreas Gohr ); 1166d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1167a15ce62dSEsther Brunner return $tpl; 11687146cee2SAndreas Gohr} 11697146cee2SAndreas Gohr 11707146cee2SAndreas Gohr/** 117115fae107Sandi * Returns the raw Wiki Text in three slices. 117215fae107Sandi * 117315fae107Sandi * The range parameter needs to have the form "from-to" 117415cfe303Sandi * and gives the range of the section in bytes - no 117515cfe303Sandi * UTF-8 awareness is needed. 1176f3f0262cSandi * The returned order is prefix, section and suffix. 117715fae107Sandi * 1178140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1179140cfbcdSGerrit Uitslag * @param string $id page id 1180140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 118142ea7f44SGerrit Uitslag * @return string[] with three slices 11828b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11838b19906eSAndreas Gohr * 1184f3f0262cSandi */ 1185d868eb89SAndreas Gohrfunction rawWikiSlices($range, $id, $rev = '') 1186d868eb89SAndreas Gohr{ 1187cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1188f3f0262cSandi 118980fcb268SAdrian Lang // Parse range 119024870174SAndreas Gohr [$from, $to] = sexplode('-', $range, 2); 119180fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 119224870174SAndreas Gohr $from = $from ? $from - 1 : (0); 119324870174SAndreas Gohr $to = $to ? $to - 1 : (strlen($text)); 119480fcb268SAdrian Lang 119524870174SAndreas Gohr $slices = []; 119680fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 119780fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 119815cfe303Sandi $slices[2] = substr($text, $to); 1199f3f0262cSandi return $slices; 1200f3f0262cSandi} 1201f3f0262cSandi 1202f3f0262cSandi/** 120315fae107Sandi * Joins wiki text slices 120415fae107Sandi * 120580fcb268SAdrian Lang * function to join the text slices. 1206f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1207f3f0262cSandi * lines between sections if needed (used on saving). 120815fae107Sandi * 1209140cfbcdSGerrit Uitslag * @param string $pre prefix 1210140cfbcdSGerrit Uitslag * @param string $text text in the middle 1211140cfbcdSGerrit Uitslag * @param string $suf suffix 1212140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1213140cfbcdSGerrit Uitslag * @return string 12148b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12158b19906eSAndreas Gohr * 1216f3f0262cSandi */ 1217d868eb89SAndreas Gohrfunction con($pre, $text, $suf, $pretty = false) 1218d868eb89SAndreas Gohr{ 1219f3f0262cSandi if ($pretty) { 12207d34963bSAndreas Gohr if ( 12216c16a3a9Sfiwswe $pre !== '' && !str_ends_with($pre, "\n") && 12226c16a3a9Sfiwswe !str_starts_with($text, "\n") 12233272d797SAndreas Gohr ) { 122480fcb268SAdrian Lang $pre .= "\n"; 122580fcb268SAdrian Lang } 12267d34963bSAndreas Gohr if ( 12276c16a3a9Sfiwswe $suf !== '' && !str_ends_with($text, "\n") && 12286c16a3a9Sfiwswe !str_starts_with($suf, "\n") 12293272d797SAndreas Gohr ) { 123080fcb268SAdrian Lang $text .= "\n"; 123180fcb268SAdrian Lang } 1232f3f0262cSandi } 1233f3f0262cSandi 1234f3f0262cSandi return $pre . $text . $suf; 1235f3f0262cSandi} 1236f3f0262cSandi 1237f3f0262cSandi/** 1238b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1239b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1240b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1241b24d9195SAndreas Gohr * 1242b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 12438b19906eSAndreas Gohr * wiki, triggered in @param string $id the page ID 12448b19906eSAndreas Gohr * @see saveWikiText() 1245b24d9195SAndreas Gohr * 124669f9b481SSatoshi Sahara * @deprecated 2021-11-28 1247b24d9195SAndreas Gohr */ 1248d868eb89SAndreas Gohrfunction detectExternalEdit($id) 1249d868eb89SAndreas Gohr{ 125079a2d784SGerrit Uitslag dbg_deprecated(PageFile::class . '::detectExternalEdit()'); 1251b24e9c4aSSatoshi Sahara (new PageFile($id))->detectExternalEdit(); 1252b24d9195SAndreas Gohr} 1253b24d9195SAndreas Gohr 1254b24d9195SAndreas Gohr/** 1255a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1256a701424fSBen Coburn * Also directs changelog and attic updates. 125715fae107Sandi * 1258140cfbcdSGerrit Uitslag * @param string $id page id 1259140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1260140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1261140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 12628b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12638b19906eSAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 12648b19906eSAndreas Gohr * 1265f3f0262cSandi */ 1266d868eb89SAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) 1267d868eb89SAndreas Gohr{ 1268585bf44eSChristopher Smith 1269b24e9c4aSSatoshi Sahara // get COMMON_WIKIPAGE_SAVE event data 1270b24e9c4aSSatoshi Sahara $data = (new PageFile($id))->saveWikiText($text, $summary, $minor); 1271a577fbc2SAndreas Gohr if (!$data) return; // save was cancelled (for no changes or by a plugin) 1272ac3ed4afSGerrit Uitslag 127326a0801fSAndreas Gohr // send notify mails 127424870174SAndreas Gohr ['oldRevision' => $rev, 'newRevision' => $new_rev, 'summary' => $summary] = $data; 12753b813d43SSatoshi Sahara notify($id, 'admin', $rev, $summary, $minor, $new_rev); 12763b813d43SSatoshi Sahara notify($id, 'subscribers', $rev, $summary, $minor, $new_rev); 12772eccbdaaSGina Haeussge 12782eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1279fe9ec250SChris Smith if(useHeading('content')) { 1280*6734bb8cSAndreas Gohr $pages = (new MetadataSearch())->backlinks($id, true); 12812eccbdaaSGina Haeussge foreach($pages as $page) { 12820db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 12832eccbdaaSGina Haeussge $cache->removeCache(); 12842eccbdaaSGina Haeussge } 12852eccbdaaSGina Haeussge } 1286f3f0262cSandi} 1287f3f0262cSandi 1288f3f0262cSandi/** 1289d5824ab9SSatoshi Sahara * moves the current version to the attic and returns its revision date 1290140cfbcdSGerrit Uitslag * 1291140cfbcdSGerrit Uitslag * @param string $id page id 1292140cfbcdSGerrit Uitslag * @return int|string revision timestamp 12938b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12948b19906eSAndreas Gohr * 129569f9b481SSatoshi Sahara * @deprecated 2021-11-28 1296f3f0262cSandi */ 1297d868eb89SAndreas Gohrfunction saveOldRevision($id) 1298d868eb89SAndreas Gohr{ 129979a2d784SGerrit Uitslag dbg_deprecated(PageFile::class . '::saveOldRevision()'); 1300b24e9c4aSSatoshi Sahara return (new PageFile($id))->saveOldRevision(); 1301f3f0262cSandi} 1302f3f0262cSandi 1303f3f0262cSandi/** 1304fde10de4SAdrian Lang * Sends a notify mail on page change or registration 130526a0801fSAndreas Gohr * 130626a0801fSAndreas Gohr * @param string $id The changed page 1307fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 13083272d797SAndreas Gohr * @param int|string $rev Old page revision 130926a0801fSAndreas Gohr * @param string $summary What changed 131090033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 131142ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 131283734cddSPhy * @param int|string $current_rev New page revision 13133272d797SAndreas Gohr * @return bool 1314140cfbcdSGerrit Uitslag * 131515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1316f3f0262cSandi */ 1317d868eb89SAndreas Gohrfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = [], $current_rev = false) 1318d868eb89SAndreas Gohr{ 1319f3f0262cSandi global $conf; 1320585bf44eSChristopher Smith /* @var Input $INPUT */ 1321585bf44eSChristopher Smith global $INPUT; 1322b158d625SSteven Danz 13236df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 132426a0801fSAndreas Gohr if ($who == 'admin') { 13253272d797SAndreas Gohr if (empty($conf['notify'])) return false; //notify enabled? 13262ed38036SAndreas Gohr $tpl = 'mailtext'; 132726a0801fSAndreas Gohr $to = $conf['notify']; 132826a0801fSAndreas Gohr } elseif ($who == 'subscribers') { 132984c1127cSAndreas Gohr if (!actionOK('subscribe')) return false; //subscribers enabled? 1330585bf44eSChristopher Smith if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 133124870174SAndreas Gohr $data = ['id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace]; 1332cbb44eabSAndreas Gohr Event::createAndTrigger( 1333dccd6b2bSAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', 1334dccd6b2bSAndreas Gohr $data, 133524870174SAndreas Gohr [new SubscriberManager(), 'notifyAddresses'] 13363272d797SAndreas Gohr ); 13372ed38036SAndreas Gohr $to = $data['addresslist']; 13382ed38036SAndreas Gohr if (empty($to)) return false; 13392ed38036SAndreas Gohr $tpl = 'subscr_single'; 134026a0801fSAndreas Gohr } else { 13413272d797SAndreas Gohr return false; //just to be safe 134226a0801fSAndreas Gohr } 134326a0801fSAndreas Gohr 13446df843eeSAndreas Gohr // prepare content 1345704a815fSMichael Große $subscription = new PageSubscriptionSender(); 134683734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1347f3f0262cSandi} 13482ed38036SAndreas Gohr 134915fae107Sandi/** 135071f7bde7SAndreas Gohr * extracts the query from a search engine referrer 135115fae107Sandi * 13528b19906eSAndreas Gohr * @return array|string 135371f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1354140cfbcdSGerrit Uitslag * 13558b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1356f3f0262cSandi */ 1357d868eb89SAndreas Gohrfunction getGoogleQuery() 1358d868eb89SAndreas Gohr{ 1359585bf44eSChristopher Smith /* @var Input $INPUT */ 1360585bf44eSChristopher Smith global $INPUT; 1361585bf44eSChristopher Smith 1362585bf44eSChristopher Smith if (!$INPUT->server->has('HTTP_REFERER')) { 1363c66972f2SAdrian Lang return ''; 1364c66972f2SAdrian Lang } 1365585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1366f3f0262cSandi 1367079b3ac1SAndreas Gohr // only handle common SEs 1368c7875401SJyoti S if (!array_key_exists('host', $url)) return ''; 1369079b3ac1SAndreas Gohr if (!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/', $url['host'])) return ''; 1370e4d8a516SKazutaka Miyasaka 137124870174SAndreas Gohr $query = []; 1372181adffeSJulian Jeggle if (!array_key_exists('query', $url)) return ''; 1373f3f0262cSandi parse_str($url['query'], $query); 1374e4d8a516SKazutaka Miyasaka 1375c66972f2SAdrian Lang $q = ''; 1376079b3ac1SAndreas Gohr if (isset($query['q'])) { 1377079b3ac1SAndreas Gohr $q = $query['q']; 1378079b3ac1SAndreas Gohr } elseif (isset($query['p'])) { 1379079b3ac1SAndreas Gohr $q = $query['p']; 1380079b3ac1SAndreas Gohr } elseif (isset($query['query'])) { 1381079b3ac1SAndreas Gohr $q = $query['query']; 1382079b3ac1SAndreas Gohr } 1383079b3ac1SAndreas Gohr $q = trim($q); 1384f3f0262cSandi 1385079b3ac1SAndreas Gohr if (!$q) return ''; 1386c7dc833bSPhy // ignore if query includes a full URL 1387093fe67eSAndreas Gohr if (str_contains($q, '//')) return ''; 13886531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1389f93b3b50SAndreas Gohr return $q; 1390f3f0262cSandi} 1391f3f0262cSandi 1392f3f0262cSandi/** 1393f3f0262cSandi * Return the human readable size of a file 1394f3f0262cSandi * 1395f3f0262cSandi * @param int $size A file size 1396f3f0262cSandi * @param int $dec A number of decimal places 139774160ca1SGerrit Uitslag * @return string human readable size 1398140cfbcdSGerrit Uitslag * 1399f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1400f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1401f3f0262cSandi * @version 1.0.0 1402f3f0262cSandi */ 1403d868eb89SAndreas Gohrfunction filesize_h($size, $dec = 1) 1404d868eb89SAndreas Gohr{ 140524870174SAndreas Gohr $sizes = ['B', 'KB', 'MB', 'GB']; 1406f3f0262cSandi $count = count($sizes); 1407f3f0262cSandi $i = 0; 1408f3f0262cSandi 1409f3f0262cSandi while ($size >= 1024 && ($i < $count - 1)) { 1410f3f0262cSandi $size /= 1024; 1411f3f0262cSandi $i++; 1412f3f0262cSandi } 1413f3f0262cSandi 1414ef08383eSAndreas Gohr return round($size, $dec) . "\xC2\xA0" . $sizes[$i]; //non-breaking space 1415f3f0262cSandi} 1416f3f0262cSandi 141715fae107Sandi/** 1418c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1419c57e365eSAndreas Gohr * 1420140cfbcdSGerrit Uitslag * @param int $dt timestamp 1421140cfbcdSGerrit Uitslag * @return string 14228b19906eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 14238b19906eSAndreas Gohr * 1424c57e365eSAndreas Gohr */ 1425d868eb89SAndreas Gohrfunction datetime_h($dt) 1426d868eb89SAndreas Gohr{ 1427c57e365eSAndreas Gohr global $lang; 1428c57e365eSAndreas Gohr 1429c57e365eSAndreas Gohr $ago = time() - $dt; 1430c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1431c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1432c57e365eSAndreas Gohr } 1433c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 30 * 2) { 1434c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1435c57e365eSAndreas Gohr } 1436c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 7 * 2) { 1437c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1438c57e365eSAndreas Gohr } 1439c57e365eSAndreas Gohr if ($ago > 24 * 60 * 60 * 2) { 1440c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1441c57e365eSAndreas Gohr } 1442c57e365eSAndreas Gohr if ($ago > 60 * 60 * 2) { 1443c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1444c57e365eSAndreas Gohr } 1445c57e365eSAndreas Gohr if ($ago > 60 * 2) { 1446c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1447c57e365eSAndreas Gohr } 1448c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1449c57e365eSAndreas Gohr} 1450c57e365eSAndreas Gohr 1451c57e365eSAndreas Gohr/** 1452f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1453f2263577SAndreas Gohr * 1454f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1455f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1456f2263577SAndreas Gohr * 1457140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1458140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1459140cfbcdSGerrit Uitslag * @return string 14608b19906eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 14618b19906eSAndreas Gohr * 14628b19906eSAndreas Gohr * @see datetime_h 1463f2263577SAndreas Gohr */ 1464d868eb89SAndreas Gohrfunction dformat($dt = null, $format = '') 1465d868eb89SAndreas Gohr{ 1466f2263577SAndreas Gohr global $conf; 1467f2263577SAndreas Gohr 1468f2263577SAndreas Gohr if (is_null($dt)) $dt = time(); 1469f2263577SAndreas Gohr $dt = (int)$dt; 1470f2263577SAndreas Gohr if (!$format) $format = $conf['dformat']; 1471f2263577SAndreas Gohr 1472f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1473f2263577SAndreas Gohr return strftime($format, $dt); 1474f2263577SAndreas Gohr} 1475f2263577SAndreas Gohr 1476f2263577SAndreas Gohr/** 1477c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1478c4f79b71SMichael Hamann * 14798b19906eSAndreas Gohr * @param int $int_date current date in UNIX timestamp 14808b19906eSAndreas Gohr * @return string 1481c4f79b71SMichael Hamann * @author <ungu at terong dot com> 148259752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1483140cfbcdSGerrit Uitslag * 1484c4f79b71SMichael Hamann */ 1485d868eb89SAndreas Gohrfunction date_iso8601($int_date) 1486d868eb89SAndreas Gohr{ 1487c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1488c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1489c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3) . ":" . substr($pre_timezone, 3, 2); 1490c4f79b71SMichael Hamann $date_mod .= $time_zone; 1491c4f79b71SMichael Hamann return $date_mod; 1492c4f79b71SMichael Hamann} 1493c4f79b71SMichael Hamann 1494c4f79b71SMichael Hamann/** 149500a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 149600a7b5adSEsther Brunner * 14978b19906eSAndreas Gohr * @param string $email email address 14988b19906eSAndreas Gohr * @return string 149900a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 150000a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1501140cfbcdSGerrit Uitslag * 150200a7b5adSEsther Brunner */ 1503d868eb89SAndreas Gohrfunction obfuscate($email) 1504d868eb89SAndreas Gohr{ 150500a7b5adSEsther Brunner global $conf; 150600a7b5adSEsther Brunner 150700a7b5adSEsther Brunner switch ($conf['mailguard']) { 150800a7b5adSEsther Brunner case 'visible': 150924870174SAndreas Gohr $obfuscate = ['@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] ']; 151000a7b5adSEsther Brunner return strtr($email, $obfuscate); 151100a7b5adSEsther Brunner 151200a7b5adSEsther Brunner case 'hex': 151324870174SAndreas Gohr return Conversion::toHtml($email, true); 151400a7b5adSEsther Brunner 151500a7b5adSEsther Brunner case 'none': 151600a7b5adSEsther Brunner default: 151700a7b5adSEsther Brunner return $email; 151800a7b5adSEsther Brunner } 151900a7b5adSEsther Brunner} 152000a7b5adSEsther Brunner 152100a7b5adSEsther Brunner/** 152289541d4bSAndreas Gohr * Removes quoting backslashes 152389541d4bSAndreas Gohr * 1524140cfbcdSGerrit Uitslag * @param string $string 1525140cfbcdSGerrit Uitslag * @param string $char backslashed character 1526140cfbcdSGerrit Uitslag * @return string 15278b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 15288b19906eSAndreas Gohr * 152989541d4bSAndreas Gohr */ 1530d868eb89SAndreas Gohrfunction unslash($string, $char = "'") 1531d868eb89SAndreas Gohr{ 153289541d4bSAndreas Gohr return str_replace('\\' . $char, $char, $string); 153389541d4bSAndreas Gohr} 153489541d4bSAndreas Gohr 153573038c47SAndreas Gohr/** 153673038c47SAndreas Gohr * Convert php.ini shorthands to byte 153773038c47SAndreas Gohr * 1538a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1539140cfbcdSGerrit Uitslag * 1540a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1541a81f3d99SAndreas Gohr * 1542a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1543a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1544a81f3d99SAndreas Gohr * @return int 154573038c47SAndreas Gohr */ 1546d868eb89SAndreas Gohrfunction php_to_byte($value) 1547d868eb89SAndreas Gohr{ 1548093fe67eSAndreas Gohr $ret = match (strtoupper(substr($value, -1))) { 1549093fe67eSAndreas Gohr 'G' => (int)substr($value, 0, -1) * 1024 * 1024 * 1024, 1550093fe67eSAndreas Gohr 'M' => (int)substr($value, 0, -1) * 1024 * 1024, 1551093fe67eSAndreas Gohr 'K' => (int)substr($value, 0, -1) * 1024, 1552093fe67eSAndreas Gohr default => (int)$value, 1553093fe67eSAndreas Gohr }; 155473038c47SAndreas Gohr return $ret; 155573038c47SAndreas Gohr} 155673038c47SAndreas Gohr 1557546d3a99SAndreas Gohr/** 1558546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1559140cfbcdSGerrit Uitslag * 1560140cfbcdSGerrit Uitslag * @param string $string 1561140cfbcdSGerrit Uitslag * @return string 1562546d3a99SAndreas Gohr */ 1563d868eb89SAndreas Gohrfunction preg_quote_cb($string) 1564d868eb89SAndreas Gohr{ 1565546d3a99SAndreas Gohr return preg_quote($string, '/'); 1566546d3a99SAndreas Gohr} 156773038c47SAndreas Gohr 1568bd2f6c2fSAndreas Gohr/** 1569bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1570bd2f6c2fSAndreas Gohr * 1571c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1572bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1573bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1574bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1575bd2f6c2fSAndreas Gohr * 1576bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1577bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1578bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1579bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1580bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 15813272d797SAndreas Gohr * @return string 1582bd2f6c2fSAndreas Gohr */ 1583d868eb89SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') 1584d868eb89SAndreas Gohr{ 158524870174SAndreas Gohr $max -= PhpString::strlen($keep); 1586bd2f6c2fSAndreas Gohr if ($max < $min) return $keep; 158724870174SAndreas Gohr $len = PhpString::strlen($short); 1588bd2f6c2fSAndreas Gohr if ($len <= $max) return $keep . $short; 1589bd2f6c2fSAndreas Gohr $half = floor($max / 2); 15906ce3e5f8SAndreas Gohr return $keep . 159124870174SAndreas Gohr PhpString::substr($short, 0, $half - 1) . 15926ce3e5f8SAndreas Gohr $char . 159324870174SAndreas Gohr PhpString::substr($short, $len - $half); 1594bd2f6c2fSAndreas Gohr} 1595bd2f6c2fSAndreas Gohr 1596dc58b6f4SAndy Webber/** 1597dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1598dc58b6f4SAndy Webber * in page footer and recent changes pages 1599dc58b6f4SAndy Webber * 1600b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 160115f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1602c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 160315f3bc49SGerrit Uitslag * 1604dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1605dc58b6f4SAndy Webber */ 1606d868eb89SAndreas Gohrfunction editorinfo($username, $textonly = false) 1607d868eb89SAndreas Gohr{ 1608cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1609dc58b6f4SAndy Webber} 1610dc58b6f4SAndy Webber 161160a396c8SGerrit Uitslag/** 161260a396c8SGerrit Uitslag * Returns users realname w/o link 161360a396c8SGerrit Uitslag * 1614f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 161515f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1616c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 161760a396c8SGerrit Uitslag * 161860a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 161960a396c8SGerrit Uitslag */ 1620d868eb89SAndreas Gohrfunction userlink($username = null, $textonly = false) 1621d868eb89SAndreas Gohr{ 162260a396c8SGerrit Uitslag global $conf, $INFO; 1623e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 162460a396c8SGerrit Uitslag global $auth; 162530f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 162630f6ec4bSGerrit Uitslag global $INPUT; 162760a396c8SGerrit Uitslag 162860a396c8SGerrit Uitslag // prepare initial event data 162924870174SAndreas Gohr $data = [ 163060a396c8SGerrit Uitslag 'username' => $username, // the unique user name 163160a396c8SGerrit Uitslag 'name' => '', 163224870174SAndreas Gohr 'link' => [ 163324870174SAndreas Gohr //setting 'link' to false disables linking 163460a396c8SGerrit Uitslag 'target' => '', 163560a396c8SGerrit Uitslag 'pre' => '', 163660a396c8SGerrit Uitslag 'suf' => '', 163760a396c8SGerrit Uitslag 'style' => '', 163860a396c8SGerrit Uitslag 'more' => '', 163960a396c8SGerrit Uitslag 'url' => '', 164060a396c8SGerrit Uitslag 'title' => '', 164124870174SAndreas Gohr 'class' => '', 164224870174SAndreas Gohr ], 16434d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 164424870174SAndreas Gohr 'textonly' => $textonly, 164524870174SAndreas Gohr ]; 164662c8004eSGerrit Uitslag if ($username === null) { 164730f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 164815f3bc49SGerrit Uitslag if ($textonly) { 164915f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name'] . ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 165015f3bc49SGerrit Uitslag } else { 165164159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> ' . 165264159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 165360a396c8SGerrit Uitslag } 165415f3bc49SGerrit Uitslag } 165560a396c8SGerrit Uitslag 1656e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 165760a396c8SGerrit Uitslag if ($evt->advise_before(true)) { 165860a396c8SGerrit Uitslag if (empty($data['name'])) { 16596547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin) { 16606547cfc7SGerrit Uitslag $info = $auth->getUserData($username); 16616547cfc7SGerrit Uitslag } 166265833968SGerrit Uitslag if ($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1663dc58b6f4SAndy Webber switch ($conf['showuseras']) { 1664dc58b6f4SAndy Webber case 'username': 16657f081821SGerrit Uitslag case 'username_link': 166615f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 166760a396c8SGerrit Uitslag break; 1668dc58b6f4SAndy Webber case 'email': 1669dc58b6f4SAndy Webber case 'email_link': 167060a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 167160a396c8SGerrit Uitslag break; 1672dc58b6f4SAndy Webber } 167365833968SGerrit Uitslag } else { 167465833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 167560a396c8SGerrit Uitslag } 167660a396c8SGerrit Uitslag } 16777f081821SGerrit Uitslag 16787f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 16797f081821SGerrit Uitslag static $xhtml_renderer = null; 16807f081821SGerrit Uitslag 168115f3bc49SGerrit Uitslag if (!$data['textonly'] && empty($data['link']['url'])) { 168224870174SAndreas Gohr if (in_array($conf['showuseras'], ['email_link', 'username_link'])) { 16836547cfc7SGerrit Uitslag if (!isset($info) && $auth instanceof AuthPlugin) { 16846547cfc7SGerrit Uitslag $info = $auth->getUserData($username); 168560a396c8SGerrit Uitslag } 168660a396c8SGerrit Uitslag if (isset($info) && $info) { 16877f081821SGerrit Uitslag if ($conf['showuseras'] == 'email_link') { 168860a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1689dc58b6f4SAndy Webber } else { 16907f081821SGerrit Uitslag if (is_null($xhtml_renderer)) { 16917f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 16927f081821SGerrit Uitslag } 16938407f251Ssplitbrain if ($xhtml_renderer->interwiki === []) { 16947f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 16957f081821SGerrit Uitslag } 16967f081821SGerrit Uitslag $shortcut = 'user'; 1697533772e1SGerrit Uitslag $exists = null; 16986496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 16992a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 17006496c33fSGerrit Uitslag if ($exists !== null) { 17016496c33fSGerrit Uitslag if ($exists) { 17026496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 17036496c33fSGerrit Uitslag } else { 17046496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 17056496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 17066496c33fSGerrit Uitslag } 17076496c33fSGerrit Uitslag } 1708dc58b6f4SAndy Webber } 1709dc58b6f4SAndy Webber } else { 171015f3bc49SGerrit Uitslag $data['textonly'] = true; 1711dc58b6f4SAndy Webber } 171260a396c8SGerrit Uitslag } else { 171315f3bc49SGerrit Uitslag $data['textonly'] = true; 171460a396c8SGerrit Uitslag } 171560a396c8SGerrit Uitslag } 171660a396c8SGerrit Uitslag 171715f3bc49SGerrit Uitslag if ($data['textonly']) { 17184d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 171960a396c8SGerrit Uitslag } else { 172060a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 172160a396c8SGerrit Uitslag if (is_null($xhtml_renderer)) { 172260a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 172360a396c8SGerrit Uitslag } 17244d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 172560a396c8SGerrit Uitslag } 172660a396c8SGerrit Uitslag } 172760a396c8SGerrit Uitslag $evt->advise_after(); 172860a396c8SGerrit Uitslag unset($evt); 172960a396c8SGerrit Uitslag 17304d5fc927SGerrit Uitslag return $data['userlink']; 1731066fee30SAndreas Gohr} 1732066fee30SAndreas Gohr 1733066fee30SAndreas Gohr/** 1734066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1735066fee30SAndreas Gohr * When no image exists, returns an empty string 1736066fee30SAndreas Gohr * 1737066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 17383272d797SAndreas Gohr * @return string 17398b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 17408b19906eSAndreas Gohr * 1741066fee30SAndreas Gohr */ 1742d868eb89SAndreas Gohrfunction license_img($type) 1743d868eb89SAndreas Gohr{ 1744066fee30SAndreas Gohr global $license; 1745066fee30SAndreas Gohr global $conf; 1746066fee30SAndreas Gohr if (!$conf['license']) return ''; 1747066fee30SAndreas Gohr if (!is_array($license[$conf['license']])) return ''; 174824870174SAndreas Gohr $try = []; 1749066fee30SAndreas Gohr $try[] = 'lib/images/license/' . $type . '/' . $conf['license'] . '.png'; 1750066fee30SAndreas Gohr $try[] = 'lib/images/license/' . $type . '/' . $conf['license'] . '.gif'; 17516c16a3a9Sfiwswe if (str_starts_with($conf['license'], 'cc-')) { 1752066fee30SAndreas Gohr $try[] = 'lib/images/license/' . $type . '/cc.png'; 1753066fee30SAndreas Gohr } 1754066fee30SAndreas Gohr foreach ($try as $src) { 175579e79377SAndreas Gohr if (file_exists(DOKU_INC . $src)) return $src; 1756066fee30SAndreas Gohr } 1757066fee30SAndreas Gohr return ''; 1758dc58b6f4SAndy Webber} 1759dc58b6f4SAndy Webber 176013c08e2fSMichael Klier/** 176113c08e2fSMichael Klier * Checks if the given amount of memory is available 176213c08e2fSMichael Klier * 176313c08e2fSMichael Klier * If the memory_get_usage() function is not available the 176413c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 176513c08e2fSMichael Klier * 17663272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1767140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 17683272d797SAndreas Gohr * @return bool 17698b19906eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 17708b19906eSAndreas Gohr * 17718b19906eSAndreas Gohr * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 177213c08e2fSMichael Klier */ 1773d868eb89SAndreas Gohrfunction is_mem_available($mem, $bytes = 1_048_576) 1774d868eb89SAndreas Gohr{ 177513c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 177613c08e2fSMichael Klier if (empty($limit)) return true; // no limit set! 1777985d6187SElenchus if ($limit == -1) return true; // unlimited 177813c08e2fSMichael Klier 177913c08e2fSMichael Klier // parse limit to bytes 178013c08e2fSMichael Klier $limit = php_to_byte($limit); 178113c08e2fSMichael Klier 178213c08e2fSMichael Klier // get used memory if possible 178313c08e2fSMichael Klier if (function_exists('memory_get_usage')) { 178413c08e2fSMichael Klier $used = memory_get_usage(); 178549eb6e38SAndreas Gohr } else { 178649eb6e38SAndreas Gohr $used = $bytes; 178713c08e2fSMichael Klier } 178813c08e2fSMichael Klier 178913c08e2fSMichael Klier if ($used + $mem > $limit) { 179013c08e2fSMichael Klier return false; 179113c08e2fSMichael Klier } 179213c08e2fSMichael Klier 179313c08e2fSMichael Klier return true; 179413c08e2fSMichael Klier} 179513c08e2fSMichael Klier 1796af2408d5SAndreas Gohr/** 1797af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1798af2408d5SAndreas Gohr * 1799af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1800af2408d5SAndreas Gohr * 1801af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1802af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1803140cfbcdSGerrit Uitslag * 1804140cfbcdSGerrit Uitslag * @param string $url url being directed to 1805af2408d5SAndreas Gohr */ 1806d868eb89SAndreas Gohrfunction send_redirect($url) 1807d868eb89SAndreas Gohr{ 180898ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 180998ca30d2SAndreas Gohr 1810585bf44eSChristopher Smith /* @var Input $INPUT */ 1811585bf44eSChristopher Smith global $INPUT; 1812585bf44eSChristopher Smith 18130181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 18140181f021SAndreas Gohr global $MSG; 18150181f021SAndreas Gohr if (isset($MSG) && count($MSG) && !defined('NOSESSION')) { 18160181f021SAndreas Gohr //reopen session, store data and close session again 18170181f021SAndreas Gohr @session_start(); 18180181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 18190181f021SAndreas Gohr } 18200181f021SAndreas Gohr 1821d4869846SAndreas Gohr // always close the session 1822d4869846SAndreas Gohr session_write_close(); 1823d4869846SAndreas Gohr 1824af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 18257d34963bSAndreas Gohr if ( 18267d34963bSAndreas Gohr $INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1827093fe67eSAndreas Gohr (str_contains($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI')) && 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 * @return mixed 18648b19906eSAndreas Gohr * @throws Exception 18655b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 18665b75cd1fSAdrian Lang */ 1867d868eb89SAndreas Gohrfunction valid_input_set($param, $valid_values, $array, $exc = '') 1868d868eb89SAndreas Gohr{ 18695b75cd1fSAdrian Lang if (isset($array[$param]) && in_array($array[$param], $valid_values)) { 18705b75cd1fSAdrian Lang return $array[$param]; 18715b75cd1fSAdrian Lang } elseif (isset($valid_values['default'])) { 18725b75cd1fSAdrian Lang return $valid_values['default']; 18735b75cd1fSAdrian Lang } else { 18745b75cd1fSAdrian Lang throw new Exception($exc); 18755b75cd1fSAdrian Lang } 18765b75cd1fSAdrian Lang} 18775b75cd1fSAdrian Lang 187863703ba5SAndreas Gohr/** 187963703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 18803f108b37SAndreas Gohr * 18813f108b37SAndreas Gohr * Consider using PrefCookie directly 1882140cfbcdSGerrit Uitslag * 1883140cfbcdSGerrit Uitslag * @param string $pref preference key 1884b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 18853f108b37SAndreas Gohr * @return mixed preference value 188663703ba5SAndreas Gohr */ 1887d868eb89SAndreas Gohrfunction get_doku_pref($pref, $default) 1888d868eb89SAndreas Gohr{ 18893f108b37SAndreas Gohr return (new PrefCookie())->get($pref, $default); 1890554a8c9fSAdrian Lang} 1891554a8c9fSAdrian Lang 18923c94d07bSAnika Henke/** 18933c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 18943f108b37SAndreas Gohr * 18953f108b37SAndreas Gohr * Remove it by setting $val to false. 18963f108b37SAndreas Gohr * Consider using PrefCookie directly 1897140cfbcdSGerrit Uitslag * 1898140cfbcdSGerrit Uitslag * @param string $pref preference key 18993f108b37SAndreas Gohr * @param string|false $val preference value 19003c94d07bSAnika Henke */ 1901d868eb89SAndreas Gohrfunction set_doku_pref($pref, $val) 1902d868eb89SAndreas Gohr{ 19033f108b37SAndreas Gohr if ($val === false) { 19043f108b37SAndreas Gohr $val = null; 19053a970889SAnika Henke } else { 19063f108b37SAndreas Gohr $val = (string) $val; 19073c94d07bSAnika Henke } 19083c94d07bSAnika Henke 19093f108b37SAndreas Gohr (new PrefCookie())->set($pref, $val); 19103c94d07bSAnika Henke} 19113c94d07bSAnika Henke 1912f8fb2d18SAndreas Gohr/** 1913f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 1914f8fb2d18SAndreas Gohr * 191542ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 1916f8fb2d18SAndreas Gohr */ 1917d868eb89SAndreas Gohrfunction stripsourcemaps(&$text) 1918d868eb89SAndreas Gohr{ 1919f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 1920f8fb2d18SAndreas Gohr} 1921f8fb2d18SAndreas Gohr 19223c27983bSAndreas Gohr/** 192371de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 19243c27983bSAndreas Gohr * 19253c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 19263c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 19273c27983bSAndreas Gohr * files are embedded. 19283c27983bSAndreas Gohr * 192971de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 193071de5572SAndreas Gohr * 19313c27983bSAndreas Gohr * @param string $file full path to the SVG file 19323c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 193371de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 19343c27983bSAndreas Gohr */ 1935d868eb89SAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) 1936d868eb89SAndreas Gohr{ 19373c27983bSAndreas Gohr $file = trim($file); 19383c27983bSAndreas Gohr if ($file === '') return false; 19393c27983bSAndreas Gohr if (!file_exists($file)) return false; 19403c27983bSAndreas Gohr if (filesize($file) > $maxsize) return false; 19413c27983bSAndreas Gohr if (!is_readable($file)) return false; 19423c27983bSAndreas Gohr $content = file_get_contents($file); 19430849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s', '', $content); // comments 19440849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 19450849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 19460849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 19473c27983bSAndreas Gohr $content = trim($content); 19486c16a3a9Sfiwswe if (!str_starts_with($content, '<svg ')) return false; 194971de5572SAndreas Gohr return $content; 19503c27983bSAndreas Gohr} 19513c27983bSAndreas Gohr 1952e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1953