01-06-2008, 09:59 PM
Je t'avais envoyé un script pour le cache gzip avec etag.
Cela demande la librairie html.so
Brut de décoffrage, je remettrais en forme sous peu ^^Cela demande la librairie html.so
Code PHP :
<?php
/**
*
* Web D20
* Copyright FOURNET Loïc - 2005/2008
*
*/
require_once dirname( __file__ ) . '/_config.php';
if ( $_SERVER['HTTP_HOST'] == 'localhost' )
define( '__ROOTPATH', 'http://localhost/anthor' );
else
define( '__ROOTPATH', 'http://www.anthor.net' );
ob_start();
/**
* The mkdir function does not support the recursive parameter in some version of
* PHP run by Web Hosting. This function simulates it.
*/
function mkdir_r( $dir_name, $rights = 0777 )
{
$dirs = explode( "/", $dir_name );
$dir = "";
foreach ( $dirs as $part )
{
$dir .= $part . "/";
if ( !is_dir($dir) && strlen($dir) > 0 )
mkdir( $dir, $rights );
}
}
/**
* Wich file are we asking ?
*/
$src_uri = str_replace( __ROOTPATH . '/gz-cache', '', 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
$dirname = dirname( __file__ );
/**
* List of known content types based on file extension.
* Note: These must be built-in somewhere...
* Like finfo_file, to try
*/
$known_content_types = array( "htm" => "text/html", "html" => "text/html", "js" => "text/javascript", "css" =>
"text/css", "xml" => "text/xml", "gif" => "image/gif", "jpg" => "image/jpeg", "jpeg" => "image/jpeg", "png" =>
"image/png", "txt" => "text/plain" );
/**
* Verify the existence of the target file.
* Return HTTP 404 if needed.
*/
if ( !is_file($dirname . $src_uri) )
{
header( "HTTP/1.1 404 Not Found" );
echo ( "<html><body><h1>HTTP 404 - Not Found</h1></body></html>" );
exit;
}
/**
* Set the HTTP response headers that will
* tell the client to cache the resource.
*/
$file_last_modified = filemtime( $dirname . $src_uri );
header( "Last-Modified: " . date("r", $file_last_modified) );
$max_age = 300 * 24 * 60 * 60; // 300 days
$expires = $file_last_modified + $max_age;
header( "Expires: " . date("r", $expires) );
$etag = dechex( $file_last_modified );
header( "ETag: " . $etag );
$cache_control = "must-revalidate, proxy-revalidate, max-age=" . $max_age . ", s-maxage=" . $max_age;
header( "Cache-Control: " . $cache_control );
/**
* Check if the client should use the cached version.
* Return HTTP 304 if needed.
*/
if ( function_exists("http_match_etag") && function_exists("http_match_modified") )
{
if ( http_match_etag($etag) || http_match_modified($file_last_modified) )
{
header( "HTTP/1.1 304 Not Modified" );
exit;
}
}
/**
* Extract the directory, file name and file
* extension from the "uri" parameter.
*/
$uri_dir = "";
$file_name = "";
$content_type = "";
$uri_parts = explode( "/", $src_uri );
for ( $i = 0; $i < count($uri_parts) - 1; $i++ )
$uri_dir .= $uri_parts[$i] . "/";
$file_name = end( $uri_parts );
$file_parts = explode( ".", $file_name );
if ( count($file_parts) > 1 )
{
$file_extension = end( $file_parts );
$content_type = $known_content_types[$file_extension];
}
/**
* Get the target file.
* If the browser accepts gzip encoding, the target file
* will be the gzipped version of the requested file.
*/
$dst_uri = $dirname . $src_uri;
$compress = true;
/**
* Let's compress only text files...
*/
$compress = $compress && ( strpos($content_type, "text") !== false );
/**
* Finally, see if the client sent us the correct Accept-encoding: header value...
*/
$compress = $compress && ( strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false );
if ( $compress )
{
$gz_uri = $dirname . '/_cache/gzip' . $src_uri . '.gz';
if ( file_exists($gz_uri) )
{
$src_last_modified = filemtime( $dirname . $src_uri );
$dst_last_modified = filemtime( $gz_uri );
/**
* The gzip version of the file exists, but it is older
* than the source file. We need to recreate it...
*/
if ( $src_last_modified > $dst_last_modified )
unlink( $gz_uri );
}
if ( !file_exists($gz_uri) )
{
if ( !file_exists($dirname . '/_cache/gzip' . $uri_dir) )
mkdir_r( $dirname . '/_cache/gzip' . $uri_dir );
$error = false;
/**
* Output a gzipped minified javascript version of the input file
*/
if ( strpos($content_type, "javascript") !== false )
{
require_once dirname( __file__ ) . '/_moteur/jsmin.php';
if ( $fp_out = gzopen($gz_uri, "wb") )
{
if ( $data = file_get_contents($dirname . $src_uri) )
{
gzwrite( $fp_out, JSMin::minify($data) );
gzclose( $fp_out );
}
else
{
$error = true;
}
}
else
{
$error = true;
}
}
/**
* Output a gzipped minified css version of the input file
*/ elseif ( strpos($content_type, "css") !== false )
{
require_once dirname( __file__ ) . '/_moteur/cssmin.php';
if ( $fp_out = gzopen($gz_uri, "wb") )
{
if ( $data = file_get_contents($dirname . $src_uri) )
{
gzwrite( $fp_out, cssmin::minify($data) );
gzclose( $fp_out );
}
else
{
$error = true;
}
}
else
{
$error = true;
}
}
/**
* Output a gzipped text version of the input file
*/
else
{
if ( $fp_out = gzopen($gz_uri, "wb") )
{
if ( $data = file_get_contents($dirname . $src_uri) )
{
gzwrite( $fp_out, $data );
gzclose( $fp_out );
}
else
{
$error = true;
}
}
else
{
$error = true;
}
}
if ( !$error )
{
$dst_uri = $gz_uri;
header( "Content-Encoding: gzip" );
}
}
else
{
$dst_uri = $gz_uri;
header( "Content-Encoding: gzip" );
}
}
/**
* Output the target file and set the appropriate HTTP headers.
*/
if ( $content_type )
header( "Content-Type: " . $content_type );
header( "Content-Length: " . filesize($dst_uri) );
readfile( $dst_uri );
ob_end_flush();
?>
PS: Ton fichier fera 30 mo ^^