1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Common DokuWiki functions 415fae107Sandi * 515fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 715fae107Sandi */ 815fae107Sandi 9fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 10f3f0262cSandi 11f3f0262cSandi/** 12b6912aeaSAndreas Gohr * These constants are used with the recents function 13b6912aeaSAndreas Gohr */ 14b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_DELETED', 2); 15b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_MINORS', 4); 16b6912aeaSAndreas Gohrdefine('RECENTS_SKIP_SUBSPACES', 8); 170b926329SKate Arzamastsevadefine('RECENTS_MEDIA_CHANGES', 16); 180b926329SKate Arzamastsevadefine('RECENTS_MEDIA_PAGES_MIXED', 32); 19b6912aeaSAndreas Gohr 20b6912aeaSAndreas Gohr/** 21d5197206Schris * Wrapper around htmlspecialchars() 22d5197206Schris * 23d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 24d5197206Schris * @see htmlspecialchars() 25d5197206Schris */ 26d5197206Schrisfunction hsc($string) { 27d5197206Schris return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 28d5197206Schris} 29d5197206Schris 30d5197206Schris/** 31d5197206Schris * print a newline terminated string 32d5197206Schris * 33d5197206Schris * You can give an indention as optional parameter 34d5197206Schris * 35d5197206Schris * @author Andreas Gohr <andi@splitbrain.org> 36d5197206Schris */ 3725ec097bSChris Smithfunction ptln($string, $indent = 0) { 3825ec097bSChris Smith echo str_repeat(' ', $indent)."$string\n"; 3902b0b681SAndreas Gohr} 4002b0b681SAndreas Gohr 4102b0b681SAndreas Gohr/** 4202b0b681SAndreas Gohr * strips control characters (<32) from the given string 4302b0b681SAndreas Gohr * 4402b0b681SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 4502b0b681SAndreas Gohr */ 4602b0b681SAndreas Gohrfunction stripctl($string) { 4702b0b681SAndreas Gohr return preg_replace('/[\x00-\x1F]+/s', '', $string); 48d5197206Schris} 49d5197206Schris 50d5197206Schris/** 51634d7150SAndreas Gohr * Return a secret token to be used for CSRF attack prevention 52634d7150SAndreas Gohr * 53634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 54634d7150SAndreas Gohr * @link http://en.wikipedia.org/wiki/Cross-site_request_forgery 55634d7150SAndreas Gohr * @link http://christ1an.blogspot.com/2007/04/preventing-csrf-efficiently.html 56634d7150SAndreas Gohr * @return string 57634d7150SAndreas Gohr */ 58634d7150SAndreas Gohrfunction getSecurityToken() { 59585bf44eSChristopher Smith /** @var Input $INPUT */ 60585bf44eSChristopher Smith global $INPUT; 61585bf44eSChristopher Smith return PassHash::hmac('md5', session_id().$INPUT->server->str('REMOTE_USER'), auth_cookiesalt()); 62634d7150SAndreas Gohr} 63634d7150SAndreas Gohr 64634d7150SAndreas Gohr/** 65634d7150SAndreas Gohr * Check the secret CSRF token 66634d7150SAndreas Gohr */ 67634d7150SAndreas Gohrfunction checkSecurityToken($token = null) { 68585bf44eSChristopher Smith /** @var Input $INPUT */ 697d01a0eaSTom N Harris global $INPUT; 70585bf44eSChristopher Smith if(!$INPUT->server->str('REMOTE_USER')) return true; // no logged in user, no need for a check 71df97eaacSAndreas Gohr 727d01a0eaSTom N Harris if(is_null($token)) $token = $INPUT->str('sectok'); 73634d7150SAndreas Gohr if(getSecurityToken() != $token) { 74634d7150SAndreas Gohr msg('Security Token did not match. Possible CSRF attack.', -1); 75634d7150SAndreas Gohr return false; 76634d7150SAndreas Gohr } 77634d7150SAndreas Gohr return true; 78634d7150SAndreas Gohr} 79634d7150SAndreas Gohr 80634d7150SAndreas Gohr/** 81634d7150SAndreas Gohr * Print a hidden form field with a secret CSRF token 82634d7150SAndreas Gohr * 83634d7150SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 84634d7150SAndreas Gohr */ 85634d7150SAndreas Gohrfunction formSecurityToken($print = true) { 862404d0edSAnika Henke $ret = '<div class="no"><input type="hidden" name="sectok" value="'.getSecurityToken().'" /></div>'."\n"; 873272d797SAndreas Gohr if($print) echo $ret; 88634d7150SAndreas Gohr return $ret; 89634d7150SAndreas Gohr} 90634d7150SAndreas Gohr 91634d7150SAndreas Gohr/** 921015a57dSChristopher Smith * Determine basic information for a request of $id 9315fae107Sandi * 9415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 957e87a794SChristopher Smith * @author Chris Smith <chris@jalakai.co.uk> 96f3f0262cSandi */ 971015a57dSChristopher Smithfunction basicinfo($id, $htmlClient=true){ 98f3f0262cSandi global $USERINFO; 99585bf44eSChristopher Smith /* @var Input $INPUT */ 100585bf44eSChristopher Smith global $INPUT; 1016afe8dcaSchris 102c66972f2SAdrian Lang // set info about manager/admin status. 103c66972f2SAdrian Lang $info['isadmin'] = false; 104c66972f2SAdrian Lang $info['ismanager'] = false; 105585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 106f3f0262cSandi $info['userinfo'] = $USERINFO; 1071015a57dSChristopher Smith $info['perm'] = auth_quickaclcheck($id); 108585bf44eSChristopher Smith $info['client'] = $INPUT->server->str('REMOTE_USER'); 10917ee7f66SAndreas Gohr 110f8cc712eSAndreas Gohr if($info['perm'] == AUTH_ADMIN) { 111f8cc712eSAndreas Gohr $info['isadmin'] = true; 112f8cc712eSAndreas Gohr $info['ismanager'] = true; 113f8cc712eSAndreas Gohr } elseif(auth_ismanager()) { 114f8cc712eSAndreas Gohr $info['ismanager'] = true; 115f8cc712eSAndreas Gohr } 116f8cc712eSAndreas Gohr 11717ee7f66SAndreas Gohr // if some outside auth were used only REMOTE_USER is set 11817ee7f66SAndreas Gohr if(!$info['userinfo']['name']) { 119585bf44eSChristopher Smith $info['userinfo']['name'] = $INPUT->server->str('REMOTE_USER'); 12017ee7f66SAndreas Gohr } 121ee4c4a1bSAndreas Gohr 122f3f0262cSandi } else { 1231015a57dSChristopher Smith $info['perm'] = auth_aclcheck($id, '', null); 124ee4c4a1bSAndreas Gohr $info['client'] = clientIP(true); 125f3f0262cSandi } 126f3f0262cSandi 1271015a57dSChristopher Smith $info['namespace'] = getNS($id); 1281015a57dSChristopher Smith 1291015a57dSChristopher Smith // mobile detection 1301015a57dSChristopher Smith if ($htmlClient) { 1311015a57dSChristopher Smith $info['ismobile'] = clientismobile(); 1321015a57dSChristopher Smith } 1331015a57dSChristopher Smith 1341015a57dSChristopher Smith return $info; 1351015a57dSChristopher Smith } 1361015a57dSChristopher Smith 1371015a57dSChristopher Smith/** 1381015a57dSChristopher Smith * Return info about the current document as associative 1391015a57dSChristopher Smith * array. 1401015a57dSChristopher Smith * 1411015a57dSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org> 1421015a57dSChristopher Smith */ 1431015a57dSChristopher Smithfunction pageinfo() { 1441015a57dSChristopher Smith global $ID; 1451015a57dSChristopher Smith global $REV; 1461015a57dSChristopher Smith global $RANGE; 1471015a57dSChristopher Smith global $lang; 148585bf44eSChristopher Smith /* @var Input $INPUT */ 149585bf44eSChristopher Smith global $INPUT; 1501015a57dSChristopher Smith 1511015a57dSChristopher Smith $info = basicinfo($ID); 1521015a57dSChristopher Smith 1531015a57dSChristopher Smith // include ID & REV not redundant, as some parts of DokuWiki may temporarily change $ID, e.g. p_wiki_xhtml 1541015a57dSChristopher Smith // FIXME ... perhaps it would be better to ensure the temporary changes weren't necessary 1551015a57dSChristopher Smith $info['id'] = $ID; 1561015a57dSChristopher Smith $info['rev'] = $REV; 1571015a57dSChristopher Smith 158585bf44eSChristopher Smith if($INPUT->server->has('REMOTE_USER')) { 1597e87a794SChristopher Smith $sub = new Subscription(); 1607e87a794SChristopher Smith $info['subscribed'] = $sub->user_subscription(); 1617e87a794SChristopher Smith } else { 1627e87a794SChristopher Smith $info['subscribed'] = false; 1637e87a794SChristopher Smith } 1647e87a794SChristopher Smith 165f3f0262cSandi $info['locked'] = checklock($ID); 16600976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID)); 1672ca9d91cSBen Coburn $info['exists'] = @file_exists($info['filepath']); 16801c9a118SAndreas Gohr $info['currentrev'] = @filemtime($info['filepath']); 1692ca9d91cSBen Coburn if($REV) { 1702ca9d91cSBen Coburn //check if current revision was meant 17101c9a118SAndreas Gohr if($info['exists'] && ($info['currentrev'] == $REV)) { 172d8117804Slisps $REV = ''; 1737b3a6803SAndreas Gohr } elseif($RANGE) { 1747b3a6803SAndreas Gohr //section editing does not work with old revisions! 1757b3a6803SAndreas Gohr $REV = ''; 1767b3a6803SAndreas Gohr $RANGE = ''; 1777b3a6803SAndreas Gohr msg($lang['nosecedit'], 0); 1782ca9d91cSBen Coburn } else { 1792ca9d91cSBen Coburn //really use old revision 18000976812SAndreas Gohr $info['filepath'] = fullpath(wikiFN($ID, $REV)); 181f3f0262cSandi $info['exists'] = @file_exists($info['filepath']); 182f3f0262cSandi } 183f3f0262cSandi } 184c112d578Sandi $info['rev'] = $REV; 185f3f0262cSandi if($info['exists']) { 186f3f0262cSandi $info['writable'] = (is_writable($info['filepath']) && 187f3f0262cSandi ($info['perm'] >= AUTH_EDIT)); 188f3f0262cSandi } else { 189f3f0262cSandi $info['writable'] = ($info['perm'] >= AUTH_CREATE); 190f3f0262cSandi } 19150e988b1SAndreas Gohr $info['editable'] = ($info['writable'] && empty($info['locked'])); 192f3f0262cSandi $info['lastmod'] = @filemtime($info['filepath']); 193f3f0262cSandi 19471726d78SBen Coburn //load page meta data 19571726d78SBen Coburn $info['meta'] = p_get_metadata($ID); 19671726d78SBen Coburn 197652610a2Sandi //who's the editor 198047bad06SGerrit Uitslag $pagelog = new PageChangeLog($ID, 1024); 199652610a2Sandi if($REV) { 200f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($REV); 201652610a2Sandi } else { 2020e80bb5eSChristopher Smith if(!empty($info['meta']['last_change']) && is_array($info['meta']['last_change'])) { 203aa27cf05SAndreas Gohr $revinfo = $info['meta']['last_change']; 204aa27cf05SAndreas Gohr } else { 205f523c971SGerrit Uitslag $revinfo = $pagelog->getRevisionInfo($info['lastmod']); 206cd00a034SBen Coburn // cache most recent changelog line in metadata if missing and still valid 207cd00a034SBen Coburn if($revinfo !== false) { 208cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 209cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 210cd00a034SBen Coburn } 211cd00a034SBen Coburn } 212cd00a034SBen Coburn } 213cd00a034SBen Coburn //and check for an external edit 214cd00a034SBen Coburn if($revinfo !== false && $revinfo['date'] != $info['lastmod']) { 215cd00a034SBen Coburn // cached changelog line no longer valid 216cd00a034SBen Coburn $revinfo = false; 217cd00a034SBen Coburn $info['meta']['last_change'] = $revinfo; 218cd00a034SBen Coburn p_set_metadata($ID, array('last_change' => $revinfo)); 219652610a2Sandi } 220bb4866bdSchris 221652610a2Sandi $info['ip'] = $revinfo['ip']; 222652610a2Sandi $info['user'] = $revinfo['user']; 223652610a2Sandi $info['sum'] = $revinfo['sum']; 22471726d78SBen Coburn // See also $INFO['meta']['last_change'] which is the most recent log line for page $ID. 225ebf1501fSBen Coburn // Use $INFO['meta']['last_change']['type']===DOKU_CHANGE_TYPE_MINOR_EDIT in place of $info['minor']. 22659f257aeSchris 22788f522e9Sandi if($revinfo['user']) { 22888f522e9Sandi $info['editor'] = $revinfo['user']; 22988f522e9Sandi } else { 23088f522e9Sandi $info['editor'] = $revinfo['ip']; 23188f522e9Sandi } 232652610a2Sandi 233ee4c4a1bSAndreas Gohr // draft 234ee4c4a1bSAndreas Gohr $draft = getCacheName($info['client'].$ID, '.draft'); 235ee4c4a1bSAndreas Gohr if(@file_exists($draft)) { 236ee4c4a1bSAndreas Gohr if(@filemtime($draft) < @filemtime(wikiFN($ID))) { 237ee4c4a1bSAndreas Gohr // remove stale draft 238ee4c4a1bSAndreas Gohr @unlink($draft); 239ee4c4a1bSAndreas Gohr } else { 240ee4c4a1bSAndreas Gohr $info['draft'] = $draft; 241ee4c4a1bSAndreas Gohr } 242ee4c4a1bSAndreas Gohr } 243ee4c4a1bSAndreas Gohr 2441015a57dSChristopher Smith return $info; 2451015a57dSChristopher Smith} 2461015a57dSChristopher Smith 2471015a57dSChristopher Smith/** 2481015a57dSChristopher Smith * Return information about the current media item as an associative array. 2491015a57dSChristopher Smith */ 2501015a57dSChristopher Smithfunction mediainfo(){ 2511015a57dSChristopher Smith global $NS; 2521015a57dSChristopher Smith global $IMG; 2531015a57dSChristopher Smith 2541015a57dSChristopher Smith $info = basicinfo("$NS:*"); 2551015a57dSChristopher Smith $info['image'] = $IMG; 2561c548ebeSAndreas Gohr 257f3f0262cSandi return $info; 258f3f0262cSandi} 259f3f0262cSandi 260f3f0262cSandi/** 2612684e50aSAndreas Gohr * Build an string of URL parameters 2622684e50aSAndreas Gohr * 2632684e50aSAndreas Gohr * @author Andreas Gohr 2642684e50aSAndreas Gohr */ 265b174aeaeSchrisfunction buildURLparams($params, $sep = '&') { 2662684e50aSAndreas Gohr $url = ''; 2672684e50aSAndreas Gohr $amp = false; 2682684e50aSAndreas Gohr foreach($params as $key => $val) { 269b174aeaeSchris if($amp) $url .= $sep; 2702684e50aSAndreas Gohr 27185e6871fSAdrian Lang $url .= rawurlencode($key).'='; 2723a50618cSgweissbach $url .= rawurlencode((string) $val); 2732684e50aSAndreas Gohr $amp = true; 2742684e50aSAndreas Gohr } 2752684e50aSAndreas Gohr return $url; 2762684e50aSAndreas Gohr} 2772684e50aSAndreas Gohr 2782684e50aSAndreas Gohr/** 2792684e50aSAndreas Gohr * Build an string of html tag attributes 2802684e50aSAndreas Gohr * 2817bff22c0SAndreas Gohr * Skips keys starting with '_', values get HTML encoded 2827bff22c0SAndreas Gohr * 2832684e50aSAndreas Gohr * @author Andreas Gohr 2842684e50aSAndreas Gohr */ 2854b030ce7SAndreas Gohrfunction buildAttributes($params, $skipempty = false) { 2862684e50aSAndreas Gohr $url = ''; 2879063ec14SAdrian Lang $white = false; 2882684e50aSAndreas Gohr foreach($params as $key => $val) { 2897bff22c0SAndreas Gohr if($key{0} == '_') continue; 290b1c94f1dSAndreas Gohr if($val === '' && $skipempty) continue; 2919063ec14SAdrian Lang if($white) $url .= ' '; 2927bff22c0SAndreas Gohr 2932684e50aSAndreas Gohr $url .= $key.'="'; 2942684e50aSAndreas Gohr $url .= htmlspecialchars($val); 2952684e50aSAndreas Gohr $url .= '"'; 2969063ec14SAdrian Lang $white = true; 2972684e50aSAndreas Gohr } 2982684e50aSAndreas Gohr return $url; 2992684e50aSAndreas Gohr} 3002684e50aSAndreas Gohr 3012684e50aSAndreas Gohr/** 30215fae107Sandi * This builds the breadcrumb trail and returns it as array 30315fae107Sandi * 30415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 305f3f0262cSandi */ 306f3f0262cSandifunction breadcrumbs() { 3078746e727Sandi // we prepare the breadcrumbs early for quick session closing 3088746e727Sandi static $crumbs = null; 3098746e727Sandi if($crumbs != null) return $crumbs; 3108746e727Sandi 311f3f0262cSandi global $ID; 312f3f0262cSandi global $ACT; 313f3f0262cSandi global $conf; 314f3f0262cSandi 315f3f0262cSandi //first visit? 316c66972f2SAdrian Lang $crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array(); 317f3f0262cSandi //we only save on show and existing wiki documents 318a77f5846Sjan $file = wikiFN($ID); 319a77f5846Sjan if($ACT != 'show' || !@file_exists($file)) { 320e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 321f3f0262cSandi return $crumbs; 322f3f0262cSandi } 323a77f5846Sjan 324a77f5846Sjan // page names 3251a84a0f3SAnika Henke $name = noNSorNS($ID); 326fe9ec250SChris Smith if(useHeading('navigation')) { 327a77f5846Sjan // get page title 32867c15eceSMichael Hamann $title = p_get_first_heading($ID, METADATA_RENDER_USING_SIMPLE_CACHE); 329a77f5846Sjan if($title) { 330a77f5846Sjan $name = $title; 331a77f5846Sjan } 332a77f5846Sjan } 333a77f5846Sjan 334f3f0262cSandi //remove ID from array 335a77f5846Sjan if(isset($crumbs[$ID])) { 336a77f5846Sjan unset($crumbs[$ID]); 337f3f0262cSandi } 338f3f0262cSandi 339f3f0262cSandi //add to array 340a77f5846Sjan $crumbs[$ID] = $name; 341f3f0262cSandi //reduce size 342f3f0262cSandi while(count($crumbs) > $conf['breadcrumbs']) { 343f3f0262cSandi array_shift($crumbs); 344f3f0262cSandi } 345f3f0262cSandi //save to session 346e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['bc'] = $crumbs; 347f3f0262cSandi return $crumbs; 348f3f0262cSandi} 349f3f0262cSandi 350f3f0262cSandi/** 35115fae107Sandi * Filter for page IDs 35215fae107Sandi * 353f3f0262cSandi * This is run on a ID before it is outputted somewhere 354f3f0262cSandi * currently used to replace the colon with something else 355907f24f7SAndreas Gohr * on Windows (non-IIS) systems and to have proper URL encoding 356907f24f7SAndreas Gohr * 357907f24f7SAndreas Gohr * See discussions at https://github.com/splitbrain/dokuwiki/pull/84 and 358907f24f7SAndreas Gohr * https://github.com/splitbrain/dokuwiki/pull/173 why we use a whitelist of 359907f24f7SAndreas Gohr * unaffected servers instead of blacklisting affected servers here. 36015fae107Sandi * 36149c713a3Sandi * Urlencoding is ommitted when the second parameter is false 36249c713a3Sandi * 36315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 364f3f0262cSandi */ 36549c713a3Sandifunction idfilter($id, $ue = true) { 366f3f0262cSandi global $conf; 367585bf44eSChristopher Smith /* @var Input $INPUT */ 368585bf44eSChristopher Smith global $INPUT; 369585bf44eSChristopher Smith 370f3f0262cSandi if($conf['useslash'] && $conf['userewrite']) { 371f3f0262cSandi $id = strtr($id, ':', '/'); 372f3f0262cSandi } elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && 37358bedc8aSborekb $conf['userewrite'] && 374585bf44eSChristopher Smith strpos($INPUT->server->str('SERVER_SOFTWARE'), 'Microsoft-IIS') === false 3753272d797SAndreas Gohr ) { 376f3f0262cSandi $id = strtr($id, ':', ';'); 377f3f0262cSandi } 37849c713a3Sandi if($ue) { 379b6c6979fSAndreas Gohr $id = rawurlencode($id); 380f3f0262cSandi $id = str_replace('%3A', ':', $id); //keep as colon 381f3f0262cSandi $id = str_replace('%2F', '/', $id); //keep as slash 38249c713a3Sandi } 383f3f0262cSandi return $id; 384f3f0262cSandi} 385f3f0262cSandi 386f3f0262cSandi/** 387ed7b5f09Sandi * This builds a link to a wikipage 38815fae107Sandi * 3896c7843b5Sandi * It handles URL rewriting and adds additional parameter if 3906c7843b5Sandi * given in $more 3916c7843b5Sandi * 39215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 393f3f0262cSandi */ 39416f15a81SDominik Eckelmannfunction wl($id = '', $urlParameters = '', $absolute = false, $separator = '&') { 395f3f0262cSandi global $conf; 39616f15a81SDominik Eckelmann if(is_array($urlParameters)) { 3974bde2196Slisps if(isset($urlParameters['rev']) && !$urlParameters['rev']) unset($urlParameters['rev']); 398*7b62b42dSlisps if(isset($urlParameters['at']) && $conf['date_at_format']) $urlParameters['at'] = date($conf['date_at_format'],$urlParameters['at']); 39916f15a81SDominik Eckelmann $urlParameters = buildURLparams($urlParameters, $separator); 4006de3759aSAndreas Gohr } else { 40116f15a81SDominik Eckelmann $urlParameters = str_replace(',', $separator, $urlParameters); 4026de3759aSAndreas Gohr } 40316f15a81SDominik Eckelmann if($id === '') { 40416f15a81SDominik Eckelmann $id = $conf['start']; 40516f15a81SDominik Eckelmann } 406f3f0262cSandi $id = idfilter($id); 40716f15a81SDominik Eckelmann if($absolute) { 408ed7b5f09Sandi $xlink = DOKU_URL; 409ed7b5f09Sandi } else { 410ed7b5f09Sandi $xlink = DOKU_BASE; 411ed7b5f09Sandi } 412f3f0262cSandi 4136c7843b5Sandi if($conf['userewrite'] == 2) { 4146c7843b5Sandi $xlink .= DOKU_SCRIPT.'/'.$id; 41516f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 4166c7843b5Sandi } elseif($conf['userewrite']) { 417f3f0262cSandi $xlink .= $id; 41816f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 419bce3726dSAndreas Gohr } elseif($id) { 4206c7843b5Sandi $xlink .= DOKU_SCRIPT.'?id='.$id; 42116f15a81SDominik Eckelmann if($urlParameters) $xlink .= $separator.$urlParameters; 422bce3726dSAndreas Gohr } else { 423bce3726dSAndreas Gohr $xlink .= DOKU_SCRIPT; 42416f15a81SDominik Eckelmann if($urlParameters) $xlink .= '?'.$urlParameters; 425f3f0262cSandi } 426f3f0262cSandi 427f3f0262cSandi return $xlink; 428f3f0262cSandi} 429f3f0262cSandi 430f3f0262cSandi/** 431f5c2808fSBen Coburn * This builds a link to an alternate page format 432f5c2808fSBen Coburn * 433f5c2808fSBen Coburn * Handles URL rewriting if enabled. Follows the style of wl(). 434f5c2808fSBen Coburn * 435f5c2808fSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 436f5c2808fSBen Coburn */ 437f5c2808fSBen Coburnfunction exportlink($id = '', $format = 'raw', $more = '', $abs = false, $sep = '&') { 438f5c2808fSBen Coburn global $conf; 439f5c2808fSBen Coburn if(is_array($more)) { 440f5c2808fSBen Coburn $more = buildURLparams($more, $sep); 441f5c2808fSBen Coburn } else { 442f5c2808fSBen Coburn $more = str_replace(',', $sep, $more); 443f5c2808fSBen Coburn } 444f5c2808fSBen Coburn 445f5c2808fSBen Coburn $format = rawurlencode($format); 446f5c2808fSBen Coburn $id = idfilter($id); 447f5c2808fSBen Coburn if($abs) { 448f5c2808fSBen Coburn $xlink = DOKU_URL; 449f5c2808fSBen Coburn } else { 450f5c2808fSBen Coburn $xlink = DOKU_BASE; 451f5c2808fSBen Coburn } 452f5c2808fSBen Coburn 453f5c2808fSBen Coburn if($conf['userewrite'] == 2) { 454f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'/'.$id.'?do=export_'.$format; 455f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 456f5c2808fSBen Coburn } elseif($conf['userewrite'] == 1) { 457f5c2808fSBen Coburn $xlink .= '_export/'.$format.'/'.$id; 458f5c2808fSBen Coburn if($more) $xlink .= '?'.$more; 459f5c2808fSBen Coburn } else { 460f5c2808fSBen Coburn $xlink .= DOKU_SCRIPT.'?do=export_'.$format.$sep.'id='.$id; 461f5c2808fSBen Coburn if($more) $xlink .= $sep.$more; 462f5c2808fSBen Coburn } 463f5c2808fSBen Coburn 464f5c2808fSBen Coburn return $xlink; 465f5c2808fSBen Coburn} 466f5c2808fSBen Coburn 467f5c2808fSBen Coburn/** 4686de3759aSAndreas Gohr * Build a link to a media file 4696de3759aSAndreas Gohr * 4706de3759aSAndreas Gohr * Will return a link to the detail page if $direct is false 4718c08db0aSAndreas Gohr * 4728c08db0aSAndreas Gohr * The $more parameter should always be given as array, the function then 4738c08db0aSAndreas Gohr * will strip default parameters to produce even cleaner URLs 4748c08db0aSAndreas Gohr * 4753272d797SAndreas Gohr * @param string $id the media file id or URL 4763272d797SAndreas Gohr * @param mixed $more string or array with additional parameters 4773272d797SAndreas Gohr * @param bool $direct link to detail page if false 4783272d797SAndreas Gohr * @param string $sep URL parameter separator 4793272d797SAndreas Gohr * @param bool $abs Create an absolute URL 4803272d797SAndreas Gohr * @return string 4816de3759aSAndreas Gohr */ 48255b2b31bSAndreas Gohrfunction ml($id = '', $more = '', $direct = true, $sep = '&', $abs = false) { 4836de3759aSAndreas Gohr global $conf; 484b9ee6a44SKlap-in $isexternalimage = media_isexternal($id); 485826d2766SKlap-in if(!$isexternalimage) { 486826d2766SKlap-in $id = cleanID($id); 487826d2766SKlap-in } 488826d2766SKlap-in 4896de3759aSAndreas Gohr if(is_array($more)) { 4900f4e0092SChristopher Smith // add token for resized images 491443e135dSChristopher Smith if(!empty($more['w']) || !empty($more['h']) || $isexternalimage){ 4920f4e0092SChristopher Smith $more['tok'] = media_get_token($id,$more['w'],$more['h']); 4930f4e0092SChristopher Smith } 4948c08db0aSAndreas Gohr // strip defaults for shorter URLs 4958c08db0aSAndreas Gohr if(isset($more['cache']) && $more['cache'] == 'cache') unset($more['cache']); 496443e135dSChristopher Smith if(empty($more['w'])) unset($more['w']); 497443e135dSChristopher Smith if(empty($more['h'])) unset($more['h']); 4988c08db0aSAndreas Gohr if(isset($more['id']) && $direct) unset($more['id']); 49978b874e6Slisps if(isset($more['rev']) && !$more['rev']) unset($more['rev']); 500b174aeaeSchris $more = buildURLparams($more, $sep); 5016de3759aSAndreas Gohr } else { 5025e7db1e2SChristopher Smith $matches = array(); 503cc036f74SKlap-in if (preg_match_all('/\b(w|h)=(\d*)\b/',$more,$matches,PREG_SET_ORDER) || $isexternalimage){ 5045e7db1e2SChristopher Smith $resize = array('w'=>0, 'h'=>0); 5055e7db1e2SChristopher Smith foreach ($matches as $match){ 5065e7db1e2SChristopher Smith $resize[$match[1]] = $match[2]; 5075e7db1e2SChristopher Smith } 508cc036f74SKlap-in $more .= $more === '' ? '' : $sep; 509cc036f74SKlap-in $more .= 'tok='.media_get_token($id,$resize['w'],$resize['h']); 5105e7db1e2SChristopher Smith } 5118c08db0aSAndreas Gohr $more = str_replace('cache=cache', '', $more); //skip default 5128c08db0aSAndreas Gohr $more = str_replace(',,', ',', $more); 513b174aeaeSchris $more = str_replace(',', $sep, $more); 5146de3759aSAndreas Gohr } 5156de3759aSAndreas Gohr 51655b2b31bSAndreas Gohr if($abs) { 51755b2b31bSAndreas Gohr $xlink = DOKU_URL; 51855b2b31bSAndreas Gohr } else { 5196de3759aSAndreas Gohr $xlink = DOKU_BASE; 52055b2b31bSAndreas Gohr } 5216de3759aSAndreas Gohr 5226de3759aSAndreas Gohr // external URLs are always direct without rewriting 523826d2766SKlap-in if($isexternalimage) { 5246de3759aSAndreas Gohr $xlink .= 'lib/exe/fetch.php'; 525cc036f74SKlap-in $xlink .= '?'.$more; 526b174aeaeSchris $xlink .= $sep.'media='.rawurlencode($id); 5276de3759aSAndreas Gohr return $xlink; 5286de3759aSAndreas Gohr } 5296de3759aSAndreas Gohr 5306de3759aSAndreas Gohr $id = idfilter($id); 5316de3759aSAndreas Gohr 5326de3759aSAndreas Gohr // decide on scriptname 5336de3759aSAndreas Gohr if($direct) { 5346de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 5356de3759aSAndreas Gohr $script = '_media'; 5366de3759aSAndreas Gohr } else { 5376de3759aSAndreas Gohr $script = 'lib/exe/fetch.php'; 5386de3759aSAndreas Gohr } 5396de3759aSAndreas Gohr } else { 5406de3759aSAndreas Gohr if($conf['userewrite'] == 1) { 5416de3759aSAndreas Gohr $script = '_detail'; 5426de3759aSAndreas Gohr } else { 5436de3759aSAndreas Gohr $script = 'lib/exe/detail.php'; 5446de3759aSAndreas Gohr } 5456de3759aSAndreas Gohr } 5466de3759aSAndreas Gohr 5476de3759aSAndreas Gohr // build URL based on rewrite mode 5486de3759aSAndreas Gohr if($conf['userewrite']) { 5496de3759aSAndreas Gohr $xlink .= $script.'/'.$id; 5506de3759aSAndreas Gohr if($more) $xlink .= '?'.$more; 5516de3759aSAndreas Gohr } else { 5526de3759aSAndreas Gohr if($more) { 553a99d3236SEsther Brunner $xlink .= $script.'?'.$more; 554b174aeaeSchris $xlink .= $sep.'media='.$id; 5556de3759aSAndreas Gohr } else { 556a99d3236SEsther Brunner $xlink .= $script.'?media='.$id; 5576de3759aSAndreas Gohr } 5586de3759aSAndreas Gohr } 5596de3759aSAndreas Gohr 5606de3759aSAndreas Gohr return $xlink; 5616de3759aSAndreas Gohr} 5626de3759aSAndreas Gohr 5636de3759aSAndreas Gohr/** 56425ca5b17SAndreas Gohr * Returns the URL to the DokuWiki base script 56515fae107Sandi * 56625ca5b17SAndreas Gohr * Consider using wl() instead, unless you absoutely need the doku.php endpoint 56725ca5b17SAndreas Gohr * 56815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 569f3f0262cSandi */ 57025ca5b17SAndreas Gohrfunction script() { 571ed7b5f09Sandi return DOKU_BASE.DOKU_SCRIPT; 572f3f0262cSandi} 573f3f0262cSandi 574f3f0262cSandi/** 57515fae107Sandi * Spamcheck against wordlist 57615fae107Sandi * 577f3f0262cSandi * Checks the wikitext against a list of blocked expressions 578f3f0262cSandi * returns true if the text contains any bad words 57915fae107Sandi * 580e403cc58SMichael Klier * Triggers COMMON_WORDBLOCK_BLOCKED 581e403cc58SMichael Klier * 582e403cc58SMichael Klier * Action Plugins can use this event to inspect the blocked data 583e403cc58SMichael Klier * and gain information about the user who was blocked. 584e403cc58SMichael Klier * 585e403cc58SMichael Klier * Event data: 586e403cc58SMichael Klier * data['matches'] - array of matches 587e403cc58SMichael Klier * data['userinfo'] - information about the blocked user 588e403cc58SMichael Klier * [ip] - ip address 589e403cc58SMichael Klier * [user] - username (if logged in) 590e403cc58SMichael Klier * [mail] - mail address (if logged in) 591e403cc58SMichael Klier * [name] - real name (if logged in) 592e403cc58SMichael Klier * 59315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 5946dffa0e0SAndreas Gohr * @author Michael Klier <chi@chimeric.de> 5956dffa0e0SAndreas Gohr * @param string $text - optional text to check, if not given the globals are used 5966dffa0e0SAndreas Gohr * @return bool - true if a spam word was found 597f3f0262cSandi */ 5986dffa0e0SAndreas Gohrfunction checkwordblock($text = '') { 599f3f0262cSandi global $TEXT; 6006dffa0e0SAndreas Gohr global $PRE; 6016dffa0e0SAndreas Gohr global $SUF; 602e0086ca2SAndreas Gohr global $SUM; 603f3f0262cSandi global $conf; 604e403cc58SMichael Klier global $INFO; 605585bf44eSChristopher Smith /* @var Input $INPUT */ 606585bf44eSChristopher Smith global $INPUT; 607f3f0262cSandi 608f3f0262cSandi if(!$conf['usewordblock']) return false; 609f3f0262cSandi 610e0086ca2SAndreas Gohr if(!$text) $text = "$PRE $TEXT $SUF $SUM"; 6116dffa0e0SAndreas Gohr 612041d1964SAndreas Gohr // we prepare the text a tiny bit to prevent spammers circumventing URL checks 6136dffa0e0SAndreas Gohr $text = preg_replace('!(\b)(www\.[\w.:?\-;,]+?\.[\w.:?\-;,]+?[\w/\#~:.?+=&%@\!\-.:?\-;,]+?)([.:?\-;,]*[^\w/\#~:.?+=&%@\!\-.:?\-;,])!i', '\1http://\2 \2\3', $text); 614041d1964SAndreas Gohr 615b9ac8716Schris $wordblocks = getWordblocks(); 6163e2965d7Sandi // how many lines to read at once (to work around some PCRE limits) 6173e2965d7Sandi if(version_compare(phpversion(), '4.3.0', '<')) { 6183e2965d7Sandi // old versions of PCRE define a maximum of parenthesises even if no 6193e2965d7Sandi // backreferences are used - the maximum is 99 6203e2965d7Sandi // this is very bad performancewise and may even be too high still 6213e2965d7Sandi $chunksize = 40; 6223e2965d7Sandi } else { 623a51d08efSAndreas Gohr // read file in chunks of 200 - this should work around the 6243e2965d7Sandi // MAX_PATTERN_SIZE in modern PCRE 625a51d08efSAndreas Gohr $chunksize = 200; 6263e2965d7Sandi } 627b9ac8716Schris while($blocks = array_splice($wordblocks, 0, $chunksize)) { 628f3f0262cSandi $re = array(); 62949eb6e38SAndreas Gohr // build regexp from blocks 630f3f0262cSandi foreach($blocks as $block) { 631f3f0262cSandi $block = preg_replace('/#.*$/', '', $block); 632f3f0262cSandi $block = trim($block); 633f3f0262cSandi if(empty($block)) continue; 634f3f0262cSandi $re[] = $block; 635f3f0262cSandi } 636e403cc58SMichael Klier if(count($re) && preg_match('#('.join('|', $re).')#si', $text, $matches)) { 637e403cc58SMichael Klier // prepare event data 638e403cc58SMichael Klier $data['matches'] = $matches; 639585bf44eSChristopher Smith $data['userinfo']['ip'] = $INPUT->server->str('REMOTE_ADDR'); 640585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 641585bf44eSChristopher Smith $data['userinfo']['user'] = $INPUT->server->str('REMOTE_USER'); 642e403cc58SMichael Klier $data['userinfo']['name'] = $INFO['userinfo']['name']; 643e403cc58SMichael Klier $data['userinfo']['mail'] = $INFO['userinfo']['mail']; 644e403cc58SMichael Klier } 645e403cc58SMichael Klier $callback = create_function('', 'return true;'); 646e403cc58SMichael Klier return trigger_event('COMMON_WORDBLOCK_BLOCKED', $data, $callback, true); 647b9ac8716Schris } 648703f6fdeSandi } 649f3f0262cSandi return false; 650f3f0262cSandi} 651f3f0262cSandi 652f3f0262cSandi/** 65315fae107Sandi * Return the IP of the client 65415fae107Sandi * 6556d8affe6SAndreas Gohr * Honours X-Forwarded-For and X-Real-IP Proxy Headers 65615fae107Sandi * 6576d8affe6SAndreas Gohr * It returns a comma separated list of IPs if the above mentioned 6586d8affe6SAndreas Gohr * headers are set. If the single parameter is set, it tries to return 6596d8affe6SAndreas Gohr * a routable public address, prefering the ones suplied in the X 6606d8affe6SAndreas Gohr * headers 6616d8affe6SAndreas Gohr * 66215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6633272d797SAndreas Gohr * @param boolean $single If set only a single IP is returned 6643272d797SAndreas Gohr * @return string 665f3f0262cSandi */ 6666d8affe6SAndreas Gohrfunction clientIP($single = false) { 667585bf44eSChristopher Smith /* @var Input $INPUT */ 668585bf44eSChristopher Smith global $INPUT; 669585bf44eSChristopher Smith 6706d8affe6SAndreas Gohr $ip = array(); 671585bf44eSChristopher Smith $ip[] = $INPUT->server->str('REMOTE_ADDR'); 672585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_FORWARDED_FOR')) { 673585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_FORWARDED_FOR')))); 674585bf44eSChristopher Smith } 675585bf44eSChristopher Smith if($INPUT->server->str('HTTP_X_REAL_IP')) { 676585bf44eSChristopher Smith $ip = array_merge($ip, explode(',', str_replace(' ', '', $INPUT->server->str('HTTP_X_REAL_IP')))); 677585bf44eSChristopher Smith } 6786d8affe6SAndreas Gohr 679dc14c6d1SGuy Brand // some IPv4/v6 regexps borrowed from Feyd 680dc14c6d1SGuy Brand // see: http://forums.devnetwork.net/viewtopic.php?f=38&t=53479 681dc14c6d1SGuy Brand $dec_octet = '(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|[0-9])'; 682dc14c6d1SGuy Brand $hex_digit = '[A-Fa-f0-9]'; 683dc14c6d1SGuy Brand $h16 = "{$hex_digit}{1,4}"; 684dc14c6d1SGuy Brand $IPv4Address = "$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet"; 685dc14c6d1SGuy Brand $ls32 = "(?:$h16:$h16|$IPv4Address)"; 686dc14c6d1SGuy Brand $IPv6Address = 687dc14c6d1SGuy Brand "(?:(?:{$IPv4Address})|(?:". 688dc14c6d1SGuy Brand "(?:$h16:){6}$ls32". 689dc14c6d1SGuy Brand "|::(?:$h16:){5}$ls32". 690dc14c6d1SGuy Brand "|(?:$h16)?::(?:$h16:){4}$ls32". 691dc14c6d1SGuy Brand "|(?:(?:$h16:){0,1}$h16)?::(?:$h16:){3}$ls32". 692dc14c6d1SGuy Brand "|(?:(?:$h16:){0,2}$h16)?::(?:$h16:){2}$ls32". 693dc14c6d1SGuy Brand "|(?:(?:$h16:){0,3}$h16)?::(?:$h16:){1}$ls32". 694dc14c6d1SGuy Brand "|(?:(?:$h16:){0,4}$h16)?::$ls32". 695dc14c6d1SGuy Brand "|(?:(?:$h16:){0,5}$h16)?::$h16". 696dc14c6d1SGuy Brand "|(?:(?:$h16:){0,6}$h16)?::". 697dc14c6d1SGuy Brand ")(?:\\/(?:12[0-8]|1[0-1][0-9]|[1-9][0-9]|[0-9]))?)"; 698dc14c6d1SGuy Brand 6996d8affe6SAndreas Gohr // remove any non-IP stuff 7006d8affe6SAndreas Gohr $cnt = count($ip); 7014ff28443Schris $match = array(); 7026d8affe6SAndreas Gohr for($i = 0; $i < $cnt; $i++) { 703dc14c6d1SGuy Brand if(preg_match("/^$IPv4Address$/", $ip[$i], $match) || preg_match("/^$IPv6Address$/", $ip[$i], $match)) { 7044ff28443Schris $ip[$i] = $match[0]; 7054ff28443Schris } else { 7064ff28443Schris $ip[$i] = ''; 7074ff28443Schris } 7086d8affe6SAndreas Gohr if(empty($ip[$i])) unset($ip[$i]); 709f3f0262cSandi } 7106d8affe6SAndreas Gohr $ip = array_values(array_unique($ip)); 7116d8affe6SAndreas Gohr if(!$ip[0]) $ip[0] = '0.0.0.0'; // for some strange reason we don't have a IP 7126d8affe6SAndreas Gohr 7136d8affe6SAndreas Gohr if(!$single) return join(',', $ip); 7146d8affe6SAndreas Gohr 7156d8affe6SAndreas Gohr // decide which IP to use, trying to avoid local addresses 7166d8affe6SAndreas Gohr $ip = array_reverse($ip); 7176d8affe6SAndreas Gohr foreach($ip as $i) { 7182343a762SAndreas Gohr if(preg_match('/^(::1|[fF][eE]80:|127\.|10\.|192\.168\.|172\.((1[6-9])|(2[0-9])|(3[0-1]))\.)/', $i)) { 7196d8affe6SAndreas Gohr continue; 7206d8affe6SAndreas Gohr } else { 7216d8affe6SAndreas Gohr return $i; 7226d8affe6SAndreas Gohr } 7236d8affe6SAndreas Gohr } 7246d8affe6SAndreas Gohr // still here? just use the first (last) address 7256d8affe6SAndreas Gohr return $ip[0]; 726f3f0262cSandi} 727f3f0262cSandi 728f3f0262cSandi/** 7291c548ebeSAndreas Gohr * Check if the browser is on a mobile device 7301c548ebeSAndreas Gohr * 7311c548ebeSAndreas Gohr * Adapted from the example code at url below 7321c548ebeSAndreas Gohr * 7331c548ebeSAndreas Gohr * @link http://www.brainhandles.com/2007/10/15/detecting-mobile-browsers/#code 7341c548ebeSAndreas Gohr */ 7351c548ebeSAndreas Gohrfunction clientismobile() { 736585bf44eSChristopher Smith /* @var Input $INPUT */ 737585bf44eSChristopher Smith global $INPUT; 7381c548ebeSAndreas Gohr 739585bf44eSChristopher Smith if($INPUT->server->has('HTTP_X_WAP_PROFILE')) return true; 7401c548ebeSAndreas Gohr 741585bf44eSChristopher Smith if(preg_match('/wap\.|\.wap/i', $INPUT->server->str('HTTP_ACCEPT'))) return true; 7421c548ebeSAndreas Gohr 743585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_USER_AGENT')) return false; 7441c548ebeSAndreas Gohr 7451c548ebeSAndreas 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'; 7461c548ebeSAndreas Gohr 747585bf44eSChristopher Smith if(preg_match("/$uamatches/i", $INPUT->server->str('HTTP_USER_AGENT'))) return true; 7481c548ebeSAndreas Gohr 7491c548ebeSAndreas Gohr return false; 7501c548ebeSAndreas Gohr} 7511c548ebeSAndreas Gohr 7521c548ebeSAndreas Gohr/** 75363211f61SGlen Harris * Convert one or more comma separated IPs to hostnames 75463211f61SGlen Harris * 75522ef1e32SAndreas Gohr * If $conf['dnslookups'] is disabled it simply returns the input string 75622ef1e32SAndreas Gohr * 75763211f61SGlen Harris * @author Glen Harris <astfgl@iamnota.org> 7583272d797SAndreas Gohr * @param string $ips comma separated list of IP addresses 7593272d797SAndreas Gohr * @return string a comma separated list of hostnames 76063211f61SGlen Harris */ 76163211f61SGlen Harrisfunction gethostsbyaddrs($ips) { 76222ef1e32SAndreas Gohr global $conf; 76322ef1e32SAndreas Gohr if(!$conf['dnslookups']) return $ips; 76422ef1e32SAndreas Gohr 76563211f61SGlen Harris $hosts = array(); 76663211f61SGlen Harris $ips = explode(',', $ips); 767551a720fSMichael Klier 768551a720fSMichael Klier if(is_array($ips)) { 7693886270dSAndreas Gohr foreach($ips as $ip) { 770551a720fSMichael Klier $hosts[] = gethostbyaddr(trim($ip)); 77163211f61SGlen Harris } 772551a720fSMichael Klier return join(',', $hosts); 773551a720fSMichael Klier } else { 774551a720fSMichael Klier return gethostbyaddr(trim($ips)); 775551a720fSMichael Klier } 77663211f61SGlen Harris} 77763211f61SGlen Harris 77863211f61SGlen Harris/** 77915fae107Sandi * Checks if a given page is currently locked. 78015fae107Sandi * 781f3f0262cSandi * removes stale lockfiles 78215fae107Sandi * 78315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 784f3f0262cSandi */ 785f3f0262cSandifunction checklock($id) { 786f3f0262cSandi global $conf; 787585bf44eSChristopher Smith /* @var Input $INPUT */ 788585bf44eSChristopher Smith global $INPUT; 789585bf44eSChristopher Smith 790c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 791f3f0262cSandi 792f3f0262cSandi //no lockfile 793f3f0262cSandi if(!@file_exists($lock)) return false; 794f3f0262cSandi 795f3f0262cSandi //lockfile expired 796f3f0262cSandi if((time() - filemtime($lock)) > $conf['locktime']) { 797d8186216SBen Coburn @unlink($lock); 798f3f0262cSandi return false; 799f3f0262cSandi } 800f3f0262cSandi 801f3f0262cSandi //my own lock 8026d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 803585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 804f3f0262cSandi return false; 805f3f0262cSandi } 806f3f0262cSandi 807f3f0262cSandi return $ip; 808f3f0262cSandi} 809f3f0262cSandi 810f3f0262cSandi/** 81115fae107Sandi * Lock a page for editing 81215fae107Sandi * 81315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 814f3f0262cSandi */ 815f3f0262cSandifunction lock($id) { 816544ed901SDaniel Calviño Sánchez global $conf; 817585bf44eSChristopher Smith /* @var Input $INPUT */ 818585bf44eSChristopher Smith global $INPUT; 819544ed901SDaniel Calviño Sánchez 820544ed901SDaniel Calviño Sánchez if($conf['locktime'] == 0) { 821544ed901SDaniel Calviño Sánchez return; 822544ed901SDaniel Calviño Sánchez } 823544ed901SDaniel Calviño Sánchez 824c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 825585bf44eSChristopher Smith if($INPUT->server->str('REMOTE_USER')) { 826585bf44eSChristopher Smith io_saveFile($lock, $INPUT->server->str('REMOTE_USER')); 827f3f0262cSandi } else { 82885fef7e2SAndreas Gohr io_saveFile($lock, clientIP()."\n".session_id()); 829f3f0262cSandi } 830f3f0262cSandi} 831f3f0262cSandi 832f3f0262cSandi/** 83315fae107Sandi * Unlock a page if it was locked by the user 834f3f0262cSandi * 83515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 8363272d797SAndreas Gohr * @param string $id page id to unlock 83715fae107Sandi * @return bool true if a lock was removed 838f3f0262cSandi */ 839f3f0262cSandifunction unlock($id) { 840585bf44eSChristopher Smith /* @var Input $INPUT */ 841585bf44eSChristopher Smith global $INPUT; 842585bf44eSChristopher Smith 843c9b4bd1eSBen Coburn $lock = wikiLockFN($id); 844f3f0262cSandi if(@file_exists($lock)) { 8456d2af55dSChristopher Smith @list($ip, $session) = explode("\n", io_readFile($lock)); 846585bf44eSChristopher Smith if($ip == $INPUT->server->str('REMOTE_USER') || $ip == clientIP() || $session == session_id()) { 847f3f0262cSandi @unlink($lock); 848f3f0262cSandi return true; 849f3f0262cSandi } 850f3f0262cSandi } 851f3f0262cSandi return false; 852f3f0262cSandi} 853f3f0262cSandi 854f3f0262cSandi/** 855f3f0262cSandi * convert line ending to unix format 856f3f0262cSandi * 8576db7468bSAndreas Gohr * also makes sure the given text is valid UTF-8 8586db7468bSAndreas Gohr * 85915fae107Sandi * @see formText() for 2crlf conversion 86015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 861f3f0262cSandi */ 862f3f0262cSandifunction cleanText($text) { 863f3f0262cSandi $text = preg_replace("/(\015\012)|(\015)/", "\012", $text); 8646db7468bSAndreas Gohr 8656db7468bSAndreas Gohr // if the text is not valid UTF-8 we simply assume latin1 8666db7468bSAndreas Gohr // this won't break any worse than it breaks with the wrong encoding 8676db7468bSAndreas Gohr // but might actually fix the problem in many cases 8686db7468bSAndreas Gohr if(!utf8_check($text)) $text = utf8_encode($text); 8696db7468bSAndreas Gohr 870f3f0262cSandi return $text; 871f3f0262cSandi} 872f3f0262cSandi 873f3f0262cSandi/** 874f3f0262cSandi * Prepares text for print in Webforms by encoding special chars. 875f3f0262cSandi * It also converts line endings to Windows format which is 876f3f0262cSandi * pseudo standard for webforms. 877f3f0262cSandi * 87815fae107Sandi * @see cleanText() for 2unix conversion 87915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 880f3f0262cSandi */ 881f3f0262cSandifunction formText($text) { 8825b7d45a5SAndreas Gohr $text = str_replace("\012", "\015\012", $text); 883f3f0262cSandi return htmlspecialchars($text); 884f3f0262cSandi} 885f3f0262cSandi 886f3f0262cSandi/** 88715fae107Sandi * Returns the specified local text in raw format 88815fae107Sandi * 88915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 890f3f0262cSandi */ 8912adaf2b8SAndreas Gohrfunction rawLocale($id, $ext = 'txt') { 8922adaf2b8SAndreas Gohr return io_readFile(localeFN($id, $ext)); 893f3f0262cSandi} 894f3f0262cSandi 895f3f0262cSandi/** 896f3f0262cSandi * Returns the raw WikiText 89715fae107Sandi * 89815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 899f3f0262cSandi */ 900f3f0262cSandifunction rawWiki($id, $rev = '') { 901cc7d0c94SBen Coburn return io_readWikiPage(wikiFN($id, $rev), $id, $rev); 902f3f0262cSandi} 903f3f0262cSandi 904f3f0262cSandi/** 9057146cee2SAndreas Gohr * Returns the pagetemplate contents for the ID's namespace 9067146cee2SAndreas Gohr * 9077b84afa2SAndreas Gohr * @triggers COMMON_PAGETPL_LOAD 9087146cee2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 9097146cee2SAndreas Gohr */ 910fe17917eSAdrian Langfunction pageTemplate($id) { 911a15ce62dSEsther Brunner global $conf; 912e29549feSAndreas Gohr 913fe17917eSAdrian Lang if(is_array($id)) $id = $id[0]; 914e29549feSAndreas Gohr 9157b84afa2SAndreas Gohr // prepare initial event data 9167b84afa2SAndreas Gohr $data = array( 9177b84afa2SAndreas Gohr 'id' => $id, // the id of the page to be created 9187b84afa2SAndreas Gohr 'tpl' => '', // the text used as template 9197b84afa2SAndreas Gohr 'tplfile' => '', // the file above text was/should be loaded from 9207b84afa2SAndreas Gohr 'doreplace' => true // should wildcard replacements be done on the text? 9217b84afa2SAndreas Gohr ); 9227b84afa2SAndreas Gohr 9237b84afa2SAndreas Gohr $evt = new Doku_Event('COMMON_PAGETPL_LOAD', $data); 9247b84afa2SAndreas Gohr if($evt->advise_before(true)) { 9257b84afa2SAndreas Gohr // the before event might have loaded the content already 9267b84afa2SAndreas Gohr if(empty($data['tpl'])) { 9277b84afa2SAndreas Gohr // if the before event did not set a template file, try to find one 9287b84afa2SAndreas Gohr if(empty($data['tplfile'])) { 929fe17917eSAdrian Lang $path = dirname(wikiFN($id)); 930e29549feSAndreas Gohr if(@file_exists($path.'/_template.txt')) { 9317b84afa2SAndreas Gohr $data['tplfile'] = $path.'/_template.txt'; 932e29549feSAndreas Gohr } else { 933e29549feSAndreas Gohr // search upper namespaces for templates 934e29549feSAndreas Gohr $len = strlen(rtrim($conf['datadir'], '/')); 935e29549feSAndreas Gohr while(strlen($path) >= $len) { 936e29549feSAndreas Gohr if(@file_exists($path.'/__template.txt')) { 9377b84afa2SAndreas Gohr $data['tplfile'] = $path.'/__template.txt'; 938e29549feSAndreas Gohr break; 939e29549feSAndreas Gohr } 940e29549feSAndreas Gohr $path = substr($path, 0, strrpos($path, '/')); 941e29549feSAndreas Gohr } 942e29549feSAndreas Gohr } 9437b84afa2SAndreas Gohr } 9447b84afa2SAndreas Gohr // load the content 9453d7ac595SMichael Hamann $data['tpl'] = io_readFile($data['tplfile']); 9467b84afa2SAndreas Gohr } 947a1bbd05bSMichael Hamann if($data['doreplace']) parsePageTemplate($data); 9487b84afa2SAndreas Gohr } 9497b84afa2SAndreas Gohr $evt->advise_after(); 9507b84afa2SAndreas Gohr unset($evt); 9517b84afa2SAndreas Gohr 952fe17917eSAdrian Lang return $data['tpl']; 9532b1223ecSAdrian Lang} 9542b1223ecSAdrian Lang 9552b1223ecSAdrian Lang/** 9562b1223ecSAdrian Lang * Performs common page template replacements 9577b84afa2SAndreas Gohr * This works on data from COMMON_PAGETPL_LOAD 9582b1223ecSAdrian Lang * 9592b1223ecSAdrian Lang * @author Andreas Gohr <andi@splitbrain.org> 9602b1223ecSAdrian Lang */ 961d535a2e9Sstretchyboyfunction parsePageTemplate(&$data) { 9623272d797SAndreas Gohr /** 9633272d797SAndreas Gohr * @var string $id the id of the page to be created 9643272d797SAndreas Gohr * @var string $tpl the text used as template 9653272d797SAndreas Gohr * @var string $tplfile the file above text was/should be loaded from 9663272d797SAndreas Gohr * @var bool $doreplace should wildcard replacements be done on the text? 9673272d797SAndreas Gohr */ 968fe17917eSAdrian Lang extract($data); 969fe17917eSAdrian Lang 970b856f7dfSAdrian Lang global $USERINFO; 971bce53b1fSAdrian Lang global $conf; 972585bf44eSChristopher Smith /* @var Input $INPUT */ 973585bf44eSChristopher Smith global $INPUT; 974e29549feSAndreas Gohr 975e29549feSAndreas Gohr // replace placeholders 97626ece5a7SAndreas Gohr $file = noNS($id); 97737c1acbdSAdrian Lang $page = strtr($file, $conf['sepchar'], ' '); 97826ece5a7SAndreas Gohr 9793272d797SAndreas Gohr $tpl = str_replace( 9803272d797SAndreas Gohr array( 98126ece5a7SAndreas Gohr '@ID@', 98226ece5a7SAndreas Gohr '@NS@', 98326ece5a7SAndreas Gohr '@FILE@', 98426ece5a7SAndreas Gohr '@!FILE@', 98526ece5a7SAndreas Gohr '@!FILE!@', 98626ece5a7SAndreas Gohr '@PAGE@', 98726ece5a7SAndreas Gohr '@!PAGE@', 98826ece5a7SAndreas Gohr '@!!PAGE@', 98926ece5a7SAndreas Gohr '@!PAGE!@', 99026ece5a7SAndreas Gohr '@USER@', 99126ece5a7SAndreas Gohr '@NAME@', 99226ece5a7SAndreas Gohr '@MAIL@', 99326ece5a7SAndreas Gohr '@DATE@', 99426ece5a7SAndreas Gohr ), 99526ece5a7SAndreas Gohr array( 99626ece5a7SAndreas Gohr $id, 99726ece5a7SAndreas Gohr getNS($id), 99826ece5a7SAndreas Gohr $file, 99926ece5a7SAndreas Gohr utf8_ucfirst($file), 100026ece5a7SAndreas Gohr utf8_strtoupper($file), 100126ece5a7SAndreas Gohr $page, 100226ece5a7SAndreas Gohr utf8_ucfirst($page), 100326ece5a7SAndreas Gohr utf8_ucwords($page), 100426ece5a7SAndreas Gohr utf8_strtoupper($page), 1005585bf44eSChristopher Smith $INPUT->server->str('REMOTE_USER'), 1006b856f7dfSAdrian Lang $USERINFO['name'], 1007b856f7dfSAdrian Lang $USERINFO['mail'], 100826ece5a7SAndreas Gohr $conf['dformat'], 10093272d797SAndreas Gohr ), $tpl 10103272d797SAndreas Gohr ); 101126ece5a7SAndreas Gohr 10127d644fc8SAndreas Gohr // we need the callback to work around strftime's char limit 10137d644fc8SAndreas Gohr $tpl = preg_replace_callback('/%./', create_function('$m', 'return strftime($m[0]);'), $tpl); 1014d535a2e9Sstretchyboy $data['tpl'] = $tpl; 1015a15ce62dSEsther Brunner return $tpl; 10167146cee2SAndreas Gohr} 10177146cee2SAndreas Gohr 10187146cee2SAndreas Gohr/** 101915fae107Sandi * Returns the raw Wiki Text in three slices. 102015fae107Sandi * 102115fae107Sandi * The range parameter needs to have the form "from-to" 102215cfe303Sandi * and gives the range of the section in bytes - no 102315cfe303Sandi * UTF-8 awareness is needed. 1024f3f0262cSandi * The returned order is prefix, section and suffix. 102515fae107Sandi * 102615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1027f3f0262cSandi */ 1028f3f0262cSandifunction rawWikiSlices($range, $id, $rev = '') { 1029cc7d0c94SBen Coburn $text = io_readWikiPage(wikiFN($id, $rev), $id, $rev); 1030f3f0262cSandi 103180fcb268SAdrian Lang // Parse range 103280fcb268SAdrian Lang list($from, $to) = explode('-', $range, 2); 103380fcb268SAdrian Lang // Make range zero-based, use defaults if marker is missing 103480fcb268SAdrian Lang $from = !$from ? 0 : ($from - 1); 103580fcb268SAdrian Lang $to = !$to ? strlen($text) : ($to - 1); 103680fcb268SAdrian Lang 103780fcb268SAdrian Lang $slices[0] = substr($text, 0, $from); 103880fcb268SAdrian Lang $slices[1] = substr($text, $from, $to - $from); 103915cfe303Sandi $slices[2] = substr($text, $to); 1040f3f0262cSandi return $slices; 1041f3f0262cSandi} 1042f3f0262cSandi 1043f3f0262cSandi/** 104415fae107Sandi * Joins wiki text slices 104515fae107Sandi * 104680fcb268SAdrian Lang * function to join the text slices. 1047f3f0262cSandi * When the pretty parameter is set to true it adds additional empty 1048f3f0262cSandi * lines between sections if needed (used on saving). 104915fae107Sandi * 105015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1051f3f0262cSandi */ 1052f3f0262cSandifunction con($pre, $text, $suf, $pretty = false) { 1053f3f0262cSandi if($pretty) { 105480fcb268SAdrian Lang if($pre !== '' && substr($pre, -1) !== "\n" && 10553272d797SAndreas Gohr substr($text, 0, 1) !== "\n" 10563272d797SAndreas Gohr ) { 105780fcb268SAdrian Lang $pre .= "\n"; 105880fcb268SAdrian Lang } 105980fcb268SAdrian Lang if($suf !== '' && substr($text, -1) !== "\n" && 10603272d797SAndreas Gohr substr($suf, 0, 1) !== "\n" 10613272d797SAndreas Gohr ) { 106280fcb268SAdrian Lang $text .= "\n"; 106380fcb268SAdrian Lang } 1064f3f0262cSandi } 1065f3f0262cSandi 1066f3f0262cSandi return $pre.$text.$suf; 1067f3f0262cSandi} 1068f3f0262cSandi 1069f3f0262cSandi/** 1070a701424fSBen Coburn * Saves a wikitext by calling io_writeWikiPage. 1071a701424fSBen Coburn * Also directs changelog and attic updates. 107215fae107Sandi * 107315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 107471726d78SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 1075f3f0262cSandi */ 1076b6912aeaSAndreas Gohrfunction saveWikiText($id, $text, $summary, $minor = false) { 1077a701424fSBen Coburn /* Note to developers: 1078a701424fSBen Coburn This code is subtle and delicate. Test the behavior of 1079a701424fSBen Coburn the attic and changelog with dokuwiki and external edits 1080a701424fSBen Coburn after any changes. External edits change the wiki page 1081a701424fSBen Coburn directly without using php or dokuwiki. 1082a701424fSBen Coburn */ 1083f3f0262cSandi global $conf; 1084f3f0262cSandi global $lang; 108571726d78SBen Coburn global $REV; 1086585bf44eSChristopher Smith /* @var Input $INPUT */ 1087585bf44eSChristopher Smith global $INPUT; 1088585bf44eSChristopher Smith 1089f3f0262cSandi // ignore if no changes were made 1090f3f0262cSandi if($text == rawWiki($id, '')) { 1091f3f0262cSandi return; 1092f3f0262cSandi } 1093f3f0262cSandi 1094f3f0262cSandi $file = wikiFN($id); 1095a701424fSBen Coburn $old = @filemtime($file); // from page 1096407e65b9SAndreas Gohr $wasRemoved = (trim($text) == ''); // check for empty or whitespace only 1097d8186216SBen Coburn $wasCreated = !@file_exists($file); 109871726d78SBen Coburn $wasReverted = ($REV == true); 1099047bad06SGerrit Uitslag $pagelog = new PageChangeLog($id, 1024); 1100e45b34cdSBen Coburn $newRev = false; 1101f523c971SGerrit Uitslag $oldRev = $pagelog->getRevisions(-1, 1); // from changelog 1102a701424fSBen Coburn $oldRev = (int) (empty($oldRev) ? 0 : $oldRev[0]); 1103a701424fSBen Coburn if(!@file_exists(wikiFN($id, $old)) && @file_exists($file) && $old >= $oldRev) { 110446844156SBen Coburn // add old revision to the attic if missing 110546844156SBen Coburn saveOldRevision($id); 110646844156SBen Coburn // add a changelog entry if this edit came from outside dokuwiki 1107a701424fSBen Coburn if($old > $oldRev) { 1108ebf1501fSBen Coburn addLogEntry($old, $id, DOKU_CHANGE_TYPE_EDIT, $lang['external_edit'], '', array('ExternalEdit'=> true)); 110946844156SBen Coburn // remove soon to be stale instructions 111046844156SBen Coburn $cache = new cache_instructions($id, $file); 111146844156SBen Coburn $cache->removeCache(); 111246844156SBen Coburn } 111346844156SBen Coburn } 1114f3f0262cSandi 111571726d78SBen Coburn if($wasRemoved) { 111630725328SGabriel Birke // Send "update" event with empty data, so plugins can react to page deletion 111730725328SGabriel Birke $data = array(array($file, '', false), getNS($id), noNS($id), false); 111830725328SGabriel Birke trigger_event('IO_WIKIPAGE_WRITE', $data); 1119e45b34cdSBen Coburn // pre-save deleted revision 1120e45b34cdSBen Coburn @touch($file); 112146844156SBen Coburn clearstatcache(); 1122e45b34cdSBen Coburn $newRev = saveOldRevision($id); 1123e1f3d9e1SEsther Brunner // remove empty file 1124f3f0262cSandi @unlink($file); 1125c5f92742SMichael Hamann // don't remove old meta info as it should be saved, plugins can use IO_WIKIPAGE_WRITE for removing their metadata... 1126c5f92742SMichael Hamann // purge non-persistant meta data 11273d1f9ec3SMichael Klier p_purge_metadata($id); 1128f3f0262cSandi $del = true; 11293ce054b3Sandi // autoset summary on deletion 11303ce054b3Sandi if(empty($summary)) $summary = $lang['deleted']; 113153d6ccfeSandi // remove empty namespaces 1132cc7d0c94SBen Coburn io_sweepNS($id, 'datadir'); 1133cc7d0c94SBen Coburn io_sweepNS($id, 'mediadir'); 1134f3f0262cSandi } else { 1135cc7d0c94SBen Coburn // save file (namespace dir is created in io_writeWikiPage) 1136cc7d0c94SBen Coburn io_writeWikiPage($file, $text, $id); 113746844156SBen Coburn // pre-save the revision, to keep the attic in sync 113846844156SBen Coburn $newRev = saveOldRevision($id); 1139f3f0262cSandi $del = false; 1140f3f0262cSandi } 1141f3f0262cSandi 114271726d78SBen Coburn // select changelog line type 114371726d78SBen Coburn $extra = ''; 1144ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_EDIT; 114571726d78SBen Coburn if($wasReverted) { 1146ebf1501fSBen Coburn $type = DOKU_CHANGE_TYPE_REVERT; 114771726d78SBen Coburn $extra = $REV; 11483272d797SAndreas Gohr } else if($wasCreated) { 11493272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_CREATE; 11503272d797SAndreas Gohr } else if($wasRemoved) { 11513272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_DELETE; 1152585bf44eSChristopher Smith } else if($minor && $conf['useacl'] && $INPUT->server->str('REMOTE_USER')) { 11533272d797SAndreas Gohr $type = DOKU_CHANGE_TYPE_MINOR_EDIT; 11543272d797SAndreas Gohr } //minor edits only for logged in users 115571726d78SBen Coburn 1156e45b34cdSBen Coburn addLogEntry($newRev, $id, $type, $summary, $extra); 115726a0801fSAndreas Gohr // send notify mails 115890033e9dSAndreas Gohr notify($id, 'admin', $old, $summary, $minor); 115990033e9dSAndreas Gohr notify($id, 'subscribers', $old, $summary, $minor); 1160f3f0262cSandi 1161ce6b63d9Schris // update the purgefile (timestamp of the last time anything within the wiki was changed) 116298407a7aSandi io_saveFile($conf['cachedir'].'/purgefile', time()); 11632eccbdaaSGina Haeussge 11642eccbdaaSGina Haeussge // if useheading is enabled, purge the cache of all linking pages 1165fe9ec250SChris Smith if(useHeading('content')) { 116607ff0babSMichael Hamann $pages = ft_backlinks($id, true); 11672eccbdaaSGina Haeussge foreach($pages as $page) { 11682eccbdaaSGina Haeussge $cache = new cache_renderer($page, wikiFN($page), 'xhtml'); 11692eccbdaaSGina Haeussge $cache->removeCache(); 11702eccbdaaSGina Haeussge } 11712eccbdaaSGina Haeussge } 1172f3f0262cSandi} 1173f3f0262cSandi 1174f3f0262cSandi/** 1175f3f0262cSandi * moves the current version to the attic and returns its 1176f3f0262cSandi * revision date 117715fae107Sandi * 117815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1179f3f0262cSandi */ 1180f3f0262cSandifunction saveOldRevision($id) { 1181f3f0262cSandi $oldf = wikiFN($id); 1182f3f0262cSandi if(!@file_exists($oldf)) return ''; 1183f3f0262cSandi $date = filemtime($oldf); 1184f3f0262cSandi $newf = wikiFN($id, $date); 1185cc7d0c94SBen Coburn io_writeWikiPage($newf, rawWiki($id), $id, $date); 1186f3f0262cSandi return $date; 1187f3f0262cSandi} 1188f3f0262cSandi 1189f3f0262cSandi/** 1190fde10de4SAdrian Lang * Sends a notify mail on page change or registration 119126a0801fSAndreas Gohr * 119226a0801fSAndreas Gohr * @param string $id The changed page 1193fde10de4SAdrian Lang * @param string $who Who to notify (admin|subscribers|register) 11943272d797SAndreas Gohr * @param int|string $rev Old page revision 119526a0801fSAndreas Gohr * @param string $summary What changed 119690033e9dSAndreas Gohr * @param boolean $minor Is this a minor edit? 119702a498e7Schris * @param array $replace Additional string substitutions, @KEY@ to be replaced by value 119815fae107Sandi * 11993272d797SAndreas Gohr * @return bool 120015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1201f3f0262cSandi */ 120202a498e7Schrisfunction notify($id, $who, $rev = '', $summary = '', $minor = false, $replace = array()) { 1203f3f0262cSandi global $conf; 1204585bf44eSChristopher Smith /* @var Input $INPUT */ 1205585bf44eSChristopher Smith global $INPUT; 1206b158d625SSteven Danz 12076df843eeSAndreas Gohr // decide if there is something to do, eg. whom to mail 120826a0801fSAndreas Gohr if($who == 'admin') { 12093272d797SAndreas Gohr if(empty($conf['notify'])) return false; //notify enabled? 12102ed38036SAndreas Gohr $tpl = 'mailtext'; 121126a0801fSAndreas Gohr $to = $conf['notify']; 121226a0801fSAndreas Gohr } elseif($who == 'subscribers') { 121384c1127cSAndreas Gohr if(!actionOK('subscribe')) return false; //subscribers enabled? 1214585bf44eSChristopher Smith if($conf['useacl'] && $INPUT->server->str('REMOTE_USER') && $minor) return false; //skip minors 12158881fcc9SAdrian Lang $data = array('id' => $id, 'addresslist' => '', 'self' => false); 12163272d797SAndreas Gohr trigger_event( 12173272d797SAndreas Gohr 'COMMON_NOTIFY_ADDRESSLIST', $data, 1218835242b0SAndreas Gohr array(new Subscription(), 'notifyaddresses') 12193272d797SAndreas Gohr ); 12202ed38036SAndreas Gohr $to = $data['addresslist']; 12212ed38036SAndreas Gohr if(empty($to)) return false; 12222ed38036SAndreas Gohr $tpl = 'subscr_single'; 122326a0801fSAndreas Gohr } else { 12243272d797SAndreas Gohr return false; //just to be safe 122526a0801fSAndreas Gohr } 122626a0801fSAndreas Gohr 12276df843eeSAndreas Gohr // prepare content 12282ed38036SAndreas Gohr $subscription = new Subscription(); 12292ed38036SAndreas Gohr return $subscription->send_diff($to, $tpl, $id, $rev, $summary); 1230f3f0262cSandi} 12312ed38036SAndreas Gohr 123215fae107Sandi/** 123371f7bde7SAndreas Gohr * extracts the query from a search engine referrer 123415fae107Sandi * 123515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 123671f7bde7SAndreas Gohr * @author Todd Augsburger <todd@rollerorgans.com> 1237f3f0262cSandi */ 1238f3f0262cSandifunction getGoogleQuery() { 1239585bf44eSChristopher Smith /* @var Input $INPUT */ 1240585bf44eSChristopher Smith global $INPUT; 1241585bf44eSChristopher Smith 1242585bf44eSChristopher Smith if(!$INPUT->server->has('HTTP_REFERER')) { 1243c66972f2SAdrian Lang return ''; 1244c66972f2SAdrian Lang } 1245585bf44eSChristopher Smith $url = parse_url($INPUT->server->str('HTTP_REFERER')); 1246f3f0262cSandi 1247079b3ac1SAndreas Gohr // only handle common SEs 1248079b3ac1SAndreas Gohr if(!preg_match('/(google|bing|yahoo|ask|duckduckgo|babylon|aol|yandex)/',$url['host'])) return ''; 1249e4d8a516SKazutaka Miyasaka 1250079b3ac1SAndreas Gohr $query = array(); 1251e4d8a516SKazutaka Miyasaka // temporary workaround against PHP bug #49733 1252e4d8a516SKazutaka Miyasaka // see http://bugs.php.net/bug.php?id=49733 1253e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) $enc = mb_internal_encoding(); 1254f3f0262cSandi parse_str($url['query'], $query); 1255e4d8a516SKazutaka Miyasaka if(UTF8_MBSTRING) mb_internal_encoding($enc); 1256e4d8a516SKazutaka Miyasaka 1257c66972f2SAdrian Lang $q = ''; 1258079b3ac1SAndreas Gohr if(isset($query['q'])){ 1259079b3ac1SAndreas Gohr $q = $query['q']; 1260079b3ac1SAndreas Gohr }elseif(isset($query['p'])){ 1261079b3ac1SAndreas Gohr $q = $query['p']; 1262079b3ac1SAndreas Gohr }elseif(isset($query['query'])){ 1263079b3ac1SAndreas Gohr $q = $query['query']; 1264079b3ac1SAndreas Gohr } 1265079b3ac1SAndreas Gohr $q = trim($q); 1266f3f0262cSandi 1267079b3ac1SAndreas Gohr if(!$q) return ''; 12686531ab03SAndreas Gohr $q = preg_split('/[\s\'"\\\\`()\]\[?:!\.{};,#+*<>\\/]+/', $q, -1, PREG_SPLIT_NO_EMPTY); 1269f93b3b50SAndreas Gohr return $q; 1270f3f0262cSandi} 1271f3f0262cSandi 1272f3f0262cSandi/** 1273f3f0262cSandi * Return the human readable size of a file 1274f3f0262cSandi * 1275f3f0262cSandi * @param int $size A file size 1276f3f0262cSandi * @param int $dec A number of decimal places 127774160ca1SGerrit Uitslag * @return string human readable size 1278f3f0262cSandi * @author Martin Benjamin <b.martin@cybernet.ch> 1279f3f0262cSandi * @author Aidan Lister <aidan@php.net> 1280f3f0262cSandi * @version 1.0.0 1281f3f0262cSandi */ 1282f31d5b73Sandifunction filesize_h($size, $dec = 1) { 1283f3f0262cSandi $sizes = array('B', 'KB', 'MB', 'GB'); 1284f3f0262cSandi $count = count($sizes); 1285f3f0262cSandi $i = 0; 1286f3f0262cSandi 1287f3f0262cSandi while($size >= 1024 && ($i < $count - 1)) { 1288f3f0262cSandi $size /= 1024; 1289f3f0262cSandi $i++; 1290f3f0262cSandi } 1291f3f0262cSandi 1292f3f0262cSandi return round($size, $dec).' '.$sizes[$i]; 1293f3f0262cSandi} 1294f3f0262cSandi 129515fae107Sandi/** 1296c57e365eSAndreas Gohr * Return the given timestamp as human readable, fuzzy age 1297c57e365eSAndreas Gohr * 1298c57e365eSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1299c57e365eSAndreas Gohr */ 1300c57e365eSAndreas Gohrfunction datetime_h($dt) { 1301c57e365eSAndreas Gohr global $lang; 1302c57e365eSAndreas Gohr 1303c57e365eSAndreas Gohr $ago = time() - $dt; 1304c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 12 * 2) { 1305c57e365eSAndreas Gohr return sprintf($lang['years'], round($ago / (24 * 60 * 60 * 30 * 12))); 1306c57e365eSAndreas Gohr } 1307c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 30 * 2) { 1308c57e365eSAndreas Gohr return sprintf($lang['months'], round($ago / (24 * 60 * 60 * 30))); 1309c57e365eSAndreas Gohr } 1310c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 7 * 2) { 1311c57e365eSAndreas Gohr return sprintf($lang['weeks'], round($ago / (24 * 60 * 60 * 7))); 1312c57e365eSAndreas Gohr } 1313c57e365eSAndreas Gohr if($ago > 24 * 60 * 60 * 2) { 1314c57e365eSAndreas Gohr return sprintf($lang['days'], round($ago / (24 * 60 * 60))); 1315c57e365eSAndreas Gohr } 1316c57e365eSAndreas Gohr if($ago > 60 * 60 * 2) { 1317c57e365eSAndreas Gohr return sprintf($lang['hours'], round($ago / (60 * 60))); 1318c57e365eSAndreas Gohr } 1319c57e365eSAndreas Gohr if($ago > 60 * 2) { 1320c57e365eSAndreas Gohr return sprintf($lang['minutes'], round($ago / (60))); 1321c57e365eSAndreas Gohr } 1322c57e365eSAndreas Gohr return sprintf($lang['seconds'], $ago); 1323c57e365eSAndreas Gohr} 1324c57e365eSAndreas Gohr 1325c57e365eSAndreas Gohr/** 1326f2263577SAndreas Gohr * Wraps around strftime but provides support for fuzzy dates 1327f2263577SAndreas Gohr * 1328f2263577SAndreas Gohr * The format default to $conf['dformat']. It is passed to 1329f2263577SAndreas Gohr * strftime - %f can be used to get the value from datetime_h() 1330f2263577SAndreas Gohr * 1331f2263577SAndreas Gohr * @see datetime_h 1332f2263577SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1333f2263577SAndreas Gohr */ 1334f2263577SAndreas Gohrfunction dformat($dt = null, $format = '') { 1335f2263577SAndreas Gohr global $conf; 1336f2263577SAndreas Gohr 1337f2263577SAndreas Gohr if(is_null($dt)) $dt = time(); 1338f2263577SAndreas Gohr $dt = (int) $dt; 1339f2263577SAndreas Gohr if(!$format) $format = $conf['dformat']; 1340f2263577SAndreas Gohr 1341f2263577SAndreas Gohr $format = str_replace('%f', datetime_h($dt), $format); 1342f2263577SAndreas Gohr return strftime($format, $dt); 1343f2263577SAndreas Gohr} 1344f2263577SAndreas Gohr 1345f2263577SAndreas Gohr/** 1346c4f79b71SMichael Hamann * Formats a timestamp as ISO 8601 date 1347c4f79b71SMichael Hamann * 1348c4f79b71SMichael Hamann * @author <ungu at terong dot com> 1349c4f79b71SMichael Hamann * @link http://www.php.net/manual/en/function.date.php#54072 135063703ba5SAndreas Gohr * @param int $int_date: current date in UNIX timestamp 13513272d797SAndreas Gohr * @return string 1352c4f79b71SMichael Hamann */ 1353c4f79b71SMichael Hamannfunction date_iso8601($int_date) { 1354c4f79b71SMichael Hamann $date_mod = date('Y-m-d\TH:i:s', $int_date); 1355c4f79b71SMichael Hamann $pre_timezone = date('O', $int_date); 1356c4f79b71SMichael Hamann $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 1357c4f79b71SMichael Hamann $date_mod .= $time_zone; 1358c4f79b71SMichael Hamann return $date_mod; 1359c4f79b71SMichael Hamann} 1360c4f79b71SMichael Hamann 1361c4f79b71SMichael Hamann/** 136200a7b5adSEsther Brunner * return an obfuscated email address in line with $conf['mailguard'] setting 136300a7b5adSEsther Brunner * 136400a7b5adSEsther Brunner * @author Harry Fuecks <hfuecks@gmail.com> 136500a7b5adSEsther Brunner * @author Christopher Smith <chris@jalakai.co.uk> 136600a7b5adSEsther Brunner */ 136700a7b5adSEsther Brunnerfunction obfuscate($email) { 136800a7b5adSEsther Brunner global $conf; 136900a7b5adSEsther Brunner 137000a7b5adSEsther Brunner switch($conf['mailguard']) { 137100a7b5adSEsther Brunner case 'visible' : 137200a7b5adSEsther Brunner $obfuscate = array('@' => ' [at] ', '.' => ' [dot] ', '-' => ' [dash] '); 137300a7b5adSEsther Brunner return strtr($email, $obfuscate); 137400a7b5adSEsther Brunner 137500a7b5adSEsther Brunner case 'hex' : 137600a7b5adSEsther Brunner $encode = ''; 137749eb6e38SAndreas Gohr $len = strlen($email); 137849eb6e38SAndreas Gohr for($x = 0; $x < $len; $x++) { 137949eb6e38SAndreas Gohr $encode .= '&#x'.bin2hex($email{$x}).';'; 138049eb6e38SAndreas Gohr } 138100a7b5adSEsther Brunner return $encode; 138200a7b5adSEsther Brunner 138300a7b5adSEsther Brunner case 'none' : 138400a7b5adSEsther Brunner default : 138500a7b5adSEsther Brunner return $email; 138600a7b5adSEsther Brunner } 138700a7b5adSEsther Brunner} 138800a7b5adSEsther Brunner 138900a7b5adSEsther Brunner/** 139089541d4bSAndreas Gohr * Removes quoting backslashes 139189541d4bSAndreas Gohr * 139289541d4bSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 139389541d4bSAndreas Gohr */ 139489541d4bSAndreas Gohrfunction unslash($string, $char = "'") { 139589541d4bSAndreas Gohr return str_replace('\\'.$char, $char, $string); 139689541d4bSAndreas Gohr} 139789541d4bSAndreas Gohr 139873038c47SAndreas Gohr/** 139973038c47SAndreas Gohr * Convert php.ini shorthands to byte 140073038c47SAndreas Gohr * 140173038c47SAndreas Gohr * @author <gilthans dot NO dot SPAM at gmail dot com> 140273038c47SAndreas Gohr * @link http://de3.php.net/manual/en/ini.core.php#79564 140373038c47SAndreas Gohr */ 140473038c47SAndreas Gohrfunction php_to_byte($v) { 140573038c47SAndreas Gohr $l = substr($v, -1); 140673038c47SAndreas Gohr $ret = substr($v, 0, -1); 140773038c47SAndreas Gohr switch(strtoupper($l)) { 140874160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 140973038c47SAndreas Gohr case 'P': 141073038c47SAndreas Gohr $ret *= 1024; 141174160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 141273038c47SAndreas Gohr case 'T': 141373038c47SAndreas Gohr $ret *= 1024; 141474160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 141573038c47SAndreas Gohr case 'G': 141673038c47SAndreas Gohr $ret *= 1024; 141774160ca1SGerrit Uitslag /** @noinspection PhpMissingBreakStatementInspection */ 141873038c47SAndreas Gohr case 'M': 141973038c47SAndreas Gohr $ret *= 1024; 142073038c47SAndreas Gohr case 'K': 142173038c47SAndreas Gohr $ret *= 1024; 142273038c47SAndreas Gohr break; 142349cbd23eSOtto Vainio default; 142449cbd23eSOtto Vainio $ret *= 10; 142549cbd23eSOtto Vainio break; 142673038c47SAndreas Gohr } 142773038c47SAndreas Gohr return $ret; 142873038c47SAndreas Gohr} 142973038c47SAndreas Gohr 1430546d3a99SAndreas Gohr/** 1431546d3a99SAndreas Gohr * Wrapper around preg_quote adding the default delimiter 1432546d3a99SAndreas Gohr */ 1433546d3a99SAndreas Gohrfunction preg_quote_cb($string) { 1434546d3a99SAndreas Gohr return preg_quote($string, '/'); 1435546d3a99SAndreas Gohr} 143673038c47SAndreas Gohr 1437bd2f6c2fSAndreas Gohr/** 1438bd2f6c2fSAndreas Gohr * Shorten a given string by removing data from the middle 1439bd2f6c2fSAndreas Gohr * 1440c66972f2SAdrian Lang * You can give the string in two parts, the first part $keep 1441bd2f6c2fSAndreas Gohr * will never be shortened. The second part $short will be cut 1442bd2f6c2fSAndreas Gohr * in the middle to shorten but only if at least $min chars are 1443bd2f6c2fSAndreas Gohr * left to display it. Otherwise it will be left off. 1444bd2f6c2fSAndreas Gohr * 1445bd2f6c2fSAndreas Gohr * @param string $keep the part to keep 1446bd2f6c2fSAndreas Gohr * @param string $short the part to shorten 1447bd2f6c2fSAndreas Gohr * @param int $max maximum chars you want for the whole string 1448bd2f6c2fSAndreas Gohr * @param int $min minimum number of chars to have left for middle shortening 1449bd2f6c2fSAndreas Gohr * @param string $char the shortening character to use 14503272d797SAndreas Gohr * @return string 1451bd2f6c2fSAndreas Gohr */ 1452a5d27328SAndreas Gohrfunction shorten($keep, $short, $max, $min = 9, $char = '…') { 1453bd2f6c2fSAndreas Gohr $max = $max - utf8_strlen($keep); 1454bd2f6c2fSAndreas Gohr if($max < $min) return $keep; 1455bd2f6c2fSAndreas Gohr $len = utf8_strlen($short); 1456bd2f6c2fSAndreas Gohr if($len <= $max) return $keep.$short; 1457bd2f6c2fSAndreas Gohr $half = floor($max / 2); 1458bd2f6c2fSAndreas Gohr return $keep.utf8_substr($short, 0, $half - 1).$char.utf8_substr($short, $len - $half); 1459bd2f6c2fSAndreas Gohr} 1460bd2f6c2fSAndreas Gohr 1461dc58b6f4SAndy Webber/** 1462dc58b6f4SAndy Webber * Return the users realname or e-mail address for use 1463dc58b6f4SAndy Webber * in page footer and recent changes pages 1464dc58b6f4SAndy Webber * 146515f3bc49SGerrit Uitslag * @param string|bool $username or false when currently logged-in user should be used 146615f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1467c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 146815f3bc49SGerrit Uitslag * 1469dc58b6f4SAndy Webber * @author Andy Webber <dokuwiki AT andywebber DOT com> 1470dc58b6f4SAndy Webber */ 147115f3bc49SGerrit Uitslagfunction editorinfo($username, $textonly = false) { 1472cd4635eeSGerrit Uitslag return userlink($username, $textonly); 1473dc58b6f4SAndy Webber} 1474dc58b6f4SAndy Webber 147560a396c8SGerrit Uitslag/** 147660a396c8SGerrit Uitslag * Returns users realname w/o link 147760a396c8SGerrit Uitslag * 147860a396c8SGerrit Uitslag * @param string|bool $username or false when currently logged-in user should be used 147915f3bc49SGerrit Uitslag * @param bool $textonly true returns only plain text, true allows returning html 1480c0953023SGerrit Uitslag * @return string html or plain text(not escaped) of formatted user name 148160a396c8SGerrit Uitslag * 148260a396c8SGerrit Uitslag * @triggers COMMON_USER_LINK 148360a396c8SGerrit Uitslag */ 1484cd4635eeSGerrit Uitslagfunction userlink($username = null, $textonly = false) { 148560a396c8SGerrit Uitslag global $conf, $INFO; 148660a396c8SGerrit Uitslag /** @var DokuWiki_Auth_Plugin $auth */ 148760a396c8SGerrit Uitslag global $auth; 148830f6ec4bSGerrit Uitslag /** @var Input $INPUT */ 148930f6ec4bSGerrit Uitslag global $INPUT; 149060a396c8SGerrit Uitslag 149160a396c8SGerrit Uitslag // prepare initial event data 149260a396c8SGerrit Uitslag $data = array( 149360a396c8SGerrit Uitslag 'username' => $username, // the unique user name 149460a396c8SGerrit Uitslag 'name' => '', 149560a396c8SGerrit Uitslag 'link' => array( //setting 'link' to false disables linking 149660a396c8SGerrit Uitslag 'target' => '', 149760a396c8SGerrit Uitslag 'pre' => '', 149860a396c8SGerrit Uitslag 'suf' => '', 149960a396c8SGerrit Uitslag 'style' => '', 150060a396c8SGerrit Uitslag 'more' => '', 150160a396c8SGerrit Uitslag 'url' => '', 150260a396c8SGerrit Uitslag 'title' => '', 150360a396c8SGerrit Uitslag 'class' => '' 150460a396c8SGerrit Uitslag ), 15054d5fc927SGerrit Uitslag 'userlink' => '', // formatted user name as will be returned 150615f3bc49SGerrit Uitslag 'textonly' => $textonly 150760a396c8SGerrit Uitslag ); 150862c8004eSGerrit Uitslag if($username === null) { 150930f6ec4bSGerrit Uitslag $data['username'] = $username = $INPUT->server->str('REMOTE_USER'); 151015f3bc49SGerrit Uitslag if($textonly){ 151115f3bc49SGerrit Uitslag $data['name'] = $INFO['userinfo']['name']. ' (' . $INPUT->server->str('REMOTE_USER') . ')'; 151215f3bc49SGerrit Uitslag }else { 151330f6ec4bSGerrit Uitslag $data['name'] = '<bdi>' . hsc($INFO['userinfo']['name']) . '</bdi> (<bdi>' . hsc($INPUT->server->str('REMOTE_USER')) . '</bdi>)'; 151460a396c8SGerrit Uitslag } 151515f3bc49SGerrit Uitslag } 151660a396c8SGerrit Uitslag 151760a396c8SGerrit Uitslag $evt = new Doku_Event('COMMON_USER_LINK', $data); 151860a396c8SGerrit Uitslag if($evt->advise_before(true)) { 151960a396c8SGerrit Uitslag if(empty($data['name'])) { 152060a396c8SGerrit Uitslag if($conf['showuseras'] == 'loginname') { 152115f3bc49SGerrit Uitslag $data['name'] = $textonly ? $data['username'] : hsc($data['username']); 152260a396c8SGerrit Uitslag } else { 152360a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 1524dc58b6f4SAndy Webber if(isset($info) && $info) { 1525dc58b6f4SAndy Webber switch($conf['showuseras']) { 1526dc58b6f4SAndy Webber case 'username': 15277f081821SGerrit Uitslag case 'username_link': 152815f3bc49SGerrit Uitslag $data['name'] = $textonly ? $info['name'] : hsc($info['name']); 152960a396c8SGerrit Uitslag break; 1530dc58b6f4SAndy Webber case 'email': 1531dc58b6f4SAndy Webber case 'email_link': 153260a396c8SGerrit Uitslag $data['name'] = obfuscate($info['mail']); 153360a396c8SGerrit Uitslag break; 1534dc58b6f4SAndy Webber } 153560a396c8SGerrit Uitslag } 153660a396c8SGerrit Uitslag } 153760a396c8SGerrit Uitslag } 15387f081821SGerrit Uitslag 15397f081821SGerrit Uitslag /** @var Doku_Renderer_xhtml $xhtml_renderer */ 15407f081821SGerrit Uitslag static $xhtml_renderer = null; 15417f081821SGerrit Uitslag 154215f3bc49SGerrit Uitslag if(!$data['textonly'] && empty($data['link']['url'])) { 15437f081821SGerrit Uitslag 15447f081821SGerrit Uitslag if(in_array($conf['showuseras'], array('email_link', 'username_link'))) { 154560a396c8SGerrit Uitslag if(!isset($info)) { 154660a396c8SGerrit Uitslag if($auth) $info = $auth->getUserData($username); 154760a396c8SGerrit Uitslag } 154860a396c8SGerrit Uitslag if(isset($info) && $info) { 15497f081821SGerrit Uitslag if($conf['showuseras'] == 'email_link') { 155060a396c8SGerrit Uitslag $data['link']['url'] = 'mailto:' . obfuscate($info['mail']); 1551dc58b6f4SAndy Webber } else { 15527f081821SGerrit Uitslag if(is_null($xhtml_renderer)) { 15537f081821SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 15547f081821SGerrit Uitslag } 15557f081821SGerrit Uitslag if(empty($xhtml_renderer->interwiki)) { 15567f081821SGerrit Uitslag $xhtml_renderer->interwiki = getInterwiki(); 15577f081821SGerrit Uitslag } 15587f081821SGerrit Uitslag $shortcut = 'user'; 1559533772e1SGerrit Uitslag $exists = null; 15606496c33fSGerrit Uitslag $data['link']['url'] = $xhtml_renderer->_resolveInterWiki($shortcut, $username, $exists); 15612a2a43c4SGerrit Uitslag $data['link']['class'] .= ' interwiki iw_user'; 15626496c33fSGerrit Uitslag if($exists !== null) { 15636496c33fSGerrit Uitslag if($exists) { 15646496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink1'; 15656496c33fSGerrit Uitslag } else { 15666496c33fSGerrit Uitslag $data['link']['class'] .= ' wikilink2'; 15676496c33fSGerrit Uitslag $data['link']['rel'] = 'nofollow'; 15686496c33fSGerrit Uitslag } 15696496c33fSGerrit Uitslag } 1570dc58b6f4SAndy Webber } 1571dc58b6f4SAndy Webber } else { 157215f3bc49SGerrit Uitslag $data['textonly'] = true; 1573dc58b6f4SAndy Webber } 157460a396c8SGerrit Uitslag 157560a396c8SGerrit Uitslag } else { 157615f3bc49SGerrit Uitslag $data['textonly'] = true; 157760a396c8SGerrit Uitslag } 157860a396c8SGerrit Uitslag } 157960a396c8SGerrit Uitslag 158015f3bc49SGerrit Uitslag if($data['textonly']) { 15814d5fc927SGerrit Uitslag $data['userlink'] = $data['name']; 158260a396c8SGerrit Uitslag } else { 158360a396c8SGerrit Uitslag $data['link']['name'] = $data['name']; 158460a396c8SGerrit Uitslag if(is_null($xhtml_renderer)) { 158560a396c8SGerrit Uitslag $xhtml_renderer = p_get_renderer('xhtml'); 158660a396c8SGerrit Uitslag } 15874d5fc927SGerrit Uitslag $data['userlink'] = $xhtml_renderer->_formatLink($data['link']); 158860a396c8SGerrit Uitslag } 158960a396c8SGerrit Uitslag } 159060a396c8SGerrit Uitslag $evt->advise_after(); 159160a396c8SGerrit Uitslag unset($evt); 159260a396c8SGerrit Uitslag 15934d5fc927SGerrit Uitslag return $data['userlink']; 1594066fee30SAndreas Gohr} 1595066fee30SAndreas Gohr 1596066fee30SAndreas Gohr/** 1597066fee30SAndreas Gohr * Returns the path to a image file for the currently chosen license. 1598066fee30SAndreas Gohr * When no image exists, returns an empty string 1599066fee30SAndreas Gohr * 1600066fee30SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1601066fee30SAndreas Gohr * @param string $type - type of image 'badge' or 'button' 16023272d797SAndreas Gohr * @return string 1603066fee30SAndreas Gohr */ 1604066fee30SAndreas Gohrfunction license_img($type) { 1605066fee30SAndreas Gohr global $license; 1606066fee30SAndreas Gohr global $conf; 1607066fee30SAndreas Gohr if(!$conf['license']) return ''; 1608066fee30SAndreas Gohr if(!is_array($license[$conf['license']])) return ''; 1609066fee30SAndreas Gohr $lic = $license[$conf['license']]; 1610066fee30SAndreas Gohr $try = array(); 1611066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.png'; 1612066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/'.$conf['license'].'.gif'; 1613066fee30SAndreas Gohr if(substr($conf['license'], 0, 3) == 'cc-') { 1614066fee30SAndreas Gohr $try[] = 'lib/images/license/'.$type.'/cc.png'; 1615066fee30SAndreas Gohr } 1616066fee30SAndreas Gohr foreach($try as $src) { 1617066fee30SAndreas Gohr if(@file_exists(DOKU_INC.$src)) return $src; 1618066fee30SAndreas Gohr } 1619066fee30SAndreas Gohr return ''; 1620dc58b6f4SAndy Webber} 1621dc58b6f4SAndy Webber 162213c08e2fSMichael Klier/** 162313c08e2fSMichael Klier * Checks if the given amount of memory is available 162413c08e2fSMichael Klier * 162513c08e2fSMichael Klier * If the memory_get_usage() function is not available the 162613c08e2fSMichael Klier * function just assumes $bytes of already allocated memory 162713c08e2fSMichael Klier * 162813c08e2fSMichael Klier * @author Filip Oscadal <webmaster@illusionsoftworks.cz> 162913c08e2fSMichael Klier * @author Andreas Gohr <andi@splitbrain.org> 16303272d797SAndreas Gohr * 16313272d797SAndreas Gohr * @param int $mem Size of memory you want to allocate in bytes 16323272d797SAndreas Gohr * @param int $bytes 16333272d797SAndreas Gohr * @internal param int $used already allocated memory (see above) 16343272d797SAndreas Gohr * @return bool 163513c08e2fSMichael Klier */ 163613c08e2fSMichael Klierfunction is_mem_available($mem, $bytes = 1048576) { 163713c08e2fSMichael Klier $limit = trim(ini_get('memory_limit')); 163813c08e2fSMichael Klier if(empty($limit)) return true; // no limit set! 163913c08e2fSMichael Klier 164013c08e2fSMichael Klier // parse limit to bytes 164113c08e2fSMichael Klier $limit = php_to_byte($limit); 164213c08e2fSMichael Klier 164313c08e2fSMichael Klier // get used memory if possible 164413c08e2fSMichael Klier if(function_exists('memory_get_usage')) { 164513c08e2fSMichael Klier $used = memory_get_usage(); 164649eb6e38SAndreas Gohr } else { 164749eb6e38SAndreas Gohr $used = $bytes; 164813c08e2fSMichael Klier } 164913c08e2fSMichael Klier 165013c08e2fSMichael Klier if($used + $mem > $limit) { 165113c08e2fSMichael Klier return false; 165213c08e2fSMichael Klier } 165313c08e2fSMichael Klier 165413c08e2fSMichael Klier return true; 165513c08e2fSMichael Klier} 165613c08e2fSMichael Klier 1657af2408d5SAndreas Gohr/** 1658af2408d5SAndreas Gohr * Send a HTTP redirect to the browser 1659af2408d5SAndreas Gohr * 1660af2408d5SAndreas Gohr * Works arround Microsoft IIS cookie sending bug. Exits the script. 1661af2408d5SAndreas Gohr * 1662af2408d5SAndreas Gohr * @link http://support.microsoft.com/kb/q176113/ 1663af2408d5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 1664af2408d5SAndreas Gohr */ 1665af2408d5SAndreas Gohrfunction send_redirect($url) { 1666585bf44eSChristopher Smith /* @var Input $INPUT */ 1667585bf44eSChristopher Smith global $INPUT; 1668585bf44eSChristopher Smith 16690181f021SAndreas Gohr //are there any undisplayed messages? keep them in session for display 16700181f021SAndreas Gohr global $MSG; 16710181f021SAndreas Gohr if(isset($MSG) && count($MSG) && !defined('NOSESSION')) { 16720181f021SAndreas Gohr //reopen session, store data and close session again 16730181f021SAndreas Gohr @session_start(); 16740181f021SAndreas Gohr $_SESSION[DOKU_COOKIE]['msg'] = $MSG; 16750181f021SAndreas Gohr } 16760181f021SAndreas Gohr 1677d4869846SAndreas Gohr // always close the session 1678d4869846SAndreas Gohr session_write_close(); 1679d4869846SAndreas Gohr 1680c10dcb7dSAndreas Gohr // work around IE bug 1681c10dcb7dSAndreas Gohr // http://www.ianhoar.com/2008/11/16/internet-explorer-6-and-redirected-anchor-links/ 16826d2af55dSChristopher Smith @list($url, $hash) = explode('#', $url); 1683c10dcb7dSAndreas Gohr if($hash) { 1684c10dcb7dSAndreas Gohr if(strpos($url, '?')) { 1685c10dcb7dSAndreas Gohr $url = $url.'&#'.$hash; 1686c10dcb7dSAndreas Gohr } else { 1687c10dcb7dSAndreas Gohr $url = $url.'?&#'.$hash; 1688c10dcb7dSAndreas Gohr } 1689c10dcb7dSAndreas Gohr } 1690c10dcb7dSAndreas Gohr 1691af2408d5SAndreas Gohr // check if running on IIS < 6 with CGI-PHP 1692585bf44eSChristopher Smith if($INPUT->server->has('SERVER_SOFTWARE') && $INPUT->server->has('GATEWAY_INTERFACE') && 1693585bf44eSChristopher Smith (strpos($INPUT->server->str('GATEWAY_INTERFACE'), 'CGI') !== false) && 1694585bf44eSChristopher Smith (preg_match('|^Microsoft-IIS/(\d)\.\d$|', trim($INPUT->server->str('SERVER_SOFTWARE')), $matches)) && 16953272d797SAndreas Gohr $matches[1] < 6 16963272d797SAndreas Gohr ) { 1697af2408d5SAndreas Gohr header('Refresh: 0;url='.$url); 1698af2408d5SAndreas Gohr } else { 1699af2408d5SAndreas Gohr header('Location: '.$url); 1700af2408d5SAndreas Gohr } 1701af2408d5SAndreas Gohr exit; 1702af2408d5SAndreas Gohr} 1703af2408d5SAndreas Gohr 17045b75cd1fSAdrian Lang/** 17055b75cd1fSAdrian Lang * Validate a value using a set of valid values 17065b75cd1fSAdrian Lang * 17075b75cd1fSAdrian Lang * This function checks whether a specified value is set and in the array 17085b75cd1fSAdrian Lang * $valid_values. If not, the function returns a default value or, if no 17095b75cd1fSAdrian Lang * default is specified, throws an exception. 17105b75cd1fSAdrian Lang * 17115b75cd1fSAdrian Lang * @param string $param The name of the parameter 17125b75cd1fSAdrian Lang * @param array $valid_values A set of valid values; Optionally a default may 17135b75cd1fSAdrian Lang * be marked by the key “default”. 17145b75cd1fSAdrian Lang * @param array $array The array containing the value (typically $_POST 17155b75cd1fSAdrian Lang * or $_GET) 17165b75cd1fSAdrian Lang * @param string $exc The text of the raised exception 17175b75cd1fSAdrian Lang * 17183272d797SAndreas Gohr * @throws Exception 17193272d797SAndreas Gohr * @return mixed 17205b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 17215b75cd1fSAdrian Lang */ 17225b75cd1fSAdrian Langfunction valid_input_set($param, $valid_values, $array, $exc = '') { 17235b75cd1fSAdrian Lang if(isset($array[$param]) && in_array($array[$param], $valid_values)) { 17245b75cd1fSAdrian Lang return $array[$param]; 17255b75cd1fSAdrian Lang } elseif(isset($valid_values['default'])) { 17265b75cd1fSAdrian Lang return $valid_values['default']; 17275b75cd1fSAdrian Lang } else { 17285b75cd1fSAdrian Lang throw new Exception($exc); 17295b75cd1fSAdrian Lang } 17305b75cd1fSAdrian Lang} 17315b75cd1fSAdrian Lang 173263703ba5SAndreas Gohr/** 173363703ba5SAndreas Gohr * Read a preference from the DokuWiki cookie 1734646a531aSChristopher Smith * (remembering both keys & values are urlencoded) 173563703ba5SAndreas Gohr */ 1736554a8c9fSAdrian Langfunction get_doku_pref($pref, $default) { 1737646a531aSChristopher Smith $enc_pref = urlencode($pref); 1738646a531aSChristopher Smith if(strpos($_COOKIE['DOKU_PREFS'], $enc_pref) !== false) { 1739554a8c9fSAdrian Lang $parts = explode('#', $_COOKIE['DOKU_PREFS']); 174063703ba5SAndreas Gohr $cnt = count($parts); 174163703ba5SAndreas Gohr for($i = 0; $i < $cnt; $i += 2) { 1742646a531aSChristopher Smith if($parts[$i] == $enc_pref) { 1743646a531aSChristopher Smith return urldecode($parts[$i + 1]); 1744554a8c9fSAdrian Lang } 1745554a8c9fSAdrian Lang } 1746554a8c9fSAdrian Lang } 1747554a8c9fSAdrian Lang return $default; 1748554a8c9fSAdrian Lang} 1749554a8c9fSAdrian Lang 17503c94d07bSAnika Henke/** 17513c94d07bSAnika Henke * Add a preference to the DokuWiki cookie 175236ec377eSChristopher Smith * (remembering $_COOKIE['DOKU_PREFS'] is urlencoded) 17533c94d07bSAnika Henke */ 17543c94d07bSAnika Henkefunction set_doku_pref($pref, $val) { 17553c94d07bSAnika Henke global $conf; 17563c94d07bSAnika Henke $orig = get_doku_pref($pref, false); 17573c94d07bSAnika Henke $cookieVal = ''; 17583c94d07bSAnika Henke 17593c94d07bSAnika Henke if($orig && ($orig != $val)) { 17603c94d07bSAnika Henke $parts = explode('#', $_COOKIE['DOKU_PREFS']); 17613c94d07bSAnika Henke $cnt = count($parts); 176236ec377eSChristopher Smith // urlencode $pref for the comparison 176336ec377eSChristopher Smith $enc_pref = rawurlencode($pref); 17643c94d07bSAnika Henke for($i = 0; $i < $cnt; $i += 2) { 176536ec377eSChristopher Smith if($parts[$i] == $enc_pref) { 176636ec377eSChristopher Smith $parts[$i + 1] = rawurlencode($val); 176750f261f7SMichael Hamann break; 17683c94d07bSAnika Henke } 17693c94d07bSAnika Henke } 17703c94d07bSAnika Henke $cookieVal = implode('#', $parts); 17713c94d07bSAnika Henke } else if (!$orig) { 177236ec377eSChristopher Smith $cookieVal = ($_COOKIE['DOKU_PREFS'] ? $_COOKIE['DOKU_PREFS'].'#' : '').rawurlencode($pref).'#'.rawurlencode($val); 17733c94d07bSAnika Henke } 17743c94d07bSAnika Henke 17753c94d07bSAnika Henke if (!empty($cookieVal)) { 177675e4dd8aSGerrit Uitslag $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 177775e4dd8aSGerrit Uitslag setcookie('DOKU_PREFS', $cookieVal, time()+365*24*3600, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 17783c94d07bSAnika Henke } 17793c94d07bSAnika Henke} 17803c94d07bSAnika Henke 1781f8fb2d18SAndreas Gohr/** 1782f8fb2d18SAndreas Gohr * Strips source mapping declarations from given text #601 1783f8fb2d18SAndreas Gohr * 1784f8fb2d18SAndreas Gohr * @param &string $text reference to the CSS or JavaScript code to clean 1785f8fb2d18SAndreas Gohr */ 1786f8fb2d18SAndreas Gohrfunction stripsourcemaps(&$text){ 1787f8fb2d18SAndreas Gohr $text = preg_replace('/^(\/\/|\/\*)[@#]\s+sourceMappingURL=.*?(\*\/)?$/im', '\\1\\2', $text); 1788f8fb2d18SAndreas Gohr} 1789f8fb2d18SAndreas Gohr 1790e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1791