xref: /dokuwiki/inc/pageutils.php (revision 585bf44e2b756eac2e1cfce7035ef237bc02a788)
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>
206c7843b5Sandi */
2142905504SAndreas Gohrfunction getID($param='id',$clean=true){
22*585bf44eSChristopher Smith    /** @var Input $INPUT */
237d01a0eaSTom N Harris    global $INPUT;
246c7843b5Sandi    global $conf;
254e90caaaSMichael Hamann    global $ACT;
266c7843b5Sandi
277d01a0eaSTom N Harris    $id = $INPUT->str($param);
2848665d38SAndreas Gohr
296c7843b5Sandi    //construct page id from request URI
306c7843b5Sandi    if(empty($id) && $conf['userewrite'] == 2){
31*585bf44eSChristopher Smith        $request = $INPUT->server->str('REQUEST_URI');
3206368e4dSMichael Hamann        $script = '';
3306368e4dSMichael Hamann
346c7843b5Sandi        //get the script URL
356c7843b5Sandi        if($conf['basedir']){
3681124000Sjan            $relpath = '';
3781124000Sjan            if($param != 'id') {
3881124000Sjan                $relpath = 'lib/exe/';
3981124000Sjan            }
40*585bf44eSChristopher Smith            $script = $conf['basedir'].$relpath.utf8_basename($INPUT->server->str('SCRIPT_FILENAME'));
417d71d4b7SAndreas Gohr
42*585bf44eSChristopher Smith        }elseif($INPUT->server->str('PATH_INFO')){
43*585bf44eSChristopher Smith            $request = $INPUT->server->str('PATH_INFO');
44*585bf44eSChristopher Smith        }elseif($INPUT->server->str('SCRIPT_NAME')){
45*585bf44eSChristopher Smith            $script = $INPUT->server->str('SCRIPT_NAME');
46*585bf44eSChristopher Smith        }elseif($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')){
47*585bf44eSChristopher Smith            $script = preg_replace ('/^'.preg_quote($INPUT->server->str('DOCUMENT_ROOT'),'/').'/','',
48*585bf44eSChristopher Smith                    $INPUT->server->str('SCRIPT_FILENAME'));
496c7843b5Sandi            $script = '/'.$script;
506c7843b5Sandi        }
516c7843b5Sandi
5252339126Sandi        //clean script and request (fixes a windows problem)
5352339126Sandi        $script  = preg_replace('/\/\/+/','/',$script);
547d71d4b7SAndreas Gohr        $request = preg_replace('/\/\/+/','/',$request);
5552339126Sandi
566c7843b5Sandi        //remove script URL and Querystring to gain the id
5752339126Sandi        if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
586c7843b5Sandi            $id = preg_replace ('/\?.*/','',$match[1]);
596c7843b5Sandi        }
606de3759aSAndreas Gohr        $id = urldecode($id);
6142905504SAndreas Gohr        //strip leading slashes
6242905504SAndreas Gohr        $id = preg_replace('!^/+!','',$id);
636c7843b5Sandi    }
64671a58a6SGuy Brand
65671a58a6SGuy Brand    // Namespace autolinking from URL
66b6084253SAndreas Gohr    if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
67103c256aSChris Smith        if(page_exists($id.$conf['start'])){
68671a58a6SGuy Brand            // start page inside namespace
69671a58a6SGuy Brand            $id = $id.$conf['start'];
70103c256aSChris Smith        }elseif(page_exists($id.noNS(cleanID($id)))){
71671a58a6SGuy Brand            // page named like the NS inside the NS
72671a58a6SGuy Brand            $id = $id.noNS(cleanID($id));
73103c256aSChris Smith        }elseif(page_exists($id)){
74671a58a6SGuy Brand            // page like namespace exists
757a42ac9eSBen Coburn            $id = substr($id,0,-1);
76671a58a6SGuy Brand        }else{
77671a58a6SGuy Brand            // fall back to default
78671a58a6SGuy Brand            $id = $id.$conf['start'];
79671a58a6SGuy Brand        }
804e90caaaSMichael Hamann        if (isset($ACT) && $ACT === 'show') send_redirect(wl($id,'',true));
81671a58a6SGuy Brand    }
82671a58a6SGuy Brand
8342905504SAndreas Gohr    if($clean) $id = cleanID($id);
840868021bSAndreas Gohr    if(empty($id) && $param=='id') $id = $conf['start'];
856c7843b5Sandi
866c7843b5Sandi    return $id;
876c7843b5Sandi}
88b625487dSandi
89b625487dSandi/**
90b625487dSandi * Remove unwanted chars from ID
91b625487dSandi *
92b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
93b625487dSandi * converted to unaccented ones
94b625487dSandi *
95b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
966e0cc83aSchris * @param  string  $raw_id    The pageid to clean
978a831f2bSAndreas Gohr * @param  boolean $ascii     Force ASCII
98dbf714f7SGerrit Uitslag * @return string cleaned id
99b625487dSandi */
100c8bbb094SAnika Henkefunction cleanID($raw_id,$ascii=false){
101b625487dSandi    global $conf;
1024b5db43bSjoe.lapp    static $sepcharpat = null;
1034b5db43bSjoe.lapp
104dc2c0e04Schris    global $cache_cleanid;
105dc2c0e04Schris    $cache = & $cache_cleanid;
1066e0cc83aSchris
1076e0cc83aSchris    // check if it's already in the memory cache
1083a50618cSgweissbach    if (isset($cache[(string)$raw_id])) {
1093a50618cSgweissbach        return $cache[(string)$raw_id];
1106e0cc83aSchris    }
1116e0cc83aSchris
1124b5db43bSjoe.lapp    $sepchar = $conf['sepchar'];
1134b5db43bSjoe.lapp    if($sepcharpat == null) // build string only once to save clock cycles
1144b5db43bSjoe.lapp        $sepcharpat = '#\\'.$sepchar.'+#';
1154b5db43bSjoe.lapp
1163a50618cSgweissbach    $id = trim((string)$raw_id);
117b625487dSandi    $id = utf8_strtolower($id);
118b625487dSandi
119b625487dSandi    //alternative namespace seperator
120b625487dSandi    if($conf['useslash']){
1213755fc25STom N Harris        $id = strtr($id,';/','::');
122b625487dSandi    }else{
1233755fc25STom N Harris        $id = strtr($id,';/',':'.$sepchar);
124b625487dSandi    }
125b625487dSandi
1268a831f2bSAndreas Gohr    if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1278a831f2bSAndreas Gohr    if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
128b625487dSandi
129b625487dSandi    //remove specials
130ad81d431SAndreas Gohr    $id = utf8_stripspecials($id,$sepchar,'\*');
131b625487dSandi
1328a831f2bSAndreas Gohr    if($ascii) $id = utf8_strip($id);
1338a831f2bSAndreas Gohr
134b625487dSandi    //clean up
1354b5db43bSjoe.lapp    $id = preg_replace($sepcharpat,$sepchar,$id);
136b625487dSandi    $id = preg_replace('#:+#',':',$id);
1373543c6deSAndreas Gohr    $id = trim($id,':._-');
138b625487dSandi    $id = preg_replace('#:[:\._\-]+#',':',$id);
139b680ea06SAndreas Gohr    $id = preg_replace('#[:\._\-]+:#',':',$id);
140b625487dSandi
1413a50618cSgweissbach    $cache[(string)$raw_id] = $id;
142b625487dSandi    return($id);
143b625487dSandi}
144b625487dSandi
145b625487dSandi/**
146b625487dSandi * Return namespacepart of a wiki ID
147b625487dSandi *
148b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
149b625487dSandi */
150b625487dSandifunction getNS($id){
1513a50618cSgweissbach    $pos = strrpos((string)$id,':');
152c4e0e4a1SAndreas Gohr    if($pos!==false){
1533a50618cSgweissbach        return substr((string)$id,0,$pos);
154b625487dSandi    }
155b625487dSandi    return false;
156b625487dSandi}
157b625487dSandi
158b625487dSandi/**
159b625487dSandi * Returns the ID without the namespace
160b625487dSandi *
161b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
162b625487dSandi */
163b625487dSandifunction noNS($id) {
1642844584fSBen Coburn    $pos = strrpos($id, ':');
1652844584fSBen Coburn    if ($pos!==false) {
1662844584fSBen Coburn        return substr($id, $pos+1);
1672844584fSBen Coburn    } else {
1682844584fSBen Coburn        return $id;
1692844584fSBen Coburn    }
1701a84a0f3SAnika Henke}
1711a84a0f3SAnika Henke
1721a84a0f3SAnika Henke/**
1731a84a0f3SAnika Henke * Returns the current namespace
1741a84a0f3SAnika Henke *
1751a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1761a84a0f3SAnika Henke */
1771a84a0f3SAnika Henkefunction curNS($id) {
1781a84a0f3SAnika Henke    return noNS(getNS($id));
1791a84a0f3SAnika Henke}
1801a84a0f3SAnika Henke
1811a84a0f3SAnika Henke/**
1821a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages
1831a84a0f3SAnika Henke *
1841a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1851a84a0f3SAnika Henke */
1861a84a0f3SAnika Henkefunction noNSorNS($id) {
1871a84a0f3SAnika Henke    global $conf;
1881a84a0f3SAnika Henke
1891a84a0f3SAnika Henke    $p = noNS($id);
1909708106bSAdrian Lang    if ($p == $conf['start'] || $p == false) {
1911a84a0f3SAnika Henke        $p = curNS($id);
1921a84a0f3SAnika Henke        if ($p == false) {
1939708106bSAdrian Lang            return $conf['start'];
1941a84a0f3SAnika Henke        }
1951a84a0f3SAnika Henke    }
1961a84a0f3SAnika Henke    return $p;
197b625487dSandi}
1984ceab83fSAndreas Gohr
1994ceab83fSAndreas Gohr/**
2004ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title
2014ceab83fSAndreas Gohr *
2024ceab83fSAndreas Gohr * @param string  $title   The headline title
203c857afe0SMichael Hamann * @param array|bool   $check   Existing IDs (title => number)
204c857afe0SMichael Hamann * @return string the title
2054ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2064ceab83fSAndreas Gohr */
207443d207bSAndreas Gohrfunction sectionID($title,&$check) {
208de9114eaSAnika Henke    $title = str_replace(array(':','.'),'',cleanID($title));
209de9114eaSAnika Henke    $new = ltrim($title,'0123456789_-');
2104ceab83fSAndreas Gohr    if(empty($new)){
2114ceab83fSAndreas Gohr        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
2124ceab83fSAndreas Gohr    }else{
2134ceab83fSAndreas Gohr        $title = $new;
2144ceab83fSAndreas Gohr    }
2154ceab83fSAndreas Gohr
216443d207bSAndreas Gohr    if(is_array($check)){
2174ceab83fSAndreas Gohr        // make sure tiles are unique
21801e3159cSChris Tapp        if (!array_key_exists ($title,$check)) {
21901e3159cSChris Tapp            $check[$title] = 0;
22001e3159cSChris Tapp        } else {
22101e3159cSChris Tapp            $title .= ++ $check[$title];
2224ceab83fSAndreas Gohr        }
2234ceab83fSAndreas Gohr    }
2244ceab83fSAndreas Gohr
2254ceab83fSAndreas Gohr    return $title;
2264ceab83fSAndreas Gohr}
2274ceab83fSAndreas Gohr
228b625487dSandi
229b625487dSandi/**
230103c256aSChris Smith * Wiki page existence check
231103c256aSChris Smith *
232103c256aSChris Smith * parameters as for wikiFN
233103c256aSChris Smith *
234103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk>
235103c256aSChris Smith */
236103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
237103c256aSChris Smith    return @file_exists(wikiFN($id,$rev,$clean));
238103c256aSChris Smith}
239103c256aSChris Smith
240103c256aSChris Smith/**
241103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
242b625487dSandi *
243b625487dSandi * The filename is URL encoded to protect Unicode chars
244b625487dSandi *
245103c256aSChris Smith * @param  $raw_id  string   id of wikipage
246103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
247103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
248103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
249dbf714f7SGerrit Uitslag * @return string full path
250103c256aSChris Smith *
251b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
252b625487dSandi */
2536e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
254b625487dSandi    global $conf;
2556e0cc83aSchris
256dc2c0e04Schris    global $cache_wikifn;
257dc2c0e04Schris    $cache = & $cache_wikifn;
258dc2c0e04Schris
2596e0cc83aSchris    if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2606e0cc83aSchris        return $cache[$raw_id][$rev];
2616e0cc83aSchris    }
2626e0cc83aSchris
2636e0cc83aSchris    $id = $raw_id;
2646e0cc83aSchris
2650d8ea614Schris    if ($clean) $id = cleanID($id);
266b625487dSandi    $id = str_replace(':','/',$id);
267b625487dSandi    if(empty($rev)){
268b625487dSandi        $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
269b625487dSandi    }else{
270b625487dSandi        $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
271ff3ed99fSmarcel        if($conf['compression']){
272ff3ed99fSmarcel            //test for extensions here, we want to read both compressions
273d8186216SBen Coburn            if (@file_exists($fn . '.gz')){
274b625487dSandi                $fn .= '.gz';
275d8186216SBen Coburn            }else if(@file_exists($fn . '.bz2')){
276ff3ed99fSmarcel                $fn .= '.bz2';
277ff3ed99fSmarcel            }else{
278ff3ed99fSmarcel                //file doesnt exist yet, so we take the configured extension
279ff3ed99fSmarcel                $fn .= '.' . $conf['compression'];
280ff3ed99fSmarcel            }
281b625487dSandi        }
282b625487dSandi    }
2836e0cc83aSchris
28450602150SBen Coburn    if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2856e0cc83aSchris    $cache[$raw_id][$rev] = $fn;
286b625487dSandi    return $fn;
287b625487dSandi}
288b625487dSandi
289b625487dSandi/**
290c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
291c9b4bd1eSBen Coburn *
292c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
293c9b4bd1eSBen Coburn */
294c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
295c9b4bd1eSBen Coburn    global $conf;
296662ff478SAndreas Gohr    return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
297c9b4bd1eSBen Coburn}
298c9b4bd1eSBen Coburn
299c9b4bd1eSBen Coburn
300c9b4bd1eSBen Coburn/**
3011380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
302b158d625SSteven Danz *
303b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
304b158d625SSteven Danz */
3051380fc45SAndreas Gohrfunction metaFN($id,$ext){
306b158d625SSteven Danz    global $conf;
307b158d625SSteven Danz    $id = cleanID($id);
308b158d625SSteven Danz    $id = str_replace(':','/',$id);
3091380fc45SAndreas Gohr    $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
310b158d625SSteven Danz    return $fn;
311b158d625SSteven Danz}
312b158d625SSteven Danz
313b158d625SSteven Danz/**
314e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension
315e4f389efSKate Arzamastseva *
316cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
317e4f389efSKate Arzamastseva */
318e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){
319e4f389efSKate Arzamastseva    global $conf;
320e4f389efSKate Arzamastseva    $id = cleanID($id);
321e4f389efSKate Arzamastseva    $id = str_replace(':','/',$id);
322e4f389efSKate Arzamastseva    $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext;
323e4f389efSKate Arzamastseva    return $fn;
324e4f389efSKate Arzamastseva}
325e4f389efSKate Arzamastseva
326e4f389efSKate Arzamastseva/**
327e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
328e1f3d9e1SEsther Brunner *
329e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
330ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de>
331e1f3d9e1SEsther Brunner */
332e1f3d9e1SEsther Brunnerfunction metaFiles($id){
333ba0267b3SMichael Hamann    $basename = metaFN($id, '');
334ba0267b3SMichael Hamann    $files    = glob($basename.'.*', GLOB_MARK);
335ba0267b3SMichael Hamann    // filter files like foo.bar.meta when $id == 'foo'
336ba0267b3SMichael Hamann    return    $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array();
337e1f3d9e1SEsther Brunner}
338e1f3d9e1SEsther Brunner
339e1f3d9e1SEsther Brunner/**
340b625487dSandi * returns the full path to the mediafile specified by ID
341b625487dSandi *
342b625487dSandi * The filename is URL encoded to protect Unicode chars
343b625487dSandi *
344b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
345cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
346b625487dSandi */
347e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){
348b625487dSandi    global $conf;
349b625487dSandi    $id = cleanID($id);
350b625487dSandi    $id = str_replace(':','/',$id);
351e4f389efSKate Arzamastseva    if(empty($rev)){
352b625487dSandi        $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
353e4f389efSKate Arzamastseva    }else{
354cbe26ad6SKate Arzamastseva        $ext = mimetype($id);
3558e69fd30SKate Arzamastseva        $name = substr($id,0, -1*strlen($ext[0])-1);
35661f1aad8SKate Arzamastseva        $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]);
357e4f389efSKate Arzamastseva    }
358b625487dSandi    return $fn;
359b625487dSandi}
360b625487dSandi
361b625487dSandi/**
3622adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local
363b625487dSandi * version isn't found the english one is returned
364b625487dSandi *
3652adaf2b8SAndreas Gohr * @param  string $id  The id of the local file
3662adaf2b8SAndreas Gohr * @param  string $ext The file extension (usually txt)
367dbf714f7SGerrit Uitslag * @return string full filepath to localized file
368b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
369b625487dSandi */
3702adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){
371b625487dSandi    global $conf;
3728819fbf5ShArpanet    $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext;
373e6cecb08SMichael Hamann    if(!@file_exists($file)){
3742adaf2b8SAndreas Gohr        $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext;
375b625487dSandi        if(!@file_exists($file)){
376b625487dSandi            //fall back to english
3772adaf2b8SAndreas Gohr            $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext;
378b625487dSandi        }
379e6cecb08SMichael Hamann    }
380b625487dSandi    return $file;
381b625487dSandi}
382b625487dSandi
383b625487dSandi/**
384c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
385c4e0e4a1SAndreas Gohr *
386c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
387c4e0e4a1SAndreas Gohr * instead
388c4e0e4a1SAndreas Gohr *
389c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
390c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
391c4e0e4a1SAndreas Gohr *
392c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
393c4e0e4a1SAndreas Gohr */
394a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
395c662a49aSAndreas Gohr    global $conf;
396c662a49aSAndreas Gohr
397c662a49aSAndreas Gohr    // some pre cleaning for useslash:
398c662a49aSAndreas Gohr    if($conf['useslash']) $id = str_replace('/',':',$id);
399c662a49aSAndreas Gohr
400c4e0e4a1SAndreas Gohr    // if the id starts with a dot we need to handle the
401c4e0e4a1SAndreas Gohr    // relative stuff
402443e135dSChristopher Smith    if($id && $id{0} == '.'){
403c4e0e4a1SAndreas Gohr        // normalize initial dots without a colon
404c4e0e4a1SAndreas Gohr        $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
405c4e0e4a1SAndreas Gohr        // prepend the current namespace
406c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
407c4e0e4a1SAndreas Gohr
408c4e0e4a1SAndreas Gohr        // cleanup relatives
409c4e0e4a1SAndreas Gohr        $result = array();
410c4e0e4a1SAndreas Gohr        $pathA  = explode(':', $id);
411c4e0e4a1SAndreas Gohr        if (!$pathA[0]) $result[] = '';
412c4e0e4a1SAndreas Gohr        foreach ($pathA AS $key => $dir) {
413c4e0e4a1SAndreas Gohr            if ($dir == '..') {
414c4e0e4a1SAndreas Gohr                if (end($result) == '..') {
415c4e0e4a1SAndreas Gohr                    $result[] = '..';
416c4e0e4a1SAndreas Gohr                } elseif (!array_pop($result)) {
417c4e0e4a1SAndreas Gohr                    $result[] = '..';
418c4e0e4a1SAndreas Gohr                }
419c4e0e4a1SAndreas Gohr            } elseif ($dir && $dir != '.') {
420c4e0e4a1SAndreas Gohr                $result[] = $dir;
421c4e0e4a1SAndreas Gohr            }
422c4e0e4a1SAndreas Gohr        }
423c4e0e4a1SAndreas Gohr        if (!end($pathA)) $result[] = '';
424c4e0e4a1SAndreas Gohr        $id = implode(':', $result);
425c4e0e4a1SAndreas Gohr    }elseif($ns !== false && strpos($id,':') === false){
426c4e0e4a1SAndreas Gohr        //if link contains no namespace. add current namespace (if any)
427c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
428c4e0e4a1SAndreas Gohr    }
429c4e0e4a1SAndreas Gohr
430a6ef4796SAndreas Gohr    if($clean) $id = cleanID($id);
431a6ef4796SAndreas Gohr    return $id;
432c4e0e4a1SAndreas Gohr}
433c4e0e4a1SAndreas Gohr
434c4e0e4a1SAndreas Gohr/**
435b625487dSandi * Returns a full media id
436b625487dSandi *
437b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
438b625487dSandi */
43937e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
440c4e0e4a1SAndreas Gohr    $page   = resolve_id($ns,$page);
441b625487dSandi    $file   = mediaFN($page);
442b625487dSandi    $exists = @file_exists($file);
443b625487dSandi}
444b625487dSandi
445b625487dSandi/**
446b625487dSandi * Returns a full page id
447b625487dSandi *
448b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
449b625487dSandi */
45037e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
451b625487dSandi    global $conf;
452c006739eSIzidor Matušov    global $ID;
4530b7c14c2Sandi    $exists = false;
454b625487dSandi
455c006739eSIzidor Matušov    //empty address should point to current page
456c006739eSIzidor Matušov    if ($page === "") {
457c006739eSIzidor Matušov        $page = $ID;
458c006739eSIzidor Matušov    }
459c006739eSIzidor Matušov
460b625487dSandi    //keep hashlink if exists then clean both parts
46103c4aec3Schris    if (strpos($page,'#')) {
4624b7f9e70STom N Harris        list($page,$hash) = explode('#',$page,2);
46303c4aec3Schris    } else {
46403c4aec3Schris        $hash = '';
46503c4aec3Schris    }
466b625487dSandi    $hash = cleanID($hash);
467a6ef4796SAndreas Gohr    $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
468b625487dSandi
469a6ef4796SAndreas Gohr    // get filename (calls clean itself)
470b625487dSandi    $file = wikiFN($page);
471b625487dSandi
4721179df0eSGuy Brand    // if ends with colon or slash we have a namespace link
473b26cdbbeSAdrian Lang    if(in_array(substr($page,-1), array(':', ';')) ||
474b26cdbbeSAdrian Lang       ($conf['useslash'] && substr($page,-1) == '/')){
475103c256aSChris Smith        if(page_exists($page.$conf['start'])){
476a6ef4796SAndreas Gohr            // start page inside namespace
477a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
478a6ef4796SAndreas Gohr            $exists = true;
479103c256aSChris Smith        }elseif(page_exists($page.noNS(cleanID($page)))){
480a6ef4796SAndreas Gohr            // page named like the NS inside the NS
481a6ef4796SAndreas Gohr            $page = $page.noNS(cleanID($page));
482a6ef4796SAndreas Gohr            $exists = true;
483103c256aSChris Smith        }elseif(page_exists($page)){
484a6ef4796SAndreas Gohr            // page like namespace exists
485a6ef4796SAndreas Gohr            $page = $page;
486a6ef4796SAndreas Gohr            $exists = true;
487a6ef4796SAndreas Gohr        }else{
488a6ef4796SAndreas Gohr            // fall back to default
489a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
490a6ef4796SAndreas Gohr        }
491a6ef4796SAndreas Gohr    }else{
492b625487dSandi        //check alternative plural/nonplural form
493b625487dSandi        if(!@file_exists($file)){
494b625487dSandi            if( $conf['autoplural'] ){
495b625487dSandi                if(substr($page,-1) == 's'){
496b625487dSandi                    $try = substr($page,0,-1);
497b625487dSandi                }else{
498b625487dSandi                    $try = $page.'s';
499b625487dSandi                }
500103c256aSChris Smith                if(page_exists($try)){
501b625487dSandi                    $page   = $try;
502b625487dSandi                    $exists = true;
503b625487dSandi                }
504b625487dSandi            }
505b625487dSandi        }else{
506b625487dSandi            $exists = true;
507b625487dSandi        }
508a6ef4796SAndreas Gohr    }
509a6ef4796SAndreas Gohr
510a6ef4796SAndreas Gohr    // now make sure we have a clean page
511a6ef4796SAndreas Gohr    $page = cleanID($page);
512b625487dSandi
513b625487dSandi    //add hash if any
514b2d7d3f2Sandi    if(!empty($hash)) $page .= '#'.$hash;
515b625487dSandi}
516b625487dSandi
51798407a7aSandi/**
51898407a7aSandi * Returns the name of a cachefile from given data
51998407a7aSandi *
52098407a7aSandi * The needed directory is created by this function!
52198407a7aSandi *
52298407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
52398407a7aSandi *
52498407a7aSandi * @param string $data  This data is used to create a unique md5 name
52598407a7aSandi * @param string $ext   This is appended to the filename if given
52698407a7aSandi * @return string       The filename of the cachefile
52798407a7aSandi */
52898407a7aSandifunction getCacheName($data,$ext=''){
52998407a7aSandi    global $conf;
53098407a7aSandi    $md5  = md5($data);
53198407a7aSandi    $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
53298407a7aSandi    io_makeFileDir($file);
53398407a7aSandi    return $file;
53498407a7aSandi}
53598407a7aSandi
5360dc92c6fSAndreas Gohr/**
5370dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
5380dc92c6fSAndreas Gohr *
5390dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5400dc92c6fSAndreas Gohr */
5410dc92c6fSAndreas Gohrfunction isHiddenPage($id){
5428449cc9dSDominik Eckelmann    $data = array(
5438449cc9dSDominik Eckelmann        'id' => $id,
5448449cc9dSDominik Eckelmann        'hidden' => false
5458449cc9dSDominik Eckelmann    );
546fb55b51eSDominik Eckelmann    trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage');
547fb55b51eSDominik Eckelmann    return $data['hidden'];
5480dc92c6fSAndreas Gohr}
549fb55b51eSDominik Eckelmann
550dbf714f7SGerrit Uitslag/**
551dbf714f7SGerrit Uitslag * callback checks if page is hidden
552dbf714f7SGerrit Uitslag *
553dbf714f7SGerrit Uitslag * @param array $data event data    see isHiddenPage()
554dbf714f7SGerrit Uitslag */
555fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) {
556fb55b51eSDominik Eckelmann    global $conf;
557fb55b51eSDominik Eckelmann    global $ACT;
558fb55b51eSDominik Eckelmann
559fb55b51eSDominik Eckelmann    if ($data['hidden']) return;
560fb55b51eSDominik Eckelmann    if(empty($conf['hidepages'])) return;
561fb55b51eSDominik Eckelmann    if($ACT == 'admin') return;
562fb55b51eSDominik Eckelmann
563fb55b51eSDominik Eckelmann    if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){
564fb55b51eSDominik Eckelmann        $data['hidden'] = true;
565fb55b51eSDominik Eckelmann    }
5660dc92c6fSAndreas Gohr}
5670dc92c6fSAndreas Gohr
5680dc92c6fSAndreas Gohr/**
5690dc92c6fSAndreas Gohr * Reverse of isHiddenPage
5700dc92c6fSAndreas Gohr *
5710dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5720dc92c6fSAndreas Gohr */
5730dc92c6fSAndreas Gohrfunction isVisiblePage($id){
5740dc92c6fSAndreas Gohr    return !isHiddenPage($id);
5750dc92c6fSAndreas Gohr}
5760dc92c6fSAndreas Gohr
5775b75cd1fSAdrian Lang/**
5785b75cd1fSAdrian Lang * Format an id for output to a user
5795b75cd1fSAdrian Lang *
5805b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is
5815b75cd1fSAdrian Lang * “*”. Output is escaped.
5825b75cd1fSAdrian Lang *
5835b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
5845b75cd1fSAdrian Lang */
5850ac9a84dSoliver
5865b75cd1fSAdrian Langfunction prettyprint_id($id) {
5875b75cd1fSAdrian Lang    if (!$id || $id === ':') {
5885b75cd1fSAdrian Lang        return '*';
5895b75cd1fSAdrian Lang    }
5905b75cd1fSAdrian Lang    if ((substr($id, -1, 1) === ':')) {
5915b75cd1fSAdrian Lang        $id .= '*';
5925b75cd1fSAdrian Lang    }
5935b75cd1fSAdrian Lang    return hsc($id);
5945b75cd1fSAdrian Lang}
595f03fd957SAndreas Gohr
596f03fd957SAndreas Gohr/**
597f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem
598f03fd957SAndreas Gohr *
599f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
600f03fd957SAndreas Gohr *
601f03fd957SAndreas Gohr * When the second parameter is true the string will
602f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected -
603f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the
604f03fd957SAndreas Gohr * same string (default is true)
605f03fd957SAndreas Gohr *
606f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
607f03fd957SAndreas Gohr * @see    urlencode
608f03fd957SAndreas Gohr */
609f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){
610f03fd957SAndreas Gohr    global $conf;
611f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
612f03fd957SAndreas Gohr
613f03fd957SAndreas Gohr    if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
614f03fd957SAndreas Gohr        return $file;
615f03fd957SAndreas Gohr    }
616f03fd957SAndreas Gohr
617f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
618f03fd957SAndreas Gohr        return SafeFN::encode($file);
619f03fd957SAndreas Gohr    }
620f03fd957SAndreas Gohr
621f03fd957SAndreas Gohr    $file = urlencode($file);
622f03fd957SAndreas Gohr    $file = str_replace('%2F','/',$file);
623f03fd957SAndreas Gohr    return $file;
624f03fd957SAndreas Gohr}
625f03fd957SAndreas Gohr
626f03fd957SAndreas Gohr/**
627f03fd957SAndreas Gohr * Decode a filename back to UTF-8
628f03fd957SAndreas Gohr *
629f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
630f03fd957SAndreas Gohr *
631f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
632f03fd957SAndreas Gohr * @see    urldecode
633f03fd957SAndreas Gohr */
634f03fd957SAndreas Gohrfunction utf8_decodeFN($file){
635f03fd957SAndreas Gohr    global $conf;
636f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
637f03fd957SAndreas Gohr
638f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
639f03fd957SAndreas Gohr        return SafeFN::decode($file);
640f03fd957SAndreas Gohr    }
641f03fd957SAndreas Gohr
642f03fd957SAndreas Gohr    return urldecode($file);
643f03fd957SAndreas Gohr}
644f03fd957SAndreas Gohr
645e66d3e6dSAndreas Gohr/**
646e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any
647e66d3e6dSAndreas Gohr * higher namespace
648e66d3e6dSAndreas Gohr *
649e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well
650e66d3e6dSAndreas Gohr *
651e66d3e6dSAndreas Gohr * @todo   add event hook
652e66d3e6dSAndreas Gohr * @param  string $page the pagename you're looking for
653e66d3e6dSAndreas Gohr * @return string|false the full page id of the found page, false if any
654e66d3e6dSAndreas Gohr */
655e66d3e6dSAndreas Gohrfunction page_findnearest($page){
656c786a1b6SAnika Henke    if (!$page) return false;
657e66d3e6dSAndreas Gohr    global $ID;
658e66d3e6dSAndreas Gohr
659e66d3e6dSAndreas Gohr    $ns = $ID;
660e66d3e6dSAndreas Gohr    do {
661e66d3e6dSAndreas Gohr        $ns = getNS($ns);
662e66d3e6dSAndreas Gohr        $pageid = ltrim("$ns:$page",':');
663e66d3e6dSAndreas Gohr        if(page_exists($pageid)){
664e66d3e6dSAndreas Gohr            return $pageid;
665e66d3e6dSAndreas Gohr        }
666e66d3e6dSAndreas Gohr    } while($ns);
667e66d3e6dSAndreas Gohr
668e66d3e6dSAndreas Gohr    return false;
669e66d3e6dSAndreas Gohr}
670