Now all modules are in core modules folder

This commit is contained in:
Manuel Cillero 2017-08-08 12:14:45 +02:00
parent 5ba1cdfa0b
commit 05b6a91b0c
1907 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,61 @@
<?php
/**
* @file
* Admin page callbacks for the advagg JS compression module.
*/
/**
* Page generation function for admin/settings/js-compress
*/
function advagg_js_compress_admin_page() {
$output = '';
return $output . drupal_get_form('advagg_js_compress_admin_settings_form');
}
/**
* Form builder; Configure advagg settings.
*
* @ingroup forms
* @see system_settings_form()
*/
function advagg_js_compress_admin_settings_form() {
$form = array();
$form['advagg_js_compress_agg_files'] = array(
'#type' => 'checkbox',
'#title' => t('Compress JS Files'),
'#default_value' => variable_get('advagg_js_compress_agg_files', ADVAGG_JS_COMPRESS_AGG_FILES),
);
$form['advagg_js_compress_inline'] = array(
'#type' => 'checkbox',
'#title' => t('Compress Inline JS'),
'#default_value' => variable_get('advagg_js_compress_inline', ADVAGG_JS_COMPRESS_INLINE),
);
$description = '';
$options = array(0 => t('JSMin+'));
if (function_exists('jsmin')) {
$options[1] = t('JSMin');
$description .= t('JSMin is the C complied version and is about 25 times faster. Recommend using it.');
}
else {
$description .= t('You can use the much faster C version of JSMin by installing the <a href="@php_jsmin">JSMin PHP Extension</a> on this server.', array('@php_jsmin' => 'http://www.ypass.net/software/php_jsmin/'));
}
$form['advagg_js_compressor'] = array(
'#type' => 'radios',
'#title' => t('Select the compression program to use'),
'#default_value' => variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR),
'#options' => $options,
'#description' => filter_xss($description),
);
$form['advagg_js_compress_packer_enable'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Packer'),
'#default_value' => variable_get('advagg_js_compress_packer_enable', ADVAGG_JS_COMPRESS_PACKER_ENABLE),
'#description' => t('If enabled the non gzip version of JS files will be compressed using the JS Packer. WARNING: This has a high chance of breaking your JS. Only Enable on production after testing the non gzipped version locally.'),
);
return system_settings_form($form);
}

View file

@ -0,0 +1,12 @@
name = AdvAgg Compress Javascript
description = Compress Javascript with a 3rd party compressor, JSMin+ currently.
package = Advanced CSS/JS Aggregation
core = 6.x
dependencies[] = advagg
; Information added by Drupal.org packaging script on 2017-03-18
version = "6.x-1.11"
core = "6.x"
project = "advagg"
datestamp = "1489800488"

View file

