xref: /dokuwiki/inc/pageutils.php (revision 63b0c1a70fbc1d51a775e2fa30b6a520bac2a6e4)
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){
286c7843b5Sandi    //get the script URL
296c7843b5Sandi    if($conf['basedir']){
3081124000Sjan      $relpath = '';
3181124000Sjan      if($param != 'id') {
3281124000Sjan        $relpath = 'lib/exe/';
3381124000Sjan      }
3481124000Sjan      $script = $conf['basedir'].$relpath.basename($_SERVER['SCRIPT_FILENAME']);
356c7843b5Sandi    }elseif($_SERVER['DOCUMENT_ROOT'] && $_SERVER['SCRIPT_FILENAME']){
366c7843b5Sandi      $script = preg_replace ('/^'.preg_quote($_SERVER['DOCUMENT_ROOT'],'/').'/','',
376c7843b5Sandi                              $_SERVER['SCRIPT_FILENAME']);
386c7843b5Sandi      $script = '/'.$script;
396c7843b5Sandi    }else{
406c7843b5Sandi      $script = $_SERVER['SCRIPT_NAME'];
416c7843b5Sandi    }
426c7843b5Sandi
4352339126Sandi    //clean script and request (fixes a windows problem)
4452339126Sandi    $script  = preg_replace('/\/\/+/','/',$script);
4552339126Sandi    $request = preg_replace('/\/\/+/','/',$_SERVER['REQUEST_URI']);
4652339126Sandi
476c7843b5Sandi    //remove script URL and Querystring to gain the id
4852339126Sandi    if(preg_match('/^'.preg_quote($script,'/').'(.*)/',$request, $match)){
496c7843b5Sandi      $id = preg_replace ('/\?.*/','',$match[1]);
506c7843b5Sandi    }
516de3759aSAndreas Gohr    $id = urldecode($id);
5242905504SAndreas Gohr    //strip leading slashes
5342905504SAndreas Gohr    $id = preg_replace('!^/+!','',$id);
546c7843b5Sandi  }
55671a58a6SGuy Brand
56671a58a6SGuy Brand  // Namespace autolinking from URL
57671a58a6SGuy Brand  if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){
58103c256aSChris Smith    if(page_exists($id.$conf['start'])){
59671a58a6SGuy Brand      // start page inside namespace
60671a58a6SGuy Brand      $id = $id.$conf['start'];
61103c256aSChris Smith    }elseif(page_exists($id.noNS(cleanID($id)))){
62671a58a6SGuy Brand      // page named like the NS inside the NS
63671a58a6SGuy Brand      $id = $id.noNS(cleanID($id));
64103c256aSChris Smith    }elseif(page_exists($id)){
65671a58a6SGuy Brand      // page like namespace exists
667a42ac9eSBen Coburn      $id = substr($id,0,-1);
67671a58a6SGuy Brand    }else{
68671a58a6SGuy Brand      // fall back to default
69671a58a6SGuy Brand      $id = $id.$conf['start'];
70671a58a6SGuy Brand    }
71671a58a6SGuy Brand    header("Location: ".wl($id,'',true));
72671a58a6SGuy Brand  }
73671a58a6SGuy Brand
7442905504SAndreas Gohr  if($clean) $id = cleanID($id);
750868021bSAndreas Gohr  if(empty($id) && $param=='id') $id = $conf['start'];
766c7843b5Sandi
776c7843b5Sandi  return $id;
786c7843b5Sandi}
79b625487dSandi
80b625487dSandi/**
81b625487dSandi * Remove unwanted chars from ID
82b625487dSandi *
83b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
84b625487dSandi * converted to unaccented ones
85b625487dSandi *
86b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
876e0cc83aSchris * @param  string  $raw_id    The pageid to clean
888a831f2bSAndreas Gohr * @param  boolean $ascii     Force ASCII
89*63b0c1a7SGina Haeussge * @param  boolean $media     Allow leading or trailing _ for media files
90b625487dSandi */
91*63b0c1a7SGina Haeussgefunction cleanID($raw_id,$ascii=false,$media=false){
92b625487dSandi  global $conf;
93b625487dSandi  global $lang;
944b5db43bSjoe.lapp  static $sepcharpat = null;
954b5db43bSjoe.lapp
96dc2c0e04Schris  global $cache_cleanid;
97dc2c0e04Schris  $cache = & $cache_cleanid;
986e0cc83aSchris
996e0cc83aSchris  // check if it's already in the memory cache
1003a50618cSgweissbach  if (isset($cache[(string)$raw_id])) {
1013a50618cSgweissbach    return $cache[(string)$raw_id];
1026e0cc83aSchris    }
1036e0cc83aSchris
1044b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
1054b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
1064b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
1074b5db43bSjoe.lapp
1083a50618cSgweissbach  $id = trim((string)$raw_id);
109b625487dSandi  $id = utf8_strtolower($id);
110b625487dSandi
111b625487dSandi  //alternative namespace seperator
112b625487dSandi  $id = strtr($id,';',':');
113b625487dSandi  if($conf['useslash']){
114b625487dSandi    $id = strtr($id,'/',':');
115b625487dSandi  }else{
1164eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
117b625487dSandi  }
118b625487dSandi
1198a831f2bSAndreas Gohr  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1208a831f2bSAndreas Gohr  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
121b625487dSandi
122b625487dSandi  //remove specials
123ad81d431SAndreas Gohr  $id = utf8_stripspecials($id,$sepchar,'\*');
124b625487dSandi
1258a831f2bSAndreas Gohr  if($ascii) $id = utf8_strip($id);
1268a831f2bSAndreas Gohr
127b625487dSandi  //clean up
1284b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
129b625487dSandi  $id = preg_replace('#:+#',':',$id);
130*63b0c1a7SGina Haeussge  $id = ($media ? trim($id,':.-') : trim($id,':._-'));
131b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
132b625487dSandi
1333a50618cSgweissbach  $cache[(string)$raw_id] = $id;
134b625487dSandi  return($id);
135b625487dSandi}
136b625487dSandi
137b625487dSandi/**
138b625487dSandi * Return namespacepart of a wiki ID
139b625487dSandi *
140b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
141b625487dSandi */
142b625487dSandifunction getNS($id){
1433a50618cSgweissbach  $pos = strrpos((string)$id,':');
144c4e0e4a1SAndreas Gohr  if($pos!==false){
1453a50618cSgweissbach    return substr((string)$id,0,$pos);
146b625487dSandi  }
147b625487dSandi  return false;
148b625487dSandi}
149b625487dSandi
150b625487dSandi/**
151b625487dSandi * Returns the ID without the namespace
152b625487dSandi *
153b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
154b625487dSandi */
155b625487dSandifunction noNS($id) {
1562844584fSBen Coburn  $pos = strrpos($id, ':');
1572844584fSBen Coburn  if ($pos!==false) {
1582844584fSBen Coburn    return substr($id, $pos+1);
1592844584fSBen Coburn  } else {
1602844584fSBen Coburn    return $id;
1612844584fSBen Coburn  }
1621a84a0f3SAnika Henke}
1631a84a0f3SAnika Henke
1641a84a0f3SAnika Henke/**
1651a84a0f3SAnika Henke* Returns the current namespace
1661a84a0f3SAnika Henke*
1671a84a0f3SAnika Henke* @author Nathan Fritz <fritzn@crown.edu>
1681a84a0f3SAnika Henke*/
1691a84a0f3SAnika Henkefunction curNS($id) {
1701a84a0f3SAnika Henke    return noNS(getNS($id));
1711a84a0f3SAnika Henke}
1721a84a0f3SAnika Henke
1731a84a0f3SAnika Henke/**
1741a84a0f3SAnika Henke* Returns the ID without the namespace or current namespace for 'start' pages
1751a84a0f3SAnika Henke*
1761a84a0f3SAnika Henke* @author Nathan Fritz <fritzn@crown.edu>
1771a84a0f3SAnika Henke*/
1781a84a0f3SAnika Henkefunction noNSorNS($id) {
1791a84a0f3SAnika Henke    global $conf;
1801a84a0f3SAnika Henke
1811a84a0f3SAnika Henke    $p = noNS($id);
1821a84a0f3SAnika Henke    if ($p == $conf['start']) {
1831a84a0f3SAnika Henke        $p = curNS($id);
1841a84a0f3SAnika Henke        if ($p == false) {
1851a84a0f3SAnika Henke            return noNS($id);
1861a84a0f3SAnika Henke        }
1871a84a0f3SAnika Henke    }
1881a84a0f3SAnika Henke    return $p;
189b625487dSandi}
190b625487dSandi
191b625487dSandi/**
192103c256aSChris Smith *  Wiki page existence check
193103c256aSChris Smith *
194103c256aSChris Smith *  parameters as for wikiFN
195103c256aSChris Smith *
196103c256aSChris Smith *  @author Chris Smith <chris@jalakai.co.uk>
197103c256aSChris Smith */
198103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
199103c256aSChris Smith  return @file_exists(wikiFN($id,$rev,$clean));
200103c256aSChris Smith}
201103c256aSChris Smith
202103c256aSChris Smith/**
203103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
204b625487dSandi *
205b625487dSandi * The filename is URL encoded to protect Unicode chars
206b625487dSandi *
207103c256aSChris Smith * @param  $raw_id  string   id of wikipage
208103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
209103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
210103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
211103c256aSChris Smith *
212b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
213b625487dSandi */
2146e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
215b625487dSandi  global $conf;
2166e0cc83aSchris
217dc2c0e04Schris  global $cache_wikifn;
218dc2c0e04Schris  $cache = & $cache_wikifn;
219dc2c0e04Schris
2206e0cc83aSchris  if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2216e0cc83aSchris    return $cache[$raw_id][$rev];
2226e0cc83aSchris  }
2236e0cc83aSchris
2246e0cc83aSchris  $id = $raw_id;
2256e0cc83aSchris
2260d8ea614Schris  if ($clean) $id = cleanID($id);
227b625487dSandi  $id = str_replace(':','/',$id);
228b625487dSandi  if(empty($rev)){
229b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
230b625487dSandi  }else{
231b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
232ff3ed99fSmarcel    if($conf['compression']){
233ff3ed99fSmarcel      //test for extensions here, we want to read both compressions
234d8186216SBen Coburn       if (@file_exists($fn . '.gz')){
235b625487dSandi          $fn .= '.gz';
236d8186216SBen Coburn       }else if(@file_exists($fn . '.bz2')){
237ff3ed99fSmarcel          $fn .= '.bz2';
238ff3ed99fSmarcel       }else{
239ff3ed99fSmarcel          //file doesnt exist yet, so we take the configured extension
240ff3ed99fSmarcel          $fn .= '.' . $conf['compression'];
241ff3ed99fSmarcel       }
242b625487dSandi    }
243b625487dSandi  }
2446e0cc83aSchris
24550602150SBen Coburn  if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2466e0cc83aSchris  $cache[$raw_id][$rev] = $fn;
247b625487dSandi  return $fn;
248b625487dSandi}
249b625487dSandi
250b625487dSandi/**
251c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
252c9b4bd1eSBen Coburn *
253c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
254c9b4bd1eSBen Coburn */
255c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
256c9b4bd1eSBen Coburn  global $conf;
257662ff478SAndreas Gohr  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
258c9b4bd1eSBen Coburn}
259c9b4bd1eSBen Coburn
260c9b4bd1eSBen Coburn
261c9b4bd1eSBen Coburn/**
2621380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
263b158d625SSteven Danz *
264b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
265b158d625SSteven Danz *
266b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
267b158d625SSteven Danz */
2681380fc45SAndreas Gohrfunction metaFN($id,$ext){
269b158d625SSteven Danz  global $conf;
270b158d625SSteven Danz  $id = cleanID($id);
271b158d625SSteven Danz  $id = str_replace(':','/',$id);
2721380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
273b158d625SSteven Danz  return $fn;
274b158d625SSteven Danz}
275b158d625SSteven Danz
276b158d625SSteven Danz/**
277e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
278e1f3d9e1SEsther Brunner *
279e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
280e1f3d9e1SEsther Brunner */
281e1f3d9e1SEsther Brunnerfunction metaFiles($id){
282e1f3d9e1SEsther Brunner   $name   = noNS($id);
2836a5b38cdSMichael Klier   $ns     = getNS($id);
2846a5b38cdSMichael Klier   $dir    = ($ns) ? metaFN($ns,'').'/' : metaFN($ns,'');
285e1f3d9e1SEsther Brunner   $files  = array();
286e1f3d9e1SEsther Brunner
287e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
2885011da9dSEsther Brunner   if(!$dh) return $files;
289e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
2901a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
291e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
292e1f3d9e1SEsther Brunner   }
293e1f3d9e1SEsther Brunner   closedir($dh);
294e1f3d9e1SEsther Brunner
295e1f3d9e1SEsther Brunner   return $files;
296e1f3d9e1SEsther Brunner}
297e1f3d9e1SEsther Brunner
298e1f3d9e1SEsther Brunner/**
299b625487dSandi * returns the full path to the mediafile specified by ID
300b625487dSandi *
301b625487dSandi * The filename is URL encoded to protect Unicode chars
302b625487dSandi *
303b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
304b625487dSandi */
305b625487dSandifunction mediaFN($id){
306b625487dSandi  global $conf;
307b625487dSandi  $id = cleanID($id);
308b625487dSandi  $id = str_replace(':','/',$id);
309b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
310b625487dSandi  return $fn;
311b625487dSandi}
312b625487dSandi
313b625487dSandi/**
314b625487dSandi * Returns the full filepath to a localized textfile if local
315b625487dSandi * version isn't found the english one is returned
316b625487dSandi *
317b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
318b625487dSandi */
319b625487dSandifunction localeFN($id){
320b625487dSandi  global $conf;
321bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
322b625487dSandi  if(!@file_exists($file)){
323b625487dSandi    //fall back to english
324bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
325b625487dSandi  }
326b625487dSandi  return $file;
327b625487dSandi}
328b625487dSandi
329b625487dSandi/**
330c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
331c4e0e4a1SAndreas Gohr *
332c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
333c4e0e4a1SAndreas Gohr * instead
334c4e0e4a1SAndreas Gohr *
335c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
336c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
337c4e0e4a1SAndreas Gohr *
338c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
339c4e0e4a1SAndreas Gohr */
340a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
341c662a49aSAndreas Gohr  global $conf;
342c662a49aSAndreas Gohr
343c662a49aSAndreas Gohr  // some pre cleaning for useslash:
344c662a49aSAndreas Gohr  if($conf['useslash']) $id = str_replace('/',':',$id);
345c662a49aSAndreas Gohr
346c4e0e4a1SAndreas Gohr  // if the id starts with a dot we need to handle the
347c4e0e4a1SAndreas Gohr  // relative stuff
348c4e0e4a1SAndreas Gohr  if($id{0} == '.'){
349c4e0e4a1SAndreas Gohr    // normalize initial dots without a colon
350c4e0e4a1SAndreas Gohr    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
351c4e0e4a1SAndreas Gohr    // prepend the current namespace
352c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
353c4e0e4a1SAndreas Gohr
354c4e0e4a1SAndreas Gohr    // cleanup relatives
355c4e0e4a1SAndreas Gohr    $result = array();
356c4e0e4a1SAndreas Gohr    $pathA  = explode(':', $id);
357c4e0e4a1SAndreas Gohr    if (!$pathA[0]) $result[] = '';
358c4e0e4a1SAndreas Gohr    foreach ($pathA AS $key => $dir) {
359c4e0e4a1SAndreas Gohr      if ($dir == '..') {
360c4e0e4a1SAndreas Gohr        if (end($result) == '..') {
361c4e0e4a1SAndreas Gohr          $result[] = '..';
362c4e0e4a1SAndreas Gohr        } elseif (!array_pop($result)) {
363c4e0e4a1SAndreas Gohr          $result[] = '..';
364c4e0e4a1SAndreas Gohr        }
365c4e0e4a1SAndreas Gohr      } elseif ($dir && $dir != '.') {
366c4e0e4a1SAndreas Gohr        $result[] = $dir;
367c4e0e4a1SAndreas Gohr      }
368c4e0e4a1SAndreas Gohr    }
369c4e0e4a1SAndreas Gohr    if (!end($pathA)) $result[] = '';
370c4e0e4a1SAndreas Gohr    $id = implode(':', $result);
371c4e0e4a1SAndreas Gohr  }elseif($ns !== false && strpos($id,':') === false){
372c4e0e4a1SAndreas Gohr    //if link contains no namespace. add current namespace (if any)
373c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
374c4e0e4a1SAndreas Gohr  }
375c4e0e4a1SAndreas Gohr
376a6ef4796SAndreas Gohr  if($clean) $id = cleanID($id);
377a6ef4796SAndreas Gohr  return $id;
378c4e0e4a1SAndreas Gohr}
379c4e0e4a1SAndreas Gohr
380c4e0e4a1SAndreas Gohr/**
381b625487dSandi * Returns a full media id
382b625487dSandi *
383b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
384b625487dSandi */
38537e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
386c4e0e4a1SAndreas Gohr  $page   = resolve_id($ns,$page);
387b625487dSandi  $file   = mediaFN($page);
388b625487dSandi  $exists = @file_exists($file);
389b625487dSandi}
390b625487dSandi
391b625487dSandi/**
392b625487dSandi * Returns a full page id
393b625487dSandi *
394b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
395b625487dSandi */
39637e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
397b625487dSandi  global $conf;
3980b7c14c2Sandi  $exists = false;
399b625487dSandi
400b625487dSandi  //keep hashlink if exists then clean both parts
40103c4aec3Schris  if (strpos($page,'#')) {
402b625487dSandi    list($page,$hash) = split('#',$page,2);
40303c4aec3Schris  } else {
40403c4aec3Schris    $hash = '';
40503c4aec3Schris  }
406b625487dSandi  $hash = cleanID($hash);
407a6ef4796SAndreas Gohr  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
408b625487dSandi
409a6ef4796SAndreas Gohr  // get filename (calls clean itself)
410b625487dSandi  $file = wikiFN($page);
411b625487dSandi
4121179df0eSGuy Brand  // if ends with colon or slash we have a namespace link
4131179df0eSGuy Brand  if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){
414103c256aSChris Smith    if(page_exists($page.$conf['start'])){
415a6ef4796SAndreas Gohr      // start page inside namespace
416a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
417a6ef4796SAndreas Gohr      $exists = true;
418103c256aSChris Smith    }elseif(page_exists($page.noNS(cleanID($page)))){
419a6ef4796SAndreas Gohr      // page named like the NS inside the NS
420a6ef4796SAndreas Gohr      $page = $page.noNS(cleanID($page));
421a6ef4796SAndreas Gohr      $exists = true;
422103c256aSChris Smith    }elseif(page_exists($page)){
423a6ef4796SAndreas Gohr      // page like namespace exists
424a6ef4796SAndreas Gohr      $page = $page;
425a6ef4796SAndreas Gohr      $exists = true;
426a6ef4796SAndreas Gohr    }else{
427a6ef4796SAndreas Gohr      // fall back to default
428a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
429a6ef4796SAndreas Gohr    }
430a6ef4796SAndreas Gohr  }else{
431b625487dSandi    //check alternative plural/nonplural form
432b625487dSandi    if(!@file_exists($file)){
433b625487dSandi      if( $conf['autoplural'] ){
434b625487dSandi        if(substr($page,-1) == 's'){
435b625487dSandi          $try = substr($page,0,-1);
436b625487dSandi        }else{
437b625487dSandi          $try = $page.'s';
438b625487dSandi        }
439103c256aSChris Smith        if(page_exists($try)){
440b625487dSandi          $page   = $try;
441b625487dSandi          $exists = true;
442b625487dSandi        }
443b625487dSandi      }
444b625487dSandi    }else{
445b625487dSandi      $exists = true;
446b625487dSandi    }
447a6ef4796SAndreas Gohr  }
448a6ef4796SAndreas Gohr
449a6ef4796SAndreas Gohr  // now make sure we have a clean page
450a6ef4796SAndreas Gohr  $page = cleanID($page);
451b625487dSandi
452b625487dSandi  //add hash if any
453b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
454b625487dSandi}
455b625487dSandi
45698407a7aSandi/**
45798407a7aSandi * Returns the name of a cachefile from given data
45898407a7aSandi *
45998407a7aSandi * The needed directory is created by this function!
46098407a7aSandi *
46198407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
46298407a7aSandi *
46398407a7aSandi * @param string $data  This data is used to create a unique md5 name
46498407a7aSandi * @param string $ext   This is appended to the filename if given
46598407a7aSandi * @return string       The filename of the cachefile
46698407a7aSandi */
46798407a7aSandifunction getCacheName($data,$ext=''){
46898407a7aSandi  global $conf;
46998407a7aSandi  $md5  = md5($data);
47098407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
47198407a7aSandi  io_makeFileDir($file);
47298407a7aSandi  return $file;
47398407a7aSandi}
47498407a7aSandi
4750dc92c6fSAndreas Gohr/**
4760dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
4770dc92c6fSAndreas Gohr *
4780dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4790dc92c6fSAndreas Gohr */
4800dc92c6fSAndreas Gohrfunction isHiddenPage($id){
4810dc92c6fSAndreas Gohr  global $conf;
4820dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
4830dc92c6fSAndreas Gohr
4840dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
4850dc92c6fSAndreas Gohr    return true;
4860dc92c6fSAndreas Gohr  }
4870dc92c6fSAndreas Gohr  return false;
4880dc92c6fSAndreas Gohr}
4890dc92c6fSAndreas Gohr
4900dc92c6fSAndreas Gohr/**
4910dc92c6fSAndreas Gohr * Reverse of isHiddenPage
4920dc92c6fSAndreas Gohr *
4930dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4940dc92c6fSAndreas Gohr */
4950dc92c6fSAndreas Gohrfunction isVisiblePage($id){
4960dc92c6fSAndreas Gohr  return !isHiddenPage($id);
4970dc92c6fSAndreas Gohr}
4980dc92c6fSAndreas Gohr
499254e5c84SBen Coburn/**
500254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests
501254e5c84SBen Coburn *
502254e5c84SBen Coburn * @author   Simon Willison <swillison@gmail.com>
503254e5c84SBen Coburn * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
5040ac9a84dSoliver * @param    timestamp $timestamp lastmodified time of the cache file
5050ac9a84dSoliver * @returns  void or void with previously header() commands executed
506254e5c84SBen Coburn */
507254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){
508254e5c84SBen Coburn  // A PHP implementation of conditional get, see
509254e5c84SBen Coburn  //   http://fishbowl.pastiche.org/archives/001132.html
5106d88439aSAndreas Gohr  $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
511254e5c84SBen Coburn  $etag = '"'.md5($last_modified).'"';
512254e5c84SBen Coburn  // Send the headers
513254e5c84SBen Coburn  header("Last-Modified: $last_modified");
514254e5c84SBen Coburn  header("ETag: $etag");
515254e5c84SBen Coburn  // See if the client has provided the required headers
5160ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
5170ac9a84dSoliver    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
5180ac9a84dSoliver  }else{
5190ac9a84dSoliver    $if_modified_since = false;
5200ac9a84dSoliver  }
5210ac9a84dSoliver
5220ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
5230ac9a84dSoliver    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
5240ac9a84dSoliver  }else{
5250ac9a84dSoliver    $if_none_match = false;
5260ac9a84dSoliver  }
5270ac9a84dSoliver
528254e5c84SBen Coburn  if (!$if_modified_since && !$if_none_match){
529254e5c84SBen Coburn    return;
530254e5c84SBen Coburn  }
5310ac9a84dSoliver
532254e5c84SBen Coburn  // At least one of the headers is there - check them
533254e5c84SBen Coburn  if ($if_none_match && $if_none_match != $etag) {
534254e5c84SBen Coburn    return; // etag is there but doesn't match
535254e5c84SBen Coburn  }
5360ac9a84dSoliver
537254e5c84SBen Coburn  if ($if_modified_since && $if_modified_since != $last_modified) {
538254e5c84SBen Coburn    return; // if-modified-since is there but doesn't match
539254e5c84SBen Coburn  }
5400ac9a84dSoliver
541254e5c84SBen Coburn  // Nothing has changed since their last request - serve a 304 and exit
542254e5c84SBen Coburn  header('HTTP/1.0 304 Not Modified');
543fc39f9b4SJens Wilke
544fc39f9b4SJens Wilke  // don't produce output, even if compression is on
545fc39f9b4SJens Wilke  ob_end_clean();
546254e5c84SBen Coburn  exit;
547254e5c84SBen Coburn}
548254e5c84SBen Coburn
549b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
550