Skip to content

Commit

Permalink
Issue 129 : Minify HTML page by prepending pageBuffer.php. Kind of a …
Browse files Browse the repository at this point in the history
  • Loading branch information
mrclay committed Aug 5, 2009
1 parent c63f0bf commit efe2f7d
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 0 deletions.
92 changes: 92 additions & 0 deletions min/pageBuffer.php
@@ -0,0 +1,92 @@
<?php
/**
* Include for serving HTML pages through Minify
*
* DO NOT EDIT! Configure this utility via pageConfig.php
*
* @package Minify
*/

$_min_preIncludedFiles = get_included_files();

function ob_minify_page($content) {
global $min_serveOptions, $min_cacheMaxAge;

$includedFiles = array_diff(
$GLOBALS['_min_preIncludedFiles'], get_included_files()
);
$includedFiles[] = realpath($_SERVER['SCRIPT_FILENAME']);
$mtime = 0;
foreach ($includedFiles as $file) {
$mtime = max($mtime, filemtime($file));
}

define('MINIFY_MIN_DIR', dirname(__FILE__));

// load config
require MINIFY_MIN_DIR . '/pageConfig.php';

// setup include path
set_include_path($min_libPath . PATH_SEPARATOR . get_include_path());

require $min_libPath . '/Minify.php';

Minify::$uploaderHoursBehind = $min_uploaderHoursBehind;
Minify::setCache(
isset($min_cachePath) ? $min_cachePath : ''
,$min_cacheFileLocking
);

if ($min_errorLogger) {
require_once $min_libPath . '/Minify/Logger.php';
if (true === $min_errorLogger) {
require_once $min_libPath . '/FirePHP.php';
Minify_Logger::setLogger(FirePHP::getInstance(true));
} else {
Minify_Logger::setLogger($min_errorLogger);
}
}

// some array keys may already be set in globals
$min_serveOptions['id'] = $_SERVER['SCRIPT_FILENAME'];
$min_serveOptions['content'] = $content;
$min_serveOptions['lastModified'] = $mtime;
$min_serveOptions['minifyAll'] = true;
$min_serveOptions['quiet'] = true;

// page option: $min_lastModified
if (isset($GLOBALS['min_lastModified'])) {
$min_serveOptions['lastModified'] = $GLOBALS['min_lastModified'];
}

if ($min_cacheMaxAge) {
$min_serveOptions['lastModified'] = _steppedTime(
$min_cacheMaxAge, $_SERVER['SCRIPT_FILENAME']);
}

$out = Minify::serve('Page', $min_serveOptions);

foreach ($out['headers'] as $prop => $value) {
header($prop === '_responseCode' ? $value : "{$prop}: {$value}");
}
return $out['content'];
}

/**
* Get a stepped version of time with a random-like constistent offset
*
* This emulates the mtime() of a file being modified regularly. The offset
* allows you to use the same period in several instances without them all
* stepping up simultaneously. Offsets should be evenly distributed.
*
* @param int $period in seconds
* @param string $instanceId
* @return int
*/
function _steppedTime($period, $instanceId = '') {
$hashInt = hexdec(substr(md5($instanceId), 0, 8));
$t = $_SERVER['REQUEST_TIME'];
return $t - ($t % $period) - ($hashInt % $period);
}

ob_start('ob_minify_page');
82 changes: 82 additions & 0 deletions min/pageConfig.php
@@ -0,0 +1,82 @@
<?php
/**
* Configuration for pageBuffer.php
* @package Minify
*/


/**
* Set to true to log messages to FirePHP (Firefox Firebug addon).
* Set to false for no error logging (Minify may be slightly faster).
* @link http://www.firephp.org/
*
* If you want to use a custom error logger, set this to your logger
* instance. Your object should have a method log(string $message).
*
* @todo cache system does not have error logging yet.
*/
$min_errorLogger = false;


/**
* For best performance, specify your temp directory here. Otherwise Minify
* will have to load extra code to guess. Some examples below:
*/
//$min_cachePath = 'c:\\WINDOWS\\Temp';
//$min_cachePath = '/tmp';
//$min_cachePath = preg_replace('/^\\d+;/', '', session_save_path());


/**
* Cache file locking. Set to false if filesystem is NFS. On at least one
* NFS system flock-ing attempts stalled PHP for 30 seconds!
*/
$min_cacheFileLocking = true;


/**
* Maximum age of browser cache in seconds. After this period, the browser
* will send another conditional GET. Use a longer period for lower traffic
* but you may want to shorten this before making changes if it's crucial
* those changes are seen immediately.
*/
$min_serveOptions['maxAge'] = 1800;


