xref: /dokuwiki/inc/pageutils.php (revision ba0267b3bcb1b5feeb707e9a92766c4a619409bb)
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){
226c7843b5Sandi    global $conf;
236c7843b5Sandi
2403c4aec3Schris    $id = isset($_REQUEST[$param]) ? $_REQUEST[$param] : null;
2548665d38SAndreas Gohr
266c7843b5Sandi    //construct page id from request URI
276c7843b5Sandi    if(empty($id) && $conf['userewrite'] == 2){
2806368e4dSMichael Hamann        $request = $_SERVER['REQUEST_URI'];
2906368e4dSMichael Hamann        $script = '';
3006368e4dSMichael Hamann
316c7843b5Sandi        //get the script URL
326c7843b5Sandi        if($conf['basedir']){
3381124000Sjan            $relpath = '';
3481124000Sjan            if($param != 'id') {
3581124000Sjan                $relpath = 'lib/exe/';
3681124000Sjan            }
3781124000Sjan            $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
387d71d4b7SAndreas Gohr
3906368e4dSMichael Hamann        }elseif($_SERVER['PATH_INFO']){
4006368e4dSMichael Hamann            $request = $_SERVER['PATH_INFO'];
4106368e4dSMichael Hamann        }elseif($_SERVER['SCRIPT_NAME']){
4206368e4dSMichael Hamann            $script = $_SERVER['SCRIPT_NAME'];
436c7843b5Sandi        }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
446c7843b5Sandi            $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
456c7843b5Sandi                    $_SERVER['SCRIPT_FILENAME']);
466c7843b5Sandi            $script = '/'.$script;
476c7843b5Sandi        }
486c7843b5Sandi
4952339126Sandi        //clean script and request (fixes a windows problem)
5052339126Sandi        $script  = preg_replace('/\/\/+/','/',$script);
517d71d4b7SAndreas Gohr        $request = preg_replace('/\/\/+/','/',$request);
5252339126Sandi
536c7843b5Sandi        //remove script URL and Querystring to gain the id
5452339126Sandi        if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
556c7843b5Sandi            $id = preg_replace ('/\?.*/','',$match[1]);
566c7843b5Sandi        }
576de3759aSAndreas Gohr        $id = urldecode($id);
5842905504SAndreas Gohr        //strip leading slashes
5942905504SAndreas Gohr        $id = preg_replace('!^/+!','',$id);
606c7843b5Sandi    }
61671a58a6SGuy Brand
62671a58a6SGuy Brand    // Namespace autolinking from URL
63b6084253SAndreas Gohr    if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
64103c256aSChris Smith        if(page_exists($id.$conf['start'])){
65671a58a6SGuy Brand            // start page inside namespace
66671a58a6SGuy Brand            $id = $id.$conf['start'];
67103c256aSChris Smith        }elseif(page_exists($id.noNS(cleanID($id)))){
68671a58a6SGuy Brand            // page named like the NS inside the NS
69671a58a6SGuy Brand            $id = $id.noNS(cleanID($id));
70103c256aSChris Smith        }elseif(page_exists($id)){
71671a58a6SGuy Brand            // page like namespace exists
727a42ac9eSBen Coburn            $id = substr($id,0,-1);
73671a58a6SGuy Brand        }else{
74671a58a6SGuy Brand            // fall back to default
75671a58a6SGuy Brand            $id = $id.$conf['start'];
76671a58a6SGuy Brand        }
77397a8c4eSHelmut Tischer        send_redirect(wl($id,'',true));
78671a58a6SGuy Brand    }
79671a58a6SGuy Brand
8042905504SAndreas Gohr    if($clean) $id = cleanID($id);
810868021bSAndreas Gohr    if(empty($id) && $param=='id') $id = $conf['start'];
826c7843b5Sandi
836c7843b5Sandi    return $id;
846c7843b5Sandi}
85b625487dSandi
86b625487dSandi/**
87b625487dSandi * Remove unwanted chars from ID
88b625487dSandi *
89b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
90b625487dSandi * converted to unaccented ones
91b625487dSandi *
92b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
936e0cc83aSchris * @param  string  $raw_id    The pageid to clean
948a831f2bSAndreas Gohr * @param  boolean $ascii     Force ASCII
9563b0c1a7SGina Haeussge * @param  boolean $media     Allow leading or trailing _ for media files
96b625487dSandi */
9763b0c1a7SGina Haeussgefunction cleanID($raw_id,$ascii=false,$media=false){
98b625487dSandi    global $conf;
994b5db43bSjoe.lapp    static $sepcharpat = null;
1004b5db43bSjoe.lapp
101dc2c0e04Schris    global $cache_cleanid;
102dc2c0e04Schris    $cache = & $cache_cleanid;
1036e0cc83aSchris
1046e0cc83aSchris    // check if it's already in the memory cache
1053a50618cSgweissbach    if (isset($cache[(string)$raw_id])) {
1063a50618cSgweissbach        return $cache[(string)$raw_id];
1076e0cc83aSchris    }
1086e0cc83aSchris
1094b5db43bSjoe.lapp    $sepchar = $conf['sepchar'];
1104b5db43bSjoe.lapp    if($sepcharpat == null) // build string only once to save clock cycles
1114b5db43bSjoe.lapp        $sepcharpat = '#\\'.$sepchar.'+#';
1124b5db43bSjoe.lapp
1133a50618cSgweissbach    $id = trim((string)$raw_id);
114b625487dSandi    $id = utf8_strtolower($id);
115b625487dSandi
116b625487dSandi    //alternative namespace seperator
117b625487dSandi    $id = strtr($id,';',':');
118b625487dSandi    if($conf['useslash']){
119b625487dSandi        $id = strtr($id,'/',':');
120b625487dSandi    }else{
1214eeffcd2SAndreas Gohr        $id = strtr($id,'/',$sepchar);
122b625487dSandi    }
123b625487dSandi
1248a831f2bSAndreas Gohr    if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1258a831f2bSAndreas Gohr    if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
126b625487dSandi
127b625487dSandi    //remove specials
128ad81d431SAndreas Gohr    $id = utf8_stripspecials($id,$sepchar,'\*');
129b625487dSandi
1308a831f2bSAndreas Gohr    if($ascii) $id = utf8_strip($id);
1318a831f2bSAndreas Gohr
132b625487dSandi    //clean up
1334b5db43bSjoe.lapp    $id = preg_replace($sepcharpat,$sepchar,$id);
134b625487dSandi    $id = preg_replace('#:+#',':',$id);
13563b0c1a7SGina Haeussge    $id = ($media ? trim($id,':.-') : trim($id,':._-'));
136b625487dSandi    $id = preg_replace('#:[:\._\-]+#',':',$id);
137b625487dSandi
1383a50618cSgweissbach    $cache[(string)$raw_id] = $id;
139b625487dSandi    return($id);
140b625487dSandi}
141b625487dSandi
142b625487dSandi/**
143b625487dSandi * Return namespacepart of a wiki ID
144b625487dSandi *
145b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
146b625487dSandi */
147b625487dSandifunction getNS($id){
1483a50618cSgweissbach    $pos = strrpos((string)$id,':');
149c4e0e4a1SAndreas Gohr    if($pos!==false){
1503a50618cSgweissbach        return substr((string)$id,0,$pos);
151b625487dSandi    }
152b625487dSandi    return false;
153b625487dSandi}
154b625487dSandi
155b625487dSandi/**
156b625487dSandi * Returns the ID without the namespace
157b625487dSandi *
158b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
159b625487dSandi */
160b625487dSandifunction noNS($id) {
1612844584fSBen Coburn    $pos = strrpos($id, ':');
1622844584fSBen Coburn    if ($pos!==false) {
1632844584fSBen Coburn        return substr($id, $pos+1);
1642844584fSBen Coburn    } else {
1652844584fSBen Coburn        return $id;
1662844584fSBen Coburn    }
1671a84a0f3SAnika Henke}
1681a84a0f3SAnika Henke
1691a84a0f3SAnika Henke/**
1701a84a0f3SAnika Henke * Returns the current namespace
1711a84a0f3SAnika Henke *
1721a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1731a84a0f3SAnika Henke */
1741a84a0f3SAnika Henkefunction curNS($id) {
1751a84a0f3SAnika Henke    return noNS(getNS($id));
1761a84a0f3SAnika Henke}
1771a84a0f3SAnika Henke
1781a84a0f3SAnika Henke/**
1791a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages
1801a84a0f3SAnika Henke *
1811a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1821a84a0f3SAnika Henke */
1831a84a0f3SAnika Henkefunction noNSorNS($id) {
1841a84a0f3SAnika Henke    global $conf;
1851a84a0f3SAnika Henke
1861a84a0f3SAnika Henke    $p = noNS($id);
1879708106bSAdrian Lang    if ($p == $conf['start'] || $p == false) {
1881a84a0f3SAnika Henke        $p = curNS($id);
1891a84a0f3SAnika Henke        if ($p == false) {
1909708106bSAdrian Lang            return $conf['start'];
1911a84a0f3SAnika Henke        }
1921a84a0f3SAnika Henke    }
1931a84a0f3SAnika Henke    return $p;
194b625487dSandi}
1954ceab83fSAndreas Gohr
1964ceab83fSAndreas Gohr/**
1974ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title
1984ceab83fSAndreas Gohr *
1994ceab83fSAndreas Gohr * @param string  $title   The headline title
20001e3159cSChris Tapp * @param array   $check   Existing IDs (title => number)
2014ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2024ceab83fSAndreas Gohr */
203443d207bSAndreas Gohrfunction sectionID($title,&$check) {
204de9114eaSAnika Henke    $title = str_replace(array(':','.'),'',cleanID($title));
205de9114eaSAnika Henke    $new = ltrim($title,'0123456789_-');
2064ceab83fSAndreas Gohr    if(empty($new)){
2074ceab83fSAndreas Gohr        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
2084ceab83fSAndreas Gohr    }else{
2094ceab83fSAndreas Gohr        $title = $new;
2104ceab83fSAndreas Gohr    }
2114ceab83fSAndreas Gohr
212443d207bSAndreas Gohr    if(is_array($check)){
2134ceab83fSAndreas Gohr        // make sure tiles are unique
21401e3159cSChris Tapp        if (!array_key_exists ($title,$check)) {
21501e3159cSChris Tapp           $check[$title] = 0;
21601e3159cSChris Tapp        } else {
21701e3159cSChris Tapp           $title .= ++ $check[$title];
2184ceab83fSAndreas Gohr        }
2194ceab83fSAndreas Gohr    }
2204ceab83fSAndreas Gohr
2214ceab83fSAndreas Gohr    return $title;
2224ceab83fSAndreas Gohr}
2234ceab83fSAndreas Gohr
224b625487dSandi
225b625487dSandi/**
226103c256aSChris Smith * Wiki page existence check
227103c256aSChris Smith *
228103c256aSChris Smith * parameters as for wikiFN
229103c256aSChris Smith *
230103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk>
231103c256aSChris Smith */
232103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
233103c256aSChris Smith    return @file_exists(wikiFN($id,$rev,$clean));
234103c256aSChris Smith}
235103c256aSChris Smith
236103c256aSChris Smith/**
237103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
238b625487dSandi *
239b625487dSandi * The filename is URL encoded to protect Unicode chars
240b625487dSandi *
241103c256aSChris Smith * @param  $raw_id  string   id of wikipage
242103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
243103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
244103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
245103c256aSChris Smith *
246b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
247b625487dSandi */
2486e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
249b625487dSandi    global $conf;
2506e0cc83aSchris
251dc2c0e04Schris    global $cache_wikifn;
252dc2c0e04Schris    $cache = & $cache_wikifn;
253dc2c0e04Schris
2546e0cc83aSchris    if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2556e0cc83aSchris        return $cache[$raw_id][$rev];
2566e0cc83aSchris    }
2576e0cc83aSchris
2586e0cc83aSchris    $id = $raw_id;
2596e0cc83aSchris
2600d8ea614Schris    if ($clean) $id = cleanID($id);
261b625487dSandi    $id = str_replace(':','/',$id);
262b625487dSandi    if(empty($rev)){
263b625487dSandi        $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
264b625487dSandi    }else{
265b625487dSandi        $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
266ff3ed99fSmarcel        if($conf['compression']){
267ff3ed99fSmarcel            //test for extensions here, we want to read both compressions
268d8186216SBen Coburn            if (@file_exists($fn . '.gz')){
269b625487dSandi                $fn .= '.gz';
270d8186216SBen Coburn            }else if(@file_exists($fn . '.bz2')){
271ff3ed99fSmarcel                $fn .= '.bz2';
272ff3ed99fSmarcel            }else{
273ff3ed99fSmarcel                //file doesnt exist yet, so we take the configured extension
274ff3ed99fSmarcel                $fn .= '.' . $conf['compression'];
275ff3ed99fSmarcel            }
276b625487dSandi        }
277b625487dSandi    }
2786e0cc83aSchris
27950602150SBen Coburn    if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2806e0cc83aSchris    $cache[$raw_id][$rev] = $fn;
281b625487dSandi    return $fn;
282b625487dSandi}
283b625487dSandi
284b625487dSandi/**
285c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
286c9b4bd1eSBen Coburn *
287c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
288c9b4bd1eSBen Coburn */
289c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
290c9b4bd1eSBen Coburn    global $conf;
291662ff478SAndreas Gohr    return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
292c9b4bd1eSBen Coburn}
293c9b4bd1eSBen Coburn
294c9b4bd1eSBen Coburn
295c9b4bd1eSBen Coburn/**
2961380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
297b158d625SSteven Danz *
298b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
299b158d625SSteven Danz *
300b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
301b158d625SSteven Danz */
3021380fc45SAndreas Gohrfunction metaFN($id,$ext){
303b158d625SSteven Danz    global $conf;
304b158d625SSteven Danz    $id = cleanID($id);
305b158d625SSteven Danz    $id = str_replace(':','/',$id);
3061380fc45SAndreas Gohr    $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
307b158d625SSteven Danz    return $fn;
308b158d625SSteven Danz}
309b158d625SSteven Danz
310b158d625SSteven Danz/**
311e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
312e1f3d9e1SEsther Brunner *
313e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
314*ba0267b3SMichael Hamann * @author Michael Hamann <michael@content-space.de>
315e1f3d9e1SEsther Brunner */
316e1f3d9e1SEsther Brunnerfunction metaFiles($id){
317*ba0267b3SMichael Hamann    $basename = metaFN($id, '');
318*ba0267b3SMichael Hamann    $files    = glob($basename.'.*', GLOB_MARK);
319*ba0267b3SMichael Hamann    // filter files like foo.bar.meta when $id == 'foo'
320*ba0267b3SMichael Hamann    return    $files ? preg_grep('/^'.preg_quote($basename, '/').'\.[^.\/]*$/u', $files) : array();
321e1f3d9e1SEsther Brunner}
322e1f3d9e1SEsther Brunner
323e1f3d9e1SEsther Brunner/**
324b625487dSandi * returns the full path to the mediafile specified by ID
325b625487dSandi *
326b625487dSandi * The filename is URL encoded to protect Unicode chars
327b625487dSandi *
328b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
329b625487dSandi */
330b625487dSandifunction mediaFN($id){
331b625487dSandi    global $conf;
332b625487dSandi    $id = cleanID($id);
333b625487dSandi    $id = str_replace(':','/',$id);
334b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
335b625487dSandi    return $fn;
336b625487dSandi}
337b625487dSandi
338b625487dSandi/**
339b625487dSandi * Returns the full filepath to a localized textfile if local
340b625487dSandi * version isn't found the english one is returned
341b625487dSandi *
342b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
343b625487dSandi */
344b625487dSandifunction localeFN($id){
345b625487dSandi    global $conf;
346e6cecb08SMichael Hamann    $file = DOKU_CONF.'/lang/'.$conf['lang'].'/'.$id.'.txt';
347e6cecb08SMichael Hamann    if(!@file_exists($file)){
348bc3b6aecSandi        $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
349b625487dSandi        if(!@file_exists($file)){
350b625487dSandi            //fall back to english
351bc3b6aecSandi            $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
352b625487dSandi        }
353e6cecb08SMichael Hamann    }
354b625487dSandi    return $file;
355b625487dSandi}
356b625487dSandi
357b625487dSandi/**
358c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
359c4e0e4a1SAndreas Gohr *
360c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
361c4e0e4a1SAndreas Gohr * instead
362c4e0e4a1SAndreas Gohr *
363c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
364c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
365c4e0e4a1SAndreas Gohr *
366c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
367c4e0e4a1SAndreas Gohr */
368a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
369c662a49aSAndreas Gohr    global $conf;
370c662a49aSAndreas Gohr
371c662a49aSAndreas Gohr    // some pre cleaning for useslash:
372c662a49aSAndreas Gohr    if($conf['useslash']) $id = str_replace('/',':',$id);
373c662a49aSAndreas Gohr
374c4e0e4a1SAndreas Gohr    // if the id starts with a dot we need to handle the
375c4e0e4a1SAndreas Gohr    // relative stuff
376c4e0e4a1SAndreas Gohr    if($id{0} == '.'){
377c4e0e4a1SAndreas Gohr        // normalize initial dots without a colon
378c4e0e4a1SAndreas Gohr        $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
379c4e0e4a1SAndreas Gohr        // prepend the current namespace
380c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
381c4e0e4a1SAndreas Gohr
382c4e0e4a1SAndreas Gohr        // cleanup relatives
383c4e0e4a1SAndreas Gohr        $result = array();
384c4e0e4a1SAndreas Gohr        $pathA  = explode(':', $id);
385c4e0e4a1SAndreas Gohr        if (!$pathA[0]) $result[] = '';
386c4e0e4a1SAndreas Gohr        foreach ($pathA AS $key => $dir) {
387c4e0e4a1SAndreas Gohr            if ($dir == '..') {
388c4e0e4a1SAndreas Gohr                if (end($result) == '..') {
389c4e0e4a1SAndreas Gohr                    $result[] = '..';
390c4e0e4a1SAndreas Gohr                } elseif (!array_pop($result)) {
391c4e0e4a1SAndreas Gohr                    $result[] = '..';
392c4e0e4a1SAndreas Gohr                }
393c4e0e4a1SAndreas Gohr            } elseif ($dir && $dir != '.') {
394c4e0e4a1SAndreas Gohr                $result[] = $dir;
395c4e0e4a1SAndreas Gohr            }
396c4e0e4a1SAndreas Gohr        }
397c4e0e4a1SAndreas Gohr        if (!end($pathA)) $result[] = '';
398c4e0e4a1SAndreas Gohr        $id = implode(':', $result);
399c4e0e4a1SAndreas Gohr    }elseif($ns !== false && strpos($id,':') === false){
400c4e0e4a1SAndreas Gohr        //if link contains no namespace. add current namespace (if any)
401c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
402c4e0e4a1SAndreas Gohr    }
403c4e0e4a1SAndreas Gohr
404a6ef4796SAndreas Gohr    if($clean) $id = cleanID($id);
405a6ef4796SAndreas Gohr    return $id;
406c4e0e4a1SAndreas Gohr}
407c4e0e4a1SAndreas Gohr
408c4e0e4a1SAndreas Gohr/**
409b625487dSandi * Returns a full media id
410b625487dSandi *
411b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
412b625487dSandi */
41337e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
414c4e0e4a1SAndreas Gohr    $page   = resolve_id($ns,$page);
415b625487dSandi    $file   = mediaFN($page);
416b625487dSandi    $exists = @file_exists($file);
417b625487dSandi}
418b625487dSandi
419b625487dSandi/**
420b625487dSandi * Returns a full page id
421b625487dSandi *
422b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
423b625487dSandi */
42437e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
425b625487dSandi    global $conf;
4260b7c14c2Sandi    $exists = false;
427b625487dSandi
428b625487dSandi    //keep hashlink if exists then clean both parts
42903c4aec3Schris    if (strpos($page,'#')) {
4304b7f9e70STom N Harris        list($page,$hash) = explode('#',$page,2);
43103c4aec3Schris    } else {
43203c4aec3Schris        $hash = '';
43303c4aec3Schris    }
434b625487dSandi    $hash = cleanID($hash);
435a6ef4796SAndreas Gohr    $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
436b625487dSandi
437a6ef4796SAndreas Gohr    // get filename (calls clean itself)
438b625487dSandi    $file = wikiFN($page);
439b625487dSandi
4401179df0eSGuy Brand    // if ends with colon or slash we have a namespace link
441b26cdbbeSAdrian Lang    if(in_array(substr($page,-1), array(':', ';')) ||
442b26cdbbeSAdrian Lang       ($conf['useslash'] && substr($page,-1) == '/')){
443103c256aSChris Smith        if(page_exists($page.$conf['start'])){
444a6ef4796SAndreas Gohr            // start page inside namespace
445a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
446a6ef4796SAndreas Gohr            $exists = true;
447103c256aSChris Smith        }elseif(page_exists($page.noNS(cleanID($page)))){
448a6ef4796SAndreas Gohr            // page named like the NS inside the NS
449a6ef4796SAndreas Gohr            $page = $page.noNS(cleanID($page));
450a6ef4796SAndreas Gohr            $exists = true;
451103c256aSChris Smith        }elseif(page_exists($page)){
452a6ef4796SAndreas Gohr            // page like namespace exists
453a6ef4796SAndreas Gohr            $page = $page;
454a6ef4796SAndreas Gohr            $exists = true;
455a6ef4796SAndreas Gohr        }else{
456a6ef4796SAndreas Gohr            // fall back to default
457a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
458a6ef4796SAndreas Gohr        }
459a6ef4796SAndreas Gohr    }else{
460b625487dSandi        //check alternative plural/nonplural form
461b625487dSandi        if(!@file_exists($file)){
462b625487dSandi            if( $conf['autoplural'] ){
463b625487dSandi                if(substr($page,-1) == 's'){
464b625487dSandi                    $try = substr($page,0,-1);
465b625487dSandi                }else{
466b625487dSandi                    $try = $page.'s';
467b625487dSandi                }
468103c256aSChris Smith                if(page_exists($try)){
469b625487dSandi                    $page   = $try;
470b625487dSandi                    $exists = true;
471b625487dSandi                }
472b625487dSandi            }
473b625487dSandi        }else{
474b625487dSandi            $exists = true;
475b625487dSandi        }
476a6ef4796SAndreas Gohr    }
477a6ef4796SAndreas Gohr
478a6ef4796SAndreas Gohr    // now make sure we have a clean page
479a6ef4796SAndreas Gohr    $page = cleanID($page);
480b625487dSandi
481b625487dSandi    //add hash if any
482b2d7d3f2Sandi    if(!empty($hash)) $page .= '#'.$hash;
483b625487dSandi}
484b625487dSandi
48598407a7aSandi/**
48698407a7aSandi * Returns the name of a cachefile from given data
48798407a7aSandi *
48898407a7aSandi * The needed directory is created by this function!
48998407a7aSandi *
49098407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
49198407a7aSandi *
49298407a7aSandi * @param string $data  This data is used to create a unique md5 name
49398407a7aSandi * @param string $ext   This is appended to the filename if given
49498407a7aSandi * @return string       The filename of the cachefile
49598407a7aSandi */
49698407a7aSandifunction getCacheName($data,$ext=''){
49798407a7aSandi    global $conf;
49898407a7aSandi    $md5  = md5($data);
49998407a7aSandi    $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
50098407a7aSandi    io_makeFileDir($file);
50198407a7aSandi    return $file;
50298407a7aSandi}
50398407a7aSandi
5040dc92c6fSAndreas Gohr/**
5050dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
5060dc92c6fSAndreas Gohr *
5070dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5080dc92c6fSAndreas Gohr */
5090dc92c6fSAndreas Gohrfunction isHiddenPage($id){
5100dc92c6fSAndreas Gohr    global $conf;
511e6a873d7SMichael Klier    global $ACT;
5120dc92c6fSAndreas Gohr    if(empty($conf['hidepages'])) return false;
513e6a873d7SMichael Klier    if($ACT == 'admin') return false;
5140dc92c6fSAndreas Gohr
5150dc92c6fSAndreas Gohr    if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
5160dc92c6fSAndreas Gohr        return true;
5170dc92c6fSAndreas Gohr    }
5180dc92c6fSAndreas Gohr    return false;
5190dc92c6fSAndreas Gohr}
5200dc92c6fSAndreas Gohr
5210dc92c6fSAndreas Gohr/**
5220dc92c6fSAndreas Gohr * Reverse of isHiddenPage
5230dc92c6fSAndreas Gohr *
5240dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5250dc92c6fSAndreas Gohr */
5260dc92c6fSAndreas Gohrfunction isVisiblePage($id){
5270dc92c6fSAndreas Gohr    return !isHiddenPage($id);
5280dc92c6fSAndreas Gohr}
5290dc92c6fSAndreas Gohr
5305b75cd1fSAdrian Lang/**
5315b75cd1fSAdrian Lang * Format an id for output to a user
5325b75cd1fSAdrian Lang *
5335b75cd1fSAdrian Lang * Namespaces are denoted by a trailing “:*”. The root namespace is
5345b75cd1fSAdrian Lang * “*”. Output is escaped.
5355b75cd1fSAdrian Lang *
5365b75cd1fSAdrian Lang * @author Adrian Lang <lang@cosmocode.de>
5375b75cd1fSAdrian Lang */
5380ac9a84dSoliver
5395b75cd1fSAdrian Langfunction prettyprint_id($id) {
5405b75cd1fSAdrian Lang    if (!$id || $id === ':') {
5415b75cd1fSAdrian Lang        return '*';
5425b75cd1fSAdrian Lang    }
5435b75cd1fSAdrian Lang    if ((substr($id, -1, 1) === ':')) {
5445b75cd1fSAdrian Lang        $id .= '*';
5455b75cd1fSAdrian Lang    }
5465b75cd1fSAdrian Lang    return hsc($id);
5475b75cd1fSAdrian Lang}
548f03fd957SAndreas Gohr
549f03fd957SAndreas Gohr/**
550f03fd957SAndreas Gohr * Encode a UTF-8 filename to use on any filesystem
551f03fd957SAndreas Gohr *
552f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
553f03fd957SAndreas Gohr *
554f03fd957SAndreas Gohr * When the second parameter is true the string will
555f03fd957SAndreas Gohr * be encoded only if non ASCII characters are detected -
556f03fd957SAndreas Gohr * This makes it safe to run it multiple times on the
557f03fd957SAndreas Gohr * same string (default is true)
558f03fd957SAndreas Gohr *
559f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
560f03fd957SAndreas Gohr * @see    urlencode
561f03fd957SAndreas Gohr */
562f03fd957SAndreas Gohrfunction utf8_encodeFN($file,$safe=true){
563f03fd957SAndreas Gohr    global $conf;
564f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
565f03fd957SAndreas Gohr
566f03fd957SAndreas Gohr    if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
567f03fd957SAndreas Gohr        return $file;
568f03fd957SAndreas Gohr    }
569f03fd957SAndreas Gohr
570f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
571f03fd957SAndreas Gohr        return SafeFN::encode($file);
572f03fd957SAndreas Gohr    }
573f03fd957SAndreas Gohr
574f03fd957SAndreas Gohr    $file = urlencode($file);
575f03fd957SAndreas Gohr    $file = str_replace('%2F','/',$file);
576f03fd957SAndreas Gohr    return $file;
577f03fd957SAndreas Gohr}
578f03fd957SAndreas Gohr
579f03fd957SAndreas Gohr/**
580f03fd957SAndreas Gohr * Decode a filename back to UTF-8
581f03fd957SAndreas Gohr *
582f03fd957SAndreas Gohr * Uses the 'fnencode' option to determine encoding
583f03fd957SAndreas Gohr *
584f03fd957SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
585f03fd957SAndreas Gohr * @see    urldecode
586f03fd957SAndreas Gohr */
587f03fd957SAndreas Gohrfunction utf8_decodeFN($file){
588f03fd957SAndreas Gohr    global $conf;
589f03fd957SAndreas Gohr    if($conf['fnencode'] == 'utf-8') return $file;
590f03fd957SAndreas Gohr
591f03fd957SAndreas Gohr    if($conf['fnencode'] == 'safe'){
592f03fd957SAndreas Gohr        return SafeFN::decode($file);
593f03fd957SAndreas Gohr    }
594f03fd957SAndreas Gohr
595f03fd957SAndreas Gohr    return urldecode($file);
596f03fd957SAndreas Gohr}
597f03fd957SAndreas Gohr
598