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 100c3a5702SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog; 110c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 120c3a5702SAndreas Gohr 136c7843b5Sandi/** 146de3759aSAndreas Gohr * Fetch the an ID from request 156c7843b5Sandi * 166c7843b5Sandi * Uses either standard $_REQUEST variable or extracts it from 176c7843b5Sandi * the full request URI when userewrite is set to 2 186c7843b5Sandi * 1942905504SAndreas Gohr * For $param='id' $conf['start'] is returned if no id was found. 2042905504SAndreas Gohr * If the second parameter is true (default) the ID is cleaned. 216c7843b5Sandi * 226c7843b5Sandi * @author Andreas Gohr <andi@splitbrain.org> 2384657ea2SGerrit Uitslag * 2484657ea2SGerrit Uitslag * @param string $param the $_REQUEST variable name, default 'id' 2584657ea2SGerrit Uitslag * @param bool $clean if true, ID is cleaned 2642ea7f44SGerrit Uitslag * @return string 276c7843b5Sandi */ 2842905504SAndreas Gohrfunction getID($param='id',$clean=true){ 29585bf44eSChristopher Smith /** @var Input $INPUT */ 307d01a0eaSTom N Harris global $INPUT; 316c7843b5Sandi global $conf; 324e90caaaSMichael Hamann global $ACT; 336c7843b5Sandi 347d01a0eaSTom N Harris $id = $INPUT->str($param); 3548665d38SAndreas Gohr 366c7843b5Sandi //construct page id from request URI 376c7843b5Sandi if(empty($id) && $conf['userewrite'] == 2){ 38585bf44eSChristopher Smith $request = $INPUT->server->str('REQUEST_URI'); 3906368e4dSMichael Hamann $script = ''; 4006368e4dSMichael Hamann 416c7843b5Sandi //get the script URL 426c7843b5Sandi if($conf['basedir']){ 4381124000Sjan $relpath = ''; 4481124000Sjan if($param != 'id') { 4581124000Sjan $relpath = 'lib/exe/'; 4681124000Sjan } 47585bf44eSChristopher Smith $script = $conf['basedir'].$relpath.utf8_basename($INPUT->server->str('SCRIPT_FILENAME')); 487d71d4b7SAndreas Gohr 49585bf44eSChristopher Smith }elseif($INPUT->server->str('PATH_INFO')){ 50585bf44eSChristopher Smith $request = $INPUT->server->str('PATH_INFO'); 51585bf44eSChristopher Smith }elseif($INPUT->server->str('SCRIPT_NAME')){ 52585bf44eSChristopher Smith $script = $INPUT->server->str('SCRIPT_NAME'); 53585bf44eSChristopher Smith }elseif($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')){ 54585bf44eSChristopher Smith $script = preg_replace ('/^'.preg_quote($INPUT->server->str('DOCUMENT_ROOT'),'/').'/','', 55585bf44eSChristopher Smith $INPUT->server->str('SCRIPT_FILENAME')); 566c7843b5Sandi $script = '/'.$script; 576c7843b5Sandi } 586c7843b5Sandi 5952339126Sandi //clean script and request (fixes a windows problem) 6052339126Sandi $script = preg_replace('/\/\/+/','/',$script); 617d71d4b7SAndreas Gohr $request = preg_replace('/\/\/+/','/',$request); 6252339126Sandi 636c7843b5Sandi //remove script URL and Querystring to gain the id 6452339126Sandi if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){ 656c7843b5Sandi $id = preg_replace ('/\?.*/','',$match[1]); 666c7843b5Sandi } 676de3759aSAndreas Gohr $id = urldecode($id); 6842905504SAndreas Gohr //strip leading slashes 6942905504SAndreas Gohr $id = preg_replace('!^/+!','',$id); 706c7843b5Sandi } 71671a58a6SGuy Brand 72671a58a6SGuy Brand // Namespace autolinking from URL 73b6084253SAndreas Gohr if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ 74103c256aSChris Smith if(page_exists($id.$conf['start'])){ 75671a58a6SGuy Brand // start page inside namespace 76671a58a6SGuy Brand $id = $id.$conf['start']; 77103c256aSChris Smith }elseif(page_exists($id.noNS(cleanID($id)))){ 78671a58a6SGuy Brand // page named like the NS inside the NS 79671a58a6SGuy Brand $id = $id.noNS(cleanID($id)); 80103c256aSChris Smith }elseif(page_exists($id)){ 81671a58a6SGuy Brand // page like namespace exists 827a42ac9eSBen Coburn $id = substr($id,0,-1); 83671a58a6SGuy Brand }else{ 84671a58a6SGuy Brand // fall back to default 85671a58a6SGuy Brand $id = $id.$conf['start']; 86671a58a6SGuy Brand } 879dc53973SMichael Grosse if (isset($ACT) && $ACT === 'show') { 889dc53973SMichael Grosse $urlParameters = $_GET; 89e380abb2SMichael Grosse if (isset($urlParameters['id'])) { 90e380abb2SMichael Grosse unset($urlParameters['id']); 91e380abb2SMichael Grosse } 92e9fede20SPhy send_redirect(wl($id, $urlParameters, true, '&')); 939dc53973SMichael Grosse } 94671a58a6SGuy Brand } 9542905504SAndreas Gohr if($clean) $id = cleanID($id); 9640b5fb5bSPhy if($id === '' && $param=='id') $id = $conf['start']; 976c7843b5Sandi 986c7843b5Sandi return $id; 996c7843b5Sandi} 100b625487dSandi 101b625487dSandi/** 102b625487dSandi * Remove unwanted chars from ID 103b625487dSandi * 104b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are 105b625487dSandi * converted to unaccented ones 106b625487dSandi * 107b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 10842ea7f44SGerrit Uitslag * 1096e0cc83aSchris * @param string $raw_id The pageid to clean 1108a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 111dbf714f7SGerrit Uitslag * @return string cleaned id 112b625487dSandi */ 113c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){ 114b625487dSandi global $conf; 1154b5db43bSjoe.lapp static $sepcharpat = null; 1164b5db43bSjoe.lapp 117dc2c0e04Schris global $cache_cleanid; 118dc2c0e04Schris $cache = & $cache_cleanid; 1196e0cc83aSchris 1206e0cc83aSchris // check if it's already in the memory cache 12130f3bd15SMichael Grosse if (!$ascii && isset($cache[(string)$raw_id])) { 1223a50618cSgweissbach return $cache[(string)$raw_id]; 1236e0cc83aSchris } 1246e0cc83aSchris 1254b5db43bSjoe.lapp $sepchar = $conf['sepchar']; 1264b5db43bSjoe.lapp if($sepcharpat == null) // build string only once to save clock cycles 1274b5db43bSjoe.lapp $sepcharpat = '#\\'.$sepchar.'+#'; 1284b5db43bSjoe.lapp 1293a50618cSgweissbach $id = trim((string)$raw_id); 130b625487dSandi $id = utf8_strtolower($id); 131b625487dSandi 132b625487dSandi //alternative namespace seperator 133b625487dSandi if($conf['useslash']){ 1343755fc25STom N Harris $id = strtr($id,';/','::'); 135b625487dSandi }else{ 1363755fc25STom N Harris $id = strtr($id,';/',':'.$sepchar); 137b625487dSandi } 138b625487dSandi 1398a831f2bSAndreas Gohr if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 1408a831f2bSAndreas Gohr if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 141b625487dSandi 142b625487dSandi //remove specials 143ad81d431SAndreas Gohr $id = utf8_stripspecials($id,$sepchar,'\*'); 144b625487dSandi 1458a831f2bSAndreas Gohr if($ascii) $id = utf8_strip($id); 1468a831f2bSAndreas Gohr 147b625487dSandi //clean up 1484b5db43bSjoe.lapp $id = preg_replace($sepcharpat,$sepchar,$id); 149b625487dSandi $id = preg_replace('#:+#',':',$id); 1503543c6deSAndreas Gohr $id = trim($id,':._-'); 151b625487dSandi $id = preg_replace('#:[:\._\-]+#',':',$id); 152b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#',':',$id); 153b625487dSandi 15430f3bd15SMichael Grosse if (!$ascii) $cache[(string)$raw_id] = $id; 155b625487dSandi return($id); 156b625487dSandi} 157b625487dSandi 158b625487dSandi/** 159b625487dSandi * Return namespacepart of a wiki ID 160b625487dSandi * 161b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 16215851b98SGerrit Uitslag * 16315851b98SGerrit Uitslag * @param string $id 16442ea7f44SGerrit Uitslag * @return string|false the namespace part or false if the given ID has no namespace (root) 165b625487dSandi */ 166b625487dSandifunction getNS($id){ 1673a50618cSgweissbach $pos = strrpos((string)$id,':'); 168c4e0e4a1SAndreas Gohr if($pos!==false){ 1693a50618cSgweissbach return substr((string)$id,0,$pos); 170b625487dSandi } 171ef11fcfcSAndreas Gohr return false; 172b625487dSandi} 173b625487dSandi 174b625487dSandi/** 175b625487dSandi * Returns the ID without the namespace 176b625487dSandi * 177b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 17815851b98SGerrit Uitslag * 17915851b98SGerrit Uitslag * @param string $id 18015851b98SGerrit Uitslag * @return string 181b625487dSandi */ 182b625487dSandifunction noNS($id) { 1832844584fSBen Coburn $pos = strrpos($id, ':'); 1842844584fSBen Coburn if ($pos!==false) { 1852844584fSBen Coburn return substr($id, $pos+1); 1862844584fSBen Coburn } else { 1872844584fSBen Coburn return $id; 1882844584fSBen Coburn } 1891a84a0f3SAnika Henke} 1901a84a0f3SAnika Henke 1911a84a0f3SAnika Henke/** 1921a84a0f3SAnika Henke * Returns the current namespace 1931a84a0f3SAnika Henke * 1941a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 19584657ea2SGerrit Uitslag * 19684657ea2SGerrit Uitslag * @param string $id 19784657ea2SGerrit Uitslag * @return string 1981a84a0f3SAnika Henke */ 1991a84a0f3SAnika Henkefunction curNS($id) { 2001a84a0f3SAnika Henke return noNS(getNS($id)); 2011a84a0f3SAnika Henke} 2021a84a0f3SAnika Henke 2031a84a0f3SAnika Henke/** 2041a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 2051a84a0f3SAnika Henke * 2061a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 20784657ea2SGerrit Uitslag * 20884657ea2SGerrit Uitslag * @param string $id 20984657ea2SGerrit Uitslag * @return string 2101a84a0f3SAnika Henke */ 2111a84a0f3SAnika Henkefunction noNSorNS($id) { 2121a84a0f3SAnika Henke global $conf; 2131a84a0f3SAnika Henke 2141a84a0f3SAnika Henke $p = noNS($id); 2159708106bSAdrian Lang if ($p == $conf['start'] || $p == false) { 2161a84a0f3SAnika Henke $p = curNS($id); 2171a84a0f3SAnika Henke if ($p == false) { 2189708106bSAdrian Lang return $conf['start']; 2191a84a0f3SAnika Henke } 2201a84a0f3SAnika Henke } 2211a84a0f3SAnika Henke return $p; 222b625487dSandi} 2234ceab83fSAndreas Gohr 2244ceab83fSAndreas Gohr/** 2254ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 2264ceab83fSAndreas Gohr * 2274ceab83fSAndreas Gohr * @param string $title The headline title 228c857afe0SMichael Hamann * @param array|bool $check Existing IDs (title => number) 229c857afe0SMichael Hamann * @return string the title 23084657ea2SGerrit Uitslag * 2314ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2324ceab83fSAndreas Gohr */ 233443d207bSAndreas Gohrfunction sectionID($title,&$check) { 234de9114eaSAnika Henke $title = str_replace(array(':','.'),'',cleanID($title)); 235de9114eaSAnika Henke $new = ltrim($title,'0123456789_-'); 2364ceab83fSAndreas Gohr if(empty($new)){ 2374ceab83fSAndreas Gohr $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline 2384ceab83fSAndreas Gohr }else{ 2394ceab83fSAndreas Gohr $title = $new; 2404ceab83fSAndreas Gohr } 2414ceab83fSAndreas Gohr 242443d207bSAndreas Gohr if(is_array($check)){ 2434ceab83fSAndreas Gohr // make sure tiles are unique 24401e3159cSChris Tapp if (!array_key_exists ($title,$check)) { 24501e3159cSChris Tapp $check[$title] = 0; 24601e3159cSChris Tapp } else { 24701e3159cSChris Tapp $title .= ++ $check[$title]; 2484ceab83fSAndreas Gohr } 2494ceab83fSAndreas Gohr } 2504ceab83fSAndreas Gohr 2514ceab83fSAndreas Gohr return $title; 2524ceab83fSAndreas Gohr} 2534ceab83fSAndreas Gohr 254b625487dSandi/** 255103c256aSChris Smith * Wiki page existence check 256103c256aSChris Smith * 257103c256aSChris Smith * parameters as for wikiFN 258103c256aSChris Smith * 259103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk> 26084657ea2SGerrit Uitslag * 26184657ea2SGerrit Uitslag * @param string $id page id 26284657ea2SGerrit Uitslag * @param string|int $rev empty or revision timestamp 26384657ea2SGerrit Uitslag * @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well) 2647de86af9SGerrit Uitslag * @param bool $date_at 26584657ea2SGerrit Uitslag * @return bool exists? 266103c256aSChris Smith */ 2671d053a56Slispsfunction page_exists($id,$rev='',$clean=true, $date_at=false) { 26890bee600Slisps if($rev !== '' && $date_at) { 2691d053a56Slisps $pagelog = new PageChangeLog($id); 27090bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 27190bee600Slisps if($pagelog_rev !== false) 27290bee600Slisps $rev = $pagelog_rev; 27390bee600Slisps } 27479e79377SAndreas Gohr return file_exists(wikiFN($id,$rev,$clean)); 275103c256aSChris Smith} 276103c256aSChris Smith 277103c256aSChris Smith/** 278103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 279b625487dSandi * 280b625487dSandi * The filename is URL encoded to protect Unicode chars 281b625487dSandi * 282103c256aSChris Smith * @param $raw_id string id of wikipage 283e0c26282SGerrit Uitslag * @param $rev int|string page revision, empty string for current 284103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 285103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 286dbf714f7SGerrit Uitslag * @return string full path 287103c256aSChris Smith * 288b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 289b625487dSandi */ 2906e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 291b625487dSandi global $conf; 2926e0cc83aSchris 293dc2c0e04Schris global $cache_wikifn; 294dc2c0e04Schris $cache = & $cache_wikifn; 295dc2c0e04Schris 2966e0cc83aSchris $id = $raw_id; 2976e0cc83aSchris 2980d8ea614Schris if ($clean) $id = cleanID($id); 299b625487dSandi $id = str_replace(':','/',$id); 300b018ecbeSMichael Grosse 301b018ecbeSMichael Grosse if (isset($cache[$id]) && isset($cache[$id][$rev])) { 302b018ecbeSMichael Grosse return $cache[$id][$rev]; 303b018ecbeSMichael Grosse } 304b018ecbeSMichael Grosse 305b625487dSandi if(empty($rev)){ 306b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 307b625487dSandi }else{ 308b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 309ff3ed99fSmarcel if($conf['compression']){ 310ff3ed99fSmarcel //test for extensions here, we want to read both compressions 31179e79377SAndreas Gohr if (file_exists($fn . '.gz')){ 312b625487dSandi $fn .= '.gz'; 31379e79377SAndreas Gohr }else if(file_exists($fn . '.bz2')){ 314ff3ed99fSmarcel $fn .= '.bz2'; 315ff3ed99fSmarcel }else{ 316ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 317ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 318ff3ed99fSmarcel } 319b625487dSandi } 320b625487dSandi } 3216e0cc83aSchris 322b018ecbeSMichael Grosse if (!isset($cache[$id])) { $cache[$id] = array(); } 323b018ecbeSMichael Grosse $cache[$id][$rev] = $fn; 324b625487dSandi return $fn; 325b625487dSandi} 326b625487dSandi 327b625487dSandi/** 328c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 329c9b4bd1eSBen Coburn * 330c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 33184657ea2SGerrit Uitslag * 33284657ea2SGerrit Uitslag * @param string $id page id 33384657ea2SGerrit Uitslag * @return string full path 334c9b4bd1eSBen Coburn */ 335c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 336c9b4bd1eSBen Coburn global $conf; 337662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 338c9b4bd1eSBen Coburn} 339c9b4bd1eSBen Coburn 340c9b4bd1eSBen Coburn 341c9b4bd1eSBen Coburn/** 3421380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 343b158d625SSteven Danz * 344b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 34584657ea2SGerrit Uitslag * 34684657ea2SGerrit Uitslag * @param string $id page id 34784657ea2SGerrit Uitslag * @param string $ext file extension 34884657ea2SGerrit Uitslag * @return string full path 349b158d625SSteven Danz */ 3501380fc45SAndreas Gohrfunction metaFN($id,$ext){ 351b158d625SSteven Danz global $conf; 352b158d625SSteven Danz $id = cleanID($id); 353b158d625SSteven Danz $id = str_replace(':','/',$id); 3541380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 355b158d625SSteven Danz return $fn; 356b158d625SSteven Danz} 357b158d625SSteven Danz 358b158d625SSteven Danz/** 359e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 360e4f389efSKate Arzamastseva * 361cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 36284657ea2SGerrit Uitslag * 36384657ea2SGerrit Uitslag * @param string $id media id 36484657ea2SGerrit Uitslag * @param string $ext extension of media 36584657ea2SGerrit Uitslag * @return string 366e4f389efSKate Arzamastseva */ 367e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){ 368e4f389efSKate Arzamastseva global $conf; 369e4f389efSKate Arzamastseva $id = cleanID($id); 370e4f389efSKate Arzamastseva $id = str_replace(':','/',$id); 371e4f389efSKate Arzamastseva $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 372e4f389efSKate Arzamastseva return $fn; 373e4f389efSKate Arzamastseva} 374e4f389efSKate Arzamastseva 375e4f389efSKate Arzamastseva/** 376e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 377e1f3d9e1SEsther Brunner * 378e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 379ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 38084657ea2SGerrit Uitslag * 38184657ea2SGerrit Uitslag * @param string $id page id 38284657ea2SGerrit Uitslag * @return array 383e1f3d9e1SEsther Brunner */ 384e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 385ba0267b3SMichael Hamann $basename = metaFN($id, ''); 386ba0267b3SMichael Hamann $files = glob($basename.'.*', GLOB_MARK); 387ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 388ba0267b3SMichael Hamann return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 389e1f3d9e1SEsther Brunner} 390e1f3d9e1SEsther Brunner 391e1f3d9e1SEsther Brunner/** 392b625487dSandi * returns the full path to the mediafile specified by ID 393b625487dSandi * 394b625487dSandi * The filename is URL encoded to protect Unicode chars 395b625487dSandi * 396b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 397cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 39884657ea2SGerrit Uitslag * 39984657ea2SGerrit Uitslag * @param string $id media id 40084657ea2SGerrit Uitslag * @param string|int $rev empty string or revision timestamp 401f50a239bSTakamura * @param bool $clean 402f50a239bSTakamura * 40384657ea2SGerrit Uitslag * @return string full path 404b625487dSandi */ 405d0e997c6SMichael Großefunction mediaFN($id, $rev='', $clean=true){ 406b625487dSandi global $conf; 407d0e997c6SMichael Große if ($clean) $id = cleanID($id); 408b625487dSandi $id = str_replace(':','/',$id); 409e4f389efSKate Arzamastseva if(empty($rev)){ 410b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 411e4f389efSKate Arzamastseva }else{ 412cbe26ad6SKate Arzamastseva $ext = mimetype($id); 4138e69fd30SKate Arzamastseva $name = substr($id,0, -1*strlen($ext[0])-1); 41461f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 415e4f389efSKate Arzamastseva } 416b625487dSandi return $fn; 417b625487dSandi} 418b625487dSandi 419b625487dSandi/** 4202adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 421b625487dSandi * version isn't found the english one is returned 422b625487dSandi * 4232adaf2b8SAndreas Gohr * @param string $id The id of the local file 4242adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 425dbf714f7SGerrit Uitslag * @return string full filepath to localized file 42684657ea2SGerrit Uitslag * 427b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 428b625487dSandi */ 4292adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){ 430b625487dSandi global $conf; 4318819fbf5ShArpanet $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext; 43279e79377SAndreas Gohr if(!file_exists($file)){ 4332adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 43479e79377SAndreas Gohr if(!file_exists($file)){ 435b625487dSandi //fall back to english 4362adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 437b625487dSandi } 438e6cecb08SMichael Hamann } 439b625487dSandi return $file; 440b625487dSandi} 441b625487dSandi 442b625487dSandi/** 443c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 444c4e0e4a1SAndreas Gohr * 445c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 446c4e0e4a1SAndreas Gohr * instead 447c4e0e4a1SAndreas Gohr * 448c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 44959752844SAnders Sandblad * http://php.net/manual/en/function.realpath.php#57016 450c4e0e4a1SAndreas Gohr * 451c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 45284657ea2SGerrit Uitslag * 45384657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 45484657ea2SGerrit Uitslag * @param string $id relative id 45584657ea2SGerrit Uitslag * @param bool $clean flag indicating that id should be cleaned 45642ea7f44SGerrit Uitslag * @return string 457c4e0e4a1SAndreas Gohr */ 458a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 459c662a49aSAndreas Gohr global $conf; 460c662a49aSAndreas Gohr 461c662a49aSAndreas Gohr // some pre cleaning for useslash: 462c662a49aSAndreas Gohr if($conf['useslash']) $id = str_replace('/',':',$id); 463c662a49aSAndreas Gohr 464c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 465c4e0e4a1SAndreas Gohr // relative stuff 466443e135dSChristopher Smith if($id && $id{0} == '.'){ 467c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 468c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 469c4e0e4a1SAndreas Gohr // prepend the current namespace 470c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 471c4e0e4a1SAndreas Gohr 472c4e0e4a1SAndreas Gohr // cleanup relatives 473c4e0e4a1SAndreas Gohr $result = array(); 474c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 475c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 476c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 477c4e0e4a1SAndreas Gohr if ($dir == '..') { 478c4e0e4a1SAndreas Gohr if (end($result) == '..') { 479c4e0e4a1SAndreas Gohr $result[] = '..'; 480c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 481c4e0e4a1SAndreas Gohr $result[] = '..'; 482c4e0e4a1SAndreas Gohr } 483c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 484c4e0e4a1SAndreas Gohr $result[] = $dir; 485c4e0e4a1SAndreas Gohr } 486c4e0e4a1SAndreas Gohr } 487c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 488c4e0e4a1SAndreas Gohr $id = implode(':', $result); 489c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 490c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 491c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 492c4e0e4a1SAndreas Gohr } 493c4e0e4a1SAndreas Gohr 494a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 495a6ef4796SAndreas Gohr return $id; 496c4e0e4a1SAndreas Gohr} 497c4e0e4a1SAndreas Gohr 498c4e0e4a1SAndreas Gohr/** 499b625487dSandi * Returns a full media id 500b625487dSandi * 501b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 50284657ea2SGerrit Uitslag * 50384657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 50484657ea2SGerrit Uitslag * @param string &$page (reference) relative media id, updated to resolved id 50584657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 5067de86af9SGerrit Uitslag * @param int|string $rev 5077de86af9SGerrit Uitslag * @param bool $date_at 508b625487dSandi */ 50990bee600Slispsfunction resolve_mediaid($ns,&$page,&$exists,$rev='',$date_at=false){ 510c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 51190bee600Slisps if($rev !== '' && $date_at){ 5121d053a56Slisps $medialog = new MediaChangeLog($page); 51390bee600Slisps $medialog_rev = $medialog->getLastRevisionAt($rev); 51490bee600Slisps if($medialog_rev !== false) { 51590bee600Slisps $rev = $medialog_rev; 51690bee600Slisps } 51790bee600Slisps } 5181d053a56Slisps 5194cd9f791Slisps $file = mediaFN($page,$rev); 52079e79377SAndreas Gohr $exists = file_exists($file); 521b625487dSandi} 522b625487dSandi 523b625487dSandi/** 524b625487dSandi * Returns a full page id 525b625487dSandi * 526b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 52784657ea2SGerrit Uitslag * 52884657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 52984657ea2SGerrit Uitslag * @param string &$page (reference) relative page id, updated to resolved id 53084657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 5317de86af9SGerrit Uitslag * @param string $rev 5327de86af9SGerrit Uitslag * @param bool $date_at 533b625487dSandi */ 53490bee600Slispsfunction resolve_pageid($ns,&$page,&$exists,$rev='',$date_at=false ){ 535b625487dSandi global $conf; 536c006739eSIzidor Matušov global $ID; 5370b7c14c2Sandi $exists = false; 538b625487dSandi 539c006739eSIzidor Matušov //empty address should point to current page 540c006739eSIzidor Matušov if ($page === "") { 541c006739eSIzidor Matušov $page = $ID; 542c006739eSIzidor Matušov } 543c006739eSIzidor Matušov 544b625487dSandi //keep hashlink if exists then clean both parts 54503c4aec3Schris if (strpos($page,'#')) { 5464b7f9e70STom N Harris list($page,$hash) = explode('#',$page,2); 54703c4aec3Schris } else { 54803c4aec3Schris $hash = ''; 54903c4aec3Schris } 550b625487dSandi $hash = cleanID($hash); 551a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 552b625487dSandi 553a6ef4796SAndreas Gohr // get filename (calls clean itself) 55490bee600Slisps if($rev !== '' && $date_at) { 55590bee600Slisps $pagelog = new PageChangeLog($page); 55690bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 55790bee600Slisps if($pagelog_rev !== false)//something found 55890bee600Slisps $rev = $pagelog_rev; 55990bee600Slisps } 5604cd9f791Slisps $file = wikiFN($page,$rev); 561b625487dSandi 5621179df0eSGuy Brand // if ends with colon or slash we have a namespace link 563b26cdbbeSAdrian Lang if(in_array(substr($page,-1), array(':', ';')) || 564b26cdbbeSAdrian Lang ($conf['useslash'] && substr($page,-1) == '/')){ 56590bee600Slisps if(page_exists($page.$conf['start'],$rev,true,$date_at)){ 566a6ef4796SAndreas Gohr // start page inside namespace 567a6ef4796SAndreas Gohr $page = $page.$conf['start']; 568a6ef4796SAndreas Gohr $exists = true; 56990bee600Slisps }elseif(page_exists($page.noNS(cleanID($page)),$rev,true,$date_at)){ 570a6ef4796SAndreas Gohr // page named like the NS inside the NS 571a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 572a6ef4796SAndreas Gohr $exists = true; 57390bee600Slisps }elseif(page_exists($page,$rev,true,$date_at)){ 574a6ef4796SAndreas Gohr // page like namespace exists 575a6ef4796SAndreas Gohr $page = $page; 576a6ef4796SAndreas Gohr $exists = true; 577a6ef4796SAndreas Gohr }else{ 578a6ef4796SAndreas Gohr // fall back to default 579a6ef4796SAndreas Gohr $page = $page.$conf['start']; 580a6ef4796SAndreas Gohr } 581a6ef4796SAndreas Gohr }else{ 582b625487dSandi //check alternative plural/nonplural form 58379e79377SAndreas Gohr if(!file_exists($file)){ 584b625487dSandi if( $conf['autoplural'] ){ 585b625487dSandi if(substr($page,-1) == 's'){ 586b625487dSandi $try = substr($page,0,-1); 587b625487dSandi }else{ 588b625487dSandi $try = $page.'s'; 589b625487dSandi } 59090bee600Slisps if(page_exists($try,$rev,true,$date_at)){ 591b625487dSandi $page = $try; 592b625487dSandi $exists = true; 593b625487dSandi } 594b625487dSandi } 595b625487dSandi }else{ 596b625487dSandi $exists = true; 597b625487dSandi } 598a6ef4796SAndreas Gohr } 599a6ef4796SAndreas Gohr 600a6ef4796SAndreas Gohr // now make sure we have a clean page 601a6ef4796SAndreas Gohr $page = cleanID($page); 602b625487dSandi 603b625487dSandi //add hash if any 604b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 605b625487dSandi} 606b625487dSandi 60798407a7aSandi/** 60898407a7aSandi * Returns the name of a cachefile from given data 60998407a7aSandi * 61098407a7aSandi * The needed directory is created by this function! 61198407a7aSandi * 61298407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 61398407a7aSandi * 61498407a7aSandi * @param string $data This data is used to create a unique md5 name 61598407a7aSandi * @param string $ext This is appended to the filename if given 61698407a7aSandi * @return string The filename of the cachefile 61798407a7aSandi */ 61898407a7aSandifunction getCacheName($data,$ext=''){ 61998407a7aSandi global $conf; 62098407a7aSandi $md5 = md5($data); 62198407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 62298407a7aSandi io_makeFileDir($file); 62398407a7aSandi return $file; 62498407a7aSandi} 62598407a7aSandi 6260dc92c6fSAndreas Gohr/** 6270dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 6280dc92c6fSAndreas Gohr * 6290dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 63084657ea2SGerrit Uitslag * 63184657ea2SGerrit Uitslag * @param string $id page id 63284657ea2SGerrit Uitslag * @return bool 6330dc92c6fSAndreas Gohr */ 6340dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 6358449cc9dSDominik Eckelmann $data = array( 6368449cc9dSDominik Eckelmann 'id' => $id, 6378449cc9dSDominik Eckelmann 'hidden' => false 6388449cc9dSDominik Eckelmann ); 639*cbb44eabSAndreas Gohr \dokuwiki\Extension\Event::createAndTrigger('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 640fb55b51eSDominik Eckelmann return $data['hidden']; 6410dc92c6fSAndreas Gohr} 642fb55b51eSDominik Eckelmann 643dbf714f7SGerrit Uitslag/** 644dbf714f7SGerrit Uitslag * callback checks if page is hidden 645dbf714f7SGerrit Uitslag * 64684657ea2SGerrit Uitslag * @param array $data event data - see isHiddenPage() 647dbf714f7SGerrit Uitslag */ 648fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) { 649fb55b51eSDominik Eckelmann global $conf; 650fb55b51eSDominik Eckelmann global $ACT; 651fb55b51eSDominik Eckelmann 652fb55b51eSDominik Eckelmann if ($data['hidden']) return; 653fb55b51eSDominik Eckelmann if(empty($conf['hidepages'])) return; 654fb55b51eSDominik Eckelmann if($ACT == 'admin') return; 655fb55b51eSDominik Eckelmann 656fb55b51eSDominik Eckelmann if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){ 657fb55b51eSDominik Eckelmann $data['hidden'] = true; 658fb55b51eSDominik Eckelmann } 6590dc92c6fSAndreas Gohr} 6600dc92c6fSAndreas Gohr 6610dc92c6fSAndreas Gohr/** 6620dc92c6fSAndreas Gohr * Reverse of isHiddenPage 6630dc92c6fSAndreas Gohr * 6640dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 66584657ea2SGerrit Uitslag * 66684657ea2SGerrit Uitslag * @param string $id page id 66784657ea2SGerrit Uitslag * @return bool 6680dc92c6fSAndreas Gohr */ 6690dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 6700dc92c6fSAndreas Gohr return !isHiddenPage($id); 6710dc92c6fSAndreas Gohr} 6720dc92c6fSAndreas Gohr 6735b75cd1fSAdrian Lang/** 6745b75cd1fSAdrian Lang * Format an id for output to a user 6755b75cd1fSAdrian Lang * 6765b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 6775b75cd1fSAdrian Lang * “*”. Output is escaped. 6785b75cd1fSAdrian Lang * 6795b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 68084657ea2SGerrit Uitslag * 68184657ea2SGerrit Uitslag * @param string $id page id 68284657ea2SGerrit Uitslag * @return string 6835b75cd1fSAdrian Lang */ 6845b75cd1fSAdrian Langfunction prettyprint_id($id) { 6855b75cd1fSAdrian Lang if (!$id || $id === ':') { 6865b75cd1fSAdrian Lang return '*'; 6875b75cd1fSAdrian Lang } 6885b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 6895b75cd1fSAdrian Lang $id .= '*'; 6905b75cd1fSAdrian Lang } 6915b75cd1fSAdrian Lang return hsc($id); 6925b75cd1fSAdrian Lang} 693f03fd957SAndreas Gohr 694f03fd957SAndreas Gohr/** 695f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 696f03fd957SAndreas Gohr * 697f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 698f03fd957SAndreas Gohr * 699f03fd957SAndreas Gohr * When the second parameter is true the string will 700f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 701f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 702f03fd957SAndreas Gohr * same string (default is true) 703f03fd957SAndreas Gohr * 704f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 705f03fd957SAndreas Gohr * @see urlencode 70684657ea2SGerrit Uitslag * 70784657ea2SGerrit Uitslag * @param string $file file name 70884657ea2SGerrit Uitslag * @param bool $safe if true, only encoded when non ASCII characters detected 70984657ea2SGerrit Uitslag * @return string 710f03fd957SAndreas Gohr */ 711f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){ 712f03fd957SAndreas Gohr global $conf; 713f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 714f03fd957SAndreas Gohr 715f03fd957SAndreas Gohr if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 716f03fd957SAndreas Gohr return $file; 717f03fd957SAndreas Gohr } 718f03fd957SAndreas Gohr 719f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 720f03fd957SAndreas Gohr return SafeFN::encode($file); 721f03fd957SAndreas Gohr } 722f03fd957SAndreas Gohr 723f03fd957SAndreas Gohr $file = urlencode($file); 724f03fd957SAndreas Gohr $file = str_replace('%2F','/',$file); 725f03fd957SAndreas Gohr return $file; 726f03fd957SAndreas Gohr} 727f03fd957SAndreas Gohr 728f03fd957SAndreas Gohr/** 729f03fd957SAndreas Gohr * Decode a filename back to UTF-8 730f03fd957SAndreas Gohr * 731f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 732f03fd957SAndreas Gohr * 733f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 734f03fd957SAndreas Gohr * @see urldecode 73584657ea2SGerrit Uitslag * 73684657ea2SGerrit Uitslag * @param string $file file name 73784657ea2SGerrit Uitslag * @return string 738f03fd957SAndreas Gohr */ 739f03fd957SAndreas Gohrfunction utf8_decodeFN($file){ 740f03fd957SAndreas Gohr global $conf; 741f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 742f03fd957SAndreas Gohr 743f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 744f03fd957SAndreas Gohr return SafeFN::decode($file); 745f03fd957SAndreas Gohr } 746f03fd957SAndreas Gohr 747f03fd957SAndreas Gohr return urldecode($file); 748f03fd957SAndreas Gohr} 749f03fd957SAndreas Gohr 750e66d3e6dSAndreas Gohr/** 751e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any 752cc529468SMichael Hamann * higher namespace that can be accessed by the current user, 753cc529468SMichael Hamann * this condition can be overriden by an optional parameter. 754e66d3e6dSAndreas Gohr * 755e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well 756e66d3e6dSAndreas Gohr * 757e66d3e6dSAndreas Gohr * @todo add event hook 75842ea7f44SGerrit Uitslag * 759e66d3e6dSAndreas Gohr * @param string $page the pagename you're looking for 7607c3e4a67SAndreas Gohr * @param bool $useacl only return pages readable by the current user, false to ignore ACLs 761cc529468SMichael Hamann * @return false|string the full page id of the found page, false if any 762e66d3e6dSAndreas Gohr */ 7637c3e4a67SAndreas Gohrfunction page_findnearest($page, $useacl = true){ 76406a70133SPhy if ((string) $page === '') return false; 765e66d3e6dSAndreas Gohr global $ID; 766e66d3e6dSAndreas Gohr 767e66d3e6dSAndreas Gohr $ns = $ID; 768e66d3e6dSAndreas Gohr do { 769e66d3e6dSAndreas Gohr $ns = getNS($ns); 770cc529468SMichael Hamann $pageid = cleanID("$ns:$page"); 7717c3e4a67SAndreas Gohr if(page_exists($pageid) && (!$useacl || auth_quickaclcheck($pageid) >= AUTH_READ)){ 772e66d3e6dSAndreas Gohr return $pageid; 773e66d3e6dSAndreas Gohr } 77406a70133SPhy } while($ns !== false); 775e66d3e6dSAndreas Gohr 776e66d3e6dSAndreas Gohr return false; 777e66d3e6dSAndreas Gohr} 778