1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Common DokuWiki functions 415fae107Sandi * 515fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 715fae107Sandi */ 815fae107Sandi 90db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 100db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 110c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 12704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 1375d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 14e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 15e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 160c3a5702SAndreas Gohr 17f3f0262cSandi/** 18b6912aeaSAndreas Gohr * These constants are used with the recents function 19b6912aeaSAndreas Gohr */ 20b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED', 2); 21b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS', 4); 22b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES', 8); 230b926329SKate Arzamastsevadefine('RECENTS_MEDIA_CHANGES', 16); 240b926329SKate Arzamastsevadefine('RECENTS_MEDIA_PAGES_MIXED', 32); 25*08e9b52fSPhydefine('RECENTS_ONLY_CREATION', 64); 26b6912aeaSAndreas Gohr 27b6912aeaSAndreas Gohr/** 28d5197206Schris * Wrapper around htmlspecialchars() 29d5197206Schris * 30d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 31d5197206Schris * @see htmlspecialchars() 32140cfbcdSGerrit Uitslag * 33140cfbcdSGerrit Uitslag * @param string $string the string being converted 34140cfbcdSGerrit Uitslag * @return string converted string 35d5197206Schris */ 36d5197206Schrisfunction hsc($string) { 37d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 38d5197206Schris} 39d5197206Schris 40d5197206Schris/** 415b571377SAndreas Gohr * Checks if the given input is blank 425b571377SAndreas Gohr * 435b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 445b571377SAndreas Gohr * 4567234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 4667234204SAndreas Gohr * with a NULL value without warning. 4767234204SAndreas Gohr * 4867234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 4967234204SAndreas Gohr * 5067234204SAndreas Gohr * (isset($foo) && !blank($foo)) 5167234204SAndreas Gohr * (!isset($foo) || blank($foo)) 5267234204SAndreas Gohr * 535b571377SAndreas Gohr * @param $in 545b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 555b571377SAndreas Gohr * @return bool 565b571377SAndreas Gohr */ 575b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 585b571377SAndreas Gohr if(is_null($in)) return true; 595b571377SAndreas Gohr if(is_array($in)) return empty($in); 605b571377SAndreas Gohr if($in === "\0") return true; 615b571377SAndreas Gohr if($trim && trim($in) === '') return true; 625b571377SAndreas Gohr if(strlen($in) > 0) return false; 635b571377SAndreas Gohr return empty($in); 645b571377SAndreas Gohr} 655b571377SAndreas Gohr 665b571377SAndreas Gohr/** 67d5197206Schris * print a newline terminated string 68d5197206Schris * 69d5197206Schris * You can give an indention as optional parameter 70d5197206Schris * 71d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 72140cfbcdSGerrit Uitslag * 73140cfbcdSGerrit Uitslag * @param string $string line of text 74140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 75d5197206Schris */ 7625ec097bSChris Smithfunction ptln($string, $indent = 0) { 7725ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 7802b0b681SAndreas Gohr} 7902b0b681SAndreas Gohr 8002b0b681SAndreas Gohr/** 8102b0b681SAndreas Gohr * strips control characters (<32) from the given string 8202b0b681SAndreas Gohr * 8302b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 84140cfbcdSGerrit Uitslag * 8542ea7f44SGerrit Uitslag * @param string $string being stripped 86140cfbcdSGerrit Uitslag * @return string 8702b0b681SAndreas Gohr */ 8802b0b681SAndreas Gohrfunction stripctl($string) { 8902b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 90d5197206Schris} 91d5197206Schris 92d5197206Schris/** 93634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 94634d7150SAndreas Gohr * 95634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 96634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 97634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 9842ea7f44SGerrit Uitslag * 99634d7150SAndreas Gohr * @return string 100634d7150SAndreas Gohr */ 101634d7150SAndreas Gohrfunction getSecurityToken() { 102585bf44eSChristopher Smith /** @var Input $INPUT */ 103585bf44eSChristopher Smith global $INPUT; 1043680e2cdSAndreas Gohr 1053680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 1063680e2cdSAndreas Gohr $session = session_id(); 1073680e2cdSAndreas Gohr 1083680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1093680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 110c3cc6e05SAndreas Gohr return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 111634d7150SAndreas Gohr} 112634d7150SAndreas Gohr 113634d7150SAndreas Gohr/** 114634d7150SAndreas Gohr * Check the secret CSRF token 115140cfbcdSGerrit Uitslag * 116140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 117140cfbcdSGerrit Uitslag * @return bool success if the token matched 118634d7150SAndreas Gohr */ 119634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 120585bf44eSChristopher Smith /** @var Input $INPUT */ 1217d01a0eaSTom N Harris global $INPUT; 122585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 123df97eaacSAndreas Gohr 1247d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 125634d7150SAndreas Gohr if(getSecurityToken() != $token) { 126634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 127634d7150SAndreas Gohr return false; 128634d7150SAndreas Gohr } 129634d7150SAndreas Gohr return true; 130634d7150SAndreas Gohr} 131634d7150SAndreas Gohr 132634d7150SAndreas Gohr/** 133634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 134634d7150SAndreas Gohr * 135634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 136140cfbcdSGerrit Uitslag * 137140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 13842ea7f44SGerrit Uitslag * @return string html of hidden form field 139634d7150SAndreas Gohr */ 140634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1412404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1423272d797SAndreas Gohr if($print) echo $ret; 143634d7150SAndreas Gohr return $ret; 144634d7150SAndreas Gohr} 145634d7150SAndreas Gohr 146634d7150SAndreas Gohr/** 1471015a57dSChristopher Smith * Determine basic information for a request of $id 14815fae107Sandi * 14915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1507e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 151140cfbcdSGerrit Uitslag * 152140cfbcdSGerrit Uitslag * @param string $id pageid 153140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 154140cfbcdSGerrit Uitslag * @return array with info for a request of $id 155140cfbcdSGerrit Uitslag * 156f3f0262cSandi */ 1571015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 158f3f0262cSandi global $USERINFO; 159585bf44eSChristopher Smith /* @var Input $INPUT */ 160585bf44eSChristopher Smith global $INPUT; 1616afe8dcaSchris 162c66972f2SAdrian Lang // set info about manager/admin status. 16359bc3b48SGerrit Uitslag $info = array(); 164c66972f2SAdrian Lang $info['isadmin'] = false; 165c66972f2SAdrian Lang $info['ismanager'] = false; 166585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 167f3f0262cSandi $info['userinfo'] = $USERINFO; 1681015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 169585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 17017ee7f66SAndreas Gohr 171f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 172f8cc712eSAndreas Gohr $info['isadmin'] = true; 173f8cc712eSAndreas Gohr $info['ismanager'] = true; 174f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 175f8cc712eSAndreas Gohr $info['ismanager'] = true; 176f8cc712eSAndreas Gohr } 177f8cc712eSAndreas Gohr 17817ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 17917ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 180585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 18117ee7f66SAndreas Gohr } 182ee4c4a1bSAndreas Gohr 183f3f0262cSandi } else { 1841015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 185ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 186f3f0262cSandi } 187f3f0262cSandi 1881015a57dSChristopher Smith $info['namespace'] = getNS($id); 1891015a57dSChristopher Smith 1901015a57dSChristopher Smith // mobile detection 1911015a57dSChristopher Smith if ($htmlClient) { 1921015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1931015a57dSChristopher Smith } 1941015a57dSChristopher Smith 1951015a57dSChristopher Smith return $info; 1961015a57dSChristopher Smith } 1971015a57dSChristopher Smith 1981015a57dSChristopher Smith/** 1991015a57dSChristopher Smith * Return info about the current document as associative 2001015a57dSChristopher Smith * array. 2011015a57dSChristopher Smith * 2021015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 203140cfbcdSGerrit Uitslag * 204140cfbcdSGerrit Uitslag * @return array with info about current document 2051015a57dSChristopher Smith */ 2061015a57dSChristopher Smithfunction pageinfo() { 2071015a57dSChristopher Smith global $ID; 2081015a57dSChristopher Smith global $REV; 2091015a57dSChristopher Smith global $RANGE; 2101015a57dSChristopher Smith global $lang; 211585bf44eSChristopher Smith /* @var Input $INPUT */ 212585bf44eSChristopher Smith global $INPUT; 2131015a57dSChristopher Smith 2141015a57dSChristopher Smith $info = basicinfo($ID); 2151015a57dSChristopher Smith 2161015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2171015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2181015a57dSChristopher Smith $info['id'] = $ID; 2191015a57dSChristopher Smith $info['rev'] = $REV; 2201015a57dSChristopher Smith 221585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 22275d66495SMichael Große $subManager = new SubscriberManager(); 22375d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2247e87a794SChristopher Smith } else { 2257e87a794SChristopher Smith $info['subscribed'] = false; 2267e87a794SChristopher Smith } 2277e87a794SChristopher Smith 228f3f0262cSandi $info['locked'] = checklock($ID); 229317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 23079e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 23101c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2322ca9d91cSBen Coburn if($REV) { 2332ca9d91cSBen Coburn //check if current revision was meant 23401c9a118SAndreas Gohr if($info['exists'] && ($info['currentrev'] == $REV)) { 2352ca9d91cSBen Coburn $REV = ''; 2367b3a6803SAndreas Gohr } elseif($RANGE) { 2377b3a6803SAndreas Gohr //section editing does not work with old revisions! 2387b3a6803SAndreas Gohr $REV = ''; 2397b3a6803SAndreas Gohr $RANGE = ''; 2407b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2412ca9d91cSBen Coburn } else { 2422ca9d91cSBen Coburn //really use old revision 243317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 24479e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 245f3f0262cSandi } 246f3f0262cSandi } 247c112d578Sandi $info['rev'] = $REV; 248f3f0262cSandi if($info['exists']) { 249f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 250f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 251f3f0262cSandi } else { 252f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 253f3f0262cSandi } 25450e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 255f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 256f3f0262cSandi 25771726d78SBen Coburn //load page meta data 25871726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 25971726d78SBen Coburn 260652610a2Sandi //who's the editor 261047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 262652610a2Sandi if($REV) { 263f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 264652610a2Sandi } else { 2650e80bb5eSChristopher Smith if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 266aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 267aa27cf05SAndreas Gohr } else { 268f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 269cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 270cd00a034SBen Coburn if($revinfo !== false) { 271cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 272cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 273cd00a034SBen Coburn } 274cd00a034SBen Coburn } 275cd00a034SBen Coburn } 276cd00a034SBen Coburn //and check for an external edit 277cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 278cd00a034SBen Coburn // cached changelog line no longer valid 279cd00a034SBen Coburn $revinfo = false; 280cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 281cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 282652610a2Sandi } 283bb4866bdSchris 2840a444b5aSPhy if($revinfo !== false){ 285652610a2Sandi $info['ip'] = $revinfo['ip']; 286652610a2Sandi $info['user'] = $revinfo['user']; 287652610a2Sandi $info['sum'] = $revinfo['sum']; 28871726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 289ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 29059f257aeSchris 29188f522e9Sandi if($revinfo['user']) { 29288f522e9Sandi $info['editor'] = $revinfo['user']; 29388f522e9Sandi } else { 29488f522e9Sandi $info['editor'] = $revinfo['ip']; 29588f522e9Sandi } 2960a444b5aSPhy }else{ 2970a444b5aSPhy $info['ip'] = null; 2980a444b5aSPhy $info['user'] = null; 2990a444b5aSPhy $info['sum'] = null; 3000a444b5aSPhy $info['editor'] = null; 3010a444b5aSPhy } 302652610a2Sandi 303ee4c4a1bSAndreas Gohr // draft 3040aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 3050aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 3060aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 307ee4c4a1bSAndreas Gohr } 308ee4c4a1bSAndreas Gohr 3091015a57dSChristopher Smith return $info; 3101015a57dSChristopher Smith} 3111015a57dSChristopher Smith 3121015a57dSChristopher Smith/** 3130c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3140c39d46cSMichael Große */ 3150c39d46cSMichael Großefunction jsinfo() { 3160c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3170c39d46cSMichael Große 3180c39d46cSMichael Große if (!is_array($JSINFO)) { 3190c39d46cSMichael Große $JSINFO = []; 3200c39d46cSMichael Große } 3210c39d46cSMichael Große //export minimal info to JS, plugins can add more 3220c39d46cSMichael Große $JSINFO['id'] = $ID; 32368491db9SPhy $JSINFO['namespace'] = isset($INFO) ? (string) $INFO['namespace'] : ''; 3240c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3250c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3260c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3270c39d46cSMichael Große} 3280c39d46cSMichael Große 3290c39d46cSMichael Große/** 3301015a57dSChristopher Smith * Return information about the current media item as an associative array. 331140cfbcdSGerrit Uitslag * 332140cfbcdSGerrit Uitslag * @return array with info about current media item 3331015a57dSChristopher Smith */ 3341015a57dSChristopher Smithfunction mediainfo(){ 3351015a57dSChristopher Smith global $NS; 3361015a57dSChristopher Smith global $IMG; 3371015a57dSChristopher Smith 3381015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3391015a57dSChristopher Smith $info['image'] = $IMG; 3401c548ebeSAndreas Gohr 341f3f0262cSandi return $info; 342f3f0262cSandi} 343f3f0262cSandi 344f3f0262cSandi/** 3452684e50aSAndreas Gohr * Build an string of URL parameters 3462684e50aSAndreas Gohr * 3472684e50aSAndreas Gohr * @author Andreas Gohr 348140cfbcdSGerrit Uitslag * 349140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 350140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 351140cfbcdSGerrit Uitslag * @return string query string 3522684e50aSAndreas Gohr */ 353b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3542684e50aSAndreas Gohr $url = ''; 3552684e50aSAndreas Gohr $amp = false; 3562684e50aSAndreas Gohr foreach($params as $key => $val) { 357b174aeaeSchris if($amp) $url .= $sep; 3582684e50aSAndreas Gohr 35985e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3603a50618cSgweissbach $url .= rawurlencode((string) $val); 3612684e50aSAndreas Gohr $amp = true; 3622684e50aSAndreas Gohr } 3632684e50aSAndreas Gohr return $url; 3642684e50aSAndreas Gohr} 3652684e50aSAndreas Gohr 3662684e50aSAndreas Gohr/** 3672684e50aSAndreas Gohr * Build an string of html tag attributes 3682684e50aSAndreas Gohr * 3697bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3707bff22c0SAndreas Gohr * 3712684e50aSAndreas Gohr * @author Andreas Gohr 372140cfbcdSGerrit Uitslag * 373140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 374246d3337SMichael Große * @param bool $skipEmptyStrings skip empty string values? 375140cfbcdSGerrit Uitslag * @return string 3762684e50aSAndreas Gohr */ 377246d3337SMichael Großefunction buildAttributes($params, $skipEmptyStrings = false) { 3782684e50aSAndreas Gohr $url = ''; 3799063ec14SAdrian Lang $white = false; 3802684e50aSAndreas Gohr foreach($params as $key => $val) { 3812401f18dSSyntaxseed if($key[0] == '_') continue; 382246d3337SMichael Große if($val === '' && $skipEmptyStrings) continue; 3839063ec14SAdrian Lang if($white) $url .= ' '; 3847bff22c0SAndreas Gohr 3852684e50aSAndreas Gohr $url .= $key.'="'; 3862684e50aSAndreas Gohr $url .= htmlspecialchars($val); 3872684e50aSAndreas Gohr $url .= '"'; 3889063ec14SAdrian Lang $white = true; 3892684e50aSAndreas Gohr } 3902684e50aSAndreas Gohr return $url; 3912684e50aSAndreas Gohr} 3922684e50aSAndreas Gohr 3932684e50aSAndreas Gohr/** 39415fae107Sandi * This builds the breadcrumb trail and returns it as array 39515fae107Sandi * 39615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 397140cfbcdSGerrit Uitslag * 398e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 399f3f0262cSandi */ 400f3f0262cSandifunction breadcrumbs() { 4018746e727Sandi // we prepare the breadcrumbs early for quick session closing 4028746e727Sandi static $crumbs = null; 4038746e727Sandi if($crumbs != null) return $crumbs; 4048746e727Sandi 405f3f0262cSandi global $ID; 406f3f0262cSandi global $ACT; 407f3f0262cSandi global $conf; 4080ea5ebb4SB_S666 global $INFO; 409f3f0262cSandi 410f3f0262cSandi //first visit? 411c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 4125603d3c1SHenry Pan //we only save on show and existing visible readable wiki documents 413a77f5846Sjan $file = wikiFN($ID); 4145603d3c1SHenry Pan if($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) { 415e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 416f3f0262cSandi return $crumbs; 417f3f0262cSandi } 418a77f5846Sjan 419a77f5846Sjan // page names 4201a84a0f3SAnika Henke $name = noNSorNS($ID); 421fe9ec250SChris Smith if(useHeading('navigation')) { 422a77f5846Sjan // get page title 42367c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 424a77f5846Sjan if($title) { 425a77f5846Sjan $name = $title; 426a77f5846Sjan } 427a77f5846Sjan } 428a77f5846Sjan 429f3f0262cSandi //remove ID from array 430a77f5846Sjan if(isset($crumbs[$ID])) { 431a77f5846Sjan unset($crumbs[$ID]); 432f3f0262cSandi } 433f3f0262cSandi 434f3f0262cSandi //add to array 435a77f5846Sjan $crumbs[$ID] = $name; 436f3f0262cSandi //reduce size 437f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 438f3f0262cSandi array_shift($crumbs); 439f3f0262cSandi } 440f3f0262cSandi //save to session 441e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 442f3f0262cSandi return $crumbs; 443f3f0262cSandi} 444f3f0262cSandi 445f3f0262cSandi/** 44615fae107Sandi * Filter for page IDs 44715fae107Sandi * 448f3f0262cSandi * This is run on a ID before it is outputted somewhere 449f3f0262cSandi * currently used to replace the colon with something else 450907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 451907f24f7SAndreas Gohr * 452907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 453907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 454907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 45515fae107Sandi * 45649c713a3Sandi * Urlencoding is ommitted when the second parameter is false 45749c713a3Sandi * 45815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 459140cfbcdSGerrit Uitslag * 460140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 461140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 462140cfbcdSGerrit Uitslag * @return string 463f3f0262cSandi */ 46449c713a3Sandifunction idfilter($id, $ue = true) { 465f3f0262cSandi global $conf; 466585bf44eSChristopher Smith /* @var Input $INPUT */ 467585bf44eSChristopher Smith global $INPUT; 468585bf44eSChristopher Smith 469f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 470f3f0262cSandi $id = strtr($id, ':', '/'); 471f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 47258bedc8aSborekb $conf['userewrite'] && 473585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4743272d797SAndreas Gohr ) { 475f3f0262cSandi $id = strtr($id, ':', ';'); 476f3f0262cSandi } 47749c713a3Sandi if($ue) { 478b6c6979fSAndreas Gohr $id = rawurlencode($id); 479f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 480edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 481f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 48249c713a3Sandi } 483f3f0262cSandi return $id; 484f3f0262cSandi} 485f3f0262cSandi 486f3f0262cSandi/** 487ed7b5f09Sandi * This builds a link to a wikipage 48815fae107Sandi * 4894bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4906c7843b5Sandi * 49115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4924bc480e5SAndreas Gohr * 4934bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4944bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4954bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4964bc480e5SAndreas Gohr * @param string $separator parameter separator 4974bc480e5SAndreas Gohr * @return string 498f3f0262cSandi */ 49916f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 500f3f0262cSandi global $conf; 50116f15a81SDominik Eckelmann if(is_array($urlParameters)) { 5024bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 50364159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 50464159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 50564159a61SAndreas Gohr } 50616f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 5076de3759aSAndreas Gohr } else { 50816f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 5096de3759aSAndreas Gohr } 51016f15a81SDominik Eckelmann if($id === '') { 51116f15a81SDominik Eckelmann $id = $conf['start']; 51216f15a81SDominik Eckelmann } 513f3f0262cSandi $id = idfilter($id); 51416f15a81SDominik Eckelmann if($absolute) { 515ed7b5f09Sandi $xlink = DOKU_URL; 516ed7b5f09Sandi } else { 517ed7b5f09Sandi $xlink = DOKU_BASE; 518ed7b5f09Sandi } 519f3f0262cSandi 5206c7843b5Sandi if($conf['userewrite'] == 2) { 5216c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 52216f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5236c7843b5Sandi } elseif($conf['userewrite']) { 524f3f0262cSandi $xlink .= $id; 52516f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 52640b5fb5bSPhy } elseif($id !== '') { 5276c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 52816f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 529bce3726dSAndreas Gohr } else { 530bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 53116f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 532f3f0262cSandi } 533f3f0262cSandi 534f3f0262cSandi return $xlink; 535f3f0262cSandi} 536f3f0262cSandi 537f3f0262cSandi/** 538f5c2808fSBen Coburn * This builds a link to an alternate page format 539f5c2808fSBen Coburn * 540f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 541f5c2808fSBen Coburn * 542f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5434bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5444bc480e5SAndreas Gohr * @param string $format the export renderer to use 5454bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5464bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5474bc480e5SAndreas Gohr * @param string $sep parameter separator 5484bc480e5SAndreas Gohr * @return string 549f5c2808fSBen Coburn */ 5504bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 551f5c2808fSBen Coburn global $conf; 5524bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5534bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 554f5c2808fSBen Coburn } else { 5554bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 556f5c2808fSBen Coburn } 557f5c2808fSBen Coburn 558f5c2808fSBen Coburn $format = rawurlencode($format); 559f5c2808fSBen Coburn $id = idfilter($id); 560f5c2808fSBen Coburn if($abs) { 561f5c2808fSBen Coburn $xlink = DOKU_URL; 562f5c2808fSBen Coburn } else { 563f5c2808fSBen Coburn $xlink = DOKU_BASE; 564f5c2808fSBen Coburn } 565f5c2808fSBen Coburn 566f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 567f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5684bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 569f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 570f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5714bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 572f5c2808fSBen Coburn } else { 573f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5744bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 575f5c2808fSBen Coburn } 576f5c2808fSBen Coburn 577f5c2808fSBen Coburn return $xlink; 578f5c2808fSBen Coburn} 579f5c2808fSBen Coburn 580f5c2808fSBen Coburn/** 5816de3759aSAndreas Gohr * Build a link to a media file 5826de3759aSAndreas Gohr * 5836de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5848c08db0aSAndreas Gohr * 5858c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5868c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5878c08db0aSAndreas Gohr * 5883272d797SAndreas Gohr * @param string $id the media file id or URL 5893272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5903272d797SAndreas Gohr * @param bool $direct link to detail page if false 5913272d797SAndreas Gohr * @param string $sep URL parameter separator 5923272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5933272d797SAndreas Gohr * @return string 5946de3759aSAndreas Gohr */ 59555b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5966de3759aSAndreas Gohr global $conf; 597b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 598826d2766SKlap-in if(!$isexternalimage) { 599826d2766SKlap-in $id = cleanID($id); 600826d2766SKlap-in } 601826d2766SKlap-in 6026de3759aSAndreas Gohr if(is_array($more)) { 6030f4e0092SChristopher Smith // add token for resized images 604443e135dSChristopher Smith if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){ 6050f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 6060f4e0092SChristopher Smith } 6078c08db0aSAndreas Gohr // strip defaults for shorter URLs 6088c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 609443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 610443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 6118c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 61278b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 613b174aeaeSchris $more = buildURLparams($more, $sep); 6146de3759aSAndreas Gohr } else { 6155e7db1e2SChristopher Smith $matches = array(); 616cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6175e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6185e7db1e2SChristopher Smith foreach ($matches as $match){ 6195e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6205e7db1e2SChristopher Smith } 621cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 622cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6235e7db1e2SChristopher Smith } 6248c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6258c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 626b174aeaeSchris $more = str_replace(',', $sep, $more); 6276de3759aSAndreas Gohr } 6286de3759aSAndreas Gohr 62955b2b31bSAndreas Gohr if($abs) { 63055b2b31bSAndreas Gohr $xlink = DOKU_URL; 63155b2b31bSAndreas Gohr } else { 6326de3759aSAndreas Gohr $xlink = DOKU_BASE; 63355b2b31bSAndreas Gohr } 6346de3759aSAndreas Gohr 6356de3759aSAndreas Gohr // external URLs are always direct without rewriting 636826d2766SKlap-in if($isexternalimage) { 6376de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 638cc036f74SKlap-in $xlink .= '?'.$more; 639b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6406de3759aSAndreas Gohr return $xlink; 6416de3759aSAndreas Gohr } 6426de3759aSAndreas Gohr 6436de3759aSAndreas Gohr $id = idfilter($id); 6446de3759aSAndreas Gohr 6456de3759aSAndreas Gohr // decide on scriptname 6466de3759aSAndreas Gohr if($direct) { 6476de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6486de3759aSAndreas Gohr $script = '_media'; 6496de3759aSAndreas Gohr } else { 6506de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6516de3759aSAndreas Gohr } 6526de3759aSAndreas Gohr } else { 6536de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6546de3759aSAndreas Gohr $script = '_detail'; 6556de3759aSAndreas Gohr } else { 6566de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6576de3759aSAndreas Gohr } 6586de3759aSAndreas Gohr } 6596de3759aSAndreas Gohr 6606de3759aSAndreas Gohr // build URL based on rewrite mode 6616de3759aSAndreas Gohr if($conf['userewrite']) { 6626de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6636de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6646de3759aSAndreas Gohr } else { 6656de3759aSAndreas Gohr if($more) { 666a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 667b174aeaeSchris $xlink .= $sep.'media='.$id; 6686de3759aSAndreas Gohr } else { 669a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6706de3759aSAndreas Gohr } 6716de3759aSAndreas Gohr } 6726de3759aSAndreas Gohr 6736de3759aSAndreas Gohr return $xlink; 6746de3759aSAndreas Gohr} 6756de3759aSAndreas Gohr 6766de3759aSAndreas Gohr/** 67725ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 67815fae107Sandi * 67925ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 68025ca5b17SAndreas Gohr * 68115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 682140cfbcdSGerrit Uitslag * 683140cfbcdSGerrit Uitslag * @return string 684f3f0262cSandi */ 68525ca5b17SAndreas Gohrfunction script() { 686ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 687f3f0262cSandi} 688f3f0262cSandi 689f3f0262cSandi/** 69015fae107Sandi * Spamcheck against wordlist 69115fae107Sandi * 692f3f0262cSandi * Checks the wikitext against a list of blocked expressions 693f3f0262cSandi * returns true if the text contains any bad words 69415fae107Sandi * 695e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 696e403cc58SMichael Klier * 697e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 698e403cc58SMichael Klier * and gain information about the user who was blocked. 699e403cc58SMichael Klier * 700e403cc58SMichael Klier * Event data: 701e403cc58SMichael Klier * data['matches'] - array of matches 702e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 703e403cc58SMichael Klier * [ip] - ip address 704e403cc58SMichael Klier * [user] - username (if logged in) 705e403cc58SMichael Klier * [mail] - mail address (if logged in) 706e403cc58SMichael Klier * [name] - real name (if logged in) 707e403cc58SMichael Klier * 70815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 7096dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 710140cfbcdSGerrit Uitslag * 7116dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 7126dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 713f3f0262cSandi */ 7146dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 715f3f0262cSandi global $TEXT; 7166dffa0e0SAndreas Gohr global $PRE; 7176dffa0e0SAndreas Gohr global $SUF; 718e0086ca2SAndreas Gohr global $SUM; 719f3f0262cSandi global $conf; 720e403cc58SMichael Klier global $INFO; 721585bf44eSChristopher Smith /* @var Input $INPUT */ 722585bf44eSChristopher Smith global $INPUT; 723f3f0262cSandi 724f3f0262cSandi if(!$conf['usewordblock']) return false; 725f3f0262cSandi 726e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7276dffa0e0SAndreas Gohr 728041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 72964159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 73064159a61SAndreas Gohr $text = preg_replace( 73164159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 73264159a61SAndreas Gohr '\1http://\2 \2\3', 73364159a61SAndreas Gohr $text 73464159a61SAndreas Gohr ); 73564159a61SAndreas Gohr // phpcs:enable 736041d1964SAndreas Gohr 737b9ac8716Schris $wordblocks = getWordblocks(); 7383e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 7393e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 7403e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 7413e2965d7Sandi // backreferences are used - the maximum is 99 7423e2965d7Sandi // this is very bad performancewise and may even be too high still 7433e2965d7Sandi $chunksize = 40; 7443e2965d7Sandi } else { 745a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7463e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 747a51d08efSAndreas Gohr $chunksize = 200; 7483e2965d7Sandi } 749b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 750f3f0262cSandi $re = array(); 75149eb6e38SAndreas Gohr // build regexp from blocks 752f3f0262cSandi foreach($blocks as $block) { 753f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 754f3f0262cSandi $block = trim($block); 755f3f0262cSandi if(empty($block)) continue; 756f3f0262cSandi $re[] = $block; 757f3f0262cSandi } 758e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 759e403cc58SMichael Klier // prepare event data 76059bc3b48SGerrit Uitslag $data = array(); 761e403cc58SMichael Klier $data['matches'] = $matches; 762585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 763585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 764585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 765e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 766e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 767e403cc58SMichael Klier } 768bad6fc0dSAndreas Gohr $callback = function () { 769bad6fc0dSAndreas Gohr return true; 770bad6fc0dSAndreas Gohr }; 771cbb44eabSAndreas Gohr return Event::createAndTrigger('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 772b9ac8716Schris } 773703f6fdeSandi } 774f3f0262cSandi return false; 775f3f0262cSandi} 776f3f0262cSandi 777f3f0262cSandi/** 77815fae107Sandi * Return the IP of the client 77915fae107Sandi * 7806d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 78115fae107Sandi * 7826d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 7836d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 7846d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 7856d8affe6SAndreas Gohr * headers 7866d8affe6SAndreas Gohr * 78715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 788140cfbcdSGerrit Uitslag * 7893272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 7903272d797SAndreas Gohr * @return string 791f3f0262cSandi */ 7926d8affe6SAndreas Gohrfunction clientIP($single = false) { 793585bf44eSChristopher Smith /* @var Input $INPUT */ 794925105e8SPhy global $INPUT, $conf; 795585bf44eSChristopher Smith 7966d8affe6SAndreas Gohr $ip = array(); 797585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 798585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 799585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 800585bf44eSChristopher Smith } 801585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 802585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 803585bf44eSChristopher Smith } 8046d8affe6SAndreas Gohr 805dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 806dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 807dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 808dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 809dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 810dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 811dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 812dc14c6d1SGuy Brand $IPv6Address = 813dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 814dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 815dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 816dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 817dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 818dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 819dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 820dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 821dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 822dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 823dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 824dc14c6d1SGuy Brand 8256d8affe6SAndreas Gohr // remove any non-IP stuff 8266d8affe6SAndreas Gohr $cnt = count($ip); 8274ff28443Schris $match = array(); 8286d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 829dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 8304ff28443Schris $ip[$i] = $match[0]; 8314ff28443Schris } else { 8324ff28443Schris $ip[$i] = ''; 8334ff28443Schris } 8346d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 835f3f0262cSandi } 8366d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 8376d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8386d8affe6SAndreas Gohr 8396d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 8406d8affe6SAndreas Gohr 841925105e8SPhy // skip trusted local addresses 8426d8affe6SAndreas Gohr foreach($ip as $i) { 843925105e8SPhy if(!empty($conf['trustedproxy']) && preg_match('/'.$conf['trustedproxy'].'/', $i)) { 8446d8affe6SAndreas Gohr continue; 8456d8affe6SAndreas Gohr } else { 8466d8affe6SAndreas Gohr return $i; 8476d8affe6SAndreas Gohr } 8486d8affe6SAndreas Gohr } 849925105e8SPhy 850925105e8SPhy // still here? just use the last address 851925105e8SPhy // this case all ips in the list are trusted 852925105e8SPhy return $ip[count($ip)-1]; 853f3f0262cSandi} 854f3f0262cSandi 855f3f0262cSandi/** 8561c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8571c548ebeSAndreas Gohr * 8581c548ebeSAndreas Gohr * Adapted from the example code at url below 8591c548ebeSAndreas Gohr * 8601c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 861140cfbcdSGerrit Uitslag * 86264159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 863140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8641c548ebeSAndreas Gohr */ 8651c548ebeSAndreas Gohrfunction clientismobile() { 866585bf44eSChristopher Smith /* @var Input $INPUT */ 867585bf44eSChristopher Smith global $INPUT; 8681c548ebeSAndreas Gohr 869585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8701c548ebeSAndreas Gohr 871585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8721c548ebeSAndreas Gohr 873585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8741c548ebeSAndreas Gohr 87564159a61SAndreas Gohr $uamatches = join( 87664159a61SAndreas Gohr '|', 87764159a61SAndreas Gohr [ 87864159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 87964159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 88064159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 88164159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 88264159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 88364159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 88464159a61SAndreas Gohr '\d\d\di', 'moto' 88564159a61SAndreas Gohr ] 88664159a61SAndreas Gohr ); 8871c548ebeSAndreas Gohr 888585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8891c548ebeSAndreas Gohr 8901c548ebeSAndreas Gohr return false; 8911c548ebeSAndreas Gohr} 8921c548ebeSAndreas Gohr 8931c548ebeSAndreas Gohr/** 8946efc45a2SDmitry Katsubo * check if a given link is interwiki link 8956efc45a2SDmitry Katsubo * 8966efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8976efc45a2SDmitry Katsubo * @return bool 8986efc45a2SDmitry Katsubo */ 8996efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 9006efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 9016efc45a2SDmitry Katsubo return false; 9026efc45a2SDmitry Katsubo} 9036efc45a2SDmitry Katsubo 9046efc45a2SDmitry Katsubo/** 90563211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 90663211f61SGlen Harris * 90722ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 90822ef1e32SAndreas Gohr * 90963211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 910140cfbcdSGerrit Uitslag * 9113272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 9123272d797SAndreas Gohr * @return string a comma separated list of hostnames 91363211f61SGlen Harris */ 91463211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 91522ef1e32SAndreas Gohr global $conf; 91622ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 91722ef1e32SAndreas Gohr 91863211f61SGlen Harris $hosts = array(); 91963211f61SGlen Harris $ips = explode(',', $ips); 920551a720fSMichael Klier 921551a720fSMichael Klier if(is_array($ips)) { 9223886270dSAndreas Gohr foreach($ips as $ip) { 923551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 92463211f61SGlen Harris } 925551a720fSMichael Klier return join(',', $hosts); 926551a720fSMichael Klier } else { 927551a720fSMichael Klier return gethostbyaddr(trim($ips)); 928551a720fSMichael Klier } 92963211f61SGlen Harris} 93063211f61SGlen Harris 93163211f61SGlen Harris/** 93215fae107Sandi * Checks if a given page is currently locked. 93315fae107Sandi * 934f3f0262cSandi * removes stale lockfiles 93515fae107Sandi * 93615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 937140cfbcdSGerrit Uitslag * 938140cfbcdSGerrit Uitslag * @param string $id page id 939140cfbcdSGerrit Uitslag * @return bool page is locked? 940f3f0262cSandi */ 941f3f0262cSandifunction checklock($id) { 942f3f0262cSandi global $conf; 943585bf44eSChristopher Smith /* @var Input $INPUT */ 944585bf44eSChristopher Smith global $INPUT; 945585bf44eSChristopher Smith 946c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 947f3f0262cSandi 948f3f0262cSandi //no lockfile 94979e79377SAndreas Gohr if(!file_exists($lock)) return false; 950f3f0262cSandi 951f3f0262cSandi //lockfile expired 952f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 953d8186216SBen Coburn @unlink($lock); 954f3f0262cSandi return false; 955f3f0262cSandi } 956f3f0262cSandi 957f3f0262cSandi //my own lock 9586d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 9590712fefaSAndreas Gohr if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) { 960f3f0262cSandi return false; 961f3f0262cSandi } 962f3f0262cSandi 963f3f0262cSandi return $ip; 964f3f0262cSandi} 965f3f0262cSandi 966f3f0262cSandi/** 96715fae107Sandi * Lock a page for editing 96815fae107Sandi * 96915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 970140cfbcdSGerrit Uitslag * 971140cfbcdSGerrit Uitslag * @param string $id page id to lock 972f3f0262cSandi */ 973f3f0262cSandifunction lock($id) { 974544ed901SDaniel Calviño Sánchez global $conf; 975585bf44eSChristopher Smith /* @var Input $INPUT */ 976585bf44eSChristopher Smith global $INPUT; 977544ed901SDaniel Calviño Sánchez 978544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 979544ed901SDaniel Calviño Sánchez return; 980544ed901SDaniel Calviño Sánchez } 981544ed901SDaniel Calviño Sánchez 982c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 983585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 984585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 985f3f0262cSandi } else { 98685fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 987f3f0262cSandi } 988f3f0262cSandi} 989f3f0262cSandi 990f3f0262cSandi/** 99115fae107Sandi * Unlock a page if it was locked by the user 992f3f0262cSandi * 99315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 994140cfbcdSGerrit Uitslag * 9953272d797SAndreas Gohr * @param string $id page id to unlock 99615fae107Sandi * @return bool true if a lock was removed 997f3f0262cSandi */ 998f3f0262cSandifunction unlock($id) { 999585bf44eSChristopher Smith /* @var Input $INPUT */ 1000585bf44eSChristopher Smith global $INPUT; 1001585bf44eSChristopher Smith 1002c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 100379e79377SAndreas Gohr if(file_exists($lock)) { 10046d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 1005585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 1006f3f0262cSandi @unlink($lock); 1007f3f0262cSandi return true; 1008f3f0262cSandi } 1009f3f0262cSandi } 1010f3f0262cSandi return false; 1011f3f0262cSandi} 1012f3f0262cSandi 1013f3f0262cSandi/** 1014f3f0262cSandi * convert line ending to unix format 1015f3f0262cSandi * 10166db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 10176db7468bSAndreas Gohr * 101815fae107Sandi * @see formText() for 2crlf conversion 101915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1020140cfbcdSGerrit Uitslag * 1021140cfbcdSGerrit Uitslag * @param string $text 1022140cfbcdSGerrit Uitslag * @return string 1023f3f0262cSandi */ 1024f3f0262cSandifunction cleanText($text) { 1025f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10266db7468bSAndreas Gohr 10276db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10286db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10296db7468bSAndreas Gohr // but might actually fix the problem in many cases 10308cbc5ee8SAndreas Gohr if(!\dokuwiki\Utf8\Clean::isUtf8($text)) $text = utf8_encode($text); 10316db7468bSAndreas Gohr 1032f3f0262cSandi return $text; 1033f3f0262cSandi} 1034f3f0262cSandi 1035f3f0262cSandi/** 1036f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1037f3f0262cSandi * It also converts line endings to Windows format which is 1038f3f0262cSandi * pseudo standard for webforms. 1039f3f0262cSandi * 104015fae107Sandi * @see cleanText() for 2unix conversion 104115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1042140cfbcdSGerrit Uitslag * 1043140cfbcdSGerrit Uitslag * @param string $text 1044140cfbcdSGerrit Uitslag * @return string 1045f3f0262cSandi */ 1046f3f0262cSandifunction formText($text) { 10475b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 1048f3f0262cSandi return htmlspecialchars($text); 1049f3f0262cSandi} 1050f3f0262cSandi 1051f3f0262cSandi/** 105215fae107Sandi * Returns the specified local text in raw format 105315fae107Sandi * 105415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1055140cfbcdSGerrit Uitslag * 1056140cfbcdSGerrit Uitslag * @param string $id page id 1057140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1058140cfbcdSGerrit Uitslag * @return string 1059f3f0262cSandi */ 10602adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10612adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1062f3f0262cSandi} 1063f3f0262cSandi 1064f3f0262cSandi/** 1065f3f0262cSandi * Returns the raw WikiText 106615fae107Sandi * 106715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1068140cfbcdSGerrit Uitslag * 1069140cfbcdSGerrit Uitslag * @param string $id page id 1070e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1071140cfbcdSGerrit Uitslag * @return string 1072f3f0262cSandi */ 1073f3f0262cSandifunction rawWiki($id, $rev = '') { 1074cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1075f3f0262cSandi} 1076f3f0262cSandi 1077f3f0262cSandi/** 10787146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10797146cee2SAndreas Gohr * 10807b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10817146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1082140cfbcdSGerrit Uitslag * 1083140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1084140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10857146cee2SAndreas Gohr */ 1086fe17917eSAdrian Langfunction pageTemplate($id) { 1087a15ce62dSEsther Brunner global $conf; 1088e29549feSAndreas Gohr 1089fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1090e29549feSAndreas Gohr 10917b84afa2SAndreas Gohr // prepare initial event data 10927b84afa2SAndreas Gohr $data = array( 10937b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10947b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10957b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10967b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10977b84afa2SAndreas Gohr ); 10987b84afa2SAndreas Gohr 1099e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_PAGETPL_LOAD', $data); 11007b84afa2SAndreas Gohr if($evt->advise_before(true)) { 11017b84afa2SAndreas Gohr // the before event might have loaded the content already 11027b84afa2SAndreas Gohr if(empty($data['tpl'])) { 11037b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 11047b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1105fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 110679e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 11077b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1108e29549feSAndreas Gohr } else { 1109e29549feSAndreas Gohr // search upper namespaces for templates 1110e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1111e29549feSAndreas Gohr while(strlen($path) >= $len) { 111279e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 11137b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1114e29549feSAndreas Gohr break; 1115e29549feSAndreas Gohr } 1116e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1117e29549feSAndreas Gohr } 1118e29549feSAndreas Gohr } 11197b84afa2SAndreas Gohr } 11207b84afa2SAndreas Gohr // load the content 11213d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 11227b84afa2SAndreas Gohr } 1123a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 11247b84afa2SAndreas Gohr } 11257b84afa2SAndreas Gohr $evt->advise_after(); 11267b84afa2SAndreas Gohr unset($evt); 11277b84afa2SAndreas Gohr 1128fe17917eSAdrian Lang return $data['tpl']; 11292b1223ecSAdrian Lang} 11302b1223ecSAdrian Lang 11312b1223ecSAdrian Lang/** 11322b1223ecSAdrian Lang * Performs common page template replacements 11337b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11342b1223ecSAdrian Lang * 11352b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1136140cfbcdSGerrit Uitslag * 1137140cfbcdSGerrit Uitslag * @param array $data array with event data 1138140cfbcdSGerrit Uitslag * @return string 11392b1223ecSAdrian Lang */ 1140d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11413272d797SAndreas Gohr /** 11423272d797SAndreas Gohr * @var string $id the id of the page to be created 11433272d797SAndreas Gohr * @var string $tpl the text used as template 11443272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11453272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11463272d797SAndreas Gohr */ 1147fe17917eSAdrian Lang extract($data); 1148fe17917eSAdrian Lang 1149b856f7dfSAdrian Lang global $USERINFO; 1150bce53b1fSAdrian Lang global $conf; 1151585bf44eSChristopher Smith /* @var Input $INPUT */ 1152585bf44eSChristopher Smith global $INPUT; 1153e29549feSAndreas Gohr 1154e29549feSAndreas Gohr // replace placeholders 115526ece5a7SAndreas Gohr $file = noNS($id); 115637c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 115726ece5a7SAndreas Gohr 11583272d797SAndreas Gohr $tpl = str_replace( 11593272d797SAndreas Gohr array( 116026ece5a7SAndreas Gohr '@ID@', 116126ece5a7SAndreas Gohr '@NS@', 11628a7bcf66SShota Miyazaki '@CURNS@', 1163a3db0ab0SSimon Lees '@!CURNS@', 1164a3db0ab0SSimon Lees '@!!CURNS@', 1165a3db0ab0SSimon Lees '@!CURNS!@', 116626ece5a7SAndreas Gohr '@FILE@', 116726ece5a7SAndreas Gohr '@!FILE@', 116826ece5a7SAndreas Gohr '@!FILE!@', 116926ece5a7SAndreas Gohr '@PAGE@', 117026ece5a7SAndreas Gohr '@!PAGE@', 117126ece5a7SAndreas Gohr '@!!PAGE@', 117226ece5a7SAndreas Gohr '@!PAGE!@', 117326ece5a7SAndreas Gohr '@USER@', 117426ece5a7SAndreas Gohr '@NAME@', 117526ece5a7SAndreas Gohr '@MAIL@', 117626ece5a7SAndreas Gohr '@DATE@', 117726ece5a7SAndreas Gohr ), 117826ece5a7SAndreas Gohr array( 117926ece5a7SAndreas Gohr $id, 118026ece5a7SAndreas Gohr getNS($id), 11818a7bcf66SShota Miyazaki curNS($id), 1182a3db0ab0SSimon Lees utf8_ucfirst(curNS($id)), 1183a3db0ab0SSimon Lees utf8_ucwords(curNS($id)), 1184a3db0ab0SSimon Lees utf8_strtoupper(curNS($id)), 118526ece5a7SAndreas Gohr $file, 11868cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($file), 11878cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($file), 118826ece5a7SAndreas Gohr $page, 11898cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($page), 11908cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords($page), 11918cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($page), 1192585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11933e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 11943e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 119526ece5a7SAndreas Gohr $conf['dformat'], 11963272d797SAndreas Gohr ), $tpl 11973272d797SAndreas Gohr ); 119826ece5a7SAndreas Gohr 11997d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1200bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1201bad6fc0dSAndreas Gohr '/%./', 1202bad6fc0dSAndreas Gohr function ($m) { 1203bad6fc0dSAndreas Gohr return strftime($m[0]); 1204bad6fc0dSAndreas Gohr }, 1205bad6fc0dSAndreas Gohr $tpl 1206bad6fc0dSAndreas Gohr ); 1207d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1208a15ce62dSEsther Brunner return $tpl; 12097146cee2SAndreas Gohr} 12107146cee2SAndreas Gohr 12117146cee2SAndreas Gohr/** 121215fae107Sandi * Returns the raw Wiki Text in three slices. 121315fae107Sandi * 121415fae107Sandi * The range parameter needs to have the form "from-to" 121515cfe303Sandi * and gives the range of the section in bytes - no 121615cfe303Sandi * UTF-8 awareness is needed. 1217f3f0262cSandi * The returned order is prefix, section and suffix. 121815fae107Sandi * 121915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1220140cfbcdSGerrit Uitslag * 1221140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1222140cfbcdSGerrit Uitslag * @param string $id page id 1223140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 122442ea7f44SGerrit Uitslag * @return string[] with three slices 1225f3f0262cSandi */ 1226f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1227cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1228f3f0262cSandi 122980fcb268SAdrian Lang // Parse range 123080fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 123180fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 123280fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 123380fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 123480fcb268SAdrian Lang 123559bc3b48SGerrit Uitslag $slices = array(); 123680fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 123780fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 123815cfe303Sandi $slices[2] = substr($text, $to); 1239f3f0262cSandi return $slices; 1240f3f0262cSandi} 1241f3f0262cSandi 1242f3f0262cSandi/** 124315fae107Sandi * Joins wiki text slices 124415fae107Sandi * 124580fcb268SAdrian Lang * function to join the text slices. 1246f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1247f3f0262cSandi * lines between sections if needed (used on saving). 124815fae107Sandi * 124915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1250140cfbcdSGerrit Uitslag * 1251140cfbcdSGerrit Uitslag * @param string $pre prefix 1252140cfbcdSGerrit Uitslag * @param string $text text in the middle 1253140cfbcdSGerrit Uitslag * @param string $suf suffix 1254140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1255140cfbcdSGerrit Uitslag * @return string 1256f3f0262cSandi */ 1257f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1258f3f0262cSandi if($pretty) { 125980fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12603272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12613272d797SAndreas Gohr ) { 126280fcb268SAdrian Lang $pre .= "\n"; 126380fcb268SAdrian Lang } 126480fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12653272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12663272d797SAndreas Gohr ) { 126780fcb268SAdrian Lang $text .= "\n"; 126880fcb268SAdrian Lang } 1269f3f0262cSandi } 1270f3f0262cSandi 1271f3f0262cSandi return $pre.$text.$suf; 1272f3f0262cSandi} 1273f3f0262cSandi 1274f3f0262cSandi/** 1275b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1276b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1277b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1278b24d9195SAndreas Gohr * 1279b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1280b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1281b24d9195SAndreas Gohr * 1282b24d9195SAndreas Gohr * @param string $id the page ID 1283b24d9195SAndreas Gohr */ 1284b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 1285b24d9195SAndreas Gohr global $lang; 1286b24d9195SAndreas Gohr 12878c7319beSGerrit Uitslag $fileLastMod = wikiFN($id); 12888c7319beSGerrit Uitslag $lastMod = @filemtime($fileLastMod); // from page 1289b24d9195SAndreas Gohr $pagelog = new PageChangeLog($id, 1024); 12908c7319beSGerrit Uitslag $lastRev = $pagelog->getRevisions(-1, 1); // from changelog 12918c7319beSGerrit Uitslag $lastRev = (int) (empty($lastRev) ? 0 : $lastRev[0]); 1292b24d9195SAndreas Gohr 12938c7319beSGerrit Uitslag if(!file_exists(wikiFN($id, $lastMod)) && file_exists($fileLastMod) && $lastMod >= $lastRev) { 1294b24d9195SAndreas Gohr // add old revision to the attic if missing 1295b24d9195SAndreas Gohr saveOldRevision($id); 1296b24d9195SAndreas Gohr // add a changelog entry if this edit came from outside dokuwiki 12978c7319beSGerrit Uitslag if($lastMod > $lastRev) { 12988c7319beSGerrit Uitslag $fileLastRev = wikiFN($id, $lastRev); 12998c7319beSGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($lastRev); 13003c48b1d0SGerrit Uitslag if(empty($lastRev) || !file_exists($fileLastRev) || $revinfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 13014b5aebc1SGerrit Uitslag $filesize_old = 0; 13024b5aebc1SGerrit Uitslag } else { 13038c7319beSGerrit Uitslag $filesize_old = io_getSizeFile($fileLastRev); 13044b5aebc1SGerrit Uitslag } 13058c7319beSGerrit Uitslag $filesize_new = filesize($fileLastMod); 13062966355bSGerrit Uitslag $sizechange = $filesize_new - $filesize_old; 13072966355bSGerrit Uitslag 130864159a61SAndreas Gohr addLogEntry( 130964159a61SAndreas Gohr $lastMod, 131064159a61SAndreas Gohr $id, 131164159a61SAndreas Gohr DOKU_CHANGE_TYPE_EDIT, 131264159a61SAndreas Gohr $lang['external_edit'], 131364159a61SAndreas Gohr '', 131464159a61SAndreas Gohr array('ExternalEdit' => true), 131564159a61SAndreas Gohr $sizechange 131664159a61SAndreas Gohr ); 1317b24d9195SAndreas Gohr // remove soon to be stale instructions 13180db5771eSMichael Große $cache = new CacheInstructions($id, $fileLastMod); 1319b24d9195SAndreas Gohr $cache->removeCache(); 1320b24d9195SAndreas Gohr } 1321b24d9195SAndreas Gohr } 1322b24d9195SAndreas Gohr} 1323b24d9195SAndreas Gohr 1324b24d9195SAndreas Gohr/** 1325a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1326a701424fSBen Coburn * Also directs changelog and attic updates. 132715fae107Sandi * 132815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 132971726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1330140cfbcdSGerrit Uitslag * 1331140cfbcdSGerrit Uitslag * @param string $id page id 1332140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1333140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1334140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1335f3f0262cSandi */ 1336b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1337a701424fSBen Coburn /* Note to developers: 1338a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1339a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1340a701424fSBen Coburn after any changes. External edits change the wiki page 1341a701424fSBen Coburn directly without using php or dokuwiki. 1342a701424fSBen Coburn */ 1343f3f0262cSandi global $conf; 1344f3f0262cSandi global $lang; 134571726d78SBen Coburn global $REV; 1346585bf44eSChristopher Smith /* @var Input $INPUT */ 1347585bf44eSChristopher Smith global $INPUT; 1348585bf44eSChristopher Smith 1349b24d9195SAndreas Gohr // prepare data for event 1350b24d9195SAndreas Gohr $svdta = array(); 1351b24d9195SAndreas Gohr $svdta['id'] = $id; 1352b24d9195SAndreas Gohr $svdta['file'] = wikiFN($id); 1353b24d9195SAndreas Gohr $svdta['revertFrom'] = $REV; 1354b24d9195SAndreas Gohr $svdta['oldRevision'] = @filemtime($svdta['file']); 1355b24d9195SAndreas Gohr $svdta['newRevision'] = 0; 1356b24d9195SAndreas Gohr $svdta['newContent'] = $text; 1357b24d9195SAndreas Gohr $svdta['oldContent'] = rawWiki($id); 1358b24d9195SAndreas Gohr $svdta['summary'] = $summary; 1359b24d9195SAndreas Gohr $svdta['contentChanged'] = ($svdta['newContent'] != $svdta['oldContent']); 1360b24d9195SAndreas Gohr $svdta['changeInfo'] = ''; 1361b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_EDIT; 13622966355bSGerrit Uitslag $svdta['sizechange'] = null; 1363b24d9195SAndreas Gohr 1364b24d9195SAndreas Gohr // select changelog line type 1365b24d9195SAndreas Gohr if($REV) { 1366b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_REVERT; 1367b24d9195SAndreas Gohr $svdta['changeInfo'] = $REV; 1368b24d9195SAndreas Gohr } else if(!file_exists($svdta['file'])) { 1369b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_CREATE; 1370b24d9195SAndreas Gohr } else if(trim($text) == '') { 1371b24d9195SAndreas Gohr // empty or whitespace only content deletes 1372b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_DELETE; 1373b24d9195SAndreas Gohr // autoset summary on deletion 1374655ddc1dSGerrit Uitslag if(blank($svdta['summary'])) { 1375655ddc1dSGerrit Uitslag $svdta['summary'] = $lang['deleted']; 1376655ddc1dSGerrit Uitslag } 1377b24d9195SAndreas Gohr } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 1378b24d9195SAndreas Gohr //minor edits only for logged in users 1379b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_MINOR_EDIT; 1380f3f0262cSandi } 1381f3f0262cSandi 1382e1d9dcc8SAndreas Gohr $event = new Event('COMMON_WIKIPAGE_SAVE', $svdta); 1383b24d9195SAndreas Gohr if(!$event->advise_before()) return; 1384f3f0262cSandi 1385b24d9195SAndreas Gohr // if the content has not been changed, no save happens (plugins may override this) 1386b24d9195SAndreas Gohr if(!$svdta['contentChanged']) return; 1387b24d9195SAndreas Gohr 1388b24d9195SAndreas Gohr detectExternalEdit($id); 1389f3f0262cSandi 13904b5aebc1SGerrit Uitslag if( 13914b5aebc1SGerrit Uitslag $svdta['changeType'] == DOKU_CHANGE_TYPE_CREATE || 13924b5aebc1SGerrit Uitslag ($svdta['changeType'] == DOKU_CHANGE_TYPE_REVERT && !file_exists($svdta['file'])) 13934b5aebc1SGerrit Uitslag ) { 1394ac3ed4afSGerrit Uitslag $filesize_old = 0; 1395ac3ed4afSGerrit Uitslag } else { 13962966355bSGerrit Uitslag $filesize_old = filesize($svdta['file']); 1397ac3ed4afSGerrit Uitslag } 1398b24d9195SAndreas Gohr if($svdta['changeType'] == DOKU_CHANGE_TYPE_DELETE) { 139930725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 1400b24d9195SAndreas Gohr $data = array(array($svdta['file'], '', false), getNS($id), noNS($id), false); 1401cbb44eabSAndreas Gohr Event::createAndTrigger('IO_WIKIPAGE_WRITE', $data); 1402e45b34cdSBen Coburn // pre-save deleted revision 1403b24d9195SAndreas Gohr @touch($svdta['file']); 140446844156SBen Coburn clearstatcache(); 14052d69eb44SMichael Hamann $svdta['newRevision'] = saveOldRevision($id); 1406e1f3d9e1SEsther Brunner // remove empty file 1407b24d9195SAndreas Gohr @unlink($svdta['file']); 1408ac3ed4afSGerrit Uitslag $filesize_new = 0; 140964159a61SAndreas Gohr // don't remove old meta info as it should be saved, plugins can use 141064159a61SAndreas Gohr // IO_WIKIPAGE_WRITE for removing their metadata... 1411c5f92742SMichael Hamann // purge non-persistant meta data 14123d1f9ec3SMichael Klier p_purge_metadata($id); 141353d6ccfeSandi // remove empty namespaces 1414cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1415cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1416f3f0262cSandi } else { 1417cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 141833d979e7SMichael Große io_writeWikiPage($svdta['file'], $svdta['newContent'], $id); 141946844156SBen Coburn // pre-save the revision, to keep the attic in sync 1420b24d9195SAndreas Gohr $svdta['newRevision'] = saveOldRevision($id); 14212966355bSGerrit Uitslag $filesize_new = filesize($svdta['file']); 1422f3f0262cSandi } 14232966355bSGerrit Uitslag $svdta['sizechange'] = $filesize_new - $filesize_old; 1424f3f0262cSandi 1425b24d9195SAndreas Gohr $event->advise_after(); 142671726d78SBen Coburn 142764159a61SAndreas Gohr addLogEntry( 142864159a61SAndreas Gohr $svdta['newRevision'], 142964159a61SAndreas Gohr $svdta['id'], 143064159a61SAndreas Gohr $svdta['changeType'], 143164159a61SAndreas Gohr $svdta['summary'], 143264159a61SAndreas Gohr $svdta['changeInfo'], 143364159a61SAndreas Gohr null, 143464159a61SAndreas Gohr $svdta['sizechange'] 143564159a61SAndreas Gohr ); 1436ac3ed4afSGerrit Uitslag 143726a0801fSAndreas Gohr // send notify mails 143883734cddSPhy notify($svdta['id'], 'admin', $svdta['oldRevision'], $svdta['summary'], $minor, $svdta['newRevision']); 143983734cddSPhy notify($svdta['id'], 'subscribers', $svdta['oldRevision'], $svdta['summary'], $minor, $svdta['newRevision']); 1440f3f0262cSandi 1441ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 144298407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 14432eccbdaaSGina Haeussge 14442eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1445fe9ec250SChris Smith if(useHeading('content')) { 144607ff0babSMichael Hamann $pages = ft_backlinks($id, true); 14472eccbdaaSGina Haeussge foreach($pages as $page) { 14480db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 14492eccbdaaSGina Haeussge $cache->removeCache(); 14502eccbdaaSGina Haeussge } 14512eccbdaaSGina Haeussge } 1452f3f0262cSandi} 1453f3f0262cSandi 1454f3f0262cSandi/** 1455f3f0262cSandi * moves the current version to the attic and returns its 1456f3f0262cSandi * revision date 145715fae107Sandi * 145815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1459140cfbcdSGerrit Uitslag * 1460140cfbcdSGerrit Uitslag * @param string $id page id 1461140cfbcdSGerrit Uitslag * @return int|string revision timestamp 1462f3f0262cSandi */ 1463f3f0262cSandifunction saveOldRevision($id) { 1464f3f0262cSandi $oldf = wikiFN($id); 146579e79377SAndreas Gohr if(!file_exists($oldf)) return ''; 1466f3f0262cSandi $date = filemtime($oldf); 1467f3f0262cSandi $newf = wikiFN($id, $date); 1468cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1469f3f0262cSandi return $date; 1470f3f0262cSandi} 1471f3f0262cSandi 1472f3f0262cSandi/** 1473fde10de4SAdrian Lang * Sends a notify mail on page change or registration 147426a0801fSAndreas Gohr * 147526a0801fSAndreas Gohr * @param string $id The changed page 1476fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 14773272d797SAndreas Gohr * @param int|string $rev Old page revision 147826a0801fSAndreas Gohr * @param string $summary What changed 147990033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 148042ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 148183734cddSPhy * @param int|string $current_rev New page revision 14823272d797SAndreas Gohr * @return bool 1483140cfbcdSGerrit Uitslag * 148415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1485f3f0262cSandi */ 148683734cddSPhyfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array(), $current_rev = false) { 1487f3f0262cSandi global $conf; 1488585bf44eSChristopher Smith /* @var Input $INPUT */ 1489585bf44eSChristopher Smith global $INPUT; 1490b158d625SSteven Danz 14916df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 149226a0801fSAndreas Gohr if($who == 'admin') { 14933272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 14942ed38036SAndreas Gohr $tpl = 'mailtext'; 149526a0801fSAndreas Gohr $to = $conf['notify']; 149626a0801fSAndreas Gohr } elseif($who == 'subscribers') { 149784c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1498585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 14990bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 1500cbb44eabSAndreas Gohr Event::createAndTrigger( 15013272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1502835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 15033272d797SAndreas Gohr ); 15042ed38036SAndreas Gohr $to = $data['addresslist']; 15052ed38036SAndreas Gohr if(empty($to)) return false; 15062ed38036SAndreas Gohr $tpl = 'subscr_single'; 150726a0801fSAndreas Gohr } else { 15083272d797SAndreas Gohr return false; //just to be safe 150926a0801fSAndreas Gohr } 151026a0801fSAndreas Gohr 15116df843eeSAndreas Gohr // prepare content 1512704a815fSMichael Große $subscription = new PageSubscriptionSender(); 151383734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1514f3f0262cSandi} 15152ed38036SAndreas Gohr 151615fae107Sandi/** 151771f7bde7SAndreas Gohr * extracts the query from a search engine referrer 151815fae107Sandi * 151915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 152071f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1521140cfbcdSGerrit Uitslag * 1522140cfbcdSGerrit Uitslag * @return array|string 1523f3f0262cSandi */ 1524f3f0262cSandifunction getGoogleQuery() { 1525585bf44eSChristopher Smith /* @var Input $INPUT */ 1526585bf44eSChristopher Smith global $INPUT; 1527585bf44eSChristopher Smith 1528585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1529c66972f2SAdrian Lang return ''; 1530c66972f2SAdrian Lang } 1531585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1532f3f0262cSandi 1533079b3ac1SAndreas Gohr // only handle common SEs 1534079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1535e4d8a516SKazutaka Miyasaka 1536079b3ac1SAndreas Gohr $query = array(); 1537f3f0262cSandi parse_str($url['query'], $query); 1538e4d8a516SKazutaka Miyasaka 1539c66972f2SAdrian Lang $q = ''; 1540079b3ac1SAndreas Gohr if(isset($query['q'])){ 1541079b3ac1SAndreas Gohr $q = $query['q']; 1542079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1543079b3ac1SAndreas Gohr $q = $query['p']; 1544079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1545079b3ac1SAndreas Gohr $q = $query['query']; 1546079b3ac1SAndreas Gohr } 1547079b3ac1SAndreas Gohr $q = trim($q); 1548f3f0262cSandi 1549079b3ac1SAndreas Gohr if(!$q) return ''; 1550c7dc833bSPhy // ignore if query includes a full URL 1551c7dc833bSPhy if(strpos($q, '//') !== false) return ''; 15526531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1553f93b3b50SAndreas Gohr return $q; 1554f3f0262cSandi} 1555f3f0262cSandi 1556f3f0262cSandi/** 1557f3f0262cSandi * Return the human readable size of a file 1558f3f0262cSandi * 1559f3f0262cSandi * @param int $size A file size 1560f3f0262cSandi * @param int $dec A number of decimal places 156174160ca1SGerrit Uitslag * @return string human readable size 1562140cfbcdSGerrit Uitslag * 1563f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1564f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1565f3f0262cSandi * @version 1.0.0 1566f3f0262cSandi */ 1567f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1568f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1569f3f0262cSandi $count = count($sizes); 1570f3f0262cSandi $i = 0; 1571f3f0262cSandi 1572f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1573f3f0262cSandi $size /= 1024; 1574f3f0262cSandi $i++; 1575f3f0262cSandi } 1576f3f0262cSandi 1577ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1578f3f0262cSandi} 1579f3f0262cSandi 158015fae107Sandi/** 1581c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1582c57e365eSAndreas Gohr * 1583c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1584140cfbcdSGerrit Uitslag * 1585140cfbcdSGerrit Uitslag * @param int $dt timestamp 1586140cfbcdSGerrit Uitslag * @return string 1587c57e365eSAndreas Gohr */ 1588c57e365eSAndreas Gohrfunction datetime_h($dt) { 1589c57e365eSAndreas Gohr global $lang; 1590c57e365eSAndreas Gohr 1591c57e365eSAndreas Gohr $ago = time() - $dt; 1592c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1593c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1594c57e365eSAndreas Gohr } 1595c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1596c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1597c57e365eSAndreas Gohr } 1598c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1599c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1600c57e365eSAndreas Gohr } 1601c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1602c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1603c57e365eSAndreas Gohr } 1604c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1605c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1606c57e365eSAndreas Gohr } 1607c57e365eSAndreas Gohr if($ago > 60 * 2) { 1608c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1609c57e365eSAndreas Gohr } 1610c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1611c57e365eSAndreas Gohr} 1612c57e365eSAndreas Gohr 1613c57e365eSAndreas Gohr/** 1614f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1615f2263577SAndreas Gohr * 1616f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1617f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1618f2263577SAndreas Gohr * 1619f2263577SAndreas Gohr * @see datetime_h 1620f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1621140cfbcdSGerrit Uitslag * 1622140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1623140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1624140cfbcdSGerrit Uitslag * @return string 1625f2263577SAndreas Gohr */ 1626f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1627f2263577SAndreas Gohr global $conf; 1628f2263577SAndreas Gohr 1629f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1630f2263577SAndreas Gohr $dt = (int) $dt; 1631f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1632f2263577SAndreas Gohr 1633f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1634f2263577SAndreas Gohr return strftime($format, $dt); 1635f2263577SAndreas Gohr} 1636f2263577SAndreas Gohr 1637f2263577SAndreas Gohr/** 1638c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1639c4f79b71SMichael Hamann * 1640c4f79b71SMichael Hamann * @author <ungu at terong dot com> 164159752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1642140cfbcdSGerrit Uitslag * 16437e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 16443272d797SAndreas Gohr * @return string 1645c4f79b71SMichael Hamann */ 1646c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1647c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1648c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1649c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1650c4f79b71SMichael Hamann $date_mod .= $time_zone; 1651c4f79b71SMichael Hamann return $date_mod; 1652c4f79b71SMichael Hamann} 1653c4f79b71SMichael Hamann 1654c4f79b71SMichael Hamann/** 165500a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 165600a7b5adSEsther Brunner * 165700a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 165800a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1659140cfbcdSGerrit Uitslag * 1660140cfbcdSGerrit Uitslag * @param string $email email address 1661140cfbcdSGerrit Uitslag * @return string 166200a7b5adSEsther Brunner */ 166300a7b5adSEsther Brunnerfunction obfuscate($email) { 166400a7b5adSEsther Brunner global $conf; 166500a7b5adSEsther Brunner 166600a7b5adSEsther Brunner switch($conf['mailguard']) { 166700a7b5adSEsther Brunner case 'visible' : 166800a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 166900a7b5adSEsther Brunner return strtr($email, $obfuscate); 167000a7b5adSEsther Brunner 167100a7b5adSEsther Brunner case 'hex' : 1672debc52aaSPhy return utf8_tohtml($email, true); 167300a7b5adSEsther Brunner 167400a7b5adSEsther Brunner case 'none' : 167500a7b5adSEsther Brunner default : 167600a7b5adSEsther Brunner return $email; 167700a7b5adSEsther Brunner } 167800a7b5adSEsther Brunner} 167900a7b5adSEsther Brunner 168000a7b5adSEsther Brunner/** 168189541d4bSAndreas Gohr * Removes quoting backslashes 168289541d4bSAndreas Gohr * 168389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1684140cfbcdSGerrit Uitslag * 1685140cfbcdSGerrit Uitslag * @param string $string 1686140cfbcdSGerrit Uitslag * @param string $char backslashed character 1687140cfbcdSGerrit Uitslag * @return string 168889541d4bSAndreas Gohr */ 168989541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 169089541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 169189541d4bSAndreas Gohr} 169289541d4bSAndreas Gohr 169373038c47SAndreas Gohr/** 169473038c47SAndreas Gohr * Convert php.ini shorthands to byte 169573038c47SAndreas Gohr * 1696a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1697140cfbcdSGerrit Uitslag * 1698a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1699a81f3d99SAndreas Gohr * 1700a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1701a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1702a81f3d99SAndreas Gohr * @return int 170373038c47SAndreas Gohr */ 1704a81f3d99SAndreas Gohrfunction php_to_byte($value) { 1705f5c0c80bSAndreas Gohr switch (strtoupper(substr($value,-1))) { 170673038c47SAndreas Gohr case 'G': 1707a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024 * 1024; 170873038c47SAndreas Gohr break; 170973038c47SAndreas Gohr case 'M': 1710a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024; 1711a81f3d99SAndreas Gohr break; 171273038c47SAndreas Gohr case 'K': 1713a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024; 171473038c47SAndreas Gohr break; 17159eeeb775SAndreas Gohr default: 1716a81f3d99SAndreas Gohr $ret = intval($value); 171749cbd23eSOtto Vainio break; 171873038c47SAndreas Gohr } 171973038c47SAndreas Gohr return $ret; 172073038c47SAndreas Gohr} 172173038c47SAndreas Gohr 1722546d3a99SAndreas Gohr/** 1723546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1724140cfbcdSGerrit Uitslag * 1725140cfbcdSGerrit Uitslag * @param string $string 1726140cfbcdSGerrit Uitslag * @return string 1727546d3a99SAndreas Gohr */ 1728546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1729546d3a99SAndreas Gohr return preg_quote($string, '/'); 1730546d3a99SAndreas Gohr} 173173038c47SAndreas Gohr 1732bd2f6c2fSAndreas Gohr/** 1733bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1734bd2f6c2fSAndreas Gohr * 1735c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1736bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1737bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1738bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1739bd2f6c2fSAndreas Gohr * 1740bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1741bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1742bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1743bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1744bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 17453272d797SAndreas Gohr * @return string 1746bd2f6c2fSAndreas Gohr */ 1747a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 17488cbc5ee8SAndreas Gohr $max = $max - \dokuwiki\Utf8\PhpString::strlen($keep); 1749bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 17508cbc5ee8SAndreas Gohr $len = \dokuwiki\Utf8\PhpString::strlen($short); 1751bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1752bd2f6c2fSAndreas Gohr $half = floor($max / 2); 17536ce3e5f8SAndreas Gohr return $keep . 17546ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, 0, $half - 1) . 17556ce3e5f8SAndreas Gohr $char . 17566ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, $len - $half); 1757bd2f6c2fSAndreas Gohr} 1758bd2f6c2fSAndreas Gohr 1759dc58b6f4SAndy Webber/** 1760dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1761dc58b6f4SAndy Webber * in page footer and recent changes pages 1762dc58b6f4SAndy Webber * 1763b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 176415f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1765c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 176615f3bc49SGerrit Uitslag * 1767dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1768dc58b6f4SAndy Webber */ 176915f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1770cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1771dc58b6f4SAndy Webber} 1772dc58b6f4SAndy Webber 177360a396c8SGerrit Uitslag/** 177460a396c8SGerrit Uitslag * Returns users realname w/o link 177560a396c8SGerrit Uitslag * 1776f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 177715f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1778c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 177960a396c8SGerrit Uitslag * 178060a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 178160a396c8SGerrit Uitslag */ 1782cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 178360a396c8SGerrit Uitslag global $conf, $INFO; 1784e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 178560a396c8SGerrit Uitslag global $auth; 178630f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 178730f6ec4bSGerrit Uitslag global $INPUT; 178860a396c8SGerrit Uitslag 178960a396c8SGerrit Uitslag // prepare initial event data 179060a396c8SGerrit Uitslag $data = array( 179160a396c8SGerrit Uitslag 'username' => $username, // the unique user name 179260a396c8SGerrit Uitslag 'name' => '', 179360a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 179460a396c8SGerrit Uitslag 'target' => '', 179560a396c8SGerrit Uitslag 'pre' => '', 179660a396c8SGerrit Uitslag 'suf' => '', 179760a396c8SGerrit Uitslag 'style' => '', 179860a396c8SGerrit Uitslag 'more' => '', 179960a396c8SGerrit Uitslag 'url' => '', 180060a396c8SGerrit Uitslag 'title' => '', 180160a396c8SGerrit Uitslag 'class' => '' 180260a396c8SGerrit Uitslag ), 18034d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 180415f3bc49SGerrit Uitslag 'textonly' => $textonly 180560a396c8SGerrit Uitslag ); 180662c8004eSGerrit Uitslag if($username === null) { 180730f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 180815f3bc49SGerrit Uitslag if($textonly){ 180915f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 181015f3bc49SGerrit Uitslag }else { 181164159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 181264159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 181360a396c8SGerrit Uitslag } 181415f3bc49SGerrit Uitslag } 181560a396c8SGerrit Uitslag 1816e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 181760a396c8SGerrit Uitslag if($evt->advise_before(true)) { 181860a396c8SGerrit Uitslag if(empty($data['name'])) { 181960a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 182065833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1821dc58b6f4SAndy Webber switch($conf['showuseras']) { 1822dc58b6f4SAndy Webber case 'username': 18237f081821SGerrit Uitslag case 'username_link': 182415f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 182560a396c8SGerrit Uitslag break; 1826dc58b6f4SAndy Webber case 'email': 1827dc58b6f4SAndy Webber case 'email_link': 182860a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 182960a396c8SGerrit Uitslag break; 1830dc58b6f4SAndy Webber } 183165833968SGerrit Uitslag } else { 183265833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 183360a396c8SGerrit Uitslag } 183460a396c8SGerrit Uitslag } 18357f081821SGerrit Uitslag 18367f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 18377f081821SGerrit Uitslag static $xhtml_renderer = null; 18387f081821SGerrit Uitslag 183915f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 18407f081821SGerrit Uitslag 18417f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 184260a396c8SGerrit Uitslag if(!isset($info)) { 184360a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 184460a396c8SGerrit Uitslag } 184560a396c8SGerrit Uitslag if(isset($info) && $info) { 18467f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 184760a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1848dc58b6f4SAndy Webber } else { 18497f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 18507f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 18517f081821SGerrit Uitslag } 18527f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 18537f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 18547f081821SGerrit Uitslag } 18557f081821SGerrit Uitslag $shortcut = 'user'; 1856533772e1SGerrit Uitslag $exists = null; 18576496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 18582a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 18596496c33fSGerrit Uitslag if($exists !== null) { 18606496c33fSGerrit Uitslag if($exists) { 18616496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 18626496c33fSGerrit Uitslag } else { 18636496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 18646496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 18656496c33fSGerrit Uitslag } 18666496c33fSGerrit Uitslag } 1867dc58b6f4SAndy Webber } 1868dc58b6f4SAndy Webber } else { 186915f3bc49SGerrit Uitslag $data['textonly'] = true; 1870dc58b6f4SAndy Webber } 187160a396c8SGerrit Uitslag 187260a396c8SGerrit Uitslag } else { 187315f3bc49SGerrit Uitslag $data['textonly'] = true; 187460a396c8SGerrit Uitslag } 187560a396c8SGerrit Uitslag } 187660a396c8SGerrit Uitslag 187715f3bc49SGerrit Uitslag if($data['textonly']) { 18784d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 187960a396c8SGerrit Uitslag } else { 188060a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 188160a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 188260a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 188360a396c8SGerrit Uitslag } 18844d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 188560a396c8SGerrit Uitslag } 188660a396c8SGerrit Uitslag } 188760a396c8SGerrit Uitslag $evt->advise_after(); 188860a396c8SGerrit Uitslag unset($evt); 188960a396c8SGerrit Uitslag 18904d5fc927SGerrit Uitslag return $data['userlink']; 1891066fee30SAndreas Gohr} 1892066fee30SAndreas Gohr 1893066fee30SAndreas Gohr/** 1894066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1895066fee30SAndreas Gohr * When no image exists, returns an empty string 1896066fee30SAndreas Gohr * 1897066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1898140cfbcdSGerrit Uitslag * 1899066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 19003272d797SAndreas Gohr * @return string 1901066fee30SAndreas Gohr */ 1902066fee30SAndreas Gohrfunction license_img($type) { 1903066fee30SAndreas Gohr global $license; 1904066fee30SAndreas Gohr global $conf; 1905066fee30SAndreas Gohr if(!$conf['license']) return ''; 1906066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1907066fee30SAndreas Gohr $try = array(); 1908066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1909066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1910066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1911066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1912066fee30SAndreas Gohr } 1913066fee30SAndreas Gohr foreach($try as $src) { 191479e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1915066fee30SAndreas Gohr } 1916066fee30SAndreas Gohr return ''; 1917dc58b6f4SAndy Webber} 1918dc58b6f4SAndy Webber 191913c08e2fSMichael Klier/** 192013c08e2fSMichael Klier * Checks if the given amount of memory is available 192113c08e2fSMichael Klier * 192213c08e2fSMichael Klier * If the memory_get_usage() function is not available the 192313c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 192413c08e2fSMichael Klier * 192513c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 192613c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 19273272d797SAndreas Gohr * 19283272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1929140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 19303272d797SAndreas Gohr * @return bool 193113c08e2fSMichael Klier */ 193213c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 193313c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 193413c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1935985d6187SElenchus if($limit == -1) return true; // unlimited 193613c08e2fSMichael Klier 193713c08e2fSMichael Klier // parse limit to bytes 193813c08e2fSMichael Klier $limit = php_to_byte($limit); 193913c08e2fSMichael Klier 194013c08e2fSMichael Klier // get used memory if possible 194113c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 194213c08e2fSMichael Klier $used = memory_get_usage(); 194349eb6e38SAndreas Gohr } else { 194449eb6e38SAndreas Gohr $used = $bytes; 194513c08e2fSMichael Klier } 194613c08e2fSMichael Klier 194713c08e2fSMichael Klier if($used + $mem > $limit) { 194813c08e2fSMichael Klier return false; 194913c08e2fSMichael Klier } 195013c08e2fSMichael Klier 195113c08e2fSMichael Klier return true; 195213c08e2fSMichael Klier} 195313c08e2fSMichael Klier 1954af2408d5SAndreas Gohr/** 1955af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1956af2408d5SAndreas Gohr * 1957af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1958af2408d5SAndreas Gohr * 1959af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1960af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1961140cfbcdSGerrit Uitslag * 1962140cfbcdSGerrit Uitslag * @param string $url url being directed to 1963af2408d5SAndreas Gohr */ 1964af2408d5SAndreas Gohrfunction send_redirect($url) { 196598ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 196698ca30d2SAndreas Gohr 1967585bf44eSChristopher Smith /* @var Input $INPUT */ 1968585bf44eSChristopher Smith global $INPUT; 1969585bf44eSChristopher Smith 19700181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 19710181f021SAndreas Gohr global $MSG; 19720181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 19730181f021SAndreas Gohr //reopen session, store data and close session again 19740181f021SAndreas Gohr @session_start(); 19750181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 19760181f021SAndreas Gohr } 19770181f021SAndreas Gohr 1978d4869846SAndreas Gohr // always close the session 1979d4869846SAndreas Gohr session_write_close(); 1980d4869846SAndreas Gohr 1981af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1982585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1983585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1984585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 19853272d797SAndreas Gohr $matches[1] < 6 19863272d797SAndreas Gohr ) { 1987af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1988af2408d5SAndreas Gohr } else { 1989af2408d5SAndreas Gohr header('Location: '.$url); 1990af2408d5SAndreas Gohr } 199181781cb6SAndreas Gohr 1992572dc222SLarsDW223 // no exits during unit tests 199327c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 199427c0c399SAndreas Gohr // pass info about the redirect back to the test suite 199527c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 199627c0c399SAndreas Gohr if($testRequest !== null) { 199727c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 199827c0c399SAndreas Gohr } 1999572dc222SLarsDW223 return; 2000572dc222SLarsDW223 } 200127c0c399SAndreas Gohr 2002af2408d5SAndreas Gohr exit; 2003af2408d5SAndreas Gohr} 2004af2408d5SAndreas Gohr 20055b75cd1fSAdrian Lang/** 20065b75cd1fSAdrian Lang * Validate a value using a set of valid values 20075b75cd1fSAdrian Lang * 20085b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 20095b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 20105b75cd1fSAdrian Lang * default is specified, throws an exception. 20115b75cd1fSAdrian Lang * 20125b75cd1fSAdrian Lang * @param string $param The name of the parameter 20135b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 20145b75cd1fSAdrian Lang * be marked by the key “default”. 20155b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 20165b75cd1fSAdrian Lang * or $_GET) 20175b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 20185b75cd1fSAdrian Lang * 20193272d797SAndreas Gohr * @throws Exception 20203272d797SAndreas Gohr * @return mixed 20215b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 20225b75cd1fSAdrian Lang */ 20235b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 20245b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 20255b75cd1fSAdrian Lang return $array[$param]; 20265b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 20275b75cd1fSAdrian Lang return $valid_values['default']; 20285b75cd1fSAdrian Lang } else { 20295b75cd1fSAdrian Lang throw new Exception($exc); 20305b75cd1fSAdrian Lang } 20315b75cd1fSAdrian Lang} 20325b75cd1fSAdrian Lang 203363703ba5SAndreas Gohr/** 203463703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 2035646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 2036140cfbcdSGerrit Uitslag * 2037140cfbcdSGerrit Uitslag * @param string $pref preference key 2038b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 2039140cfbcdSGerrit Uitslag * @return string preference value 204063703ba5SAndreas Gohr */ 2041554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 2042646a531aSChristopher Smith $enc_pref = urlencode($pref); 204306c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 2044554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 204563703ba5SAndreas Gohr $cnt = count($parts); 20461c3eca7dSPhy 20471c3eca7dSPhy // due to #2721 there might be duplicate entries, 20481c3eca7dSPhy // so we read from the end 20491c3eca7dSPhy for($i = $cnt-2; $i >= 0; $i -= 2) { 2050646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 2051646a531aSChristopher Smith return urldecode($parts[$i + 1]); 2052554a8c9fSAdrian Lang } 2053554a8c9fSAdrian Lang } 2054554a8c9fSAdrian Lang } 2055554a8c9fSAdrian Lang return $default; 2056554a8c9fSAdrian Lang} 2057554a8c9fSAdrian Lang 20583c94d07bSAnika Henke/** 20593c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 206036ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 20613a970889SAnika Henke * Remove it by setting $val to false 2062140cfbcdSGerrit Uitslag * 2063140cfbcdSGerrit Uitslag * @param string $pref preference key 2064140cfbcdSGerrit Uitslag * @param string $val preference value 20653c94d07bSAnika Henke */ 20663c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 20673c94d07bSAnika Henke global $conf; 20683c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 20693c94d07bSAnika Henke $cookieVal = ''; 20703c94d07bSAnika Henke 20711c3eca7dSPhy if($orig !== false && ($orig !== $val)) { 20723c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 20733c94d07bSAnika Henke $cnt = count($parts); 207436ec377eSChristopher Smith // urlencode $pref for the comparison 207536ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 20761c3eca7dSPhy $seen = false; 20773c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 207836ec377eSChristopher Smith if ($parts[$i] == $enc_pref) { 20791c3eca7dSPhy if (!$seen){ 20803a970889SAnika Henke if ($val !== false) { 208136ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 20823a970889SAnika Henke } else { 20833a970889SAnika Henke unset($parts[$i]); 20843a970889SAnika Henke unset($parts[$i + 1]); 20853a970889SAnika Henke } 20861c3eca7dSPhy $seen = true; 20871c3eca7dSPhy } else { 20881c3eca7dSPhy // no break because we want to remove duplicate entries 20891c3eca7dSPhy unset($parts[$i]); 20901c3eca7dSPhy unset($parts[$i + 1]); 20911c3eca7dSPhy } 20923c94d07bSAnika Henke } 20933c94d07bSAnika Henke } 20943c94d07bSAnika Henke $cookieVal = implode('#', $parts); 20951c3eca7dSPhy } else if ($orig === false && $val !== false) { 209664159a61SAndreas Gohr $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 209764159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 20983c94d07bSAnika Henke } 20993c94d07bSAnika Henke 210075e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 21015833995aSPhy if(defined('DOKU_UNITTEST')) { 21025833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 21035833995aSPhy }else{ 210475e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 21053c94d07bSAnika Henke } 21063c94d07bSAnika Henke} 21073c94d07bSAnika Henke 2108f8fb2d18SAndreas Gohr/** 2109f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 2110f8fb2d18SAndreas Gohr * 211142ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 2112f8fb2d18SAndreas Gohr */ 2113f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 2114f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 2115f8fb2d18SAndreas Gohr} 2116f8fb2d18SAndreas Gohr 21173c27983bSAndreas Gohr/** 211871de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 21193c27983bSAndreas Gohr * 21203c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 21213c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 21223c27983bSAndreas Gohr * files are embedded. 21233c27983bSAndreas Gohr * 212471de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 212571de5572SAndreas Gohr * 21263c27983bSAndreas Gohr * @param string $file full path to the SVG file 21273c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 212871de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 21293c27983bSAndreas Gohr */ 21304cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 21313c27983bSAndreas Gohr $file = trim($file); 21323c27983bSAndreas Gohr if($file === '') return false; 21333c27983bSAndreas Gohr if(!file_exists($file)) return false; 21343c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 21353c27983bSAndreas Gohr if(!is_readable($file)) return false; 21363c27983bSAndreas Gohr $content = file_get_contents($file); 21370849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 21380849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 21390849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 21400849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 21413c27983bSAndreas Gohr $content = trim($content); 21423c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 214371de5572SAndreas Gohr return $content; 21443c27983bSAndreas Gohr} 21453c27983bSAndreas Gohr 2146e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 2147