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 9*0c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 10*0c3a5702SAndreas Gohr 11f3f0262cSandi/** 12b6912aeaSAndreas Gohr * These constants are used with the recents function 13b6912aeaSAndreas Gohr */ 14b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED', 2); 15b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS', 4); 16b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES', 8); 170b926329SKate Arzamastsevadefine('RECENTS_MEDIA_CHANGES', 16); 180b926329SKate Arzamastsevadefine('RECENTS_MEDIA_PAGES_MIXED', 32); 19b6912aeaSAndreas Gohr 20b6912aeaSAndreas Gohr/** 21d5197206Schris * Wrapper around htmlspecialchars() 22d5197206Schris * 23d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 24d5197206Schris * @see htmlspecialchars() 25140cfbcdSGerrit Uitslag * 26140cfbcdSGerrit Uitslag * @param string $string the string being converted 27140cfbcdSGerrit Uitslag * @return string converted string 28d5197206Schris */ 29d5197206Schrisfunction hsc($string) { 30d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 31d5197206Schris} 32d5197206Schris 33d5197206Schris/** 345b571377SAndreas Gohr * Checks if the given input is blank 355b571377SAndreas Gohr * 365b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 375b571377SAndreas Gohr * 3867234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 3967234204SAndreas Gohr * with a NULL value without warning. 4067234204SAndreas Gohr * 4167234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 4267234204SAndreas Gohr * 4367234204SAndreas Gohr * (isset($foo) && !blank($foo)) 4467234204SAndreas Gohr * (!isset($foo) || blank($foo)) 4567234204SAndreas Gohr * 465b571377SAndreas Gohr * @param $in 475b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 485b571377SAndreas Gohr * @return bool 495b571377SAndreas Gohr */ 505b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 515b571377SAndreas Gohr if(is_null($in)) return true; 525b571377SAndreas Gohr if(is_array($in)) return empty($in); 535b571377SAndreas Gohr if($in === "\0") return true; 545b571377SAndreas Gohr if($trim && trim($in) === '') return true; 555b571377SAndreas Gohr if(strlen($in) > 0) return false; 565b571377SAndreas Gohr return empty($in); 575b571377SAndreas Gohr} 585b571377SAndreas Gohr 595b571377SAndreas Gohr/** 60d5197206Schris * print a newline terminated string 61d5197206Schris * 62d5197206Schris * You can give an indention as optional parameter 63d5197206Schris * 64d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 65140cfbcdSGerrit Uitslag * 66140cfbcdSGerrit Uitslag * @param string $string line of text 67140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 68d5197206Schris */ 6925ec097bSChris Smithfunction ptln($string, $indent = 0) { 7025ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 7102b0b681SAndreas Gohr} 7202b0b681SAndreas Gohr 7302b0b681SAndreas Gohr/** 7402b0b681SAndreas Gohr * strips control characters (<32) from the given string 7502b0b681SAndreas Gohr * 7602b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 77140cfbcdSGerrit Uitslag * 7842ea7f44SGerrit Uitslag * @param string $string being stripped 79140cfbcdSGerrit Uitslag * @return string 8002b0b681SAndreas Gohr */ 8102b0b681SAndreas Gohrfunction stripctl($string) { 8202b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 83d5197206Schris} 84d5197206Schris 85d5197206Schris/** 86634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 87634d7150SAndreas Gohr * 88634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 89634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 90634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 9142ea7f44SGerrit Uitslag * 92634d7150SAndreas Gohr * @return string 93634d7150SAndreas Gohr */ 94634d7150SAndreas Gohrfunction getSecurityToken() { 95585bf44eSChristopher Smith /** @var Input $INPUT */ 96585bf44eSChristopher Smith global $INPUT; 973680e2cdSAndreas Gohr 983680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 993680e2cdSAndreas Gohr $session = session_id(); 1003680e2cdSAndreas Gohr 1013680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1023680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 1033680e2cdSAndreas Gohr return PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 104634d7150SAndreas Gohr} 105634d7150SAndreas Gohr 106634d7150SAndreas Gohr/** 107634d7150SAndreas Gohr * Check the secret CSRF token 108140cfbcdSGerrit Uitslag * 109140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 110140cfbcdSGerrit Uitslag * @return bool success if the token matched 111634d7150SAndreas Gohr */ 112634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 113585bf44eSChristopher Smith /** @var Input $INPUT */ 1147d01a0eaSTom N Harris global $INPUT; 115585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 116df97eaacSAndreas Gohr 1177d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 118634d7150SAndreas Gohr if(getSecurityToken() != $token) { 119634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 120634d7150SAndreas Gohr return false; 121634d7150SAndreas Gohr } 122634d7150SAndreas Gohr return true; 123634d7150SAndreas Gohr} 124634d7150SAndreas Gohr 125634d7150SAndreas Gohr/** 126634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 127634d7150SAndreas Gohr * 128634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 129140cfbcdSGerrit Uitslag * 130140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 13142ea7f44SGerrit Uitslag * @return string html of hidden form field 132634d7150SAndreas Gohr */ 133634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1342404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1353272d797SAndreas Gohr if($print) echo $ret; 136634d7150SAndreas Gohr return $ret; 137634d7150SAndreas Gohr} 138634d7150SAndreas Gohr 139634d7150SAndreas Gohr/** 1401015a57dSChristopher Smith * Determine basic information for a request of $id 14115fae107Sandi * 14215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1437e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 144140cfbcdSGerrit Uitslag * 145140cfbcdSGerrit Uitslag * @param string $id pageid 146140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 147140cfbcdSGerrit Uitslag * @return array with info for a request of $id 148140cfbcdSGerrit Uitslag * 149f3f0262cSandi */ 1501015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 151f3f0262cSandi global $USERINFO; 152585bf44eSChristopher Smith /* @var Input $INPUT */ 153585bf44eSChristopher Smith global $INPUT; 1546afe8dcaSchris 155c66972f2SAdrian Lang // set info about manager/admin status. 15659bc3b48SGerrit Uitslag $info = array(); 157c66972f2SAdrian Lang $info['isadmin'] = false; 158c66972f2SAdrian Lang $info['ismanager'] = false; 159585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 160f3f0262cSandi $info['userinfo'] = $USERINFO; 1611015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 162585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 16317ee7f66SAndreas Gohr 164f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 165f8cc712eSAndreas Gohr $info['isadmin'] = true; 166f8cc712eSAndreas Gohr $info['ismanager'] = true; 167f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 168f8cc712eSAndreas Gohr $info['ismanager'] = true; 169f8cc712eSAndreas Gohr } 170f8cc712eSAndreas Gohr 17117ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 17217ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 173585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 17417ee7f66SAndreas Gohr } 175ee4c4a1bSAndreas Gohr 176f3f0262cSandi } else { 1771015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 178ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 179f3f0262cSandi } 180f3f0262cSandi 1811015a57dSChristopher Smith $info['namespace'] = getNS($id); 1821015a57dSChristopher Smith 1831015a57dSChristopher Smith // mobile detection 1841015a57dSChristopher Smith if ($htmlClient) { 1851015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1861015a57dSChristopher Smith } 1871015a57dSChristopher Smith 1881015a57dSChristopher Smith return $info; 1891015a57dSChristopher Smith } 1901015a57dSChristopher Smith 1911015a57dSChristopher Smith/** 1921015a57dSChristopher Smith * Return info about the current document as associative 1931015a57dSChristopher Smith * array. 1941015a57dSChristopher Smith * 1951015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 196140cfbcdSGerrit Uitslag * 197140cfbcdSGerrit Uitslag * @return array with info about current document 1981015a57dSChristopher Smith */ 1991015a57dSChristopher Smithfunction pageinfo() { 2001015a57dSChristopher Smith global $ID; 2011015a57dSChristopher Smith global $REV; 2021015a57dSChristopher Smith global $RANGE; 2031015a57dSChristopher Smith global $lang; 204585bf44eSChristopher Smith /* @var Input $INPUT */ 205585bf44eSChristopher Smith global $INPUT; 2061015a57dSChristopher Smith 2071015a57dSChristopher Smith $info = basicinfo($ID); 2081015a57dSChristopher Smith 2091015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2101015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2111015a57dSChristopher Smith $info['id'] = $ID; 2121015a57dSChristopher Smith $info['rev'] = $REV; 2131015a57dSChristopher Smith 214585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 2157e87a794SChristopher Smith $sub = new Subscription(); 2167e87a794SChristopher Smith $info['subscribed'] = $sub->user_subscription(); 2177e87a794SChristopher Smith } else { 2187e87a794SChristopher Smith $info['subscribed'] = false; 2197e87a794SChristopher Smith } 2207e87a794SChristopher Smith 221f3f0262cSandi $info['locked'] = checklock($ID); 222317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 22379e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 22401c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2252ca9d91cSBen Coburn if($REV) { 2262ca9d91cSBen Coburn //check if current revision was meant 22701c9a118SAndreas Gohr if($info['exists'] && ($info['currentrev'] == $REV)) { 2282ca9d91cSBen Coburn $REV = ''; 2297b3a6803SAndreas Gohr } elseif($RANGE) { 2307b3a6803SAndreas Gohr //section editing does not work with old revisions! 2317b3a6803SAndreas Gohr $REV = ''; 2327b3a6803SAndreas Gohr $RANGE = ''; 2337b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2342ca9d91cSBen Coburn } else { 2352ca9d91cSBen Coburn //really use old revision 236317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 23779e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 238f3f0262cSandi } 239f3f0262cSandi } 240c112d578Sandi $info['rev'] = $REV; 241f3f0262cSandi if($info['exists']) { 242f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 243f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 244f3f0262cSandi } else { 245f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 246f3f0262cSandi } 24750e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 248f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 249f3f0262cSandi 25071726d78SBen Coburn //load page meta data 25171726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 25271726d78SBen Coburn 253652610a2Sandi //who's the editor 254047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 255652610a2Sandi if($REV) { 256f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 257652610a2Sandi } else { 2580e80bb5eSChristopher Smith if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 259aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 260aa27cf05SAndreas Gohr } else { 261f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 262cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 263cd00a034SBen Coburn if($revinfo !== false) { 264cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 265cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 266cd00a034SBen Coburn } 267cd00a034SBen Coburn } 268cd00a034SBen Coburn } 269cd00a034SBen Coburn //and check for an external edit 270cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 271cd00a034SBen Coburn // cached changelog line no longer valid 272cd00a034SBen Coburn $revinfo = false; 273cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 274cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 275652610a2Sandi } 276bb4866bdSchris 277652610a2Sandi $info['ip'] = $revinfo['ip']; 278652610a2Sandi $info['user'] = $revinfo['user']; 279652610a2Sandi $info['sum'] = $revinfo['sum']; 28071726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 281ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 28259f257aeSchris 28388f522e9Sandi if($revinfo['user']) { 28488f522e9Sandi $info['editor'] = $revinfo['user']; 28588f522e9Sandi } else { 28688f522e9Sandi $info['editor'] = $revinfo['ip']; 28788f522e9Sandi } 288652610a2Sandi 289ee4c4a1bSAndreas Gohr // draft 2900aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 2910aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 2920aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 293ee4c4a1bSAndreas Gohr } 294ee4c4a1bSAndreas Gohr 2951015a57dSChristopher Smith return $info; 2961015a57dSChristopher Smith} 2971015a57dSChristopher Smith 2981015a57dSChristopher Smith/** 2990c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 3000c39d46cSMichael Große */ 3010c39d46cSMichael Großefunction jsinfo() { 3020c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3030c39d46cSMichael Große 3040c39d46cSMichael Große if (!is_array($JSINFO)) { 3050c39d46cSMichael Große $JSINFO = []; 3060c39d46cSMichael Große } 3070c39d46cSMichael Große //export minimal info to JS, plugins can add more 3080c39d46cSMichael Große $JSINFO['id'] = $ID; 3090c39d46cSMichael Große $JSINFO['namespace'] = (string) $INFO['namespace']; 3100c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3110c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3120c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3130c39d46cSMichael Große} 3140c39d46cSMichael Große 3150c39d46cSMichael Große/** 3161015a57dSChristopher Smith * Return information about the current media item as an associative array. 317140cfbcdSGerrit Uitslag * 318140cfbcdSGerrit Uitslag * @return array with info about current media item 3191015a57dSChristopher Smith */ 3201015a57dSChristopher Smithfunction mediainfo(){ 3211015a57dSChristopher Smith global $NS; 3221015a57dSChristopher Smith global $IMG; 3231015a57dSChristopher Smith 3241015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3251015a57dSChristopher Smith $info['image'] = $IMG; 3261c548ebeSAndreas Gohr 327f3f0262cSandi return $info; 328f3f0262cSandi} 329f3f0262cSandi 330f3f0262cSandi/** 3312684e50aSAndreas Gohr * Build an string of URL parameters 3322684e50aSAndreas Gohr * 3332684e50aSAndreas Gohr * @author Andreas Gohr 334140cfbcdSGerrit Uitslag * 335140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 336140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 337140cfbcdSGerrit Uitslag * @return string query string 3382684e50aSAndreas Gohr */ 339b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3402684e50aSAndreas Gohr $url = ''; 3412684e50aSAndreas Gohr $amp = false; 3422684e50aSAndreas Gohr foreach($params as $key => $val) { 343b174aeaeSchris if($amp) $url .= $sep; 3442684e50aSAndreas Gohr 34585e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3463a50618cSgweissbach $url .= rawurlencode((string) $val); 3472684e50aSAndreas Gohr $amp = true; 3482684e50aSAndreas Gohr } 3492684e50aSAndreas Gohr return $url; 3502684e50aSAndreas Gohr} 3512684e50aSAndreas Gohr 3522684e50aSAndreas Gohr/** 3532684e50aSAndreas Gohr * Build an string of html tag attributes 3542684e50aSAndreas Gohr * 3557bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3567bff22c0SAndreas Gohr * 3572684e50aSAndreas Gohr * @author Andreas Gohr 358140cfbcdSGerrit Uitslag * 359140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 360140cfbcdSGerrit Uitslag * @param bool $skipempty skip empty string values? 361140cfbcdSGerrit Uitslag * @return string 3622684e50aSAndreas Gohr */ 3634b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 3642684e50aSAndreas Gohr $url = ''; 3659063ec14SAdrian Lang $white = false; 3662684e50aSAndreas Gohr foreach($params as $key => $val) { 3677bff22c0SAndreas Gohr if($key{0} == '_') continue; 368b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 3699063ec14SAdrian Lang if($white) $url .= ' '; 3707bff22c0SAndreas Gohr 3712684e50aSAndreas Gohr $url .= $key.'="'; 3722684e50aSAndreas Gohr $url .= htmlspecialchars($val); 3732684e50aSAndreas Gohr $url .= '"'; 3749063ec14SAdrian Lang $white = true; 3752684e50aSAndreas Gohr } 3762684e50aSAndreas Gohr return $url; 3772684e50aSAndreas Gohr} 3782684e50aSAndreas Gohr 3792684e50aSAndreas Gohr/** 38015fae107Sandi * This builds the breadcrumb trail and returns it as array 38115fae107Sandi * 38215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 383140cfbcdSGerrit Uitslag * 384e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 385f3f0262cSandi */ 386f3f0262cSandifunction breadcrumbs() { 3878746e727Sandi // we prepare the breadcrumbs early for quick session closing 3888746e727Sandi static $crumbs = null; 3898746e727Sandi if($crumbs != null) return $crumbs; 3908746e727Sandi 391f3f0262cSandi global $ID; 392f3f0262cSandi global $ACT; 393f3f0262cSandi global $conf; 394f3f0262cSandi 395f3f0262cSandi //first visit? 396c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 3974d1fee4cSB_S666 //we only save on show and existing visible wiki documents 398a77f5846Sjan $file = wikiFN($ID); 3994d1fee4cSB_S666 if($ACT != 'show' || isHiddenPage($ID) || !file_exists($file)) { 400e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 401f3f0262cSandi return $crumbs; 402f3f0262cSandi } 403a77f5846Sjan 404a77f5846Sjan // page names 4051a84a0f3SAnika Henke $name = noNSorNS($ID); 406fe9ec250SChris Smith if(useHeading('navigation')) { 407a77f5846Sjan // get page title 40867c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 409a77f5846Sjan if($title) { 410a77f5846Sjan $name = $title; 411a77f5846Sjan } 412a77f5846Sjan } 413a77f5846Sjan 414f3f0262cSandi //remove ID from array 415a77f5846Sjan if(isset($crumbs[$ID])) { 416a77f5846Sjan unset($crumbs[$ID]); 417f3f0262cSandi } 418f3f0262cSandi 419f3f0262cSandi //add to array 420a77f5846Sjan $crumbs[$ID] = $name; 421f3f0262cSandi //reduce size 422f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 423f3f0262cSandi array_shift($crumbs); 424f3f0262cSandi } 425f3f0262cSandi //save to session 426e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 427f3f0262cSandi return $crumbs; 428f3f0262cSandi} 429f3f0262cSandi 430f3f0262cSandi/** 43115fae107Sandi * Filter for page IDs 43215fae107Sandi * 433f3f0262cSandi * This is run on a ID before it is outputted somewhere 434f3f0262cSandi * currently used to replace the colon with something else 435907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 436907f24f7SAndreas Gohr * 437907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 438907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 439907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 44015fae107Sandi * 44149c713a3Sandi * Urlencoding is ommitted when the second parameter is false 44249c713a3Sandi * 44315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 444140cfbcdSGerrit Uitslag * 445140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 446140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 447140cfbcdSGerrit Uitslag * @return string 448f3f0262cSandi */ 44949c713a3Sandifunction idfilter($id, $ue = true) { 450f3f0262cSandi global $conf; 451585bf44eSChristopher Smith /* @var Input $INPUT */ 452585bf44eSChristopher Smith global $INPUT; 453585bf44eSChristopher Smith 454f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 455f3f0262cSandi $id = strtr($id, ':', '/'); 456f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 45758bedc8aSborekb $conf['userewrite'] && 458585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4593272d797SAndreas Gohr ) { 460f3f0262cSandi $id = strtr($id, ':', ';'); 461f3f0262cSandi } 46249c713a3Sandi if($ue) { 463b6c6979fSAndreas Gohr $id = rawurlencode($id); 464f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 465edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 466f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 46749c713a3Sandi } 468f3f0262cSandi return $id; 469f3f0262cSandi} 470f3f0262cSandi 471f3f0262cSandi/** 472ed7b5f09Sandi * This builds a link to a wikipage 47315fae107Sandi * 4744bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4756c7843b5Sandi * 47615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4774bc480e5SAndreas Gohr * 4784bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4794bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4804bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4814bc480e5SAndreas Gohr * @param string $separator parameter separator 4824bc480e5SAndreas Gohr * @return string 483f3f0262cSandi */ 48416f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 485f3f0262cSandi global $conf; 48616f15a81SDominik Eckelmann if(is_array($urlParameters)) { 4874bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 48864159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 48964159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 49064159a61SAndreas Gohr } 49116f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4926de3759aSAndreas Gohr } else { 49316f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4946de3759aSAndreas Gohr } 49516f15a81SDominik Eckelmann if($id === '') { 49616f15a81SDominik Eckelmann $id = $conf['start']; 49716f15a81SDominik Eckelmann } 498f3f0262cSandi $id = idfilter($id); 49916f15a81SDominik Eckelmann if($absolute) { 500ed7b5f09Sandi $xlink = DOKU_URL; 501ed7b5f09Sandi } else { 502ed7b5f09Sandi $xlink = DOKU_BASE; 503ed7b5f09Sandi } 504f3f0262cSandi 5056c7843b5Sandi if($conf['userewrite'] == 2) { 5066c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 50716f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5086c7843b5Sandi } elseif($conf['userewrite']) { 509f3f0262cSandi $xlink .= $id; 51016f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 511bce3726dSAndreas Gohr } elseif($id) { 5126c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 51316f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 514bce3726dSAndreas Gohr } else { 515bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 51616f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 517f3f0262cSandi } 518f3f0262cSandi 519f3f0262cSandi return $xlink; 520f3f0262cSandi} 521f3f0262cSandi 522f3f0262cSandi/** 523f5c2808fSBen Coburn * This builds a link to an alternate page format 524f5c2808fSBen Coburn * 525f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 526f5c2808fSBen Coburn * 527f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5284bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5294bc480e5SAndreas Gohr * @param string $format the export renderer to use 5304bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5314bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5324bc480e5SAndreas Gohr * @param string $sep parameter separator 5334bc480e5SAndreas Gohr * @return string 534f5c2808fSBen Coburn */ 5354bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 536f5c2808fSBen Coburn global $conf; 5374bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5384bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 539f5c2808fSBen Coburn } else { 5404bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 541f5c2808fSBen Coburn } 542f5c2808fSBen Coburn 543f5c2808fSBen Coburn $format = rawurlencode($format); 544f5c2808fSBen Coburn $id = idfilter($id); 545f5c2808fSBen Coburn if($abs) { 546f5c2808fSBen Coburn $xlink = DOKU_URL; 547f5c2808fSBen Coburn } else { 548f5c2808fSBen Coburn $xlink = DOKU_BASE; 549f5c2808fSBen Coburn } 550f5c2808fSBen Coburn 551f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 552f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5534bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 554f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 555f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5564bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 557f5c2808fSBen Coburn } else { 558f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5594bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 560f5c2808fSBen Coburn } 561f5c2808fSBen Coburn 562f5c2808fSBen Coburn return $xlink; 563f5c2808fSBen Coburn} 564f5c2808fSBen Coburn 565f5c2808fSBen Coburn/** 5666de3759aSAndreas Gohr * Build a link to a media file 5676de3759aSAndreas Gohr * 5686de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5698c08db0aSAndreas Gohr * 5708c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5718c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5728c08db0aSAndreas Gohr * 5733272d797SAndreas Gohr * @param string $id the media file id or URL 5743272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5753272d797SAndreas Gohr * @param bool $direct link to detail page if false 5763272d797SAndreas Gohr * @param string $sep URL parameter separator 5773272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5783272d797SAndreas Gohr * @return string 5796de3759aSAndreas Gohr */ 58055b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5816de3759aSAndreas Gohr global $conf; 582b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 583826d2766SKlap-in if(!$isexternalimage) { 584826d2766SKlap-in $id = cleanID($id); 585826d2766SKlap-in } 586826d2766SKlap-in 5876de3759aSAndreas Gohr if(is_array($more)) { 5880f4e0092SChristopher Smith // add token for resized images 589443e135dSChristopher Smith if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){ 5900f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 5910f4e0092SChristopher Smith } 5928c08db0aSAndreas Gohr // strip defaults for shorter URLs 5938c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 594443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 595443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 5968c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 59778b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 598b174aeaeSchris $more = buildURLparams($more, $sep); 5996de3759aSAndreas Gohr } else { 6005e7db1e2SChristopher Smith $matches = array(); 601cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 6025e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 6035e7db1e2SChristopher Smith foreach ($matches as $match){ 6045e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 6055e7db1e2SChristopher Smith } 606cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 607cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 6085e7db1e2SChristopher Smith } 6098c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 6108c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 611b174aeaeSchris $more = str_replace(',', $sep, $more); 6126de3759aSAndreas Gohr } 6136de3759aSAndreas Gohr 61455b2b31bSAndreas Gohr if($abs) { 61555b2b31bSAndreas Gohr $xlink = DOKU_URL; 61655b2b31bSAndreas Gohr } else { 6176de3759aSAndreas Gohr $xlink = DOKU_BASE; 61855b2b31bSAndreas Gohr } 6196de3759aSAndreas Gohr 6206de3759aSAndreas Gohr // external URLs are always direct without rewriting 621826d2766SKlap-in if($isexternalimage) { 6226de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 623cc036f74SKlap-in $xlink .= '?'.$more; 624b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 6256de3759aSAndreas Gohr return $xlink; 6266de3759aSAndreas Gohr } 6276de3759aSAndreas Gohr 6286de3759aSAndreas Gohr $id = idfilter($id); 6296de3759aSAndreas Gohr 6306de3759aSAndreas Gohr // decide on scriptname 6316de3759aSAndreas Gohr if($direct) { 6326de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6336de3759aSAndreas Gohr $script = '_media'; 6346de3759aSAndreas Gohr } else { 6356de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 6366de3759aSAndreas Gohr } 6376de3759aSAndreas Gohr } else { 6386de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 6396de3759aSAndreas Gohr $script = '_detail'; 6406de3759aSAndreas Gohr } else { 6416de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 6426de3759aSAndreas Gohr } 6436de3759aSAndreas Gohr } 6446de3759aSAndreas Gohr 6456de3759aSAndreas Gohr // build URL based on rewrite mode 6466de3759aSAndreas Gohr if($conf['userewrite']) { 6476de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 6486de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 6496de3759aSAndreas Gohr } else { 6506de3759aSAndreas Gohr if($more) { 651a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 652b174aeaeSchris $xlink .= $sep.'media='.$id; 6536de3759aSAndreas Gohr } else { 654a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 6556de3759aSAndreas Gohr } 6566de3759aSAndreas Gohr } 6576de3759aSAndreas Gohr 6586de3759aSAndreas Gohr return $xlink; 6596de3759aSAndreas Gohr} 6606de3759aSAndreas Gohr 6616de3759aSAndreas Gohr/** 66225ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 66315fae107Sandi * 66425ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 66525ca5b17SAndreas Gohr * 66615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 667140cfbcdSGerrit Uitslag * 668140cfbcdSGerrit Uitslag * @return string 669f3f0262cSandi */ 67025ca5b17SAndreas Gohrfunction script() { 671ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 672f3f0262cSandi} 673f3f0262cSandi 674f3f0262cSandi/** 67515fae107Sandi * Spamcheck against wordlist 67615fae107Sandi * 677f3f0262cSandi * Checks the wikitext against a list of blocked expressions 678f3f0262cSandi * returns true if the text contains any bad words 67915fae107Sandi * 680e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 681e403cc58SMichael Klier * 682e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 683e403cc58SMichael Klier * and gain information about the user who was blocked. 684e403cc58SMichael Klier * 685e403cc58SMichael Klier * Event data: 686e403cc58SMichael Klier * data['matches'] - array of matches 687e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 688e403cc58SMichael Klier * [ip] - ip address 689e403cc58SMichael Klier * [user] - username (if logged in) 690e403cc58SMichael Klier * [mail] - mail address (if logged in) 691e403cc58SMichael Klier * [name] - real name (if logged in) 692e403cc58SMichael Klier * 69315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6946dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 695140cfbcdSGerrit Uitslag * 6966dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 6976dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 698f3f0262cSandi */ 6996dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 700f3f0262cSandi global $TEXT; 7016dffa0e0SAndreas Gohr global $PRE; 7026dffa0e0SAndreas Gohr global $SUF; 703e0086ca2SAndreas Gohr global $SUM; 704f3f0262cSandi global $conf; 705e403cc58SMichael Klier global $INFO; 706585bf44eSChristopher Smith /* @var Input $INPUT */ 707585bf44eSChristopher Smith global $INPUT; 708f3f0262cSandi 709f3f0262cSandi if(!$conf['usewordblock']) return false; 710f3f0262cSandi 711e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 7126dffa0e0SAndreas Gohr 713041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 71464159a61SAndreas Gohr // phpcs:disable Generic.Files.LineLength.TooLong 71564159a61SAndreas Gohr $text = preg_replace( 71664159a61SAndreas Gohr '!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', 71764159a61SAndreas Gohr '\1http://\2 \2\3', 71864159a61SAndreas Gohr $text 71964159a61SAndreas Gohr ); 72064159a61SAndreas Gohr // phpcs:enable 721041d1964SAndreas Gohr 722b9ac8716Schris $wordblocks = getWordblocks(); 7233e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 7243e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 7253e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 7263e2965d7Sandi // backreferences are used - the maximum is 99 7273e2965d7Sandi // this is very bad performancewise and may even be too high still 7283e2965d7Sandi $chunksize = 40; 7293e2965d7Sandi } else { 730a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 7313e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 732a51d08efSAndreas Gohr $chunksize = 200; 7333e2965d7Sandi } 734b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 735f3f0262cSandi $re = array(); 73649eb6e38SAndreas Gohr // build regexp from blocks 737f3f0262cSandi foreach($blocks as $block) { 738f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 739f3f0262cSandi $block = trim($block); 740f3f0262cSandi if(empty($block)) continue; 741f3f0262cSandi $re[] = $block; 742f3f0262cSandi } 743e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 744e403cc58SMichael Klier // prepare event data 74559bc3b48SGerrit Uitslag $data = array(); 746e403cc58SMichael Klier $data['matches'] = $matches; 747585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 748585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 749585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 750e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 751e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 752e403cc58SMichael Klier } 753bad6fc0dSAndreas Gohr $callback = function () { 754bad6fc0dSAndreas Gohr return true; 755bad6fc0dSAndreas Gohr }; 756e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 757b9ac8716Schris } 758703f6fdeSandi } 759f3f0262cSandi return false; 760f3f0262cSandi} 761f3f0262cSandi 762f3f0262cSandi/** 76315fae107Sandi * Return the IP of the client 76415fae107Sandi * 7656d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 76615fae107Sandi * 7676d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 7686d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 7696d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 7706d8affe6SAndreas Gohr * headers 7716d8affe6SAndreas Gohr * 77215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 773140cfbcdSGerrit Uitslag * 7743272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 7753272d797SAndreas Gohr * @return string 776f3f0262cSandi */ 7776d8affe6SAndreas Gohrfunction clientIP($single = false) { 778585bf44eSChristopher Smith /* @var Input $INPUT */ 779585bf44eSChristopher Smith global $INPUT; 780585bf44eSChristopher Smith 7816d8affe6SAndreas Gohr $ip = array(); 782585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 783585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 784585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 785585bf44eSChristopher Smith } 786585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 787585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 788585bf44eSChristopher Smith } 7896d8affe6SAndreas Gohr 790dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 791dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 792dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 793dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 794dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 795dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 796dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 797dc14c6d1SGuy Brand $IPv6Address = 798dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 799dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 800dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 801dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 802dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 803dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 804dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 805dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 806dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 807dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 808dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 809dc14c6d1SGuy Brand 8106d8affe6SAndreas Gohr // remove any non-IP stuff 8116d8affe6SAndreas Gohr $cnt = count($ip); 8124ff28443Schris $match = array(); 8136d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 814dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 8154ff28443Schris $ip[$i] = $match[0]; 8164ff28443Schris } else { 8174ff28443Schris $ip[$i] = ''; 8184ff28443Schris } 8196d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 820f3f0262cSandi } 8216d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 8226d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 8236d8affe6SAndreas Gohr 8246d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 8256d8affe6SAndreas Gohr 8266d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 8276d8affe6SAndreas Gohr $ip = array_reverse($ip); 8286d8affe6SAndreas Gohr foreach($ip as $i) { 8292343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 8306d8affe6SAndreas Gohr continue; 8316d8affe6SAndreas Gohr } else { 8326d8affe6SAndreas Gohr return $i; 8336d8affe6SAndreas Gohr } 8346d8affe6SAndreas Gohr } 8356d8affe6SAndreas Gohr // still here? just use the first (last) address 8366d8affe6SAndreas Gohr return $ip[0]; 837f3f0262cSandi} 838f3f0262cSandi 839f3f0262cSandi/** 8401c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8411c548ebeSAndreas Gohr * 8421c548ebeSAndreas Gohr * Adapted from the example code at url below 8431c548ebeSAndreas Gohr * 8441c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 845140cfbcdSGerrit Uitslag * 84664159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 847140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8481c548ebeSAndreas Gohr */ 8491c548ebeSAndreas Gohrfunction clientismobile() { 850585bf44eSChristopher Smith /* @var Input $INPUT */ 851585bf44eSChristopher Smith global $INPUT; 8521c548ebeSAndreas Gohr 853585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8541c548ebeSAndreas Gohr 855585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8561c548ebeSAndreas Gohr 857585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8581c548ebeSAndreas Gohr 85964159a61SAndreas Gohr $uamatches = join( 86064159a61SAndreas Gohr '|', 86164159a61SAndreas Gohr [ 86264159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 86364159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 86464159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 86564159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 86664159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 86764159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 86864159a61SAndreas Gohr '\d\d\di', 'moto' 86964159a61SAndreas Gohr ] 87064159a61SAndreas Gohr ); 8711c548ebeSAndreas Gohr 872585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8731c548ebeSAndreas Gohr 8741c548ebeSAndreas Gohr return false; 8751c548ebeSAndreas Gohr} 8761c548ebeSAndreas Gohr 8771c548ebeSAndreas Gohr/** 8786efc45a2SDmitry Katsubo * check if a given link is interwiki link 8796efc45a2SDmitry Katsubo * 8806efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8816efc45a2SDmitry Katsubo * @return bool 8826efc45a2SDmitry Katsubo */ 8836efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 8846efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 8856efc45a2SDmitry Katsubo return false; 8866efc45a2SDmitry Katsubo} 8876efc45a2SDmitry Katsubo 8886efc45a2SDmitry Katsubo/** 88963211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 89063211f61SGlen Harris * 89122ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 89222ef1e32SAndreas Gohr * 89363211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 894140cfbcdSGerrit Uitslag * 8953272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 8963272d797SAndreas Gohr * @return string a comma separated list of hostnames 89763211f61SGlen Harris */ 89863211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 89922ef1e32SAndreas Gohr global $conf; 90022ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 90122ef1e32SAndreas Gohr 90263211f61SGlen Harris $hosts = array(); 90363211f61SGlen Harris $ips = explode(',', $ips); 904551a720fSMichael Klier 905551a720fSMichael Klier if(is_array($ips)) { 9063886270dSAndreas Gohr foreach($ips as $ip) { 907551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 90863211f61SGlen Harris } 909551a720fSMichael Klier return join(',', $hosts); 910551a720fSMichael Klier } else { 911551a720fSMichael Klier return gethostbyaddr(trim($ips)); 912551a720fSMichael Klier } 91363211f61SGlen Harris} 91463211f61SGlen Harris 91563211f61SGlen Harris/** 91615fae107Sandi * Checks if a given page is currently locked. 91715fae107Sandi * 918f3f0262cSandi * removes stale lockfiles 91915fae107Sandi * 92015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 921140cfbcdSGerrit Uitslag * 922140cfbcdSGerrit Uitslag * @param string $id page id 923140cfbcdSGerrit Uitslag * @return bool page is locked? 924f3f0262cSandi */ 925f3f0262cSandifunction checklock($id) { 926f3f0262cSandi global $conf; 927585bf44eSChristopher Smith /* @var Input $INPUT */ 928585bf44eSChristopher Smith global $INPUT; 929585bf44eSChristopher Smith 930c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 931f3f0262cSandi 932f3f0262cSandi //no lockfile 93379e79377SAndreas Gohr if(!file_exists($lock)) return false; 934f3f0262cSandi 935f3f0262cSandi //lockfile expired 936f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 937d8186216SBen Coburn @unlink($lock); 938f3f0262cSandi return false; 939f3f0262cSandi } 940f3f0262cSandi 941f3f0262cSandi //my own lock 9426d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 9430712fefaSAndreas Gohr if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) { 944f3f0262cSandi return false; 945f3f0262cSandi } 946f3f0262cSandi 947f3f0262cSandi return $ip; 948f3f0262cSandi} 949f3f0262cSandi 950f3f0262cSandi/** 95115fae107Sandi * Lock a page for editing 95215fae107Sandi * 95315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 954140cfbcdSGerrit Uitslag * 955140cfbcdSGerrit Uitslag * @param string $id page id to lock 956f3f0262cSandi */ 957f3f0262cSandifunction lock($id) { 958544ed901SDaniel Calviño Sánchez global $conf; 959585bf44eSChristopher Smith /* @var Input $INPUT */ 960585bf44eSChristopher Smith global $INPUT; 961544ed901SDaniel Calviño Sánchez 962544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 963544ed901SDaniel Calviño Sánchez return; 964544ed901SDaniel Calviño Sánchez } 965544ed901SDaniel Calviño Sánchez 966c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 967585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 968585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 969f3f0262cSandi } else { 97085fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 971f3f0262cSandi } 972f3f0262cSandi} 973f3f0262cSandi 974f3f0262cSandi/** 97515fae107Sandi * Unlock a page if it was locked by the user 976f3f0262cSandi * 97715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 978140cfbcdSGerrit Uitslag * 9793272d797SAndreas Gohr * @param string $id page id to unlock 98015fae107Sandi * @return bool true if a lock was removed 981f3f0262cSandi */ 982f3f0262cSandifunction unlock($id) { 983585bf44eSChristopher Smith /* @var Input $INPUT */ 984585bf44eSChristopher Smith global $INPUT; 985585bf44eSChristopher Smith 986c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 98779e79377SAndreas Gohr if(file_exists($lock)) { 9886d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 989585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 990f3f0262cSandi @unlink($lock); 991f3f0262cSandi return true; 992f3f0262cSandi } 993f3f0262cSandi } 994f3f0262cSandi return false; 995f3f0262cSandi} 996f3f0262cSandi 997f3f0262cSandi/** 998f3f0262cSandi * convert line ending to unix format 999f3f0262cSandi * 10006db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 10016db7468bSAndreas Gohr * 100215fae107Sandi * @see formText() for 2crlf conversion 100315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1004140cfbcdSGerrit Uitslag * 1005140cfbcdSGerrit Uitslag * @param string $text 1006140cfbcdSGerrit Uitslag * @return string 1007f3f0262cSandi */ 1008f3f0262cSandifunction cleanText($text) { 1009f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10106db7468bSAndreas Gohr 10116db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10126db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10136db7468bSAndreas Gohr // but might actually fix the problem in many cases 10146db7468bSAndreas Gohr if(!utf8_check($text)) $text = utf8_encode($text); 10156db7468bSAndreas Gohr 1016f3f0262cSandi return $text; 1017f3f0262cSandi} 1018f3f0262cSandi 1019f3f0262cSandi/** 1020f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1021f3f0262cSandi * It also converts line endings to Windows format which is 1022f3f0262cSandi * pseudo standard for webforms. 1023f3f0262cSandi * 102415fae107Sandi * @see cleanText() for 2unix conversion 102515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1026140cfbcdSGerrit Uitslag * 1027140cfbcdSGerrit Uitslag * @param string $text 1028140cfbcdSGerrit Uitslag * @return string 1029f3f0262cSandi */ 1030f3f0262cSandifunction formText($text) { 10315b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 1032f3f0262cSandi return htmlspecialchars($text); 1033f3f0262cSandi} 1034f3f0262cSandi 1035f3f0262cSandi/** 103615fae107Sandi * Returns the specified local text in raw format 103715fae107Sandi * 103815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1039140cfbcdSGerrit Uitslag * 1040140cfbcdSGerrit Uitslag * @param string $id page id 1041140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1042140cfbcdSGerrit Uitslag * @return string 1043f3f0262cSandi */ 10442adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10452adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1046f3f0262cSandi} 1047f3f0262cSandi 1048f3f0262cSandi/** 1049f3f0262cSandi * Returns the raw WikiText 105015fae107Sandi * 105115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1052140cfbcdSGerrit Uitslag * 1053140cfbcdSGerrit Uitslag * @param string $id page id 1054e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1055140cfbcdSGerrit Uitslag * @return string 1056f3f0262cSandi */ 1057f3f0262cSandifunction rawWiki($id, $rev = '') { 1058cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1059f3f0262cSandi} 1060f3f0262cSandi 1061f3f0262cSandi/** 10627146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10637146cee2SAndreas Gohr * 10647b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10657146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1066140cfbcdSGerrit Uitslag * 1067140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1068140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10697146cee2SAndreas Gohr */ 1070fe17917eSAdrian Langfunction pageTemplate($id) { 1071a15ce62dSEsther Brunner global $conf; 1072e29549feSAndreas Gohr 1073fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1074e29549feSAndreas Gohr 10757b84afa2SAndreas Gohr // prepare initial event data 10767b84afa2SAndreas Gohr $data = array( 10777b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10787b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10797b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10807b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10817b84afa2SAndreas Gohr ); 10827b84afa2SAndreas Gohr 10837b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 10847b84afa2SAndreas Gohr if($evt->advise_before(true)) { 10857b84afa2SAndreas Gohr // the before event might have loaded the content already 10867b84afa2SAndreas Gohr if(empty($data['tpl'])) { 10877b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10887b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1089fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 109079e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 10917b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1092e29549feSAndreas Gohr } else { 1093e29549feSAndreas Gohr // search upper namespaces for templates 1094e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1095e29549feSAndreas Gohr while(strlen($path) >= $len) { 109679e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 10977b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1098e29549feSAndreas Gohr break; 1099e29549feSAndreas Gohr } 1100e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1101e29549feSAndreas Gohr } 1102e29549feSAndreas Gohr } 11037b84afa2SAndreas Gohr } 11047b84afa2SAndreas Gohr // load the content 11053d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 11067b84afa2SAndreas Gohr } 1107a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 11087b84afa2SAndreas Gohr } 11097b84afa2SAndreas Gohr $evt->advise_after(); 11107b84afa2SAndreas Gohr unset($evt); 11117b84afa2SAndreas Gohr 1112fe17917eSAdrian Lang return $data['tpl']; 11132b1223ecSAdrian Lang} 11142b1223ecSAdrian Lang 11152b1223ecSAdrian Lang/** 11162b1223ecSAdrian Lang * Performs common page template replacements 11177b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11182b1223ecSAdrian Lang * 11192b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1120140cfbcdSGerrit Uitslag * 1121140cfbcdSGerrit Uitslag * @param array $data array with event data 1122140cfbcdSGerrit Uitslag * @return string 11232b1223ecSAdrian Lang */ 1124d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11253272d797SAndreas Gohr /** 11263272d797SAndreas Gohr * @var string $id the id of the page to be created 11273272d797SAndreas Gohr * @var string $tpl the text used as template 11283272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11293272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11303272d797SAndreas Gohr */ 1131fe17917eSAdrian Lang extract($data); 1132fe17917eSAdrian Lang 1133b856f7dfSAdrian Lang global $USERINFO; 1134bce53b1fSAdrian Lang global $conf; 1135585bf44eSChristopher Smith /* @var Input $INPUT */ 1136585bf44eSChristopher Smith global $INPUT; 1137e29549feSAndreas Gohr 1138e29549feSAndreas Gohr // replace placeholders 113926ece5a7SAndreas Gohr $file = noNS($id); 114037c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 114126ece5a7SAndreas Gohr 11423272d797SAndreas Gohr $tpl = str_replace( 11433272d797SAndreas Gohr array( 114426ece5a7SAndreas Gohr '@ID@', 114526ece5a7SAndreas Gohr '@NS@', 11468a7bcf66SShota Miyazaki '@CURNS@', 114726ece5a7SAndreas Gohr '@FILE@', 114826ece5a7SAndreas Gohr '@!FILE@', 114926ece5a7SAndreas Gohr '@!FILE!@', 115026ece5a7SAndreas Gohr '@PAGE@', 115126ece5a7SAndreas Gohr '@!PAGE@', 115226ece5a7SAndreas Gohr '@!!PAGE@', 115326ece5a7SAndreas Gohr '@!PAGE!@', 115426ece5a7SAndreas Gohr '@USER@', 115526ece5a7SAndreas Gohr '@NAME@', 115626ece5a7SAndreas Gohr '@MAIL@', 115726ece5a7SAndreas Gohr '@DATE@', 115826ece5a7SAndreas Gohr ), 115926ece5a7SAndreas Gohr array( 116026ece5a7SAndreas Gohr $id, 116126ece5a7SAndreas Gohr getNS($id), 11628a7bcf66SShota Miyazaki curNS($id), 116326ece5a7SAndreas Gohr $file, 116426ece5a7SAndreas Gohr utf8_ucfirst($file), 116526ece5a7SAndreas Gohr utf8_strtoupper($file), 116626ece5a7SAndreas Gohr $page, 116726ece5a7SAndreas Gohr utf8_ucfirst($page), 116826ece5a7SAndreas Gohr utf8_ucwords($page), 116926ece5a7SAndreas Gohr utf8_strtoupper($page), 1170585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 1171b856f7dfSAdrian Lang $USERINFO['name'], 1172b856f7dfSAdrian Lang $USERINFO['mail'], 117326ece5a7SAndreas Gohr $conf['dformat'], 11743272d797SAndreas Gohr ), $tpl 11753272d797SAndreas Gohr ); 117626ece5a7SAndreas Gohr 11777d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1178bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1179bad6fc0dSAndreas Gohr '/%./', 1180bad6fc0dSAndreas Gohr function ($m) { 1181bad6fc0dSAndreas Gohr return strftime($m[0]); 1182bad6fc0dSAndreas Gohr }, 1183bad6fc0dSAndreas Gohr $tpl 1184bad6fc0dSAndreas Gohr ); 1185d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1186a15ce62dSEsther Brunner return $tpl; 11877146cee2SAndreas Gohr} 11887146cee2SAndreas Gohr 11897146cee2SAndreas Gohr/** 119015fae107Sandi * Returns the raw Wiki Text in three slices. 119115fae107Sandi * 119215fae107Sandi * The range parameter needs to have the form "from-to" 119315cfe303Sandi * and gives the range of the section in bytes - no 119415cfe303Sandi * UTF-8 awareness is needed. 1195f3f0262cSandi * The returned order is prefix, section and suffix. 119615fae107Sandi * 119715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1198140cfbcdSGerrit Uitslag * 1199140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1200140cfbcdSGerrit Uitslag * @param string $id page id 1201140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 120242ea7f44SGerrit Uitslag * @return string[] with three slices 1203f3f0262cSandi */ 1204f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1205cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1206f3f0262cSandi 120780fcb268SAdrian Lang // Parse range 120880fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 120980fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 121080fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 121180fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 121280fcb268SAdrian Lang 121359bc3b48SGerrit Uitslag $slices = array(); 121480fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 121580fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 121615cfe303Sandi $slices[2] = substr($text, $to); 1217f3f0262cSandi return $slices; 1218f3f0262cSandi} 1219f3f0262cSandi 1220f3f0262cSandi/** 122115fae107Sandi * Joins wiki text slices 122215fae107Sandi * 122380fcb268SAdrian Lang * function to join the text slices. 1224f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1225f3f0262cSandi * lines between sections if needed (used on saving). 122615fae107Sandi * 122715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1228140cfbcdSGerrit Uitslag * 1229140cfbcdSGerrit Uitslag * @param string $pre prefix 1230140cfbcdSGerrit Uitslag * @param string $text text in the middle 1231140cfbcdSGerrit Uitslag * @param string $suf suffix 1232140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1233140cfbcdSGerrit Uitslag * @return string 1234f3f0262cSandi */ 1235f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1236f3f0262cSandi if($pretty) { 123780fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12383272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12393272d797SAndreas Gohr ) { 124080fcb268SAdrian Lang $pre .= "\n"; 124180fcb268SAdrian Lang } 124280fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12433272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12443272d797SAndreas Gohr ) { 124580fcb268SAdrian Lang $text .= "\n"; 124680fcb268SAdrian Lang } 1247f3f0262cSandi } 1248f3f0262cSandi 1249f3f0262cSandi return $pre.$text.$suf; 1250f3f0262cSandi} 1251f3f0262cSandi 1252f3f0262cSandi/** 1253b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1254b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1255b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1256b24d9195SAndreas Gohr * 1257b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1258b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1259b24d9195SAndreas Gohr * 1260b24d9195SAndreas Gohr * @param string $id the page ID 1261b24d9195SAndreas Gohr */ 1262b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 1263b24d9195SAndreas Gohr global $lang; 1264b24d9195SAndreas Gohr 12658c7319beSGerrit Uitslag $fileLastMod = wikiFN($id); 12668c7319beSGerrit Uitslag $lastMod = @filemtime($fileLastMod); // from page 1267b24d9195SAndreas Gohr $pagelog = new PageChangeLog($id, 1024); 12688c7319beSGerrit Uitslag $lastRev = $pagelog->getRevisions(-1, 1); // from changelog 12698c7319beSGerrit Uitslag $lastRev = (int) (empty($lastRev) ? 0 : $lastRev[0]); 1270b24d9195SAndreas Gohr 12718c7319beSGerrit Uitslag if(!file_exists(wikiFN($id, $lastMod)) && file_exists($fileLastMod) && $lastMod >= $lastRev) { 1272b24d9195SAndreas Gohr // add old revision to the attic if missing 1273b24d9195SAndreas Gohr saveOldRevision($id); 1274b24d9195SAndreas Gohr // add a changelog entry if this edit came from outside dokuwiki 12758c7319beSGerrit Uitslag if($lastMod > $lastRev) { 12768c7319beSGerrit Uitslag $fileLastRev = wikiFN($id, $lastRev); 12778c7319beSGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($lastRev); 12783c48b1d0SGerrit Uitslag if(empty($lastRev) || !file_exists($fileLastRev) || $revinfo['type'] == DOKU_CHANGE_TYPE_DELETE) { 12794b5aebc1SGerrit Uitslag $filesize_old = 0; 12804b5aebc1SGerrit Uitslag } else { 12818c7319beSGerrit Uitslag $filesize_old = io_getSizeFile($fileLastRev); 12824b5aebc1SGerrit Uitslag } 12838c7319beSGerrit Uitslag $filesize_new = filesize($fileLastMod); 12842966355bSGerrit Uitslag $sizechange = $filesize_new - $filesize_old; 12852966355bSGerrit Uitslag 128664159a61SAndreas Gohr addLogEntry( 128764159a61SAndreas Gohr $lastMod, 128864159a61SAndreas Gohr $id, 128964159a61SAndreas Gohr DOKU_CHANGE_TYPE_EDIT, 129064159a61SAndreas Gohr $lang['external_edit'], 129164159a61SAndreas Gohr '', 129264159a61SAndreas Gohr array('ExternalEdit' => true), 129364159a61SAndreas Gohr $sizechange 129464159a61SAndreas Gohr ); 1295b24d9195SAndreas Gohr // remove soon to be stale instructions 12968c7319beSGerrit Uitslag $cache = new cache_instructions($id, $fileLastMod); 1297b24d9195SAndreas Gohr $cache->removeCache(); 1298b24d9195SAndreas Gohr } 1299b24d9195SAndreas Gohr } 1300b24d9195SAndreas Gohr} 1301b24d9195SAndreas Gohr 1302b24d9195SAndreas Gohr/** 1303a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1304a701424fSBen Coburn * Also directs changelog and attic updates. 130515fae107Sandi * 130615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 130771726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1308140cfbcdSGerrit Uitslag * 1309140cfbcdSGerrit Uitslag * @param string $id page id 1310140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1311140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1312140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1313f3f0262cSandi */ 1314b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1315a701424fSBen Coburn /* Note to developers: 1316a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1317a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1318a701424fSBen Coburn after any changes. External edits change the wiki page 1319a701424fSBen Coburn directly without using php or dokuwiki. 1320a701424fSBen Coburn */ 1321f3f0262cSandi global $conf; 1322f3f0262cSandi global $lang; 132371726d78SBen Coburn global $REV; 1324585bf44eSChristopher Smith /* @var Input $INPUT */ 1325585bf44eSChristopher Smith global $INPUT; 1326585bf44eSChristopher Smith 1327b24d9195SAndreas Gohr // prepare data for event 1328b24d9195SAndreas Gohr $svdta = array(); 1329b24d9195SAndreas Gohr $svdta['id'] = $id; 1330b24d9195SAndreas Gohr $svdta['file'] = wikiFN($id); 1331b24d9195SAndreas Gohr $svdta['revertFrom'] = $REV; 1332b24d9195SAndreas Gohr $svdta['oldRevision'] = @filemtime($svdta['file']); 1333b24d9195SAndreas Gohr $svdta['newRevision'] = 0; 1334b24d9195SAndreas Gohr $svdta['newContent'] = $text; 1335b24d9195SAndreas Gohr $svdta['oldContent'] = rawWiki($id); 1336b24d9195SAndreas Gohr $svdta['summary'] = $summary; 1337b24d9195SAndreas Gohr $svdta['contentChanged'] = ($svdta['newContent'] != $svdta['oldContent']); 1338b24d9195SAndreas Gohr $svdta['changeInfo'] = ''; 1339b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_EDIT; 13402966355bSGerrit Uitslag $svdta['sizechange'] = null; 1341b24d9195SAndreas Gohr 1342b24d9195SAndreas Gohr // select changelog line type 1343b24d9195SAndreas Gohr if($REV) { 1344b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_REVERT; 1345b24d9195SAndreas Gohr $svdta['changeInfo'] = $REV; 1346b24d9195SAndreas Gohr } else if(!file_exists($svdta['file'])) { 1347b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_CREATE; 1348b24d9195SAndreas Gohr } else if(trim($text) == '') { 1349b24d9195SAndreas Gohr // empty or whitespace only content deletes 1350b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_DELETE; 1351b24d9195SAndreas Gohr // autoset summary on deletion 1352655ddc1dSGerrit Uitslag if(blank($svdta['summary'])) { 1353655ddc1dSGerrit Uitslag $svdta['summary'] = $lang['deleted']; 1354655ddc1dSGerrit Uitslag } 1355b24d9195SAndreas Gohr } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 1356b24d9195SAndreas Gohr //minor edits only for logged in users 1357b24d9195SAndreas Gohr $svdta['changeType'] = DOKU_CHANGE_TYPE_MINOR_EDIT; 1358f3f0262cSandi } 1359f3f0262cSandi 1360b24d9195SAndreas Gohr $event = new Doku_Event('COMMON_WIKIPAGE_SAVE', $svdta); 1361b24d9195SAndreas Gohr if(!$event->advise_before()) return; 1362f3f0262cSandi 1363b24d9195SAndreas Gohr // if the content has not been changed, no save happens (plugins may override this) 1364b24d9195SAndreas Gohr if(!$svdta['contentChanged']) return; 1365b24d9195SAndreas Gohr 1366b24d9195SAndreas Gohr detectExternalEdit($id); 1367f3f0262cSandi 13684b5aebc1SGerrit Uitslag if( 13694b5aebc1SGerrit Uitslag $svdta['changeType'] == DOKU_CHANGE_TYPE_CREATE || 13704b5aebc1SGerrit Uitslag ($svdta['changeType'] == DOKU_CHANGE_TYPE_REVERT && !file_exists($svdta['file'])) 13714b5aebc1SGerrit Uitslag ) { 1372ac3ed4afSGerrit Uitslag $filesize_old = 0; 1373ac3ed4afSGerrit Uitslag } else { 13742966355bSGerrit Uitslag $filesize_old = filesize($svdta['file']); 1375ac3ed4afSGerrit Uitslag } 1376b24d9195SAndreas Gohr if($svdta['changeType'] == DOKU_CHANGE_TYPE_DELETE) { 137730725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 1378b24d9195SAndreas Gohr $data = array(array($svdta['file'], '', false), getNS($id), noNS($id), false); 137930725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1380e45b34cdSBen Coburn // pre-save deleted revision 1381b24d9195SAndreas Gohr @touch($svdta['file']); 138246844156SBen Coburn clearstatcache(); 13832d69eb44SMichael Hamann $svdta['newRevision'] = saveOldRevision($id); 1384e1f3d9e1SEsther Brunner // remove empty file 1385b24d9195SAndreas Gohr @unlink($svdta['file']); 1386ac3ed4afSGerrit Uitslag $filesize_new = 0; 138764159a61SAndreas Gohr // don't remove old meta info as it should be saved, plugins can use 138864159a61SAndreas Gohr // IO_WIKIPAGE_WRITE for removing their metadata... 1389c5f92742SMichael Hamann // purge non-persistant meta data 13903d1f9ec3SMichael Klier p_purge_metadata($id); 139153d6ccfeSandi // remove empty namespaces 1392cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1393cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1394f3f0262cSandi } else { 1395cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 139633d979e7SMichael Große io_writeWikiPage($svdta['file'], $svdta['newContent'], $id); 139746844156SBen Coburn // pre-save the revision, to keep the attic in sync 1398b24d9195SAndreas Gohr $svdta['newRevision'] = saveOldRevision($id); 13992966355bSGerrit Uitslag $filesize_new = filesize($svdta['file']); 1400f3f0262cSandi } 14012966355bSGerrit Uitslag $svdta['sizechange'] = $filesize_new - $filesize_old; 1402f3f0262cSandi 1403b24d9195SAndreas Gohr $event->advise_after(); 140471726d78SBen Coburn 140564159a61SAndreas Gohr addLogEntry( 140664159a61SAndreas Gohr $svdta['newRevision'], 140764159a61SAndreas Gohr $svdta['id'], 140864159a61SAndreas Gohr $svdta['changeType'], 140964159a61SAndreas Gohr $svdta['summary'], 141064159a61SAndreas Gohr $svdta['changeInfo'], 141164159a61SAndreas Gohr null, 141264159a61SAndreas Gohr $svdta['sizechange'] 141364159a61SAndreas Gohr ); 1414ac3ed4afSGerrit Uitslag 141526a0801fSAndreas Gohr // send notify mails 1416b24d9195SAndreas Gohr notify($svdta['id'], 'admin', $svdta['oldRevision'], $svdta['summary'], $minor); 1417b24d9195SAndreas Gohr notify($svdta['id'], 'subscribers', $svdta['oldRevision'], $svdta['summary'], $minor); 1418f3f0262cSandi 1419ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 142098407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 14212eccbdaaSGina Haeussge 14222eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1423fe9ec250SChris Smith if(useHeading('content')) { 142407ff0babSMichael Hamann $pages = ft_backlinks($id, true); 14252eccbdaaSGina Haeussge foreach($pages as $page) { 14262eccbdaaSGina Haeussge $cache = new cache_renderer($page, wikiFN($page), 'xhtml'); 14272eccbdaaSGina Haeussge $cache->removeCache(); 14282eccbdaaSGina Haeussge } 14292eccbdaaSGina Haeussge } 1430f3f0262cSandi} 1431f3f0262cSandi 1432f3f0262cSandi/** 1433f3f0262cSandi * moves the current version to the attic and returns its 1434f3f0262cSandi * revision date 143515fae107Sandi * 143615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1437140cfbcdSGerrit Uitslag * 1438140cfbcdSGerrit Uitslag * @param string $id page id 1439140cfbcdSGerrit Uitslag * @return int|string revision timestamp 1440f3f0262cSandi */ 1441f3f0262cSandifunction saveOldRevision($id) { 1442f3f0262cSandi $oldf = wikiFN($id); 144379e79377SAndreas Gohr if(!file_exists($oldf)) return ''; 1444f3f0262cSandi $date = filemtime($oldf); 1445f3f0262cSandi $newf = wikiFN($id, $date); 1446cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1447f3f0262cSandi return $date; 1448f3f0262cSandi} 1449f3f0262cSandi 1450f3f0262cSandi/** 1451fde10de4SAdrian Lang * Sends a notify mail on page change or registration 145226a0801fSAndreas Gohr * 145326a0801fSAndreas Gohr * @param string $id The changed page 1454fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 14553272d797SAndreas Gohr * @param int|string $rev Old page revision 145626a0801fSAndreas Gohr * @param string $summary What changed 145790033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 145842ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 14593272d797SAndreas Gohr * @return bool 1460140cfbcdSGerrit Uitslag * 146115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1462f3f0262cSandi */ 146302a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1464f3f0262cSandi global $conf; 1465585bf44eSChristopher Smith /* @var Input $INPUT */ 1466585bf44eSChristopher Smith global $INPUT; 1467b158d625SSteven Danz 14686df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 146926a0801fSAndreas Gohr if($who == 'admin') { 14703272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 14712ed38036SAndreas Gohr $tpl = 'mailtext'; 147226a0801fSAndreas Gohr $to = $conf['notify']; 147326a0801fSAndreas Gohr } elseif($who == 'subscribers') { 147484c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1475585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 14760bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 14773272d797SAndreas Gohr trigger_event( 14783272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1479835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 14803272d797SAndreas Gohr ); 14812ed38036SAndreas Gohr $to = $data['addresslist']; 14822ed38036SAndreas Gohr if(empty($to)) return false; 14832ed38036SAndreas Gohr $tpl = 'subscr_single'; 148426a0801fSAndreas Gohr } else { 14853272d797SAndreas Gohr return false; //just to be safe 148626a0801fSAndreas Gohr } 148726a0801fSAndreas Gohr 14886df843eeSAndreas Gohr // prepare content 14892ed38036SAndreas Gohr $subscription = new Subscription(); 14902ed38036SAndreas Gohr return $subscription->send_diff($to, $tpl, $id, $rev, $summary); 1491f3f0262cSandi} 14922ed38036SAndreas Gohr 149315fae107Sandi/** 149471f7bde7SAndreas Gohr * extracts the query from a search engine referrer 149515fae107Sandi * 149615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 149771f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1498140cfbcdSGerrit Uitslag * 1499140cfbcdSGerrit Uitslag * @return array|string 1500f3f0262cSandi */ 1501f3f0262cSandifunction getGoogleQuery() { 1502585bf44eSChristopher Smith /* @var Input $INPUT */ 1503585bf44eSChristopher Smith global $INPUT; 1504585bf44eSChristopher Smith 1505585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1506c66972f2SAdrian Lang return ''; 1507c66972f2SAdrian Lang } 1508585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1509f3f0262cSandi 1510079b3ac1SAndreas Gohr // only handle common SEs 1511079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1512e4d8a516SKazutaka Miyasaka 1513079b3ac1SAndreas Gohr $query = array(); 1514e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1515e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1516e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1517f3f0262cSandi parse_str($url['query'], $query); 1518e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1519e4d8a516SKazutaka Miyasaka 1520c66972f2SAdrian Lang $q = ''; 1521079b3ac1SAndreas Gohr if(isset($query['q'])){ 1522079b3ac1SAndreas Gohr $q = $query['q']; 1523079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1524079b3ac1SAndreas Gohr $q = $query['p']; 1525079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1526079b3ac1SAndreas Gohr $q = $query['query']; 1527079b3ac1SAndreas Gohr } 1528079b3ac1SAndreas Gohr $q = trim($q); 1529f3f0262cSandi 1530079b3ac1SAndreas Gohr if(!$q) return ''; 15316531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1532f93b3b50SAndreas Gohr return $q; 1533f3f0262cSandi} 1534f3f0262cSandi 1535f3f0262cSandi/** 1536f3f0262cSandi * Return the human readable size of a file 1537f3f0262cSandi * 1538f3f0262cSandi * @param int $size A file size 1539f3f0262cSandi * @param int $dec A number of decimal places 154074160ca1SGerrit Uitslag * @return string human readable size 1541140cfbcdSGerrit Uitslag * 1542f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1543f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1544f3f0262cSandi * @version 1.0.0 1545f3f0262cSandi */ 1546f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1547f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1548f3f0262cSandi $count = count($sizes); 1549f3f0262cSandi $i = 0; 1550f3f0262cSandi 1551f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1552f3f0262cSandi $size /= 1024; 1553f3f0262cSandi $i++; 1554f3f0262cSandi } 1555f3f0262cSandi 1556ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1557f3f0262cSandi} 1558f3f0262cSandi 155915fae107Sandi/** 1560c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1561c57e365eSAndreas Gohr * 1562c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1563140cfbcdSGerrit Uitslag * 1564140cfbcdSGerrit Uitslag * @param int $dt timestamp 1565140cfbcdSGerrit Uitslag * @return string 1566c57e365eSAndreas Gohr */ 1567c57e365eSAndreas Gohrfunction datetime_h($dt) { 1568c57e365eSAndreas Gohr global $lang; 1569c57e365eSAndreas Gohr 1570c57e365eSAndreas Gohr $ago = time() - $dt; 1571c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1572c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1573c57e365eSAndreas Gohr } 1574c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1575c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1576c57e365eSAndreas Gohr } 1577c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1578c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1579c57e365eSAndreas Gohr } 1580c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1581c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1582c57e365eSAndreas Gohr } 1583c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1584c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1585c57e365eSAndreas Gohr } 1586c57e365eSAndreas Gohr if($ago > 60 * 2) { 1587c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1588c57e365eSAndreas Gohr } 1589c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1590c57e365eSAndreas Gohr} 1591c57e365eSAndreas Gohr 1592c57e365eSAndreas Gohr/** 1593f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1594f2263577SAndreas Gohr * 1595f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1596f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1597f2263577SAndreas Gohr * 1598f2263577SAndreas Gohr * @see datetime_h 1599f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1600140cfbcdSGerrit Uitslag * 1601140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1602140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1603140cfbcdSGerrit Uitslag * @return string 1604f2263577SAndreas Gohr */ 1605f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1606f2263577SAndreas Gohr global $conf; 1607f2263577SAndreas Gohr 1608f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1609f2263577SAndreas Gohr $dt = (int) $dt; 1610f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1611f2263577SAndreas Gohr 1612f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1613f2263577SAndreas Gohr return strftime($format, $dt); 1614f2263577SAndreas Gohr} 1615f2263577SAndreas Gohr 1616f2263577SAndreas Gohr/** 1617c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1618c4f79b71SMichael Hamann * 1619c4f79b71SMichael Hamann * @author <ungu at terong dot com> 162059752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1621140cfbcdSGerrit Uitslag * 16227e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 16233272d797SAndreas Gohr * @return string 1624c4f79b71SMichael Hamann */ 1625c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1626c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1627c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1628c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1629c4f79b71SMichael Hamann $date_mod .= $time_zone; 1630c4f79b71SMichael Hamann return $date_mod; 1631c4f79b71SMichael Hamann} 1632c4f79b71SMichael Hamann 1633c4f79b71SMichael Hamann/** 163400a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 163500a7b5adSEsther Brunner * 163600a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 163700a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1638140cfbcdSGerrit Uitslag * 1639140cfbcdSGerrit Uitslag * @param string $email email address 1640140cfbcdSGerrit Uitslag * @return string 164100a7b5adSEsther Brunner */ 164200a7b5adSEsther Brunnerfunction obfuscate($email) { 164300a7b5adSEsther Brunner global $conf; 164400a7b5adSEsther Brunner 164500a7b5adSEsther Brunner switch($conf['mailguard']) { 164600a7b5adSEsther Brunner case 'visible' : 164700a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 164800a7b5adSEsther Brunner return strtr($email, $obfuscate); 164900a7b5adSEsther Brunner 165000a7b5adSEsther Brunner case 'hex' : 165100a7b5adSEsther Brunner $encode = ''; 165249eb6e38SAndreas Gohr $len = strlen($email); 165349eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 165449eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 165549eb6e38SAndreas Gohr } 165600a7b5adSEsther Brunner return $encode; 165700a7b5adSEsther Brunner 165800a7b5adSEsther Brunner case 'none' : 165900a7b5adSEsther Brunner default : 166000a7b5adSEsther Brunner return $email; 166100a7b5adSEsther Brunner } 166200a7b5adSEsther Brunner} 166300a7b5adSEsther Brunner 166400a7b5adSEsther Brunner/** 166589541d4bSAndreas Gohr * Removes quoting backslashes 166689541d4bSAndreas Gohr * 166789541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1668140cfbcdSGerrit Uitslag * 1669140cfbcdSGerrit Uitslag * @param string $string 1670140cfbcdSGerrit Uitslag * @param string $char backslashed character 1671140cfbcdSGerrit Uitslag * @return string 167289541d4bSAndreas Gohr */ 167389541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 167489541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 167589541d4bSAndreas Gohr} 167689541d4bSAndreas Gohr 167773038c47SAndreas Gohr/** 167873038c47SAndreas Gohr * Convert php.ini shorthands to byte 167973038c47SAndreas Gohr * 168073038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 168159752844SAnders Sandblad * @link http://php.net/manual/en/ini.core.php#79564 1682140cfbcdSGerrit Uitslag * 1683140cfbcdSGerrit Uitslag * @param string $v shorthands 1684140cfbcdSGerrit Uitslag * @return int|string 168573038c47SAndreas Gohr */ 168673038c47SAndreas Gohrfunction php_to_byte($v) { 168773038c47SAndreas Gohr $l = substr($v, -1); 168873038c47SAndreas Gohr $ret = substr($v, 0, -1); 168973038c47SAndreas Gohr switch(strtoupper($l)) { 169074160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 169173038c47SAndreas Gohr case 'P': 169273038c47SAndreas Gohr $ret *= 1024; 169374160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 169473038c47SAndreas Gohr case 'T': 169573038c47SAndreas Gohr $ret *= 1024; 169674160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 169773038c47SAndreas Gohr case 'G': 169873038c47SAndreas Gohr $ret *= 1024; 169974160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 170073038c47SAndreas Gohr case 'M': 170173038c47SAndreas Gohr $ret *= 1024; 1702f168548cSGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 170373038c47SAndreas Gohr case 'K': 170473038c47SAndreas Gohr $ret *= 1024; 170573038c47SAndreas Gohr break; 170649cbd23eSOtto Vainio default; 170749cbd23eSOtto Vainio $ret *= 10; 170849cbd23eSOtto Vainio break; 170973038c47SAndreas Gohr } 171073038c47SAndreas Gohr return $ret; 171173038c47SAndreas Gohr} 171273038c47SAndreas Gohr 1713546d3a99SAndreas Gohr/** 1714546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1715140cfbcdSGerrit Uitslag * 1716140cfbcdSGerrit Uitslag * @param string $string 1717140cfbcdSGerrit Uitslag * @return string 1718546d3a99SAndreas Gohr */ 1719546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1720546d3a99SAndreas Gohr return preg_quote($string, '/'); 1721546d3a99SAndreas Gohr} 172273038c47SAndreas Gohr 1723bd2f6c2fSAndreas Gohr/** 1724bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1725bd2f6c2fSAndreas Gohr * 1726c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1727bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1728bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1729bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1730bd2f6c2fSAndreas Gohr * 1731bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1732bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1733bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1734bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1735bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 17363272d797SAndreas Gohr * @return string 1737bd2f6c2fSAndreas Gohr */ 1738a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 1739bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1740bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1741bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1742bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1743bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1744bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1745bd2f6c2fSAndreas Gohr} 1746bd2f6c2fSAndreas Gohr 1747dc58b6f4SAndy Webber/** 1748dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1749dc58b6f4SAndy Webber * in page footer and recent changes pages 1750dc58b6f4SAndy Webber * 1751b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 175215f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1753c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 175415f3bc49SGerrit Uitslag * 1755dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1756dc58b6f4SAndy Webber */ 175715f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1758cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1759dc58b6f4SAndy Webber} 1760dc58b6f4SAndy Webber 176160a396c8SGerrit Uitslag/** 176260a396c8SGerrit Uitslag * Returns users realname w/o link 176360a396c8SGerrit Uitslag * 1764f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 176515f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1766c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 176760a396c8SGerrit Uitslag * 176860a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 176960a396c8SGerrit Uitslag */ 1770cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 177160a396c8SGerrit Uitslag global $conf, $INFO; 177260a396c8SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 177360a396c8SGerrit Uitslag global $auth; 177430f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 177530f6ec4bSGerrit Uitslag global $INPUT; 177660a396c8SGerrit Uitslag 177760a396c8SGerrit Uitslag // prepare initial event data 177860a396c8SGerrit Uitslag $data = array( 177960a396c8SGerrit Uitslag 'username' => $username, // the unique user name 178060a396c8SGerrit Uitslag 'name' => '', 178160a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 178260a396c8SGerrit Uitslag 'target' => '', 178360a396c8SGerrit Uitslag 'pre' => '', 178460a396c8SGerrit Uitslag 'suf' => '', 178560a396c8SGerrit Uitslag 'style' => '', 178660a396c8SGerrit Uitslag 'more' => '', 178760a396c8SGerrit Uitslag 'url' => '', 178860a396c8SGerrit Uitslag 'title' => '', 178960a396c8SGerrit Uitslag 'class' => '' 179060a396c8SGerrit Uitslag ), 17914d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 179215f3bc49SGerrit Uitslag 'textonly' => $textonly 179360a396c8SGerrit Uitslag ); 179462c8004eSGerrit Uitslag if($username === null) { 179530f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 179615f3bc49SGerrit Uitslag if($textonly){ 179715f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 179815f3bc49SGerrit Uitslag }else { 179964159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 180064159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 180160a396c8SGerrit Uitslag } 180215f3bc49SGerrit Uitslag } 180360a396c8SGerrit Uitslag 180460a396c8SGerrit Uitslag $evt = new Doku_Event('COMMON_USER_LINK', $data); 180560a396c8SGerrit Uitslag if($evt->advise_before(true)) { 180660a396c8SGerrit Uitslag if(empty($data['name'])) { 180760a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 180865833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1809dc58b6f4SAndy Webber switch($conf['showuseras']) { 1810dc58b6f4SAndy Webber case 'username': 18117f081821SGerrit Uitslag case 'username_link': 181215f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 181360a396c8SGerrit Uitslag break; 1814dc58b6f4SAndy Webber case 'email': 1815dc58b6f4SAndy Webber case 'email_link': 181660a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 181760a396c8SGerrit Uitslag break; 1818dc58b6f4SAndy Webber } 181965833968SGerrit Uitslag } else { 182065833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 182160a396c8SGerrit Uitslag } 182260a396c8SGerrit Uitslag } 18237f081821SGerrit Uitslag 18247f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 18257f081821SGerrit Uitslag static $xhtml_renderer = null; 18267f081821SGerrit Uitslag 182715f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 18287f081821SGerrit Uitslag 18297f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 183060a396c8SGerrit Uitslag if(!isset($info)) { 183160a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 183260a396c8SGerrit Uitslag } 183360a396c8SGerrit Uitslag if(isset($info) && $info) { 18347f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 183560a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1836dc58b6f4SAndy Webber } else { 18377f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 18387f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 18397f081821SGerrit Uitslag } 18407f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 18417f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 18427f081821SGerrit Uitslag } 18437f081821SGerrit Uitslag $shortcut = 'user'; 1844533772e1SGerrit Uitslag $exists = null; 18456496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 18462a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 18476496c33fSGerrit Uitslag if($exists !== null) { 18486496c33fSGerrit Uitslag if($exists) { 18496496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 18506496c33fSGerrit Uitslag } else { 18516496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 18526496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 18536496c33fSGerrit Uitslag } 18546496c33fSGerrit Uitslag } 1855dc58b6f4SAndy Webber } 1856dc58b6f4SAndy Webber } else { 185715f3bc49SGerrit Uitslag $data['textonly'] = true; 1858dc58b6f4SAndy Webber } 185960a396c8SGerrit Uitslag 186060a396c8SGerrit Uitslag } else { 186115f3bc49SGerrit Uitslag $data['textonly'] = true; 186260a396c8SGerrit Uitslag } 186360a396c8SGerrit Uitslag } 186460a396c8SGerrit Uitslag 186515f3bc49SGerrit Uitslag if($data['textonly']) { 18664d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 186760a396c8SGerrit Uitslag } else { 186860a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 186960a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 187060a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 187160a396c8SGerrit Uitslag } 18724d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 187360a396c8SGerrit Uitslag } 187460a396c8SGerrit Uitslag } 187560a396c8SGerrit Uitslag $evt->advise_after(); 187660a396c8SGerrit Uitslag unset($evt); 187760a396c8SGerrit Uitslag 18784d5fc927SGerrit Uitslag return $data['userlink']; 1879066fee30SAndreas Gohr} 1880066fee30SAndreas Gohr 1881066fee30SAndreas Gohr/** 1882066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1883066fee30SAndreas Gohr * When no image exists, returns an empty string 1884066fee30SAndreas Gohr * 1885066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1886140cfbcdSGerrit Uitslag * 1887066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 18883272d797SAndreas Gohr * @return string 1889066fee30SAndreas Gohr */ 1890066fee30SAndreas Gohrfunction license_img($type) { 1891066fee30SAndreas Gohr global $license; 1892066fee30SAndreas Gohr global $conf; 1893066fee30SAndreas Gohr if(!$conf['license']) return ''; 1894066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1895066fee30SAndreas Gohr $try = array(); 1896066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1897066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1898066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1899066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1900066fee30SAndreas Gohr } 1901066fee30SAndreas Gohr foreach($try as $src) { 190279e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1903066fee30SAndreas Gohr } 1904066fee30SAndreas Gohr return ''; 1905dc58b6f4SAndy Webber} 1906dc58b6f4SAndy Webber 190713c08e2fSMichael Klier/** 190813c08e2fSMichael Klier * Checks if the given amount of memory is available 190913c08e2fSMichael Klier * 191013c08e2fSMichael Klier * If the memory_get_usage() function is not available the 191113c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 191213c08e2fSMichael Klier * 191313c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 191413c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 19153272d797SAndreas Gohr * 19163272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1917140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 19183272d797SAndreas Gohr * @return bool 191913c08e2fSMichael Klier */ 192013c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 192113c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 192213c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 192313c08e2fSMichael Klier 192413c08e2fSMichael Klier // parse limit to bytes 192513c08e2fSMichael Klier $limit = php_to_byte($limit); 192613c08e2fSMichael Klier 192713c08e2fSMichael Klier // get used memory if possible 192813c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 192913c08e2fSMichael Klier $used = memory_get_usage(); 193049eb6e38SAndreas Gohr } else { 193149eb6e38SAndreas Gohr $used = $bytes; 193213c08e2fSMichael Klier } 193313c08e2fSMichael Klier 193413c08e2fSMichael Klier if($used + $mem > $limit) { 193513c08e2fSMichael Klier return false; 193613c08e2fSMichael Klier } 193713c08e2fSMichael Klier 193813c08e2fSMichael Klier return true; 193913c08e2fSMichael Klier} 194013c08e2fSMichael Klier 1941af2408d5SAndreas Gohr/** 1942af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1943af2408d5SAndreas Gohr * 1944af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1945af2408d5SAndreas Gohr * 1946af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1947af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1948140cfbcdSGerrit Uitslag * 1949140cfbcdSGerrit Uitslag * @param string $url url being directed to 1950af2408d5SAndreas Gohr */ 1951af2408d5SAndreas Gohrfunction send_redirect($url) { 195298ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 195398ca30d2SAndreas Gohr 1954585bf44eSChristopher Smith /* @var Input $INPUT */ 1955585bf44eSChristopher Smith global $INPUT; 1956585bf44eSChristopher Smith 19570181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 19580181f021SAndreas Gohr global $MSG; 19590181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 19600181f021SAndreas Gohr //reopen session, store data and close session again 19610181f021SAndreas Gohr @session_start(); 19620181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 19630181f021SAndreas Gohr } 19640181f021SAndreas Gohr 1965d4869846SAndreas Gohr // always close the session 1966d4869846SAndreas Gohr session_write_close(); 1967d4869846SAndreas Gohr 1968af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1969585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1970585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1971585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 19723272d797SAndreas Gohr $matches[1] < 6 19733272d797SAndreas Gohr ) { 1974af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1975af2408d5SAndreas Gohr } else { 1976af2408d5SAndreas Gohr header('Location: '.$url); 1977af2408d5SAndreas Gohr } 197881781cb6SAndreas Gohr 1979572dc222SLarsDW223 // no exits during unit tests 198027c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 198127c0c399SAndreas Gohr // pass info about the redirect back to the test suite 198227c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 198327c0c399SAndreas Gohr if($testRequest !== null) { 198427c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 198527c0c399SAndreas Gohr } 1986572dc222SLarsDW223 return; 1987572dc222SLarsDW223 } 198827c0c399SAndreas Gohr 1989af2408d5SAndreas Gohr exit; 1990af2408d5SAndreas Gohr} 1991af2408d5SAndreas Gohr 19925b75cd1fSAdrian Lang/** 19935b75cd1fSAdrian Lang * Validate a value using a set of valid values 19945b75cd1fSAdrian Lang * 19955b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 19965b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 19975b75cd1fSAdrian Lang * default is specified, throws an exception. 19985b75cd1fSAdrian Lang * 19995b75cd1fSAdrian Lang * @param string $param The name of the parameter 20005b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 20015b75cd1fSAdrian Lang * be marked by the key “default”. 20025b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 20035b75cd1fSAdrian Lang * or $_GET) 20045b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 20055b75cd1fSAdrian Lang * 20063272d797SAndreas Gohr * @throws Exception 20073272d797SAndreas Gohr * @return mixed 20085b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 20095b75cd1fSAdrian Lang */ 20105b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 20115b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 20125b75cd1fSAdrian Lang return $array[$param]; 20135b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 20145b75cd1fSAdrian Lang return $valid_values['default']; 20155b75cd1fSAdrian Lang } else { 20165b75cd1fSAdrian Lang throw new Exception($exc); 20175b75cd1fSAdrian Lang } 20185b75cd1fSAdrian Lang} 20195b75cd1fSAdrian Lang 202063703ba5SAndreas Gohr/** 202163703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 2022646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 2023140cfbcdSGerrit Uitslag * 2024140cfbcdSGerrit Uitslag * @param string $pref preference key 2025b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 2026140cfbcdSGerrit Uitslag * @return string preference value 202763703ba5SAndreas Gohr */ 2028554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 2029646a531aSChristopher Smith $enc_pref = urlencode($pref); 203006c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 2031554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 203263703ba5SAndreas Gohr $cnt = count($parts); 203363703ba5SAndreas Gohr for($i = 0; $i < $cnt; $i += 2) { 2034646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 2035646a531aSChristopher Smith return urldecode($parts[$i + 1]); 2036554a8c9fSAdrian Lang } 2037554a8c9fSAdrian Lang } 2038554a8c9fSAdrian Lang } 2039554a8c9fSAdrian Lang return $default; 2040554a8c9fSAdrian Lang} 2041554a8c9fSAdrian Lang 20423c94d07bSAnika Henke/** 20433c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 204436ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 20453a970889SAnika Henke * Remove it by setting $val to false 2046140cfbcdSGerrit Uitslag * 2047140cfbcdSGerrit Uitslag * @param string $pref preference key 2048140cfbcdSGerrit Uitslag * @param string $val preference value 20493c94d07bSAnika Henke */ 20503c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 20513c94d07bSAnika Henke global $conf; 20523c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 20533c94d07bSAnika Henke $cookieVal = ''; 20543c94d07bSAnika Henke 20553c94d07bSAnika Henke if($orig && ($orig != $val)) { 20563c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 20573c94d07bSAnika Henke $cnt = count($parts); 205836ec377eSChristopher Smith // urlencode $pref for the comparison 205936ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 20603c94d07bSAnika Henke for($i = 0; $i < $cnt; $i += 2) { 206136ec377eSChristopher Smith if($parts[$i] == $enc_pref) { 20623a970889SAnika Henke if ($val !== false) { 206336ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 20643a970889SAnika Henke } else { 20653a970889SAnika Henke unset($parts[$i]); 20663a970889SAnika Henke unset($parts[$i + 1]); 20673a970889SAnika Henke } 206850f261f7SMichael Hamann break; 20693c94d07bSAnika Henke } 20703c94d07bSAnika Henke } 20713c94d07bSAnika Henke $cookieVal = implode('#', $parts); 20723a970889SAnika Henke } else if (!$orig && $val !== false) { 207364159a61SAndreas Gohr $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : ''). 207464159a61SAndreas Gohr rawurlencode($pref).'#'.rawurlencode($val); 20753c94d07bSAnika Henke } 20763c94d07bSAnika Henke 20773c94d07bSAnika Henke if (!empty($cookieVal)) { 207875e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 207975e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 20803c94d07bSAnika Henke } 20813c94d07bSAnika Henke} 20823c94d07bSAnika Henke 2083f8fb2d18SAndreas Gohr/** 2084f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 2085f8fb2d18SAndreas Gohr * 208642ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 2087f8fb2d18SAndreas Gohr */ 2088f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 2089f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 2090f8fb2d18SAndreas Gohr} 2091f8fb2d18SAndreas Gohr 20923c27983bSAndreas Gohr/** 209371de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 20943c27983bSAndreas Gohr * 20953c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 20963c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 20973c27983bSAndreas Gohr * files are embedded. 20983c27983bSAndreas Gohr * 209971de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 210071de5572SAndreas Gohr * 21013c27983bSAndreas Gohr * @param string $file full path to the SVG file 21023c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 210371de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 21043c27983bSAndreas Gohr */ 21054cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 21063c27983bSAndreas Gohr $file = trim($file); 21073c27983bSAndreas Gohr if($file === '') return false; 21083c27983bSAndreas Gohr if(!file_exists($file)) return false; 21093c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 21103c27983bSAndreas Gohr if(!is_readable($file)) return false; 21113c27983bSAndreas Gohr $content = file_get_contents($file); 21120849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 21130849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 21140849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 21150849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 21163c27983bSAndreas Gohr $content = trim($content); 21173c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 211871de5572SAndreas Gohr return $content; 21193c27983bSAndreas Gohr} 21203c27983bSAndreas Gohr 2121e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 2122