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; 12*704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 1375d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 140c3a5702SAndreas Gohr 15f3f0262cSandi/** 16b6912aeaSAndreas Gohr * These constants are used with the recents function 17b6912aeaSAndreas Gohr */ 18b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED', 2); 19b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS', 4); 20b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES', 8); 210b926329SKate Arzamastsevadefine('RECENTS_MEDIA_CHANGES', 16); 220b926329SKate Arzamastsevadefine('RECENTS_MEDIA_PAGES_MIXED', 32); 23b6912aeaSAndreas Gohr 24b6912aeaSAndreas Gohr/** 25d5197206Schris * Wrapper around htmlspecialchars() 26d5197206Schris * 27d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 28d5197206Schris * @see htmlspecialchars() 29140cfbcdSGerrit Uitslag * 30140cfbcdSGerrit Uitslag * @param string $string the string being converted 31140cfbcdSGerrit Uitslag * @return string converted string 32d5197206Schris */ 33d5197206Schrisfunction hsc($string) { 34d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 35d5197206Schris} 36d5197206Schris 37d5197206Schris/** 385b571377SAndreas Gohr * Checks if the given input is blank 395b571377SAndreas Gohr * 405b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 415b571377SAndreas Gohr * 4267234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 4367234204SAndreas Gohr * with a NULL value without warning. 4467234204SAndreas Gohr * 4567234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 4667234204SAndreas Gohr * 4767234204SAndreas Gohr * (isset($foo) && !blank($foo)) 4867234204SAndreas Gohr * (!isset($foo) || blank($foo)) 4967234204SAndreas Gohr * 505b571377SAndreas Gohr * @param $in 515b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 525b571377SAndreas Gohr * @return bool 535b571377SAndreas Gohr */ 545b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 555b571377SAndreas Gohr if(is_null($in)) return true; 565b571377SAndreas Gohr if(is_array($in)) return empty($in); 575b571377SAndreas Gohr if($in === "\0") return true; 585b571377SAndreas Gohr if($trim && trim($in) === '') return true; 595b571377SAndreas Gohr if(strlen($in) > 0) return false; 605b571377SAndreas Gohr return empty($in); 615b571377SAndreas Gohr} 625b571377SAndreas Gohr 635b571377SAndreas Gohr/** 64d5197206Schris * print a newline terminated string 65d5197206Schris * 66d5197206Schris * You can give an indention as optional parameter 67d5197206Schris * 68d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 69140cfbcdSGerrit Uitslag * 70140cfbcdSGerrit Uitslag * @param string $string line of text 71140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 72d5197206Schris */ 7325ec097bSChris Smithfunction ptln($string, $indent = 0) { 7425ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 7502b0b681SAndreas Gohr} 7602b0b681SAndreas Gohr 7702b0b681SAndreas Gohr/** 7802b0b681SAndreas Gohr * strips control characters (<32) from the given string 7902b0b681SAndreas Gohr * 8002b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 81140cfbcdSGerrit Uitslag * 8242ea7f44SGerrit Uitslag * @param string $string being stripped 83140cfbcdSGerrit Uitslag * @return string 8402b0b681SAndreas Gohr */ 8502b0b681SAndreas Gohrfunction stripctl($string) { 8602b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 87d5197206Schris} 88d5197206Schris 89d5197206Schris/** 90634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 91634d7150SAndreas Gohr * 92634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 93634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 94634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 9542ea7f44SGerrit Uitslag * 96634d7150SAndreas Gohr * @return string 97634d7150SAndreas Gohr */ 98634d7150SAndreas Gohrfunction getSecurityToken() { 99585bf44eSChristopher Smith /** @var Input $INPUT */ 100585bf44eSChristopher Smith global $INPUT; 1013680e2cdSAndreas Gohr 1023680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 1033680e2cdSAndreas Gohr $session = session_id(); 1043680e2cdSAndreas Gohr 1053680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1063680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 107c3cc6e05SAndreas Gohr return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 108634d7150SAndreas Gohr} 109634d7150SAndreas Gohr 110634d7150SAndreas Gohr/** 111634d7150SAndreas Gohr * Check the secret CSRF token 112140cfbcdSGerrit Uitslag * 113140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 114140cfbcdSGerrit Uitslag * @return bool success if the token matched 115634d7150SAndreas Gohr */ 116634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 117585bf44eSChristopher Smith /** @var Input $INPUT */ 1187d01a0eaSTom N Harris global $INPUT; 119585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 120df97eaacSAndreas Gohr 1217d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 122634d7150SAndreas Gohr if(getSecurityToken() != $token) { 123634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 124634d7150SAndreas Gohr return false; 125634d7150SAndreas Gohr } 126634d7150SAndreas Gohr return true; 127634d7150SAndreas Gohr} 128634d7150SAndreas Gohr 129634d7150SAndreas Gohr/** 130634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 131634d7150SAndreas Gohr * 132634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 133140cfbcdSGerrit Uitslag * 134140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 13542ea7f44SGerrit Uitslag * @return string html of hidden form field 136634d7150SAndreas Gohr */ 137634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1382404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1393272d797SAndreas Gohr if($print) echo $ret; 140634d7150SAndreas Gohr return $ret; 141634d7150SAndreas Gohr} 142634d7150SAndreas Gohr 143634d7150SAndreas Gohr/** 1441015a57dSChristopher Smith * Determine basic information for a request of $id 14515fae107Sandi * 14615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1477e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 148140cfbcdSGerrit Uitslag * 149140cfbcdSGerrit Uitslag * @param string $id pageid 150140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 151140cfbcdSGerrit Uitslag * @return array with info for a request of $id 152140cfbcdSGerrit Uitslag * 153f3f0262cSandi */ 1541015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 155f3f0262cSandi global $USERINFO; 156585bf44eSChristopher Smith /* @var Input $INPUT */ 157585bf44eSChristopher Smith global $INPUT; 1586afe8dcaSchris 159c66972f2SAdrian Lang // set info about manager/admin status. 16059bc3b48SGerrit Uitslag $info = array(); 161c66972f2SAdrian Lang $info['isadmin'] = false; 162c66972f2SAdrian Lang $info['ismanager'] = false; 163585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 164f3f0262cSandi $info['userinfo'] = $USERINFO; 1651015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 166585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 16717ee7f66SAndreas Gohr 168f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 169f8cc712eSAndreas Gohr $info['isadmin'] = true; 170f8cc712eSAndreas Gohr $info['ismanager'] = true; 171f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 172f8cc712eSAndreas Gohr $info['ismanager'] = true; 173f8cc712eSAndreas Gohr } 174f8cc712eSAndreas Gohr 17517ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 17617ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 177585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 17817ee7f66SAndreas Gohr } 179ee4c4a1bSAndreas Gohr 180f3f0262cSandi } else { 1811015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 182ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 183f3f0262cSandi } 184f3f0262cSandi 1851015a57dSChristopher Smith $info['namespace'] = getNS($id); 1861015a57dSChristopher Smith 1871015a57dSChristopher Smith // mobile detection 1881015a57dSChristopher Smith if ($htmlClient) { 1891015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1901015a57dSChristopher Smith } 1911015a57dSChristopher Smith 1921015a57dSChristopher Smith return $info; 1931015a57dSChristopher Smith } 1941015a57dSChristopher Smith 1951015a57dSChristopher Smith/** 1961015a57dSChristopher Smith * Return info about the current document as associative 1971015a57dSChristopher Smith * array. 1981015a57dSChristopher Smith * 1991015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 200140cfbcdSGerrit Uitslag * 201140cfbcdSGerrit Uitslag * @return array with info about current document 2021015a57dSChristopher Smith */ 2031015a57dSChristopher Smithfunction pageinfo() { 2041015a57dSChristopher Smith global $ID; 2051015a57dSChristopher Smith global $REV; 2061015a57dSChristopher Smith global $RANGE; 2071015a57dSChristopher Smith global $lang; 208585bf44eSChristopher Smith /* @var Input $INPUT */ 209585bf44eSChristopher Smith global $INPUT; 2101015a57dSChristopher Smith 2111015a57dSChristopher Smith $info = basicinfo($ID); 2121015a57dSChristopher Smith 2131015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2141015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2151015a57dSChristopher Smith $info['id'] = $ID; 2161015a57dSChristopher Smith $info['rev'] = $REV; 2171015a57dSChristopher Smith 218585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 21975d66495SMichael Große $subManager = new SubscriberManager(); 22075d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2217e87a794SChristopher Smith } else { 2227e87a794SChristopher Smith $info['subscribed'] = false; 2237e87a794SChristopher Smith } 2247e87a794SChristopher Smith 225f3f0262cSandi $info['locked'] = checklock($ID); 226317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 22779e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 22801c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2292ca9d91cSBen Coburn if($REV) { 2302ca9d91cSBen Coburn //check if current revision was meant 23101c9a118SAndreas Gohr if($info['exists'] && ($info['currentrev'] == $REV)) { 2322ca9d91cSBen Coburn $REV = ''; 2337b3a6803SAndreas Gohr } elseif($RANGE) { 2347b3a6803SAndreas Gohr //section editing does not work with old revisions! 2357b3a6803SAndreas Gohr $REV = ''; 2367b3a6803SAndreas Gohr $RANGE = ''; 2377b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2382ca9d91cSBen Coburn } else { 2392ca9d91cSBen Coburn //really use old revision 240317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 24179e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 242f3f0262cSandi } 243f3f0262cSandi } 244c112d578Sandi $info['rev'] = $REV; 245f3f0262cSandi if($info['exists']) { 246f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 247f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 248f3f0262cSandi } else { 249f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 250f3f0262cSandi } 25150e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 252f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 253f3f0262cSandi 25471726d78SBen Coburn //load page meta data 25571726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 25671726d78SBen Coburn 257652610a2Sandi //who's the editor 258047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 259652610a2Sandi if($REV) { 260f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 261652610a2Sandi } else { 2620e80bb5eSChristopher Smith if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 263aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 264aa27cf05SAndreas Gohr } else { 265f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 266cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 267cd00a034SBen Coburn if($revinfo !== false) { 268cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 269cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 270cd00a034SBen Coburn } 271cd00a034SBen Coburn } 272cd00a034SBen Coburn } 273cd00a034SBen Coburn //and check for an external edit 274cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 275cd00a034SBen Coburn // cached changelog line no longer valid 276cd00a034SBen Coburn $revinfo = false; 277cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 278cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 279652610a2Sandi } 280bb4866bdSchris 281652610a2Sandi $info['ip'] = $revinfo['ip']; 282652610a2Sandi $info['user'] = $revinfo['user']; 283652610a2Sandi $info['sum'] = $revinfo['sum']; 28471726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 285ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 28659f257aeSchris 28788f522e9Sandi if($revinfo['user']) { 28888f522e9Sandi $info['editor'] = $revinfo['user']; 28988f522e9Sandi } else { 29088f522e9Sandi $info['editor'] = $revinfo['ip']; 29188f522e9Sandi } 292652610a2Sandi 293ee4c4a1bSAndreas Gohr // draft 2940aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 2950aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 2960aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 297ee4c4a1bSAndreas Gohr } 298ee4c4a1bSAndreas Gohr 2991015a57dSChristopher Smith return $info; 3001015a57dSChristopher Smith} 3011015a57dSChristopher Smith 3021015a57dSChristopher Smith/** 3030c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3040c39d46cSMichael Große */ 3050c39d46cSMichael Großefunction jsinfo() { 3060c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3070c39d46cSMichael Große 3080c39d46cSMichael Große if (!is_array($JSINFO)) { 3090c39d46cSMichael Große $JSINFO = []; 3100c39d46cSMichael Große } 3110c39d46cSMichael Große //export minimal info to JS, plugins can add more 3120c39d46cSMichael Große $JSINFO['id'] = $ID; 3130c39d46cSMichael Große $JSINFO['namespace'] = (string) $INFO['namespace']; 3140c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3150c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3160c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3170c39d46cSMichael Große} 3180c39d46cSMichael Große 3190c39d46cSMichael Große/** 3201015a57dSChristopher Smith * Return information about the current media item as an associative array. 321140cfbcdSGerrit Uitslag * 322140cfbcdSGerrit Uitslag * @return array with info about current media item 3231015a57dSChristopher Smith */ 3241015a57dSChristopher Smithfunction mediainfo(){ 3251015a57dSChristopher Smith global $NS; 3261015a57dSChristopher Smith global $IMG; 3271015a57dSChristopher Smith 3281015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3291015a57dSChristopher Smith $info['image'] = $IMG; 3301c548ebeSAndreas Gohr 331f3f0262cSandi return $info; 332f3f0262cSandi} 333f3f0262cSandi 334f3f0262cSandi/** 3352684e50aSAndreas Gohr * Build an string of URL parameters 3362684e50aSAndreas Gohr * 3372684e50aSAndreas Gohr * @author Andreas Gohr 338140cfbcdSGerrit Uitslag * 339140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 340140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 341140cfbcdSGerrit Uitslag * @return string query string 3422684e50aSAndreas Gohr */ 343b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3442684e50aSAndreas Gohr $url = ''; 3452684e50aSAndreas Gohr $amp = false; 3462684e50aSAndreas Gohr foreach($params as $key => $val) { 347b174aeaeSchris if($amp) $url .= $sep; 3482684e50aSAndreas Gohr 34985e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3503a50618cSgweissbach $url .= rawurlencode((string) $val); 3512684e50aSAndreas Gohr $amp = true; 3522684e50aSAndreas Gohr } 3532684e50aSAndreas Gohr return $url; 3542684e50aSAndreas Gohr} 3552684e50aSAndreas Gohr 3562684e50aSAndreas Gohr/** 3572684e50aSAndreas Gohr * Build an string of html tag attributes 3582684e50aSAndreas Gohr * 3597bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3607bff22c0SAndreas Gohr * 3612684e50aSAndreas Gohr * @author Andreas Gohr 362140cfbcdSGerrit Uitslag * 363140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 364140cfbcdSGerrit Uitslag * @param bool $skipempty skip empty string values? 365140cfbcdSGerrit Uitslag * @return string 3662684e50aSAndreas Gohr */ 3674b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 3682684e50aSAndreas Gohr $url = ''; 3699063ec14SAdrian Lang $white = false; 3702684e50aSAndreas Gohr foreach($params as $key => $val) { 3717bff22c0SAndreas Gohr if($key{0} == '_') continue; 372b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 3739063ec14SAdrian Lang if($white) $url .= ' '; 3747bff22c0SAndreas Gohr 3752684e50aSAndreas Gohr $url .= $key.'="'; 3762684e50aSAndreas Gohr $url .= htmlspecialchars($val); 3772684e50aSAndreas Gohr $url .= '"'; 3789063ec14SAdrian Lang $white = true; 3792684e50aSAndreas Gohr } 3802684e50aSAndreas Gohr return $url; 3812684e50aSAndreas Gohr} 3822684e50aSAndreas Gohr 3832684e50aSAndreas Gohr/** 38415fae107Sandi * This builds the breadcrumb trail and returns it as array 38515fae107Sandi * 38615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 387140cfbcdSGerrit Uitslag * 388e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 389f3f0262cSandi */ 390f3f0262cSandifunction breadcrumbs() { 3918746e727Sandi // we prepare the breadcrumbs early for quick session closing 3928746e727Sandi static $crumbs = null; 3938746e727Sandi if($crumbs != null) return $crumbs; 3948746e727Sandi 395f3f0262cSandi global $ID; 396f3f0262cSandi global $ACT; 397f3f0262cSandi global $conf; 398f3f0262cSandi 399f3f0262cSandi //first visit? 400c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 4014d1fee4cSB_S666 //we only save on show and existing visible wiki documents 402a77f5846Sjan $file = wikiFN($ID); 4034d1fee4cSB_S666 if($ACT != 'show' || isHiddenPage($ID) || !file_exists($file)) { 404e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 405f3f0262cSandi return $crumbs; 406f3f0262cSandi } 407a77f5846Sjan 408a77f5846Sjan // page names 4091a84a0f3SAnika Henke $name = noNSorNS($ID); 410fe9ec250SChris Smith if(useHeading('navigation')) { 411a77f5846Sjan // get page title 41267c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 413a77f5846Sjan if($title) { 414a77f5846Sjan $name = $title; 415a77f5846Sjan } 416a77f5846Sjan } 417a77f5846Sjan 418f3f0262cSandi //remove ID from array 419a77f5846Sjan if(isset($crumbs[$ID])) { 420a77f5846Sjan unset($crumbs[$ID]); 421f3f0262cSandi } 422f3f0262cSandi 423f3f0262cSandi //add to array 424a77f5846Sjan $crumbs[$ID] = $name; 425f3f0262cSandi //reduce size 426f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 427f3f0262cSandi array_shift($crumbs); 428f3f0262cSandi } 429f3f0262cSandi //save to session 430e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 431f3f0262cSandi return $crumbs; 432f3f0262cSandi} 433f3f0262cSandi 434f3f0262cSandi/** 43515fae107Sandi * Filter for page IDs 43615fae107Sandi * 437f3f0262cSandi * This is run on a ID before it is outputted somewhere 438f3f0262cSandi * currently used to replace the colon with something else 439907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 440907f24f7SAndreas Gohr * 441907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 442907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 443907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 44415fae107Sandi * 44549c713a3Sandi * Urlencoding is ommitted when the second parameter is false 44649c713a3Sandi * 44715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 448140cfbcdSGerrit Uitslag * 449140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 450140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 451140cfbcdSGerrit Uitslag * @return string 452f3f0262cSandi */ 45349c713a3Sandifunction idfilter($id, $ue = true) { 454f3f0262cSandi global $conf; 455585bf44eSChristopher Smith /* @var Input $INPUT */ 456585bf44eSChristopher Smith global $INPUT; 457585bf44eSChristopher Smith 458f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 459f3f0262cSandi $id = strtr($id, ':', '/'); 460f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 46158bedc8aSborekb $conf['userewrite'] && 462585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4633272d797SAndreas Gohr ) { 464f3f0262cSandi $id = strtr($id, ':', ';'); 465f3f0262cSandi } 46649c713a3Sandi if($ue) { 467b6c6979fSAndreas Gohr $id = rawurlencode($id); 468f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 469edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 470f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 47149c713a3Sandi } 472f3f0262cSandi return $id; 473f3f0262cSandi} 474f3f0262cSandi 475f3f0262cSandi/** 476ed7b5f09Sandi * This builds a link to a wikipage 47715fae107Sandi * 4784bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4796c7843b5Sandi * 48015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4814bc480e5SAndreas Gohr * 4824bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4834bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4844bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4854bc480e5SAndreas Gohr * @param string $separator parameter separator 4864bc480e5SAndreas Gohr * @return string 487f3f0262cSandi */ 48816f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 489f3f0262cSandi global $conf; 49016f15a81SDominik Eckelmann if(is_array($urlParameters)) { 4914bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 49264159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 49364159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 49464159a61SAndreas Gohr } 49516f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4966de3759aSAndreas Gohr } else { 49716f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4986de3759aSAndreas Gohr } 49916f15a81SDominik Eckelmann if($id === '') { 50016f15a81SDominik Eckelmann $id = $conf['start']; 50116f15a81SDominik Eckelmann } 502f3f0262cSandi $id = idfilter($id); 50316f15a81SDominik Eckelmann if($absolute) { 504ed7b5f09Sandi $xlink = DOKU_URL; 505ed7b5f09Sandi } else { 506ed7b5f09Sandi $xlink = DOKU_BASE; 507ed7b5f09Sandi } 508f3f0262cSandi 5096c7843b5Sandi if($conf['userewrite'] == 2) { 5106c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 51116f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5126c7843b5Sandi } elseif($conf['userewrite']) { 513f3f0262cSandi $xlink .= $id; 51416f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 51540b5fb5bSPhy } elseif($id !== '') { 5166c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 51716f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 518bce3726dSAndreas Gohr } else { 519bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 52016f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 521f3f0262cSandi } 522f3f0262cSandi 523f3f0262cSandi return $xlink; 524f3f0262cSandi} 525f3f0262cSandi 526f3f0262cSandi/** 527f5c2808fSBen Coburn * This builds a link to an alternate page format 528f5c2808fSBen Coburn * 529f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 530f5c2808fSBen Coburn * 531f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5324bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5334bc480e5SAndreas Gohr * @param string $format the export renderer to use 5344bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5354bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5364bc480e5SAndreas Gohr * @param string $sep parameter separator 5374bc480e5SAndreas Gohr * @return string 538f5c2808fSBen Coburn */ 5394bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 540f5c2808fSBen Coburn global $conf; 5414bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5424bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 543f5c2808fSBen Coburn } else { 5444bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 545f5c2808fSBen Coburn } 546f5c2808fSBen Coburn 547f5c2808fSBen Coburn $format = rawurlencode($format); 548f5c2808fSBen Coburn $id = idfilter($id); 549f5c2808fSBen Coburn if($abs) { 550f5c2808fSBen Coburn $xlink = DOKU_URL; 551f5c2808fSBen Coburn } else { 552f5c2808fSBen Coburn $xlink = DOKU_BASE; 553f5c2808fSBen Coburn } 554f5c2808fSBen Coburn 555f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 556f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5574bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 558f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 559f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5604bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 561f5c2808fSBen Coburn } else { 562f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5634bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 564f5c2808fSBen Coburn } 565f5c2808fSBen Coburn 566f5c2808fSBen Coburn return $xlink; 567f5c2808fSBen Coburn} 568f5c2808fSBen Coburn 569f5c2808fSBen Coburn/** 5706de3759aSAndreas Gohr * Build a link to a media file 5716de3759aSAndreas Gohr * 5726de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5738c08db0aSAndreas Gohr * 5748c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5758c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5768c08db0aSAndreas Gohr * 5773272d797SAndreas Gohr * @param string $id the media file id or URL 5783272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5793272d797SAndreas Gohr * @param bool $direct link to detail page if false 5803272d797SAndreas Gohr * @param string $sep URL parameter separator 5813272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5823272d797SAndreas Gohr * @return string 5836de3759aSAndreas Gohr */ 58455b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5856de3759aSAndreas Gohr global $conf; 586b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 587826d2766SKlap-in if(!$isexternalimage) { 588826d2766SKlap-in $id = cleanID($id); 589826d2766SKlap-in } 590826d2766SKlap-in 5916de3759aSAndreas Gohr if(is_array($more)) { 5920f4e0092SChristopher Smith // add token for resized images 593443e135dSChristopher Smith if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){ 5940f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 5950f4e0092SChristopher Smith } 5968c08db0aSAndreas Gohr // strip defaults for shorter URLs 5978c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 598443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 599443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 6008c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 60178b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 602b174aeaeSchris $more = buildURLparams($more, $sep); 6036de3759aSAndreas Gohr } else { 6045e7db1e2SChristopher Smith $matches = array(); 605cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6065e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6075e7db1e2SChristopher Smith foreach ($matches as $match){ 6085e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6095e7db1e2SChristopher Smith } 610cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 611cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6125e7db1e2SChristopher Smith } 6138c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6148c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 615b174aeaeSchris $more = str_replace(',', $sep, $more); 6166de3759aSAndreas Gohr } 6176de3759aSAndreas Gohr 61855b2b31bSAndreas Gohr if($abs) { 61955b2b31bSAndreas Gohr $xlink = DOKU_URL; 62055b2b31bSAndreas Gohr } else { 6216de3759aSAndreas Gohr $xlink = DOKU_BASE; 62255b2b31bSAndreas Gohr } 6236de3759aSAndreas Gohr 6246de3759aSAndreas Gohr // external URLs are always direct without rewriting 625826d2766SKlap-in if($isexternalimage) { 6266de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 627cc036f74SKlap-in $xlink .= '?'.$more; 628b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6296de3759aSAndreas Gohr return $xlink; 6306de3759aSAndreas Gohr } 6316de3759aSAndreas Gohr 6326de3759aSAndreas Gohr $id = idfilter($id); 6336de3759aSAndreas Gohr 6346de3759aSAndreas Gohr // decide on scriptname 6356de3759aSAndreas Gohr if($direct) { 6366de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6376de3759aSAndreas Gohr $script = '_media'; 6386de3759aSAndreas Gohr } else { 6396de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6406de3759aSAndreas Gohr } 6416de3759aSAndreas Gohr } else { 6426de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6436de3759aSAndreas Gohr $script = '_detail'; 6446de3759aSAndreas Gohr } else { 6456de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6466de3759aSAndreas Gohr } 6476de3759aSAndreas Gohr } 6486de3759aSAndreas Gohr 6496de3759aSAndreas Gohr // build URL based on rewrite mode 6506de3759aSAndreas Gohr if($conf['userewrite']) { 6516de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6526de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6536de3759aSAndreas Gohr } else { 6546de3759aSAndreas Gohr if($more) { 655a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 656b174aeaeSchris $xlink .= $sep.'media='.$id; 6576de3759aSAndreas Gohr } else { 658a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6596de3759aSAndreas Gohr } 6606de3759aSAndreas Gohr } 6616de3759aSAndreas Gohr 6626de3759aSAndreas Gohr return $xlink; 6636de3759aSAndreas Gohr} 6646de3759aSAndreas Gohr 6656de3759aSAndreas Gohr/** 66625ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 66715fae107Sandi * 66825ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 66925ca5b17SAndreas Gohr * 67015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 671140cfbcdSGerrit Uitslag * 672140cfbcdSGerrit Uitslag * @return string 673f3f0262cSandi */ 67425ca5b17SAndreas Gohrfunction script() { 675ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 676f3f0262cSandi} 677f3f0262cSandi 678f3f0262cSandi/** 67915fae107Sandi * Spamcheck against wordlist 68015fae107Sandi * 681f3f0262cSandi * Checks the wikitext against a list of blocked expressions 682f3f0262cSandi * returns true if the text contains any bad words 68315fae107Sandi * 684e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 685e403cc58SMichael Klier * 686e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 687e403cc58SMichael Klier * and gain information about the user who was blocked. 688e403cc58SMichael Klier * 689e403cc58SMichael Klier * Event data: 690e403cc58SMichael Klier * data['matches'] - array of matches 691e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 692e403cc58SMichael Klier * [ip] - ip address 693e403cc58SMichael Klier * [user] - username (if logged in) 694e403cc58SMichael Klier * [mail] - mail address (if logged in) 695e403cc58SMichael Klier * [name] - real name (if logged in) 696e403cc58SMichael Klier * 69715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6986dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 699140cfbcdSGerrit Uitslag * 7006dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 7016dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 702f3f0262cSandi */ 7036dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 704f3f0262cSandi global $TEXT; 7056dffa0e0SAndreas Gohr global $PRE; 7066dffa0e0SAndreas Gohr global $SUF; 707e0086ca2SAndreas Gohr global $SUM; 708f3f0262cSandi global $conf; 709e403cc58SMichael Klier global $INFO; 710585bf44eSChristopher Smith /* @var Input $INPUT */ 711585bf44eSChristopher Smith global $INPUT; 712f3f0262cSandi 713f3f0262cSandi if(!$conf['usewordblock']) return false; 714f3f0262cSandi 715e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7166dffa0e0SAndreas Gohr 717041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 71864159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 71964159a61SAndreas Gohr $text = preg_replace( 72064159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 72164159a61SAndreas Gohr '\1http://\2 \2\3', 72264159a61SAndreas Gohr $text 72364159a61SAndreas Gohr ); 72464159a61SAndreas Gohr // phpcs:enable 725041d1964SAndreas Gohr 726b9ac8716Schris $wordblocks = getWordblocks(); 7273e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 7283e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 7293e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 7303e2965d7Sandi // backreferences are used - the maximum is 99 7313e2965d7Sandi // this is very bad performancewise and may even be too high still 7323e2965d7Sandi $chunksize = 40; 7333e2965d7Sandi } else { 734a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7353e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 736a51d08efSAndreas Gohr $chunksize = 200; 7373e2965d7Sandi } 738b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 739f3f0262cSandi $re = array(); 74049eb6e38SAndreas Gohr // build regexp from blocks 741f3f0262cSandi foreach($blocks as $block) { 742f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 743f3f0262cSandi $block = trim($block); 744f3f0262cSandi if(empty($block)) continue; 745f3f0262cSandi $re[] = $block; 746f3f0262cSandi } 747e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 748e403cc58SMichael Klier // prepare event data 74959bc3b48SGerrit Uitslag $data = array(); 750e403cc58SMichael Klier $data['matches'] = $matches; 751585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 752585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 753585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 754e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 755e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 756e403cc58SMichael Klier } 757bad6fc0dSAndreas Gohr $callback = function () { 758bad6fc0dSAndreas Gohr return true; 759bad6fc0dSAndreas Gohr }; 760e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 761b9ac8716Schris } 762703f6fdeSandi } 763f3f0262cSandi return false; 764f3f0262cSandi} 765f3f0262cSandi 766f3f0262cSandi/** 76715fae107Sandi * Return the IP of the client 76815fae107Sandi * 7696d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 77015fae107Sandi * 7716d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 7726d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 7736d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 7746d8affe6SAndreas Gohr * headers 7756d8affe6SAndreas Gohr * 77615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 777140cfbcdSGerrit Uitslag * 7783272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 7793272d797SAndreas Gohr * @return string 780f3f0262cSandi */ 7816d8affe6SAndreas Gohrfunction clientIP($single = false) { 782585bf44eSChristopher Smith /* @var Input $INPUT */ 783585bf44eSChristopher Smith global $INPUT; 784585bf44eSChristopher Smith 7856d8affe6SAndreas Gohr $ip = array(); 786585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 787585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 788585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 789585bf44eSChristopher Smith } 790585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 791585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 792585bf44eSChristopher Smith } 7936d8affe6SAndreas Gohr 794dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 795dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 796dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 797dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 798dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 799dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 800dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 801dc14c6d1SGuy Brand $IPv6Address = 802dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 803dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 804dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 805dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 806dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 807dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 808dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 809dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 810dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 811dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 812dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 813dc14c6d1SGuy Brand 8146d8affe6SAndreas Gohr // remove any non-IP stuff 8156d8affe6SAndreas Gohr $cnt = count($ip); 8164ff28443Schris $match = array(); 8176d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 818dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 8194ff28443Schris $ip[$i] = $match[0]; 8204ff28443Schris } else { 8214ff28443Schris $ip[$i] = ''; 8224ff28443Schris } 8236d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 824f3f0262cSandi } 8256d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 8266d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8276d8affe6SAndreas Gohr 8286d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 8296d8affe6SAndreas Gohr 8306d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 8316d8affe6SAndreas Gohr $ip = array_reverse($ip); 8326d8affe6SAndreas Gohr foreach($ip as $i) { 8332343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 8346d8affe6SAndreas Gohr continue; 8356d8affe6SAndreas Gohr } else { 8366d8affe6SAndreas Gohr return $i; 8376d8affe6SAndreas Gohr } 8386d8affe6SAndreas Gohr } 8396d8affe6SAndreas Gohr // still here? just use the first (last) address 8406d8affe6SAndreas Gohr return $ip[0]; 841f3f0262cSandi} 842f3f0262cSandi 843f3f0262cSandi/** 8441c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8451c548ebeSAndreas Gohr * 8461c548ebeSAndreas Gohr * Adapted from the example code at url below 8471c548ebeSAndreas Gohr * 8481c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 849140cfbcdSGerrit Uitslag * 85064159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 851140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8521c548ebeSAndreas Gohr */ 8531c548ebeSAndreas Gohrfunction clientismobile() { 854585bf44eSChristopher Smith /* @var Input $INPUT */ 855585bf44eSChristopher Smith global $INPUT; 8561c548ebeSAndreas Gohr 857585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8581c548ebeSAndreas Gohr 859585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8601c548ebeSAndreas Gohr 861585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8621c548ebeSAndreas Gohr 86364159a61SAndreas Gohr $uamatches = join( 86464159a61SAndreas Gohr '|', 86564159a61SAndreas Gohr [ 86664159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 86764159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 86864159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 86964159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 87064159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 87164159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 87264159a61SAndreas Gohr '\d\d\di', 'moto' 87364159a61SAndreas Gohr ] 87464159a61SAndreas Gohr ); 8751c548ebeSAndreas Gohr 876585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8771c548ebeSAndreas Gohr 8781c548ebeSAndreas Gohr return false; 8791c548ebeSAndreas Gohr} 8801c548ebeSAndreas Gohr 8811c548ebeSAndreas Gohr/** 8826efc45a2SDmitry Katsubo * check if a given link is interwiki link 8836efc45a2SDmitry Katsubo * 8846efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8856efc45a2SDmitry Katsubo * @return bool 8866efc45a2SDmitry Katsubo */ 8876efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 8886efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 8896efc45a2SDmitry Katsubo return false; 8906efc45a2SDmitry Katsubo} 8916efc45a2SDmitry Katsubo 8926efc45a2SDmitry Katsubo/** 89363211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 89463211f61SGlen Harris * 89522ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 89622ef1e32SAndreas Gohr * 89763211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 898140cfbcdSGerrit Uitslag * 8993272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 9003272d797SAndreas Gohr * @return string a comma separated list of hostnames 90163211f61SGlen Harris */ 90263211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 90322ef1e32SAndreas Gohr global $conf; 90422ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 90522ef1e32SAndreas Gohr 90663211f61SGlen Harris $hosts = array(); 90763211f61SGlen Harris $ips = explode(',', $ips); 908551a720fSMichael Klier 909551a720fSMichael Klier if(is_array($ips)) { 9103886270dSAndreas Gohr foreach($ips as $ip) { 911551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 91263211f61SGlen Harris } 913551a720fSMichael Klier return join(',', $hosts); 914551a720fSMichael Klier } else { 915551a720fSMichael Klier return gethostbyaddr(trim($ips)); 916551a720fSMichael Klier } 91763211f61SGlen Harris} 91863211f61SGlen Harris 91963211f61SGlen Harris/** 92015fae107Sandi * Checks if a given page is currently locked. 92115fae107Sandi * 922f3f0262cSandi * removes stale lockfiles 92315fae107Sandi * 92415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 925140cfbcdSGerrit Uitslag * 926140cfbcdSGerrit Uitslag * @param string $id page id 927140cfbcdSGerrit Uitslag * @return bool page is locked? 928f3f0262cSandi */ 929f3f0262cSandifunction checklock($id) { 930f3f0262cSandi global $conf; 931585bf44eSChristopher Smith /* @var Input $INPUT */ 932585bf44eSChristopher Smith global $INPUT; 933585bf44eSChristopher Smith 934c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 935f3f0262cSandi 936f3f0262cSandi //no lockfile 93779e79377SAndreas Gohr if(!file_exists($lock)) return false; 938f3f0262cSandi 939f3f0262cSandi //lockfile expired 940f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 941d8186216SBen Coburn @unlink($lock); 942f3f0262cSandi return false; 943f3f0262cSandi } 944f3f0262cSandi 945f3f0262cSandi //my own lock 9466d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 9470712fefaSAndreas Gohr if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) { 948f3f0262cSandi return false; 949f3f0262cSandi } 950f3f0262cSandi 951f3f0262cSandi return $ip; 952f3f0262cSandi} 953f3f0262cSandi 954f3f0262cSandi/** 95515fae107Sandi * Lock a page for editing 95615fae107Sandi * 95715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 958140cfbcdSGerrit Uitslag * 959140cfbcdSGerrit Uitslag * @param string $id page id to lock 960f3f0262cSandi */ 961f3f0262cSandifunction lock($id) { 962544ed901SDaniel Calviño Sánchez global $conf; 963585bf44eSChristopher Smith /* @var Input $INPUT */ 964585bf44eSChristopher Smith global $INPUT; 965544ed901SDaniel Calviño Sánchez 966544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 967544ed901SDaniel Calviño Sánchez return; 968544ed901SDaniel Calviño Sánchez } 969544ed901SDaniel Calviño Sánchez 970c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 971585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 972585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 973f3f0262cSandi } else { 97485fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 975f3f0262cSandi } 976f3f0262cSandi} 977f3f0262cSandi 978f3f0262cSandi/** 97915fae107Sandi * Unlock a page if it was locked by the user 980f3f0262cSandi * 98115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 982140cfbcdSGerrit Uitslag * 9833272d797SAndreas Gohr * @param string $id page id to unlock 98415fae107Sandi * @return bool true if a lock was removed 985f3f0262cSandi */ 986f3f0262cSandifunction unlock($id) { 987585bf44eSChristopher Smith /* @var Input $INPUT */ 988585bf44eSChristopher Smith global $INPUT; 989585bf44eSChristopher Smith 990c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 99179e79377SAndreas Gohr if(file_exists($lock)) { 9926d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 993585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 994f3f0262cSandi @unlink($lock); 995f3f0262cSandi return true; 996f3f0262cSandi } 997f3f0262cSandi } 998f3f0262cSandi return false; 999f3f0262cSandi} 1000f3f0262cSandi 1001f3f0262cSandi/** 1002f3f0262cSandi * convert line ending to unix format 1003f3f0262cSandi * 10046db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 10056db7468bSAndreas Gohr * 100615fae107Sandi * @see formText() for 2crlf conversion 100715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1008140cfbcdSGerrit Uitslag * 1009140cfbcdSGerrit Uitslag * @param string $text 1010140cfbcdSGerrit Uitslag * @return string 1011f3f0262cSandi */ 1012f3f0262cSandifunction cleanText($text) { 1013f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10146db7468bSAndreas Gohr 10156db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10166db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10176db7468bSAndreas Gohr // but might actually fix the problem in many cases 10186db7468bSAndreas Gohr if(!utf8_check($text)) $text = utf8_encode($text); 10196db7468bSAndreas Gohr 1020f3f0262cSandi return $text; 1021f3f0262cSandi} 1022f3f0262cSandi 1023f3f0262cSandi/** 1024f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1025f3f0262cSandi * It also converts line endings to Windows format which is 1026f3f0262cSandi * pseudo standard for webforms. 1027f3f0262cSandi * 102815fae107Sandi * @see cleanText() for 2unix conversion 102915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1030140cfbcdSGerrit Uitslag * 1031140cfbcdSGerrit Uitslag * @param string $text 1032140cfbcdSGerrit Uitslag * @return string 1033f3f0262cSandi */ 1034f3f0262cSandifunction formText($text) { 10355b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 1036f3f0262cSandi return htmlspecialchars($text); 1037f3f0262cSandi} 1038f3f0262cSandi 1039f3f0262cSandi/** 104015fae107Sandi * Returns the specified local text in raw format 104115fae107Sandi * 104215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1043140cfbcdSGerrit Uitslag * 1044140cfbcdSGerrit Uitslag * @param string $id page id 1045140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1046140cfbcdSGerrit Uitslag * @return string 1047f3f0262cSandi */ 10482adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10492adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1050f3f0262cSandi} 1051f3f0262cSandi 1052f3f0262cSandi/** 1053f3f0262cSandi * Returns the raw WikiText 105415fae107Sandi * 105515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1056140cfbcdSGerrit Uitslag * 1057140cfbcdSGerrit Uitslag * @param string $id page id 1058e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1059140cfbcdSGerrit Uitslag * @return string 1060f3f0262cSandi */ 1061f3f0262cSandifunction rawWiki($id, $rev = '') { 1062cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1063f3f0262cSandi} 1064f3f0262cSandi 1065f3f0262cSandi/** 10667146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10677146cee2SAndreas Gohr * 10687b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10697146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1070140cfbcdSGerrit Uitslag * 1071140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1072140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10737146cee2SAndreas Gohr */ 1074fe17917eSAdrian Langfunction pageTemplate($id) { 1075a15ce62dSEsther Brunner global $conf; 1076e29549feSAndreas Gohr 1077fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1078e29549feSAndreas Gohr 10797b84afa2SAndreas Gohr // prepare initial event data 10807b84afa2SAndreas Gohr $data = array( 10817b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10827b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10837b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10847b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10857b84afa2SAndreas Gohr ); 10867b84afa2SAndreas Gohr 10877b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 10887b84afa2SAndreas Gohr if($evt->advise_before(true)) { 10897b84afa2SAndreas Gohr // the before event might have loaded the content already 10907b84afa2SAndreas Gohr if(empty($data['tpl'])) { 10917b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10927b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1093fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 109479e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 10957b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1096e29549feSAndreas Gohr } else { 1097e29549feSAndreas Gohr // search upper namespaces for templates 1098e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1099e29549feSAndreas Gohr while(strlen($path) >= $len) { 110079e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 11017b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1102e29549feSAndreas Gohr break; 1103e29549feSAndreas Gohr } 1104e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1105e29549feSAndreas Gohr } 1106e29549feSAndreas Gohr } 11077b84afa2SAndreas Gohr } 11087b84afa2SAndreas Gohr // load the content 11093d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 11107b84afa2SAndreas Gohr } 1111a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 11127b84afa2SAndreas Gohr } 11137b84afa2SAndreas Gohr $evt->advise_after(); 11147b84afa2SAndreas Gohr unset($evt); 11157b84afa2SAndreas Gohr 1116fe17917eSAdrian Lang return $data['tpl']; 11172b1223ecSAdrian Lang} 11182b1223ecSAdrian Lang 11192b1223ecSAdrian Lang/** 11202b1223ecSAdrian Lang * Performs common page template replacements 11217b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11222b1223ecSAdrian Lang * 11232b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1124140cfbcdSGerrit Uitslag * 1125140cfbcdSGerrit Uitslag * @param array $data array with event data 1126140cfbcdSGerrit Uitslag * @return string 11272b1223ecSAdrian Lang */ 1128d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11293272d797SAndreas Gohr /** 11303272d797SAndreas Gohr * @var string $id the id of the page to be created 11313272d797SAndreas Gohr * @var string $tpl the text used as template 11323272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11333272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11343272d797SAndreas Gohr */ 1135fe17917eSAdrian Lang extract($data); 1136fe17917eSAdrian Lang 1137b856f7dfSAdrian Lang global $USERINFO; 1138bce53b1fSAdrian Lang global $conf; 1139585bf44eSChristopher Smith /* @var Input $INPUT */ 1140585bf44eSChristopher Smith global $INPUT; 1141e29549feSAndreas Gohr 1142e29549feSAndreas Gohr // replace placeholders 114326ece5a7SAndreas Gohr $file = noNS($id); 114437c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 114526ece5a7SAndreas Gohr 11463272d797SAndreas Gohr $tpl = str_replace( 11473272d797SAndreas Gohr array( 114826ece5a7SAndreas Gohr '@ID@', 114926ece5a7SAndreas Gohr '@NS@', 11508a7bcf66SShota Miyazaki '@CURNS@', 115126ece5a7SAndreas Gohr '@FILE@', 115226ece5a7SAndreas Gohr '@!FILE@', 115326ece5a7SAndreas Gohr '@!FILE!@', 115426ece5a7SAndreas Gohr '@PAGE@', 115526ece5a7SAndreas Gohr '@!PAGE@', 115626ece5a7SAndreas Gohr '@!!PAGE@', 115726ece5a7SAndreas Gohr '@!PAGE!@', 115826ece5a7SAndreas Gohr '@USER@', 115926ece5a7SAndreas Gohr '@NAME@', 116026ece5a7SAndreas Gohr '@MAIL@', 116126ece5a7SAndreas Gohr '@DATE@', 116226ece5a7SAndreas Gohr ), 116326ece5a7SAndreas Gohr array( 116426ece5a7SAndreas Gohr $id, 116526ece5a7SAndreas Gohr getNS($id), 11668a7bcf66SShota Miyazaki curNS($id), 116726ece5a7SAndreas Gohr $file, 116826ece5a7SAndreas Gohr utf8_ucfirst($file), 116926ece5a7SAndreas Gohr utf8_strtoupper($file), 117026ece5a7SAndreas Gohr $page, 117126ece5a7SAndreas Gohr utf8_ucfirst($page), 117226ece5a7SAndreas Gohr utf8_ucwords($page), 117326ece5a7SAndreas Gohr utf8_strtoupper($page), 1174585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 1175b856f7dfSAdrian Lang $USERINFO['name'], 1176b856f7dfSAdrian Lang $USERINFO['mail'], 117726ece5a7SAndreas Gohr $conf['dformat'], 11783272d797SAndreas Gohr ), $tpl 11793272d797SAndreas Gohr ); 118026ece5a7SAndreas Gohr 11817d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1182bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1183bad6fc0dSAndreas Gohr '/%./', 1184bad6fc0dSAndreas Gohr function ($m) { 1185bad6fc0dSAndreas Gohr return strftime($m[0]); 1186bad6fc0dSAndreas Gohr }, 1187bad6fc0dSAndreas Gohr $tpl 1188bad6fc0dSAndreas Gohr ); 1189d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1190a15ce62dSEsther Brunner return $tpl; 11917146cee2SAndreas Gohr} 11927146cee2SAndreas Gohr 11937146cee2SAndreas Gohr/** 119415fae107Sandi * Returns the raw Wiki Text in three slices. 119515fae107Sandi * 119615fae107Sandi * The range parameter needs to have the form "from-to" 119715cfe303Sandi * and gives the range of the section in bytes - no 119815cfe303Sandi * UTF-8 awareness is needed. 1199f3f0262cSandi * The returned order is prefix, section and suffix. 120015fae107Sandi * 120115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1202140cfbcdSGerrit Uitslag * 1203140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1204140cfbcdSGerrit Uitslag * @param string $id page id 1205140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 120642ea7f44SGerrit Uitslag * @return string[] with three slices 1207f3f0262cSandi */ 1208f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1209cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1210f3f0262cSandi 121180fcb268SAdrian Lang // Parse range 121280fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 121380fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 121480fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 121580fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 121680fcb268SAdrian Lang 121759bc3b48SGerrit Uitslag $slices = array(); 121880fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 121980fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 122015cfe303Sandi $slices[2] = substr($text, $to); 1221f3f0262cSandi return $slices; 1222f3f0262cSandi} 1223f3f0262cSandi 1224f3f0262cSandi/** 122515fae107Sandi * Joins wiki text slices 122615fae107Sandi * 122780fcb268SAdrian Lang * function to join the text slices. 1228f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1229f3f0262cSandi * lines between sections if needed (used on saving). 123015fae107Sandi * 123115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1232140cfbcdSGerrit Uitslag * 1233140cfbcdSGerrit Uitslag * @param string $pre prefix 1234140cfbcdSGerrit Uitslag * @param string $text text in the middle 1235140cfbcdSGerrit Uitslag * @param string $suf suffix 1236140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1237140cfbcdSGerrit Uitslag * @return string 1238f3f0262cSandi */ 1239f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1240f3f0262cSandi if($pretty) { 124180fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12423272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12433272d797SAndreas Gohr ) { 124480fcb268SAdrian Lang $pre .= "\n"; 124580fcb268SAdrian Lang } 124680fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12473272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12483272d797SAndreas Gohr ) { 124980fcb268SAdrian Lang $text .= "\n"; 125080fcb268SAdrian Lang } 1251f3f0262cSandi } 1252f3f0262cSandi 1253f3f0262cSandi return $pre.$text.$suf; 1254f3f0262cSandi} 1255f3f0262cSandi 1256f3f0262cSandi/** 1257b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1258b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1259b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1260b24d9195SAndreas Gohr * 1261b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1262b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1263b24d9195SAndreas Gohr * 1264b24d9195SAndreas Gohr * @param string $id the page ID 1265b24d9195SAndreas Gohr */ 1266b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 1267b24d9195SAndreas Gohr global $lang; 1268b24d9195SAndreas Gohr 12698c7319beSGerrit Uitslag $fileLastMod = wikiFN($id); 12708c7319beSGerrit Uitslag $lastMod = @filemtime($fileLastMod); // from page 1271b24d9195SAndreas Gohr $pagelog = new PageChangeLog($id, 1024); 12728c7319beSGerrit Uitslag $lastRev = $pagelog->getRevisions(-1, 1); // from changelog 12738c7319beSGerrit Uitslag $lastRev = (int) (empty($lastRev) ? 0 : $lastRev[0]); 1274b24d9195SAndreas Gohr 12758c7319beSGerrit Uitslag if(!file_exists(wikiFN($id, $lastMod)) && file_exists($fileLastMod) && $lastMod >= $lastRev) { 1276b24d9195SAndreas Gohr // add old revision to the attic if missing 1277b24d9195SAndreas Gohr saveOldRevision($id); 1278b24d9195SAndreas Gohr // add a changelog entry if this edit came from outside dokuwiki 12798c7319beSGerrit Uitslag if($lastMod > $lastRev) { 12808c7319beSGerrit Uitslag $fileLastRev = wikiFN($id, $lastRev); 12818c7319beSGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($lastRev); 12823c48b1d0SGerrit Uitslag if(empty($lastRev) || !file_exists($fileLastRev) || $revinfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 12834b5aebc1SGerrit Uitslag $filesize_old = 0; 12844b5aebc1SGerrit Uitslag } else { 12858c7319beSGerrit Uitslag $filesize_old = io_getSizeFile($fileLastRev); 12864b5aebc1SGerrit Uitslag } 12878c7319beSGerrit Uitslag $filesize_new = filesize($fileLastMod); 12882966355bSGerrit Uitslag $sizechange = $filesize_new - $filesize_old; 12892966355bSGerrit Uitslag 129064159a61SAndreas Gohr addLogEntry( 129164159a61SAndreas Gohr $lastMod, 129264159a61SAndreas Gohr $id, 129364159a61SAndreas Gohr DOKU_CHANGE_TYPE_EDIT, 129464159a61SAndreas Gohr $lang['external_edit'], 129564159a61SAndreas Gohr '', 129664159a61SAndreas Gohr array('ExternalEdit' => true), 129764159a61SAndreas Gohr $sizechange 129864159a61SAndreas Gohr ); 1299b24d9195SAndreas Gohr // remove soon to be stale instructions 13000db5771eSMichael Große $cache = new CacheInstructions($id, $fileLastMod); 1301b24d9195SAndreas Gohr $cache->removeCache(); 1302b24d9195SAndreas Gohr } 1303b24d9195SAndreas Gohr } 1304b24d9195SAndreas Gohr} 1305b24d9195SAndreas Gohr 1306b24d9195SAndreas Gohr/** 1307a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1308a701424fSBen Coburn * Also directs changelog and attic updates. 130915fae107Sandi * 131015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 131171726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1312140cfbcdSGerrit Uitslag * 1313140cfbcdSGerrit Uitslag * @param string $id page id 1314140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1315140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1316140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1317f3f0262cSandi */ 1318b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1319a701424fSBen Coburn /* Note to developers: 1320a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1321a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1322a701424fSBen Coburn after any changes. External edits change the wiki page 1323a701424fSBen Coburn directly without using php or dokuwiki. 1324a701424fSBen Coburn */ 1325f3f0262cSandi global $conf; 1326f3f0262cSandi global $lang; 132771726d78SBen Coburn global $REV; 1328585bf44eSChristopher Smith /* @var Input $INPUT */ 1329585bf44eSChristopher Smith global $INPUT; 1330585bf44eSChristopher Smith 1331b24d9195SAndreas Gohr // prepare data for event 1332b24d9195SAndreas Gohr $svdta = array(); 1333b24d9195SAndreas Gohr $svdta['id'] = $id; 1334b24d9195SAndreas Gohr $svdta['file'] = wikiFN($id); 1335b24d9195SAndreas Gohr $svdta['revertFrom'] = $REV; 1336b24d9195SAndreas Gohr $svdta['oldRevision'] = @filemtime($svdta['file']); 1337b24d9195SAndreas Gohr $svdta['newRevision'] = 0; 1338b24d9195SAndreas Gohr $svdta['newContent'] = $text; 1339b24d9195SAndreas Gohr $svdta['oldContent'] = rawWiki($id); 1340b24d9195SAndreas Gohr $svdta['summary'] = $summary; 1341b24d9195SAndreas Gohr $svdta['contentChanged'] = ($svdta['newContent'] != $svdta['oldContent']); 1342b24d9195SAndreas Gohr $svdta['changeInfo'] = ''; 1343b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_EDIT; 13442966355bSGerrit Uitslag $svdta['sizechange'] = null; 1345b24d9195SAndreas Gohr 1346b24d9195SAndreas Gohr // select changelog line type 1347b24d9195SAndreas Gohr if($REV) { 1348b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_REVERT; 1349b24d9195SAndreas Gohr $svdta['changeInfo'] = $REV; 1350b24d9195SAndreas Gohr } else if(!file_exists($svdta['file'])) { 1351b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_CREATE; 1352b24d9195SAndreas Gohr } else if(trim($text) == '') { 1353b24d9195SAndreas Gohr // empty or whitespace only content deletes 1354b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_DELETE; 1355b24d9195SAndreas Gohr // autoset summary on deletion 1356655ddc1dSGerrit Uitslag if(blank($svdta['summary'])) { 1357655ddc1dSGerrit Uitslag $svdta['summary'] = $lang['deleted']; 1358655ddc1dSGerrit Uitslag } 1359b24d9195SAndreas Gohr } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 1360b24d9195SAndreas Gohr //minor edits only for logged in users 1361b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_MINOR_EDIT; 1362f3f0262cSandi } 1363f3f0262cSandi 1364b24d9195SAndreas Gohr $event = new Doku_Event('COMMON_WIKIPAGE_SAVE', $svdta); 1365b24d9195SAndreas Gohr if(!$event->advise_before()) return; 1366f3f0262cSandi 1367b24d9195SAndreas Gohr // if the content has not been changed, no save happens (plugins may override this) 1368b24d9195SAndreas Gohr if(!$svdta['contentChanged']) return; 1369b24d9195SAndreas Gohr 1370b24d9195SAndreas Gohr detectExternalEdit($id); 1371f3f0262cSandi 13724b5aebc1SGerrit Uitslag if( 13734b5aebc1SGerrit Uitslag $svdta['changeType'] == DOKU_CHANGE_TYPE_CREATE || 13744b5aebc1SGerrit Uitslag ($svdta['changeType'] == DOKU_CHANGE_TYPE_REVERT && !file_exists($svdta['file'])) 13754b5aebc1SGerrit Uitslag ) { 1376ac3ed4afSGerrit Uitslag $filesize_old = 0; 1377ac3ed4afSGerrit Uitslag } else { 13782966355bSGerrit Uitslag $filesize_old = filesize($svdta['file']); 1379ac3ed4afSGerrit Uitslag } 1380b24d9195SAndreas Gohr if($svdta['changeType'] == DOKU_CHANGE_TYPE_DELETE) { 138130725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 1382b24d9195SAndreas Gohr $data = array(array($svdta['file'], '', false), getNS($id), noNS($id), false); 138330725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1384e45b34cdSBen Coburn // pre-save deleted revision 1385b24d9195SAndreas Gohr @touch($svdta['file']); 138646844156SBen Coburn clearstatcache(); 13872d69eb44SMichael Hamann $svdta['newRevision'] = saveOldRevision($id); 1388e1f3d9e1SEsther Brunner // remove empty file 1389b24d9195SAndreas Gohr @unlink($svdta['file']); 1390ac3ed4afSGerrit Uitslag $filesize_new = 0; 139164159a61SAndreas Gohr // don't remove old meta info as it should be saved, plugins can use 139264159a61SAndreas Gohr // IO_WIKIPAGE_WRITE for removing their metadata... 1393c5f92742SMichael Hamann // purge non-persistant meta data 13943d1f9ec3SMichael Klier p_purge_metadata($id); 139553d6ccfeSandi // remove empty namespaces 1396cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1397cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1398f3f0262cSandi } else { 1399cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 140033d979e7SMichael Große io_writeWikiPage($svdta['file'], $svdta['newContent'], $id); 140146844156SBen Coburn // pre-save the revision, to keep the attic in sync 1402b24d9195SAndreas Gohr $svdta['newRevision'] = saveOldRevision($id); 14032966355bSGerrit Uitslag $filesize_new = filesize($svdta['file']); 1404f3f0262cSandi } 14052966355bSGerrit Uitslag $svdta['sizechange'] = $filesize_new - $filesize_old; 1406f3f0262cSandi 1407b24d9195SAndreas Gohr $event->advise_after(); 140871726d78SBen Coburn 140964159a61SAndreas Gohr addLogEntry( 141064159a61SAndreas Gohr $svdta['newRevision'], 141164159a61SAndreas Gohr $svdta['id'], 141264159a61SAndreas Gohr $svdta['changeType'], 141364159a61SAndreas Gohr $svdta['summary'], 141464159a61SAndreas Gohr $svdta['changeInfo'], 141564159a61SAndreas Gohr null, 141664159a61SAndreas Gohr $svdta['sizechange'] 141764159a61SAndreas Gohr ); 1418ac3ed4afSGerrit Uitslag 141926a0801fSAndreas Gohr // send notify mails 1420b24d9195SAndreas Gohr notify($svdta['id'], 'admin', $svdta['oldRevision'], $svdta['summary'], $minor); 1421b24d9195SAndreas Gohr notify($svdta['id'], 'subscribers', $svdta['oldRevision'], $svdta['summary'], $minor); 1422f3f0262cSandi 1423ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 142498407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 14252eccbdaaSGina Haeussge 14262eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1427fe9ec250SChris Smith if(useHeading('content')) { 142807ff0babSMichael Hamann $pages = ft_backlinks($id, true); 14292eccbdaaSGina Haeussge foreach($pages as $page) { 14300db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 14312eccbdaaSGina Haeussge $cache->removeCache(); 14322eccbdaaSGina Haeussge } 14332eccbdaaSGina Haeussge } 1434f3f0262cSandi} 1435f3f0262cSandi 1436f3f0262cSandi/** 1437f3f0262cSandi * moves the current version to the attic and returns its 1438f3f0262cSandi * revision date 143915fae107Sandi * 144015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1441140cfbcdSGerrit Uitslag * 1442140cfbcdSGerrit Uitslag * @param string $id page id 1443140cfbcdSGerrit Uitslag * @return int|string revision timestamp 1444f3f0262cSandi */ 1445f3f0262cSandifunction saveOldRevision($id) { 1446f3f0262cSandi $oldf = wikiFN($id); 144779e79377SAndreas Gohr if(!file_exists($oldf)) return ''; 1448f3f0262cSandi $date = filemtime($oldf); 1449f3f0262cSandi $newf = wikiFN($id, $date); 1450cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1451f3f0262cSandi return $date; 1452f3f0262cSandi} 1453f3f0262cSandi 1454f3f0262cSandi/** 1455fde10de4SAdrian Lang * Sends a notify mail on page change or registration 145626a0801fSAndreas Gohr * 145726a0801fSAndreas Gohr * @param string $id The changed page 1458fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 14593272d797SAndreas Gohr * @param int|string $rev Old page revision 146026a0801fSAndreas Gohr * @param string $summary What changed 146190033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 146242ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 14633272d797SAndreas Gohr * @return bool 1464140cfbcdSGerrit Uitslag * 146515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1466f3f0262cSandi */ 146702a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1468f3f0262cSandi global $conf; 1469585bf44eSChristopher Smith /* @var Input $INPUT */ 1470585bf44eSChristopher Smith global $INPUT; 1471b158d625SSteven Danz 14726df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 147326a0801fSAndreas Gohr if($who == 'admin') { 14743272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 14752ed38036SAndreas Gohr $tpl = 'mailtext'; 147626a0801fSAndreas Gohr $to = $conf['notify']; 147726a0801fSAndreas Gohr } elseif($who == 'subscribers') { 147884c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1479585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 14800bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 14813272d797SAndreas Gohr trigger_event( 14823272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1483835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 14843272d797SAndreas Gohr ); 14852ed38036SAndreas Gohr $to = $data['addresslist']; 14862ed38036SAndreas Gohr if(empty($to)) return false; 14872ed38036SAndreas Gohr $tpl = 'subscr_single'; 148826a0801fSAndreas Gohr } else { 14893272d797SAndreas Gohr return false; //just to be safe 149026a0801fSAndreas Gohr } 149126a0801fSAndreas Gohr 14926df843eeSAndreas Gohr // prepare content 1493*704a815fSMichael Große $subscription = new PageSubscriptionSender(); 149475d66495SMichael Große return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary); 1495f3f0262cSandi} 14962ed38036SAndreas Gohr 149715fae107Sandi/** 149871f7bde7SAndreas Gohr * extracts the query from a search engine referrer 149915fae107Sandi * 150015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 150171f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1502140cfbcdSGerrit Uitslag * 1503140cfbcdSGerrit Uitslag * @return array|string 1504f3f0262cSandi */ 1505f3f0262cSandifunction getGoogleQuery() { 1506585bf44eSChristopher Smith /* @var Input $INPUT */ 1507585bf44eSChristopher Smith global $INPUT; 1508585bf44eSChristopher Smith 1509585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1510c66972f2SAdrian Lang return ''; 1511c66972f2SAdrian Lang } 1512585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1513f3f0262cSandi 1514079b3ac1SAndreas Gohr // only handle common SEs 1515079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1516e4d8a516SKazutaka Miyasaka 1517079b3ac1SAndreas Gohr $query = array(); 1518e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1519e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1520e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1521f3f0262cSandi parse_str($url['query'], $query); 1522e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1523e4d8a516SKazutaka Miyasaka 1524c66972f2SAdrian Lang $q = ''; 1525079b3ac1SAndreas Gohr if(isset($query['q'])){ 1526079b3ac1SAndreas Gohr $q = $query['q']; 1527079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1528079b3ac1SAndreas Gohr $q = $query['p']; 1529079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1530079b3ac1SAndreas Gohr $q = $query['query']; 1531079b3ac1SAndreas Gohr } 1532079b3ac1SAndreas Gohr $q = trim($q); 1533f3f0262cSandi 1534079b3ac1SAndreas Gohr if(!$q) return ''; 15356531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1536f93b3b50SAndreas Gohr return $q; 1537f3f0262cSandi} 1538f3f0262cSandi 1539f3f0262cSandi/** 1540f3f0262cSandi * Return the human readable size of a file 1541f3f0262cSandi * 1542f3f0262cSandi * @param int $size A file size 1543f3f0262cSandi * @param int $dec A number of decimal places 154474160ca1SGerrit Uitslag * @return string human readable size 1545140cfbcdSGerrit Uitslag * 1546f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1547f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1548f3f0262cSandi * @version 1.0.0 1549f3f0262cSandi */ 1550f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1551f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1552f3f0262cSandi $count = count($sizes); 1553f3f0262cSandi $i = 0; 1554f3f0262cSandi 1555f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1556f3f0262cSandi $size /= 1024; 1557f3f0262cSandi $i++; 1558f3f0262cSandi } 1559f3f0262cSandi 1560ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1561f3f0262cSandi} 1562f3f0262cSandi 156315fae107Sandi/** 1564c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1565c57e365eSAndreas Gohr * 1566c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1567140cfbcdSGerrit Uitslag * 1568140cfbcdSGerrit Uitslag * @param int $dt timestamp 1569140cfbcdSGerrit Uitslag * @return string 1570c57e365eSAndreas Gohr */ 1571c57e365eSAndreas Gohrfunction datetime_h($dt) { 1572c57e365eSAndreas Gohr global $lang; 1573c57e365eSAndreas Gohr 1574c57e365eSAndreas Gohr $ago = time() - $dt; 1575c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1576c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1577c57e365eSAndreas Gohr } 1578c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1579c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1580c57e365eSAndreas Gohr } 1581c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1582c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1583c57e365eSAndreas Gohr } 1584c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1585c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1586c57e365eSAndreas Gohr } 1587c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1588c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1589c57e365eSAndreas Gohr } 1590c57e365eSAndreas Gohr if($ago > 60 * 2) { 1591c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1592c57e365eSAndreas Gohr } 1593c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1594c57e365eSAndreas Gohr} 1595c57e365eSAndreas Gohr 1596c57e365eSAndreas Gohr/** 1597f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1598f2263577SAndreas Gohr * 1599f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1600f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1601f2263577SAndreas Gohr * 1602f2263577SAndreas Gohr * @see datetime_h 1603f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1604140cfbcdSGerrit Uitslag * 1605140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1606140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1607140cfbcdSGerrit Uitslag * @return string 1608f2263577SAndreas Gohr */ 1609f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1610f2263577SAndreas Gohr global $conf; 1611f2263577SAndreas Gohr 1612f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1613f2263577SAndreas Gohr $dt = (int) $dt; 1614f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1615f2263577SAndreas Gohr 1616f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1617f2263577SAndreas Gohr return strftime($format, $dt); 1618f2263577SAndreas Gohr} 1619f2263577SAndreas Gohr 1620f2263577SAndreas Gohr/** 1621c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1622c4f79b71SMichael Hamann * 1623c4f79b71SMichael Hamann * @author <ungu at terong dot com> 162459752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1625140cfbcdSGerrit Uitslag * 16267e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 16273272d797SAndreas Gohr * @return string 1628c4f79b71SMichael Hamann */ 1629c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1630c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1631c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1632c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1633c4f79b71SMichael Hamann $date_mod .= $time_zone; 1634c4f79b71SMichael Hamann return $date_mod; 1635c4f79b71SMichael Hamann} 1636c4f79b71SMichael Hamann 1637c4f79b71SMichael Hamann/** 163800a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 163900a7b5adSEsther Brunner * 164000a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 164100a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1642140cfbcdSGerrit Uitslag * 1643140cfbcdSGerrit Uitslag * @param string $email email address 1644140cfbcdSGerrit Uitslag * @return string 164500a7b5adSEsther Brunner */ 164600a7b5adSEsther Brunnerfunction obfuscate($email) { 164700a7b5adSEsther Brunner global $conf; 164800a7b5adSEsther Brunner 164900a7b5adSEsther Brunner switch($conf['mailguard']) { 165000a7b5adSEsther Brunner case 'visible' : 165100a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 165200a7b5adSEsther Brunner return strtr($email, $obfuscate); 165300a7b5adSEsther Brunner 165400a7b5adSEsther Brunner case 'hex' : 165500a7b5adSEsther Brunner $encode = ''; 165649eb6e38SAndreas Gohr $len = strlen($email); 165749eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 165849eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 165949eb6e38SAndreas Gohr } 166000a7b5adSEsther Brunner return $encode; 166100a7b5adSEsther Brunner 166200a7b5adSEsther Brunner case 'none' : 166300a7b5adSEsther Brunner default : 166400a7b5adSEsther Brunner return $email; 166500a7b5adSEsther Brunner } 166600a7b5adSEsther Brunner} 166700a7b5adSEsther Brunner 166800a7b5adSEsther Brunner/** 166989541d4bSAndreas Gohr * Removes quoting backslashes 167089541d4bSAndreas Gohr * 167189541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1672140cfbcdSGerrit Uitslag * 1673140cfbcdSGerrit Uitslag * @param string $string 1674140cfbcdSGerrit Uitslag * @param string $char backslashed character 1675140cfbcdSGerrit Uitslag * @return string 167689541d4bSAndreas Gohr */ 167789541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 167889541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 167989541d4bSAndreas Gohr} 168089541d4bSAndreas Gohr 168173038c47SAndreas Gohr/** 168273038c47SAndreas Gohr * Convert php.ini shorthands to byte 168373038c47SAndreas Gohr * 168473038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 168559752844SAnders Sandblad * @link http://php.net/manual/en/ini.core.php#79564 1686140cfbcdSGerrit Uitslag * 1687140cfbcdSGerrit Uitslag * @param string $v shorthands 1688140cfbcdSGerrit Uitslag * @return int|string 168973038c47SAndreas Gohr */ 169073038c47SAndreas Gohrfunction php_to_byte($v) { 169173038c47SAndreas Gohr $l = substr($v, -1); 169273038c47SAndreas Gohr $ret = substr($v, 0, -1); 169373038c47SAndreas Gohr switch(strtoupper($l)) { 169474160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 16952402e44bSAndreas Gohr // no-break 169673038c47SAndreas Gohr case 'P': 169773038c47SAndreas Gohr $ret *= 1024; 169874160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 16992402e44bSAndreas Gohr // no-break 170073038c47SAndreas Gohr case 'T': 170173038c47SAndreas Gohr $ret *= 1024; 170274160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 17032402e44bSAndreas Gohr // no-break 170473038c47SAndreas Gohr case 'G': 170573038c47SAndreas Gohr $ret *= 1024; 170674160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 17072402e44bSAndreas Gohr // no-break 170873038c47SAndreas Gohr case 'M': 170973038c47SAndreas Gohr $ret *= 1024; 1710f168548cSGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 17112402e44bSAndreas Gohr // no-break 171273038c47SAndreas Gohr case 'K': 171373038c47SAndreas Gohr $ret *= 1024; 171473038c47SAndreas Gohr break; 17152402e44bSAndreas Gohr default: 171649cbd23eSOtto Vainio $ret *= 10; 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 = '…') { 1748bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1749bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1750bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1751bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1752bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1753bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1754bd2f6c2fSAndreas Gohr} 1755bd2f6c2fSAndreas Gohr 1756dc58b6f4SAndy Webber/** 1757dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1758dc58b6f4SAndy Webber * in page footer and recent changes pages 1759dc58b6f4SAndy Webber * 1760b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 176115f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1762c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 176315f3bc49SGerrit Uitslag * 1764dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1765dc58b6f4SAndy Webber */ 176615f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1767cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1768dc58b6f4SAndy Webber} 1769dc58b6f4SAndy Webber 177060a396c8SGerrit Uitslag/** 177160a396c8SGerrit Uitslag * Returns users realname w/o link 177260a396c8SGerrit Uitslag * 1773f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 177415f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1775c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 177660a396c8SGerrit Uitslag * 177760a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 177860a396c8SGerrit Uitslag */ 1779cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 178060a396c8SGerrit Uitslag global $conf, $INFO; 178160a396c8SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 178260a396c8SGerrit Uitslag global $auth; 178330f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 178430f6ec4bSGerrit Uitslag global $INPUT; 178560a396c8SGerrit Uitslag 178660a396c8SGerrit Uitslag // prepare initial event data 178760a396c8SGerrit Uitslag $data = array( 178860a396c8SGerrit Uitslag 'username' => $username, // the unique user name 178960a396c8SGerrit Uitslag 'name' => '', 179060a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 179160a396c8SGerrit Uitslag 'target' => '', 179260a396c8SGerrit Uitslag 'pre' => '', 179360a396c8SGerrit Uitslag 'suf' => '', 179460a396c8SGerrit Uitslag 'style' => '', 179560a396c8SGerrit Uitslag 'more' => '', 179660a396c8SGerrit Uitslag 'url' => '', 179760a396c8SGerrit Uitslag 'title' => '', 179860a396c8SGerrit Uitslag 'class' => '' 179960a396c8SGerrit Uitslag ), 18004d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 180115f3bc49SGerrit Uitslag 'textonly' => $textonly 180260a396c8SGerrit Uitslag ); 180362c8004eSGerrit Uitslag if($username === null) { 180430f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 180515f3bc49SGerrit Uitslag if($textonly){ 180615f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 180715f3bc49SGerrit Uitslag }else { 180864159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 180964159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 181060a396c8SGerrit Uitslag } 181115f3bc49SGerrit Uitslag } 181260a396c8SGerrit Uitslag 181360a396c8SGerrit Uitslag $evt = new Doku_Event('COMMON_USER_LINK', $data); 181460a396c8SGerrit Uitslag if($evt->advise_before(true)) { 181560a396c8SGerrit Uitslag if(empty($data['name'])) { 181660a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 181765833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1818dc58b6f4SAndy Webber switch($conf['showuseras']) { 1819dc58b6f4SAndy Webber case 'username': 18207f081821SGerrit Uitslag case 'username_link': 182115f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 182260a396c8SGerrit Uitslag break; 1823dc58b6f4SAndy Webber case 'email': 1824dc58b6f4SAndy Webber case 'email_link': 182560a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 182660a396c8SGerrit Uitslag break; 1827dc58b6f4SAndy Webber } 182865833968SGerrit Uitslag } else { 182965833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 183060a396c8SGerrit Uitslag } 183160a396c8SGerrit Uitslag } 18327f081821SGerrit Uitslag 18337f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 18347f081821SGerrit Uitslag static $xhtml_renderer = null; 18357f081821SGerrit Uitslag 183615f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 18377f081821SGerrit Uitslag 18387f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 183960a396c8SGerrit Uitslag if(!isset($info)) { 184060a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 184160a396c8SGerrit Uitslag } 184260a396c8SGerrit Uitslag if(isset($info) && $info) { 18437f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 184460a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1845dc58b6f4SAndy Webber } else { 18467f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 18477f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 18487f081821SGerrit Uitslag } 18497f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 18507f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 18517f081821SGerrit Uitslag } 18527f081821SGerrit Uitslag $shortcut = 'user'; 1853533772e1SGerrit Uitslag $exists = null; 18546496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 18552a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 18566496c33fSGerrit Uitslag if($exists !== null) { 18576496c33fSGerrit Uitslag if($exists) { 18586496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 18596496c33fSGerrit Uitslag } else { 18606496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 18616496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 18626496c33fSGerrit Uitslag } 18636496c33fSGerrit Uitslag } 1864dc58b6f4SAndy Webber } 1865dc58b6f4SAndy Webber } else { 186615f3bc49SGerrit Uitslag $data['textonly'] = true; 1867dc58b6f4SAndy Webber } 186860a396c8SGerrit Uitslag 186960a396c8SGerrit Uitslag } else { 187015f3bc49SGerrit Uitslag $data['textonly'] = true; 187160a396c8SGerrit Uitslag } 187260a396c8SGerrit Uitslag } 187360a396c8SGerrit Uitslag 187415f3bc49SGerrit Uitslag if($data['textonly']) { 18754d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 187660a396c8SGerrit Uitslag } else { 187760a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 187860a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 187960a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 188060a396c8SGerrit Uitslag } 18814d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 188260a396c8SGerrit Uitslag } 188360a396c8SGerrit Uitslag } 188460a396c8SGerrit Uitslag $evt->advise_after(); 188560a396c8SGerrit Uitslag unset($evt); 188660a396c8SGerrit Uitslag 18874d5fc927SGerrit Uitslag return $data['userlink']; 1888066fee30SAndreas Gohr} 1889066fee30SAndreas Gohr 1890066fee30SAndreas Gohr/** 1891066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1892066fee30SAndreas Gohr * When no image exists, returns an empty string 1893066fee30SAndreas Gohr * 1894066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1895140cfbcdSGerrit Uitslag * 1896066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 18973272d797SAndreas Gohr * @return string 1898066fee30SAndreas Gohr */ 1899066fee30SAndreas Gohrfunction license_img($type) { 1900066fee30SAndreas Gohr global $license; 1901066fee30SAndreas Gohr global $conf; 1902066fee30SAndreas Gohr if(!$conf['license']) return ''; 1903066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1904066fee30SAndreas Gohr $try = array(); 1905066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1906066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1907066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1908066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1909066fee30SAndreas Gohr } 1910066fee30SAndreas Gohr foreach($try as $src) { 191179e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1912066fee30SAndreas Gohr } 1913066fee30SAndreas Gohr return ''; 1914dc58b6f4SAndy Webber} 1915dc58b6f4SAndy Webber 191613c08e2fSMichael Klier/** 191713c08e2fSMichael Klier * Checks if the given amount of memory is available 191813c08e2fSMichael Klier * 191913c08e2fSMichael Klier * If the memory_get_usage() function is not available the 192013c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 192113c08e2fSMichael Klier * 192213c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 192313c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 19243272d797SAndreas Gohr * 19253272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1926140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 19273272d797SAndreas Gohr * @return bool 192813c08e2fSMichael Klier */ 192913c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 193013c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 193113c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1932985d6187SElenchus if($limit == -1) return true; // unlimited 193313c08e2fSMichael Klier 193413c08e2fSMichael Klier // parse limit to bytes 193513c08e2fSMichael Klier $limit = php_to_byte($limit); 193613c08e2fSMichael Klier 193713c08e2fSMichael Klier // get used memory if possible 193813c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 193913c08e2fSMichael Klier $used = memory_get_usage(); 194049eb6e38SAndreas Gohr } else { 194149eb6e38SAndreas Gohr $used = $bytes; 194213c08e2fSMichael Klier } 194313c08e2fSMichael Klier 194413c08e2fSMichael Klier if($used + $mem > $limit) { 194513c08e2fSMichael Klier return false; 194613c08e2fSMichael Klier } 194713c08e2fSMichael Klier 194813c08e2fSMichael Klier return true; 194913c08e2fSMichael Klier} 195013c08e2fSMichael Klier 1951af2408d5SAndreas Gohr/** 1952af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1953af2408d5SAndreas Gohr * 1954af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1955af2408d5SAndreas Gohr * 1956af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1957af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1958140cfbcdSGerrit Uitslag * 1959140cfbcdSGerrit Uitslag * @param string $url url being directed to 1960af2408d5SAndreas Gohr */ 1961af2408d5SAndreas Gohrfunction send_redirect($url) { 196298ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 196398ca30d2SAndreas Gohr 1964585bf44eSChristopher Smith /* @var Input $INPUT */ 1965585bf44eSChristopher Smith global $INPUT; 1966585bf44eSChristopher Smith 19670181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 19680181f021SAndreas Gohr global $MSG; 19690181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 19700181f021SAndreas Gohr //reopen session, store data and close session again 19710181f021SAndreas Gohr @session_start(); 19720181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 19730181f021SAndreas Gohr } 19740181f021SAndreas Gohr 1975d4869846SAndreas Gohr // always close the session 1976d4869846SAndreas Gohr session_write_close(); 1977d4869846SAndreas Gohr 1978af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1979585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1980585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1981585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 19823272d797SAndreas Gohr $matches[1] < 6 19833272d797SAndreas Gohr ) { 1984af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1985af2408d5SAndreas Gohr } else { 1986af2408d5SAndreas Gohr header('Location: '.$url); 1987af2408d5SAndreas Gohr } 198881781cb6SAndreas Gohr 1989572dc222SLarsDW223 // no exits during unit tests 199027c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 199127c0c399SAndreas Gohr // pass info about the redirect back to the test suite 199227c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 199327c0c399SAndreas Gohr if($testRequest !== null) { 199427c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 199527c0c399SAndreas Gohr } 1996572dc222SLarsDW223 return; 1997572dc222SLarsDW223 } 199827c0c399SAndreas Gohr 1999af2408d5SAndreas Gohr exit; 2000af2408d5SAndreas Gohr} 2001af2408d5SAndreas Gohr 20025b75cd1fSAdrian Lang/** 20035b75cd1fSAdrian Lang * Validate a value using a set of valid values 20045b75cd1fSAdrian Lang * 20055b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 20065b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 20075b75cd1fSAdrian Lang * default is specified, throws an exception. 20085b75cd1fSAdrian Lang * 20095b75cd1fSAdrian Lang * @param string $param The name of the parameter 20105b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 20115b75cd1fSAdrian Lang * be marked by the key “default”. 20125b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 20135b75cd1fSAdrian Lang * or $_GET) 20145b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 20155b75cd1fSAdrian Lang * 20163272d797SAndreas Gohr * @throws Exception 20173272d797SAndreas Gohr * @return mixed 20185b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 20195b75cd1fSAdrian Lang */ 20205b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 20215b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 20225b75cd1fSAdrian Lang return $array[$param]; 20235b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 20245b75cd1fSAdrian Lang return $valid_values['default']; 20255b75cd1fSAdrian Lang } else { 20265b75cd1fSAdrian Lang throw new Exception($exc); 20275b75cd1fSAdrian Lang } 20285b75cd1fSAdrian Lang} 20295b75cd1fSAdrian Lang 203063703ba5SAndreas Gohr/** 203163703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 2032646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 2033140cfbcdSGerrit Uitslag * 2034140cfbcdSGerrit Uitslag * @param string $pref preference key 2035b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 2036140cfbcdSGerrit Uitslag * @return string preference value 203763703ba5SAndreas Gohr */ 2038554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 2039646a531aSChristopher Smith $enc_pref = urlencode($pref); 204006c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 2041554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 204263703ba5SAndreas Gohr $cnt = count($parts); 20431c3eca7dSPhy 20441c3eca7dSPhy // due to #2721 there might be duplicate entries, 20451c3eca7dSPhy // so we read from the end 20461c3eca7dSPhy for($i = $cnt-2; $i >= 0; $i -= 2) { 2047646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 2048646a531aSChristopher Smith return urldecode($parts[$i + 1]); 2049554a8c9fSAdrian Lang } 2050554a8c9fSAdrian Lang } 2051554a8c9fSAdrian Lang } 2052554a8c9fSAdrian Lang return $default; 2053554a8c9fSAdrian Lang} 2054554a8c9fSAdrian Lang 20553c94d07bSAnika Henke/** 20563c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 205736ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 20583a970889SAnika Henke * Remove it by setting $val to false 2059140cfbcdSGerrit Uitslag * 2060140cfbcdSGerrit Uitslag * @param string $pref preference key 2061140cfbcdSGerrit Uitslag * @param string $val preference value 20623c94d07bSAnika Henke */ 20633c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 20643c94d07bSAnika Henke global $conf; 20653c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 20663c94d07bSAnika Henke $cookieVal = ''; 20673c94d07bSAnika Henke 20681c3eca7dSPhy if($orig !== false && ($orig !== $val)) { 20693c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 20703c94d07bSAnika Henke $cnt = count($parts); 207136ec377eSChristopher Smith // urlencode $pref for the comparison 207236ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 20731c3eca7dSPhy $seen = false; 20743c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 207536ec377eSChristopher Smith if ($parts[$i] == $enc_pref) { 20761c3eca7dSPhy if (!$seen){ 20773a970889SAnika Henke if ($val !== false) { 207836ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 20793a970889SAnika Henke } else { 20803a970889SAnika Henke unset($parts[$i]); 20813a970889SAnika Henke unset($parts[$i + 1]); 20823a970889SAnika Henke } 20831c3eca7dSPhy $seen = true; 20841c3eca7dSPhy } else { 20851c3eca7dSPhy // no break because we want to remove duplicate entries 20861c3eca7dSPhy unset($parts[$i]); 20871c3eca7dSPhy unset($parts[$i + 1]); 20881c3eca7dSPhy } 20893c94d07bSAnika Henke } 20903c94d07bSAnika Henke } 20913c94d07bSAnika Henke $cookieVal = implode('#', $parts); 20921c3eca7dSPhy } else if ($orig === false && $val !== false) { 209364159a61SAndreas Gohr $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 209464159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 20953c94d07bSAnika Henke } 20963c94d07bSAnika Henke 209775e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 20985833995aSPhy if(defined('DOKU_UNITTEST')) { 20995833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 21005833995aSPhy }else{ 210175e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 21023c94d07bSAnika Henke } 21033c94d07bSAnika Henke} 21043c94d07bSAnika Henke 2105f8fb2d18SAndreas Gohr/** 2106f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 2107f8fb2d18SAndreas Gohr * 210842ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 2109f8fb2d18SAndreas Gohr */ 2110f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 2111f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 2112f8fb2d18SAndreas Gohr} 2113f8fb2d18SAndreas Gohr 21143c27983bSAndreas Gohr/** 211571de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 21163c27983bSAndreas Gohr * 21173c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 21183c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 21193c27983bSAndreas Gohr * files are embedded. 21203c27983bSAndreas Gohr * 212171de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 212271de5572SAndreas Gohr * 21233c27983bSAndreas Gohr * @param string $file full path to the SVG file 21243c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 212571de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 21263c27983bSAndreas Gohr */ 21274cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 21283c27983bSAndreas Gohr $file = trim($file); 21293c27983bSAndreas Gohr if($file === '') return false; 21303c27983bSAndreas Gohr if(!file_exists($file)) return false; 21313c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 21323c27983bSAndreas Gohr if(!is_readable($file)) return false; 21333c27983bSAndreas Gohr $content = file_get_contents($file); 21340849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 21350849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 21360849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 21370849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 21383c27983bSAndreas Gohr $content = trim($content); 21393c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 214071de5572SAndreas Gohr return $content; 21413c27983bSAndreas Gohr} 21423c27983bSAndreas Gohr 2143e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 2144