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() { 598071beaaSAndreas Gohr return md5(auth_cookiesalt().session_id().$_SERVER['REMOTE_USER']); 60634d7150SAndreas Gohr} 61634d7150SAndreas Gohr 62634d7150SAndreas Gohr/** 63634d7150SAndreas Gohr * Check the secret CSRF token 64634d7150SAndreas Gohr */ 65634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 667d01a0eaSTom N Harris global $INPUT; 67df97eaacSAndreas Gohr if(!$_SERVER['REMOTE_USER']) return true; // no logged in user, no need for a check 68df97eaacSAndreas Gohr 697d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 70634d7150SAndreas Gohr if(getSecurityToken() != $token) { 71634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 72634d7150SAndreas Gohr return false; 73634d7150SAndreas Gohr } 74634d7150SAndreas Gohr return true; 75634d7150SAndreas Gohr} 76634d7150SAndreas Gohr 77634d7150SAndreas Gohr/** 78634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 79634d7150SAndreas Gohr * 80634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 81634d7150SAndreas Gohr */ 82634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 832404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 843272d797SAndreas Gohr if($print) echo $ret; 85634d7150SAndreas Gohr return $ret; 86634d7150SAndreas Gohr} 87634d7150SAndreas Gohr 88634d7150SAndreas Gohr/** 89*1015a57dSChristopher Smith * Determine basic information for a request of $id 9015fae107Sandi * 91*1015a57dSChristopher Smith * @param unknown_type $id 92*1015a57dSChristopher Smith * @param unknown_type $httpClient 93f3f0262cSandi */ 94*1015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 95f3f0262cSandi global $USERINFO; 966afe8dcaSchris 97c66972f2SAdrian Lang // set info about manager/admin status. 98c66972f2SAdrian Lang $info['isadmin'] = false; 99c66972f2SAdrian Lang $info['ismanager'] = false; 100c66972f2SAdrian Lang if(isset($_SERVER['REMOTE_USER'])) { 101f3f0262cSandi $info['userinfo'] = $USERINFO; 102*1015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 103ee4c4a1bSAndreas Gohr $info['client'] = $_SERVER['REMOTE_USER']; 10417ee7f66SAndreas Gohr 105f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 106f8cc712eSAndreas Gohr $info['isadmin'] = true; 107f8cc712eSAndreas Gohr $info['ismanager'] = true; 108f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 109f8cc712eSAndreas Gohr $info['ismanager'] = true; 110f8cc712eSAndreas Gohr } 111f8cc712eSAndreas Gohr 11217ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 11317ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 11417ee7f66SAndreas Gohr $info['userinfo']['name'] = $_SERVER['REMOTE_USER']; 11517ee7f66SAndreas Gohr } 116ee4c4a1bSAndreas Gohr 117f3f0262cSandi } else { 118*1015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 1191380fc45SAndreas Gohr $info['subscribed'] = false; 120ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 121f3f0262cSandi } 122f3f0262cSandi 123*1015a57dSChristopher Smith $info['namespace'] = getNS($id); 124*1015a57dSChristopher Smith 125*1015a57dSChristopher Smith // mobile detection 126*1015a57dSChristopher Smith if ($htmlClient) { 127*1015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 128*1015a57dSChristopher Smith } 129*1015a57dSChristopher Smith 130*1015a57dSChristopher Smith return $info; 131*1015a57dSChristopher Smith } 132*1015a57dSChristopher Smith 133*1015a57dSChristopher Smith/** 134*1015a57dSChristopher Smith * Return info about the current document as associative 135*1015a57dSChristopher Smith * array. 136*1015a57dSChristopher Smith * 137*1015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 138*1015a57dSChristopher Smith */ 139*1015a57dSChristopher Smithfunction pageinfo() { 140*1015a57dSChristopher Smith global $ID; 141*1015a57dSChristopher Smith global $REV; 142*1015a57dSChristopher Smith global $RANGE; 143*1015a57dSChristopher Smith global $lang; 144*1015a57dSChristopher Smith 145*1015a57dSChristopher Smith $info = basicinfo($ID); 146*1015a57dSChristopher Smith 147*1015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 148*1015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 149*1015a57dSChristopher Smith $info['id'] = $ID; 150*1015a57dSChristopher Smith $info['rev'] = $REV; 151*1015a57dSChristopher Smith 152f3f0262cSandi $info['locked'] = checklock($ID); 15300976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID)); 1542ca9d91cSBen Coburn $info['exists'] = @file_exists($info['filepath']); 1552ca9d91cSBen Coburn if($REV) { 1562ca9d91cSBen Coburn //check if current revision was meant 1572ca9d91cSBen Coburn if($info['exists'] && (@filemtime($info['filepath']) == $REV)) { 1582ca9d91cSBen Coburn $REV = ''; 1597b3a6803SAndreas Gohr } elseif($RANGE) { 1607b3a6803SAndreas Gohr //section editing does not work with old revisions! 1617b3a6803SAndreas Gohr $REV = ''; 1627b3a6803SAndreas Gohr $RANGE = ''; 1637b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 1642ca9d91cSBen Coburn } else { 1652ca9d91cSBen Coburn //really use old revision 16600976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID, $REV)); 167f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 168f3f0262cSandi } 169f3f0262cSandi } 170c112d578Sandi $info['rev'] = $REV; 171f3f0262cSandi if($info['exists']) { 172f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 173f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 174f3f0262cSandi } else { 175f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 176f3f0262cSandi } 17750e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 178f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 179f3f0262cSandi 18071726d78SBen Coburn //load page meta data 18171726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 18271726d78SBen Coburn 183652610a2Sandi //who's the editor 184652610a2Sandi if($REV) { 18571726d78SBen Coburn $revinfo = getRevisionInfo($ID, $REV, 1024); 186652610a2Sandi } else { 187aa27cf05SAndreas Gohr if(is_array($info['meta']['last_change'])) { 188aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 189aa27cf05SAndreas Gohr } else { 190cd00a034SBen Coburn $revinfo = getRevisionInfo($ID, $info['lastmod'], 1024); 191cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 192cd00a034SBen Coburn if($revinfo !== false) { 193cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 194cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 195cd00a034SBen Coburn } 196cd00a034SBen Coburn } 197cd00a034SBen Coburn } 198cd00a034SBen Coburn //and check for an external edit 199cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 200cd00a034SBen Coburn // cached changelog line no longer valid 201cd00a034SBen Coburn $revinfo = false; 202cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 203cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 204652610a2Sandi } 205bb4866bdSchris 206652610a2Sandi $info['ip'] = $revinfo['ip']; 207652610a2Sandi $info['user'] = $revinfo['user']; 208652610a2Sandi $info['sum'] = $revinfo['sum']; 20971726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 210ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 21159f257aeSchris 21288f522e9Sandi if($revinfo['user']) { 21388f522e9Sandi $info['editor'] = $revinfo['user']; 21488f522e9Sandi } else { 21588f522e9Sandi $info['editor'] = $revinfo['ip']; 21688f522e9Sandi } 217652610a2Sandi 218ee4c4a1bSAndreas Gohr // draft 219ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID, '.draft'); 220ee4c4a1bSAndreas Gohr if(@file_exists($draft)) { 221ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))) { 222ee4c4a1bSAndreas Gohr // remove stale draft 223ee4c4a1bSAndreas Gohr @unlink($draft); 224ee4c4a1bSAndreas Gohr } else { 225ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 226ee4c4a1bSAndreas Gohr } 227ee4c4a1bSAndreas Gohr } 228ee4c4a1bSAndreas Gohr 229*1015a57dSChristopher Smith return $info; 230*1015a57dSChristopher Smith} 231*1015a57dSChristopher Smith 232*1015a57dSChristopher Smith/** 233*1015a57dSChristopher Smith * Return information about the current media item as an associative array. 234*1015a57dSChristopher Smith */ 235*1015a57dSChristopher Smithfunction mediainfo(){ 236*1015a57dSChristopher Smith global $NS; 237*1015a57dSChristopher Smith global $IMG; 238*1015a57dSChristopher Smith 239*1015a57dSChristopher Smith $info = basicinfo("$NS:*"); 240*1015a57dSChristopher Smith $info['image'] = $IMG; 2411c548ebeSAndreas Gohr 242f3f0262cSandi return $info; 243f3f0262cSandi} 244f3f0262cSandi 245f3f0262cSandi/** 2462684e50aSAndreas Gohr * Build an string of URL parameters 2472684e50aSAndreas Gohr * 2482684e50aSAndreas Gohr * @author Andreas Gohr 2492684e50aSAndreas Gohr */ 250b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 2512684e50aSAndreas Gohr $url = ''; 2522684e50aSAndreas Gohr $amp = false; 2532684e50aSAndreas Gohr foreach($params as $key => $val) { 254b174aeaeSchris if($amp) $url .= $sep; 2552684e50aSAndreas Gohr 25685e6871fSAdrian Lang $url .= rawurlencode($key).'='; 2573a50618cSgweissbach $url .= rawurlencode((string) $val); 2582684e50aSAndreas Gohr $amp = true; 2592684e50aSAndreas Gohr } 2602684e50aSAndreas Gohr return $url; 2612684e50aSAndreas Gohr} 2622684e50aSAndreas Gohr 2632684e50aSAndreas Gohr/** 2642684e50aSAndreas Gohr * Build an string of html tag attributes 2652684e50aSAndreas Gohr * 2667bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 2677bff22c0SAndreas Gohr * 2682684e50aSAndreas Gohr * @author Andreas Gohr 2692684e50aSAndreas Gohr */ 2704b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 2712684e50aSAndreas Gohr $url = ''; 2729063ec14SAdrian Lang $white = false; 2732684e50aSAndreas Gohr foreach($params as $key => $val) { 2747bff22c0SAndreas Gohr if($key{0} == '_') continue; 275b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 2769063ec14SAdrian Lang if($white) $url .= ' '; 2777bff22c0SAndreas Gohr 2782684e50aSAndreas Gohr $url .= $key.'="'; 2792684e50aSAndreas Gohr $url .= htmlspecialchars($val); 2802684e50aSAndreas Gohr $url .= '"'; 2819063ec14SAdrian Lang $white = true; 2822684e50aSAndreas Gohr } 2832684e50aSAndreas Gohr return $url; 2842684e50aSAndreas Gohr} 2852684e50aSAndreas Gohr 2862684e50aSAndreas Gohr/** 28715fae107Sandi * This builds the breadcrumb trail and returns it as array 28815fae107Sandi * 28915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 290f3f0262cSandi */ 291f3f0262cSandifunction breadcrumbs() { 2928746e727Sandi // we prepare the breadcrumbs early for quick session closing 2938746e727Sandi static $crumbs = null; 2948746e727Sandi if($crumbs != null) return $crumbs; 2958746e727Sandi 296f3f0262cSandi global $ID; 297f3f0262cSandi global $ACT; 298f3f0262cSandi global $conf; 299f3f0262cSandi 300f3f0262cSandi //first visit? 301c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 302f3f0262cSandi //we only save on show and existing wiki documents 303a77f5846Sjan $file = wikiFN($ID); 304a77f5846Sjan if($ACT != 'show' || !@file_exists($file)) { 305e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 306f3f0262cSandi return $crumbs; 307f3f0262cSandi } 308a77f5846Sjan 309a77f5846Sjan // page names 3101a84a0f3SAnika Henke $name = noNSorNS($ID); 311fe9ec250SChris Smith if(useHeading('navigation')) { 312a77f5846Sjan // get page title 31367c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 314a77f5846Sjan if($title) { 315a77f5846Sjan $name = $title; 316a77f5846Sjan } 317a77f5846Sjan } 318a77f5846Sjan 319f3f0262cSandi //remove ID from array 320a77f5846Sjan if(isset($crumbs[$ID])) { 321a77f5846Sjan unset($crumbs[$ID]); 322f3f0262cSandi } 323f3f0262cSandi 324f3f0262cSandi //add to array 325a77f5846Sjan $crumbs[$ID] = $name; 326f3f0262cSandi //reduce size 327f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 328f3f0262cSandi array_shift($crumbs); 329f3f0262cSandi } 330f3f0262cSandi //save to session 331e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 332f3f0262cSandi return $crumbs; 333f3f0262cSandi} 334f3f0262cSandi 335f3f0262cSandi/** 33615fae107Sandi * Filter for page IDs 33715fae107Sandi * 338f3f0262cSandi * This is run on a ID before it is outputted somewhere 339f3f0262cSandi * currently used to replace the colon with something else 340907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 341907f24f7SAndreas Gohr * 342907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 343907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 344907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 34515fae107Sandi * 34649c713a3Sandi * Urlencoding is ommitted when the second parameter is false 34749c713a3Sandi * 34815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 349f3f0262cSandi */ 35049c713a3Sandifunction idfilter($id, $ue = true) { 351f3f0262cSandi global $conf; 352f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 353f3f0262cSandi $id = strtr($id, ':', '/'); 354f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 35558bedc8aSborekb $conf['userewrite'] && 35658bedc8aSborekb strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === false 3573272d797SAndreas Gohr ) { 358f3f0262cSandi $id = strtr($id, ':', ';'); 359f3f0262cSandi } 36049c713a3Sandi if($ue) { 361b6c6979fSAndreas Gohr $id = rawurlencode($id); 362f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 363f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 36449c713a3Sandi } 365f3f0262cSandi return $id; 366f3f0262cSandi} 367f3f0262cSandi 368f3f0262cSandi/** 369ed7b5f09Sandi * This builds a link to a wikipage 37015fae107Sandi * 3716c7843b5Sandi * It handles URL rewriting and adds additional parameter if 3726c7843b5Sandi * given in $more 3736c7843b5Sandi * 37415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 375f3f0262cSandi */ 37616f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 377f3f0262cSandi global $conf; 37816f15a81SDominik Eckelmann if(is_array($urlParameters)) { 37916f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 3806de3759aSAndreas Gohr } else { 38116f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 3826de3759aSAndreas Gohr } 38316f15a81SDominik Eckelmann if($id === '') { 38416f15a81SDominik Eckelmann $id = $conf['start']; 38516f15a81SDominik Eckelmann } 386f3f0262cSandi $id = idfilter($id); 38716f15a81SDominik Eckelmann if($absolute) { 388ed7b5f09Sandi $xlink = DOKU_URL; 389ed7b5f09Sandi } else { 390ed7b5f09Sandi $xlink = DOKU_BASE; 391ed7b5f09Sandi } 392f3f0262cSandi 3936c7843b5Sandi if($conf['userewrite'] == 2) { 3946c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 39516f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 3966c7843b5Sandi } elseif($conf['userewrite']) { 397f3f0262cSandi $xlink .= $id; 39816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 399bce3726dSAndreas Gohr } elseif($id) { 4006c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 40116f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 402bce3726dSAndreas Gohr } else { 403bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 40416f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 405f3f0262cSandi } 406f3f0262cSandi 407f3f0262cSandi return $xlink; 408f3f0262cSandi} 409f3f0262cSandi 410f3f0262cSandi/** 411f5c2808fSBen Coburn * This builds a link to an alternate page format 412f5c2808fSBen Coburn * 413f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 414f5c2808fSBen Coburn * 415f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 416f5c2808fSBen Coburn */ 417f5c2808fSBen Coburnfunction exportlink($id = '', $format = 'raw', $more = '', $abs = false, $sep = '&') { 418f5c2808fSBen Coburn global $conf; 419f5c2808fSBen Coburn if(is_array($more)) { 420f5c2808fSBen Coburn $more = buildURLparams($more, $sep); 421f5c2808fSBen Coburn } else { 422f5c2808fSBen Coburn $more = str_replace(',', $sep, $more); 423f5c2808fSBen Coburn } 424f5c2808fSBen Coburn 425f5c2808fSBen Coburn $format = rawurlencode($format); 426f5c2808fSBen Coburn $id = idfilter($id); 427f5c2808fSBen Coburn if($abs) { 428f5c2808fSBen Coburn $xlink = DOKU_URL; 429f5c2808fSBen Coburn } else { 430f5c2808fSBen Coburn $xlink = DOKU_BASE; 431f5c2808fSBen Coburn } 432f5c2808fSBen Coburn 433f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 434f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 435f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 436f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 437f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 438f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 439f5c2808fSBen Coburn } else { 440f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 441f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 442f5c2808fSBen Coburn } 443f5c2808fSBen Coburn 444f5c2808fSBen Coburn return $xlink; 445f5c2808fSBen Coburn} 446f5c2808fSBen Coburn 447f5c2808fSBen Coburn/** 4486de3759aSAndreas Gohr * Build a link to a media file 4496de3759aSAndreas Gohr * 4506de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 4518c08db0aSAndreas Gohr * 4528c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 4538c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 4548c08db0aSAndreas Gohr * 4553272d797SAndreas Gohr * @param string $id the media file id or URL 4563272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 4573272d797SAndreas Gohr * @param bool $direct link to detail page if false 4583272d797SAndreas Gohr * @param string $sep URL parameter separator 4593272d797SAndreas Gohr * @param bool $abs Create an absolute URL 4603272d797SAndreas Gohr * @return string 4616de3759aSAndreas Gohr */ 46255b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 4636de3759aSAndreas Gohr global $conf; 4646de3759aSAndreas Gohr if(is_array($more)) { 4658c08db0aSAndreas Gohr // strip defaults for shorter URLs 4668c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 4678c08db0aSAndreas Gohr if(!$more['w']) unset($more['w']); 4688c08db0aSAndreas Gohr if(!$more['h']) unset($more['h']); 4698c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 470b174aeaeSchris $more = buildURLparams($more, $sep); 4716de3759aSAndreas Gohr } else { 4728c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 4738c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 474b174aeaeSchris $more = str_replace(',', $sep, $more); 4756de3759aSAndreas Gohr } 4766de3759aSAndreas Gohr 47755b2b31bSAndreas Gohr if($abs) { 47855b2b31bSAndreas Gohr $xlink = DOKU_URL; 47955b2b31bSAndreas Gohr } else { 4806de3759aSAndreas Gohr $xlink = DOKU_BASE; 48155b2b31bSAndreas Gohr } 4826de3759aSAndreas Gohr 4836de3759aSAndreas Gohr // external URLs are always direct without rewriting 4846de3759aSAndreas Gohr if(preg_match('#^(https?|ftp)://#i', $id)) { 4856de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 48669d17d94SAndreas Gohr // add hash: 48769d17d94SAndreas Gohr $xlink .= '?hash='.substr(md5(auth_cookiesalt().$id), 0, 6); 4886de3759aSAndreas Gohr if($more) { 48969d17d94SAndreas Gohr $xlink .= $sep.$more; 490b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 4916de3759aSAndreas Gohr } else { 49269d17d94SAndreas Gohr $xlink .= $sep.'media='.rawurlencode($id); 4936de3759aSAndreas Gohr } 4946de3759aSAndreas Gohr return $xlink; 4956de3759aSAndreas Gohr } 4966de3759aSAndreas Gohr 4976de3759aSAndreas Gohr $id = idfilter($id); 4986de3759aSAndreas Gohr 4996de3759aSAndreas Gohr // decide on scriptname 5006de3759aSAndreas Gohr if($direct) { 5016de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 5026de3759aSAndreas Gohr $script = '_media'; 5036de3759aSAndreas Gohr } else { 5046de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 5056de3759aSAndreas Gohr } 5066de3759aSAndreas Gohr } else { 5076de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 5086de3759aSAndreas Gohr $script = '_detail'; 5096de3759aSAndreas Gohr } else { 5106de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 5116de3759aSAndreas Gohr } 5126de3759aSAndreas Gohr } 5136de3759aSAndreas Gohr 5146de3759aSAndreas Gohr // build URL based on rewrite mode 5156de3759aSAndreas Gohr if($conf['userewrite']) { 5166de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 5176de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 5186de3759aSAndreas Gohr } else { 5196de3759aSAndreas Gohr if($more) { 520a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 521b174aeaeSchris $xlink .= $sep.'media='.$id; 5226de3759aSAndreas Gohr } else { 523a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 5246de3759aSAndreas Gohr } 5256de3759aSAndreas Gohr } 5266de3759aSAndreas Gohr 5276de3759aSAndreas Gohr return $xlink; 5286de3759aSAndreas Gohr} 5296de3759aSAndreas Gohr 5306de3759aSAndreas Gohr/** 53125ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 53215fae107Sandi * 53325ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 53425ca5b17SAndreas Gohr * 53515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 536f3f0262cSandi */ 53725ca5b17SAndreas Gohrfunction script() { 538ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 539f3f0262cSandi} 540f3f0262cSandi 541f3f0262cSandi/** 54215fae107Sandi * Spamcheck against wordlist 54315fae107Sandi * 544f3f0262cSandi * Checks the wikitext against a list of blocked expressions 545f3f0262cSandi * returns true if the text contains any bad words 54615fae107Sandi * 547e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 548e403cc58SMichael Klier * 549e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 550e403cc58SMichael Klier * and gain information about the user who was blocked. 551e403cc58SMichael Klier * 552e403cc58SMichael Klier * Event data: 553e403cc58SMichael Klier * data['matches'] - array of matches 554e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 555e403cc58SMichael Klier * [ip] - ip address 556e403cc58SMichael Klier * [user] - username (if logged in) 557e403cc58SMichael Klier * [mail] - mail address (if logged in) 558e403cc58SMichael Klier * [name] - real name (if logged in) 559e403cc58SMichael Klier * 56015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5616dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 5626dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 5636dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 564f3f0262cSandi */ 5656dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 566f3f0262cSandi global $TEXT; 5676dffa0e0SAndreas Gohr global $PRE; 5686dffa0e0SAndreas Gohr global $SUF; 569f3f0262cSandi global $conf; 570e403cc58SMichael Klier global $INFO; 571f3f0262cSandi 572f3f0262cSandi if(!$conf['usewordblock']) return false; 573f3f0262cSandi 5746dffa0e0SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF"; 5756dffa0e0SAndreas Gohr 576041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 5776dffa0e0SAndreas Gohr $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', '\1http://\2 \2\3', $text); 578041d1964SAndreas Gohr 579b9ac8716Schris $wordblocks = getWordblocks(); 5803e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 5813e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 5823e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 5833e2965d7Sandi // backreferences are used - the maximum is 99 5843e2965d7Sandi // this is very bad performancewise and may even be too high still 5853e2965d7Sandi $chunksize = 40; 5863e2965d7Sandi } else { 587a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 5883e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 589a51d08efSAndreas Gohr $chunksize = 200; 5903e2965d7Sandi } 591b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 592f3f0262cSandi $re = array(); 59349eb6e38SAndreas Gohr // build regexp from blocks 594f3f0262cSandi foreach($blocks as $block) { 595f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 596f3f0262cSandi $block = trim($block); 597f3f0262cSandi if(empty($block)) continue; 598f3f0262cSandi $re[] = $block; 599f3f0262cSandi } 600e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 601e403cc58SMichael Klier // prepare event data 602e403cc58SMichael Klier $data['matches'] = $matches; 603e403cc58SMichael Klier $data['userinfo']['ip'] = $_SERVER['REMOTE_ADDR']; 604e403cc58SMichael Klier if($_SERVER['REMOTE_USER']) { 605e403cc58SMichael Klier $data['userinfo']['user'] = $_SERVER['REMOTE_USER']; 606e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 607e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 608e403cc58SMichael Klier } 609e403cc58SMichael Klier $callback = create_function('', 'return true;'); 610e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 611b9ac8716Schris } 612703f6fdeSandi } 613f3f0262cSandi return false; 614f3f0262cSandi} 615f3f0262cSandi 616f3f0262cSandi/** 61715fae107Sandi * Return the IP of the client 61815fae107Sandi * 6196d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 62015fae107Sandi * 6216d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 6226d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 6236d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 6246d8affe6SAndreas Gohr * headers 6256d8affe6SAndreas Gohr * 62615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6273272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 6283272d797SAndreas Gohr * @return string 629f3f0262cSandi */ 6306d8affe6SAndreas Gohrfunction clientIP($single = false) { 6316d8affe6SAndreas Gohr $ip = array(); 6326d8affe6SAndreas Gohr $ip[] = $_SERVER['REMOTE_ADDR']; 633bb4866bdSchris if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 6345cbeffbfSMarcel Pennewiß $ip = array_merge($ip, explode(',', str_replace(' ', '', $_SERVER['HTTP_X_FORWARDED_FOR']))); 635bb4866bdSchris if(!empty($_SERVER['HTTP_X_REAL_IP'])) 6365cbeffbfSMarcel Pennewiß $ip = array_merge($ip, explode(',', str_replace(' ', '', $_SERVER['HTTP_X_REAL_IP']))); 6376d8affe6SAndreas Gohr 638dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 639dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 640dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 641dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 642dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 643dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 644dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 645dc14c6d1SGuy Brand $IPv6Address = 646dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 647dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 648dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 649dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 650dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 651dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 652dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 653dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 654dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 655dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 656dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 657dc14c6d1SGuy Brand 6586d8affe6SAndreas Gohr // remove any non-IP stuff 6596d8affe6SAndreas Gohr $cnt = count($ip); 6604ff28443Schris $match = array(); 6616d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 662dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 6634ff28443Schris $ip[$i] = $match[0]; 6644ff28443Schris } else { 6654ff28443Schris $ip[$i] = ''; 6664ff28443Schris } 6676d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 668f3f0262cSandi } 6696d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 6706d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 6716d8affe6SAndreas Gohr 6726d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 6736d8affe6SAndreas Gohr 6746d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 6756d8affe6SAndreas Gohr $ip = array_reverse($ip); 6766d8affe6SAndreas Gohr foreach($ip as $i) { 6772343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 6786d8affe6SAndreas Gohr continue; 6796d8affe6SAndreas Gohr } else { 6806d8affe6SAndreas Gohr return $i; 6816d8affe6SAndreas Gohr } 6826d8affe6SAndreas Gohr } 6836d8affe6SAndreas Gohr // still here? just use the first (last) address 6846d8affe6SAndreas Gohr return $ip[0]; 685f3f0262cSandi} 686f3f0262cSandi 687f3f0262cSandi/** 6881c548ebeSAndreas Gohr * Check if the browser is on a mobile device 6891c548ebeSAndreas Gohr * 6901c548ebeSAndreas Gohr * Adapted from the example code at url below 6911c548ebeSAndreas Gohr * 6921c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 6931c548ebeSAndreas Gohr */ 6941c548ebeSAndreas Gohrfunction clientismobile() { 6951c548ebeSAndreas Gohr 6961c548ebeSAndreas Gohr if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) return true; 6971c548ebeSAndreas Gohr 6981c548ebeSAndreas Gohr if(preg_match('/wap\.|\.wap/i', $_SERVER['HTTP_ACCEPT'])) return true; 6991c548ebeSAndreas Gohr 7001c548ebeSAndreas Gohr if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; 7011c548ebeSAndreas Gohr 7021c548ebeSAndreas 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'; 7031c548ebeSAndreas Gohr 7041c548ebeSAndreas Gohr if(preg_match("/$uamatches/i", $_SERVER['HTTP_USER_AGENT'])) return true; 7051c548ebeSAndreas Gohr 7061c548ebeSAndreas Gohr return false; 7071c548ebeSAndreas Gohr} 7081c548ebeSAndreas Gohr 7091c548ebeSAndreas Gohr/** 71063211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 71163211f61SGlen Harris * 71222ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 71322ef1e32SAndreas Gohr * 71463211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 7153272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 7163272d797SAndreas Gohr * @return string a comma separated list of hostnames 71763211f61SGlen Harris */ 71863211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 71922ef1e32SAndreas Gohr global $conf; 72022ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 72122ef1e32SAndreas Gohr 72263211f61SGlen Harris $hosts = array(); 72363211f61SGlen Harris $ips = explode(',', $ips); 724551a720fSMichael Klier 725551a720fSMichael Klier if(is_array($ips)) { 7263886270dSAndreas Gohr foreach($ips as $ip) { 727551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 72863211f61SGlen Harris } 729551a720fSMichael Klier return join(',', $hosts); 730551a720fSMichael Klier } else { 731551a720fSMichael Klier return gethostbyaddr(trim($ips)); 732551a720fSMichael Klier } 73363211f61SGlen Harris} 73463211f61SGlen Harris 73563211f61SGlen Harris/** 73615fae107Sandi * Checks if a given page is currently locked. 73715fae107Sandi * 738f3f0262cSandi * removes stale lockfiles 73915fae107Sandi * 74015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 741f3f0262cSandi */ 742f3f0262cSandifunction checklock($id) { 743f3f0262cSandi global $conf; 744c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 745f3f0262cSandi 746f3f0262cSandi //no lockfile 747f3f0262cSandi if(!@file_exists($lock)) return false; 748f3f0262cSandi 749f3f0262cSandi //lockfile expired 750f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 751d8186216SBen Coburn @unlink($lock); 752f3f0262cSandi return false; 753f3f0262cSandi } 754f3f0262cSandi 755f3f0262cSandi //my own lock 75685fef7e2SAndreas Gohr list($ip, $session) = explode("\n", io_readFile($lock)); 75785fef7e2SAndreas Gohr if($ip == $_SERVER['REMOTE_USER'] || $ip == clientIP() || $session == session_id()) { 758f3f0262cSandi return false; 759f3f0262cSandi } 760f3f0262cSandi 761f3f0262cSandi return $ip; 762f3f0262cSandi} 763f3f0262cSandi 764f3f0262cSandi/** 76515fae107Sandi * Lock a page for editing 76615fae107Sandi * 76715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 768f3f0262cSandi */ 769f3f0262cSandifunction lock($id) { 770544ed901SDaniel Calviño Sánchez global $conf; 771544ed901SDaniel Calviño Sánchez 772544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 773544ed901SDaniel Calviño Sánchez return; 774544ed901SDaniel Calviño Sánchez } 775544ed901SDaniel Calviño Sánchez 776c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 777f3f0262cSandi if($_SERVER['REMOTE_USER']) { 778f3f0262cSandi io_saveFile($lock, $_SERVER['REMOTE_USER']); 779f3f0262cSandi } else { 78085fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 781f3f0262cSandi } 782f3f0262cSandi} 783f3f0262cSandi 784f3f0262cSandi/** 78515fae107Sandi * Unlock a page if it was locked by the user 786f3f0262cSandi * 78715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 7883272d797SAndreas Gohr * @param string $id page id to unlock 78915fae107Sandi * @return bool true if a lock was removed 790f3f0262cSandi */ 791f3f0262cSandifunction unlock($id) { 792c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 793f3f0262cSandi if(@file_exists($lock)) { 79485fef7e2SAndreas Gohr list($ip, $session) = explode("\n", io_readFile($lock)); 79585fef7e2SAndreas Gohr if($ip == $_SERVER['REMOTE_USER'] || $ip == clientIP() || $session == session_id()) { 796f3f0262cSandi @unlink($lock); 797f3f0262cSandi return true; 798f3f0262cSandi } 799f3f0262cSandi } 800f3f0262cSandi return false; 801f3f0262cSandi} 802f3f0262cSandi 803f3f0262cSandi/** 804f3f0262cSandi * convert line ending to unix format 805f3f0262cSandi * 80615fae107Sandi * @see formText() for 2crlf conversion 80715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 808f3f0262cSandi */ 809f3f0262cSandifunction cleanText($text) { 810f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 811f3f0262cSandi return $text; 812f3f0262cSandi} 813f3f0262cSandi 814f3f0262cSandi/** 815f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 816f3f0262cSandi * It also converts line endings to Windows format which is 817f3f0262cSandi * pseudo standard for webforms. 818f3f0262cSandi * 81915fae107Sandi * @see cleanText() for 2unix conversion 82015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 821f3f0262cSandi */ 822f3f0262cSandifunction formText($text) { 8235b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 824f3f0262cSandi return htmlspecialchars($text); 825f3f0262cSandi} 826f3f0262cSandi 827f3f0262cSandi/** 82815fae107Sandi * Returns the specified local text in raw format 82915fae107Sandi * 83015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 831f3f0262cSandi */ 8322adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 8332adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 834f3f0262cSandi} 835f3f0262cSandi 836f3f0262cSandi/** 837f3f0262cSandi * Returns the raw WikiText 83815fae107Sandi * 83915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 840f3f0262cSandi */ 841f3f0262cSandifunction rawWiki($id, $rev = '') { 842cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 843f3f0262cSandi} 844f3f0262cSandi 845f3f0262cSandi/** 8467146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 8477146cee2SAndreas Gohr * 8487b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 8497146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8507146cee2SAndreas Gohr */ 851fe17917eSAdrian Langfunction pageTemplate($id) { 852a15ce62dSEsther Brunner global $conf; 853e29549feSAndreas Gohr 854fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 855e29549feSAndreas Gohr 8567b84afa2SAndreas Gohr // prepare initial event data 8577b84afa2SAndreas Gohr $data = array( 8587b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 8597b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 8607b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 8617b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 8627b84afa2SAndreas Gohr ); 8637b84afa2SAndreas Gohr 8647b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 8657b84afa2SAndreas Gohr if($evt->advise_before(true)) { 8667b84afa2SAndreas Gohr // the before event might have loaded the content already 8677b84afa2SAndreas Gohr if(empty($data['tpl'])) { 8687b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 8697b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 870fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 871e29549feSAndreas Gohr if(@file_exists($path.'/_template.txt')) { 8727b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 873e29549feSAndreas Gohr } else { 874e29549feSAndreas Gohr // search upper namespaces for templates 875e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 876e29549feSAndreas Gohr while(strlen($path) >= $len) { 877e29549feSAndreas Gohr if(@file_exists($path.'/__template.txt')) { 8787b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 879e29549feSAndreas Gohr break; 880e29549feSAndreas Gohr } 881e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 882e29549feSAndreas Gohr } 883e29549feSAndreas Gohr } 8847b84afa2SAndreas Gohr } 8857b84afa2SAndreas Gohr // load the content 8863d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 8877b84afa2SAndreas Gohr } 888a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 8897b84afa2SAndreas Gohr } 8907b84afa2SAndreas Gohr $evt->advise_after(); 8917b84afa2SAndreas Gohr unset($evt); 8927b84afa2SAndreas Gohr 893fe17917eSAdrian Lang return $data['tpl']; 8942b1223ecSAdrian Lang} 8952b1223ecSAdrian Lang 8962b1223ecSAdrian Lang/** 8972b1223ecSAdrian Lang * Performs common page template replacements 8987b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 8992b1223ecSAdrian Lang * 9002b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 9012b1223ecSAdrian Lang */ 902d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 9033272d797SAndreas Gohr /** 9043272d797SAndreas Gohr * @var string $id the id of the page to be created 9053272d797SAndreas Gohr * @var string $tpl the text used as template 9063272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 9073272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 9083272d797SAndreas Gohr */ 909fe17917eSAdrian Lang extract($data); 910fe17917eSAdrian Lang 911b856f7dfSAdrian Lang global $USERINFO; 912bce53b1fSAdrian Lang global $conf; 913e29549feSAndreas Gohr 914e29549feSAndreas Gohr // replace placeholders 91526ece5a7SAndreas Gohr $file = noNS($id); 91637c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 91726ece5a7SAndreas Gohr 9183272d797SAndreas Gohr $tpl = str_replace( 9193272d797SAndreas Gohr array( 92026ece5a7SAndreas Gohr '@ID@', 92126ece5a7SAndreas Gohr '@NS@', 92226ece5a7SAndreas Gohr '@FILE@', 92326ece5a7SAndreas Gohr '@!FILE@', 92426ece5a7SAndreas Gohr '@!FILE!@', 92526ece5a7SAndreas Gohr '@PAGE@', 92626ece5a7SAndreas Gohr '@!PAGE@', 92726ece5a7SAndreas Gohr '@!!PAGE@', 92826ece5a7SAndreas Gohr '@!PAGE!@', 92926ece5a7SAndreas Gohr '@USER@', 93026ece5a7SAndreas Gohr '@NAME@', 93126ece5a7SAndreas Gohr '@MAIL@', 93226ece5a7SAndreas Gohr '@DATE@', 93326ece5a7SAndreas Gohr ), 93426ece5a7SAndreas Gohr array( 93526ece5a7SAndreas Gohr $id, 93626ece5a7SAndreas Gohr getNS($id), 93726ece5a7SAndreas Gohr $file, 93826ece5a7SAndreas Gohr utf8_ucfirst($file), 93926ece5a7SAndreas Gohr utf8_strtoupper($file), 94026ece5a7SAndreas Gohr $page, 94126ece5a7SAndreas Gohr utf8_ucfirst($page), 94226ece5a7SAndreas Gohr utf8_ucwords($page), 94326ece5a7SAndreas Gohr utf8_strtoupper($page), 94426ece5a7SAndreas Gohr $_SERVER['REMOTE_USER'], 945b856f7dfSAdrian Lang $USERINFO['name'], 946b856f7dfSAdrian Lang $USERINFO['mail'], 94726ece5a7SAndreas Gohr $conf['dformat'], 9483272d797SAndreas Gohr ), $tpl 9493272d797SAndreas Gohr ); 95026ece5a7SAndreas Gohr 9517d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 9527d644fc8SAndreas Gohr $tpl = preg_replace_callback('/%./', create_function('$m', 'return strftime($m[0]);'), $tpl); 953d535a2e9Sstretchyboy $data['tpl'] = $tpl; 954a15ce62dSEsther Brunner return $tpl; 9557146cee2SAndreas Gohr} 9567146cee2SAndreas Gohr 9577146cee2SAndreas Gohr/** 95815fae107Sandi * Returns the raw Wiki Text in three slices. 95915fae107Sandi * 96015fae107Sandi * The range parameter needs to have the form "from-to" 96115cfe303Sandi * and gives the range of the section in bytes - no 96215cfe303Sandi * UTF-8 awareness is needed. 963f3f0262cSandi * The returned order is prefix, section and suffix. 96415fae107Sandi * 96515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 966f3f0262cSandi */ 967f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 968cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 969f3f0262cSandi 97080fcb268SAdrian Lang // Parse range 97180fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 97280fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 97380fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 97480fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 97580fcb268SAdrian Lang 97680fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 97780fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 97815cfe303Sandi $slices[2] = substr($text, $to); 979f3f0262cSandi return $slices; 980f3f0262cSandi} 981f3f0262cSandi 982f3f0262cSandi/** 98315fae107Sandi * Joins wiki text slices 98415fae107Sandi * 98580fcb268SAdrian Lang * function to join the text slices. 986f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 987f3f0262cSandi * lines between sections if needed (used on saving). 98815fae107Sandi * 98915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 990f3f0262cSandi */ 991f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 992f3f0262cSandi if($pretty) { 99380fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 9943272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 9953272d797SAndreas Gohr ) { 99680fcb268SAdrian Lang $pre .= "\n"; 99780fcb268SAdrian Lang } 99880fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 9993272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 10003272d797SAndreas Gohr ) { 100180fcb268SAdrian Lang $text .= "\n"; 100280fcb268SAdrian Lang } 1003f3f0262cSandi } 1004f3f0262cSandi 1005f3f0262cSandi return $pre.$text.$suf; 1006f3f0262cSandi} 1007f3f0262cSandi 1008f3f0262cSandi/** 1009a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1010a701424fSBen Coburn * Also directs changelog and attic updates. 101115fae107Sandi * 101215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 101371726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1014f3f0262cSandi */ 1015b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1016a701424fSBen Coburn /* Note to developers: 1017a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1018a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1019a701424fSBen Coburn after any changes. External edits change the wiki page 1020a701424fSBen Coburn directly without using php or dokuwiki. 1021a701424fSBen Coburn */ 1022f3f0262cSandi global $conf; 1023f3f0262cSandi global $lang; 102471726d78SBen Coburn global $REV; 1025f3f0262cSandi // ignore if no changes were made 1026f3f0262cSandi if($text == rawWiki($id, '')) { 1027f3f0262cSandi return; 1028f3f0262cSandi } 1029f3f0262cSandi 1030f3f0262cSandi $file = wikiFN($id); 1031a701424fSBen Coburn $old = @filemtime($file); // from page 1032407e65b9SAndreas Gohr $wasRemoved = (trim($text) == ''); // check for empty or whitespace only 1033d8186216SBen Coburn $wasCreated = !@file_exists($file); 103471726d78SBen Coburn $wasReverted = ($REV == true); 1035e45b34cdSBen Coburn $newRev = false; 1036a701424fSBen Coburn $oldRev = getRevisions($id, -1, 1, 1024); // from changelog 1037a701424fSBen Coburn $oldRev = (int) (empty($oldRev) ? 0 : $oldRev[0]); 1038a701424fSBen Coburn if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old >= $oldRev) { 103946844156SBen Coburn // add old revision to the attic if missing 104046844156SBen Coburn saveOldRevision($id); 104146844156SBen Coburn // add a changelog entry if this edit came from outside dokuwiki 1042a701424fSBen Coburn if($old > $oldRev) { 1043ebf1501fSBen Coburn addLogEntry($old, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=> true)); 104446844156SBen Coburn // remove soon to be stale instructions 104546844156SBen Coburn $cache = new cache_instructions($id, $file); 104646844156SBen Coburn $cache->removeCache(); 104746844156SBen Coburn } 104846844156SBen Coburn } 1049f3f0262cSandi 105071726d78SBen Coburn if($wasRemoved) { 105130725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 105230725328SGabriel Birke $data = array(array($file, '', false), getNS($id), noNS($id), false); 105330725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1054e45b34cdSBen Coburn // pre-save deleted revision 1055e45b34cdSBen Coburn @touch($file); 105646844156SBen Coburn clearstatcache(); 1057e45b34cdSBen Coburn $newRev = saveOldRevision($id); 1058e1f3d9e1SEsther Brunner // remove empty file 1059f3f0262cSandi @unlink($file); 1060c5f92742SMichael Hamann // don't remove old meta info as it should be saved, plugins can use IO_WIKIPAGE_WRITE for removing their metadata... 1061c5f92742SMichael Hamann // purge non-persistant meta data 10623d1f9ec3SMichael Klier p_purge_metadata($id); 1063f3f0262cSandi $del = true; 10643ce054b3Sandi // autoset summary on deletion 10653ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 106653d6ccfeSandi // remove empty namespaces 1067cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1068cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1069f3f0262cSandi } else { 1070cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 1071cc7d0c94SBen Coburn io_writeWikiPage($file, $text, $id); 107246844156SBen Coburn // pre-save the revision, to keep the attic in sync 107346844156SBen Coburn $newRev = saveOldRevision($id); 1074f3f0262cSandi $del = false; 1075f3f0262cSandi } 1076f3f0262cSandi 107771726d78SBen Coburn // select changelog line type 107871726d78SBen Coburn $extra = ''; 1079ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_EDIT; 108071726d78SBen Coburn if($wasReverted) { 1081ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_REVERT; 108271726d78SBen Coburn $extra = $REV; 10833272d797SAndreas Gohr } else if($wasCreated) { 10843272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_CREATE; 10853272d797SAndreas Gohr } else if($wasRemoved) { 10863272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_DELETE; 10873272d797SAndreas Gohr } else if($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { 10883272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_MINOR_EDIT; 10893272d797SAndreas Gohr } //minor edits only for logged in users 109071726d78SBen Coburn 1091e45b34cdSBen Coburn addLogEntry($newRev, $id, $type, $summary, $extra); 109226a0801fSAndreas Gohr // send notify mails 109390033e9dSAndreas Gohr notify($id, 'admin', $old, $summary, $minor); 109490033e9dSAndreas Gohr notify($id, 'subscribers', $old, $summary, $minor); 1095f3f0262cSandi 1096ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 109798407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 10982eccbdaaSGina Haeussge 10992eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1100fe9ec250SChris Smith if(useHeading('content')) { 11012eccbdaaSGina Haeussge $pages = ft_backlinks($id); 11022eccbdaaSGina Haeussge foreach($pages as $page) { 11032eccbdaaSGina Haeussge $cache = new cache_renderer($page, wikiFN($page), 'xhtml'); 11042eccbdaaSGina Haeussge $cache->removeCache(); 11052eccbdaaSGina Haeussge } 11062eccbdaaSGina Haeussge } 1107f3f0262cSandi} 1108f3f0262cSandi 1109f3f0262cSandi/** 1110f3f0262cSandi * moves the current version to the attic and returns its 1111f3f0262cSandi * revision date 111215fae107Sandi * 111315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1114f3f0262cSandi */ 1115f3f0262cSandifunction saveOldRevision($id) { 1116f3f0262cSandi global $conf; 1117f3f0262cSandi $oldf = wikiFN($id); 1118f3f0262cSandi if(!@file_exists($oldf)) return ''; 1119f3f0262cSandi $date = filemtime($oldf); 1120f3f0262cSandi $newf = wikiFN($id, $date); 1121cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1122f3f0262cSandi return $date; 1123f3f0262cSandi} 1124f3f0262cSandi 1125f3f0262cSandi/** 1126fde10de4SAdrian Lang * Sends a notify mail on page change or registration 112726a0801fSAndreas Gohr * 112826a0801fSAndreas Gohr * @param string $id The changed page 1129fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 11303272d797SAndreas Gohr * @param int|string $rev Old page revision 113126a0801fSAndreas Gohr * @param string $summary What changed 113290033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 113302a498e7Schris * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 113415fae107Sandi * 11353272d797SAndreas Gohr * @return bool 113615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1137f3f0262cSandi */ 113802a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1139f3f0262cSandi global $conf; 1140b158d625SSteven Danz 11416df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 114226a0801fSAndreas Gohr if($who == 'admin') { 11433272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 11442ed38036SAndreas Gohr $tpl = 'mailtext'; 114526a0801fSAndreas Gohr $to = $conf['notify']; 114626a0801fSAndreas Gohr } elseif($who == 'subscribers') { 114784c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 11483272d797SAndreas Gohr if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return false; //skip minors 11498881fcc9SAdrian Lang $data = array('id' => $id, 'addresslist' => '', 'self' => false); 11503272d797SAndreas Gohr trigger_event( 11513272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1152835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 11533272d797SAndreas Gohr ); 11542ed38036SAndreas Gohr $to = $data['addresslist']; 11552ed38036SAndreas Gohr if(empty($to)) return false; 11562ed38036SAndreas Gohr $tpl = 'subscr_single'; 115726a0801fSAndreas Gohr } else { 11583272d797SAndreas Gohr return false; //just to be safe 115926a0801fSAndreas Gohr } 116026a0801fSAndreas Gohr 11616df843eeSAndreas Gohr // prepare content 11622ed38036SAndreas Gohr $subscription = new Subscription(); 11632ed38036SAndreas Gohr return $subscription->send_diff($to, $tpl, $id, $rev, $summary); 1164f3f0262cSandi} 11652ed38036SAndreas Gohr 116615fae107Sandi/** 116771f7bde7SAndreas Gohr * extracts the query from a search engine referrer 116815fae107Sandi * 116915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 117071f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1171f3f0262cSandi */ 1172f3f0262cSandifunction getGoogleQuery() { 1173c66972f2SAdrian Lang if(!isset($_SERVER['HTTP_REFERER'])) { 1174c66972f2SAdrian Lang return ''; 1175c66972f2SAdrian Lang } 1176f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 1177f3f0262cSandi 1178079b3ac1SAndreas Gohr // only handle common SEs 1179079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1180e4d8a516SKazutaka Miyasaka 1181079b3ac1SAndreas Gohr $query = array(); 1182e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1183e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1184e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1185f3f0262cSandi parse_str($url['query'], $query); 1186e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1187e4d8a516SKazutaka Miyasaka 1188c66972f2SAdrian Lang $q = ''; 1189079b3ac1SAndreas Gohr if(isset($query['q'])){ 1190079b3ac1SAndreas Gohr $q = $query['q']; 1191079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1192079b3ac1SAndreas Gohr $q = $query['p']; 1193079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1194079b3ac1SAndreas Gohr $q = $query['query']; 1195079b3ac1SAndreas Gohr } 1196079b3ac1SAndreas Gohr $q = trim($q); 1197f3f0262cSandi 1198079b3ac1SAndreas Gohr if(!$q) return ''; 11996531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1200f93b3b50SAndreas Gohr return $q; 1201f3f0262cSandi} 1202f3f0262cSandi 1203f3f0262cSandi/** 120415fae107Sandi * Try to set correct locale 120515fae107Sandi * 1206095bfd5cSandi * @deprecated No longer used 120715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1208f3f0262cSandi */ 1209f3f0262cSandifunction setCorrectLocale() { 1210f3f0262cSandi global $conf; 1211f3f0262cSandi global $lang; 1212f3f0262cSandi 1213f3f0262cSandi $enc = strtoupper($lang['encoding']); 1214f3f0262cSandi foreach($lang['locales'] as $loc) { 1215f3f0262cSandi //try locale 1216f3f0262cSandi if(@setlocale(LC_ALL, $loc)) return; 1217f3f0262cSandi //try loceale with encoding 1218f3f0262cSandi if(@setlocale(LC_ALL, "$loc.$enc")) return; 1219f3f0262cSandi } 1220f3f0262cSandi //still here? try to set from environment 1221f3f0262cSandi @setlocale(LC_ALL, ""); 1222f3f0262cSandi} 1223f3f0262cSandi 1224f3f0262cSandi/** 1225f3f0262cSandi * Return the human readable size of a file 1226f3f0262cSandi * 1227f3f0262cSandi * @param int $size A file size 1228f3f0262cSandi * @param int $dec A number of decimal places 1229f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1230f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1231f3f0262cSandi * @version 1.0.0 1232f3f0262cSandi */ 1233f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1234f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1235f3f0262cSandi $count = count($sizes); 1236f3f0262cSandi $i = 0; 1237f3f0262cSandi 1238f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1239f3f0262cSandi $size /= 1024; 1240f3f0262cSandi $i++; 1241f3f0262cSandi } 1242f3f0262cSandi 1243f3f0262cSandi return round($size, $dec).' '.$sizes[$i]; 1244f3f0262cSandi} 1245f3f0262cSandi 124615fae107Sandi/** 1247c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1248c57e365eSAndreas Gohr * 1249c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1250c57e365eSAndreas Gohr */ 1251c57e365eSAndreas Gohrfunction datetime_h($dt) { 1252c57e365eSAndreas Gohr global $lang; 1253c57e365eSAndreas Gohr 1254c57e365eSAndreas Gohr $ago = time() - $dt; 1255c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1256c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1257c57e365eSAndreas Gohr } 1258c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1259c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1260c57e365eSAndreas Gohr } 1261c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1262c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1263c57e365eSAndreas Gohr } 1264c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1265c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1266c57e365eSAndreas Gohr } 1267c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1268c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1269c57e365eSAndreas Gohr } 1270c57e365eSAndreas Gohr if($ago > 60 * 2) { 1271c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1272c57e365eSAndreas Gohr } 1273c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1274c57e365eSAndreas Gohr} 1275c57e365eSAndreas Gohr 1276c57e365eSAndreas Gohr/** 1277f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1278f2263577SAndreas Gohr * 1279f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1280f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1281f2263577SAndreas Gohr * 1282f2263577SAndreas Gohr * @see datetime_h 1283f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1284f2263577SAndreas Gohr */ 1285f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1286f2263577SAndreas Gohr global $conf; 1287f2263577SAndreas Gohr 1288f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1289f2263577SAndreas Gohr $dt = (int) $dt; 1290f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1291f2263577SAndreas Gohr 1292f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1293f2263577SAndreas Gohr return strftime($format, $dt); 1294f2263577SAndreas Gohr} 1295f2263577SAndreas Gohr 1296f2263577SAndreas Gohr/** 1297c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1298c4f79b71SMichael Hamann * 1299c4f79b71SMichael Hamann * @author <ungu at terong dot com> 1300c4f79b71SMichael Hamann * @link http://www.php.net/manual/en/function.date.php#54072 130163703ba5SAndreas Gohr * @param int $int_date: current date in UNIX timestamp 13023272d797SAndreas Gohr * @return string 1303c4f79b71SMichael Hamann */ 1304c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1305c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1306c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1307c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1308c4f79b71SMichael Hamann $date_mod .= $time_zone; 1309c4f79b71SMichael Hamann return $date_mod; 1310c4f79b71SMichael Hamann} 1311c4f79b71SMichael Hamann 1312c4f79b71SMichael Hamann/** 131300a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 131400a7b5adSEsther Brunner * 131500a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 131600a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 131700a7b5adSEsther Brunner */ 131800a7b5adSEsther Brunnerfunction obfuscate($email) { 131900a7b5adSEsther Brunner global $conf; 132000a7b5adSEsther Brunner 132100a7b5adSEsther Brunner switch($conf['mailguard']) { 132200a7b5adSEsther Brunner case 'visible' : 132300a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 132400a7b5adSEsther Brunner return strtr($email, $obfuscate); 132500a7b5adSEsther Brunner 132600a7b5adSEsther Brunner case 'hex' : 132700a7b5adSEsther Brunner $encode = ''; 132849eb6e38SAndreas Gohr $len = strlen($email); 132949eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 133049eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 133149eb6e38SAndreas Gohr } 133200a7b5adSEsther Brunner return $encode; 133300a7b5adSEsther Brunner 133400a7b5adSEsther Brunner case 'none' : 133500a7b5adSEsther Brunner default : 133600a7b5adSEsther Brunner return $email; 133700a7b5adSEsther Brunner } 133800a7b5adSEsther Brunner} 133900a7b5adSEsther Brunner 134000a7b5adSEsther Brunner/** 134189541d4bSAndreas Gohr * Removes quoting backslashes 134289541d4bSAndreas Gohr * 134389541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 134489541d4bSAndreas Gohr */ 134589541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 134689541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 134789541d4bSAndreas Gohr} 134889541d4bSAndreas Gohr 134973038c47SAndreas Gohr/** 135073038c47SAndreas Gohr * Convert php.ini shorthands to byte 135173038c47SAndreas Gohr * 135273038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 135373038c47SAndreas Gohr * @link http://de3.php.net/manual/en/ini.core.php#79564 135473038c47SAndreas Gohr */ 135573038c47SAndreas Gohrfunction php_to_byte($v) { 135673038c47SAndreas Gohr $l = substr($v, -1); 135773038c47SAndreas Gohr $ret = substr($v, 0, -1); 135873038c47SAndreas Gohr switch(strtoupper($l)) { 135973038c47SAndreas Gohr case 'P': 136073038c47SAndreas Gohr $ret *= 1024; 136173038c47SAndreas Gohr case 'T': 136273038c47SAndreas Gohr $ret *= 1024; 136373038c47SAndreas Gohr case 'G': 136473038c47SAndreas Gohr $ret *= 1024; 136573038c47SAndreas Gohr case 'M': 136673038c47SAndreas Gohr $ret *= 1024; 136773038c47SAndreas Gohr case 'K': 136873038c47SAndreas Gohr $ret *= 1024; 136973038c47SAndreas Gohr break; 137049cbd23eSOtto Vainio default; 137149cbd23eSOtto Vainio $ret *= 10; 137249cbd23eSOtto Vainio break; 137373038c47SAndreas Gohr } 137473038c47SAndreas Gohr return $ret; 137573038c47SAndreas Gohr} 137673038c47SAndreas Gohr 1377546d3a99SAndreas Gohr/** 1378546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1379546d3a99SAndreas Gohr */ 1380546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1381546d3a99SAndreas Gohr return preg_quote($string, '/'); 1382546d3a99SAndreas Gohr} 138373038c47SAndreas Gohr 1384bd2f6c2fSAndreas Gohr/** 1385bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1386bd2f6c2fSAndreas Gohr * 1387c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1388bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1389bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1390bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1391bd2f6c2fSAndreas Gohr * 1392bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1393bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1394bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1395bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1396bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 13973272d797SAndreas Gohr * @return string 1398bd2f6c2fSAndreas Gohr */ 1399a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 1400bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1401bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1402bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1403bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1404bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1405bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1406bd2f6c2fSAndreas Gohr} 1407bd2f6c2fSAndreas Gohr 1408dc58b6f4SAndy Webber/** 1409dc58b6f4SAndy Webber * Return the users realname or e-mail address for use 1410dc58b6f4SAndy Webber * in page footer and recent changes pages 1411dc58b6f4SAndy Webber * 1412dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1413dc58b6f4SAndy Webber */ 1414dc58b6f4SAndy Webberfunction editorinfo($username) { 1415dc58b6f4SAndy Webber global $conf; 1416dc58b6f4SAndy Webber global $auth; 1417dc58b6f4SAndy Webber 1418dc58b6f4SAndy Webber switch($conf['showuseras']) { 1419dc58b6f4SAndy Webber case 'username': 1420dc58b6f4SAndy Webber case 'email': 1421dc58b6f4SAndy Webber case 'email_link': 1422173d78c4SAndreas Gohr if($auth) $info = $auth->getUserData($username); 1423dc58b6f4SAndy Webber break; 1424dc58b6f4SAndy Webber default: 1425dc58b6f4SAndy Webber return hsc($username); 1426dc58b6f4SAndy Webber } 1427dc58b6f4SAndy Webber 1428dc58b6f4SAndy Webber if(isset($info) && $info) { 1429dc58b6f4SAndy Webber switch($conf['showuseras']) { 1430dc58b6f4SAndy Webber case 'username': 1431dc58b6f4SAndy Webber return hsc($info['name']); 1432dc58b6f4SAndy Webber case 'email': 1433dc58b6f4SAndy Webber return obfuscate($info['mail']); 1434dc58b6f4SAndy Webber case 'email_link': 1435dc58b6f4SAndy Webber $mail = obfuscate($info['mail']); 1436dc58b6f4SAndy Webber return '<a href="mailto:'.$mail.'">'.$mail.'</a>'; 1437dc58b6f4SAndy Webber default: 1438dc58b6f4SAndy Webber return hsc($username); 1439dc58b6f4SAndy Webber } 1440dc58b6f4SAndy Webber } else { 1441dc58b6f4SAndy Webber return hsc($username); 1442dc58b6f4SAndy Webber } 1443066fee30SAndreas Gohr} 1444066fee30SAndreas Gohr 1445066fee30SAndreas Gohr/** 1446066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1447066fee30SAndreas Gohr * When no image exists, returns an empty string 1448066fee30SAndreas Gohr * 1449066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1450066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 14513272d797SAndreas Gohr * @return string 1452066fee30SAndreas Gohr */ 1453066fee30SAndreas Gohrfunction license_img($type) { 1454066fee30SAndreas Gohr global $license; 1455066fee30SAndreas Gohr global $conf; 1456066fee30SAndreas Gohr if(!$conf['license']) return ''; 1457066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1458066fee30SAndreas Gohr $lic = $license[$conf['license']]; 1459066fee30SAndreas Gohr $try = array(); 1460066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1461066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1462066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1463066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1464066fee30SAndreas Gohr } 1465066fee30SAndreas Gohr foreach($try as $src) { 1466066fee30SAndreas Gohr if(@file_exists(DOKU_INC.$src)) return $src; 1467066fee30SAndreas Gohr } 1468066fee30SAndreas Gohr return ''; 1469dc58b6f4SAndy Webber} 1470dc58b6f4SAndy Webber 147113c08e2fSMichael Klier/** 147213c08e2fSMichael Klier * Checks if the given amount of memory is available 147313c08e2fSMichael Klier * 147413c08e2fSMichael Klier * If the memory_get_usage() function is not available the 147513c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 147613c08e2fSMichael Klier * 147713c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 147813c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 14793272d797SAndreas Gohr * 14803272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 14813272d797SAndreas Gohr * @param int $bytes 14823272d797SAndreas Gohr * @internal param int $used already allocated memory (see above) 14833272d797SAndreas Gohr * @return bool 148413c08e2fSMichael Klier */ 148513c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 148613c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 148713c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 148813c08e2fSMichael Klier 148913c08e2fSMichael Klier // parse limit to bytes 149013c08e2fSMichael Klier $limit = php_to_byte($limit); 149113c08e2fSMichael Klier 149213c08e2fSMichael Klier // get used memory if possible 149313c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 149413c08e2fSMichael Klier $used = memory_get_usage(); 149549eb6e38SAndreas Gohr } else { 149649eb6e38SAndreas Gohr $used = $bytes; 149713c08e2fSMichael Klier } 149813c08e2fSMichael Klier 149913c08e2fSMichael Klier if($used + $mem > $limit) { 150013c08e2fSMichael Klier return false; 150113c08e2fSMichael Klier } 150213c08e2fSMichael Klier 150313c08e2fSMichael Klier return true; 150413c08e2fSMichael Klier} 150513c08e2fSMichael Klier 1506af2408d5SAndreas Gohr/** 1507af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1508af2408d5SAndreas Gohr * 1509af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1510af2408d5SAndreas Gohr * 1511af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1512af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1513af2408d5SAndreas Gohr */ 1514af2408d5SAndreas Gohrfunction send_redirect($url) { 15150181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 15160181f021SAndreas Gohr global $MSG; 15170181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 15180181f021SAndreas Gohr //reopen session, store data and close session again 15190181f021SAndreas Gohr @session_start(); 15200181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 15210181f021SAndreas Gohr } 15220181f021SAndreas Gohr 1523d4869846SAndreas Gohr // always close the session 1524d4869846SAndreas Gohr session_write_close(); 1525d4869846SAndreas Gohr 1526c10dcb7dSAndreas Gohr // work around IE bug 1527c10dcb7dSAndreas Gohr // http://www.ianhoar.com/2008/11/16/internet-explorer-6-and-redirected-anchor-links/ 1528c10dcb7dSAndreas Gohr list($url, $hash) = explode('#', $url); 1529c10dcb7dSAndreas Gohr if($hash) { 1530c10dcb7dSAndreas Gohr if(strpos($url, '?')) { 1531c10dcb7dSAndreas Gohr $url = $url.'&#'.$hash; 1532c10dcb7dSAndreas Gohr } else { 1533c10dcb7dSAndreas Gohr $url = $url.'?&#'.$hash; 1534c10dcb7dSAndreas Gohr } 1535c10dcb7dSAndreas Gohr } 1536c10dcb7dSAndreas Gohr 1537af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1538af2408d5SAndreas Gohr if(isset($_SERVER['SERVER_SOFTWARE']) && isset($_SERVER['GATEWAY_INTERFACE']) && 1539af2408d5SAndreas Gohr (strpos($_SERVER['GATEWAY_INTERFACE'], 'CGI') !== false) && 1540af2408d5SAndreas Gohr (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) && 15413272d797SAndreas Gohr $matches[1] < 6 15423272d797SAndreas Gohr ) { 1543af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1544af2408d5SAndreas Gohr } else { 1545af2408d5SAndreas Gohr header('Location: '.$url); 1546af2408d5SAndreas Gohr } 1547af2408d5SAndreas Gohr exit; 1548af2408d5SAndreas Gohr} 1549af2408d5SAndreas Gohr 15505b75cd1fSAdrian Lang/** 15515b75cd1fSAdrian Lang * Validate a value using a set of valid values 15525b75cd1fSAdrian Lang * 15535b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 15545b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 15555b75cd1fSAdrian Lang * default is specified, throws an exception. 15565b75cd1fSAdrian Lang * 15575b75cd1fSAdrian Lang * @param string $param The name of the parameter 15585b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 15595b75cd1fSAdrian Lang * be marked by the key “default”. 15605b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 15615b75cd1fSAdrian Lang * or $_GET) 15625b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 15635b75cd1fSAdrian Lang * 15643272d797SAndreas Gohr * @throws Exception 15653272d797SAndreas Gohr * @return mixed 15665b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 15675b75cd1fSAdrian Lang */ 15685b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 15695b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 15705b75cd1fSAdrian Lang return $array[$param]; 15715b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 15725b75cd1fSAdrian Lang return $valid_values['default']; 15735b75cd1fSAdrian Lang } else { 15745b75cd1fSAdrian Lang throw new Exception($exc); 15755b75cd1fSAdrian Lang } 15765b75cd1fSAdrian Lang} 15775b75cd1fSAdrian Lang 157863703ba5SAndreas Gohr/** 157963703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1580646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 158163703ba5SAndreas Gohr */ 1582554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1583646a531aSChristopher Smith $enc_pref = urlencode($pref); 1584646a531aSChristopher Smith if(strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1585554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 158663703ba5SAndreas Gohr $cnt = count($parts); 158763703ba5SAndreas Gohr for($i = 0; $i < $cnt; $i += 2) { 1588646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1589646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1590554a8c9fSAdrian Lang } 1591554a8c9fSAdrian Lang } 1592554a8c9fSAdrian Lang } 1593554a8c9fSAdrian Lang return $default; 1594554a8c9fSAdrian Lang} 1595554a8c9fSAdrian Lang 15963c94d07bSAnika Henke/** 15973c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 159836ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 15993c94d07bSAnika Henke */ 16003c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 16013c94d07bSAnika Henke global $conf; 16023c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 16033c94d07bSAnika Henke $cookieVal = ''; 16043c94d07bSAnika Henke 16053c94d07bSAnika Henke if($orig && ($orig != $val)) { 16063c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 16073c94d07bSAnika Henke $cnt = count($parts); 160836ec377eSChristopher Smith // urlencode $pref for the comparison 160936ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 16103c94d07bSAnika Henke for($i = 0; $i < $cnt; $i += 2) { 161136ec377eSChristopher Smith if($parts[$i] == $enc_pref) { 161236ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 161350f261f7SMichael Hamann break; 16143c94d07bSAnika Henke } 16153c94d07bSAnika Henke } 16163c94d07bSAnika Henke $cookieVal = implode('#', $parts); 16173c94d07bSAnika Henke } else if (!$orig) { 161836ec377eSChristopher Smith $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : '').rawurlencode($pref).'#'.rawurlencode($val); 16193c94d07bSAnika Henke } 16203c94d07bSAnika Henke 16213c94d07bSAnika Henke if (!empty($cookieVal)) { 162250f261f7SMichael Hamann setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, DOKU_BASE, '', ($conf['securecookie'] && is_ssl())); 16233c94d07bSAnika Henke } 16243c94d07bSAnika Henke} 16253c94d07bSAnika Henke 1626e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1627