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){ 22*7d01a0eaSTom N Harris global $INPUT; 236c7843b5Sandi global $conf; 246c7843b5Sandi 25*7d01a0eaSTom N Harris $id = $INPUT->str($param); 2648665d38SAndreas Gohr 276c7843b5Sandi //construct page id from request URI 286c7843b5Sandi if(empty($id) && $conf['userewrite'] == 2){ 2906368e4dSMichael Hamann $request = $_SERVER['REQUEST_URI']; 3006368e4dSMichael Hamann $script = ''; 3106368e4dSMichael Hamann 326c7843b5Sandi //get the script URL 336c7843b5Sandi if($conf['basedir']){ 3481124000Sjan $relpath = ''; 3581124000Sjan if($param != 'id') { 3681124000Sjan $relpath = 'lib/exe/'; 3781124000Sjan } 3881124000Sjan $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']); 397d71d4b7SAndreas Gohr 4006368e4dSMichael Hamann }elseif($_SERVER['PATH_INFO']){ 4106368e4dSMichael Hamann $request = $_SERVER['PATH_INFO']; 4206368e4dSMichael Hamann }elseif($_SERVER['SCRIPT_NAME']){ 4306368e4dSMichael Hamann $script = $_SERVER['SCRIPT_NAME']; 446c7843b5Sandi }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){ 456c7843b5Sandi $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','', 466c7843b5Sandi $_SERVER['SCRIPT_FILENAME']); 476c7843b5Sandi $script = '/'.$script; 486c7843b5Sandi } 496c7843b5Sandi 5052339126Sandi //clean script and request (fixes a windows problem) 5152339126Sandi $script = preg_replace('/\/\/+/','/',$script); 527d71d4b7SAndreas Gohr $request = preg_replace('/\/\/+/','/',$request); 5352339126Sandi 546c7843b5Sandi //remove script URL and Querystring to gain the id 5552339126Sandi if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 566c7843b5Sandi $id = preg_replace ('/\?.*/','',$match[1]); 576c7843b5Sandi } 586de3759aSAndreas Gohr $id = urldecode($id); 5942905504SAndreas Gohr //strip leading slashes 6042905504SAndreas Gohr $id = preg_replace('!^/+!','',$id); 616c7843b5Sandi } 62671a58a6SGuy Brand 63671a58a6SGuy Brand // Namespace autolinking from URL 64b6084253SAndreas Gohr if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ 65103c256aSChris Smith if(page_exists($id.$conf['start'])){ 66671a58a6SGuy Brand // start page inside namespace 67671a58a6SGuy Brand $id = $id.$conf['start']; 68103c256aSChris Smith }elseif(page_exists($id.noNS(cleanID($id)))){ 69671a58a6SGuy Brand // page named like the NS inside the NS 70671a58a6SGuy Brand $id = $id.noNS(cleanID($id)); 71103c256aSChris Smith }elseif(page_exists($id)){ 72671a58a6SGuy Brand // page like namespace exists 737a42ac9eSBen Coburn $id = substr($id,0,-1); 74671a58a6SGuy Brand }else{ 75671a58a6SGuy Brand // fall back to default 76671a58a6SGuy Brand $id = $id.$conf['start']; 77671a58a6SGuy Brand } 78397a8c4eSHelmut Tischer send_redirect(wl($id,'',true)); 79671a58a6SGuy Brand } 80671a58a6SGuy Brand 8142905504SAndreas Gohr if($clean) $id = cleanID($id); 820868021bSAndreas Gohr if(empty($id) && $param=='id') $id = $conf['start']; 836c7843b5Sandi 846c7843b5Sandi return $id; 856c7843b5Sandi} 86b625487dSandi 87b625487dSandi/** 88b625487dSandi * Remove unwanted chars from ID 89b625487dSandi * 90b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are 91b625487dSandi * converted to unaccented ones 92b625487dSandi * 93b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 946e0cc83aSchris * @param string $raw_id The pageid to clean 958a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 963543c6deSAndreas Gohr * @param boolean $media DEPRECATED 97b625487dSandi */ 9863b0c1a7SGina Haeussgefunction cleanID($raw_id,$ascii=false,$media=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 $id = strtr($id,';',':'); 119b625487dSandi if($conf['useslash']){ 120b625487dSandi $id = strtr($id,'/',':'); 121b625487dSandi }else{ 1224eeffcd2SAndreas Gohr $id = strtr($id,'/',$sepchar); 123b625487dSandi } 124b625487dSandi 1258a831f2bSAndreas Gohr if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 1268a831f2bSAndreas Gohr if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 127b625487dSandi 128b625487dSandi //remove specials 129ad81d431SAndreas Gohr $id = utf8_stripspecials($id,$sepchar,'\*'); 130b625487dSandi 1318a831f2bSAndreas Gohr if($ascii) $id = utf8_strip($id); 1328a831f2bSAndreas Gohr 133b625487dSandi //clean up 1344b5db43bSjoe.lapp $id = preg_replace($sepcharpat,$sepchar,$id); 135b625487dSandi $id = preg_replace('#:+#',':',$id); 1363543c6deSAndreas Gohr $id = trim($id,':._-'); 137b625487dSandi $id = preg_replace('#:[:\._\-]+#',':',$id); 138b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#',':',$id); 139b625487dSandi 1403a50618cSgweissbach $cache[(string)$raw_id] = $id; 141b625487dSandi return($id); 142b625487dSandi} 143b625487dSandi 144b625487dSandi/** 145b625487dSandi * Return namespacepart of a wiki ID 146b625487dSandi * 147b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 148b625487dSandi */ 149b625487dSandifunction getNS($id){ 1503a50618cSgweissbach $pos = strrpos((string)$id,':'); 151c4e0e4a1SAndreas Gohr if($pos!==false){ 1523a50618cSgweissbach return substr((string)$id,0,$pos); 153b625487dSandi } 154b625487dSandi return false; 155b625487dSandi} 156b625487dSandi 157b625487dSandi/** 158b625487dSandi * Returns the ID without the namespace 159b625487dSandi * 160b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 161b625487dSandi */ 162b625487dSandifunction noNS($id) { 1632844584fSBen Coburn $pos = strrpos($id, ':'); 1642844584fSBen Coburn if ($pos!==false) { 1652844584fSBen Coburn return substr($id, $pos+1); 1662844584fSBen Coburn } else { 1672844584fSBen Coburn return $id; 1682844584fSBen Coburn } 1691a84a0f3SAnika Henke} 1701a84a0f3SAnika Henke 1711a84a0f3SAnika Henke/** 1721a84a0f3SAnika Henke * Returns the current namespace 1731a84a0f3SAnika Henke * 1741a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 1751a84a0f3SAnika Henke */ 1761a84a0f3SAnika Henkefunction curNS($id) { 1771a84a0f3SAnika Henke return noNS(getNS($id)); 1781a84a0f3SAnika Henke} 1791a84a0f3SAnika Henke 1801a84a0f3SAnika Henke/** 1811a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 1821a84a0f3SAnika Henke * 1831a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 1841a84a0f3SAnika Henke */ 1851a84a0f3SAnika Henkefunction noNSorNS($id) { 1861a84a0f3SAnika Henke global $conf; 1871a84a0f3SAnika Henke 1881a84a0f3SAnika Henke $p = noNS($id); 1899708106bSAdrian Lang if ($p == $conf['start'] || $p == false) { 1901a84a0f3SAnika Henke $p = curNS($id); 1911a84a0f3SAnika Henke if ($p == false) { 1929708106bSAdrian Lang return $conf['start']; 1931a84a0f3SAnika Henke } 1941a84a0f3SAnika Henke } 1951a84a0f3SAnika Henke return $p; 196b625487dSandi} 1974ceab83fSAndreas Gohr 1984ceab83fSAndreas Gohr/** 1994ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 2004ceab83fSAndreas Gohr * 2014ceab83fSAndreas Gohr * @param string $title The headline title 20201e3159cSChris Tapp * @param array $check Existing IDs (title => number) 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 */ 234103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) { 235103c256aSChris Smith return @file_exists(wikiFN($id,$rev,$clean)); 236103c256aSChris Smith} 237103c256aSChris Smith 238103c256aSChris Smith/** 239103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 240b625487dSandi * 241b625487dSandi * The filename is URL encoded to protect Unicode chars 242b625487dSandi * 243103c256aSChris Smith * @param $raw_id string id of wikipage 244103c256aSChris Smith * @param $rev string page revision, empty string for current 245103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 246103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 247103c256aSChris Smith * 248b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 249b625487dSandi */ 2506e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 251b625487dSandi global $conf; 2526e0cc83aSchris 253dc2c0e04Schris global $cache_wikifn; 254dc2c0e04Schris $cache = & $cache_wikifn; 255dc2c0e04Schris 2566e0cc83aSchris if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { 2576e0cc83aSchris return $cache[$raw_id][$rev]; 2586e0cc83aSchris } 2596e0cc83aSchris 2606e0cc83aSchris $id = $raw_id; 2616e0cc83aSchris 2620d8ea614Schris if ($clean) $id = cleanID($id); 263b625487dSandi $id = str_replace(':','/',$id); 264b625487dSandi if(empty($rev)){ 265b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 266b625487dSandi }else{ 267b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 268ff3ed99fSmarcel if($conf['compression']){ 269ff3ed99fSmarcel //test for extensions here, we want to read both compressions 270d8186216SBen Coburn if (@file_exists($fn . '.gz')){ 271b625487dSandi $fn .= '.gz'; 272d8186216SBen Coburn }else if(@file_exists($fn . '.bz2')){ 273ff3ed99fSmarcel $fn .= '.bz2'; 274ff3ed99fSmarcel }else{ 275ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 276ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 277ff3ed99fSmarcel } 278b625487dSandi } 279b625487dSandi } 2806e0cc83aSchris 28150602150SBen Coburn if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } 2826e0cc83aSchris $cache[$raw_id][$rev] = $fn; 283b625487dSandi return $fn; 284b625487dSandi} 285b625487dSandi 286b625487dSandi/** 287c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 288c9b4bd1eSBen Coburn * 289c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 290c9b4bd1eSBen Coburn */ 291c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 292c9b4bd1eSBen Coburn global $conf; 293662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 294c9b4bd1eSBen Coburn} 295c9b4bd1eSBen Coburn 296c9b4bd1eSBen Coburn 297c9b4bd1eSBen Coburn/** 2981380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 299b158d625SSteven Danz * 300b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 301b158d625SSteven Danz */ 3021380fc45SAndreas Gohrfunction metaFN($id,$ext){ 303b158d625SSteven Danz global $conf; 304b158d625SSteven Danz $id = cleanID($id); 305b158d625SSteven Danz $id = str_replace(':','/',$id); 3061380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 307b158d625SSteven Danz return $fn; 308b158d625SSteven Danz} 309b158d625SSteven Danz 310b158d625SSteven Danz/** 311e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 312e4f389efSKate Arzamastseva * 313cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 314e4f389efSKate Arzamastseva */ 315e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){ 316e4f389efSKate Arzamastseva global $conf; 317e4f389efSKate Arzamastseva $id = cleanID($id); 318e4f389efSKate Arzamastseva $id = str_replace(':','/',$id); 319e4f389efSKate Arzamastseva $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 320e4f389efSKate Arzamastseva return $fn; 321e4f389efSKate Arzamastseva} 322e4f389efSKate Arzamastseva 323e4f389efSKate Arzamastseva/** 324e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 325e1f3d9e1SEsther Brunner * 326e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 327ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 328e1f3d9e1SEsther Brunner */ 329e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 330ba0267b3SMichael Hamann $basename = metaFN($id, ''); 331ba0267b3SMichael Hamann $files = glob($basename.'.*', GLOB_MARK); 332ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 333ba0267b3SMichael Hamann return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 334e1f3d9e1SEsther Brunner} 335e1f3d9e1SEsther Brunner 336e1f3d9e1SEsther Brunner/** 337b625487dSandi * returns the full path to the mediafile specified by ID 338b625487dSandi * 339b625487dSandi * The filename is URL encoded to protect Unicode chars 340b625487dSandi * 341b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 342cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 343b625487dSandi */ 344e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){ 345b625487dSandi global $conf; 346b625487dSandi $id = cleanID($id); 347b625487dSandi $id = str_replace(':','/',$id); 348e4f389efSKate Arzamastseva if(empty($rev)){ 349b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 350e4f389efSKate Arzamastseva }else{ 351cbe26ad6SKate Arzamastseva $ext = mimetype($id); 3528e69fd30SKate Arzamastseva $name = substr($id,0, -1*strlen($ext[0])-1); 35361f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 354e4f389efSKate Arzamastseva } 355b625487dSandi return $fn; 356b625487dSandi} 357b625487dSandi 358b625487dSandi/** 3592adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 360b625487dSandi * version isn't found the english one is returned 361b625487dSandi * 3622adaf2b8SAndreas Gohr * @param string $id The id of the local file 3632adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 364b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 365b625487dSandi */ 3662adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){ 367b625487dSandi global $conf; 3682adaf2b8SAndreas Gohr $file = DOKU_CONF.'/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 369e6cecb08SMichael Hamann if(!@file_exists($file)){ 3702adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 371b625487dSandi if(!@file_exists($file)){ 372b625487dSandi //fall back to english 3732adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 374b625487dSandi } 375e6cecb08SMichael Hamann } 376b625487dSandi return $file; 377b625487dSandi} 378b625487dSandi 379b625487dSandi/** 380c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 381c4e0e4a1SAndreas Gohr * 382c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 383c4e0e4a1SAndreas Gohr * instead 384c4e0e4a1SAndreas Gohr * 385c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 386c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016 387c4e0e4a1SAndreas Gohr * 388c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 389c4e0e4a1SAndreas Gohr */ 390a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 391c662a49aSAndreas Gohr global $conf; 392c662a49aSAndreas Gohr 393c662a49aSAndreas Gohr // some pre cleaning for useslash: 394c662a49aSAndreas Gohr if($conf['useslash']) $id = str_replace('/',':',$id); 395c662a49aSAndreas Gohr 396c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 397c4e0e4a1SAndreas Gohr // relative stuff 398c4e0e4a1SAndreas Gohr if($id{0} == '.'){ 399c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 400c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 401c4e0e4a1SAndreas Gohr // prepend the current namespace 402c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 403c4e0e4a1SAndreas Gohr 404c4e0e4a1SAndreas Gohr // cleanup relatives 405c4e0e4a1SAndreas Gohr $result = array(); 406c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 407c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 408c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 409c4e0e4a1SAndreas Gohr if ($dir == '..') { 410c4e0e4a1SAndreas Gohr if (end($result) == '..') { 411c4e0e4a1SAndreas Gohr $result[] = '..'; 412c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 413c4e0e4a1SAndreas Gohr $result[] = '..'; 414c4e0e4a1SAndreas Gohr } 415c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 416c4e0e4a1SAndreas Gohr $result[] = $dir; 417c4e0e4a1SAndreas Gohr } 418c4e0e4a1SAndreas Gohr } 419c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 420c4e0e4a1SAndreas Gohr $id = implode(':', $result); 421c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 422c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 423c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 424c4e0e4a1SAndreas Gohr } 425c4e0e4a1SAndreas Gohr 426a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 427a6ef4796SAndreas Gohr return $id; 428c4e0e4a1SAndreas Gohr} 429c4e0e4a1SAndreas Gohr 430c4e0e4a1SAndreas Gohr/** 431b625487dSandi * Returns a full media id 432b625487dSandi * 433b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 434b625487dSandi */ 43537e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){ 436c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 437b625487dSandi $file = mediaFN($page); 438b625487dSandi $exists = @file_exists($file); 439b625487dSandi} 440b625487dSandi 441b625487dSandi/** 442b625487dSandi * Returns a full page id 443b625487dSandi * 444b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 445b625487dSandi */ 44637e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){ 447b625487dSandi global $conf; 448c006739eSIzidor Matušov global $ID; 4490b7c14c2Sandi $exists = false; 450b625487dSandi 451c006739eSIzidor Matušov //empty address should point to current page 452c006739eSIzidor Matušov if ($page === "") { 453c006739eSIzidor Matušov $page = $ID; 454c006739eSIzidor Matušov } 455c006739eSIzidor Matušov 456b625487dSandi //keep hashlink if exists then clean both parts 45703c4aec3Schris if (strpos($page,'#')) { 4584b7f9e70STom N Harris list($page,$hash) = explode('#',$page,2); 45903c4aec3Schris } else { 46003c4aec3Schris $hash = ''; 46103c4aec3Schris } 462b625487dSandi $hash = cleanID($hash); 463a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 464b625487dSandi 465a6ef4796SAndreas Gohr // get filename (calls clean itself) 466b625487dSandi $file = wikiFN($page); 467b625487dSandi 4681179df0eSGuy Brand // if ends with colon or slash we have a namespace link 469b26cdbbeSAdrian Lang if(in_array(substr($page,-1), array(':', ';')) || 470b26cdbbeSAdrian Lang ($conf['useslash'] && substr($page,-1) == '/')){ 471103c256aSChris Smith if(page_exists($page.$conf['start'])){ 472a6ef4796SAndreas Gohr // start page inside namespace 473a6ef4796SAndreas Gohr $page = $page.$conf['start']; 474a6ef4796SAndreas Gohr $exists = true; 475103c256aSChris Smith }elseif(page_exists($page.noNS(cleanID($page)))){ 476a6ef4796SAndreas Gohr // page named like the NS inside the NS 477a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 478a6ef4796SAndreas Gohr $exists = true; 479103c256aSChris Smith }elseif(page_exists($page)){ 480a6ef4796SAndreas Gohr // page like namespace exists 481a6ef4796SAndreas Gohr $page = $page; 482a6ef4796SAndreas Gohr $exists = true; 483a6ef4796SAndreas Gohr }else{ 484a6ef4796SAndreas Gohr // fall back to default 485a6ef4796SAndreas Gohr $page = $page.$conf['start']; 486a6ef4796SAndreas Gohr } 487a6ef4796SAndreas Gohr }else{ 488b625487dSandi //check alternative plural/nonplural form 489b625487dSandi if(!@file_exists($file)){ 490b625487dSandi if( $conf['autoplural'] ){ 491b625487dSandi if(substr($page,-1) == 's'){ 492b625487dSandi $try = substr($page,0,-1); 493b625487dSandi }else{ 494b625487dSandi $try = $page.'s'; 495b625487dSandi } 496103c256aSChris Smith if(page_exists($try)){ 497b625487dSandi $page = $try; 498b625487dSandi $exists = true; 499b625487dSandi } 500b625487dSandi } 501b625487dSandi }else{ 502b625487dSandi $exists = true; 503b625487dSandi } 504a6ef4796SAndreas Gohr } 505a6ef4796SAndreas Gohr 506a6ef4796SAndreas Gohr // now make sure we have a clean page 507a6ef4796SAndreas Gohr $page = cleanID($page); 508b625487dSandi 509b625487dSandi //add hash if any 510b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 511b625487dSandi} 512b625487dSandi 51398407a7aSandi/** 51498407a7aSandi * Returns the name of a cachefile from given data 51598407a7aSandi * 51698407a7aSandi * The needed directory is created by this function! 51798407a7aSandi * 51898407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 51998407a7aSandi * 52098407a7aSandi * @param string $data This data is used to create a unique md5 name 52198407a7aSandi * @param string $ext This is appended to the filename if given 52298407a7aSandi * @return string The filename of the cachefile 52398407a7aSandi */ 52498407a7aSandifunction getCacheName($data,$ext=''){ 52598407a7aSandi global $conf; 52698407a7aSandi $md5 = md5($data); 52798407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 52898407a7aSandi io_makeFileDir($file); 52998407a7aSandi return $file; 53098407a7aSandi} 53198407a7aSandi 5320dc92c6fSAndreas Gohr/** 5330dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 5340dc92c6fSAndreas Gohr * 5350dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5360dc92c6fSAndreas Gohr */ 5370dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 5380dc92c6fSAndreas Gohr global $conf; 539e6a873d7SMichael Klier global $ACT; 5400dc92c6fSAndreas Gohr if(empty($conf['hidepages'])) return false; 541e6a873d7SMichael Klier if($ACT == 'admin') return false; 5420dc92c6fSAndreas Gohr 5430dc92c6fSAndreas Gohr if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){ 5440dc92c6fSAndreas Gohr return true; 5450dc92c6fSAndreas Gohr } 5460dc92c6fSAndreas Gohr return false; 5470dc92c6fSAndreas Gohr} 5480dc92c6fSAndreas Gohr 5490dc92c6fSAndreas Gohr/** 5500dc92c6fSAndreas Gohr * Reverse of isHiddenPage 5510dc92c6fSAndreas Gohr * 5520dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5530dc92c6fSAndreas Gohr */ 5540dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 5550dc92c6fSAndreas Gohr return !isHiddenPage($id); 5560dc92c6fSAndreas Gohr} 5570dc92c6fSAndreas Gohr 5585b75cd1fSAdrian Lang/** 5595b75cd1fSAdrian Lang * Format an id for output to a user 5605b75cd1fSAdrian Lang * 5615b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 5625b75cd1fSAdrian Lang * “*”. Output is escaped. 5635b75cd1fSAdrian Lang * 5645b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 5655b75cd1fSAdrian Lang */ 5660ac9a84dSoliver 5675b75cd1fSAdrian Langfunction prettyprint_id($id) { 5685b75cd1fSAdrian Lang if (!$id || $id === ':') { 5695b75cd1fSAdrian Lang return '*'; 5705b75cd1fSAdrian Lang } 5715b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 5725b75cd1fSAdrian Lang $id .= '*'; 5735b75cd1fSAdrian Lang } 5745b75cd1fSAdrian Lang return hsc($id); 5755b75cd1fSAdrian Lang} 576f03fd957SAndreas Gohr 577f03fd957SAndreas Gohr/** 578f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 579f03fd957SAndreas Gohr * 580f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 581f03fd957SAndreas Gohr * 582f03fd957SAndreas Gohr * When the second parameter is true the string will 583f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 584f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 585f03fd957SAndreas Gohr * same string (default is true) 586f03fd957SAndreas Gohr * 587f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 588f03fd957SAndreas Gohr * @see urlencode 589f03fd957SAndreas Gohr */ 590f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){ 591f03fd957SAndreas Gohr global $conf; 592f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 593f03fd957SAndreas Gohr 594f03fd957SAndreas Gohr if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 595f03fd957SAndreas Gohr return $file; 596f03fd957SAndreas Gohr } 597f03fd957SAndreas Gohr 598f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 599f03fd957SAndreas Gohr return SafeFN::encode($file); 600f03fd957SAndreas Gohr } 601f03fd957SAndreas Gohr 602f03fd957SAndreas Gohr $file = urlencode($file); 603f03fd957SAndreas Gohr $file = str_replace('%2F','/',$file); 604f03fd957SAndreas Gohr return $file; 605f03fd957SAndreas Gohr} 606f03fd957SAndreas Gohr 607f03fd957SAndreas Gohr/** 608f03fd957SAndreas Gohr * Decode a filename back to UTF-8 609f03fd957SAndreas Gohr * 610f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 611f03fd957SAndreas Gohr * 612f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 613f03fd957SAndreas Gohr * @see urldecode 614f03fd957SAndreas Gohr */ 615f03fd957SAndreas Gohrfunction utf8_decodeFN($file){ 616f03fd957SAndreas Gohr global $conf; 617f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 618f03fd957SAndreas Gohr 619f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 620f03fd957SAndreas Gohr return SafeFN::decode($file); 621f03fd957SAndreas Gohr } 622f03fd957SAndreas Gohr 623f03fd957SAndreas Gohr return urldecode($file); 624f03fd957SAndreas Gohr} 625f03fd957SAndreas Gohr 626