xref: /dokuwiki/inc/pageutils.php (revision dc2c0e04646a8bbd31f08140918d61d1dda25c2e)
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  }
5542905504SAndreas Gohr  if($clean) $id = cleanID($id);
560868021bSAndreas Gohr  if(empty($id) && $param=='id') $id = $conf['start'];
576c7843b5Sandi
586c7843b5Sandi  return $id;
596c7843b5Sandi}
60b625487dSandi
61*dc2c0e04Schris// function result cache for cleanID
62*dc2c0e04Schrisglobal $cache_cleanid;
63*dc2c0e04Schris$cache_cleanid = array();
64*dc2c0e04Schris
65b625487dSandi/**
66b625487dSandi * Remove unwanted chars from ID
67b625487dSandi *
68b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
69b625487dSandi * converted to unaccented ones
70b625487dSandi *
71b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
726e0cc83aSchris * @param  string  $raw_id    The pageid to clean
738a831f2bSAndreas Gohr * @param  boolean $ascii     Force ASCII
74b625487dSandi */
756e0cc83aSchrisfunction cleanID($raw_id,$ascii=false){
76b625487dSandi  global $conf;
77b625487dSandi  global $lang;
784b5db43bSjoe.lapp  static $sepcharpat = null;
794b5db43bSjoe.lapp
80*dc2c0e04Schris  global $cache_cleanid;
81*dc2c0e04Schris  $cache = & $cache_cleanid;
826e0cc83aSchris
836e0cc83aSchris  // check if it's already in the memory cache
846e0cc83aSchris  if (isset($cache[$raw_id])) {
856e0cc83aSchris    return $cache[$raw_id];
866e0cc83aSchris	}
876e0cc83aSchris
884b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
894b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
904b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
914b5db43bSjoe.lapp
926e0cc83aSchris  $id = trim($raw_id);
93b625487dSandi  $id = utf8_strtolower($id);
94b625487dSandi
95b625487dSandi  //alternative namespace seperator
96b625487dSandi  $id = strtr($id,';',':');
97b625487dSandi  if($conf['useslash']){
98b625487dSandi    $id = strtr($id,'/',':');
99b625487dSandi  }else{
1004eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
101b625487dSandi  }
102b625487dSandi
1038a831f2bSAndreas Gohr  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
1048a831f2bSAndreas Gohr  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
105b625487dSandi
106b625487dSandi  //remove specials
107ad81d431SAndreas Gohr  $id = utf8_stripspecials($id,$sepchar,'\*');
108b625487dSandi
1098a831f2bSAndreas Gohr  if($ascii) $id = utf8_strip($id);
1108a831f2bSAndreas Gohr
111b625487dSandi  //clean up
1124b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
113b625487dSandi  $id = preg_replace('#:+#',':',$id);
114b625487dSandi  $id = trim($id,':._-');
115b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
116b625487dSandi
1176e0cc83aSchris  $cache[$raw_id] = $id;
118b625487dSandi  return($id);
119b625487dSandi}
120b625487dSandi
121b625487dSandi/**
122b625487dSandi * Return namespacepart of a wiki ID
123b625487dSandi *
124b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
125b625487dSandi */
126b625487dSandifunction getNS($id){
127c4e0e4a1SAndreas Gohr  $pos = strrpos($id,':');
128c4e0e4a1SAndreas Gohr  if($pos!==false){
129c4e0e4a1SAndreas Gohr    return substr($id,0,$pos);
130b625487dSandi  }
131b625487dSandi  return false;
132b625487dSandi}
133b625487dSandi
134b625487dSandi/**
135b625487dSandi * Returns the ID without the namespace
136b625487dSandi *
137b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
138b625487dSandi */
139b625487dSandifunction noNS($id) {
1402844584fSBen Coburn  $pos = strrpos($id, ':');
1412844584fSBen Coburn  if ($pos!==false) {
1422844584fSBen Coburn    return substr($id, $pos+1);
1432844584fSBen Coburn  } else {
1442844584fSBen Coburn    return $id;
1452844584fSBen Coburn  }
146b625487dSandi}
147b625487dSandi
148*dc2c0e04Schris// function result cache for wikiFN
149*dc2c0e04Schrisglobal $cache_wikifn;
150*dc2c0e04Schris$cache_wikifn = array();
151*dc2c0e04Schris
152b625487dSandi/**
153b625487dSandi * returns the full path to the datafile specified by ID and
154b625487dSandi * optional revision
155b625487dSandi *
156b625487dSandi * The filename is URL encoded to protect Unicode chars
157b625487dSandi *
158b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
159b625487dSandi */
1606e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){
161b625487dSandi  global $conf;
1626e0cc83aSchris
163*dc2c0e04Schris  global $cache_wikifn;
164*dc2c0e04Schris  $cache = & $cache_wikifn;
165*dc2c0e04Schris
1666e0cc83aSchris  if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) {
1676e0cc83aSchris    return $cache[$raw_id][$rev];
1686e0cc83aSchris  }
1696e0cc83aSchris
1706e0cc83aSchris  $id = $raw_id;
1716e0cc83aSchris
1720d8ea614Schris  if ($clean) $id = cleanID($id);
173b625487dSandi  $id = str_replace(':','/',$id);
174b625487dSandi  if(empty($rev)){
175b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
176b625487dSandi  }else{
177b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
178ff3ed99fSmarcel    if($conf['compression']){
179ff3ed99fSmarcel      //test for extensions here, we want to read both compressions
180ff3ed99fSmarcel       if (file_exists($fn . '.gz')){
181b625487dSandi          $fn .= '.gz';
182ff3ed99fSmarcel       }else if(file_exists($fn . '.bz2')){
183ff3ed99fSmarcel          $fn .= '.bz2';
184ff3ed99fSmarcel       }else{
185ff3ed99fSmarcel          //file doesnt exist yet, so we take the configured extension
186ff3ed99fSmarcel          $fn .= '.' . $conf['compression'];
187ff3ed99fSmarcel       }
188b625487dSandi    }
189b625487dSandi  }
1906e0cc83aSchris
1916e0cc83aSchris  $cache[$raw_id][$rev] = $fn;
192b625487dSandi  return $fn;
193b625487dSandi}
194b625487dSandi
195b625487dSandi/**
196c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
197c9b4bd1eSBen Coburn *
198c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
199c9b4bd1eSBen Coburn */
200c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
201c9b4bd1eSBen Coburn  global $conf;
202662ff478SAndreas Gohr  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
203c9b4bd1eSBen Coburn}
204c9b4bd1eSBen Coburn
205c9b4bd1eSBen Coburn
206c9b4bd1eSBen Coburn/**
2071380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
208b158d625SSteven Danz *
209b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
210b158d625SSteven Danz *
211b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
212b158d625SSteven Danz */
2131380fc45SAndreas Gohrfunction metaFN($id,$ext){
214b158d625SSteven Danz  global $conf;
215b158d625SSteven Danz  $id = cleanID($id);
216b158d625SSteven Danz  $id = str_replace(':','/',$id);
2171380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
218b158d625SSteven Danz  return $fn;
219b158d625SSteven Danz}
220b158d625SSteven Danz
221b158d625SSteven Danz/**
222e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
223e1f3d9e1SEsther Brunner *
224e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
225e1f3d9e1SEsther Brunner */
226e1f3d9e1SEsther Brunnerfunction metaFiles($id){
227e1f3d9e1SEsther Brunner   $name   = noNS($id);
228e1f3d9e1SEsther Brunner   $dir    = metaFN(getNS($id),'');
229e1f3d9e1SEsther Brunner   $files  = array();
230e1f3d9e1SEsther Brunner
231e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
2325011da9dSEsther Brunner   if(!$dh) return $files;
233e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
2341a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
235e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
236e1f3d9e1SEsther Brunner   }
237e1f3d9e1SEsther Brunner   closedir($dh);
238e1f3d9e1SEsther Brunner
239e1f3d9e1SEsther Brunner   return $files;
240e1f3d9e1SEsther Brunner}
241e1f3d9e1SEsther Brunner
242e1f3d9e1SEsther Brunner/**
243b625487dSandi * returns the full path to the mediafile specified by ID
244b625487dSandi *
245b625487dSandi * The filename is URL encoded to protect Unicode chars
246b625487dSandi *
247b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
248b625487dSandi */
249b625487dSandifunction mediaFN($id){
250b625487dSandi  global $conf;
251b625487dSandi  $id = cleanID($id);
252b625487dSandi  $id = str_replace(':','/',$id);
253b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
254b625487dSandi  return $fn;
255b625487dSandi}
256b625487dSandi
257b625487dSandi/**
258b625487dSandi * Returns the full filepath to a localized textfile if local
259b625487dSandi * version isn't found the english one is returned
260b625487dSandi *
261b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
262b625487dSandi */
263b625487dSandifunction localeFN($id){
264b625487dSandi  global $conf;
265bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
266b625487dSandi  if(!@file_exists($file)){
267b625487dSandi    //fall back to english
268bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
269b625487dSandi  }
270b625487dSandi  return $file;
271b625487dSandi}
272b625487dSandi
273b625487dSandi/**
274c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
275c4e0e4a1SAndreas Gohr *
276c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
277c4e0e4a1SAndreas Gohr * instead
278c4e0e4a1SAndreas Gohr *
279c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
280c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
281c4e0e4a1SAndreas Gohr *
282c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
283c4e0e4a1SAndreas Gohr */
284a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
285c4e0e4a1SAndreas Gohr  // if the id starts with a dot we need to handle the
286c4e0e4a1SAndreas Gohr  // relative stuff
287c4e0e4a1SAndreas Gohr  if($id{0} == '.'){
288c4e0e4a1SAndreas Gohr    // normalize initial dots without a colon
289c4e0e4a1SAndreas Gohr    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
290c4e0e4a1SAndreas Gohr    // prepend the current namespace
291c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
292c4e0e4a1SAndreas Gohr
293c4e0e4a1SAndreas Gohr    // cleanup relatives
294c4e0e4a1SAndreas Gohr    $result = array();
295c4e0e4a1SAndreas Gohr    $pathA  = explode(':', $id);
296c4e0e4a1SAndreas Gohr    if (!$pathA[0]) $result[] = '';
297c4e0e4a1SAndreas Gohr    foreach ($pathA AS $key => $dir) {
298c4e0e4a1SAndreas Gohr      if ($dir == '..') {
299c4e0e4a1SAndreas Gohr        if (end($result) == '..') {
300c4e0e4a1SAndreas Gohr          $result[] = '..';
301c4e0e4a1SAndreas Gohr        } elseif (!array_pop($result)) {
302c4e0e4a1SAndreas Gohr          $result[] = '..';
303c4e0e4a1SAndreas Gohr        }
304c4e0e4a1SAndreas Gohr      } elseif ($dir && $dir != '.') {
305c4e0e4a1SAndreas Gohr        $result[] = $dir;
306c4e0e4a1SAndreas Gohr      }
307c4e0e4a1SAndreas Gohr    }
308c4e0e4a1SAndreas Gohr    if (!end($pathA)) $result[] = '';
309c4e0e4a1SAndreas Gohr    $id = implode(':', $result);
310c4e0e4a1SAndreas Gohr  }elseif($ns !== false && strpos($id,':') === false){
311c4e0e4a1SAndreas Gohr    //if link contains no namespace. add current namespace (if any)
312c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
313c4e0e4a1SAndreas Gohr  }
314c4e0e4a1SAndreas Gohr
315a6ef4796SAndreas Gohr  if($clean) $id = cleanID($id);
316a6ef4796SAndreas Gohr  return $id;
317c4e0e4a1SAndreas Gohr}
318c4e0e4a1SAndreas Gohr
319c4e0e4a1SAndreas Gohr/**
320b625487dSandi * Returns a full media id
321b625487dSandi *
322b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
323b625487dSandi */
32437e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
325c4e0e4a1SAndreas Gohr  $page   = resolve_id($ns,$page);
326b625487dSandi  $file   = mediaFN($page);
327b625487dSandi  $exists = @file_exists($file);
328b625487dSandi}
329b625487dSandi
330b625487dSandi/**
331b625487dSandi * Returns a full page id
332b625487dSandi *
333b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
334b625487dSandi */
33537e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
336b625487dSandi  global $conf;
3370b7c14c2Sandi  $exists = false;
338b625487dSandi
339b625487dSandi  //keep hashlink if exists then clean both parts
34003c4aec3Schris  if (strpos($page,'#')) {
341b625487dSandi    list($page,$hash) = split('#',$page,2);
34203c4aec3Schris  } else {
34303c4aec3Schris    $hash = '';
34403c4aec3Schris  }
345b625487dSandi  $hash = cleanID($hash);
346a6ef4796SAndreas Gohr  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
347b625487dSandi
348a6ef4796SAndreas Gohr  // get filename (calls clean itself)
349b625487dSandi  $file = wikiFN($page);
350b625487dSandi
351a6ef4796SAndreas Gohr  // if ends with colon we have a namespace link
352a6ef4796SAndreas Gohr  if(substr($page,-1) == ':'){
353a6ef4796SAndreas Gohr    if(@file_exists(wikiFN($page.$conf['start']))){
354a6ef4796SAndreas Gohr      // start page inside namespace
355a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
356a6ef4796SAndreas Gohr      $exists = true;
357a6ef4796SAndreas Gohr    }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){
358a6ef4796SAndreas Gohr      // page named like the NS inside the NS
359a6ef4796SAndreas Gohr      $page = $page.noNS(cleanID($page));
360a6ef4796SAndreas Gohr      $exists = true;
361a6ef4796SAndreas Gohr    }elseif(@file_exists(wikiFN($page))){
362a6ef4796SAndreas Gohr      // page like namespace exists
363a6ef4796SAndreas Gohr      $page = $page;
364a6ef4796SAndreas Gohr      $exists = true;
365a6ef4796SAndreas Gohr    }else{
366a6ef4796SAndreas Gohr      // fall back to default
367a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
368a6ef4796SAndreas Gohr    }
369a6ef4796SAndreas Gohr  }else{
370b625487dSandi    //check alternative plural/nonplural form
371b625487dSandi    if(!@file_exists($file)){
372b625487dSandi      if( $conf['autoplural'] ){
373b625487dSandi        if(substr($page,-1) == 's'){
374b625487dSandi          $try = substr($page,0,-1);
375b625487dSandi        }else{
376b625487dSandi          $try = $page.'s';
377b625487dSandi        }
378b625487dSandi        if(@file_exists(wikiFN($try))){
379b625487dSandi          $page   = $try;
380b625487dSandi          $exists = true;
381b625487dSandi        }
382b625487dSandi      }
383b625487dSandi    }else{
384b625487dSandi      $exists = true;
385b625487dSandi    }
386a6ef4796SAndreas Gohr  }
387a6ef4796SAndreas Gohr
388a6ef4796SAndreas Gohr  // now make sure we have a clean page
389a6ef4796SAndreas Gohr  $page = cleanID($page);
390b625487dSandi
391b625487dSandi  //add hash if any
392b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
393b625487dSandi}
394b625487dSandi
39598407a7aSandi/**
39698407a7aSandi * Returns the name of a cachefile from given data
39798407a7aSandi *
39898407a7aSandi * The needed directory is created by this function!
39998407a7aSandi *
40098407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
40198407a7aSandi *
40298407a7aSandi * @param string $data  This data is used to create a unique md5 name
40398407a7aSandi * @param string $ext   This is appended to the filename if given
40498407a7aSandi * @return string       The filename of the cachefile
40598407a7aSandi */
40698407a7aSandifunction getCacheName($data,$ext=''){
40798407a7aSandi  global $conf;
40898407a7aSandi  $md5  = md5($data);
40998407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
41098407a7aSandi  io_makeFileDir($file);
41198407a7aSandi  return $file;
41298407a7aSandi}
41398407a7aSandi
4140dc92c6fSAndreas Gohr/**
4150dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
4160dc92c6fSAndreas Gohr *
4170dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4180dc92c6fSAndreas Gohr */
4190dc92c6fSAndreas Gohrfunction isHiddenPage($id){
4200dc92c6fSAndreas Gohr  global $conf;
4210dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
4220dc92c6fSAndreas Gohr
4230dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
4240dc92c6fSAndreas Gohr    return true;
4250dc92c6fSAndreas Gohr  }
4260dc92c6fSAndreas Gohr  return false;
4270dc92c6fSAndreas Gohr}
4280dc92c6fSAndreas Gohr
4290dc92c6fSAndreas Gohr/**
4300dc92c6fSAndreas Gohr * Reverse of isHiddenPage
4310dc92c6fSAndreas Gohr *
4320dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4330dc92c6fSAndreas Gohr */
4340dc92c6fSAndreas Gohrfunction isVisiblePage($id){
4350dc92c6fSAndreas Gohr  return !isHiddenPage($id);
4360dc92c6fSAndreas Gohr}
4370dc92c6fSAndreas Gohr
438254e5c84SBen Coburn/**
439254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests
440254e5c84SBen Coburn *
441254e5c84SBen Coburn * @author   Simon Willison <swillison@gmail.com>
442254e5c84SBen Coburn * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
4430ac9a84dSoliver * @param    timestamp $timestamp lastmodified time of the cache file
4440ac9a84dSoliver * @returns  void or void with previously header() commands executed
445254e5c84SBen Coburn */
446254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){
447254e5c84SBen Coburn  // A PHP implementation of conditional get, see
448254e5c84SBen Coburn  //   http://fishbowl.pastiche.org/archives/001132.html
449254e5c84SBen Coburn  $last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
450254e5c84SBen Coburn  $etag = '"'.md5($last_modified).'"';
451254e5c84SBen Coburn  // Send the headers
452254e5c84SBen Coburn  header("Last-Modified: $last_modified");
453254e5c84SBen Coburn  header("ETag: $etag");
454254e5c84SBen Coburn  // See if the client has provided the required headers
4550ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
4560ac9a84dSoliver    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
4570ac9a84dSoliver  }else{
4580ac9a84dSoliver    $if_modified_since = false;
4590ac9a84dSoliver  }
4600ac9a84dSoliver
4610ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
4620ac9a84dSoliver    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
4630ac9a84dSoliver  }else{
4640ac9a84dSoliver    $if_none_match = false;
4650ac9a84dSoliver  }
4660ac9a84dSoliver
467254e5c84SBen Coburn  if (!$if_modified_since && !$if_none_match){
468254e5c84SBen Coburn    return;
469254e5c84SBen Coburn  }
4700ac9a84dSoliver
471254e5c84SBen Coburn  // At least one of the headers is there - check them
472254e5c84SBen Coburn  if ($if_none_match && $if_none_match != $etag) {
473254e5c84SBen Coburn    return; // etag is there but doesn't match
474254e5c84SBen Coburn  }
4750ac9a84dSoliver
476254e5c84SBen Coburn  if ($if_modified_since && $if_modified_since != $last_modified) {
477254e5c84SBen Coburn    return; // if-modified-since is there but doesn't match
478254e5c84SBen Coburn  }
4790ac9a84dSoliver
480254e5c84SBen Coburn  // Nothing has changed since their last request - serve a 304 and exit
481254e5c84SBen Coburn  header('HTTP/1.0 304 Not Modified');
482254e5c84SBen Coburn  exit;
483254e5c84SBen Coburn}
484254e5c84SBen Coburn
485b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
486