@ -0,0 +1,197 @@
<?php
/**
* @file
* Handles AdvAgg JS compress installation and upgrade tasks.
*/
/**
* Implementation of hook_enable().
*/
function advagg_js_compress_enable() {
// Flush advagg caches.
$cache_tables = advagg_flush_caches();
foreach ($cache_tables as $table) {
cache_clear_all('*', $table, TRUE);
}
// Check runtime requirements
register_shutdown_function('advagg_js_compress_requirements', 'runtime');
}
/**
* Implementation of hook_disable().
*/
function advagg_js_compress_disable() {
// Flush advagg caches.
$cache_tables = advagg_flush_caches();
foreach ($cache_tables as $table) {
cache_clear_all('*', $table, TRUE);
}
}
/**
* Implementation of hook_install().
*/
function advagg_js_compress_install() {
drupal_install_schema('advagg_js_compress');
}
/**
* Implementation of hook_uninstall().
*/
function advagg_js_compress_uninstall() {
// Remove variables.
variable_del('advagg_js_compress_packer_enable');
variable_del('advagg_js_compress_inline_cache');
variable_del('advagg_js_compress_file_cache');
variable_del('advagg_js_compress_agg_files');
variable_del('advagg_js_max_compress_ratio');
variable_del('advagg_js_compress_callback');
variable_del('advagg_js_compress_inline');
variable_del('advagg_js_compress_ratio');
variable_del('advagg_js_compressor');
// Remove our cache table.
cache_clear_all('*', 'cache_advagg_js_compress_inline', TRUE);
cache_clear_all('*', 'cache_advagg_js_compress_file', TRUE);
drupal_uninstall_schema('advagg_js_compress');
}
/**
* Implementation of hook_requirements().
*/
function advagg_js_compress_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time
$t = get_t();
// Report Drupal version
if ($phase == 'runtime') {
$compressible = advagg_js_compress_check_callback();
$advagg_js_compress_callback = variable_get('advagg_js_compress_callback', ADVAGG_JS_COMPRESS_CALLBACK);
if (is_array($compressible)) {
$requirements['advagg_js_compress_callback'] = array(
'title' => $t('AdvAgg JS Compress - Callback'),
'severity' => REQUIREMENT_WARNING,
'value' => $t('The callback for testing if a JS file is compressible is not working.'),
'description' => $t('As a result if jsmin+ encounters a file that it cannot compress, it will kill that PHP process.'),
);
if ($advagg_js_compress_callback != FALSE) {
variable_set('advagg_js_compress_callback', FALSE);
}
}
else {
$requirements['advagg_js_compress_callback'] = array(
'title' => $t('AdvAgg JS Compress - Callback'),
'severity' => REQUIREMENT_OK,
'value' => $t('The callback is working correctly.'),
);
if ($advagg_js_compress_callback == FALSE) {
variable_set('advagg_js_compress_callback', TRUE);
}
}
// Test the 'memory_limit' PHP configuration directive
$memory_limit = ini_get('memory_limit');
$compressor = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
// If $memory_limit contains a value of -1, the PHP runtime
// doesn't impose a limit on memory used by PHP scripts
if ($compressor == 0 && $memory_limit && $memory_limit != -1 && parse_size($memory_limit) < parse_size('96M')) {
$requirements['advagg_js_compress_memory_limit'] = array(
'title' => $t('AdvAgg JS Compress - Memory Limit'),
'value' => $memory_limit,
'severity' => REQUIREMENT_WARNING,
'description' => $t('It is highly recommended that you set your PHP memory_limit at least 96M if you are going to use JSMin+.'),
);
}
}
return $requirements;
}
/**
* Check to see if the CSS/JS generator is working.
*/
function advagg_js_compress_check_callback() {
$filename = drupal_get_path('module', 'advagg_js_compress') . '/jquery.form.js';
$files_to_test = array();
$files_to_test[] = array(
'md5' => md5($filename),
'filename' => $filename,
);
$compressible = advagg_js_compress_test_compression($files_to_test);
return $compressible;
}
/**
* Implementation of hook_schema().
*/
function advagg_js_compress_schema() {
$schema = array();
// Create cache tables.
$schema['cache_advagg_js_compress_inline'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_advagg_js_compress_inline']['description'] = t('Cache table for Advanced CSS/JS Aggregations JS Compress module. Used to keep inline versions of compressed JS.');
$schema['cache_advagg_js_compress_file'] = drupal_get_schema_unprocessed('system', 'cache');
$schema['cache_advagg_js_compress_file']['description'] = t('Cache table for Advanced CSS/JS Aggregations JS Compress module. Used to keep the compressed JavaScript from the js files.');
return $schema;
}
/**
* Update 6100 - Clear file cache.
*/
function advagg_js_compress_update_6100() {
$ret = array();
cache_clear_all('*', 'cache_advagg_files_data', TRUE);
$ret[] = array(
'success' => TRUE,
'query' => 'advagg files_data cache flushed.',
);
return $ret;
}
/**
* Update 6101 - Create the cache_advagg_css_compress_inline cache table.
*/
function advagg_js_compress_update_6101() {
$ret = array();
// Create cache table.
$schema = advagg_js_compress_schema();
db_create_table($ret, 'cache_advagg_js_compress_inline', $schema['cache_advagg_js_compress_inline']);
return $ret;
}
/**
* Update 6102 - Create the cache_advagg_css_compress_file cache table.
*/
function advagg_js_compress_update_6102() {
$ret = array();
// Create cache table.
$schema = advagg_js_compress_schema();
db_create_table($ret, 'cache_advagg_js_compress_file', $schema['cache_advagg_js_compress_file']);
return $ret;
}
/**
* Update 6103 - Clear the cache_advagg_css_compress_file cache table.
*/
function advagg_js_compress_update_6103() {
$ret = array();
// Clear cache_advagg_js_compress_file cache.
cache_clear_all('*', 'cache_advagg_js_compress_file', TRUE);
$ret[] = array(
'success' => TRUE,
'query' => 'The cache_advagg_js_compress_file table has been cleared.',
);
return $ret;
}

View file

@ -0,0 +1,616 @@
<?php
/**
* @file
* Advanced CSS/JS aggregation js compression module.
*
*/
/**
* Default value to see if the callback is working.
*/
define('ADVAGG_JS_COMPRESS_CALLBACK', FALSE);
/**
* Default value to see packer is enabled.
*/
define('ADVAGG_JS_COMPRESS_PACKER_ENABLE', FALSE);
/**
* Default value to see what compressor to use. 0 is JSMin+.
*/
define('ADVAGG_JS_COMPRESSOR', 0);
/**
* Default value for the compression ratio test.
*/
define('ADVAGG_JS_COMPRESS_RATIO', 0.1);
/**
* Default value for the compression ratio test.
*/
define('ADVAGG_JS_MAX_COMPRESS_RATIO', 0.98);
/**
* Default value to see if this will compress aggregated files.
*/
define('ADVAGG_JS_COMPRESS_AGG_FILES', TRUE);
/**
* Default value to see if this will compress inline js.
*/
define('ADVAGG_JS_COMPRESS_INLINE', TRUE);
/**
* Default value to see if this will cache the compressed inline js.
*/
define('ADVAGG_JS_COMPRESS_INLINE_CACHE', TRUE);
/**
* Default value to see if this will cache the compressed inline js.
*/
define('ADVAGG_JS_COMPRESS_FILE_CACHE', TRUE);
/**
* Implementation of hook_menu
*/
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;
}
/**
* Implement hook_init.
*/
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';
}
}
/**
* Implement hook_advagg_files_table.
*/
function advagg_js_compress_advagg_files_table($row, $checksum) {
// IF the file has changed, test it's compressibility.
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);
}
}
/**
* Implement hook_advagg_js_pre_alter.
*/
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]);
}
}
}
}
/**
* Implement hook_advagg_js_alter.
*/
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);
}
/**
* Implement hook_advagg_js_inline_alter.
*/
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 using a cache, try to get the contents of it.
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;
}
// Make sure the returned string is not empty or has a VERY high
// compression ratio.
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);
// Ensure that $contents ends with ; or }.
if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {
// ; or } not found, add in ; to the end of $contents.
$contents = trim($contents) . ';';
}
}
// If using a cache set it.
if (isset($key)) {
cache_set($key, $contents, $table, CACHE_TEMPORARY);
}
}
/**
* Compress a JS string
*
* @param $contents
* Javascript string.
*/
function advagg_js_compress_prep(&$contents, $files, $bundle_md5) {
// Make sure every file in this aggregate is compressible.
$files_to_test = array();
$list_bad = array();
foreach ($files as $filename) {
$filename_md5 = md5($filename);
$data = advagg_get_file_data($filename_md5);
// File needs to be tested.
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) {
// Send test files to worker.
if (!empty($files_to_test)) {
$compressible = advagg_js_compress_test_compression($files_to_test);
// If an array then it is a list of files that can not be compressed.
if (is_array($compressible)) {
// Place filename in an array key.
foreach ($compressible as $filedata) {
$filename = $filedata['filename'];
$list_bad[$filename] = $filename;
}
}
}
}
$contents = '';
// Do not compress the file that it bombs on.
// Compress each file individually.
foreach ($files as $file) {
if (!empty($list_bad[$file])) {
$contents .= advagg_build_js_bundle(array($file));
}
else {
$data = advagg_build_js_bundle(array($file));
// If using a cache, try to get the contents of it.
$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;
}
// Make sure the returned string is not empty or has a VERY high
// compression ratio.
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)) {
// If using a cache set it.
cache_set($key, $data, $table);
}
}
elseif ($compressor == 1) {
$contents = jsmin($contents);
// Ensure that $contents ends with ; or }.
if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {
// ; or } not found, add in ; to the end of $contents.
$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";
}
}
}
/**
* Compress a JS string using jsmin+
*
* @param $contents
* Javascript string.
* @return
* array with the size before and after.
*/
function advagg_js_compress_jsminplus(&$contents) {
// Try to allocate enough time to run JSMin+.
if (function_exists('set_time_limit')) {
@set_time_limit(variable_get('advagg_set_time_limit', ADVAGG_SET_TIME_LIMIT));
}
// Only include jsminplus.inc if the JSMinPlus class doesn't exist.
if (!class_exists('JSMinPlus')) {
include(drupal_get_path('module', 'advagg_js_compress') . '/jsminplus.inc');
}
// Get the JS string length before the compression operation.
$before = strlen($contents);
$original_contents = $contents;
try {
// Strip Byte Order Marks (BOM's) from the file, JSMin+ cannot parse these.
$contents = str_replace(pack("CCC", 0xef, 0xbb, 0xbf), "", $contents);
ob_start();
// JSMin+ the contents of the aggregated file.
$contents = JSMinPlus::minify($contents);
$error = trim(ob_get_contents());
if (!empty($error)) {
throw new Exception($error);
}
// Ensure that $contents ends with ; or }.
if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {
// ; or } not found, add in ; to the end of $contents.
$contents = trim($contents) . ';';
}
// Get the JS string length after the compression operation.
$after = strlen($contents);
}
catch (Exception $e) {
// Log the exception thrown by JSMin+ and roll back to uncompressed content.
watchdog('advagg', $e->getMessage() . '<pre>' . $original_contents . '</pre>', NULL, WATCHDOG_WARNING);
$contents = $original_contents;
$after = $before;
}
ob_end_clean();
return array($before, $after);
}
/**
* Run various theme functions so the cache is primed.
*
* @param $files_to_test
* array with md5 and filename.
* @return
* TRUE if all files are compressible. List of files that failed otherwise.
*/
function advagg_js_compress_test_compression($files_to_test) {
global $base_path;
$bad_files = array();
// Blacklist jquery.min.js from getting compressed.
if (module_exists('jquery_update')) {
foreach ($files_to_test as $key => $info) {
if (strpos($info['filename'], 'jquery.min.js') !== FALSE) {
// Add file to the bad list.
$bad_files[] = $info;
unset($files_to_test[$key]);
// Get file data.
$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);
// Set to -2
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);
}
// Clear the cache for this file
cache_clear_all($info['filename'], 'cache_advagg_js_compress_file');
// Setup request URL and headers.
$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);
// Get file data.
$filename_md5 = md5($info['filename']);
$data = advagg_get_file_data($filename_md5);
// Mark as a bad file.
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;
}
/**
* Run various theme functions so the cache is primed.
*
* @param $values
* object File info
*/
function advagg_js_compress_test_file($values = NULL) {
// watchdog('debug', str_replace(' ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($values, TRUE) . print_r($_REQUEST, TRUE)))));
// Exit if key does not match & called with $file not set.
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'];
// Compression test file if it exists.
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);
// Set to "-1" so if php bombs out, the file will be marked as bad.
$data['advagg_js_compress']['tested']['jsminplus'] = -1;
advagg_set_file_data($md5, $data);
// Compress the data.
list($before, $after) = advagg_js_compress_jsminplus($contents);
// Set to "-2" if compression ratio sucks.
$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;
}
// Set to "-3" if the compression ratio is way too good.
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;
}
// Everything worked, mark this file as compressible.
$data['advagg_js_compress']['tested']['jsminplus'] = 1;
advagg_set_file_data($md5, $data);
// Set the file cache.
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;
}
/**
* Save a string to the specified destination. Verify that file size is not zero.
*
* @param $data
* A string containing the contents of the file.
* @param $dest
* A string containing the destination location.
* @return
* Boolean indicating if the file save was successful.
*/
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);
}
// Get file save function
$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 first.
$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;
}
// Make sure filesize is not zero.
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)) {
// Filename is bad, create a new one next time.
file_delete($gzip_dest);
return FALSE;
}
}
}
// Use packer on JS data.
advagg_js_compress_jspacker($data);
// Write File.
if (!$file_save_data($data, $dest, FILE_EXISTS_REPLACE)) {
return FALSE;
}
// Make sure filesize is not zero.
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)) {
// Filename is bad, create a new one next time.
file_delete($dest);
return FALSE;
}
}
// Make sure .htaccess file exists.
advagg_htaccess_check_generate($dest);
cache_set($dest, time(), 'cache_advagg', CACHE_PERMANENT);
return TRUE;
}
/**
* Compress a JS string using packer.
*
* @param $contents
* Javascript string.
*/
function advagg_js_compress_jspacker(&$contents) {
// Use Packer on the contents of the aggregated file.
require_once(drupal_get_path('module', 'advagg_js_compress') . '/jspacker.inc');
// Add semicolons to the end of lines if missing.
$contents = str_replace("}\n", "};\n", $contents);
$contents = str_replace("\nfunction", ";\nfunction", $contents);
// Remove char returns, looking at you lightbox2.
$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();
}
/**
* Implementation of hook_flush_caches().
*/
function advagg_js_compress_flush_caches() {
return array('cache_advagg_js_compress_inline');
}
/**
* Implementation of hook_advagg_master_reset().
*/
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);
}

