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/** 8915fae107Sandi * Return info about the current document as associative 90f3f0262cSandi * array. 9115fae107Sandi * 9215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 93f3f0262cSandi */ 94f3f0262cSandifunction pageinfo() { 95f3f0262cSandi global $ID; 96f3f0262cSandi global $REV; 977b3a6803SAndreas Gohr global $RANGE; 98f3f0262cSandi global $USERINFO; 997b3a6803SAndreas Gohr global $lang; 100f3f0262cSandi 1016afe8dcaSchris // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 1026afe8dcaSchris // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 1036afe8dcaSchris $info['id'] = $ID; 1046afe8dcaSchris $info['rev'] = $REV; 1056afe8dcaSchris 106c66972f2SAdrian Lang // set info about manager/admin status. 107c66972f2SAdrian Lang $info['isadmin'] = false; 108c66972f2SAdrian Lang $info['ismanager'] = false; 109c66972f2SAdrian Lang if(isset($_SERVER['REMOTE_USER'])) { 110adec979fSAndreas Gohr $sub = new Subscription(); 111adec979fSAndreas Gohr 112f3f0262cSandi $info['userinfo'] = $USERINFO; 113f3f0262cSandi $info['perm'] = auth_quickaclcheck($ID); 114adec979fSAndreas Gohr $info['subscribed'] = $sub->user_subscription(); 115ee4c4a1bSAndreas Gohr $info['client'] = $_SERVER['REMOTE_USER']; 11617ee7f66SAndreas Gohr 117f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 118f8cc712eSAndreas Gohr $info['isadmin'] = true; 119f8cc712eSAndreas Gohr $info['ismanager'] = true; 120f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 121f8cc712eSAndreas Gohr $info['ismanager'] = true; 122f8cc712eSAndreas Gohr } 123f8cc712eSAndreas Gohr 12417ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 12517ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 12617ee7f66SAndreas Gohr $info['userinfo']['name'] = $_SERVER['REMOTE_USER']; 12717ee7f66SAndreas Gohr } 128ee4c4a1bSAndreas Gohr 129f3f0262cSandi } else { 130f3f0262cSandi $info['perm'] = auth_aclcheck($ID, '', null); 1311380fc45SAndreas Gohr $info['subscribed'] = false; 132ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 133f3f0262cSandi } 134f3f0262cSandi 135f3f0262cSandi $info['namespace'] = getNS($ID); 136f3f0262cSandi $info['locked'] = checklock($ID); 13700976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID)); 1382ca9d91cSBen Coburn $info['exists'] = @file_exists($info['filepath']); 1392ca9d91cSBen Coburn if($REV) { 1402ca9d91cSBen Coburn //check if current revision was meant 1412ca9d91cSBen Coburn if($info['exists'] && (@filemtime($info['filepath']) == $REV)) { 1422ca9d91cSBen Coburn $REV = ''; 1437b3a6803SAndreas Gohr } elseif($RANGE) { 1447b3a6803SAndreas Gohr //section editing does not work with old revisions! 1457b3a6803SAndreas Gohr $REV = ''; 1467b3a6803SAndreas Gohr $RANGE = ''; 1477b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 1482ca9d91cSBen Coburn } else { 1492ca9d91cSBen Coburn //really use old revision 15000976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID, $REV)); 151f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 152f3f0262cSandi } 153f3f0262cSandi } 154c112d578Sandi $info['rev'] = $REV; 155f3f0262cSandi if($info['exists']) { 156f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 157f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 158f3f0262cSandi } else { 159f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 160f3f0262cSandi } 16150e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 162f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 163f3f0262cSandi 16471726d78SBen Coburn //load page meta data 16571726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 16671726d78SBen Coburn 167652610a2Sandi //who's the editor 168652610a2Sandi if($REV) { 16971726d78SBen Coburn $revinfo = getRevisionInfo($ID, $REV, 1024); 170652610a2Sandi } else { 171aa27cf05SAndreas Gohr if(is_array($info['meta']['last_change'])) { 172aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 173aa27cf05SAndreas Gohr } else { 174cd00a034SBen Coburn $revinfo = getRevisionInfo($ID, $info['lastmod'], 1024); 175cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 176cd00a034SBen Coburn if($revinfo !== false) { 177cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 178cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 179cd00a034SBen Coburn } 180cd00a034SBen Coburn } 181cd00a034SBen Coburn } 182cd00a034SBen Coburn //and check for an external edit 183cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 184cd00a034SBen Coburn // cached changelog line no longer valid 185cd00a034SBen Coburn $revinfo = false; 186cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 187cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 188652610a2Sandi } 189bb4866bdSchris 190652610a2Sandi $info['ip'] = $revinfo['ip']; 191652610a2Sandi $info['user'] = $revinfo['user']; 192652610a2Sandi $info['sum'] = $revinfo['sum']; 19371726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 194ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 19559f257aeSchris 19688f522e9Sandi if($revinfo['user']) { 19788f522e9Sandi $info['editor'] = $revinfo['user']; 19888f522e9Sandi } else { 19988f522e9Sandi $info['editor'] = $revinfo['ip']; 20088f522e9Sandi } 201652610a2Sandi 202ee4c4a1bSAndreas Gohr // draft 203ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID, '.draft'); 204ee4c4a1bSAndreas Gohr if(@file_exists($draft)) { 205ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))) { 206ee4c4a1bSAndreas Gohr // remove stale draft 207ee4c4a1bSAndreas Gohr @unlink($draft); 208ee4c4a1bSAndreas Gohr } else { 209ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 210ee4c4a1bSAndreas Gohr } 211ee4c4a1bSAndreas Gohr } 212ee4c4a1bSAndreas Gohr 2131c548ebeSAndreas Gohr // mobile detection 2141c548ebeSAndreas Gohr $info['ismobile'] = clientismobile(); 2151c548ebeSAndreas Gohr 216f3f0262cSandi return $info; 217f3f0262cSandi} 218f3f0262cSandi 219f3f0262cSandi/** 2202684e50aSAndreas Gohr * Build an string of URL parameters 2212684e50aSAndreas Gohr * 2222684e50aSAndreas Gohr * @author Andreas Gohr 2232684e50aSAndreas Gohr */ 224b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 2252684e50aSAndreas Gohr $url = ''; 2262684e50aSAndreas Gohr $amp = false; 2272684e50aSAndreas Gohr foreach($params as $key => $val) { 228b174aeaeSchris if($amp) $url .= $sep; 2292684e50aSAndreas Gohr 23085e6871fSAdrian Lang $url .= rawurlencode($key).'='; 2313a50618cSgweissbach $url .= rawurlencode((string) $val); 2322684e50aSAndreas Gohr $amp = true; 2332684e50aSAndreas Gohr } 2342684e50aSAndreas Gohr return $url; 2352684e50aSAndreas Gohr} 2362684e50aSAndreas Gohr 2372684e50aSAndreas Gohr/** 2382684e50aSAndreas Gohr * Build an string of html tag attributes 2392684e50aSAndreas Gohr * 2407bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 2417bff22c0SAndreas Gohr * 2422684e50aSAndreas Gohr * @author Andreas Gohr 2432684e50aSAndreas Gohr */ 2444b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 2452684e50aSAndreas Gohr $url = ''; 2469063ec14SAdrian Lang $white = false; 2472684e50aSAndreas Gohr foreach($params as $key => $val) { 2487bff22c0SAndreas Gohr if($key{0} == '_') continue; 249b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 2509063ec14SAdrian Lang if($white) $url .= ' '; 2517bff22c0SAndreas Gohr 2522684e50aSAndreas Gohr $url .= $key.'="'; 2532684e50aSAndreas Gohr $url .= htmlspecialchars($val); 2542684e50aSAndreas Gohr $url .= '"'; 2559063ec14SAdrian Lang $white = true; 2562684e50aSAndreas Gohr } 2572684e50aSAndreas Gohr return $url; 2582684e50aSAndreas Gohr} 2592684e50aSAndreas Gohr 2602684e50aSAndreas Gohr/** 26115fae107Sandi * This builds the breadcrumb trail and returns it as array 26215fae107Sandi * 26315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 264f3f0262cSandi */ 265f3f0262cSandifunction breadcrumbs() { 2668746e727Sandi // we prepare the breadcrumbs early for quick session closing 2678746e727Sandi static $crumbs = null; 2688746e727Sandi if($crumbs != null) return $crumbs; 2698746e727Sandi 270f3f0262cSandi global $ID; 271f3f0262cSandi global $ACT; 272f3f0262cSandi global $conf; 273f3f0262cSandi 274f3f0262cSandi //first visit? 275c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 276f3f0262cSandi //we only save on show and existing wiki documents 277a77f5846Sjan $file = wikiFN($ID); 278a77f5846Sjan if($ACT != 'show' || !@file_exists($file)) { 279e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 280f3f0262cSandi return $crumbs; 281f3f0262cSandi } 282a77f5846Sjan 283a77f5846Sjan // page names 2841a84a0f3SAnika Henke $name = noNSorNS($ID); 285fe9ec250SChris Smith if(useHeading('navigation')) { 286a77f5846Sjan // get page title 28767c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 288a77f5846Sjan if($title) { 289a77f5846Sjan $name = $title; 290a77f5846Sjan } 291a77f5846Sjan } 292a77f5846Sjan 293f3f0262cSandi //remove ID from array 294a77f5846Sjan if(isset($crumbs[$ID])) { 295a77f5846Sjan unset($crumbs[$ID]); 296f3f0262cSandi } 297f3f0262cSandi 298f3f0262cSandi //add to array 299a77f5846Sjan $crumbs[$ID] = $name; 300f3f0262cSandi //reduce size 301f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 302f3f0262cSandi array_shift($crumbs); 303f3f0262cSandi } 304f3f0262cSandi //save to session 305e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 306f3f0262cSandi return $crumbs; 307f3f0262cSandi} 308f3f0262cSandi 309f3f0262cSandi/** 31015fae107Sandi * Filter for page IDs 31115fae107Sandi * 312f3f0262cSandi * This is run on a ID before it is outputted somewhere 313f3f0262cSandi * currently used to replace the colon with something else 314907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 315907f24f7SAndreas Gohr * 316907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 317907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 318907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 31915fae107Sandi * 32049c713a3Sandi * Urlencoding is ommitted when the second parameter is false 32149c713a3Sandi * 32215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 323f3f0262cSandi */ 32449c713a3Sandifunction idfilter($id, $ue = true) { 325f3f0262cSandi global $conf; 326f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 327f3f0262cSandi $id = strtr($id, ':', '/'); 328f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 32958bedc8aSborekb $conf['userewrite'] && 33058bedc8aSborekb strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') === false 3313272d797SAndreas Gohr ) { 332f3f0262cSandi $id = strtr($id, ':', ';'); 333f3f0262cSandi } 33449c713a3Sandi if($ue) { 335b6c6979fSAndreas Gohr $id = rawurlencode($id); 336f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 337f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 33849c713a3Sandi } 339f3f0262cSandi return $id; 340f3f0262cSandi} 341f3f0262cSandi 342f3f0262cSandi/** 343ed7b5f09Sandi * This builds a link to a wikipage 34415fae107Sandi * 3456c7843b5Sandi * It handles URL rewriting and adds additional parameter if 3466c7843b5Sandi * given in $more 3476c7843b5Sandi * 34815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 349f3f0262cSandi */ 35016f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 351f3f0262cSandi global $conf; 35216f15a81SDominik Eckelmann if(is_array($urlParameters)) { 35316f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 3546de3759aSAndreas Gohr } else { 35516f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 3566de3759aSAndreas Gohr } 35716f15a81SDominik Eckelmann if($id === '') { 35816f15a81SDominik Eckelmann $id = $conf['start']; 35916f15a81SDominik Eckelmann } 360f3f0262cSandi $id = idfilter($id); 36116f15a81SDominik Eckelmann if($absolute) { 362ed7b5f09Sandi $xlink = DOKU_URL; 363ed7b5f09Sandi } else { 364ed7b5f09Sandi $xlink = DOKU_BASE; 365ed7b5f09Sandi } 366f3f0262cSandi 3676c7843b5Sandi if($conf['userewrite'] == 2) { 3686c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 36916f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 3706c7843b5Sandi } elseif($conf['userewrite']) { 371f3f0262cSandi $xlink .= $id; 37216f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 373bce3726dSAndreas Gohr } elseif($id) { 3746c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 37516f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 376bce3726dSAndreas Gohr } else { 377bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 37816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 379f3f0262cSandi } 380f3f0262cSandi 381f3f0262cSandi return $xlink; 382f3f0262cSandi} 383f3f0262cSandi 384f3f0262cSandi/** 385f5c2808fSBen Coburn * This builds a link to an alternate page format 386f5c2808fSBen Coburn * 387f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 388f5c2808fSBen Coburn * 389f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 390f5c2808fSBen Coburn */ 391f5c2808fSBen Coburnfunction exportlink($id = '', $format = 'raw', $more = '', $abs = false, $sep = '&') { 392f5c2808fSBen Coburn global $conf; 393f5c2808fSBen Coburn if(is_array($more)) { 394f5c2808fSBen Coburn $more = buildURLparams($more, $sep); 395f5c2808fSBen Coburn } else { 396f5c2808fSBen Coburn $more = str_replace(',', $sep, $more); 397f5c2808fSBen Coburn } 398f5c2808fSBen Coburn 399f5c2808fSBen Coburn $format = rawurlencode($format); 400f5c2808fSBen Coburn $id = idfilter($id); 401f5c2808fSBen Coburn if($abs) { 402f5c2808fSBen Coburn $xlink = DOKU_URL; 403f5c2808fSBen Coburn } else { 404f5c2808fSBen Coburn $xlink = DOKU_BASE; 405f5c2808fSBen Coburn } 406f5c2808fSBen Coburn 407f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 408f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 409f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 410f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 411f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 412f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 413f5c2808fSBen Coburn } else { 414f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 415f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 416f5c2808fSBen Coburn } 417f5c2808fSBen Coburn 418f5c2808fSBen Coburn return $xlink; 419f5c2808fSBen Coburn} 420f5c2808fSBen Coburn 421f5c2808fSBen Coburn/** 4226de3759aSAndreas Gohr * Build a link to a media file 4236de3759aSAndreas Gohr * 4246de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 4258c08db0aSAndreas Gohr * 4268c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 4278c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 4288c08db0aSAndreas Gohr * 4293272d797SAndreas Gohr * @param string $id the media file id or URL 4303272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 4313272d797SAndreas Gohr * @param bool $direct link to detail page if false 4323272d797SAndreas Gohr * @param string $sep URL parameter separator 4333272d797SAndreas Gohr * @param bool $abs Create an absolute URL 4343272d797SAndreas Gohr * @return string 4356de3759aSAndreas Gohr */ 43655b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 4376de3759aSAndreas Gohr global $conf; 4386de3759aSAndreas Gohr if(is_array($more)) { 4390f4e0092SChristopher Smith // add token for resized images 4400f4e0092SChristopher Smith if($more['w'] || $more['h']){ 4410f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 4420f4e0092SChristopher Smith } 4438c08db0aSAndreas Gohr // strip defaults for shorter URLs 4448c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 4458c08db0aSAndreas Gohr if(!$more['w']) unset($more['w']); 4468c08db0aSAndreas Gohr if(!$more['h']) unset($more['h']); 4478c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 448b174aeaeSchris $more = buildURLparams($more, $sep); 4496de3759aSAndreas Gohr } else { 4505e7db1e2SChristopher Smith $matches = array(); 4515e7db1e2SChristopher Smith if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER)){ 4525e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 4535e7db1e2SChristopher Smith foreach ($matches as $match){ 4545e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 4555e7db1e2SChristopher Smith } 4565e7db1e2SChristopher Smith $more .= $sep.'tok='.media_get_token($id,$resize['w'],$resize['h']); 4575e7db1e2SChristopher Smith } 4588c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 4598c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 460b174aeaeSchris $more = str_replace(',', $sep, $more); 4616de3759aSAndreas Gohr } 4626de3759aSAndreas Gohr 46355b2b31bSAndreas Gohr if($abs) { 46455b2b31bSAndreas Gohr $xlink = DOKU_URL; 46555b2b31bSAndreas Gohr } else { 4666de3759aSAndreas Gohr $xlink = DOKU_BASE; 46755b2b31bSAndreas Gohr } 4686de3759aSAndreas Gohr 4696de3759aSAndreas Gohr // external URLs are always direct without rewriting 4706de3759aSAndreas Gohr if(preg_match('#^(https?|ftp)://#i', $id)) { 4716de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 47269d17d94SAndreas Gohr // add hash: 473*cdcd66dfSAndreas Gohr $xlink .= '?hash='.substr(PassHash::hmac('md5', $id, auth_cookiesalt()), 0, 6); 4746de3759aSAndreas Gohr if($more) { 47569d17d94SAndreas Gohr $xlink .= $sep.$more; 476b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 4776de3759aSAndreas Gohr } else { 47869d17d94SAndreas Gohr $xlink .= $sep.'media='.rawurlencode($id); 4796de3759aSAndreas Gohr } 4806de3759aSAndreas Gohr return $xlink; 4816de3759aSAndreas Gohr } 4826de3759aSAndreas Gohr 4836de3759aSAndreas Gohr $id = idfilter($id); 4846de3759aSAndreas Gohr 4856de3759aSAndreas Gohr // decide on scriptname 4866de3759aSAndreas Gohr if($direct) { 4876de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 4886de3759aSAndreas Gohr $script = '_media'; 4896de3759aSAndreas Gohr } else { 4906de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 4916de3759aSAndreas Gohr } 4926de3759aSAndreas Gohr } else { 4936de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 4946de3759aSAndreas Gohr $script = '_detail'; 4956de3759aSAndreas Gohr } else { 4966de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 4976de3759aSAndreas Gohr } 4986de3759aSAndreas Gohr } 4996de3759aSAndreas Gohr 5006de3759aSAndreas Gohr // build URL based on rewrite mode 5016de3759aSAndreas Gohr if($conf['userewrite']) { 5026de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 5036de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 5046de3759aSAndreas Gohr } else { 5056de3759aSAndreas Gohr if($more) { 506a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 507b174aeaeSchris $xlink .= $sep.'media='.$id; 5086de3759aSAndreas Gohr } else { 509a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 5106de3759aSAndreas Gohr } 5116de3759aSAndreas Gohr } 5126de3759aSAndreas Gohr 5136de3759aSAndreas Gohr return $xlink; 5146de3759aSAndreas Gohr} 5156de3759aSAndreas Gohr 5166de3759aSAndreas Gohr/** 51725ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 51815fae107Sandi * 51925ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 52025ca5b17SAndreas Gohr * 52115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 522f3f0262cSandi */ 52325ca5b17SAndreas Gohrfunction script() { 524ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 525f3f0262cSandi} 526f3f0262cSandi 527f3f0262cSandi/** 52815fae107Sandi * Spamcheck against wordlist 52915fae107Sandi * 530f3f0262cSandi * Checks the wikitext against a list of blocked expressions 531f3f0262cSandi * returns true if the text contains any bad words 53215fae107Sandi * 533e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 534e403cc58SMichael Klier * 535e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 536e403cc58SMichael Klier * and gain information about the user who was blocked. 537e403cc58SMichael Klier * 538e403cc58SMichael Klier * Event data: 539e403cc58SMichael Klier * data['matches'] - array of matches 540e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 541e403cc58SMichael Klier * [ip] - ip address 542e403cc58SMichael Klier * [user] - username (if logged in) 543e403cc58SMichael Klier * [mail] - mail address (if logged in) 544e403cc58SMichael Klier * [name] - real name (if logged in) 545e403cc58SMichael Klier * 54615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5476dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 5486dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 5496dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 550f3f0262cSandi */ 5516dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 552f3f0262cSandi global $TEXT; 5536dffa0e0SAndreas Gohr global $PRE; 5546dffa0e0SAndreas Gohr global $SUF; 555f3f0262cSandi global $conf; 556e403cc58SMichael Klier global $INFO; 557f3f0262cSandi 558f3f0262cSandi if(!$conf['usewordblock']) return false; 559f3f0262cSandi 5606dffa0e0SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF"; 5616dffa0e0SAndreas Gohr 562041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 5636dffa0e0SAndreas Gohr $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', '\1http://\2 \2\3', $text); 564041d1964SAndreas Gohr 565b9ac8716Schris $wordblocks = getWordblocks(); 5663e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 5673e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 5683e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 5693e2965d7Sandi // backreferences are used - the maximum is 99 5703e2965d7Sandi // this is very bad performancewise and may even be too high still 5713e2965d7Sandi $chunksize = 40; 5723e2965d7Sandi } else { 573a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 5743e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 575a51d08efSAndreas Gohr $chunksize = 200; 5763e2965d7Sandi } 577b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 578f3f0262cSandi $re = array(); 57949eb6e38SAndreas Gohr // build regexp from blocks 580f3f0262cSandi foreach($blocks as $block) { 581f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 582f3f0262cSandi $block = trim($block); 583f3f0262cSandi if(empty($block)) continue; 584f3f0262cSandi $re[] = $block; 585f3f0262cSandi } 586e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 587e403cc58SMichael Klier // prepare event data 588e403cc58SMichael Klier $data['matches'] = $matches; 589e403cc58SMichael Klier $data['userinfo']['ip'] = $_SERVER['REMOTE_ADDR']; 590e403cc58SMichael Klier if($_SERVER['REMOTE_USER']) { 591e403cc58SMichael Klier $data['userinfo']['user'] = $_SERVER['REMOTE_USER']; 592e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 593e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 594e403cc58SMichael Klier } 595e403cc58SMichael Klier $callback = create_function('', 'return true;'); 596e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 597b9ac8716Schris } 598703f6fdeSandi } 599f3f0262cSandi return false; 600f3f0262cSandi} 601f3f0262cSandi 602f3f0262cSandi/** 60315fae107Sandi * Return the IP of the client 60415fae107Sandi * 6056d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 60615fae107Sandi * 6076d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 6086d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 6096d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 6106d8affe6SAndreas Gohr * headers 6116d8affe6SAndreas Gohr * 61215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6133272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 6143272d797SAndreas Gohr * @return string 615f3f0262cSandi */ 6166d8affe6SAndreas Gohrfunction clientIP($single = false) { 6176d8affe6SAndreas Gohr $ip = array(); 6186d8affe6SAndreas Gohr $ip[] = $_SERVER['REMOTE_ADDR']; 619bb4866bdSchris if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) 6205cbeffbfSMarcel Pennewiß $ip = array_merge($ip, explode(',', str_replace(' ', '', $_SERVER['HTTP_X_FORWARDED_FOR']))); 621bb4866bdSchris if(!empty($_SERVER['HTTP_X_REAL_IP'])) 6225cbeffbfSMarcel Pennewiß $ip = array_merge($ip, explode(',', str_replace(' ', '', $_SERVER['HTTP_X_REAL_IP']))); 6236d8affe6SAndreas Gohr 624dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 625dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 626dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 627dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 628dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 629dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 630dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 631dc14c6d1SGuy Brand $IPv6Address = 632dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 633dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 634dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 635dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 636dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 637dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 638dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 639dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 640dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 641dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 642dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 643dc14c6d1SGuy Brand 6446d8affe6SAndreas Gohr // remove any non-IP stuff 6456d8affe6SAndreas Gohr $cnt = count($ip); 6464ff28443Schris $match = array(); 6476d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 648dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 6494ff28443Schris $ip[$i] = $match[0]; 6504ff28443Schris } else { 6514ff28443Schris $ip[$i] = ''; 6524ff28443Schris } 6536d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 654f3f0262cSandi } 6556d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 6566d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 6576d8affe6SAndreas Gohr 6586d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 6596d8affe6SAndreas Gohr 6606d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 6616d8affe6SAndreas Gohr $ip = array_reverse($ip); 6626d8affe6SAndreas Gohr foreach($ip as $i) { 6632343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 6646d8affe6SAndreas Gohr continue; 6656d8affe6SAndreas Gohr } else { 6666d8affe6SAndreas Gohr return $i; 6676d8affe6SAndreas Gohr } 6686d8affe6SAndreas Gohr } 6696d8affe6SAndreas Gohr // still here? just use the first (last) address 6706d8affe6SAndreas Gohr return $ip[0]; 671f3f0262cSandi} 672f3f0262cSandi 673f3f0262cSandi/** 6741c548ebeSAndreas Gohr * Check if the browser is on a mobile device 6751c548ebeSAndreas Gohr * 6761c548ebeSAndreas Gohr * Adapted from the example code at url below 6771c548ebeSAndreas Gohr * 6781c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 6791c548ebeSAndreas Gohr */ 6801c548ebeSAndreas Gohrfunction clientismobile() { 6811c548ebeSAndreas Gohr 6821c548ebeSAndreas Gohr if(isset($_SERVER['HTTP_X_WAP_PROFILE'])) return true; 6831c548ebeSAndreas Gohr 6841c548ebeSAndreas Gohr if(preg_match('/wap\.|\.wap/i', $_SERVER['HTTP_ACCEPT'])) return true; 6851c548ebeSAndreas Gohr 6861c548ebeSAndreas Gohr if(!isset($_SERVER['HTTP_USER_AGENT'])) return false; 6871c548ebeSAndreas Gohr 6881c548ebeSAndreas 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'; 6891c548ebeSAndreas Gohr 6901c548ebeSAndreas Gohr if(preg_match("/$uamatches/i", $_SERVER['HTTP_USER_AGENT'])) return true; 6911c548ebeSAndreas Gohr 6921c548ebeSAndreas Gohr return false; 6931c548ebeSAndreas Gohr} 6941c548ebeSAndreas Gohr 6951c548ebeSAndreas Gohr/** 69663211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 69763211f61SGlen Harris * 69822ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 69922ef1e32SAndreas Gohr * 70063211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 7013272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 7023272d797SAndreas Gohr * @return string a comma separated list of hostnames 70363211f61SGlen Harris */ 70463211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 70522ef1e32SAndreas Gohr global $conf; 70622ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 70722ef1e32SAndreas Gohr 70863211f61SGlen Harris $hosts = array(); 70963211f61SGlen Harris $ips = explode(',', $ips); 710551a720fSMichael Klier 711551a720fSMichael Klier if(is_array($ips)) { 7123886270dSAndreas Gohr foreach($ips as $ip) { 713551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 71463211f61SGlen Harris } 715551a720fSMichael Klier return join(',', $hosts); 716551a720fSMichael Klier } else { 717551a720fSMichael Klier return gethostbyaddr(trim($ips)); 718551a720fSMichael Klier } 71963211f61SGlen Harris} 72063211f61SGlen Harris 72163211f61SGlen Harris/** 72215fae107Sandi * Checks if a given page is currently locked. 72315fae107Sandi * 724f3f0262cSandi * removes stale lockfiles 72515fae107Sandi * 72615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 727f3f0262cSandi */ 728f3f0262cSandifunction checklock($id) { 729f3f0262cSandi global $conf; 730c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 731f3f0262cSandi 732f3f0262cSandi //no lockfile 733f3f0262cSandi if(!@file_exists($lock)) return false; 734f3f0262cSandi 735f3f0262cSandi //lockfile expired 736f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 737d8186216SBen Coburn @unlink($lock); 738f3f0262cSandi return false; 739f3f0262cSandi } 740f3f0262cSandi 741f3f0262cSandi //my own lock 74285fef7e2SAndreas Gohr list($ip, $session) = explode("\n", io_readFile($lock)); 74385fef7e2SAndreas Gohr if($ip == $_SERVER['REMOTE_USER'] || $ip == clientIP() || $session == session_id()) { 744f3f0262cSandi return false; 745f3f0262cSandi } 746f3f0262cSandi 747f3f0262cSandi return $ip; 748f3f0262cSandi} 749f3f0262cSandi 750f3f0262cSandi/** 75115fae107Sandi * Lock a page for editing 75215fae107Sandi * 75315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 754f3f0262cSandi */ 755f3f0262cSandifunction lock($id) { 756544ed901SDaniel Calviño Sánchez global $conf; 757544ed901SDaniel Calviño Sánchez 758544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 759544ed901SDaniel Calviño Sánchez return; 760544ed901SDaniel Calviño Sánchez } 761544ed901SDaniel Calviño Sánchez 762c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 763f3f0262cSandi if($_SERVER['REMOTE_USER']) { 764f3f0262cSandi io_saveFile($lock, $_SERVER['REMOTE_USER']); 765f3f0262cSandi } else { 76685fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 767f3f0262cSandi } 768f3f0262cSandi} 769f3f0262cSandi 770f3f0262cSandi/** 77115fae107Sandi * Unlock a page if it was locked by the user 772f3f0262cSandi * 77315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 7743272d797SAndreas Gohr * @param string $id page id to unlock 77515fae107Sandi * @return bool true if a lock was removed 776f3f0262cSandi */ 777f3f0262cSandifunction unlock($id) { 778c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 779f3f0262cSandi if(@file_exists($lock)) { 78085fef7e2SAndreas Gohr list($ip, $session) = explode("\n", io_readFile($lock)); 78185fef7e2SAndreas Gohr if($ip == $_SERVER['REMOTE_USER'] || $ip == clientIP() || $session == session_id()) { 782f3f0262cSandi @unlink($lock); 783f3f0262cSandi return true; 784f3f0262cSandi } 785f3f0262cSandi } 786f3f0262cSandi return false; 787f3f0262cSandi} 788f3f0262cSandi 789f3f0262cSandi/** 790f3f0262cSandi * convert line ending to unix format 791f3f0262cSandi * 7926db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 7936db7468bSAndreas Gohr * 79415fae107Sandi * @see formText() for 2crlf conversion 79515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 796f3f0262cSandi */ 797f3f0262cSandifunction cleanText($text) { 798f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 7996db7468bSAndreas Gohr 8006db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 8016db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 8026db7468bSAndreas Gohr // but might actually fix the problem in many cases 8036db7468bSAndreas Gohr if(!utf8_check($text)) $text = utf8_encode($text); 8046db7468bSAndreas Gohr 805f3f0262cSandi return $text; 806f3f0262cSandi} 807f3f0262cSandi 808f3f0262cSandi/** 809f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 810f3f0262cSandi * It also converts line endings to Windows format which is 811f3f0262cSandi * pseudo standard for webforms. 812f3f0262cSandi * 81315fae107Sandi * @see cleanText() for 2unix conversion 81415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 815f3f0262cSandi */ 816f3f0262cSandifunction formText($text) { 8175b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 818f3f0262cSandi return htmlspecialchars($text); 819f3f0262cSandi} 820f3f0262cSandi 821f3f0262cSandi/** 82215fae107Sandi * Returns the specified local text in raw format 82315fae107Sandi * 82415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 825f3f0262cSandi */ 8262adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 8272adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 828f3f0262cSandi} 829f3f0262cSandi 830f3f0262cSandi/** 831f3f0262cSandi * Returns the raw WikiText 83215fae107Sandi * 83315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 834f3f0262cSandi */ 835f3f0262cSandifunction rawWiki($id, $rev = '') { 836cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 837f3f0262cSandi} 838f3f0262cSandi 839f3f0262cSandi/** 8407146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 8417146cee2SAndreas Gohr * 8427b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 8437146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8447146cee2SAndreas Gohr */ 845fe17917eSAdrian Langfunction pageTemplate($id) { 846a15ce62dSEsther Brunner global $conf; 847e29549feSAndreas Gohr 848fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 849e29549feSAndreas Gohr 8507b84afa2SAndreas Gohr // prepare initial event data 8517b84afa2SAndreas Gohr $data = array( 8527b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 8537b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 8547b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 8557b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 8567b84afa2SAndreas Gohr ); 8577b84afa2SAndreas Gohr 8587b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 8597b84afa2SAndreas Gohr if($evt->advise_before(true)) { 8607b84afa2SAndreas Gohr // the before event might have loaded the content already 8617b84afa2SAndreas Gohr if(empty($data['tpl'])) { 8627b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 8637b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 864fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 865e29549feSAndreas Gohr if(@file_exists($path.'/_template.txt')) { 8667b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 867e29549feSAndreas Gohr } else { 868e29549feSAndreas Gohr // search upper namespaces for templates 869e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 870e29549feSAndreas Gohr while(strlen($path) >= $len) { 871e29549feSAndreas Gohr if(@file_exists($path.'/__template.txt')) { 8727b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 873e29549feSAndreas Gohr break; 874e29549feSAndreas Gohr } 875e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 876e29549feSAndreas Gohr } 877e29549feSAndreas Gohr } 8787b84afa2SAndreas Gohr } 8797b84afa2SAndreas Gohr // load the content 8803d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 8817b84afa2SAndreas Gohr } 882a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 8837b84afa2SAndreas Gohr } 8847b84afa2SAndreas Gohr $evt->advise_after(); 8857b84afa2SAndreas Gohr unset($evt); 8867b84afa2SAndreas Gohr 887fe17917eSAdrian Lang return $data['tpl']; 8882b1223ecSAdrian Lang} 8892b1223ecSAdrian Lang 8902b1223ecSAdrian Lang/** 8912b1223ecSAdrian Lang * Performs common page template replacements 8927b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 8932b1223ecSAdrian Lang * 8942b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 8952b1223ecSAdrian Lang */ 896d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 8973272d797SAndreas Gohr /** 8983272d797SAndreas Gohr * @var string $id the id of the page to be created 8993272d797SAndreas Gohr * @var string $tpl the text used as template 9003272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 9013272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 9023272d797SAndreas Gohr */ 903fe17917eSAdrian Lang extract($data); 904fe17917eSAdrian Lang 905b856f7dfSAdrian Lang global $USERINFO; 906bce53b1fSAdrian Lang global $conf; 907e29549feSAndreas Gohr 908e29549feSAndreas Gohr // replace placeholders 90926ece5a7SAndreas Gohr $file = noNS($id); 91037c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 91126ece5a7SAndreas Gohr 9123272d797SAndreas Gohr $tpl = str_replace( 9133272d797SAndreas Gohr array( 91426ece5a7SAndreas Gohr '@ID@', 91526ece5a7SAndreas Gohr '@NS@', 91626ece5a7SAndreas Gohr '@FILE@', 91726ece5a7SAndreas Gohr '@!FILE@', 91826ece5a7SAndreas Gohr '@!FILE!@', 91926ece5a7SAndreas Gohr '@PAGE@', 92026ece5a7SAndreas Gohr '@!PAGE@', 92126ece5a7SAndreas Gohr '@!!PAGE@', 92226ece5a7SAndreas Gohr '@!PAGE!@', 92326ece5a7SAndreas Gohr '@USER@', 92426ece5a7SAndreas Gohr '@NAME@', 92526ece5a7SAndreas Gohr '@MAIL@', 92626ece5a7SAndreas Gohr '@DATE@', 92726ece5a7SAndreas Gohr ), 92826ece5a7SAndreas Gohr array( 92926ece5a7SAndreas Gohr $id, 93026ece5a7SAndreas Gohr getNS($id), 93126ece5a7SAndreas Gohr $file, 93226ece5a7SAndreas Gohr utf8_ucfirst($file), 93326ece5a7SAndreas Gohr utf8_strtoupper($file), 93426ece5a7SAndreas Gohr $page, 93526ece5a7SAndreas Gohr utf8_ucfirst($page), 93626ece5a7SAndreas Gohr utf8_ucwords($page), 93726ece5a7SAndreas Gohr utf8_strtoupper($page), 93826ece5a7SAndreas Gohr $_SERVER['REMOTE_USER'], 939b856f7dfSAdrian Lang $USERINFO['name'], 940b856f7dfSAdrian Lang $USERINFO['mail'], 94126ece5a7SAndreas Gohr $conf['dformat'], 9423272d797SAndreas Gohr ), $tpl 9433272d797SAndreas Gohr ); 94426ece5a7SAndreas Gohr 9457d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 9467d644fc8SAndreas Gohr $tpl = preg_replace_callback('/%./', create_function('$m', 'return strftime($m[0]);'), $tpl); 947d535a2e9Sstretchyboy $data['tpl'] = $tpl; 948a15ce62dSEsther Brunner return $tpl; 9497146cee2SAndreas Gohr} 9507146cee2SAndreas Gohr 9517146cee2SAndreas Gohr/** 95215fae107Sandi * Returns the raw Wiki Text in three slices. 95315fae107Sandi * 95415fae107Sandi * The range parameter needs to have the form "from-to" 95515cfe303Sandi * and gives the range of the section in bytes - no 95615cfe303Sandi * UTF-8 awareness is needed. 957f3f0262cSandi * The returned order is prefix, section and suffix. 95815fae107Sandi * 95915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 960f3f0262cSandi */ 961f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 962cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 963f3f0262cSandi 96480fcb268SAdrian Lang // Parse range 96580fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 96680fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 96780fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 96880fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 96980fcb268SAdrian Lang 97080fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 97180fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 97215cfe303Sandi $slices[2] = substr($text, $to); 973f3f0262cSandi return $slices; 974f3f0262cSandi} 975f3f0262cSandi 976f3f0262cSandi/** 97715fae107Sandi * Joins wiki text slices 97815fae107Sandi * 97980fcb268SAdrian Lang * function to join the text slices. 980f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 981f3f0262cSandi * lines between sections if needed (used on saving). 98215fae107Sandi * 98315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 984f3f0262cSandi */ 985f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 986f3f0262cSandi if($pretty) { 98780fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 9883272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 9893272d797SAndreas Gohr ) { 99080fcb268SAdrian Lang $pre .= "\n"; 99180fcb268SAdrian Lang } 99280fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 9933272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 9943272d797SAndreas Gohr ) { 99580fcb268SAdrian Lang $text .= "\n"; 99680fcb268SAdrian Lang } 997f3f0262cSandi } 998f3f0262cSandi 999f3f0262cSandi return $pre.$text.$suf; 1000f3f0262cSandi} 1001f3f0262cSandi 1002f3f0262cSandi/** 1003a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1004a701424fSBen Coburn * Also directs changelog and attic updates. 100515fae107Sandi * 100615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 100771726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1008f3f0262cSandi */ 1009b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1010a701424fSBen Coburn /* Note to developers: 1011a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1012a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1013a701424fSBen Coburn after any changes. External edits change the wiki page 1014a701424fSBen Coburn directly without using php or dokuwiki. 1015a701424fSBen Coburn */ 1016f3f0262cSandi global $conf; 1017f3f0262cSandi global $lang; 101871726d78SBen Coburn global $REV; 1019f3f0262cSandi // ignore if no changes were made 1020f3f0262cSandi if($text == rawWiki($id, '')) { 1021f3f0262cSandi return; 1022f3f0262cSandi } 1023f3f0262cSandi 1024f3f0262cSandi $file = wikiFN($id); 1025a701424fSBen Coburn $old = @filemtime($file); // from page 1026407e65b9SAndreas Gohr $wasRemoved = (trim($text) == ''); // check for empty or whitespace only 1027d8186216SBen Coburn $wasCreated = !@file_exists($file); 102871726d78SBen Coburn $wasReverted = ($REV == true); 1029e45b34cdSBen Coburn $newRev = false; 1030a701424fSBen Coburn $oldRev = getRevisions($id, -1, 1, 1024); // from changelog 1031a701424fSBen Coburn $oldRev = (int) (empty($oldRev) ? 0 : $oldRev[0]); 1032a701424fSBen Coburn if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old >= $oldRev) { 103346844156SBen Coburn // add old revision to the attic if missing 103446844156SBen Coburn saveOldRevision($id); 103546844156SBen Coburn // add a changelog entry if this edit came from outside dokuwiki 1036a701424fSBen Coburn if($old > $oldRev) { 1037ebf1501fSBen Coburn addLogEntry($old, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=> true)); 103846844156SBen Coburn // remove soon to be stale instructions 103946844156SBen Coburn $cache = new cache_instructions($id, $file); 104046844156SBen Coburn $cache->removeCache(); 104146844156SBen Coburn } 104246844156SBen Coburn } 1043f3f0262cSandi 104471726d78SBen Coburn if($wasRemoved) { 104530725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 104630725328SGabriel Birke $data = array(array($file, '', false), getNS($id), noNS($id), false); 104730725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1048e45b34cdSBen Coburn // pre-save deleted revision 1049e45b34cdSBen Coburn @touch($file); 105046844156SBen Coburn clearstatcache(); 1051e45b34cdSBen Coburn $newRev = saveOldRevision($id); 1052e1f3d9e1SEsther Brunner // remove empty file 1053f3f0262cSandi @unlink($file); 1054c5f92742SMichael Hamann // don't remove old meta info as it should be saved, plugins can use IO_WIKIPAGE_WRITE for removing their metadata... 1055c5f92742SMichael Hamann // purge non-persistant meta data 10563d1f9ec3SMichael Klier p_purge_metadata($id); 1057f3f0262cSandi $del = true; 10583ce054b3Sandi // autoset summary on deletion 10593ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 106053d6ccfeSandi // remove empty namespaces 1061cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1062cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1063f3f0262cSandi } else { 1064cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 1065cc7d0c94SBen Coburn io_writeWikiPage($file, $text, $id); 106646844156SBen Coburn // pre-save the revision, to keep the attic in sync 106746844156SBen Coburn $newRev = saveOldRevision($id); 1068f3f0262cSandi $del = false; 1069f3f0262cSandi } 1070f3f0262cSandi 107171726d78SBen Coburn // select changelog line type 107271726d78SBen Coburn $extra = ''; 1073ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_EDIT; 107471726d78SBen Coburn if($wasReverted) { 1075ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_REVERT; 107671726d78SBen Coburn $extra = $REV; 10773272d797SAndreas Gohr } else if($wasCreated) { 10783272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_CREATE; 10793272d797SAndreas Gohr } else if($wasRemoved) { 10803272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_DELETE; 10813272d797SAndreas Gohr } else if($minor && $conf['useacl'] && $_SERVER['REMOTE_USER']) { 10823272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_MINOR_EDIT; 10833272d797SAndreas Gohr } //minor edits only for logged in users 108471726d78SBen Coburn 1085e45b34cdSBen Coburn addLogEntry($newRev, $id, $type, $summary, $extra); 108626a0801fSAndreas Gohr // send notify mails 108790033e9dSAndreas Gohr notify($id, 'admin', $old, $summary, $minor); 108890033e9dSAndreas Gohr notify($id, 'subscribers', $old, $summary, $minor); 1089f3f0262cSandi 1090ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 109198407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 10922eccbdaaSGina Haeussge 10932eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1094fe9ec250SChris Smith if(useHeading('content')) { 10952eccbdaaSGina Haeussge $pages = ft_backlinks($id); 10962eccbdaaSGina Haeussge foreach($pages as $page) { 10972eccbdaaSGina Haeussge $cache = new cache_renderer($page, wikiFN($page), 'xhtml'); 10982eccbdaaSGina Haeussge $cache->removeCache(); 10992eccbdaaSGina Haeussge } 11002eccbdaaSGina Haeussge } 1101f3f0262cSandi} 1102f3f0262cSandi 1103f3f0262cSandi/** 1104f3f0262cSandi * moves the current version to the attic and returns its 1105f3f0262cSandi * revision date 110615fae107Sandi * 110715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1108f3f0262cSandi */ 1109f3f0262cSandifunction saveOldRevision($id) { 1110f3f0262cSandi global $conf; 1111f3f0262cSandi $oldf = wikiFN($id); 1112f3f0262cSandi if(!@file_exists($oldf)) return ''; 1113f3f0262cSandi $date = filemtime($oldf); 1114f3f0262cSandi $newf = wikiFN($id, $date); 1115cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1116f3f0262cSandi return $date; 1117f3f0262cSandi} 1118f3f0262cSandi 1119f3f0262cSandi/** 1120fde10de4SAdrian Lang * Sends a notify mail on page change or registration 112126a0801fSAndreas Gohr * 112226a0801fSAndreas Gohr * @param string $id The changed page 1123fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 11243272d797SAndreas Gohr * @param int|string $rev Old page revision 112526a0801fSAndreas Gohr * @param string $summary What changed 112690033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 112702a498e7Schris * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 112815fae107Sandi * 11293272d797SAndreas Gohr * @return bool 113015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1131f3f0262cSandi */ 113202a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1133f3f0262cSandi global $conf; 1134b158d625SSteven Danz 11356df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 113626a0801fSAndreas Gohr if($who == 'admin') { 11373272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 11382ed38036SAndreas Gohr $tpl = 'mailtext'; 113926a0801fSAndreas Gohr $to = $conf['notify']; 114026a0801fSAndreas Gohr } elseif($who == 'subscribers') { 114184c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 11423272d797SAndreas Gohr if($conf['useacl'] && $_SERVER['REMOTE_USER'] && $minor) return false; //skip minors 11438881fcc9SAdrian Lang $data = array('id' => $id, 'addresslist' => '', 'self' => false); 11443272d797SAndreas Gohr trigger_event( 11453272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1146835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 11473272d797SAndreas Gohr ); 11482ed38036SAndreas Gohr $to = $data['addresslist']; 11492ed38036SAndreas Gohr if(empty($to)) return false; 11502ed38036SAndreas Gohr $tpl = 'subscr_single'; 115126a0801fSAndreas Gohr } else { 11523272d797SAndreas Gohr return false; //just to be safe 115326a0801fSAndreas Gohr } 115426a0801fSAndreas Gohr 11556df843eeSAndreas Gohr // prepare content 11562ed38036SAndreas Gohr $subscription = new Subscription(); 11572ed38036SAndreas Gohr return $subscription->send_diff($to, $tpl, $id, $rev, $summary); 1158f3f0262cSandi} 11592ed38036SAndreas Gohr 116015fae107Sandi/** 116171f7bde7SAndreas Gohr * extracts the query from a search engine referrer 116215fae107Sandi * 116315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 116471f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1165f3f0262cSandi */ 1166f3f0262cSandifunction getGoogleQuery() { 1167c66972f2SAdrian Lang if(!isset($_SERVER['HTTP_REFERER'])) { 1168c66972f2SAdrian Lang return ''; 1169c66972f2SAdrian Lang } 1170f3f0262cSandi $url = parse_url($_SERVER['HTTP_REFERER']); 1171f3f0262cSandi 1172079b3ac1SAndreas Gohr // only handle common SEs 1173079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1174e4d8a516SKazutaka Miyasaka 1175079b3ac1SAndreas Gohr $query = array(); 1176e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1177e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1178e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1179f3f0262cSandi parse_str($url['query'], $query); 1180e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1181e4d8a516SKazutaka Miyasaka 1182c66972f2SAdrian Lang $q = ''; 1183079b3ac1SAndreas Gohr if(isset($query['q'])){ 1184079b3ac1SAndreas Gohr $q = $query['q']; 1185079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1186079b3ac1SAndreas Gohr $q = $query['p']; 1187079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1188079b3ac1SAndreas Gohr $q = $query['query']; 1189079b3ac1SAndreas Gohr } 1190079b3ac1SAndreas Gohr $q = trim($q); 1191f3f0262cSandi 1192079b3ac1SAndreas Gohr if(!$q) return ''; 11936531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1194f93b3b50SAndreas Gohr return $q; 1195f3f0262cSandi} 1196f3f0262cSandi 1197f3f0262cSandi/** 1198f3f0262cSandi * Return the human readable size of a file 1199f3f0262cSandi * 1200f3f0262cSandi * @param int $size A file size 1201f3f0262cSandi * @param int $dec A number of decimal places 1202f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1203f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1204f3f0262cSandi * @version 1.0.0 1205f3f0262cSandi */ 1206f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1207f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1208f3f0262cSandi $count = count($sizes); 1209f3f0262cSandi $i = 0; 1210f3f0262cSandi 1211f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1212f3f0262cSandi $size /= 1024; 1213f3f0262cSandi $i++; 1214f3f0262cSandi } 1215f3f0262cSandi 1216f3f0262cSandi return round($size, $dec).' '.$sizes[$i]; 1217f3f0262cSandi} 1218f3f0262cSandi 121915fae107Sandi/** 1220c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1221c57e365eSAndreas Gohr * 1222c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1223c57e365eSAndreas Gohr */ 1224c57e365eSAndreas Gohrfunction datetime_h($dt) { 1225c57e365eSAndreas Gohr global $lang; 1226c57e365eSAndreas Gohr 1227c57e365eSAndreas Gohr $ago = time() - $dt; 1228c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1229c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1230c57e365eSAndreas Gohr } 1231c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1232c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1233c57e365eSAndreas Gohr } 1234c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1235c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1236c57e365eSAndreas Gohr } 1237c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1238c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1239c57e365eSAndreas Gohr } 1240c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1241c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1242c57e365eSAndreas Gohr } 1243c57e365eSAndreas Gohr if($ago > 60 * 2) { 1244c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1245c57e365eSAndreas Gohr } 1246c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1247c57e365eSAndreas Gohr} 1248c57e365eSAndreas Gohr 1249c57e365eSAndreas Gohr/** 1250f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1251f2263577SAndreas Gohr * 1252f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1253f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1254f2263577SAndreas Gohr * 1255f2263577SAndreas Gohr * @see datetime_h 1256f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1257f2263577SAndreas Gohr */ 1258f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1259f2263577SAndreas Gohr global $conf; 1260f2263577SAndreas Gohr 1261f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1262f2263577SAndreas Gohr $dt = (int) $dt; 1263f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1264f2263577SAndreas Gohr 1265f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1266f2263577SAndreas Gohr return strftime($format, $dt); 1267f2263577SAndreas Gohr} 1268f2263577SAndreas Gohr 1269f2263577SAndreas Gohr/** 1270c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1271c4f79b71SMichael Hamann * 1272c4f79b71SMichael Hamann * @author <ungu at terong dot com> 1273c4f79b71SMichael Hamann * @link http://www.php.net/manual/en/function.date.php#54072 127463703ba5SAndreas Gohr * @param int $int_date: current date in UNIX timestamp 12753272d797SAndreas Gohr * @return string 1276c4f79b71SMichael Hamann */ 1277c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1278c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1279c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1280c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1281c4f79b71SMichael Hamann $date_mod .= $time_zone; 1282c4f79b71SMichael Hamann return $date_mod; 1283c4f79b71SMichael Hamann} 1284c4f79b71SMichael Hamann 1285c4f79b71SMichael Hamann/** 128600a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 128700a7b5adSEsther Brunner * 128800a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 128900a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 129000a7b5adSEsther Brunner */ 129100a7b5adSEsther Brunnerfunction obfuscate($email) { 129200a7b5adSEsther Brunner global $conf; 129300a7b5adSEsther Brunner 129400a7b5adSEsther Brunner switch($conf['mailguard']) { 129500a7b5adSEsther Brunner case 'visible' : 129600a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 129700a7b5adSEsther Brunner return strtr($email, $obfuscate); 129800a7b5adSEsther Brunner 129900a7b5adSEsther Brunner case 'hex' : 130000a7b5adSEsther Brunner $encode = ''; 130149eb6e38SAndreas Gohr $len = strlen($email); 130249eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 130349eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 130449eb6e38SAndreas Gohr } 130500a7b5adSEsther Brunner return $encode; 130600a7b5adSEsther Brunner 130700a7b5adSEsther Brunner case 'none' : 130800a7b5adSEsther Brunner default : 130900a7b5adSEsther Brunner return $email; 131000a7b5adSEsther Brunner } 131100a7b5adSEsther Brunner} 131200a7b5adSEsther Brunner 131300a7b5adSEsther Brunner/** 131489541d4bSAndreas Gohr * Removes quoting backslashes 131589541d4bSAndreas Gohr * 131689541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 131789541d4bSAndreas Gohr */ 131889541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 131989541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 132089541d4bSAndreas Gohr} 132189541d4bSAndreas Gohr 132273038c47SAndreas Gohr/** 132373038c47SAndreas Gohr * Convert php.ini shorthands to byte 132473038c47SAndreas Gohr * 132573038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 132673038c47SAndreas Gohr * @link http://de3.php.net/manual/en/ini.core.php#79564 132773038c47SAndreas Gohr */ 132873038c47SAndreas Gohrfunction php_to_byte($v) { 132973038c47SAndreas Gohr $l = substr($v, -1); 133073038c47SAndreas Gohr $ret = substr($v, 0, -1); 133173038c47SAndreas Gohr switch(strtoupper($l)) { 133273038c47SAndreas Gohr case 'P': 133373038c47SAndreas Gohr $ret *= 1024; 133473038c47SAndreas Gohr case 'T': 133573038c47SAndreas Gohr $ret *= 1024; 133673038c47SAndreas Gohr case 'G': 133773038c47SAndreas Gohr $ret *= 1024; 133873038c47SAndreas Gohr case 'M': 133973038c47SAndreas Gohr $ret *= 1024; 134073038c47SAndreas Gohr case 'K': 134173038c47SAndreas Gohr $ret *= 1024; 134273038c47SAndreas Gohr break; 134349cbd23eSOtto Vainio default; 134449cbd23eSOtto Vainio $ret *= 10; 134549cbd23eSOtto Vainio break; 134673038c47SAndreas Gohr } 134773038c47SAndreas Gohr return $ret; 134873038c47SAndreas Gohr} 134973038c47SAndreas Gohr 1350546d3a99SAndreas Gohr/** 1351546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1352546d3a99SAndreas Gohr */ 1353546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1354546d3a99SAndreas Gohr return preg_quote($string, '/'); 1355546d3a99SAndreas Gohr} 135673038c47SAndreas Gohr 1357bd2f6c2fSAndreas Gohr/** 1358bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1359bd2f6c2fSAndreas Gohr * 1360c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1361bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1362bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1363bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1364bd2f6c2fSAndreas Gohr * 1365bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1366bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1367bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1368bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1369bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 13703272d797SAndreas Gohr * @return string 1371bd2f6c2fSAndreas Gohr */ 1372a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 1373bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1374bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1375bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1376bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1377bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1378bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1379bd2f6c2fSAndreas Gohr} 1380bd2f6c2fSAndreas Gohr 1381dc58b6f4SAndy Webber/** 1382dc58b6f4SAndy Webber * Return the users realname or e-mail address for use 1383dc58b6f4SAndy Webber * in page footer and recent changes pages 1384dc58b6f4SAndy Webber * 1385dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1386dc58b6f4SAndy Webber */ 1387dc58b6f4SAndy Webberfunction editorinfo($username) { 1388dc58b6f4SAndy Webber global $conf; 1389dc58b6f4SAndy Webber global $auth; 1390dc58b6f4SAndy Webber 1391dc58b6f4SAndy Webber switch($conf['showuseras']) { 1392dc58b6f4SAndy Webber case 'username': 1393dc58b6f4SAndy Webber case 'email': 1394dc58b6f4SAndy Webber case 'email_link': 1395173d78c4SAndreas Gohr if($auth) $info = $auth->getUserData($username); 1396dc58b6f4SAndy Webber break; 1397dc58b6f4SAndy Webber default: 1398dc58b6f4SAndy Webber return hsc($username); 1399dc58b6f4SAndy Webber } 1400dc58b6f4SAndy Webber 1401dc58b6f4SAndy Webber if(isset($info) && $info) { 1402dc58b6f4SAndy Webber switch($conf['showuseras']) { 1403dc58b6f4SAndy Webber case 'username': 1404dc58b6f4SAndy Webber return hsc($info['name']); 1405dc58b6f4SAndy Webber case 'email': 1406dc58b6f4SAndy Webber return obfuscate($info['mail']); 1407dc58b6f4SAndy Webber case 'email_link': 1408dc58b6f4SAndy Webber $mail = obfuscate($info['mail']); 1409dc58b6f4SAndy Webber return '<a href="mailto:'.$mail.'">'.$mail.'</a>'; 1410dc58b6f4SAndy Webber default: 1411dc58b6f4SAndy Webber return hsc($username); 1412dc58b6f4SAndy Webber } 1413dc58b6f4SAndy Webber } else { 1414dc58b6f4SAndy Webber return hsc($username); 1415dc58b6f4SAndy Webber } 1416066fee30SAndreas Gohr} 1417066fee30SAndreas Gohr 1418066fee30SAndreas Gohr/** 1419066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1420066fee30SAndreas Gohr * When no image exists, returns an empty string 1421066fee30SAndreas Gohr * 1422066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1423066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 14243272d797SAndreas Gohr * @return string 1425066fee30SAndreas Gohr */ 1426066fee30SAndreas Gohrfunction license_img($type) { 1427066fee30SAndreas Gohr global $license; 1428066fee30SAndreas Gohr global $conf; 1429066fee30SAndreas Gohr if(!$conf['license']) return ''; 1430066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1431066fee30SAndreas Gohr $lic = $license[$conf['license']]; 1432066fee30SAndreas Gohr $try = array(); 1433066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1434066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1435066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1436066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1437066fee30SAndreas Gohr } 1438066fee30SAndreas Gohr foreach($try as $src) { 1439066fee30SAndreas Gohr if(@file_exists(DOKU_INC.$src)) return $src; 1440066fee30SAndreas Gohr } 1441066fee30SAndreas Gohr return ''; 1442dc58b6f4SAndy Webber} 1443dc58b6f4SAndy Webber 144413c08e2fSMichael Klier/** 144513c08e2fSMichael Klier * Checks if the given amount of memory is available 144613c08e2fSMichael Klier * 144713c08e2fSMichael Klier * If the memory_get_usage() function is not available the 144813c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 144913c08e2fSMichael Klier * 145013c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 145113c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 14523272d797SAndreas Gohr * 14533272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 14543272d797SAndreas Gohr * @param int $bytes 14553272d797SAndreas Gohr * @internal param int $used already allocated memory (see above) 14563272d797SAndreas Gohr * @return bool 145713c08e2fSMichael Klier */ 145813c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 145913c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 146013c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 146113c08e2fSMichael Klier 146213c08e2fSMichael Klier // parse limit to bytes 146313c08e2fSMichael Klier $limit = php_to_byte($limit); 146413c08e2fSMichael Klier 146513c08e2fSMichael Klier // get used memory if possible 146613c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 146713c08e2fSMichael Klier $used = memory_get_usage(); 146849eb6e38SAndreas Gohr } else { 146949eb6e38SAndreas Gohr $used = $bytes; 147013c08e2fSMichael Klier } 147113c08e2fSMichael Klier 147213c08e2fSMichael Klier if($used + $mem > $limit) { 147313c08e2fSMichael Klier return false; 147413c08e2fSMichael Klier } 147513c08e2fSMichael Klier 147613c08e2fSMichael Klier return true; 147713c08e2fSMichael Klier} 147813c08e2fSMichael Klier 1479af2408d5SAndreas Gohr/** 1480af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1481af2408d5SAndreas Gohr * 1482af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1483af2408d5SAndreas Gohr * 1484af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1485af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1486af2408d5SAndreas Gohr */ 1487af2408d5SAndreas Gohrfunction send_redirect($url) { 14880181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 14890181f021SAndreas Gohr global $MSG; 14900181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 14910181f021SAndreas Gohr //reopen session, store data and close session again 14920181f021SAndreas Gohr @session_start(); 14930181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 14940181f021SAndreas Gohr } 14950181f021SAndreas Gohr 1496d4869846SAndreas Gohr // always close the session 1497d4869846SAndreas Gohr session_write_close(); 1498d4869846SAndreas Gohr 1499c10dcb7dSAndreas Gohr // work around IE bug 1500c10dcb7dSAndreas Gohr // http://www.ianhoar.com/2008/11/16/internet-explorer-6-and-redirected-anchor-links/ 1501c10dcb7dSAndreas Gohr list($url, $hash) = explode('#', $url); 1502c10dcb7dSAndreas Gohr if($hash) { 1503c10dcb7dSAndreas Gohr if(strpos($url, '?')) { 1504c10dcb7dSAndreas Gohr $url = $url.'&#'.$hash; 1505c10dcb7dSAndreas Gohr } else { 1506c10dcb7dSAndreas Gohr $url = $url.'?&#'.$hash; 1507c10dcb7dSAndreas Gohr } 1508c10dcb7dSAndreas Gohr } 1509c10dcb7dSAndreas Gohr 1510af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1511af2408d5SAndreas Gohr if(isset($_SERVER['SERVER_SOFTWARE']) && isset($_SERVER['GATEWAY_INTERFACE']) && 1512af2408d5SAndreas Gohr (strpos($_SERVER['GATEWAY_INTERFACE'], 'CGI') !== false) && 1513af2408d5SAndreas Gohr (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($_SERVER['SERVER_SOFTWARE']), $matches)) && 15143272d797SAndreas Gohr $matches[1] < 6 15153272d797SAndreas Gohr ) { 1516af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1517af2408d5SAndreas Gohr } else { 1518af2408d5SAndreas Gohr header('Location: '.$url); 1519af2408d5SAndreas Gohr } 1520af2408d5SAndreas Gohr exit; 1521af2408d5SAndreas Gohr} 1522af2408d5SAndreas Gohr 15235b75cd1fSAdrian Lang/** 15245b75cd1fSAdrian Lang * Validate a value using a set of valid values 15255b75cd1fSAdrian Lang * 15265b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 15275b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 15285b75cd1fSAdrian Lang * default is specified, throws an exception. 15295b75cd1fSAdrian Lang * 15305b75cd1fSAdrian Lang * @param string $param The name of the parameter 15315b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 15325b75cd1fSAdrian Lang * be marked by the key “default”. 15335b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 15345b75cd1fSAdrian Lang * or $_GET) 15355b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 15365b75cd1fSAdrian Lang * 15373272d797SAndreas Gohr * @throws Exception 15383272d797SAndreas Gohr * @return mixed 15395b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 15405b75cd1fSAdrian Lang */ 15415b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 15425b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 15435b75cd1fSAdrian Lang return $array[$param]; 15445b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 15455b75cd1fSAdrian Lang return $valid_values['default']; 15465b75cd1fSAdrian Lang } else { 15475b75cd1fSAdrian Lang throw new Exception($exc); 15485b75cd1fSAdrian Lang } 15495b75cd1fSAdrian Lang} 15505b75cd1fSAdrian Lang 155163703ba5SAndreas Gohr/** 155263703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1553646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 155463703ba5SAndreas Gohr */ 1555554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1556646a531aSChristopher Smith $enc_pref = urlencode($pref); 1557646a531aSChristopher Smith if(strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1558554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 155963703ba5SAndreas Gohr $cnt = count($parts); 156063703ba5SAndreas Gohr for($i = 0; $i < $cnt; $i += 2) { 1561646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1562646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1563554a8c9fSAdrian Lang } 1564554a8c9fSAdrian Lang } 1565554a8c9fSAdrian Lang } 1566554a8c9fSAdrian Lang return $default; 1567554a8c9fSAdrian Lang} 1568554a8c9fSAdrian Lang 15693c94d07bSAnika Henke/** 15703c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 157136ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 15723c94d07bSAnika Henke */ 15733c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 15743c94d07bSAnika Henke global $conf; 15753c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 15763c94d07bSAnika Henke $cookieVal = ''; 15773c94d07bSAnika Henke 15783c94d07bSAnika Henke if($orig && ($orig != $val)) { 15793c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 15803c94d07bSAnika Henke $cnt = count($parts); 158136ec377eSChristopher Smith // urlencode $pref for the comparison 158236ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 15833c94d07bSAnika Henke for($i = 0; $i < $cnt; $i += 2) { 158436ec377eSChristopher Smith if($parts[$i] == $enc_pref) { 158536ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 158650f261f7SMichael Hamann break; 15873c94d07bSAnika Henke } 15883c94d07bSAnika Henke } 15893c94d07bSAnika Henke $cookieVal = implode('#', $parts); 15903c94d07bSAnika Henke } else if (!$orig) { 159136ec377eSChristopher Smith $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : '').rawurlencode($pref).'#'.rawurlencode($val); 15923c94d07bSAnika Henke } 15933c94d07bSAnika Henke 15943c94d07bSAnika Henke if (!empty($cookieVal)) { 159550f261f7SMichael Hamann setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, DOKU_BASE, '', ($conf['securecookie'] && is_ssl())); 15963c94d07bSAnika Henke } 15973c94d07bSAnika Henke} 15983c94d07bSAnika Henke 1599e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1600