/**
* By default Minify will check the mtime of the loaded page and any pages it
* includes to determine the last modified time. If you would rather have it
* regularly rebuild the cache, set this to the period, in seconds, that the
* cache should be rebuilt (note this only occurs if a request is made).
*/
$min_cacheMaxAge = false;


/**
* If you upload files from Windows to a non-Windows server, Windows may report
* incorrect mtimes for the files. This may cause Minify to keep serving stale
* cache files when source file changes are made too frequently (e.g. more than
* once an hour).
*
* Immediately after modifying and uploading a file, use the touch command to
* update the mtime on the server. If the mtime jumps ahead by a number of hours,
* set this variable to that number. If the mtime moves back, this should not be
* needed.
*
* In the Windows SFTP client WinSCP, there's an option that may fix this
* issue without changing the variable below. Under login > environment,
* select the option "Adjust remote timestamp with DST".
* @link http://winscp.net/eng/docs/ui_login_environment#daylight_saving_time
*/
$min_uploaderHoursBehind = 0;


/**
* Path to Minify's lib folder. If you happen to move it, change
* this accordingly.
*/
$min_libPath = dirname(__FILE__) . '/lib';


// try to disable output_compression (may not have an effect)
ini_set('zlib.output_compression', '0');
96 changes: 96 additions & 0 deletions min/test/pageBuffer.php
@@ -0,0 +1,96 @@
<?php
require '../pageBuffer.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
<!-- comments get removed -->
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Dave Shea" />
<!-- also whitespace around block or undisplayed elements -->
<meta name="keywords" content="design, css, cascading, style, sheets, xhtml, graphic design, w3c, web standards, visual, display" />
<meta name="description" content="A demonstration of what can be accomplished visually through CSS-based design." />
<meta name="robots" content="all" />
<title>css Zen Garden: The Beauty in CSS Design</title>

<!-- to correct the unsightly Flash of Unstyled Content. http://www.bluerobot.com/web/css/fouc.asp -->
<script type="text/javascript"><!--
// js comment inside SCRIPT element
var is = {
ie: navigator.appName == 'Microsoft Internet Explorer',
java: navigator.javaEnabled(),
ns: navigator.appName == 'Netscape',
ua: navigator.userAgent.toLowerCase(),
version: parseFloat(navigator.appVersion.substr(21)) ||
parseFloat(navigator.appVersion),
win: navigator.platform == 'Win32'
}
is.mac = is.ua.indexOf('mac') >= 0;
if (is.ua.indexOf('opera') >= 0) {
is.ie = is.ns = false;
is.opera = true;
}
if (is.ua.indexOf('gecko') >= 0) {
is.ie = is.ns = false;
is.gecko = true;
}
// --></script>
<script type="text/javascript">
//<![CDATA[
var i = 0;
while (++i < 10)
{
// ...
}
//]]>
</script>
<script type="text/javascript">
/* <![CDATA[ */ i = 1; /* ]]> */
</script>
<script type="text/javascript">
(i < 1); /* CDATA needed */
</script>
<!--[if IE 6]>
<style type="text/css">
/*! copyright: you'll need CDATA for this < & */
body {background:white;}
</style>
<![endif]-->
<style type="text/css" title="currentStyle" media="screen">
@import "/001/001.css";
/*\*/ css hack {} /* */
/* normal CSS comment */
/*/*/ css hack {} /* */
css hack {
display/**/:/**/none;
display:none;
}
</style>
<link
rel="alternate"
type="application/rss+xml"
title="RSS"
href="http://www.csszengarden.com/zengarden.xml" />
</head>
<body id="css-zen-garden">
<!--[if !IE]>--><p>Browser != IE</p><!--<![endif]-->
<div id="container">
<div id="pageHeader">
<h1><span>css Zen Garden</span></h1>
<h2><span>The Beauty of <acronym title="Cascading Style Sheets">CSS</acronym>
Design</span></h2>
</div>
<pre>
White space is important here!
</pre>
<div id="quickSummary">
<p class="p1"><span>A demonstration of what can be accomplished visually through <acronym title="Cascading Style Sheets">CSS</acronym>-based design. Select any style sheet from the list to load it into this page.</span></p>
<p class="p2"><span>Download the sample <a href="/zengarden-sample.html" title="This page's source HTML code, not to be modified.">html file</a> and <a href="/zengarden-sample.css" title="This page's sample CSS, the file you may modify.">css file</a></span></p>
</div>
<textarea name="comment" id="comment" rows="6" class="maxwidth" cols="80">66666

1234567890</textarea>

</div>
</body>
</html>

0 comments on commit efe2f7d

Please sign in to comment.