Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.44 KB | None | 0 0
  1. <?php
  2.  
  3. /* The HTML code won't be compressed -> Does nothing! */
  4. define('CM_NONE', 0);
  5. /* Compress the HTML in the gzip format. Note that this will also add the header Content-Encoding! */
  6. define('CM_COMPRESS_HTTP_DEFLATE', 2);
  7. define('CM_COMPRESS_HTTP_GZIP', 4);
  8. /* Removes control characters (spaces, tabs, returns,...) from the HTML code */
  9. define('CM_REMOVE_HTML_SPACES', 16);
  10. /* Converts the images to Base64 strings so the images won't be loaded from the server. */
  11. define('CM_ENCODE_HTML_IMAGES', 32);
  12. /* Converts the images in the CSS file to Base64 data URLs */
  13. define('CM_ENCODE_CSS_IMAGES', 64);
  14. /* Executes all compression methods. See the flags CM_GZIP, CM_REMOVESPACES, CM_IMAGEENCODE for further information. */
  15. define('CM_HTML', CM_COMPRESS_HTTP_DEFLATE|CM_REMOVE_HTML_SPACES);
  16. define('CM_CSS', CM_ENCODE_CSS_IMAGES);
  17.  
  18. define('CM_GZIP_COMPRESSION_LEVEL', 9);
  19. define('CM_DEFLATE_COMPRESSION_LEVEL', 9);
  20.  
  21.  
  22. class NRCompress
  23. {  
  24.   private static function Callback_EncodeHTMLImages($matches)
  25.   {
  26.     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 */
  27.     $file = file_get_contents($matches[1]);
  28.     $file_extension = substr($matches[1], strripos($matches[1], ".")+1); /* Extract the file extension (without the point) */
  29.     $base64 = "data:image/".$file_extension.";base64,".base64_encode($file); /* Encodes the image to Base64 */
  30.  
  31.    
  32.     $end = str_replace($matches[1], $base64, $matches[0]); /* Assembly the img-tag and the new src-attribute */
  33.     return $end;
  34.   }
  35.   private static function Callback_EncodeCSSImages($matches)
  36.   {
  37.     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 */
  38.     $file = file_get_contents($matches[3]);
  39.     $file_extension = substr($matches[3], strripos($matches[3], ".")+1); /* Extract the file extension (without the point) */
  40.     $base64 = "data:image/".$file_extension.";base64,".base64_encode($file); /* Encodes the image to Base64 */
  41.  
  42.  
  43.     $end = str_replace($matches[3], $base64, $matches[0]); /* Assembly the img-tag and the new src-attribute */
  44.     return $end;
  45.   }
  46.  
  47.   private static function RemoveSpacesFromHTML($html)
  48.   {
  49.     $control_chars = array("\a", "\t", "\b", "\v", "\f", "\r");
  50.     $nothing = array("",   "",   "",   "",  "","", "", "");
  51.     $html = str_replace($control_chars, $nothing, $html);
  52.     return $html;
  53.   }
  54.   private static function EncodeImagesInHTML($html)
  55.   {
  56.     $pattern = '/<img.+?src\s*=\s*"([^"]+)"/i';
  57.     $html = preg_replace_callback($pattern, "self::Callback_EncodeHTMLImages", $html);
  58.     return $html;
  59.   }
  60.   private static function EncodeImagesInCSS($css)
  61.   {
  62.     $pattern = '/url\(((\'|"|\s?)(.*)(\"|\'|\s?))\)/Usi';
  63.     $css = preg_replace_callback($pattern, "self::Callback_EncodeCSSImages", $css);
  64.     return $css;
  65.   }
  66.   private static function CompressHTTP($code, $compress_methods)
  67.   {
  68.     if (Request::CheckBrowserDeflateSupport() && function_exists("gzdeflate") && $compress_methods & CM_COMPRESS_HTTP_DEFLATE)
  69.     {
  70.       $code = gzdeflate($code, CM_DEFLATE_COMPRESSION_LEVEL);
  71.       return $code;
  72.     }
  73.     if (Request::CheckBrowserGZIPSupport() && function_exists("gzencode") && $compress_methods & CM_COMPRESS_HTTP_GZIP)
  74.     {
  75.       $code = gzencode($code, CM_GZIP_COMPRESSION_LEVEL);
  76.       return $code;
  77.     }
  78.   }
  79.   private static function SetRightHTTPHeader($compress_methods)
  80.   {
  81.     if ($compress_methods & CM_COMPRESS_HTTP_DEFLATE)
  82.     {
  83.       HTTPresponse::addHeader("Content-Encoding", "deflate");
  84.     }
  85.     else if ($compress_methods & CM_COMPRESS_HTTP_GZIP)
  86.     {
  87.       HTTPresponse::addHeader("Content-Encoding", "gzip");
  88.     }
  89.   }
  90.   private static function BuildCacheFilename($code, $compress_methods)
  91.   {
  92.     return ("includes/temp/" . md5($code) . "_" . (string)$compress_methods . ".nrc");
  93.   }
  94.  
  95.   public static function GetBestHTTPCompression()
  96.   {
  97.     if (Request::CheckBrowserDeflateSupport())
  98.       return CM_COMPRESS_HTTP_DEFLATE;
  99.     else if (Request::CheckBrowserGZIPSupport())
  100.       return CM_COMPRESS_HTTP_GZIP;
  101.    
  102.     return CM_NONE;
  103.   }
  104.  
  105.  
  106.   public static function Compress($code, $compress_methods=CM_ALL, $cache=true)
  107.   {
  108.     if (Core::is_ajax()) return $code;
  109.     if ($compress_methods)
  110.    
  111.     if ($cache)
  112.     {
  113.       if (file_exists(self::BuildCacheFilename($code, $compress_methods)))
  114.       {
  115.         self::SetRightHTTPHeader($compress_methods);
  116.         return file_get_contents( self::BuildCacheFilename($code, $compress_methods) );
  117.       }
  118.     }
  119.  
  120.     if ($compress_methods & CM_REMOVE_HTML_SPACES) /* Not working right yet! */
  121.     {    
  122.       $code = self::RemoveSpacesFromHTML($code);
  123.     }
  124.     if ($compress_methods & CM_ENCODE_HTML_IMAGES)
  125.     {
  126.       $code = self::EncodeImagesInHTML($code);
  127.     }
  128.     if ($compress_methods & CM_ENCODE_CSS_IMAGES)
  129.     {
  130.       $code = self::EncodeImagesInCSS($code);
  131.     }
  132.     if ($compress_methods & CM_COMPRESS_HTTP_DEFLATE ||
  133.         $compress_methods & CM_COMPRESS_HTTP_GZIP)
  134.     {
  135.       self::CompressHTTP($code, $compress_methods);
  136.       //self::SetRightHTTPHeader($compress_methods);
  137.       HTTPresponse::addHeader("Content-Encoding", "gzip");
  138.     }
  139.     if ($cache)
  140.     {
  141.       $file = fopen(self::BuildCacheFilename($code, $compress_methods), "wb");
  142.       fwrite($file, $code);
  143.       fclose($file);
  144.     }  
  145.     return $code;
  146.   }
  147.    
  148. }
  149.  
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement