1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00

Create a quick cached wrapper to attempt to gzip and cache static files

We don't really control how the mirrors configure these things,
so we have to do it ourselves..
This commit is contained in:
Hannes Magnusson
2013-11-21 23:22:57 -08:00
parent acf529a4e4
commit f9bafe1cc5
3 changed files with 44 additions and 4 deletions

40
cached.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
$_SERVER['BASE_PAGE'] = 'cached.php';
include_once 'include/prepend.inc';
if (!isset($_GET["t"], $_GET["f"])) {
header("Location: http://php.net/");
exit;
}
$tsstring = gmdate("D, d M Y H:i:s ", $_GET["t"]) . "GMT";
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) &&
($_SERVER["HTTP_IF_MODIFIED_SINCE"] == $tsstring)) {
header("HTTP/1.1 304 Not Modified");
exit;
}
header("Last-Modified: " . $tsstring);
$abs = $_SERVER["DOCUMENT_ROOT"]. "/" .(string)$_GET["f"];
$abs = realpath($abs);
if (strncmp($abs, $_SERVER["DOCUMENT_ROOT"], strlen($_SERVER["DOCUMENT_ROOT"])) != 0) {
header("Location: http://php.net");
exit;
}
if (substr($abs, -3) == ".js" || substr($abs, -5) == ".json") {
header("Content-Type: application/javascript");
} elseif (substr($abs, -4) == ".css") {
header("Content-Type: text/css");
}
if (function_exists("ob_gzhandler")) {
ob_start("ob_gzhandler");
readfile($abs);
ob_end_flush();
} else {
readfile($abs);
}

View File

@@ -62,15 +62,15 @@
<!-- External and third party libraries. -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="/js/ext/hogan-2.0.0.min.js"></script>
<script type="text/javascript" src="/js/ext/typeahead.min.js"></script>
<script type="text/javascript" src="/cached.php?f=/js/ext/hogan-2.0.0.min.js&t=2"></script>
<script type="text/javascript" src="/cached.php?f=/js/ext/typeahead.min.js&t=2"></script>
<?php
// Nick bjori's last modified code for our own JS files.
$jsfiles = array('search.js', 'common.js');
foreach ($jsfiles as $filename) {
$path = dirname(dirname(__FILE__)).'/js/'.$filename;
echo '<script type="text/javascript" src="/js/'.$filename.'?t='.@filemtime($path).'"></script>';
echo '<script type="text/javascript" src="/cached.php?f=/js/'.$filename.'&t='.@filemtime($path).'"></script>';
}
include 'uservoice.inc';

View File

@@ -50,7 +50,7 @@ if (isset($shortname) && $shortname) {
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600,400italic,600italic|Source+Code+Pro&amp;subset=latin,latin-ext' rel='stylesheet' type='text/css'>
<?php foreach($CSS as $filename => $modified): ?>
<link rel="stylesheet" type="text/css" href="<?php echo $STATIC_ROOT ?>styles/<?php echo $filename?>?t=<?php echo $modified?>" media="screen">
<link rel="stylesheet" type="text/css" href="<?php echo $STATIC_ROOT ?>cached.php?f=styles/<?php echo $filename?>&t=<?php echo $modified?>fda" media="screen">
<?php endforeach ?>
<!--[if lte IE 7]>