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> 2084657ea2SGerrit Uitslag * 2184657ea2SGerrit Uitslag * @param string $param the $_REQUEST variable name, default 'id' 2284657ea2SGerrit Uitslag * @param bool $clean if true, ID is cleaned 2384657ea2SGerrit Uitslag * @return mixed|string 246c7843b5Sandi */ 2542905504SAndreas Gohrfunction getID($param='id',$clean=true){ 26585bf44eSChristopher Smith /** @var Input $INPUT */ 277d01a0eaSTom N Harris global $INPUT; 286c7843b5Sandi global $conf; 294e90caaaSMichael Hamann global $ACT; 306c7843b5Sandi 317d01a0eaSTom N Harris $id = $INPUT->str($param); 3248665d38SAndreas Gohr 336c7843b5Sandi //construct page id from request URI 346c7843b5Sandi if(empty($id) && $conf['userewrite'] == 2){ 35585bf44eSChristopher Smith $request = $INPUT->server->str('REQUEST_URI'); 3606368e4dSMichael Hamann $script = ''; 3706368e4dSMichael Hamann 386c7843b5Sandi //get the script URL 396c7843b5Sandi if($conf['basedir']){ 4081124000Sjan $relpath = ''; 4181124000Sjan if($param != 'id') { 4281124000Sjan $relpath = 'lib/exe/'; 4381124000Sjan } 44585bf44eSChristopher Smith $script = $conf['basedir'].$relpath.utf8_basename($INPUT->server->str('SCRIPT_FILENAME')); 457d71d4b7SAndreas Gohr 46585bf44eSChristopher Smith }elseif($INPUT->server->str('PATH_INFO')){ 47585bf44eSChristopher Smith $request = $INPUT->server->str('PATH_INFO'); 48585bf44eSChristopher Smith }elseif($INPUT->server->str('SCRIPT_NAME')){ 49585bf44eSChristopher Smith $script = $INPUT->server->str('SCRIPT_NAME'); 50585bf44eSChristopher Smith }elseif($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')){ 51585bf44eSChristopher Smith $script = preg_replace ('/^'.preg_quote($INPUT->server->str('DOCUMENT_ROOT'),'/').'/','', 52585bf44eSChristopher Smith $INPUT->server->str('SCRIPT_FILENAME')); 536c7843b5Sandi $script = '/'.$script; 546c7843b5Sandi } 556c7843b5Sandi 5652339126Sandi //clean script and request (fixes a windows problem) 5752339126Sandi $script = preg_replace('/\/\/+/','/',$script); 587d71d4b7SAndreas Gohr $request = preg_replace('/\/\/+/','/',$request); 5952339126Sandi 606c7843b5Sandi //remove script URL and Querystring to gain the id 6152339126Sandi if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 626c7843b5Sandi $id = preg_replace ('/\?.*/','',$match[1]); 636c7843b5Sandi } 646de3759aSAndreas Gohr $id = urldecode($id); 6542905504SAndreas Gohr //strip leading slashes 6642905504SAndreas Gohr $id = preg_replace('!^/+!','',$id); 676c7843b5Sandi } 68671a58a6SGuy Brand 69671a58a6SGuy Brand // Namespace autolinking from URL 70b6084253SAndreas Gohr if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ 71103c256aSChris Smith if(page_exists($id.$conf['start'])){ 72671a58a6SGuy Brand // start page inside namespace 73671a58a6SGuy Brand $id = $id.$conf['start']; 74103c256aSChris Smith }elseif(page_exists($id.noNS(cleanID($id)))){ 75671a58a6SGuy Brand // page named like the NS inside the NS 76671a58a6SGuy Brand $id = $id.noNS(cleanID($id)); 77103c256aSChris Smith }elseif(page_exists($id)){ 78671a58a6SGuy Brand // page like namespace exists 797a42ac9eSBen Coburn $id = substr($id,0,-1); 80671a58a6SGuy Brand }else{ 81671a58a6SGuy Brand // fall back to default 82671a58a6SGuy Brand $id = $id.$conf['start']; 83671a58a6SGuy Brand } 844e90caaaSMichael Hamann if (isset($ACT) && $ACT === 'show') send_redirect(wl($id,'',true)); 85671a58a6SGuy Brand } 86671a58a6SGuy Brand 8742905504SAndreas Gohr if($clean) $id = cleanID($id); 880868021bSAndreas Gohr if(empty($id) && $param=='id') $id = $conf['start']; 896c7843b5Sandi 906c7843b5Sandi return $id; 916c7843b5Sandi} 92b625487dSandi 93b625487dSandi/** 94b625487dSandi * Remove unwanted chars from ID 95b625487dSandi * 96b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are 97b625487dSandi * converted to unaccented ones 98b625487dSandi * 99b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 1006e0cc83aSchris * @param string $raw_id The pageid to clean 1018a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 102dbf714f7SGerrit Uitslag * @return string cleaned id 103b625487dSandi */ 104c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){ 105b625487dSandi global $conf; 1064b5db43bSjoe.lapp static $sepcharpat = null; 1074b5db43bSjoe.lapp 108dc2c0e04Schris global $cache_cleanid; 109dc2c0e04Schris $cache = & $cache_cleanid; 1106e0cc83aSchris 1116e0cc83aSchris // check if it's already in the memory cache 1123a50618cSgweissbach if (isset($cache[(string)$raw_id])) { 1133a50618cSgweissbach return $cache[(string)$raw_id]; 1146e0cc83aSchris } 1156e0cc83aSchris 1164b5db43bSjoe.lapp $sepchar = $conf['sepchar']; 1174b5db43bSjoe.lapp if($sepcharpat == null) // build string only once to save clock cycles 1184b5db43bSjoe.lapp $sepcharpat = '#\\'.$sepchar.'+#'; 1194b5db43bSjoe.lapp 1203a50618cSgweissbach $id = trim((string)$raw_id); 121b625487dSandi $id = utf8_strtolower($id); 122b625487dSandi 123b625487dSandi //alternative namespace seperator 124b625487dSandi if($conf['useslash']){ 1253755fc25STom N Harris $id = strtr($id,';/','::'); 126b625487dSandi }else{ 1273755fc25STom N Harris $id = strtr($id,';/',':'.$sepchar); 128b625487dSandi } 129b625487dSandi 1308a831f2bSAndreas Gohr if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 1318a831f2bSAndreas Gohr if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 132b625487dSandi 133b625487dSandi //remove specials 134ad81d431SAndreas Gohr $id = utf8_stripspecials($id,$sepchar,'\*'); 135b625487dSandi 1368a831f2bSAndreas Gohr if($ascii) $id = utf8_strip($id); 1378a831f2bSAndreas Gohr 138b625487dSandi //clean up 1394b5db43bSjoe.lapp $id = preg_replace($sepcharpat,$sepchar,$id); 140b625487dSandi $id = preg_replace('#:+#',':',$id); 1413543c6deSAndreas Gohr $id = trim($id,':._-'); 142b625487dSandi $id = preg_replace('#:[:\._\-]+#',':',$id); 143b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#',':',$id); 144b625487dSandi 1453a50618cSgweissbach $cache[(string)$raw_id] = $id; 146b625487dSandi return($id); 147b625487dSandi} 148b625487dSandi 149b625487dSandi/** 150b625487dSandi * Return namespacepart of a wiki ID 151b625487dSandi * 152b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 15315851b98SGerrit Uitslag * 15415851b98SGerrit Uitslag * @param string $id 155ef11fcfcSAndreas Gohr * @return string|bool the namespace part or false if the given ID has no namespace (root) 156b625487dSandi */ 157b625487dSandifunction getNS($id){ 1583a50618cSgweissbach $pos = strrpos((string)$id,':'); 159c4e0e4a1SAndreas Gohr if($pos!==false){ 1603a50618cSgweissbach return substr((string)$id,0,$pos); 161b625487dSandi } 162ef11fcfcSAndreas Gohr return false; 163b625487dSandi} 164b625487dSandi 165b625487dSandi/** 166b625487dSandi * Returns the ID without the namespace 167b625487dSandi * 168b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 16915851b98SGerrit Uitslag * 17015851b98SGerrit Uitslag * @param string $id 17115851b98SGerrit Uitslag * @return string 172b625487dSandi */ 173b625487dSandifunction noNS($id) { 1742844584fSBen Coburn $pos = strrpos($id, ':'); 1752844584fSBen Coburn if ($pos!==false) { 1762844584fSBen Coburn return substr($id, $pos+1); 1772844584fSBen Coburn } else { 1782844584fSBen Coburn return $id; 1792844584fSBen Coburn } 1801a84a0f3SAnika Henke} 1811a84a0f3SAnika Henke 1821a84a0f3SAnika Henke/** 1831a84a0f3SAnika Henke * Returns the current namespace 1841a84a0f3SAnika Henke * 1851a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 18684657ea2SGerrit Uitslag * 18784657ea2SGerrit Uitslag * @param string $id 18884657ea2SGerrit Uitslag * @return string 1891a84a0f3SAnika Henke */ 1901a84a0f3SAnika Henkefunction curNS($id) { 1911a84a0f3SAnika Henke return noNS(getNS($id)); 1921a84a0f3SAnika Henke} 1931a84a0f3SAnika Henke 1941a84a0f3SAnika Henke/** 1951a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 1961a84a0f3SAnika Henke * 1971a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 19884657ea2SGerrit Uitslag * 19984657ea2SGerrit Uitslag * @param string $id 20084657ea2SGerrit Uitslag * @return string 2011a84a0f3SAnika Henke */ 2021a84a0f3SAnika Henkefunction noNSorNS($id) { 2031a84a0f3SAnika Henke global $conf; 2041a84a0f3SAnika Henke 2051a84a0f3SAnika Henke $p = noNS($id); 2069708106bSAdrian Lang if ($p == $conf['start'] || $p == false) { 2071a84a0f3SAnika Henke $p = curNS($id); 2081a84a0f3SAnika Henke if ($p == false) { 2099708106bSAdrian Lang return $conf['start']; 2101a84a0f3SAnika Henke } 2111a84a0f3SAnika Henke } 2121a84a0f3SAnika Henke return $p; 213b625487dSandi} 2144ceab83fSAndreas Gohr 2154ceab83fSAndreas Gohr/** 2164ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 2174ceab83fSAndreas Gohr * 2184ceab83fSAndreas Gohr * @param string $title The headline title 219c857afe0SMichael Hamann * @param array|bool $check Existing IDs (title => number) 220c857afe0SMichael Hamann * @return string the title 22184657ea2SGerrit Uitslag * 2224ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2234ceab83fSAndreas Gohr */ 224443d207bSAndreas Gohrfunction sectionID($title,&$check) { 225de9114eaSAnika Henke $title = str_replace(array(':','.'),'',cleanID($title)); 226de9114eaSAnika Henke $new = ltrim($title,'0123456789_-'); 2274ceab83fSAndreas Gohr if(empty($new)){ 2284ceab83fSAndreas Gohr $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline 2294ceab83fSAndreas Gohr }else{ 2304ceab83fSAndreas Gohr $title = $new; 2314ceab83fSAndreas Gohr } 2324ceab83fSAndreas Gohr 233443d207bSAndreas Gohr if(is_array($check)){ 2344ceab83fSAndreas Gohr // make sure tiles are unique 23501e3159cSChris Tapp if (!array_key_exists ($title,$check)) { 23601e3159cSChris Tapp $check[$title] = 0; 23701e3159cSChris Tapp } else { 23801e3159cSChris Tapp $title .= ++ $check[$title]; 2394ceab83fSAndreas Gohr } 2404ceab83fSAndreas Gohr } 2414ceab83fSAndreas Gohr 2424ceab83fSAndreas Gohr return $title; 2434ceab83fSAndreas Gohr} 2444ceab83fSAndreas Gohr 245b625487dSandi 246b625487dSandi/** 247103c256aSChris Smith * Wiki page existence check 248103c256aSChris Smith * 249103c256aSChris Smith * parameters as for wikiFN 250103c256aSChris Smith * 251103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk> 25284657ea2SGerrit Uitslag * 25384657ea2SGerrit Uitslag * @param string $id page id 25484657ea2SGerrit Uitslag * @param string|int $rev empty or revision timestamp 25584657ea2SGerrit Uitslag * @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well) 25684657ea2SGerrit Uitslag * @return bool exists? 257103c256aSChris Smith */ 258103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) { 259103c256aSChris Smith return @file_exists(wikiFN($id,$rev,$clean)); 260103c256aSChris Smith} 261103c256aSChris Smith 262103c256aSChris Smith/** 263103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 264b625487dSandi * 265b625487dSandi * The filename is URL encoded to protect Unicode chars 266b625487dSandi * 267103c256aSChris Smith * @param $raw_id string id of wikipage 268*e0c26282SGerrit Uitslag * @param $rev int|string page revision, empty string for current 269103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 270103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 271dbf714f7SGerrit Uitslag * @return string full path 272103c256aSChris Smith * 273b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 274b625487dSandi */ 2756e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 276b625487dSandi global $conf; 2776e0cc83aSchris 278dc2c0e04Schris global $cache_wikifn; 279dc2c0e04Schris $cache = & $cache_wikifn; 280dc2c0e04Schris 2816e0cc83aSchris if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { 2826e0cc83aSchris return $cache[$raw_id][$rev]; 2836e0cc83aSchris } 2846e0cc83aSchris 2856e0cc83aSchris $id = $raw_id; 2866e0cc83aSchris 2870d8ea614Schris if ($clean) $id = cleanID($id); 288b625487dSandi $id = str_replace(':','/',$id); 289b625487dSandi if(empty($rev)){ 290b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 291b625487dSandi }else{ 292b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 293ff3ed99fSmarcel if($conf['compression']){ 294ff3ed99fSmarcel //test for extensions here, we want to read both compressions 295d8186216SBen Coburn if (@file_exists($fn . '.gz')){ 296b625487dSandi $fn .= '.gz'; 297d8186216SBen Coburn }else if(@file_exists($fn . '.bz2')){ 298ff3ed99fSmarcel $fn .= '.bz2'; 299ff3ed99fSmarcel }else{ 300ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 301ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 302ff3ed99fSmarcel } 303b625487dSandi } 304b625487dSandi } 3056e0cc83aSchris 30650602150SBen Coburn if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } 3076e0cc83aSchris $cache[$raw_id][$rev] = $fn; 308b625487dSandi return $fn; 309b625487dSandi} 310b625487dSandi 311b625487dSandi/** 312c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 313c9b4bd1eSBen Coburn * 314c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 31584657ea2SGerrit Uitslag * 31684657ea2SGerrit Uitslag * @param string $id page id 31784657ea2SGerrit Uitslag * @return string full path 318c9b4bd1eSBen Coburn */ 319c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 320c9b4bd1eSBen Coburn global $conf; 321662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 322c9b4bd1eSBen Coburn} 323c9b4bd1eSBen Coburn 324c9b4bd1eSBen Coburn 325c9b4bd1eSBen Coburn/** 3261380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 327b158d625SSteven Danz * 328b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 32984657ea2SGerrit Uitslag * 33084657ea2SGerrit Uitslag * @param string $id page id 33184657ea2SGerrit Uitslag * @param string $ext file extension 33284657ea2SGerrit Uitslag * @return string full path 333b158d625SSteven Danz */ 3341380fc45SAndreas Gohrfunction metaFN($id,$ext){ 335b158d625SSteven Danz global $conf; 336b158d625SSteven Danz $id = cleanID($id); 337b158d625SSteven Danz $id = str_replace(':','/',$id); 3381380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 339b158d625SSteven Danz return $fn; 340b158d625SSteven Danz} 341b158d625SSteven Danz 342b158d625SSteven Danz/** 343e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 344e4f389efSKate Arzamastseva * 345cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 34684657ea2SGerrit Uitslag * 34784657ea2SGerrit Uitslag * @param string $id media id 34884657ea2SGerrit Uitslag * @param string $ext extension of media 34984657ea2SGerrit Uitslag * @return string 350e4f389efSKate Arzamastseva */ 351e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){ 352e4f389efSKate Arzamastseva global $conf; 353e4f389efSKate Arzamastseva $id = cleanID($id); 354e4f389efSKate Arzamastseva $id = str_replace(':','/',$id); 355e4f389efSKate Arzamastseva $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 356e4f389efSKate Arzamastseva return $fn; 357e4f389efSKate Arzamastseva} 358e4f389efSKate Arzamastseva 359e4f389efSKate Arzamastseva/** 360e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 361e1f3d9e1SEsther Brunner * 362e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 363ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 36484657ea2SGerrit Uitslag * 36584657ea2SGerrit Uitslag * @param string $id page id 36684657ea2SGerrit Uitslag * @return array 367e1f3d9e1SEsther Brunner */ 368e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 369ba0267b3SMichael Hamann $basename = metaFN($id, ''); 370ba0267b3SMichael Hamann $files = glob($basename.'.*', GLOB_MARK); 371ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 372ba0267b3SMichael Hamann return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 373e1f3d9e1SEsther Brunner} 374e1f3d9e1SEsther Brunner 375e1f3d9e1SEsther Brunner/** 376b625487dSandi * returns the full path to the mediafile specified by ID 377b625487dSandi * 378b625487dSandi * The filename is URL encoded to protect Unicode chars 379b625487dSandi * 380b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 381cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 38284657ea2SGerrit Uitslag * 38384657ea2SGerrit Uitslag * @param string $id media id 38484657ea2SGerrit Uitslag * @param string|int $rev empty string or revision timestamp 38584657ea2SGerrit Uitslag * @return string full path 386b625487dSandi */ 387e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){ 388b625487dSandi global $conf; 389b625487dSandi $id = cleanID($id); 390b625487dSandi $id = str_replace(':','/',$id); 391e4f389efSKate Arzamastseva if(empty($rev)){ 392b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 393e4f389efSKate Arzamastseva }else{ 394cbe26ad6SKate Arzamastseva $ext = mimetype($id); 3958e69fd30SKate Arzamastseva $name = substr($id,0, -1*strlen($ext[0])-1); 39661f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 397e4f389efSKate Arzamastseva } 398b625487dSandi return $fn; 399b625487dSandi} 400b625487dSandi 401b625487dSandi/** 4022adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 403b625487dSandi * version isn't found the english one is returned 404b625487dSandi * 4052adaf2b8SAndreas Gohr * @param string $id The id of the local file 4062adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 407dbf714f7SGerrit Uitslag * @return string full filepath to localized file 40884657ea2SGerrit Uitslag * 409b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 410b625487dSandi */ 4112adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){ 412b625487dSandi global $conf; 4138819fbf5ShArpanet $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext; 414e6cecb08SMichael Hamann if(!@file_exists($file)){ 4152adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 416b625487dSandi if(!@file_exists($file)){ 417b625487dSandi //fall back to english 4182adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 419b625487dSandi } 420e6cecb08SMichael Hamann } 421b625487dSandi return $file; 422b625487dSandi} 423b625487dSandi 424b625487dSandi/** 425c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 426c4e0e4a1SAndreas Gohr * 427c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 428c4e0e4a1SAndreas Gohr * instead 429c4e0e4a1SAndreas Gohr * 430c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 431c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016 432c4e0e4a1SAndreas Gohr * 433c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 43484657ea2SGerrit Uitslag * 43584657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 43684657ea2SGerrit Uitslag * @param string $id relative id 43784657ea2SGerrit Uitslag * @param bool $clean flag indicating that id should be cleaned 43884657ea2SGerrit Uitslag * @return mixed|string 439c4e0e4a1SAndreas Gohr */ 440a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 441c662a49aSAndreas Gohr global $conf; 442c662a49aSAndreas Gohr 443c662a49aSAndreas Gohr // some pre cleaning for useslash: 444c662a49aSAndreas Gohr if($conf['useslash']) $id = str_replace('/',':',$id); 445c662a49aSAndreas Gohr 446c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 447c4e0e4a1SAndreas Gohr // relative stuff 448443e135dSChristopher Smith if($id && $id{0} == '.'){ 449c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 450c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 451c4e0e4a1SAndreas Gohr // prepend the current namespace 452c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 453c4e0e4a1SAndreas Gohr 454c4e0e4a1SAndreas Gohr // cleanup relatives 455c4e0e4a1SAndreas Gohr $result = array(); 456c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 457c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 458c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 459c4e0e4a1SAndreas Gohr if ($dir == '..') { 460c4e0e4a1SAndreas Gohr if (end($result) == '..') { 461c4e0e4a1SAndreas Gohr $result[] = '..'; 462c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 463c4e0e4a1SAndreas Gohr $result[] = '..'; 464c4e0e4a1SAndreas Gohr } 465c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 466c4e0e4a1SAndreas Gohr $result[] = $dir; 467c4e0e4a1SAndreas Gohr } 468c4e0e4a1SAndreas Gohr } 469c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 470c4e0e4a1SAndreas Gohr $id = implode(':', $result); 471c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 472c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 473c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 474c4e0e4a1SAndreas Gohr } 475c4e0e4a1SAndreas Gohr 476a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 477a6ef4796SAndreas Gohr return $id; 478c4e0e4a1SAndreas Gohr} 479c4e0e4a1SAndreas Gohr 480c4e0e4a1SAndreas Gohr/** 481b625487dSandi * Returns a full media id 482b625487dSandi * 483b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 48484657ea2SGerrit Uitslag * 48584657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 48684657ea2SGerrit Uitslag * @param string &$page (reference) relative media id, updated to resolved id 48784657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 488b625487dSandi */ 48937e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){ 490c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 491b625487dSandi $file = mediaFN($page); 492b625487dSandi $exists = @file_exists($file); 493b625487dSandi} 494b625487dSandi 495b625487dSandi/** 496b625487dSandi * Returns a full page id 497b625487dSandi * 498b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 49984657ea2SGerrit Uitslag * 50084657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 50184657ea2SGerrit Uitslag * @param string &$page (reference) relative page id, updated to resolved id 50284657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 503b625487dSandi */ 50437e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){ 505b625487dSandi global $conf; 506c006739eSIzidor Matušov global $ID; 5070b7c14c2Sandi $exists = false; 508b625487dSandi 509c006739eSIzidor Matušov //empty address should point to current page 510c006739eSIzidor Matušov if ($page === "") { 511c006739eSIzidor Matušov $page = $ID; 512c006739eSIzidor Matušov } 513c006739eSIzidor Matušov 514b625487dSandi //keep hashlink if exists then clean both parts 51503c4aec3Schris if (strpos($page,'#')) { 5164b7f9e70STom N Harris list($page,$hash) = explode('#',$page,2); 51703c4aec3Schris } else { 51803c4aec3Schris $hash = ''; 51903c4aec3Schris } 520b625487dSandi $hash = cleanID($hash); 521a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 522b625487dSandi 523a6ef4796SAndreas Gohr // get filename (calls clean itself) 524b625487dSandi $file = wikiFN($page); 525b625487dSandi 5261179df0eSGuy Brand // if ends with colon or slash we have a namespace link 527b26cdbbeSAdrian Lang if(in_array(substr($page,-1), array(':', ';')) || 528b26cdbbeSAdrian Lang ($conf['useslash'] && substr($page,-1) == '/')){ 529103c256aSChris Smith if(page_exists($page.$conf['start'])){ 530a6ef4796SAndreas Gohr // start page inside namespace 531a6ef4796SAndreas Gohr $page = $page.$conf['start']; 532a6ef4796SAndreas Gohr $exists = true; 533103c256aSChris Smith }elseif(page_exists($page.noNS(cleanID($page)))){ 534a6ef4796SAndreas Gohr // page named like the NS inside the NS 535a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 536a6ef4796SAndreas Gohr $exists = true; 537103c256aSChris Smith }elseif(page_exists($page)){ 538a6ef4796SAndreas Gohr // page like namespace exists 539a6ef4796SAndreas Gohr $page = $page; 540a6ef4796SAndreas Gohr $exists = true; 541a6ef4796SAndreas Gohr }else{ 542a6ef4796SAndreas Gohr // fall back to default 543a6ef4796SAndreas Gohr $page = $page.$conf['start']; 544a6ef4796SAndreas Gohr } 545a6ef4796SAndreas Gohr }else{ 546b625487dSandi //check alternative plural/nonplural form 547b625487dSandi if(!@file_exists($file)){ 548b625487dSandi if( $conf['autoplural'] ){ 549b625487dSandi if(substr($page,-1) == 's'){ 550b625487dSandi $try = substr($page,0,-1); 551b625487dSandi }else{ 552b625487dSandi $try = $page.'s'; 553b625487dSandi } 554103c256aSChris Smith if(page_exists($try)){ 555b625487dSandi $page = $try; 556b625487dSandi $exists = true; 557b625487dSandi } 558b625487dSandi } 559b625487dSandi }else{ 560b625487dSandi $exists = true; 561b625487dSandi } 562a6ef4796SAndreas Gohr } 563a6ef4796SAndreas Gohr 564a6ef4796SAndreas Gohr // now make sure we have a clean page 565a6ef4796SAndreas Gohr $page = cleanID($page); 566b625487dSandi 567b625487dSandi //add hash if any 568b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 569b625487dSandi} 570b625487dSandi 57198407a7aSandi/** 57298407a7aSandi * Returns the name of a cachefile from given data 57398407a7aSandi * 57498407a7aSandi * The needed directory is created by this function! 57598407a7aSandi * 57698407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 57798407a7aSandi * 57898407a7aSandi * @param string $data This data is used to create a unique md5 name 57998407a7aSandi * @param string $ext This is appended to the filename if given 58098407a7aSandi * @return string The filename of the cachefile 58198407a7aSandi */ 58298407a7aSandifunction getCacheName($data,$ext=''){ 58398407a7aSandi global $conf; 58498407a7aSandi $md5 = md5($data); 58598407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 58698407a7aSandi io_makeFileDir($file); 58798407a7aSandi return $file; 58898407a7aSandi} 58998407a7aSandi 5900dc92c6fSAndreas Gohr/** 5910dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 5920dc92c6fSAndreas Gohr * 5930dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 59484657ea2SGerrit Uitslag * 59584657ea2SGerrit Uitslag * @param string $id page id 59684657ea2SGerrit Uitslag * @return bool 5970dc92c6fSAndreas Gohr */ 5980dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 5998449cc9dSDominik Eckelmann $data = array( 6008449cc9dSDominik Eckelmann 'id' => $id, 6018449cc9dSDominik Eckelmann 'hidden' => false 6028449cc9dSDominik Eckelmann ); 603fb55b51eSDominik Eckelmann trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 604fb55b51eSDominik Eckelmann return $data['hidden']; 6050dc92c6fSAndreas Gohr} 606fb55b51eSDominik Eckelmann 607dbf714f7SGerrit Uitslag/** 608dbf714f7SGerrit Uitslag * callback checks if page is hidden 609dbf714f7SGerrit Uitslag * 61084657ea2SGerrit Uitslag * @param array $data event data - see isHiddenPage() 611dbf714f7SGerrit Uitslag */ 612fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) { 613fb55b51eSDominik Eckelmann global $conf; 614fb55b51eSDominik Eckelmann global $ACT; 615fb55b51eSDominik Eckelmann 616fb55b51eSDominik Eckelmann if ($data['hidden']) return; 617fb55b51eSDominik Eckelmann if(empty($conf['hidepages'])) return; 618fb55b51eSDominik Eckelmann if($ACT == 'admin') return; 619fb55b51eSDominik Eckelmann 620fb55b51eSDominik Eckelmann if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){ 621fb55b51eSDominik Eckelmann $data['hidden'] = true; 622fb55b51eSDominik Eckelmann } 6230dc92c6fSAndreas Gohr} 6240dc92c6fSAndreas Gohr 6250dc92c6fSAndreas Gohr/** 6260dc92c6fSAndreas Gohr * Reverse of isHiddenPage 6270dc92c6fSAndreas Gohr * 6280dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 62984657ea2SGerrit Uitslag * 63084657ea2SGerrit Uitslag * @param string $id page id 63184657ea2SGerrit Uitslag * @return bool 6320dc92c6fSAndreas Gohr */ 6330dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 6340dc92c6fSAndreas Gohr return !isHiddenPage($id); 6350dc92c6fSAndreas Gohr} 6360dc92c6fSAndreas Gohr 6375b75cd1fSAdrian Lang/** 6385b75cd1fSAdrian Lang * Format an id for output to a user 6395b75cd1fSAdrian Lang * 6405b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 6415b75cd1fSAdrian Lang * “*”. Output is escaped. 6425b75cd1fSAdrian Lang * 6435b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 64484657ea2SGerrit Uitslag * 64584657ea2SGerrit Uitslag * @param string $id page id 64684657ea2SGerrit Uitslag * @return string 6475b75cd1fSAdrian Lang */ 6485b75cd1fSAdrian Langfunction prettyprint_id($id) { 6495b75cd1fSAdrian Lang if (!$id || $id === ':') { 6505b75cd1fSAdrian Lang return '*'; 6515b75cd1fSAdrian Lang } 6525b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 6535b75cd1fSAdrian Lang $id .= '*'; 6545b75cd1fSAdrian Lang } 6555b75cd1fSAdrian Lang return hsc($id); 6565b75cd1fSAdrian Lang} 657f03fd957SAndreas Gohr 658f03fd957SAndreas Gohr/** 659f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 660f03fd957SAndreas Gohr * 661f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 662f03fd957SAndreas Gohr * 663f03fd957SAndreas Gohr * When the second parameter is true the string will 664f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 665f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 666f03fd957SAndreas Gohr * same string (default is true) 667f03fd957SAndreas Gohr * 668f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 669f03fd957SAndreas Gohr * @see urlencode 67084657ea2SGerrit Uitslag * 67184657ea2SGerrit Uitslag * @param string $file file name 67284657ea2SGerrit Uitslag * @param bool $safe if true, only encoded when non ASCII characters detected 67384657ea2SGerrit Uitslag * @return string 674f03fd957SAndreas Gohr */ 675f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){ 676f03fd957SAndreas Gohr global $conf; 677f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 678f03fd957SAndreas Gohr 679f03fd957SAndreas Gohr if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 680f03fd957SAndreas Gohr return $file; 681f03fd957SAndreas Gohr } 682f03fd957SAndreas Gohr 683f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 684f03fd957SAndreas Gohr return SafeFN::encode($file); 685f03fd957SAndreas Gohr } 686f03fd957SAndreas Gohr 687f03fd957SAndreas Gohr $file = urlencode($file); 688f03fd957SAndreas Gohr $file = str_replace('%2F','/',$file); 689f03fd957SAndreas Gohr return $file; 690f03fd957SAndreas Gohr} 691f03fd957SAndreas Gohr 692f03fd957SAndreas Gohr/** 693f03fd957SAndreas Gohr * Decode a filename back to UTF-8 694f03fd957SAndreas Gohr * 695f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 696f03fd957SAndreas Gohr * 697f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 698f03fd957SAndreas Gohr * @see urldecode 69984657ea2SGerrit Uitslag * 70084657ea2SGerrit Uitslag * @param string $file file name 70184657ea2SGerrit Uitslag * @return string 702f03fd957SAndreas Gohr */ 703f03fd957SAndreas Gohrfunction utf8_decodeFN($file){ 704f03fd957SAndreas Gohr global $conf; 705f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 706f03fd957SAndreas Gohr 707f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 708f03fd957SAndreas Gohr return SafeFN::decode($file); 709f03fd957SAndreas Gohr } 710f03fd957SAndreas Gohr 711f03fd957SAndreas Gohr return urldecode($file); 712f03fd957SAndreas Gohr} 713f03fd957SAndreas Gohr 714e66d3e6dSAndreas Gohr/** 715e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any 716e66d3e6dSAndreas Gohr * higher namespace 717e66d3e6dSAndreas Gohr * 718e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well 719e66d3e6dSAndreas Gohr * 720e66d3e6dSAndreas Gohr * @todo add event hook 721e66d3e6dSAndreas Gohr * @param string $page the pagename you're looking for 722e66d3e6dSAndreas Gohr * @return string|false the full page id of the found page, false if any 723e66d3e6dSAndreas Gohr */ 724e66d3e6dSAndreas Gohrfunction page_findnearest($page){ 725c786a1b6SAnika Henke if (!$page) return false; 726e66d3e6dSAndreas Gohr global $ID; 727e66d3e6dSAndreas Gohr 728e66d3e6dSAndreas Gohr $ns = $ID; 729e66d3e6dSAndreas Gohr do { 730e66d3e6dSAndreas Gohr $ns = getNS($ns); 731e66d3e6dSAndreas Gohr $pageid = ltrim("$ns:$page",':'); 732e66d3e6dSAndreas Gohr if(page_exists($pageid)){ 733e66d3e6dSAndreas Gohr return $pageid; 734e66d3e6dSAndreas Gohr } 735e66d3e6dSAndreas Gohr } while($ns); 736e66d3e6dSAndreas Gohr 737e66d3e6dSAndreas Gohr return false; 738e66d3e6dSAndreas Gohr} 739