xref: /dokuwiki/inc/pageutils.php (revision ff3ed99f17e6204cc4f6331830c53a084f385d9a)
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
61b625487dSandi/**
62b625487dSandi * Remove unwanted chars from ID
63b625487dSandi *
64b625487dSandi * Cleans a given ID to only use allowed characters. Accented characters are
65b625487dSandi * converted to unaccented ones
66b625487dSandi *
67b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
688a831f2bSAndreas Gohr * @param  string  $id    The pageid to clean
698a831f2bSAndreas Gohr * @param  boolean $ascii Force ASCII
70b625487dSandi */
718a831f2bSAndreas Gohrfunction cleanID($id,$ascii=false){
72b625487dSandi  global $conf;
73b625487dSandi  global $lang;
744b5db43bSjoe.lapp  static $sepcharpat = null;
754b5db43bSjoe.lapp
764b5db43bSjoe.lapp  $sepchar = $conf['sepchar'];
774b5db43bSjoe.lapp  if($sepcharpat == null) // build string only once to save clock cycles
784b5db43bSjoe.lapp    $sepcharpat = '#\\'.$sepchar.'+#';
794b5db43bSjoe.lapp
80b625487dSandi  $id = trim($id);
81b625487dSandi  $id = utf8_strtolower($id);
82b625487dSandi
83b625487dSandi  //alternative namespace seperator
84b625487dSandi  $id = strtr($id,';',':');
85b625487dSandi  if($conf['useslash']){
86b625487dSandi    $id = strtr($id,'/',':');
87b625487dSandi  }else{
884eeffcd2SAndreas Gohr    $id = strtr($id,'/',$sepchar);
89b625487dSandi  }
90b625487dSandi
918a831f2bSAndreas Gohr  if($conf['deaccent'] == 2 || $ascii) $id = utf8_romanize($id);
928a831f2bSAndreas Gohr  if($conf['deaccent'] || $ascii) $id = utf8_deaccent($id,-1);
93b625487dSandi
94b625487dSandi  //remove specials
95ad81d431SAndreas Gohr  $id = utf8_stripspecials($id,$sepchar,'\*');
96b625487dSandi
978a831f2bSAndreas Gohr  if($ascii) $id = utf8_strip($id);
988a831f2bSAndreas Gohr
99b625487dSandi  //clean up
1004b5db43bSjoe.lapp  $id = preg_replace($sepcharpat,$sepchar,$id);
101b625487dSandi  $id = preg_replace('#:+#',':',$id);
102b625487dSandi  $id = trim($id,':._-');
103b625487dSandi  $id = preg_replace('#:[:\._\-]+#',':',$id);
104b625487dSandi
105b625487dSandi  return($id);
106b625487dSandi}
107b625487dSandi
108b625487dSandi/**
109b625487dSandi * Return namespacepart of a wiki ID
110b625487dSandi *
111b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
112b625487dSandi */
113b625487dSandifunction getNS($id){
114c4e0e4a1SAndreas Gohr  $pos = strrpos($id,':');
115c4e0e4a1SAndreas Gohr  if($pos!==false){
116c4e0e4a1SAndreas Gohr    return substr($id,0,$pos);
117b625487dSandi  }
118b625487dSandi  return false;
119b625487dSandi}
120b625487dSandi
121b625487dSandi/**
122b625487dSandi * Returns the ID without the namespace
123b625487dSandi *
124b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
125b625487dSandi */
126b625487dSandifunction noNS($id) {
1272844584fSBen Coburn  $pos = strrpos($id, ':');
1282844584fSBen Coburn  if ($pos!==false) {
1292844584fSBen Coburn    return substr($id, $pos+1);
1302844584fSBen Coburn  } else {
1312844584fSBen Coburn    return $id;
1322844584fSBen Coburn  }
133b625487dSandi}
134b625487dSandi
135b625487dSandi/**
136b625487dSandi * returns the full path to the datafile specified by ID and
137b625487dSandi * optional revision
138b625487dSandi *
139b625487dSandi * The filename is URL encoded to protect Unicode chars
140b625487dSandi *
141b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
142b625487dSandi */
143b625487dSandifunction wikiFN($id,$rev=''){
144b625487dSandi  global $conf;
145b625487dSandi  $id = cleanID($id);
146b625487dSandi  $id = str_replace(':','/',$id);
147b625487dSandi  if(empty($rev)){
148b625487dSandi    $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt';
149b625487dSandi  }else{
150b625487dSandi    $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt';
151*ff3ed99fSmarcel    if($conf['compression']){
152*ff3ed99fSmarcel      //test for extensions here, we want to read both compressions
153*ff3ed99fSmarcel       if (file_exists($fn . '.gz')){
154b625487dSandi          $fn .= '.gz';
155*ff3ed99fSmarcel       }else if(file_exists($fn . '.bz2')){
156*ff3ed99fSmarcel          $fn .= '.bz2';
157*ff3ed99fSmarcel       }else{
158*ff3ed99fSmarcel          //file doesnt exist yet, so we take the configured extension
159*ff3ed99fSmarcel          $fn .= '.' . $conf['compression'];
160*ff3ed99fSmarcel       }
161b625487dSandi    }
162b625487dSandi  }
163b625487dSandi  return $fn;
164b625487dSandi}
165b625487dSandi
166b625487dSandi/**
167c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing.
168c9b4bd1eSBen Coburn *
169c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
170c9b4bd1eSBen Coburn */
171c9b4bd1eSBen Coburnfunction wikiLockFN($id) {
172c9b4bd1eSBen Coburn  global $conf;
173662ff478SAndreas Gohr  return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock';
174c9b4bd1eSBen Coburn}
175c9b4bd1eSBen Coburn
176c9b4bd1eSBen Coburn
177c9b4bd1eSBen Coburn/**
1781380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension
179b158d625SSteven Danz *
180b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars
181b158d625SSteven Danz *
182b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com>
183b158d625SSteven Danz */
1841380fc45SAndreas Gohrfunction metaFN($id,$ext){
185b158d625SSteven Danz  global $conf;
186b158d625SSteven Danz  $id = cleanID($id);
187b158d625SSteven Danz  $id = str_replace(':','/',$id);
1881380fc45SAndreas Gohr  $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext;
189b158d625SSteven Danz  return $fn;
190b158d625SSteven Danz}
191b158d625SSteven Danz
192b158d625SSteven Danz/**
193e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID
194e1f3d9e1SEsther Brunner *
195e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch>
196e1f3d9e1SEsther Brunner */
197e1f3d9e1SEsther Brunnerfunction metaFiles($id){
198e1f3d9e1SEsther Brunner   $name   = noNS($id);
199e1f3d9e1SEsther Brunner   $dir    = metaFN(getNS($id),'');
200e1f3d9e1SEsther Brunner   $files  = array();
201e1f3d9e1SEsther Brunner
202e1f3d9e1SEsther Brunner   $dh = @opendir($dir);
2035011da9dSEsther Brunner   if(!$dh) return $files;
204e1f3d9e1SEsther Brunner   while(($file = readdir($dh)) !== false){
2051a54dfabSEsther Brunner     if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file))
206e1f3d9e1SEsther Brunner       $files[] = $dir.$file;
207e1f3d9e1SEsther Brunner   }
208e1f3d9e1SEsther Brunner   closedir($dh);
209e1f3d9e1SEsther Brunner
210e1f3d9e1SEsther Brunner   return $files;
211e1f3d9e1SEsther Brunner}
212e1f3d9e1SEsther Brunner
213e1f3d9e1SEsther Brunner/**
214b625487dSandi * returns the full path to the mediafile specified by ID
215b625487dSandi *
216b625487dSandi * The filename is URL encoded to protect Unicode chars
217b625487dSandi *
218b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
219b625487dSandi */
220b625487dSandifunction mediaFN($id){
221b625487dSandi  global $conf;
222b625487dSandi  $id = cleanID($id);
223b625487dSandi  $id = str_replace(':','/',$id);
224b625487dSandi    $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
225b625487dSandi  return $fn;
226b625487dSandi}
227b625487dSandi
228b625487dSandi/**
229b625487dSandi * Returns the full filepath to a localized textfile if local
230b625487dSandi * version isn't found the english one is returned
231b625487dSandi *
232b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
233b625487dSandi */
234b625487dSandifunction localeFN($id){
235b625487dSandi  global $conf;
236bc3b6aecSandi  $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt';
237b625487dSandi  if(!@file_exists($file)){
238b625487dSandi    //fall back to english
239bc3b6aecSandi    $file = DOKU_INC.'inc/lang/en/'.$id.'.txt';
240b625487dSandi  }
241b625487dSandi  return $file;
242b625487dSandi}
243b625487dSandi
244b625487dSandi/**
245c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs
246c4e0e4a1SAndreas Gohr *
247c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid
248c4e0e4a1SAndreas Gohr * instead
249c4e0e4a1SAndreas Gohr *
250c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at
251c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016
252c4e0e4a1SAndreas Gohr *
253c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl>
254c4e0e4a1SAndreas Gohr */
255a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){
256c4e0e4a1SAndreas Gohr  // if the id starts with a dot we need to handle the
257c4e0e4a1SAndreas Gohr  // relative stuff
258c4e0e4a1SAndreas Gohr  if($id{0} == '.'){
259c4e0e4a1SAndreas Gohr    // normalize initial dots without a colon
260c4e0e4a1SAndreas Gohr    $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id);
261c4e0e4a1SAndreas Gohr    // prepend the current namespace
262c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
263c4e0e4a1SAndreas Gohr
264c4e0e4a1SAndreas Gohr    // cleanup relatives
265c4e0e4a1SAndreas Gohr    $result = array();
266c4e0e4a1SAndreas Gohr    $pathA  = explode(':', $id);
267c4e0e4a1SAndreas Gohr    if (!$pathA[0]) $result[] = '';
268c4e0e4a1SAndreas Gohr    foreach ($pathA AS $key => $dir) {
269c4e0e4a1SAndreas Gohr      if ($dir == '..') {
270c4e0e4a1SAndreas Gohr        if (end($result) == '..') {
271c4e0e4a1SAndreas Gohr          $result[] = '..';
272c4e0e4a1SAndreas Gohr        } elseif (!array_pop($result)) {
273c4e0e4a1SAndreas Gohr          $result[] = '..';
274c4e0e4a1SAndreas Gohr        }
275c4e0e4a1SAndreas Gohr      } elseif ($dir && $dir != '.') {
276c4e0e4a1SAndreas Gohr        $result[] = $dir;
277c4e0e4a1SAndreas Gohr      }
278c4e0e4a1SAndreas Gohr    }
279c4e0e4a1SAndreas Gohr    if (!end($pathA)) $result[] = '';
280c4e0e4a1SAndreas Gohr    $id = implode(':', $result);
281c4e0e4a1SAndreas Gohr  }elseif($ns !== false && strpos($id,':') === false){
282c4e0e4a1SAndreas Gohr    //if link contains no namespace. add current namespace (if any)
283c4e0e4a1SAndreas Gohr    $id = $ns.':'.$id;
284c4e0e4a1SAndreas Gohr  }
285c4e0e4a1SAndreas Gohr
286a6ef4796SAndreas Gohr  if($clean) $id = cleanID($id);
287a6ef4796SAndreas Gohr  return $id;
288c4e0e4a1SAndreas Gohr}
289c4e0e4a1SAndreas Gohr
290c4e0e4a1SAndreas Gohr/**
291b625487dSandi * Returns a full media id
292b625487dSandi *
293b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
294b625487dSandi */
29537e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){
296c4e0e4a1SAndreas Gohr  $page   = resolve_id($ns,$page);
297b625487dSandi  $file   = mediaFN($page);
298b625487dSandi  $exists = @file_exists($file);
299b625487dSandi}
300b625487dSandi
301b625487dSandi/**
302b625487dSandi * Returns a full page id
303b625487dSandi *
304b625487dSandi * @author Andreas Gohr <andi@splitbrain.org>
305b625487dSandi */
30637e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){
307b625487dSandi  global $conf;
3080b7c14c2Sandi  $exists = false;
309b625487dSandi
310b625487dSandi  //keep hashlink if exists then clean both parts
31103c4aec3Schris  if (strpos($page,'#')) {
312b625487dSandi    list($page,$hash) = split('#',$page,2);
31303c4aec3Schris  } else {
31403c4aec3Schris    $hash = '';
31503c4aec3Schris  }
316b625487dSandi  $hash = cleanID($hash);
317a6ef4796SAndreas Gohr  $page = resolve_id($ns,$page,false); // resolve but don't clean, yet
318b625487dSandi
319a6ef4796SAndreas Gohr  // get filename (calls clean itself)
320b625487dSandi  $file = wikiFN($page);
321b625487dSandi
322a6ef4796SAndreas Gohr  // if ends with colon we have a namespace link
323a6ef4796SAndreas Gohr  if(substr($page,-1) == ':'){
324a6ef4796SAndreas Gohr    if(@file_exists(wikiFN($page.$conf['start']))){
325a6ef4796SAndreas Gohr      // start page inside namespace
326a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
327a6ef4796SAndreas Gohr      $exists = true;
328a6ef4796SAndreas Gohr    }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){
329a6ef4796SAndreas Gohr      // page named like the NS inside the NS
330a6ef4796SAndreas Gohr      $page = $page.noNS(cleanID($page));
331a6ef4796SAndreas Gohr      $exists = true;
332a6ef4796SAndreas Gohr    }elseif(@file_exists(wikiFN($page))){
333a6ef4796SAndreas Gohr      // page like namespace exists
334a6ef4796SAndreas Gohr      $page = $page;
335a6ef4796SAndreas Gohr      $exists = true;
336a6ef4796SAndreas Gohr    }else{
337a6ef4796SAndreas Gohr      // fall back to default
338a6ef4796SAndreas Gohr      $page = $page.$conf['start'];
339a6ef4796SAndreas Gohr    }
340a6ef4796SAndreas Gohr  }else{
341b625487dSandi    //check alternative plural/nonplural form
342b625487dSandi    if(!@file_exists($file)){
343b625487dSandi      if( $conf['autoplural'] ){
344b625487dSandi        if(substr($page,-1) == 's'){
345b625487dSandi          $try = substr($page,0,-1);
346b625487dSandi        }else{
347b625487dSandi          $try = $page.'s';
348b625487dSandi        }
349b625487dSandi        if(@file_exists(wikiFN($try))){
350b625487dSandi          $page   = $try;
351b625487dSandi          $exists = true;
352b625487dSandi        }
353b625487dSandi      }
354b625487dSandi    }else{
355b625487dSandi      $exists = true;
356b625487dSandi    }
357a6ef4796SAndreas Gohr  }
358a6ef4796SAndreas Gohr
359a6ef4796SAndreas Gohr  // now make sure we have a clean page
360a6ef4796SAndreas Gohr  $page = cleanID($page);
361b625487dSandi
362b625487dSandi  //add hash if any
363b2d7d3f2Sandi  if(!empty($hash)) $page .= '#'.$hash;
364b625487dSandi}
365b625487dSandi
36698407a7aSandi/**
36798407a7aSandi * Returns the name of a cachefile from given data
36898407a7aSandi *
36998407a7aSandi * The needed directory is created by this function!
37098407a7aSandi *
37198407a7aSandi * @author Andreas Gohr <andi@splitbrain.org>
37298407a7aSandi *
37398407a7aSandi * @param string $data  This data is used to create a unique md5 name
37498407a7aSandi * @param string $ext   This is appended to the filename if given
37598407a7aSandi * @return string       The filename of the cachefile
37698407a7aSandi */
37798407a7aSandifunction getCacheName($data,$ext=''){
37898407a7aSandi  global $conf;
37998407a7aSandi  $md5  = md5($data);
38098407a7aSandi  $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext;
38198407a7aSandi  io_makeFileDir($file);
38298407a7aSandi  return $file;
38398407a7aSandi}
38498407a7aSandi
3850dc92c6fSAndreas Gohr/**
3860dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages']
3870dc92c6fSAndreas Gohr *
3880dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
3890dc92c6fSAndreas Gohr */
3900dc92c6fSAndreas Gohrfunction isHiddenPage($id){
3910dc92c6fSAndreas Gohr  global $conf;
3920dc92c6fSAndreas Gohr  if(empty($conf['hidepages'])) return false;
3930dc92c6fSAndreas Gohr
3940dc92c6fSAndreas Gohr  if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){
3950dc92c6fSAndreas Gohr    return true;
3960dc92c6fSAndreas Gohr  }
3970dc92c6fSAndreas Gohr  return false;
3980dc92c6fSAndreas Gohr}
3990dc92c6fSAndreas Gohr
4000dc92c6fSAndreas Gohr/**
4010dc92c6fSAndreas Gohr * Reverse of isHiddenPage
4020dc92c6fSAndreas Gohr *
4030dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de>
4040dc92c6fSAndreas Gohr */
4050dc92c6fSAndreas Gohrfunction isVisiblePage($id){
4060dc92c6fSAndreas Gohr  return !isHiddenPage($id);
4070dc92c6fSAndreas Gohr}
4080dc92c6fSAndreas Gohr
409254e5c84SBen Coburn/**
410254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests
411254e5c84SBen Coburn *
412254e5c84SBen Coburn * @author   Simon Willison <swillison@gmail.com>
413254e5c84SBen Coburn * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
4140ac9a84dSoliver * @param    timestamp $timestamp lastmodified time of the cache file
4150ac9a84dSoliver * @returns  void or void with previously header() commands executed
416254e5c84SBen Coburn */
417254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){
418254e5c84SBen Coburn  // A PHP implementation of conditional get, see
419254e5c84SBen Coburn  //   http://fishbowl.pastiche.org/archives/001132.html
420254e5c84SBen Coburn  $last_modified = substr(date('r', $timestamp), 0, -5).'GMT';
421254e5c84SBen Coburn  $etag = '"'.md5($last_modified).'"';
422254e5c84SBen Coburn  // Send the headers
423254e5c84SBen Coburn  header("Last-Modified: $last_modified");
424254e5c84SBen Coburn  header("ETag: $etag");
425254e5c84SBen Coburn  // See if the client has provided the required headers
4260ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
4270ac9a84dSoliver    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
4280ac9a84dSoliver  }else{
4290ac9a84dSoliver    $if_modified_since = false;
4300ac9a84dSoliver  }
4310ac9a84dSoliver
4320ac9a84dSoliver  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
4330ac9a84dSoliver    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
4340ac9a84dSoliver  }else{
4350ac9a84dSoliver    $if_none_match = false;
4360ac9a84dSoliver  }
4370ac9a84dSoliver
438254e5c84SBen Coburn  if (!$if_modified_since && !$if_none_match){
439254e5c84SBen Coburn    return;
440254e5c84SBen Coburn  }
4410ac9a84dSoliver
442254e5c84SBen Coburn  // At least one of the headers is there - check them
443254e5c84SBen Coburn  if ($if_none_match && $if_none_match != $etag) {
444254e5c84SBen Coburn    return; // etag is there but doesn't match
445254e5c84SBen Coburn  }
4460ac9a84dSoliver
447254e5c84SBen Coburn  if ($if_modified_since && $if_modified_since != $last_modified) {
448254e5c84SBen Coburn    return; // if-modified-since is there but doesn't match
449254e5c84SBen Coburn  }
4500ac9a84dSoliver
451254e5c84SBen Coburn  // Nothing has changed since their last request - serve a 304 and exit
452254e5c84SBen Coburn  header('HTTP/1.0 304 Not Modified');
453254e5c84SBen Coburn  exit;
454254e5c84SBen Coburn}
455254e5c84SBen Coburn
456b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 :
457