View file

@ -0,0 +1,660 @@
/*
* jQuery Form Plugin
* version: 2.36 (07-NOV-2009)
* @requires jQuery v1.2.6 or later
*
* Examples and documentation at: http://malsup.com/jquery/form/
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
;(function($) {
/*
Usage Note:
-----------
Do not use both ajaxSubmit and ajaxForm on the same form. These
functions are intended to be exclusive. Use ajaxSubmit if you want
to bind your own submit handler to the form. For example,
$(document).ready(function() {
$('#myForm').bind('submit', function() {
$(this).ajaxSubmit({
target: '#output'
});
return false; // <-- important!
});
});
Use ajaxForm when you want the plugin to manage all the event binding
for you. For example,
$(document).ready(function() {
$('#myForm').ajaxForm({
target: '#output'
});
});
When using ajaxForm, the ajaxSubmit function will be invoked for you
at the appropriate time.
*/
/**
* ajaxSubmit() provides a mechanism for immediately submitting
* an HTML form using AJAX.
*/
$.fn.ajaxSubmit = function(options) {
// fast fail if nothing selected (http://dev.jquery.com/ticket/2752)
if (!this.length) {
log('ajaxSubmit: skipping submit process - no element selected');
return this;
}
if (typeof options == 'function')
options = { success: options };
var url = $.trim(this.attr('action'));
if (url) {
// clean url (don't include hash vaue)
url = (url.match(/^([^#]+)/)||[])[1];
}
url = url || window.location.href || '';
options = $.extend({
url: url,
type: this.attr('method') || 'GET',
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank'
}, options || {});
// hook for manipulating the form data before it is extracted;
// convenient for use with rich editors like tinyMCE or FCKEditor
var veto = {};
this.trigger('form-pre-serialize', [this, options, veto]);
if (veto.veto) {
log('ajaxSubmit: submit vetoed via form-pre-serialize trigger');
return this;
}
// provide opportunity to alter form data before it is serialized
if (options.beforeSerialize && options.beforeSerialize(this, options) === false) {
log('ajaxSubmit: submit aborted via beforeSerialize callback');
return this;
}
var a = this.formToArray(options.semantic);
if (options.data) {
options.extraData = options.data;
for (var n in options.data) {
if(options.data[n] instanceof Array) {
for (var k in options.data[n])
a.push( { name: n, value: options.data[n][k] } );
}
else
a.push( { name: n, value: options.data[n] } );
}
}
// give pre-submit callback an opportunity to abort the submit
if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) {
log('ajaxSubmit: submit aborted via beforeSubmit callback');
return this;
}
// fire vetoable 'validate' event
this.trigger('form-submit-validate', [a, this, options, veto]);
if (veto.veto) {
log('ajaxSubmit: submit vetoed via form-submit-validate trigger');
return this;
}
var q = $.param(a);
if (options.type.toUpperCase() == 'GET') {
options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q;
options.data = null; // data is null for 'get'
}
else
options.data = q; // data is the query string for 'post'
var $form = this, callbacks = [];
if (options.resetForm) callbacks.push(function() { $form.resetForm(); });
if (options.clearForm) callbacks.push(function() { $form.clearForm(); });
// perform a load on the target only if dataType is not provided
if (!options.dataType && options.target) {
var oldSuccess = options.success || function(){};
callbacks.push(function(data) {
$(options.target).html(data).each(oldSuccess, arguments);
});
}
else if (options.success)
callbacks.push(options.success);
options.success = function(data, status) {
for (var i=0, max=callbacks.length; i < max; i++)
callbacks[i].apply(options, [data, status, $form]);
};
// are there files to upload?
var files = $('input:file', this).fieldValue();
var found = false;
for (var j=0; j < files.length; j++)
if (files[j])
found = true;
var multipart = false;
// var mp = 'multipart/form-data';
// multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp);
// options.iframe allows user to force iframe mode
// 06-NOV-09: now defaulting to iframe mode if file input is detected
if ((files.length && options.iframe !== false) || options.iframe || found || multipart) {
// hack to fix Safari hang (thanks to Tim Molendijk for this)
// see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d
if (options.closeKeepAlive)
$.get(options.closeKeepAlive, fileUpload);
else
fileUpload();
}
else
$.ajax(options);
// fire 'notify' event
this.trigger('form-submit-notify', [this, options]);
return this;
// private function for handling file uploads (hat tip to YAHOO!)
function fileUpload() {
var form = $form[0];
if ($(':input[name=submit]', form).length) {
alert(Drupal.t('Error: Form elements must not be named "submit".'));
return;
}
var opts = $.extend({}, $.ajaxSettings, options);
var s = $.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts);
var id = 'jqFormIO' + (new Date().getTime());
var $io = $('<iframe id="' + id + '" name="' + id + '" src="'+ opts.iframeSrc +'" />');
var io = $io[0];
$io.css({ position: 'absolute', top: '-1000px', left: '-1000px' });
var xhr = { // mock object
aborted: 0,
responseText: null,
responseXML: null,
status: 0,
statusText: 'n/a',
getAllResponseHeaders: function() {},
getResponseHeader: function() {},
setRequestHeader: function() {},
abort: function() {
this.aborted = 1;
$io.attr('src', opts.iframeSrc); // abort op in progress
}
};
var g = opts.global;
// trigger ajax global events so that activity/block indicators work like normal
if (g && ! $.active++) $.event.trigger("ajaxStart");
if (g) $.event.trigger("ajaxSend", [xhr, opts]);
if (s.beforeSend && s.beforeSend(xhr, s) === false) {
s.global && $.active--;
return;
}
if (xhr.aborted)
return;
var cbInvoked = 0;
var timedOut = 0;
// add submitting element to data if we know it
var sub = form.clk;
if (sub) {
var n = sub.name;
if (n && !sub.disabled) {
options.extraData = options.extraData || {};
options.extraData[n] = sub.value;
if (sub.type == "image") {
options.extraData[name+'.x'] = form.clk_x;
options.extraData[name+'.y'] = form.clk_y;
}
}
}
// take a breath so that pending repaints get some cpu time before the upload starts
setTimeout(function() {
// make sure form attrs are set
var t = $form.attr('target'), a = $form.attr('action');
// update form attrs in IE friendly way
form.setAttribute('target',id);
if (form.getAttribute('method') != 'POST')
form.setAttribute('method', 'POST');
if (form.getAttribute('action') != opts.url)
form.setAttribute('action', opts.url);
// ie borks in some cases when setting encoding
if (! options.skipEncodingOverride) {
$form.attr({
encoding: 'multipart/form-data',
enctype: 'multipart/form-data'
});
}
// support timout
if (opts.timeout)
setTimeout(function() { timedOut = true; cb(); }, opts.timeout);
// add "extra" data to form if provided in options
var extraInputs = [];
try {
if (options.extraData)
for (var n in options.extraData)
extraInputs.push(
$('<input type="hidden" name="'+n+'" value="'+options.extraData[n]+'" />')
.appendTo(form)[0]);
// add iframe to doc and submit the form
$io.appendTo('body');
io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false);
form.submit();
}
finally {
// reset attrs and remove "extra" input elements
form.setAttribute('action',a);
t ? form.setAttribute('target', t) : $form.removeAttr('target');
$(extraInputs).remove();
}
}, 10);
var domCheckCount = 50;
function cb() {
if (cbInvoked++) return;
io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false);
var ok = true;
try {
if (timedOut) throw 'timeout';
// extract the server response from the iframe
var data, doc;
doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document;
var isXml = opts.dataType == 'xml' || doc.XMLDocument || $.isXMLDoc(doc);
log('isXml='+isXml);
if (!isXml && (doc.body == null || doc.body.innerHTML == '')) {
if (--domCheckCount) {
// in some browsers (Opera) the iframe DOM is not always traversable when
// the onload callback fires, so we loop a bit to accommodate
cbInvoked = 0;
setTimeout(cb, 100);
return;
}
log('Could not access iframe DOM after 50 tries.');
return;
}
xhr.responseText = doc.body ? doc.body.innerHTML : null;
xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc;
xhr.getResponseHeader = function(header){
var headers = {'content-type': opts.dataType};
return headers[header];
};
if (opts.dataType == 'json' || opts.dataType == 'script') {
// see if user embedded response in textarea
var ta = doc.getElementsByTagName('textarea')[0];
if (ta)
xhr.responseText = ta.value;
else {
// account for browsers injecting pre around json response
var pre = doc.getElementsByTagName('pre')[0];
if (pre)
xhr.responseText = pre.innerHTML;
}
}
else if (opts.dataType == 'xml' && !xhr.responseXML && xhr.responseText != null) {
xhr.responseXML = toXml(xhr.responseText);
}
data = $.httpData(xhr, opts.dataType);
}
catch(e){
ok = false;
$.handleError(opts, xhr, 'error', e);
}
// ordering of these callbacks/triggers is odd, but that's how $.ajax does it
if (ok) {
opts.success(data, 'success');
if (g) $.event.trigger("ajaxSuccess", [xhr, opts]);
}
if (g) $.event.trigger("ajaxComplete", [xhr, opts]);
if (g && ! --$.active) $.event.trigger("ajaxStop");
if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error');
// clean up
setTimeout(function() {
$io.remove();
xhr.responseXML = null;
}, 100);
};
function toXml(s, doc) {
if (window.ActiveXObject) {
doc = new ActiveXObject('Microsoft.XMLDOM');
doc.async = 'false';
doc.loadXML(s);
}
else
doc = (new DOMParser()).parseFromString(s, 'text/xml');
return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null;
};
};
};
/**
* ajaxForm() provides a mechanism for fully automating form submission.
*
* The advantages of using this method instead of ajaxSubmit() are:
*
* 1: This method will include coordinates for <input type="image" /> elements (if the element
* is used to submit the form).
* 2. This method will include the submit element's name/value data (for the element that was
* used to submit the form).
* 3. This method binds the submit() method to the form for you.
*
* The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely
* passes the options argument along after properly binding events for submit elements and
* the form itself.
*/
$.fn.ajaxForm = function(options) {
return this.ajaxFormUnbind().bind('submit.form-plugin', function() {
$(this).ajaxSubmit(options);
return false;
}).bind('click.form-plugin', function(e) {
var target = e.target;
var $el = $(target);
if (!($el.is(":submit,input:image"))) {
// is this a child element of the submit el? (ex: a span within a button)
var t = $el.closest(':submit');
if (t.length == 0)
return;
target = t[0];
}
var form = this;
form.clk = target;
if (target.type == 'image') {
if (e.offsetX != undefined) {
form.clk_x = e.offsetX;
form.clk_y = e.offsetY;
} else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin
var offset = $el.offset();
form.clk_x = e.pageX - offset.left;
form.clk_y = e.pageY - offset.top;
} else {
form.clk_x = e.pageX - target.offsetLeft;
form.clk_y = e.pageY - target.offsetTop;
}
}
// clear form vars
setTimeout(function() { form.clk = form.clk_x = form.clk_y = null; }, 100);
});
};
// ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm
$.fn.ajaxFormUnbind = function() {
return this.unbind('submit.form-plugin click.form-plugin');
};
/**
* formToArray() gathers form element data into an array of objects that can
* be passed to any of the following ajax functions: $.get, $.post, or load.
* Each object in the array has both a 'name' and 'value' property. An example of
* an array for a simple login form might be:
*
* [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
*
* It is this array that is passed to pre-submit callback functions provided to the
* ajaxSubmit() and ajaxForm() methods.
*/
$.fn.formToArray = function(semantic) {
var a = [];
if (this.length == 0) return a;
var form = this[0];
var els = semantic ? form.getElementsByTagName('*') : form.elements;
if (!els) return a;
for(var i=0, max=els.length; i < max; i++) {
var el = els[i];
var n = el.name;
if (!n) continue;
if (semantic && form.clk && el.type == "image") {
// handle image inputs on the fly when semantic == true
if(!el.disabled && form.clk == el) {
a.push({name: n, value: $(el).val()});
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
}
continue;
}
var v = $.fieldValue(el, true);
if (v && v.constructor == Array) {
for(var j=0, jmax=v.length; j < jmax; j++)
a.push({name: n, value: v[j]});
}
else if (v !== null && typeof v != 'undefined')
a.push({name: n, value: v});
}
if (!semantic && form.clk) {
// input type=='image' are not found in elements array! handle it here
var $input = $(form.clk), input = $input[0], n = input.name;
if (n && !input.disabled && input.type == 'image') {
a.push({name: n, value: $input.val()});
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
}
}
return a;
};
/**
* Serializes form data into a 'submittable' string. This method will return a string
* in the format: name1=value1&amp;name2=value2
*/
$.fn.formSerialize = function(semantic) {
//hand off to jQuery.param for proper encoding
return $.param(this.formToArray(semantic));
};
/**
* Serializes all field elements in the jQuery object into a query string.
* This method will return a string in the format: name1=value1&amp;name2=value2
*/
$.fn.fieldSerialize = function(successful) {
var a = [];
this.each(function() {
var n = this.name;
if (!n) return;
var v = $.fieldValue(this, successful);
if (v && v.constructor == Array) {
for (var i=0,max=v.length; i < max; i++)
a.push({name: n, value: v[i]});
}
else if (v !== null && typeof v != 'undefined')
a.push({name: this.name, value: v});
});
//hand off to jQuery.param for proper encoding
return $.param(a);
};
/**
* Returns the value(s) of the element in the matched set. For example, consider the following form:
*
* <form><fieldset>
* <input name="A" type="text" />
* <input name="A" type="text" />
* <input name="B" type="checkbox" value="B1" />
* <input name="B" type="checkbox" value="B2"/>
* <input name="C" type="radio" value="C1" />
* <input name="C" type="radio" value="C2" />
* </fieldset></form>
*
* var v = $(':text').fieldValue();
* // if no values are entered into the text inputs
* v == ['','']
* // if values entered into the text inputs are 'foo' and 'bar'
* v == ['foo','bar']
*
* var v = $(':checkbox').fieldValue();
* // if neither checkbox is checked
* v === undefined
* // if both checkboxes are checked
* v == ['B1', 'B2']
*
* var v = $(':radio').fieldValue();
* // if neither radio is checked
* v === undefined
* // if first radio is checked
* v == ['C1']
*
* The successful argument controls whether or not the field element must be 'successful'
* (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls).
* The default value of the successful argument is true. If this value is false the value(s)
* for each element is returned.
*
* Note: This method *always* returns an array. If no valid value can be determined the
* array will be empty, otherwise it will contain one or more values.
*/
$.fn.fieldValue = function(successful) {
for (var val=[], i=0, max=this.length; i < max; i++) {
var el = this[i];
var v = $.fieldValue(el, successful);
if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length))
continue;
v.constructor == Array ? $.merge(val, v) : val.push(v);
}
return val;
};
/**
* Returns the value of the field element.
*/
$.fieldValue = function(el, successful) {
var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
if (typeof successful == 'undefined') successful = true;
if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
(t == 'checkbox' || t == 'radio') && !el.checked ||
(t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
tag == 'select' && el.selectedIndex == -1))
return null;
if (tag == 'select') {
var index = el.selectedIndex;
if (index < 0) return null;
var a = [], ops = el.options;
var one = (t == 'select-one');
var max = (one ? index+1 : ops.length);
for(var i=(one ? index : 0); i < max; i++) {
var op = ops[i];
if (op.selected) {
var v = op.value;
if (!v) // extra pain for IE...
v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value;
if (one) return v;
a.push(v);
}
}
return a;
}
return el.value;
};
/**
* Clears the form data. Takes the following actions on the form's input fields:
* - input text fields will have their 'value' property set to the empty string
* - select elements will have their 'selectedIndex' property set to -1
* - checkbox and radio inputs will have their 'checked' property set to false
* - inputs of type submit, button, reset, and hidden will *not* be effected
* - button elements will *not* be effected
*/
$.fn.clearForm = function() {
return this.each(function() {
$('input,select,textarea', this).clearFields();
});
};
/**
* Clears the selected form elements.
*/
$.fn.clearFields = $.fn.clearInputs = function() {
return this.each(function() {
var t = this.type, tag = this.tagName.toLowerCase();
if (t == 'text' || t == 'password' || tag == 'textarea')
this.value = '';
else if (t == 'checkbox' || t == 'radio')
this.checked = false;
else if (tag == 'select')
this.selectedIndex = -1;
});
};
/**
* Resets the form data. Causes all form elements to be reset to their original value.
*/
$.fn.resetForm = function() {
return this.each(function() {
// guard against an input with the name of 'reset'
// note that IE reports the reset function as an 'object'
if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
this.reset();
});
};
/**
* Enables or disables any matching elements.
*/
$.fn.enable = function(b) {
if (b == undefined) b = true;
return this.each(function() {
this.disabled = !b;
});
};
/**
* Checks/unchecks any matching checkboxes or radio buttons and
* selects/deselects and matching option elements.
*/
$.fn.selected = function(select) {
if (select == undefined) select = true;
return this.each(function() {
var t = this.type;
if (t == 'checkbox' || t == 'radio')
this.checked = select;
else if (this.tagName.toLowerCase() == 'option') {
var $sel = $(this).parent('select');
if (select && $sel[0] && $sel[0].type == 'select-one') {
// deselect all other options
$sel.find('option').selected(false);
}
this.selected = select;
}
});
};
// helper fn for console logging
// set $.fn.ajaxSubmit.debug to true to enable debug logging
function log() {
if ($.fn.ajaxSubmit.debug && window.console && window.console.log)
window.console.log('[jquery.form] ' + Array.prototype.join.call(arguments,''));
};
})(jQuery);

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,777 @@
<?php
/* 9 April 2008. version 1.1
*
* This is the php version of the Dean Edwards JavaScript's Packer,
* Based on :
*
* ParseMaster, version 1.0.2 (2005-08-19) Copyright 2005, Dean Edwards
* a multi-pattern parser.
* KNOWN BUG: erroneous behavior when using escapeChar with a replacement
* value that is a function
*
* packer, version 2.0.2 (2005-08-19) Copyright 2004-2005, Dean Edwards
*
* License: http://creativecommons.org/licenses/LGPL/2.1/
*
* Ported to PHP by Nicolas Martin.
*
* ----------------------------------------------------------------------
* changelog:
* 1.1 : correct a bug, '\0' packed then unpacked becomes '\'.
* ----------------------------------------------------------------------
*
* examples of usage :
* $myPacker = new JavaScriptPacker($script, 62, true, false);
* $packed = $myPacker->pack();
*
* or
*
* $myPacker = new JavaScriptPacker($script, 'Normal', true, false);
* $packed = $myPacker->pack();
*
* or (default values)
*
* $myPacker = new JavaScriptPacker($script);
* $packed = $myPacker->pack();
*
*
* params of the constructor :
* $script: the JavaScript to pack, string.
* $encoding: level of encoding, int or string :
* 0,10,62,95 or 'None', 'Numeric', 'Normal', 'High ASCII'.
* default: 62.
* $fastDecode: include the fast decoder in the packed result, boolean.
* default : true.
* $specialChars: if you are flagged your private and local variables
* in the script, boolean.
* default: false.
*
* The pack() method return the compressed JavasScript, as a string.
*
* see http://dean.edwards.name/packer/usage/ for more information.
*
* Notes :
* # need PHP 5 . Tested with PHP 5.1.2, 5.1.3, 5.1.4, 5.2.3
*
* # The packed result may be different than with the Dean Edwards
* version, but with the same length. The reason is that the PHP
* function usort to sort array don't necessarily preserve the
* original order of two equal member. The Javascript sort function
* in fact preserve this order (but that's not require by the
* ECMAScript standard). So the encoded keywords order can be
* different in the two results.
*
* # Be careful with the 'High ASCII' Level encoding if you use
* UTF-8 in your files...
*/
class JavaScriptPacker {
// constants
const IGNORE = '$1';
// validate parameters
private $_script = '';
private $_encoding = 62;
private $_fastDecode = true;
private $_specialChars = false;
private $LITERAL_ENCODING = array(
'None' => 0,
'Numeric' => 10,
'Normal' => 62,
'High ASCII' => 95,
);
public function __construct($_script, $_encoding = 62, $_fastDecode = true, $_specialChars = false) {
$this->_script = $_script . "\n";
if (array_key_exists($_encoding, $this->LITERAL_ENCODING)) {
$_encoding = $this->LITERAL_ENCODING[$_encoding];
}
$this->_encoding = min((int) $_encoding, 95);
$this->_fastDecode = $_fastDecode;
$this->_specialChars = $_specialChars;
}
public function pack() {
$this->_addParser('_basicCompression');
if ($this->_specialChars) {
$this->_addParser('_encodeSpecialChars');
}
if ($this->_encoding) {
$this->_addParser('_encodeKeywords');
}
// go!
return $this->_pack($this->_script);
}
// apply all parsing routines
private function _pack($script) {
for ($i = 0; isset($this->_parsers[$i]); $i++) {
$script = call_user_func(array(&$this, $this->_parsers[$i]), $script);
}
return $script;
}
// keep a list of parsing functions, they'll be executed all at once
private $_parsers = array();
private function _addParser($parser) {
$this->_parsers[] = $parser;
}
// zero encoding - just removal of white space and comments
private function _basicCompression($script) {
$parser = new ParseMaster();
// make safe
$parser->escapeChar = '\\';
// protect strings
$parser->add('/\'[^\'\\n\\r]*\'/', self::IGNORE);
$parser->add('/"[^"\\n\\r]*"/', self::IGNORE);
// remove comments
$parser->add('/\\/\\/[^\\n\\r]*[\\n\\r]/', ' ');
$parser->add('/\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\//', ' ');
// protect regular expressions
$parser->add('/\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)/', '$2'); // IGNORE
$parser->add('/[^\\w\\x24\\/\'"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?/', self::IGNORE);
// remove: ;;; doSomething();
if ($this->_specialChars) {
$parser->add('/;;;[^\\n\\r]+[\\n\\r]/');
}
// remove redundant semi-colons
$parser->add('/\\(;;\\)/', self::IGNORE); // protect for (;;) loops
$parser->add('/;+\\s*([};])/', '$2');
// apply the above
$script = $parser->exec($script);
// remove white-space
$parser->add('/(\\b|\\x24)\\s+(\\b|\\x24)/', '$2 $3');
$parser->add('/([+\\-])\\s+([+\\-])/', '$2 $3');
$parser->add('/\\s+/', '');
// done
return $parser->exec($script);
}
private function _encodeSpecialChars($script) {
$parser = new ParseMaster();
// replace: $name -> n, $$name -> na
$parser->add('/((\\x24+)([a-zA-Z$_]+))(\\d*)/',
array('fn' => '_replace_name')
);
// replace: _name -> _0, double-underscore (__name) is ignored
$regexp = '/\\b_[A-Za-z\\d]\\w*/';
// build the word list
$keywords = $this->_analyze($script, $regexp, '_encodePrivate');
// quick ref
$encoded = $keywords['encoded'];
$parser->add($regexp,
array(
'fn' => '_replace_encoded',
'data' => $encoded,
)
);
return $parser->exec($script);
}
private function _encodeKeywords($script) {
// escape high-ascii values already in the script (i.e. in strings)
if ($this->_encoding > 62) {
$script = $this->_escape95($script);
}
// create the parser
$parser = new ParseMaster();
$encode = $this->_getEncoder($this->_encoding);
// for high-ascii, don't encode single character low-ascii
$regexp = ($this->_encoding > 62) ? '/\\w\\w+/' : '/\\w+/';
// build the word list
$keywords = $this->_analyze($script, $regexp, $encode);
$encoded = $keywords['encoded'];
// encode
$parser->add($regexp,
array(
'fn' => '_replace_encoded',
'data' => $encoded,
)
);
if (empty($script)) {
return $script;
}
else {
//$res = $parser->exec($script);
//$res = $this->_bootStrap($res, $keywords);
//return $res;
return $this->_bootStrap($parser->exec($script), $keywords);
}
}
private function _analyze($script, $regexp, $encode) {
// analyse
// retreive all words in the script
$all = array();
preg_match_all($regexp, $script, $all);
$_sorted = array(); // list of words sorted by frequency
$_encoded = array(); // dictionary of word->encoding
$_protected = array(); // instances of "protected" words
$all = $all[0]; // simulate the javascript comportement of global match
if (!empty($all)) {
$unsorted = array(); // same list, not sorted
$protected = array(); // "protected" words (dictionary of word->"word")
$value = array(); // dictionary of charCode->encoding (eg. 256->ff)
$this->_count = array(); // word->count
$i = count($all);
$j = 0; //$word = null;
// count the occurrences - used for sorting later
do {
--$i;
$word = '$' . $all[$i];
if (!isset($this->_count[$word])) {
$this->_count[$word] = 0;
$unsorted[$j] = $word;
// make a dictionary of all of the protected words in this script
// these are words that might be mistaken for encoding
//if (is_string($encode) && method_exists($this, $encode))
$values[$j] = call_user_func(array(&$this, $encode), $j);
$protected['$' . $values[$j]] = $j++;
}
// increment the word counter
$this->_count[$word]++;
} while ($i > 0);
// prepare to sort the word list, first we must protect
// words that are also used as codes. we assign them a code
// equivalent to the word itself.
// e.g. if "do" falls within our encoding range
// then we store keywords["do"] = "do";
// this avoids problems when decoding
$i = count($unsorted);
do {
$word = $unsorted[--$i];
if (isset($protected[$word]) /*!= null*/) {
$_sorted[$protected[$word]] = substr($word, 1);
$_protected[$protected[$word]] = true;
$this->_count[$word] = 0;
}
} while ($i);
// sort the words by frequency
// Note: the javascript and php version of sort can be different :
// in php manual, usort :
// " If two members compare as equal,
// their order in the sorted array is undefined."
// so the final packed script is different of the Dean's javascript version
// but equivalent.
// the ECMAscript standard does not guarantee this behaviour,
// and thus not all browsers (e.g. Mozilla versions dating back to at
// least 2003) respect this.
usort($unsorted, array(&$this, '_sortWords'));
$j = 0;
// because there are "protected" words in the list
// we must add the sorted words around them
do {
if (!isset($_sorted[$i])) {
$_sorted[$i] = substr($unsorted[$j++], 1);
}
$_encoded[$_sorted[$i]] = $values[$i];
} while (++$i < count($unsorted));
}
return array(
'sorted' => $_sorted,
'encoded' => $_encoded,
'protected' => $_protected,
);
}
private $_count = array();
private function _sortWords($match1, $match2) {
return $this->_count[$match2] - $this->_count[$match1];
}
// build the boot function used for loading and decoding
private function _bootStrap($packed, $keywords) {
$ENCODE = $this->_safeRegExp('$encode\\($count\\)');
// $packed: the packed script
$packed = "'" . $this->_escape($packed) . "'";
// $ascii: base for encoding
$ascii = min(count($keywords['sorted']), $this->_encoding);
if ($ascii == 0) {
$ascii = 1;
}
// $count: number of words contained in the script
$count = count($keywords['sorted']);
// $keywords: list of words contained in the script
foreach ($keywords['protected'] as $i => $value) {
$keywords['sorted'][$i] = '';
}
// convert from a string to an array
ksort($keywords['sorted']);
$keywords = "'" . implode('|', $keywords['sorted']) . "'.split('|')";
$encode = ($this->_encoding > 62) ? '_encode95' : $this->_getEncoder($ascii);
$encode = $this->_getJSFunction($encode);
$encode = preg_replace('/_encoding/', '$ascii', $encode);
$encode = preg_replace('/arguments\\.callee/', '$encode', $encode);
$inline = '\\$count' . ($ascii > 10 ? '.toString(\\$ascii)' : '');
// $decode: code snippet to speed up decoding
if ($this->_fastDecode) {
// create the decoder
$decode = $this->_getJSFunction('_decodeBody');
if ($this->_encoding > 62) {
$decode = preg_replace('/\\\\w/', '[\\xa1-\\xff]', $decode);
}
// perform the encoding inline for lower ascii values
elseif ($ascii < 36) {
$decode = preg_replace($ENCODE, $inline, $decode);
}
// special case: when $count==0 there are no keywords. I want to keep
// the basic shape of the unpacking funcion so i'll frig the code...
if ($count == 0) {
$decode = preg_replace($this->_safeRegExp('($count)\\s*=\\s*1'), '$1=0', $decode, 1);
}
}
// boot function
$unpack = $this->_getJSFunction('_unpack');
if ($this->_fastDecode) {
// insert the decoder
$this->buffer = $decode;
$unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastDecode'), $unpack, 1);
}
$unpack = preg_replace('/"/', "'", $unpack);
if ($this->_encoding > 62) { // high-ascii
// get rid of the word-boundaries for regexp matches
$unpack = preg_replace('/\'\\\\\\\\b\'\s*\\+|\\+\s*\'\\\\\\\\b\'/', '', $unpack);
}
if ($ascii > 36 || $this->_encoding > 62 || $this->_fastDecode) {
// insert the encode function
$this->buffer = $encode;
$unpack = preg_replace_callback('/\\{/', array(&$this, '_insertFastEncode'), $unpack, 1);
}
else {
// perform the encoding inline
$unpack = preg_replace($ENCODE, $inline, $unpack);
}
// pack the boot function too
$unpackPacker = new JavaScriptPacker($unpack, 0, false, true);
$unpack = $unpackPacker->pack();
// arguments
$params = array($packed, $ascii, $count, $keywords);
if ($this->_fastDecode) {
$params[] = 0;
$params[] = '{}';
}
$params = implode(',', $params);
// the whole thing
return 'eval(' . $unpack . '(' . $params . "))\n";
}
private $buffer;
private function _insertFastDecode($match) {
return '{' . $this->buffer . ';';
}
private function _insertFastEncode($match) {
return '{$encode=' . $this->buffer . ';';
}
// mmm.. ..which one do i need ??
private function _getEncoder($ascii) {
return $ascii > 10 ? $ascii > 36 ? $ascii > 62 ?
'_encode95' : '_encode62' : '_encode36' : '_encode10';
}
// zero encoding
// characters: 0123456789
private function _encode10($charCode) {
return $charCode;
}
// inherent base36 support
// characters: 0123456789abcdefghijklmnopqrstuvwxyz
private function _encode36($charCode) {
return base_convert($charCode, 10, 36);
}
// hitch a ride on base36 and add the upper case alpha characters
// characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
private function _encode62($charCode) {
$res = '';
if ($charCode >= $this->_encoding) {
$res = $this->_encode62((int) ($charCode / $this->_encoding));
}
$charCode = $charCode % $this->_encoding;
if ($charCode > 35) {
return $res . chr($charCode + 29);
}
else {
return $res . base_convert($charCode, 10, 36);
}
}
// use high-ascii values
// characters: ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
private function _encode95($charCode) {
$res = '';
if ($charCode >= $this->_encoding) {
$res = $this->_encode95($charCode / $this->_encoding);
}
return $res . chr(($charCode % $this->_encoding) + 161);
}
private function _safeRegExp($string) {
return '/' . preg_replace('/\$/', '\\\$', $string) . '/';
}
private function _encodePrivate($charCode) {
return "_" . $charCode;
}
// protect characters used by the parser
private function _escape($script) {
return preg_replace('/([\\\\\'])/', '\\\$1', $script);
}
// protect high-ascii characters already in the script
private function _escape95($script) {
return preg_replace_callback(
'/[\\xa1-\\xff]/',
array(&$this, '_escape95Bis'),
$script
);
}
private function _escape95Bis($match) {
return '\x' . ((string) dechex(ord($match)));
}
private function _getJSFunction($aName) {
if (defined('self::JSFUNCTION' . $aName)) {
return constant('self::JSFUNCTION' . $aName);
}
else {
return '';
}
}
// JavaScript Functions used.
// Note : In Dean's version, these functions are converted
// with 'String(aFunctionName);'.
// This internal conversion complete the original code, ex :
// 'while (aBool) anAction();' is converted to
// 'while (aBool) { anAction(); }'.
// The JavaScript functions below are corrected.
// unpacking function - this is the boot strap function
// data extracted from this packing routine is passed to
// this function when decoded in the target
// NOTE ! : without the ';' final.
const JSFUNCTION_unpack =
'function($packed, $ascii, $count, $keywords, $encode, $decode) {
while ($count--) {
if ($keywords[$count]) {
$packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
}
}
return $packed;
}';
/*
'function($packed, $ascii, $count, $keywords, $encode, $decode) {
while ($count--)
if ($keywords[$count])
$packed = $packed.replace(new RegExp(\'\\\\b\' + $encode($count) + \'\\\\b\', \'g\'), $keywords[$count]);
return $packed;
}';
*/
// code-snippet inserted into the unpacker to speed up decoding
const JSFUNCTION_decodeBody =
//_decode = function() {
// does the browser support String.replace where the
// replacement value is a function?
' if (!\'\'.replace(/^/, String)) {
// decode all the values we need
while ($count--) {
$decode[$encode($count)] = $keywords[$count] || $encode($count);
}
// global replacement function
$keywords = [function ($encoded) {return $decode[$encoded]}];
// generic match
$encode = function () {return \'\\\\w+\'};
// reset the loop counter - we are now doing a global replace
$count = 1;
}
';
//};
/*
' if (!\'\'.replace(/^/, String)) {
// decode all the values we need
while ($count--) $decode[$encode($count)] = $keywords[$count] || $encode($count);
// global replacement function
$keywords = [function ($encoded) {return $decode[$encoded]}];
// generic match
$encode = function () {return\'\\\\w+\'};
// reset the loop counter - we are now doing a global replace
$count = 1;
}';
*/
// zero encoding
// characters: 0123456789
const JSFUNCTION_encode10 =
'function($charCode) {
return $charCode;
}'; //;';
// inherent base36 support
// characters: 0123456789abcdefghijklmnopqrstuvwxyz
const JSFUNCTION_encode36 =
'function($charCode) {
return $charCode.toString(36);
}'; //;';
// hitch a ride on base36 and add the upper case alpha characters
// characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
const JSFUNCTION_encode62 =
'function($charCode) {
return ($charCode < _encoding ? \'\' : arguments.callee(parseInt($charCode / _encoding))) +
(($charCode = $charCode % _encoding) > 35 ? String.fromCharCode($charCode + 29) : $charCode.toString(36));
}';
// use high-ascii values
// characters: ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ
const JSFUNCTION_encode95 =
'function($charCode) {
return ($charCode < _encoding ? \'\' : arguments.callee($charCode / _encoding)) +
String.fromCharCode($charCode % _encoding + 161);
}';
}
class ParseMaster {
public $ignoreCase = false;
public $escapeChar = '';
// constants
const EXPRESSION = 0;
const REPLACEMENT = 1;
const LENGTH = 2;
// used to determine nesting levels
private $GROUPS = '/\\(/'; //g
private $SUB_REPLACE = '/\\$\\d/';
private $INDEXED = '/^\\$\\d+$/';
private $TRIM = '/([\'"])\\1\\.(.*)\\.\\1\\1$/';
private $ESCAPE = '/\\\./'; //g
private $QUOTE = '/\'/';
private $DELETED = '/\\x01[^\\x01]*\\x01/'; //g
public function add($expression, $replacement = '') {
// count the number of sub-expressions
// - add one because each pattern is itself a sub-expression
$length = 1 + preg_match_all($this->GROUPS, $this->_internalEscape((string) $expression), $out);
// treat only strings $replacement
if (is_string($replacement)) {
// does the pattern deal with sub-expressions?
if (preg_match($this->SUB_REPLACE, $replacement)) {
// a simple lookup? (e.g. "$2")
if (preg_match($this->INDEXED, $replacement)) {
// store the index (used for fast retrieval of matched strings)
$replacement = (int) (substr($replacement, 1)) - 1;
}
else { // a complicated lookup (e.g. "Hello $2 $1")
// build a function to do the lookup
$quote = preg_match($this->QUOTE, $this->_internalEscape($replacement))
? '"' : "'";
$replacement = array(
'fn' => '_backReferences',
'data' => array(
'replacement' => $replacement,
'length' => $length,
'quote' => $quote,
),
);
}
}
}
// pass the modified arguments
if (!empty($expression)) {
$this->_add($expression, $replacement, $length);
}
else {
$this->_add('/^$/', $replacement, $length);
}
}
public function exec($string) {
// execute the global replacement
$this->_escaped = array();
// simulate the _patterns.toSTring of Dean
$regexp = '/';
foreach ($this->_patterns as $reg) {
$regexp .= '(' . substr($reg[self::EXPRESSION], 1, -1) . ')|';
}
$regexp = substr($regexp, 0, -1) . '/';
$regexp .= ($this->ignoreCase) ? 'i' : '';
$string = $this->_escape($string, $this->escapeChar);
$string = preg_replace_callback(
$regexp,
array(
&$this,
'_replacement',
),
$string
);
$string = $this->_unescape($string, $this->escapeChar);
return preg_replace($this->DELETED, '', $string);
}
public function reset() {
// clear the patterns collection so that this object may be re-used
$this->_patterns = array();
}
// private
private $_escaped = array(); // escaped characters
private $_patterns = array(); // patterns stored by index
// create and add a new pattern to the patterns collection
private function _add() {
$arguments = func_get_args();
$this->_patterns[] = $arguments;
}
// this is the global replace function (it's quite complicated)
private function _replacement($arguments) {
if (empty($arguments)) {
return '';
}
$i = 1;
$j = 0;
// loop through the patterns
while (isset($this->_patterns[$j])) {
$pattern = $this->_patterns[$j++];
// do we have a result?
if (isset($arguments[$i]) && ($arguments[$i] != '')) {
$replacement = $pattern[self::REPLACEMENT];
if (is_array($replacement) && isset($replacement['fn'])) {
if (isset($replacement['data'])) {
$this->buffer = $replacement['data'];
}
return call_user_func(array(&$this, $replacement['fn']), $arguments, $i);
}
elseif (is_int($replacement)) {
return $arguments[$replacement + $i];
}
$delete = ($this->escapeChar == '' ||
strpos($arguments[$i], $this->escapeChar) === false)
? '' : "\x01" . $arguments[$i] . "\x01";
return $delete . $replacement;
// skip over references to sub-expressions
}
else {
$i += $pattern[self::LENGTH];
}
}
}
private function _backReferences($match, $offset) {
$replacement = $this->buffer['replacement'];
$quote = $this->buffer['quote'];
$i = $this->buffer['length'];
while ($i) {
$replacement = str_replace('$' . $i--, $match[$offset + $i], $replacement);
}
return $replacement;
}
private function _replace_name($match, $offset) {
$length = strlen($match[$offset + 2]);
$start = $length - max($length - strlen($match[$offset + 3]), 0);
return substr($match[$offset + 1], $start, $length) . $match[$offset + 4];
}
private function _replace_encoded($match, $offset) {
return $this->buffer[$match[$offset]];
}
// php : we cannot pass additional data to preg_replace_callback,
// and we cannot use &$this in create_function, so let's go to lower level
private $buffer;
// encode escaped characters
private function _escape($string, $escapeChar) {
if ($escapeChar) {
$this->buffer = $escapeChar;
return preg_replace_callback(
'/\\' . $escapeChar . '(.)' . '/',
array(&$this, '_escapeBis'),
$string
);
}
else {
return $string;
}
}
private function _escapeBis($match) {
$this->_escaped[] = $match[1];
return $this->buffer;
}
// decode escaped characters
private function _unescape($string, $escapeChar) {
if ($escapeChar) {
$regexp = '/' . '\\' . $escapeChar . '/';
$this->buffer = array(
'escapeChar' => $escapeChar,
'i' => 0,
);
return preg_replace_callback(
$regexp,
array(&$this, '_unescapeBis'),
$string
);
}
else {
return $string;
}
}
private function _unescapeBis() {
if (isset($this->_escaped[$this->buffer['i']])
&& $this->_escaped[$this->buffer['i']] != '') {
$temp = $this->_escaped[$this->buffer['i']];
}
else {
$temp = '';
}
$this->buffer['i']++;
return $this->buffer['escapeChar'] . $temp;
}
private function _internalEscape($string) {
return preg_replace($this->ESCAPE, '', $string);
}
}