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 2342ea7f44SGerrit Uitslag * @return 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> 10042ea7f44SGerrit Uitslag * 1016e0cc83aSchris * @param string $raw_id The pageid to clean 1028a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 103dbf714f7SGerrit Uitslag * @return string cleaned id 104b625487dSandi */ 105c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){ 106b625487dSandi global $conf; 1074b5db43bSjoe.lapp static $sepcharpat = null; 1084b5db43bSjoe.lapp 109dc2c0e04Schris global $cache_cleanid; 110dc2c0e04Schris $cache = & $cache_cleanid; 1116e0cc83aSchris 1126e0cc83aSchris // check if it's already in the memory cache 1133a50618cSgweissbach if (isset($cache[(string)$raw_id])) { 1143a50618cSgweissbach return $cache[(string)$raw_id]; 1156e0cc83aSchris } 1166e0cc83aSchris 1174b5db43bSjoe.lapp $sepchar = $conf['sepchar']; 1184b5db43bSjoe.lapp if($sepcharpat == null) // build string only once to save clock cycles 1194b5db43bSjoe.lapp $sepcharpat = '#\\'.$sepchar.'+#'; 1204b5db43bSjoe.lapp 1213a50618cSgweissbach $id = trim((string)$raw_id); 122b625487dSandi $id = utf8_strtolower($id); 123b625487dSandi 124b625487dSandi //alternative namespace seperator 125b625487dSandi if($conf['useslash']){ 1263755fc25STom N Harris $id = strtr($id,';/','::'); 127b625487dSandi }else{ 1283755fc25STom N Harris $id = strtr($id,';/',':'.$sepchar); 129b625487dSandi } 130b625487dSandi 1318a831f2bSAndreas Gohr if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 1328a831f2bSAndreas Gohr if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 133b625487dSandi 134b625487dSandi //remove specials 135ad81d431SAndreas Gohr $id = utf8_stripspecials($id,$sepchar,'\*'); 136b625487dSandi 1378a831f2bSAndreas Gohr if($ascii) $id = utf8_strip($id); 1388a831f2bSAndreas Gohr 139b625487dSandi //clean up 1404b5db43bSjoe.lapp $id = preg_replace($sepcharpat,$sepchar,$id); 141b625487dSandi $id = preg_replace('#:+#',':',$id); 1423543c6deSAndreas Gohr $id = trim($id,':._-'); 143b625487dSandi $id = preg_replace('#:[:\._\-]+#',':',$id); 144b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#',':',$id); 145b625487dSandi 1463a50618cSgweissbach $cache[(string)$raw_id] = $id; 147b625487dSandi return($id); 148b625487dSandi} 149b625487dSandi 150b625487dSandi/** 151b625487dSandi * Return namespacepart of a wiki ID 152b625487dSandi * 153b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 15415851b98SGerrit Uitslag * 15515851b98SGerrit Uitslag * @param string $id 15642ea7f44SGerrit Uitslag * @return string|false the namespace part or false if the given ID has no namespace (root) 157b625487dSandi */ 158b625487dSandifunction getNS($id){ 1593a50618cSgweissbach $pos = strrpos((string)$id,':'); 160c4e0e4a1SAndreas Gohr if($pos!==false){ 1613a50618cSgweissbach return substr((string)$id,0,$pos); 162b625487dSandi } 163ef11fcfcSAndreas Gohr return false; 164b625487dSandi} 165b625487dSandi 166b625487dSandi/** 167b625487dSandi * Returns the ID without the namespace 168b625487dSandi * 169b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 17015851b98SGerrit Uitslag * 17115851b98SGerrit Uitslag * @param string $id 17215851b98SGerrit Uitslag * @return string 173b625487dSandi */ 174b625487dSandifunction noNS($id) { 1752844584fSBen Coburn $pos = strrpos($id, ':'); 1762844584fSBen Coburn if ($pos!==false) { 1772844584fSBen Coburn return substr($id, $pos+1); 1782844584fSBen Coburn } else { 1792844584fSBen Coburn return $id; 1802844584fSBen Coburn } 1811a84a0f3SAnika Henke} 1821a84a0f3SAnika Henke 1831a84a0f3SAnika Henke/** 1841a84a0f3SAnika Henke * Returns the current namespace 1851a84a0f3SAnika Henke * 1861a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 18784657ea2SGerrit Uitslag * 18884657ea2SGerrit Uitslag * @param string $id 18984657ea2SGerrit Uitslag * @return string 1901a84a0f3SAnika Henke */ 1911a84a0f3SAnika Henkefunction curNS($id) { 1921a84a0f3SAnika Henke return noNS(getNS($id)); 1931a84a0f3SAnika Henke} 1941a84a0f3SAnika Henke 1951a84a0f3SAnika Henke/** 1961a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 1971a84a0f3SAnika Henke * 1981a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 19984657ea2SGerrit Uitslag * 20084657ea2SGerrit Uitslag * @param string $id 20184657ea2SGerrit Uitslag * @return string 2021a84a0f3SAnika Henke */ 2031a84a0f3SAnika Henkefunction noNSorNS($id) { 2041a84a0f3SAnika Henke global $conf; 2051a84a0f3SAnika Henke 2061a84a0f3SAnika Henke $p = noNS($id); 2079708106bSAdrian Lang if ($p == $conf['start'] || $p == false) { 2081a84a0f3SAnika Henke $p = curNS($id); 2091a84a0f3SAnika Henke if ($p == false) { 2109708106bSAdrian Lang return $conf['start']; 2111a84a0f3SAnika Henke } 2121a84a0f3SAnika Henke } 2131a84a0f3SAnika Henke return $p; 214b625487dSandi} 2154ceab83fSAndreas Gohr 2164ceab83fSAndreas Gohr/** 2174ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 2184ceab83fSAndreas Gohr * 2194ceab83fSAndreas Gohr * @param string $title The headline title 220c857afe0SMichael Hamann * @param array|bool $check Existing IDs (title => number) 221c857afe0SMichael Hamann * @return string the title 22284657ea2SGerrit Uitslag * 2234ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2244ceab83fSAndreas Gohr */ 225443d207bSAndreas Gohrfunction sectionID($title,&$check) { 226de9114eaSAnika Henke $title = str_replace(array(':','.'),'',cleanID($title)); 227de9114eaSAnika Henke $new = ltrim($title,'0123456789_-'); 2284ceab83fSAndreas Gohr if(empty($new)){ 2294ceab83fSAndreas Gohr $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline 2304ceab83fSAndreas Gohr }else{ 2314ceab83fSAndreas Gohr $title = $new; 2324ceab83fSAndreas Gohr } 2334ceab83fSAndreas Gohr 234443d207bSAndreas Gohr if(is_array($check)){ 2354ceab83fSAndreas Gohr // make sure tiles are unique 23601e3159cSChris Tapp if (!array_key_exists ($title,$check)) { 23701e3159cSChris Tapp $check[$title] = 0; 23801e3159cSChris Tapp } else { 23901e3159cSChris Tapp $title .= ++ $check[$title]; 2404ceab83fSAndreas Gohr } 2414ceab83fSAndreas Gohr } 2424ceab83fSAndreas Gohr 2434ceab83fSAndreas Gohr return $title; 2444ceab83fSAndreas Gohr} 2454ceab83fSAndreas Gohr 246b625487dSandi 247b625487dSandi/** 248103c256aSChris Smith * Wiki page existence check 249103c256aSChris Smith * 250103c256aSChris Smith * parameters as for wikiFN 251103c256aSChris Smith * 252103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk> 25384657ea2SGerrit Uitslag * 25484657ea2SGerrit Uitslag * @param string $id page id 25584657ea2SGerrit Uitslag * @param string|int $rev empty or revision timestamp 25684657ea2SGerrit Uitslag * @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well) 25784657ea2SGerrit Uitslag * @return bool exists? 258103c256aSChris Smith */ 2591d053a56Slispsfunction page_exists($id,$rev='',$clean=true, $date_at=false) { 26090bee600Slisps if($rev !== '' && $date_at) { 2611d053a56Slisps $pagelog = new PageChangeLog($id); 26290bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 26390bee600Slisps if($pagelog_rev !== false) 26490bee600Slisps $rev = $pagelog_rev; 26590bee600Slisps } 266*79e79377SAndreas Gohr return file_exists(wikiFN($id,$rev,$clean)); 267103c256aSChris Smith} 268103c256aSChris Smith 269103c256aSChris Smith/** 270103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 271b625487dSandi * 272b625487dSandi * The filename is URL encoded to protect Unicode chars 273b625487dSandi * 274103c256aSChris Smith * @param $raw_id string id of wikipage 275e0c26282SGerrit Uitslag * @param $rev int|string page revision, empty string for current 276103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 277103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 278dbf714f7SGerrit Uitslag * @return string full path 279103c256aSChris Smith * 280b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 281b625487dSandi */ 2826e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 283b625487dSandi global $conf; 2846e0cc83aSchris 285dc2c0e04Schris global $cache_wikifn; 286dc2c0e04Schris $cache = & $cache_wikifn; 287dc2c0e04Schris 2886e0cc83aSchris if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { 2896e0cc83aSchris return $cache[$raw_id][$rev]; 2906e0cc83aSchris } 2916e0cc83aSchris 2926e0cc83aSchris $id = $raw_id; 2936e0cc83aSchris 2940d8ea614Schris if ($clean) $id = cleanID($id); 295b625487dSandi $id = str_replace(':','/',$id); 296b625487dSandi if(empty($rev)){ 297b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 298b625487dSandi }else{ 299b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 300ff3ed99fSmarcel if($conf['compression']){ 301ff3ed99fSmarcel //test for extensions here, we want to read both compressions 302*79e79377SAndreas Gohr if (file_exists($fn . '.gz')){ 303b625487dSandi $fn .= '.gz'; 304*79e79377SAndreas Gohr }else if(file_exists($fn . '.bz2')){ 305ff3ed99fSmarcel $fn .= '.bz2'; 306ff3ed99fSmarcel }else{ 307ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 308ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 309ff3ed99fSmarcel } 310b625487dSandi } 311b625487dSandi } 3126e0cc83aSchris 31350602150SBen Coburn if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } 3146e0cc83aSchris $cache[$raw_id][$rev] = $fn; 315b625487dSandi return $fn; 316b625487dSandi} 317b625487dSandi 318b625487dSandi/** 319c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 320c9b4bd1eSBen Coburn * 321c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 32284657ea2SGerrit Uitslag * 32384657ea2SGerrit Uitslag * @param string $id page id 32484657ea2SGerrit Uitslag * @return string full path 325c9b4bd1eSBen Coburn */ 326c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 327c9b4bd1eSBen Coburn global $conf; 328662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 329c9b4bd1eSBen Coburn} 330c9b4bd1eSBen Coburn 331c9b4bd1eSBen Coburn 332c9b4bd1eSBen Coburn/** 3331380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 334b158d625SSteven Danz * 335b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 33684657ea2SGerrit Uitslag * 33784657ea2SGerrit Uitslag * @param string $id page id 33884657ea2SGerrit Uitslag * @param string $ext file extension 33984657ea2SGerrit Uitslag * @return string full path 340b158d625SSteven Danz */ 3411380fc45SAndreas Gohrfunction metaFN($id,$ext){ 342b158d625SSteven Danz global $conf; 343b158d625SSteven Danz $id = cleanID($id); 344b158d625SSteven Danz $id = str_replace(':','/',$id); 3451380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 346b158d625SSteven Danz return $fn; 347b158d625SSteven Danz} 348b158d625SSteven Danz 349b158d625SSteven Danz/** 350e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 351e4f389efSKate Arzamastseva * 352cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 35384657ea2SGerrit Uitslag * 35484657ea2SGerrit Uitslag * @param string $id media id 35584657ea2SGerrit Uitslag * @param string $ext extension of media 35684657ea2SGerrit Uitslag * @return string 357e4f389efSKate Arzamastseva */ 358e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){ 359e4f389efSKate Arzamastseva global $conf; 360e4f389efSKate Arzamastseva $id = cleanID($id); 361e4f389efSKate Arzamastseva $id = str_replace(':','/',$id); 362e4f389efSKate Arzamastseva $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 363e4f389efSKate Arzamastseva return $fn; 364e4f389efSKate Arzamastseva} 365e4f389efSKate Arzamastseva 366e4f389efSKate Arzamastseva/** 367e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 368e1f3d9e1SEsther Brunner * 369e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 370ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 37184657ea2SGerrit Uitslag * 37284657ea2SGerrit Uitslag * @param string $id page id 37384657ea2SGerrit Uitslag * @return array 374e1f3d9e1SEsther Brunner */ 375e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 376ba0267b3SMichael Hamann $basename = metaFN($id, ''); 377ba0267b3SMichael Hamann $files = glob($basename.'.*', GLOB_MARK); 378ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 379ba0267b3SMichael Hamann return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 380e1f3d9e1SEsther Brunner} 381e1f3d9e1SEsther Brunner 382e1f3d9e1SEsther Brunner/** 383b625487dSandi * returns the full path to the mediafile specified by ID 384b625487dSandi * 385b625487dSandi * The filename is URL encoded to protect Unicode chars 386b625487dSandi * 387b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 388cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 38984657ea2SGerrit Uitslag * 39084657ea2SGerrit Uitslag * @param string $id media id 39184657ea2SGerrit Uitslag * @param string|int $rev empty string or revision timestamp 39284657ea2SGerrit Uitslag * @return string full path 393b625487dSandi */ 394e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){ 395b625487dSandi global $conf; 396b625487dSandi $id = cleanID($id); 397b625487dSandi $id = str_replace(':','/',$id); 398e4f389efSKate Arzamastseva if(empty($rev)){ 399b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 400e4f389efSKate Arzamastseva }else{ 401cbe26ad6SKate Arzamastseva $ext = mimetype($id); 4028e69fd30SKate Arzamastseva $name = substr($id,0, -1*strlen($ext[0])-1); 40361f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 404e4f389efSKate Arzamastseva } 405b625487dSandi return $fn; 406b625487dSandi} 407b625487dSandi 408b625487dSandi/** 4092adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 410b625487dSandi * version isn't found the english one is returned 411b625487dSandi * 4122adaf2b8SAndreas Gohr * @param string $id The id of the local file 4132adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 414dbf714f7SGerrit Uitslag * @return string full filepath to localized file 41584657ea2SGerrit Uitslag * 416b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 417b625487dSandi */ 4182adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){ 419b625487dSandi global $conf; 4208819fbf5ShArpanet $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext; 421*79e79377SAndreas Gohr if(!file_exists($file)){ 4222adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 423*79e79377SAndreas Gohr if(!file_exists($file)){ 424b625487dSandi //fall back to english 4252adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 426b625487dSandi } 427e6cecb08SMichael Hamann } 428b625487dSandi return $file; 429b625487dSandi} 430b625487dSandi 431b625487dSandi/** 432c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 433c4e0e4a1SAndreas Gohr * 434c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 435c4e0e4a1SAndreas Gohr * instead 436c4e0e4a1SAndreas Gohr * 437c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 438c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016 439c4e0e4a1SAndreas Gohr * 440c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 44184657ea2SGerrit Uitslag * 44284657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 44384657ea2SGerrit Uitslag * @param string $id relative id 44484657ea2SGerrit Uitslag * @param bool $clean flag indicating that id should be cleaned 44542ea7f44SGerrit Uitslag * @return string 446c4e0e4a1SAndreas Gohr */ 447a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 448c662a49aSAndreas Gohr global $conf; 449c662a49aSAndreas Gohr 450c662a49aSAndreas Gohr // some pre cleaning for useslash: 451c662a49aSAndreas Gohr if($conf['useslash']) $id = str_replace('/',':',$id); 452c662a49aSAndreas Gohr 453c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 454c4e0e4a1SAndreas Gohr // relative stuff 455443e135dSChristopher Smith if($id && $id{0} == '.'){ 456c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 457c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 458c4e0e4a1SAndreas Gohr // prepend the current namespace 459c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 460c4e0e4a1SAndreas Gohr 461c4e0e4a1SAndreas Gohr // cleanup relatives 462c4e0e4a1SAndreas Gohr $result = array(); 463c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 464c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 465c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 466c4e0e4a1SAndreas Gohr if ($dir == '..') { 467c4e0e4a1SAndreas Gohr if (end($result) == '..') { 468c4e0e4a1SAndreas Gohr $result[] = '..'; 469c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 470c4e0e4a1SAndreas Gohr $result[] = '..'; 471c4e0e4a1SAndreas Gohr } 472c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 473c4e0e4a1SAndreas Gohr $result[] = $dir; 474c4e0e4a1SAndreas Gohr } 475c4e0e4a1SAndreas Gohr } 476c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 477c4e0e4a1SAndreas Gohr $id = implode(':', $result); 478c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 479c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 480c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 481c4e0e4a1SAndreas Gohr } 482c4e0e4a1SAndreas Gohr 483a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 484a6ef4796SAndreas Gohr return $id; 485c4e0e4a1SAndreas Gohr} 486c4e0e4a1SAndreas Gohr 487c4e0e4a1SAndreas Gohr/** 488b625487dSandi * Returns a full media id 489b625487dSandi * 490b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 49184657ea2SGerrit Uitslag * 49284657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 49384657ea2SGerrit Uitslag * @param string &$page (reference) relative media id, updated to resolved id 49484657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 495b625487dSandi */ 49690bee600Slispsfunction resolve_mediaid($ns,&$page,&$exists,$rev='',$date_at=false){ 497c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 49890bee600Slisps if($rev !== '' && $date_at){ 4991d053a56Slisps $medialog = new MediaChangeLog($page); 50090bee600Slisps $medialog_rev = $medialog->getLastRevisionAt($rev); 50190bee600Slisps if($medialog_rev !== false) { 50290bee600Slisps $rev = $medialog_rev; 50390bee600Slisps } 50490bee600Slisps } 5051d053a56Slisps 5064cd9f791Slisps $file = mediaFN($page,$rev); 507*79e79377SAndreas Gohr $exists = file_exists($file); 508b625487dSandi} 509b625487dSandi 510b625487dSandi/** 511b625487dSandi * Returns a full page id 512b625487dSandi * 513b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 51484657ea2SGerrit Uitslag * 51584657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 51684657ea2SGerrit Uitslag * @param string &$page (reference) relative page id, updated to resolved id 51784657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 518b625487dSandi */ 51990bee600Slispsfunction resolve_pageid($ns,&$page,&$exists,$rev='',$date_at=false ){ 520b625487dSandi global $conf; 521c006739eSIzidor Matušov global $ID; 5220b7c14c2Sandi $exists = false; 523b625487dSandi 524c006739eSIzidor Matušov //empty address should point to current page 525c006739eSIzidor Matušov if ($page === "") { 526c006739eSIzidor Matušov $page = $ID; 527c006739eSIzidor Matušov } 528c006739eSIzidor Matušov 529b625487dSandi //keep hashlink if exists then clean both parts 53003c4aec3Schris if (strpos($page,'#')) { 5314b7f9e70STom N Harris list($page,$hash) = explode('#',$page,2); 53203c4aec3Schris } else { 53303c4aec3Schris $hash = ''; 53403c4aec3Schris } 535b625487dSandi $hash = cleanID($hash); 536a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 537b625487dSandi 538a6ef4796SAndreas Gohr // get filename (calls clean itself) 53990bee600Slisps if($rev !== '' && $date_at) { 54090bee600Slisps $pagelog = new PageChangeLog($page); 54190bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 54290bee600Slisps if($pagelog_rev !== false)//something found 54390bee600Slisps $rev = $pagelog_rev; 54490bee600Slisps } 5454cd9f791Slisps $file = wikiFN($page,$rev); 546b625487dSandi 5471179df0eSGuy Brand // if ends with colon or slash we have a namespace link 548b26cdbbeSAdrian Lang if(in_array(substr($page,-1), array(':', ';')) || 549b26cdbbeSAdrian Lang ($conf['useslash'] && substr($page,-1) == '/')){ 55090bee600Slisps if(page_exists($page.$conf['start'],$rev,true,$date_at)){ 551a6ef4796SAndreas Gohr // start page inside namespace 552a6ef4796SAndreas Gohr $page = $page.$conf['start']; 553a6ef4796SAndreas Gohr $exists = true; 55490bee600Slisps }elseif(page_exists($page.noNS(cleanID($page)),$rev,true,$date_at)){ 555a6ef4796SAndreas Gohr // page named like the NS inside the NS 556a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 557a6ef4796SAndreas Gohr $exists = true; 55890bee600Slisps }elseif(page_exists($page,$rev,true,$date_at)){ 559a6ef4796SAndreas Gohr // page like namespace exists 560a6ef4796SAndreas Gohr $page = $page; 561a6ef4796SAndreas Gohr $exists = true; 562a6ef4796SAndreas Gohr }else{ 563a6ef4796SAndreas Gohr // fall back to default 564a6ef4796SAndreas Gohr $page = $page.$conf['start']; 565a6ef4796SAndreas Gohr } 566a6ef4796SAndreas Gohr }else{ 567b625487dSandi //check alternative plural/nonplural form 568*79e79377SAndreas Gohr if(!file_exists($file)){ 569b625487dSandi if( $conf['autoplural'] ){ 570b625487dSandi if(substr($page,-1) == 's'){ 571b625487dSandi $try = substr($page,0,-1); 572b625487dSandi }else{ 573b625487dSandi $try = $page.'s'; 574b625487dSandi } 57590bee600Slisps if(page_exists($try,$rev,true,$date_at)){ 576b625487dSandi $page = $try; 577b625487dSandi $exists = true; 578b625487dSandi } 579b625487dSandi } 580b625487dSandi }else{ 581b625487dSandi $exists = true; 582b625487dSandi } 583a6ef4796SAndreas Gohr } 584a6ef4796SAndreas Gohr 585a6ef4796SAndreas Gohr // now make sure we have a clean page 586a6ef4796SAndreas Gohr $page = cleanID($page); 587b625487dSandi 588b625487dSandi //add hash if any 589b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 590b625487dSandi} 591b625487dSandi 59298407a7aSandi/** 59398407a7aSandi * Returns the name of a cachefile from given data 59498407a7aSandi * 59598407a7aSandi * The needed directory is created by this function! 59698407a7aSandi * 59798407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 59898407a7aSandi * 59998407a7aSandi * @param string $data This data is used to create a unique md5 name 60098407a7aSandi * @param string $ext This is appended to the filename if given 60198407a7aSandi * @return string The filename of the cachefile 60298407a7aSandi */ 60398407a7aSandifunction getCacheName($data,$ext=''){ 60498407a7aSandi global $conf; 60598407a7aSandi $md5 = md5($data); 60698407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 60798407a7aSandi io_makeFileDir($file); 60898407a7aSandi return $file; 60998407a7aSandi} 61098407a7aSandi 6110dc92c6fSAndreas Gohr/** 6120dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 6130dc92c6fSAndreas Gohr * 6140dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 61584657ea2SGerrit Uitslag * 61684657ea2SGerrit Uitslag * @param string $id page id 61784657ea2SGerrit Uitslag * @return bool 6180dc92c6fSAndreas Gohr */ 6190dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 6208449cc9dSDominik Eckelmann $data = array( 6218449cc9dSDominik Eckelmann 'id' => $id, 6228449cc9dSDominik Eckelmann 'hidden' => false 6238449cc9dSDominik Eckelmann ); 624fb55b51eSDominik Eckelmann trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 625fb55b51eSDominik Eckelmann return $data['hidden']; 6260dc92c6fSAndreas Gohr} 627fb55b51eSDominik Eckelmann 628dbf714f7SGerrit Uitslag/** 629dbf714f7SGerrit Uitslag * callback checks if page is hidden 630dbf714f7SGerrit Uitslag * 63184657ea2SGerrit Uitslag * @param array $data event data - see isHiddenPage() 632dbf714f7SGerrit Uitslag */ 633fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) { 634fb55b51eSDominik Eckelmann global $conf; 635fb55b51eSDominik Eckelmann global $ACT; 636fb55b51eSDominik Eckelmann 637fb55b51eSDominik Eckelmann if ($data['hidden']) return; 638fb55b51eSDominik Eckelmann if(empty($conf['hidepages'])) return; 639fb55b51eSDominik Eckelmann if($ACT == 'admin') return; 640fb55b51eSDominik Eckelmann 641fb55b51eSDominik Eckelmann if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){ 642fb55b51eSDominik Eckelmann $data['hidden'] = true; 643fb55b51eSDominik Eckelmann } 6440dc92c6fSAndreas Gohr} 6450dc92c6fSAndreas Gohr 6460dc92c6fSAndreas Gohr/** 6470dc92c6fSAndreas Gohr * Reverse of isHiddenPage 6480dc92c6fSAndreas Gohr * 6490dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 65084657ea2SGerrit Uitslag * 65184657ea2SGerrit Uitslag * @param string $id page id 65284657ea2SGerrit Uitslag * @return bool 6530dc92c6fSAndreas Gohr */ 6540dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 6550dc92c6fSAndreas Gohr return !isHiddenPage($id); 6560dc92c6fSAndreas Gohr} 6570dc92c6fSAndreas Gohr 6585b75cd1fSAdrian Lang/** 6595b75cd1fSAdrian Lang * Format an id for output to a user 6605b75cd1fSAdrian Lang * 6615b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 6625b75cd1fSAdrian Lang * “*”. Output is escaped. 6635b75cd1fSAdrian Lang * 6645b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 66584657ea2SGerrit Uitslag * 66684657ea2SGerrit Uitslag * @param string $id page id 66784657ea2SGerrit Uitslag * @return string 6685b75cd1fSAdrian Lang */ 6695b75cd1fSAdrian Langfunction prettyprint_id($id) { 6705b75cd1fSAdrian Lang if (!$id || $id === ':') { 6715b75cd1fSAdrian Lang return '*'; 6725b75cd1fSAdrian Lang } 6735b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 6745b75cd1fSAdrian Lang $id .= '*'; 6755b75cd1fSAdrian Lang } 6765b75cd1fSAdrian Lang return hsc($id); 6775b75cd1fSAdrian Lang} 678f03fd957SAndreas Gohr 679f03fd957SAndreas Gohr/** 680f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 681f03fd957SAndreas Gohr * 682f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 683f03fd957SAndreas Gohr * 684f03fd957SAndreas Gohr * When the second parameter is true the string will 685f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 686f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 687f03fd957SAndreas Gohr * same string (default is true) 688f03fd957SAndreas Gohr * 689f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 690f03fd957SAndreas Gohr * @see urlencode 69184657ea2SGerrit Uitslag * 69284657ea2SGerrit Uitslag * @param string $file file name 69384657ea2SGerrit Uitslag * @param bool $safe if true, only encoded when non ASCII characters detected 69484657ea2SGerrit Uitslag * @return string 695f03fd957SAndreas Gohr */ 696f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){ 697f03fd957SAndreas Gohr global $conf; 698f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 699f03fd957SAndreas Gohr 700f03fd957SAndreas Gohr if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 701f03fd957SAndreas Gohr return $file; 702f03fd957SAndreas Gohr } 703f03fd957SAndreas Gohr 704f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 705f03fd957SAndreas Gohr return SafeFN::encode($file); 706f03fd957SAndreas Gohr } 707f03fd957SAndreas Gohr 708f03fd957SAndreas Gohr $file = urlencode($file); 709f03fd957SAndreas Gohr $file = str_replace('%2F','/',$file); 710f03fd957SAndreas Gohr return $file; 711f03fd957SAndreas Gohr} 712f03fd957SAndreas Gohr 713f03fd957SAndreas Gohr/** 714f03fd957SAndreas Gohr * Decode a filename back to UTF-8 715f03fd957SAndreas Gohr * 716f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 717f03fd957SAndreas Gohr * 718f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 719f03fd957SAndreas Gohr * @see urldecode 72084657ea2SGerrit Uitslag * 72184657ea2SGerrit Uitslag * @param string $file file name 72284657ea2SGerrit Uitslag * @return string 723f03fd957SAndreas Gohr */ 724f03fd957SAndreas Gohrfunction utf8_decodeFN($file){ 725f03fd957SAndreas Gohr global $conf; 726f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 727f03fd957SAndreas Gohr 728f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 729f03fd957SAndreas Gohr return SafeFN::decode($file); 730f03fd957SAndreas Gohr } 731f03fd957SAndreas Gohr 732f03fd957SAndreas Gohr return urldecode($file); 733f03fd957SAndreas Gohr} 734f03fd957SAndreas Gohr 735e66d3e6dSAndreas Gohr/** 736e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any 737e66d3e6dSAndreas Gohr * higher namespace 738e66d3e6dSAndreas Gohr * 739e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well 740e66d3e6dSAndreas Gohr * 741e66d3e6dSAndreas Gohr * @todo add event hook 74242ea7f44SGerrit Uitslag * 743e66d3e6dSAndreas Gohr * @param string $page the pagename you're looking for 744e66d3e6dSAndreas Gohr * @return string|false the full page id of the found page, false if any 745e66d3e6dSAndreas Gohr */ 746e66d3e6dSAndreas Gohrfunction page_findnearest($page){ 747c786a1b6SAnika Henke if (!$page) return false; 748e66d3e6dSAndreas Gohr global $ID; 749e66d3e6dSAndreas Gohr 750e66d3e6dSAndreas Gohr $ns = $ID; 751e66d3e6dSAndreas Gohr do { 752e66d3e6dSAndreas Gohr $ns = getNS($ns); 753e66d3e6dSAndreas Gohr $pageid = ltrim("$ns:$page",':'); 754e66d3e6dSAndreas Gohr if(page_exists($pageid)){ 755e66d3e6dSAndreas Gohr return $pageid; 756e66d3e6dSAndreas Gohr } 757e66d3e6dSAndreas Gohr } while($ns); 758e66d3e6dSAndreas Gohr 759e66d3e6dSAndreas Gohr return false; 760e66d3e6dSAndreas Gohr} 761