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 } 55*671a58a6SGuy Brand 56*671a58a6SGuy Brand // Namespace autolinking from URL 57*671a58a6SGuy Brand if(substr($id,-1) == ':' || ($conf['useslash'] && substr($id,-1) == '/')){ 58*671a58a6SGuy Brand if(@file_exists(wikiFN($id.$conf['start']))){ 59*671a58a6SGuy Brand // start page inside namespace 60*671a58a6SGuy Brand $id = $id.$conf['start']; 61*671a58a6SGuy Brand }elseif(@file_exists(wikiFN($id.noNS(cleanID($id))))){ 62*671a58a6SGuy Brand // page named like the NS inside the NS 63*671a58a6SGuy Brand $id = $id.noNS(cleanID($id)); 64*671a58a6SGuy Brand }elseif(@file_exists(wikiFN($id))){ 65*671a58a6SGuy Brand // page like namespace exists 66*671a58a6SGuy Brand $id = $id; 67*671a58a6SGuy Brand }else{ 68*671a58a6SGuy Brand // fall back to default 69*671a58a6SGuy Brand $id = $id.$conf['start']; 70*671a58a6SGuy Brand } 71*671a58a6SGuy Brand header("Location: ".wl($id,'',true)); 72*671a58a6SGuy Brand } 73*671a58a6SGuy 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 } 161b625487dSandi} 162b625487dSandi 163b625487dSandi/** 164b625487dSandi * returns the full path to the datafile specified by ID and 165b625487dSandi * optional revision 166b625487dSandi * 167b625487dSandi * The filename is URL encoded to protect Unicode chars 168b625487dSandi * 169b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 170b625487dSandi */ 1716e0cc83aSchrisfunction wikiFN($raw_id,$rev='',$clean=true){ 172b625487dSandi global $conf; 1736e0cc83aSchris 174dc2c0e04Schris global $cache_wikifn; 175dc2c0e04Schris $cache = & $cache_wikifn; 176dc2c0e04Schris 1776e0cc83aSchris if (isset($cache[$raw_id]) && isset($cache[$raw_id][$rev])) { 1786e0cc83aSchris return $cache[$raw_id][$rev]; 1796e0cc83aSchris } 1806e0cc83aSchris 1816e0cc83aSchris $id = $raw_id; 1826e0cc83aSchris 1830d8ea614Schris if ($clean) $id = cleanID($id); 184b625487dSandi $id = str_replace(':','/',$id); 185b625487dSandi if(empty($rev)){ 186b625487dSandi $fn = $conf['datadir'].'/'.utf8_encodeFN($id).'.txt'; 187b625487dSandi }else{ 188b625487dSandi $fn = $conf['olddir'].'/'.utf8_encodeFN($id).'.'.$rev.'.txt'; 189ff3ed99fSmarcel if($conf['compression']){ 190ff3ed99fSmarcel //test for extensions here, we want to read both compressions 191d8186216SBen Coburn if (@file_exists($fn . '.gz')){ 192b625487dSandi $fn .= '.gz'; 193d8186216SBen Coburn }else if(@file_exists($fn . '.bz2')){ 194ff3ed99fSmarcel $fn .= '.bz2'; 195ff3ed99fSmarcel }else{ 196ff3ed99fSmarcel //file doesnt exist yet, so we take the configured extension 197ff3ed99fSmarcel $fn .= '.' . $conf['compression']; 198ff3ed99fSmarcel } 199b625487dSandi } 200b625487dSandi } 2016e0cc83aSchris 20250602150SBen Coburn if (!isset($cache[$raw_id])) { $cache[$raw_id] = array(); } 2036e0cc83aSchris $cache[$raw_id][$rev] = $fn; 204b625487dSandi return $fn; 205b625487dSandi} 206b625487dSandi 207b625487dSandi/** 208c9b4bd1eSBen Coburn * Returns the full path to the file for locking the page while editing. 209c9b4bd1eSBen Coburn * 210c9b4bd1eSBen Coburn * @author Ben Coburn <btcoburn@silicodon.net> 211c9b4bd1eSBen Coburn */ 212c9b4bd1eSBen Coburnfunction wikiLockFN($id) { 213c9b4bd1eSBen Coburn global $conf; 214662ff478SAndreas Gohr return $conf['lockdir'].'/'.md5(cleanID($id)).'.lock'; 215c9b4bd1eSBen Coburn} 216c9b4bd1eSBen Coburn 217c9b4bd1eSBen Coburn 218c9b4bd1eSBen Coburn/** 2191380fc45SAndreas Gohr * returns the full path to the meta file specified by ID and extension 220b158d625SSteven Danz * 221b158d625SSteven Danz * The filename is URL encoded to protect Unicode chars 222b158d625SSteven Danz * 223b158d625SSteven Danz * @author Steven Danz <steven-danz@kc.rr.com> 224b158d625SSteven Danz */ 2251380fc45SAndreas Gohrfunction metaFN($id,$ext){ 226b158d625SSteven Danz global $conf; 227b158d625SSteven Danz $id = cleanID($id); 228b158d625SSteven Danz $id = str_replace(':','/',$id); 2291380fc45SAndreas Gohr $fn = $conf['metadir'].'/'.utf8_encodeFN($id).$ext; 230b158d625SSteven Danz return $fn; 231b158d625SSteven Danz} 232b158d625SSteven Danz 233b158d625SSteven Danz/** 234e1f3d9e1SEsther Brunner * returns an array of full paths to all metafiles of a given ID 235e1f3d9e1SEsther Brunner * 236e1f3d9e1SEsther Brunner * @author Esther Brunner <esther@kaffeehaus.ch> 237e1f3d9e1SEsther Brunner */ 238e1f3d9e1SEsther Brunnerfunction metaFiles($id){ 239e1f3d9e1SEsther Brunner $name = noNS($id); 240e1f3d9e1SEsther Brunner $dir = metaFN(getNS($id),''); 241e1f3d9e1SEsther Brunner $files = array(); 242e1f3d9e1SEsther Brunner 243e1f3d9e1SEsther Brunner $dh = @opendir($dir); 2445011da9dSEsther Brunner if(!$dh) return $files; 245e1f3d9e1SEsther Brunner while(($file = readdir($dh)) !== false){ 2461a54dfabSEsther Brunner if(strpos($file,$name.'.') === 0 && !is_dir($dir.$file)) 247e1f3d9e1SEsther Brunner $files[] = $dir.$file; 248e1f3d9e1SEsther Brunner } 249e1f3d9e1SEsther Brunner closedir($dh); 250e1f3d9e1SEsther Brunner 251e1f3d9e1SEsther Brunner return $files; 252e1f3d9e1SEsther Brunner} 253e1f3d9e1SEsther Brunner 254e1f3d9e1SEsther Brunner/** 255b625487dSandi * returns the full path to the mediafile specified by ID 256b625487dSandi * 257b625487dSandi * The filename is URL encoded to protect Unicode chars 258b625487dSandi * 259b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 260b625487dSandi */ 261b625487dSandifunction mediaFN($id){ 262b625487dSandi global $conf; 263b625487dSandi $id = cleanID($id); 264b625487dSandi $id = str_replace(':','/',$id); 265b625487dSandi $fn = $conf['mediadir'].'/'.utf8_encodeFN($id); 266b625487dSandi return $fn; 267b625487dSandi} 268b625487dSandi 269b625487dSandi/** 270b625487dSandi * Returns the full filepath to a localized textfile if local 271b625487dSandi * version isn't found the english one is returned 272b625487dSandi * 273b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 274b625487dSandi */ 275b625487dSandifunction localeFN($id){ 276b625487dSandi global $conf; 277bc3b6aecSandi $file = DOKU_INC.'inc/lang/'.$conf['lang'].'/'.$id.'.txt'; 278b625487dSandi if(!@file_exists($file)){ 279b625487dSandi //fall back to english 280bc3b6aecSandi $file = DOKU_INC.'inc/lang/en/'.$id.'.txt'; 281b625487dSandi } 282b625487dSandi return $file; 283b625487dSandi} 284b625487dSandi 285b625487dSandi/** 286c4e0e4a1SAndreas Gohr * Resolve relative paths in IDs 287c4e0e4a1SAndreas Gohr * 288c4e0e4a1SAndreas Gohr * Do not call directly use resolve_mediaid or resolve_pageid 289c4e0e4a1SAndreas Gohr * instead 290c4e0e4a1SAndreas Gohr * 291c4e0e4a1SAndreas Gohr * Partyly based on a cleanPath function found at 292c4e0e4a1SAndreas Gohr * http://www.php.net/manual/en/function.realpath.php#57016 293c4e0e4a1SAndreas Gohr * 294c4e0e4a1SAndreas Gohr * @author <bart at mediawave dot nl> 295c4e0e4a1SAndreas Gohr */ 296a6ef4796SAndreas Gohrfunction resolve_id($ns,$id,$clean=true){ 297c4e0e4a1SAndreas Gohr // if the id starts with a dot we need to handle the 298c4e0e4a1SAndreas Gohr // relative stuff 299c4e0e4a1SAndreas Gohr if($id{0} == '.'){ 300c4e0e4a1SAndreas Gohr // normalize initial dots without a colon 301c4e0e4a1SAndreas Gohr $id = preg_replace('/^(\.+)(?=[^:\.])/','\1:',$id); 302c4e0e4a1SAndreas Gohr // prepend the current namespace 303c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 304c4e0e4a1SAndreas Gohr 305c4e0e4a1SAndreas Gohr // cleanup relatives 306c4e0e4a1SAndreas Gohr $result = array(); 307c4e0e4a1SAndreas Gohr $pathA = explode(':', $id); 308c4e0e4a1SAndreas Gohr if (!$pathA[0]) $result[] = ''; 309c4e0e4a1SAndreas Gohr foreach ($pathA AS $key => $dir) { 310c4e0e4a1SAndreas Gohr if ($dir == '..') { 311c4e0e4a1SAndreas Gohr if (end($result) == '..') { 312c4e0e4a1SAndreas Gohr $result[] = '..'; 313c4e0e4a1SAndreas Gohr } elseif (!array_pop($result)) { 314c4e0e4a1SAndreas Gohr $result[] = '..'; 315c4e0e4a1SAndreas Gohr } 316c4e0e4a1SAndreas Gohr } elseif ($dir && $dir != '.') { 317c4e0e4a1SAndreas Gohr $result[] = $dir; 318c4e0e4a1SAndreas Gohr } 319c4e0e4a1SAndreas Gohr } 320c4e0e4a1SAndreas Gohr if (!end($pathA)) $result[] = ''; 321c4e0e4a1SAndreas Gohr $id = implode(':', $result); 322c4e0e4a1SAndreas Gohr }elseif($ns !== false && strpos($id,':') === false){ 323c4e0e4a1SAndreas Gohr //if link contains no namespace. add current namespace (if any) 324c4e0e4a1SAndreas Gohr $id = $ns.':'.$id; 325c4e0e4a1SAndreas Gohr } 326c4e0e4a1SAndreas Gohr 327a6ef4796SAndreas Gohr if($clean) $id = cleanID($id); 328a6ef4796SAndreas Gohr return $id; 329c4e0e4a1SAndreas Gohr} 330c4e0e4a1SAndreas Gohr 331c4e0e4a1SAndreas Gohr/** 332b625487dSandi * Returns a full media id 333b625487dSandi * 334b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 335b625487dSandi */ 33637e34a5eSandifunction resolve_mediaid($ns,&$page,&$exists){ 337c4e0e4a1SAndreas Gohr $page = resolve_id($ns,$page); 338b625487dSandi $file = mediaFN($page); 339b625487dSandi $exists = @file_exists($file); 340b625487dSandi} 341b625487dSandi 342b625487dSandi/** 343b625487dSandi * Returns a full page id 344b625487dSandi * 345b625487dSandi * @author Andreas Gohr <andi@splitbrain.org> 346b625487dSandi */ 34737e34a5eSandifunction resolve_pageid($ns,&$page,&$exists){ 348b625487dSandi global $conf; 3490b7c14c2Sandi $exists = false; 350b625487dSandi 351b625487dSandi //keep hashlink if exists then clean both parts 35203c4aec3Schris if (strpos($page,'#')) { 353b625487dSandi list($page,$hash) = split('#',$page,2); 35403c4aec3Schris } else { 35503c4aec3Schris $hash = ''; 35603c4aec3Schris } 357b625487dSandi $hash = cleanID($hash); 358a6ef4796SAndreas Gohr $page = resolve_id($ns,$page,false); // resolve but don't clean, yet 359b625487dSandi 360a6ef4796SAndreas Gohr // get filename (calls clean itself) 361b625487dSandi $file = wikiFN($page); 362b625487dSandi 3631179df0eSGuy Brand // if ends with colon or slash we have a namespace link 3641179df0eSGuy Brand if(substr($page,-1) == ':' || ($conf['useslash'] && substr($page,-1) == '/')){ 365a6ef4796SAndreas Gohr if(@file_exists(wikiFN($page.$conf['start']))){ 366a6ef4796SAndreas Gohr // start page inside namespace 367a6ef4796SAndreas Gohr $page = $page.$conf['start']; 368a6ef4796SAndreas Gohr $exists = true; 369a6ef4796SAndreas Gohr }elseif(@file_exists(wikiFN($page.noNS(cleanID($page))))){ 370a6ef4796SAndreas Gohr // page named like the NS inside the NS 371a6ef4796SAndreas Gohr $page = $page.noNS(cleanID($page)); 372a6ef4796SAndreas Gohr $exists = true; 373a6ef4796SAndreas Gohr }elseif(@file_exists(wikiFN($page))){ 374a6ef4796SAndreas Gohr // page like namespace exists 375a6ef4796SAndreas Gohr $page = $page; 376a6ef4796SAndreas Gohr $exists = true; 377a6ef4796SAndreas Gohr }else{ 378a6ef4796SAndreas Gohr // fall back to default 379a6ef4796SAndreas Gohr $page = $page.$conf['start']; 380a6ef4796SAndreas Gohr } 381a6ef4796SAndreas Gohr }else{ 382b625487dSandi //check alternative plural/nonplural form 383b625487dSandi if(!@file_exists($file)){ 384b625487dSandi if( $conf['autoplural'] ){ 385b625487dSandi if(substr($page,-1) == 's'){ 386b625487dSandi $try = substr($page,0,-1); 387b625487dSandi }else{ 388b625487dSandi $try = $page.'s'; 389b625487dSandi } 390b625487dSandi if(@file_exists(wikiFN($try))){ 391b625487dSandi $page = $try; 392b625487dSandi $exists = true; 393b625487dSandi } 394b625487dSandi } 395b625487dSandi }else{ 396b625487dSandi $exists = true; 397b625487dSandi } 398a6ef4796SAndreas Gohr } 399a6ef4796SAndreas Gohr 400a6ef4796SAndreas Gohr // now make sure we have a clean page 401a6ef4796SAndreas Gohr $page = cleanID($page); 402b625487dSandi 403b625487dSandi //add hash if any 404b2d7d3f2Sandi if(!empty($hash)) $page .= '#'.$hash; 405b625487dSandi} 406b625487dSandi 40798407a7aSandi/** 40898407a7aSandi * Returns the name of a cachefile from given data 40998407a7aSandi * 41098407a7aSandi * The needed directory is created by this function! 41198407a7aSandi * 41298407a7aSandi * @author Andreas Gohr <andi@splitbrain.org> 41398407a7aSandi * 41498407a7aSandi * @param string $data This data is used to create a unique md5 name 41598407a7aSandi * @param string $ext This is appended to the filename if given 41698407a7aSandi * @return string The filename of the cachefile 41798407a7aSandi */ 41898407a7aSandifunction getCacheName($data,$ext=''){ 41998407a7aSandi global $conf; 42098407a7aSandi $md5 = md5($data); 42198407a7aSandi $file = $conf['cachedir'].'/'.$md5{0}.'/'.$md5.$ext; 42298407a7aSandi io_makeFileDir($file); 42398407a7aSandi return $file; 42498407a7aSandi} 42598407a7aSandi 4260dc92c6fSAndreas Gohr/** 4270dc92c6fSAndreas Gohr * Checks a pageid against $conf['hidepages'] 4280dc92c6fSAndreas Gohr * 4290dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 4300dc92c6fSAndreas Gohr */ 4310dc92c6fSAndreas Gohrfunction isHiddenPage($id){ 4320dc92c6fSAndreas Gohr global $conf; 4330dc92c6fSAndreas Gohr if(empty($conf['hidepages'])) return false; 4340dc92c6fSAndreas Gohr 4350dc92c6fSAndreas Gohr if(preg_match('/'.$conf['hidepages'].'/ui',':'.$id)){ 4360dc92c6fSAndreas Gohr return true; 4370dc92c6fSAndreas Gohr } 4380dc92c6fSAndreas Gohr return false; 4390dc92c6fSAndreas Gohr} 4400dc92c6fSAndreas Gohr 4410dc92c6fSAndreas Gohr/** 4420dc92c6fSAndreas Gohr * Reverse of isHiddenPage 4430dc92c6fSAndreas Gohr * 4440dc92c6fSAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 4450dc92c6fSAndreas Gohr */ 4460dc92c6fSAndreas Gohrfunction isVisiblePage($id){ 4470dc92c6fSAndreas Gohr return !isHiddenPage($id); 4480dc92c6fSAndreas Gohr} 4490dc92c6fSAndreas Gohr 450254e5c84SBen Coburn/** 451254e5c84SBen Coburn * Checks and sets HTTP headers for conditional HTTP requests 452254e5c84SBen Coburn * 453254e5c84SBen Coburn * @author Simon Willison <swillison@gmail.com> 454254e5c84SBen Coburn * @link http://simon.incutio.com/archive/2003/04/23/conditionalGet 4550ac9a84dSoliver * @param timestamp $timestamp lastmodified time of the cache file 4560ac9a84dSoliver * @returns void or void with previously header() commands executed 457254e5c84SBen Coburn */ 458254e5c84SBen Coburnfunction http_conditionalRequest($timestamp){ 459254e5c84SBen Coburn // A PHP implementation of conditional get, see 460254e5c84SBen Coburn // http://fishbowl.pastiche.org/archives/001132.html 4616d88439aSAndreas Gohr $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT'; 462254e5c84SBen Coburn $etag = '"'.md5($last_modified).'"'; 463254e5c84SBen Coburn // Send the headers 464254e5c84SBen Coburn header("Last-Modified: $last_modified"); 465254e5c84SBen Coburn header("ETag: $etag"); 466254e5c84SBen Coburn // See if the client has provided the required headers 4670ac9a84dSoliver if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){ 4680ac9a84dSoliver $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']); 4690ac9a84dSoliver }else{ 4700ac9a84dSoliver $if_modified_since = false; 4710ac9a84dSoliver } 4720ac9a84dSoliver 4730ac9a84dSoliver if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){ 4740ac9a84dSoliver $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']); 4750ac9a84dSoliver }else{ 4760ac9a84dSoliver $if_none_match = false; 4770ac9a84dSoliver } 4780ac9a84dSoliver 479254e5c84SBen Coburn if (!$if_modified_since && !$if_none_match){ 480254e5c84SBen Coburn return; 481254e5c84SBen Coburn } 4820ac9a84dSoliver 483254e5c84SBen Coburn // At least one of the headers is there - check them 484254e5c84SBen Coburn if ($if_none_match && $if_none_match != $etag) { 485254e5c84SBen Coburn return; // etag is there but doesn't match 486254e5c84SBen Coburn } 4870ac9a84dSoliver 488254e5c84SBen Coburn if ($if_modified_since && $if_modified_since != $last_modified) { 489254e5c84SBen Coburn return; // if-modified-since is there but doesn't match 490254e5c84SBen Coburn } 4910ac9a84dSoliver 492254e5c84SBen Coburn // Nothing has changed since their last request - serve a 304 and exit 493254e5c84SBen Coburn header('HTTP/1.0 304 Not Modified'); 494254e5c84SBen Coburn exit; 495254e5c84SBen Coburn} 496254e5c84SBen Coburn 497b625487dSandi//Setup VIM: ex: et ts=2 enc=utf-8 : 498