1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Common DokuWiki functions 415fae107Sandi * 515fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 715fae107Sandi */ 815fae107Sandi 90db5771eSMichael Großeuse dokuwiki\Cache\CacheInstructions; 100db5771eSMichael Großeuse dokuwiki\Cache\CacheRenderer; 110c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 12*66f4cdd4SSatoshi Saharause dokuwiki\Logger; 13704a815fSMichael Großeuse dokuwiki\Subscriptions\PageSubscriptionSender; 1475d66495SMichael Großeuse dokuwiki\Subscriptions\SubscriberManager; 15e1d9dcc8SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 16e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 170c3a5702SAndreas Gohr 18f3f0262cSandi/** 19d5197206Schris * Wrapper around htmlspecialchars() 20d5197206Schris * 21d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 22d5197206Schris * @see htmlspecialchars() 23140cfbcdSGerrit Uitslag * 24140cfbcdSGerrit Uitslag * @param string $string the string being converted 25140cfbcdSGerrit Uitslag * @return string converted string 26d5197206Schris */ 27d5197206Schrisfunction hsc($string) { 28d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 29d5197206Schris} 30d5197206Schris 31d5197206Schris/** 325b571377SAndreas Gohr * Checks if the given input is blank 335b571377SAndreas Gohr * 345b571377SAndreas Gohr * This is similar to empty() but will return false for "0". 355b571377SAndreas Gohr * 3667234204SAndreas Gohr * Please note: when you pass uninitialized variables, they will implicitly be created 3767234204SAndreas Gohr * with a NULL value without warning. 3867234204SAndreas Gohr * 3967234204SAndreas Gohr * To avoid this it's recommended to guard the call with isset like this: 4067234204SAndreas Gohr * 4167234204SAndreas Gohr * (isset($foo) && !blank($foo)) 4267234204SAndreas Gohr * (!isset($foo) || blank($foo)) 4367234204SAndreas Gohr * 445b571377SAndreas Gohr * @param $in 455b571377SAndreas Gohr * @param bool $trim Consider a string of whitespace to be blank 465b571377SAndreas Gohr * @return bool 475b571377SAndreas Gohr */ 485b571377SAndreas Gohrfunction blank(&$in, $trim = false) { 495b571377SAndreas Gohr if(is_null($in)) return true; 505b571377SAndreas Gohr if(is_array($in)) return empty($in); 515b571377SAndreas Gohr if($in === "\0") return true; 525b571377SAndreas Gohr if($trim && trim($in) === '') return true; 535b571377SAndreas Gohr if(strlen($in) > 0) return false; 545b571377SAndreas Gohr return empty($in); 555b571377SAndreas Gohr} 565b571377SAndreas Gohr 575b571377SAndreas Gohr/** 58d5197206Schris * print a newline terminated string 59d5197206Schris * 60d5197206Schris * You can give an indention as optional parameter 61d5197206Schris * 62d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 63140cfbcdSGerrit Uitslag * 64140cfbcdSGerrit Uitslag * @param string $string line of text 65140cfbcdSGerrit Uitslag * @param int $indent number of spaces indention 66d5197206Schris */ 6725ec097bSChris Smithfunction ptln($string, $indent = 0) { 6825ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 6902b0b681SAndreas Gohr} 7002b0b681SAndreas Gohr 7102b0b681SAndreas Gohr/** 7202b0b681SAndreas Gohr * strips control characters (<32) from the given string 7302b0b681SAndreas Gohr * 7402b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 75140cfbcdSGerrit Uitslag * 7642ea7f44SGerrit Uitslag * @param string $string being stripped 77140cfbcdSGerrit Uitslag * @return string 7802b0b681SAndreas Gohr */ 7902b0b681SAndreas Gohrfunction stripctl($string) { 8002b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 81d5197206Schris} 82d5197206Schris 83d5197206Schris/** 84634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 85634d7150SAndreas Gohr * 86634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 87634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 88634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 8942ea7f44SGerrit Uitslag * 90634d7150SAndreas Gohr * @return string 91634d7150SAndreas Gohr */ 92634d7150SAndreas Gohrfunction getSecurityToken() { 93585bf44eSChristopher Smith /** @var Input $INPUT */ 94585bf44eSChristopher Smith global $INPUT; 953680e2cdSAndreas Gohr 963680e2cdSAndreas Gohr $user = $INPUT->server->str('REMOTE_USER'); 973680e2cdSAndreas Gohr $session = session_id(); 983680e2cdSAndreas Gohr 993680e2cdSAndreas Gohr // CSRF checks are only for logged in users - do not generate for anonymous 1003680e2cdSAndreas Gohr if(trim($user) == '' || trim($session) == '') return ''; 101c3cc6e05SAndreas Gohr return \dokuwiki\PassHash::hmac('md5', $session.$user, auth_cookiesalt()); 102634d7150SAndreas Gohr} 103634d7150SAndreas Gohr 104634d7150SAndreas Gohr/** 105634d7150SAndreas Gohr * Check the secret CSRF token 106140cfbcdSGerrit Uitslag * 107140cfbcdSGerrit Uitslag * @param null|string $token security token or null to read it from request variable 108140cfbcdSGerrit Uitslag * @return bool success if the token matched 109634d7150SAndreas Gohr */ 110634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 111585bf44eSChristopher Smith /** @var Input $INPUT */ 1127d01a0eaSTom N Harris global $INPUT; 113585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 114df97eaacSAndreas Gohr 1157d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 116634d7150SAndreas Gohr if(getSecurityToken() != $token) { 117634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 118634d7150SAndreas Gohr return false; 119634d7150SAndreas Gohr } 120634d7150SAndreas Gohr return true; 121634d7150SAndreas Gohr} 122634d7150SAndreas Gohr 123634d7150SAndreas Gohr/** 124634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 125634d7150SAndreas Gohr * 126634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 127140cfbcdSGerrit Uitslag * 128140cfbcdSGerrit Uitslag * @param bool $print if true print the field, otherwise html of the field is returned 12942ea7f44SGerrit Uitslag * @return string html of hidden form field 130634d7150SAndreas Gohr */ 131634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 1322404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 1333272d797SAndreas Gohr if($print) echo $ret; 134634d7150SAndreas Gohr return $ret; 135634d7150SAndreas Gohr} 136634d7150SAndreas Gohr 137634d7150SAndreas Gohr/** 1381015a57dSChristopher Smith * Determine basic information for a request of $id 13915fae107Sandi * 14015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1417e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 142140cfbcdSGerrit Uitslag * 143140cfbcdSGerrit Uitslag * @param string $id pageid 144140cfbcdSGerrit Uitslag * @param bool $htmlClient add info about whether is mobile browser 145140cfbcdSGerrit Uitslag * @return array with info for a request of $id 146140cfbcdSGerrit Uitslag * 147f3f0262cSandi */ 1481015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 149f3f0262cSandi global $USERINFO; 150585bf44eSChristopher Smith /* @var Input $INPUT */ 151585bf44eSChristopher Smith global $INPUT; 1526afe8dcaSchris 153c66972f2SAdrian Lang // set info about manager/admin status. 15459bc3b48SGerrit Uitslag $info = array(); 155c66972f2SAdrian Lang $info['isadmin'] = false; 156c66972f2SAdrian Lang $info['ismanager'] = false; 157585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 158f3f0262cSandi $info['userinfo'] = $USERINFO; 1591015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 160585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 16117ee7f66SAndreas Gohr 162f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 163f8cc712eSAndreas Gohr $info['isadmin'] = true; 164f8cc712eSAndreas Gohr $info['ismanager'] = true; 165f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 166f8cc712eSAndreas Gohr $info['ismanager'] = true; 167f8cc712eSAndreas Gohr } 168f8cc712eSAndreas Gohr 16917ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 17017ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 171585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 17217ee7f66SAndreas Gohr } 173ee4c4a1bSAndreas Gohr 174f3f0262cSandi } else { 1751015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 176ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 177f3f0262cSandi } 178f3f0262cSandi 1791015a57dSChristopher Smith $info['namespace'] = getNS($id); 1801015a57dSChristopher Smith 1811015a57dSChristopher Smith // mobile detection 1821015a57dSChristopher Smith if ($htmlClient) { 1831015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1841015a57dSChristopher Smith } 1851015a57dSChristopher Smith 1861015a57dSChristopher Smith return $info; 1871015a57dSChristopher Smith } 1881015a57dSChristopher Smith 1891015a57dSChristopher Smith/** 1901015a57dSChristopher Smith * Return info about the current document as associative 1911015a57dSChristopher Smith * array. 1921015a57dSChristopher Smith * 1931015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 194140cfbcdSGerrit Uitslag * 195140cfbcdSGerrit Uitslag * @return array with info about current document 1961015a57dSChristopher Smith */ 1971015a57dSChristopher Smithfunction pageinfo() { 1981015a57dSChristopher Smith global $ID; 1991015a57dSChristopher Smith global $REV; 2001015a57dSChristopher Smith global $RANGE; 2011015a57dSChristopher Smith global $lang; 202585bf44eSChristopher Smith /* @var Input $INPUT */ 203585bf44eSChristopher Smith global $INPUT; 2041015a57dSChristopher Smith 2051015a57dSChristopher Smith $info = basicinfo($ID); 2061015a57dSChristopher Smith 2071015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 2081015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 2091015a57dSChristopher Smith $info['id'] = $ID; 2101015a57dSChristopher Smith $info['rev'] = $REV; 2111015a57dSChristopher Smith 21275d66495SMichael Große $subManager = new SubscriberManager(); 21375d66495SMichael Große $info['subscribed'] = $subManager->userSubscription(); 2147e87a794SChristopher Smith 215f3f0262cSandi $info['locked'] = checklock($ID); 216317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID); 21779e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 21801c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 2195ec96136SSatoshi Sahara 2202ca9d91cSBen Coburn if ($REV) { 2212ca9d91cSBen Coburn //check if current revision was meant 22201c9a118SAndreas Gohr if ($info['exists'] && ($info['currentrev'] == $REV)) { 2232ca9d91cSBen Coburn $REV = ''; 2247b3a6803SAndreas Gohr } elseif ($RANGE) { 2257b3a6803SAndreas Gohr //section editing does not work with old revisions! 2267b3a6803SAndreas Gohr $REV = ''; 2277b3a6803SAndreas Gohr $RANGE = ''; 2287b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 2292ca9d91cSBen Coburn } else { 2302ca9d91cSBen Coburn //really use old revision 231317a04c4SSatoshi Sahara $info['filepath'] = wikiFN($ID, $REV); 23279e79377SAndreas Gohr $info['exists'] = file_exists($info['filepath']); 233f3f0262cSandi } 234f3f0262cSandi } 235c112d578Sandi $info['rev'] = $REV; 236f3f0262cSandi if ($info['exists']) { 237252acce3SSatoshi Sahara $info['writable'] = (is_writable($info['filepath']) && $info['perm'] >= AUTH_EDIT); 238f3f0262cSandi } else { 239f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 240f3f0262cSandi } 24150e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 242f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 243f3f0262cSandi 24471726d78SBen Coburn //load page meta data 24571726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 24671726d78SBen Coburn 247652610a2Sandi //who's the editor 248047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 249652610a2Sandi if ($REV) { 250f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 251652610a2Sandi } else { 2520e80bb5eSChristopher Smith if (!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 253aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 254aa27cf05SAndreas Gohr } else { 255f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 256cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 257cd00a034SBen Coburn if ($revinfo !== false) { 258cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 259cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 260cd00a034SBen Coburn } 261cd00a034SBen Coburn } 262cd00a034SBen Coburn } 263cd00a034SBen Coburn //and check for an external edit 264cd00a034SBen Coburn if ($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 265cd00a034SBen Coburn // cached changelog line no longer valid 266cd00a034SBen Coburn $revinfo = false; 267cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 268cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 269652610a2Sandi } 270bb4866bdSchris 2710a444b5aSPhy if ($revinfo !== false) { 272652610a2Sandi $info['ip'] = $revinfo['ip']; 273652610a2Sandi $info['user'] = $revinfo['user']; 274652610a2Sandi $info['sum'] = $revinfo['sum']; 27571726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 276ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 27759f257aeSchris 278252acce3SSatoshi Sahara $info['editor'] = $revinfo['user'] ?: $revinfo['ip']; 2790a444b5aSPhy } else { 2800a444b5aSPhy $info['ip'] = null; 2810a444b5aSPhy $info['user'] = null; 2820a444b5aSPhy $info['sum'] = null; 2830a444b5aSPhy $info['editor'] = null; 2840a444b5aSPhy } 285652610a2Sandi 286ee4c4a1bSAndreas Gohr // draft 2870aabe6f8SMichael Große $draft = new \dokuwiki\Draft($ID, $info['client']); 2880aabe6f8SMichael Große if ($draft->isDraftAvailable()) { 2890aabe6f8SMichael Große $info['draft'] = $draft->getDraftFilename(); 290ee4c4a1bSAndreas Gohr } 291ee4c4a1bSAndreas Gohr 2921015a57dSChristopher Smith return $info; 2931015a57dSChristopher Smith} 2941015a57dSChristopher Smith 2951015a57dSChristopher Smith/** 2960c39d46cSMichael Große * Initialize and/or fill global $JSINFO with some basic info to be given to javascript 2970c39d46cSMichael Große */ 2980c39d46cSMichael Großefunction jsinfo() { 2990c39d46cSMichael Große global $JSINFO, $ID, $INFO, $ACT; 3000c39d46cSMichael Große 3010c39d46cSMichael Große if (!is_array($JSINFO)) { 3020c39d46cSMichael Große $JSINFO = []; 3030c39d46cSMichael Große } 3040c39d46cSMichael Große //export minimal info to JS, plugins can add more 3050c39d46cSMichael Große $JSINFO['id'] = $ID; 30668491db9SPhy $JSINFO['namespace'] = isset($INFO) ? (string) $INFO['namespace'] : ''; 3070c39d46cSMichael Große $JSINFO['ACT'] = act_clean($ACT); 3080c39d46cSMichael Große $JSINFO['useHeadingNavigation'] = (int) useHeading('navigation'); 3090c39d46cSMichael Große $JSINFO['useHeadingContent'] = (int) useHeading('content'); 3100c39d46cSMichael Große} 3110c39d46cSMichael Große 3120c39d46cSMichael Große/** 3131015a57dSChristopher Smith * Return information about the current media item as an associative array. 314140cfbcdSGerrit Uitslag * 315140cfbcdSGerrit Uitslag * @return array with info about current media item 3161015a57dSChristopher Smith */ 3171015a57dSChristopher Smithfunction mediainfo() { 3181015a57dSChristopher Smith global $NS; 3191015a57dSChristopher Smith global $IMG; 3201015a57dSChristopher Smith 3211015a57dSChristopher Smith $info = basicinfo("$NS:*"); 3221015a57dSChristopher Smith $info['image'] = $IMG; 3231c548ebeSAndreas Gohr 324f3f0262cSandi return $info; 325f3f0262cSandi} 326f3f0262cSandi 327f3f0262cSandi/** 3282684e50aSAndreas Gohr * Build an string of URL parameters 3292684e50aSAndreas Gohr * 3302684e50aSAndreas Gohr * @author Andreas Gohr 331140cfbcdSGerrit Uitslag * 332140cfbcdSGerrit Uitslag * @param array $params array with key-value pairs 333140cfbcdSGerrit Uitslag * @param string $sep series of pairs are separated by this character 334140cfbcdSGerrit Uitslag * @return string query string 3352684e50aSAndreas Gohr */ 336b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 3372684e50aSAndreas Gohr $url = ''; 3382684e50aSAndreas Gohr $amp = false; 3392684e50aSAndreas Gohr foreach($params as $key => $val) { 340b174aeaeSchris if($amp) $url .= $sep; 3412684e50aSAndreas Gohr 34285e6871fSAdrian Lang $url .= rawurlencode($key).'='; 3433a50618cSgweissbach $url .= rawurlencode((string) $val); 3442684e50aSAndreas Gohr $amp = true; 3452684e50aSAndreas Gohr } 3462684e50aSAndreas Gohr return $url; 3472684e50aSAndreas Gohr} 3482684e50aSAndreas Gohr 3492684e50aSAndreas Gohr/** 3502684e50aSAndreas Gohr * Build an string of html tag attributes 3512684e50aSAndreas Gohr * 3527bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 3537bff22c0SAndreas Gohr * 3542684e50aSAndreas Gohr * @author Andreas Gohr 355140cfbcdSGerrit Uitslag * 356140cfbcdSGerrit Uitslag * @param array $params array with (attribute name-attribute value) pairs 357246d3337SMichael Große * @param bool $skipEmptyStrings skip empty string values? 358140cfbcdSGerrit Uitslag * @return string 3592684e50aSAndreas Gohr */ 360246d3337SMichael Großefunction buildAttributes($params, $skipEmptyStrings = false) { 3612684e50aSAndreas Gohr $url = ''; 3629063ec14SAdrian Lang $white = false; 3632684e50aSAndreas Gohr foreach($params as $key => $val) { 3642401f18dSSyntaxseed if($key[0] == '_') continue; 365246d3337SMichael Große if($val === '' && $skipEmptyStrings) continue; 3669063ec14SAdrian Lang if($white) $url .= ' '; 3677bff22c0SAndreas Gohr 3682684e50aSAndreas Gohr $url .= $key.'="'; 3692684e50aSAndreas Gohr $url .= htmlspecialchars($val); 3702684e50aSAndreas Gohr $url .= '"'; 3719063ec14SAdrian Lang $white = true; 3722684e50aSAndreas Gohr } 3732684e50aSAndreas Gohr return $url; 3742684e50aSAndreas Gohr} 3752684e50aSAndreas Gohr 3762684e50aSAndreas Gohr/** 37715fae107Sandi * This builds the breadcrumb trail and returns it as array 37815fae107Sandi * 37915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 380140cfbcdSGerrit Uitslag * 381e3710957SGerrit Uitslag * @return string[] with the data: array(pageid=>name, ... ) 382f3f0262cSandi */ 383f3f0262cSandifunction breadcrumbs() { 3848746e727Sandi // we prepare the breadcrumbs early for quick session closing 3858746e727Sandi static $crumbs = null; 3868746e727Sandi if($crumbs != null) return $crumbs; 3878746e727Sandi 388f3f0262cSandi global $ID; 389f3f0262cSandi global $ACT; 390f3f0262cSandi global $conf; 3910ea5ebb4SB_S666 global $INFO; 392f3f0262cSandi 393f3f0262cSandi //first visit? 394c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 3955603d3c1SHenry Pan //we only save on show and existing visible readable wiki documents 396a77f5846Sjan $file = wikiFN($ID); 3975603d3c1SHenry Pan if($ACT != 'show' || $INFO['perm'] < AUTH_READ || isHiddenPage($ID) || !file_exists($file)) { 398e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 399f3f0262cSandi return $crumbs; 400f3f0262cSandi } 401a77f5846Sjan 402a77f5846Sjan // page names 4031a84a0f3SAnika Henke $name = noNSorNS($ID); 404fe9ec250SChris Smith if(useHeading('navigation')) { 405a77f5846Sjan // get page title 40667c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 407a77f5846Sjan if($title) { 408a77f5846Sjan $name = $title; 409a77f5846Sjan } 410a77f5846Sjan } 411a77f5846Sjan 412f3f0262cSandi //remove ID from array 413a77f5846Sjan if(isset($crumbs[$ID])) { 414a77f5846Sjan unset($crumbs[$ID]); 415f3f0262cSandi } 416f3f0262cSandi 417f3f0262cSandi //add to array 418a77f5846Sjan $crumbs[$ID] = $name; 419f3f0262cSandi //reduce size 420f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 421f3f0262cSandi array_shift($crumbs); 422f3f0262cSandi } 423f3f0262cSandi //save to session 424e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 425f3f0262cSandi return $crumbs; 426f3f0262cSandi} 427f3f0262cSandi 428f3f0262cSandi/** 42915fae107Sandi * Filter for page IDs 43015fae107Sandi * 431f3f0262cSandi * This is run on a ID before it is outputted somewhere 432f3f0262cSandi * currently used to replace the colon with something else 433907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 434907f24f7SAndreas Gohr * 435907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 436907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 437907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 43815fae107Sandi * 43949c713a3Sandi * Urlencoding is ommitted when the second parameter is false 44049c713a3Sandi * 44115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 442140cfbcdSGerrit Uitslag * 443140cfbcdSGerrit Uitslag * @param string $id pageid being filtered 444140cfbcdSGerrit Uitslag * @param bool $ue apply urlencoding? 445140cfbcdSGerrit Uitslag * @return string 446f3f0262cSandi */ 44749c713a3Sandifunction idfilter($id, $ue = true) { 448f3f0262cSandi global $conf; 449585bf44eSChristopher Smith /* @var Input $INPUT */ 450585bf44eSChristopher Smith global $INPUT; 451585bf44eSChristopher Smith 452f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 453f3f0262cSandi $id = strtr($id, ':', '/'); 454f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 45558bedc8aSborekb $conf['userewrite'] && 456585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 4573272d797SAndreas Gohr ) { 458f3f0262cSandi $id = strtr($id, ':', ';'); 459f3f0262cSandi } 46049c713a3Sandi if($ue) { 461b6c6979fSAndreas Gohr $id = rawurlencode($id); 462f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 463edd95259SGerrit Uitslag $id = str_replace('%3B', ';', $id); //keep as semicolon 464f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 46549c713a3Sandi } 466f3f0262cSandi return $id; 467f3f0262cSandi} 468f3f0262cSandi 469f3f0262cSandi/** 470ed7b5f09Sandi * This builds a link to a wikipage 47115fae107Sandi * 4724bc480e5SAndreas Gohr * It handles URL rewriting and adds additional parameters 4736c7843b5Sandi * 47415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 4754bc480e5SAndreas Gohr * 4764bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 4774bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 4784bc480e5SAndreas Gohr * @param bool $absolute request an absolute URL instead of relative 4794bc480e5SAndreas Gohr * @param string $separator parameter separator 4804bc480e5SAndreas Gohr * @return string 481f3f0262cSandi */ 48216f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 483f3f0262cSandi global $conf; 48416f15a81SDominik Eckelmann if(is_array($urlParameters)) { 4854bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 48664159a61SAndreas Gohr if(isset($urlParameters['at']) && $conf['date_at_format']) { 48764159a61SAndreas Gohr $urlParameters['at'] = date($conf['date_at_format'], $urlParameters['at']); 48864159a61SAndreas Gohr } 48916f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4906de3759aSAndreas Gohr } else { 49116f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4926de3759aSAndreas Gohr } 49316f15a81SDominik Eckelmann if($id === '') { 49416f15a81SDominik Eckelmann $id = $conf['start']; 49516f15a81SDominik Eckelmann } 496f3f0262cSandi $id = idfilter($id); 49716f15a81SDominik Eckelmann if($absolute) { 498ed7b5f09Sandi $xlink = DOKU_URL; 499ed7b5f09Sandi } else { 500ed7b5f09Sandi $xlink = DOKU_BASE; 501ed7b5f09Sandi } 502f3f0262cSandi 5036c7843b5Sandi if($conf['userewrite'] == 2) { 5046c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 50516f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 5066c7843b5Sandi } elseif($conf['userewrite']) { 507f3f0262cSandi $xlink .= $id; 50816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 50940b5fb5bSPhy } elseif($id !== '') { 5106c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 51116f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 512bce3726dSAndreas Gohr } else { 513bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 51416f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 515f3f0262cSandi } 516f3f0262cSandi 517f3f0262cSandi return $xlink; 518f3f0262cSandi} 519f3f0262cSandi 520f3f0262cSandi/** 521f5c2808fSBen Coburn * This builds a link to an alternate page format 522f5c2808fSBen Coburn * 523f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 524f5c2808fSBen Coburn * 525f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 5264bc480e5SAndreas Gohr * @param string $id page id, defaults to start page 5274bc480e5SAndreas Gohr * @param string $format the export renderer to use 5284bc480e5SAndreas Gohr * @param string|array $urlParameters URL parameters, associative array recommended 5294bc480e5SAndreas Gohr * @param bool $abs request an absolute URL instead of relative 5304bc480e5SAndreas Gohr * @param string $sep parameter separator 5314bc480e5SAndreas Gohr * @return string 532f5c2808fSBen Coburn */ 5334bc480e5SAndreas Gohrfunction exportlink($id = '', $format = 'raw', $urlParameters = '', $abs = false, $sep = '&') { 534f5c2808fSBen Coburn global $conf; 5354bc480e5SAndreas Gohr if(is_array($urlParameters)) { 5364bc480e5SAndreas Gohr $urlParameters = buildURLparams($urlParameters, $sep); 537f5c2808fSBen Coburn } else { 5384bc480e5SAndreas Gohr $urlParameters = str_replace(',', $sep, $urlParameters); 539f5c2808fSBen Coburn } 540f5c2808fSBen Coburn 541f5c2808fSBen Coburn $format = rawurlencode($format); 542f5c2808fSBen Coburn $id = idfilter($id); 543f5c2808fSBen Coburn if($abs) { 544f5c2808fSBen Coburn $xlink = DOKU_URL; 545f5c2808fSBen Coburn } else { 546f5c2808fSBen Coburn $xlink = DOKU_BASE; 547f5c2808fSBen Coburn } 548f5c2808fSBen Coburn 549f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 550f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 5514bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 552f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 553f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 5544bc480e5SAndreas Gohr if($urlParameters) $xlink .= '?'.$urlParameters; 555f5c2808fSBen Coburn } else { 556f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 5574bc480e5SAndreas Gohr if($urlParameters) $xlink .= $sep.$urlParameters; 558f5c2808fSBen Coburn } 559f5c2808fSBen Coburn 560f5c2808fSBen Coburn return $xlink; 561f5c2808fSBen Coburn} 562f5c2808fSBen Coburn 563f5c2808fSBen Coburn/** 5646de3759aSAndreas Gohr * Build a link to a media file 5656de3759aSAndreas Gohr * 5666de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 5678c08db0aSAndreas Gohr * 5688c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 5698c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 5708c08db0aSAndreas Gohr * 5713272d797SAndreas Gohr * @param string $id the media file id or URL 5723272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 5733272d797SAndreas Gohr * @param bool $direct link to detail page if false 5743272d797SAndreas Gohr * @param string $sep URL parameter separator 5753272d797SAndreas Gohr * @param bool $abs Create an absolute URL 5763272d797SAndreas Gohr * @return string 5776de3759aSAndreas Gohr */ 57855b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 5796de3759aSAndreas Gohr global $conf; 580b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 581826d2766SKlap-in if(!$isexternalimage) { 582826d2766SKlap-in $id = cleanID($id); 583826d2766SKlap-in } 584826d2766SKlap-in 5856de3759aSAndreas Gohr if(is_array($more)) { 5860f4e0092SChristopher Smith // add token for resized images 587357c9a39SDamien Regad $w = isset($more['w']) ? $more['w'] : null; 588357c9a39SDamien Regad $h = isset($more['h']) ? $more['h'] : null; 58998fe1ac9SDamien Regad if($w || $h || $isexternalimage){ 590357c9a39SDamien Regad $more['tok'] = media_get_token($id, $w, $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 }; 756cbb44eabSAndreas Gohr return Event::createAndTrigger('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 */ 779925105e8SPhy global $INPUT, $conf; 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)); 822056bf31fSDamien Regad if(empty($ip) || !$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 826925105e8SPhy // skip trusted local addresses 8276d8affe6SAndreas Gohr foreach($ip as $i) { 828925105e8SPhy if(!empty($conf['trustedproxy']) && preg_match('/'.$conf['trustedproxy'].'/', $i)) { 8296d8affe6SAndreas Gohr continue; 8306d8affe6SAndreas Gohr } else { 8316d8affe6SAndreas Gohr return $i; 8326d8affe6SAndreas Gohr } 8336d8affe6SAndreas Gohr } 834925105e8SPhy 835925105e8SPhy // still here? just use the last address 836925105e8SPhy // this case all ips in the list are trusted 837925105e8SPhy return $ip[count($ip)-1]; 838f3f0262cSandi} 839f3f0262cSandi 840f3f0262cSandi/** 8411c548ebeSAndreas Gohr * Check if the browser is on a mobile device 8421c548ebeSAndreas Gohr * 8431c548ebeSAndreas Gohr * Adapted from the example code at url below 8441c548ebeSAndreas Gohr * 8451c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 846140cfbcdSGerrit Uitslag * 84764159a61SAndreas Gohr * @deprecated 2018-04-27 you probably want media queries instead anyway 848140cfbcdSGerrit Uitslag * @return bool if true, client is mobile browser; otherwise false 8491c548ebeSAndreas Gohr */ 8501c548ebeSAndreas Gohrfunction clientismobile() { 851585bf44eSChristopher Smith /* @var Input $INPUT */ 852585bf44eSChristopher Smith global $INPUT; 8531c548ebeSAndreas Gohr 854585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 8551c548ebeSAndreas Gohr 856585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 8571c548ebeSAndreas Gohr 858585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 8591c548ebeSAndreas Gohr 86064159a61SAndreas Gohr $uamatches = join( 86164159a61SAndreas Gohr '|', 86264159a61SAndreas Gohr [ 86364159a61SAndreas Gohr 'midp', 'j2me', 'avantg', 'docomo', 'novarra', 'palmos', 'palmsource', '240x320', 'opwv', 86464159a61SAndreas Gohr 'chtml', 'pda', 'windows ce', 'mmp\/', 'blackberry', 'mib\/', 'symbian', 'wireless', 'nokia', 86564159a61SAndreas Gohr 'hand', 'mobi', 'phone', 'cdm', 'up\.b', 'audio', 'SIE\-', 'SEC\-', 'samsung', 'HTC', 'mot\-', 86664159a61SAndreas Gohr 'mitsu', 'sagem', 'sony', 'alcatel', 'lg', 'erics', 'vx', 'NEC', 'philips', 'mmm', 'xx', 86764159a61SAndreas Gohr 'panasonic', 'sharp', 'wap', 'sch', 'rover', 'pocket', 'benq', 'java', 'pt', 'pg', 'vox', 86864159a61SAndreas Gohr 'amoi', 'bird', 'compal', 'kg', 'voda', 'sany', 'kdd', 'dbt', 'sendo', 'sgh', 'gradi', 'jb', 86964159a61SAndreas Gohr '\d\d\di', 'moto' 87064159a61SAndreas Gohr ] 87164159a61SAndreas Gohr ); 8721c548ebeSAndreas Gohr 873585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 8741c548ebeSAndreas Gohr 8751c548ebeSAndreas Gohr return false; 8761c548ebeSAndreas Gohr} 8771c548ebeSAndreas Gohr 8781c548ebeSAndreas Gohr/** 8796efc45a2SDmitry Katsubo * check if a given link is interwiki link 8806efc45a2SDmitry Katsubo * 8816efc45a2SDmitry Katsubo * @param string $link the link, e.g. "wiki>page" 8826efc45a2SDmitry Katsubo * @return bool 8836efc45a2SDmitry Katsubo */ 8846efc45a2SDmitry Katsubofunction link_isinterwiki($link){ 8856efc45a2SDmitry Katsubo if (preg_match('/^[a-zA-Z0-9\.]+>/u',$link)) return true; 8866efc45a2SDmitry Katsubo return false; 8876efc45a2SDmitry Katsubo} 8886efc45a2SDmitry Katsubo 8896efc45a2SDmitry Katsubo/** 89063211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 89163211f61SGlen Harris * 89222ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 89322ef1e32SAndreas Gohr * 89463211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 895140cfbcdSGerrit Uitslag * 8963272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 8973272d797SAndreas Gohr * @return string a comma separated list of hostnames 89863211f61SGlen Harris */ 89963211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 90022ef1e32SAndreas Gohr global $conf; 90122ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 90222ef1e32SAndreas Gohr 90363211f61SGlen Harris $hosts = array(); 90463211f61SGlen Harris $ips = explode(',', $ips); 905551a720fSMichael Klier 906551a720fSMichael Klier if(is_array($ips)) { 9073886270dSAndreas Gohr foreach($ips as $ip) { 908551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 90963211f61SGlen Harris } 910551a720fSMichael Klier return join(',', $hosts); 911551a720fSMichael Klier } else { 912551a720fSMichael Klier return gethostbyaddr(trim($ips)); 913551a720fSMichael Klier } 91463211f61SGlen Harris} 91563211f61SGlen Harris 91663211f61SGlen Harris/** 91715fae107Sandi * Checks if a given page is currently locked. 91815fae107Sandi * 919f3f0262cSandi * removes stale lockfiles 92015fae107Sandi * 92115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 922140cfbcdSGerrit Uitslag * 923140cfbcdSGerrit Uitslag * @param string $id page id 924140cfbcdSGerrit Uitslag * @return bool page is locked? 925f3f0262cSandi */ 926f3f0262cSandifunction checklock($id) { 927f3f0262cSandi global $conf; 928585bf44eSChristopher Smith /* @var Input $INPUT */ 929585bf44eSChristopher Smith global $INPUT; 930585bf44eSChristopher Smith 931c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 932f3f0262cSandi 933f3f0262cSandi //no lockfile 93479e79377SAndreas Gohr if(!file_exists($lock)) return false; 935f3f0262cSandi 936f3f0262cSandi //lockfile expired 937f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 938d8186216SBen Coburn @unlink($lock); 939f3f0262cSandi return false; 940f3f0262cSandi } 941f3f0262cSandi 942f3f0262cSandi //my own lock 9436d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 9440712fefaSAndreas Gohr if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) { 945f3f0262cSandi return false; 946f3f0262cSandi } 947f3f0262cSandi 948f3f0262cSandi return $ip; 949f3f0262cSandi} 950f3f0262cSandi 951f3f0262cSandi/** 95215fae107Sandi * Lock a page for editing 95315fae107Sandi * 95415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 955140cfbcdSGerrit Uitslag * 956140cfbcdSGerrit Uitslag * @param string $id page id to lock 957f3f0262cSandi */ 958f3f0262cSandifunction lock($id) { 959544ed901SDaniel Calviño Sánchez global $conf; 960585bf44eSChristopher Smith /* @var Input $INPUT */ 961585bf44eSChristopher Smith global $INPUT; 962544ed901SDaniel Calviño Sánchez 963544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 964544ed901SDaniel Calviño Sánchez return; 965544ed901SDaniel Calviño Sánchez } 966544ed901SDaniel Calviño Sánchez 967c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 968585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 969585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 970f3f0262cSandi } else { 97185fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 972f3f0262cSandi } 973f3f0262cSandi} 974f3f0262cSandi 975f3f0262cSandi/** 97615fae107Sandi * Unlock a page if it was locked by the user 977f3f0262cSandi * 97815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 979140cfbcdSGerrit Uitslag * 9803272d797SAndreas Gohr * @param string $id page id to unlock 98115fae107Sandi * @return bool true if a lock was removed 982f3f0262cSandi */ 983f3f0262cSandifunction unlock($id) { 984585bf44eSChristopher Smith /* @var Input $INPUT */ 985585bf44eSChristopher Smith global $INPUT; 986585bf44eSChristopher Smith 987c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 98879e79377SAndreas Gohr if(file_exists($lock)) { 9896d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 990585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 991f3f0262cSandi @unlink($lock); 992f3f0262cSandi return true; 993f3f0262cSandi } 994f3f0262cSandi } 995f3f0262cSandi return false; 996f3f0262cSandi} 997f3f0262cSandi 998f3f0262cSandi/** 999f3f0262cSandi * convert line ending to unix format 1000f3f0262cSandi * 10016db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 10026db7468bSAndreas Gohr * 100315fae107Sandi * @see formText() for 2crlf conversion 100415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1005140cfbcdSGerrit Uitslag * 1006140cfbcdSGerrit Uitslag * @param string $text 1007140cfbcdSGerrit Uitslag * @return string 1008f3f0262cSandi */ 1009f3f0262cSandifunction cleanText($text) { 1010f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 10116db7468bSAndreas Gohr 10126db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 10136db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 10146db7468bSAndreas Gohr // but might actually fix the problem in many cases 10158cbc5ee8SAndreas Gohr if(!\dokuwiki\Utf8\Clean::isUtf8($text)) $text = utf8_encode($text); 10166db7468bSAndreas Gohr 1017f3f0262cSandi return $text; 1018f3f0262cSandi} 1019f3f0262cSandi 1020f3f0262cSandi/** 1021f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 1022f3f0262cSandi * It also converts line endings to Windows format which is 1023f3f0262cSandi * pseudo standard for webforms. 1024f3f0262cSandi * 102515fae107Sandi * @see cleanText() for 2unix conversion 102615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1027140cfbcdSGerrit Uitslag * 1028140cfbcdSGerrit Uitslag * @param string $text 1029140cfbcdSGerrit Uitslag * @return string 1030f3f0262cSandi */ 1031f3f0262cSandifunction formText($text) { 10325b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 1033f3f0262cSandi return htmlspecialchars($text); 1034f3f0262cSandi} 1035f3f0262cSandi 1036f3f0262cSandi/** 103715fae107Sandi * Returns the specified local text in raw format 103815fae107Sandi * 103915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1040140cfbcdSGerrit Uitslag * 1041140cfbcdSGerrit Uitslag * @param string $id page id 1042140cfbcdSGerrit Uitslag * @param string $ext extension of file being read, default 'txt' 1043140cfbcdSGerrit Uitslag * @return string 1044f3f0262cSandi */ 10452adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 10462adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 1047f3f0262cSandi} 1048f3f0262cSandi 1049f3f0262cSandi/** 1050f3f0262cSandi * Returns the raw WikiText 105115fae107Sandi * 105215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1053140cfbcdSGerrit Uitslag * 1054140cfbcdSGerrit Uitslag * @param string $id page id 1055e0c26282SGerrit Uitslag * @param string|int $rev timestamp when a revision of wikitext is desired 1056140cfbcdSGerrit Uitslag * @return string 1057f3f0262cSandi */ 1058f3f0262cSandifunction rawWiki($id, $rev = '') { 1059cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1060f3f0262cSandi} 1061f3f0262cSandi 1062f3f0262cSandi/** 10637146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 10647146cee2SAndreas Gohr * 10657b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 10667146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1067140cfbcdSGerrit Uitslag * 1068140cfbcdSGerrit Uitslag * @param string $id the id of the page to be created 1069140cfbcdSGerrit Uitslag * @return string parsed pagetemplate content 10707146cee2SAndreas Gohr */ 1071fe17917eSAdrian Langfunction pageTemplate($id) { 1072a15ce62dSEsther Brunner global $conf; 1073e29549feSAndreas Gohr 1074fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 1075e29549feSAndreas Gohr 10767b84afa2SAndreas Gohr // prepare initial event data 10777b84afa2SAndreas Gohr $data = array( 10787b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 10797b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 10807b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 10817b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 10827b84afa2SAndreas Gohr ); 10837b84afa2SAndreas Gohr 1084e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_PAGETPL_LOAD', $data); 10857b84afa2SAndreas Gohr if($evt->advise_before(true)) { 10867b84afa2SAndreas Gohr // the before event might have loaded the content already 10877b84afa2SAndreas Gohr if(empty($data['tpl'])) { 10887b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 10897b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 1090fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 109179e79377SAndreas Gohr if(file_exists($path.'/_template.txt')) { 10927b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 1093e29549feSAndreas Gohr } else { 1094e29549feSAndreas Gohr // search upper namespaces for templates 1095e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 1096e29549feSAndreas Gohr while(strlen($path) >= $len) { 109779e79377SAndreas Gohr if(file_exists($path.'/__template.txt')) { 10987b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 1099e29549feSAndreas Gohr break; 1100e29549feSAndreas Gohr } 1101e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 1102e29549feSAndreas Gohr } 1103e29549feSAndreas Gohr } 11047b84afa2SAndreas Gohr } 11057b84afa2SAndreas Gohr // load the content 11063d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 11077b84afa2SAndreas Gohr } 1108a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 11097b84afa2SAndreas Gohr } 11107b84afa2SAndreas Gohr $evt->advise_after(); 11117b84afa2SAndreas Gohr unset($evt); 11127b84afa2SAndreas Gohr 1113fe17917eSAdrian Lang return $data['tpl']; 11142b1223ecSAdrian Lang} 11152b1223ecSAdrian Lang 11162b1223ecSAdrian Lang/** 11172b1223ecSAdrian Lang * Performs common page template replacements 11187b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 11192b1223ecSAdrian Lang * 11202b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 1121140cfbcdSGerrit Uitslag * 1122140cfbcdSGerrit Uitslag * @param array $data array with event data 1123140cfbcdSGerrit Uitslag * @return string 11242b1223ecSAdrian Lang */ 1125d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 11263272d797SAndreas Gohr /** 11273272d797SAndreas Gohr * @var string $id the id of the page to be created 11283272d797SAndreas Gohr * @var string $tpl the text used as template 11293272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 11303272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 11313272d797SAndreas Gohr */ 1132fe17917eSAdrian Lang extract($data); 1133fe17917eSAdrian Lang 1134b856f7dfSAdrian Lang global $USERINFO; 1135bce53b1fSAdrian Lang global $conf; 1136585bf44eSChristopher Smith /* @var Input $INPUT */ 1137585bf44eSChristopher Smith global $INPUT; 1138e29549feSAndreas Gohr 1139e29549feSAndreas Gohr // replace placeholders 114026ece5a7SAndreas Gohr $file = noNS($id); 114137c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 114226ece5a7SAndreas Gohr 11433272d797SAndreas Gohr $tpl = str_replace( 11443272d797SAndreas Gohr array( 114526ece5a7SAndreas Gohr '@ID@', 114626ece5a7SAndreas Gohr '@NS@', 11478a7bcf66SShota Miyazaki '@CURNS@', 1148a3db0ab0SSimon Lees '@!CURNS@', 1149a3db0ab0SSimon Lees '@!!CURNS@', 1150a3db0ab0SSimon Lees '@!CURNS!@', 115126ece5a7SAndreas Gohr '@FILE@', 115226ece5a7SAndreas Gohr '@!FILE@', 115326ece5a7SAndreas Gohr '@!FILE!@', 115426ece5a7SAndreas Gohr '@PAGE@', 115526ece5a7SAndreas Gohr '@!PAGE@', 115626ece5a7SAndreas Gohr '@!!PAGE@', 115726ece5a7SAndreas Gohr '@!PAGE!@', 115826ece5a7SAndreas Gohr '@USER@', 115926ece5a7SAndreas Gohr '@NAME@', 116026ece5a7SAndreas Gohr '@MAIL@', 116126ece5a7SAndreas Gohr '@DATE@', 116226ece5a7SAndreas Gohr ), 116326ece5a7SAndreas Gohr array( 116426ece5a7SAndreas Gohr $id, 116526ece5a7SAndreas Gohr getNS($id), 11668a7bcf66SShota Miyazaki curNS($id), 1167c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst(curNS($id)), 1168c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords(curNS($id)), 1169c1ec88ceSAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper(curNS($id)), 117026ece5a7SAndreas Gohr $file, 11718cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($file), 11728cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($file), 117326ece5a7SAndreas Gohr $page, 11748cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucfirst($page), 11758cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::ucwords($page), 11768cbc5ee8SAndreas Gohr \dokuwiki\Utf8\PhpString::strtoupper($page), 1177585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 11783e9ae63dSPhy $USERINFO ? $USERINFO['name'] : '', 11793e9ae63dSPhy $USERINFO ? $USERINFO['mail'] : '', 118026ece5a7SAndreas Gohr $conf['dformat'], 11813272d797SAndreas Gohr ), $tpl 11823272d797SAndreas Gohr ); 118326ece5a7SAndreas Gohr 11847d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 1185bad6fc0dSAndreas Gohr $tpl = preg_replace_callback( 1186bad6fc0dSAndreas Gohr '/%./', 1187bad6fc0dSAndreas Gohr function ($m) { 1188bad6fc0dSAndreas Gohr return strftime($m[0]); 1189bad6fc0dSAndreas Gohr }, 1190bad6fc0dSAndreas Gohr $tpl 1191bad6fc0dSAndreas Gohr ); 1192d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1193a15ce62dSEsther Brunner return $tpl; 11947146cee2SAndreas Gohr} 11957146cee2SAndreas Gohr 11967146cee2SAndreas Gohr/** 119715fae107Sandi * Returns the raw Wiki Text in three slices. 119815fae107Sandi * 119915fae107Sandi * The range parameter needs to have the form "from-to" 120015cfe303Sandi * and gives the range of the section in bytes - no 120115cfe303Sandi * UTF-8 awareness is needed. 1202f3f0262cSandi * The returned order is prefix, section and suffix. 120315fae107Sandi * 120415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1205140cfbcdSGerrit Uitslag * 1206140cfbcdSGerrit Uitslag * @param string $range in form "from-to" 1207140cfbcdSGerrit Uitslag * @param string $id page id 1208140cfbcdSGerrit Uitslag * @param string $rev optional, the revision timestamp 120942ea7f44SGerrit Uitslag * @return string[] with three slices 1210f3f0262cSandi */ 1211f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1212cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1213f3f0262cSandi 121480fcb268SAdrian Lang // Parse range 121580fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 121680fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 121780fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 121880fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 121980fcb268SAdrian Lang 122059bc3b48SGerrit Uitslag $slices = array(); 122180fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 122280fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 122315cfe303Sandi $slices[2] = substr($text, $to); 1224f3f0262cSandi return $slices; 1225f3f0262cSandi} 1226f3f0262cSandi 1227f3f0262cSandi/** 122815fae107Sandi * Joins wiki text slices 122915fae107Sandi * 123080fcb268SAdrian Lang * function to join the text slices. 1231f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1232f3f0262cSandi * lines between sections if needed (used on saving). 123315fae107Sandi * 123415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1235140cfbcdSGerrit Uitslag * 1236140cfbcdSGerrit Uitslag * @param string $pre prefix 1237140cfbcdSGerrit Uitslag * @param string $text text in the middle 1238140cfbcdSGerrit Uitslag * @param string $suf suffix 1239140cfbcdSGerrit Uitslag * @param bool $pretty add additional empty lines between sections 1240140cfbcdSGerrit Uitslag * @return string 1241f3f0262cSandi */ 1242f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1243f3f0262cSandi if($pretty) { 124480fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 12453272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 12463272d797SAndreas Gohr ) { 124780fcb268SAdrian Lang $pre .= "\n"; 124880fcb268SAdrian Lang } 124980fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 12503272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 12513272d797SAndreas Gohr ) { 125280fcb268SAdrian Lang $text .= "\n"; 125380fcb268SAdrian Lang } 1254f3f0262cSandi } 1255f3f0262cSandi 1256f3f0262cSandi return $pre.$text.$suf; 1257f3f0262cSandi} 1258f3f0262cSandi 1259f3f0262cSandi/** 1260b24d9195SAndreas Gohr * Checks if the current page version is newer than the last entry in the page's 1261b24d9195SAndreas Gohr * changelog. If so, we assume it has been an external edit and we create an 1262b24d9195SAndreas Gohr * attic copy and add a proper changelog line. 1263b24d9195SAndreas Gohr * 1264b24d9195SAndreas Gohr * This check is only executed when the page is about to be saved again from the 1265b24d9195SAndreas Gohr * wiki, triggered in @see saveWikiText() 1266b24d9195SAndreas Gohr * 1267b24d9195SAndreas Gohr * @param string $id the page ID 1268b24d9195SAndreas Gohr */ 1269b24d9195SAndreas Gohrfunction detectExternalEdit($id) { 1270b24d9195SAndreas Gohr 1271b24d9195SAndreas Gohr $pagelog = new PageChangeLog($id, 1024); 1272c7192766SSatoshi Sahara $revInfo = $pagelog->getCurrentRevisionInfo(); 1273b24d9195SAndreas Gohr 1274d5824ab9SSatoshi Sahara // only interested in external revision 1275c7192766SSatoshi Sahara if (empty($revInfo) || !array_key_exists('timestamp', $revInfo)) return; 1276c7192766SSatoshi Sahara 1277*66f4cdd4SSatoshi Sahara if ($revInfo['type'] != DOKU_CHANGE_TYPE_DELETE && !$revInfo['timestamp']) { 1278*66f4cdd4SSatoshi Sahara // file is older than last revision, that is erroneous/incorrect occurence. 1279*66f4cdd4SSatoshi Sahara // try to change file modification time 1280*66f4cdd4SSatoshi Sahara $fileLastMod = wikiFN($id); 1281*66f4cdd4SSatoshi Sahara $wrong_timestamp = filemtime($fileLastMod); 1282*66f4cdd4SSatoshi Sahara if (touch($fileLastMod, $revInfo['date'])) { 1283*66f4cdd4SSatoshi Sahara clearstatcache(); 1284*66f4cdd4SSatoshi Sahara $msg = "detectExternalEdit($id): timestamp successfully modified"; 1285*66f4cdd4SSatoshi Sahara $details = '('.$wrong_timestamp.' -> '.$revInfo['date'].')'; 1286*66f4cdd4SSatoshi Sahara Logger::error($msg, $details, $fileLastMod); 1287*66f4cdd4SSatoshi Sahara } else { 1288*66f4cdd4SSatoshi Sahara // runtime error 1289*66f4cdd4SSatoshi Sahara $msg = "detectExternalEdit($id): page file should be newer than last revision " 1290*66f4cdd4SSatoshi Sahara .'('.filemtime($fileLastMod).' < '. $pagelog->lastRevision() .')'; 1291*66f4cdd4SSatoshi Sahara throw new \RuntimeException($msg); 1292*66f4cdd4SSatoshi Sahara } 1293*66f4cdd4SSatoshi Sahara } 1294*66f4cdd4SSatoshi Sahara 1295*66f4cdd4SSatoshi Sahara // keep at least 1 sec before new page save 1296*66f4cdd4SSatoshi Sahara if ($revInfo['date'] == time()) sleep(1); // wait a tick 1297c7192766SSatoshi Sahara 1298c7192766SSatoshi Sahara // store externally edited file to the attic folder 1299b24d9195SAndreas Gohr saveOldRevision($id); 1300c7192766SSatoshi Sahara // add a changelog entry for externally edited file 1301*66f4cdd4SSatoshi Sahara $revInfo = $pagelog->addLogEntry($revInfo); 1302b24d9195SAndreas Gohr // remove soon to be stale instructions 1303c7192766SSatoshi Sahara $cache = new CacheInstructions($id, wikiFN($id)); 1304b24d9195SAndreas Gohr $cache->removeCache(); 1305b24d9195SAndreas Gohr} 1306b24d9195SAndreas Gohr 1307b24d9195SAndreas Gohr/** 1308a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1309a701424fSBen Coburn * Also directs changelog and attic updates. 131015fae107Sandi * 131115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 131271726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1313140cfbcdSGerrit Uitslag * 1314140cfbcdSGerrit Uitslag * @param string $id page id 1315140cfbcdSGerrit Uitslag * @param string $text wikitext being saved 1316140cfbcdSGerrit Uitslag * @param string $summary summary of text update 1317140cfbcdSGerrit Uitslag * @param bool $minor mark this saved version as minor update 1318f3f0262cSandi */ 1319b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1320a701424fSBen Coburn /* Note to developers: 1321a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1322a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1323a701424fSBen Coburn after any changes. External edits change the wiki page 1324a701424fSBen Coburn directly without using php or dokuwiki. 1325a701424fSBen Coburn */ 1326f3f0262cSandi global $conf; 1327f3f0262cSandi global $lang; 132871726d78SBen Coburn global $REV; 1329585bf44eSChristopher Smith /* @var Input $INPUT */ 1330585bf44eSChristopher Smith global $INPUT; 1331585bf44eSChristopher Smith 1332d5824ab9SSatoshi Sahara $pagefile = wikiFN($id); 1333d5824ab9SSatoshi Sahara $currentRevision = @filemtime($pagefile); // int or false 1334d5824ab9SSatoshi Sahara $currentContent = rawWiki($id); 1335d5824ab9SSatoshi Sahara $currentSize = file_exists($pagefile) ? filesize($pagefile) : 0; 1336b24d9195SAndreas Gohr 1337d5824ab9SSatoshi Sahara // prepare data for event COMMON_WIKIPAGE_SAVE 1338d5824ab9SSatoshi Sahara $data = array( 1339d5824ab9SSatoshi Sahara 'id' => $id, // should not be altered by any handlers 1340d5824ab9SSatoshi Sahara 'file' => $pagefile, // same above 13419c42b79dSSatoshi Sahara 'changeType' => null, // set prior to event, and confirm later 1342d5824ab9SSatoshi Sahara 'revertFrom' => $REV, 1343d5824ab9SSatoshi Sahara 'oldRevision' => $currentRevision, 13449c42b79dSSatoshi Sahara 'oldContent' => $currentContent, 1345d5824ab9SSatoshi Sahara 'newRevision' => 0, // only available in the after hook 1346d5824ab9SSatoshi Sahara 'newContent' => $text, 1347d5824ab9SSatoshi Sahara 'summary' => $summary, 1348d5824ab9SSatoshi Sahara 'contentChanged' => (bool)($text != $currentContent), // confirm later 13499c42b79dSSatoshi Sahara 'changeInfo' => '', // automatically determined by revertFrom 1350d5824ab9SSatoshi Sahara 'sizechange' => strlen($text) - strlen($currentContent), // TBD 1351d5824ab9SSatoshi Sahara ); 13529c42b79dSSatoshi Sahara 13539c42b79dSSatoshi Sahara // determine tentatively change type and relevant elements of event data 13549c42b79dSSatoshi Sahara $tentative = true; 13559c42b79dSSatoshi Sahara DETERMINE_CHANGE_TYPE: { 13569c42b79dSSatoshi Sahara if ($data['revertFrom']) { 13579c42b79dSSatoshi Sahara // new text may differ from exact revert revision 1358d5824ab9SSatoshi Sahara $data['changeType'] = DOKU_CHANGE_TYPE_REVERT; 1359d5824ab9SSatoshi Sahara $data['changeInfo'] = $REV; 13609c42b79dSSatoshi Sahara } elseif (trim($data['newContent']) == '') { 1361b24d9195SAndreas Gohr // empty or whitespace only content deletes 1362d5824ab9SSatoshi Sahara $data['changeType'] = DOKU_CHANGE_TYPE_DELETE; 13639c42b79dSSatoshi Sahara } elseif (!file_exists($pagefile)) { 13649c42b79dSSatoshi Sahara $data['changeType'] = DOKU_CHANGE_TYPE_CREATE; 1365d5824ab9SSatoshi Sahara } else { 13669c42b79dSSatoshi Sahara // minor edits allowable only for logged in users 13679c42b79dSSatoshi Sahara $is_minor_change = ($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')); 13689c42b79dSSatoshi Sahara $data['changeType'] = $is_minor_change 13699c42b79dSSatoshi Sahara ? DOKU_CHANGE_TYPE_MINOR_EDIT 13709c42b79dSSatoshi Sahara : DOKU_CHANGE_TYPE_EDIT; 13719c42b79dSSatoshi Sahara } 13729c42b79dSSatoshi Sahara if (!$tentative) goto MAIN; 13739c42b79dSSatoshi Sahara /* FIXME: reluctantly use of goto statement to avoid declare new function in this file. 13749c42b79dSSatoshi Sahara nice to have a dedicated class that implements saveWikiText(), as well as 13759c42b79dSSatoshi Sahara determineChangeType(), saveOldRevision(), detectExternalEdit(), ... 13769c42b79dSSatoshi Sahara */ 1377f3f0262cSandi } 1378f3f0262cSandi 1379d5824ab9SSatoshi Sahara $event = new Event('COMMON_WIKIPAGE_SAVE', $data); 1380b24d9195SAndreas Gohr if (!$event->advise_before()) return; 1381f3f0262cSandi 1382b24d9195SAndreas Gohr // if the content has not been changed, no save happens (plugins may override this) 1383d5824ab9SSatoshi Sahara if (!$data['contentChanged']) return; 1384b24d9195SAndreas Gohr 13859c42b79dSSatoshi Sahara // Confirm again both event data and pagefile that may altered by event handlers 13869c42b79dSSatoshi Sahara // 13879c42b79dSSatoshi Sahara // Event handlers may also modify the pagefile as well as oldRevision of event data. 13889c42b79dSSatoshi Sahara // For example, we can imagine an action plugin that provides alternative approach for 13899c42b79dSSatoshi Sahara // handling external edits in changelog; merging early external edits into one normal 13909c42b79dSSatoshi Sahara // edit entry instead of separating two entries of external and normal edits. 13919c42b79dSSatoshi Sahara // This will be achievable if the pagefile could be restored to the last revision 13929c42b79dSSatoshi Sahara // during $event->advise_before() using attic data. 13939c42b79dSSatoshi Sahara // 13949c42b79dSSatoshi Sahara $tentative = false; 13959c42b79dSSatoshi Sahara goto DETERMINE_CHANGE_TYPE; 13969c42b79dSSatoshi Sahara MAIN: 13979c42b79dSSatoshi Sahara // Check whether the pagefile has modified during $event->advise_before() 13989c42b79dSSatoshi Sahara clearstatcache(false, $pagefile); 13999c42b79dSSatoshi Sahara $fileRev = @filemtime($pagefile); 14009c42b79dSSatoshi Sahara if ($fileRev === $currentRevision) { 14019c42b79dSSatoshi Sahara // pagefile has not touched by plugin 14029c42b79dSSatoshi Sahara // add a potential external edit entry to changelog and store it into attic 1403d5824ab9SSatoshi Sahara detectExternalEdit($id); 1404d5824ab9SSatoshi Sahara $filesize_old = $currentSize; 1405ac3ed4afSGerrit Uitslag } else { 14069c42b79dSSatoshi Sahara // pagefile has modified by plugin that must be responsible for changelog 1407d5824ab9SSatoshi Sahara $filesize_old = ( 1408d5824ab9SSatoshi Sahara $data['changeType'] == DOKU_CHANGE_TYPE_CREATE || ( 1409d5824ab9SSatoshi Sahara $data['changeType'] == DOKU_CHANGE_TYPE_REVERT && !file_exists($pagefile)) 1410d5824ab9SSatoshi Sahara ) ? 0 : filesize($pagefile); 1411ac3ed4afSGerrit Uitslag } 1412d5824ab9SSatoshi Sahara 1413d5824ab9SSatoshi Sahara // make change to the current file 1414d5824ab9SSatoshi Sahara if ($data['changeType'] == DOKU_CHANGE_TYPE_DELETE) { 14159c42b79dSSatoshi Sahara // nothing to do when the file has already deleted 14169c42b79dSSatoshi Sahara if (!file_exists($pagefile)) return; 14179c42b79dSSatoshi Sahara // autoset summary on deletion 14189c42b79dSSatoshi Sahara if (blank($data['summary'])) { 14199c42b79dSSatoshi Sahara $data['summary'] = $lang['deleted']; 14209c42b79dSSatoshi Sahara } 142130725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 1422d5824ab9SSatoshi Sahara $ioData = array([$pagefile, '', false], getNS($id), noNS($id), false); 1423d5824ab9SSatoshi Sahara Event::createAndTrigger('IO_WIKIPAGE_WRITE', $ioData); 1424e45b34cdSBen Coburn // pre-save deleted revision 1425d5824ab9SSatoshi Sahara @touch($pagefile); 142646844156SBen Coburn clearstatcache(); 1427d5824ab9SSatoshi Sahara $data['newRevision'] = saveOldRevision($id); 1428e1f3d9e1SEsther Brunner // remove empty file 1429d5824ab9SSatoshi Sahara @unlink($pagefile); 1430ac3ed4afSGerrit Uitslag $filesize_new = 0; 143164159a61SAndreas Gohr // don't remove old meta info as it should be saved, plugins can use 143264159a61SAndreas Gohr // IO_WIKIPAGE_WRITE for removing their metadata... 1433c5f92742SMichael Hamann // purge non-persistant meta data 14343d1f9ec3SMichael Klier p_purge_metadata($id); 143553d6ccfeSandi // remove empty namespaces 1436cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1437cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1438f3f0262cSandi } else { 1439cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 1440d5824ab9SSatoshi Sahara io_writeWikiPage($pagefile, $data['newContent'], $id); 144146844156SBen Coburn // pre-save the revision, to keep the attic in sync 1442d5824ab9SSatoshi Sahara $data['newRevision'] = saveOldRevision($id); 1443d5824ab9SSatoshi Sahara $filesize_new = filesize($pagefile); 1444f3f0262cSandi } 1445d5824ab9SSatoshi Sahara $data['sizechange'] = $filesize_new - $filesize_old; 1446f3f0262cSandi 1447b24d9195SAndreas Gohr $event->advise_after(); 144871726d78SBen Coburn 1449c7192766SSatoshi Sahara // adds an entry to the changelog and saves the metadata for the page 145064159a61SAndreas Gohr addLogEntry( 1451d5824ab9SSatoshi Sahara $data['newRevision'], 1452d5824ab9SSatoshi Sahara $id, 1453d5824ab9SSatoshi Sahara $data['changeType'], 1454d5824ab9SSatoshi Sahara $data['summary'], 1455d5824ab9SSatoshi Sahara $data['changeInfo'], 145664159a61SAndreas Gohr null, 1457d5824ab9SSatoshi Sahara $data['sizechange'] 145864159a61SAndreas Gohr ); 1459ac3ed4afSGerrit Uitslag 146026a0801fSAndreas Gohr // send notify mails 1461d5824ab9SSatoshi Sahara notify($id, 'admin', $data['oldRevision'], $data['summary'], $minor, $data['newRevision']); 1462d5824ab9SSatoshi Sahara notify($id, 'subscribers', $data['oldRevision'], $data['summary'], $minor, $data['newRevision']); 1463f3f0262cSandi 1464ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 146598407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 14662eccbdaaSGina Haeussge 14672eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1468fe9ec250SChris Smith if (useHeading('content')) { 146907ff0babSMichael Hamann $pages = ft_backlinks($id, true); 14702eccbdaaSGina Haeussge foreach ($pages as $page) { 14710db5771eSMichael Große $cache = new CacheRenderer($page, wikiFN($page), 'xhtml'); 14722eccbdaaSGina Haeussge $cache->removeCache(); 14732eccbdaaSGina Haeussge } 14742eccbdaaSGina Haeussge } 1475f3f0262cSandi} 1476f3f0262cSandi 1477f3f0262cSandi/** 1478d5824ab9SSatoshi Sahara * moves the current version to the attic and returns its revision date 147915fae107Sandi * 148015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1481140cfbcdSGerrit Uitslag * 1482140cfbcdSGerrit Uitslag * @param string $id page id 1483140cfbcdSGerrit Uitslag * @return int|string revision timestamp 1484f3f0262cSandi */ 1485f3f0262cSandifunction saveOldRevision($id) { 1486d5824ab9SSatoshi Sahara $oldfile = wikiFN($id); 1487d5824ab9SSatoshi Sahara if (!file_exists($oldfile)) return ''; 1488d5824ab9SSatoshi Sahara $date = filemtime($oldfile); 1489d5824ab9SSatoshi Sahara $newfile = wikiFN($id, $date); 1490d5824ab9SSatoshi Sahara io_writeWikiPage($newfile, rawWiki($id), $id, $date); 1491f3f0262cSandi return $date; 1492f3f0262cSandi} 1493f3f0262cSandi 1494f3f0262cSandi/** 1495fde10de4SAdrian Lang * Sends a notify mail on page change or registration 149626a0801fSAndreas Gohr * 149726a0801fSAndreas Gohr * @param string $id The changed page 1498fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 14993272d797SAndreas Gohr * @param int|string $rev Old page revision 150026a0801fSAndreas Gohr * @param string $summary What changed 150190033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 150242ea7f44SGerrit Uitslag * @param string[] $replace Additional string substitutions, @KEY@ to be replaced by value 150383734cddSPhy * @param int|string $current_rev New page revision 15043272d797SAndreas Gohr * @return bool 1505140cfbcdSGerrit Uitslag * 150615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1507f3f0262cSandi */ 150883734cddSPhyfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array(), $current_rev = false) { 1509f3f0262cSandi global $conf; 1510585bf44eSChristopher Smith /* @var Input $INPUT */ 1511585bf44eSChristopher Smith global $INPUT; 1512b158d625SSteven Danz 15136df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 151426a0801fSAndreas Gohr if($who == 'admin') { 15153272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 15162ed38036SAndreas Gohr $tpl = 'mailtext'; 151726a0801fSAndreas Gohr $to = $conf['notify']; 151826a0801fSAndreas Gohr } elseif($who == 'subscribers') { 151984c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1520585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 15210bb37868SGerrit Uitslag $data = array('id' => $id, 'addresslist' => '', 'self' => false, 'replacements' => $replace); 1522cbb44eabSAndreas Gohr Event::createAndTrigger( 15233272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1524c8cc4053SAndreas Gohr array(new SubscriberManager(), 'notifyAddresses') 15253272d797SAndreas Gohr ); 15262ed38036SAndreas Gohr $to = $data['addresslist']; 15272ed38036SAndreas Gohr if(empty($to)) return false; 15282ed38036SAndreas Gohr $tpl = 'subscr_single'; 152926a0801fSAndreas Gohr } else { 15303272d797SAndreas Gohr return false; //just to be safe 153126a0801fSAndreas Gohr } 153226a0801fSAndreas Gohr 15336df843eeSAndreas Gohr // prepare content 1534704a815fSMichael Große $subscription = new PageSubscriptionSender(); 153583734cddSPhy return $subscription->sendPageDiff($to, $tpl, $id, $rev, $summary, $current_rev); 1536f3f0262cSandi} 15372ed38036SAndreas Gohr 153815fae107Sandi/** 153971f7bde7SAndreas Gohr * extracts the query from a search engine referrer 154015fae107Sandi * 154115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 154271f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1543140cfbcdSGerrit Uitslag * 1544140cfbcdSGerrit Uitslag * @return array|string 1545f3f0262cSandi */ 1546f3f0262cSandifunction getGoogleQuery() { 1547585bf44eSChristopher Smith /* @var Input $INPUT */ 1548585bf44eSChristopher Smith global $INPUT; 1549585bf44eSChristopher Smith 1550585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1551c66972f2SAdrian Lang return ''; 1552c66972f2SAdrian Lang } 1553585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1554f3f0262cSandi 1555079b3ac1SAndreas Gohr // only handle common SEs 1556079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1557e4d8a516SKazutaka Miyasaka 1558079b3ac1SAndreas Gohr $query = array(); 1559f3f0262cSandi parse_str($url['query'], $query); 1560e4d8a516SKazutaka Miyasaka 1561c66972f2SAdrian Lang $q = ''; 1562079b3ac1SAndreas Gohr if(isset($query['q'])){ 1563079b3ac1SAndreas Gohr $q = $query['q']; 1564079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1565079b3ac1SAndreas Gohr $q = $query['p']; 1566079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1567079b3ac1SAndreas Gohr $q = $query['query']; 1568079b3ac1SAndreas Gohr } 1569079b3ac1SAndreas Gohr $q = trim($q); 1570f3f0262cSandi 1571079b3ac1SAndreas Gohr if(!$q) return ''; 1572c7dc833bSPhy // ignore if query includes a full URL 1573c7dc833bSPhy if(strpos($q, '//') !== false) return ''; 15746531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1575f93b3b50SAndreas Gohr return $q; 1576f3f0262cSandi} 1577f3f0262cSandi 1578f3f0262cSandi/** 1579f3f0262cSandi * Return the human readable size of a file 1580f3f0262cSandi * 1581f3f0262cSandi * @param int $size A file size 1582f3f0262cSandi * @param int $dec A number of decimal places 158374160ca1SGerrit Uitslag * @return string human readable size 1584140cfbcdSGerrit Uitslag * 1585f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1586f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1587f3f0262cSandi * @version 1.0.0 1588f3f0262cSandi */ 1589f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1590f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1591f3f0262cSandi $count = count($sizes); 1592f3f0262cSandi $i = 0; 1593f3f0262cSandi 1594f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1595f3f0262cSandi $size /= 1024; 1596f3f0262cSandi $i++; 1597f3f0262cSandi } 1598f3f0262cSandi 1599ef08383eSAndreas Gohr return round($size, $dec)."\xC2\xA0".$sizes[$i]; //non-breaking space 1600f3f0262cSandi} 1601f3f0262cSandi 160215fae107Sandi/** 1603c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1604c57e365eSAndreas Gohr * 1605c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1606140cfbcdSGerrit Uitslag * 1607140cfbcdSGerrit Uitslag * @param int $dt timestamp 1608140cfbcdSGerrit Uitslag * @return string 1609c57e365eSAndreas Gohr */ 1610c57e365eSAndreas Gohrfunction datetime_h($dt) { 1611c57e365eSAndreas Gohr global $lang; 1612c57e365eSAndreas Gohr 1613c57e365eSAndreas Gohr $ago = time() - $dt; 1614c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1615c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1616c57e365eSAndreas Gohr } 1617c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1618c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1619c57e365eSAndreas Gohr } 1620c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1621c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1622c57e365eSAndreas Gohr } 1623c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1624c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1625c57e365eSAndreas Gohr } 1626c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1627c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1628c57e365eSAndreas Gohr } 1629c57e365eSAndreas Gohr if($ago > 60 * 2) { 1630c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1631c57e365eSAndreas Gohr } 1632c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1633c57e365eSAndreas Gohr} 1634c57e365eSAndreas Gohr 1635c57e365eSAndreas Gohr/** 1636f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1637f2263577SAndreas Gohr * 1638f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1639f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1640f2263577SAndreas Gohr * 1641f2263577SAndreas Gohr * @see datetime_h 1642f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1643140cfbcdSGerrit Uitslag * 1644140cfbcdSGerrit Uitslag * @param int|null $dt timestamp when given, null will take current timestamp 1645140cfbcdSGerrit Uitslag * @param string $format empty default to $conf['dformat'], or provide format as recognized by strftime() 1646140cfbcdSGerrit Uitslag * @return string 1647f2263577SAndreas Gohr */ 1648f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1649f2263577SAndreas Gohr global $conf; 1650f2263577SAndreas Gohr 1651f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1652f2263577SAndreas Gohr $dt = (int) $dt; 1653f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1654f2263577SAndreas Gohr 1655f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1656f2263577SAndreas Gohr return strftime($format, $dt); 1657f2263577SAndreas Gohr} 1658f2263577SAndreas Gohr 1659f2263577SAndreas Gohr/** 1660c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1661c4f79b71SMichael Hamann * 1662c4f79b71SMichael Hamann * @author <ungu at terong dot com> 166359752844SAnders Sandblad * @link http://php.net/manual/en/function.date.php#54072 1664140cfbcdSGerrit Uitslag * 16657e8500eeSGerrit Uitslag * @param int $int_date current date in UNIX timestamp 16663272d797SAndreas Gohr * @return string 1667c4f79b71SMichael Hamann */ 1668c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1669c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1670c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1671c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1672c4f79b71SMichael Hamann $date_mod .= $time_zone; 1673c4f79b71SMichael Hamann return $date_mod; 1674c4f79b71SMichael Hamann} 1675c4f79b71SMichael Hamann 1676c4f79b71SMichael Hamann/** 167700a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 167800a7b5adSEsther Brunner * 167900a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 168000a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 1681140cfbcdSGerrit Uitslag * 1682140cfbcdSGerrit Uitslag * @param string $email email address 1683140cfbcdSGerrit Uitslag * @return string 168400a7b5adSEsther Brunner */ 168500a7b5adSEsther Brunnerfunction obfuscate($email) { 168600a7b5adSEsther Brunner global $conf; 168700a7b5adSEsther Brunner 168800a7b5adSEsther Brunner switch($conf['mailguard']) { 168900a7b5adSEsther Brunner case 'visible' : 169000a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 169100a7b5adSEsther Brunner return strtr($email, $obfuscate); 169200a7b5adSEsther Brunner 169300a7b5adSEsther Brunner case 'hex' : 1694c1ec88ceSAndreas Gohr return \dokuwiki\Utf8\Conversion::toHtml($email, true); 169500a7b5adSEsther Brunner 169600a7b5adSEsther Brunner case 'none' : 169700a7b5adSEsther Brunner default : 169800a7b5adSEsther Brunner return $email; 169900a7b5adSEsther Brunner } 170000a7b5adSEsther Brunner} 170100a7b5adSEsther Brunner 170200a7b5adSEsther Brunner/** 170389541d4bSAndreas Gohr * Removes quoting backslashes 170489541d4bSAndreas Gohr * 170589541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1706140cfbcdSGerrit Uitslag * 1707140cfbcdSGerrit Uitslag * @param string $string 1708140cfbcdSGerrit Uitslag * @param string $char backslashed character 1709140cfbcdSGerrit Uitslag * @return string 171089541d4bSAndreas Gohr */ 171189541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 171289541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 171389541d4bSAndreas Gohr} 171489541d4bSAndreas Gohr 171573038c47SAndreas Gohr/** 171673038c47SAndreas Gohr * Convert php.ini shorthands to byte 171773038c47SAndreas Gohr * 1718a81f3d99SAndreas Gohr * On 32 bit systems values >= 2GB will fail! 1719140cfbcdSGerrit Uitslag * 1720a81f3d99SAndreas Gohr * -1 (infinite size) will be reported as -1 1721a81f3d99SAndreas Gohr * 1722a81f3d99SAndreas Gohr * @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes 1723a81f3d99SAndreas Gohr * @param string $value PHP size shorthand 1724a81f3d99SAndreas Gohr * @return int 172573038c47SAndreas Gohr */ 1726a81f3d99SAndreas Gohrfunction php_to_byte($value) { 1727f5c0c80bSAndreas Gohr switch (strtoupper(substr($value,-1))) { 172873038c47SAndreas Gohr case 'G': 1729a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024 * 1024; 173073038c47SAndreas Gohr break; 173173038c47SAndreas Gohr case 'M': 1732a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024 * 1024; 1733a81f3d99SAndreas Gohr break; 173473038c47SAndreas Gohr case 'K': 1735a81f3d99SAndreas Gohr $ret = intval(substr($value, 0, -1)) * 1024; 173673038c47SAndreas Gohr break; 17379eeeb775SAndreas Gohr default: 1738a81f3d99SAndreas Gohr $ret = intval($value); 173949cbd23eSOtto Vainio break; 174073038c47SAndreas Gohr } 174173038c47SAndreas Gohr return $ret; 174273038c47SAndreas Gohr} 174373038c47SAndreas Gohr 1744546d3a99SAndreas Gohr/** 1745546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1746140cfbcdSGerrit Uitslag * 1747140cfbcdSGerrit Uitslag * @param string $string 1748140cfbcdSGerrit Uitslag * @return string 1749546d3a99SAndreas Gohr */ 1750546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1751546d3a99SAndreas Gohr return preg_quote($string, '/'); 1752546d3a99SAndreas Gohr} 175373038c47SAndreas Gohr 1754bd2f6c2fSAndreas Gohr/** 1755bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1756bd2f6c2fSAndreas Gohr * 1757c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1758bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1759bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1760bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1761bd2f6c2fSAndreas Gohr * 1762bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1763bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1764bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1765bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1766bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 17673272d797SAndreas Gohr * @return string 1768bd2f6c2fSAndreas Gohr */ 1769a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 17708cbc5ee8SAndreas Gohr $max = $max - \dokuwiki\Utf8\PhpString::strlen($keep); 1771bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 17728cbc5ee8SAndreas Gohr $len = \dokuwiki\Utf8\PhpString::strlen($short); 1773bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1774bd2f6c2fSAndreas Gohr $half = floor($max / 2); 17756ce3e5f8SAndreas Gohr return $keep . 17766ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, 0, $half - 1) . 17776ce3e5f8SAndreas Gohr $char . 17786ce3e5f8SAndreas Gohr \dokuwiki\Utf8\PhpString::substr($short, $len - $half); 1779bd2f6c2fSAndreas Gohr} 1780bd2f6c2fSAndreas Gohr 1781dc58b6f4SAndy Webber/** 1782dc58b6f4SAndy Webber * Return the users real name or e-mail address for use 1783dc58b6f4SAndy Webber * in page footer and recent changes pages 1784dc58b6f4SAndy Webber * 1785b4b6c9a1SGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 178615f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1787c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 178815f3bc49SGerrit Uitslag * 1789dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1790dc58b6f4SAndy Webber */ 179115f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1792cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1793dc58b6f4SAndy Webber} 1794dc58b6f4SAndy Webber 179560a396c8SGerrit Uitslag/** 179660a396c8SGerrit Uitslag * Returns users realname w/o link 179760a396c8SGerrit Uitslag * 1798f168548cSGerrit Uitslag * @param string|null $username or null when currently logged-in user should be used 179915f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1800c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 180160a396c8SGerrit Uitslag * 180260a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 180360a396c8SGerrit Uitslag */ 1804cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 180560a396c8SGerrit Uitslag global $conf, $INFO; 1806e1d9dcc8SAndreas Gohr /** @var AuthPlugin $auth */ 180760a396c8SGerrit Uitslag global $auth; 180830f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 180930f6ec4bSGerrit Uitslag global $INPUT; 181060a396c8SGerrit Uitslag 181160a396c8SGerrit Uitslag // prepare initial event data 181260a396c8SGerrit Uitslag $data = array( 181360a396c8SGerrit Uitslag 'username' => $username, // the unique user name 181460a396c8SGerrit Uitslag 'name' => '', 181560a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 181660a396c8SGerrit Uitslag 'target' => '', 181760a396c8SGerrit Uitslag 'pre' => '', 181860a396c8SGerrit Uitslag 'suf' => '', 181960a396c8SGerrit Uitslag 'style' => '', 182060a396c8SGerrit Uitslag 'more' => '', 182160a396c8SGerrit Uitslag 'url' => '', 182260a396c8SGerrit Uitslag 'title' => '', 182360a396c8SGerrit Uitslag 'class' => '' 182460a396c8SGerrit Uitslag ), 18254d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 182615f3bc49SGerrit Uitslag 'textonly' => $textonly 182760a396c8SGerrit Uitslag ); 182862c8004eSGerrit Uitslag if($username === null) { 182930f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 183015f3bc49SGerrit Uitslag if($textonly){ 183115f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 183215f3bc49SGerrit Uitslag }else { 183364159a61SAndreas Gohr $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> '. 183464159a61SAndreas Gohr '(<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 183560a396c8SGerrit Uitslag } 183615f3bc49SGerrit Uitslag } 183760a396c8SGerrit Uitslag 1838e1d9dcc8SAndreas Gohr $evt = new Event('COMMON_USER_LINK', $data); 183960a396c8SGerrit Uitslag if($evt->advise_before(true)) { 184060a396c8SGerrit Uitslag if(empty($data['name'])) { 184160a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 184265833968SGerrit Uitslag if($conf['showuseras'] != 'loginname' && isset($info) && $info) { 1843dc58b6f4SAndy Webber switch($conf['showuseras']) { 1844dc58b6f4SAndy Webber case 'username': 18457f081821SGerrit Uitslag case 'username_link': 184615f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 184760a396c8SGerrit Uitslag break; 1848dc58b6f4SAndy Webber case 'email': 1849dc58b6f4SAndy Webber case 'email_link': 185060a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 185160a396c8SGerrit Uitslag break; 1852dc58b6f4SAndy Webber } 185365833968SGerrit Uitslag } else { 185465833968SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 185560a396c8SGerrit Uitslag } 185660a396c8SGerrit Uitslag } 18577f081821SGerrit Uitslag 18587f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 18597f081821SGerrit Uitslag static $xhtml_renderer = null; 18607f081821SGerrit Uitslag 186115f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 18627f081821SGerrit Uitslag 18637f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 186460a396c8SGerrit Uitslag if(!isset($info)) { 186560a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 186660a396c8SGerrit Uitslag } 186760a396c8SGerrit Uitslag if(isset($info) && $info) { 18687f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 186960a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1870dc58b6f4SAndy Webber } else { 18717f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 18727f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 18737f081821SGerrit Uitslag } 18747f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 18757f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 18767f081821SGerrit Uitslag } 18777f081821SGerrit Uitslag $shortcut = 'user'; 1878533772e1SGerrit Uitslag $exists = null; 18796496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 18802a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 18816496c33fSGerrit Uitslag if($exists !== null) { 18826496c33fSGerrit Uitslag if($exists) { 18836496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 18846496c33fSGerrit Uitslag } else { 18856496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 18866496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 18876496c33fSGerrit Uitslag } 18886496c33fSGerrit Uitslag } 1889dc58b6f4SAndy Webber } 1890dc58b6f4SAndy Webber } else { 189115f3bc49SGerrit Uitslag $data['textonly'] = true; 1892dc58b6f4SAndy Webber } 189360a396c8SGerrit Uitslag 189460a396c8SGerrit Uitslag } else { 189515f3bc49SGerrit Uitslag $data['textonly'] = true; 189660a396c8SGerrit Uitslag } 189760a396c8SGerrit Uitslag } 189860a396c8SGerrit Uitslag 189915f3bc49SGerrit Uitslag if($data['textonly']) { 19004d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 190160a396c8SGerrit Uitslag } else { 190260a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 190360a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 190460a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 190560a396c8SGerrit Uitslag } 19064d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 190760a396c8SGerrit Uitslag } 190860a396c8SGerrit Uitslag } 190960a396c8SGerrit Uitslag $evt->advise_after(); 191060a396c8SGerrit Uitslag unset($evt); 191160a396c8SGerrit Uitslag 19124d5fc927SGerrit Uitslag return $data['userlink']; 1913066fee30SAndreas Gohr} 1914066fee30SAndreas Gohr 1915066fee30SAndreas Gohr/** 1916066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1917066fee30SAndreas Gohr * When no image exists, returns an empty string 1918066fee30SAndreas Gohr * 1919066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1920140cfbcdSGerrit Uitslag * 1921066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 19223272d797SAndreas Gohr * @return string 1923066fee30SAndreas Gohr */ 1924066fee30SAndreas Gohrfunction license_img($type) { 1925066fee30SAndreas Gohr global $license; 1926066fee30SAndreas Gohr global $conf; 1927066fee30SAndreas Gohr if(!$conf['license']) return ''; 1928066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1929066fee30SAndreas Gohr $try = array(); 1930066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1931066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1932066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1933066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1934066fee30SAndreas Gohr } 1935066fee30SAndreas Gohr foreach($try as $src) { 193679e79377SAndreas Gohr if(file_exists(DOKU_INC.$src)) return $src; 1937066fee30SAndreas Gohr } 1938066fee30SAndreas Gohr return ''; 1939dc58b6f4SAndy Webber} 1940dc58b6f4SAndy Webber 194113c08e2fSMichael Klier/** 194213c08e2fSMichael Klier * Checks if the given amount of memory is available 194313c08e2fSMichael Klier * 194413c08e2fSMichael Klier * If the memory_get_usage() function is not available the 194513c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 194613c08e2fSMichael Klier * 194713c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 194813c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 19493272d797SAndreas Gohr * 19503272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 1951140cfbcdSGerrit Uitslag * @param int $bytes already allocated memory (see above) 19523272d797SAndreas Gohr * @return bool 195313c08e2fSMichael Klier */ 195413c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 195513c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 195613c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 1957985d6187SElenchus if($limit == -1) return true; // unlimited 195813c08e2fSMichael Klier 195913c08e2fSMichael Klier // parse limit to bytes 196013c08e2fSMichael Klier $limit = php_to_byte($limit); 196113c08e2fSMichael Klier 196213c08e2fSMichael Klier // get used memory if possible 196313c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 196413c08e2fSMichael Klier $used = memory_get_usage(); 196549eb6e38SAndreas Gohr } else { 196649eb6e38SAndreas Gohr $used = $bytes; 196713c08e2fSMichael Klier } 196813c08e2fSMichael Klier 196913c08e2fSMichael Klier if($used + $mem > $limit) { 197013c08e2fSMichael Klier return false; 197113c08e2fSMichael Klier } 197213c08e2fSMichael Klier 197313c08e2fSMichael Klier return true; 197413c08e2fSMichael Klier} 197513c08e2fSMichael Klier 1976af2408d5SAndreas Gohr/** 1977af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1978af2408d5SAndreas Gohr * 1979af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1980af2408d5SAndreas Gohr * 1981af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1982af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1983140cfbcdSGerrit Uitslag * 1984140cfbcdSGerrit Uitslag * @param string $url url being directed to 1985af2408d5SAndreas Gohr */ 1986af2408d5SAndreas Gohrfunction send_redirect($url) { 198798ca30d2SAndreas Gohr $url = stripctl($url); // defend against HTTP Response Splitting 198898ca30d2SAndreas Gohr 1989585bf44eSChristopher Smith /* @var Input $INPUT */ 1990585bf44eSChristopher Smith global $INPUT; 1991585bf44eSChristopher Smith 19920181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 19930181f021SAndreas Gohr global $MSG; 19940181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 19950181f021SAndreas Gohr //reopen session, store data and close session again 19960181f021SAndreas Gohr @session_start(); 19970181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 19980181f021SAndreas Gohr } 19990181f021SAndreas Gohr 2000d4869846SAndreas Gohr // always close the session 2001d4869846SAndreas Gohr session_write_close(); 2002d4869846SAndreas Gohr 2003af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 2004585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 2005585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 2006585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 20073272d797SAndreas Gohr $matches[1] < 6 20083272d797SAndreas Gohr ) { 2009af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 2010af2408d5SAndreas Gohr } else { 2011af2408d5SAndreas Gohr header('Location: '.$url); 2012af2408d5SAndreas Gohr } 201381781cb6SAndreas Gohr 2014572dc222SLarsDW223 // no exits during unit tests 201527c0c399SAndreas Gohr if(defined('DOKU_UNITTEST')) { 201627c0c399SAndreas Gohr // pass info about the redirect back to the test suite 201727c0c399SAndreas Gohr $testRequest = TestRequest::getRunning(); 201827c0c399SAndreas Gohr if($testRequest !== null) { 201927c0c399SAndreas Gohr $testRequest->addData('send_redirect', $url); 202027c0c399SAndreas Gohr } 2021572dc222SLarsDW223 return; 2022572dc222SLarsDW223 } 202327c0c399SAndreas Gohr 2024af2408d5SAndreas Gohr exit; 2025af2408d5SAndreas Gohr} 2026af2408d5SAndreas Gohr 20275b75cd1fSAdrian Lang/** 20285b75cd1fSAdrian Lang * Validate a value using a set of valid values 20295b75cd1fSAdrian Lang * 20305b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 20315b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 20325b75cd1fSAdrian Lang * default is specified, throws an exception. 20335b75cd1fSAdrian Lang * 20345b75cd1fSAdrian Lang * @param string $param The name of the parameter 20355b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 20365b75cd1fSAdrian Lang * be marked by the key “default”. 20375b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 20385b75cd1fSAdrian Lang * or $_GET) 20395b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 20405b75cd1fSAdrian Lang * 20413272d797SAndreas Gohr * @throws Exception 20423272d797SAndreas Gohr * @return mixed 20435b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 20445b75cd1fSAdrian Lang */ 20455b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 20465b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 20475b75cd1fSAdrian Lang return $array[$param]; 20485b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 20495b75cd1fSAdrian Lang return $valid_values['default']; 20505b75cd1fSAdrian Lang } else { 20515b75cd1fSAdrian Lang throw new Exception($exc); 20525b75cd1fSAdrian Lang } 20535b75cd1fSAdrian Lang} 20545b75cd1fSAdrian Lang 205563703ba5SAndreas Gohr/** 205663703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 2057646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 2058140cfbcdSGerrit Uitslag * 2059140cfbcdSGerrit Uitslag * @param string $pref preference key 2060b4b6c9a1SGerrit Uitslag * @param mixed $default value returned when preference not found 2061140cfbcdSGerrit Uitslag * @return string preference value 206263703ba5SAndreas Gohr */ 2063554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 2064646a531aSChristopher Smith $enc_pref = urlencode($pref); 206506c9ee33SMarius van Witzenburg if(isset($_COOKIE['DOKU_PREFS']) && strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 2066554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 206763703ba5SAndreas Gohr $cnt = count($parts); 20681c3eca7dSPhy 20691c3eca7dSPhy // due to #2721 there might be duplicate entries, 20701c3eca7dSPhy // so we read from the end 20711c3eca7dSPhy for($i = $cnt-2; $i >= 0; $i -= 2) { 2072646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 2073646a531aSChristopher Smith return urldecode($parts[$i + 1]); 2074554a8c9fSAdrian Lang } 2075554a8c9fSAdrian Lang } 2076554a8c9fSAdrian Lang } 2077554a8c9fSAdrian Lang return $default; 2078554a8c9fSAdrian Lang} 2079554a8c9fSAdrian Lang 20803c94d07bSAnika Henke/** 20813c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 208236ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 20833a970889SAnika Henke * Remove it by setting $val to false 2084140cfbcdSGerrit Uitslag * 2085140cfbcdSGerrit Uitslag * @param string $pref preference key 2086140cfbcdSGerrit Uitslag * @param string $val preference value 20873c94d07bSAnika Henke */ 20883c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 20893c94d07bSAnika Henke global $conf; 20903c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 20913c94d07bSAnika Henke $cookieVal = ''; 20923c94d07bSAnika Henke 20931c3eca7dSPhy if($orig !== false && ($orig !== $val)) { 20943c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 20953c94d07bSAnika Henke $cnt = count($parts); 209636ec377eSChristopher Smith // urlencode $pref for the comparison 209736ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 20981c3eca7dSPhy $seen = false; 20993c94d07bSAnika Henke for ($i = 0; $i < $cnt; $i += 2) { 210036ec377eSChristopher Smith if ($parts[$i] == $enc_pref) { 21011c3eca7dSPhy if (!$seen){ 21023a970889SAnika Henke if ($val !== false) { 210336ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 21043a970889SAnika Henke } else { 21053a970889SAnika Henke unset($parts[$i]); 21063a970889SAnika Henke unset($parts[$i + 1]); 21073a970889SAnika Henke } 21081c3eca7dSPhy $seen = true; 21091c3eca7dSPhy } else { 21101c3eca7dSPhy // no break because we want to remove duplicate entries 21111c3eca7dSPhy unset($parts[$i]); 21121c3eca7dSPhy unset($parts[$i + 1]); 21131c3eca7dSPhy } 21143c94d07bSAnika Henke } 21153c94d07bSAnika Henke } 21163c94d07bSAnika Henke $cookieVal = implode('#', $parts); 21171c3eca7dSPhy } else if ($orig === false && $val !== false) { 2118c10f256aSDamien Regad $cookieVal = (isset($_COOKIE['DOKU_PREFS']) ? $_COOKIE['DOKU_PREFS'] . '#' : '') . 211964159a61SAndreas Gohr rawurlencode($pref) . '#' . rawurlencode($val); 21203c94d07bSAnika Henke } 21213c94d07bSAnika Henke 212275e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 21235833995aSPhy if(defined('DOKU_UNITTEST')) { 21245833995aSPhy $_COOKIE['DOKU_PREFS'] = $cookieVal; 21255833995aSPhy }else{ 212675e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 21273c94d07bSAnika Henke } 21283c94d07bSAnika Henke} 21293c94d07bSAnika Henke 2130f8fb2d18SAndreas Gohr/** 2131f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 2132f8fb2d18SAndreas Gohr * 213342ea7f44SGerrit Uitslag * @param string &$text reference to the CSS or JavaScript code to clean 2134f8fb2d18SAndreas Gohr */ 2135f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 2136f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 2137f8fb2d18SAndreas Gohr} 2138f8fb2d18SAndreas Gohr 21393c27983bSAndreas Gohr/** 214071de5572SAndreas Gohr * Returns the contents of a given SVG file for embedding 21413c27983bSAndreas Gohr * 21423c27983bSAndreas Gohr * Inlining SVGs saves on HTTP requests and more importantly allows for styling them through 21433c27983bSAndreas Gohr * CSS. However it should used with small SVGs only. The $maxsize setting ensures only small 21443c27983bSAndreas Gohr * files are embedded. 21453c27983bSAndreas Gohr * 214671de5572SAndreas Gohr * This strips unneeded headers, comments and newline. The result is not a vaild standalone SVG! 214771de5572SAndreas Gohr * 21483c27983bSAndreas Gohr * @param string $file full path to the SVG file 21493c27983bSAndreas Gohr * @param int $maxsize maximum allowed size for the SVG to be embedded 215071de5572SAndreas Gohr * @return string|false the SVG content, false if the file couldn't be loaded 21513c27983bSAndreas Gohr */ 21524cd2074fSAndreas Gohrfunction inlineSVG($file, $maxsize = 2048) { 21533c27983bSAndreas Gohr $file = trim($file); 21543c27983bSAndreas Gohr if($file === '') return false; 21553c27983bSAndreas Gohr if(!file_exists($file)) return false; 21563c27983bSAndreas Gohr if(filesize($file) > $maxsize) return false; 21573c27983bSAndreas Gohr if(!is_readable($file)) return false; 21583c27983bSAndreas Gohr $content = file_get_contents($file); 21590849fa88SAndreas Gohr $content = preg_replace('/<!--.*?(-->)/s','', $content); // comments 21600849fa88SAndreas Gohr $content = preg_replace('/<\?xml .*?\?>/i', '', $content); // xml header 21610849fa88SAndreas Gohr $content = preg_replace('/<!DOCTYPE .*?>/i', '', $content); // doc type 21620849fa88SAndreas Gohr $content = preg_replace('/>\s+</s', '><', $content); // newlines between tags 21633c27983bSAndreas Gohr $content = trim($content); 21643c27983bSAndreas Gohr if(substr($content, 0, 5) !== '<svg ') return false; 216571de5572SAndreas Gohr return $content; 21663c27983bSAndreas Gohr} 21673c27983bSAndreas Gohr 2168e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 2169