Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* The HTML code won't be compressed -> Does nothing! */
- define('CM_NONE', 0);
- /* Compress the HTML in the gzip format. Note that this will also add the header Content-Encoding! */
- define('CM_COMPRESS_HTTP_DEFLATE', 2);
- define('CM_COMPRESS_HTTP_GZIP', 4);
- /* Removes control characters (spaces, tabs, returns,...) from the HTML code */
- define('CM_REMOVE_HTML_SPACES', 16);
- /* Converts the images to Base64 strings so the images won't be loaded from the server. */
- define('CM_ENCODE_HTML_IMAGES', 32);
- /* Converts the images in the CSS file to Base64 data URLs */
- define('CM_ENCODE_CSS_IMAGES', 64);
- /* Executes all compression methods. See the flags CM_GZIP, CM_REMOVESPACES, CM_IMAGEENCODE for further information. */
- define('CM_HTML', CM_COMPRESS_HTTP_DEFLATE|CM_REMOVE_HTML_SPACES);
- define('CM_CSS', CM_ENCODE_CSS_IMAGES);
- define('CM_GZIP_COMPRESSION_LEVEL', 9);
- define('CM_DEFLATE_COMPRESSION_LEVEL', 9);
- class NRCompress
- {
- private static function Callback_EncodeHTMLImages($matches)
- {
- if (!file_exists($matches[1])) return $matches[0].$matches[1]; /* If the file doesn't exists, the return value will be the same as in the HTML code */
- $file = file_get_contents($matches[1]);
- $file_extension = substr($matches[1], strripos($matches[1], ".")+1); /* Extract the file extension (without the point) */
- $base64 = "data:image/".$file_extension.";base64,".base64_encode($file); /* Encodes the image to Base64 */
- $end = str_replace($matches[1], $base64, $matches[0]); /* Assembly the img-tag and the new src-attribute */
- return $end;
- }
- private static function Callback_EncodeCSSImages($matches)
- {
- if (!file_exists($matches[3])) return $matches[0]; /* If the file doesn't exists, the return value will be the same as in the HTML code */
- $file = file_get_contents($matches[3]);
- $file_extension = substr($matches[3], strripos($matches[3], ".")+1); /* Extract the file extension (without the point) */
- $base64 = "data:image/".$file_extension.";base64,".base64_encode($file); /* Encodes the image to Base64 */
- $end = str_replace($matches[3], $base64, $matches[0]); /* Assembly the img-tag and the new src-attribute */
- return $end;
- }
- private static function RemoveSpacesFromHTML($html)
- {
- $control_chars = array("\a", "\t", "\b", "\v", "\f", "\r");
- $nothing = array("", "", "", "", "","", "", "");
- $html = str_replace($control_chars, $nothing, $html);
- return $html;
- }
- private static function EncodeImagesInHTML($html)
- {
- $pattern = '/<img.+?src\s*=\s*"([^"]+)"/i';
- $html = preg_replace_callback($pattern, "self::Callback_EncodeHTMLImages", $html);
- return $html;
- }
- private static function EncodeImagesInCSS($css)
- {
- $pattern = '/url\(((\'|"|\s?)(.*)(\"|\'|\s?))\)/Usi';
- $css = preg_replace_callback($pattern, "self::Callback_EncodeCSSImages", $css);
- return $css;
- }
- private static function CompressHTTP($code, $compress_methods)
- {
- if (Request::CheckBrowserDeflateSupport() && function_exists("gzdeflate") && $compress_methods & CM_COMPRESS_HTTP_DEFLATE)
- {
- $code = gzdeflate($code, CM_DEFLATE_COMPRESSION_LEVEL);
- return $code;
- }
- if (Request::CheckBrowserGZIPSupport() && function_exists("gzencode") && $compress_methods & CM_COMPRESS_HTTP_GZIP)
- {
- $code = gzencode($code, CM_GZIP_COMPRESSION_LEVEL);
- return $code;
- }
- }
- private static function SetRightHTTPHeader($compress_methods)
- {
- if ($compress_methods & CM_COMPRESS_HTTP_DEFLATE)
- {
- HTTPresponse::addHeader("Content-Encoding", "deflate");
- }
- else if ($compress_methods & CM_COMPRESS_HTTP_GZIP)
- {
- HTTPresponse::addHeader("Content-Encoding", "gzip");
- }
- }
- private static function BuildCacheFilename($code, $compress_methods)
- {
- return ("includes/temp/" . md5($code) . "_" . (string)$compress_methods . ".nrc");
- }
- public static function GetBestHTTPCompression()
- {
- if (Request::CheckBrowserDeflateSupport())
- return CM_COMPRESS_HTTP_DEFLATE;
- else if (Request::CheckBrowserGZIPSupport())
- return CM_COMPRESS_HTTP_GZIP;
- return CM_NONE;
- }
- public static function Compress($code, $compress_methods=CM_ALL, $cache=true)
- {
- if (Core::is_ajax()) return $code;
- if ($compress_methods)
- if ($cache)
- {
- if (file_exists(self::BuildCacheFilename($code, $compress_methods)))
- {
- self::SetRightHTTPHeader($compress_methods);
- return file_get_contents( self::BuildCacheFilename($code, $compress_methods) );
- }
- }
- if ($compress_methods & CM_REMOVE_HTML_SPACES) /* Not working right yet! */
- {
- $code = self::RemoveSpacesFromHTML($code);
- }
- if ($compress_methods & CM_ENCODE_HTML_IMAGES)
- {
- $code = self::EncodeImagesInHTML($code);
- }
- if ($compress_methods & CM_ENCODE_CSS_IMAGES)
- {
- $code = self::EncodeImagesInCSS($code);
- }
- if ($compress_methods & CM_COMPRESS_HTTP_DEFLATE ||
- $compress_methods & CM_COMPRESS_HTTP_GZIP)
- {
- self::CompressHTTP($code, $compress_methods);
- //self::SetRightHTTPHeader($compress_methods);
- HTTPresponse::addHeader("Content-Encoding", "gzip");
- }
- if ($cache)
- {
- $file = fopen(self::BuildCacheFilename($code, $compress_methods), "wb");
- fwrite($file, $code);
- fclose($file);
- }
- return $code;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement