com web/php: Create a quick cached wrapper to attempt to gzip and cache static files: cached.php include/footer.inc include/header.inc
| From: | Hannes Magnusson | Date: | Fri, 22 Nov 2013 07:23:46 +0000 |
| Subject: | com web/php: Create a quick cached wrapper to attempt to gzip and cache static files: cached.php include/footer.inc include/header.inc | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-17186@lists.php.net to get a copy of this message | ||
Commit: f9bafe1cc599852ce5d777622e79283b4e9edbef
Author: Hannes Magnusson <bjori@mongodb.com> Thu, 21 Nov 2013 23:22:57 -0800
Parents: acf529a4e4a03c392b181fa27bb1613db4f5bdac
Branches: master
Link: http://git.php.net/?p=web/php.git;a=commitdiff;h=f9bafe1cc599852ce5d777622e79283b4e9edbef
Log:
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..
Changed paths:
A cached.php
M include/footer.inc
M include/header.inc
Diff:
diff --git a/cached.php b/cached.php
new file mode 100644
index 0000000..196fd77
--- /dev/null
+++ b/cached.php
@@ -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);
+}
+
+
diff --git a/include/footer.inc b/include/footer.inc
index 49640da..b8a2db2 100644
--- a/include/footer.inc
+++ b/include/footer.inc
@@ -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';
diff --git a/include/header.inc b/include/header..inc
index 1167845..e4b347c 100755
--- a/include/header.inc
+++ b/include/header.inc
@@ -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&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]>