xref: /dokuwiki/inc/pageutils.php (revision 42ea7f447f39fbc2f79eaaec31f8c10ede59c5d0)
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
23*42ea7f44SGerrit Uitslag * @return string
246c7843b5Sandi */
2542905504SAndreas Gohrfunction getID($param='id',$clean=true){
26585bf44eSChristopher Smith    /** @var Input $INPUT */
277d01a0eaSTom N Harris    global $INPUT;
286c7843b5Sandi    global $conf;
294e90caaaSMichael Hamann    global $ACT;
306c7843b5Sandi
317d01a0eaSTom N Harris    $id = $INPUT->str($param);
3248665d38SAndreas Gohr
336c7843b5Sandi    //construct page id from request URI
346c7843b5Sandi    if(empty($id) && $conf['userewrite'] == 2){
35585bf44eSChristopher Smith        $request = $INPUT->server->str('REQUEST_URI');
3606368e4dSMichael Hamann        $script = '';
3706368e4dSMichael Hamann
386c7843b5Sandi        //get the script URL
396c7843b5Sandi        if($conf['basedir']){
4081124000Sjan            $relpath = '';
4181124000Sjan            if($param != 'id') {
4281124000Sjan                $relpath = 'lib/exe/';
4381124000Sjan            }
44585bf44eSChristopher Smith            $script = $conf['basedir'].$relpath.utf8_basename($INPUT->server->str('SCRIPT_FILENAME'));
457d71d4b7SAndreas Gohr
46585bf44eSChristopher Smith        }elseif($INPUT->server->str('PATH_INFO')){
47585bf44eSChristopher Smith            $request = $INPUT->server->str('PATH_INFO');
48585bf44eSChristopher Smith        }elseif($INPUT->server->str('SCRIPT_NAME')){
49585bf44eSChristopher Smith            $script = $INPUT->server->str('SCRIPT_NAME');
50585bf44eSChristopher Smith        }elseif($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')){
51585bf44eSChristopher Smith            $script = preg_replace ('/^'.preg_quote($INPUT->server->str('DOCUMENT_ROOT'),'/').'/','',
52585bf44eSChristopher Smith                    $INPUT->server->str('SCRIPT_FILENAME'));
536c7843b5Sandi            $script = '/'.$script;
546c7843b5Sandi        }
556c7843b5Sandi
5652339126Sandi        //clean script and request (fixes a windows problem)
5752339126Sandi        $script  = preg_replace('/\/\/+/','/',$script);
587d71d4b7SAndreas Gohr        $request = preg_replace('/\/\/+/','/',$request);
5952339126Sandi
606c7843b5Sandi        //remove script URL and Querystring to gain the id
6152339126Sandi        if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
626c7843b5Sandi            $id = preg_replace ('/\?.*/','',$match[1]);
636c7843b5Sandi        }
646de3759aSAndreas Gohr        $id = urldecode($id);
6542905504SAndreas Gohr        //strip leading slashes
6642905504SAndreas Gohr        $id = preg_replace('!^/+!','',$id);
676c7843b5Sandi    }
68671a58a6SGuy Brand
69671a58a6SGuy Brand    // Namespace autolinking from URL
70b6084253SAndreas Gohr    if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
71103c256aSChris Smith        if(page_exists($id.$conf['start'])){
72671a58a6SGuy Brand            // start page inside namespace
73671a58a6SGuy Brand            $id = $id.$conf['start'];
74103c256aSChris Smith        }elseif(page_exists($id.noNS(cleanID($id)))){
75671a58a6SGuy Brand            // page named like the NS inside the NS
76671a58a6SGuy Brand            $id = $id.noNS(cleanID($id));
77103c256aSChris Smith        }elseif(page_exists($id)){
78671a58a6SGuy Brand            // page like namespace exists
797a42ac9eSBen Coburn            $id = substr($id,0,-1);
80671a58a6SGuy Brand        }else{
81671a58a6SGuy Brand            // fall back to default
82671a58a6SGuy Brand            $id = $id.$conf['start'];
83671a58a6SGuy Brand        }
844e90caaaSMichael Hamann        if (isset($ACT) && $ACT === 'show') send_redirect(wl($id,'',true));
85671a58a6SGuy Brand    }
86671a58a6SGuy Brand
8742905504SAndreas Gohr    if($clean) $id = cleanID($id);
880868021bSAndreas Gohr    if(empty($id) && $param=='id') $id = $conf['start'];
896c7843b5Sandi
906c7843b5Sandi    return $id;
916c7843b5Sandi}
92b625487dSandi
93b625487dSandi/**
94b625487dSandi * Remove unwanted chars from ID
95b625487dSandi *
96b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
97b625487dSandi * converted to unaccented ones
98b625487dSandi *
99b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
100*42ea7f44SGerrit Uitslag *
1016e0cc83aSchris * @param  string  $raw_id    The pageid to clean
1028a831f2bSAndreas Gohr * @param  boolean $ascii     Force ASCII
103dbf714f7SGerrit Uitslag * @return string cleaned id
104b625487dSandi */
105c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){
106b625487dSandi    global $conf;
1074b5db43bSjoe.lapp    static $sepcharpat = null;
1084b5db43bSjoe.lapp
109dc2c0e04Schris    global $cache_cleanid;
110dc2c0e04Schris    $cache = & $cache_cleanid;
1116e0cc83aSchris
1126e0cc83aSchris    // check if it's already in the memory cache
1133a50618cSgweissbach    if (isset($cache[(string)$raw_id])) {
1143a50618cSgweissbach        return $cache[(string)$raw_id];
1156e0cc83aSchris    }
1166e0cc83aSchris
1174b5db43bSjoe.lapp    $sepchar = $conf['sepchar'];
1184b5db43bSjoe.lapp    if($sepcharpat == null) // build string only once to save clock cycles
1194b5db43bSjoe.lapp        $sepcharpat = '#\\'.$sepchar.'+#';
1204b5db43bSjoe.lapp
1213a50618cSgweissbach    $id = trim((string)$raw_id);
122b625487dSandi    $id = utf8_strtolower($id);
123b625487dSandi
124b625487dSandi    //alternative namespace seperator
125b625487dSandi    if($conf['useslash']){
1263755fc25STom N Harris        $id = strtr($id,';/','::');
127b625487dSandi    }else{
1283755fc25STom N Harris        $id = strtr($id,';/',':'.$sepchar);
129b625487dSandi    }
130b625487dSandi
1318a831f2bSAndreas Gohr    if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1328a831f2bSAndreas Gohr    if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
133b625487dSandi
134b625487dSandi    //remove specials
135ad81d431SAndreas Gohr    $id = utf8_stripspecials($id,$sepchar,'\*');
136b625487dSandi
1378a831f2bSAndreas Gohr    if($ascii) $id = utf8_strip($id);
1388a831f2bSAndreas Gohr
139b625487dSandi    //clean up
1404b5db43bSjoe.lapp    $id = preg_replace($sepcharpat,$sepchar,$id);
141b625487dSandi    $id = preg_replace('#:+#',':',$id);
1423543c6deSAndreas Gohr    $id = trim($id,':._-');
143b625487dSandi    $id = preg_replace('#:[:\._\-]+#',':',$id);
144b680ea06SAndreas Gohr    $id = preg_replace('#[:\._\-]+:#',':',$id);
145b625487dSandi
1463a50618cSgweissbach    $cache[(string)$raw_id] = $id;
147b625487dSandi    return($id);
148b625487dSandi}
149b625487dSandi
150b625487dSandi/**
151b625487dSandi * Return namespacepart of a wiki ID
152b625487dSandi *
153b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
15415851b98SGerrit Uitslag *
15515851b98SGerrit Uitslag * @param string $id
156*42ea7f44SGerrit Uitslag * @return string|false the namespace part or false if the given ID has no namespace (root)
157b625487dSandi */
158b625487dSandifunction getNS($id){
1593a50618cSgweissbach    $pos = strrpos((string)$id,':');
160c4e0e4a1SAndreas Gohr    if($pos!==false){
1613a50618cSgweissbach        return substr((string)$id,0,$pos);
162b625487dSandi    }
163ef11fcfcSAndreas Gohr    return false;
164b625487dSandi}
165b625487dSandi
166b625487dSandi/**
167b625487dSandi * Returns the ID without the namespace
168b625487dSandi *
169b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
17015851b98SGerrit Uitslag *
17115851b98SGerrit Uitslag * @param string $id
17215851b98SGerrit Uitslag * @return string
173b625487dSandi */
174b625487dSandifunction noNS($id) {
1752844584fSBen Coburn    $pos = strrpos($id, ':');
1762844584fSBen Coburn    if ($pos!==false) {
1772844584fSBen Coburn        return substr($id, $pos+1);
1782844584fSBen Coburn    } else {
1792844584fSBen Coburn        return $id;
1802844584fSBen Coburn    }
1811a84a0f3SAnika Henke}
1821a84a0f3SAnika Henke
1831a84a0f3SAnika Henke/**
1841a84a0f3SAnika Henke * Returns the current namespace
1851a84a0f3SAnika Henke *
1861a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
18784657ea2SGerrit Uitslag *
18884657ea2SGerrit Uitslag * @param string $id
18984657ea2SGerrit Uitslag * @return string
1901a84a0f3SAnika Henke */
1911a84a0f3SAnika Henkefunction curNS($id) {
1921a84a0f3SAnika Henke    return noNS(getNS($id));
1931a84a0f3SAnika Henke}
1941a84a0f3SAnika Henke
1951a84a0f3SAnika Henke/**
1961a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages
1971a84a0f3SAnika Henke *
1981a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
19984657ea2SGerrit Uitslag *
20084657ea2SGerrit Uitslag * @param string $id
20184657ea2SGerrit Uitslag * @return string
2021a84a0f3SAnika Henke */
2031a84a0f3SAnika Henkefunction noNSorNS($id) {
2041a84a0f3SAnika Henke    global $conf;
2051a84a0f3SAnika Henke
2061a84a0f3SAnika Henke    $p = noNS($id);
2079708106bSAdrian Lang    if ($p == $conf['start'] || $p == false) {
2081a84a0f3SAnika Henke        $p = curNS($id);
2091a84a0f3SAnika Henke        if ($p == false) {
2109708106bSAdrian Lang            return $conf['start'];
2111a84a0f3SAnika Henke        }
2121a84a0f3SAnika Henke    }
2131a84a0f3SAnika Henke    return $p;
214b625487dSandi}
2154ceab83fSAndreas Gohr
2164ceab83fSAndreas Gohr/**
2174ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title
2184ceab83fSAndreas Gohr *
2194ceab83fSAndreas Gohr * @param string  $title   The headline title
220c857afe0SMichael Hamann * @param array|bool   $check   Existing IDs (title => number)
221c857afe0SMichael Hamann * @return string the title
22284657ea2SGerrit Uitslag *
2234ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2244ceab83fSAndreas Gohr */
225443d207bSAndreas Gohrfunction sectionID($title,&$check) {
226de9114eaSAnika Henke    $title = str_replace(array(':','.'),'',cleanID($title));
227de9114eaSAnika Henke    $new = ltrim($title,'0123456789_-');
2284ceab83fSAndreas Gohr    if(empty($new)){
2294ceab83fSAndreas Gohr        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
2304ceab83fSAndreas Gohr    }else{
2314ceab83fSAndreas Gohr        $title = $new;
2324ceab83fSAndreas Gohr    }
2334ceab83fSAndreas Gohr
234443d207bSAndreas Gohr    if(is_array($check)){
2354ceab83fSAndreas Gohr        // make sure tiles are unique
23601e3159cSChris Tapp        if (!array_key_exists ($title,$check)) {
23701e3159cSChris Tapp            $check[$title] = 0;
23801e3159cSChris Tapp        } else {
23901e3159cSChris Tapp            $title .= ++ $check[$title];
2404ceab83fSAndreas Gohr        }
2414ceab83fSAndreas Gohr    }
2424ceab83fSAndreas Gohr
2434ceab83fSAndreas Gohr    return $title;
2444ceab83fSAndreas Gohr}
2454ceab83fSAndreas Gohr
246b625487dSandi
247b625487dSandi/**
248103c256aSChris Smith * Wiki page existence check
249103c256aSChris Smith *
250103c256aSChris Smith * parameters as for wikiFN
251103c256aSChris Smith *
252103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk>
25384657ea2SGerrit Uitslag *
25484657ea2SGerrit Uitslag * @param string     $id     page id
25584657ea2SGerrit Uitslag * @param string|int $rev    empty or revision timestamp
25684657ea2SGerrit Uitslag * @param bool       $clean  flag indicating that $id should be cleaned (see wikiFN as well)
25784657ea2SGerrit Uitslag * @return bool exists?
258103c256aSChris Smith */
259103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
260103c256aSChris Smith    return @file_exists(wikiFN($id,$rev,$clean));
261103c256aSChris Smith}
262103c256aSChris Smith
263103c256aSChris Smith/**
264103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
265b625487dSandi *
266b625487dSandi * The filename is URL encoded to protect Unicode chars
267b625487dSandi *
268103c256aSChris Smith * @param  $raw_id  string   id of wikipage
269e0c26282SGerrit Uitslag * @param  $rev     int|string   page revision, empty string for current
270103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
271103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
272dbf714f7SGerrit Uitslag * @return string full path
273103c256aSChris Smith *
274b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
275b625487dSandi */
2766e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
277b625487dSandi    global $conf;
2786e0cc83aSchris
279dc2c0e04Schris    global $cache_wikifn;
280dc2c0e04Schris    $cache = & $cache_wikifn;
281dc2c0e04Schris
2826e0cc83aSchris    if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2836e0cc83aSchris        return $cache[$raw_id][$rev];
2846e0cc83aSchris    }
2856e0cc83aSchris
2866e0cc83aSchris    $id = $raw_id;
2876e0cc83aSchris
2880d8ea614Schris    if ($clean) $id = cleanID($id);
289b625487dSandi    $id = str_replace(':','/',$id);
290b625487dSandi    if(empty($rev)){
291b625487dSandi        $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
292b625487dSandi    }else{
293b625487dSandi        $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
294ff3ed99fSmarcel        if($conf['compression']){
295ff3ed99fSmarcel            //test for extensions here, we want to read both compressions
296d8186216SBen Coburn            if (@file_exists($fn . '.gz')){
297b625487dSandi                $fn .= '.gz';
298d8186216SBen Coburn            }else if(@file_exists($fn . '.bz2')){
299ff3ed99fSmarcel                $fn .= '.bz2';
300ff3ed99fSmarcel            }else{
301ff3ed99fSmarcel                //file doesnt exist yet, so we take the configured extension
302ff3ed99fSmarcel                $fn .= '.' . $conf['compression'];
303ff3ed99fSmarcel            }
304b625487dSandi        }
305b625487dSandi    }
3066e0cc83aSchris
30750602150SBen Coburn    if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
3086e0cc83aSchris    $cache[$raw_id][$rev] = $fn;
309b625487dSandi    return $fn;
310b625487dSandi}
311b625487dSandi
312b625487dSandi/**
313c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
314c9b4bd1eSBen Coburn *
315c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
31684657ea2SGerrit Uitslag *
31784657ea2SGerrit Uitslag * @param string $id page id
31884657ea2SGerrit Uitslag * @return string full path
319c9b4bd1eSBen Coburn */
320c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
321c9b4bd1eSBen Coburn    global $conf;
322662ff478SAndreas Gohr    return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
323c9b4bd1eSBen Coburn}
324c9b4bd1eSBen Coburn
325c9b4bd1eSBen Coburn
326c9b4bd1eSBen Coburn/**
3271380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
328b158d625SSteven Danz *
329b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
33084657ea2SGerrit Uitslag *
33184657ea2SGerrit Uitslag * @param string $id   page id
33284657ea2SGerrit Uitslag * @param string $ext  file extension
33384657ea2SGerrit Uitslag * @return string full path
334b158d625SSteven Danz */
3351380fc45SAndreas Gohrfunction metaFN($id,$ext){
336b158d625SSteven Danz    global $conf;
337b158d625SSteven Danz    $id = cleanID($id);
338b158d625SSteven Danz    $id = str_replace(':','/',$id);
3391380fc45SAndreas Gohr    $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
340b158d625SSteven Danz    return $fn;
341b158d625SSteven Danz}
342b158d625SSteven Danz
343b158d625SSteven Danz/**
344e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension
345e4f389efSKate Arzamastseva *
346cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
34784657ea2SGerrit Uitslag *
34884657ea2SGerrit Uitslag * @param string $id   media id
34984657ea2SGerrit Uitslag * @param string $ext  extension of media
35084657ea2SGerrit Uitslag * @return string
351e4f389efSKate Arzamastseva */
352e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){
353e4f389efSKate Arzamastseva    global $conf;
354e4f389efSKate Arzamastseva    $id = cleanID($id);
355e4f389efSKate Arzamastseva    $id = str_replace(':','/',$id);
356e4f389efSKate Arzamastseva    $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext;
357e4f389efSKate Arzamastseva    return $fn;
358e4f389efSKate Arzamastseva}
359e4f389efSKate Arzamastseva
360e4f389efSKate Arzamastseva/**
361e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
362e1f3d9e1SEsther Brunner *
363e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
364ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de>
36584657ea2SGerrit Uitslag *
36684657ea2SGerrit Uitslag * @param string $id page id
36784657ea2SGerrit Uitslag * @return array
368e1f3d9e1SEsther Brunner */
369e1f3d9e1SEsther Brunnerfunction metaFiles($id){
370ba0267b3SMichael Hamann    $basename = metaFN($id, '');
371ba0267b3SMichael Hamann    $files    = glob($basename.'.*', GLOB_MARK);
372ba0267b3SMichael Hamann    // filter files like foo.bar.meta when $id == 'foo'
373ba0267b3SMichael Hamann    return    $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array();
374e1f3d9e1SEsther Brunner}
375e1f3d9e1SEsther Brunner
376e1f3d9e1SEsther Brunner/**
377b625487dSandi * returns the full path to the mediafile specified by ID
378b625487dSandi *
379b625487dSandi * The filename is URL encoded to protect Unicode chars
380b625487dSandi *
381b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
382cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
38384657ea2SGerrit Uitslag *
38484657ea2SGerrit Uitslag * @param string     $id  media id
38584657ea2SGerrit Uitslag * @param string|int $rev empty string or revision timestamp
38684657ea2SGerrit Uitslag * @return string full path
387b625487dSandi */
388e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){
389b625487dSandi    global $conf;
390b625487dSandi    $id = cleanID($id);
391b625487dSandi    $id = str_replace(':','/',$id);
392e4f389efSKate Arzamastseva    if(empty($rev)){
393b625487dSandi        $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
394e4f389efSKate Arzamastseva    }else{
395cbe26ad6SKate Arzamastseva        $ext = mimetype($id);
3968e69fd30SKate Arzamastseva        $name = substr($id,0, -1*strlen($ext[0])-1);
39761f1aad8SKate Arzamastseva        $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]);
398e4f389efSKate Arzamastseva    }
399b625487dSandi    return $fn;
400b625487dSandi}
401b625487dSandi
402b625487dSandi/**
4032adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local
404b625487dSandi * version isn't found the english one is returned
405b625487dSandi *
4062adaf2b8SAndreas Gohr * @param  string $id  The id of the local file
4072adaf2b8SAndreas Gohr * @param  string $ext The file extension (usually txt)
408dbf714f7SGerrit Uitslag * @return string full filepath to localized file
40984657ea2SGerrit Uitslag *
410b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
411b625487dSandi */
4122adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){
413b625487dSandi    global $conf;
4148819fbf5ShArpanet    $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext;
415e6cecb08SMichael Hamann    if(!@file_exists($file)){
4162adaf2b8SAndreas Gohr        $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext;
417b625487dSandi        if(!@file_exists($file)){
418b625487dSandi            //fall back to english
4192adaf2b8SAndreas Gohr            $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext;
420b625487dSandi        }
421e6cecb08SMichael Hamann    }
422b625487dSandi    return $file;
423b625487dSandi}
424b625487dSandi
425b625487dSandi/**
426c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
427c4e0e4a1SAndreas Gohr *
428c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
429c4e0e4a1SAndreas Gohr * instead
430c4e0e4a1SAndreas Gohr *
431c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
432c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
433c4e0e4a1SAndreas Gohr *
434c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
43584657ea2SGerrit Uitslag *
43684657ea2SGerrit Uitslag * @param string $ns     namespace which is context of id
43784657ea2SGerrit Uitslag * @param string $id     relative id
43884657ea2SGerrit Uitslag * @param bool   $clean  flag indicating that id should be cleaned
439*42ea7f44SGerrit Uitslag * @return string
440c4e0e4a1SAndreas Gohr */
441a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
442c662a49aSAndreas Gohr    global $conf;
443c662a49aSAndreas Gohr
444c662a49aSAndreas Gohr    // some pre cleaning for useslash:
445c662a49aSAndreas Gohr    if($conf['useslash']) $id = str_replace('/',':',$id);
446c662a49aSAndreas Gohr
447c4e0e4a1SAndreas Gohr    // if the id starts with a dot we need to handle the
448c4e0e4a1SAndreas Gohr    // relative stuff
449443e135dSChristopher Smith    if($id && $id{0} == '.'){
450c4e0e4a1SAndreas Gohr        // normalize initial dots without a colon
451c4e0e4a1SAndreas Gohr        $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
452c4e0e4a1SAndreas Gohr        // prepend the current namespace
453c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
454c4e0e4a1SAndreas Gohr
455c4e0e4a1SAndreas Gohr        // cleanup relatives
456c4e0e4a1SAndreas Gohr        $result = array();
457c4e0e4a1SAndreas Gohr        $pathA  = explode(':', $id);
458c4e0e4a1SAndreas Gohr        if (!$pathA[0]) $result[] = '';
459c4e0e4a1SAndreas Gohr        foreach ($pathA AS $key => $dir) {
460c4e0e4a1SAndreas Gohr            if ($dir == '..') {
461c4e0e4a1SAndreas Gohr                if (end($result) == '..') {
462c4e0e4a1SAndreas Gohr                    $result[] = '..';
463c4e0e4a1SAndreas Gohr                } elseif (!array_pop($result)) {
464c4e0e4a1SAndreas Gohr                    $result[] = '..';
465c4e0e4a1SAndreas Gohr                }
466c4e0e4a1SAndreas Gohr            } elseif ($dir && $dir != '.') {
467c4e0e4a1SAndreas Gohr                $result[] = $dir;
468c4e0e4a1SAndreas Gohr            }
469c4e0e4a1SAndreas Gohr        }
470c4e0e4a1SAndreas Gohr        if (!end($pathA)) $result[] = '';
471c4e0e4a1SAndreas Gohr        $id = implode(':', $result);
472c4e0e4a1SAndreas Gohr    }elseif($ns !== false && strpos($id,':') === false){
473c4e0e4a1SAndreas Gohr        //if link contains no namespace. add current namespace (if any)
474c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
475c4e0e4a1SAndreas Gohr    }
476c4e0e4a1SAndreas Gohr
477a6ef4796SAndreas Gohr    if($clean) $id = cleanID($id);
478a6ef4796SAndreas Gohr    return $id;
479c4e0e4a1SAndreas Gohr}
480c4e0e4a1SAndreas Gohr
481c4e0e4a1SAndreas Gohr/**
482b625487dSandi * Returns a full media id
483b625487dSandi *
484b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
48584657ea2SGerrit Uitslag *
48684657ea2SGerrit Uitslag * @param string  $ns     namespace which is context of id
48784657ea2SGerrit Uitslag * @param string &$page   (reference) relative media id, updated to resolved id
48884657ea2SGerrit Uitslag * @param bool   &$exists (reference) updated with existance of media
489b625487dSandi */
49037e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
491c4e0e4a1SAndreas Gohr    $page   = resolve_id($ns,$page);
492b625487dSandi    $file   = mediaFN($page);
493b625487dSandi    $exists = @file_exists($file);
494b625487dSandi}
495b625487dSandi
496b625487dSandi/**
497b625487dSandi * Returns a full page id
498b625487dSandi *
499b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
50084657ea2SGerrit Uitslag *
50184657ea2SGerrit Uitslag * @param string  $ns     namespace which is context of id
50284657ea2SGerrit Uitslag * @param string &$page   (reference) relative page id, updated to resolved id
50384657ea2SGerrit Uitslag * @param bool   &$exists (reference) updated with existance of media
504b625487dSandi */
50537e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
506b625487dSandi    global $conf;
507c006739eSIzidor Matušov    global $ID;
5080b7c14c2Sandi    $exists = false;
509b625487dSandi
510c006739eSIzidor Matušov    //empty address should point to current page
511c006739eSIzidor Matušov    if ($page === "") {
512c006739eSIzidor Matušov        $page = $ID;
513c006739eSIzidor Matušov    }
514c006739eSIzidor Matušov
515b625487dSandi    //keep hashlink if exists then clean both parts
51603c4aec3Schris    if (strpos($page,'#')) {
5174b7f9e70STom N Harris        list($page,$hash) = explode('#',$page,2);
51803c4aec3Schris    } else {
51903c4aec3Schris        $hash = '';
52003c4aec3Schris    }
521b625487dSandi    $hash = cleanID($hash);
522a6ef4796SAndreas Gohr    $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
523b625487dSandi
524a6ef4796SAndreas Gohr    // get filename (calls clean itself)
525b625487dSandi    $file = wikiFN($page);
526b625487dSandi
5271179df0eSGuy Brand    // if ends with colon or slash we have a namespace link
528b26cdbbeSAdrian Lang    if(in_array(substr($page,-1), array(':', ';')) ||
529b26cdbbeSAdrian Lang       ($conf['useslash'] && substr($page,-1) == '/')){
530103c256aSChris Smith        if(page_exists($page.$conf['start'])){
531a6ef4796SAndreas Gohr            // start page inside namespace
532a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
533a6ef4796SAndreas Gohr            $exists = true;
534103c256aSChris Smith        }elseif(page_exists($page.noNS(cleanID($page)))){
535a6ef4796SAndreas Gohr            // page named like the NS inside the NS
536a6ef4796SAndreas Gohr            $page = $page.noNS(cleanID($page));
537a6ef4796SAndreas Gohr            $exists = true;
538103c256aSChris Smith        }elseif(page_exists($page)){
539a6ef4796SAndreas Gohr            // page like namespace exists
540a6ef4796SAndreas Gohr            $page = $page;
541a6ef4796SAndreas Gohr            $exists = true;
542a6ef4796SAndreas Gohr        }else{
543a6ef4796SAndreas Gohr            // fall back to default
544a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
545a6ef4796SAndreas Gohr        }
546a6ef4796SAndreas Gohr    }else{
547b625487dSandi        //check alternative plural/nonplural form
548b625487dSandi        if(!@file_exists($file)){
549b625487dSandi            if( $conf['autoplural'] ){
550b625487dSandi                if(substr($page,-1) == 's'){
551b625487dSandi                    $try = substr($page,0,-1);
552b625487dSandi                }else{
553b625487dSandi                    $try = $page.'s';
554b625487dSandi                }
555103c256aSChris Smith                if(page_exists($try)){
556b625487dSandi                    $page   = $try;
557b625487dSandi                    $exists = true;
558b625487dSandi                }
559b625487dSandi            }
560b625487dSandi        }else{
561b625487dSandi            $exists = true;
562b625487dSandi        }
563a6ef4796SAndreas Gohr    }
564a6ef4796SAndreas Gohr
565a6ef4796SAndreas Gohr    // now make sure we have a clean page
566a6ef4796SAndreas Gohr    $page = cleanID($page);
567b625487dSandi
568b625487dSandi    //add hash if any
569b2d7d3f2Sandi    if(!empty($hash)) $page .= '#'.$hash;
570b625487dSandi}
571b625487dSandi
57298407a7aSandi/**
57398407a7aSandi * Returns the name of a cachefile from given data
57498407a7aSandi *
57598407a7aSandi * The needed directory is created by this function!
57698407a7aSandi *
57798407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
57898407a7aSandi *
57998407a7aSandi * @param string $data  This data is used to create a unique md5 name
58098407a7aSandi * @param string $ext   This is appended to the filename if given
58198407a7aSandi * @return string       The filename of the cachefile
58298407a7aSandi */
58398407a7aSandifunction getCacheName($data,$ext=''){
58498407a7aSandi    global $conf;
58598407a7aSandi    $md5  = md5($data);
58698407a7aSandi    $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
58798407a7aSandi    io_makeFileDir($file);
58898407a7aSandi    return $file;
58998407a7aSandi}
59098407a7aSandi
5910dc92c6fSAndreas Gohr/**
5920dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
5930dc92c6fSAndreas Gohr *
5940dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
59584657ea2SGerrit Uitslag *
59684657ea2SGerrit Uitslag * @param string $id page id
59784657ea2SGerrit Uitslag * @return bool
5980dc92c6fSAndreas Gohr */
5990dc92c6fSAndreas Gohrfunction isHiddenPage($id){
6008449cc9dSDominik Eckelmann    $data = array(
6018449cc9dSDominik Eckelmann        'id' => $id,
6028449cc9dSDominik Eckelmann        'hidden' => false
6038449cc9dSDominik Eckelmann    );
604fb55b51eSDominik Eckelmann    trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage');
605fb55b51eSDominik Eckelmann    return $data['hidden'];
6060dc92c6fSAndreas Gohr}
607fb55b51eSDominik Eckelmann
608dbf714f7SGerrit Uitslag/**
609dbf714f7SGerrit Uitslag * callback checks if page is hidden
610dbf714f7SGerrit Uitslag *
61184657ea2SGerrit Uitslag * @param array $data event data    - see isHiddenPage()
612dbf714f7SGerrit Uitslag */
613fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) {
614fb55b51eSDominik Eckelmann    global $conf;
615fb55b51eSDominik Eckelmann    global $ACT;
616fb55b51eSDominik Eckelmann
617fb55b51eSDominik Eckelmann    if ($data['hidden']) return;
618fb55b51eSDominik Eckelmann    if(empty($conf['hidepages'])) return;
619fb55b51eSDominik Eckelmann    if($ACT == 'admin') return;
620fb55b51eSDominik Eckelmann
621fb55b51eSDominik Eckelmann    if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){
622fb55b51eSDominik Eckelmann        $data['hidden'] = true;
623fb55b51eSDominik Eckelmann    }
6240dc92c6fSAndreas Gohr}
6250dc92c6fSAndreas Gohr
6260dc92c6fSAndreas Gohr/**
6270dc92c6fSAndreas Gohr * Reverse of isHiddenPage
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 isVisiblePage($id){
6350dc92c6fSAndreas Gohr    return !isHiddenPage($id);
6360dc92c6fSAndreas Gohr}
6370dc92c6fSAndreas Gohr
6385b75cd1fSAdrian Lang/**
6395b75cd1fSAdrian Lang * Format an id for output to a user
6405b75cd1fSAdrian Lang *
6415b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is
6425b75cd1fSAdrian Lang * “*”. Output is escaped.
6435b75cd1fSAdrian Lang *
6445b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
64584657ea2SGerrit Uitslag *
64684657ea2SGerrit Uitslag * @param string $id page id
64784657ea2SGerrit Uitslag * @return string
6485b75cd1fSAdrian Lang */
6495b75cd1fSAdrian Langfunction prettyprint_id($id) {
6505b75cd1fSAdrian Lang    if (!$id || $id === ':') {
6515b75cd1fSAdrian Lang        return '*';
6525b75cd1fSAdrian Lang    }
6535b75cd1fSAdrian Lang    if ((substr($id, -1, 1) === ':')) {
6545b75cd1fSAdrian Lang        $id .= '*';
6555b75cd1fSAdrian Lang    }
6565b75cd1fSAdrian Lang    return hsc($id);
6575b75cd1fSAdrian Lang}
658f03fd957SAndreas Gohr
659f03fd957SAndreas Gohr/**
660f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem
661f03fd957SAndreas Gohr *
662f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
663f03fd957SAndreas Gohr *
664f03fd957SAndreas Gohr * When the second parameter is true the string will
665f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected -
666f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the
667f03fd957SAndreas Gohr * same string (default is true)
668f03fd957SAndreas Gohr *
669f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
670f03fd957SAndreas Gohr * @see    urlencode
67184657ea2SGerrit Uitslag *
67284657ea2SGerrit Uitslag * @param string $file file name
67384657ea2SGerrit Uitslag * @param bool   $safe if true, only encoded when non ASCII characters detected
67484657ea2SGerrit Uitslag * @return string
675f03fd957SAndreas Gohr */
676f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){
677f03fd957SAndreas Gohr    global $conf;
678f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
679f03fd957SAndreas Gohr
680f03fd957SAndreas Gohr    if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
681f03fd957SAndreas Gohr        return $file;
682f03fd957SAndreas Gohr    }
683f03fd957SAndreas Gohr
684f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
685f03fd957SAndreas Gohr        return SafeFN::encode($file);
686f03fd957SAndreas Gohr    }
687f03fd957SAndreas Gohr
688f03fd957SAndreas Gohr    $file = urlencode($file);
689f03fd957SAndreas Gohr    $file = str_replace('%2F','/',$file);
690f03fd957SAndreas Gohr    return $file;
691f03fd957SAndreas Gohr}
692f03fd957SAndreas Gohr
693f03fd957SAndreas Gohr/**
694f03fd957SAndreas Gohr * Decode a filename back to UTF-8
695f03fd957SAndreas Gohr *
696f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
697f03fd957SAndreas Gohr *
698f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
699f03fd957SAndreas Gohr * @see    urldecode
70084657ea2SGerrit Uitslag *
70184657ea2SGerrit Uitslag * @param string $file file name
70284657ea2SGerrit Uitslag * @return string
703f03fd957SAndreas Gohr */
704f03fd957SAndreas Gohrfunction utf8_decodeFN($file){
705f03fd957SAndreas Gohr    global $conf;
706f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
707f03fd957SAndreas Gohr
708f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
709f03fd957SAndreas Gohr        return SafeFN::decode($file);
710f03fd957SAndreas Gohr    }
711f03fd957SAndreas Gohr
712f03fd957SAndreas Gohr    return urldecode($file);
713f03fd957SAndreas Gohr}
714f03fd957SAndreas Gohr
715e66d3e6dSAndreas Gohr/**
716e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any
717e66d3e6dSAndreas Gohr * higher namespace
718e66d3e6dSAndreas Gohr *
719e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well
720e66d3e6dSAndreas Gohr *
721e66d3e6dSAndreas Gohr * @todo   add event hook
722*42ea7f44SGerrit Uitslag *
723e66d3e6dSAndreas Gohr * @param  string $page the pagename you're looking for
724e66d3e6dSAndreas Gohr * @return string|false the full page id of the found page, false if any
725e66d3e6dSAndreas Gohr */
726e66d3e6dSAndreas Gohrfunction page_findnearest($page){
727c786a1b6SAnika Henke    if (!$page) return false;
728e66d3e6dSAndreas Gohr    global $ID;
729e66d3e6dSAndreas Gohr
730e66d3e6dSAndreas Gohr    $ns = $ID;
731e66d3e6dSAndreas Gohr    do {
732e66d3e6dSAndreas Gohr        $ns = getNS($ns);
733e66d3e6dSAndreas Gohr        $pageid = ltrim("$ns:$page",':');
734e66d3e6dSAndreas Gohr        if(page_exists($pageid)){
735e66d3e6dSAndreas Gohr            return $pageid;
736e66d3e6dSAndreas Gohr        }
737e66d3e6dSAndreas Gohr    } while($ns);
738e66d3e6dSAndreas Gohr
739e66d3e6dSAndreas Gohr    return false;
740e66d3e6dSAndreas Gohr}
741