View source
<?php
define('ADVAGG_SPACE', '__');
define('ADVAGG_ENABLED', TRUE);
define('ADVAGG_GZIP', TRUE);
if (module_exists('advagg_bundler') && variable_get('advagg_bundler_active', TRUE)) {
define('ADVAGG_CORE_GROUPS', FALSE);
}
else {
define('ADVAGG_CORE_GROUPS', TRUE);
}
define('ADVAGG_CACHE_LEVEL', 1);
define('ADVAGG_GLOBAL_COUNTER', 0);
define('ADVAGG_USE_HTTPRL', FALSE);
define('ADVAGG_COMBINE_CSS_MEDIA', FALSE);
define('ADVAGG_IE_CSS_SELECTOR_LIMITER', FALSE);
define('ADVAGG_IE_CSS_SELECTOR_LIMITER_VALUE', 4095);
define('ADVAGG_ADMIN_CONFIG_ROOT_PATH', 'admin/config/development/performance');
define('ADVAGG_DEBUG', FALSE);
define('ADVAGG_JS_FIX_TYPE', TRUE);
define('ADVAGG_CSS_FIX_TYPE', TRUE);
define('ADVAGG_HTACCESS_CHECK_GENERATE', TRUE);
define('ADVAGG_SHOW_BYPASS_COOKIE_MESSAGE', TRUE);
define('ADVAGG_URL_INBOUND_ALTER', TRUE);
define('ADVAGG_SCRIPTS_SCOPE_ANYWHERE', FALSE);
define('ADVAGG_CLEAR_SCRIPTS', TRUE);
define('ADVAGG_NEEDS_UPDATE', FALSE);
define('ADVAGG_CRON_FREQUENCY', 82800);
define('ADVAGG_REMOVE_MISSING_FILES_FROM_DB_TIME', 1209600);
define('ADVAGG_REMOVE_OLD_UNUSED_AGGREGATES_TIME', 3888000);
define('ADVAGG_RUN_ALTER_AFTER_THEME', TRUE);
define('ADVAGG_FAST_FILESYSTEM', TRUE);
define('ADVAGG_PREGENERATE_AGGREGATE_FILES', TRUE);
define('ADVAGG_INCLUDE_BASE_URL', FALSE);
define('ADVAGG_CONVERT_ABSOLUTE_TO_RELATIVE_PATH', TRUE);
define('ADVAGG_CONVERT_ABSOLUTE_TO_PROTOCOL_RELATIVE_PATH', TRUE);
define('ADVAGG_FORCE_HTTPS_PATH', FALSE);
define('ADVAGG_CSS_ABSOLUTE_PATH', FALSE);
define('ADVAGG_CSS_IN_JS', FALSE);
define('ADVAGG_AJAX_RENDER_ALTER', TRUE);
define('ADVAGG_AUTH_BASIC_USER', '');
define('ADVAGG_AUTH_BASIC_PASS', '');
define('ADVAGG_SKIP_FAR_FUTURE_CHECK', FALSE);
define('ADVAGG_SKIP_ENABLED_PREPROCESS_CHECK', FALSE);
define('ADVAGG_RESOURCE_HINTS_DNS_PREFETCH', FALSE);
define('ADVAGG_RESOURCE_HINTS_PRECONNECT', FALSE);
define('ADVAGG_RESOURCE_HINTS_PRELOAD', FALSE);
define('ADVAGG_RESOURCE_HINTS_LOCATION', 1);
define('ADVAGG_SERIALIZE', 'json_encode');
define('ADVAGG_ROOT_DIR_PREFIX', 'public://');
define('ADVAGG_SKIP_GZIP_CHECK', FALSE);
if (module_exists('cdn')) {
define('ADVAGG_SKIP_FILE_CREATE_URL_INSIDE_CSS', FALSE);
}
else {
define('ADVAGG_SKIP_FILE_CREATE_URL_INSIDE_CSS', TRUE);
}
define('ADVAGG_SHOW_FILE_CHANGED_MESSAGE', TRUE);
define('ADVAGG_FILE_READ_FAILURE_TIMEOUT', 1800);
define('ADVAGG_STRICT_MTIME_CHECK', TRUE);
if (function_exists('brotli_compress')) {
define('ADVAGG_BROTLI', TRUE);
}
else {
define('ADVAGG_BROTLI', FALSE);
}
define('ADVAGG_NO_ZOPFLI', FALSE);
define('ADVAGG_SKIP_304_CHECK', FALSE);
define('ADVAGG_RESOURCE_HINTS_USE_IMMUTABLE', TRUE);
define('ADVAGG_CHROME_HEADER_ENABLED', FALSE);
define('ADVAGG_HTACCESS_SYMLINKSIFOWNERMATCH', FALSE);
define('ADVAGG_JS_HEADER_LENGTH', 24576);
define('ADVAGG_HTACCESS_REWRITEBASE', '');
define('ADVAGG_RESOURCE_HINTS_PRELOAD_MAX_SIZE', 3072);
define('ADVAGG_CSS_REMOVE_EMPTY_FILES', FALSE);
define('ADVAGG_JS_REMOVE_EMPTY_FILES', FALSE);
define('ADVAGG_DISABLE_ON_ADMIN', FALSE);
define('ADVAGG_HTTP_200_CODE', 200);
define('ADVAGG_WEAK_FILE_VERIFICATION', FALSE);
define('ADVAGG_NO_LOCKS', FALSE);
define('ADVAGG_HTML_HEAD_IN_CSS_LOCATION', FALSE);
define('ADVAGG_ADMIN_MODE', 4);
define('ADVAGG_FARFUTURE_PHP', FALSE);
define('ADVAGG_DISABLE_ON_LISTED_PAGES', '');
define('ADVAGG_REMOTE_VERSION_CHECK', TRUE);
function advagg_help($path, $arg) {
switch ($path) {
case 'admin/help#advagg':
$filepath = dirname(__FILE__) . '/README.txt';
if (file_exists($filepath)) {
$readme = file_get_contents($filepath);
}
if (!isset($readme)) {
return NULL;
}
if (module_exists('markdown')) {
$filters = module_invoke('markdown', 'filter_info');
$info = $filters['filter_markdown'];
if (function_exists($info['process callback'])) {
$output = $info['process callback']($readme, NULL);
}
else {
$output = '<pre>' . $readme . '</pre>';
}
}
else {
$output = '<pre>' . $readme . '</pre>';
}
return $output;
}
}
function advagg_block_view_alter(&$data, $block) {
if (!advagg_enabled()) {
return;
}
if (!variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
return;
}
if (empty($data) || empty($data['content'])) {
return;
}
$block_info = $block->module . ':' . $block->delta;
$prefix = "<!-- AdvAgg block:prefix:{$block_info} tag -->";
$suffix = "<!-- AdvAgg block:suffix:{$block_info} tag -->";
if (is_string($data['content'])) {
$data['content'] = $prefix . $data['content'] . $suffix;
}
else {
if (!isset($data['content']['#prefix'])) {
$data['content']['#prefix'] = '';
}
$data['content']['#prefix'] .= $prefix;
if (!isset($data['content']['#suffix'])) {
$data['content']['#suffix'] = '';
}
$data['content']['#suffix'] .= $suffix;
}
}
function advagg_views_pre_render(&$view) {
if (!advagg_enabled()) {
return;
}
if (!variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
return;
}
$info = "{$view->name}:{$view->current_display}";
$prefix = "<!-- AdvAgg view:prefix:{$info} tag -->";
$suffix = "<!-- AdvAgg view:suffix:{$info} tag -->";
if (!isset($view->attachment_before)) {
$view->attachment_before = '';
}
$view->attachment_before .= $prefix;
if (!isset($view->attachment_after)) {
$view->attachment_after = '';
}
$view->attachment_after .= $suffix;
}
function advagg_panels_pre_render($panels_display, &$renderer) {
if (!advagg_enabled()) {
return;
}
if (!variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
return;
}
$info = "{$panels_display->layout}:{$panels_display->css_id}";
$prefix = "<!-- AdvAgg panels:prefix:{$info} tag -->";
$suffix = "<!-- AdvAgg panels:suffix:{$info} tag -->";
if (!isset($renderer->prefix)) {
$renderer->prefix = '';
}
$renderer->prefix .= $prefix;
if (!isset($renderer->suffix)) {
$renderer->suffix = '';
}
$renderer->suffix .= $suffix;
}
function advagg_url_inbound_alter(&$path, $original_path, $path_language) {
if (!variable_get('advagg_url_inbound_alter', ADVAGG_URL_INBOUND_ALTER)) {
return;
}
$already_ran =& drupal_static(__FUNCTION__);
if (!isset($already_ran)) {
$already_ran = array();
}
$request_path = request_path();
if (array_key_exists($request_path, $already_ran)) {
$path = $already_ran[$request_path];
return;
}
if (!empty($path) && $path != $request_path && advagg_match_file_pattern($request_path)) {
$advagg_path = advagg_get_root_files_dir();
$top_level = substr($advagg_path[0][1], 0, strpos($advagg_path[0][1], 'advagg_css'));
$start = strpos($request_path, $top_level . 'advagg_');
if ($start === 0) {
$path = substr($request_path, $start);
$already_ran[$request_path] = $path;
}
else {
$language_list = language_list();
$prefixes = array();
foreach ($language_list as $lang) {
if ($lang->enabled && !empty($lang->prefix) && strpos($request_path, $lang->prefix) !== FALSE) {
$prefixes[$lang->prefix] = $lang->prefix;
}
}
if (!empty($prefixes)) {
$substr_to_shrink = substr($request_path, 0, $start);
foreach ($prefixes as $prefix) {
$substr_to_shrink = str_replace($prefix . '/', '', $substr_to_shrink);
}
$path = $substr_to_shrink . substr($request_path, $start);
$already_ran[$request_path] = $path;
}
}
}
}
function advagg_hook_info() {
$advagg_hooks = array(
'advagg_get_css_file_contents_pre_alter',
'advagg_get_css_file_contents_alter',
'advagg_get_js_file_contents_alter',
'advagg_get_css_aggregate_contents_alter',
'advagg_get_js_aggregate_contents_alter',
'advagg_save_aggregate_pre_alter',
'advagg_save_aggregate_alter',
'advagg_build_aggregate_plans_alter',
'advagg_build_aggregate_plans_post_alter',
'advagg_css_groups_alter',
'advagg_js_groups_alter',
'advagg_modify_css_pre_render_alter',
'advagg_modify_js_pre_render_alter',
'advagg_changed_files',
'advagg_removed_aggregates',
'advagg_scan_for_changes',
'advagg_get_info_on_files_alter',
'advagg_context_alter',
'advagg_missing_root_file',
);
$hooks = array();
foreach ($advagg_hooks as $hook) {
$hooks[$hook] = array(
'group' => 'advagg',
);
}
return $hooks;
}
function advagg_module_implements_alter(&$implementations, $hook) {
if ($hook === 'theme_registry_alter' && array_key_exists('advagg', $implementations)) {
$item = array(
'advagg' => $implementations['advagg'],
);
unset($implementations['advagg']);
$implementations = array_merge($item, $implementations);
}
if ($hook === 'ajax_render_alter' && array_key_exists('advagg', $implementations)) {
$item = array(
'advagg' => $implementations['advagg'],
);
unset($implementations['advagg']);
$implementations = array_merge($item, $implementations);
}
if ($hook === 'element_info_alter' && array_key_exists('advagg', $implementations)) {
$item = $implementations['advagg'];
unset($implementations['advagg']);
$implementations['advagg'] = $item;
}
if ($hook === 'js_alter' && array_key_exists('locale', $implementations)) {
unset($implementations['locale']);
$implementations['_advagg_locale'] = FALSE;
}
if ($hook === 'file_url_alter' && array_key_exists('advagg', $implementations)) {
$item = $implementations['advagg'];
unset($implementations['advagg']);
$implementations['advagg'] = $item;
}
if ($hook === 'requirements') {
if (array_key_exists('advagg', $implementations)) {
$item = $implementations['advagg'];
unset($implementations['advagg']);
$implementations['advagg'] = $item;
}
if (array_key_exists('advagg_css_cdn', $implementations)) {
$item = $implementations['advagg_css_cdn'];
unset($implementations['advagg_css_cdn']);
$implementations['advagg_css_cdn'] = $item;
}
if (array_key_exists('advagg_css_compress', $implementations)) {
$item = $implementations['advagg_css_compress'];
unset($implementations['advagg_css_compress']);
$implementations['advagg_css_compress'] = $item;
}
if (array_key_exists('advagg_js_cdn', $implementations)) {
$item = $implementations['advagg_js_cdn'];
unset($implementations['advagg_js_cdn']);
$implementations['advagg_js_cdn'] = $item;
}
if (array_key_exists('advagg_js_compress', $implementations)) {
$item = $implementations['advagg_js_compress'];
unset($implementations['advagg_js_compress']);
$implementations['advagg_js_compress'] = $item;
}
}
if ($hook === 'cron' && array_key_exists('advagg', $implementations)) {
$item = $implementations['advagg'];
unset($implementations['advagg']);
$implementations['advagg'] = $item;
}
}
function _advagg_locale_js_alter(&$js) {
$name = 'javascript_parsed';
$parsed = variable_get($name, array());
if (empty($parsed)) {
$variables = array_map('unserialize', db_query('SELECT name, value FROM {variable} WHERE name = :name', array(
':name' => $name,
))
->fetchAllKeyed());
if (!empty($variables[$name])) {
$GLOBALS['conf'][$name] = $variables[$name];
}
}
$dir = 'public://' . variable_get('locale_js_directory', 'languages');
$new_files = FALSE;
if (!empty($parsed['refresh:' . $GLOBALS['language']->language])) {
$new_files = TRUE;
}
if (empty($new_files)) {
foreach ($js as $item) {
if ($item['type'] === 'file' && !in_array($item['data'], $parsed) && substr($item['data'], 0, strlen($dir)) != $dir) {
$new_files = TRUE;
break;
}
}
}
if (empty($new_files)) {
advagg_locale_js_add_translations($js, $dir);
return;
}
$count = 0;
while (!lock_acquire('locale_js_alter', 10)) {
++$count;
if ($count > 3) {
lock_release('locale_js_alter');
advagg_locale_js_add_translations($js, $dir);
drupal_page_is_cacheable(FALSE);
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) > 1) {
$GLOBALS['conf']['advagg_cache_level'] = 0;
}
return;
}
lock_wait('locale_js_alter');
}
try {
locale_js_alter($js);
} catch (PDOException $e) {
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
watchdog('advagg', 'Development Mode - Caught PDO Exception: <code>@info</code>', array(
'@info' => $e,
));
}
}
lock_release('locale_js_alter');
}
function advagg_system_info_alter(&$info, $file, $type) {
$config_path =& drupal_static(__FUNCTION__);
if (empty($config_path)) {
$config_path = advagg_admin_config_root_path();
}
if (!empty($info['configure']) && strpos($info['configure'], '/advagg') !== FALSE && (!empty($info['dependencies']) && is_array($info['dependencies']) && in_array('advagg', $info['dependencies']) || $file->name === 'advagg')) {
$pos = strpos($info['configure'], '/advagg') + 7;
$substr = substr($info['configure'], 0, $pos);
$info['configure'] = str_replace($substr, $config_path . '/advagg', $info['configure']);
}
}
function advagg_permission() {
return array(
'bypass advanced aggregation' => array(
'title' => t('bypass advanced aggregation'),
'description' => t('User can use URL query strings to bypass AdvAgg.'),
),
);
}
function advagg_file_url_alter(&$original_uri) {
if (strpos($original_uri, '/advagg_') === FALSE || !advagg_match_file_pattern($original_uri)) {
return;
}
if (variable_get('maintenance_mode', FALSE) || !module_exists('cdn') || !variable_get(CDN_BASIC_FARFUTURE_VARIABLE, CDN_BASIC_FARFUTURE_DEFAULT) || variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC) != CDN_MODE_BASIC || strpos($original_uri, 'cdn/farfuture/') === FALSE) {
return;
}
$original_uri = preg_replace('/cdn\\/farfuture\\/[A-Za-z0-9-_]{43}\\/[A-Za-z]+\\:[A-Za-z0-9-_]+\\//', '', $original_uri);
}
function advagg_menu() {
list($css_path, $js_path) = advagg_get_root_files_dir();
$file_path = drupal_get_path('module', 'advagg');
$config_path = advagg_admin_config_root_path();
$path_defined = FALSE;
if (advagg_s3fs_evaluate_no_rewrite_cssjs(FALSE)) {
$external_css = trim(parse_url(str_replace('/test.css', '/%', file_create_url($css_path[0] . '/test.css')), PHP_URL_PATH));
if (strpos($external_css, $GLOBALS['base_path']) === 0) {
$external_css = substr($external_css, strlen($GLOBALS['base_path']));
}
$external_js = trim(parse_url(str_replace('/test.js', '/%', file_create_url($js_path[0] . '/test.js')), PHP_URL_PATH));
if (strpos($external_js, $GLOBALS['base_path']) === 0) {
$external_js = substr($external_js, strlen($GLOBALS['base_path']));
}
$items[$external_css] = array(
'title' => "Generate CSS Aggregate",
'page callback' => 'advagg_missing_aggregate',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => $file_path,
'file' => 'advagg.missing.inc',
);
$items[$external_js] = array(
'title' => "Generate JS Aggregate",
'page callback' => 'advagg_missing_aggregate',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => $file_path,
'file' => 'advagg.missing.inc',
);
$path_defined = TRUE;
}
if (!$path_defined) {
$items[$css_path[1] . '/%'] = array(
'title' => "Generate CSS Aggregate",
'page callback' => 'advagg_missing_aggregate',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => $file_path,
'file' => 'advagg.missing.inc',
);
$items[$js_path[1] . '/%'] = array(
'title' => "Generate JS Aggregate",
'page callback' => 'advagg_missing_aggregate',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => $file_path,
'file' => 'advagg.missing.inc',
);
}
$advagg_additional_generate_paths = variable_get('advagg_additional_generate_paths', array());
if (!empty($advagg_additional_generate_paths)) {
foreach ($advagg_additional_generate_paths as $path) {
$items[$path] = array(
'title' => "Generate CSS/JS Aggregate",
'page callback' => 'advagg_missing_aggregate',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
'file path' => $file_path,
'file' => 'advagg.missing.inc',
);
}
}
$items[$config_path . '/default'] = array(
'title' => 'Performance',
'type' => MENU_DEFAULT_LOCAL_TASK,
'file path' => drupal_get_path('module', 'system'),
'weight' => -10,
);
$items[$config_path . '/advagg'] = array(
'title' => 'Advanced CSS/JS Aggregation',
'description' => 'Configuration for Advanced CSS/JS Aggregation.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'advagg_admin_settings_form',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'advagg.admin.inc',
'weight' => 1,
);
$items[$config_path . '/advagg/config'] = array(
'title' => 'Configuration',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[$config_path . '/advagg/info'] = array(
'title' => 'Information',
'description' => 'More detailed information about advagg.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'advagg_admin_info_form',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'advagg.admin.inc',
'weight' => 18,
);
$items[$config_path . '/advagg/operations'] = array(
'title' => 'Operations',
'description' => 'Flush caches, set the bypass cookie, take drastic actions.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'advagg_admin_operations_form',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer site configuration',
),
'file path' => $file_path,
'file' => 'advagg.admin.inc',
'weight' => 20,
);
return $items;
}
function advagg_cron($bypass_time_check = FALSE) {
if (!$bypass_time_check && variable_get('advagg_cron_timestamp', 0) > REQUEST_TIME - variable_get('advagg_cron_frequency', ADVAGG_CRON_FREQUENCY)) {
return array();
}
variable_set('advagg_cron_timestamp', REQUEST_TIME);
cache_clear_all(NULL, 'cache_advagg_info');
$return = array();
module_load_include('inc', 'advagg', 'advagg.cache');
$return[] = advagg_delete_stale_aggregates();
$return[] = advagg_delete_empty_aggregates();
$return[] = advagg_delete_orphaned_aggregates();
$return[] = advagg_remove_missing_files_from_db();
$return[] = advagg_remove_old_unused_aggregates();
$return[] = advagg_cleanup_semaphore_table();
$return[] = advagg_remove_temp_files();
$return[] = advagg_refresh_all_locale_files();
advagg_get_remote_libraries_versions(TRUE);
return $return;
}
function advagg_flush_caches($all_bins = FALSE, $push_new_changes = TRUE) {
if (!db_table_exists('advagg_aggregates_versions')) {
return array();
}
module_load_include('inc', 'advagg', 'advagg.cache');
if ($push_new_changes) {
advagg_push_new_changes();
}
$bins = array(
'cache_advagg_aggregates',
);
if ($all_bins) {
$bins[] = 'cache_advagg_info';
}
return $bins;
}
function advagg_element_info_alter(&$type) {
$type['styles']['#items'] = array();
if (!isset($type['styles']['#pre_render'])) {
$type['styles']['#pre_render'] = array();
}
$key = array_search('drupal_pre_render_styles', $type['styles']['#pre_render']);
if ($key !== FALSE) {
$type['styles']['#pre_render'][$key] = 'advagg_pre_render_styles';
}
else {
$type['styles']['#pre_render'][] = 'advagg_pre_render_styles';
}
$type['styles']['#pre_render'][] = 'advagg_modify_css_pre_render';
$type['styles']['#group_callback'] = 'drupal_group_css';
$type['styles']['#aggregate_callback'] = '_advagg_aggregate_css';
$type['styles']['#type'] = 'styles';
$type['scripts']['#items'] = array();
if (!isset($type['scripts']['#pre_render'])) {
$type['scripts']['#pre_render'] = array();
}
$key_drupal = array_search('drupal_pre_render_scripts', $type['scripts']['#pre_render']);
$key_omega = array_search('omega_pre_render_scripts', $type['scripts']['#pre_render']);
$key_aurora = array_search('aurora_pre_render_scripts', $type['scripts']['#pre_render']);
if ($key_drupal !== FALSE) {
$type['scripts']['#pre_render'][$key_drupal] = 'advagg_pre_render_scripts';
}
elseif ($key_omega !== FALSE) {
$type['scripts']['#pre_render'][$key_omega] = 'advagg_pre_render_scripts';
}
elseif ($key_aurora !== FALSE) {
$type['scripts']['#pre_render'][$key_aurora] = 'advagg_pre_render_scripts';
}
else {
$type['scripts']['#pre_render'][] = 'advagg_pre_render_scripts';
}
$type['scripts']['#pre_render'][] = 'advagg_modify_js_pre_render';
$type['scripts']['#group_callback'] = 'advagg_group_js';
$type['scripts']['#aggregate_callback'] = '_advagg_aggregate_js';
$type['scripts']['#type'] = 'scripts';
$type['html_script_tag'] = $type['html_tag'];
$type['html_script_tag']['#theme'] = 'html_script_tag';
$type['html_script_tag']['#type'] = 'html_script_tag';
}
function advagg_theme_registry_alter(&$theme_registry) {
if (!isset($theme_registry['html'])) {
return;
}
$index = array_search('template_process_html', $theme_registry['html']['process functions']);
if ($index !== FALSE) {
$theme_registry['html']['process functions'][$index] = '_advagg_process_html';
}
else {
$theme_registry['html']['process functions'][] = '_advagg_process_html';
}
$theme_registry['html_script_tag'] = $theme_registry['html_tag'];
$theme_registry['html_script_tag']['function'] = 'theme_html_script_tag';
if (isset($theme_registry['imce_page'])) {
$advagg_path = drupal_get_path('module', 'advagg');
$imce_path = drupal_get_path('module', 'imce');
if (strpos($theme_registry['imce_page']['path'], $imce_path) !== FALSE) {
$theme_registry['imce_page']['path'] = $advagg_path . '/tpl';
}
}
}
function advagg_ajax_render_alter(&$commands) {
if (!advagg_enabled()) {
return;
}
if (!variable_get('advagg_ajax_render_alter', ADVAGG_AJAX_RENDER_ALTER)) {
return;
}
if (function_exists('drupal_add_js_page_defaults')) {
drupal_add_js_page_defaults();
}
list(, $core_scripts_header, $core_scripts_footer, $items, $settings) = advagg_build_ajax_js_css();
$scripts_header = $scripts_footer = '';
if (!empty($items['js'])) {
$scripts_footer_array = advagg_get_js('footer', $items['js'], TRUE);
$scripts_footer = drupal_render($scripts_footer_array);
$scripts_header_array = advagg_get_js('header', $items['js'], TRUE);
$scripts_header = drupal_render($scripts_header_array);
}
foreach ($commands as $key => $values) {
if (!is_array($values) || empty($values['command'])) {
continue;
}
if ($values['command'] === 'settings' && is_array($values['settings']) && !empty($values['merge'])) {
unset($commands[$key]);
continue;
}
if ($values['command'] === 'insert' && is_null($values['settings']) && $values['method'] === 'prepend' && $values['data'] == $core_scripts_header) {
unset($commands[$key]);
continue;
}
if ($values['command'] === 'insert' && is_null($values['settings']) && $values['method'] === 'append' && $values['data'] == $core_scripts_footer) {
unset($commands[$key]);
continue;
}
}
$extra_commands = array();
if (!empty($scripts_header)) {
$extra_commands[] = ajax_command_prepend('head', $scripts_header);
}
if (!empty($scripts_footer)) {
$extra_commands[] = ajax_command_append('body', $scripts_footer);
}
if (!empty($extra_commands)) {
$commands = array_merge($extra_commands, $commands);
}
if (!empty($settings)) {
array_unshift($commands, ajax_command_settings(advagg_cleanup_settings_array(drupal_array_merge_deep_array(array_filter($settings['data'], 'is_array'))), TRUE));
}
}
function advagg_preprocess_page() {
advagg_scan_filesystem_for_changes_live();
}
function advagg_preprocess_html() {
$fix_blocking_css_ie = array(
'#weight' => '-999999',
'#type' => 'markup',
'#markup' => "<!--[if IE]><![endif]-->\n",
);
drupal_add_html_head($fix_blocking_css_ie, 'fix_blocking_css_ie');
if (!variable_get('advagg_combine_css_media', ADVAGG_COMBINE_CSS_MEDIA)) {
return;
}
$x_ua_compatible = 'IE=edge';
if (variable_get('advagg_chrome_header_enabled', ADVAGG_CHROME_HEADER_ENABLED)) {
$x_ua_compatible .= ',chrome=1';
}
drupal_add_http_header('X-UA-Compatible', $x_ua_compatible);
}
function advagg_form_system_performance_settings_alter(&$form, &$form_state) {
module_load_include('admin.inc', 'advagg');
advagg_admin_system_performance_settings_form($form, $form_state);
}
function advagg_js_alter(&$js) {
if (module_exists('admin_menu')) {
$path = drupal_get_path('module', 'admin_menu');
$filename = $path . '/admin_menu.js';
if (isset($js[$filename])) {
$js[$filename]['scope'] = 'footer';
}
}
}
function advagg_cron_alter(&$data) {
if (isset($data['advagg_js_compress_cron'])) {
$data['advagg_js_compress_cron']['rule'] = '*/2 * * * *';
}
if (isset($data['advagg_relocate_cron'])) {
$data['advagg_relocate_cron']['rule'] = '*/5 * * * *';
}
if (isset($data['advagg_cron'])) {
$data['advagg_cron']['rule'] = '0 0 * * *';
}
}
function advagg_password_policy_force_change_allowed_paths_alter(&$allowed_paths) {
$advagg_items = advagg_menu();
foreach ($advagg_items as $path => $attributes) {
if (!empty($attributes['page callback']) && $attributes['page callback'] === 'advagg_missing_aggregate') {
$allowed_paths[] = str_replace('/%', '/*', $path);
}
}
}
function advagg_s3fs_upload_params_alter(&$upload_params) {
list($css_path, $js_path) = advagg_get_root_files_dir();
$scheme_css = file_uri_scheme($css_path[1]);
if ($scheme_css) {
$css_path_dir = str_replace("{$scheme_css}://", '', $css_path[1]);
}
else {
$css_path_dir = ltrim($css_path[1], '/');
}
$scheme_js = file_uri_scheme($js_path[1]);
if ($scheme_js) {
$js_path_dir = str_replace("{$scheme_js}://", '', $js_path[1]);
}
else {
$js_path_dir = ltrim($js_path[1], '/');
}
$type = '';
if (strpos($upload_params['Bucket'] . '/' . $upload_params['Key'], $css_path_dir) !== FALSE) {
$type = 'css';
}
if (strpos($upload_params['Bucket'] . '/' . $upload_params['Key'], $js_path_dir) !== FALSE) {
$type = 'js';
}
if ($js_path_dir === $css_path_dir && !empty($type)) {
$pathinfo = pathinfo($upload_params['Key']);
if ($pathinfo['extension'] === 'gz') {
$pathinfo = pathinfo($pathinfo['filename']);
}
$type = $pathinfo['extension'];
}
if (empty($type)) {
return;
}
if (variable_get('advagg_resource_hints_use_immutable', ADVAGG_RESOURCE_HINTS_USE_IMMUTABLE)) {
$upload_params['CacheControl'] = 'max-age=31449600, public, immutable';
}
else {
$upload_params['CacheControl'] = 'max-age=31449600, public';
}
$upload_params['Expires'] = gmdate('D, d M Y H:i:s \\G\\M\\T', REQUEST_TIME + 365 * 24 * 60 * 60);
$pathinfo = pathinfo($upload_params['Key']);
if ($pathinfo['extension'] === $type) {
if (variable_get('advagg_gzip', ADVAGG_GZIP)) {
$upload_params['ContentEncoding'] = 'gzip';
}
elseif (variable_get('advagg_brotli', ADVAGG_BROTLI)) {
$upload_params['ContentEncoding'] = 'br';
}
}
}
function advagg_get_s3fs_config($key = NULL) {
if (module_exists('s3fs') && is_callable('_s3fs_get_config')) {
$s3fs_config = _s3fs_get_config();
if (empty($key)) {
return $s3fs_config;
}
elseif (isset($s3fs_config[$key])) {
return $s3fs_config[$key];
}
}
return NULL;
}
function advagg_s3fs_evaluate_no_rewrite_cssjs($is_set = TRUE) {
$s3fs_no_rewrite_cssjs = advagg_get_s3fs_config('no_rewrite_cssjs');
if (!is_null($s3fs_no_rewrite_cssjs)) {
return $is_set ? !empty($s3fs_no_rewrite_cssjs) : empty($s3fs_no_rewrite_cssjs);
}
else {
return FALSE;
}
}
function advagg_admin_menu_cache_info() {
if (variable_get('advagg_enabled', ADVAGG_ENABLED)) {
$caches['advagg'] = array(
'title' => t('Adv CSS/JS Agg'),
'callback' => 'advagg_admin_flush_cache',
);
return $caches;
}
}
function advagg_admin_menu_output_alter(array &$content) {
if (variable_get('advagg_enabled', ADVAGG_ENABLED)) {
unset($content['icon']['icon']['flush-cache']['assets']);
}
}
function advagg_anonymous_login_paths_alter(&$paths) {
list($css_path, $js_path) = advagg_get_root_files_dir();
$paths['exclude'][] = $css_path[1] . '/*';
$paths['exclude'][] = $js_path[1] . '/*';
}
function advagg_pre_flush_all_caches() {
static $run_once;
if (!isset($run_once)) {
$run_once = TRUE;
module_load_include('admin.inc', 'advagg');
advagg_admin_truncate_advagg_files();
}
}
function advagg_locale_js_add_translations(array &$javascript, $dir) {
if (!empty($GLOBALS['language']->javascript)) {
$file = $dir . '/' . $GLOBALS['language']->language . '_' . $GLOBALS['language']->javascript . '.js';
$javascript[$file] = drupal_js_defaults($file);
}
}
function advagg_modify_js_pre_render(array $elements) {
$children = array_intersect_key($elements, array_flip(element_children($elements)));
drupal_alter('advagg_modify_js_pre_render', $children, $elements);
foreach ($children as $key => $value) {
if (isset($elements[$key])) {
unset($elements[$key]);
}
}
$elements += $children;
return $elements;
}
function advagg_modify_css_pre_render(array $elements) {
if (!advagg_enabled()) {
return $elements;
}
$children = array();
foreach ($elements as $key => &$value) {
if ($key !== '' && is_string($key) && 0 === strpos($key, '#')) {
continue;
}
$children[$key] =& $value;
}
unset($value);
drupal_alter('advagg_modify_css_pre_render', $children, $elements);
return $elements;
}
function _advagg_aggregate_css(array &$css_groups) {
if (!advagg_enabled()) {
return drupal_aggregate_css($css_groups);
}
if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
$GLOBALS['_advagg']['debug']['css_groups_before'][] = $css_groups;
}
$preprocess_css = advagg_file_aggregation_enabled('css');
drupal_alter('advagg_css_groups', $css_groups, $preprocess_css);
$files_to_aggregate = array();
$gap_counter = 0;
foreach ($css_groups as $key => $group) {
switch ($group['type']) {
case 'file':
if ($group['preprocess'] && $preprocess_css) {
$files_to_aggregate[$gap_counter][$key] = $group;
}
else {
++$gap_counter;
}
break;
case 'inline':
++$gap_counter;
$css_groups[$key]['data'] = '';
foreach ($group['items'] as $item) {
$css_groups[$key]['data'] .= advagg_load_stylesheet_content($item['data'], $item['preprocess']);
}
break;
case 'external':
++$gap_counter;
break;
}
}
if (!empty($files_to_aggregate)) {
$hooks_hash = advagg_get_current_hooks_hash();
$serialize_function = variable_get('advagg_serialize', ADVAGG_SERIALIZE);
$css_hash = drupal_hash_base64($serialize_function($files_to_aggregate));
$cache_id = 'advagg:css:' . $hooks_hash . ':' . $css_hash;
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 1 && ($cache = cache_get($cache_id, 'cache_advagg_aggregates'))) {
$plans = $cache->data;
}
else {
module_load_include('inc', 'advagg', 'advagg');
$plans = advagg_build_aggregate_plans($files_to_aggregate, 'css');
if (!empty($plans) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 1) {
cache_set($cache_id, $plans, 'cache_advagg_aggregates', CACHE_TEMPORARY);
}
}
$css_groups = advagg_merge_plans($css_groups, $plans);
}
if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
$GLOBALS['_advagg']['debug']['css_groups_after'][] = $css_groups;
}
}
function _advagg_aggregate_js(array &$js_groups) {
if (!advagg_enabled()) {
if (function_exists('drupal_aggregate_js')) {
return drupal_aggregate_js($js_groups);
}
else {
return;
}
}
if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
$GLOBALS['_advagg']['debug']['js_groups_before'][] = $js_groups;
}
$preprocess_js = advagg_file_aggregation_enabled('js');
drupal_alter('advagg_js_groups', $js_groups, $preprocess_js);
$files_to_aggregate = array();
$gap_counter = 0;
if ($preprocess_js) {
$all_in_footer = FALSE;
if (module_exists('advagg_mod') && variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER) >= 2) {
$all_in_footer = TRUE;
}
foreach ($js_groups as $key => &$group) {
switch ($group['type']) {
case 'file':
if (!empty($group['preprocess'])) {
if ($all_in_footer && $group['scope'] === 'footer' && $group['group'] > 9000) {
++$gap_counter;
$all_in_footer = FALSE;
}
$files_to_aggregate[$gap_counter][$key] = $group;
}
else {
++$gap_counter;
}
break;
case 'inline':
++$gap_counter;
break;
case 'external':
++$gap_counter;
break;
}
}
unset($group);
}
if (!empty($files_to_aggregate)) {
$hooks_hash = advagg_get_current_hooks_hash();
$serialize_function = variable_get('advagg_serialize', ADVAGG_SERIALIZE);
$js_hash = drupal_hash_base64($serialize_function($files_to_aggregate));
$cache_id = 'advagg:js:' . $hooks_hash . ':' . $js_hash;
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 1 && ($cache = cache_get($cache_id, 'cache_advagg_aggregates'))) {
$plans = $cache->data;
}
else {
module_load_include('inc', 'advagg', 'advagg');
$plans = advagg_build_aggregate_plans($files_to_aggregate, 'js');
if (!empty($plans) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 1) {
cache_set($cache_id, $plans, 'cache_advagg_aggregates', CACHE_TEMPORARY);
}
}
$js_groups = advagg_merge_plans($js_groups, $plans);
}
if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
$GLOBALS['_advagg']['debug']['js_groups_after'][] = $js_groups;
}
}
function _advagg_build_css_arrays_for_rendering($skip_alter = FALSE) {
$raw_css = drupal_add_css();
$full_css = advagg_get_css($raw_css, $skip_alter);
if (!empty($full_css['#attached'])) {
drupal_process_attached($full_css);
unset($full_css['#attached']);
}
return array(
$raw_css,
$full_css,
);
}
function _advagg_build_js_arrays_for_rendering($skip_alter = FALSE) {
$javascript = drupal_add_js();
$full_javascript = advagg_get_full_js($javascript, $skip_alter);
$scopes = advagg_get_js_scopes($full_javascript);
$js_scope_array = array();
$js_scope_settings_array = array();
foreach ($scopes as $scope => $use) {
if (!$use) {
continue;
}
$scripts = advagg_get_js($scope, $full_javascript);
if (isset($scripts['#items']['settings'])) {
$js_scope_settings_array[$scope]['settings'] = $scripts['#items']['settings'];
$scripts['#items']['settings'] = array();
}
$js_scope_array[$scope] = $scripts;
}
if (count($js_scope_settings_array) > 1) {
$max = -1;
$max_scope = '';
foreach ($js_scope_settings_array as $scope => $settings) {
$count = count($settings);
$max = max($max, $count);
if ($max == $count) {
$max_scope = $scope;
}
}
foreach ($js_scope_settings_array as $scope => $settings) {
if ($scope !== $max_scope) {
unset($js_scope_settings_array[$scope]);
}
}
}
return array(
$javascript,
$js_scope_settings_array,
$js_scope_array,
);
}
function advagg_css_in_js($css_cache = NULL) {
if (module_exists('advagg_mod') && variable_get('advagg_mod_css_defer', ADVAGG_MOD_CSS_DEFER)) {
return TRUE;
}
if (module_exists('css_delivery') && css_delivery_enabled()) {
return TRUE;
}
if (!empty($css_cache->data[1]['#items'])) {
foreach ($css_cache->data[1]['#items'] as $values) {
if (!empty($values['critical-css'])) {
return TRUE;
}
}
}
return variable_get('advagg_css_in_js', ADVAGG_CSS_IN_JS);
}
function advagg_get_render_cache(array $full_css, array $js_scope_array) {
$cids = array();
$css_cache_id = '';
$js_cache_id = '';
$hooks_hash = advagg_get_current_hooks_hash();
$serialize_function = variable_get('advagg_serialize', ADVAGG_SERIALIZE);
if (advagg_file_aggregation_enabled('css')) {
$cids[] = $css_cache_id = 'advagg:css:full:1.1:' . $hooks_hash . ':' . drupal_hash_base64($serialize_function($full_css));
}
if (advagg_file_aggregation_enabled('js')) {
$cids[] = $js_cache_id = 'advagg:js:full:1.1:' . $hooks_hash . ':' . drupal_hash_base64($serialize_function($js_scope_array));
}
if (!empty($cids)) {
$cached_data = cache_get_multiple($cids, 'cache_advagg_aggregates');
if (isset($cached_data[$css_cache_id])) {
$css_cache = $cached_data[$css_cache_id];
}
if (isset($cached_data[$js_cache_id])) {
$js_cache = $cached_data[$js_cache_id];
}
}
if (!empty($css_cache) && empty($js_cache) && advagg_css_in_js($css_cache)) {
unset($css_cache);
}
if (!isset($css_cache)) {
$css_cache = new stdClass();
}
if (!isset($js_cache)) {
$js_cache = new stdClass();
}
return array(
$css_cache,
$js_cache,
$css_cache_id,
$js_cache_id,
);
}
function _advagg_process_html(&$variables) {
if (drupal_get_http_header('status') === '404 Not Found') {
$uri = request_uri();
if (stripos($uri, '/advagg_') !== FALSE) {
$advagg_items = advagg_menu();
$css = reset($advagg_items);
$css_path = key($advagg_items);
$css_path = substr($css_path, 0, strlen($css_path) - 1);
$css_start = strpos($uri, $css_path);
if ($css_start !== FALSE) {
$filename = substr($uri, $css_start + strlen($css_path));
}
else {
if (variable_get('advagg_weak_file_verification', ADVAGG_WEAK_FILE_VERIFICATION)) {
$css_start = strpos($uri, '/' . basename($css_path) . '/');
if ($css_start !== FALSE) {
$filename = substr($uri, $css_start + strlen('/' . basename($css_path) . '/'));
}
}
}
if (empty($filename)) {
$js = next($advagg_items);
$js_path = key($advagg_items);
$js_path = substr($js_path, 0, strlen($js_path) - 1);
$js_start = strpos($uri, $js_path);
if ($js_start !== FALSE) {
$filename = substr($uri, $js_start + strlen($js_path));
}
else {
if (variable_get('advagg_weak_file_verification', ADVAGG_WEAK_FILE_VERIFICATION)) {
$js_start = strpos($uri, '/' . basename($js_path) . '/');
if ($js_start !== FALSE) {
$filename = substr($uri, $js_start + strlen('/' . basename($js_path) . '/'));
}
}
}
}
if (!empty($filename)) {
$router_item = $css;
if (isset($js)) {
$router_item = $js;
}
if ($router_item['file']) {
$included = module_load_include($router_item['file'], 'advagg');
if (!$included && !function_exists($router_item['page callback'])) {
$file = DRUPAL_ROOT . '/' . drupal_get_path('module', 'advagg') . '/' . $router_item['file'];
if (is_file($file)) {
require_once $file;
}
}
}
if (function_exists($router_item['page callback'])) {
if ($pos = strpos($filename, '?')) {
$filename = substr($filename, 0, $pos);
}
if ($pos = strpos($filename, '#')) {
$filename = substr($filename, 0, $pos);
}
call_user_func_array($router_item['page callback'], array(
$filename,
));
}
else {
watchdog('advagg', 'You need to flush your menu cache. This can be done at the top of the <a href="@performance">performance page</a>. The advagg callback failed while trying to generate this file: @uri', array(
'@performance' => url('admin/config/development/performance'),
'@uri' => $uri,
), WATCHDOG_CRITICAL);
}
}
}
}
if (!advagg_enabled()) {
template_process_html($variables);
return;
}
if (isset($variables['page']) && is_array($variables['page']) && isset($variables['page']['page_top'])) {
$variables['page_top'] = drupal_render($variables['page']['page_top']);
}
elseif (!isset($variables['page_top'])) {
$variables['page_top'] = '';
}
if (isset($variables['page']) && is_array($variables['page']) && isset($variables['page']['page_bottom'])) {
$variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
}
elseif (!isset($variables['page_bottom'])) {
$variables['page_bottom'] = '';
}
if (isset($variables['page']) && is_array($variables['page']) && isset($variables['page']['#children'])) {
$variables['page'] = $variables['page']['#children'];
}
$advagg_script_alt_scope_scripts = array();
if (variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
$prefix = "<!-- AdvAgg page:prefix tag -->";
$suffix = "<!-- AdvAgg page:suffix tag -->";
$variables['page'] = $prefix . $variables['page'] . $suffix;
$prefix = "<!-- AdvAgg page_top:prefix tag -->";
$suffix = "<!-- AdvAgg page_top:suffix tag -->";
$variables['page_top'] = $prefix . $variables['page_top'] . $suffix;
$prefix = "<!-- AdvAgg page_bottom:prefix tag -->";
$suffix = "<!-- AdvAgg page_bottom:suffix tag -->";
$variables['page_bottom'] = $prefix . $variables['page_bottom'] . $suffix;
$matches = array();
preg_match_all('/<!-- AdvAgg (.*?) tag -->/', $variables['page_top'], $matches);
$advagg_script_alt_scope_scripts = array_merge($matches[1], $advagg_script_alt_scope_scripts);
preg_match_all('/<!-- AdvAgg (.*?) tag -->/', $variables['page'], $matches);
$advagg_script_alt_scope_scripts = array_merge($matches[1], $advagg_script_alt_scope_scripts);
preg_match_all('/<!-- AdvAgg (.*?) tag -->/', $variables['page_bottom'], $matches);
$advagg_script_alt_scope_scripts = array_merge($matches[1], $advagg_script_alt_scope_scripts);
}
$elements = drupal_add_html_head();
if (is_callable('advagg_mod_html_head_post_alter')) {
advagg_mod_html_head_post_alter($elements);
}
if (function_exists('drupal_add_js_page_defaults')) {
drupal_add_js_page_defaults();
}
$javascript = array();
if (!variable_get('advagg_debug', ADVAGG_DEBUG)) {
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 5 && !module_exists('advagg_relocate')) {
list($variables['css'], $full_css) = _advagg_build_css_arrays_for_rendering(TRUE);
list($javascript, $js_scope_settings_array, $js_scope_array) = _advagg_build_js_arrays_for_rendering(TRUE);
list($css_cache, $js_cache, $css_cache_id_no_alter, $js_cache_id_no_alter) = advagg_get_render_cache($full_css, $js_scope_array);
}
if ((empty($css_cache->data) || empty($js_cache->data)) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 3) {
list($variables['css'], $full_css) = _advagg_build_css_arrays_for_rendering();
list($javascript, $js_scope_settings_array, $js_scope_array) = _advagg_build_js_arrays_for_rendering();
list($css_cache, $js_cache, $css_cache_id, $js_cache_id) = advagg_get_render_cache($full_css, $js_scope_array);
}
}
if (!empty($css_cache->data)) {
list($variables['styles'], $full_css) = $css_cache->data;
}
else {
if (empty($full_css)) {
list($variables['css'], $full_css) = _advagg_build_css_arrays_for_rendering();
}
$variables['styles'] = drupal_render($full_css);
if (!empty($css_cache_id) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 3) {
cache_set($css_cache_id, array(
$variables['styles'],
$full_css,
), 'cache_advagg_aggregates', CACHE_TEMPORARY);
}
if (!empty($css_cache_id_no_alter) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 5) {
cache_set($css_cache_id_no_alter, array(
$variables['styles'],
$full_css,
), 'cache_advagg_aggregates', CACHE_TEMPORARY);
}
}
if (module_exists('advagg_font') && variable_get('advagg_font_fontfaceobserver', ADVAGG_FONT_FONTFACEOBSERVER)) {
$fonts = array();
foreach ($full_css['#groups'] as $groups) {
if (isset($groups['items']['files'])) {
foreach ($groups['items']['files'] as $file) {
if (isset($file['advagg_font'])) {
foreach ($file['advagg_font'] as $class => $name) {
$fonts[$class] = $name;
}
}
}
}
}
if (!empty($fonts)) {
if (isset($js_scope_settings_array)) {
$key = key($js_scope_settings_array);
$js_scope_settings_array[$key]['settings']['data'][] = array(
'advagg_font' => $fonts,
);
}
drupal_add_js(array(
'advagg_font' => $fonts,
), array(
'type' => 'setting',
));
}
}
if (variable_get('advagg_resource_hints_preload', ADVAGG_RESOURCE_HINTS_PRELOAD)) {
foreach ($full_css['#groups'] as $groups) {
if (empty($groups['data']) || $groups['type'] === 'inline') {
continue;
}
advagg_add_preload_header(advagg_convert_abs_to_rel(file_create_url($groups['data'])), 'style');
}
}
if (variable_get('advagg_clear_scripts', ADVAGG_CLEAR_SCRIPTS)) {
$variables['scripts'] = '';
}
if (!isset($variables['scripts'])) {
$variables['scripts'] = '';
}
if (!isset($variables['page_bottom']) || !is_string($variables['page_bottom'])) {
$variables['page_bottom'] = '';
}
$use_cache = FALSE;
if (!empty($js_cache->data) && !variable_get('advagg_debug', ADVAGG_DEBUG)) {
$use_cache = TRUE;
$add_to_variables = array();
$js_settings_used = array();
$js_scope_settings_array_copy = $js_scope_settings_array;
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 5) {
if (!empty($js_scope_settings_array_copy['header']) && empty($js_scope_settings_array_copy['footer'])) {
$js_scope_settings_array_copy['footer'] = $js_scope_settings_array_copy['header'];
}
}
list($js_cache_data, $js_scope_array) = $js_cache->data;
foreach ($js_cache_data as $scope => $value) {
$scope_settings = $scope;
if ($scope_settings === 'scripts') {
$scope_settings = 'header';
}
if ($scope === 'page_bottom') {
$scope_settings = 'footer';
}
$start = strpos($value, 'jQuery.extend(Drupal.settings,');
if ($start !== FALSE) {
if (!isset($js_scope_settings_array_copy[$scope_settings]['settings'])) {
$use_cache = FALSE;
break;
}
$merged = advagg_cleanup_settings_array(drupal_array_merge_deep_array(array_filter($js_scope_settings_array_copy[$scope_settings]['settings']['data'], 'is_array')));
$json_data = advagg_json_encode($merged);
if (!empty($json_data)) {
$js_settings_used[$scope_settings] = TRUE;
$value = advagg_replace_drupal_settings_string($value, $json_data);
}
}
$add_to_variables[$scope] = $value;
}
if ($use_cache) {
$all_used = array_diff(array_keys($js_scope_settings_array_copy), array_keys($js_settings_used));
if (!empty($all_used) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 5 && !empty($js_settings_used)) {
$use_cache = FALSE;
}
}
if ($use_cache) {
foreach ($add_to_variables as $scope => $value) {
if (!isset($variables[$scope]) || !is_string($variables[$scope])) {
$variables[$scope] = '';
}
$variables[$scope] .= $value;
}
}
}
if (!$use_cache) {
if (!empty($js_cache->data) && !empty($css_cache->data) && advagg_css_in_js($css_cache)) {
$variables['styles'] = drupal_render($full_css);
}
$new_js = drupal_add_js();
$diff = array_diff(array_keys($new_js), array_keys($javascript));
if (!empty($diff) || empty($javascript)) {
list($javascript, $js_scope_settings_array, $js_scope_array) = _advagg_build_js_arrays_for_rendering();
}
$js_cache = array();
$js_cache['scripts'] = '';
if (!empty($js_scope_array)) {
foreach ($js_scope_array as $scope => &$scripts_array) {
if (!empty($js_scope_settings_array[$scope]['settings'])) {
$scripts_array['#items']['settings'] = $js_scope_settings_array[$scope]['settings'];
}
$scripts = drupal_render($scripts_array);
if ($scope === 'header') {
$variables['scripts'] = $scripts . $variables['scripts'];
$js_cache['scripts'] = $scripts . $js_cache['scripts'];
}
elseif ($scope === 'footer') {
$variables['page_bottom'] .= $scripts;
$js_cache['page_bottom'] = $scripts;
}
elseif ($scope === 'above_css') {
$variables['above_css'] = $scripts;
$js_cache['above_css'] = $scripts;
}
elseif (variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
if (isset($variables[$scope]) && is_string($variables[$scope]) && array_key_exists($scope, $GLOBALS['theme_info']->info['regions'])) {
$variables[$scope] .= $scripts;
$js_cache[$scope] = $scripts;
}
elseif (array_search($scope, $advagg_script_alt_scope_scripts, TRUE) !== FALSE) {
$pos_page_top = strpos($variables['page_top'], "<!-- AdvAgg {$scope} tag -->");
$pos_page = strpos($variables['page'], "<!-- AdvAgg {$scope} tag -->");
$pos_page_bottom = strpos($variables['page_bottom'], "<!-- AdvAgg {$scope} tag -->");
if ($pos_page_top !== FALSE) {
$pos_page_top += strlen("<!-- AdvAgg {$scope} tag -->");
$variables['page_top'] = substr_replace($variables['page_top'], "\n{$scripts}", $pos_page_top, 0);
$js_cache[$scope] = $scripts;
}
elseif ($pos_page !== FALSE) {
$pos_page += strlen("<!-- AdvAgg {$scope} tag -->");
$variables['page'] = substr_replace($variables['page'], "\n{$scripts}", $pos_page, 0);
$js_cache[$scope] = $scripts;
}
elseif ($pos_page_bottom !== FALSE) {
$pos_page_bottom += strlen("<!-- AdvAgg {$scope} tag -->");
$variables['page_bottom'] = substr_replace($variables['page_bottom'], "\n{$scripts}", $pos_page_bottom, 0);
$js_cache[$scope] = $scripts;
}
}
elseif (strpos($scope, ':') === FALSE) {
$variables['scripts'] .= $scripts;
$js_cache['scripts'] .= $scripts;
}
}
}
unset($scripts_array);
foreach ($js_cache as &$string) {
$string = advagg_replace_drupal_settings_string($string, '{}');
}
unset($string);
$js_scope_array = array_intersect_key($js_scope_array, array_flip(element_children($js_scope_array)));
foreach ($js_scope_array as $scope => &$scripts_array) {
$scripts_array = array_diff_key($scripts_array, array_flip(element_children($scripts_array)));
if (isset($scripts_array['#children'])) {
unset($scripts_array['#children']);
}
if (isset($scripts_array['#items']['settings']['data']) && is_array($scripts_array['#items']['settings']['data'])) {
$scripts_array['#items']['settings']['data'] = array();
}
if (isset($scripts_array['#printed'])) {
unset($scripts_array['#printed']);
}
foreach ($scripts_array['#groups'] as $key => $groups) {
if (!isset($groups['items']['files'])) {
unset($scripts_array['#groups'][$key]);
}
}
}
unset($scripts_array);
if (!empty($js_cache_id) && !empty($js_cache) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 3) {
cache_set($js_cache_id, array(
$js_cache,
$js_scope_array,
), 'cache_advagg_aggregates', CACHE_TEMPORARY);
}
if (!empty($js_cache_id_no_alter) && !empty($js_cache) && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 5) {
cache_set($js_cache_id_no_alter, array(
$js_cache,
$js_scope_array,
), 'cache_advagg_aggregates', CACHE_TEMPORARY);
}
}
}
if (!empty($variables['above_css'])) {
$variables['styles'] = $variables['above_css'] . $variables['styles'];
}
if (variable_get('advagg_resource_hints_preload', ADVAGG_RESOURCE_HINTS_PRELOAD)) {
foreach ($js_scope_array as $scope => &$scripts_array) {
if ($scope !== 'header' && $scope !== 'footer' && $scope !== 'above_css' && !variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE)) {
continue;
}
foreach ($scripts_array['#groups'] as $groups) {
if (empty($groups['data']) || $groups['type'] === 'inline') {
continue;
}
advagg_add_preload_header(advagg_convert_abs_to_rel(file_create_url($groups['data'])), 'script');
}
}
}
$head_elements_before = drupal_add_html_head();
if (variable_get('advagg_resource_hints_dns_prefetch', ADVAGG_RESOURCE_HINTS_DNS_PREFETCH) || variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT) || variable_get('advagg_resource_hints_preload', ADVAGG_RESOURCE_HINTS_PRELOAD)) {
foreach ($full_css['#items'] as $file) {
advagg_add_resource_hints_array($file);
}
foreach ($full_css['#groups'] as $groups) {
if (isset($groups['items']['files'])) {
foreach ($groups['items']['files'] as $file) {
advagg_add_resource_hints_array($file);
}
}
}
foreach ($js_scope_array as $scope_js) {
foreach ($scope_js['#items'] as $file) {
advagg_add_resource_hints_array($file);
}
if (isset($scope_js['#groups'])) {
foreach ($scope_js['#groups'] as $groups) {
if (isset($groups['items']['files'])) {
foreach ($groups['items']['files'] as $file) {
advagg_add_resource_hints_array($file);
}
}
}
}
}
}
advagg_add_preload_header();
$head_elements_after = drupal_add_html_head();
$elements += array_diff_key($head_elements_after, $head_elements_before);
drupal_alter('html_head', $elements);
$head = drupal_render($elements);
if (variable_get('advagg_html_head_in_css_location', ADVAGG_HTML_HEAD_IN_CSS_LOCATION)) {
$variables['styles'] = $head . $variables['styles'];
$variables['head'] = '';
}
else {
$variables['head'] = $head;
}
if (variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE) && !empty($advagg_script_alt_scope_scripts) && !variable_get('theme_debug', FALSE)) {
$variables['page_top'] = preg_replace('/<!-- AdvAgg (.*?) tag -->/', '', $variables['page_top']);
$variables['page'] = preg_replace('/<!-- AdvAgg (.*?) tag -->/', '', $variables['page']);
$variables['page_bottom'] = preg_replace('/<!-- AdvAgg (.*?) tag -->/', '', $variables['page_bottom']);
}
if (variable_get('advagg_debug', ADVAGG_DEBUG)) {
$debug = $GLOBALS['_advagg']['debug'];
if (is_callable('httprl_pr')) {
$output = ' ' . httprl_pr($debug);
}
else {
$output = '<pre>' . str_replace(array(
'<',
'>',
), array(
'<',
'>',
), print_r($debug, TRUE)) . '</pre>';
}
watchdog('advagg_debug', $output, array(), WATCHDOG_DEBUG);
}
}
function advagg_replace_drupal_settings_string($subject, $replace) {
$start = strpos($subject, 'jQuery.extend(Drupal.settings,');
if ($start === FALSE) {
return $subject;
}
$script_end = stripos($subject, '</script>', $start);
$settings_substring = substr($subject, $start, $script_end - $start);
$json_end = strripos($settings_substring, '});');
$script_tag_start = strripos(substr($subject, 0, $start), '<script');
if (strpos(substr($subject, $script_tag_start, $start), '$L.wait(') !== FALSE) {
$_json_end = strripos(substr($settings_substring, 0, $json_end), '});');
if ($_json_end !== FALSE) {
$json_end = $_json_end;
}
}
$subject = substr($subject, 0, $start + 30) . $replace . substr($subject, $json_end + $start + 1);
return $subject;
}
function advagg_cleanup_settings_array(array $data) {
if (isset($data['ajaxPageState']['js'])) {
foreach ((array) $data['ajaxPageState']['js'] as $key => $value) {
if (advagg_remove_short_keys($key)) {
if (is_array($data['ajaxPageState']['js']) && isset($data['ajaxPageState']['js'][$key])) {
unset($data['ajaxPageState']['js'][$key]);
}
elseif (is_object($data['ajaxPageState']['js']) && isset($data['ajaxPageState']['js']->{$key})) {
unset($data['ajaxPageState']['js']->{$key});
}
}
}
}
if (isset($data['ajaxPageState']['css'])) {
foreach ((array) $data['ajaxPageState']['css'] as $key => $value) {
if (advagg_remove_short_keys($key, 6)) {
if (is_object($data['ajaxPageState']['css']) && isset($data['ajaxPageState']['css']->{$key})) {
unset($data['ajaxPageState']['css']->{$key});
}
elseif (is_array($data['ajaxPageState']['css']) && isset($data['ajaxPageState']['css'][$key])) {
unset($data['ajaxPageState']['css'][$key]);
}
}
}
}
if (isset($data['ajaxPageState']['js']['settings'])) {
unset($data['ajaxPageState']['js']['settings']);
}
if (isset($data['ajaxPageState']['js']->settings)) {
unset($data['ajaxPageState']['js']->settings);
}
return $data;
}
function advagg_add_resource_hints_array(array $values) {
if (variable_get('advagg_resource_hints_dns_prefetch', ADVAGG_RESOURCE_HINTS_DNS_PREFETCH) || variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT)) {
if (!empty($values['type']) && ($values['type'] === 'external' || $values['type'] === 'file')) {
advagg_add_dns_prefetch($values['data']);
}
if (!empty($values['dns_prefetch'])) {
if (is_array($values['dns_prefetch'])) {
foreach ($values['dns_prefetch'] as $url) {
advagg_add_dns_prefetch($url);
}
}
else {
advagg_add_dns_prefetch($values['dns_prefetch']);
}
}
}
if (!empty($values['preload']) && variable_get('advagg_resource_hints_preload', ADVAGG_RESOURCE_HINTS_PRELOAD)) {
if (is_array($values['preload'])) {
foreach ($values['preload'] as $url) {
advagg_add_preload_header($url);
}
}
else {
advagg_add_preload_header($values['preload']);
}
}
}
function advagg_add_dns_prefetch($url) {
$advagg_resource_hints_location = variable_get('advagg_resource_hints_location', ADVAGG_RESOURCE_HINTS_LOCATION);
static $weight = -1001;
if ($advagg_resource_hints_location == 3) {
$weight = -999.9;
}
$weight += 0.0001;
$parse = @parse_url($url);
if (empty($parse['host'])) {
if (strpos($url, '.') && strpos($url, '/') === FALSE) {
$parse['scheme'] = '//';
$parse['host'] = $url;
$pos = strpos($url, '#');
if ($pos !== FALSE) {
$parse['fragment'] = substr($url, $pos + 1);
$parse['host'] = substr($url, 0, $pos);
}
$url = advagg_glue_url($parse);
$parse = @parse_url($url);
}
if (empty($parse['host'])) {
return FALSE;
}
}
if (!empty($parse['scheme']) && $parse['scheme'] !== 'http' && $parse['scheme'] !== 'https') {
return FALSE;
}
$host = @parse_url($GLOBALS['base_root'], PHP_URL_HOST);
if ($parse['host'] === $host) {
return FALSE;
}
if (strpos($parse['host'], 'fonts.googleapis.com') !== FALSE) {
advagg_add_dns_prefetch('https://fonts.gstatic.com/#crossorigin');
}
if (variable_get('advagg_resource_hints_dns_prefetch', ADVAGG_RESOURCE_HINTS_DNS_PREFETCH)) {
$element = array(
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => array(
'rel' => 'dns-prefetch',
'href' => '//' . $parse['host'],
),
'#weight' => $weight,
);
drupal_add_html_head($element, 'advagg_resource_hints_dns_prefetch:' . $parse['host']);
}
if (variable_get('advagg_resource_hints_preconnect', ADVAGG_RESOURCE_HINTS_PRECONNECT)) {
$href = '//' . $parse['host'];
if (!$GLOBALS['is_https'] && isset($parse['scheme'])) {
$href = "{$parse['scheme']}://{$parse['host']}";
}
$element = array(
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => array(
'rel' => 'preconnect',
'href' => $href,
),
'#weight' => $weight,
);
if (!empty($parse['fragment']) && $parse['fragment'] === 'crossorigin') {
$element['#attributes']['crossorigin'] = '';
}
drupal_add_html_head($element, 'advagg_resource_hints_preconnect:' . $parse['host']);
}
if (!empty($parse['fragment']) && $parse['fragment'] === 'prefetch') {
$element = array(
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => array(
'rel' => 'prefetch',
'href' => '//' . $parse['host'] . '/robots.txt',
),
'#weight' => $weight,
);
drupal_add_html_head($element, 'advagg_prefetch:' . $parse['host']);
}
return TRUE;
}
function advagg_get_css(array $css = array(), $skip_alter = FALSE) {
if (empty($css)) {
$css = drupal_add_css();
}
if (!$skip_alter) {
advagg_add_default_dns_lookups($css, 'css');
drupal_alter('css', $css);
drupal_alter('css_post', $css);
advagg_fix_type($css, 'css');
}
advagg_drupal_sort_css_js_stable($css);
if (!empty($css)) {
$setting['ajaxPageState']['css'] = (object) array_fill_keys(array_keys($css), 1);
}
$previous_item = array();
foreach ($css as $key => $item) {
if ($item['type'] == 'file') {
$basename = isset($item['basename']) ? $item['basename'] : drupal_basename($item['data']);
if (isset($previous_item[$basename])) {
unset($css[$previous_item[$basename]]);
}
$previous_item[$basename] = $key;
}
}
advagg_remove_empty_files($css);
$styles = array(
'#type' => 'styles',
'#items' => $css,
);
if (!empty($setting)) {
$styles['#attached']['js'][] = array(
'type' => 'setting',
'data' => $setting,
);
}
return $styles;
}
function advagg_get_full_js(array $javascript = array(), $skip_alter = FALSE) {
if (empty($javascript)) {
$javascript = drupal_add_js();
}
if (empty($javascript) || isset($javascript['settings']) && count($javascript) == 1) {
return array();
}
if (!$skip_alter) {
advagg_add_default_dns_lookups($javascript, 'js');
if (is_callable('advagg_mod_js_pre_alter')) {
advagg_mod_js_pre_alter($javascript);
}
drupal_alter('js', $javascript);
drupal_alter('js_post', $javascript);
advagg_fix_type($javascript, 'js');
}
elseif (is_callable('advagg_mod_js_move_to_footer')) {
if (variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER) == 3) {
advagg_mod_js_move_to_footer($javascript);
}
}
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
$have_css = FALSE;
foreach ($javascript['settings']['data'] as $setting) {
if (!empty($setting['ajaxPageState']['css'])) {
$have_css = TRUE;
break;
}
}
if (!$have_css) {
$css = drupal_add_css();
if (!empty($css)) {
$javascript['settings']['data'][]['ajaxPageState']['css'] = (object) array_fill_keys(array_keys($css), 1);
}
}
}
advagg_remove_empty_files($javascript);
return $javascript;
}
function advagg_get_js($scope = 'header', array $javascript = array(), $ajax = FALSE) {
$page_state =& drupal_static(__FUNCTION__, array());
if (empty($javascript) && !$ajax) {
$javascript = advagg_get_full_js();
}
if (empty($javascript)) {
return array();
}
$items = array();
foreach ($javascript as $key => $item) {
if (!empty($item['scope']) && $item['scope'] === $scope) {
$items[$key] = $item;
}
}
advagg_drupal_sort_css_js_stable($items);
if ($scope === 'footer' && !empty($items['settings'])) {
$settings_js['settings'] = $items['settings'];
unset($items['settings']);
$counter = 0;
foreach ($items as $key => $item) {
if ($item['group'] > 9000) {
advagg_array_splice_assoc($items, $counter, 0, $settings_js);
unset($settings_js);
break;
}
++$counter;
}
if (isset($settings_js)) {
$items = array_merge($items, $settings_js);
}
}
else {
foreach (array_keys($items) as $key) {
if ($items[$key]['type'] === 'setting') {
$item = $items[$key];
unset($items[$key]);
$items[$key] = $item;
}
}
}
$page_state = array_merge($page_state, array_fill_keys(array_keys($items), 1));
if (isset($items['settings'])) {
$items['settings']['data'][] = array(
'ajaxPageState' => array(
'js' => $page_state,
),
);
}
if ($ajax) {
unset($items['settings']['data']);
}
foreach ($items as $key => $item) {
if (!isset($item['attributes'])) {
continue;
}
if (isset($item['attributes']['defer'])) {
$items[$key]['defer'] = $item['attributes']['defer'];
}
if (isset($item['attributes']['async'])) {
$items[$key]['async'] = $item['attributes']['async'];
}
if (isset($item['attributes']['onload'])) {
$items[$key]['onload'] = $item['attributes']['onload'];
}
if (isset($item['attributes']['onerror'])) {
$items[$key]['onerror'] = $item['attributes']['onerror'];
}
}
$elements = array(
'#type' => 'scripts',
'#items' => $items,
);
if (variable_get('advagg_enforce_scripts_callback', TRUE)) {
$scripts = element_info('scripts');
if (empty($scripts) || $scripts['#aggregate_callback'] !== '_advagg_aggregate_js') {
$element_info =& drupal_static('element_info');
advagg_element_info_alter($element_info);
if (function_exists('advagg_mod_element_info_alter')) {
advagg_mod_element_info_alter($element_info);
}
}
}
if (function_exists('advagg_mod_js_no_ajaxpagestate')) {
if (variable_get('advagg_mod_js_no_ajaxpagestate', ADVAGG_MOD_JS_NO_AJAXPAGESTATE)) {
advagg_mod_js_no_ajaxpagestate($elements);
}
}
return $elements;
}
function advagg_array_splice_assoc(array &$input, $offset, $length, $replacement) {
$replacement = (array) $replacement;
$key_indices = array_flip(array_keys($input));
if (isset($input[$offset]) && is_string($offset)) {
$offset = $key_indices[$offset];
}
if (isset($input[$length]) && is_string($length)) {
$length = $key_indices[$length] - $offset;
}
$input = array_slice($input, 0, $offset, TRUE) + $replacement + array_slice($input, $offset + $length, NULL, TRUE);
}
function advagg_remove_short_keys($value, $min_len = 3) {
if (strlen($value) < $min_len) {
return TRUE;
}
else {
return FALSE;
}
}
function advagg_get_js_scopes(array $javascript) {
if (empty($javascript)) {
return array();
}
$scopes = array();
$js_settings_in_footer = FALSE;
foreach ($javascript as $name => $item) {
if (!is_array($item) || empty($item['scope'])) {
continue;
}
if (!isset($scopes[$item['scope']])) {
$scopes[$item['scope']] = TRUE;
}
if ($name === 'settings' && $item['scope'] === 'footer') {
$js_settings_in_footer = TRUE;
}
}
if (empty($scopes)) {
$scopes['header'] = TRUE;
}
if (isset($scopes['header']) && count($scopes) > 1) {
$temp = $scopes['header'];
unset($scopes['header']);
$scopes['header'] = $temp;
}
if (isset($scopes['footer']) && count($scopes) > 1 && $js_settings_in_footer) {
$temp = $scopes['footer'];
unset($scopes['footer']);
$scopes['footer'] = $temp;
}
return $scopes;
}
function advagg_merge_plans(array $css_js_groups, array $plans) {
$used_keys = array();
foreach ($plans as $plan) {
$plan_added = FALSE;
foreach ($css_js_groups as $key => $group) {
$file_removed = FALSE;
foreach ($css_js_groups[$key]['items'] as $k => $values) {
if (is_array($values) && array_key_exists('data', $values) && is_array($plan['items']['files']) && is_string($values['data'])) {
$first_file = reset($plan['items']['files']);
if (array_key_exists($values['data'], $plan['items']['files'])) {
unset($css_js_groups[$key]['items'][$k]);
$file_removed = TRUE;
}
elseif (!empty($first_file['split'])) {
if ($values['data'] == $first_file['split_original']) {
if (!empty($first_file['split_last_part'])) {
unset($css_js_groups[$key]['items'][$k]);
}
$file_removed = TRUE;
}
}
}
}
if ($file_removed && !$plan_added) {
$step = 0;
do {
++$step;
$insert_key = '' . floatval($key) . '.' . sprintf('%03d', $step);
} while (array_key_exists($insert_key, $css_js_groups));
$css_js_groups[(string) $insert_key] = $plan;
$plan_added = TRUE;
}
}
foreach ($css_js_groups as $key => $group) {
if (empty($css_js_groups[$key]['items'])) {
unset($css_js_groups[$key]);
}
}
if (!$plan_added) {
foreach ($css_js_groups as $key => $group) {
if (empty($group['items']['aggregate_filenames_hash']) || $group['items']['aggregate_filenames_hash'] != $plan['items']['aggregate_filenames_hash'] || empty($group['items']['aggregate_contents_hash']) || $group['items']['aggregate_contents_hash'] != $plan['items']['aggregate_contents_hash']) {
continue;
}
do {
$key = '' . (floatval($key) + 0.01);
} while (array_key_exists((string) $key, $css_js_groups) || array_key_exists((string) $key, $used_keys));
$used_keys[(string) $key] = TRUE;
$css_js_groups[(string) $key] = $plan;
$plan_added = TRUE;
break;
}
}
}
ksort($css_js_groups);
$css_js_groups = array_values($css_js_groups);
return $css_js_groups;
}
function advagg_enabled() {
if (!function_exists('current_path')) {
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
}
$init =& drupal_static(__FUNCTION__);
if (!empty($init)) {
return variable_get('advagg_enabled', ADVAGG_ENABLED);
}
if (empty($GLOBALS['base_path'])) {
$GLOBALS['base_path'] = rtrim(dirname($_SERVER['SCRIPT_NAME']), '\\/') . '/';
}
$init = TRUE;
if (variable_get('advagg_needs_update', ADVAGG_NEEDS_UPDATE)) {
if (!db_table_exists('advagg_aggregates_versions')) {
$GLOBALS['conf']['advagg_enabled'] = FALSE;
if (user_access('administer site configuration')) {
drupal_set_message(t('Please run <a href="@link">database updates</a>. AdvAgg will remain disabled until done.', array(
'@link' => url('update.php'),
)), 'error');
}
}
else {
variable_del('advagg_needs_update');
}
}
else {
$config_path = advagg_admin_config_root_path();
$current_path = current_path();
$arg = arg();
$arg += array(
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
);
$admin_theme = variable_get('admin_theme');
$list_of_pages = variable_get('advagg_disable_on_listed_pages');
$pages = trim(drupal_strtolower($list_of_pages));
$path = drupal_strtolower(drupal_get_path_alias(current_path()));
$page_match = drupal_match_path($path, $pages);
if ($page_match) {
$GLOBALS['conf']['advagg_enabled'] = FALSE;
$GLOBALS['conf']['preprocess_css'] = FALSE;
$GLOBALS['conf']['preprocess_js'] = FALSE;
}
if (variable_get('advagg_disable_on_admin', ADVAGG_DISABLE_ON_ADMIN) && $GLOBALS['theme'] === $admin_theme && path_is_admin($current_path) && !($arg[1] === 'reports' && $arg[2] === 'status') && !($arg[2] === 'development' && $arg[3] === 'performance' && empty($arg[4])) && !($arg[1] === 'appearance' && $arg[2] === 'settings' && !empty($arg[3])) && stripos($current_path, $config_path . '/advagg') !== 0) {
$GLOBALS['conf']['advagg_enabled'] = FALSE;
$GLOBALS['conf']['preprocess_css'] = FALSE;
$GLOBALS['conf']['preprocess_js'] = FALSE;
}
$cookie_name = 'AdvAggDisabled';
$bypass_cookie = FALSE;
$key = drupal_hmac_base64('advagg_cookie', drupal_get_private_key() . drupal_get_hash_salt() . variable_get('cron_key', 'drupal'));
if (!empty($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] == $key) {
$bypass_cookie = TRUE;
}
if (isset($_GET['advagg']) && $_GET['advagg'] == 1 && !defined('MAINTENANCE_MODE') && (user_access('bypass advanced aggregation') || $bypass_cookie)) {
$GLOBALS['conf']['advagg_enabled'] = TRUE;
$GLOBALS['conf']['preprocess_css'] = TRUE;
$GLOBALS['conf']['preprocess_js'] = TRUE;
}
if (defined('MAINTENANCE_MODE')) {
$GLOBALS['conf']['advagg_enabled'] = FALSE;
}
if (variable_get('advagg_enabled', ADVAGG_ENABLED)) {
if ($bypass_cookie && !isset($_GET['advagg'])) {
$GLOBALS['conf']['advagg_enabled'] = FALSE;
$GLOBALS['conf']['preprocess_css'] = FALSE;
$GLOBALS['conf']['preprocess_js'] = FALSE;
$bypass_cookie = TRUE;
static $msg_set;
if (!isset($msg_set) && variable_get('advagg_show_bypass_cookie_message', ADVAGG_SHOW_BYPASS_COOKIE_MESSAGE)) {
$msg_set = TRUE;
if (user_access('administer site configuration')) {
drupal_set_message(t('The AdvAgg bypass cookie is currently enabled. Turn it off by going to the <a href="@advagg_operations">AdvAgg Operations</a> page and clicking the <em>Toggle the "aggregation bypass cookie" for this browser</em> button.', array(
'@advagg_operations' => url(advagg_admin_config_root_path() . '/advagg/operations', array(
'fragment' => 'edit-bypass',
)),
)));
}
else {
drupal_set_message(t('The AdvAgg bypass cookie is currently enabled. Turn it off by <a href="@login">logging in</a> with a user with the "administer site configuration" permissions and going to the AdvAgg Operations page (located at @advagg_operations) and clicking the <em>Toggle the "aggregation bypass cookie" for this browser</em> button.', array(
'@login' => 'user/login',
'@advagg_operations' => advagg_admin_config_root_path() . '/advagg/operations',
)));
}
}
}
if (isset($_GET['advagg']) && $_GET['advagg'] == -1 && (user_access('bypass advanced aggregation') || $bypass_cookie)) {
$GLOBALS['conf']['advagg_enabled'] = FALSE;
$GLOBALS['conf']['preprocess_css'] = FALSE;
$GLOBALS['conf']['preprocess_js'] = FALSE;
}
if (isset($_GET['advagg-core']) && $_GET['advagg-core'] == 0 && (user_access('bypass advanced aggregation') || $bypass_cookie)) {
$GLOBALS['conf']['preprocess_css'] = FALSE;
$GLOBALS['conf']['preprocess_js'] = FALSE;
}
if (isset($_GET['advagg-core']) && $_GET['advagg-core'] == 1 && (user_access('bypass advanced aggregation') || $bypass_cookie)) {
$GLOBALS['conf']['preprocess_css'] = TRUE;
$GLOBALS['conf']['preprocess_js'] = TRUE;
}
if (isset($_GET['advagg-debug']) && (user_access('bypass advanced aggregation') || $bypass_cookie)) {
$GLOBALS['conf']['advagg_debug'] = (int) $_GET['advagg-debug'];
}
}
}
return variable_get('advagg_enabled', ADVAGG_ENABLED);
}
function advagg_admin_config_root_path() {
return variable_get('advagg_admin_config_root_path', ADVAGG_ADMIN_CONFIG_ROOT_PATH);
}
function advagg_current_hooks_hash_array() {
$aggregate_settings =& drupal_static(__FUNCTION__);
if (!empty($aggregate_settings)) {
return $aggregate_settings;
}
list($css_path, $js_path) = advagg_get_root_files_dir();
$aggregate_settings = array(
'variables' => array(
'advagg_gzip' => variable_get('advagg_gzip', ADVAGG_GZIP),
'advagg_brotli' => variable_get('advagg_brotli', ADVAGG_BROTLI),
'advagg_no_zopfli' => variable_get('advagg_no_zopfli', ADVAGG_NO_ZOPFLI),
'is_https' => $GLOBALS['is_https'],
'advagg_global_counter' => advagg_get_global_counter(),
'base_path' => $GLOBALS['base_path'],
'advagg_ie_css_selector_limiter' => variable_get('advagg_ie_css_selector_limiter', ADVAGG_IE_CSS_SELECTOR_LIMITER),
'advagg_ie_css_selector_limiter_value' => variable_get('advagg_ie_css_selector_limiter_value', ADVAGG_IE_CSS_SELECTOR_LIMITER_VALUE),
'advagg_scripts_scope_anywhere' => variable_get('advagg_scripts_scope_anywhere', ADVAGG_SCRIPTS_SCOPE_ANYWHERE),
'advagg_devel' => variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0 ? TRUE : FALSE,
'advagg_convert_absolute_to_relative_path' => variable_get('advagg_convert_absolute_to_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_RELATIVE_PATH),
'advagg_convert_absolute_to_protocol_relative_path' => variable_get('advagg_convert_absolute_to_protocol_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_PROTOCOL_RELATIVE_PATH),
'advagg_css_absolute_path' => variable_get('advagg_css_absolute_path', ADVAGG_CSS_ABSOLUTE_PATH),
'advagg_force_https_path' => variable_get('advagg_force_https_path', ADVAGG_FORCE_HTTPS_PATH),
'advagg_css_dir' => $css_path[0],
'advagg_js_dir' => $js_path[0],
),
'hooks' => advagg_hooks_implemented(FALSE),
);
if (module_exists('locale')) {
$aggregate_settings['variables']['language'] = isset($GLOBALS['language']->language) ? $GLOBALS['language']->language : '';
}
if (variable_get('advagg_include_base_url', ADVAGG_INCLUDE_BASE_URL)) {
$aggregate_settings['variables']['base_url'] = $GLOBALS['base_url'];
}
if (!function_exists('cdn_advagg_current_hooks_hash_array_alter') && module_exists('cdn')) {
$aggregate_settings['variables'][CDN_MODE_VARIABLE] = variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC);
$aggregate_settings['variables'][CDN_BASIC_FARFUTURE_VARIABLE] = variable_get(CDN_BASIC_FARFUTURE_VARIABLE, CDN_BASIC_FARFUTURE_DEFAULT);
$aggregate_settings['variables'][CDN_HTTPS_SUPPORT_VARIABLE] = variable_get(CDN_HTTPS_SUPPORT_VARIABLE, FALSE);
$aggregate_settings['variables'][CDN_STATUS_VARIABLE] = variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED);
$aggregate_settings['variables']['cdn_request_is_https'] = cdn_request_is_https();
$aggregate_settings['variables']['cdn_check_drupal_path'] = cdn_check_drupal_path($_GET['q']);
}
drupal_alter('advagg_current_hooks_hash_array', $aggregate_settings);
return $aggregate_settings;
}
function advagg_get_current_hooks_hash() {
$current_hash =& drupal_static(__FUNCTION__);
if (empty($current_hash)) {
$aggregate_settings = advagg_current_hooks_hash_array();
$serialize_function = variable_get('advagg_serialize', ADVAGG_SERIALIZE);
$current_hash = drupal_hash_base64($serialize_function($aggregate_settings));
$settings = advagg_get_hash_settings($current_hash);
if (empty($settings)) {
advagg_set_hash_settings($current_hash, $aggregate_settings);
}
}
return $current_hash;
}
function advagg_set_hash_settings($hash, array $settings = array()) {
return db_merge('advagg_aggregates_hashes')
->key(array(
'hash' => $hash,
))
->fields(array(
'hash' => $hash,
'settings' => serialize($settings),
))
->execute();
}
function advagg_hooks_implemented($all = TRUE) {
$hooks = array(
'advagg_get_css_file_contents_pre_alter' => array(),
'advagg_get_css_file_contents_alter' => array(),
'advagg_get_css_aggregate_contents_alter' => array(),
'advagg_get_js_file_contents_alter' => array(),
'advagg_get_js_aggregate_contents_alter' => array(),
'advagg_save_aggregate_pre_alter' => array(),
'advagg_save_aggregate_alter' => array(),
'advagg_current_hooks_hash_array_alter' => array(),
'advagg_get_root_files_dir_alter' => array(),
'advagg_context_alter' => array(),
);
if ($all) {
$hooks += array(
'advagg_build_aggregate_plans_alter' => array(),
'advagg_build_aggregate_plans_post_alter' => array(),
'advagg_changed_files' => array(),
'advagg_css_groups_alter' => array(),
'advagg_js_groups_alter' => array(),
'advagg_modify_css_pre_render_alter' => array(),
'advagg_modify_js_pre_render_alter' => array(),
'advagg_get_info_on_files_alter' => array(),
'advagg_hooks_implemented_alter' => array(),
'advagg_removed_aggregates' => array(),
'advagg_scan_for_changes' => array(),
'advagg_missing_root_file' => array(),
'js_alter' => array(),
'css_alter' => array(),
);
}
drupal_alter('advagg_hooks_implemented', $hooks, $all);
$serialize_function = variable_get('advagg_serialize', ADVAGG_SERIALIZE);
$cid = 'advagg_hooks_implemented:' . (int) $all . ':' . drupal_hash_base64($serialize_function($hooks));
$cache = cache_get($cid, 'cache_bootstrap');
if (!empty($cache->data)) {
$hooks = $cache->data;
}
else {
foreach ($hooks as $hook => $values) {
$hooks[$hook] = module_implements($hook);
$theme_keys = array_keys(list_themes());
if (!empty($theme_keys)) {
foreach ($theme_keys as $theme_key) {
$function = $theme_key . '_' . $hook;
if (function_exists($function)) {
$hooks[$hook][] = $theme_key;
}
}
}
}
cache_set($cid, $hooks, 'cache_bootstrap', CACHE_TEMPORARY);
}
return $hooks;
}
function advagg_get_hash_settings($hash) {
$settings = db_select('advagg_aggregates_hashes', 'aah')
->fields('aah', array(
'settings',
))
->condition('hash', $hash)
->execute()
->fetchField();
return !empty($settings) ? unserialize($settings) : array();
}
function advagg_get_root_files_dir($reset = FALSE) {
$css_paths =& drupal_static(__FUNCTION__ . '_css');
$js_paths =& drupal_static(__FUNCTION__ . '_js');
if (empty($css_paths) || empty($js_paths) || $reset) {
$prefix = variable_get('advagg_root_dir_prefix', ADVAGG_ROOT_DIR_PREFIX);
$css_paths[0] = $prefix . 'advagg_css';
$js_paths[0] = $prefix . 'advagg_js';
file_prepare_directory($css_paths[0], FILE_CREATE_DIRECTORY);
file_prepare_directory($js_paths[0], FILE_CREATE_DIRECTORY);
$css_paths[1] = advagg_get_relative_path($css_paths[0], 'css');
$js_paths[1] = advagg_get_relative_path($js_paths[0], 'js');
if (empty($css_paths[1]) && !empty($js_paths[1])) {
$css_paths[1] = str_replace('/advagg_js', '/advagg_css', $js_paths[1]);
}
elseif (empty($js_paths[1]) && !empty($css_paths[1])) {
$js_paths[1] = str_replace('/advagg_css', '/advagg_js', $css_paths[1]);
}
if (empty($css_paths[1])) {
$css_paths[1] = $css_paths[0];
}
if (empty($js_paths[1])) {
$js_paths[1] = $js_paths[0];
}
drupal_alter('advagg_get_root_files_dir', $css_paths, $js_paths);
}
return array(
$css_paths,
$js_paths,
);
}
function advagg_get_relative_path($uri, $type = '') {
$wrapper = file_stream_wrapper_get_instance_by_uri($uri);
if ($wrapper instanceof DrupalLocalStreamWrapper) {
$relative_path = $wrapper
->getDirectoryPath() . '/' . file_uri_target($uri);
}
else {
$relative_path = parse_url(file_create_url($uri), PHP_URL_PATH);
if (empty($relative_path) && !empty($uri)) {
$filename = advagg_generate_advagg_filename_from_db($type);
$relative_path = parse_url(file_create_url("{$uri}/{$filename}"), PHP_URL_PATH);
$end = strpos($relative_path, "/{$filename}");
if ($end !== FALSE) {
$relative_path = substr($relative_path, 0, $end);
}
}
if (substr($relative_path, 0, strlen($GLOBALS['base_path'])) == $GLOBALS['base_path']) {
$relative_path = substr($relative_path, strlen($GLOBALS['base_path']));
}
$relative_path = ltrim($relative_path, '/');
}
return $relative_path;
}
function advagg_build_aggregates(array $filenames, $type) {
if (empty($filenames)) {
return array();
}
if (empty($type)) {
$filename = reset($filenames);
$type = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
}
module_load_include('inc', 'advagg', 'advagg.missing');
list($css_path, $js_path) = advagg_get_root_files_dir();
$return = array();
foreach ($filenames as $filename) {
if ($type === 'css') {
$uri = $css_path[0] . '/' . $filename;
}
elseif ($type === 'js') {
$uri = $js_path[0] . '/' . $filename;
}
if (file_exists($uri)) {
continue;
}
$lock_name = 'advagg_' . $filename;
if (variable_get('advagg_no_locks', ADVAGG_NO_LOCKS)) {
$return[$filename] = advagg_missing_create_file($filename);
}
elseif (lock_acquire($lock_name, 10)) {
$return[$filename] = advagg_missing_create_file($filename);
lock_release($lock_name);
}
}
return $return;
}
function advagg_build_ajax_js_css() {
$settings = array();
foreach (array(
'css',
'js',
) as $type) {
if (empty($_POST['ajax_page_state'][$type])) {
$core_items[$type] = $items[$type] = array();
$scripts = drupal_add_js();
if (!empty($scripts['settings'])) {
$settings = $scripts['settings'];
}
}
else {
$function = 'drupal_add_' . $type;
$items[$type] = $function();
drupal_alter($type, $items[$type]);
$core_items[$type] = $items[$type];
drupal_alter($type . '_post', $items[$type]);
foreach ($items[$type] as $key => $item) {
if (is_numeric($key)) {
unset($items[$type][$key]);
}
}
foreach ($core_items[$type] as $key => $core_item) {
if (is_numeric($key)) {
unset($core_items[$type][$key]);
}
}
$items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);
$core_items[$type] = array_diff_key($core_items[$type], $_POST['ajax_page_state'][$type]);
}
}
$scripts = drupal_add_js();
if (isset($scripts['settings'])) {
$settings = $scripts['settings'];
unset($items['js']['settings']);
unset($core_items['js']['settings']);
}
$styles = drupal_get_css($core_items['css'], TRUE);
$scripts_footer = drupal_get_js('footer', $core_items['js'], TRUE);
$scripts_header = drupal_get_js('header', $core_items['js'], TRUE);
return array(
$styles,
$scripts_header,
$scripts_footer,
$items,
$settings,
);
}
function advagg_file_aggregation_enabled($type) {
if (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE === 'update') {
return FALSE;
}
if (isset($_GET['advagg']) && $_GET['advagg'] == 0 && user_access('bypass advanced aggregation')) {
return FALSE;
}
if ($type === 'css') {
return variable_get('preprocess_css', FALSE);
}
if ($type === 'js') {
return variable_get('preprocess_js', FALSE);
}
}
function advagg_multi_update_atime(array $files) {
$write_done = FALSE;
$records = array();
foreach ($files as $values) {
$cid = 'advagg:db:' . $values['aggregate_filenames_hash'] . ADVAGG_SPACE . $values['aggregate_contents_hash'];
$records[$cid] = array(
'aggregate_filenames_hash' => $values['aggregate_filenames_hash'],
'aggregate_contents_hash' => $values['aggregate_contents_hash'],
'atime' => REQUEST_TIME,
);
}
$cids = array_keys($records);
$caches = cache_get_multiple($cids, 'cache_advagg_info');
if (!empty($caches)) {
foreach ($caches as $cache) {
if (!empty($cache->data['atime']) && $cache->data['atime'] > REQUEST_TIME - 12 * 60 * 60) {
unset($records[$cache->cid]);
}
}
}
if (empty($records)) {
return $write_done;
}
foreach ($records as $cid => $record) {
$result = db_merge('advagg_aggregates_versions')
->key(array(
'aggregate_filenames_hash' => $record['aggregate_filenames_hash'],
'aggregate_contents_hash' => $record['aggregate_contents_hash'],
))
->fields(array(
'atime' => $record['atime'],
))
->execute();
if (!$write_done && $result) {
$write_done = TRUE;
}
$cache = cache_get($cid, 'cache_advagg_info');
if (empty($cache->data)) {
$cache = new stdClass();
}
$cache->data['atime'] = REQUEST_TIME;
cache_set($cid, $cache->data, 'cache_advagg_info', round(REQUEST_TIME + 1209600 + mt_rand(0, 3888000), -3));
}
return $write_done;
}
function advagg_get_global_counter() {
$global_counter = variable_get('advagg_global_counter', ADVAGG_GLOBAL_COUNTER);
return $global_counter;
}
function advagg_admin_flush_cache() {
module_load_include('inc', 'advagg', 'advagg.admin');
advagg_admin_flush_cache_button();
}
function theme_html_script_tag(array $variables) {
$element = $variables['element'];
$attributes = '';
$onload = '';
$onerror = '';
if (isset($element['#attributes'])) {
if (!empty($element['#attributes']['onload'])) {
$onload = $element['#attributes']['onload'];
unset($element['#attributes']['onload']);
}
if (!empty($element['#attributes']['onerror'])) {
$onerror = $element['#attributes']['onerror'];
unset($element['#attributes']['onerror']);
}
$attributes = !empty($element['#attributes']) ? drupal_attributes($element['#attributes']) : '';
if (!empty($onload)) {
$attributes .= ' onload="' . advagg_jsspecialchars($onload) . '"';
}
if (!empty($onerror)) {
$attributes .= ' onerror="' . advagg_jsspecialchars($onerror) . '"';
}
}
if (!isset($element['#value'])) {
return '<' . $element['#tag'] . $attributes . " />\n";
}
else {
$output = '<' . $element['#tag'] . $attributes . '>';
if (isset($element['#value_prefix'])) {
$output .= $element['#value_prefix'];
}
$output .= $element['#value'];
if (isset($element['#value_suffix'])) {
$output .= $element['#value_suffix'];
}
$output .= '</' . $element['#tag'] . ">\n";
return $output;
}
}
function advagg_jsspecialchars($string = '') {
$string = str_replace('"', '"', $string);
$string = str_replace("'", ''', $string);
return $string;
}
function advagg_pre_render_scripts(array $elements) {
if (!empty($elements['#groups'])) {
return $elements;
}
if (isset($elements['#group_callback'])) {
$elements['#groups'] = $elements['#group_callback']($elements['#items']);
}
if (isset($elements['#aggregate_callback'])) {
$elements['#aggregate_callback']($elements['#groups']);
}
$default_query_string = variable_get('css_js_query_string', '0');
$embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
$embed_suffix = "\n//--><!]]>\n";
$js_version_string = variable_get('drupal_js_version_query_string', 'v=');
$element_defaults = array(
'#type' => 'html_script_tag',
'#tag' => 'script',
'#value' => '',
'#attributes' => array(),
);
$hooks = theme_get_registry(FALSE);
if (empty($hooks['html_script_tag'])) {
$element_defaults['#type'] = 'html_tag';
}
foreach ($elements['#groups'] as $group) {
if (isset($group['type']) && $group['type'] === 'file' && isset($group['data'])) {
$element = $element_defaults;
$element['#attributes']['src'] = advagg_file_create_url($group['data']) . ($group['cache'] ? '' : '?' . REQUEST_TIME);
$element['#browsers'] = $group['browsers'];
if (!empty($group['defer'])) {
$element['#attributes']['defer'] = 'defer';
}
if (!empty($group['async'])) {
$element['#attributes']['async'] = 'async';
}
if (!empty($group['onload'])) {
if (!isset($element['#attributes']['onload'])) {
$element['#attributes']['onload'] = '';
}
$element['#attributes']['onload'] .= $group['onload'];
}
if (!empty($group['onerror'])) {
if (!isset($element['#attributes']['onerror'])) {
$element['#attributes']['onerror'] = '';
}
$element['#attributes']['onerror'] .= $group['onerror'];
}
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
else {
foreach ($group['items'] as $item) {
if (empty($item['data'])) {
continue;
}
$element = $element_defaults;
if (!empty($item['defer'])) {
$element['#attributes']['defer'] = 'defer';
}
if (!empty($item['async'])) {
$element['#attributes']['async'] = 'async';
}
if (!empty($item['onload'])) {
if (!isset($element['#attributes']['onload'])) {
$element['#attributes']['onload'] = '';
}
$element['#attributes']['onload'] .= $item['onload'];
}
if (!empty($item['onerror'])) {
if (!isset($element['#attributes']['onerror'])) {
$element['#attributes']['onerror'] = '';
}
$element['#attributes']['onerror'] .= $item['onerror'];
}
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$element['#browsers'] = isset($item['browsers']) ? $item['browsers'] : array();
if (empty($item['type'])) {
if (is_array($item['data'])) {
$item['type'] = 'setting';
}
elseif (strpos($item['data'], 'http://') === 0 || strpos($item['data'], 'https://') === 0 || strpos($item['data'], '//') === 0) {
$item['type'] = 'external';
}
elseif (file_exists(trim($item['data']))) {
$item['type'] = 'file';
}
else {
$item['type'] = 'inline';
}
}
switch ($item['type']) {
case 'setting':
$data = advagg_cleanup_settings_array(drupal_array_merge_deep_array(array_filter($item['data'], 'is_array')));
$json_data = advagg_json_encode($data);
$element['#value_prefix'] = $embed_prefix;
$element['#value'] = 'jQuery.extend(Drupal.settings, ' . $json_data . ");";
$element['#value_suffix'] = $embed_suffix;
break;
case 'inline':
$encoding = advagg_get_encoding_from_bom($item['data']);
if (!empty($encoding)) {
$item['data'] = advagg_convert_to_utf8($item['data'], $encoding);
}
$element['#value_prefix'] = $embed_prefix;
$element['#value'] = $item['data'];
$element['#value_suffix'] = $embed_suffix;
break;
case 'file':
$query_string_separator = strpos($item['data'], '?') !== FALSE ? '&' : '?';
$cache_validator = REQUEST_TIME;
if (!empty($item['cache'])) {
$cache_validator = empty($item['version']) ? $default_query_string : $js_version_string . $item['version'];
}
$element['#attributes']['src'] = advagg_file_create_url($item['data']) . $query_string_separator . $cache_validator;
break;
case 'external':
$file_uri = $item['data'];
if (variable_get('advagg_convert_absolute_to_protocol_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_PROTOCOL_RELATIVE_PATH)) {
$file_uri = advagg_convert_abs_to_protocol($item['data']);
}
$element['#attributes']['src'] = $file_uri;
break;
}
$elements[] = $element;
}
}
}
return $elements;
}
function advagg_get_css_prefix_suffix() {
$embed_prefix = "\n/* <![CDATA[ */\n";
$embed_suffix = "\n/* ]]> */\n";
return array(
$embed_prefix,
$embed_suffix,
);
}
function advagg_pre_render_styles(array $elements) {
if (!advagg_enabled()) {
return drupal_pre_render_styles($elements);
}
if (!empty($elements['#groups'])) {
return $elements;
}
if (isset($elements['#group_callback'])) {
$elements['#groups'] = $elements['#group_callback']($elements['#items']);
}
if (isset($elements['#aggregate_callback'])) {
$elements['#aggregate_callback']($elements['#groups']);
}
$query_string = variable_get('css_js_query_string', '0');
list($embed_prefix, $embed_suffix) = advagg_get_css_prefix_suffix();
$link_element_defaults = array(
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => array(
'type' => 'text/css',
'rel' => 'stylesheet',
),
);
$style_element_defaults = array(
'#type' => 'html_tag',
'#tag' => 'style',
'#attributes' => array(
'type' => 'text/css',
),
);
foreach ($elements['#groups'] as $group) {
switch ($group['type']) {
case 'file':
if (isset($group['data'])) {
$element = $link_element_defaults;
$element['#attributes']['href'] = advagg_file_create_url($group['data']);
$element['#attributes']['media'] = $group['media'];
$element['#browsers'] = $group['browsers'];
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
elseif ($group['preprocess']) {
$import = array();
foreach ($group['items'] as $item) {
if (file_exists($item['data'])) {
$import[] = '@import url("' . check_plain(advagg_file_create_url($item['data']) . '?' . $query_string) . '");';
}
}
while (!empty($import)) {
$import_batch = array_slice($import, 0, 31);
$import = array_slice($import, 31);
$element = $style_element_defaults;
$element['#value'] = "\n" . implode("\n", $import_batch) . "\n";
$element['#attributes']['media'] = $group['media'];
$element['#browsers'] = $group['browsers'];
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
}
else {
foreach ($group['items'] as $item) {
$element = $link_element_defaults;
$query_string_separator = strpos($item['data'], '?') !== FALSE ? '&' : '?';
$element['#attributes']['href'] = advagg_file_create_url($item['data']) . $query_string_separator . $query_string;
$element['#attributes']['media'] = $item['media'];
$element['#browsers'] = $group['browsers'];
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
}
break;
case 'inline':
if (isset($group['data'])) {
$element = $style_element_defaults;
$element['#value'] = $group['data'];
$element['#value_prefix'] = $embed_prefix;
$element['#value_suffix'] = $embed_suffix;
$element['#attributes']['media'] = $group['media'];
$element['#browsers'] = $group['browsers'];
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
else {
foreach ($group['items'] as $item) {
$element = $style_element_defaults;
$element['#value'] = $item['data'];
$element['#value_prefix'] = $embed_prefix;
$element['#value_suffix'] = $embed_suffix;
$element['#attributes']['media'] = $item['media'];
$element['#browsers'] = $group['browsers'];
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
}
break;
case 'external':
foreach ($group['items'] as $item) {
$element = $link_element_defaults;
$file_uri = $item['data'];
if (variable_get('advagg_convert_absolute_to_protocol_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_PROTOCOL_RELATIVE_PATH)) {
$file_uri = advagg_convert_abs_to_protocol($item['data']);
}
$element['#attributes']['href'] = $file_uri;
$element['#attributes']['media'] = $item['media'];
$element['#browsers'] = $group['browsers'];
if (!empty($group['attributes'])) {
$element['#attributes'] += $group['attributes'];
}
$elements[] = $element;
}
break;
}
}
return $elements;
}
function advagg_group_js(array $javascript) {
$groups = array();
$current_group_keys = NULL;
$index = -1;
foreach ($javascript as $key => $item) {
if (empty($item)) {
continue;
}
if (isset($item['browsers'])) {
ksort($item['browsers']);
}
else {
$item['browsers'] = array();
}
if (empty($item['type'])) {
if ($key === 'settings') {
$item['type'] = 'setting';
}
elseif (stripos($item['data'], 'http://') === 0 || stripos($item['data'], 'https://') === 0 || strpos($item['data'], '//') === 0 && strpos($item['data'], '///') === FALSE) {
$item['type'] = 'external';
}
elseif (strpos($item['data'], ';') !== FALSE || strpos($item['data'], "\n") || strpos($item['data'], "\$") || strpos($item['data'], "'") || strpos($item['data'], '"')) {
$item['type'] = 'inline';
}
elseif (stripos(strrev($item['data']), strrev('.js')) === 0) {
$item['type'] = 'file';
}
}
switch ($item['type']) {
case 'file':
$group_keys = !empty($item['preprocess']) ? array(
$item['type'],
$item['group'],
$item['every_page'],
$item['browsers'],
) : FALSE;
break;
case 'external':
case 'setting':
case 'inline':
$group_keys = FALSE;
break;
default:
$group_keys = NULL;
watchdog('advagg', 'Bad javascript was added. Type is unknown. @key - @item', array(
'@key' => $key,
'@item' => print_r($item, TRUE),
), WATCHDOG_NOTICE);
break;
}
if ($group_keys !== $current_group_keys) {
++$index;
$groups[$index] = $item;
unset($groups[$index]['data'], $groups[$index]['weight']);
$groups[$index]['items'] = array();
$current_group_keys = $group_keys ? $group_keys : NULL;
}
$groups[$index]['items'][] = $item;
}
return $groups;
}
function advagg_drupal_sort_css_js_stable(array &$items) {
$nested = array();
foreach ($items as $key => &$item) {
if (!isset($item['weight'])) {
$item['weight'] = 0;
}
if (!isset($item['every_page'])) {
$item['every_page'] = FALSE;
}
if (!isset($item['group'])) {
$item['group'] = 0;
}
if (!isset($item['scope'])) {
$item['scope'] = 'header';
}
$weight = (string) $item['weight'];
$nested[$item['group']][$item['every_page'] ? 1 : 0][$weight][$key] = $item;
}
$sorted = array();
ksort($nested);
foreach ($nested as &$group_items) {
krsort($group_items);
foreach ($group_items as &$ep_items) {
ksort($ep_items);
foreach ($ep_items as &$weight_items) {
foreach ($weight_items as $key => &$item) {
$sorted[$key] = $item;
}
unset($item);
}
}
unset($ep_items);
}
unset($group_items);
$items = $sorted;
}
function advagg_json_encode($data) {
static $php550;
static $php540;
static $php530;
if (!isset($php550)) {
$php550 = version_compare(PHP_VERSION, '5.5.0', '>=');
}
if (!isset($php540)) {
$php540 = version_compare(PHP_VERSION, '5.4.0', '>=');
}
if (!isset($php530)) {
$php530 = version_compare(PHP_VERSION, '5.3.0', '>=');
}
if (!$php530) {
return @drupal_json_encode($data);
}
$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
if ($php550 && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 0) {
$options |= JSON_PARTIAL_OUTPUT_ON_ERROR;
}
if ($php540 && variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
$options |= JSON_PRETTY_PRINT;
}
$json_data = @json_encode($data, $options);
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
$error_number = json_last_error();
switch ($error_number) {
case JSON_ERROR_NONE:
$error_message = '';
break;
case JSON_ERROR_DEPTH:
$error_message = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error_message = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error_message = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error_message = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error_message = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
$error_message = 'Unknown error: ' . $error_number;
break;
}
if (!empty($error_message)) {
if (is_callable('httprl_pr')) {
$pretty_data = httprl_pr($data);
}
elseif (is_callable('kprint_r')) {
$pretty_data = kprint_r($data, TRUE);
}
else {
$pretty_data = '<pre>' . filter_xss(print_r($data, TRUE)) . '</pre>';
}
watchdog('advagg_json', 'Error with json encoding the Drupal.settings value. Error Message: %error_message. JSON Data: !data', array(
'%error_message' => $error_message,
'!data' => $pretty_data,
), WATCHDOG_ERROR);
}
}
return $json_data;
}
function advagg_scan_filesystem_for_changes_live() {
static $function_has_ran;
if (isset($function_has_ran)) {
return;
}
$function_has_ran = TRUE;
$bypass_cookie = FALSE;
$cookie_name = 'AdvAggDisabled';
$key = drupal_hmac_base64('advagg_cookie', drupal_get_private_key() . drupal_get_hash_salt() . variable_get('cron_key', 'drupal'));
if (!empty($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name] == $key) {
$bypass_cookie = TRUE;
}
if (!advagg_enabled() && !$bypass_cookie || variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 0) {
return;
}
module_load_include('inc', 'advagg', 'advagg.cache');
$flushed = advagg_push_new_changes();
if (empty($flushed) || !user_is_logged_in()) {
return;
}
list($css_path) = advagg_get_root_files_dir();
$parts_uri = $css_path[1] . '/parts';
foreach ($flushed as $filename => $data) {
if (strpos($filename, $parts_uri) === 0) {
continue;
}
if (variable_get('advagg_show_file_changed_message', ADVAGG_SHOW_FILE_CHANGED_MESSAGE)) {
$ext = pathinfo($filename, PATHINFO_EXTENSION);
drupal_set_message(t('The file %filename has changed. %db_usage aggregates are using this file. %db_count db cache entries and all %type full cache entries have been flushed from the cache bins. Trigger: <code>@changes</code>', array(
'%filename' => $filename,
'%db_usage' => count($data[0]),
'%db_count' => count($data[1]),
'@changes' => print_r($data[2], TRUE),
'%type' => $ext,
)));
}
}
}
function advagg_match_file_pattern($filename) {
return preg_match('/.*(j|cs)s' . ADVAGG_SPACE . '[A-Za-z0-9-_]{43}' . ADVAGG_SPACE . '[A-Za-z0-9-_]{43}' . ADVAGG_SPACE . '[A-Za-z0-9-_]{43}\\.(j|cs)s$/', $filename);
}
function advagg_convert_abs_to_rel($path, $strip_base_path = FALSE) {
$base_url = $GLOBALS['base_url'];
if (strpos(strrev($base_url), '/') !== 0) {
$base_url .= '/';
}
$base_path = $GLOBALS['base_path'];
if ($strip_base_path) {
$base_path = '';
}
$base_url_https = advagg_force_https_path($base_url);
$path = str_replace($base_url_https, $base_path, $path);
$base_url_http = advagg_force_http_path($base_url);
$path = str_replace($base_url_http, $base_path, $path);
$base_url = advagg_convert_abs_to_protocol($GLOBALS['base_url']);
if (strpos(strrev($base_url), '/') !== 0) {
$base_url .= '/';
}
$path = str_replace($base_url, $base_path, $path);
return $path;
}
function advagg_convert_abs_to_protocol($path) {
if (strpos($path, 'http://') === 0) {
$path = substr($path, 5);
}
return $path;
}
function advagg_force_https_path($path) {
if (strpos($path, 'http://') === 0) {
$path = 'https://' . substr($path, 7);
}
elseif (strpos($path, '//') === 0) {
$path = 'https:' . $path;
}
return $path;
}
function advagg_force_http_path($path) {
if (strpos($path, 'https://') === 0) {
$path = 'http://' . substr($path, 8);
}
return $path;
}
function advagg_file_create_url($path, array $aggregate_settings = array(), $run_file_create_url = TRUE, $source_type = '') {
$file_uri = $path;
if ($run_file_create_url) {
$file_uri = file_create_url($path);
}
elseif (strpos($path, '/') !== 0 && !advagg_is_external($path)) {
$file_uri = '/' . $path;
}
if (isset($aggregate_settings['variables']['advagg_convert_absolute_to_relative_path']) && $aggregate_settings['variables']['advagg_convert_absolute_to_relative_path'] || !isset($aggregate_settings['variables']['advagg_convert_absolute_to_relative_path']) && variable_get('advagg_convert_absolute_to_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_RELATIVE_PATH)) {
$file_uri = advagg_convert_abs_to_rel($file_uri);
}
if (isset($aggregate_settings['variables']['advagg_convert_absolute_to_protocol_relative_path']) && $aggregate_settings['variables']['advagg_convert_absolute_to_protocol_relative_path'] || !isset($aggregate_settings['variables']['advagg_convert_absolute_to_protocol_relative_path']) && variable_get('advagg_convert_absolute_to_protocol_relative_path', ADVAGG_CONVERT_ABSOLUTE_TO_PROTOCOL_RELATIVE_PATH)) {
$file_uri = advagg_convert_abs_to_protocol($file_uri);
}
if ($source_type === 'css' && !advagg_is_external($file_uri) && (isset($aggregate_settings['variables']['advagg_css_absolute_path']) && $aggregate_settings['variables']['advagg_css_absolute_path'] || !isset($aggregate_settings['variables']['advagg_css_absolute_path']) && variable_get('advagg_css_absolute_path', ADVAGG_CSS_ABSOLUTE_PATH))) {
list($css_path) = advagg_get_root_files_dir();
$parsed = parse_url($css_path[0]);
$new_parsed = array();
if (!empty($parsed['host'])) {
$new_parsed['host'] = $parsed['host'];
}
if (!empty($parsed['path'])) {
$new_parsed['path'] = $parsed['path'];
}
$css_path_0 = advagg_glue_url($new_parsed);
$parsed = parse_url($css_path[1]);
$new_parsed = array();
if (!empty($parsed['host'])) {
$new_parsed['host'] = $parsed['host'];
}
if (!empty($parsed['path'])) {
$new_parsed['path'] = $parsed['path'];
}
$css_path_1 = advagg_glue_url($new_parsed);
$pos = strpos($css_path_1, $css_path_0);
if (!empty($pos)) {
$public_dir = substr($css_path_1, 0, $pos);
if (strpos($file_uri, $public_dir) === FALSE) {
$file_uri = url($path, array(
'absolute' => TRUE,
));
}
}
}
if (isset($aggregate_settings['variables']['advagg_force_https_path']) && $aggregate_settings['variables']['advagg_force_https_path'] || !isset($aggregate_settings['variables']['advagg_force_https_path']) && variable_get('advagg_force_https_path', ADVAGG_FORCE_HTTPS_PATH)) {
$file_uri = advagg_force_https_path($file_uri);
}
return $file_uri;
}
function advagg_load_stylesheet($file, $optimize = FALSE, $reset_basepath = TRUE, $contents = '') {
static $_optimize, $basepath;
if ($reset_basepath) {
$basepath = '';
}
if (isset($optimize)) {
$_optimize = $optimize;
}
if ($basepath && !file_uri_scheme($file)) {
$file = $basepath . '/' . $file;
}
$parent_base_path = $basepath;
$basepath = dirname($file);
$content = '';
if (empty($contents) && !empty($file)) {
$contents = (string) @advagg_file_get_contents($file);
}
if ($contents) {
$content = advagg_load_stylesheet_content($contents, $_optimize);
}
$basepath = $parent_base_path;
if ($_optimize) {
$content = trim($content);
}
return $content;
}
function advagg_get_encoding_from_bom($data) {
static $bom_map = array(
"" => 'UTF-8',
"" => 'UTF-16BE',
"" => 'UTF-16LE',
"" => 'UTF-32BE',
"" => 'UTF-32LE',
"+/v8" => 'UTF-7',
"+/v9" => 'UTF-7',
"+/v+" => 'UTF-7',
"+/v/" => 'UTF-7',
"+/v8-" => 'UTF-7',
);
foreach ($bom_map as $bom => $encoding) {
if (strpos($data, $bom) === 0) {
return $encoding;
}
}
return FALSE;
}
function advagg_convert_to_utf8($data, $encoding) {
if (function_exists('iconv')) {
return @iconv($encoding, 'utf-8', $data);
}
elseif (function_exists('mb_convert_encoding')) {
return @mb_convert_encoding($data, 'utf-8', $encoding);
}
elseif (function_exists('recode_string')) {
return @recode_string($encoding . '..utf-8', $data);
}
return FALSE;
}
function advagg_load_stylesheet_content($contents, $optimize = FALSE) {
$encoding = advagg_get_encoding_from_bom($contents);
if (!empty($encoding)) {
$contents = advagg_convert_to_utf8($contents, $encoding);
}
if ($optimize) {
$comment = '/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/';
$double_quot = '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
$single_quot = "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'";
$contents = preg_replace("<({$double_quot}|{$single_quot})|{$comment}>Ss", "\$1", $contents);
$contents = preg_replace('<
# Do not strip any space from within single or double quotes
(' . $double_quot . '|' . $single_quot . ')
# Strip leading and trailing whitespace.
| \\s*([@{};,])\\s*
# Strip only leading whitespace from:
# - Closing parenthesis: Retain "@media (bar) and foo".
| \\s+([\\)])
# Strip only trailing whitespace from:
# - Opening parenthesis: Retain "@media (bar) and foo".
# - Colon: Retain :pseudo-selectors.
| ([\\(:])\\s+
>xSs', '$1$2$3$4', $contents);
$contents = trim($contents);
$contents .= "\n";
}
$contents = preg_replace('/^@charset\\s+[\'"](\\S*?)\\b[\'"];/i', '', $contents);
$contents = preg_replace_callback('%@import\\s*+(?:url\\(\\s*+)?+[\'"]?+(?![a-z]++:|/)([^\'"()\\s]++)[\'"]?+\\s*+\\)?+\\s*+;%i', '_advagg_load_stylesheet', $contents);
return $contents;
}
function _advagg_load_stylesheet(array $matches) {
$filename = $matches[1];
$file = advagg_load_stylesheet($filename, NULL, FALSE);
if (empty($file)) {
if (strpos($matches[0], 'http://') === 0 || strpos($matches[0], 'https://') === 0 || strpos($matches[0], '//') === 0) {
return $matches[0];
}
if (variable_get('advagg_debug', ADVAGG_DEBUG) >= 2) {
watchdog('advagg-debug', 'Trying to load @file via @import statement but it was not found.', array(
'@file' => $filename,
), WATCHDOG_DEBUG);
}
if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) <= 1) {
return $matches[0];
}
else {
return '';
}
}
$directory = dirname($filename);
$directory = $directory == '.' ? '' : $directory . '/';
return preg_replace('%url\\(\\s*+([\'"]?+)(?![a-z]++:|/)([^\'")]+)([\'"]?+)\\s*\\)%i', 'url(\\1' . $directory . '\\2\\3)', $file);
}
function advagg_aggressive_cache_conflicts() {
$hooks = array(
'css_alter' => TRUE,
'js_alter' => TRUE,
);
foreach ($hooks as $hook => $values) {
$hooks[$hook] = module_implements($hook);
$themes = list_themes();
$theme_keys = array_keys($themes);
if (!empty($theme_keys)) {
foreach ($theme_keys as $theme_key) {
$function = $theme_key . '_' . $hook;
if (function_exists($function)) {
$hooks[$hook][] = $theme_key;
continue;
}
if (empty($themes[$theme_key]->status)) {
continue;
}
$file = dirname($themes[$theme_key]->filename) . '/template.php';
if (file_exists($file)) {
$contents = (string) @advagg_file_get_contents($file);
if (stripos($contents, $function)) {
$hooks[$hook][] = $theme_key;
}
}
}
}
}
$whitelist = array(
'locale',
'simpletest',
'seven',
'at_commerce',
'bluecheese',
'conditional_fields',
'elfinder',
'fences',
'jquery_dollar',
'labjs_js',
'panopoly_core',
'speedy',
'logintoboggan',
);
drupal_alter('advagg_aggressive_cache_conflicts', $whitelist);
$questionable_modules = array();
foreach ($hooks as $hook => $modules) {
foreach ($modules as $key => $module) {
if (strpos($module, 'advagg') === 0 || strpos($module, '_advagg') === 0) {
unset($modules[$key]);
continue;
}
if (in_array($module, $whitelist)) {
unset($modules[$key]);
}
else {
$questionable_modules[$module] = $module;
}
}
}
return $questionable_modules;
}
function advagg_glue_url(array $parsed, $strip_query_and_fragment = FALSE) {
$uri = '';
if (isset($parsed['scheme'])) {
switch (strtolower($parsed['scheme'])) {
case 'mailto':
$uri .= $parsed['scheme'] . ':';
break;
case '//':
$uri .= $parsed['scheme'];
break;
default:
$uri .= $parsed['scheme'] . '://';
}
}
$uri .= isset($parsed['user']) ? $parsed['user'] . (isset($parsed['pass']) ? ':' . $parsed['pass'] : '') . '@' : '';
$uri .= isset($parsed['host']) ? $parsed['host'] : '';
$uri .= !empty($parsed['port']) ? ':' . $parsed['port'] : '';
if (isset($parsed['path'])) {
$uri .= substr($parsed['path'], 0, 1) === '/' ? $parsed['path'] : (!empty($uri) ? '/' : '') . $parsed['path'];
}
if (!$strip_query_and_fragment) {
$uri .= isset($parsed['query']) ? '?' . $parsed['query'] : '';
$uri .= isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';
}
return $uri;
}
function advagg_cache_clear_admin_submit() {
$cache_bins = advagg_flush_caches();
foreach ($cache_bins as $bin) {
cache_clear_all('*', $bin, TRUE);
}
cache_clear_all('hook_info', 'cache_bootstrap');
cache_clear_all('advagg_hooks_implemented:', 'cache_bootstrap', TRUE);
}
function advagg_get_resource_hints_preload_settings($return_defaults = FALSE) {
$sub_defaults = array(
'enabled' => 1,
'push' => 0,
'local' => 1,
'external' => 1,
);
$advagg_resource_hints_preload_settings_defaults = array(
'style' => $sub_defaults + array(
'#weight' => -10,
'title' => t('CSS Files'),
),
'font' => $sub_defaults + array(
'#weight' => -9,
'title' => t('Font Files'),
),
'script' => $sub_defaults + array(
'#weight' => -8,
'title' => t('JS Files'),
),
'svg' => $sub_defaults + array(
'#weight' => -7,
'title' => t('SVG Files'),
),
'image' => $sub_defaults + array(
'#weight' => -6,
'title' => t('Image Files'),
),
'all_others' => $sub_defaults + array(
'#weight' => -5,
'title' => t('All Other Files'),
),
);
if ($return_defaults) {
return $advagg_resource_hints_preload_settings_defaults;
}
$advagg_resource_hints_preload_settings = variable_get('advagg_resource_hints_preload_settings', $advagg_resource_hints_preload_settings_defaults);
foreach ($advagg_resource_hints_preload_settings as $id => &$entry) {
if (isset($advagg_resource_hints_preload_settings_defaults[$id])) {
$entry += $advagg_resource_hints_preload_settings_defaults[$id];
}
ksort($entry);
}
unset($entry);
uasort($advagg_resource_hints_preload_settings, 'element_sort');
return $advagg_resource_hints_preload_settings;
}
function advagg_htaccess_rewritebase($location = DRUPAL_ROOT) {
if (is_readable($location . '/.htaccess')) {
$htaccess = advagg_file_get_contents($location . '/.htaccess');
$matches = array();
$found = preg_match_all('/\\n\\s*RewriteBase\\s.*/i', $htaccess, $matches);
if ($found && !empty($matches[0])) {
$matches[0] = array_map('trim', $matches[0]);
return array_pop($matches[0]);
}
}
return '';
}
function advagg_get_github_version_json(array $library, array $options, $url) {
$http_options = array(
'timeout' => 2,
);
$package = drupal_http_request($url, $http_options);
if (empty($package->data)) {
$package = drupal_http_request($url, array(
'timeout' => 5,
));
}
if (empty($package->data)) {
$url = advagg_force_http_path($url);
$package = drupal_http_request($url, $http_options);
}
if (!empty($package->data)) {
$package = json_decode($package->data);
if (isset($package->version)) {
return (string) $package->version;
}
}
return 0;
}
function advagg_get_github_version_txt(array $library, array $options, $url) {
$http_options = array(
'timeout' => 2,
);
$request = drupal_http_request($url, $http_options);
if (empty($request->data)) {
$request = drupal_http_request($url, array(
'timeout' => 5,
));
}
if (empty($request->data)) {
$url = advagg_force_http_path($url);
$request = drupal_http_request($url, $http_options);
}
if (!empty($request->data)) {
$matches = array();
if (preg_match($options['pattern'], $request->data, $matches)) {
return $matches[1];
}
}
return '0';
}
function advagg_get_remote_libraries_versions($refresh = FALSE) {
$cid = __FUNCTION__;
$versions = array();
if (!$refresh) {
$versions = advagg_get_remote_libraries_versions_cache($cid);
if (!empty($versions)) {
return $versions;
}
}
if (is_callable('libraries_info')) {
$libraries = libraries_info();
foreach ($libraries as $key => $library) {
$libraries_detect = libraries_detect($key);
if (isset($libraries_detect['version'])) {
$versions[$key]['local'] = $libraries_detect['version'];
}
elseif (!empty($libraries_detect['local version'])) {
$versions[$key]['local'] = $libraries_detect['local version'];
}
$remote = advagg_get_remote_libraries_version($key, $library, FALSE);
if (!empty($remote)) {
$versions[$key]['remote'] = $remote;
}
}
}
if (!empty($versions)) {
cache_set($cid, $versions, 'cache_advagg_info');
}
return $versions;
}
function advagg_get_remote_libraries_versions_cache($cid = '') {
if (empty($cid)) {
$cid = 'advagg_get_remote_libraries_versions';
}
$versions =& drupal_static($cid, array());
if (empty($versions)) {
$cache = cache_get($cid, 'cache_advagg_info');
if (!empty($cache) && !empty($cache->data)) {
$versions = $cache->data;
}
}
return $versions;
}
function advagg_get_remote_libraries_version($name, array $library, $use_cache = TRUE) {
if ($use_cache) {
$cid = 'advagg_get_remote_libraries_versions';
$versions = advagg_get_remote_libraries_versions_cache($cid);
if (isset($versions[$name]['remote'])) {
return $versions[$name]['remote'];
}
}
if (empty($library['remote']['url']) && !empty($library['version arguments'])) {
if (!isset($library['version arguments']['file']) && isset($library['version arguments']['variants'])) {
$file = reset($library['version arguments']['variants']);
$library['version arguments']['file'] = $file['file'];
$library['version arguments']['pattern'] = $file['pattern'];
}
if (!empty($library['version arguments']['file'])) {
if (!empty($library['vendor url'])) {
if (strpos($library['vendor url'], 'https://github.com/') === 0) {
$parsed_vendor = @parse_url($library['vendor url']);
$library['remote']['url'] = "https://cdn.jsdelivr.net/gh{$parsed_vendor['path']}@master/{$library['version arguments']['file']}";
}
}
if (empty($library['remote']['url']) && !empty($library['download url'])) {
if (strpos($library['download url'], 'https://github.com/') === 0) {
$parsed_download = @parse_url($library['download url']);
$paths = explode('/', $parsed_download['path']);
$parsed_download['path'] = "/{$paths[1]}/{$paths[2]}";
$library['remote']['url'] = "https://cdn.jsdelivr.net/gh{$parsed_download['path']}@master/{$library['version arguments']['file']}";
}
}
}
}
if (empty($library['remote']['callback']) && isset($library['version arguments']['file'])) {
if (!empty($library['version callback'])) {
$library['remote']['callback'] = $library['version callback'];
}
else {
if ($library['version arguments']['file'] === 'package.json') {
$library['remote']['callback'] = 'advagg_get_github_version_json';
}
else {
$library['remote']['callback'] = 'advagg_get_github_version_txt';
}
}
}
$return = 0;
if (!empty($library['remote']) && !empty($library['remote']['callback']) && !empty($library['remote']['url']) && isset($library['version arguments']) && is_callable($library['remote']['callback']) && variable_get('advagg_remote_version_check', ADVAGG_REMOTE_VERSION_CHECK)) {
$return = $library['remote']['callback']($library, $library['version arguments'], $library['remote']['url']);
if (empty($return) && $library['version arguments']['file'] !== 'package.json') {
$pos = strrpos($library['remote']['url'], $library['version arguments']['file']);
$library['remote']['url'] = substr($library['remote']['url'], 0, $pos) . 'package.json';
$library['remote']['callback'] = 'advagg_get_github_version_json';
$return = $library['remote']['callback']($library, $library['version arguments'], $library['remote']['url']);
}
}
if (empty($return) && !empty($library['version arguments']['default_version'])) {
$return = $library['version arguments']['default_version'];
}
return $return;
}
function advagg_get_library($name, $module_name) {
$library = cache_get($name, 'cache_libraries');
if ($library) {
$library = $library->data;
}
else {
if (is_callable('libraries_detect')) {
$library = libraries_detect($name);
}
elseif (is_callable("{$module_name}_libraries_info")) {
$library = call_user_func("{$module_name}_libraries_info");
$library = $library[$name];
}
}
return $library;
}
function advagg_fix_type(array &$array, $type) {
if (!advagg_enabled()) {
return;
}
if ($type === 'css' && !variable_get('advagg_css_fix_type', ADVAGG_CSS_FIX_TYPE)) {
return;
}
if ($type === 'js' && !variable_get('advagg_js_fix_type', ADVAGG_JS_FIX_TYPE)) {
return;
}
$mod_base_url = substr($GLOBALS['base_root'] . $GLOBALS['base_path'], strpos($GLOBALS['base_root'] . $GLOBALS['base_path'], '//') + 2);
$mod_base_url_len = strlen($mod_base_url);
foreach ($array as &$value) {
if (empty($value['data']) || !is_string($value['data'])) {
continue;
}
if (!isset($value['type'])) {
$value['type'] = 'file';
}
if ($value['type'] === 'inline') {
continue;
}
if ($value['type'] !== 'file' && $value['type'] !== 'inline' && $value['type'] !== 'external' && $value['type'] !== 'setting') {
if ($value['type'] === 'settings') {
$value['type'] = 'setting';
}
else {
$value['type'] = 'file';
}
}
$lower = strtolower($value['data']);
$http_pos = strpos($lower, 'http://');
$https_pos = strpos($lower, 'https://');
$double_slash_pos = strpos($lower, '//');
$tripple_slash_pos = strpos($lower, '///');
$mod_base_url_pos = stripos($value['data'], $mod_base_url);
if ($value['type'] === 'external' && $http_pos !== 0 && $https_pos !== 0 && $double_slash_pos !== 0) {
if (is_readable($value['data'])) {
$value['type'] = 'file';
}
else {
$parsed = parse_url($value['data']);
if (strpos($parsed['path'], $GLOBALS['base_path']) === 0) {
$path = substr($parsed['path'], strlen($GLOBALS['base_path']));
if (is_readable($path)) {
$value['data'] = $path;
$value['type'] = 'file';
}
}
}
}
if ($value['type'] === 'file' && ($http_pos === 0 || $https_pos === 0 || $double_slash_pos === 0 && $tripple_slash_pos === FALSE)) {
$value['type'] = 'external';
}
if ($value['type'] === 'external' && $mod_base_url_pos - 2 === $double_slash_pos && ($http_pos === 0 || $https_pos === 0 || $double_slash_pos === 0)) {
$path = substr($value['data'], $mod_base_url_pos + $mod_base_url_len);
if (is_readable($path)) {
$value['data'] = $path;
$value['type'] = 'file';
$value['group'] = JS_LIBRARY;
$value['every_page'] = TRUE;
$value['weight'] = -40000;
}
else {
$parsed = parse_url($path);
if (strpos($parsed['path'], $GLOBALS['base_path']) === 0) {
$path = substr($parsed['path'], strlen($GLOBALS['base_path']));
if (is_readable($path)) {
$value['data'] = $path;
$value['type'] = 'file';
$value['group'] = JS_LIBRARY;
$value['every_page'] = TRUE;
$value['weight'] = -40000;
}
}
}
}
}
unset($value);
}
function advagg_remove_empty_files(array &$array) {
if (!variable_get('advagg_js_remove_empty_files', ADVAGG_JS_REMOVE_EMPTY_FILES)) {
return;
}
if (variable_get('advagg_fast_filesystem', ADVAGG_FAST_FILESYSTEM)) {
foreach ($array as $key => $value) {
if ($value['type'] !== 'file') {
continue;
}
if (!is_readable($value['data']) || filesize($value['data']) == 0) {
unset($array[$key]);
}
}
}
else {
module_load_include('inc', 'advagg', 'advagg');
$files = array();
foreach ($array as $key => $value) {
if ($value['type'] !== 'file') {
continue;
}
$files[$key] = $value['data'];
}
$info = advagg_get_info_on_files($files);
foreach ($info as $key => $values) {
if (empty($values['filesize'])) {
$key = array_search($values['data'], $files);
if (isset($array[$key])) {
unset($array[$key]);
}
}
}
}
}
function advagg_add_default_dns_lookups(array &$array, $type) {
if (!advagg_enabled()) {
return;
}
if ($type !== 'js') {
return;
}
foreach ($array as &$value) {
if (!is_string($value['data'])) {
continue;
}
if (strpos($value['data'], '/google_service.') !== FALSE) {
if (!empty($value['dns_prefetch']) && is_string($value['dns_prefetch'])) {
$temp = $value['dns_prefetch'];
unset($value['dns_prefetch']);
$value['dns_prefetch'] = array(
$temp,
);
}
$value['dns_prefetch'][] = 'https://csi.gstatic.com';
$value['dns_prefetch'][] = 'https://pubads.g.doubleclick.net';
$value['dns_prefetch'][] = 'https://partner.googleadservices.com';
$value['dns_prefetch'][] = 'https://securepubads.g.doubleclick.net';
$value['dns_prefetch'][] = 'https://pagead2.googlesyndication.com';
$value['dns_prefetch'][] = 'https://cm.g.doubleclick.net';
$value['dns_prefetch'][] = 'https://tpc.googlesyndication.com';
}
if (strpos($value['data'], 'GoogleAnalyticsObject') !== FALSE || strpos($value['data'], '.google-analytics.com/ga.js') !== FALSE) {
if (!empty($value['dns_prefetch']) && is_string($value['dns_prefetch'])) {
$temp = $value['dns_prefetch'];
unset($value['dns_prefetch']);
$value['dns_prefetch'] = array(
$temp,
);
}
if ($GLOBALS['is_https'] && strpos($value['data'], '.google-analytics.com/ga.js') !== FALSE) {
$value['dns_prefetch'][] = 'https://ssl.google-analytics.com';
}
else {
$value['dns_prefetch'][] = 'https://www.google-analytics.com';
}
$value['dns_prefetch'][] = 'https://stats.g.doubleclick.net';
}
}
}
function advagg_insert_into_array_at_location(array $input_array, array $new_value, $location_key) {
return array_merge(array_slice($input_array, 0, $location_key), $new_value, array_slice($input_array, $location_key));
}
function advagg_insert_into_array_at_key(array $input_array, array $insert, $target_key, $location = 1) {
$output = array();
$new_value = reset($insert);
$new_key = key($insert);
foreach ($input_array as $key => $value) {
if ($key === $target_key) {
if ($location == -1) {
$output[$new_key] = $new_value;
$output[$key] = $value;
}
if ($location == 0) {
$output[$new_key] = $new_value;
}
if ($location == 1) {
$output[$key] = $value;
$output[$new_key] = $new_value;
}
}
else {
if (is_numeric($key)) {
while (isset($output[$key])) {
$key++;
}
}
$output[$key] = $value;
}
}
if (!isset($output[$new_key])) {
if ($location == -1) {
$output = $insert + $output;
}
if ($location == 1) {
$output[$new_key] = $new_value;
}
}
return $output;
}
function advagg_url_to_filename($url, $strict = TRUE) {
$decoded1 = _advagg_url_to_filename_filter($url, $strict);
$decoded2 = _advagg_url_to_filename_filter($decoded1, $strict);
while ($decoded1 != $decoded2) {
$decoded1 = _advagg_url_to_filename_filter($decoded2, $strict);
$decoded2 = _advagg_url_to_filename_filter($decoded1, $strict);
}
$filename = $decoded1;
$filename = mb_strcut($filename, 0, 250, mb_detect_encoding($filename));
return $filename;
}
function _advagg_url_to_filename_filter($url, $strict = TRUE) {
$decoded1 = $url;
$decoded2 = rawurldecode($decoded1);
while ($decoded1 != $decoded2) {
$decoded1 = rawurldecode($decoded2);
$decoded2 = rawurldecode($decoded1);
}
$url = $decoded1;
$filename = str_replace(array(
'%20',
'+',
), '-', $url);
if ($strict) {
$filename = preg_replace('~[<>:"/\\|?*]|[\\x00-\\x1F]|[\\x7F\\xA0\\xAD]|[#\\[\\]@!$&\'()+,;=%]|[{}^\\~`]~x', '-', $filename);
}
else {
$filename = preg_replace('~[<>:"\\|?*]|[\\x00-\\x1F]|[\\x7F\\xA0\\xAD]|[#\\[\\]@!$&\'()+,;=%]|[{}^\\~`]~x', '-', $filename);
}
$filename = preg_replace('/[\\r\\n\\t -]+/', '-', $filename);
$filename = ltrim($filename, '.-');
$filename = preg_replace(array(
'/ +/',
'/_+/',
'/-+/',
), '-', $filename);
$filename = preg_replace(array(
'/-*\\.-*/',
'/\\.{2,}/',
), '.', $filename);
$filename = mb_strtolower($filename, 'UTF-8');
$filename = str_replace(array(
'?',
'\\',
'..',
), '', $filename);
$filename = trim($filename, '.-');
return $filename;
}
function advagg_is_external($uri) {
$http_pos = strpos($uri, 'http://');
$https_pos = strpos($uri, 'https://');
$double_slash_pos = strpos($uri, '//');
if ($http_pos !== 0 && $https_pos !== 0 && $double_slash_pos !== 0) {
return FALSE;
}
return TRUE;
}
function advagg_file_get_contents() {
$file_contents = call_user_func_array('file_get_contents', func_get_args());
if ($file_contents === FALSE) {
return $file_contents;
}
$encoding = advagg_get_encoding_from_bom($file_contents);
if (!empty($encoding)) {
$file_contents = advagg_convert_to_utf8($file_contents, $encoding);
}
return $file_contents;
}
function advagg_get_version_description($library_name, $module_name, $only_remote_ok = FALSE) {
$t = get_t();
$versions =& drupal_static(__FUNCTION__);
if (!isset($versions)) {
$versions = advagg_get_remote_libraries_versions(TRUE);
}
$description = '';
if (!empty($versions[$library_name]['remote']) && (empty($versions[$library_name]['local']) || $versions[$library_name]['local'] !== $versions[$library_name]['remote'])) {
$library = advagg_get_library($library_name, $module_name);
if (empty($versions[$library_name]['local'])) {
$versions[$library_name]['local'] = 'NULL';
}
if (!empty($library['installed'])) {
$description = $t('Go to the <a href="@url-page">@name</a> page and <a href="@url-zip">download</a> the latest version (@remote) into the @lib_path folder. An example valid filename is %version_file. Current Version: %version.', array(
'@name' => $library['name'],
'@lib_path' => $library['library path'],
'@url-page' => $library['vendor url'],
'@url-zip' => $library['download url'],
'@remote' => $versions[$library_name]['remote'],
'%version' => $versions[$library_name]['local'],
'%version_file' => $library['library path'] . '/' . $library['version arguments']['file'],
));
}
elseif (!$only_remote_ok && is_callable('libraries_load')) {
$description = $t('Go to the <a href="@url-page">@name</a> page and <a href="@url-zip">download</a> the latest version (@remote) into the libraries folder (usually sites/all/libraries). An example valid filename is %version_file.', array(
'@name' => $library['name'],
'@url-page' => $library['vendor url'],
'@url-zip' => $library['download url'],
'@remote' => $versions[$library_name]['remote'],
'%version_file' => "sites/all/libraries/{$library_name}/{$library['version arguments']['file']}",
));
}
elseif (!$only_remote_ok) {
$description = $t('Install the <a href="@url-lib-api">Libraries API</a> module and then go to the <a href="@url-page">@name</a> page and <a href="@url-zip">download</a> the latest version (@remote) into the libraries folder (usually sites/all/libraries). An example valid filename is %version_file.', array(
'@name' => $library['name'],
'@url-page' => $library['vendor url'],
'@url-zip' => $library['download url'],
'@remote' => $versions[$library_name]['remote'],
'%version_file' => "sites/all/libraries/{$library_name}/{$library['version arguments']['file']}",
'@url-lib-api' => 'https://www.drupal.org/project/libraries',
));
}
}
$path = drupal_get_path('module', $module_name);
$info = drupal_parse_info_file("{$path}/{$module_name}.info");
if (!empty($description) && is_callable('libraries_get_libraries')) {
$libraries_paths = libraries_get_libraries();
if (!empty($libraries_paths["{$library_name}-master"])) {
$description = $t('Rename @lib_dir_master to @lib_dir at this location: @lib_path_master.', array(
'@lib_dir_master' => "{$library_name}-master",
'@lib_path_master' => $libraries_paths["{$library_name}-master"],
'@lib_dir' => $library_name,
));
}
}
return array(
$description,
$info,
);
}
function advagg_generate_advagg_filename_from_db($type) {
$query = db_select('advagg_aggregates_versions', 'aav');
$query
->join('advagg_aggregates', 'aa', 'aa.aggregate_filenames_hash =
aav.aggregate_filenames_hash');
$query
->join('advagg_files', 'af', 'af.filename_hash = aa.filename_hash AND
af.filetype = :type', array(
':type' => $type,
));
$query = $query
->fields('aav', array(
'aggregate_filenames_hash',
'aggregate_contents_hash',
))
->orderBy('atime', 'DESC')
->range(0, 1);
$results = $query
->execute();
if (empty($results)) {
return '';
}
$hooks_hash = advagg_get_current_hooks_hash();
foreach ($results as $row) {
return $type . ADVAGG_SPACE . $row->aggregate_filenames_hash . ADVAGG_SPACE . $row->aggregate_contents_hash . ADVAGG_SPACE . $hooks_hash . '.' . $type;
}
}
function advagg_display_message_if_requirements_not_met(array $requirements = array()) {
include_once DRUPAL_ROOT . '/includes/install.inc';
module_load_include('install', 'advagg');
$requirements += advagg_install_fast_checks();
if (!empty($requirements)) {
module_load_include('admin.inc', 'system');
usort($requirements, '_system_sort_requirements');
$report = theme('status_report', array(
'requirements' => $requirements,
));
drupal_set_message(t('Go to the <a href="@url">status report page</a> and fix the issues that AdvAgg lists there. Sneak peak: !report', array(
'@url' => url('admin/reports/status'),
'!report' => $report,
)));
}
}
function advagg_add_preload_header($url = '', $as = '') {
$list =& drupal_static(__FUNCTION__ . ':list', array());
$output =& drupal_static(__FUNCTION__ . ':output');
$header_strlen =& drupal_static(__FUNCTION__ . ':strlen', 0);
static $resource_hints_preload_order;
if (!isset($resource_hints_preload_order)) {
$resource_hints_preload_order = advagg_get_resource_hints_preload_settings();
}
if (!isset($output)) {
$keys = array_keys($resource_hints_preload_order);
$output = array_fill_keys($keys, array());
}
if (empty($url)) {
drupal_alter('advagg_preload_header', $output);
$header_value = '';
foreach ($output as $value) {
if (!empty($value)) {
$value = array_filter($value);
foreach ($value as $string) {
$header_strlen += strlen($string) + 2;
if ($header_strlen >= variable_get('advagg_resource_hints_preload_max_size', ADVAGG_RESOURCE_HINTS_PRELOAD_MAX_SIZE)) {
continue;
}
if (empty($header_value)) {
$header_value = $string;
}
else {
$header_value .= ',' . $string;
}
}
}
}
if (!empty($header_value)) {
drupal_add_http_header('Link', $header_value, TRUE);
}
return FALSE;
}
if (isset($list[$url])) {
return FALSE;
}
$payload = "<{$url}>; rel=preload";
list($as, $type, $crossorigin, $parse) = advagg_get_preload_info_from_url($url, $as);
if (!empty($as) && $as === 'php') {
$list[$url] = FALSE;
return FALSE;
}
$additional_info = array();
if (!empty($crossorigin)) {
$additional_info[] = "crossorigin";
}
if (!empty($type)) {
if ($type !== 'text/css' || $type !== 'text/javascript' || $as !== 'image') {
$additional_info[] = "type=\"{$type}\"";
}
}
$additional_info = implode('; ', $additional_info);
if (!empty($as) && !empty($resource_hints_preload_order[$as])) {
$settings = $resource_hints_preload_order[$as];
}
elseif (!empty($resource_hints_preload_order['all_others'])) {
$settings = $resource_hints_preload_order['all_others'];
}
if (!$settings['enabled']) {
$list[$url] = FALSE;
return FALSE;
}
if (!$settings['local'] && empty($parse['host'])) {
$list[$url] = FALSE;
return FALSE;
}
if (!$settings['external'] && !empty($parse['host'])) {
$list[$url] = FALSE;
return FALSE;
}
if (!empty($as)) {
$payload .= "; as={$as}";
}
if (!empty($additional_info)) {
$payload .= "; {$additional_info}";
}
if (!$settings['push']) {
$payload .= "; nopush";
}
$list[$url] = TRUE;
$output[$as][] = $payload;
}
function advagg_get_preload_info_from_url($url, $as = '', $type = '', $crossorigin = NULL) {
$parse = @parse_url($url);
if (empty($parse['path'])) {
return FALSE;
}
$file_ext = strtolower(pathinfo($parse['path'], PATHINFO_EXTENSION));
if (empty($file_ext)) {
$file_ext = basename($parse['path']);
}
$list = advagg_preload_list();
if (empty($as) && !empty($file_ext)) {
foreach ($list as $as_key => $list_type) {
$key = array_search($file_ext, $list_type);
if ($key !== FALSE) {
$as = $as_key;
if ($as === 'font' && $file_ext === 'svg' && stripos($url, 'font') === FALSE) {
$as = '';
}
}
if (!empty($as)) {
break;
}
}
}
if ($file_ext !== 'css' && empty($type) && !empty($as)) {
$type = "{$as}/{$file_ext}";
if ($file_ext === 'svg') {
$type .= '+xml';
}
if ($file_ext === 'js') {
$type = 'text/javascript';
}
if ($file_ext === 'css') {
$type = 'text/css';
}
}
if ($as === 'font' && is_null($crossorigin)) {
$crossorigin = 'anonymous';
}
return array(
$as,
$type,
$crossorigin,
$parse,
);
}
function advagg_add_preload_link($url, $media = '', $as = '', $type = '', $crossorigin = NULL) {
static $weight = -2000;
$weight += 0.0001;
$href = advagg_file_create_url($url);
$key = "advagg_preload:{$href}";
$stored_head = drupal_static('drupal_add_html_head');
if (isset($stored_head[$key])) {
return TRUE;
}
$attributes = array(
'rel' => 'preload',
'href' => $href,
);
list($as, $type, $crossorigin) = advagg_get_preload_info_from_url($url, $as, $type, $crossorigin);
if (empty($as)) {
return FALSE;
}
$attributes['as'] = $as;
if (!empty($type)) {
$attributes['type'] = $type;
}
if (!empty($crossorigin)) {
$attributes['crossorigin'] = $crossorigin;
}
if (!empty($media)) {
$attributes['media'] = $media;
}
drupal_alter('advagg_preload_link_attributes', $attributes);
$element = array(
'#type' => 'html_tag',
'#tag' => 'link',
'#attributes' => $attributes,
'#weight' => $weight,
);
drupal_add_html_head($element, $key);
return TRUE;
}
function advagg_preload_list() {
$list = array(
'font' => array(
'woff2',
'woff',
'ttf',
'otf',
'eot',
'svg',
),
'image' => array(
'gif',
'jpg',
'jpeg',
'jpe',
'jif',
'jfif',
'jfi',
'png',
'webp',
'jp2',
'jpx',
'jxr',
'heif',
'heic',
'bpg',
'svg',
),
'style' => array(
'css',
),
'script' => array(
'js',
),
'video' => array(
'mp4',
'webm',
'ogg',
),
);
drupal_alter('advagg_preload_list', $list);
return $list;
}
function advagg_find_all_recommended_admin_values(array &$element, $key_name = '#recommended_value') {
$results = array();
$children = element_children($element);
foreach ($children as $key) {
$child = $element[$key];
if (is_array($child)) {
if (!empty($child['#type']) && !empty($child['#name']) && isset($child[$key_name])) {
$results[$child['#name']] = $child[$key_name];
}
$results = array_merge($results, advagg_find_all_recommended_admin_values($child, $key_name));
}
unset($child);
}
return $results;
}
function advagg_find_all_changed_admin_values(array &$element) {
$results = array();
$children = element_children($element);
foreach ($children as $key) {
$child = $element[$key];
if (is_array($child)) {
if (!empty($child['#type']) && !empty($child['#name']) && isset($child['#default_value']) && isset($child['#value'])) {
if ($child['#type'] === 'checkboxes') {
$child['#value'] += array_diff_assoc($child['#default_value'], $child['#value']);
}
if ($child['#default_value'] != $child['#value']) {
$results[$child['#name']] = array(
$child['#value'],
$child['#default_value'],
);
}
}
$results = array_merge($results, advagg_find_all_changed_admin_values($child));
}
unset($child);
}
return $results;
}
function advagg_find_title(array &$element) {
$results = array();
$children = element_children($element);
foreach ($children as $key) {
$child = $element[$key];
if (is_array($child)) {
if (!empty($child['#type']) && !empty($child['#name']) && isset($child['#title']) && isset($child['#default_value']) && !isset($results[$child['#name']]) && $child['#type'] !== 'radio') {
$results[$child['#name']] = $child['#title'];
}
$results = array_merge($results, advagg_find_title($child));
}
unset($child);
}
return $results;
}
function advagg_set_admin_form_defaults_recommended(array &$form_state, $trigger_key) {
$changed = array();
$recommended_values = array();
if ($form_state['values'][$trigger_key] == 2) {
$recommended_values = advagg_find_all_recommended_admin_values($form_state['complete form']);
foreach ($recommended_values as $key => $value) {
if (!isset($form_state['values'][$key])) {
$changed[$key] = array(
$value,
);
}
elseif ($value != $form_state['values'][$key]) {
$changed[$key] = array(
$value,
$form_state['values'][$key],
);
}
$form_state['values'][$key] = $value;
}
}
if ($form_state['values'][$trigger_key] == 0 || $form_state['values'][$trigger_key] == 2) {
foreach ($form_state['values'] as $key => &$value) {
if (strpos($key, 'advagg_') !== 0 || $key === $trigger_key || isset($changed[$key]) || isset($recommended_values[$key])) {
continue;
}
$default = FALSE;
if (defined(strtoupper($key))) {
$default = constant(strtoupper($key));
}
if ($key === 'advagg_resource_hints_preload_settings') {
$default = advagg_get_resource_hints_preload_settings(TRUE);
foreach ($default as $key => &$values) {
$default[$key]['weight'] = $values['#weight'];
unset($default[$key]['#weight'], $values['#weight'], $default[$key]['title'], $values['title']);
ksort($values);
}
ksort($default);
foreach ($value as $key => &$values) {
ksort($values);
}
ksort($value);
}
if ($key === 'advagg_relocate_css_inline_import_browsers') {
$default = array(
'woff2' => 'woff2',
'woff' => 'woff',
'ttf' => 'ttf',
'eot' => 0,
'svg' => 0,
);
}
if ($default != $value) {
$changed[$key] = array(
$default,
$value,
);
$value = $default;
}
}
}
if ($form_state['values'][$trigger_key] == 4) {
$changed = advagg_find_all_changed_admin_values($form_state['complete form']);
if (isset($changed[$trigger_key])) {
unset($changed[$trigger_key]);
}
}
$all_titles_descriptions = advagg_find_title($form_state['complete form']);
foreach ($changed as $key => $values) {
if (isset($values[1])) {
if ($values[0] == $values[1]) {
unset($changed[$key]);
continue;
}
if (is_string($values[0]) && is_string($values[1]) && trim($values[0]) == trim($values[1])) {
unset($changed[$key]);
continue;
}
}
if (!isset($values[1])) {
$values[1] = NULL;
}
if (is_bool($values[0]) && !is_bool($values[1]) || !is_bool($values[0]) && is_bool($values[1])) {
$values[0] = (bool) $values[0];
$values[1] = (bool) $values[1];
}
if (is_int($values[0]) && !is_int($values[1]) || !is_int($values[0]) && is_int($values[1])) {
$values[0] = (int) $values[0];
$values[1] = (int) $values[1];
}
if (empty($all_titles_descriptions[$key])) {
drupal_set_message(t('%before -> %after for %title', array(
'%title' => $key,
'%before' => var_export($values[1], TRUE),
'%after' => var_export($values[0], TRUE),
)));
}
else {
drupal_set_message(t('%before -> %after for %title', array(
'%title' => $all_titles_descriptions[$key],
'%before' => var_export($values[1], TRUE),
'%after' => var_export($values[0], TRUE),
)));
}
}
return $changed;
}
function advagg_diff_multi(array $defaults, $new_values) {
$result = array();
foreach ($defaults as $key => $val) {
if (is_array($val) && isset($new_values[$key])) {
$tmp = advagg_diff_multi($val, $new_values[$key]);
if ($tmp) {
$result[$key] = $tmp;
}
}
elseif (!isset($new_values[$key])) {
$result[$key] = NULL;
}
elseif ($val != $new_values[$key]) {
$result[$key] = $new_values[$key];
}
if (isset($new_values[$key])) {
unset($new_values[$key]);
}
}
$result = $result + $new_values;
return $result;
}