1<?php 2/** 3 * Utilities for handling pagenames 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 * @todo Combine similar functions like {wiki,media,meta}FN() 8 */ 9 10/** 11 * Fetch the an ID from request 12 * 13 * Uses either standard $_REQUEST variable or extracts it from 14 * the full request URI when userewrite is set to 2 15 * 16 * For $param='id' $conf['start'] is returned if no id was found. 17 * If the second parameter is true (default) the ID is cleaned. 18 * 19 * @author Andreas Gohr <andi@splitbrain.org> 20 */ 21function getID($param='id',$clean=true){ 22 global $conf; 23 24 $id = $_REQUEST[$param]; 25 26 //construct page id from request URI 27 if(empty($id) && $conf['userewrite'] == 2){ 28 //get the script URL 29 if($conf['basedir']){ 30 $relpath = ''; 31 if($param != 'id') { 32 $relpath = 'lib/exe/'; 33 } 34 $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']); 35 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 36 $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 37 $_SERVER['SCRIPT_FILENAME']); 38 $script = '/'.$script; 39 }else{ 40 $script = $_SERVER['SCRIPT_NAME']; 41 } 42 43 //clean script and request (fixes a windows problem) 44 $script = preg_replace('/\/\/+/','/',$script); 45 $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']); 46 47 //remove script URL and Querystring to gain the id 48 if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 49 $id = preg_replace ('/\?.*/','',$match[1]); 50 } 51 $id = urldecode($id); 52 //strip leading slashes 53 $id = preg_replace('!^/+!','',$id); 54 } 55 if($clean) $id = cleanID($id); 56 if(empty($id) && $param=='id') $id = $conf['start']; 57 58 return $id; 59} 60 61/** 62 * Remove unwanted chars from ID 63 * 64 * Cleans a given ID to only use allowed characters. Accented characters are 65 * converted to unaccented ones 66 * 67 * @author Andreas Gohr <andi@splitbrain.org> 68 * @param string $id The pageid to clean 69 * @param boolean $ascii Force ASCII 70 */ 71function cleanID($id,$ascii=false){ 72 global $conf; 73 global $lang; 74 static $sepcharpat = null; 75 76 $sepchar = $conf['sepchar']; 77 if($sepcharpat == null) // build string only once to save clock cycles 78 $sepcharpat = '#\\'.$sepchar.'+#'; 79 80 $id = trim($id); 81 $id = utf8_strtolower($id); 82 83 //alternative namespace seperator 84 $id = strtr($id,';',':'); 85 if($conf['useslash']){ 86 $id = strtr($id,'/',':'); 87 }else{ 88 $id = strtr($id,'/',$sepchar); 89 } 90 91 if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 92 if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 93 94 //remove specials 95 $id = utf8_stripspecials($id,$sepchar,'\*'); 96 97 if($ascii) $id = utf8_strip($id); 98 99 //clean up 100 $id = preg_replace($sepcharpat,$sepchar,$id); 101 $id = preg_replace('#:+#',':',$id); 102 $id = trim($id,':._-'); 103 $id = preg_replace('#:[:\._\-]+#',':',$id); 104 105 return($id); 106} 107 108/** 109 * Return namespacepart of a wiki ID 110 * 111 * @author Andreas Gohr <andi@splitbrain.org> 112 */ 113function getNS($id){ 114 $pos = strrpos($id,':'); 115 if($pos!==false){ 116 return substr($id,0,$pos); 117 } 118 return false; 119} 120 121/** 122 * Returns the ID without the namespace 123 * 124 * @author Andreas Gohr <andi@splitbrain.org> 125 */ 126function noNS($id) { 127 $pos = strrpos($id, ':'); 128 if ($pos!==false) { 129 return substr($id, $pos+1); 130 } else { 131 return $id; 132 } 133} 134 135/** 136 * returns the full path to the datafile specified by ID and 137 * optional revision 138 * 139 * The filename is URL encoded to protect Unicode chars 140 * 141 * @author Andreas Gohr <andi@splitbrain.org> 142 */ 143function wikiFN($id,$rev=''){ 144 global $conf; 145 $id = cleanID($id); 146 $id = str_replace(':','/',$id); 147 if(empty($rev)){ 148 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 149 }else{ 150 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 151 if($conf['usegzip'] && !@file_exists($fn)){ 152 //return gzip if enabled and plaintext doesn't exist 153 $fn .= '.gz'; 154 } 155 } 156 return $fn; 157} 158 159/** 160 * Returns the full path to the file for locking the page while editing. 161 * 162 * @author Ben Coburn <btcoburn@silicodon.net> 163 */ 164function wikiLockFN($id) { 165 global $conf; 166 return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 167} 168 169 170/** 171 * returns the full path to the meta file specified by ID and extension 172 * 173 * The filename is URL encoded to protect Unicode chars 174 * 175 * @author Steven Danz <steven-danz@kc.rr.com> 176 */ 177function metaFN($id,$ext){ 178 global $conf; 179 $id = cleanID($id); 180 $id = str_replace(':','/',$id); 181 $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 182 return $fn; 183} 184 185/** 186 * returns an array of full paths to all metafiles of a given ID 187 * 188 * @author Esther Brunner <esther@kaffeehaus.ch> 189 */ 190function metaFiles($id){ 191 $name = noNS($id); 192 $dir = metaFN(getNS($id),''); 193 $files = array(); 194 195 $dh = @opendir($dir); 196 if(!$dh) return $files; 197 while(($file = readdir($dh)) !== false){ 198 if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file)) 199 $files[] = $dir.$file; 200 } 201 closedir($dh); 202 203 return $files; 204} 205 206/** 207 * returns the full path to the mediafile specified by ID 208 * 209 * The filename is URL encoded to protect Unicode chars 210 * 211 * @author Andreas Gohr <andi@splitbrain.org> 212 */ 213function mediaFN($id){ 214 global $conf; 215 $id = cleanID($id); 216 $id = str_replace(':','/',$id); 217 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 218 return $fn; 219} 220 221/** 222 * Returns the full filepath to a localized textfile if local 223 * version isn't found the english one is returned 224 * 225 * @author Andreas Gohr <andi@splitbrain.org> 226 */ 227function localeFN($id){ 228 global $conf; 229 $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt'; 230 if(!@file_exists($file)){ 231 //fall back to english 232 $file = DOKU_INC.'inc/lang/en/'.$id.'.txt'; 233 } 234 return $file; 235} 236 237/** 238 * Resolve relative paths in IDs 239 * 240 * Do not call directly use resolve_mediaid or resolve_pageid 241 * instead 242 * 243 * Partyly based on a cleanPath function found at 244 * http://www.php.net/manual/en/function.realpath.php#57016 245 * 246 * @author <bart at mediawave dot nl> 247 */ 248function resolve_id($ns,$id,$clean=true){ 249 // if the id starts with a dot we need to handle the 250 // relative stuff 251 if($id{0} == '.'){ 252 // normalize initial dots without a colon 253 $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 254 // prepend the current namespace 255 $id = $ns.':'.$id; 256 257 // cleanup relatives 258 $result = array(); 259 $pathA = explode(':', $id); 260 if (!$pathA[0]) $result[] = ''; 261 foreach ($pathA AS $key => $dir) { 262 if ($dir == '..') { 263 if (end($result) == '..') { 264 $result[] = '..'; 265 } elseif (!array_pop($result)) { 266 $result[] = '..'; 267 } 268 } elseif ($dir && $dir != '.') { 269 $result[] = $dir; 270 } 271 } 272 if (!end($pathA)) $result[] = ''; 273 $id = implode(':', $result); 274 }elseif($ns !== false && strpos($id,':') === false){ 275 //if link contains no namespace. add current namespace (if any) 276 $id = $ns.':'.$id; 277 } 278 279 if($clean) $id = cleanID($id); 280 return $id; 281} 282 283/** 284 * Returns a full media id 285 * 286 * @author Andreas Gohr <andi@splitbrain.org> 287 */ 288function resolve_mediaid($ns,&$page,&$exists){ 289 $page = resolve_id($ns,$page); 290 $file = mediaFN($page); 291 $exists = @file_exists($file); 292} 293 294/** 295 * Returns a full page id 296 * 297 * @author Andreas Gohr <andi@splitbrain.org> 298 */ 299function resolve_pageid($ns,&$page,&$exists){ 300 global $conf; 301 $exists = false; 302 303 //keep hashlink if exists then clean both parts 304 list($page,$hash) = split('#',$page,2); 305 $hash = cleanID($hash); 306 $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 307 308 // get filename (calls clean itself) 309 $file = wikiFN($page); 310 311 // if ends with colon we have a namespace link 312 if(substr($page,-1) == ':'){ 313 if(@file_exists(wikiFN($page.$conf['start']))){ 314 // start page inside namespace 315 $page = $page.$conf['start']; 316 $exists = true; 317 }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){ 318 // page named like the NS inside the NS 319 $page = $page.noNS(cleanID($page)); 320 $exists = true; 321 }elseif(@file_exists(wikiFN($page))){ 322 // page like namespace exists 323 $page = $page; 324 $exists = true; 325 }else{ 326 // fall back to default 327 $page = $page.$conf['start']; 328 } 329 }else{ 330 //check alternative plural/nonplural form 331 if(!@file_exists($file)){ 332 if( $conf['autoplural'] ){ 333 if(substr($page,-1) == 's'){ 334 $try = substr($page,0,-1); 335 }else{ 336 $try = $page.'s'; 337 } 338 if(@file_exists(wikiFN($try))){ 339 $page = $try; 340 $exists = true; 341 } 342 } 343 }else{ 344 $exists = true; 345 } 346 } 347 348 // now make sure we have a clean page 349 $page = cleanID($page); 350 351 //add hash if any 352 if(!empty($hash)) $page .= '#'.$hash; 353} 354 355/** 356 * Returns the name of a cachefile from given data 357 * 358 * The needed directory is created by this function! 359 * 360 * @author Andreas Gohr <andi@splitbrain.org> 361 * 362 * @param string $data This data is used to create a unique md5 name 363 * @param string $ext This is appended to the filename if given 364 * @return string The filename of the cachefile 365 */ 366function getCacheName($data,$ext=''){ 367 global $conf; 368 $md5 = md5($data); 369 $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 370 io_makeFileDir($file); 371 return $file; 372} 373 374/** 375 * Checks a pageid against $conf['hidepages'] 376 * 377 * @author Andreas Gohr <gohr@cosmocode.de> 378 */ 379function isHiddenPage($id){ 380 global $conf; 381 if(empty($conf['hidepages'])) return false; 382 383 if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){ 384 return true; 385 } 386 return false; 387} 388 389/** 390 * Reverse of isHiddenPage 391 * 392 * @author Andreas Gohr <gohr@cosmocode.de> 393 */ 394function isVisiblePage($id){ 395 return !isHiddenPage($id); 396} 397 398/** 399 * Checks and sets HTTP headers for conditional HTTP requests 400 * 401 * @author Simon Willison <swillison@gmail.com> 402 * @link http://simon.incutio.com/archive/2003/04/23/conditionalGet 403 * @param timestamp $timestamp lastmodified time of the cache file 404 * @returns void or void with previously header() commands executed 405 */ 406function http_conditionalRequest($timestamp){ 407 // A PHP implementation of conditional get, see 408 // http://fishbowl.pastiche.org/archives/001132.html 409 $last_modified = substr(date('r', $timestamp), 0, -5).'GMT'; 410 $etag = '"'.md5($last_modified).'"'; 411 // Send the headers 412 header("Last-Modified: $last_modified"); 413 header("ETag: $etag"); 414 // See if the client has provided the required headers 415 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ 416 $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']); 417 }else{ 418 $if_modified_since = false; 419 } 420 421 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){ 422 $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); 423 }else{ 424 $if_none_match = false; 425 } 426 427 if (!$if_modified_since && !$if_none_match){ 428 return; 429 } 430 431 // At least one of the headers is there - check them 432 if ($if_none_match && $if_none_match != $etag) { 433 return; // etag is there but doesn't match 434 } 435 436 if ($if_modified_since && $if_modified_since != $last_modified) { 437 return; // if-modified-since is there but doesn't match 438 } 439 440 // Nothing has changed since their last request - serve a 304 and exit 441 header('HTTP/1.0 304 Not Modified'); 442 exit; 443} 444 445//Setup VIM: ex: et ts=2 enc=utf-8 : 446