1b625487dSandi<?php 2a5752066Sjpedryc 3b625487dSandi/** 4b625487dSandi * Utilities for handling pagenames 5b625487dSandi * 6b625487dSandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 81380fc45SAndreas Gohr * @todo Combine similar functions like {wiki,media,meta}FN() 9b625487dSandi */ 10*24870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 11*24870174SAndreas Gohruse dokuwiki\Utf8\Clean; 12*24870174SAndreas Gohruse dokuwiki\File\Resolver; 13*24870174SAndreas Gohruse dokuwiki\Extension\Event; 140c3a5702SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog; 150c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog; 162cd6cc0aSAndreas Gohruse dokuwiki\File\MediaResolver; 172cd6cc0aSAndreas Gohruse dokuwiki\File\PageResolver; 180c3a5702SAndreas Gohr 196c7843b5Sandi/** 206de3759aSAndreas Gohr * Fetch the an ID from request 216c7843b5Sandi * 226c7843b5Sandi * Uses either standard $_REQUEST variable or extracts it from 236c7843b5Sandi * the full request URI when userewrite is set to 2 246c7843b5Sandi * 2542905504SAndreas Gohr * For $param='id' $conf['start'] is returned if no id was found. 2642905504SAndreas Gohr * If the second parameter is true (default) the ID is cleaned. 276c7843b5Sandi * 286c7843b5Sandi * @author Andreas Gohr <andi@splitbrain.org> 2984657ea2SGerrit Uitslag * 3084657ea2SGerrit Uitslag * @param string $param the $_REQUEST variable name, default 'id' 3184657ea2SGerrit Uitslag * @param bool $clean if true, ID is cleaned 3242ea7f44SGerrit Uitslag * @return string 336c7843b5Sandi */ 34a5752066Sjpedrycfunction getID($param = 'id', $clean = true) 35a5752066Sjpedryc{ 36585bf44eSChristopher Smith /** @var Input $INPUT */ 377d01a0eaSTom N Harris global $INPUT; 386c7843b5Sandi global $conf; 394e90caaaSMichael Hamann global $ACT; 406c7843b5Sandi 417d01a0eaSTom N Harris $id = $INPUT->str($param); 4248665d38SAndreas Gohr 436c7843b5Sandi //construct page id from request URI 446c7843b5Sandi if (empty($id) && $conf['userewrite'] == 2) { 45585bf44eSChristopher Smith $request = $INPUT->server->str('REQUEST_URI'); 4606368e4dSMichael Hamann $script = ''; 4706368e4dSMichael Hamann 486c7843b5Sandi //get the script URL 496c7843b5Sandi if ($conf['basedir']) { 5081124000Sjan $relpath = ''; 5181124000Sjan if ($param != 'id') { 5281124000Sjan $relpath = 'lib/exe/'; 5381124000Sjan } 546ce3e5f8SAndreas Gohr $script = $conf['basedir'] . $relpath . 55*24870174SAndreas Gohr PhpString::basename($INPUT->server->str('SCRIPT_FILENAME')); 56585bf44eSChristopher Smith } elseif ($INPUT->server->str('PATH_INFO')) { 57585bf44eSChristopher Smith $request = $INPUT->server->str('PATH_INFO'); 58585bf44eSChristopher Smith } elseif ($INPUT->server->str('SCRIPT_NAME')) { 59585bf44eSChristopher Smith $script = $INPUT->server->str('SCRIPT_NAME'); 60585bf44eSChristopher Smith } elseif ($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')) { 61a5752066Sjpedryc $script = preg_replace( 62a5752066Sjpedryc '/^' . preg_quote($INPUT->server->str('DOCUMENT_ROOT'), '/') . '/', 63a5752066Sjpedryc '', 64a5752066Sjpedryc $INPUT->server->str('SCRIPT_FILENAME') 65a5752066Sjpedryc ); 666c7843b5Sandi $script = '/' . $script; 676c7843b5Sandi } 686c7843b5Sandi 6952339126Sandi //clean script and request (fixes a windows problem) 7052339126Sandi $script = preg_replace('/\/\/+/', '/', $script); 717d71d4b7SAndreas Gohr $request = preg_replace('/\/\/+/', '/', $request); 7252339126Sandi 736c7843b5Sandi //remove script URL and Querystring to gain the id 7452339126Sandi if (preg_match('/^' . preg_quote($script, '/') . '(.*)/', $request, $match)) { 756c7843b5Sandi $id = preg_replace('/\?.*/', '', $match[1]); 766c7843b5Sandi } 776de3759aSAndreas Gohr $id = urldecode($id); 7842905504SAndreas Gohr //strip leading slashes 7942905504SAndreas Gohr $id = preg_replace('!^/+!', '', $id); 806c7843b5Sandi } 81671a58a6SGuy Brand 82671a58a6SGuy Brand // Namespace autolinking from URL 83b6084253SAndreas Gohr if (substr($id, -1) == ':' || ($conf['useslash'] && substr($id, -1) == '/')) { 84103c256aSChris Smith if (page_exists($id . $conf['start'])) { 85671a58a6SGuy Brand // start page inside namespace 86*24870174SAndreas Gohr $id .= $conf['start']; 87103c256aSChris Smith } elseif (page_exists($id . noNS(cleanID($id)))) { 88671a58a6SGuy Brand // page named like the NS inside the NS 89*24870174SAndreas Gohr $id .= noNS(cleanID($id)); 90103c256aSChris Smith } elseif (page_exists($id)) { 91671a58a6SGuy Brand // page like namespace exists 927a42ac9eSBen Coburn $id = substr($id, 0, -1); 93671a58a6SGuy Brand } else { 94671a58a6SGuy Brand // fall back to default 95*24870174SAndreas Gohr $id .= $conf['start']; 96671a58a6SGuy Brand } 979dc53973SMichael Grosse if (isset($ACT) && $ACT === 'show') { 989dc53973SMichael Grosse $urlParameters = $_GET; 99e380abb2SMichael Grosse if (isset($urlParameters['id'])) { 100e380abb2SMichael Grosse unset($urlParameters['id']); 101e380abb2SMichael Grosse } 102e9fede20SPhy send_redirect(wl($id, $urlParameters, true, '&')); 1039dc53973SMichael Grosse } 104671a58a6SGuy Brand } 10542905504SAndreas Gohr if ($clean) $id = cleanID($id); 10640b5fb5bSPhy if ($id === '' && $param == 'id') $id = $conf['start']; 1076c7843b5Sandi 1086c7843b5Sandi return $id; 1096c7843b5Sandi} 110b625487dSandi 111b625487dSandi/** 112b625487dSandi * Remove unwanted chars from ID 113b625487dSandi * 114b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are 115b625487dSandi * converted to unaccented ones 116b625487dSandi * 117b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 11842ea7f44SGerrit Uitslag * 1196e0cc83aSchris * @param string $raw_id The pageid to clean 1208a831f2bSAndreas Gohr * @param boolean $ascii Force ASCII 121dbf714f7SGerrit Uitslag * @return string cleaned id 122b625487dSandi */ 123a5752066Sjpedrycfunction cleanID($raw_id, $ascii = false) 124a5752066Sjpedryc{ 125b625487dSandi global $conf; 1264b5db43bSjoe.lapp static $sepcharpat = null; 1274b5db43bSjoe.lapp 128dc2c0e04Schris global $cache_cleanid; 129dc2c0e04Schris $cache = & $cache_cleanid; 1306e0cc83aSchris 1316e0cc83aSchris // check if it's already in the memory cache 13230f3bd15SMichael Grosse if (!$ascii && isset($cache[(string)$raw_id])) { 1333a50618cSgweissbach return $cache[(string)$raw_id]; 1346e0cc83aSchris } 1356e0cc83aSchris 1364b5db43bSjoe.lapp $sepchar = $conf['sepchar']; 1374b5db43bSjoe.lapp if ($sepcharpat == null) // build string only once to save clock cycles 1384b5db43bSjoe.lapp $sepcharpat = '#\\' . $sepchar . '+#'; 1394b5db43bSjoe.lapp 1403a50618cSgweissbach $id = trim((string)$raw_id); 141*24870174SAndreas Gohr $id = PhpString::strtolower($id); 142b625487dSandi 143b625487dSandi //alternative namespace seperator 144b625487dSandi if ($conf['useslash']) { 1453755fc25STom N Harris $id = strtr($id, ';/', '::'); 146b625487dSandi } else { 1473755fc25STom N Harris $id = strtr($id, ';/', ':' . $sepchar); 148b625487dSandi } 149b625487dSandi 150*24870174SAndreas Gohr if ($conf['deaccent'] == 2 || $ascii) $id = Clean::romanize($id); 151*24870174SAndreas Gohr if ($conf['deaccent'] || $ascii) $id = Clean::deaccent($id, -1); 152b625487dSandi 153b625487dSandi //remove specials 154*24870174SAndreas Gohr $id = Clean::stripspecials($id, $sepchar, '\*'); 155b625487dSandi 156*24870174SAndreas Gohr if ($ascii) $id = Clean::strip($id); 1578a831f2bSAndreas Gohr 158b625487dSandi //clean up 1594b5db43bSjoe.lapp $id = preg_replace($sepcharpat, $sepchar, $id); 160b625487dSandi $id = preg_replace('#:+#', ':', $id); 1613543c6deSAndreas Gohr $id = trim($id, ':._-'); 162b625487dSandi $id = preg_replace('#:[:\._\-]+#', ':', $id); 163b680ea06SAndreas Gohr $id = preg_replace('#[:\._\-]+:#', ':', $id); 164b625487dSandi 16530f3bd15SMichael Grosse if (!$ascii) $cache[(string)$raw_id] = $id; 166b625487dSandi return($id); 167b625487dSandi} 168b625487dSandi 169b625487dSandi/** 170b625487dSandi * Return namespacepart of a wiki ID 171b625487dSandi * 172b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 17315851b98SGerrit Uitslag * 17415851b98SGerrit Uitslag * @param string $id 17542ea7f44SGerrit Uitslag * @return string|false the namespace part or false if the given ID has no namespace (root) 176b625487dSandi */ 177a5752066Sjpedrycfunction getNS($id) 178a5752066Sjpedryc{ 1793a50618cSgweissbach $pos = strrpos((string)$id, ':'); 180c4e0e4a1SAndreas Gohr if ($pos !== false) { 1813a50618cSgweissbach return substr((string)$id, 0, $pos); 182b625487dSandi } 183ef11fcfcSAndreas Gohr return false; 184b625487dSandi} 185b625487dSandi 186b625487dSandi/** 187b625487dSandi * Returns the ID without the namespace 188b625487dSandi * 189b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 19015851b98SGerrit Uitslag * 19115851b98SGerrit Uitslag * @param string $id 19215851b98SGerrit Uitslag * @return string 193b625487dSandi */ 194a5752066Sjpedrycfunction noNS($id) 195a5752066Sjpedryc{ 1962844584fSBen Coburn $pos = strrpos($id, ':'); 1972844584fSBen Coburn if ($pos !== false) { 1982844584fSBen Coburn return substr($id, $pos + 1); 1992844584fSBen Coburn } else { 2002844584fSBen Coburn return $id; 2012844584fSBen Coburn } 2021a84a0f3SAnika Henke} 2031a84a0f3SAnika Henke 2041a84a0f3SAnika Henke/** 2051a84a0f3SAnika Henke * Returns the current namespace 2061a84a0f3SAnika Henke * 2071a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 20884657ea2SGerrit Uitslag * 20984657ea2SGerrit Uitslag * @param string $id 21084657ea2SGerrit Uitslag * @return string 2111a84a0f3SAnika Henke */ 212a5752066Sjpedrycfunction curNS($id) 213a5752066Sjpedryc{ 2141a84a0f3SAnika Henke return noNS(getNS($id)); 2151a84a0f3SAnika Henke} 2161a84a0f3SAnika Henke 2171a84a0f3SAnika Henke/** 2181a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages 2191a84a0f3SAnika Henke * 2201a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu> 22184657ea2SGerrit Uitslag * 22284657ea2SGerrit Uitslag * @param string $id 22384657ea2SGerrit Uitslag * @return string 2241a84a0f3SAnika Henke */ 225a5752066Sjpedrycfunction noNSorNS($id) 226a5752066Sjpedryc{ 2271a84a0f3SAnika Henke global $conf; 2281a84a0f3SAnika Henke 2291a84a0f3SAnika Henke $p = noNS($id); 230c077d4dcSAndreas Gohr if ($p === $conf['start'] || $p === false || $p === '') { 2311a84a0f3SAnika Henke $p = curNS($id); 232c077d4dcSAndreas Gohr if ($p === false || $p === '') { 2339708106bSAdrian Lang return $conf['start']; 2341a84a0f3SAnika Henke } 2351a84a0f3SAnika Henke } 2361a84a0f3SAnika Henke return $p; 237b625487dSandi} 2384ceab83fSAndreas Gohr 2394ceab83fSAndreas Gohr/** 2404ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title 2414ceab83fSAndreas Gohr * 2424ceab83fSAndreas Gohr * @param string $title The headline title 2434f582736SGuillaume Turri * @param array|bool $check Existing IDs 244c857afe0SMichael Hamann * @return string the title 24584657ea2SGerrit Uitslag * 2464ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 2474ceab83fSAndreas Gohr */ 248a5752066Sjpedrycfunction sectionID($title, &$check) 249a5752066Sjpedryc{ 250*24870174SAndreas Gohr $title = str_replace([':', '.'], '', cleanID($title)); 251de9114eaSAnika Henke $new = ltrim($title, '0123456789_-'); 2524ceab83fSAndreas Gohr if (empty($new)) { 2534ceab83fSAndreas Gohr $title = 'section' . preg_replace('/[^0-9]+/', '', $title); //keep numbers from headline 2544ceab83fSAndreas Gohr } else { 2554ceab83fSAndreas Gohr $title = $new; 2564ceab83fSAndreas Gohr } 2574ceab83fSAndreas Gohr 258443d207bSAndreas Gohr if (is_array($check)) { 2594f582736SGuillaume Turri $suffix = 0; 2604f582736SGuillaume Turri $candidateTitle = $title; 2614f582736SGuillaume Turri while (in_array($candidateTitle, $check)) { 2624f582736SGuillaume Turri $candidateTitle = $title . ++$suffix; 2634f582736SGuillaume Turri } 2644f582736SGuillaume Turri $check [] = $candidateTitle; 2654f582736SGuillaume Turri return $candidateTitle; 26601e3159cSChris Tapp } else { 2674ceab83fSAndreas Gohr return $title; 2684ceab83fSAndreas Gohr } 2694f582736SGuillaume Turri} 2704ceab83fSAndreas Gohr 271b625487dSandi/** 272103c256aSChris Smith * Wiki page existence check 273103c256aSChris Smith * 274103c256aSChris Smith * parameters as for wikiFN 275103c256aSChris Smith * 276103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk> 27784657ea2SGerrit Uitslag * 27884657ea2SGerrit Uitslag * @param string $id page id 27984657ea2SGerrit Uitslag * @param string|int $rev empty or revision timestamp 28084657ea2SGerrit Uitslag * @param bool $clean flag indicating that $id should be cleaned (see wikiFN as well) 2817de86af9SGerrit Uitslag * @param bool $date_at 28284657ea2SGerrit Uitslag * @return bool exists? 283103c256aSChris Smith */ 284a5752066Sjpedrycfunction page_exists($id, $rev = '', $clean = true, $date_at = false) 285a5752066Sjpedryc{ 28692085f13SAndreas Gohr $id = (explode('#', $id, 2))[0]; // #3608 28792085f13SAndreas Gohr 28890bee600Slisps if ($rev !== '' && $date_at) { 2891d053a56Slisps $pagelog = new PageChangeLog($id); 29090bee600Slisps $pagelog_rev = $pagelog->getLastRevisionAt($rev); 29190bee600Slisps if ($pagelog_rev !== false) 29290bee600Slisps $rev = $pagelog_rev; 29390bee600Slisps } 29479e79377SAndreas Gohr return file_exists(wikiFN($id, $rev, $clean)); 295103c256aSChris Smith} 296103c256aSChris Smith 297103c256aSChris Smith/** 2985c844bb3SAndreas Gohr * Media existence check 2995c844bb3SAndreas Gohr * 3005c844bb3SAndreas Gohr * @param string $id page id 3015c844bb3SAndreas Gohr * @param string|int $rev empty or revision timestamp 3025c844bb3SAndreas Gohr * @param bool $clean flag indicating that $id should be cleaned (see mediaFN as well) 3035c844bb3SAndreas Gohr * @param bool $date_at 3045c844bb3SAndreas Gohr * @return bool exists? 3055c844bb3SAndreas Gohr */ 3065c844bb3SAndreas Gohrfunction media_exists($id, $rev = '', $clean = true, $date_at = false) 3075c844bb3SAndreas Gohr{ 3085c844bb3SAndreas Gohr if ($rev !== '' && $date_at) { 3095c844bb3SAndreas Gohr $changeLog = new MediaChangeLog($id); 3105c844bb3SAndreas Gohr $changelog_rev = $changeLog->getLastRevisionAt($rev); 3115c844bb3SAndreas Gohr if ($changelog_rev !== false) { 3125c844bb3SAndreas Gohr $rev = $changelog_rev; 3135c844bb3SAndreas Gohr } 3145c844bb3SAndreas Gohr } 3155c844bb3SAndreas Gohr return file_exists(mediaFN($id, $rev, $clean)); 3165c844bb3SAndreas Gohr} 3175c844bb3SAndreas Gohr 3185c844bb3SAndreas Gohr/** 319103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision 320b625487dSandi * 321b625487dSandi * The filename is URL encoded to protect Unicode chars 322b625487dSandi * 323103c256aSChris Smith * @param $raw_id string id of wikipage 324e0c26282SGerrit Uitslag * @param $rev int|string page revision, empty string for current 325103c256aSChris Smith * @param $clean bool flag indicating that $raw_id should be cleaned. Only set to false 326103c256aSChris Smith * when $id is guaranteed to have been cleaned already. 327dbf714f7SGerrit Uitslag * @return string full path 328103c256aSChris Smith * 329b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 330b625487dSandi */ 331a5752066Sjpedrycfunction wikiFN($raw_id, $rev = '', $clean = true) 332a5752066Sjpedryc{ 333b625487dSandi global $conf; 3346e0cc83aSchris 335dc2c0e04Schris global $cache_wikifn; 336dc2c0e04Schris $cache = & $cache_wikifn; 337dc2c0e04Schris 3386e0cc83aSchris $id = $raw_id; 3396e0cc83aSchris 3400d8ea614Schris if ($clean) $id = cleanID($id); 341b625487dSandi $id = str_replace(':', '/', $id); 342b018ecbeSMichael Grosse 343b018ecbeSMichael Grosse if (isset($cache[$id]) && isset($cache[$id][$rev])) { 344b018ecbeSMichael Grosse return $cache[$id][$rev]; 345b018ecbeSMichael Grosse } 346b018ecbeSMichael Grosse 347b625487dSandi if (empty($rev)) { 348b625487dSandi $fn = $conf['datadir'] . '/' . utf8_encodeFN($id) . '.txt'; 349b625487dSandi } else { 350b625487dSandi $fn = $conf['olddir'] . '/' . utf8_encodeFN($id) . '.' . $rev . '.txt'; 351ff3ed99fSmarcel if ($conf['compression']) { 352ff3ed99fSmarcel //test for extensions here, we want to read both compressions 35379e79377SAndreas Gohr if (file_exists($fn . '.gz')) { 354b625487dSandi $fn .= '.gz'; 35579e79377SAndreas Gohr } elseif (file_exists($fn . '.bz2')) { 356ff3ed99fSmarcel $fn .= '.bz2'; 357ff3ed99fSmarcel } else { 358ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 359ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 360ff3ed99fSmarcel } 361b625487dSandi } 362b625487dSandi } 3636e0cc83aSchris 364a5752066Sjpedryc if (!isset($cache[$id])) { 365*24870174SAndreas Gohr $cache[$id] = []; 366a5752066Sjpedryc } 367b018ecbeSMichael Grosse $cache[$id][$rev] = $fn; 368b625487dSandi return $fn; 369b625487dSandi} 370b625487dSandi 371b625487dSandi/** 372c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 373c9b4bd1eSBen Coburn * 374c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 37584657ea2SGerrit Uitslag * 37684657ea2SGerrit Uitslag * @param string $id page id 37784657ea2SGerrit Uitslag * @return string full path 378c9b4bd1eSBen Coburn */ 379a5752066Sjpedrycfunction wikiLockFN($id) 380a5752066Sjpedryc{ 381c9b4bd1eSBen Coburn global $conf; 382662ff478SAndreas Gohr return $conf['lockdir'] . '/' . md5(cleanID($id)) . '.lock'; 383c9b4bd1eSBen Coburn} 384c9b4bd1eSBen Coburn 385c9b4bd1eSBen Coburn 386c9b4bd1eSBen Coburn/** 3871380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 388b158d625SSteven Danz * 389b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 39084657ea2SGerrit Uitslag * 39184657ea2SGerrit Uitslag * @param string $id page id 39284657ea2SGerrit Uitslag * @param string $ext file extension 39384657ea2SGerrit Uitslag * @return string full path 394b158d625SSteven Danz */ 395a5752066Sjpedrycfunction metaFN($id, $ext) 396a5752066Sjpedryc{ 397b158d625SSteven Danz global $conf; 398b158d625SSteven Danz $id = cleanID($id); 399b158d625SSteven Danz $id = str_replace(':', '/', $id); 400*24870174SAndreas Gohr 4011380fc45SAndreas Gohr $fn = $conf['metadir'] . '/' . utf8_encodeFN($id) . $ext; 402b158d625SSteven Danz return $fn; 403b158d625SSteven Danz} 404b158d625SSteven Danz 405b158d625SSteven Danz/** 406e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension 407e4f389efSKate Arzamastseva * 408cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 40984657ea2SGerrit Uitslag * 41084657ea2SGerrit Uitslag * @param string $id media id 41184657ea2SGerrit Uitslag * @param string $ext extension of media 41284657ea2SGerrit Uitslag * @return string 413e4f389efSKate Arzamastseva */ 414a5752066Sjpedrycfunction mediaMetaFN($id, $ext) 415a5752066Sjpedryc{ 416e4f389efSKate Arzamastseva global $conf; 417e4f389efSKate Arzamastseva $id = cleanID($id); 418e4f389efSKate Arzamastseva $id = str_replace(':', '/', $id); 419*24870174SAndreas Gohr 420e4f389efSKate Arzamastseva $fn = $conf['mediametadir'] . '/' . utf8_encodeFN($id) . $ext; 421e4f389efSKate Arzamastseva return $fn; 422e4f389efSKate Arzamastseva} 423e4f389efSKate Arzamastseva 424e4f389efSKate Arzamastseva/** 425e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 426e1f3d9e1SEsther Brunner * 427e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 428ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de> 42984657ea2SGerrit Uitslag * 43084657ea2SGerrit Uitslag * @param string $id page id 43184657ea2SGerrit Uitslag * @return array 432e1f3d9e1SEsther Brunner */ 433a5752066Sjpedrycfunction metaFiles($id) 434a5752066Sjpedryc{ 435ba0267b3SMichael Hamann $basename = metaFN($id, ''); 436ba0267b3SMichael Hamann $files = glob($basename . '.*', GLOB_MARK); 437ba0267b3SMichael Hamann // filter files like foo.bar.meta when $id == 'foo' 438*24870174SAndreas Gohr return $files ? preg_grep('/^' . preg_quote($basename, '/') . '\.[^.\/]*$/u', $files) : []; 439e1f3d9e1SEsther Brunner} 440e1f3d9e1SEsther Brunner 441e1f3d9e1SEsther Brunner/** 442b625487dSandi * returns the full path to the mediafile specified by ID 443b625487dSandi * 444b625487dSandi * The filename is URL encoded to protect Unicode chars 445b625487dSandi * 446b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 447cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net> 44884657ea2SGerrit Uitslag * 44984657ea2SGerrit Uitslag * @param string $id media id 45084657ea2SGerrit Uitslag * @param string|int $rev empty string or revision timestamp 451f50a239bSTakamura * @param bool $clean 452f50a239bSTakamura * 45384657ea2SGerrit Uitslag * @return string full path 454b625487dSandi */ 455a5752066Sjpedrycfunction mediaFN($id, $rev = '', $clean = true) 456a5752066Sjpedryc{ 457b625487dSandi global $conf; 458d0e997c6SMichael Große if ($clean) $id = cleanID($id); 459b625487dSandi $id = str_replace(':', '/', $id); 460e4f389efSKate Arzamastseva if (empty($rev)) { 461b625487dSandi $fn = $conf['mediadir'] . '/' . utf8_encodeFN($id); 462e4f389efSKate Arzamastseva } else { 463cbe26ad6SKate Arzamastseva $ext = mimetype($id); 4648e69fd30SKate Arzamastseva $name = substr($id, 0, -1 * strlen($ext[0]) - 1); 46561f1aad8SKate Arzamastseva $fn = $conf['mediaolddir'] . '/' . utf8_encodeFN($name . '.' . ( (int) $rev ) . '.' . $ext[0]); 466e4f389efSKate Arzamastseva } 467b625487dSandi return $fn; 468b625487dSandi} 469b625487dSandi 470b625487dSandi/** 4712adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local 472b625487dSandi * version isn't found the english one is returned 473b625487dSandi * 4742adaf2b8SAndreas Gohr * @param string $id The id of the local file 4752adaf2b8SAndreas Gohr * @param string $ext The file extension (usually txt) 476dbf714f7SGerrit Uitslag * @return string full filepath to localized file 47784657ea2SGerrit Uitslag * 478b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 479b625487dSandi */ 480a5752066Sjpedrycfunction localeFN($id, $ext = 'txt') 481a5752066Sjpedryc{ 482b625487dSandi global $conf; 4838819fbf5ShArpanet $file = DOKU_CONF . 'lang/' . $conf['lang'] . '/' . $id . '.' . $ext; 48479e79377SAndreas Gohr if (!file_exists($file)) { 4852adaf2b8SAndreas Gohr $file = DOKU_INC . 'inc/lang/' . $conf['lang'] . '/' . $id . '.' . $ext; 48679e79377SAndreas Gohr if (!file_exists($file)) { 487b625487dSandi //fall back to english 4882adaf2b8SAndreas Gohr $file = DOKU_INC . 'inc/lang/en/' . $id . '.' . $ext; 489b625487dSandi } 490e6cecb08SMichael Hamann } 491b625487dSandi return $file; 492b625487dSandi} 493b625487dSandi 494b625487dSandi/** 495c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 496c4e0e4a1SAndreas Gohr * 497c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 498c4e0e4a1SAndreas Gohr * instead 499c4e0e4a1SAndreas Gohr * 500c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 50159752844SAnders Sandblad * http://php.net/manual/en/function.realpath.php#57016 502c4e0e4a1SAndreas Gohr * 503bfcf8009SAndreas Gohr * @deprecated 2020-09-30 50484657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 50584657ea2SGerrit Uitslag * @param string $id relative id 50684657ea2SGerrit Uitslag * @param bool $clean flag indicating that id should be cleaned 50742ea7f44SGerrit Uitslag * @return string 508c4e0e4a1SAndreas Gohr */ 509a5752066Sjpedrycfunction resolve_id($ns, $id, $clean = true) 510a5752066Sjpedryc{ 511c662a49aSAndreas Gohr global $conf; 512*24870174SAndreas Gohr dbg_deprecated(Resolver::class . ' and its children'); 513c662a49aSAndreas Gohr 514c662a49aSAndreas Gohr // some pre cleaning for useslash: 515c662a49aSAndreas Gohr if ($conf['useslash']) $id = str_replace('/', ':', $id); 516c662a49aSAndreas Gohr 517c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 518c4e0e4a1SAndreas Gohr // relative stuff 5192401f18dSSyntaxseed if ($id && $id[0] == '.') { 520c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 5214986a584SPhy $id = preg_replace('/^((\.+:)*)(\.+)(?=[^:\.])/', '\1\3:', $id); 522c4e0e4a1SAndreas Gohr // prepend the current namespace 523c4e0e4a1SAndreas Gohr $id = $ns . ':' . $id; 524c4e0e4a1SAndreas Gohr 525c4e0e4a1SAndreas Gohr // cleanup relatives 526*24870174SAndreas Gohr $result = []; 527c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 528c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 529*24870174SAndreas Gohr foreach ($pathA as $dir) { 530c4e0e4a1SAndreas Gohr if ($dir == '..') { 531c4e0e4a1SAndreas Gohr if (end($result) == '..') { 532c4e0e4a1SAndreas Gohr $result[] = '..'; 533c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 534c4e0e4a1SAndreas Gohr $result[] = '..'; 535c4e0e4a1SAndreas Gohr } 536c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 537c4e0e4a1SAndreas Gohr $result[] = $dir; 538c4e0e4a1SAndreas Gohr } 539c4e0e4a1SAndreas Gohr } 540c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 541c4e0e4a1SAndreas Gohr $id = implode(':', $result); 542c4e0e4a1SAndreas Gohr } elseif ($ns !== false && strpos($id, ':') === false) { 543c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 544c4e0e4a1SAndreas Gohr $id = $ns . ':' . $id; 545c4e0e4a1SAndreas Gohr } 546c4e0e4a1SAndreas Gohr 547a6ef4796SAndreas Gohr if ($clean) $id = cleanID($id); 548a6ef4796SAndreas Gohr return $id; 549c4e0e4a1SAndreas Gohr} 550c4e0e4a1SAndreas Gohr 551c4e0e4a1SAndreas Gohr/** 552b625487dSandi * Returns a full media id 553b625487dSandi * 55484657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 5555c844bb3SAndreas Gohr * @param string &$media (reference) relative media id, updated to resolved id 55684657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 5577de86af9SGerrit Uitslag * @param int|string $rev 5587de86af9SGerrit Uitslag * @param bool $date_at 5595c844bb3SAndreas Gohr * @deprecated 2020-09-30 560b625487dSandi */ 561a5752066Sjpedrycfunction resolve_mediaid($ns, &$media, &$exists, $rev = '', $date_at = false) 562a5752066Sjpedryc{ 5635c844bb3SAndreas Gohr dbg_deprecated(MediaResolver::class); 5645c844bb3SAndreas Gohr $resolver = new MediaResolver("$ns:deprecated"); 5655c844bb3SAndreas Gohr $media = $resolver->resolveId($media, $rev, $date_at); 5665c844bb3SAndreas Gohr $exists = media_exists($media, $rev, false, $date_at); 567b625487dSandi} 568b625487dSandi 569b625487dSandi/** 570b625487dSandi * Returns a full page id 571b625487dSandi * 572bfcf8009SAndreas Gohr * @deprecated 2020-09-30 57384657ea2SGerrit Uitslag * @param string $ns namespace which is context of id 57484657ea2SGerrit Uitslag * @param string &$page (reference) relative page id, updated to resolved id 57584657ea2SGerrit Uitslag * @param bool &$exists (reference) updated with existance of media 5767de86af9SGerrit Uitslag * @param string $rev 5777de86af9SGerrit Uitslag * @param bool $date_at 578b625487dSandi */ 579bfcf8009SAndreas Gohrfunction resolve_pageid($ns, &$page, &$exists, $rev = '', $date_at = false) 580bfcf8009SAndreas Gohr{ 5818c6be208SAndreas Gohr dbg_deprecated(PageResolver::class); 58254611a7aSAndreas Gohr 58354611a7aSAndreas Gohr global $ID; 58454611a7aSAndreas Gohr if (getNS($ID) == $ns) { 58554611a7aSAndreas Gohr $context = $ID; // this is usually the case 58654611a7aSAndreas Gohr } else { 58754611a7aSAndreas Gohr $context = "$ns:deprecated"; // only used when a different context namespace was given 58854611a7aSAndreas Gohr } 58954611a7aSAndreas Gohr 59054611a7aSAndreas Gohr $resolver = new PageResolver($context); 591bfcf8009SAndreas Gohr $page = $resolver->resolveId($page, $rev, $date_at); 592bfcf8009SAndreas Gohr $exists = page_exists($page, $rev, false, $date_at); 593b625487dSandi} 594b625487dSandi 59598407a7aSandi/** 59698407a7aSandi * Returns the name of a cachefile from given data 59798407a7aSandi * 59898407a7aSandi * The needed directory is created by this function! 59998407a7aSandi * 60098407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 60198407a7aSandi * 60298407a7aSandi * @param string $data This data is used to create a unique md5 name 60398407a7aSandi * @param string $ext This is appended to the filename if given 60498407a7aSandi * @return string The filename of the cachefile 60598407a7aSandi */ 606a5752066Sjpedrycfunction getCacheName($data, $ext = '') 607a5752066Sjpedryc{ 60898407a7aSandi global $conf; 60998407a7aSandi $md5 = md5($data); 6102401f18dSSyntaxseed $file = $conf['cachedir'] . '/' . $md5[0] . '/' . $md5 . $ext; 61198407a7aSandi io_makeFileDir($file); 61298407a7aSandi return $file; 61398407a7aSandi} 61498407a7aSandi 6150dc92c6fSAndreas Gohr/** 6160dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 6170dc92c6fSAndreas Gohr * 6180dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 61984657ea2SGerrit Uitslag * 62084657ea2SGerrit Uitslag * @param string $id page id 62184657ea2SGerrit Uitslag * @return bool 6220dc92c6fSAndreas Gohr */ 623a5752066Sjpedrycfunction isHiddenPage($id) 624a5752066Sjpedryc{ 625*24870174SAndreas Gohr $data = ['id' => $id, 'hidden' => false]; 626*24870174SAndreas Gohr Event::createAndTrigger('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage'); 627fb55b51eSDominik Eckelmann return $data['hidden']; 6280dc92c6fSAndreas Gohr} 629fb55b51eSDominik Eckelmann 630dbf714f7SGerrit Uitslag/** 631dbf714f7SGerrit Uitslag * callback checks if page is hidden 632dbf714f7SGerrit Uitslag * 63384657ea2SGerrit Uitslag * @param array $data event data - see isHiddenPage() 634dbf714f7SGerrit Uitslag */ 635a5752066Sjpedrycfunction _isHiddenPage(&$data) 636a5752066Sjpedryc{ 637fb55b51eSDominik Eckelmann global $conf; 638fb55b51eSDominik Eckelmann global $ACT; 639fb55b51eSDominik Eckelmann 640fb55b51eSDominik Eckelmann if ($data['hidden']) return; 641fb55b51eSDominik Eckelmann if (empty($conf['hidepages'])) return; 642fb55b51eSDominik Eckelmann if ($ACT == 'admin') return; 643fb55b51eSDominik Eckelmann 644fb55b51eSDominik Eckelmann if (preg_match('/' . $conf['hidepages'] . '/ui', ':' . $data['id'])) { 645fb55b51eSDominik Eckelmann $data['hidden'] = true; 646fb55b51eSDominik Eckelmann } 6470dc92c6fSAndreas Gohr} 6480dc92c6fSAndreas Gohr 6490dc92c6fSAndreas Gohr/** 6500dc92c6fSAndreas Gohr * Reverse of isHiddenPage 6510dc92c6fSAndreas Gohr * 6520dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 65384657ea2SGerrit Uitslag * 65484657ea2SGerrit Uitslag * @param string $id page id 65584657ea2SGerrit Uitslag * @return bool 6560dc92c6fSAndreas Gohr */ 657a5752066Sjpedrycfunction isVisiblePage($id) 658a5752066Sjpedryc{ 6590dc92c6fSAndreas Gohr return !isHiddenPage($id); 6600dc92c6fSAndreas Gohr} 6610dc92c6fSAndreas Gohr 6625b75cd1fSAdrian Lang/** 6635b75cd1fSAdrian Lang * Format an id for output to a user 6645b75cd1fSAdrian Lang * 6655b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is 6665b75cd1fSAdrian Lang * “*”. Output is escaped. 6675b75cd1fSAdrian Lang * 6685b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de> 66984657ea2SGerrit Uitslag * 67084657ea2SGerrit Uitslag * @param string $id page id 67184657ea2SGerrit Uitslag * @return string 6725b75cd1fSAdrian Lang */ 673a5752066Sjpedrycfunction prettyprint_id($id) 674a5752066Sjpedryc{ 6755b75cd1fSAdrian Lang if (!$id || $id === ':') { 6765b75cd1fSAdrian Lang return '*'; 6775b75cd1fSAdrian Lang } 6785b75cd1fSAdrian Lang if ((substr($id, -1, 1) === ':')) { 6795b75cd1fSAdrian Lang $id .= '*'; 6805b75cd1fSAdrian Lang } 6815b75cd1fSAdrian Lang return hsc($id); 6825b75cd1fSAdrian Lang} 683f03fd957SAndreas Gohr 684f03fd957SAndreas Gohr/** 685f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem 686f03fd957SAndreas Gohr * 687f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 688f03fd957SAndreas Gohr * 689f03fd957SAndreas Gohr * When the second parameter is true the string will 690f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected - 691f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the 692f03fd957SAndreas Gohr * same string (default is true) 693f03fd957SAndreas Gohr * 694f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 695f03fd957SAndreas Gohr * @see urlencode 69684657ea2SGerrit Uitslag * 69784657ea2SGerrit Uitslag * @param string $file file name 69884657ea2SGerrit Uitslag * @param bool $safe if true, only encoded when non ASCII characters detected 69984657ea2SGerrit Uitslag * @return string 700f03fd957SAndreas Gohr */ 701a5752066Sjpedrycfunction utf8_encodeFN($file, $safe = true) 702a5752066Sjpedryc{ 703f03fd957SAndreas Gohr global $conf; 704f03fd957SAndreas Gohr if ($conf['fnencode'] == 'utf-8') return $file; 705f03fd957SAndreas Gohr 706f03fd957SAndreas Gohr if ($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#', $file)) { 707f03fd957SAndreas Gohr return $file; 708f03fd957SAndreas Gohr } 709f03fd957SAndreas Gohr 710f03fd957SAndreas Gohr if ($conf['fnencode'] == 'safe') { 711f03fd957SAndreas Gohr return SafeFN::encode($file); 712f03fd957SAndreas Gohr } 713f03fd957SAndreas Gohr 714f03fd957SAndreas Gohr $file = urlencode($file); 715f03fd957SAndreas Gohr $file = str_replace('%2F', '/', $file); 716f03fd957SAndreas Gohr return $file; 717f03fd957SAndreas Gohr} 718f03fd957SAndreas Gohr 719f03fd957SAndreas Gohr/** 720f03fd957SAndreas Gohr * Decode a filename back to UTF-8 721f03fd957SAndreas Gohr * 722f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding 723f03fd957SAndreas Gohr * 724f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 725f03fd957SAndreas Gohr * @see urldecode 72684657ea2SGerrit Uitslag * 72784657ea2SGerrit Uitslag * @param string $file file name 72884657ea2SGerrit Uitslag * @return string 729f03fd957SAndreas Gohr */ 730a5752066Sjpedrycfunction utf8_decodeFN($file) 731a5752066Sjpedryc{ 732f03fd957SAndreas Gohr global $conf; 733f03fd957SAndreas Gohr if ($conf['fnencode'] == 'utf-8') return $file; 734f03fd957SAndreas Gohr 735f03fd957SAndreas Gohr if ($conf['fnencode'] == 'safe') { 736f03fd957SAndreas Gohr return SafeFN::decode($file); 737f03fd957SAndreas Gohr } 738f03fd957SAndreas Gohr 739f03fd957SAndreas Gohr return urldecode($file); 740f03fd957SAndreas Gohr} 741f03fd957SAndreas Gohr 742e66d3e6dSAndreas Gohr/** 743e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any 744cc529468SMichael Hamann * higher namespace that can be accessed by the current user, 745cc529468SMichael Hamann * this condition can be overriden by an optional parameter. 746e66d3e6dSAndreas Gohr * 747e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well 748e66d3e6dSAndreas Gohr * 749e66d3e6dSAndreas Gohr * @todo add event hook 75042ea7f44SGerrit Uitslag * 751e66d3e6dSAndreas Gohr * @param string $page the pagename you're looking for 7527c3e4a67SAndreas Gohr * @param bool $useacl only return pages readable by the current user, false to ignore ACLs 753cc529468SMichael Hamann * @return false|string the full page id of the found page, false if any 754e66d3e6dSAndreas Gohr */ 755a5752066Sjpedrycfunction page_findnearest($page, $useacl = true) 756a5752066Sjpedryc{ 75706a70133SPhy if ((string) $page === '') return false; 758e66d3e6dSAndreas Gohr global $ID; 759e66d3e6dSAndreas Gohr 760e66d3e6dSAndreas Gohr $ns = $ID; 761e66d3e6dSAndreas Gohr do { 762e66d3e6dSAndreas Gohr $ns = getNS($ns); 763cc529468SMichael Hamann $pageid = cleanID("$ns:$page"); 7647c3e4a67SAndreas Gohr if (page_exists($pageid) && (!$useacl || auth_quickaclcheck($pageid) >= AUTH_READ)) { 765e66d3e6dSAndreas Gohr return $pageid; 766e66d3e6dSAndreas Gohr } 76706a70133SPhy } while ($ns !== false); 768e66d3e6dSAndreas Gohr 769e66d3e6dSAndreas Gohr return false; 770e66d3e6dSAndreas Gohr} 771