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 } 849dc53973SMichael Grosse if (isset($ACT) && $ACT === 'show') { 859dc53973SMichael Grosse $urlParameters = $_GET; 86*e380abb2SMichael Grosse if (isset($urlParameters['id'])) { 87*e380abb2SMichael Grosse unset($urlParameters['id']); 88*e380abb2SMichael Grosse } 899dc53973SMichael Grosse send_redirect(wl($id,$urlParameters,true)); 909dc53973SMichael Grosse } 91671a58a6SGuy Brand } 92671a58a6SGuy Brand 9342905504SAndreas Gohr if($clean) $id = cleanID($id); 940868021bSAndreas Gohr if(empty($id) && $param=='id') $id = $conf['start']; 956c7843b5Sandi 966c7843b5Sandi return $id; 976c7843b5Sandi} 98b625487dSandi 99b625487dSandi/** 100b625487dSandi * Remove unwanted chars from ID 101b625487dSandi * 102b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are 103b625487dSandi * converted to unaccented ones 104b625487dSandi * 105b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 10642ea7f44SGerrit Uitslag * 1076e0cc83aSchris * @param string $raw_id The pageid to clean 1088a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 109dbf714f7SGerrit Uitslag * @return string cleaned id 110b625487dSandi */ 111c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){ 112b625487dSandi global $conf; 1134b5db43bSjoe.lapp static $sepcharpat = null; 1144b5db43bSjoe.lapp 115dc2c0e04Schris global $cache_cleanid; 116dc2c0e04Schris $cache = & $cache_cleanid; 1176e0cc83aSchris 1186e0cc83aSchris // check if it's already in the memory cache 11930f3bd15SMichael Grosse if (!$ascii && isset($cache[(string)$raw_id])) { 1203a50618cSgweissbach return $cache[(string)$raw_id]; 1216e0cc83aSchris } 1226e0cc83aSchris 1234b5db43bSjoe.lapp $sepchar = $conf['sepchar']; 1244b5db43bSjoe.lapp if($sepcharpat == null) // build string only once to save clock cycles 1254b5db43bSjoe.lapp $sepcharpat = '#\\'.$sepchar.'+#'; 1264b5db43bSjoe.lapp 1273a50618cSgweissbach $id = trim((string)$raw_id); 128b625487dSandi $id = utf8_strtolower($id); 129b625487dSandi 130b625487dSandi //alternative namespace seperator 131b625487dSandi if($conf['useslash']){ 1323755fc25STom N Harris $id = strtr($id,';/','::'); 133b625487dSandi }else{ 1343755fc25STom N Harris $id = strtr($id,';/',':'.$sepchar); 135b625487dSandi } 136b625487dSandi 1378a831f2bSAndreas Gohr if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id); 1388a831f2bSAndreas Gohr if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1); 139b625487dSandi 140b625487dSandi //remove specials 141ad81d431SAndreas Gohr $id = utf8_stripspecials($id,$sepchar,'\*'); 142b625487dSandi 1438a831f2bSAndreas Gohr if($ascii) $id = utf8_strip($id); 1448a831f2bSAndreas Gohr 145b625487dSandi //clean up 1464b5db43bSjoe.lapp $id = preg_replace($sepcharpat,$sepchar,$id); 147b625487dSandi $id = preg_replace('#:+#',':',$id); 1483543c6deSAndreas Gohr $id = trim($id,':._-'); 149b625487dSandi $id = preg_replace('#:[:\._\-]+#',':',$id); 150b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#',':',$id); 151b625487dSandi 15230f3bd15SMichael Grosse if (!$ascii) $cache[(string)$raw_id] = $id; 153b625487dSandi return($id); 154b625487dSandi} 155b625487dSandi 156b625487dSandi/** 157b625487dSandi * Return namespacepart of a wiki ID 158b625487dSandi * 159b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 16015851b98SGerrit Uitslag * 16115851b98SGerrit Uitslag * @param string $id 16242ea7f44SGerrit Uitslag * @return string|false the namespace part or false if the given ID has no namespace (root) 163b625487dSandi */ 164b625487dSandifunction getNS($id){ 1653a50618cSgweissbach $pos = strrpos((string)$id,':'); 166c4e0e4a1SAndreas Gohr if($pos!==false){ 1673a50618cSgweissbach return substr((string)$id,0,$pos); 168b625487dSandi } 169ef11fcfcSAndreas Gohr return false; 170b625487dSandi} 171b625487dSandi 172b625487dSandi/** 173b625487dSandi * Returns the ID without the namespace 174b625487dSandi * 175b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 17615851b98SGerrit Uitslag * 17715851b98SGerrit Uitslag * @param string $id 17815851b98SGerrit Uitslag * @return string 179b625487dSandi */ 180b625487dSandifunction noNS($id) { 1812844584fSBen Coburn $pos = strrpos($id, ':'); 1822844584fSBen Coburn if ($pos!==false) { 1832844584fSBen Coburn return substr($id, $pos+1); 1842844584fSBen Coburn } else { 1852844584fSBen Coburn return $id; 1862844584fSBen Coburn } 1871a84a0f3SAnika Henke} 1881a84a0f3SAnika Henke 1891a84a0f3SAnika Henke/** 1901a84a0f3SAnika Henke * Returns the current namespace 1911a84a0f3SAnika Henke * 1921a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 19384657ea2SGerrit Uitslag * 19484657ea2SGerrit Uitslag * @param string $id 19584657ea2SGerrit Uitslag * @return string 1961a84a0f3SAnika Henke */ 1971a84a0f3SAnika Henkefunction curNS($id) { 1981a84a0f3SAnika Henke return noNS(getNS($id)); 1991a84a0f3SAnika Henke} 2001a84a0f3SAnika Henke 2011a84a0f3SAnika Henke/** 2021a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 2031a84a0f3SAnika Henke * 2041a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 20584657ea2SGerrit Uitslag * 20684657ea2SGerrit Uitslag * @param string $id 20784657ea2SGerrit Uitslag * @return string 2081a84a0f3SAnika Henke */ 2091a84a0f3SAnika Henkefunction noNSorNS($id) { 2101a84a0f3SAnika Henke global $conf; 2111a84a0f3SAnika Henke 2121a84a0f3SAnika Henke $p = noNS($id); 2139708106bSAdrian Lang if ($p == $conf['start'] || $p == false) { 2141a84a0f3SAnika Henke $p = curNS($id); 2151a84a0f3SAnika Henke if ($p == false) { 2169708106bSAdrian Lang return $conf['start']; 2171a84a0f3SAnika Henke } 2181a84a0f3SAnika Henke } 2191a84a0f3SAnika Henke return $p; 220b625487dSandi} 2214ceab83fSAndreas Gohr 2224ceab83fSAndreas Gohr/** 2234ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 2244ceab83fSAndreas Gohr * 2254ceab83fSAndreas Gohr * @param string $title The headline title 226c857afe0SMichael Hamann * @param array|bool $check Existing IDs (title => number) 227c857afe0SMichael Hamann * @return string the title 22884657ea2SGerrit Uitslag * 2294ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2304ceab83fSAndreas Gohr */ 231443d207bSAndreas Gohrfunction sectionID($title,&$check) { 232de9114eaSAnika Henke $title = str_replace(array(':','.'),'',cleanID($title)); 233de9114eaSAnika Henke $new = ltrim($title,'0123456789_-'); 2344ceab83fSAndreas Gohr if(empty($new)){ 2354ceab83fSAndreas Gohr $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline 2364ceab83fSAndreas Gohr }else{ 2374ceab83fSAndreas Gohr $title = $new; 2384ceab83fSAndreas Gohr } 2394ceab83fSAndreas Gohr 240443d207bSAndreas Gohr if(is_array($check)){ 2414ceab83fSAndreas Gohr // make sure tiles are unique 24201e3159cSChris Tapp if (!array_key_exists ($title,$check)) { 24301e3159cSChris Tapp $check[$title] = 0; 24401e3159cSChris Tapp } else { 24501e3159cSChris Tapp $title .= ++ $check[$title]; 2464ceab83fSAndreas Gohr } 2474ceab83fSAndreas Gohr } 2484ceab83fSAndreas Gohr 2494ceab83fSAndreas Gohr return $title; 2504ceab83fSAndreas Gohr} 2514ceab83fSAndreas Gohr 252b625487dSandi/** 253103c256aSChris Smith * Wiki page existence check 254103c256aSChris Smith * 255103c256aSChris Smith * parameters as for wikiFN 256103c256aSChris Smith * 257103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk> 25884657ea2SGerrit Uitslag * 25984657ea2SGerrit Uitslag * @param string $id page id 26084657ea2SGerrit Uitslag * @param string|int $rev empty or revision timestamp 26184657ea2SGerrit Uitslag * @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well) 2627de86af9SGerrit Uitslag * @param bool $date_at 26384657ea2SGerrit Uitslag * @return bool exists? 264103c256aSChris Smith */ 2651d053a56Slispsfunction page_exists($id,$rev='',$clean=true, $date_at=false) { 26690bee600Slisps if($rev !== '' && $date_at) { 2671d053a56Slisps $pagelog = new PageChangeLog($id); 26890bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 26990bee600Slisps if($pagelog_rev !== false) 27090bee600Slisps $rev = $pagelog_rev; 27190bee600Slisps } 27279e79377SAndreas Gohr return file_exists(wikiFN($id,$rev,$clean)); 273103c256aSChris Smith} 274103c256aSChris Smith 275103c256aSChris Smith/** 276103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 277b625487dSandi * 278b625487dSandi * The filename is URL encoded to protect Unicode chars 279b625487dSandi * 280103c256aSChris Smith * @param $raw_id string id of wikipage 281e0c26282SGerrit Uitslag * @param $rev int|string page revision, empty string for current 282103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 283103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 284dbf714f7SGerrit Uitslag * @return string full path 285103c256aSChris Smith * 286b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 287b625487dSandi */ 2886e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 289b625487dSandi global $conf; 2906e0cc83aSchris 291dc2c0e04Schris global $cache_wikifn; 292dc2c0e04Schris $cache = & $cache_wikifn; 293dc2c0e04Schris 2946e0cc83aSchris $id = $raw_id; 2956e0cc83aSchris 2960d8ea614Schris if ($clean) $id = cleanID($id); 297b625487dSandi $id = str_replace(':','/',$id); 298b018ecbeSMichael Grosse 299b018ecbeSMichael Grosse if (isset($cache[$id]) && isset($cache[$id][$rev])) { 300b018ecbeSMichael Grosse return $cache[$id][$rev]; 301b018ecbeSMichael Grosse } 302b018ecbeSMichael Grosse 303b625487dSandi if(empty($rev)){ 304b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 305b625487dSandi }else{ 306b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 307ff3ed99fSmarcel if($conf['compression']){ 308ff3ed99fSmarcel //test for extensions here, we want to read both compressions 30979e79377SAndreas Gohr if (file_exists($fn . '.gz')){ 310b625487dSandi $fn .= '.gz'; 31179e79377SAndreas Gohr }else if(file_exists($fn . '.bz2')){ 312ff3ed99fSmarcel $fn .= '.bz2'; 313ff3ed99fSmarcel }else{ 314ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 315ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 316ff3ed99fSmarcel } 317b625487dSandi } 318b625487dSandi } 3196e0cc83aSchris 320b018ecbeSMichael Grosse if (!isset($cache[$id])) { $cache[$id] = array(); } 321b018ecbeSMichael Grosse $cache[$id][$rev] = $fn; 322b625487dSandi return $fn; 323b625487dSandi} 324b625487dSandi 325b625487dSandi/** 326c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 327c9b4bd1eSBen Coburn * 328c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 32984657ea2SGerrit Uitslag * 33084657ea2SGerrit Uitslag * @param string $id page id 33184657ea2SGerrit Uitslag * @return string full path 332c9b4bd1eSBen Coburn */ 333c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 334c9b4bd1eSBen Coburn global $conf; 335662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 336c9b4bd1eSBen Coburn} 337c9b4bd1eSBen Coburn 338c9b4bd1eSBen Coburn 339c9b4bd1eSBen Coburn/** 3401380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 341b158d625SSteven Danz * 342b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 34384657ea2SGerrit Uitslag * 34484657ea2SGerrit Uitslag * @param string $id page id 34584657ea2SGerrit Uitslag * @param string $ext file extension 34684657ea2SGerrit Uitslag * @return string full path 347b158d625SSteven Danz */ 3481380fc45SAndreas Gohrfunction metaFN($id,$ext){ 349b158d625SSteven Danz global $conf; 350b158d625SSteven Danz $id = cleanID($id); 351b158d625SSteven Danz $id = str_replace(':','/',$id); 3521380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 353b158d625SSteven Danz return $fn; 354b158d625SSteven Danz} 355b158d625SSteven Danz 356b158d625SSteven Danz/** 357e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 358e4f389efSKate Arzamastseva * 359cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 36084657ea2SGerrit Uitslag * 36184657ea2SGerrit Uitslag * @param string $id media id 36284657ea2SGerrit Uitslag * @param string $ext extension of media 36384657ea2SGerrit Uitslag * @return string 364e4f389efSKate Arzamastseva */ 365e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){ 366e4f389efSKate Arzamastseva global $conf; 367e4f389efSKate Arzamastseva $id = cleanID($id); 368e4f389efSKate Arzamastseva $id = str_replace(':','/',$id); 369e4f389efSKate Arzamastseva $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext; 370e4f389efSKate Arzamastseva return $fn; 371e4f389efSKate Arzamastseva} 372e4f389efSKate Arzamastseva 373e4f389efSKate Arzamastseva/** 374e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 375e1f3d9e1SEsther Brunner * 376e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 377ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 37884657ea2SGerrit Uitslag * 37984657ea2SGerrit Uitslag * @param string $id page id 38084657ea2SGerrit Uitslag * @return array 381e1f3d9e1SEsther Brunner */ 382e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 383ba0267b3SMichael Hamann $basename = metaFN($id, ''); 384ba0267b3SMichael Hamann $files = glob($basename.'.*', GLOB_MARK); 385ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 386ba0267b3SMichael Hamann return $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array(); 387e1f3d9e1SEsther Brunner} 388e1f3d9e1SEsther Brunner 389e1f3d9e1SEsther Brunner/** 390b625487dSandi * returns the full path to the mediafile specified by ID 391b625487dSandi * 392b625487dSandi * The filename is URL encoded to protect Unicode chars 393b625487dSandi * 394b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 395cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 39684657ea2SGerrit Uitslag * 39784657ea2SGerrit Uitslag * @param string $id media id 39884657ea2SGerrit Uitslag * @param string|int $rev empty string or revision timestamp 39984657ea2SGerrit Uitslag * @return string full path 400b625487dSandi */ 401d0e997c6SMichael Großefunction mediaFN($id, $rev='', $clean=true){ 402b625487dSandi global $conf; 403d0e997c6SMichael Große if ($clean) $id = cleanID($id); 404b625487dSandi $id = str_replace(':','/',$id); 405e4f389efSKate Arzamastseva if(empty($rev)){ 406b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 407e4f389efSKate Arzamastseva }else{ 408cbe26ad6SKate Arzamastseva $ext = mimetype($id); 4098e69fd30SKate Arzamastseva $name = substr($id,0, -1*strlen($ext[0])-1); 41061f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]); 411e4f389efSKate Arzamastseva } 412b625487dSandi return $fn; 413b625487dSandi} 414b625487dSandi 415b625487dSandi/** 4162adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 417b625487dSandi * version isn't found the english one is returned 418b625487dSandi * 4192adaf2b8SAndreas Gohr * @param string $id The id of the local file 4202adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 421dbf714f7SGerrit Uitslag * @return string full filepath to localized file 42284657ea2SGerrit Uitslag * 423b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 424b625487dSandi */ 4252adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){ 426b625487dSandi global $conf; 4278819fbf5ShArpanet $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext; 42879e79377SAndreas Gohr if(!file_exists($file)){ 4292adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext; 43079e79377SAndreas Gohr if(!file_exists($file)){ 431b625487dSandi //fall back to english 4322adaf2b8SAndreas Gohr $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext; 433b625487dSandi } 434e6cecb08SMichael Hamann } 435b625487dSandi return $file; 436b625487dSandi} 437b625487dSandi 438b625487dSandi/** 439c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 440c4e0e4a1SAndreas Gohr * 441c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 442c4e0e4a1SAndreas Gohr * instead 443c4e0e4a1SAndreas Gohr * 444c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 445c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016 446c4e0e4a1SAndreas Gohr * 447c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 44884657ea2SGerrit Uitslag * 44984657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 45084657ea2SGerrit Uitslag * @param string $id relative id 45184657ea2SGerrit Uitslag * @param bool $clean flag indicating that id should be cleaned 45242ea7f44SGerrit Uitslag * @return string 453c4e0e4a1SAndreas Gohr */ 454a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 455c662a49aSAndreas Gohr global $conf; 456c662a49aSAndreas Gohr 457c662a49aSAndreas Gohr // some pre cleaning for useslash: 458c662a49aSAndreas Gohr if($conf['useslash']) $id = str_replace('/',':',$id); 459c662a49aSAndreas Gohr 460c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 461c4e0e4a1SAndreas Gohr // relative stuff 462443e135dSChristopher Smith if($id && $id{0} == '.'){ 463c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 464c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 465c4e0e4a1SAndreas Gohr // prepend the current namespace 466c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 467c4e0e4a1SAndreas Gohr 468c4e0e4a1SAndreas Gohr // cleanup relatives 469c4e0e4a1SAndreas Gohr $result = array(); 470c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 471c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 472c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 473c4e0e4a1SAndreas Gohr if ($dir == '..') { 474c4e0e4a1SAndreas Gohr if (end($result) == '..') { 475c4e0e4a1SAndreas Gohr $result[] = '..'; 476c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 477c4e0e4a1SAndreas Gohr $result[] = '..'; 478c4e0e4a1SAndreas Gohr } 479c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 480c4e0e4a1SAndreas Gohr $result[] = $dir; 481c4e0e4a1SAndreas Gohr } 482c4e0e4a1SAndreas Gohr } 483c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 484c4e0e4a1SAndreas Gohr $id = implode(':', $result); 485c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 486c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 487c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 488c4e0e4a1SAndreas Gohr } 489c4e0e4a1SAndreas Gohr 490a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 491a6ef4796SAndreas Gohr return $id; 492c4e0e4a1SAndreas Gohr} 493c4e0e4a1SAndreas Gohr 494c4e0e4a1SAndreas Gohr/** 495b625487dSandi * Returns a full media id 496b625487dSandi * 497b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 49884657ea2SGerrit Uitslag * 49984657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 50084657ea2SGerrit Uitslag * @param string &$page (reference) relative media id, updated to resolved id 50184657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 5027de86af9SGerrit Uitslag * @param int|string $rev 5037de86af9SGerrit Uitslag * @param bool $date_at 504b625487dSandi */ 50590bee600Slispsfunction resolve_mediaid($ns,&$page,&$exists,$rev='',$date_at=false){ 506c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 50790bee600Slisps if($rev !== '' && $date_at){ 5081d053a56Slisps $medialog = new MediaChangeLog($page); 50990bee600Slisps $medialog_rev = $medialog->getLastRevisionAt($rev); 51090bee600Slisps if($medialog_rev !== false) { 51190bee600Slisps $rev = $medialog_rev; 51290bee600Slisps } 51390bee600Slisps } 5141d053a56Slisps 5154cd9f791Slisps $file = mediaFN($page,$rev); 51679e79377SAndreas Gohr $exists = file_exists($file); 517b625487dSandi} 518b625487dSandi 519b625487dSandi/** 520b625487dSandi * Returns a full page id 521b625487dSandi * 522b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 52384657ea2SGerrit Uitslag * 52484657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 52584657ea2SGerrit Uitslag * @param string &$page (reference) relative page id, updated to resolved id 52684657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 5277de86af9SGerrit Uitslag * @param string $rev 5287de86af9SGerrit Uitslag * @param bool $date_at 529b625487dSandi */ 53090bee600Slispsfunction resolve_pageid($ns,&$page,&$exists,$rev='',$date_at=false ){ 531b625487dSandi global $conf; 532c006739eSIzidor Matušov global $ID; 5330b7c14c2Sandi $exists = false; 534b625487dSandi 535c006739eSIzidor Matušov //empty address should point to current page 536c006739eSIzidor Matušov if ($page === "") { 537c006739eSIzidor Matušov $page = $ID; 538c006739eSIzidor Matušov } 539c006739eSIzidor Matušov 540b625487dSandi //keep hashlink if exists then clean both parts 54103c4aec3Schris if (strpos($page,'#')) { 5424b7f9e70STom N Harris list($page,$hash) = explode('#',$page,2); 54303c4aec3Schris } else { 54403c4aec3Schris $hash = ''; 54503c4aec3Schris } 546b625487dSandi $hash = cleanID($hash); 547a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 548b625487dSandi 549a6ef4796SAndreas Gohr // get filename (calls clean itself) 55090bee600Slisps if($rev !== '' && $date_at) { 55190bee600Slisps $pagelog = new PageChangeLog($page); 55290bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 55390bee600Slisps if($pagelog_rev !== false)//something found 55490bee600Slisps $rev = $pagelog_rev; 55590bee600Slisps } 5564cd9f791Slisps $file = wikiFN($page,$rev); 557b625487dSandi 5581179df0eSGuy Brand // if ends with colon or slash we have a namespace link 559b26cdbbeSAdrian Lang if(in_array(substr($page,-1), array(':', ';')) || 560b26cdbbeSAdrian Lang ($conf['useslash'] && substr($page,-1) == '/')){ 56190bee600Slisps if(page_exists($page.$conf['start'],$rev,true,$date_at)){ 562a6ef4796SAndreas Gohr // start page inside namespace 563a6ef4796SAndreas Gohr $page = $page.$conf['start']; 564a6ef4796SAndreas Gohr $exists = true; 56590bee600Slisps }elseif(page_exists($page.noNS(cleanID($page)),$rev,true,$date_at)){ 566a6ef4796SAndreas Gohr // page named like the NS inside the NS 567a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 568a6ef4796SAndreas Gohr $exists = true; 56990bee600Slisps }elseif(page_exists($page,$rev,true,$date_at)){ 570a6ef4796SAndreas Gohr // page like namespace exists 571a6ef4796SAndreas Gohr $page = $page; 572a6ef4796SAndreas Gohr $exists = true; 573a6ef4796SAndreas Gohr }else{ 574a6ef4796SAndreas Gohr // fall back to default 575a6ef4796SAndreas Gohr $page = $page.$conf['start']; 576a6ef4796SAndreas Gohr } 577a6ef4796SAndreas Gohr }else{ 578b625487dSandi //check alternative plural/nonplural form 57979e79377SAndreas Gohr if(!file_exists($file)){ 580b625487dSandi if( $conf['autoplural'] ){ 581b625487dSandi if(substr($page,-1) == 's'){ 582b625487dSandi $try = substr($page,0,-1); 583b625487dSandi }else{ 584b625487dSandi $try = $page.'s'; 585b625487dSandi } 58690bee600Slisps if(page_exists($try,$rev,true,$date_at)){ 587b625487dSandi $page = $try; 588b625487dSandi $exists = true; 589b625487dSandi } 590b625487dSandi } 591b625487dSandi }else{ 592b625487dSandi $exists = true; 593b625487dSandi } 594a6ef4796SAndreas Gohr } 595a6ef4796SAndreas Gohr 596a6ef4796SAndreas Gohr // now make sure we have a clean page 597a6ef4796SAndreas Gohr $page = cleanID($page); 598b625487dSandi 599b625487dSandi //add hash if any 600b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 601b625487dSandi} 602b625487dSandi 60398407a7aSandi/** 60498407a7aSandi * Returns the name of a cachefile from given data 60598407a7aSandi * 60698407a7aSandi * The needed directory is created by this function! 60798407a7aSandi * 60898407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 60998407a7aSandi * 61098407a7aSandi * @param string $data This data is used to create a unique md5 name 61198407a7aSandi * @param string $ext This is appended to the filename if given 61298407a7aSandi * @return string The filename of the cachefile 61398407a7aSandi */ 61498407a7aSandifunction getCacheName($data,$ext=''){ 61598407a7aSandi global $conf; 61698407a7aSandi $md5 = md5($data); 61798407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 61898407a7aSandi io_makeFileDir($file); 61998407a7aSandi return $file; 62098407a7aSandi} 62198407a7aSandi 6220dc92c6fSAndreas Gohr/** 6230dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 6240dc92c6fSAndreas Gohr * 6250dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 62684657ea2SGerrit Uitslag * 62784657ea2SGerrit Uitslag * @param string $id page id 62884657ea2SGerrit Uitslag * @return bool 6290dc92c6fSAndreas Gohr */ 6300dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 6318449cc9dSDominik Eckelmann $data = array( 6328449cc9dSDominik Eckelmann 'id' => $id, 6338449cc9dSDominik Eckelmann 'hidden' => false 6348449cc9dSDominik Eckelmann ); 635fb55b51eSDominik Eckelmann trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 636fb55b51eSDominik Eckelmann return $data['hidden']; 6370dc92c6fSAndreas Gohr} 638fb55b51eSDominik Eckelmann 639dbf714f7SGerrit Uitslag/** 640dbf714f7SGerrit Uitslag * callback checks if page is hidden 641dbf714f7SGerrit Uitslag * 64284657ea2SGerrit Uitslag * @param array $data event data - see isHiddenPage() 643dbf714f7SGerrit Uitslag */ 644fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) { 645fb55b51eSDominik Eckelmann global $conf; 646fb55b51eSDominik Eckelmann global $ACT; 647fb55b51eSDominik Eckelmann 648fb55b51eSDominik Eckelmann if ($data['hidden']) return; 649fb55b51eSDominik Eckelmann if(empty($conf['hidepages'])) return; 650fb55b51eSDominik Eckelmann if($ACT == 'admin') return; 651fb55b51eSDominik Eckelmann 652fb55b51eSDominik Eckelmann if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){ 653fb55b51eSDominik Eckelmann $data['hidden'] = true; 654fb55b51eSDominik Eckelmann } 6550dc92c6fSAndreas Gohr} 6560dc92c6fSAndreas Gohr 6570dc92c6fSAndreas Gohr/** 6580dc92c6fSAndreas Gohr * Reverse of isHiddenPage 6590dc92c6fSAndreas Gohr * 6600dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 66184657ea2SGerrit Uitslag * 66284657ea2SGerrit Uitslag * @param string $id page id 66384657ea2SGerrit Uitslag * @return bool 6640dc92c6fSAndreas Gohr */ 6650dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 6660dc92c6fSAndreas Gohr return !isHiddenPage($id); 6670dc92c6fSAndreas Gohr} 6680dc92c6fSAndreas Gohr 6695b75cd1fSAdrian Lang/** 6705b75cd1fSAdrian Lang * Format an id for output to a user 6715b75cd1fSAdrian Lang * 6725b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 6735b75cd1fSAdrian Lang * “*”. Output is escaped. 6745b75cd1fSAdrian Lang * 6755b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 67684657ea2SGerrit Uitslag * 67784657ea2SGerrit Uitslag * @param string $id page id 67884657ea2SGerrit Uitslag * @return string 6795b75cd1fSAdrian Lang */ 6805b75cd1fSAdrian Langfunction prettyprint_id($id) { 6815b75cd1fSAdrian Lang if (!$id || $id === ':') { 6825b75cd1fSAdrian Lang return '*'; 6835b75cd1fSAdrian Lang } 6845b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 6855b75cd1fSAdrian Lang $id .= '*'; 6865b75cd1fSAdrian Lang } 6875b75cd1fSAdrian Lang return hsc($id); 6885b75cd1fSAdrian Lang} 689f03fd957SAndreas Gohr 690f03fd957SAndreas Gohr/** 691f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 692f03fd957SAndreas Gohr * 693f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 694f03fd957SAndreas Gohr * 695f03fd957SAndreas Gohr * When the second parameter is true the string will 696f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 697f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 698f03fd957SAndreas Gohr * same string (default is true) 699f03fd957SAndreas Gohr * 700f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 701f03fd957SAndreas Gohr * @see urlencode 70284657ea2SGerrit Uitslag * 70384657ea2SGerrit Uitslag * @param string $file file name 70484657ea2SGerrit Uitslag * @param bool $safe if true, only encoded when non ASCII characters detected 70584657ea2SGerrit Uitslag * @return string 706f03fd957SAndreas Gohr */ 707f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){ 708f03fd957SAndreas Gohr global $conf; 709f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 710f03fd957SAndreas Gohr 711f03fd957SAndreas Gohr if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){ 712f03fd957SAndreas Gohr return $file; 713f03fd957SAndreas Gohr } 714f03fd957SAndreas Gohr 715f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 716f03fd957SAndreas Gohr return SafeFN::encode($file); 717f03fd957SAndreas Gohr } 718f03fd957SAndreas Gohr 719f03fd957SAndreas Gohr $file = urlencode($file); 720f03fd957SAndreas Gohr $file = str_replace('%2F','/',$file); 721f03fd957SAndreas Gohr return $file; 722f03fd957SAndreas Gohr} 723f03fd957SAndreas Gohr 724f03fd957SAndreas Gohr/** 725f03fd957SAndreas Gohr * Decode a filename back to UTF-8 726f03fd957SAndreas Gohr * 727f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 728f03fd957SAndreas Gohr * 729f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 730f03fd957SAndreas Gohr * @see urldecode 73184657ea2SGerrit Uitslag * 73284657ea2SGerrit Uitslag * @param string $file file name 73384657ea2SGerrit Uitslag * @return string 734f03fd957SAndreas Gohr */ 735f03fd957SAndreas Gohrfunction utf8_decodeFN($file){ 736f03fd957SAndreas Gohr global $conf; 737f03fd957SAndreas Gohr if($conf['fnencode'] == 'utf-8') return $file; 738f03fd957SAndreas Gohr 739f03fd957SAndreas Gohr if($conf['fnencode'] == 'safe'){ 740f03fd957SAndreas Gohr return SafeFN::decode($file); 741f03fd957SAndreas Gohr } 742f03fd957SAndreas Gohr 743f03fd957SAndreas Gohr return urldecode($file); 744f03fd957SAndreas Gohr} 745f03fd957SAndreas Gohr 746e66d3e6dSAndreas Gohr/** 747e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any 748cc529468SMichael Hamann * higher namespace that can be accessed by the current user, 749cc529468SMichael Hamann * this condition can be overriden by an optional parameter. 750e66d3e6dSAndreas Gohr * 751e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well 752e66d3e6dSAndreas Gohr * 753e66d3e6dSAndreas Gohr * @todo add event hook 75442ea7f44SGerrit Uitslag * 755e66d3e6dSAndreas Gohr * @param string $page the pagename you're looking for 7567c3e4a67SAndreas Gohr * @param bool $useacl only return pages readable by the current user, false to ignore ACLs 757cc529468SMichael Hamann * @return false|string the full page id of the found page, false if any 758e66d3e6dSAndreas Gohr */ 7597c3e4a67SAndreas Gohrfunction page_findnearest($page, $useacl = true){ 760c786a1b6SAnika Henke if (!$page) return false; 761e66d3e6dSAndreas Gohr global $ID; 762e66d3e6dSAndreas Gohr 763e66d3e6dSAndreas Gohr $ns = $ID; 764e66d3e6dSAndreas Gohr do { 765e66d3e6dSAndreas Gohr $ns = getNS($ns); 766cc529468SMichael Hamann $pageid = cleanID("$ns:$page"); 7677c3e4a67SAndreas Gohr if(page_exists($pageid) && (!$useacl || auth_quickaclcheck($pageid) >= AUTH_READ)){ 768e66d3e6dSAndreas Gohr return $pageid; 769e66d3e6dSAndreas Gohr } 770e66d3e6dSAndreas Gohr } while($ns); 771e66d3e6dSAndreas Gohr 772e66d3e6dSAndreas Gohr return false; 773e66d3e6dSAndreas Gohr} 774