xref: /dokuwiki/inc/pageutils.php (revision 6a5b38cd97b00e27beaae7e3158f868dfcde4193)
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
89b625487dSandi */
906e0cc83aSchrisfunction cleanID($raw_id,$ascii=false){
91b625487dSandi  global $conf;
92b625487dSandi  global $lang;
934b5db43bSjoe.lapp  static $sepcharpat = null;
944b5db43bSjoe.lapp
95dc2c0e04Schris  global $cache_cleanid;
96dc2c0e04Schris  $cache = & $cache_cleanid;
976e0cc83aSchris
986e0cc83aSchris  // check if it's already in the memory cache
996e0cc83aSchris  if (isset($cache[$raw_id])) {
1006e0cc83aSchris    return $cache[$raw_id];
1016e0cc83aSchris    }
1026e0cc83aSchris
1034b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
1044b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
1054b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
1064b5db43bSjoe.lapp
1076e0cc83aSchris  $id = trim($raw_id);
108b625487dSandi  $id = utf8_strtolower($id);
109b625487dSandi
110b625487dSandi  //alternative namespace seperator
111b625487dSandi  $id = strtr($id,';',':');
112b625487dSandi  if($conf['useslash']){
113b625487dSandi    $id = strtr($id,'/',':');
114b625487dSandi  }else{
1154eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
116b625487dSandi  }
117b625487dSandi
1188a831f2bSAndreas Gohr  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1198a831f2bSAndreas Gohr  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
120b625487dSandi
121b625487dSandi  //remove specials
122ad81d431SAndreas Gohr  $id = utf8_stripspecials($id,$sepchar,'\*');
123b625487dSandi
1248a831f2bSAndreas Gohr  if($ascii) $id = utf8_strip($id);
1258a831f2bSAndreas Gohr
126b625487dSandi  //clean up
1274b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
128b625487dSandi  $id = preg_replace('#:+#',':',$id);
129b625487dSandi  $id = trim($id,':._-');
130b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
131b625487dSandi
1326e0cc83aSchris  $cache[$raw_id] = $id;
133b625487dSandi  return($id);
134b625487dSandi}
135b625487dSandi
136b625487dSandi/**
137b625487dSandi * Return namespacepart of a wiki ID
138b625487dSandi *
139b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
140b625487dSandi */
141b625487dSandifunction getNS($id){
142c4e0e4a1SAndreas Gohr  $pos = strrpos($id,':');
143c4e0e4a1SAndreas Gohr  if($pos!==false){
144c4e0e4a1SAndreas Gohr    return substr($id,0,$pos);
145b625487dSandi  }
146b625487dSandi  return false;
147b625487dSandi}
148b625487dSandi
149b625487dSandi/**
150b625487dSandi * Returns the ID without the namespace
151b625487dSandi *
152b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
153b625487dSandi */
154b625487dSandifunction noNS($id) {
1552844584fSBen Coburn  $pos = strrpos($id, ':');
1562844584fSBen Coburn  if ($pos!==false) {
1572844584fSBen Coburn    return substr($id, $pos+1);
1582844584fSBen Coburn  } else {
1592844584fSBen Coburn    return $id;
1602844584fSBen Coburn  }
1611a84a0f3SAnika Henke}
1621a84a0f3SAnika Henke
1631a84a0f3SAnika Henke/**
1641a84a0f3SAnika Henke* Returns the current namespace
1651a84a0f3SAnika Henke*
1661a84a0f3SAnika Henke* @author Nathan Fritz <fritzn@crown.edu>
1671a84a0f3SAnika Henke*/
1681a84a0f3SAnika Henkefunction curNS($id) {
1691a84a0f3SAnika Henke    return noNS(getNS($id));
1701a84a0f3SAnika Henke}
1711a84a0f3SAnika Henke
1721a84a0f3SAnika Henke/**
1731a84a0f3SAnika Henke* Returns the ID without the namespace or current namespace for 'start' pages
1741a84a0f3SAnika Henke*
1751a84a0f3SAnika Henke* @author Nathan Fritz <fritzn@crown.edu>
1761a84a0f3SAnika Henke*/
1771a84a0f3SAnika Henkefunction noNSorNS($id) {
1781a84a0f3SAnika Henke    global $conf;
1791a84a0f3SAnika Henke
1801a84a0f3SAnika Henke    $p = noNS($id);
1811a84a0f3SAnika Henke    if ($p == $conf['start']) {
1821a84a0f3SAnika Henke        $p = curNS($id);
1831a84a0f3SAnika Henke        if ($p == false) {
1841a84a0f3SAnika Henke            return noNS($id);
1851a84a0f3SAnika Henke        }
1861a84a0f3SAnika Henke    }
1871a84a0f3SAnika Henke    return $p;
188b625487dSandi}
189b625487dSandi
190b625487dSandi/**
191103c256aSChris Smith *  Wiki page existence check
192103c256aSChris Smith *
193103c256aSChris Smith *  parameters as for wikiFN
194103c256aSChris Smith *
195103c256aSChris Smith *  @author Chris Smith <chris@jalakai.co.uk>
196103c256aSChris Smith */
197103c256aSChris Smithfunction page_exists($id,$rev='',$clean=true) {
198103c256aSChris Smith  return @file_exists(wikiFN($id,$rev,$clean));
199103c256aSChris Smith}
200103c256aSChris Smith
201103c256aSChris Smith/**
202103c256aSChris Smith * returns the full path to the datafile specified by ID and optional revision
203b625487dSandi *
204b625487dSandi * The filename is URL encoded to protect Unicode chars
205b625487dSandi *
206103c256aSChris Smith * @param  $raw_id  string   id of wikipage
207103c256aSChris Smith * @param  $rev     string   page revision, empty string for current
208103c256aSChris Smith * @param  $clean   bool     flag indicating that $raw_id should be cleaned.  Only set to false
209103c256aSChris Smith *                           when $id is guaranteed to have been cleaned already.
210103c256aSChris Smith *
211b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
212b625487dSandi */
2136e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
214b625487dSandi  global $conf;
2156e0cc83aSchris
216dc2c0e04Schris  global $cache_wikifn;
217dc2c0e04Schris  $cache = & $cache_wikifn;
218dc2c0e04Schris
2196e0cc83aSchris  if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2206e0cc83aSchris    return $cache[$raw_id][$rev];
2216e0cc83aSchris  }
2226e0cc83aSchris
2236e0cc83aSchris  $id = $raw_id;
2246e0cc83aSchris
2250d8ea614Schris  if ($clean) $id = cleanID($id);
226b625487dSandi  $id = str_replace(':','/',$id);
227b625487dSandi  if(empty($rev)){
228b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
229b625487dSandi  }else{
230b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
231ff3ed99fSmarcel    if($conf['compression']){
232ff3ed99fSmarcel      //test for extensions here, we want to read both compressions
233d8186216SBen Coburn       if (@file_exists($fn . '.gz')){
234b625487dSandi          $fn .= '.gz';
235d8186216SBen Coburn       }else if(@file_exists($fn . '.bz2')){
236ff3ed99fSmarcel          $fn .= '.bz2';
237ff3ed99fSmarcel       }else{
238ff3ed99fSmarcel          //file doesnt exist yet, so we take the configured extension
239ff3ed99fSmarcel          $fn .= '.' . $conf['compression'];
240ff3ed99fSmarcel       }
241b625487dSandi    }
242b625487dSandi  }
2436e0cc83aSchris
24450602150SBen Coburn  if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2456e0cc83aSchris  $cache[$raw_id][$rev] = $fn;
246b625487dSandi  return $fn;
247b625487dSandi}
248b625487dSandi
249b625487dSandi/**
250c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
251c9b4bd1eSBen Coburn *
252c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
253c9b4bd1eSBen Coburn */
254c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
255c9b4bd1eSBen Coburn  global $conf;
256662ff478SAndreas Gohr  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
257c9b4bd1eSBen Coburn}
258c9b4bd1eSBen Coburn
259c9b4bd1eSBen Coburn
260c9b4bd1eSBen Coburn/**
2611380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
262b158d625SSteven Danz *
263b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
264b158d625SSteven Danz *
265b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
266b158d625SSteven Danz */
2671380fc45SAndreas Gohrfunction metaFN($id,$ext){
268b158d625SSteven Danz  global $conf;
269b158d625SSteven Danz  $id = cleanID($id);
270b158d625SSteven Danz  $id = str_replace(':','/',$id);
2711380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
272b158d625SSteven Danz  return $fn;
273b158d625SSteven Danz}
274b158d625SSteven Danz
275b158d625SSteven Danz/**
276e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
277e1f3d9e1SEsther Brunner *
278e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
279e1f3d9e1SEsther Brunner */
280e1f3d9e1SEsther Brunnerfunction metaFiles($id){
281e1f3d9e1SEsther Brunner   $name   = noNS($id);
282*6a5b38cdSMichael Klier   $ns     = getNS($id);
283*6a5b38cdSMichael Klier   $dir    = ($ns) ? metaFN($ns,'').'/' : metaFN($ns,'');
284e1f3d9e1SEsther Brunner   $files  = array();
285e1f3d9e1SEsther Brunner
286e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
2875011da9dSEsther Brunner   if(!$dh) return $files;
288e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
2891a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
290e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
291e1f3d9e1SEsther Brunner   }
292e1f3d9e1SEsther Brunner   closedir($dh);
293e1f3d9e1SEsther Brunner
294e1f3d9e1SEsther Brunner   return $files;
295e1f3d9e1SEsther Brunner}
296e1f3d9e1SEsther Brunner
297e1f3d9e1SEsther Brunner/**
298b625487dSandi * returns the full path to the mediafile specified by ID
299b625487dSandi *
300b625487dSandi * The filename is URL encoded to protect Unicode chars
301b625487dSandi *
302b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
303b625487dSandi */
304b625487dSandifunction mediaFN($id){
305b625487dSandi  global $conf;
306b625487dSandi  $id = cleanID($id);
307b625487dSandi  $id = str_replace(':','/',$id);
308b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
309b625487dSandi  return $fn;
310b625487dSandi}
311b625487dSandi
312b625487dSandi/**
313b625487dSandi * Returns the full filepath to a localized textfile if local
314b625487dSandi * version isn't found the english one is returned
315b625487dSandi *
316b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
317b625487dSandi */
318b625487dSandifunction localeFN($id){
319b625487dSandi  global $conf;
320bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
321b625487dSandi  if(!@file_exists($file)){
322b625487dSandi    //fall back to english
323bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
324b625487dSandi  }
325b625487dSandi  return $file;
326b625487dSandi}
327b625487dSandi
328b625487dSandi/**
329c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
330c4e0e4a1SAndreas Gohr *
331c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
332c4e0e4a1SAndreas Gohr * instead
333c4e0e4a1SAndreas Gohr *
334c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
335c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
336c4e0e4a1SAndreas Gohr *
337c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
338c4e0e4a1SAndreas Gohr */
339a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
340c662a49aSAndreas Gohr  global $conf;
341c662a49aSAndreas Gohr
342c662a49aSAndreas Gohr  // some pre cleaning for useslash:
343c662a49aSAndreas Gohr  if($conf['useslash']) $id = str_replace('/',':',$id);
344c662a49aSAndreas Gohr
345c4e0e4a1SAndreas Gohr  // if the id starts with a dot we need to handle the
346c4e0e4a1SAndreas Gohr  // relative stuff
347c4e0e4a1SAndreas Gohr  if($id{0} == '.'){
348c4e0e4a1SAndreas Gohr    // normalize initial dots without a colon
349c4e0e4a1SAndreas Gohr    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
350c4e0e4a1SAndreas Gohr    // prepend the current namespace
351c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
352c4e0e4a1SAndreas Gohr
353c4e0e4a1SAndreas Gohr    // cleanup relatives
354c4e0e4a1SAndreas Gohr    $result = array();
355c4e0e4a1SAndreas Gohr    $pathA  = explode(':', $id);
356c4e0e4a1SAndreas Gohr    if (!$pathA[0]) $result[] = '';
357c4e0e4a1SAndreas Gohr    foreach ($pathA AS $key => $dir) {
358c4e0e4a1SAndreas Gohr      if ($dir == '..') {
359c4e0e4a1SAndreas Gohr        if (end($result) == '..') {
360c4e0e4a1SAndreas Gohr          $result[] = '..';
361c4e0e4a1SAndreas Gohr        } elseif (!array_pop($result)) {
362c4e0e4a1SAndreas Gohr          $result[] = '..';
363c4e0e4a1SAndreas Gohr        }
364c4e0e4a1SAndreas Gohr      } elseif ($dir && $dir != '.') {
365c4e0e4a1SAndreas Gohr        $result[] = $dir;
366c4e0e4a1SAndreas Gohr      }
367c4e0e4a1SAndreas Gohr    }
368c4e0e4a1SAndreas Gohr    if (!end($pathA)) $result[] = '';
369c4e0e4a1SAndreas Gohr    $id = implode(':', $result);
370c4e0e4a1SAndreas Gohr  }elseif($ns !== false && strpos($id,':') === false){
371c4e0e4a1SAndreas Gohr    //if link contains no namespace. add current namespace (if any)
372c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
373c4e0e4a1SAndreas Gohr  }
374c4e0e4a1SAndreas Gohr
375a6ef4796SAndreas Gohr  if($clean) $id = cleanID($id);
376a6ef4796SAndreas Gohr  return $id;
377c4e0e4a1SAndreas Gohr}
378c4e0e4a1SAndreas Gohr
379c4e0e4a1SAndreas Gohr/**
380b625487dSandi * Returns a full media id
381b625487dSandi *
382b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
383b625487dSandi */
38437e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
385c4e0e4a1SAndreas Gohr  $page   = resolve_id($ns,$page);
386b625487dSandi  $file   = mediaFN($page);
387b625487dSandi  $exists = @file_exists($file);
388b625487dSandi}
389b625487dSandi
390b625487dSandi/**
391b625487dSandi * Returns a full page id
392b625487dSandi *
393b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
394b625487dSandi */
39537e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
396b625487dSandi  global $conf;
3970b7c14c2Sandi  $exists = false;
398b625487dSandi
399b625487dSandi  //keep hashlink if exists then clean both parts
40003c4aec3Schris  if (strpos($page,'#')) {
401b625487dSandi    list($page,$hash) = split('#',$page,2);
40203c4aec3Schris  } else {
40303c4aec3Schris    $hash = '';
40403c4aec3Schris  }
405b625487dSandi  $hash = cleanID($hash);
406a6ef4796SAndreas Gohr  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
407b625487dSandi
408a6ef4796SAndreas Gohr  // get filename (calls clean itself)
409b625487dSandi  $file = wikiFN($page);
410b625487dSandi
4111179df0eSGuy Brand  // if ends with colon or slash we have a namespace link
4121179df0eSGuy Brand  if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){
413103c256aSChris Smith    if(page_exists($page.$conf['start'])){
414a6ef4796SAndreas Gohr      // start page inside namespace
415a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
416a6ef4796SAndreas Gohr      $exists = true;
417103c256aSChris Smith    }elseif(page_exists($page.noNS(cleanID($page)))){
418a6ef4796SAndreas Gohr      // page named like the NS inside the NS
419a6ef4796SAndreas Gohr      $page = $page.noNS(cleanID($page));
420a6ef4796SAndreas Gohr      $exists = true;
421103c256aSChris Smith    }elseif(page_exists($page)){
422a6ef4796SAndreas Gohr      // page like namespace exists
423a6ef4796SAndreas Gohr      $page = $page;
424a6ef4796SAndreas Gohr      $exists = true;
425a6ef4796SAndreas Gohr    }else{
426a6ef4796SAndreas Gohr      // fall back to default
427a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
428a6ef4796SAndreas Gohr    }
429a6ef4796SAndreas Gohr  }else{
430b625487dSandi    //check alternative plural/nonplural form
431b625487dSandi    if(!@file_exists($file)){
432b625487dSandi      if( $conf['autoplural'] ){
433b625487dSandi        if(substr($page,-1) == 's'){
434b625487dSandi          $try = substr($page,0,-1);
435b625487dSandi        }else{
436b625487dSandi          $try = $page.'s';
437b625487dSandi        }
438103c256aSChris Smith        if(page_exists($try)){
439b625487dSandi          $page   = $try;
440b625487dSandi          $exists = true;
441b625487dSandi        }
442b625487dSandi      }
443b625487dSandi    }else{
444b625487dSandi      $exists = true;
445b625487dSandi    }
446a6ef4796SAndreas Gohr  }
447a6ef4796SAndreas Gohr
448a6ef4796SAndreas Gohr  // now make sure we have a clean page
449a6ef4796SAndreas Gohr  $page = cleanID($page);
450b625487dSandi
451b625487dSandi  //add hash if any
452b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
453b625487dSandi}
454b625487dSandi
45598407a7aSandi/**
45698407a7aSandi * Returns the name of a cachefile from given data
45798407a7aSandi *
45898407a7aSandi * The needed directory is created by this function!
45998407a7aSandi *
46098407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
46198407a7aSandi *
46298407a7aSandi * @param string $data  This data is used to create a unique md5 name
46398407a7aSandi * @param string $ext   This is appended to the filename if given
46498407a7aSandi * @return string       The filename of the cachefile
46598407a7aSandi */
46698407a7aSandifunction getCacheName($data,$ext=''){
46798407a7aSandi  global $conf;
46898407a7aSandi  $md5  = md5($data);
46998407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
47098407a7aSandi  io_makeFileDir($file);
47198407a7aSandi  return $file;
47298407a7aSandi}
47398407a7aSandi
4740dc92c6fSAndreas Gohr/**
4750dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
4760dc92c6fSAndreas Gohr *
4770dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4780dc92c6fSAndreas Gohr */
4790dc92c6fSAndreas Gohrfunction isHiddenPage($id){
4800dc92c6fSAndreas Gohr  global $conf;
4810dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
4820dc92c6fSAndreas Gohr
4830dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
4840dc92c6fSAndreas Gohr    return true;
4850dc92c6fSAndreas Gohr  }
4860dc92c6fSAndreas Gohr  return false;
4870dc92c6fSAndreas Gohr}
4880dc92c6fSAndreas Gohr
4890dc92c6fSAndreas Gohr/**
4900dc92c6fSAndreas Gohr * Reverse of isHiddenPage
4910dc92c6fSAndreas Gohr *
4920dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4930dc92c6fSAndreas Gohr */
4940dc92c6fSAndreas Gohrfunction isVisiblePage($id){
4950dc92c6fSAndreas Gohr  return !isHiddenPage($id);
4960dc92c6fSAndreas Gohr}
4970dc92c6fSAndreas Gohr
498254e5c84SBen Coburn/**
499254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests
500254e5c84SBen Coburn *
501254e5c84SBen Coburn * @author   Simon Willison <swillison@gmail.com>
502254e5c84SBen Coburn * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
5030ac9a84dSoliver * @param    timestamp $timestamp lastmodified time of the cache file
5040ac9a84dSoliver * @returns  void or void with previously header() commands executed
505254e5c84SBen Coburn */
506254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){
507254e5c84SBen Coburn  // A PHP implementation of conditional get, see
508254e5c84SBen Coburn  //   http://fishbowl.pastiche.org/archives/001132.html
5096d88439aSAndreas Gohr  $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
510254e5c84SBen Coburn  $etag = '"'.md5($last_modified).'"';
511254e5c84SBen Coburn  // Send the headers
512254e5c84SBen Coburn  header("Last-Modified: $last_modified");
513254e5c84SBen Coburn  header("ETag: $etag");
514254e5c84SBen Coburn  // See if the client has provided the required headers
5150ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
5160ac9a84dSoliver    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
5170ac9a84dSoliver  }else{
5180ac9a84dSoliver    $if_modified_since = false;
5190ac9a84dSoliver  }
5200ac9a84dSoliver
5210ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
5220ac9a84dSoliver    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
5230ac9a84dSoliver  }else{
5240ac9a84dSoliver    $if_none_match = false;
5250ac9a84dSoliver  }
5260ac9a84dSoliver
527254e5c84SBen Coburn  if (!$if_modified_since && !$if_none_match){
528254e5c84SBen Coburn    return;
529254e5c84SBen Coburn  }
5300ac9a84dSoliver
531254e5c84SBen Coburn  // At least one of the headers is there - check them
532254e5c84SBen Coburn  if ($if_none_match && $if_none_match != $etag) {
533254e5c84SBen Coburn    return; // etag is there but doesn't match
534254e5c84SBen Coburn  }
5350ac9a84dSoliver
536254e5c84SBen Coburn  if ($if_modified_since && $if_modified_since != $last_modified) {
537254e5c84SBen Coburn    return; // if-modified-since is there but doesn't match
538254e5c84SBen Coburn  }
5390ac9a84dSoliver
540254e5c84SBen Coburn  // Nothing has changed since their last request - serve a 304 and exit
541254e5c84SBen Coburn  header('HTTP/1.0 304 Not Modified');
542fc39f9b4SJens Wilke
543fc39f9b4SJens Wilke  // don't produce output, even if compression is on
544fc39f9b4SJens Wilke  ob_end_clean();
545254e5c84SBen Coburn  exit;
546254e5c84SBen Coburn}
547254e5c84SBen Coburn
548b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
549