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 $INPUT; 23 global $conf; 24 global $ACT; 25 26 $id = $INPUT->str($param); 27 28 //construct page id from request URI 29 if(empty($id) && $conf['userewrite'] == 2){ 30 $request = $_SERVER['REQUEST_URI']; 31 $script = ''; 32 33 //get the script URL 34 if($conf['basedir']){ 35 $relpath = ''; 36 if($param != 'id') { 37 $relpath = 'lib/exe/'; 38 } 39 $script = $conf['basedir'].$relpath.utf8_basename($_SERVER['SCRIPT_FILENAME']); 40 41 }elseif($_SERVER['PATH_INFO']){ 42 $request = $_SERVER['PATH_INFO']; 43 }elseif($_SERVER['SCRIPT_NAME']){ 44 $script = $_SERVER['SCRIPT_NAME']; 45 }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 46 $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 47 $_SERVER['SCRIPT_FILENAME']); 48 $script = '/'.$script; 49 } 50 51 //clean script and request (fixes a windows problem) 52 $script = preg_replace('/\/\/+/','/',$script); 53 $request = preg_replace('/\/\/+/','/',$request); 54 55 //remove script URL and Querystring to gain the id 56 if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 57 $id = preg_replace ('/\?.*/','',$match[1]); 58 } 59 $id = urldecode($id); 60 //strip leading slashes 61 $id = preg_replace('!^/+!','',$id); 62 } 63 64 // Namespace autolinking from URL 65 if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ 66 if(page_exists($id.$conf['start'])){ 67 // start page inside namespace 68 $id = $id.$conf['start']; 69 }elseif(page_exists($id.noNS(cleanID($id)))){ 70 // page named like the NS inside the NS 71 $id = $id.noNS(cleanID($id)); 72 }elseif(page_exists($id)){ 73 // page like namespace exists 74 $id = substr($id,0,-1); 75 }else{ 76 // fall back to default 77 $id = $id.$conf['start']; 78 } 79 if (isset($ACT) && $ACT === 'show') send_redirect(wl($id,'',true)); 80 } 81 82 if($clean) $id = cleanID($id); 83 if(empty($id) && $param=='id') $id = $conf['start']; 84 85 return $id; 86} 87 88/** 89 * Remove unwanted chars from ID 90 * 91 * Cleans a given ID to only use allowed characters. Accented characters are 92 * converted to unaccented ones 93 * 94 * @author Andreas Gohr <andi@splitbrain.org> 95 * @param string $raw_id The pageid to clean 96 * @param boolean $ascii Force ASCII 97 * @param boolean $media DEPRECATED 98 */ 99function cleanID($raw_id,$ascii=false,$media=false){ 100 global $conf; 101 static $sepcharpat = null; 102 103 global $cache_cleanid; 104 $cache = & $cache_cleanid; 105 106 // check if it's already in the memory cache 107 if (isset($cache[(string)$raw_id])) { 108 return $cache[(string)$raw_id]; 109 } 110 111 $sepchar = $conf['sepchar']; 112 if($sepcharpat == null) // build string only once to save clock cycles 113 $sepcharpat = '#\\'.$sepchar.'+#'; 114 115 $id = trim((string)$raw_id); 116 $id = utf8_strtolower($id); 117 118 //alternative namespace seperator 119 if($conf['useslash']){ 120 $id = strtr($id,';/','::'); 121 }else{ 122 $id = strtr($id,';/',':'.$sepchar); 123 } 124 125 if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 126 if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 127 128 //remove specials 129 $id = utf8_stripspecials($id,$sepchar,'\*'); 130 131 if($ascii) $id = utf8_strip($id); 132 133 //clean up 134 $id = preg_replace($sepcharpat,$sepchar,$id); 135 $id = preg_replace('#:+#',':',$id); 136 $id = trim($id,':._-'); 137 $id = preg_replace('#:[:\._\-]+#',':',$id); 138 $id = preg_replace('#[:\._\-]+:#',':',$id); 139 140 $cache[(string)$raw_id] = $id; 141 return($id); 142} 143 144/** 145 * Return namespacepart of a wiki ID 146 * 147 * @author Andreas Gohr <andi@splitbrain.org> 148 */ 149function getNS($id){ 150 $pos = strrpos((string)$id,':'); 151 if($pos!==false){ 152 return substr((string)$id,0,$pos); 153 } 154 return false; 155} 156 157/** 158 * Returns the ID without the namespace 159 * 160 * @author Andreas Gohr <andi@splitbrain.org> 161 */ 162function noNS($id) { 163 $pos = strrpos($id, ':'); 164 if ($pos!==false) { 165 return substr($id, $pos+1); 166 } else { 167 return $id; 168 } 169} 170 171/** 172 * Returns the current namespace 173 * 174 * @author Nathan Fritz <fritzn@crown.edu> 175 */ 176function curNS($id) { 177 return noNS(getNS($id)); 178} 179 180/** 181 * Returns the ID without the namespace or current namespace for 'start' pages 182 * 183 * @author Nathan Fritz <fritzn@crown.edu> 184 */ 185function noNSorNS($id) { 186 global $conf; 187 188 $p = noNS($id); 189 if ($p == $conf['start'] || $p == false) { 190 $p = curNS($id); 191 if ($p == false) { 192 return $conf['start']; 193 } 194 } 195 return $p; 196} 197 198/** 199 * Creates a XHTML valid linkid from a given headline title 200 * 201 * @param string $title The headline title 202 * @param array|bool $check Existing IDs (title => number) 203 * @return string the title 204 * @author Andreas Gohr <andi@splitbrain.org> 205 */ 206function sectionID($title,&$check) { 207 $title = str_replace(array(':','.'),'',cleanID($title)); 208 $new = ltrim($title,'0123456789_-'); 209 if(empty($new)){ 210 $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline 211 }else{ 212 $title = $new; 213 } 214 215 if(is_array($check)){ 216 // make sure tiles are unique 217 if (!array_key_exists ($title,$check)) { 218 $check[$title] = 0; 219 } else { 220 $title .= ++ $check[$title]; 221 } 222 } 223 224 return $title; 225} 226 227 228/** 229 * Wiki page existence check 230 * 231 * parameters as for wikiFN 232 * 233 * @author Chris Smith <chris@jalakai.co.uk> 234 */ 235function page_exists($id,$rev='',$clean=true) { 236 return @file_exists(wikiFN($id,$rev,$clean)); 237} 238 239/** 240 * returns the full path to the datafile specified by ID and optional revision 241 * 242 * The filename is URL encoded to protect Unicode chars 243 * 244 * @param $raw_id string id of wikipage 245 * @param $rev string page revision, empty string for current 246 * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 247 * when $id is guaranteed to have been cleaned already. 248 * 249 * @author Andreas Gohr <andi@splitbrain.org> 250 */ 251function wikiFN($raw_id,$rev='',$clean=true){ 252 global $conf; 253 254 global $cache_wikifn; 255 $cache = & $cache_wikifn; 256 257 if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { 258 return $cache[$raw_id][$rev]; 259 } 260 261 $id = $raw_id; 262 263 if ($clean) $id = cleanID($id); 264 $id = str_replace(':','/',$id); 265 if(empty($rev)){ 266 $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 267 }else{ 268 $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 269 if($conf['compression']){ 270 //test for extensions here, we want to read both compressions 271 if (@file_exists($fn . '.gz')){ 272 $fn .= '.gz'; 273 }else if(@file_exists($fn . '.bz2')){ 274 $fn .= '.bz2'; 275 }else{ 276 //file doesnt exist yet, so we take the configured extension 277 $fn .= '.' . $conf['compression']; 278 } 279 } 280 } 281 282 if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } 283 $cache[$raw_id][$rev] = $fn; 284 return $fn; 285} 286 287/** 288 * Returns the full path to the file for locking the page while editing. 289 * 290 * @author Ben Coburn <btcoburn@silicodon.net> 291 */ 292function wikiLockFN($id) { 293 global $conf; 294 return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 295} 296 297 298/** 299 * returns the full path to the meta file specified by ID and extension 300 * 301 * @author Steven Danz <steven-danz@kc.rr.com> 302 */ 303function metaFN($id,$ext){ 304 global $conf; 305 $id = cleanID($id); 306 $id = str_replace(':','/',$id); 307 $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 308 return $fn; 309} 310 311/** 312 * returns the full path to the media's meta file specified by ID and extension 313 * 314 * @author Kate Arzamastseva <pshns@ukr.net> 315 */ 316function mediaMetaFN($id,$ext){ 317 global $conf; 318 $id = cleanID($id); 319 $id = str_replace(':','/',$id); 320 $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 321 return $fn; 322} 323 324/** 325 * returns an array of full paths to all metafiles of a given ID 326 * 327 * @author Esther Brunner <esther@kaffeehaus.ch> 328 * @author Michael Hamann <michael@content-space.de> 329 */ 330function metaFiles($id){ 331 $basename = metaFN($id, ''); 332 $files = glob($basename.'.*', GLOB_MARK); 333 // filter files like foo.bar.meta when $id == 'foo' 334 return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 335} 336 337/** 338 * returns the full path to the mediafile specified by ID 339 * 340 * The filename is URL encoded to protect Unicode chars 341 * 342 * @author Andreas Gohr <andi@splitbrain.org> 343 * @author Kate Arzamastseva <pshns@ukr.net> 344 */ 345function mediaFN($id, $rev=''){ 346 global $conf; 347 $id = cleanID($id); 348 $id = str_replace(':','/',$id); 349 if(empty($rev)){ 350 $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 351 }else{ 352 $ext = mimetype($id); 353 $name = substr($id,0, -1*strlen($ext[0])-1); 354 $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 355 } 356 return $fn; 357} 358 359/** 360 * Returns the full filepath to a localized file if local 361 * version isn't found the english one is returned 362 * 363 * @param string $id The id of the local file 364 * @param string $ext The file extension (usually txt) 365 * @author Andreas Gohr <andi@splitbrain.org> 366 */ 367function localeFN($id,$ext='txt'){ 368 global $conf; 369 $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext; 370 if(!@file_exists($file)){ 371 $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 372 if(!@file_exists($file)){ 373 //fall back to english 374 $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 375 } 376 } 377 return $file; 378} 379 380/** 381 * Resolve relative paths in IDs 382 * 383 * Do not call directly use resolve_mediaid or resolve_pageid 384 * instead 385 * 386 * Partyly based on a cleanPath function found at 387 * http://www.php.net/manual/en/function.realpath.php#57016 388 * 389 * @author <bart at mediawave dot nl> 390 */ 391function resolve_id($ns,$id,$clean=true){ 392 global $conf; 393 394 // some pre cleaning for useslash: 395 if($conf['useslash']) $id = str_replace('/',':',$id); 396 397 // if the id starts with a dot we need to handle the 398 // relative stuff 399 if($id && $id{0} == '.'){ 400 // normalize initial dots without a colon 401 $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 402 // prepend the current namespace 403 $id = $ns.':'.$id; 404 405 // cleanup relatives 406 $result = array(); 407 $pathA = explode(':', $id); 408 if (!$pathA[0]) $result[] = ''; 409 foreach ($pathA AS $key => $dir) { 410 if ($dir == '..') { 411 if (end($result) == '..') { 412 $result[] = '..'; 413 } elseif (!array_pop($result)) { 414 $result[] = '..'; 415 } 416 } elseif ($dir && $dir != '.') { 417 $result[] = $dir; 418 } 419 } 420 if (!end($pathA)) $result[] = ''; 421 $id = implode(':', $result); 422 }elseif($ns !== false && strpos($id,':') === false){ 423 //if link contains no namespace. add current namespace (if any) 424 $id = $ns.':'.$id; 425 } 426 427 if($clean) $id = cleanID($id); 428 return $id; 429} 430 431/** 432 * Returns a full media id 433 * 434 * @author Andreas Gohr <andi@splitbrain.org> 435 */ 436function resolve_mediaid($ns,&$page,&$exists){ 437 $page = resolve_id($ns,$page); 438 $file = mediaFN($page); 439 $exists = @file_exists($file); 440} 441 442/** 443 * Returns a full page id 444 * 445 * @author Andreas Gohr <andi@splitbrain.org> 446 */ 447function resolve_pageid($ns,&$page,&$exists){ 448 global $conf; 449 global $ID; 450 $exists = false; 451 452 //empty address should point to current page 453 if ($page === "") { 454 $page = $ID; 455 } 456 457 //keep hashlink if exists then clean both parts 458 if (strpos($page,'#')) { 459 list($page,$hash) = explode('#',$page,2); 460 } else { 461 $hash = ''; 462 } 463 $hash = cleanID($hash); 464 $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 465 466 // get filename (calls clean itself) 467 $file = wikiFN($page); 468 469 // if ends with colon or slash we have a namespace link 470 if(in_array(substr($page,-1), array(':', ';')) || 471 ($conf['useslash'] && substr($page,-1) == '/')){ 472 if(page_exists($page.$conf['start'])){ 473 // start page inside namespace 474 $page = $page.$conf['start']; 475 $exists = true; 476 }elseif(page_exists($page.noNS(cleanID($page)))){ 477 // page named like the NS inside the NS 478 $page = $page.noNS(cleanID($page)); 479 $exists = true; 480 }elseif(page_exists($page)){ 481 // page like namespace exists 482 $page = $page; 483 $exists = true; 484 }else{ 485 // fall back to default 486 $page = $page.$conf['start']; 487 } 488 }else{ 489 //check alternative plural/nonplural form 490 if(!@file_exists($file)){ 491 if( $conf['autoplural'] ){ 492 if(substr($page,-1) == 's'){ 493 $try = substr($page,0,-1); 494 }else{ 495 $try = $page.'s'; 496 } 497 if(page_exists($try)){ 498 $page = $try; 499 $exists = true; 500 } 501 } 502 }else{ 503 $exists = true; 504 } 505 } 506 507 // now make sure we have a clean page 508 $page = cleanID($page); 509 510 //add hash if any 511 if(!empty($hash)) $page .= '#'.$hash; 512} 513 514/** 515 * Returns the name of a cachefile from given data 516 * 517 * The needed directory is created by this function! 518 * 519 * @author Andreas Gohr <andi@splitbrain.org> 520 * 521 * @param string $data This data is used to create a unique md5 name 522 * @param string $ext This is appended to the filename if given 523 * @return string The filename of the cachefile 524 */ 525function getCacheName($data,$ext=''){ 526 global $conf; 527 $md5 = md5($data); 528 $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 529 io_makeFileDir($file); 530 return $file; 531} 532 533/** 534 * Checks a pageid against $conf['hidepages'] 535 * 536 * @author Andreas Gohr <gohr@cosmocode.de> 537 */ 538function isHiddenPage($id){ 539 $data = array( 540 'id' => $id, 541 'hidden' => false 542 ); 543 trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 544 return $data['hidden']; 545} 546 547function _isHiddenPage(&$data) { 548 global $conf; 549 global $ACT; 550 551 if ($data['hidden']) return; 552 if(empty($conf['hidepages'])) return; 553 if($ACT == 'admin') return; 554 555 if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){ 556 $data['hidden'] = true; 557 } 558} 559 560/** 561 * Reverse of isHiddenPage 562 * 563 * @author Andreas Gohr <gohr@cosmocode.de> 564 */ 565function isVisiblePage($id){ 566 return !isHiddenPage($id); 567} 568 569/** 570 * Format an id for output to a user 571 * 572 * Namespaces are denoted by a trailing “:*”. The root namespace is 573 * “*”. Output is escaped. 574 * 575 * @author Adrian Lang <lang@cosmocode.de> 576 */ 577 578function prettyprint_id($id) { 579 if (!$id || $id === ':') { 580 return '*'; 581 } 582 if ((substr($id, -1, 1) === ':')) { 583 $id .= '*'; 584 } 585 return hsc($id); 586} 587 588/** 589 * Encode a UTF-8 filename to use on any filesystem 590 * 591 * Uses the 'fnencode' option to determine encoding 592 * 593 * When the second parameter is true the string will 594 * be encoded only if non ASCII characters are detected - 595 * This makes it safe to run it multiple times on the 596 * same string (default is true) 597 * 598 * @author Andreas Gohr <andi@splitbrain.org> 599 * @see urlencode 600 */ 601function utf8_encodeFN($file,$safe=true){ 602 global $conf; 603 if($conf['fnencode'] == 'utf-8') return $file; 604 605 if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 606 return $file; 607 } 608 609 if($conf['fnencode'] == 'safe'){ 610 return SafeFN::encode($file); 611 } 612 613 $file = urlencode($file); 614 $file = str_replace('%2F','/',$file); 615 return $file; 616} 617 618/** 619 * Decode a filename back to UTF-8 620 * 621 * Uses the 'fnencode' option to determine encoding 622 * 623 * @author Andreas Gohr <andi@splitbrain.org> 624 * @see urldecode 625 */ 626function utf8_decodeFN($file){ 627 global $conf; 628 if($conf['fnencode'] == 'utf-8') return $file; 629 630 if($conf['fnencode'] == 'safe'){ 631 return SafeFN::decode($file); 632 } 633 634 return urldecode($file); 635} 636 637/** 638 * Find a page in the current namespace (determined from $ID) or any 639 * higher namespace 640 * 641 * Used for sidebars, but can be used other stuff as well 642 * 643 * @todo add event hook 644 * @param string $page the pagename you're looking for 645 * @return string|false the full page id of the found page, false if any 646 */ 647function page_findnearest($page){ 648 if (!$page) return false; 649 global $ID; 650 651 $ns = $ID; 652 do { 653 $ns = getNS($ns); 654 $pageid = ltrim("$ns:$page",':'); 655 if(page_exists($pageid)){ 656 return $pageid; 657 } 658 } while($ns); 659 660 return false; 661} 662