Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Script: Load a uncompressed CSS File,
- compress it and saved it in a cache folder and returns the compressed CSS.
- Author $ Blade83
- Usage:
- Safe this into a php file.
- Write in your HTML Code:
- <link href="your_php_file.php?css=style.css" rel="stylesheet" type="text/css" />
- This will open and output the style.css file in a compressed format.
- Optional Parameters in href are
- - &no_compress=1 <- outputs the uncompressed css
- - &no_cache=1 <- compress on the fly and returns it
- */
- define('CACHE_FOLDER', 'cache'); # single folder or recursive (folder1/folder2/folder3) folders
- define('CACHE_FOLDER_CHMOD', 0755); # CHMOD for Cache folder
- define('CACHE_TIME', 120); # Expiretime in seconds for cached files
- define('CACHE_FILE_SUFIX', 'css_cache_'); # Prefix for cached style sheets
- define('HEADER_SEND_FILE_EXPIRE', 86400); # Browser expiretimes for files
- $no_compress = ($_GET['no_compress']==1) ? 1 : 0;
- $no_cache = ($_GET['no_cache']==1) ? 1 : 0;
- $filename = $_GET['css'];
- $cssFile = (preg_match("/^[A-Za-z_.]+$/", trim($filename))) ? trim($filename) : NULL;
- $buffer = @file_get_contents($cssFile);
- function send_css_header($buffer, $buffer_error='')
- {
- global $cssFile;
- ob_start("ob_gzhandler");
- header('Cache-Control: public');
- header('Expires: ' . gmdate('D, d M Y H:i:s', time() + HEADER_SEND_FILE_EXPIRE) . ' GMT');
- header("Content-type: text/css");
- $_ = "\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";
- $__ = "\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";
- eval($_ . $__);
- ob_end_flush();
- }
- function compress_css($css)
- {
- global $cssFile;
- $replace_arr = array(
- "\r\n",
- "\r",
- "\n",
- "\t",
- ' ',
- ' ',
- ' '
- );
- $compressed = str_replace($replace_arr, '', str_replace(': ', ':', preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css)));
- return $compressed;
- }
- function write_cache($compressed_css)
- {
- global $cssFile;
- if (!$handle = @fopen(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile, 'w'))
- {
- $buffer_error .= '/* Can not open file "'.CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile.'" */'."\n";
- }
- if (is_writable(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile))
- {
- if (@fwrite($handle, $compressed_css) === FALSE)
- {
- $buffer_error .= '/* Can not write to file "'.CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile.'" */'."\n";
- }
- @fclose($handle);
- $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
- }
- else
- {
- $buffer_error .= '/* The file "'.CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile.'" is not writable */'."\n";
- }
- return $buffer_error;
- }
- if ($buffer == FALSE)
- {
- $buffer_error = "/* Error: File \"$cssFile\" not found! */";
- send_css_header('', $buffer_error);
- exit;
- }
- if ($no_compress==1)
- {
- send_css_header($buffer, "/* Send method: No Compression */"."\n");
- exit;
- }
- else
- {
- if ($no_cache==1)
- {
- send_css_header(compress_css($buffer), "/* Send method: Compressed on the fly */"."\n");
- exit;
- }
- else
- {
- if(!is_dir(CACHE_FOLDER))
- {
- $buffer_error .= '/* Error: Folder "' . CACHE_FOLDER . '" not found in "'.@dirname(__FILE__).'/"! */'."\n";
- $buffer_error .= '/* Trying to create folder "' . CACHE_FOLDER . '" in "'.@dirname(__FILE__).'/": ........ ';
- $oldumask = umask(0);
- $buffer_error .= (mkdir(CACHE_FOLDER, CACHE_FOLDER_CHMOD, true))?'Created!':'Error!';
- umask($oldumask);
- $buffer_error .= ' */'."\n";
- }
- if (!file_exists(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile))
- {
- $buffer = compress_css($buffer);
- $buffer_error .= write_cache($buffer);
- $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
- $buffer_error .= '/* Send method: Cached, expired in: '.(($filemtime+CACHE_TIME)-time()).'s */';
- send_css_header(@file_get_contents(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile), $buffer_error);
- exit;
- }
- else
- {
- $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
- if ( (time()-$filemtime) >= CACHE_TIME )
- {
- @unlink(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
- $buffer = compress_css($buffer);
- $buffer_error .= write_cache($buffer);
- $filemtime = @filemtime(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile);
- }
- $buffer_error .= '/* Send method: Cached, expired in: '.(($filemtime+CACHE_TIME)-time()).'s */';
- send_css_header(@file_get_contents(CACHE_FOLDER . '/' . CACHE_FILE_SUFIX . $cssFile), $buffer_error);
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement