1b625487dSandi<?php 2b625487dSandi/** 3b625487dSandi * Utilities for handling pagenames 4b625487dSandi * 5b625487dSandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 71380fc45SAndreas Gohr * @todo Combine similar functions like {wiki,media,meta}FN() 8b625487dSandi */ 9b625487dSandi 106c7843b5Sandi/** 116de3759aSAndreas Gohr * Fetch the an ID from request 126c7843b5Sandi * 136c7843b5Sandi * Uses either standard $_REQUEST variable or extracts it from 146c7843b5Sandi * the full request URI when userewrite is set to 2 156c7843b5Sandi * 1642905504SAndreas Gohr * For $param='id' $conf['start'] is returned if no id was found. 1742905504SAndreas Gohr * If the second parameter is true (default) the ID is cleaned. 186c7843b5Sandi * 196c7843b5Sandi * @author Andreas Gohr <andi@splitbrain.org> 206c7843b5Sandi */ 2142905504SAndreas Gohrfunction getID($param='id',$clean=true){ 227d01a0eaSTom N Harris global $INPUT; 236c7843b5Sandi global $conf; 244e90caaaSMichael Hamann global $ACT; 256c7843b5Sandi 267d01a0eaSTom N Harris $id = $INPUT->str($param); 2748665d38SAndreas Gohr 286c7843b5Sandi //construct page id from request URI 296c7843b5Sandi if(empty($id) && $conf['userewrite'] == 2){ 3006368e4dSMichael Hamann $request = $_SERVER['REQUEST_URI']; 3106368e4dSMichael Hamann $script = ''; 3206368e4dSMichael Hamann 336c7843b5Sandi //get the script URL 346c7843b5Sandi if($conf['basedir']){ 3581124000Sjan $relpath = ''; 3681124000Sjan if($param != 'id') { 3781124000Sjan $relpath = 'lib/exe/'; 3881124000Sjan } 393009a773SAndreas Gohr $script = $conf['basedir'].$relpath.utf8_basename($_SERVER['SCRIPT_FILENAME']); 407d71d4b7SAndreas Gohr 4106368e4dSMichael Hamann }elseif($_SERVER['PATH_INFO']){ 4206368e4dSMichael Hamann $request = $_SERVER['PATH_INFO']; 4306368e4dSMichael Hamann }elseif($_SERVER['SCRIPT_NAME']){ 4406368e4dSMichael Hamann $script = $_SERVER['SCRIPT_NAME']; 456c7843b5Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 466c7843b5Sandi $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 476c7843b5Sandi $_SERVER['SCRIPT_FILENAME']); 486c7843b5Sandi $script = '/'.$script; 496c7843b5Sandi } 506c7843b5Sandi 5152339126Sandi //clean script and request (fixes a windows problem) 5252339126Sandi $script = preg_replace('/\/\/+/','/',$script); 537d71d4b7SAndreas Gohr $request = preg_replace('/\/\/+/','/',$request); 5452339126Sandi 556c7843b5Sandi //remove script URL and Querystring to gain the id 5652339126Sandi if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 576c7843b5Sandi $id = preg_replace ('/\?.*/','',$match[1]); 586c7843b5Sandi } 596de3759aSAndreas Gohr $id = urldecode($id); 6042905504SAndreas Gohr //strip leading slashes 6142905504SAndreas Gohr $id = preg_replace('!^/+!','',$id); 626c7843b5Sandi } 63671a58a6SGuy Brand 64671a58a6SGuy Brand // Namespace autolinking from URL 65b6084253SAndreas Gohr if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ 66103c256aSChris Smith if(page_exists($id.$conf['start'])){ 67671a58a6SGuy Brand // start page inside namespace 68671a58a6SGuy Brand $id = $id.$conf['start']; 69103c256aSChris Smith }elseif(page_exists($id.noNS(cleanID($id)))){ 70671a58a6SGuy Brand // page named like the NS inside the NS 71671a58a6SGuy Brand $id = $id.noNS(cleanID($id)); 72103c256aSChris Smith }elseif(page_exists($id)){ 73671a58a6SGuy Brand // page like namespace exists 747a42ac9eSBen Coburn $id = substr($id,0,-1); 75671a58a6SGuy Brand }else{ 76671a58a6SGuy Brand // fall back to default 77671a58a6SGuy Brand $id = $id.$conf['start']; 78671a58a6SGuy Brand } 794e90caaaSMichael Hamann if (isset($ACT) && $ACT === 'show') send_redirect(wl($id,'',true)); 80671a58a6SGuy Brand } 81671a58a6SGuy Brand 8242905504SAndreas Gohr if($clean) $id = cleanID($id); 830868021bSAndreas Gohr if(empty($id) && $param=='id') $id = $conf['start']; 846c7843b5Sandi 856c7843b5Sandi return $id; 866c7843b5Sandi} 87b625487dSandi 88b625487dSandi/** 89b625487dSandi * Remove unwanted chars from ID 90b625487dSandi * 91b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are 92b625487dSandi * converted to unaccented ones 93b625487dSandi * 94b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 956e0cc83aSchris * @param string $raw_id The pageid to clean 968a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 97b625487dSandi */ 98c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){ 99b625487dSandi global $conf; 1004b5db43bSjoe.lapp static $sepcharpat = null; 1014b5db43bSjoe.lapp 102dc2c0e04Schris global $cache_cleanid; 103dc2c0e04Schris $cache = & $cache_cleanid; 1046e0cc83aSchris 1056e0cc83aSchris // check if it's already in the memory cache 1063a50618cSgweissbach if (isset($cache[(string)$raw_id])) { 1073a50618cSgweissbach return $cache[(string)$raw_id]; 1086e0cc83aSchris } 1096e0cc83aSchris 1104b5db43bSjoe.lapp $sepchar = $conf['sepchar']; 1114b5db43bSjoe.lapp if($sepcharpat == null) // build string only once to save clock cycles 1124b5db43bSjoe.lapp $sepcharpat = '#\\'.$sepchar.'+#'; 1134b5db43bSjoe.lapp 1143a50618cSgweissbach $id = trim((string)$raw_id); 115b625487dSandi $id = utf8_strtolower($id); 116b625487dSandi 117b625487dSandi //alternative namespace seperator 118b625487dSandi if($conf['useslash']){ 1193755fc25STom N Harris $id = strtr($id,';/','::'); 120b625487dSandi }else{ 1213755fc25STom N Harris $id = strtr($id,';/',':'.$sepchar); 122b625487dSandi } 123b625487dSandi 1248a831f2bSAndreas Gohr if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 1258a831f2bSAndreas Gohr if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 126b625487dSandi 127b625487dSandi //remove specials 128ad81d431SAndreas Gohr $id = utf8_stripspecials($id,$sepchar,'\*'); 129b625487dSandi 1308a831f2bSAndreas Gohr if($ascii) $id = utf8_strip($id); 1318a831f2bSAndreas Gohr 132b625487dSandi //clean up 1334b5db43bSjoe.lapp $id = preg_replace($sepcharpat,$sepchar,$id); 134b625487dSandi $id = preg_replace('#:+#',':',$id); 1353543c6deSAndreas Gohr $id = trim($id,':._-'); 136b625487dSandi $id = preg_replace('#:[:\._\-]+#',':',$id); 137b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#',':',$id); 138b625487dSandi 1393a50618cSgweissbach $cache[(string)$raw_id] = $id; 140b625487dSandi return($id); 141b625487dSandi} 142b625487dSandi 143b625487dSandi/** 144b625487dSandi * Return namespacepart of a wiki ID 145b625487dSandi * 146b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 147b625487dSandi */ 148b625487dSandifunction getNS($id){ 1493a50618cSgweissbach $pos = strrpos((string)$id,':'); 150c4e0e4a1SAndreas Gohr if($pos!==false){ 1513a50618cSgweissbach return substr((string)$id,0,$pos); 152b625487dSandi } 153b625487dSandi return false; 154b625487dSandi} 155b625487dSandi 156b625487dSandi/** 157b625487dSandi * Returns the ID without the namespace 158b625487dSandi * 159b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 160b625487dSandi */ 161b625487dSandifunction noNS($id) { 1622844584fSBen Coburn $pos = strrpos($id, ':'); 1632844584fSBen Coburn if ($pos!==false) { 1642844584fSBen Coburn return substr($id, $pos+1); 1652844584fSBen Coburn } else { 1662844584fSBen Coburn return $id; 1672844584fSBen Coburn } 1681a84a0f3SAnika Henke} 1691a84a0f3SAnika Henke 1701a84a0f3SAnika Henke/** 1711a84a0f3SAnika Henke * Returns the current namespace 1721a84a0f3SAnika Henke * 1731a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 1741a84a0f3SAnika Henke */ 1751a84a0f3SAnika Henkefunction curNS($id) { 1761a84a0f3SAnika Henke return noNS(getNS($id)); 1771a84a0f3SAnika Henke} 1781a84a0f3SAnika Henke 1791a84a0f3SAnika Henke/** 1801a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 1811a84a0f3SAnika Henke * 1821a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 1831a84a0f3SAnika Henke */ 1841a84a0f3SAnika Henkefunction noNSorNS($id) { 1851a84a0f3SAnika Henke global $conf; 1861a84a0f3SAnika Henke 1871a84a0f3SAnika Henke $p = noNS($id); 1889708106bSAdrian Lang if ($p == $conf['start'] || $p == false) { 1891a84a0f3SAnika Henke $p = curNS($id); 1901a84a0f3SAnika Henke if ($p == false) { 1919708106bSAdrian Lang return $conf['start']; 1921a84a0f3SAnika Henke } 1931a84a0f3SAnika Henke } 1941a84a0f3SAnika Henke return $p; 195b625487dSandi} 1964ceab83fSAndreas Gohr 1974ceab83fSAndreas Gohr/** 1984ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 1994ceab83fSAndreas Gohr * 2004ceab83fSAndreas Gohr * @param string $title The headline title 201c857afe0SMichael Hamann * @param array|bool $check Existing IDs (title => number) 202c857afe0SMichael Hamann * @return string the title 2034ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2044ceab83fSAndreas Gohr */ 205443d207bSAndreas Gohrfunction sectionID($title,&$check) { 206de9114eaSAnika Henke $title = str_replace(array(':','.'),'',cleanID($title)); 207de9114eaSAnika Henke $new = ltrim($title,'0123456789_-'); 2084ceab83fSAndreas Gohr if(empty($new)){ 2094ceab83fSAndreas Gohr $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline 2104ceab83fSAndreas Gohr }else{ 2114ceab83fSAndreas Gohr $title = $new; 2124ceab83fSAndreas Gohr } 2134ceab83fSAndreas Gohr 214443d207bSAndreas Gohr if(is_array($check)){ 2154ceab83fSAndreas Gohr // make sure tiles are unique 21601e3159cSChris Tapp if (!array_key_exists ($title,$check)) { 21701e3159cSChris Tapp $check[$title] = 0; 21801e3159cSChris Tapp } else { 21901e3159cSChris Tapp $title .= ++ $check[$title]; 2204ceab83fSAndreas Gohr } 2214ceab83fSAndreas Gohr } 2224ceab83fSAndreas Gohr 2234ceab83fSAndreas Gohr return $title; 2244ceab83fSAndreas Gohr} 2254ceab83fSAndreas Gohr 226b625487dSandi 227b625487dSandi/** 228103c256aSChris Smith * Wiki page existence check 229103c256aSChris Smith * 230103c256aSChris Smith * parameters as for wikiFN 231103c256aSChris Smith * 232103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk> 233103c256aSChris Smith */ 234*90bee600Slispsfunction page_exists($id,$rev='',$clean=true, $data_at=false) { 235*90bee600Slisps if($rev !== '' && $date_at) { 236*90bee600Slisps $pagelog = new PageChangeLog($page); 237*90bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 238*90bee600Slisps if($pagelog_rev !== false) 239*90bee600Slisps $rev = $pagelog_rev; 240*90bee600Slisps } 241103c256aSChris Smith return @file_exists(wikiFN($id,$rev,$clean)); 242103c256aSChris Smith} 243103c256aSChris Smith 244103c256aSChris Smith/** 245103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 246b625487dSandi * 247b625487dSandi * The filename is URL encoded to protect Unicode chars 248b625487dSandi * 249103c256aSChris Smith * @param $raw_id string id of wikipage 250103c256aSChris Smith * @param $rev string page revision, empty string for current 251103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 252103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 253103c256aSChris Smith * 254b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 255b625487dSandi */ 2566e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 257b625487dSandi global $conf; 2586e0cc83aSchris 259dc2c0e04Schris global $cache_wikifn; 260dc2c0e04Schris $cache = & $cache_wikifn; 261dc2c0e04Schris 2626e0cc83aSchris if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { 2636e0cc83aSchris return $cache[$raw_id][$rev]; 2646e0cc83aSchris } 2656e0cc83aSchris 2666e0cc83aSchris $id = $raw_id; 2676e0cc83aSchris 2680d8ea614Schris if ($clean) $id = cleanID($id); 269b625487dSandi $id = str_replace(':','/',$id); 270b625487dSandi if(empty($rev)){ 271b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 272b625487dSandi }else{ 273b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 274ff3ed99fSmarcel if($conf['compression']){ 275ff3ed99fSmarcel //test for extensions here, we want to read both compressions 276d8186216SBen Coburn if (@file_exists($fn . '.gz')){ 277b625487dSandi $fn .= '.gz'; 278d8186216SBen Coburn }else if(@file_exists($fn . '.bz2')){ 279ff3ed99fSmarcel $fn .= '.bz2'; 280ff3ed99fSmarcel }else{ 281ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 282ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 283ff3ed99fSmarcel } 284b625487dSandi } 285b625487dSandi } 2866e0cc83aSchris 28750602150SBen Coburn if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } 2886e0cc83aSchris $cache[$raw_id][$rev] = $fn; 289b625487dSandi return $fn; 290b625487dSandi} 291b625487dSandi 292b625487dSandi/** 293c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 294c9b4bd1eSBen Coburn * 295c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 296c9b4bd1eSBen Coburn */ 297c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 298c9b4bd1eSBen Coburn global $conf; 299662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 300c9b4bd1eSBen Coburn} 301c9b4bd1eSBen Coburn 302c9b4bd1eSBen Coburn 303c9b4bd1eSBen Coburn/** 3041380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 305b158d625SSteven Danz * 306b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 307b158d625SSteven Danz */ 3081380fc45SAndreas Gohrfunction metaFN($id,$ext){ 309b158d625SSteven Danz global $conf; 310b158d625SSteven Danz $id = cleanID($id); 311b158d625SSteven Danz $id = str_replace(':','/',$id); 3121380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 313b158d625SSteven Danz return $fn; 314b158d625SSteven Danz} 315b158d625SSteven Danz 316b158d625SSteven Danz/** 317e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 318e4f389efSKate Arzamastseva * 319cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 320e4f389efSKate Arzamastseva */ 321e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){ 322e4f389efSKate Arzamastseva global $conf; 323e4f389efSKate Arzamastseva $id = cleanID($id); 324e4f389efSKate Arzamastseva $id = str_replace(':','/',$id); 325e4f389efSKate Arzamastseva $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 326e4f389efSKate Arzamastseva return $fn; 327e4f389efSKate Arzamastseva} 328e4f389efSKate Arzamastseva 329e4f389efSKate Arzamastseva/** 330e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 331e1f3d9e1SEsther Brunner * 332e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 333ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 334e1f3d9e1SEsther Brunner */ 335e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 336ba0267b3SMichael Hamann $basename = metaFN($id, ''); 337ba0267b3SMichael Hamann $files = glob($basename.'.*', GLOB_MARK); 338ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 339ba0267b3SMichael Hamann return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 340e1f3d9e1SEsther Brunner} 341e1f3d9e1SEsther Brunner 342e1f3d9e1SEsther Brunner/** 343b625487dSandi * returns the full path to the mediafile specified by ID 344b625487dSandi * 345b625487dSandi * The filename is URL encoded to protect Unicode chars 346b625487dSandi * 347b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 348cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 349b625487dSandi */ 350e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){ 351b625487dSandi global $conf; 352b625487dSandi $id = cleanID($id); 353b625487dSandi $id = str_replace(':','/',$id); 354e4f389efSKate Arzamastseva if(empty($rev)){ 355b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 356e4f389efSKate Arzamastseva }else{ 357cbe26ad6SKate Arzamastseva $ext = mimetype($id); 3588e69fd30SKate Arzamastseva $name = substr($id,0, -1*strlen($ext[0])-1); 35961f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 360e4f389efSKate Arzamastseva } 361b625487dSandi return $fn; 362b625487dSandi} 363b625487dSandi 364b625487dSandi/** 3652adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 366b625487dSandi * version isn't found the english one is returned 367b625487dSandi * 3682adaf2b8SAndreas Gohr * @param string $id The id of the local file 3692adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 370b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 371b625487dSandi */ 3722adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){ 373b625487dSandi global $conf; 3748819fbf5ShArpanet $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext; 375e6cecb08SMichael Hamann if(!@file_exists($file)){ 3762adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 377b625487dSandi if(!@file_exists($file)){ 378b625487dSandi //fall back to english 3792adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 380b625487dSandi } 381e6cecb08SMichael Hamann } 382b625487dSandi return $file; 383b625487dSandi} 384b625487dSandi 385b625487dSandi/** 386c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 387c4e0e4a1SAndreas Gohr * 388c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 389c4e0e4a1SAndreas Gohr * instead 390c4e0e4a1SAndreas Gohr * 391c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 392c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016 393c4e0e4a1SAndreas Gohr * 394c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 395c4e0e4a1SAndreas Gohr */ 396a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 397c662a49aSAndreas Gohr global $conf; 398c662a49aSAndreas Gohr 399c662a49aSAndreas Gohr // some pre cleaning for useslash: 400c662a49aSAndreas Gohr if($conf['useslash']) $id = str_replace('/',':',$id); 401c662a49aSAndreas Gohr 402c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 403c4e0e4a1SAndreas Gohr // relative stuff 404443e135dSChristopher Smith if($id && $id{0} == '.'){ 405c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 406c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 407c4e0e4a1SAndreas Gohr // prepend the current namespace 408c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 409c4e0e4a1SAndreas Gohr 410c4e0e4a1SAndreas Gohr // cleanup relatives 411c4e0e4a1SAndreas Gohr $result = array(); 412c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 413c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 414c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 415c4e0e4a1SAndreas Gohr if ($dir == '..') { 416c4e0e4a1SAndreas Gohr if (end($result) == '..') { 417c4e0e4a1SAndreas Gohr $result[] = '..'; 418c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 419c4e0e4a1SAndreas Gohr $result[] = '..'; 420c4e0e4a1SAndreas Gohr } 421c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 422c4e0e4a1SAndreas Gohr $result[] = $dir; 423c4e0e4a1SAndreas Gohr } 424c4e0e4a1SAndreas Gohr } 425c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 426c4e0e4a1SAndreas Gohr $id = implode(':', $result); 427c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 428c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 429c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 430c4e0e4a1SAndreas Gohr } 431c4e0e4a1SAndreas Gohr 432a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 433a6ef4796SAndreas Gohr return $id; 434c4e0e4a1SAndreas Gohr} 435c4e0e4a1SAndreas Gohr 436c4e0e4a1SAndreas Gohr/** 437b625487dSandi * Returns a full media id 438b625487dSandi * 439b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 440b625487dSandi */ 441*90bee600Slispsfunction resolve_mediaid($ns,&$page,&$exists,$rev='',$date_at=false){ 442*90bee600Slisps if($rev !== '' && $date_at){ 443*90bee600Slisps $medialog = new MediaChangeLog($media_id); 444*90bee600Slisps $medialog_rev = $medialog->getLastRevisionAt($rev); 445*90bee600Slisps if($medialog_rev !== false) { 446*90bee600Slisps $rev = $medialog_rev; 447*90bee600Slisps } 448*90bee600Slisps } 449c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 4504cd9f791Slisps $file = mediaFN($page,$rev); 451b625487dSandi $exists = @file_exists($file); 452b625487dSandi} 453b625487dSandi 454b625487dSandi/** 455b625487dSandi * Returns a full page id 456b625487dSandi * 457b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 458b625487dSandi */ 459*90bee600Slispsfunction resolve_pageid($ns,&$page,&$exists,$rev='',$date_at=false ){ 460b625487dSandi global $conf; 461c006739eSIzidor Matušov global $ID; 4620b7c14c2Sandi $exists = false; 463b625487dSandi 464c006739eSIzidor Matušov //empty address should point to current page 465c006739eSIzidor Matušov if ($page === "") { 466c006739eSIzidor Matušov $page = $ID; 467c006739eSIzidor Matušov } 468c006739eSIzidor Matušov 469b625487dSandi //keep hashlink if exists then clean both parts 47003c4aec3Schris if (strpos($page,'#')) { 4714b7f9e70STom N Harris list($page,$hash) = explode('#',$page,2); 47203c4aec3Schris } else { 47303c4aec3Schris $hash = ''; 47403c4aec3Schris } 475b625487dSandi $hash = cleanID($hash); 476a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 477b625487dSandi 478a6ef4796SAndreas Gohr // get filename (calls clean itself) 479*90bee600Slisps if($rev !== '' && $date_at) { 480*90bee600Slisps $pagelog = new PageChangeLog($page); 481*90bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 482*90bee600Slisps if($pagelog_rev !== false)//something found 483*90bee600Slisps $rev = $pagelog_rev; 484*90bee600Slisps } 4854cd9f791Slisps $file = wikiFN($page,$rev); 486b625487dSandi 4871179df0eSGuy Brand // if ends with colon or slash we have a namespace link 488b26cdbbeSAdrian Lang if(in_array(substr($page,-1), array(':', ';')) || 489b26cdbbeSAdrian Lang ($conf['useslash'] && substr($page,-1) == '/')){ 490*90bee600Slisps if(page_exists($page.$conf['start'],$rev,true,$date_at)){ 491a6ef4796SAndreas Gohr // start page inside namespace 492a6ef4796SAndreas Gohr $page = $page.$conf['start']; 493a6ef4796SAndreas Gohr $exists = true; 494*90bee600Slisps }elseif(page_exists($page.noNS(cleanID($page)),$rev,true,$date_at)){ 495a6ef4796SAndreas Gohr // page named like the NS inside the NS 496a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 497a6ef4796SAndreas Gohr $exists = true; 498*90bee600Slisps }elseif(page_exists($page,$rev,true,$date_at)){ 499a6ef4796SAndreas Gohr // page like namespace exists 500a6ef4796SAndreas Gohr $page = $page; 501a6ef4796SAndreas Gohr $exists = true; 502a6ef4796SAndreas Gohr }else{ 503a6ef4796SAndreas Gohr // fall back to default 504a6ef4796SAndreas Gohr $page = $page.$conf['start']; 505a6ef4796SAndreas Gohr } 506a6ef4796SAndreas Gohr }else{ 507b625487dSandi //check alternative plural/nonplural form 508b625487dSandi if(!@file_exists($file)){ 509b625487dSandi if( $conf['autoplural'] ){ 510b625487dSandi if(substr($page,-1) == 's'){ 511b625487dSandi $try = substr($page,0,-1); 512b625487dSandi }else{ 513b625487dSandi $try = $page.'s'; 514b625487dSandi } 515*90bee600Slisps if(page_exists($try,$rev,true,$date_at)){ 516b625487dSandi $page = $try; 517b625487dSandi $exists = true; 518b625487dSandi } 519b625487dSandi } 520b625487dSandi }else{ 521b625487dSandi $exists = true; 522b625487dSandi } 523a6ef4796SAndreas Gohr } 524a6ef4796SAndreas Gohr 525a6ef4796SAndreas Gohr // now make sure we have a clean page 526a6ef4796SAndreas Gohr $page = cleanID($page); 527b625487dSandi 528b625487dSandi //add hash if any 529b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 530b625487dSandi} 531b625487dSandi 53298407a7aSandi/** 53398407a7aSandi * Returns the name of a cachefile from given data 53498407a7aSandi * 53598407a7aSandi * The needed directory is created by this function! 53698407a7aSandi * 53798407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 53898407a7aSandi * 53998407a7aSandi * @param string $data This data is used to create a unique md5 name 54098407a7aSandi * @param string $ext This is appended to the filename if given 54198407a7aSandi * @return string The filename of the cachefile 54298407a7aSandi */ 54398407a7aSandifunction getCacheName($data,$ext=''){ 54498407a7aSandi global $conf; 54598407a7aSandi $md5 = md5($data); 54698407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 54798407a7aSandi io_makeFileDir($file); 54898407a7aSandi return $file; 54998407a7aSandi} 55098407a7aSandi 5510dc92c6fSAndreas Gohr/** 5520dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 5530dc92c6fSAndreas Gohr * 5540dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5550dc92c6fSAndreas Gohr */ 5560dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 5578449cc9dSDominik Eckelmann $data = array( 5588449cc9dSDominik Eckelmann 'id' => $id, 5598449cc9dSDominik Eckelmann 'hidden' => false 5608449cc9dSDominik Eckelmann ); 561fb55b51eSDominik Eckelmann trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 562fb55b51eSDominik Eckelmann return $data['hidden']; 5630dc92c6fSAndreas Gohr} 564fb55b51eSDominik Eckelmann 565fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) { 566fb55b51eSDominik Eckelmann global $conf; 567fb55b51eSDominik Eckelmann global $ACT; 568fb55b51eSDominik Eckelmann 569fb55b51eSDominik Eckelmann if ($data['hidden']) return; 570fb55b51eSDominik Eckelmann if(empty($conf['hidepages'])) return; 571fb55b51eSDominik Eckelmann if($ACT == 'admin') return; 572fb55b51eSDominik Eckelmann 573fb55b51eSDominik Eckelmann if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){ 574fb55b51eSDominik Eckelmann $data['hidden'] = true; 575fb55b51eSDominik Eckelmann } 5760dc92c6fSAndreas Gohr} 5770dc92c6fSAndreas Gohr 5780dc92c6fSAndreas Gohr/** 5790dc92c6fSAndreas Gohr * Reverse of isHiddenPage 5800dc92c6fSAndreas Gohr * 5810dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5820dc92c6fSAndreas Gohr */ 5830dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 5840dc92c6fSAndreas Gohr return !isHiddenPage($id); 5850dc92c6fSAndreas Gohr} 5860dc92c6fSAndreas Gohr 5875b75cd1fSAdrian Lang/** 5885b75cd1fSAdrian Lang * Format an id for output to a user 5895b75cd1fSAdrian Lang * 5905b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 5915b75cd1fSAdrian Lang * “*”. Output is escaped. 5925b75cd1fSAdrian Lang * 5935b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 5945b75cd1fSAdrian Lang */ 5950ac9a84dSoliver 5965b75cd1fSAdrian Langfunction prettyprint_id($id) { 5975b75cd1fSAdrian Lang if (!$id || $id === ':') { 5985b75cd1fSAdrian Lang return '*'; 5995b75cd1fSAdrian Lang } 6005b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 6015b75cd1fSAdrian Lang $id .= '*'; 6025b75cd1fSAdrian Lang } 6035b75cd1fSAdrian Lang return hsc($id); 6045b75cd1fSAdrian Lang} 605f03fd957SAndreas Gohr 606f03fd957SAndreas Gohr/** 607f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 608f03fd957SAndreas Gohr * 609f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 610f03fd957SAndreas Gohr * 611f03fd957SAndreas Gohr * When the second parameter is true the string will 612f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 613f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 614f03fd957SAndreas Gohr * same string (default is true) 615f03fd957SAndreas Gohr * 616f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 617f03fd957SAndreas Gohr * @see urlencode 618f03fd957SAndreas Gohr */ 619f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){ 620f03fd957SAndreas Gohr global $conf; 621f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 622f03fd957SAndreas Gohr 623f03fd957SAndreas Gohr if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 624f03fd957SAndreas Gohr return $file; 625f03fd957SAndreas Gohr } 626f03fd957SAndreas Gohr 627f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 628f03fd957SAndreas Gohr return SafeFN::encode($file); 629f03fd957SAndreas Gohr } 630f03fd957SAndreas Gohr 631f03fd957SAndreas Gohr $file = urlencode($file); 632f03fd957SAndreas Gohr $file = str_replace('%2F','/',$file); 633f03fd957SAndreas Gohr return $file; 634f03fd957SAndreas Gohr} 635f03fd957SAndreas Gohr 636f03fd957SAndreas Gohr/** 637f03fd957SAndreas Gohr * Decode a filename back to UTF-8 638f03fd957SAndreas Gohr * 639f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 640f03fd957SAndreas Gohr * 641f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 642f03fd957SAndreas Gohr * @see urldecode 643f03fd957SAndreas Gohr */ 644f03fd957SAndreas Gohrfunction utf8_decodeFN($file){ 645f03fd957SAndreas Gohr global $conf; 646f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 647f03fd957SAndreas Gohr 648f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 649f03fd957SAndreas Gohr return SafeFN::decode($file); 650f03fd957SAndreas Gohr } 651f03fd957SAndreas Gohr 652f03fd957SAndreas Gohr return urldecode($file); 653f03fd957SAndreas Gohr} 654f03fd957SAndreas Gohr 655e66d3e6dSAndreas Gohr/** 656e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any 657e66d3e6dSAndreas Gohr * higher namespace 658e66d3e6dSAndreas Gohr * 659e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well 660e66d3e6dSAndreas Gohr * 661e66d3e6dSAndreas Gohr * @todo add event hook 662e66d3e6dSAndreas Gohr * @param string $page the pagename you're looking for 663e66d3e6dSAndreas Gohr * @return string|false the full page id of the found page, false if any 664e66d3e6dSAndreas Gohr */ 665e66d3e6dSAndreas Gohrfunction page_findnearest($page){ 666c786a1b6SAnika Henke if (!$page) return false; 667e66d3e6dSAndreas Gohr global $ID; 668e66d3e6dSAndreas Gohr 669e66d3e6dSAndreas Gohr $ns = $ID; 670e66d3e6dSAndreas Gohr do { 671e66d3e6dSAndreas Gohr $ns = getNS($ns); 672e66d3e6dSAndreas Gohr $pageid = ltrim("$ns:$page",':'); 673e66d3e6dSAndreas Gohr if(page_exists($pageid)){ 674e66d3e6dSAndreas Gohr return $pageid; 675e66d3e6dSAndreas Gohr } 676e66d3e6dSAndreas Gohr } while($ns); 677e66d3e6dSAndreas Gohr 678e66d3e6dSAndreas Gohr return false; 679e66d3e6dSAndreas Gohr} 680