Advertisement
Blade83

CSS Compression and Cache Script

Oct 21st, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.09 KB | None | 0 0
  1. <?php
  2. /*
  3.     Script: Load a uncompressed CSS File,
  4.         compress it and saved it in a cache folder and returns the compressed CSS.
  5.  
  6.     Author $ Blade83
  7.  
  8.     Usage:
  9.     Safe this into a php file.
  10.     Write in your HTML Code:
  11.     <link href="your_php_file.php?css=style.css" rel="stylesheet" type="text/css" />
  12.     This will open and output the style.css file in a compressed format.
  13.     Optional Parameters in href are
  14.     - &no_compress=1   <- outputs the uncompressed css
  15.     - &no_cache=1   <- compress on the fly and returns it
  16. */
  17.  
  18. define('CACHE_FOLDER',              'cache');                                                           # single folder or recursive (folder1/folder2/folder3) folders
  19. define('CACHE_FOLDER_CHMOD',        0755);                                                              # CHMOD for Cache folder
  20. define('CACHE_TIME',                120);                                                               # Expiretime in seconds for cached files
  21. define('CACHE_FILE_SUFIX',          'css_cache_');                                                      # Prefix for cached style sheets
  22. define('HEADER_SEND_FILE_EXPIRE',   86400);                                                             # Browser expiretimes for files
  23. $no_compress                        = ($_GET['no_compress']==1) ? 1 : 0;
  24. $no_cache                           = ($_GET['no_cache']==1) ? 1 : 0;
  25. $filename                           = $_GET['css'];
  26. $cssFile                            = (preg_match("/^[A-Za-z_.]+$/", trim($filename))) ? trim($filename) : NULL;
  27. $buffer                             = @file_get_contents($cssFile);
  28.  
  29. function send_css_header($buffer, $buffer_error='')
  30. {
  31.     global $cssFile;
  32.     ob_start("ob_gzhandler");
  33.     header('Cache-Control: public');
  34.     header('Expires: ' . gmdate('D, d M Y H:i:s', time() + HEADER_SEND_FILE_EXPIRE) . ' GMT');
  35.     header("Content-type: text/css");
  36.     $_ = "\x24\x63\x6F\x70\x79\x72\x69\x67\x68\x74\x20\x3D\x20\x22\x2F\x2A\x0D\x0A\x09\x56\x69\x72\x74\x75\x61\x6C\x57\x61\x72\x73\x20\x43\x53\x53\x20\x43\x6F\x6D\x70\x72\x65\x73\x73\x6F\x72\x0D\x0A\x09\x41\x75\x74\x68\x6F\x72\x3A\x20\x24\x20\x42\x6C\x61\x64\x65\x38\x33\x0D\x0A\x09\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x2D\x0D\x0A\x09\x46\x69\x6C\x65\x69\x6E\x66\x6F\x72\x6D\x61\x74\x69\x6F\x6E\x3A\x0D\x0A\x09\x46\x69\x6C\x65\x6E\x61\x6D\x65\x3A\x09\x09\x09\x22\x2E\x24\x63\x73\x73\x46\x69\x6C\x65\x2E\x22\x0D\x0A\x09\x52\x65\x61\x6C\x20\x46\x69\x6C\x65\x70\x61\x74\x68\x3A\x09\x09\x09\x22\x2E\x40\x64\x69\x72\x6E\x61\x6D\x65\x28\x5F\x5F\x46\x49\x4C\x45\x5F\x5F\x29\x2E\x22\x0D\x0A\x09\x46\x69\x6C\x65\x73\x69\x7A\x65\x20\x75\x6E\x63\x6F\x6D\x70\x72\x65\x73\x73\x65\x64\x3A\x09\x09\x22\x2E\x40\x66\x69\x6C\x65\x73\x69\x7A\x65\x28\x24\x63\x73\x73\x46\x69\x6C\x65\x29\x2E\x22\x20\x42\x79\x74\x65\x73\x0D\x0A\x09\x46\x69\x6C\x65\x73\x69\x7A\x65\x20\x63\x6F\x6D\x70\x72\x65\x73\x73\x65\x64\x3A\x09\x09\x22\x2E\x40\x66\x69\x6C\x65\x73\x69\x7A\x65\x28\x43\x41\x43\x48\x45\x5F\x46\x4F\x4C\x44\x45\x52\x20\x2E\x20\x27\x2F\x27\x20\x2E\x20\x43\x41\x43\x48\x45\x5F\x46\x49\x4C\x45\x5F\x53\x55\x46\x49\x58\x20\x2E\x20\x24\x63\x73\x73\x46\x69\x6C\x65\x29\x2E\x22\x20\x42\x79\x74\x65\x73\x0D\x0A\x09\x22\x2E\x22\x5C\x6E\x2A\x2F\x22\x3B";
  37.     $__ = "\x65\x63\x68\x6F\x20\x24\x63\x6F\x70\x79\x72\x69\x67\x68\x74\x2E\x22\x5C\x6E\x22\x2E\x24\x62\x75\x66\x66\x65\x72\x5F\x65\x72\x72\x6F\x72\x2E\x22\x5C\x6E\x22\x2E\x24\x62\x75\x66\x66\x65\x72\x3B";
  38.     eval($_ . $__);
  39.     ob_end_flush();
  40. }
  41. function compress_css($css)
  42. {
  43.     global $cssFile;
  44.     $replace_arr = array(
  45.                         "\r\n",
  46.                         "\r",
  47.                         "\n",
  48.                         "\t",
  49.                         '  ',
  50.                         '    ',
  51.                         '    '
  52.                     );
  53.     $compressed = str_replace($replace_arr, '', str_replace(': ', ':', preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css)));
  54.     return $compressed;
  55. }
  56. function write_cache($compressed_css)
  57. {
  58.     global $cssFile;
  59.     if (!$handle = @fopen(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile, 'w'))
  60.     {
  61.         $buffer_error .= '/* Can not open file "'.CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile.'" */'."\n";
  62.     }
  63.     if (is_writable(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile))
  64.     {
  65.         if (@fwrite($handle, $compressed_css) === FALSE)
  66.         {
  67.             $buffer_error .= '/* Can not write to file "'.CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile.'" */'."\n";
  68.         }
  69.         @fclose($handle);
  70.         $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
  71.     }
  72.     else
  73.     {
  74.         $buffer_error .= '/* The file "'.CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile.'" is not writable */'."\n";
  75.     }
  76.     return $buffer_error;
  77. }
  78. if ($buffer == FALSE)
  79. {
  80.     $buffer_error = "/* Error: File \"$cssFile\" not found! */";
  81.     send_css_header('', $buffer_error);
  82.     exit;
  83. }
  84. if ($no_compress==1)
  85. {
  86.     send_css_header($buffer, "/* Send method: No Compression */"."\n");
  87.     exit;
  88. }
  89. else
  90. {
  91.     if ($no_cache==1)
  92.     {
  93.         send_css_header(compress_css($buffer), "/* Send method: Compressed on the fly */"."\n");
  94.         exit;
  95.     }
  96.     else
  97.     {
  98.         if(!is_dir(CACHE_FOLDER))
  99.         {
  100.             $buffer_error .= '/* Error: Folder "' . CACHE_FOLDER . '" not found in "'.@dirname(__FILE__).'/"! */'."\n";
  101.             $buffer_error .= '/* Trying to create folder "' . CACHE_FOLDER . '" in "'.@dirname(__FILE__).'/": ........ ';
  102.             $oldumask = umask(0);
  103.             $buffer_error .= (mkdir(CACHE_FOLDER, CACHE_FOLDER_CHMOD, true))?'Created!':'Error!';
  104.             umask($oldumask);
  105.             $buffer_error .= ' */'."\n";
  106.         }
  107.         if (!file_exists(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile))
  108.         {
  109.             $buffer = compress_css($buffer);
  110.             $buffer_error .= write_cache($buffer);
  111.             $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
  112.             $buffer_error .= '/* Send method: Cached, expired in: '.(($filemtime+CACHE_TIME)-time()).'s */';
  113.             send_css_header(@file_get_contents(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile), $buffer_error);
  114.             exit;
  115.         }
  116.         else
  117.         {
  118.             $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
  119.             if ( (time()-$filemtime) >= CACHE_TIME )
  120.             {
  121.                 @unlink(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
  122.                 $buffer = compress_css($buffer);
  123.                 $buffer_error .= write_cache($buffer);
  124.                 $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
  125.             }
  126.             $buffer_error .= '/* Send method: Cached, expired in: '.(($filemtime+CACHE_TIME)-time()).'s */';
  127.             send_css_header(@file_get_contents(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile), $buffer_error);
  128.         }
  129.     }
  130. }
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement