xref: /dokuwiki/inc/pageutils.php (revision 7a42ac9e2468bf8a316de0792b1ebc3b87d8fdac)
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) == '/')){
58671a58a6SGuy Brand    if(@file_exists(wikiFN($id.$conf['start']))){
59671a58a6SGuy Brand      // start page inside namespace
60671a58a6SGuy Brand      $id = $id.$conf['start'];
61671a58a6SGuy Brand    }elseif(@file_exists(wikiFN($id.noNS(cleanID($id))))){
62671a58a6SGuy Brand      // page named like the NS inside the NS
63671a58a6SGuy Brand      $id = $id.noNS(cleanID($id));
64671a58a6SGuy Brand    }elseif(@file_exists(wikiFN($id))){
65671a58a6SGuy Brand      // page like namespace exists
66*7a42ac9eSBen 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/**
191b625487dSandi * returns the full path to the datafile specified by ID and
192b625487dSandi * optional revision
193b625487dSandi *
194b625487dSandi * The filename is URL encoded to protect Unicode chars
195b625487dSandi *
196b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
197b625487dSandi */
1986e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
199b625487dSandi  global $conf;
2006e0cc83aSchris
201dc2c0e04Schris  global $cache_wikifn;
202dc2c0e04Schris  $cache = & $cache_wikifn;
203dc2c0e04Schris
2046e0cc83aSchris  if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
2056e0cc83aSchris    return $cache[$raw_id][$rev];
2066e0cc83aSchris  }
2076e0cc83aSchris
2086e0cc83aSchris  $id = $raw_id;
2096e0cc83aSchris
2100d8ea614Schris  if ($clean) $id = cleanID($id);
211b625487dSandi  $id = str_replace(':','/',$id);
212b625487dSandi  if(empty($rev)){
213b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
214b625487dSandi  }else{
215b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
216ff3ed99fSmarcel    if($conf['compression']){
217ff3ed99fSmarcel      //test for extensions here, we want to read both compressions
218d8186216SBen Coburn       if (@file_exists($fn . '.gz')){
219b625487dSandi          $fn .= '.gz';
220d8186216SBen Coburn       }else if(@file_exists($fn . '.bz2')){
221ff3ed99fSmarcel          $fn .= '.bz2';
222ff3ed99fSmarcel       }else{
223ff3ed99fSmarcel          //file doesnt exist yet, so we take the configured extension
224ff3ed99fSmarcel          $fn .= '.' . $conf['compression'];
225ff3ed99fSmarcel       }
226b625487dSandi    }
227b625487dSandi  }
2286e0cc83aSchris
22950602150SBen Coburn  if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); }
2306e0cc83aSchris  $cache[$raw_id][$rev] = $fn;
231b625487dSandi  return $fn;
232b625487dSandi}
233b625487dSandi
234b625487dSandi/**
235c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
236c9b4bd1eSBen Coburn *
237c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
238c9b4bd1eSBen Coburn */
239c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
240c9b4bd1eSBen Coburn  global $conf;
241662ff478SAndreas Gohr  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
242c9b4bd1eSBen Coburn}
243c9b4bd1eSBen Coburn
244c9b4bd1eSBen Coburn
245c9b4bd1eSBen Coburn/**
2461380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
247b158d625SSteven Danz *
248b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
249b158d625SSteven Danz *
250b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
251b158d625SSteven Danz */
2521380fc45SAndreas Gohrfunction metaFN($id,$ext){
253b158d625SSteven Danz  global $conf;
254b158d625SSteven Danz  $id = cleanID($id);
255b158d625SSteven Danz  $id = str_replace(':','/',$id);
2561380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
257b158d625SSteven Danz  return $fn;
258b158d625SSteven Danz}
259b158d625SSteven Danz
260b158d625SSteven Danz/**
261e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
262e1f3d9e1SEsther Brunner *
263e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
264e1f3d9e1SEsther Brunner */
265e1f3d9e1SEsther Brunnerfunction metaFiles($id){
266e1f3d9e1SEsther Brunner   $name   = noNS($id);
267e1f3d9e1SEsther Brunner   $dir    = metaFN(getNS($id),'');
268e1f3d9e1SEsther Brunner   $files  = array();
269e1f3d9e1SEsther Brunner
270e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
2715011da9dSEsther Brunner   if(!$dh) return $files;
272e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
2731a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
274e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
275e1f3d9e1SEsther Brunner   }
276e1f3d9e1SEsther Brunner   closedir($dh);
277e1f3d9e1SEsther Brunner
278e1f3d9e1SEsther Brunner   return $files;
279e1f3d9e1SEsther Brunner}
280e1f3d9e1SEsther Brunner
281e1f3d9e1SEsther Brunner/**
282b625487dSandi * returns the full path to the mediafile specified by ID
283b625487dSandi *
284b625487dSandi * The filename is URL encoded to protect Unicode chars
285b625487dSandi *
286b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
287b625487dSandi */
288b625487dSandifunction mediaFN($id){
289b625487dSandi  global $conf;
290b625487dSandi  $id = cleanID($id);
291b625487dSandi  $id = str_replace(':','/',$id);
292b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
293b625487dSandi  return $fn;
294b625487dSandi}
295b625487dSandi
296b625487dSandi/**
297b625487dSandi * Returns the full filepath to a localized textfile if local
298b625487dSandi * version isn't found the english one is returned
299b625487dSandi *
300b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
301b625487dSandi */
302b625487dSandifunction localeFN($id){
303b625487dSandi  global $conf;
304bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
305b625487dSandi  if(!@file_exists($file)){
306b625487dSandi    //fall back to english
307bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
308b625487dSandi  }
309b625487dSandi  return $file;
310b625487dSandi}
311b625487dSandi
312b625487dSandi/**
313c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
314c4e0e4a1SAndreas Gohr *
315c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
316c4e0e4a1SAndreas Gohr * instead
317c4e0e4a1SAndreas Gohr *
318c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
319c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
320c4e0e4a1SAndreas Gohr *
321c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
322c4e0e4a1SAndreas Gohr */
323a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
324c4e0e4a1SAndreas Gohr  // if the id starts with a dot we need to handle the
325c4e0e4a1SAndreas Gohr  // relative stuff
326c4e0e4a1SAndreas Gohr  if($id{0} == '.'){
327c4e0e4a1SAndreas Gohr    // normalize initial dots without a colon
328c4e0e4a1SAndreas Gohr    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
329c4e0e4a1SAndreas Gohr    // prepend the current namespace
330c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
331c4e0e4a1SAndreas Gohr
332c4e0e4a1SAndreas Gohr    // cleanup relatives
333c4e0e4a1SAndreas Gohr    $result = array();
334c4e0e4a1SAndreas Gohr    $pathA  = explode(':', $id);
335c4e0e4a1SAndreas Gohr    if (!$pathA[0]) $result[] = '';
336c4e0e4a1SAndreas Gohr    foreach ($pathA AS $key => $dir) {
337c4e0e4a1SAndreas Gohr      if ($dir == '..') {
338c4e0e4a1SAndreas Gohr        if (end($result) == '..') {
339c4e0e4a1SAndreas Gohr          $result[] = '..';
340c4e0e4a1SAndreas Gohr        } elseif (!array_pop($result)) {
341c4e0e4a1SAndreas Gohr          $result[] = '..';
342c4e0e4a1SAndreas Gohr        }
343c4e0e4a1SAndreas Gohr      } elseif ($dir && $dir != '.') {
344c4e0e4a1SAndreas Gohr        $result[] = $dir;
345c4e0e4a1SAndreas Gohr      }
346c4e0e4a1SAndreas Gohr    }
347c4e0e4a1SAndreas Gohr    if (!end($pathA)) $result[] = '';
348c4e0e4a1SAndreas Gohr    $id = implode(':', $result);
349c4e0e4a1SAndreas Gohr  }elseif($ns !== false && strpos($id,':') === false){
350c4e0e4a1SAndreas Gohr    //if link contains no namespace. add current namespace (if any)
351c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
352c4e0e4a1SAndreas Gohr  }
353c4e0e4a1SAndreas Gohr
354a6ef4796SAndreas Gohr  if($clean) $id = cleanID($id);
355a6ef4796SAndreas Gohr  return $id;
356c4e0e4a1SAndreas Gohr}
357c4e0e4a1SAndreas Gohr
358c4e0e4a1SAndreas Gohr/**
359b625487dSandi * Returns a full media id
360b625487dSandi *
361b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
362b625487dSandi */
36337e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
364c4e0e4a1SAndreas Gohr  $page   = resolve_id($ns,$page);
365b625487dSandi  $file   = mediaFN($page);
366b625487dSandi  $exists = @file_exists($file);
367b625487dSandi}
368b625487dSandi
369b625487dSandi/**
370b625487dSandi * Returns a full page id
371b625487dSandi *
372b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
373b625487dSandi */
37437e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
375b625487dSandi  global $conf;
3760b7c14c2Sandi  $exists = false;
377b625487dSandi
378b625487dSandi  //keep hashlink if exists then clean both parts
37903c4aec3Schris  if (strpos($page,'#')) {
380b625487dSandi    list($page,$hash) = split('#',$page,2);
38103c4aec3Schris  } else {
38203c4aec3Schris    $hash = '';
38303c4aec3Schris  }
384b625487dSandi  $hash = cleanID($hash);
385a6ef4796SAndreas Gohr  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
386b625487dSandi
387a6ef4796SAndreas Gohr  // get filename (calls clean itself)
388b625487dSandi  $file = wikiFN($page);
389b625487dSandi
3901179df0eSGuy Brand  // if ends with colon or slash we have a namespace link
3911179df0eSGuy Brand  if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){
392a6ef4796SAndreas Gohr    if(@file_exists(wikiFN($page.$conf['start']))){
393a6ef4796SAndreas Gohr      // start page inside namespace
394a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
395a6ef4796SAndreas Gohr      $exists = true;
396a6ef4796SAndreas Gohr    }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){
397a6ef4796SAndreas Gohr      // page named like the NS inside the NS
398a6ef4796SAndreas Gohr      $page = $page.noNS(cleanID($page));
399a6ef4796SAndreas Gohr      $exists = true;
400a6ef4796SAndreas Gohr    }elseif(@file_exists(wikiFN($page))){
401a6ef4796SAndreas Gohr      // page like namespace exists
402a6ef4796SAndreas Gohr      $page = $page;
403a6ef4796SAndreas Gohr      $exists = true;
404a6ef4796SAndreas Gohr    }else{
405a6ef4796SAndreas Gohr      // fall back to default
406a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
407a6ef4796SAndreas Gohr    }
408a6ef4796SAndreas Gohr  }else{
409b625487dSandi    //check alternative plural/nonplural form
410b625487dSandi    if(!@file_exists($file)){
411b625487dSandi      if( $conf['autoplural'] ){
412b625487dSandi        if(substr($page,-1) == 's'){
413b625487dSandi          $try = substr($page,0,-1);
414b625487dSandi        }else{
415b625487dSandi          $try = $page.'s';
416b625487dSandi        }
417b625487dSandi        if(@file_exists(wikiFN($try))){
418b625487dSandi          $page   = $try;
419b625487dSandi          $exists = true;
420b625487dSandi        }
421b625487dSandi      }
422b625487dSandi    }else{
423b625487dSandi      $exists = true;
424b625487dSandi    }
425a6ef4796SAndreas Gohr  }
426a6ef4796SAndreas Gohr
427a6ef4796SAndreas Gohr  // now make sure we have a clean page
428a6ef4796SAndreas Gohr  $page = cleanID($page);
429b625487dSandi
430b625487dSandi  //add hash if any
431b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
432b625487dSandi}
433b625487dSandi
43498407a7aSandi/**
43598407a7aSandi * Returns the name of a cachefile from given data
43698407a7aSandi *
43798407a7aSandi * The needed directory is created by this function!
43898407a7aSandi *
43998407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
44098407a7aSandi *
44198407a7aSandi * @param string $data  This data is used to create a unique md5 name
44298407a7aSandi * @param string $ext   This is appended to the filename if given
44398407a7aSandi * @return string       The filename of the cachefile
44498407a7aSandi */
44598407a7aSandifunction getCacheName($data,$ext=''){
44698407a7aSandi  global $conf;
44798407a7aSandi  $md5  = md5($data);
44898407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
44998407a7aSandi  io_makeFileDir($file);
45098407a7aSandi  return $file;
45198407a7aSandi}
45298407a7aSandi
4530dc92c6fSAndreas Gohr/**
4540dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
4550dc92c6fSAndreas Gohr *
4560dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4570dc92c6fSAndreas Gohr */
4580dc92c6fSAndreas Gohrfunction isHiddenPage($id){
4590dc92c6fSAndreas Gohr  global $conf;
4600dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
4610dc92c6fSAndreas Gohr
4620dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
4630dc92c6fSAndreas Gohr    return true;
4640dc92c6fSAndreas Gohr  }
4650dc92c6fSAndreas Gohr  return false;
4660dc92c6fSAndreas Gohr}
4670dc92c6fSAndreas Gohr
4680dc92c6fSAndreas Gohr/**
4690dc92c6fSAndreas Gohr * Reverse of isHiddenPage
4700dc92c6fSAndreas Gohr *
4710dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4720dc92c6fSAndreas Gohr */
4730dc92c6fSAndreas Gohrfunction isVisiblePage($id){
4740dc92c6fSAndreas Gohr  return !isHiddenPage($id);
4750dc92c6fSAndreas Gohr}
4760dc92c6fSAndreas Gohr
477254e5c84SBen Coburn/**
478254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests
479254e5c84SBen Coburn *
480254e5c84SBen Coburn * @author   Simon Willison <swillison@gmail.com>
481254e5c84SBen Coburn * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
4820ac9a84dSoliver * @param    timestamp $timestamp lastmodified time of the cache file
4830ac9a84dSoliver * @returns  void or void with previously header() commands executed
484254e5c84SBen Coburn */
485254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){
486254e5c84SBen Coburn  // A PHP implementation of conditional get, see
487254e5c84SBen Coburn  //   http://fishbowl.pastiche.org/archives/001132.html
4886d88439aSAndreas Gohr  $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
489254e5c84SBen Coburn  $etag = '"'.md5($last_modified).'"';
490254e5c84SBen Coburn  // Send the headers
491254e5c84SBen Coburn  header("Last-Modified: $last_modified");
492254e5c84SBen Coburn  header("ETag: $etag");
493254e5c84SBen Coburn  // See if the client has provided the required headers
4940ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
4950ac9a84dSoliver    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
4960ac9a84dSoliver  }else{
4970ac9a84dSoliver    $if_modified_since = false;
4980ac9a84dSoliver  }
4990ac9a84dSoliver
5000ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
5010ac9a84dSoliver    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
5020ac9a84dSoliver  }else{
5030ac9a84dSoliver    $if_none_match = false;
5040ac9a84dSoliver  }
5050ac9a84dSoliver
506254e5c84SBen Coburn  if (!$if_modified_since && !$if_none_match){
507254e5c84SBen Coburn    return;
508254e5c84SBen Coburn  }
5090ac9a84dSoliver
510254e5c84SBen Coburn  // At least one of the headers is there - check them
511254e5c84SBen Coburn  if ($if_none_match && $if_none_match != $etag) {
512254e5c84SBen Coburn    return; // etag is there but doesn't match
513254e5c84SBen Coburn  }
5140ac9a84dSoliver
515254e5c84SBen Coburn  if ($if_modified_since && $if_modified_since != $last_modified) {
516254e5c84SBen Coburn    return; // if-modified-since is there but doesn't match
517254e5c84SBen Coburn  }
5180ac9a84dSoliver
519254e5c84SBen Coburn  // Nothing has changed since their last request - serve a 304 and exit
520254e5c84SBen Coburn  header('HTTP/1.0 304 Not Modified');
521254e5c84SBen Coburn  exit;
522254e5c84SBen Coburn}
523254e5c84SBen Coburn
524b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
525