View source
<?php
define('ADVAGG_JS_COMPRESS_CALLBACK', FALSE);
define('ADVAGG_JS_COMPRESS_PACKER_ENABLE', FALSE);
define('ADVAGG_JS_COMPRESSOR', 0);
define('ADVAGG_JS_COMPRESS_RATIO', 0.1);
define('ADVAGG_JS_MAX_COMPRESS_RATIO', 0.98);
define('ADVAGG_JS_COMPRESS_AGG_FILES', TRUE);
define('ADVAGG_JS_COMPRESS_INLINE', TRUE);
define('ADVAGG_JS_COMPRESS_INLINE_CACHE', TRUE);
define('ADVAGG_JS_COMPRESS_FILE_CACHE', TRUE);
function advagg_js_compress_menu() {
$items = array();
$file_path = drupal_get_path('module', 'advagg_js_compress');
$items['advagg/js_compress_test_file'] = array(
'page callback' => 'advagg_js_compress_test_file',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['admin/settings/advagg/js-compress'] = array(
'title' => 'JS Compression',
'description' => 'Adjust JS Compression settings.',
'page callback' => 'advagg_js_compress_admin_page',
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'advagg_js_compress.admin.inc',
'weight' => 10,
);
return $items;
}
function advagg_js_compress_init() {
global $conf;
if (variable_get('advagg_js_compress_packer_enable', ADVAGG_JS_COMPRESS_PACKER_ENABLE)) {
$conf['advagg_file_save_function'] = 'advagg_js_compress_file_saver';
}
}
function advagg_js_compress_advagg_files_table($row, $checksum) {
if ($row['filetype'] === 'js' && $checksum !== $row['checksum']) {
$files_to_test[] = array(
'md5' => $row['filename_md5'],
'filename' => $row['filename'],
);
advagg_js_compress_test_compression($files_to_test);
}
}
function advagg_js_compress_advagg_js_pre_alter(&$javascript, $preprocess_js, $public_downloads, $scope) {
if (module_exists('jquery_update')) {
return;
}
foreach ($javascript as $type => $data) {
if (!$data) {
continue;
}
if ($type == 'setting' || $type == 'inline') {
continue;
}
foreach ($data as $path => $info) {
if ($path == 'misc/jquery.form.js') {
$new_path = drupal_get_path('module', 'advagg_js_compress') . '/jquery.form.js';
$javascript[$type][$new_path] = $info;
unset($javascript[$type][$path]);
}
}
}
}
function advagg_js_compress_advagg_js_alter(&$contents, $files, $bundle_md5) {
if (!variable_get('advagg_js_compress_agg_files', ADVAGG_JS_COMPRESS_AGG_FILES)) {
return;
}
advagg_js_compress_prep($contents, $files, $bundle_md5);
}
function advagg_js_compress_advagg_js_inline_alter(&$contents) {
if (!variable_get('advagg_js_compress_inline', ADVAGG_JS_COMPRESS_INLINE)) {
return;
}
$compressor = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
if (variable_get('advagg_js_compress_inline_cache', ADVAGG_JS_COMPRESS_INLINE_CACHE)) {
$key = md5($contents) . $compressor;
$table = 'cache_advagg_js_compress_inline';
$data = cache_get($key, $table);
if (!empty($data->data)) {
$contents = $data->data;
return;
}
}
if ($compressor == 0) {
$original_contents = $contents;
list($before, $after) = advagg_js_compress_jsminplus($contents);
$ratio = 0;
if ($before != 0) {
$ratio = ($before - $after) / $before;
}
if (empty($contents) || empty($ratio) || $ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
$contents = $original_contents;
}
}
if ($compressor == 1) {
$contents = jsmin($contents);
if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {
$contents = trim($contents) . ';';
}
}
if (isset($key)) {
cache_set($key, $contents, $table, CACHE_TEMPORARY);
}
}
function advagg_js_compress_prep(&$contents, $files, $bundle_md5) {
$files_to_test = array();
$list_bad = array();
foreach ($files as $filename) {
$filename_md5 = md5($filename);
$data = advagg_get_file_data($filename_md5);
if (empty($data['advagg_js_compress']['tested'])) {
$files_to_test[] = array(
'md5' => $filename_md5,
'filename' => $filename,
);
}
elseif ($data['advagg_js_compress']['tested']['jsminplus'] != 1) {
$list_bad[$filename] = $filename;
}
}
$advagg_js_compress_callback = variable_get('advagg_js_compress_callback', ADVAGG_JS_COMPRESS_CALLBACK);
if ($advagg_js_compress_callback) {
if (!empty($files_to_test)) {
$compressible = advagg_js_compress_test_compression($files_to_test);
if (is_array($compressible)) {
foreach ($compressible as $filedata) {
$filename = $filedata['filename'];
$list_bad[$filename] = $filename;
}
}
}
}
$contents = '';
foreach ($files as $file) {
if (!empty($list_bad[$file])) {
$contents .= advagg_build_js_bundle(array(
$file,
));
}
else {
$data = advagg_build_js_bundle(array(
$file,
));
$cached = FALSE;
if (variable_get('advagg_js_compress_file_cache', ADVAGG_JS_COMPRESS_FILE_CACHE)) {
$key = $file;
$table = 'cache_advagg_js_compress_file';
$cached_data = cache_get($key, $table);
if (!empty($cached_data->data)) {
$data = $cached_data->data;
$cached = TRUE;
}
}
if (!$cached && !empty($data)) {
$compressor = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
if ($compressor == 0) {
list($before, $after) = advagg_js_compress_jsminplus($data);
$ratio = 0;
if ($before != 0) {
$ratio = ($before - $after) / $before;
}
if (empty($data) || empty($ratio) || $ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
$data = advagg_build_js_bundle(array(
$file,
));
}
elseif (isset($key)) {
cache_set($key, $data, $table);
}
}
elseif ($compressor == 1) {
$contents = jsmin($contents);
if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {
$contents = trim($contents) . ';';
}
}
}
$url = url($file, array(
'absolute' => TRUE,
));
$contents .= "/* Source and licensing information for the line(s) below can be found at {$url}. */\n" . $data . ";\n/* Source and licensing information for the above line(s) can be found at {$url}. */\n";
}
}
}
function advagg_js_compress_jsminplus(&$contents) {
if (function_exists('set_time_limit')) {
@set_time_limit(variable_get('advagg_set_time_limit', ADVAGG_SET_TIME_LIMIT));
}
if (!class_exists('JSMinPlus')) {
include drupal_get_path('module', 'advagg_js_compress') . '/jsminplus.inc';
}
$before = strlen($contents);
$original_contents = $contents;
try {
$contents = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", $contents);
ob_start();
$contents = JSMinPlus::minify($contents);
$error = trim(ob_get_contents());
if (!empty($error)) {
throw new Exception($error);
}
if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {
$contents = trim($contents) . ';';
}
$after = strlen($contents);
} catch (Exception $e) {
watchdog('advagg', $e
->getMessage() . '<pre>' . $original_contents . '</pre>', NULL, WATCHDOG_WARNING);
$contents = $original_contents;
$after = $before;
}
ob_end_clean();
return array(
$before,
$after,
);
}
function advagg_js_compress_test_compression($files_to_test) {
global $base_path;
$bad_files = array();
if (module_exists('jquery_update')) {
foreach ($files_to_test as $key => $info) {
if (strpos($info['filename'], 'jquery.min.js') !== FALSE) {
$bad_files[] = $info;
unset($files_to_test[$key]);
$filename_md5 = md5($info['filename']);
$lock_name = 'advagg_set_file_data_' . $filename_md5;
if (!lock_acquire($lock_name, 10)) {
lock_wait($lock_name);
continue;
}
$data = advagg_get_file_data($filename_md5);
if (!isset($data->data['advagg_js_compress']['tested']['jsminplus']) || $data->data['advagg_js_compress']['tested']['jsminplus'] != -2) {
$data['advagg_js_compress']['tested']['jsminplus'] = -2;
advagg_set_file_data($filename_md5, $data);
}
lock_release($lock_name);
}
}
}
foreach ($files_to_test as $info) {
$key = variable_get('advagg_js_compress_url_key', FALSE);
if (empty($key)) {
$key = mt_rand();
variable_set('advagg_js_compress_url_key', $key);
}
cache_clear_all($info['filename'], 'cache_advagg_js_compress_file');
$query['values'] = $info;
$query['key'] = $key;
$query_string = http_build_query($query, '', '&');
$url = _advagg_build_url('advagg/js_compress_test_file');
$headers = array(
'Host' => $_SERVER['HTTP_HOST'],
'Content-Type' => 'application/x-www-form-urlencoded',
'Connection' => 'close',
);
$results = drupal_http_request($url, $headers, 'POST', $query_string);
$filename_md5 = md5($info['filename']);
$data = advagg_get_file_data($filename_md5);
if (empty($data['advagg_js_compress']['tested']['jsminplus']) || $data['advagg_js_compress']['tested']['jsminplus'] != 1) {
$bad_files[] = $info;
}
}
if (empty($bad_files)) {
return TRUE;
}
return $bad_files;
}
function advagg_js_compress_test_file($values = NULL) {
if (is_null($values)) {
if (empty($_POST['key']) || empty($_POST['values'])) {
return;
}
$key = variable_get('advagg_js_compress_url_key', FALSE);
if ($key != $_POST['key']) {
return;
}
$values = array();
$values['values'] = $_POST['values'];
}
$filename = $values['values']['filename'];
$md5 = $values['values']['md5'];
advagg_clearstatcache(TRUE, $filename);
if (file_exists($filename)) {
$contents = file_get_contents($filename);
$filesize = filesize($filename);
$lock_name = 'advagg_set_file_data_' . $md5;
if (!lock_acquire($lock_name, 45)) {
lock_wait($lock_name);
echo $md5;
exit;
}
$data = advagg_get_file_data($md5);
$data['advagg_js_compress']['tested']['jsminplus'] = -1;
advagg_set_file_data($md5, $data);
list($before, $after) = advagg_js_compress_jsminplus($contents);
$ratio = 0;
if ($before != 0) {
$ratio = ($before - $after) / $before;
}
if ($ratio < variable_get('advagg_js_compress_ratio', ADVAGG_JS_COMPRESS_RATIO)) {
$data['advagg_js_compress']['tested']['jsminplus'] = -2;
advagg_set_file_data($md5, $data);
lock_release($lock_name);
echo $md5;
exit;
}
if ($ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
$data['advagg_js_compress']['tested']['jsminplus'] = -3;
advagg_set_file_data($md5, $data);
lock_release($lock_name);
echo $md5;
exit;
}
$data['advagg_js_compress']['tested']['jsminplus'] = 1;
advagg_set_file_data($md5, $data);
if (variable_get('advagg_js_compress_file_cache', ADVAGG_JS_COMPRESS_FILE_CACHE)) {
$key = $filename;
$table = 'cache_advagg_js_compress_file';
cache_set($key, $contents, $table);
}
}
if (isset($lock_name)) {
lock_release($lock_name);
}
echo $md5;
exit;
}
function advagg_js_compress_file_saver($data, $dest, $force, $type) {
if ($type == 'css') {
return advagg_file_saver($data, $dest, $force, $type);
}
if (!variable_get('advagg_gzip_compression', ADVAGG_GZIP_COMPRESSION) || !extension_loaded('zlib')) {
return advagg_file_saver($data, $dest, $force, $type);
}
$file_save_data = 'file_save_data';
$custom_path = variable_get('advagg_custom_files_dir', ADVAGG_CUSTOM_FILES_DIR);
if (!empty($custom_path)) {
$file_save_data = 'advagg_file_save_data';
}
$gzip_dest = $dest . '.gz';
advagg_clearstatcache(TRUE, $gzip_dest);
if (!file_exists($gzip_dest) || $force) {
$gzip_data = gzencode($data, 9, FORCE_GZIP);
if (!$file_save_data($gzip_data, $gzip_dest, FILE_EXISTS_REPLACE)) {
return FALSE;
}
advagg_clearstatcache(TRUE, $gzip_dest);
if (@filesize($gzip_dest) == 0 && !empty($gzip_data)) {
if (!$file_save_data($gzip_data, $gzip_dest, FILE_EXISTS_REPLACE)) {
return FALSE;
}
advagg_clearstatcache(TRUE, $gzip_dest);
if (@filesize($gzip_dest) == 0 && !empty($gzip_data)) {
file_delete($gzip_dest);
return FALSE;
}
}
}
advagg_js_compress_jspacker($data);
if (!$file_save_data($data, $dest, FILE_EXISTS_REPLACE)) {
return FALSE;
}
advagg_clearstatcache(TRUE, $dest);
if (@filesize($dest) == 0 && !empty($data)) {
if (!$file_save_data($data, $dest, FILE_EXISTS_REPLACE)) {
return FALSE;
}
advagg_clearstatcache(TRUE, $dest);
if (@filesize($dest) == 0 && !empty($data)) {
file_delete($dest);
return FALSE;
}
}
advagg_htaccess_check_generate($dest);
cache_set($dest, time(), 'cache_advagg', CACHE_PERMANENT);
return TRUE;
}
function advagg_js_compress_jspacker(&$contents) {
require_once drupal_get_path('module', 'advagg_js_compress') . '/jspacker.inc';
$contents = str_replace("}\n", "};\n", $contents);
$contents = str_replace("\nfunction", ";\nfunction", $contents);
$contents = str_replace("\n\r", "", $contents);
$contents = str_replace("\r", "", $contents);
$contents = str_replace("\n", "", $contents);
$packer = new JavaScriptPacker($contents, 62, TRUE, FALSE);
$contents = $packer
->pack();
}
function advagg_js_compress_flush_caches() {
return array(
'cache_advagg_js_compress_inline',
);
}
function advagg_js_compress_advagg_master_reset() {
cache_clear_all('*', 'cache_advagg_js_compress_inline', TRUE);
cache_clear_all('*', 'cache_advagg_js_compress_file', TRUE);
}