|
Source for file class_crushJsCss.php
Documentation is available at class_crushJsCss.php
// It initializes session to capture $_SESSION['PRODUCTION'] var
* PHP Version of the Dean Edwards 's Packer de Dean Edwards, a JavaScript Compressor/Obfuscator.
* @link http://joliclic.free.fr/php/javascript-packer/en/
require_once('phpclasses/packer/class.JavaScriptPacker.php');
* CSSTidy is an opensource CSS parser and optimiser. It is available as executeable
* file (available for Windows, Linux and OSX) which can be controlled per command
* line and as PHP script (both with almost the same functionality).
* @link http://csstidy.sourceforge.net/index.php
require_once('phpclasses/csstidy/class.csstidy.php');
//This verifies if the class already was declared.
* Merge, compact, scramble and cache Javascript and CSS files
* This compact class and scrambles JavaScript files through Dean Edwards's
* algorithm implemented in a class PHP by Nicolas Martin. Also, it compacts and
* optimize files CSS through the class CSSTidy. Finally, it compacts everything
* through gzip. And to save bandwidth, it creates files cache.
* @author Antonio Jozzolino <info@sgd.com.br>
* @since Fri Aug 01 2008 08:25:51 GMT-0300
* @changed Wed Aug 06 2008 13:04:32 GMT-0300
* @link http://www.sgd.com.br
* @link http://dean.edwards.name/packer/
* @link http://joliclic.free.fr/php/javascript-packer/en/
* @link http://csstidy.sourceforge.net/index.php
* @link http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
* @link http://www.sgd.com.br/content/view/42/88888891/
* @link http://www.phpclasses.org/browse/package/4754.html
* @link http://www.sgd.com.br/component/option,com_docman/task,doc_download/gid,10/Itemid,34/
* @example examples/index.php Html
* @example examples/javascript.php Javascript
* @example examples/css.php CSS
* @var bolean Define to false for test phase
* @var string key [ "file"] absolute script path. Use $_SERVER["DOCUMENT_ROOT"] if your server permit it.
* @var bolean key[ "pack"] optional. True to scramble script, false to no modification.
* @var string Define scramble level . Values: 'None', 'Numeric', 'Normal', 'High ASCII'. 'Normal' is default value.
* @see http://dean.edwards.name/packer/usage/
* @var bolean True is default value.
* @see http://dean.edwards.name/packer/usage/
* @var bolean False is default value.
* @see http://dean.edwards.name/packer/usage/
* @var string absolute script path to cache directory. $_SERVER["DOCUMENT_ROOT"] ."/cache" is default value. This directory should have writing permission (chmod -c 775)
* @var string "javascript" or "css". "javascript " is default value.
var $type = 'javascript';
* @var string define which compact method browser client suports, gzip, deflare or none.
* @var string name of cache file.
* @var string compact method
* @var string compact method
* @var bolean If true, Remove unnecessary backslashes ("\"). False is default value.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean If true, compat "#000000" to "#000". True is default value.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean if true, change "bold" to "700". True is default value.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean if true, change all selector to lower case. Default is false.
* @see http://csstidy.sourceforge.net/usage.php
* @var numeric 0 = Dont't Optimise, 1 = Safe Optimisations, 2 = All Optisiations. Default is 1.
* @see http://csstidy.sourceforge.net/usage.php
* @see http://www.dustindiaz.com/css-shorthand/
* @var bolean if true, Remove last ";" . False is default.
* @see http://csstidy.sourceforge.net/usage.php
* @var numeric case for properties. 0 = no change, 1 = lowercase, 2 = uppercase. 0 is default.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean sort properties in ascendant order. False is default.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean sort selectors in ascendant order. False is default.
* @see http://csstidy.sourceforge.net/usage.php
* @var numeric 0 = Do not change anything, 1 = Only separate selectors (split at .), 2 = Merge selectors with same properties (fast). 2 is default.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean Discard invalid properties based at $tidy_cfg_discard_invalid_properties value. False is default.
* @see http://csstidy.sourceforge.net/usage.php
* @var string Possible values are 'CSS1.0', 'CSS2.0', 'CSS2.1'. Default is 'CSS2.1'
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean. Save comments, hacks, etc. Most optimisations can *not* be applied if this is enabled. Default is false.
* @see http://csstidy.sourceforge.net/usage.php
* @var bolean. Insert a first line with a comment like this: "CSSTidy 1.3dev: Tue, 05 Aug 2008 14:56:53 +0200" . Default is false.
* @see http://csstidy.sourceforge.net/usage.php
* This function set defaults values and determine client browser capabilities.
// Determine supported compression method
$this->gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
$this->deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
// Determine used compression method
// Check for buggy versions of Internet Explorer
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Opera') &&
preg_match('/^Mozilla\/4\.0 \(compatible; MSIE ([0-9]\.[0-9])/i', $_SERVER['HTTP_USER_AGENT'], $matches)) {
if ($version == 6 && !strstr($_SERVER['HTTP_USER_AGENT'], 'EV1'))
* Merge, compact and scramble javascript and css files
* This function first checks whether the file needs new version or the version stored in cache can be used.
* If a new file should be created, the function checks the variable $type to determine if a file is javascript or css.
* The function also check the $production and $scripts["pack"], to determine whether the file should be compressed.
* All css or javascripts files are merged into a single file and submitted to function gzipIt, which compact and return the content according to the client browser´s capabilities.
* @return string string or binary content
if($this->type == 'javascript'){
$str .= "\n" . (preg_replace('/[\n\r]/', ";\n", $packer->pack()));
$str .= "\n" . $css->print->plain();
* It verifies cache´s contents
* It verifies that cache is valid. If yes, the version of the cache is sent. If the client's browser already has the file, nothing is sent.
* @return string or binary content
$size += strlen($file['file']);
sort($chaged, SORT_NUMERIC);
// Try the cache first to see if the combined files were already generated
header ("Etag: \"" . $hash . "\"");
if (isset ($_SERVER['HTTP_IF_NONE_MATCH']) &&
// Return visit and no modifications, so do not send anything
header ("HTTP/1.0 304 Not Modified");
header ('Vary: Accept-Encoding');
* Compact javascript and css files and save in cache directory
* It ensures the client's browser accepts compressed files. If yes, return the file to accept compaction.
* If not, return the file without compress. Save the file in the cache for later use.
* @param string $contents
* @return string string or binary content
// Send compressed contents
$contents = gzencode($contents, 9, $this->gzip ? FORCE_GZIP : FORCE_DEFLATE);
header ('Vary: Accept-Encoding');
|