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 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10f3f0262cSandi 11f3f0262cSandi/** 12b6912aeaSAndreas Gohr * These constants are used with the recents function 13b6912aeaSAndreas Gohr */ 14b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED', 2); 15b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS', 4); 16b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES', 8); 170b926329SKate Arzamastsevadefine('RECENTS_MEDIA_CHANGES', 16); 180b926329SKate Arzamastsevadefine('RECENTS_MEDIA_PAGES_MIXED', 32); 19b6912aeaSAndreas Gohr 20b6912aeaSAndreas Gohr/** 21d5197206Schris * Wrapper around htmlspecialchars() 22d5197206Schris * 23d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 24d5197206Schris * @see htmlspecialchars() 25d5197206Schris */ 26d5197206Schrisfunction hsc($string) { 27d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 28d5197206Schris} 29d5197206Schris 30d5197206Schris/** 31d5197206Schris * print a newline terminated string 32d5197206Schris * 33d5197206Schris * You can give an indention as optional parameter 34d5197206Schris * 35d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 36d5197206Schris */ 3725ec097bSChris Smithfunction ptln($string, $indent = 0) { 3825ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 3902b0b681SAndreas Gohr} 4002b0b681SAndreas Gohr 4102b0b681SAndreas Gohr/** 4202b0b681SAndreas Gohr * strips control characters (<32) from the given string 4302b0b681SAndreas Gohr * 4402b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 4502b0b681SAndreas Gohr */ 4602b0b681SAndreas Gohrfunction stripctl($string) { 4702b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 48d5197206Schris} 49d5197206Schris 50d5197206Schris/** 51634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 52634d7150SAndreas Gohr * 53634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 54634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 55634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 56634d7150SAndreas Gohr * @return string 57634d7150SAndreas Gohr */ 58634d7150SAndreas Gohrfunction getSecurityToken() { 59585bf44eSChristopher Smith /** @var Input $INPUT */ 60585bf44eSChristopher Smith global $INPUT; 61585bf44eSChristopher Smith return PassHash::hmac('md5', session_id().$INPUT->server->str('REMOTE_USER'), auth_cookiesalt()); 62634d7150SAndreas Gohr} 63634d7150SAndreas Gohr 64634d7150SAndreas Gohr/** 65634d7150SAndreas Gohr * Check the secret CSRF token 66634d7150SAndreas Gohr */ 67634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 68585bf44eSChristopher Smith /** @var Input $INPUT */ 697d01a0eaSTom N Harris global $INPUT; 70585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 71df97eaacSAndreas Gohr 727d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 73634d7150SAndreas Gohr if(getSecurityToken() != $token) { 74634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 75634d7150SAndreas Gohr return false; 76634d7150SAndreas Gohr } 77634d7150SAndreas Gohr return true; 78634d7150SAndreas Gohr} 79634d7150SAndreas Gohr 80634d7150SAndreas Gohr/** 81634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 82634d7150SAndreas Gohr * 83634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 84634d7150SAndreas Gohr */ 85634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 862404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 873272d797SAndreas Gohr if($print) echo $ret; 88634d7150SAndreas Gohr return $ret; 89634d7150SAndreas Gohr} 90634d7150SAndreas Gohr 91634d7150SAndreas Gohr/** 921015a57dSChristopher Smith * Determine basic information for a request of $id 9315fae107Sandi * 9415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 957e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 96f3f0262cSandi */ 971015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 98f3f0262cSandi global $USERINFO; 99585bf44eSChristopher Smith /* @var Input $INPUT */ 100585bf44eSChristopher Smith global $INPUT; 1016afe8dcaSchris 102c66972f2SAdrian Lang // set info about manager/admin status. 103c66972f2SAdrian Lang $info['isadmin'] = false; 104c66972f2SAdrian Lang $info['ismanager'] = false; 105585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 106f3f0262cSandi $info['userinfo'] = $USERINFO; 1071015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 108585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 10917ee7f66SAndreas Gohr 110f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 111f8cc712eSAndreas Gohr $info['isadmin'] = true; 112f8cc712eSAndreas Gohr $info['ismanager'] = true; 113f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 114f8cc712eSAndreas Gohr $info['ismanager'] = true; 115f8cc712eSAndreas Gohr } 116f8cc712eSAndreas Gohr 11717ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 11817ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 119585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 12017ee7f66SAndreas Gohr } 121ee4c4a1bSAndreas Gohr 122f3f0262cSandi } else { 1231015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 124ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 125f3f0262cSandi } 126f3f0262cSandi 1271015a57dSChristopher Smith $info['namespace'] = getNS($id); 1281015a57dSChristopher Smith 1291015a57dSChristopher Smith // mobile detection 1301015a57dSChristopher Smith if ($htmlClient) { 1311015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1321015a57dSChristopher Smith } 1331015a57dSChristopher Smith 1341015a57dSChristopher Smith return $info; 1351015a57dSChristopher Smith } 1361015a57dSChristopher Smith 1371015a57dSChristopher Smith/** 1381015a57dSChristopher Smith * Return info about the current document as associative 1391015a57dSChristopher Smith * array. 1401015a57dSChristopher Smith * 1411015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 1421015a57dSChristopher Smith */ 1431015a57dSChristopher Smithfunction pageinfo() { 1441015a57dSChristopher Smith global $ID; 1451015a57dSChristopher Smith global $REV; 1461015a57dSChristopher Smith global $RANGE; 1471015a57dSChristopher Smith global $lang; 148585bf44eSChristopher Smith /* @var Input $INPUT */ 149585bf44eSChristopher Smith global $INPUT; 1501015a57dSChristopher Smith 1511015a57dSChristopher Smith $info = basicinfo($ID); 1521015a57dSChristopher Smith 1531015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 1541015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 1551015a57dSChristopher Smith $info['id'] = $ID; 1561015a57dSChristopher Smith $info['rev'] = $REV; 1571015a57dSChristopher Smith 158585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 1597e87a794SChristopher Smith $sub = new Subscription(); 1607e87a794SChristopher Smith $info['subscribed'] = $sub->user_subscription(); 1617e87a794SChristopher Smith } else { 1627e87a794SChristopher Smith $info['subscribed'] = false; 1637e87a794SChristopher Smith } 1647e87a794SChristopher Smith 165f3f0262cSandi $info['locked'] = checklock($ID); 16600976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID)); 1672ca9d91cSBen Coburn $info['exists'] = @file_exists($info['filepath']); 16801c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 1692ca9d91cSBen Coburn if($REV) { 1702ca9d91cSBen Coburn //check if current revision was meant 17101c9a118SAndreas Gohr if($info['exists'] && ($info['currentrev'] == $REV)) { 1722ca9d91cSBen Coburn $REV = ''; 1737b3a6803SAndreas Gohr } elseif($RANGE) { 1747b3a6803SAndreas Gohr //section editing does not work with old revisions! 1757b3a6803SAndreas Gohr $REV = ''; 1767b3a6803SAndreas Gohr $RANGE = ''; 1777b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 1782ca9d91cSBen Coburn } else { 1792ca9d91cSBen Coburn //really use old revision 18000976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID, $REV)); 181f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 182f3f0262cSandi } 183f3f0262cSandi } 184c112d578Sandi $info['rev'] = $REV; 185f3f0262cSandi if($info['exists']) { 186f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 187f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 188f3f0262cSandi } else { 189f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 190f3f0262cSandi } 19150e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 192f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 193f3f0262cSandi 19471726d78SBen Coburn //load page meta data 19571726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 19671726d78SBen Coburn 197652610a2Sandi //who's the editor 198652610a2Sandi if($REV) { 19971726d78SBen Coburn $revinfo = getRevisionInfo($ID, $REV, 1024); 200652610a2Sandi } else { 2010e80bb5eSChristopher Smith if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 202aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 203aa27cf05SAndreas Gohr } else { 204cd00a034SBen Coburn $revinfo = getRevisionInfo($ID, $info['lastmod'], 1024); 205cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 206cd00a034SBen Coburn if($revinfo !== false) { 207cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 208cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 209cd00a034SBen Coburn } 210cd00a034SBen Coburn } 211cd00a034SBen Coburn } 212cd00a034SBen Coburn //and check for an external edit 213cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 214cd00a034SBen Coburn // cached changelog line no longer valid 215cd00a034SBen Coburn $revinfo = false; 216cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 217cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 218652610a2Sandi } 219bb4866bdSchris 220652610a2Sandi $info['ip'] = $revinfo['ip']; 221652610a2Sandi $info['user'] = $revinfo['user']; 222652610a2Sandi $info['sum'] = $revinfo['sum']; 22371726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 224ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 22559f257aeSchris 22688f522e9Sandi if($revinfo['user']) { 22788f522e9Sandi $info['editor'] = $revinfo['user']; 22888f522e9Sandi } else { 22988f522e9Sandi $info['editor'] = $revinfo['ip']; 23088f522e9Sandi } 231652610a2Sandi 232ee4c4a1bSAndreas Gohr // draft 233ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID, '.draft'); 234ee4c4a1bSAndreas Gohr if(@file_exists($draft)) { 235ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))) { 236ee4c4a1bSAndreas Gohr // remove stale draft 237ee4c4a1bSAndreas Gohr @unlink($draft); 238ee4c4a1bSAndreas Gohr } else { 239ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 240ee4c4a1bSAndreas Gohr } 241ee4c4a1bSAndreas Gohr } 242ee4c4a1bSAndreas Gohr 2431015a57dSChristopher Smith return $info; 2441015a57dSChristopher Smith} 2451015a57dSChristopher Smith 2461015a57dSChristopher Smith/** 2471015a57dSChristopher Smith * Return information about the current media item as an associative array. 2481015a57dSChristopher Smith */ 2491015a57dSChristopher Smithfunction mediainfo(){ 2501015a57dSChristopher Smith global $NS; 2511015a57dSChristopher Smith global $IMG; 2521015a57dSChristopher Smith 2531015a57dSChristopher Smith $info = basicinfo("$NS:*"); 2541015a57dSChristopher Smith $info['image'] = $IMG; 2551c548ebeSAndreas Gohr 256f3f0262cSandi return $info; 257f3f0262cSandi} 258f3f0262cSandi 259f3f0262cSandi/** 2602684e50aSAndreas Gohr * Build an string of URL parameters 2612684e50aSAndreas Gohr * 2622684e50aSAndreas Gohr * @author Andreas Gohr 2632684e50aSAndreas Gohr */ 264b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 2652684e50aSAndreas Gohr $url = ''; 2662684e50aSAndreas Gohr $amp = false; 2672684e50aSAndreas Gohr foreach($params as $key => $val) { 268b174aeaeSchris if($amp) $url .= $sep; 2692684e50aSAndreas Gohr 27085e6871fSAdrian Lang $url .= rawurlencode($key).'='; 2713a50618cSgweissbach $url .= rawurlencode((string) $val); 2722684e50aSAndreas Gohr $amp = true; 2732684e50aSAndreas Gohr } 2742684e50aSAndreas Gohr return $url; 2752684e50aSAndreas Gohr} 2762684e50aSAndreas Gohr 2772684e50aSAndreas Gohr/** 2782684e50aSAndreas Gohr * Build an string of html tag attributes 2792684e50aSAndreas Gohr * 2807bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 2817bff22c0SAndreas Gohr * 2822684e50aSAndreas Gohr * @author Andreas Gohr 2832684e50aSAndreas Gohr */ 2844b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 2852684e50aSAndreas Gohr $url = ''; 2869063ec14SAdrian Lang $white = false; 2872684e50aSAndreas Gohr foreach($params as $key => $val) { 2887bff22c0SAndreas Gohr if($key{0} == '_') continue; 289b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 2909063ec14SAdrian Lang if($white) $url .= ' '; 2917bff22c0SAndreas Gohr 2922684e50aSAndreas Gohr $url .= $key.'="'; 2932684e50aSAndreas Gohr $url .= htmlspecialchars($val); 2942684e50aSAndreas Gohr $url .= '"'; 2959063ec14SAdrian Lang $white = true; 2962684e50aSAndreas Gohr } 2972684e50aSAndreas Gohr return $url; 2982684e50aSAndreas Gohr} 2992684e50aSAndreas Gohr 3002684e50aSAndreas Gohr/** 30115fae107Sandi * This builds the breadcrumb trail and returns it as array 30215fae107Sandi * 30315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 304f3f0262cSandi */ 305f3f0262cSandifunction breadcrumbs() { 3068746e727Sandi // we prepare the breadcrumbs early for quick session closing 3078746e727Sandi static $crumbs = null; 3088746e727Sandi if($crumbs != null) return $crumbs; 3098746e727Sandi 310f3f0262cSandi global $ID; 311f3f0262cSandi global $ACT; 312f3f0262cSandi global $conf; 313f3f0262cSandi 314f3f0262cSandi //first visit? 315c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 316f3f0262cSandi //we only save on show and existing wiki documents 317a77f5846Sjan $file = wikiFN($ID); 318a77f5846Sjan if($ACT != 'show' || !@file_exists($file)) { 319e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 320f3f0262cSandi return $crumbs; 321f3f0262cSandi } 322a77f5846Sjan 323a77f5846Sjan // page names 3241a84a0f3SAnika Henke $name = noNSorNS($ID); 325fe9ec250SChris Smith if(useHeading('navigation')) { 326a77f5846Sjan // get page title 32767c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 328a77f5846Sjan if($title) { 329a77f5846Sjan $name = $title; 330a77f5846Sjan } 331a77f5846Sjan } 332a77f5846Sjan 333f3f0262cSandi //remove ID from array 334a77f5846Sjan if(isset($crumbs[$ID])) { 335a77f5846Sjan unset($crumbs[$ID]); 336f3f0262cSandi } 337f3f0262cSandi 338f3f0262cSandi //add to array 339a77f5846Sjan $crumbs[$ID] = $name; 340f3f0262cSandi //reduce size 341f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 342f3f0262cSandi array_shift($crumbs); 343f3f0262cSandi } 344f3f0262cSandi //save to session 345e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 346f3f0262cSandi return $crumbs; 347f3f0262cSandi} 348f3f0262cSandi 349f3f0262cSandi/** 35015fae107Sandi * Filter for page IDs 35115fae107Sandi * 352f3f0262cSandi * This is run on a ID before it is outputted somewhere 353f3f0262cSandi * currently used to replace the colon with something else 354907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 355907f24f7SAndreas Gohr * 356907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 357907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 358907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 35915fae107Sandi * 36049c713a3Sandi * Urlencoding is ommitted when the second parameter is false 36149c713a3Sandi * 36215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 363f3f0262cSandi */ 36449c713a3Sandifunction idfilter($id, $ue = true) { 365f3f0262cSandi global $conf; 366585bf44eSChristopher Smith /* @var Input $INPUT */ 367585bf44eSChristopher Smith global $INPUT; 368585bf44eSChristopher Smith 369f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 370f3f0262cSandi $id = strtr($id, ':', '/'); 371f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 37258bedc8aSborekb $conf['userewrite'] && 373585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 3743272d797SAndreas Gohr ) { 375f3f0262cSandi $id = strtr($id, ':', ';'); 376f3f0262cSandi } 37749c713a3Sandi if($ue) { 378b6c6979fSAndreas Gohr $id = rawurlencode($id); 379f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 380f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 38149c713a3Sandi } 382f3f0262cSandi return $id; 383f3f0262cSandi} 384f3f0262cSandi 385f3f0262cSandi/** 386ed7b5f09Sandi * This builds a link to a wikipage 38715fae107Sandi * 3886c7843b5Sandi * It handles URL rewriting and adds additional parameter if 3896c7843b5Sandi * given in $more 3906c7843b5Sandi * 39115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 392f3f0262cSandi */ 39316f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 394f3f0262cSandi global $conf; 39516f15a81SDominik Eckelmann if(is_array($urlParameters)) { 39616f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 3976de3759aSAndreas Gohr } else { 39816f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 3996de3759aSAndreas Gohr } 40016f15a81SDominik Eckelmann if($id === '') { 40116f15a81SDominik Eckelmann $id = $conf['start']; 40216f15a81SDominik Eckelmann } 403f3f0262cSandi $id = idfilter($id); 40416f15a81SDominik Eckelmann if($absolute) { 405ed7b5f09Sandi $xlink = DOKU_URL; 406ed7b5f09Sandi } else { 407ed7b5f09Sandi $xlink = DOKU_BASE; 408ed7b5f09Sandi } 409f3f0262cSandi 4106c7843b5Sandi if($conf['userewrite'] == 2) { 4116c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 41216f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 4136c7843b5Sandi } elseif($conf['userewrite']) { 414f3f0262cSandi $xlink .= $id; 41516f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 416bce3726dSAndreas Gohr } elseif($id) { 4176c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 41816f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 419bce3726dSAndreas Gohr } else { 420bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 42116f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 422f3f0262cSandi } 423f3f0262cSandi 424f3f0262cSandi return $xlink; 425f3f0262cSandi} 426f3f0262cSandi 427f3f0262cSandi/** 428f5c2808fSBen Coburn * This builds a link to an alternate page format 429f5c2808fSBen Coburn * 430f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 431f5c2808fSBen Coburn * 432f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 433f5c2808fSBen Coburn */ 434f5c2808fSBen Coburnfunction exportlink($id = '', $format = 'raw', $more = '', $abs = false, $sep = '&') { 435f5c2808fSBen Coburn global $conf; 436f5c2808fSBen Coburn if(is_array($more)) { 437f5c2808fSBen Coburn $more = buildURLparams($more, $sep); 438f5c2808fSBen Coburn } else { 439f5c2808fSBen Coburn $more = str_replace(',', $sep, $more); 440f5c2808fSBen Coburn } 441f5c2808fSBen Coburn 442f5c2808fSBen Coburn $format = rawurlencode($format); 443f5c2808fSBen Coburn $id = idfilter($id); 444f5c2808fSBen Coburn if($abs) { 445f5c2808fSBen Coburn $xlink = DOKU_URL; 446f5c2808fSBen Coburn } else { 447f5c2808fSBen Coburn $xlink = DOKU_BASE; 448f5c2808fSBen Coburn } 449f5c2808fSBen Coburn 450f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 451f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 452f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 453f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 454f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 455f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 456f5c2808fSBen Coburn } else { 457f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 458f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 459f5c2808fSBen Coburn } 460f5c2808fSBen Coburn 461f5c2808fSBen Coburn return $xlink; 462f5c2808fSBen Coburn} 463f5c2808fSBen Coburn 464f5c2808fSBen Coburn/** 4656de3759aSAndreas Gohr * Build a link to a media file 4666de3759aSAndreas Gohr * 4676de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 4688c08db0aSAndreas Gohr * 4698c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 4708c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 4718c08db0aSAndreas Gohr * 4723272d797SAndreas Gohr * @param string $id the media file id or URL 4733272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 4743272d797SAndreas Gohr * @param bool $direct link to detail page if false 4753272d797SAndreas Gohr * @param string $sep URL parameter separator 4763272d797SAndreas Gohr * @param bool $abs Create an absolute URL 4773272d797SAndreas Gohr * @return string 4786de3759aSAndreas Gohr */ 47955b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 4806de3759aSAndreas Gohr global $conf; 481b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 482826d2766SKlap-in if(!$isexternalimage) { 483826d2766SKlap-in $id = cleanID($id); 484826d2766SKlap-in } 485826d2766SKlap-in 4866de3759aSAndreas Gohr if(is_array($more)) { 4870f4e0092SChristopher Smith // add token for resized images 488443e135dSChristopher Smith if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){ 4890f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 4900f4e0092SChristopher Smith } 4918c08db0aSAndreas Gohr // strip defaults for shorter URLs 4928c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 493443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 494443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 4958c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 496b174aeaeSchris $more = buildURLparams($more, $sep); 4976de3759aSAndreas Gohr } else { 4985e7db1e2SChristopher Smith $matches = array(); 499cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 5005e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 5015e7db1e2SChristopher Smith foreach ($matches as $match){ 5025e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 5035e7db1e2SChristopher Smith } 504cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 505cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 5065e7db1e2SChristopher Smith } 5078c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 5088c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 509b174aeaeSchris $more = str_replace(',', $sep, $more); 5106de3759aSAndreas Gohr } 5116de3759aSAndreas Gohr 51255b2b31bSAndreas Gohr if($abs) { 51355b2b31bSAndreas Gohr $xlink = DOKU_URL; 51455b2b31bSAndreas Gohr } else { 5156de3759aSAndreas Gohr $xlink = DOKU_BASE; 51655b2b31bSAndreas Gohr } 5176de3759aSAndreas Gohr 5186de3759aSAndreas Gohr // external URLs are always direct without rewriting 519826d2766SKlap-in if($isexternalimage) { 5206de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 521cc036f74SKlap-in $xlink .= '?'.$more; 522b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 5236de3759aSAndreas Gohr return $xlink; 5246de3759aSAndreas Gohr } 5256de3759aSAndreas Gohr 5266de3759aSAndreas Gohr $id = idfilter($id); 5276de3759aSAndreas Gohr 5286de3759aSAndreas Gohr // decide on scriptname 5296de3759aSAndreas Gohr if($direct) { 5306de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 5316de3759aSAndreas Gohr $script = '_media'; 5326de3759aSAndreas Gohr } else { 5336de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 5346de3759aSAndreas Gohr } 5356de3759aSAndreas Gohr } else { 5366de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 5376de3759aSAndreas Gohr $script = '_detail'; 5386de3759aSAndreas Gohr } else { 5396de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 5406de3759aSAndreas Gohr } 5416de3759aSAndreas Gohr } 5426de3759aSAndreas Gohr 5436de3759aSAndreas Gohr // build URL based on rewrite mode 5446de3759aSAndreas Gohr if($conf['userewrite']) { 5456de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 5466de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 5476de3759aSAndreas Gohr } else { 5486de3759aSAndreas Gohr if($more) { 549a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 550b174aeaeSchris $xlink .= $sep.'media='.$id; 5516de3759aSAndreas Gohr } else { 552a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 5536de3759aSAndreas Gohr } 5546de3759aSAndreas Gohr } 5556de3759aSAndreas Gohr 5566de3759aSAndreas Gohr return $xlink; 5576de3759aSAndreas Gohr} 5586de3759aSAndreas Gohr 5596de3759aSAndreas Gohr/** 56025ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 56115fae107Sandi * 56225ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 56325ca5b17SAndreas Gohr * 56415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 565f3f0262cSandi */ 56625ca5b17SAndreas Gohrfunction script() { 567ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 568f3f0262cSandi} 569f3f0262cSandi 570f3f0262cSandi/** 57115fae107Sandi * Spamcheck against wordlist 57215fae107Sandi * 573f3f0262cSandi * Checks the wikitext against a list of blocked expressions 574f3f0262cSandi * returns true if the text contains any bad words 57515fae107Sandi * 576e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 577e403cc58SMichael Klier * 578e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 579e403cc58SMichael Klier * and gain information about the user who was blocked. 580e403cc58SMichael Klier * 581e403cc58SMichael Klier * Event data: 582e403cc58SMichael Klier * data['matches'] - array of matches 583e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 584e403cc58SMichael Klier * [ip] - ip address 585e403cc58SMichael Klier * [user] - username (if logged in) 586e403cc58SMichael Klier * [mail] - mail address (if logged in) 587e403cc58SMichael Klier * [name] - real name (if logged in) 588e403cc58SMichael Klier * 58915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5906dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 5916dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 5926dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 593f3f0262cSandi */ 5946dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 595f3f0262cSandi global $TEXT; 5966dffa0e0SAndreas Gohr global $PRE; 5976dffa0e0SAndreas Gohr global $SUF; 598e0086ca2SAndreas Gohr global $SUM; 599f3f0262cSandi global $conf; 600e403cc58SMichael Klier global $INFO; 601585bf44eSChristopher Smith /* @var Input $INPUT */ 602585bf44eSChristopher Smith global $INPUT; 603f3f0262cSandi 604f3f0262cSandi if(!$conf['usewordblock']) return false; 605f3f0262cSandi 606e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 6076dffa0e0SAndreas Gohr 608041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 6096dffa0e0SAndreas Gohr $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', '\1http://\2 \2\3', $text); 610041d1964SAndreas Gohr 611b9ac8716Schris $wordblocks = getWordblocks(); 6123e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 6133e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 6143e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 6153e2965d7Sandi // backreferences are used - the maximum is 99 6163e2965d7Sandi // this is very bad performancewise and may even be too high still 6173e2965d7Sandi $chunksize = 40; 6183e2965d7Sandi } else { 619a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 6203e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 621a51d08efSAndreas Gohr $chunksize = 200; 6223e2965d7Sandi } 623b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 624f3f0262cSandi $re = array(); 62549eb6e38SAndreas Gohr // build regexp from blocks 626f3f0262cSandi foreach($blocks as $block) { 627f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 628f3f0262cSandi $block = trim($block); 629f3f0262cSandi if(empty($block)) continue; 630f3f0262cSandi $re[] = $block; 631f3f0262cSandi } 632e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 633e403cc58SMichael Klier // prepare event data 634e403cc58SMichael Klier $data['matches'] = $matches; 635585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 636585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 637585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 638e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 639e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 640e403cc58SMichael Klier } 641e403cc58SMichael Klier $callback = create_function('', 'return true;'); 642e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 643b9ac8716Schris } 644703f6fdeSandi } 645f3f0262cSandi return false; 646f3f0262cSandi} 647f3f0262cSandi 648f3f0262cSandi/** 64915fae107Sandi * Return the IP of the client 65015fae107Sandi * 6516d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 65215fae107Sandi * 6536d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 6546d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 6556d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 6566d8affe6SAndreas Gohr * headers 6576d8affe6SAndreas Gohr * 65815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6593272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 6603272d797SAndreas Gohr * @return string 661f3f0262cSandi */ 6626d8affe6SAndreas Gohrfunction clientIP($single = false) { 663585bf44eSChristopher Smith /* @var Input $INPUT */ 664585bf44eSChristopher Smith global $INPUT; 665585bf44eSChristopher Smith 6666d8affe6SAndreas Gohr $ip = array(); 667585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 668585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 669585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 670585bf44eSChristopher Smith } 671585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 672585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 673585bf44eSChristopher Smith } 6746d8affe6SAndreas Gohr 675dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 676dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 677dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 678dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 679dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 680dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 681dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 682dc14c6d1SGuy Brand $IPv6Address = 683dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 684dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 685dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 686dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 687dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 688dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 689dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 690dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 691dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 692dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 693dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 694dc14c6d1SGuy Brand 6956d8affe6SAndreas Gohr // remove any non-IP stuff 6966d8affe6SAndreas Gohr $cnt = count($ip); 6974ff28443Schris $match = array(); 6986d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 699dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 7004ff28443Schris $ip[$i] = $match[0]; 7014ff28443Schris } else { 7024ff28443Schris $ip[$i] = ''; 7034ff28443Schris } 7046d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 705f3f0262cSandi } 7066d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 7076d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 7086d8affe6SAndreas Gohr 7096d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 7106d8affe6SAndreas Gohr 7116d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 7126d8affe6SAndreas Gohr $ip = array_reverse($ip); 7136d8affe6SAndreas Gohr foreach($ip as $i) { 7142343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 7156d8affe6SAndreas Gohr continue; 7166d8affe6SAndreas Gohr } else { 7176d8affe6SAndreas Gohr return $i; 7186d8affe6SAndreas Gohr } 7196d8affe6SAndreas Gohr } 7206d8affe6SAndreas Gohr // still here? just use the first (last) address 7216d8affe6SAndreas Gohr return $ip[0]; 722f3f0262cSandi} 723f3f0262cSandi 724f3f0262cSandi/** 7251c548ebeSAndreas Gohr * Check if the browser is on a mobile device 7261c548ebeSAndreas Gohr * 7271c548ebeSAndreas Gohr * Adapted from the example code at url below 7281c548ebeSAndreas Gohr * 7291c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 7301c548ebeSAndreas Gohr */ 7311c548ebeSAndreas Gohrfunction clientismobile() { 732585bf44eSChristopher Smith /* @var Input $INPUT */ 733585bf44eSChristopher Smith global $INPUT; 7341c548ebeSAndreas Gohr 735585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 7361c548ebeSAndreas Gohr 737585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 7381c548ebeSAndreas Gohr 739585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 7401c548ebeSAndreas Gohr 7411c548ebeSAndreas Gohr $uamatches = 'midp|j2me|avantg|docomo|novarra|palmos|palmsource|240x320|opwv|chtml|pda|windows ce|mmp\/|blackberry|mib\/|symbian|wireless|nokia|hand|mobi|phone|cdm|up\.b|audio|SIE\-|SEC\-|samsung|HTC|mot\-|mitsu|sagem|sony|alcatel|lg|erics|vx|NEC|philips|mmm|xx|panasonic|sharp|wap|sch|rover|pocket|benq|java|pt|pg|vox|amoi|bird|compal|kg|voda|sany|kdd|dbt|sendo|sgh|gradi|jb|\d\d\di|moto'; 7421c548ebeSAndreas Gohr 743585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 7441c548ebeSAndreas Gohr 7451c548ebeSAndreas Gohr return false; 7461c548ebeSAndreas Gohr} 7471c548ebeSAndreas Gohr 7481c548ebeSAndreas Gohr/** 74963211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 75063211f61SGlen Harris * 75122ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 75222ef1e32SAndreas Gohr * 75363211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 7543272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 7553272d797SAndreas Gohr * @return string a comma separated list of hostnames 75663211f61SGlen Harris */ 75763211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 75822ef1e32SAndreas Gohr global $conf; 75922ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 76022ef1e32SAndreas Gohr 76163211f61SGlen Harris $hosts = array(); 76263211f61SGlen Harris $ips = explode(',', $ips); 763551a720fSMichael Klier 764551a720fSMichael Klier if(is_array($ips)) { 7653886270dSAndreas Gohr foreach($ips as $ip) { 766551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 76763211f61SGlen Harris } 768551a720fSMichael Klier return join(',', $hosts); 769551a720fSMichael Klier } else { 770551a720fSMichael Klier return gethostbyaddr(trim($ips)); 771551a720fSMichael Klier } 77263211f61SGlen Harris} 77363211f61SGlen Harris 77463211f61SGlen Harris/** 77515fae107Sandi * Checks if a given page is currently locked. 77615fae107Sandi * 777f3f0262cSandi * removes stale lockfiles 77815fae107Sandi * 77915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 780f3f0262cSandi */ 781f3f0262cSandifunction checklock($id) { 782f3f0262cSandi global $conf; 783585bf44eSChristopher Smith /* @var Input $INPUT */ 784585bf44eSChristopher Smith global $INPUT; 785585bf44eSChristopher Smith 786c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 787f3f0262cSandi 788f3f0262cSandi //no lockfile 789f3f0262cSandi if(!@file_exists($lock)) return false; 790f3f0262cSandi 791f3f0262cSandi //lockfile expired 792f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 793d8186216SBen Coburn @unlink($lock); 794f3f0262cSandi return false; 795f3f0262cSandi } 796f3f0262cSandi 797f3f0262cSandi //my own lock 7986d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 799*0712fefaSAndreas Gohr if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || (session_id() && $session == session_id())) { 800f3f0262cSandi return false; 801f3f0262cSandi } 802f3f0262cSandi 803f3f0262cSandi return $ip; 804f3f0262cSandi} 805f3f0262cSandi 806f3f0262cSandi/** 80715fae107Sandi * Lock a page for editing 80815fae107Sandi * 80915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 810f3f0262cSandi */ 811f3f0262cSandifunction lock($id) { 812544ed901SDaniel Calviño Sánchez global $conf; 813585bf44eSChristopher Smith /* @var Input $INPUT */ 814585bf44eSChristopher Smith global $INPUT; 815544ed901SDaniel Calviño Sánchez 816544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 817544ed901SDaniel Calviño Sánchez return; 818544ed901SDaniel Calviño Sánchez } 819544ed901SDaniel Calviño Sánchez 820c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 821585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 822585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 823f3f0262cSandi } else { 82485fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 825f3f0262cSandi } 826f3f0262cSandi} 827f3f0262cSandi 828f3f0262cSandi/** 82915fae107Sandi * Unlock a page if it was locked by the user 830f3f0262cSandi * 83115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 8323272d797SAndreas Gohr * @param string $id page id to unlock 83315fae107Sandi * @return bool true if a lock was removed 834f3f0262cSandi */ 835f3f0262cSandifunction unlock($id) { 836585bf44eSChristopher Smith /* @var Input $INPUT */ 837585bf44eSChristopher Smith global $INPUT; 838585bf44eSChristopher Smith 839c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 840f3f0262cSandi if(@file_exists($lock)) { 8416d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 842585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 843f3f0262cSandi @unlink($lock); 844f3f0262cSandi return true; 845f3f0262cSandi } 846f3f0262cSandi } 847f3f0262cSandi return false; 848f3f0262cSandi} 849f3f0262cSandi 850f3f0262cSandi/** 851f3f0262cSandi * convert line ending to unix format 852f3f0262cSandi * 8536db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 8546db7468bSAndreas Gohr * 85515fae107Sandi * @see formText() for 2crlf conversion 85615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 857f3f0262cSandi */ 858f3f0262cSandifunction cleanText($text) { 859f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 8606db7468bSAndreas Gohr 8616db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 8626db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 8636db7468bSAndreas Gohr // but might actually fix the problem in many cases 8646db7468bSAndreas Gohr if(!utf8_check($text)) $text = utf8_encode($text); 8656db7468bSAndreas Gohr 866f3f0262cSandi return $text; 867f3f0262cSandi} 868f3f0262cSandi 869f3f0262cSandi/** 870f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 871f3f0262cSandi * It also converts line endings to Windows format which is 872f3f0262cSandi * pseudo standard for webforms. 873f3f0262cSandi * 87415fae107Sandi * @see cleanText() for 2unix conversion 87515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 876f3f0262cSandi */ 877f3f0262cSandifunction formText($text) { 8785b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 879f3f0262cSandi return htmlspecialchars($text); 880f3f0262cSandi} 881f3f0262cSandi 882f3f0262cSandi/** 88315fae107Sandi * Returns the specified local text in raw format 88415fae107Sandi * 88515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 886f3f0262cSandi */ 8872adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 8882adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 889f3f0262cSandi} 890f3f0262cSandi 891f3f0262cSandi/** 892f3f0262cSandi * Returns the raw WikiText 89315fae107Sandi * 89415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 895f3f0262cSandi */ 896f3f0262cSandifunction rawWiki($id, $rev = '') { 897cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 898f3f0262cSandi} 899f3f0262cSandi 900f3f0262cSandi/** 9017146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 9027146cee2SAndreas Gohr * 9037b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 9047146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9057146cee2SAndreas Gohr */ 906fe17917eSAdrian Langfunction pageTemplate($id) { 907a15ce62dSEsther Brunner global $conf; 908e29549feSAndreas Gohr 909fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 910e29549feSAndreas Gohr 9117b84afa2SAndreas Gohr // prepare initial event data 9127b84afa2SAndreas Gohr $data = array( 9137b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 9147b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 9157b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 9167b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 9177b84afa2SAndreas Gohr ); 9187b84afa2SAndreas Gohr 9197b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 9207b84afa2SAndreas Gohr if($evt->advise_before(true)) { 9217b84afa2SAndreas Gohr // the before event might have loaded the content already 9227b84afa2SAndreas Gohr if(empty($data['tpl'])) { 9237b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 9247b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 925fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 926e29549feSAndreas Gohr if(@file_exists($path.'/_template.txt')) { 9277b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 928e29549feSAndreas Gohr } else { 929e29549feSAndreas Gohr // search upper namespaces for templates 930e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 931e29549feSAndreas Gohr while(strlen($path) >= $len) { 932e29549feSAndreas Gohr if(@file_exists($path.'/__template.txt')) { 9337b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 934e29549feSAndreas Gohr break; 935e29549feSAndreas Gohr } 936e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 937e29549feSAndreas Gohr } 938e29549feSAndreas Gohr } 9397b84afa2SAndreas Gohr } 9407b84afa2SAndreas Gohr // load the content 9413d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 9427b84afa2SAndreas Gohr } 943a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 9447b84afa2SAndreas Gohr } 9457b84afa2SAndreas Gohr $evt->advise_after(); 9467b84afa2SAndreas Gohr unset($evt); 9477b84afa2SAndreas Gohr 948fe17917eSAdrian Lang return $data['tpl']; 9492b1223ecSAdrian Lang} 9502b1223ecSAdrian Lang 9512b1223ecSAdrian Lang/** 9522b1223ecSAdrian Lang * Performs common page template replacements 9537b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 9542b1223ecSAdrian Lang * 9552b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 9562b1223ecSAdrian Lang */ 957d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 9583272d797SAndreas Gohr /** 9593272d797SAndreas Gohr * @var string $id the id of the page to be created 9603272d797SAndreas Gohr * @var string $tpl the text used as template 9613272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 9623272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 9633272d797SAndreas Gohr */ 964fe17917eSAdrian Lang extract($data); 965fe17917eSAdrian Lang 966b856f7dfSAdrian Lang global $USERINFO; 967bce53b1fSAdrian Lang global $conf; 968585bf44eSChristopher Smith /* @var Input $INPUT */ 969585bf44eSChristopher Smith global $INPUT; 970e29549feSAndreas Gohr 971e29549feSAndreas Gohr // replace placeholders 97226ece5a7SAndreas Gohr $file = noNS($id); 97337c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 97426ece5a7SAndreas Gohr 9753272d797SAndreas Gohr $tpl = str_replace( 9763272d797SAndreas Gohr array( 97726ece5a7SAndreas Gohr '@ID@', 97826ece5a7SAndreas Gohr '@NS@', 97926ece5a7SAndreas Gohr '@FILE@', 98026ece5a7SAndreas Gohr '@!FILE@', 98126ece5a7SAndreas Gohr '@!FILE!@', 98226ece5a7SAndreas Gohr '@PAGE@', 98326ece5a7SAndreas Gohr '@!PAGE@', 98426ece5a7SAndreas Gohr '@!!PAGE@', 98526ece5a7SAndreas Gohr '@!PAGE!@', 98626ece5a7SAndreas Gohr '@USER@', 98726ece5a7SAndreas Gohr '@NAME@', 98826ece5a7SAndreas Gohr '@MAIL@', 98926ece5a7SAndreas Gohr '@DATE@', 99026ece5a7SAndreas Gohr ), 99126ece5a7SAndreas Gohr array( 99226ece5a7SAndreas Gohr $id, 99326ece5a7SAndreas Gohr getNS($id), 99426ece5a7SAndreas Gohr $file, 99526ece5a7SAndreas Gohr utf8_ucfirst($file), 99626ece5a7SAndreas Gohr utf8_strtoupper($file), 99726ece5a7SAndreas Gohr $page, 99826ece5a7SAndreas Gohr utf8_ucfirst($page), 99926ece5a7SAndreas Gohr utf8_ucwords($page), 100026ece5a7SAndreas Gohr utf8_strtoupper($page), 1001585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 1002b856f7dfSAdrian Lang $USERINFO['name'], 1003b856f7dfSAdrian Lang $USERINFO['mail'], 100426ece5a7SAndreas Gohr $conf['dformat'], 10053272d797SAndreas Gohr ), $tpl 10063272d797SAndreas Gohr ); 100726ece5a7SAndreas Gohr 10087d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 10097d644fc8SAndreas Gohr $tpl = preg_replace_callback('/%./', create_function('$m', 'return strftime($m[0]);'), $tpl); 1010d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1011a15ce62dSEsther Brunner return $tpl; 10127146cee2SAndreas Gohr} 10137146cee2SAndreas Gohr 10147146cee2SAndreas Gohr/** 101515fae107Sandi * Returns the raw Wiki Text in three slices. 101615fae107Sandi * 101715fae107Sandi * The range parameter needs to have the form "from-to" 101815cfe303Sandi * and gives the range of the section in bytes - no 101915cfe303Sandi * UTF-8 awareness is needed. 1020f3f0262cSandi * The returned order is prefix, section and suffix. 102115fae107Sandi * 102215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1023f3f0262cSandi */ 1024f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1025cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1026f3f0262cSandi 102780fcb268SAdrian Lang // Parse range 102880fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 102980fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 103080fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 103180fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 103280fcb268SAdrian Lang 103380fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 103480fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 103515cfe303Sandi $slices[2] = substr($text, $to); 1036f3f0262cSandi return $slices; 1037f3f0262cSandi} 1038f3f0262cSandi 1039f3f0262cSandi/** 104015fae107Sandi * Joins wiki text slices 104115fae107Sandi * 104280fcb268SAdrian Lang * function to join the text slices. 1043f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1044f3f0262cSandi * lines between sections if needed (used on saving). 104515fae107Sandi * 104615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1047f3f0262cSandi */ 1048f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1049f3f0262cSandi if($pretty) { 105080fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 10513272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 10523272d797SAndreas Gohr ) { 105380fcb268SAdrian Lang $pre .= "\n"; 105480fcb268SAdrian Lang } 105580fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 10563272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 10573272d797SAndreas Gohr ) { 105880fcb268SAdrian Lang $text .= "\n"; 105980fcb268SAdrian Lang } 1060f3f0262cSandi } 1061f3f0262cSandi 1062f3f0262cSandi return $pre.$text.$suf; 1063f3f0262cSandi} 1064f3f0262cSandi 1065f3f0262cSandi/** 1066a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1067a701424fSBen Coburn * Also directs changelog and attic updates. 106815fae107Sandi * 106915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 107071726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1071f3f0262cSandi */ 1072b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1073a701424fSBen Coburn /* Note to developers: 1074a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1075a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1076a701424fSBen Coburn after any changes. External edits change the wiki page 1077a701424fSBen Coburn directly without using php or dokuwiki. 1078a701424fSBen Coburn */ 1079f3f0262cSandi global $conf; 1080f3f0262cSandi global $lang; 108171726d78SBen Coburn global $REV; 1082585bf44eSChristopher Smith /* @var Input $INPUT */ 1083585bf44eSChristopher Smith global $INPUT; 1084585bf44eSChristopher Smith 1085f3f0262cSandi // ignore if no changes were made 1086f3f0262cSandi if($text == rawWiki($id, '')) { 1087f3f0262cSandi return; 1088f3f0262cSandi } 1089f3f0262cSandi 1090f3f0262cSandi $file = wikiFN($id); 1091a701424fSBen Coburn $old = @filemtime($file); // from page 1092407e65b9SAndreas Gohr $wasRemoved = (trim($text) == ''); // check for empty or whitespace only 1093d8186216SBen Coburn $wasCreated = !@file_exists($file); 109471726d78SBen Coburn $wasReverted = ($REV == true); 1095e45b34cdSBen Coburn $newRev = false; 1096a701424fSBen Coburn $oldRev = getRevisions($id, -1, 1, 1024); // from changelog 1097a701424fSBen Coburn $oldRev = (int) (empty($oldRev) ? 0 : $oldRev[0]); 1098a701424fSBen Coburn if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old >= $oldRev) { 109946844156SBen Coburn // add old revision to the attic if missing 110046844156SBen Coburn saveOldRevision($id); 110146844156SBen Coburn // add a changelog entry if this edit came from outside dokuwiki 1102a701424fSBen Coburn if($old > $oldRev) { 1103ebf1501fSBen Coburn addLogEntry($old, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=> true)); 110446844156SBen Coburn // remove soon to be stale instructions 110546844156SBen Coburn $cache = new cache_instructions($id, $file); 110646844156SBen Coburn $cache->removeCache(); 110746844156SBen Coburn } 110846844156SBen Coburn } 1109f3f0262cSandi 111071726d78SBen Coburn if($wasRemoved) { 111130725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 111230725328SGabriel Birke $data = array(array($file, '', false), getNS($id), noNS($id), false); 111330725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1114e45b34cdSBen Coburn // pre-save deleted revision 1115e45b34cdSBen Coburn @touch($file); 111646844156SBen Coburn clearstatcache(); 1117e45b34cdSBen Coburn $newRev = saveOldRevision($id); 1118e1f3d9e1SEsther Brunner // remove empty file 1119f3f0262cSandi @unlink($file); 1120c5f92742SMichael Hamann // don't remove old meta info as it should be saved, plugins can use IO_WIKIPAGE_WRITE for removing their metadata... 1121c5f92742SMichael Hamann // purge non-persistant meta data 11223d1f9ec3SMichael Klier p_purge_metadata($id); 1123f3f0262cSandi $del = true; 11243ce054b3Sandi // autoset summary on deletion 11253ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 112653d6ccfeSandi // remove empty namespaces 1127cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1128cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1129f3f0262cSandi } else { 1130cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 1131cc7d0c94SBen Coburn io_writeWikiPage($file, $text, $id); 113246844156SBen Coburn // pre-save the revision, to keep the attic in sync 113346844156SBen Coburn $newRev = saveOldRevision($id); 1134f3f0262cSandi $del = false; 1135f3f0262cSandi } 1136f3f0262cSandi 113771726d78SBen Coburn // select changelog line type 113871726d78SBen Coburn $extra = ''; 1139ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_EDIT; 114071726d78SBen Coburn if($wasReverted) { 1141ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_REVERT; 114271726d78SBen Coburn $extra = $REV; 11433272d797SAndreas Gohr } else if($wasCreated) { 11443272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_CREATE; 11453272d797SAndreas Gohr } else if($wasRemoved) { 11463272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_DELETE; 1147585bf44eSChristopher Smith } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 11483272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_MINOR_EDIT; 11493272d797SAndreas Gohr } //minor edits only for logged in users 115071726d78SBen Coburn 1151e45b34cdSBen Coburn addLogEntry($newRev, $id, $type, $summary, $extra); 115226a0801fSAndreas Gohr // send notify mails 115390033e9dSAndreas Gohr notify($id, 'admin', $old, $summary, $minor); 115490033e9dSAndreas Gohr notify($id, 'subscribers', $old, $summary, $minor); 1155f3f0262cSandi 1156ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 115798407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 11582eccbdaaSGina Haeussge 11592eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1160fe9ec250SChris Smith if(useHeading('content')) { 116107ff0babSMichael Hamann $pages = ft_backlinks($id, true); 11622eccbdaaSGina Haeussge foreach($pages as $page) { 11632eccbdaaSGina Haeussge $cache = new cache_renderer($page, wikiFN($page), 'xhtml'); 11642eccbdaaSGina Haeussge $cache->removeCache(); 11652eccbdaaSGina Haeussge } 11662eccbdaaSGina Haeussge } 1167f3f0262cSandi} 1168f3f0262cSandi 1169f3f0262cSandi/** 1170f3f0262cSandi * moves the current version to the attic and returns its 1171f3f0262cSandi * revision date 117215fae107Sandi * 117315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1174f3f0262cSandi */ 1175f3f0262cSandifunction saveOldRevision($id) { 1176f3f0262cSandi $oldf = wikiFN($id); 1177f3f0262cSandi if(!@file_exists($oldf)) return ''; 1178f3f0262cSandi $date = filemtime($oldf); 1179f3f0262cSandi $newf = wikiFN($id, $date); 1180cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1181f3f0262cSandi return $date; 1182f3f0262cSandi} 1183f3f0262cSandi 1184f3f0262cSandi/** 1185fde10de4SAdrian Lang * Sends a notify mail on page change or registration 118626a0801fSAndreas Gohr * 118726a0801fSAndreas Gohr * @param string $id The changed page 1188fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 11893272d797SAndreas Gohr * @param int|string $rev Old page revision 119026a0801fSAndreas Gohr * @param string $summary What changed 119190033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 119202a498e7Schris * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 119315fae107Sandi * 11943272d797SAndreas Gohr * @return bool 119515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1196f3f0262cSandi */ 119702a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1198f3f0262cSandi global $conf; 1199585bf44eSChristopher Smith /* @var Input $INPUT */ 1200585bf44eSChristopher Smith global $INPUT; 1201b158d625SSteven Danz 12026df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 120326a0801fSAndreas Gohr if($who == 'admin') { 12043272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 12052ed38036SAndreas Gohr $tpl = 'mailtext'; 120626a0801fSAndreas Gohr $to = $conf['notify']; 120726a0801fSAndreas Gohr } elseif($who == 'subscribers') { 120884c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1209585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 12108881fcc9SAdrian Lang $data = array('id' => $id, 'addresslist' => '', 'self' => false); 12113272d797SAndreas Gohr trigger_event( 12123272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1213835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 12143272d797SAndreas Gohr ); 12152ed38036SAndreas Gohr $to = $data['addresslist']; 12162ed38036SAndreas Gohr if(empty($to)) return false; 12172ed38036SAndreas Gohr $tpl = 'subscr_single'; 121826a0801fSAndreas Gohr } else { 12193272d797SAndreas Gohr return false; //just to be safe 122026a0801fSAndreas Gohr } 122126a0801fSAndreas Gohr 12226df843eeSAndreas Gohr // prepare content 12232ed38036SAndreas Gohr $subscription = new Subscription(); 12242ed38036SAndreas Gohr return $subscription->send_diff($to, $tpl, $id, $rev, $summary); 1225f3f0262cSandi} 12262ed38036SAndreas Gohr 122715fae107Sandi/** 122871f7bde7SAndreas Gohr * extracts the query from a search engine referrer 122915fae107Sandi * 123015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 123171f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1232f3f0262cSandi */ 1233f3f0262cSandifunction getGoogleQuery() { 1234585bf44eSChristopher Smith /* @var Input $INPUT */ 1235585bf44eSChristopher Smith global $INPUT; 1236585bf44eSChristopher Smith 1237585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1238c66972f2SAdrian Lang return ''; 1239c66972f2SAdrian Lang } 1240585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1241f3f0262cSandi 1242079b3ac1SAndreas Gohr // only handle common SEs 1243079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1244e4d8a516SKazutaka Miyasaka 1245079b3ac1SAndreas Gohr $query = array(); 1246e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1247e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1248e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1249f3f0262cSandi parse_str($url['query'], $query); 1250e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1251e4d8a516SKazutaka Miyasaka 1252c66972f2SAdrian Lang $q = ''; 1253079b3ac1SAndreas Gohr if(isset($query['q'])){ 1254079b3ac1SAndreas Gohr $q = $query['q']; 1255079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1256079b3ac1SAndreas Gohr $q = $query['p']; 1257079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1258079b3ac1SAndreas Gohr $q = $query['query']; 1259079b3ac1SAndreas Gohr } 1260079b3ac1SAndreas Gohr $q = trim($q); 1261f3f0262cSandi 1262079b3ac1SAndreas Gohr if(!$q) return ''; 12636531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1264f93b3b50SAndreas Gohr return $q; 1265f3f0262cSandi} 1266f3f0262cSandi 1267f3f0262cSandi/** 1268f3f0262cSandi * Return the human readable size of a file 1269f3f0262cSandi * 1270f3f0262cSandi * @param int $size A file size 1271f3f0262cSandi * @param int $dec A number of decimal places 127274160ca1SGerrit Uitslag * @return string human readable size 1273f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1274f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1275f3f0262cSandi * @version 1.0.0 1276f3f0262cSandi */ 1277f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1278f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1279f3f0262cSandi $count = count($sizes); 1280f3f0262cSandi $i = 0; 1281f3f0262cSandi 1282f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1283f3f0262cSandi $size /= 1024; 1284f3f0262cSandi $i++; 1285f3f0262cSandi } 1286f3f0262cSandi 1287f3f0262cSandi return round($size, $dec).' '.$sizes[$i]; 1288f3f0262cSandi} 1289f3f0262cSandi 129015fae107Sandi/** 1291c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1292c57e365eSAndreas Gohr * 1293c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1294c57e365eSAndreas Gohr */ 1295c57e365eSAndreas Gohrfunction datetime_h($dt) { 1296c57e365eSAndreas Gohr global $lang; 1297c57e365eSAndreas Gohr 1298c57e365eSAndreas Gohr $ago = time() - $dt; 1299c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1300c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1301c57e365eSAndreas Gohr } 1302c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1303c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1304c57e365eSAndreas Gohr } 1305c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1306c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1307c57e365eSAndreas Gohr } 1308c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1309c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1310c57e365eSAndreas Gohr } 1311c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1312c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1313c57e365eSAndreas Gohr } 1314c57e365eSAndreas Gohr if($ago > 60 * 2) { 1315c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1316c57e365eSAndreas Gohr } 1317c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1318c57e365eSAndreas Gohr} 1319c57e365eSAndreas Gohr 1320c57e365eSAndreas Gohr/** 1321f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1322f2263577SAndreas Gohr * 1323f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1324f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1325f2263577SAndreas Gohr * 1326f2263577SAndreas Gohr * @see datetime_h 1327f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1328f2263577SAndreas Gohr */ 1329f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1330f2263577SAndreas Gohr global $conf; 1331f2263577SAndreas Gohr 1332f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1333f2263577SAndreas Gohr $dt = (int) $dt; 1334f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1335f2263577SAndreas Gohr 1336f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1337f2263577SAndreas Gohr return strftime($format, $dt); 1338f2263577SAndreas Gohr} 1339f2263577SAndreas Gohr 1340f2263577SAndreas Gohr/** 1341c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1342c4f79b71SMichael Hamann * 1343c4f79b71SMichael Hamann * @author <ungu at terong dot com> 1344c4f79b71SMichael Hamann * @link http://www.php.net/manual/en/function.date.php#54072 134563703ba5SAndreas Gohr * @param int $int_date: current date in UNIX timestamp 13463272d797SAndreas Gohr * @return string 1347c4f79b71SMichael Hamann */ 1348c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1349c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1350c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1351c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1352c4f79b71SMichael Hamann $date_mod .= $time_zone; 1353c4f79b71SMichael Hamann return $date_mod; 1354c4f79b71SMichael Hamann} 1355c4f79b71SMichael Hamann 1356c4f79b71SMichael Hamann/** 135700a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 135800a7b5adSEsther Brunner * 135900a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 136000a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 136100a7b5adSEsther Brunner */ 136200a7b5adSEsther Brunnerfunction obfuscate($email) { 136300a7b5adSEsther Brunner global $conf; 136400a7b5adSEsther Brunner 136500a7b5adSEsther Brunner switch($conf['mailguard']) { 136600a7b5adSEsther Brunner case 'visible' : 136700a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 136800a7b5adSEsther Brunner return strtr($email, $obfuscate); 136900a7b5adSEsther Brunner 137000a7b5adSEsther Brunner case 'hex' : 137100a7b5adSEsther Brunner $encode = ''; 137249eb6e38SAndreas Gohr $len = strlen($email); 137349eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 137449eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 137549eb6e38SAndreas Gohr } 137600a7b5adSEsther Brunner return $encode; 137700a7b5adSEsther Brunner 137800a7b5adSEsther Brunner case 'none' : 137900a7b5adSEsther Brunner default : 138000a7b5adSEsther Brunner return $email; 138100a7b5adSEsther Brunner } 138200a7b5adSEsther Brunner} 138300a7b5adSEsther Brunner 138400a7b5adSEsther Brunner/** 138589541d4bSAndreas Gohr * Removes quoting backslashes 138689541d4bSAndreas Gohr * 138789541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 138889541d4bSAndreas Gohr */ 138989541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 139089541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 139189541d4bSAndreas Gohr} 139289541d4bSAndreas Gohr 139373038c47SAndreas Gohr/** 139473038c47SAndreas Gohr * Convert php.ini shorthands to byte 139573038c47SAndreas Gohr * 139673038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 139773038c47SAndreas Gohr * @link http://de3.php.net/manual/en/ini.core.php#79564 139873038c47SAndreas Gohr */ 139973038c47SAndreas Gohrfunction php_to_byte($v) { 140073038c47SAndreas Gohr $l = substr($v, -1); 140173038c47SAndreas Gohr $ret = substr($v, 0, -1); 140273038c47SAndreas Gohr switch(strtoupper($l)) { 140374160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 140473038c47SAndreas Gohr case 'P': 140573038c47SAndreas Gohr $ret *= 1024; 140674160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 140773038c47SAndreas Gohr case 'T': 140873038c47SAndreas Gohr $ret *= 1024; 140974160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 141073038c47SAndreas Gohr case 'G': 141173038c47SAndreas Gohr $ret *= 1024; 141274160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 141373038c47SAndreas Gohr case 'M': 141473038c47SAndreas Gohr $ret *= 1024; 141573038c47SAndreas Gohr case 'K': 141673038c47SAndreas Gohr $ret *= 1024; 141773038c47SAndreas Gohr break; 141849cbd23eSOtto Vainio default; 141949cbd23eSOtto Vainio $ret *= 10; 142049cbd23eSOtto Vainio break; 142173038c47SAndreas Gohr } 142273038c47SAndreas Gohr return $ret; 142373038c47SAndreas Gohr} 142473038c47SAndreas Gohr 1425546d3a99SAndreas Gohr/** 1426546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1427546d3a99SAndreas Gohr */ 1428546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1429546d3a99SAndreas Gohr return preg_quote($string, '/'); 1430546d3a99SAndreas Gohr} 143173038c47SAndreas Gohr 1432bd2f6c2fSAndreas Gohr/** 1433bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1434bd2f6c2fSAndreas Gohr * 1435c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1436bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1437bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1438bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1439bd2f6c2fSAndreas Gohr * 1440bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1441bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1442bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1443bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1444bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 14453272d797SAndreas Gohr * @return string 1446bd2f6c2fSAndreas Gohr */ 1447a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 1448bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1449bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1450bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1451bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1452bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1453bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1454bd2f6c2fSAndreas Gohr} 1455bd2f6c2fSAndreas Gohr 1456dc58b6f4SAndy Webber/** 1457dc58b6f4SAndy Webber * Return the users realname or e-mail address for use 1458dc58b6f4SAndy Webber * in page footer and recent changes pages 1459dc58b6f4SAndy Webber * 1460dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1461dc58b6f4SAndy Webber */ 1462dc58b6f4SAndy Webberfunction editorinfo($username) { 1463dc58b6f4SAndy Webber global $conf; 1464dc58b6f4SAndy Webber global $auth; 1465dc58b6f4SAndy Webber 1466dc58b6f4SAndy Webber switch($conf['showuseras']) { 1467dc58b6f4SAndy Webber case 'username': 1468dc58b6f4SAndy Webber case 'email': 1469dc58b6f4SAndy Webber case 'email_link': 1470173d78c4SAndreas Gohr if($auth) $info = $auth->getUserData($username); 1471dc58b6f4SAndy Webber break; 1472dc58b6f4SAndy Webber default: 1473dc58b6f4SAndy Webber return hsc($username); 1474dc58b6f4SAndy Webber } 1475dc58b6f4SAndy Webber 1476dc58b6f4SAndy Webber if(isset($info) && $info) { 1477dc58b6f4SAndy Webber switch($conf['showuseras']) { 1478dc58b6f4SAndy Webber case 'username': 1479dc58b6f4SAndy Webber return hsc($info['name']); 1480dc58b6f4SAndy Webber case 'email': 1481dc58b6f4SAndy Webber return obfuscate($info['mail']); 1482dc58b6f4SAndy Webber case 'email_link': 1483dc58b6f4SAndy Webber $mail = obfuscate($info['mail']); 1484dc58b6f4SAndy Webber return '<a href="mailto:'.$mail.'">'.$mail.'</a>'; 1485dc58b6f4SAndy Webber default: 1486dc58b6f4SAndy Webber return hsc($username); 1487dc58b6f4SAndy Webber } 1488dc58b6f4SAndy Webber } else { 1489dc58b6f4SAndy Webber return hsc($username); 1490dc58b6f4SAndy Webber } 1491066fee30SAndreas Gohr} 1492066fee30SAndreas Gohr 1493066fee30SAndreas Gohr/** 1494066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1495066fee30SAndreas Gohr * When no image exists, returns an empty string 1496066fee30SAndreas Gohr * 1497066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1498066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 14993272d797SAndreas Gohr * @return string 1500066fee30SAndreas Gohr */ 1501066fee30SAndreas Gohrfunction license_img($type) { 1502066fee30SAndreas Gohr global $license; 1503066fee30SAndreas Gohr global $conf; 1504066fee30SAndreas Gohr if(!$conf['license']) return ''; 1505066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1506066fee30SAndreas Gohr $lic = $license[$conf['license']]; 1507066fee30SAndreas Gohr $try = array(); 1508066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1509066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1510066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1511066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1512066fee30SAndreas Gohr } 1513066fee30SAndreas Gohr foreach($try as $src) { 1514066fee30SAndreas Gohr if(@file_exists(DOKU_INC.$src)) return $src; 1515066fee30SAndreas Gohr } 1516066fee30SAndreas Gohr return ''; 1517dc58b6f4SAndy Webber} 1518dc58b6f4SAndy Webber 151913c08e2fSMichael Klier/** 152013c08e2fSMichael Klier * Checks if the given amount of memory is available 152113c08e2fSMichael Klier * 152213c08e2fSMichael Klier * If the memory_get_usage() function is not available the 152313c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 152413c08e2fSMichael Klier * 152513c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 152613c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 15273272d797SAndreas Gohr * 15283272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 15293272d797SAndreas Gohr * @param int $bytes 15303272d797SAndreas Gohr * @internal param int $used already allocated memory (see above) 15313272d797SAndreas Gohr * @return bool 153213c08e2fSMichael Klier */ 153313c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 153413c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 153513c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 153613c08e2fSMichael Klier 153713c08e2fSMichael Klier // parse limit to bytes 153813c08e2fSMichael Klier $limit = php_to_byte($limit); 153913c08e2fSMichael Klier 154013c08e2fSMichael Klier // get used memory if possible 154113c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 154213c08e2fSMichael Klier $used = memory_get_usage(); 154349eb6e38SAndreas Gohr } else { 154449eb6e38SAndreas Gohr $used = $bytes; 154513c08e2fSMichael Klier } 154613c08e2fSMichael Klier 154713c08e2fSMichael Klier if($used + $mem > $limit) { 154813c08e2fSMichael Klier return false; 154913c08e2fSMichael Klier } 155013c08e2fSMichael Klier 155113c08e2fSMichael Klier return true; 155213c08e2fSMichael Klier} 155313c08e2fSMichael Klier 1554af2408d5SAndreas Gohr/** 1555af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1556af2408d5SAndreas Gohr * 1557af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1558af2408d5SAndreas Gohr * 1559af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1560af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1561af2408d5SAndreas Gohr */ 1562af2408d5SAndreas Gohrfunction send_redirect($url) { 1563585bf44eSChristopher Smith /* @var Input $INPUT */ 1564585bf44eSChristopher Smith global $INPUT; 1565585bf44eSChristopher Smith 15660181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 15670181f021SAndreas Gohr global $MSG; 15680181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 15690181f021SAndreas Gohr //reopen session, store data and close session again 15700181f021SAndreas Gohr @session_start(); 15710181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 15720181f021SAndreas Gohr } 15730181f021SAndreas Gohr 1574d4869846SAndreas Gohr // always close the session 1575d4869846SAndreas Gohr session_write_close(); 1576d4869846SAndreas Gohr 1577c10dcb7dSAndreas Gohr // work around IE bug 1578c10dcb7dSAndreas Gohr // http://www.ianhoar.com/2008/11/16/internet-explorer-6-and-redirected-anchor-links/ 15796d2af55dSChristopher Smith @list($url, $hash) = explode('#', $url); 1580c10dcb7dSAndreas Gohr if($hash) { 1581c10dcb7dSAndreas Gohr if(strpos($url, '?')) { 1582c10dcb7dSAndreas Gohr $url = $url.'&#'.$hash; 1583c10dcb7dSAndreas Gohr } else { 1584c10dcb7dSAndreas Gohr $url = $url.'?&#'.$hash; 1585c10dcb7dSAndreas Gohr } 1586c10dcb7dSAndreas Gohr } 1587c10dcb7dSAndreas Gohr 1588af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1589585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1590585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1591585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 15923272d797SAndreas Gohr $matches[1] < 6 15933272d797SAndreas Gohr ) { 1594af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1595af2408d5SAndreas Gohr } else { 1596af2408d5SAndreas Gohr header('Location: '.$url); 1597af2408d5SAndreas Gohr } 1598af2408d5SAndreas Gohr exit; 1599af2408d5SAndreas Gohr} 1600af2408d5SAndreas Gohr 16015b75cd1fSAdrian Lang/** 16025b75cd1fSAdrian Lang * Validate a value using a set of valid values 16035b75cd1fSAdrian Lang * 16045b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 16055b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 16065b75cd1fSAdrian Lang * default is specified, throws an exception. 16075b75cd1fSAdrian Lang * 16085b75cd1fSAdrian Lang * @param string $param The name of the parameter 16095b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 16105b75cd1fSAdrian Lang * be marked by the key “default”. 16115b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 16125b75cd1fSAdrian Lang * or $_GET) 16135b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 16145b75cd1fSAdrian Lang * 16153272d797SAndreas Gohr * @throws Exception 16163272d797SAndreas Gohr * @return mixed 16175b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 16185b75cd1fSAdrian Lang */ 16195b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 16205b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 16215b75cd1fSAdrian Lang return $array[$param]; 16225b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 16235b75cd1fSAdrian Lang return $valid_values['default']; 16245b75cd1fSAdrian Lang } else { 16255b75cd1fSAdrian Lang throw new Exception($exc); 16265b75cd1fSAdrian Lang } 16275b75cd1fSAdrian Lang} 16285b75cd1fSAdrian Lang 162963703ba5SAndreas Gohr/** 163063703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1631646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 163263703ba5SAndreas Gohr */ 1633554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1634646a531aSChristopher Smith $enc_pref = urlencode($pref); 1635646a531aSChristopher Smith if(strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1636554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 163763703ba5SAndreas Gohr $cnt = count($parts); 163863703ba5SAndreas Gohr for($i = 0; $i < $cnt; $i += 2) { 1639646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1640646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1641554a8c9fSAdrian Lang } 1642554a8c9fSAdrian Lang } 1643554a8c9fSAdrian Lang } 1644554a8c9fSAdrian Lang return $default; 1645554a8c9fSAdrian Lang} 1646554a8c9fSAdrian Lang 16473c94d07bSAnika Henke/** 16483c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 164936ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 16503c94d07bSAnika Henke */ 16513c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 16523c94d07bSAnika Henke global $conf; 16533c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 16543c94d07bSAnika Henke $cookieVal = ''; 16553c94d07bSAnika Henke 16563c94d07bSAnika Henke if($orig && ($orig != $val)) { 16573c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 16583c94d07bSAnika Henke $cnt = count($parts); 165936ec377eSChristopher Smith // urlencode $pref for the comparison 166036ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 16613c94d07bSAnika Henke for($i = 0; $i < $cnt; $i += 2) { 166236ec377eSChristopher Smith if($parts[$i] == $enc_pref) { 166336ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 166450f261f7SMichael Hamann break; 16653c94d07bSAnika Henke } 16663c94d07bSAnika Henke } 16673c94d07bSAnika Henke $cookieVal = implode('#', $parts); 16683c94d07bSAnika Henke } else if (!$orig) { 166936ec377eSChristopher Smith $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : '').rawurlencode($pref).'#'.rawurlencode($val); 16703c94d07bSAnika Henke } 16713c94d07bSAnika Henke 16723c94d07bSAnika Henke if (!empty($cookieVal)) { 167375e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 167475e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 16753c94d07bSAnika Henke } 16763c94d07bSAnika Henke} 16773c94d07bSAnika Henke 1678e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1679