xref: /dokuwiki/inc/pageutils.php (revision de9114ea81e4c8b66c704b873a8664d8e8120e9c)
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
267d71d4b7SAndreas Gohr    $request = $_SERVER['REQUEST_URI'];
277d71d4b7SAndreas Gohr
286c7843b5Sandi    //construct page id from request URI
296c7843b5Sandi    if(empty($id) && $conf['userewrite'] == 2){
306c7843b5Sandi        //get the script URL
316c7843b5Sandi        if($conf['basedir']){
3281124000Sjan            $relpath = '';
3381124000Sjan            if($param != 'id') {
3481124000Sjan                $relpath = 'lib/exe/';
3581124000Sjan            }
3681124000Sjan            $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
377d71d4b7SAndreas Gohr
387d71d4b7SAndreas Gohr        }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['PATH_TRANSLATED']){
397d71d4b7SAndreas Gohr            $request = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
407d71d4b7SAndreas Gohr                    $_SERVER['PATH_TRANSLATED']);
416c7843b5Sandi        }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
426c7843b5Sandi            $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
436c7843b5Sandi                    $_SERVER['SCRIPT_FILENAME']);
446c7843b5Sandi            $script = '/'.$script;
456c7843b5Sandi        }else{
466c7843b5Sandi            $script = $_SERVER['SCRIPT_NAME'];
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
63671a58a6SGuy Brand    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;
99b625487dSandi    global $lang;
1004b5db43bSjoe.lapp    static $sepcharpat = null;
1014b5db43bSjoe.lapp
102dc2c0e04Schris    global $cache_cleanid;
103dc2c0e04Schris    $cache = & $cache_cleanid;
1046e0cc83aSchris
1056e0cc83aSchris    // check if it's already in the memory cache
1063a50618cSgweissbach    if (isset($cache[(string)$raw_id])) {
1073a50618cSgweissbach        return $cache[(string)$raw_id];
1086e0cc83aSchris    }
1096e0cc83aSchris
1104b5db43bSjoe.lapp    $sepchar = $conf['sepchar'];
1114b5db43bSjoe.lapp    if($sepcharpat == null) // build string only once to save clock cycles
1124b5db43bSjoe.lapp        $sepcharpat = '#\\'.$sepchar.'+#';
1134b5db43bSjoe.lapp
1143a50618cSgweissbach    $id = trim((string)$raw_id);
115b625487dSandi    $id = utf8_strtolower($id);
116b625487dSandi
117b625487dSandi    //alternative namespace seperator
118b625487dSandi    $id = strtr($id,';',':');
119b625487dSandi    if($conf['useslash']){
120b625487dSandi        $id = strtr($id,'/',':');
121b625487dSandi    }else{
1224eeffcd2SAndreas Gohr        $id = strtr($id,'/',$sepchar);
123b625487dSandi    }
124b625487dSandi
1258a831f2bSAndreas Gohr    if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1268a831f2bSAndreas Gohr    if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
127b625487dSandi
128b625487dSandi    //remove specials
129ad81d431SAndreas Gohr    $id = utf8_stripspecials($id,$sepchar,'\*');
130b625487dSandi
1318a831f2bSAndreas Gohr    if($ascii) $id = utf8_strip($id);
1328a831f2bSAndreas Gohr
133b625487dSandi    //clean up
1344b5db43bSjoe.lapp    $id = preg_replace($sepcharpat,$sepchar,$id);
135b625487dSandi    $id = preg_replace('#:+#',':',$id);
13663b0c1a7SGina Haeussge    $id = ($media ? trim($id,':.-') : trim($id,':._-'));
137b625487dSandi    $id = preg_replace('#:[:\._\-]+#',':',$id);
138b625487dSandi
1393a50618cSgweissbach    $cache[(string)$raw_id] = $id;
140b625487dSandi    return($id);
141b625487dSandi}
142b625487dSandi
143b625487dSandi/**
144b625487dSandi * Return namespacepart of a wiki ID
145b625487dSandi *
146b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
147b625487dSandi */
148b625487dSandifunction getNS($id){
1493a50618cSgweissbach    $pos = strrpos((string)$id,':');
150c4e0e4a1SAndreas Gohr    if($pos!==false){
1513a50618cSgweissbach        return substr((string)$id,0,$pos);
152b625487dSandi    }
153b625487dSandi    return false;
154b625487dSandi}
155b625487dSandi
156b625487dSandi/**
157b625487dSandi * Returns the ID without the namespace
158b625487dSandi *
159b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
160b625487dSandi */
161b625487dSandifunction noNS($id) {
1622844584fSBen Coburn    $pos = strrpos($id, ':');
1632844584fSBen Coburn    if ($pos!==false) {
1642844584fSBen Coburn        return substr($id, $pos+1);
1652844584fSBen Coburn    } else {
1662844584fSBen Coburn        return $id;
1672844584fSBen Coburn    }
1681a84a0f3SAnika Henke}
1691a84a0f3SAnika Henke
1701a84a0f3SAnika Henke/**
1711a84a0f3SAnika Henke * Returns the current namespace
1721a84a0f3SAnika Henke *
1731a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1741a84a0f3SAnika Henke */
1751a84a0f3SAnika Henkefunction curNS($id) {
1761a84a0f3SAnika Henke    return noNS(getNS($id));
1771a84a0f3SAnika Henke}
1781a84a0f3SAnika Henke
1791a84a0f3SAnika Henke/**
1801a84a0f3SAnika Henke * Returns the ID without the namespace or current namespace for 'start' pages
1811a84a0f3SAnika Henke *
1821a84a0f3SAnika Henke * @author Nathan Fritz <fritzn@crown.edu>
1831a84a0f3SAnika Henke */
1841a84a0f3SAnika Henkefunction noNSorNS($id) {
1851a84a0f3SAnika Henke    global $conf;
1861a84a0f3SAnika Henke
1871a84a0f3SAnika Henke    $p = noNS($id);
1881a84a0f3SAnika Henke    if ($p == $conf['start']) {
1891a84a0f3SAnika Henke        $p = curNS($id);
1901a84a0f3SAnika Henke        if ($p == false) {
1911a84a0f3SAnika Henke            return noNS($id);
1921a84a0f3SAnika Henke        }
1931a84a0f3SAnika Henke    }
1941a84a0f3SAnika Henke    return $p;
195b625487dSandi}
1964ceab83fSAndreas Gohr
1974ceab83fSAndreas Gohr/**
1984ceab83fSAndreas Gohr * Creates a XHTML valid linkid from a given headline title
1994ceab83fSAndreas Gohr *
2004ceab83fSAndreas Gohr * @param string  $title   The headline title
2014ceab83fSAndreas Gohr * @param array   $check   List of existing IDs
2024ceab83fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
2034ceab83fSAndreas Gohr */
204443d207bSAndreas Gohrfunction sectionID($title,&$check) {
205*de9114eaSAnika Henke    $title = str_replace(array(':','.'),'',cleanID($title));
206*de9114eaSAnika Henke    $new = ltrim($title,'0123456789_-');
2074ceab83fSAndreas Gohr    if(empty($new)){
2084ceab83fSAndreas Gohr        $title = 'section'.preg_replace('/[^0-9]+/','',$title); //keep numbers from headline
2094ceab83fSAndreas Gohr    }else{
2104ceab83fSAndreas Gohr        $title = $new;
2114ceab83fSAndreas Gohr    }
2124ceab83fSAndreas Gohr
213443d207bSAndreas Gohr    if(is_array($check)){
2144ceab83fSAndreas Gohr        // make sure tiles are unique
2154ceab83fSAndreas Gohr        $num = '';
2164ceab83fSAndreas Gohr        while(in_array($title.$num,$check)){
2174ceab83fSAndreas Gohr            ($num) ? $num++ : $num = 1;
2184ceab83fSAndreas Gohr        }
2194ceab83fSAndreas Gohr        $title = $title.$num;
2204ceab83fSAndreas Gohr        $check[] = $title;
2214ceab83fSAndreas Gohr    }
2224ceab83fSAndreas Gohr
2234ceab83fSAndreas Gohr    return $title;
2244ceab83fSAndreas Gohr}
2254ceab83fSAndreas Gohr
226b625487dSandi
227b625487dSandi/**
228103c256aSChris Smith * Wiki page existence check
229103c256aSChris Smith *
230103c256aSChris Smith * parameters as for wikiFN
231103c256aSChris Smith *
232103c256aSChris Smith * @author Chris Smith <chris@jalakai.co.uk>
233103c256aSChris Smith */
234103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
235103c256aSChris Smith    return @file_exists(wikiFN($id,$rev,$clean));
236103c256aSChris Smith}
237103c256aSChris Smith
238103c256aSChris Smith/**
239103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
240b625487dSandi *
241b625487dSandi * The filename is URL encoded to protect Unicode chars
242b625487dSandi *
243103c256aSChris Smith * @param  $raw_id  string   id of wikipage
244103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
245103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
246103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
247103c256aSChris Smith *
248b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
249b625487dSandi */
2506e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
251b625487dSandi    global $conf;
2526e0cc83aSchris
253dc2c0e04Schris    global $cache_wikifn;
254dc2c0e04Schris    $cache = & $cache_wikifn;
255dc2c0e04Schris
2566e0cc83aSchris    if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2576e0cc83aSchris        return $cache[$raw_id][$rev];
2586e0cc83aSchris    }
2596e0cc83aSchris
2606e0cc83aSchris    $id = $raw_id;
2616e0cc83aSchris
2620d8ea614Schris    if ($clean) $id = cleanID($id);
263b625487dSandi    $id = str_replace(':','/',$id);
264b625487dSandi    if(empty($rev)){
265b625487dSandi        $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
266b625487dSandi    }else{
267b625487dSandi        $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
268ff3ed99fSmarcel        if($conf['compression']){
269ff3ed99fSmarcel            //test for extensions here, we want to read both compressions
270d8186216SBen Coburn            if (@file_exists($fn . '.gz')){
271b625487dSandi                $fn .= '.gz';
272d8186216SBen Coburn            }else if(@file_exists($fn . '.bz2')){
273ff3ed99fSmarcel                $fn .= '.bz2';
274ff3ed99fSmarcel            }else{
275ff3ed99fSmarcel                //file doesnt exist yet, so we take the configured extension
276ff3ed99fSmarcel                $fn .= '.' . $conf['compression'];
277ff3ed99fSmarcel            }
278b625487dSandi        }
279b625487dSandi    }
2806e0cc83aSchris
28150602150SBen Coburn    if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2826e0cc83aSchris    $cache[$raw_id][$rev] = $fn;
283b625487dSandi    return $fn;
284b625487dSandi}
285b625487dSandi
286b625487dSandi/**
287c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
288c9b4bd1eSBen Coburn *
289c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
290c9b4bd1eSBen Coburn */
291c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
292c9b4bd1eSBen Coburn    global $conf;
293662ff478SAndreas Gohr    return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
294c9b4bd1eSBen Coburn}
295c9b4bd1eSBen Coburn
296c9b4bd1eSBen Coburn
297c9b4bd1eSBen Coburn/**
2981380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
299b158d625SSteven Danz *
300b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
301b158d625SSteven Danz *
302b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
303b158d625SSteven Danz */
3041380fc45SAndreas Gohrfunction metaFN($id,$ext){
305b158d625SSteven Danz    global $conf;
306b158d625SSteven Danz    $id = cleanID($id);
307b158d625SSteven Danz    $id = str_replace(':','/',$id);
3081380fc45SAndreas Gohr    $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
309b158d625SSteven Danz    return $fn;
310b158d625SSteven Danz}
311b158d625SSteven Danz
312b158d625SSteven Danz/**
313e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
314e1f3d9e1SEsther Brunner *
315e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
316e1f3d9e1SEsther Brunner */
317e1f3d9e1SEsther Brunnerfunction metaFiles($id){
318e1f3d9e1SEsther Brunner    $name   = noNS($id);
3196a5b38cdSMichael Klier    $ns     = getNS($id);
3206a5b38cdSMichael Klier    $dir    = ($ns) ? metaFN($ns,'').'/' : metaFN($ns,'');
321e1f3d9e1SEsther Brunner    $files  = array();
322e1f3d9e1SEsther Brunner
323e1f3d9e1SEsther Brunner    $dh = @opendir($dir);
3245011da9dSEsther Brunner    if(!$dh) return $files;
325e1f3d9e1SEsther Brunner    while(($file = readdir($dh)) !== false){
3261a54dfabSEsther Brunner        if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
327e1f3d9e1SEsther Brunner            $files[] = $dir.$file;
328e1f3d9e1SEsther Brunner    }
329e1f3d9e1SEsther Brunner    closedir($dh);
330e1f3d9e1SEsther Brunner
331e1f3d9e1SEsther Brunner    return $files;
332e1f3d9e1SEsther Brunner}
333e1f3d9e1SEsther Brunner
334e1f3d9e1SEsther Brunner/**
335b625487dSandi * returns the full path to the mediafile specified by ID
336b625487dSandi *
337b625487dSandi * The filename is URL encoded to protect Unicode chars
338b625487dSandi *
339b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
340b625487dSandi */
341b625487dSandifunction mediaFN($id){
342b625487dSandi    global $conf;
343b625487dSandi    $id = cleanID($id);
344b625487dSandi    $id = str_replace(':','/',$id);
345b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
346b625487dSandi    return $fn;
347b625487dSandi}
348b625487dSandi
349b625487dSandi/**
350b625487dSandi * Returns the full filepath to a localized textfile if local
351b625487dSandi * version isn't found the english one is returned
352b625487dSandi *
353b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
354b625487dSandi */
355b625487dSandifunction localeFN($id){
356b625487dSandi    global $conf;
357bc3b6aecSandi    $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
358b625487dSandi    if(!@file_exists($file)){
359b625487dSandi        //fall back to english
360bc3b6aecSandi        $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
361b625487dSandi    }
362b625487dSandi    return $file;
363b625487dSandi}
364b625487dSandi
365b625487dSandi/**
366c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
367c4e0e4a1SAndreas Gohr *
368c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
369c4e0e4a1SAndreas Gohr * instead
370c4e0e4a1SAndreas Gohr *
371c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
372c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
373c4e0e4a1SAndreas Gohr *
374c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
375c4e0e4a1SAndreas Gohr */
376a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
377c662a49aSAndreas Gohr    global $conf;
378c662a49aSAndreas Gohr
379c662a49aSAndreas Gohr    // some pre cleaning for useslash:
380c662a49aSAndreas Gohr    if($conf['useslash']) $id = str_replace('/',':',$id);
381c662a49aSAndreas Gohr
382c4e0e4a1SAndreas Gohr    // if the id starts with a dot we need to handle the
383c4e0e4a1SAndreas Gohr    // relative stuff
384c4e0e4a1SAndreas Gohr    if($id{0} == '.'){
385c4e0e4a1SAndreas Gohr        // normalize initial dots without a colon
386c4e0e4a1SAndreas Gohr        $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
387c4e0e4a1SAndreas Gohr        // prepend the current namespace
388c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
389c4e0e4a1SAndreas Gohr
390c4e0e4a1SAndreas Gohr        // cleanup relatives
391c4e0e4a1SAndreas Gohr        $result = array();
392c4e0e4a1SAndreas Gohr        $pathA  = explode(':', $id);
393c4e0e4a1SAndreas Gohr        if (!$pathA[0]) $result[] = '';
394c4e0e4a1SAndreas Gohr        foreach ($pathA AS $key => $dir) {
395c4e0e4a1SAndreas Gohr            if ($dir == '..') {
396c4e0e4a1SAndreas Gohr                if (end($result) == '..') {
397c4e0e4a1SAndreas Gohr                    $result[] = '..';
398c4e0e4a1SAndreas Gohr                } elseif (!array_pop($result)) {
399c4e0e4a1SAndreas Gohr                    $result[] = '..';
400c4e0e4a1SAndreas Gohr                }
401c4e0e4a1SAndreas Gohr            } elseif ($dir && $dir != '.') {
402c4e0e4a1SAndreas Gohr                $result[] = $dir;
403c4e0e4a1SAndreas Gohr            }
404c4e0e4a1SAndreas Gohr        }
405c4e0e4a1SAndreas Gohr        if (!end($pathA)) $result[] = '';
406c4e0e4a1SAndreas Gohr        $id = implode(':', $result);
407c4e0e4a1SAndreas Gohr    }elseif($ns !== false && strpos($id,':') === false){
408c4e0e4a1SAndreas Gohr        //if link contains no namespace. add current namespace (if any)
409c4e0e4a1SAndreas Gohr        $id = $ns.':'.$id;
410c4e0e4a1SAndreas Gohr    }
411c4e0e4a1SAndreas Gohr
412a6ef4796SAndreas Gohr    if($clean) $id = cleanID($id);
413a6ef4796SAndreas Gohr    return $id;
414c4e0e4a1SAndreas Gohr}
415c4e0e4a1SAndreas Gohr
416c4e0e4a1SAndreas Gohr/**
417b625487dSandi * Returns a full media id
418b625487dSandi *
419b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
420b625487dSandi */
42137e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
422c4e0e4a1SAndreas Gohr    $page   = resolve_id($ns,$page);
423b625487dSandi    $file   = mediaFN($page);
424b625487dSandi    $exists = @file_exists($file);
425b625487dSandi}
426b625487dSandi
427b625487dSandi/**
428b625487dSandi * Returns a full page id
429b625487dSandi *
430b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
431b625487dSandi */
43237e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
433b625487dSandi    global $conf;
4340b7c14c2Sandi    $exists = false;
435b625487dSandi
436b625487dSandi    //keep hashlink if exists then clean both parts
43703c4aec3Schris    if (strpos($page,'#')) {
4384b7f9e70STom N Harris        list($page,$hash) = explode('#',$page,2);
43903c4aec3Schris    } else {
44003c4aec3Schris        $hash = '';
44103c4aec3Schris    }
442b625487dSandi    $hash = cleanID($hash);
443a6ef4796SAndreas Gohr    $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
444b625487dSandi
445a6ef4796SAndreas Gohr    // get filename (calls clean itself)
446b625487dSandi    $file = wikiFN($page);
447b625487dSandi
4481179df0eSGuy Brand    // if ends with colon or slash we have a namespace link
4491179df0eSGuy Brand    if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){
450103c256aSChris Smith        if(page_exists($page.$conf['start'])){
451a6ef4796SAndreas Gohr            // start page inside namespace
452a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
453a6ef4796SAndreas Gohr            $exists = true;
454103c256aSChris Smith        }elseif(page_exists($page.noNS(cleanID($page)))){
455a6ef4796SAndreas Gohr            // page named like the NS inside the NS
456a6ef4796SAndreas Gohr            $page = $page.noNS(cleanID($page));
457a6ef4796SAndreas Gohr            $exists = true;
458103c256aSChris Smith        }elseif(page_exists($page)){
459a6ef4796SAndreas Gohr            // page like namespace exists
460a6ef4796SAndreas Gohr            $page = $page;
461a6ef4796SAndreas Gohr            $exists = true;
462a6ef4796SAndreas Gohr        }else{
463a6ef4796SAndreas Gohr            // fall back to default
464a6ef4796SAndreas Gohr            $page = $page.$conf['start'];
465a6ef4796SAndreas Gohr        }
466a6ef4796SAndreas Gohr    }else{
467b625487dSandi        //check alternative plural/nonplural form
468b625487dSandi        if(!@file_exists($file)){
469b625487dSandi            if( $conf['autoplural'] ){
470b625487dSandi                if(substr($page,-1) == 's'){
471b625487dSandi                    $try = substr($page,0,-1);
472b625487dSandi                }else{
473b625487dSandi                    $try = $page.'s';
474b625487dSandi                }
475103c256aSChris Smith                if(page_exists($try)){
476b625487dSandi                    $page   = $try;
477b625487dSandi                    $exists = true;
478b625487dSandi                }
479b625487dSandi            }
480b625487dSandi        }else{
481b625487dSandi            $exists = true;
482b625487dSandi        }
483a6ef4796SAndreas Gohr    }
484a6ef4796SAndreas Gohr
485a6ef4796SAndreas Gohr    // now make sure we have a clean page
486a6ef4796SAndreas Gohr    $page = cleanID($page);
487b625487dSandi
488b625487dSandi    //add hash if any
489b2d7d3f2Sandi    if(!empty($hash)) $page .= '#'.$hash;
490b625487dSandi}
491b625487dSandi
49298407a7aSandi/**
49398407a7aSandi * Returns the name of a cachefile from given data
49498407a7aSandi *
49598407a7aSandi * The needed directory is created by this function!
49698407a7aSandi *
49798407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
49898407a7aSandi *
49998407a7aSandi * @param string $data  This data is used to create a unique md5 name
50098407a7aSandi * @param string $ext   This is appended to the filename if given
50198407a7aSandi * @return string       The filename of the cachefile
50298407a7aSandi */
50398407a7aSandifunction getCacheName($data,$ext=''){
50498407a7aSandi    global $conf;
50598407a7aSandi    $md5  = md5($data);
50698407a7aSandi    $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
50798407a7aSandi    io_makeFileDir($file);
50898407a7aSandi    return $file;
50998407a7aSandi}
51098407a7aSandi
5110dc92c6fSAndreas Gohr/**
5120dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
5130dc92c6fSAndreas Gohr *
5140dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5150dc92c6fSAndreas Gohr */
5160dc92c6fSAndreas Gohrfunction isHiddenPage($id){
5170dc92c6fSAndreas Gohr    global $conf;
518e6a873d7SMichael Klier    global $ACT;
5190dc92c6fSAndreas Gohr    if(empty($conf['hidepages'])) return false;
520e6a873d7SMichael Klier    if($ACT == 'admin') return false;
5210dc92c6fSAndreas Gohr
5220dc92c6fSAndreas Gohr    if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
5230dc92c6fSAndreas Gohr        return true;
5240dc92c6fSAndreas Gohr    }
5250dc92c6fSAndreas Gohr    return false;
5260dc92c6fSAndreas Gohr}
5270dc92c6fSAndreas Gohr
5280dc92c6fSAndreas Gohr/**
5290dc92c6fSAndreas Gohr * Reverse of isHiddenPage
5300dc92c6fSAndreas Gohr *
5310dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
5320dc92c6fSAndreas Gohr */
5330dc92c6fSAndreas Gohrfunction isVisiblePage($id){
5340dc92c6fSAndreas Gohr    return !isHiddenPage($id);
5350dc92c6fSAndreas Gohr}
5360dc92c6fSAndreas Gohr
5370ac9a84dSoliver
538