xref: /dokuwiki/inc/pageutils.php (revision 15851b98227ebf9671f8f7facd11426a72da6ea6)
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){
22585bf44eSChristopher 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){
31585bf44eSChristopher 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            }
40585bf44eSChristopher Smith            $script = $conf['basedir'].$relpath.utf8_basename($INPUT->server->str('SCRIPT_FILENAME'));
417d71d4b7SAndreas Gohr
42585bf44eSChristopher Smith        }elseif($INPUT->server->str('PATH_INFO')){
43585bf44eSChristopher Smith            $request = $INPUT->server->str('PATH_INFO');
44585bf44eSChristopher Smith        }elseif($INPUT->server->str('SCRIPT_NAME')){
45585bf44eSChristopher Smith            $script = $INPUT->server->str('SCRIPT_NAME');
46585bf44eSChristopher Smith        }elseif($INPUT->server->str('DOCUMENT_ROOT') && $INPUT->server->str('SCRIPT_FILENAME')){
47585bf44eSChristopher Smith            $script = preg_replace ('/^'.preg_quote($INPUT->server->str('DOCUMENT_ROOT'),'/').'/','',
48585bf44eSChristopher 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>
149*15851b98SGerrit Uitslag *
150*15851b98SGerrit Uitslag * @param string $id
151*15851b98SGerrit Uitslag * @return string
152b625487dSandi */
153b625487dSandifunction getNS($id){
1543a50618cSgweissbach    $pos = strrpos((string)$id,':');
155c4e0e4a1SAndreas Gohr    if($pos!==false){
1563a50618cSgweissbach        return substr((string)$id,0,$pos);
157b625487dSandi    }
158*15851b98SGerrit Uitslag    return '';
159b625487dSandi}
160b625487dSandi
161b625487dSandi/**
162b625487dSandi * Returns the ID without the namespace
163b625487dSandi *
164b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
165*15851b98SGerrit Uitslag *
166*15851b98SGerrit Uitslag * @param string $id
167*15851b98SGerrit Uitslag * @return string
168b625487dSandi */
169b625487dSandifunction noNS($id) {
1702844584fSBen Coburn    $pos = strrpos($id, ':');
1712844584fSBen Coburn    if ($pos!==false) {
1722844584fSBen Coburn        return substr($id, $pos+1);
1732844584fSBen Coburn    } else {
1742844584fSBen Coburn        return $id;
1752844584fSBen Coburn    }
1761a84a0f3SAnika Henke}
1771a84a0f3SAnika Henke
1781a84a0f3SAnika Henke/**
1791a84a0f3SAnika Henke * Returns the current namespace
1801a84a0f3SAnika Henke *
1811a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1821a84a0f3SAnika Henke */
1831a84a0f3SAnika Henkefunction curNS($id) {
1841a84a0f3SAnika Henke    return noNS(getNS($id));
1851a84a0f3SAnika Henke}
1861a84a0f3SAnika Henke
1871a84a0f3SAnika Henke/**
1881a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages
1891a84a0f3SAnika Henke *
1901a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1911a84a0f3SAnika Henke */
1921a84a0f3SAnika Henkefunction noNSorNS($id) {
1931a84a0f3SAnika Henke    global $conf;
1941a84a0f3SAnika Henke
1951a84a0f3SAnika Henke    $p = noNS($id);
1969708106bSAdrian Lang    if ($p == $conf['start'] || $p == false) {
1971a84a0f3SAnika Henke        $p = curNS($id);
1981a84a0f3SAnika Henke        if ($p == false) {
1999708106bSAdrian Lang            return $conf['start'];
2001a84a0f3SAnika Henke        }
2011a84a0f3SAnika Henke    }
2021a84a0f3SAnika Henke    return $p;
203b625487dSandi}
2044ceab83fSAndreas Gohr
2054ceab83fSAndreas Gohr/**
2064ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title
2074ceab83fSAndreas Gohr *
2084ceab83fSAndreas Gohr * @param string  $title   The headline title
209c857afe0SMichael Hamann * @param array|bool   $check   Existing IDs (title => number)
210c857afe0SMichael Hamann * @return string the title
2114ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2124ceab83fSAndreas Gohr */
213443d207bSAndreas Gohrfunction sectionID($title,&$check) {
214de9114eaSAnika Henke    $title = str_replace(array(':','.'),'',cleanID($title));
215de9114eaSAnika Henke    $new = ltrim($title,'0123456789_-');
2164ceab83fSAndreas Gohr    if(empty($new)){
2174ceab83fSAndreas Gohr        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
2184ceab83fSAndreas Gohr    }else{
2194ceab83fSAndreas Gohr        $title = $new;
2204ceab83fSAndreas Gohr    }
2214ceab83fSAndreas Gohr
222443d207bSAndreas Gohr    if(is_array($check)){
2234ceab83fSAndreas Gohr        // make sure tiles are unique
22401e3159cSChris Tapp        if (!array_key_exists ($title,$check)) {
22501e3159cSChris Tapp            $check[$title] = 0;
22601e3159cSChris Tapp        } else {
22701e3159cSChris Tapp            $title .= ++ $check[$title];
2284ceab83fSAndreas Gohr        }
2294ceab83fSAndreas Gohr    }
2304ceab83fSAndreas Gohr
2314ceab83fSAndreas Gohr    return $title;
2324ceab83fSAndreas Gohr}
2334ceab83fSAndreas Gohr
234b625487dSandi
235b625487dSandi/**
236103c256aSChris Smith * Wiki page existence check
237103c256aSChris Smith *
238103c256aSChris Smith * parameters as for wikiFN
239103c256aSChris Smith *
240103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk>
241103c256aSChris Smith */
242103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
243103c256aSChris Smith    return @file_exists(wikiFN($id,$rev,$clean));
244103c256aSChris Smith}
245103c256aSChris Smith
246103c256aSChris Smith/**
247103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
248b625487dSandi *
249b625487dSandi * The filename is URL encoded to protect Unicode chars
250b625487dSandi *
251103c256aSChris Smith * @param  $raw_id  string   id of wikipage
252103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
253103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
254103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
255dbf714f7SGerrit Uitslag * @return string full path
256103c256aSChris Smith *
257b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
258b625487dSandi */
2596e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
260b625487dSandi    global $conf;
2616e0cc83aSchris
262dc2c0e04Schris    global $cache_wikifn;
263dc2c0e04Schris    $cache = & $cache_wikifn;
264dc2c0e04Schris
2656e0cc83aSchris    if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2666e0cc83aSchris        return $cache[$raw_id][$rev];
2676e0cc83aSchris    }
2686e0cc83aSchris
2696e0cc83aSchris    $id = $raw_id;
2706e0cc83aSchris
2710d8ea614Schris    if ($clean) $id = cleanID($id);
272b625487dSandi    $id = str_replace(':','/',$id);
273b625487dSandi    if(empty($rev)){
274b625487dSandi        $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
275b625487dSandi    }else{
276b625487dSandi        $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
277ff3ed99fSmarcel        if($conf['compression']){
278ff3ed99fSmarcel            //test for extensions here, we want to read both compressions
279d8186216SBen Coburn            if (@file_exists($fn . '.gz')){
280b625487dSandi                $fn .= '.gz';
281d8186216SBen Coburn            }else if(@file_exists($fn . '.bz2')){
282ff3ed99fSmarcel                $fn .= '.bz2';
283ff3ed99fSmarcel            }else{
284ff3ed99fSmarcel                //file doesnt exist yet, so we take the configured extension
285ff3ed99fSmarcel                $fn .= '.' . $conf['compression'];
286ff3ed99fSmarcel            }
287b625487dSandi        }
288b625487dSandi    }
2896e0cc83aSchris
29050602150SBen Coburn    if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2916e0cc83aSchris    $cache[$raw_id][$rev] = $fn;
292b625487dSandi    return $fn;
293b625487dSandi}
294b625487dSandi
295b625487dSandi/**
296c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
297c9b4bd1eSBen Coburn *
298c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
299c9b4bd1eSBen Coburn */
300c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
301c9b4bd1eSBen Coburn    global $conf;
302662ff478SAndreas Gohr    return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
303c9b4bd1eSBen Coburn}
304c9b4bd1eSBen Coburn
305c9b4bd1eSBen Coburn
306c9b4bd1eSBen Coburn/**
3071380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
308b158d625SSteven Danz *
309b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
310b158d625SSteven Danz */
3111380fc45SAndreas Gohrfunction metaFN($id,$ext){
312b158d625SSteven Danz    global $conf;
313b158d625SSteven Danz    $id = cleanID($id);
314b158d625SSteven Danz    $id = str_replace(':','/',$id);
3151380fc45SAndreas Gohr    $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
316b158d625SSteven Danz    return $fn;
317b158d625SSteven Danz}
318b158d625SSteven Danz
319b158d625SSteven Danz/**
320e4f389efSKate Arzamastseva * returns the full path to the media's meta file specified by ID and extension
321e4f389efSKate Arzamastseva *
322cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
323e4f389efSKate Arzamastseva */
324e4f389efSKate Arzamastsevafunction mediaMetaFN($id,$ext){
325e4f389efSKate Arzamastseva    global $conf;
326e4f389efSKate Arzamastseva    $id = cleanID($id);
327e4f389efSKate Arzamastseva    $id = str_replace(':','/',$id);
328e4f389efSKate Arzamastseva    $fn = $conf['mediametadir'].'/'.utf8_encodeFN($id).$ext;
329e4f389efSKate Arzamastseva    return $fn;
330e4f389efSKate Arzamastseva}
331e4f389efSKate Arzamastseva
332e4f389efSKate Arzamastseva/**
333e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
334e1f3d9e1SEsther Brunner *
335e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
336ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de>
337e1f3d9e1SEsther Brunner */
338e1f3d9e1SEsther Brunnerfunction metaFiles($id){
339ba0267b3SMichael Hamann    $basename = metaFN($id, '');
340ba0267b3SMichael Hamann    $files    = glob($basename.'.*', GLOB_MARK);
341ba0267b3SMichael Hamann    // filter files like foo.bar.meta when $id == 'foo'
342ba0267b3SMichael Hamann    return    $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array();
343e1f3d9e1SEsther Brunner}
344e1f3d9e1SEsther Brunner
345e1f3d9e1SEsther Brunner/**
346b625487dSandi * returns the full path to the mediafile specified by ID
347b625487dSandi *
348b625487dSandi * The filename is URL encoded to protect Unicode chars
349b625487dSandi *
350b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
351cbe26ad6SKate Arzamastseva * @author Kate Arzamastseva <pshns@ukr.net>
352b625487dSandi */
353e4f389efSKate Arzamastsevafunction mediaFN($id, $rev=''){
354b625487dSandi    global $conf;
355b625487dSandi    $id = cleanID($id);
356b625487dSandi    $id = str_replace(':','/',$id);
357e4f389efSKate Arzamastseva    if(empty($rev)){
358b625487dSandi        $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
359e4f389efSKate Arzamastseva    }else{
360cbe26ad6SKate Arzamastseva        $ext = mimetype($id);
3618e69fd30SKate Arzamastseva        $name = substr($id,0, -1*strlen($ext[0])-1);
36261f1aad8SKate Arzamastseva        $fn = $conf['mediaolddir'].'/'.utf8_encodeFN($name .'.'.( (int) $rev ).'.'.$ext[0]);
363e4f389efSKate Arzamastseva    }
364b625487dSandi    return $fn;
365b625487dSandi}
366b625487dSandi
367b625487dSandi/**
3682adaf2b8SAndreas Gohr * Returns the full filepath to a localized file if local
369b625487dSandi * version isn't found the english one is returned
370b625487dSandi *
3712adaf2b8SAndreas Gohr * @param  string $id  The id of the local file
3722adaf2b8SAndreas Gohr * @param  string $ext The file extension (usually txt)
373dbf714f7SGerrit Uitslag * @return string full filepath to localized file
374b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
375b625487dSandi */
3762adaf2b8SAndreas Gohrfunction localeFN($id,$ext='txt'){
377b625487dSandi    global $conf;
3788819fbf5ShArpanet    $file = DOKU_CONF.'lang/'.$conf['lang'].'/'.$id.'.'.$ext;
379e6cecb08SMichael Hamann    if(!@file_exists($file)){
3802adaf2b8SAndreas Gohr        $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.'.$ext;
381b625487dSandi        if(!@file_exists($file)){
382b625487dSandi            //fall back to english
3832adaf2b8SAndreas Gohr            $file = DOKU_INC.'inc/lang/en/'.$id.'.'.$ext;
384b625487dSandi        }
385e6cecb08SMichael Hamann    }
386b625487dSandi    return $file;
387b625487dSandi}
388b625487dSandi
389b625487dSandi/**
390c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
391c4e0e4a1SAndreas Gohr *
392c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
393c4e0e4a1SAndreas Gohr * instead
394c4e0e4a1SAndreas Gohr *
395c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
396c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
397c4e0e4a1SAndreas Gohr *
398c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
399c4e0e4a1SAndreas Gohr */
400a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
401c662a49aSAndreas Gohr    global $conf;
402c662a49aSAndreas Gohr
403c662a49aSAndreas Gohr    // some pre cleaning for useslash:
404c662a49aSAndreas Gohr    if($conf['useslash']) $id = str_replace('/',':',$id);
405c662a49aSAndreas Gohr
406c4e0e4a1SAndreas Gohr    // if the id starts with a dot we need to handle the
407c4e0e4a1SAndreas Gohr    // relative stuff
408443e135dSChristopher Smith    if($id && $id{0} == '.'){
409c4e0e4a1SAndreas Gohr        // normalize initial dots without a colon
410c4e0e4a1SAndreas Gohr        $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
411c4e0e4a1SAndreas Gohr        // prepend the current namespace
412c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
413c4e0e4a1SAndreas Gohr
414c4e0e4a1SAndreas Gohr        // cleanup relatives
415c4e0e4a1SAndreas Gohr        $result = array();
416c4e0e4a1SAndreas Gohr        $pathA  = explode(':', $id);
417c4e0e4a1SAndreas Gohr        if (!$pathA[0]) $result[] = '';
418c4e0e4a1SAndreas Gohr        foreach ($pathA AS $key => $dir) {
419c4e0e4a1SAndreas Gohr            if ($dir == '..') {
420c4e0e4a1SAndreas Gohr                if (end($result) == '..') {
421c4e0e4a1SAndreas Gohr                    $result[] = '..';
422c4e0e4a1SAndreas Gohr                } elseif (!array_pop($result)) {
423c4e0e4a1SAndreas Gohr                    $result[] = '..';
424c4e0e4a1SAndreas Gohr                }
425c4e0e4a1SAndreas Gohr            } elseif ($dir && $dir != '.') {
426c4e0e4a1SAndreas Gohr                $result[] = $dir;
427c4e0e4a1SAndreas Gohr            }
428c4e0e4a1SAndreas Gohr        }
429c4e0e4a1SAndreas Gohr        if (!end($pathA)) $result[] = '';
430c4e0e4a1SAndreas Gohr        $id = implode(':', $result);
431c4e0e4a1SAndreas Gohr    }elseif($ns !== false && strpos($id,':') === false){
432c4e0e4a1SAndreas Gohr        //if link contains no namespace. add current namespace (if any)
433c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
434c4e0e4a1SAndreas Gohr    }
435c4e0e4a1SAndreas Gohr
436a6ef4796SAndreas Gohr    if($clean) $id = cleanID($id);
437a6ef4796SAndreas Gohr    return $id;
438c4e0e4a1SAndreas Gohr}
439c4e0e4a1SAndreas Gohr
440c4e0e4a1SAndreas Gohr/**
441b625487dSandi * Returns a full media id
442b625487dSandi *
443b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
444b625487dSandi */
44537e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
446c4e0e4a1SAndreas Gohr    $page   = resolve_id($ns,$page);
447b625487dSandi    $file   = mediaFN($page);
448b625487dSandi    $exists = @file_exists($file);
449b625487dSandi}
450b625487dSandi
451b625487dSandi/**
452b625487dSandi * Returns a full page id
453b625487dSandi *
454b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
455b625487dSandi */
45637e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
457b625487dSandi    global $conf;
458c006739eSIzidor Matušov    global $ID;
4590b7c14c2Sandi    $exists = false;
460b625487dSandi
461c006739eSIzidor Matušov    //empty address should point to current page
462c006739eSIzidor Matušov    if ($page === "") {
463c006739eSIzidor Matušov        $page = $ID;
464c006739eSIzidor Matušov    }
465c006739eSIzidor Matušov
466b625487dSandi    //keep hashlink if exists then clean both parts
46703c4aec3Schris    if (strpos($page,'#')) {
4684b7f9e70STom N Harris        list($page,$hash) = explode('#',$page,2);
46903c4aec3Schris    } else {
47003c4aec3Schris        $hash = '';
47103c4aec3Schris    }
472b625487dSandi    $hash = cleanID($hash);
473a6ef4796SAndreas Gohr    $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
474b625487dSandi
475a6ef4796SAndreas Gohr    // get filename (calls clean itself)
476b625487dSandi    $file = wikiFN($page);
477b625487dSandi
4781179df0eSGuy Brand    // if ends with colon or slash we have a namespace link
479b26cdbbeSAdrian Lang    if(in_array(substr($page,-1), array(':', ';')) ||
480b26cdbbeSAdrian Lang       ($conf['useslash'] && substr($page,-1) == '/')){
481103c256aSChris Smith        if(page_exists($page.$conf['start'])){
482a6ef4796SAndreas Gohr            // start page inside namespace
483a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
484a6ef4796SAndreas Gohr            $exists = true;
485103c256aSChris Smith        }elseif(page_exists($page.noNS(cleanID($page)))){
486a6ef4796SAndreas Gohr            // page named like the NS inside the NS
487a6ef4796SAndreas Gohr            $page = $page.noNS(cleanID($page));
488a6ef4796SAndreas Gohr            $exists = true;
489103c256aSChris Smith        }elseif(page_exists($page)){
490a6ef4796SAndreas Gohr            // page like namespace exists
491a6ef4796SAndreas Gohr            $page = $page;
492a6ef4796SAndreas Gohr            $exists = true;
493a6ef4796SAndreas Gohr        }else{
494a6ef4796SAndreas Gohr            // fall back to default
495a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
496a6ef4796SAndreas Gohr        }
497a6ef4796SAndreas Gohr    }else{
498b625487dSandi        //check alternative plural/nonplural form
499b625487dSandi        if(!@file_exists($file)){
500b625487dSandi            if( $conf['autoplural'] ){
501b625487dSandi                if(substr($page,-1) == 's'){
502b625487dSandi                    $try = substr($page,0,-1);
503b625487dSandi                }else{
504b625487dSandi                    $try = $page.'s';
505b625487dSandi                }
506103c256aSChris Smith                if(page_exists($try)){
507b625487dSandi                    $page   = $try;
508b625487dSandi                    $exists = true;
509b625487dSandi                }
510b625487dSandi            }
511b625487dSandi        }else{
512b625487dSandi            $exists = true;
513b625487dSandi        }
514a6ef4796SAndreas Gohr    }
515a6ef4796SAndreas Gohr
516a6ef4796SAndreas Gohr    // now make sure we have a clean page
517a6ef4796SAndreas Gohr    $page = cleanID($page);
518b625487dSandi
519b625487dSandi    //add hash if any
520b2d7d3f2Sandi    if(!empty($hash)) $page .= '#'.$hash;
521b625487dSandi}
522b625487dSandi
52398407a7aSandi/**
52498407a7aSandi * Returns the name of a cachefile from given data
52598407a7aSandi *
52698407a7aSandi * The needed directory is created by this function!
52798407a7aSandi *
52898407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
52998407a7aSandi *
53098407a7aSandi * @param string $data  This data is used to create a unique md5 name
53198407a7aSandi * @param string $ext   This is appended to the filename if given
53298407a7aSandi * @return string       The filename of the cachefile
53398407a7aSandi */
53498407a7aSandifunction getCacheName($data,$ext=''){
53598407a7aSandi    global $conf;
53698407a7aSandi    $md5  = md5($data);
53798407a7aSandi    $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
53898407a7aSandi    io_makeFileDir($file);
53998407a7aSandi    return $file;
54098407a7aSandi}
54198407a7aSandi
5420dc92c6fSAndreas Gohr/**
5430dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
5440dc92c6fSAndreas Gohr *
5450dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5460dc92c6fSAndreas Gohr */
5470dc92c6fSAndreas Gohrfunction isHiddenPage($id){
5488449cc9dSDominik Eckelmann    $data = array(
5498449cc9dSDominik Eckelmann        'id' => $id,
5508449cc9dSDominik Eckelmann        'hidden' => false
5518449cc9dSDominik Eckelmann    );
552fb55b51eSDominik Eckelmann    trigger_event('PAGEUTILS_ID_HIDEPAGE', $data, '_isHiddenPage');
553fb55b51eSDominik Eckelmann    return $data['hidden'];
5540dc92c6fSAndreas Gohr}
555fb55b51eSDominik Eckelmann
556dbf714f7SGerrit Uitslag/**
557dbf714f7SGerrit Uitslag * callback checks if page is hidden
558dbf714f7SGerrit Uitslag *
559dbf714f7SGerrit Uitslag * @param array $data event data    see isHiddenPage()
560dbf714f7SGerrit Uitslag */
561fb55b51eSDominik Eckelmannfunction _isHiddenPage(&$data) {
562fb55b51eSDominik Eckelmann    global $conf;
563fb55b51eSDominik Eckelmann    global $ACT;
564fb55b51eSDominik Eckelmann
565fb55b51eSDominik Eckelmann    if ($data['hidden']) return;
566fb55b51eSDominik Eckelmann    if(empty($conf['hidepages'])) return;
567fb55b51eSDominik Eckelmann    if($ACT == 'admin') return;
568fb55b51eSDominik Eckelmann
569fb55b51eSDominik Eckelmann    if(preg_match('/'.$conf['hidepages'].'/ui',':'.$data['id'])){
570fb55b51eSDominik Eckelmann        $data['hidden'] = true;
571fb55b51eSDominik Eckelmann    }
5720dc92c6fSAndreas Gohr}
5730dc92c6fSAndreas Gohr
5740dc92c6fSAndreas Gohr/**
5750dc92c6fSAndreas Gohr * Reverse of isHiddenPage
5760dc92c6fSAndreas Gohr *
5770dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5780dc92c6fSAndreas Gohr */
5790dc92c6fSAndreas Gohrfunction isVisiblePage($id){
5800dc92c6fSAndreas Gohr    return !isHiddenPage($id);
5810dc92c6fSAndreas Gohr}
5820dc92c6fSAndreas Gohr
5835b75cd1fSAdrian Lang/**
5845b75cd1fSAdrian Lang * Format an id for output to a user
5855b75cd1fSAdrian Lang *
5865b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is
5875b75cd1fSAdrian Lang * “*”. Output is escaped.
5885b75cd1fSAdrian Lang *
5895b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
5905b75cd1fSAdrian Lang */
5910ac9a84dSoliver
5925b75cd1fSAdrian Langfunction prettyprint_id($id) {
5935b75cd1fSAdrian Lang    if (!$id || $id === ':') {
5945b75cd1fSAdrian Lang        return '*';
5955b75cd1fSAdrian Lang    }
5965b75cd1fSAdrian Lang    if ((substr($id, -1, 1) === ':')) {
5975b75cd1fSAdrian Lang        $id .= '*';
5985b75cd1fSAdrian Lang    }
5995b75cd1fSAdrian Lang    return hsc($id);
6005b75cd1fSAdrian Lang}
601f03fd957SAndreas Gohr
602f03fd957SAndreas Gohr/**
603f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem
604f03fd957SAndreas Gohr *
605f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
606f03fd957SAndreas Gohr *
607f03fd957SAndreas Gohr * When the second parameter is true the string will
608f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected -
609f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the
610f03fd957SAndreas Gohr * same string (default is true)
611f03fd957SAndreas Gohr *
612f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
613f03fd957SAndreas Gohr * @see    urlencode
614f03fd957SAndreas Gohr */
615f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){
616f03fd957SAndreas Gohr    global $conf;
617f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
618f03fd957SAndreas Gohr
619f03fd957SAndreas Gohr    if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
620f03fd957SAndreas Gohr        return $file;
621f03fd957SAndreas Gohr    }
622f03fd957SAndreas Gohr
623f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
624f03fd957SAndreas Gohr        return SafeFN::encode($file);
625f03fd957SAndreas Gohr    }
626f03fd957SAndreas Gohr
627f03fd957SAndreas Gohr    $file = urlencode($file);
628f03fd957SAndreas Gohr    $file = str_replace('%2F','/',$file);
629f03fd957SAndreas Gohr    return $file;
630f03fd957SAndreas Gohr}
631f03fd957SAndreas Gohr
632f03fd957SAndreas Gohr/**
633f03fd957SAndreas Gohr * Decode a filename back to UTF-8
634f03fd957SAndreas Gohr *
635f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
636f03fd957SAndreas Gohr *
637f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
638f03fd957SAndreas Gohr * @see    urldecode
639f03fd957SAndreas Gohr */
640f03fd957SAndreas Gohrfunction utf8_decodeFN($file){
641f03fd957SAndreas Gohr    global $conf;
642f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
643f03fd957SAndreas Gohr
644f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
645f03fd957SAndreas Gohr        return SafeFN::decode($file);
646f03fd957SAndreas Gohr    }
647f03fd957SAndreas Gohr
648f03fd957SAndreas Gohr    return urldecode($file);
649f03fd957SAndreas Gohr}
650f03fd957SAndreas Gohr
651e66d3e6dSAndreas Gohr/**
652e66d3e6dSAndreas Gohr * Find a page in the current namespace (determined from $ID) or any
653e66d3e6dSAndreas Gohr * higher namespace
654e66d3e6dSAndreas Gohr *
655e66d3e6dSAndreas Gohr * Used for sidebars, but can be used other stuff as well
656e66d3e6dSAndreas Gohr *
657e66d3e6dSAndreas Gohr * @todo   add event hook
658e66d3e6dSAndreas Gohr * @param  string $page the pagename you're looking for
659e66d3e6dSAndreas Gohr * @return string|false the full page id of the found page, false if any
660e66d3e6dSAndreas Gohr */
661e66d3e6dSAndreas Gohrfunction page_findnearest($page){
662c786a1b6SAnika Henke    if (!$page) return false;
663e66d3e6dSAndreas Gohr    global $ID;
664e66d3e6dSAndreas Gohr
665e66d3e6dSAndreas Gohr    $ns = $ID;
666e66d3e6dSAndreas Gohr    do {
667e66d3e6dSAndreas Gohr        $ns = getNS($ns);
668e66d3e6dSAndreas Gohr        $pageid = ltrim("$ns:$page",':');
669e66d3e6dSAndreas Gohr        if(page_exists($pageid)){
670e66d3e6dSAndreas Gohr            return $pageid;
671e66d3e6dSAndreas Gohr        }
672e66d3e6dSAndreas Gohr    } while($ns);
673e66d3e6dSAndreas Gohr
674e66d3e6dSAndreas Gohr    return false;
675e66d3e6dSAndreas Gohr}
676