<?php

/**
 * @file
 * Advanced aggregation modifier module.
 */

// Define default variables.
/**
 * Default value to move all JS to the footer.
 */
define('ADVAGG_MOD_JS_FOOTER', 0);

/**
 * Default value to turn on preprocessing for all JavaScript files.
 */
define('ADVAGG_MOD_JS_PREPROCESS', FALSE);

/**
 * Default value to add the defer tag to all script tags.
 */
define('ADVAGG_MOD_JS_DEFER', FALSE);

/**
 * Default value to add use the async script shim for script tags.
 */
define('ADVAGG_MOD_JS_ASYNC_SHIM', FALSE);

/**
 * Default value to remove JavaScript if none was added on the page.
 */
define('ADVAGG_MOD_JS_REMOVE_UNUSED', FALSE);

/**
 * Default value to turn on preprocessing for all CSS files.
 */
define('ADVAGG_MOD_CSS_PREPROCESS', FALSE);

/**
 * Default value to translate the content attributes of CSS files.
 */
define('ADVAGG_MOD_CSS_TRANSLATE', FALSE);

/**
 * Default value to adjust the sorting of external JavaScript.
 */
define('ADVAGG_MOD_JS_ADJUST_SORT_EXTERNAL', FALSE);

/**
 * Default value to adjust the sorting of inline JavaScript.
 */
define('ADVAGG_MOD_JS_ADJUST_SORT_INLINE', FALSE);

/**
 * Default value to adjust the sorting of browser conditional JavaScript.
 */
define('ADVAGG_MOD_JS_ADJUST_SORT_BROWSERS', FALSE);

/**
 * Default value to adjust the sorting of external CSS.
 */
define('ADVAGG_MOD_CSS_ADJUST_SORT_EXTERNAL', FALSE);

/**
 * Default value to adjust the sorting of inline CSS.
 */
define('ADVAGG_MOD_CSS_ADJUST_SORT_INLINE', FALSE);

/**
 * Default value to adjust the sorting of browser conditional CSS.
 */
define('ADVAGG_MOD_CSS_ADJUST_SORT_BROWSERS', FALSE);

/**
 * Default value to use JavaScript to defer CSS loading.
 */
define('ADVAGG_MOD_CSS_DEFER', FALSE);

/**
 * Default value to move CSS into drupal_add_css().
 */
define('ADVAGG_MOD_CSS_HEAD_EXTRACT', FALSE);

/**
 * Default value to move JavaScript into drupal_add_js().
 */
define('ADVAGG_MOD_JS_HEAD_EXTRACT', FALSE);

/**
 * Default value to have async on all JS script tags.
 */
define('ADVAGG_MOD_JS_ASYNC', FALSE);

/**
 * Default value to wrap inline content javascript so it runs when it is ready.
 */
define('ADVAGG_MOD_JS_FOOTER_INLINE_ALTER', TRUE);

/**
 * Turns on functionality on every page except the listed pages.
 */
define('ADVAGG_MOD_VISIBILITY_NOTLISTED', 0);

/**
 * Turns on functionality only on the listed pages.
 */
define('ADVAGG_MOD_VISIBILITY_LISTED', 1);

/**
 * Turns on functionality if the associated PHP code returns TRUE.
 */
define('ADVAGG_MOD_VISIBILITY_PHP', 2);

/**
 * Default value of the inclusion method for the loadCSS code.
 */
define('ADVAGG_MOD_CSS_DEFER_JS_CODE', 0);

/**
 * Default value to convert inline GA code into file.
 */
define('ADVAGG_MOD_GA_INLINE_TO_FILE', FALSE);

/**
 * Default value for inline scripts that should not be altered.
 */
define('ADVAGG_MOD_WRAP_INLINE_JS_SKIP_LIST', '');

/**
 * Default value for detection of inline scripts.
 */
define('ADVAGG_MOD_WRAP_INLINE_JS_XPATH', FALSE);

/**
 * Default value to wrap inline content javascript so it runs defered.
 */
define('ADVAGG_MOD_JS_DEFER_INLINE_ALTER', FALSE);

/**
 * Default value for inline scripts that should not be deferred.
 */
define('ADVAGG_MOD_DEFER_INLINE_JS_SKIP_LIST', '');

/**
 * Default value to move async js to the header.
 */
define('ADVAGG_MOD_JS_ASYNC_IN_HEADER', FALSE);

/**
 * Default value to remove ajaxPageState if ajax.js is not used.
 */
define('ADVAGG_MOD_JS_NO_AJAXPAGESTATE', FALSE);

/**
 * Default value to scan html for src tags and do a DNS prefetch on it.
 */
define('ADVAGG_MOD_JS_GET_EXTERNAL_DNS', FALSE);

/**
 * Default value to defer jquery.
 */
define('ADVAGG_MOD_JS_DEFER_JQUERY', FALSE);

/**
 * Default value to use the prefetch tag for certain domains.
 */
define('ADVAGG_MOD_PREFETCH', FALSE);

// Core hook implementations.
/**
 * Implements hook_module_implements_alter().
 */
function advagg_mod_module_implements_alter(&$implementations, $hook) {
  // Move advagg_mod to the top.
  if ($hook === 'library_alter' && array_key_exists('advagg_mod', $implementations)) {
    $item = array('advagg_mod' => $implementations['advagg_mod']);
    unset($implementations['advagg_mod']);
    $implementations = array_merge($item, $implementations);
  }
}

/**
 * Implements hook_library_alter().
 */
function advagg_mod_library_alter(&$javascript, $module) {
  if (!module_exists('jquery_update')) {
    return;
  }
  if (!advagg_mod_inline_page_js()) {
    return;
  }
  // Set the CDN to none for this page as everything is going to inlined.
  $GLOBALS['conf']['jquery_update_jquery_cdn'] = 'none';
  $GLOBALS['conf']['jquery_update_jquery_migrate_cdn'] = 'none';
}

/**
 * Implements hook_init().
 */
function advagg_mod_init() {
  // Return if unified_multisite_dir is not set.
  $dir = rtrim(variable_get('advagg_mod_unified_multisite_dir', ''), '/');
  if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
    return;
  }

  $counter_filename = $dir . '/' . ADVAGG_SPACE . 'advagg_global_counter';
  $local_counter = advagg_get_global_counter();
  if (!file_exists($counter_filename)) {
    module_load_include('inc', 'advagg', 'advagg.missing');
    advagg_save_data($counter_filename, $local_counter);
  }
  else {
    $shared_counter = (int) file_get_contents($counter_filename);

    if ($shared_counter == $local_counter) {
      // Counters are the same, return.
      return;
    }
    elseif ($shared_counter < $local_counter) {
      // Local counter is higher, update saved file and return.
      module_load_include('inc', 'advagg', 'advagg.missing');
      advagg_save_data($counter_filename, $local_counter, TRUE);
      return;
    }
    elseif ($shared_counter > $local_counter) {
      // Shared counter is higher, update local copy and return.
      variable_set('advagg_global_counter', $shared_counter);
      return;
    }
  }
}

/**
 * Implements hook_menu().
 */
function advagg_mod_menu() {
  $file_path = drupal_get_path('module', 'advagg_mod');
  $config_path = advagg_admin_config_root_path();

  $items[$config_path . '/advagg/mod'] = array(
    'title' => 'Modifications',
    'description' => 'Turn on or off various mods for CSS/JS.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('advagg_mod_admin_settings_form'),
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array('administer site configuration'),
    'file path' => $file_path,
    'file' => 'advagg_mod.admin.inc',
    'weight' => 10,
  );

  return $items;
}

/**
 * Implements hook_js_alter().
 */
function advagg_mod_js_alter(&$js) {
  if (module_exists('advagg') && !advagg_enabled()) {
    return;
  }

  // Change google analytics inline loader to be inside of an aggregrated file.
  if (variable_get('advagg_mod_ga_inline_to_file', ADVAGG_MOD_GA_INLINE_TO_FILE)) {
    advagg_mod_ga_inline_to_file($js);
  }

  // Only add JS if it's actually needed.
  if (variable_get('advagg_mod_js_remove_unused', ADVAGG_MOD_JS_REMOVE_UNUSED)) {
    advagg_remove_js_if_not_used($js);
  }

  // Change sort order so aggregates do not get split up.
  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
  if ( variable_get('advagg_mod_js_adjust_sort_external', ADVAGG_MOD_JS_ADJUST_SORT_EXTERNAL)
    || variable_get('advagg_mod_js_adjust_sort_inline', ADVAGG_MOD_JS_ADJUST_SORT_INLINE)
    || variable_get('advagg_mod_js_adjust_sort_browsers', ADVAGG_MOD_JS_ADJUST_SORT_BROWSERS)
  ) {
    advagg_mod_sort_css_js($js, 'js');
  }

  // Move JS to the footer.
  advagg_mod_js_move_to_footer($js);

  // Force all JS to be preprocessed.
  if (variable_get('advagg_mod_js_preprocess', ADVAGG_MOD_JS_PREPROCESS)) {
    foreach ($js as &$values) {
      $values['preprocess'] = TRUE;
      $values['cache'] = TRUE;
    }
    unset($values);
  }

  // Add the defer or the async tag to JS.
  $jquery_defered = advagg_mod_js_async_defer($js);
  // Inline JS defer.
  advagg_mod_inline_defer($js, $jquery_defered);

  // Move all async JS to the header.
  if (variable_get('advagg_mod_js_async_in_header', ADVAGG_MOD_JS_ASYNC_IN_HEADER)) {
    foreach ($js as &$values) {
      // Skip if not file or external.
      if ($values['type'] !== 'file' && $values['type'] !== 'external') {
        continue;
      }
      // Skip if not async.
      if (empty($values['async']) && empty($values['attributes']['async'])) {
        continue;
      }

      // Move to the header with a group of 1000.
      $values['scope'] = 'header';
      $values['group'] = 1000;
    }
    unset($values);
  }

  advagg_mod_prefetch_link($js);
}

/**
 * Have the browser prefech this domain to open the connection.
 *
 * @param array $js
 *   JS array.
 */
function advagg_mod_prefetch_link(array &$js) {
  if (variable_get('advagg_mod_prefetch', ADVAGG_MOD_PREFETCH)) {
    foreach ($js as &$values) {
      if (isset($values['dns_prefetch'])) {
        foreach ($values['dns_prefetch'] as &$url) {
          // Prefetch stats.g.doubleclick.net domain.
          if (strpos($url, '//stats.g.doubleclick.net') !== FALSE) {
            $url .= '#prefetch';
          }
        }
      }
    }
  }
}

/**
 * Implements hook_element_info_alter().
 */
function advagg_mod_element_info_alter(&$type) {
  if (!isset($type['styles']['#pre_render'])) {
    $type['styles']['#pre_render'] = array();
  }
  $key_drupal = array_search('drupal_pre_render_styles', $type['styles']['#pre_render']);
  $key_advagg = array_search('advagg_pre_render_styles', $type['styles']['#pre_render']);
  if ($key_drupal !== FALSE) {
    $type['styles']['#pre_render'] = advagg_insert_into_array_at_location($type['styles']['#pre_render'], array('_advagg_mod_pre_render_styles'), $key_drupal);
  }
  elseif ($key_advagg !== FALSE) {
    $type['styles']['#pre_render'] = advagg_insert_into_array_at_location($type['styles']['#pre_render'], array('_advagg_mod_pre_render_styles'), $key_advagg);
  }
  else {
    $type['styles']['#pre_render'][] = '_advagg_mod_pre_render_styles';
  }

  if (!isset($type['scripts']['#pre_render'])) {
    $type['scripts']['#pre_render'] = array();
  }
  $key_drupal = array_search('drupal_pre_render_scripts', $type['scripts']['#pre_render']);
  $key_advagg = array_search('advagg_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'] = advagg_insert_into_array_at_location($type['scripts']['#pre_render'], array('_advagg_mod_pre_render_scripts'), $key_drupal);
  }
  elseif ($key_advagg !== FALSE) {
    $type['scripts']['#pre_render'] = advagg_insert_into_array_at_location($type['scripts']['#pre_render'], array('_advagg_mod_pre_render_scripts'), $key_advagg);
  }
  elseif ($key_omega !== FALSE) {
    $type['scripts']['#pre_render'] = advagg_insert_into_array_at_location($type['scripts']['#pre_render'], array('_advagg_mod_pre_render_scripts'), $key_omega);
  }
  elseif ($key_aurora !== FALSE) {
    $type['scripts']['#pre_render'] = advagg_insert_into_array_at_location($type['scripts']['#pre_render'], array('_advagg_mod_pre_render_scripts'), $key_aurora);
  }
  else {
    $type['scripts']['#pre_render'][] = '_advagg_mod_pre_render_scripts';
  }
}


/**
 * Implements hook_css_alter().
 */
function advagg_mod_css_alter(&$css) {
  if (module_exists('advagg') && !advagg_enabled()) {
    return;
  }

  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace:5
  // Change sort order so aggregates do not get split up.
  if ( variable_get('advagg_mod_css_adjust_sort_external', ADVAGG_MOD_CSS_ADJUST_SORT_EXTERNAL)
    || variable_get('advagg_mod_css_adjust_sort_inline', ADVAGG_MOD_CSS_ADJUST_SORT_INLINE)
    || variable_get('advagg_mod_css_adjust_sort_browsers', ADVAGG_MOD_CSS_ADJUST_SORT_BROWSERS)
  ) {
    advagg_mod_sort_css_js($css, 'css');
  }

  // Force all CSS to be preprocessed.
  if (variable_get('advagg_mod_css_preprocess', ADVAGG_MOD_CSS_PREPROCESS)) {
    foreach ($css as &$values) {
      $values['preprocess'] = TRUE;
    }
    unset($values);
  }
}

/**
 * Implements hook_html_head_alter().
 */
function advagg_mod_html_head_alter(&$head_elements) {
  foreach ($head_elements as $key => $element) {
    // CSS.
    // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
    if ( variable_get('advagg_mod_css_head_extract', ADVAGG_MOD_CSS_HEAD_EXTRACT)
      && !empty($element['#tag'])
      && $element['#tag'] === 'link'
      && !empty($element['#attributes']['type'])
      && $element['#attributes']['type'] === 'text/css'
      && !empty($element['#attributes']['href'])
    ) {
      $type = 'file';
      // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
      if ( strpos($element['#attributes']['href'], 'http://') === 0
        || strpos($element['#attributes']['href'], 'https://') === 0
        || strpos($element['#attributes']['href'], '//') === 0
      ) {
        $type = 'external';
      }
      drupal_add_css($element['#attributes']['href'], array(
        'type' => $type,
        'group' => CSS_SYSTEM,
        'every_page' => TRUE,
        'weight' => -50000,
      ));
      unset($head_elements[$key]);
    }
    // JS.
    // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
    if ( variable_get('advagg_mod_js_head_extract', ADVAGG_MOD_JS_HEAD_EXTRACT)
      && !empty($element['#tag'])
      && $element['#tag'] === 'script'
      && !empty($element['#attributes']['type'])
      && $element['#attributes']['type'] === 'text/javascript'
      && !empty($element['#attributes']['src'])
    ) {
      $type = 'file';
      // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
      if ( strpos($element['#attributes']['src'], 'http://') === 0
        || strpos($element['#attributes']['src'], 'https://') === 0
        || strpos($element['#attributes']['src'], '//') === 0
      ) {
        $type = 'external';
      }
      drupal_add_js($element['#attributes']['src'], array(
        'type' => $type,
        'scope' => 'header',
        'group' => JS_LIBRARY,
        'every_page' => TRUE,
        'weight' => -50000,
      ));
      unset($head_elements[$key]);
    }
  }
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Insert advagg_mod_process_move_js before _advagg_process_html.
 */
function advagg_mod_theme_registry_alter(&$theme_registry) {
  if (!isset($theme_registry['html'])) {
    return;
  }

  // Find template_process_html/_advagg_process_html.
  $index = array_search('_advagg_process_html', $theme_registry['html']['process functions']);
  if ($index === FALSE) {
    $index = array_search('template_process_html', $theme_registry['html']['process functions']);
    if ($index === FALSE) {
      return;
    }
  }

  // Insert advagg_mod_process_move_js before _advagg_process_html.
  array_splice($theme_registry['html']['process functions'], $index, 0, 'advagg_mod_process_move_js');
}

/**
 * Implements hook_process().
 *
 * Used to wrap inline JS in a function in order to prevent js errors when JS is
 * moved to the footer.
 */
function advagg_mod_process_move_js(array &$variables) {
  // Return if settings are disabled.
  if ( !variable_get('advagg_mod_js_footer_inline_alter', ADVAGG_MOD_JS_FOOTER_INLINE_ALTER)
    && !variable_get('advagg_mod_js_get_external_dns', ADVAGG_MOD_JS_GET_EXTERNAL_DNS)
  ) {
    return;
  }

  // Search all the children for script tags.
  foreach (element_children($variables) as $child) {
    // Skip if empty.
    if (empty($variables[$child])) {
      continue;
    }

    // Handle strings.
    if ( is_string($variables[$child])
      && stripos($variables[$child], '<script') !== FALSE
    ) {
      advagg_mod_js_inline_processor($variables[$child]);
    }
    if (is_array($variables[$child])) {
      if ( isset($variables[$child]['#children'])
        && is_string($variables[$child]['#children'])
        && stripos($variables[$child]['#children'], '<script') !== FALSE
      ) {
        advagg_mod_js_inline_processor($variables[$child]['#children']);
      }
      if ( isset($variables[$child]['#markup'])
        && is_string($variables[$child]['#markup'])
        && stripos($variables[$child]['#markup'], '<script') !== FALSE
      ) {
        // advagg_mod_js_inline_processor($variables[$child]['#markup']);
        // Uncomment to also process #markup.
      }
      // advagg_mod_process_move_js($variables[$child]);
      // Uncomment to make this recursive.
    }
  }
}

// AdvAgg hook implementations.
/**
 * Implements hook_advagg_modify_js_pre_render_alter().
 */
function advagg_mod_advagg_modify_js_pre_render_alter(&$children, &$elements) {
  if (module_exists('advagg') && !advagg_enabled()) {
    return;
  }

  // Do not use defer/async shim if JS is inlined.
  if (advagg_mod_inline_page() || advagg_mod_inline_page_js()) {
    return;
  }

  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
  if ( variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)
    || variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC)
  ) {
    // Capture all onload code.
    $onload_code = array();
    foreach ($children as $values) {
      if (isset($values['#attributes']['onload'])) {
        $onload_code[$values['#attributes']['src']] = $values['#attributes']['onload'];
      }
    }
    $jquery_rev = strrev('/jquery.js');
    $jquery_min_rev = strrev('/jquery.min.js');

    $ie_fixes = array();
    foreach ($elements['#groups'] as $group) {
      if ( $group['type'] !== 'file'
        || empty($group['defer'])
        || empty($group['items']['files'])
      ) {
        continue;
      }

      $found = FALSE;
      foreach ($group['items']['files'] as $name => &$values) {
        // Special handling for jQuery.
        if ( stripos(strrev($name), $jquery_rev) === 0
          || stripos(strrev($name), $jquery_min_rev) === 0
        ) {
          $found = TRUE;
        }
      }
      if ($found) {
        $ie_fixes[] = basename($group['data']);
      }
    }

    foreach ($children as $key => &$values) {
      if ( !empty($values['#attributes']['src'])
        && isset($values['#attributes']['defer'])
        && empty($values['#browsers'])
      ) {
        $ie_key = array_search(basename($values['#attributes']['src']), $ie_fixes);
        if ($ie_key !== FALSE) {
          unset($ie_fixes[$ie_key]);
          // Not IE supports defer.
          $values['#browsers'] = array(
            'IE' => FALSE,
            '!IE' => TRUE,
          );

          // IE10+ supports defer.
          $copy = $values;
          $copy['#browsers'] = array(
            'IE' => 'gt IE 9',
            '!IE' => FALSE,
          );
          $copy['#attributes']['src'] .= '#ie10+';
          array_splice($children, $key, 0, array($copy));

          // IE9- does not support defer.
          $copy = $values;
          $copy['#browsers'] = array(
            'IE' => 'lte IE 9',
            '!IE' => FALSE,
          );
          unset($copy['defer']);
          unset($copy['#attributes']['defer']);
          $copy['#attributes']['src'] .= '#ie9-';
          array_splice($children, $key, 0, array($copy));
        }
      }
    }

    foreach ($children as $key => &$values) {
      // Core's Drupal.settings. Put inside wrapper if there is an onload call
      // for init_drupal_core_settings. Have to do this here because the
      // settings needed to be rendered.
      if (!empty($values['#value']) && strpos($values['#value'], 'jQuery.extend(Drupal.settings') !== FALSE) {
        $found = FALSE;
        foreach ($onload_code as $src => $code) {
          if (strpos($code, 'init_drupal_core_settings(') !== FALSE) {
            $found = TRUE;
            unset($onload_code[$src]);
            break;
          }
        }
        if ($found) {
          $values['#value'] = "function init_drupal_core_settings() {" . $values['#value'] . "\nif(jQuery.isFunction(jQuery.holdReady)){jQuery.holdReady(false);}} if(window.jQuery && window.Drupal){init_drupal_core_settings();}";
        }
      }
    }
    unset($values);
  }

  if (variable_get('advagg_mod_js_async_shim', ADVAGG_MOD_JS_ASYNC_SHIM)) {
    foreach ($children as &$values) {
      if (isset($values['#attributes']) && isset($values['#attributes']['async']) && $values['#attributes']['async'] === 'async' && !empty($values['#attributes']['src'])) {
        $source = $values['#attributes']['src'];
        // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
        if ( strpos($source, 'http://') !== 0
          && strpos($source, 'https://') !== 0
          && strpos($source, '//') !== 0
        ) {
          $source = url($values['#attributes']['src']);
        }
        $values['#value'] = "(function() {
  var s = document.createElement('script');
  s.type = 'text/javascript';
  s.async = true;
  s.src = '$source';
  var d = document.getElementsByTagName('script')[0];
  d.parentNode.insertBefore(s, d);
})();";
        unset($values['#attributes']['async']);
        unset($values['#attributes']['src']);
      }
    }
    unset($values);
  }
}

/**
 * Implements hook_advagg_modify_css_pre_render_alter().
 */
function advagg_mod_advagg_modify_css_pre_render_alter(&$children, &$elements) {
  if (module_exists('advagg') && !advagg_enabled()) {
    return;
  }

  // Return early if this setting is disabled.
  $css_defer = variable_get('advagg_mod_css_defer', ADVAGG_MOD_CSS_DEFER);
  if (empty($css_defer)) {
    return;
  }
  $css_defer_js_code = variable_get('advagg_mod_css_defer_js_code', ADVAGG_MOD_CSS_DEFER_JS_CODE);

  // Make advagg_mod_loadStyleSheet() available.
  $type = 'external';
  // https://github.com/filamentgroup/loadCSS.
  $data = '//cdn.rawgit.com/filamentgroup/loadCSS/master/loadCSS.js';
  $min = '';
  if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 0) {
    $min = '.min';
  }
  if ($css_defer_js_code == 2) {
    $type = 'file';
    $data = drupal_get_path('module', 'advagg_mod') . "/loadCSS$min.js";
  }
  if ($css_defer_js_code == 0) {
    $type = 'inline';
    $data = '//[c]2014 @scottjehl, Filament Group, Inc. Licensed MIT.
function loadCSS(a,b,c,d){"use strict";var e=window.document.createElement("link"),f=b||window.document.getElementsByTagName("script")[0],g=window.document.styleSheets;return e.rel="stylesheet",e.href=a,e.media="only x",d&&(e.onload=d),f.parentNode.insertBefore(e,f),e.onloadcssdefined=function(b){for(var c,d=0;d<g.length;d++)g[d].href&&g[d].href.indexOf(a)>-1&&(c=!0);c?b():setTimeout(function(){e.onloadcssdefined(b)})},e.onloadcssdefined(function(){e.media=c||"all"}),e}';
  }
  $options = array(
    'type' => $type,
    'scope' => $css_defer >= 7 ? 'footer' : 'header',
    'scope_lock' => TRUE,
    'every_page' => TRUE,
    'group' => $css_defer == 1 ? JS_LIBRARY - 1 : JS_LIBRARY,
    'weight' => $css_defer == 1 ? -50000 : 0,
    'movable' => $css_defer == 1 ? FALSE : TRUE,
  );
  if ($type !== 'inline') {
    $options['async'] = TRUE;
  }
  else {
    $options['no_defer'] = TRUE;
  }
  static $added;
  if (!isset($added['inline'])) {
    drupal_add_js($data, $options);
    $added['inline'] = TRUE;
  }

  // Wrap CSS in noscript tags.
  $options = array(
    'type' => 'inline',
    'scope' => $css_defer >= 5 ? 'footer' : 'header',
    'scope_lock' => TRUE,
    'group' => $css_defer == 1 ? JS_LIBRARY - 1 : JS_DEFAULT,
    'weight' => $css_defer == 1 ? -50000 : 0,
    'movable' => $css_defer == 1 ? FALSE : TRUE,
  );
  foreach ($children as &$values) {
    // Do not defer inline scripts.
    if ($values['#tag'] === 'style') {
      continue;
    }
    if (!empty($values['#attributes']['href'])) {
      // Wrap current css in noscript tags.
      $values['#prefix'] = '<noscript>' . "\n";
      $values['#suffix'] = '</noscript>';

      // Add browsers to the js options.
      if (isset($values['#browsers'])) {
        $options['browsers'] = $values['#browsers'];
      }
      $inline = 'loadCSS("' . $values['#attributes']['href'] . '")';
      // Make async work if it's being used.
      if ($type !== 'inline') {
        $matches[2] = $matches[0] = $inline;
        $inline = advagg_mod_wrap_inline_js($matches, "window.loadCSS", 40);
      }
      // Add in script tags to load css via js.
      if (!isset($added[$values['#attributes']['href']])) {
        drupal_add_js($inline, $options);
        $added[$values['#attributes']['href']] = TRUE;
      }
      // Reset for next item in loop.
      if (isset($options['browsers'])) {
        unset($options['browsers']);
      }
    }
  }
  unset($values);
}

/**
 * Implements hook_advagg_hooks_implemented_alter().
 */
function advagg_mod_advagg_hooks_implemented_alter(&$hooks, $all) {
  if ($all) {
    $hooks += array(
      'advagg_mod_get_lists_alter' => array(),
    );
  }
}

/**
 * Implements hook_advagg_get_root_files_dir_alter().
 */
function advagg_mod_advagg_get_root_files_dir_alter(&$css_paths, &$js_paths) {
  $dir = rtrim(variable_get('advagg_mod_unified_multisite_dir', ''), '/');
  if (empty($dir) || !file_exists($dir) || !is_dir($dir)) {
    return;
  }
  // Change directory.
  $css_paths[0] = $dir . '/advagg_css';
  $js_paths[0] = $dir . '/advagg_js';

  file_prepare_directory($css_paths[0], FILE_CREATE_DIRECTORY);
  file_prepare_directory($js_paths[0], FILE_CREATE_DIRECTORY);

  // Set the URI of the directory.
  $css_paths[1] = advagg_get_relative_path($css_paths[0]);
  $js_paths[1] = advagg_get_relative_path($js_paths[0]);
}

/**
 * Implements hook_advagg_current_hooks_hash_array_alter().
 */
function advagg_mod_advagg_current_hooks_hash_array_alter(&$aggregate_settings) {
  // JS Settings.
  $aggregate_settings['variables']['advagg_mod_js_async_shim'] = variable_get('advagg_mod_js_async_shim', ADVAGG_MOD_JS_ASYNC_SHIM);

  // Make safe if using the aggressive cache.
  if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 5) {
    $aggregate_settings['variables']['advagg_mod_js_preprocess'] = variable_get('advagg_mod_js_preprocess', ADVAGG_MOD_JS_PREPROCESS);
    $aggregate_settings['variables']['advagg_mod_js_remove_unused'] = variable_get('advagg_mod_js_remove_unused', ADVAGG_MOD_JS_REMOVE_UNUSED);
    $aggregate_settings['variables']['advagg_mod_js_head_extract'] = variable_get('advagg_mod_js_head_extract', ADVAGG_MOD_JS_HEAD_EXTRACT);
    $aggregate_settings['variables']['advagg_mod_js_adjust_sort_external'] = variable_get('advagg_mod_js_adjust_sort_external', ADVAGG_MOD_JS_ADJUST_SORT_EXTERNAL);
    $aggregate_settings['variables']['advagg_mod_js_adjust_sort_inline'] = variable_get('advagg_mod_js_adjust_sort_inline', ADVAGG_MOD_JS_ADJUST_SORT_INLINE);
    $aggregate_settings['variables']['advagg_mod_js_adjust_sort_browsers'] = variable_get('advagg_mod_js_adjust_sort_browsers', ADVAGG_MOD_JS_ADJUST_SORT_BROWSERS);
    $aggregate_settings['variables']['advagg_mod_ga_inline_to_file'] = variable_get('advagg_mod_ga_inline_to_file', ADVAGG_MOD_GA_INLINE_TO_FILE);
    $aggregate_settings['variables']['advagg_mod_js_footer'] = variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER);
    $aggregate_settings['variables']['advagg_mod_js_defer'] = variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER);
    $aggregate_settings['variables']['advagg_mod_js_footer_inline_alter'] = variable_get('advagg_mod_js_footer_inline_alter', ADVAGG_MOD_JS_FOOTER_INLINE_ALTER);
    $aggregate_settings['variables']['advagg_mod_js_async'] = variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC);
  }

  // CSS Settings.
  $aggregate_settings['variables']['advagg_mod_css_translate'] = variable_get('advagg_mod_css_translate', ADVAGG_MOD_CSS_TRANSLATE);
  if (variable_get('advagg_mod_css_translate', ADVAGG_MOD_CSS_TRANSLATE)) {
    $aggregate_settings['variables']['advagg_mod_css_translate_lang'] = isset($GLOBALS['language']->language) ? $GLOBALS['language']->language : 'en';
  }

  // Make safe if using the aggressive cache.
  if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) >= 5) {
    $aggregate_settings['variables']['advagg_mod_css_preprocess'] = variable_get('advagg_mod_css_preprocess', ADVAGG_MOD_CSS_PREPROCESS);
    $aggregate_settings['variables']['advagg_mod_css_head_extract'] = variable_get('advagg_mod_css_head_extract', ADVAGG_MOD_CSS_HEAD_EXTRACT);
    $aggregate_settings['variables']['advagg_mod_css_adjust_sort_external'] = variable_get('advagg_mod_css_adjust_sort_external', ADVAGG_MOD_CSS_ADJUST_SORT_EXTERNAL);
    $aggregate_settings['variables']['advagg_mod_css_adjust_sort_inline'] = variable_get('advagg_mod_css_adjust_sort_inline', ADVAGG_MOD_CSS_ADJUST_SORT_INLINE);
    $aggregate_settings['variables']['advagg_mod_css_adjust_sort_browsers'] = variable_get('advagg_mod_css_adjust_sort_browsers', ADVAGG_MOD_CSS_ADJUST_SORT_BROWSERS);
    $aggregate_settings['variables']['advagg_mod_css_defer'] = variable_get('advagg_mod_css_defer', ADVAGG_MOD_CSS_DEFER);
    $aggregate_settings['variables']['advagg_mod_css_defer_js_code'] = variable_get('advagg_mod_css_defer_js_code', ADVAGG_MOD_CSS_DEFER_JS_CODE);
    $aggregate_settings['variables']['advagg_mod_inline_visibility'] = variable_get('advagg_mod_inline_visibility', ADVAGG_MOD_VISIBILITY_LISTED);
    $aggregate_settings['variables']['advagg_mod_inline_pages'] = variable_get('advagg_mod_inline_pages', '');
  }
}

// Helper Functions.
/**
 * Remove ajaxPageState CSS/JS if misc/ajax.js is not used.
 *
 * @param array $scripts
 *   Render array.
 */
function advagg_mod_js_no_ajaxpagestate(array &$scripts) {
  if (!variable_get('advagg_mod_js_no_ajaxpagestate', ADVAGG_MOD_JS_NO_AJAXPAGESTATE) || variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
    return;
  }

  // No ajax.js but there is a settings array.
  if (!isset($scripts['#items']['misc/ajax.js']) && isset($scripts['#items']['settings']['data'])) {
    foreach ($scripts['#items']['settings']['data'] as $delta => $setting) {
      if (array_key_exists('ajaxPageState', $setting)) {
        // Remove js files.
        if (isset($scripts['#items']['settings']['data'][$delta]['ajaxPageState']['js'])) {
          unset($scripts['#items']['settings']['data'][$delta]['ajaxPageState']['js']);
        }
        // Remove css files.
        if (isset($scripts['#items']['settings']['data'][$delta]['ajaxPageState']['css'])) {
          unset($scripts['#items']['settings']['data'][$delta]['ajaxPageState']['css']);
        }
        // Cleanup.
        if (empty($scripts['#items']['settings']['data'][$delta]['ajaxPageState'])) {
          unset($scripts['#items']['settings']['data'][$delta]['ajaxPageState']);
          if (empty($scripts['#items']['settings']['data'][$delta])) {
            unset($scripts['#items']['settings']['data'][$delta]);
          }
        }
      }
    }
  }
}

/**
 * Generate a list of rules and exceptions for js files and inline.
 *
 * Controls inline wrapping and defer. Controls no async/defer file list.
 * Controls files that say in the header.
 *
 * @return array
 *   A multidimensional array.
 */
function advagg_mod_get_lists() {
  $lists = &drupal_static(__FUNCTION__);
  if (!isset($lists)) {
    // Do not move to footer file list.
    $header_file_list = array(
      // Modernizr js.
      '/modernizr.',
      // Html5shiv and html5shiv-printshiv.
      '/html5shiv.',
      '/html5shiv-printshiv.',
    );

    // Do not move to footer inline list.
    $header_inline_list = array();

    // Do not defer/async list.
    $no_async_defer_list = array(
      // Wistia js.
      '//fast.wistia.',
      // Maps.
      '//maps.googleapis.com',
      '//dev.virtualearth.net',
      '//api.maps.yahoo.com',
    );

    // Wrap inline js so it does not run until the condition is TRUE.
    // Inline search string => js condition.
    $inline_wrapper_list = array();

    // Get inline wrap js skip list string and convert it to an array.
    $inline_js_wrap_skip_list = array_filter(array_map('trim', explode("\n", variable_get('advagg_mod_wrap_inline_js_skip_list', ADVAGG_MOD_WRAP_INLINE_JS_SKIP_LIST))));

    // Get inline defer js skip list string and convert it to an array.
    $inline_js_defer_skip_list = array_filter(array_map('trim', explode("\n", variable_get('advagg_mod_defer_inline_js_skip_list', ADVAGG_MOD_DEFER_INLINE_JS_SKIP_LIST))));
    $inline_js_defer_skip_list[] = 'loadCSS(';
    if (module_exists('googleanalytics')) {
      $inline_js_defer_skip_list[] = '_gaq.push(["_';
    }

    // If there is a fast clicker, ajax links might not work if ajax.js is
    // loaded in the footer.
    $all_in_footer_list = array(
      'misc/ajax.js' => array(
        '/jquery.js',
        '/jquery.min.js',
        '/jquery.once.js',
        '/ajax.js',
        '/drupal.js',
        'settings',
      ),
    );

    // Openlayers.
    if (module_exists('openlayers')) {
      // Openlayers fix; external scripts can not be loaded out of order.
      // Cloudmade.
      $path = variable_get('openlayers_layers_cloudmade_js', '');
      if (valid_url($path, TRUE)) {
        $no_async_defer_list['openlayers_layers_cloudmade'] = $path;
      }
      // Google.
      $mapdomain = variable_get('openlayers_layers_google_mapdomain', 'maps.google.com');
      $no_async_defer_list['openlayers_layers_google'] = $mapdomain . '/maps';
    }

    // Google Admanager.
    if (module_exists('google_admanager')) {
      // Needs to be in the header.
      $header_file_list[] = '/google_service.';

      // Needs to be in the header.
      $header_inline_list[] = 'GS_googleAddAdSenseService(';
      $header_inline_list[] = 'GS_googleEnableAllServices(';
      $header_inline_list[] = 'GA_googleAddSlot(';
      $header_inline_list[] = 'GA_googleFetchAds(';

      // Can not be async/defered.
      $no_async_defer_list[] = '/google_service.';

      // Can not be wrapped in a callback function.
      $inline_js_wrap_skip_list[] = 'GS_googleAddAdSenseService(';
      $inline_js_wrap_skip_list[] = 'GS_googleEnableAllServices(';
      $inline_js_wrap_skip_list[] = 'GA_googleAddSlot(';
      $inline_js_wrap_skip_list[] = 'GA_googleFetchAds(';
      $inline_js_wrap_skip_list[] = 'GA_googleFillSlot(';

      // Can not be wrapped in a callback function.
      $inline_js_defer_skip_list[] = 'GS_googleAddAdSenseService(';
      $inline_js_defer_skip_list[] = 'GS_googleEnableAllServices(';
      $inline_js_defer_skip_list[] = 'GA_googleAddSlot(';
      $inline_js_defer_skip_list[] = 'GA_googleFetchAds(';
      $inline_js_defer_skip_list[] = 'GA_googleFillSlot(';
    }

    // Allow other modules to add/edit the above lists.
    // Call hook_advagg_mod_get_lists_alter().
    $lists = array(
      $header_file_list,
      $header_inline_list,
      $no_async_defer_list,
      $inline_wrapper_list,
      $inline_js_wrap_skip_list,
      $inline_js_defer_skip_list,
      $all_in_footer_list,
    );
    drupal_alter('advagg_mod_get_lists', $lists);
  }
  return $lists;
}

/**
 * Move JS to the footer.
 *
 * @param array $js
 *   JS array.
 */
function advagg_mod_js_move_to_footer(array &$js) {
  // Move all JS to the footer.
  $move_js_to_footer = variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER);
  if (empty($move_js_to_footer)) {
    return;
  }

  list($header_file_list, $header_inline_list, , , , , $all_in_footer_list) = advagg_mod_get_lists();

  // Process all in footer list.
  if ($move_js_to_footer == 3 && !empty($all_in_footer_list)) {
    foreach ($all_in_footer_list as $key => $search_strings) {
      if (isset($js[$key])) {
        foreach ($js as $name => &$values) {
          foreach ($search_strings as $string) {
            if (strpos($name, $string) !== FALSE) {
              $values['scope_lock'] = TRUE;
              break;
            }
            if (is_string($values['data']) && strpos($values['data'], $string) !== FALSE) {
              $values['scope_lock'] = TRUE;
              break;
            }
          }
        }
      }
    }
  }

  foreach ($js as &$values) {
    // Skip if a library and configured to do so.
    if ($move_js_to_footer == 1 && $values['group'] <= JS_LIBRARY) {
      continue;
    }

    // Skip if the scope has been locked.
    if (!empty($values['scope_lock'])) {
      continue;
    }

    // Allow certain scripts to be kept in the header.
    if ($values['type'] !== 'inline' && $values['type'] !== 'setting') {
      foreach ($header_file_list as $search_string) {
        if (stripos($values['data'], $search_string) !== FALSE) {
          continue 2;
        }
      }
    }

    // Allow certain inline scripts to be kept in the header.
    if ($values['type'] === 'inline') {
      foreach ($header_inline_list as $search_string) {
        if (strpos($values['data'], $search_string) !== FALSE) {
          continue 2;
        }
      }
    }

    // If JS is not in the header increase group by 10000.
    if ($values['scope'] !== 'header') {
      $values['group'] += 10000;
    }
    // If JS is already in the footer increase group by 10000.
    if ($values['scope'] === 'footer') {
      $values['group'] += 10000;
    }
    $values['scope'] = 'footer';
  }
  unset($values);
}

/**
 * Add the defer and or the async tag to js.
 *
 * @param array $js
 *   JS array.
 *
 * @return bool
 *   TRUE if jQuery is defered.
 */
function advagg_mod_js_async_defer(array &$js) {
  $jquery_defered = FALSE;

  // Return eairly if this is disabled.
  $defer_setting = variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER);
  $async_setting = variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC);
  if (!$defer_setting && !$async_setting) {
    return $jquery_defered;
  }

  list(, , $no_async_defer_list, $inline_wrapper_list) = advagg_mod_get_lists();

  // Disable this section of code for now; the on error attribute only works
  // with async safe JS.
  $use_on_error = FALSE;
  if (variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY)) {
    $use_on_error = TRUE;
  }
  // If everything is async safe then we can use on error.
  // Only needed if the jquery_update javascript is loaded via async/defer.
  if ($use_on_error) {
    $jquery_update_fallback = '';
    $jquery_update_ui_fallback = '';
    $inline_array = array();
    if (module_exists('jquery_update') && variable_get('jquery_update_jquery_cdn', 'none') !== 'none') {
      foreach ($js as $name => &$values) {
        if ($values['type'] !== 'inline') {
          continue;
        }
        if ( stripos($values['data'], 'window.jQuery.ui') !== FALSE
          && stripos($values['data'], 'document.write("<script') !== FALSE
        ) {
          $path = drupal_get_path('module', 'jquery_update');
          $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
          $js_path = ($min == '.min') ? '/replace/ui/ui/minified/jquery-ui.min.js' : '/replace/ui/ui/jquery-ui.js';
          $jquery_update_ui_fallback = base_path() . $path . $js_path;
          if (empty($inline_array)) {
            $inline_array = $values;
          }
          unset($js[$name]);
          continue;
        }
        if ( stripos($values['data'], 'window.jQuery') !== FALSE
          && stripos($values['data'], 'document.write("<script') !== FALSE
        ) {
          $path = drupal_get_path('module', 'jquery_update');
          $version = variable_get('jquery_update_jquery_version', '1.10');
          $min = variable_get('jquery_update_compression_type', 'min') == 'none' ? '' : '.min';
          $jquery_update_fallback = base_path() . $path . '/replace/jquery/' . $version . '/jquery' . $min . '.js';
          $inline_array = $values;
          unset($js[$name]);
          continue;
        }
      }
      unset($values);
    }
    if (!empty($jquery_update_fallback) || !empty($jquery_update_ui_fallback)) {
      $inline_array['group'] = '-150';
      $inline_array['weight'] += -10;
      $inline_array['data'] = 'function advagg_fallback(file){var head = document.getElementsByTagName("head")[0];var script = document.createElement("script");script.src = file;script.type = "text/javascript";head.appendChild(script);};';
      $inline_array['scope_lock'] = TRUE;
      $inline_array['movable'] = FALSE;
      $inline_array['no_defer'] = TRUE;
      $inline_array['scope'] = 'header';
      $js[] = $inline_array;
    }
  }

  // Make all scripts defer and/or async.
  $jquery_rev = strrev('/jquery.js');
  $jquery_min_rev = strrev('/jquery.min.js');
  $jquery_ui_rev = strrev('/jquery-ui.js');
  $jquery_ui_min_rev = strrev('jquery-ui.min.js');
  foreach ($js as $name => &$values) {
    // Skip if not a file or external.
    if ($values['type'] !== 'file' && $values['type'] !== 'external') {
      continue;
    }

    // Everything is defer.
    if (variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)) {
      $values['defer'] = TRUE;
    }
    // Everything is async.
    if (variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC)) {
      $values['async'] = TRUE;
    }

    // Special handling for jQuery.
    if ( stripos(strrev($name), $jquery_rev) === 0
      || stripos(strrev($name), $jquery_min_rev) === 0
    ) {
      $jquery_defered = TRUE;
      // Do not fire jQuery.ready until Drupal.settings has been defined.
      $values['onload'] = "if(jQuery.isFunction(jQuery.holdReady)){jQuery.holdReady(true);}";
      // jquery_update fallback.
      if (module_exists('jquery_update') && variable_get('jquery_update_jquery_cdn', 'none') !== 'none') {
        if ($use_on_error) {
          if (!isset($values['onerror'])) {
            $values['onerror'] = '';
          }
          $values['onerror'] .= 'advagg_fallback(\'' . $jquery_update_fallback . '\');';
        }
        // Do not defer/async the loading of jquery.js
        if (!variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY)) {
          if (variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)) {
            $values['defer'] = FALSE;
            $jquery_defered = FALSE;
          }
        }
        if (variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC)) {
          $values['async'] = FALSE;
        }
        // Defer/async is off; done with loop.
        continue;
      }
    }
    // Special handling for jQuery UI.
    if ( stripos(strrev($name), $jquery_ui_rev) === 0
      || stripos(strrev($name), $jquery_ui_min_rev) === 0
    ) {
      // jquery_update ui fallback.
      if (module_exists('jquery_update') && variable_get('jquery_update_jquery_cdn', 'none') !== 'none') {
        if ($use_on_error) {
          if (!isset($values['onerror'])) {
            $values['onerror'] = '';
          }
          $values['onerror'] .= 'advagg_fallback(' . $jquery_update_ui_fallback . ');';
        }
        // Do not defer/async the loading of jquery-ui.js
        if (!variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY)) {
          if (variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)) {
            $values['defer'] = FALSE;
          }
        }
        if (variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC)) {
          $values['async'] = FALSE;
        }
        // Defer/async is off; done with loop.
        continue;
      }
    }

    // Drupal settings; don't run until misc/drupal.js has ran.
    if ($name === 'misc/drupal.js') {
      // Initialize the Drupal.settings JavaScript object after this has
      // loaded.
      if (!isset($values['onload'])) {
        $values['onload'] = '';
      }
      $matches[0] = $matches[2] = 'init_drupal_core_settings();';
      $values['onload'] .= advagg_mod_wrap_inline_js($matches, "window.init_drupal_core_settings && window.jQuery && window.Drupal", 1);
    }

    // No async defer list.
    foreach ($no_async_defer_list as $search_string) {
      if (strpos($name, $search_string) !== FALSE) {
        // Do not defer/async the loading this script.
        if (variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)) {
          $values['defer'] = FALSE;
        }
        if (variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC)) {
          $values['async'] = FALSE;
        }
      }
    }

    // Do not defer external scripts setting.
    if ($defer_setting == 2 && $values['type'] === 'external') {
      $values['defer'] = FALSE;
    }

  }
  unset($values);

  // Inline script special handling.
  foreach ($js as &$values) {
    if ($values['type'] !== 'inline') {
      continue;
    }
    foreach ($inline_wrapper_list as $search_string => $js_condition) {
      if (strpos($values['data'], $search_string) !== FALSE) {
        $matches[0] = $matches[2] = $values['data'];
        $values['data'] = advagg_mod_wrap_inline_js($matches, $js_condition);
      }
    }
  }
  unset($values);

  return $jquery_defered;
}

/**
 * Defer inline js by using setTimeout.
 *
 * @param array $js
 *   JS array.
 * @param bool $jquery_defered
 *   TRUE if jquery is defered.
 */
function advagg_mod_inline_defer(array &$js, $jquery_defered) {
  if ($jquery_defered) {
    $bootstrap_rev = strrev('/bootstrap.js');
    $bootstrap_min_rev = strrev('/bootstrap.min.js');
    foreach ($js as &$values) {
      // Defer bootstrap if jquery is defered.
      if (is_string($values['data'])
      && ( stripos(strrev($values['data']), $bootstrap_rev) === 0
        || stripos(strrev($values['data']), $bootstrap_min_rev) === 0
      )) {
        $values['defer'] = TRUE;
        continue;
      }

      // Only do inline.
      if ($values['type'] === 'inline') {
        // Skip if advagg has already wrapped this inline code.
        if (strpos($values['data'], 'advagg_mod_') !== FALSE) {
          continue;
        }
        if (!empty($values['no_defer'])) {
          continue;
        }

        // Do not wrap inline js if it contains a named function definition.
        $pattern = '/\\s*function\\s+((?:[a-z][a-z0-9_]*))\\s*\\(.*\\)\\s*\\{/smix';
        $match = preg_match($pattern, $values['data']);
        if (!$match) {
          // Defer inline scripts by wrapping the code in setTimeout callback.
          $matches[2] = $matches[0] = $values['data'];
          $values['data'] = advagg_mod_wrap_inline_js($matches);
        }
        elseif (stripos($values['data'], 'jQuery.') !== FALSE || stripos($values['data'], '(jQuery)') !== FALSE) {
          // Inline js has a named function that uses jQuery;
          // do not defer jQuery.js
          $no_jquery_defer = TRUE;
        }
      }
    }
    unset($values);
  }
  elseif (variable_get('advagg_mod_js_defer_inline_alter', ADVAGG_MOD_JS_DEFER_INLINE_ALTER)) {
    foreach ($js as &$values) {
      // Skip if not inline.
      if ($values['type'] !== 'inline') {
        continue;
      }
      // Skip if advagg has already wrapped this inline code.
      if (strpos($values['data'], 'advagg_mod_') !== FALSE) {
        continue;
      }
      if (!empty($values['no_defer'])) {
        continue;
      }

      // Do not wrap inline js if it contains a named function definition.
      $pattern = '/\\s*function\\s+((?:[a-z][a-z0-9_]*))\\s*\\(.*\\)\\s*\\{/smix';
      $match = preg_match($pattern, $values['data']);
      if (!$match) {
        // Defer the inline script by wrapping the code in setTimeout callback.
        $values['data'] = advagg_mod_defer_inline_js($values['data']);
      }
    }
    unset($values);
  }
  if (!empty($no_jquery_defer)) {
    $jquery_rev = strrev('/jquery.js');
    $jquery_min_rev = strrev('/jquery.min.js');
    foreach ($js as $name => &$values) {
      // Skip if not a file or external.
      if ($values['type'] !== 'file' && $values['type'] !== 'external') {
        continue;
      }
      // Special handling for jQuery.
      if ( stripos(strrev($name), $jquery_rev) === 0
        || stripos(strrev($name), $jquery_min_rev) === 0
      ) {
        $values['defer'] = FALSE;
      }
    }
  }
}

/**
 * Callback for pre_render to inline all JavaScript on this page.
 *
 * @param array $elements
 *   A render array containing:
 *   - #items: The JavaScript items as returned by drupal_add_js() and
 *     altered by drupal_get_js().
 *   - #group_callback: A function to call to group #items. Following
 *     this function, #aggregate_callback is called to aggregate items within
 *     the same group into a single file.
 *   - #aggregate_callback: A function to call to aggregate the items within
 *     the groups arranged by the #group_callback function.
 *
 * @return array
 *   A render array that will render to a string of JavaScript tags.
 *
 * @see drupal_get_js()
 */
function _advagg_mod_pre_render_scripts(array $elements) {
  if (advagg_mod_inline_page() || advagg_mod_inline_page_js()) {
    advagg_mod_inline_js($elements['#items']);
  }

  return $elements;
}

/**
 * A #pre_render callback to inline all CSS on this page.
 *
 * @param array $elements
 *   A render array containing:
 *   - '#items': The CSS items as returned by drupal_add_css() and altered by
 *     drupal_get_css().
 *   - '#group_callback': A function to call to group #items to enable the use
 *     of fewer tags by aggregating files and/or using multiple @import
 *     statements within a single tag.
 *   - '#aggregate_callback': A function to call to aggregate the items within
 *     the groups arranged by the #group_callback function.
 *
 * @return array
 *   A render array that will render to a string of XHTML CSS tags.
 *
 * @see drupal_get_css()
 */
function _advagg_mod_pre_render_styles(array $elements) {
  if (advagg_mod_inline_page() || advagg_mod_inline_page_css()) {
    advagg_mod_inline_css($elements['#items']);
  }

  return $elements;
}

/**
 * Use DOMDocument's loadHTML along with DOMXPath's query to find script tags.
 *
 * Once found, it will also wrap them in a javascript loader function.
 *
 * @param string $html
 *   HTML fragments.
 *
 * @return string
 *   The HTML fragment with less markup errors and script tags wrapped.
 */
function advagg_mod_xpath_script_wrapper($html) {
  // Do not throw errors when parsing the html.
  libxml_use_internal_errors(TRUE);

  $dom = new DOMDocument();
  // Load html with full tags all around.
  $dom->loadHTML('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $html . '</body></html>');
  $xpath = new DOMXPath($dom);
  // Get all script tags that
  // are not inside of a textarea
  // do not contain a src attribute
  // and the type is empty or has the type of javascript.
  $nodes = $xpath->query("//script[not(@src)][not(ancestor::textarea)][contains(translate(@type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'javascript') or not(@type)]");

  foreach ($nodes as $node) {
    $matches[2] = $node->nodeValue;
    // $matches[0] = $dom->saveHTML($node);
    $matches[0] = $node->nodeValue;
    $new_html = advagg_mod_wrap_inline_js($matches);
    $advagg = $dom->createElement('script');
    $advagg->appendchild($dom->createTextNode($new_html));
    $node->parentNode->replaceChild($advagg, $node);
  }
  // Render to HTML.
  $output = $dom->saveHTML();

  // Remove the tags we added.
  $output = str_replace(array(
    '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>',
    '</body></html>',
  ), array('', ''), $output);

  // Clear any errors.
  libxml_clear_errors();
  return $output;
}

/**
 * Use DOMDocument's loadHTML along with DOMXPath's query to find script tags.
 *
 * Once found, it will add the src to the dns prefetch list.
 *
 * @param string $html
 *   HTML fragments.
 */
function advagg_mod_xpath_script_external_dns($html) {
  // Do not throw errors when parsing the html.
  libxml_use_internal_errors(TRUE);

  $dom = new DOMDocument();
  // Load html with full tags all around.
  $dom->loadHTML('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $html . '</body></html>');
  $xpath = new DOMXPath($dom);
  // Get all script tags that
  // are not inside of a textarea
  // have a src attribute.
  $nodes = $xpath->query("//script[@src][not(ancestor::textarea)]");

  // Add the src attribute to dns-prefetch.
  foreach ($nodes as $node) {
    advagg_add_dns_prefetch($node->attributes->getNamedItem('src')->nodeValue);
  }

  // Clear any errors.
  libxml_clear_errors();
}

/**
 * Callback for preg_replace_callback.
 *
 * Used to wrap inline JS in a function in order to defer the inline js code.
 *
 * @param string $input
 *   JavaScript code to wrap in setTimeout.
 *
 * @return string
 *   Inline javascript code wrapped up in a loader.
 */
function advagg_mod_defer_inline_js($input) {
  if ( variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY)
    && ( stripos($input, 'jQuery') !== FALSE
      || stripos($input, '$(') !== FALSE
      || stripos($input, 'Drupal.') !== FALSE
    )
    ) {
    $matches[2] = $matches[0] = $input;
    return advagg_mod_wrap_inline_js($matches);
  }

  // Get inline defer js skip list.
  list(, , , , , $inline_js_defer_skip_list) = advagg_mod_get_lists();
  if (!empty($inline_js_defer_skip_list)) {
    // If the line is on the skip list then do not inline the script.
    foreach ($inline_js_defer_skip_list as $string_to_check) {
      if (stripos($input, $string_to_check) !== FALSE) {
        return $input;
      }
    }
  }

  // Use a counter in order to create unique function names.
  static $counter;
  ++$counter;

  // JS wrapper code.
  $new = "
function advagg_mod_defer_${counter}() {
  ${input};
}
window.setTimeout(advagg_mod_defer_${counter}, 0);";

  return $new;
}

/**
 * Callback for preg_replace_callback.
 *
 * Used to wrap inline JS in a function in order to prevent js errors when JS is
 * moved to the footer, or when js is loaded async.
 *
 * @param array $matches
 *   $matches[0] is the full string; $matches[2] is just the JavaScript.
 * @param string $check_string
 *   JavaScript if statement; when true, run the inline code.
 * @param int $ms_wait
 *   Default amount of time to wait until the required code is available.
 *
 * @return string
 *   Inline javascript code wrapped up in a loader to prevent errors.
 */
function advagg_mod_wrap_inline_js(array $matches, $check_string = NULL, $ms_wait = 250) {
  list(, , , $inline_wrapper_list, $inline_js_wrap_skip_list) = advagg_mod_get_lists();

  if (empty($check_string)) {
    foreach ($inline_wrapper_list as $search_string => $js_condition) {
      if (strpos($matches[2], $search_string) !== FALSE) {
        $check_string = $js_condition;
        break;
      }
    }
    if (empty($check_string)) {
      $check_string = 'window.jQuery && window.Drupal && window.Drupal.settings';
    }
  }

  if (!empty($inline_js_wrap_skip_list)) {
    // If the line is on the skip list then do not inline the script.
    foreach ($inline_js_wrap_skip_list as $string_to_check) {
      if (stripos($matches[2], $string_to_check) !== FALSE) {
        return $matches[0];
      }
    }
  }

  // Use a counter in order to create unique function names.
  static $counter;
  ++$counter;

  // JS wrapper code.
  $new = "
function advagg_mod_$counter() {
  // Count how many times this function is called.
  advagg_mod_$counter.count = ++advagg_mod_$counter.count || 1;
  try {
    if (advagg_mod_$counter.count <= 40) {
      $matches[2]

      // Set this to 100 so that this function only runs once.
      advagg_mod_$counter.count = 100;
    }
  }
  catch(e) {
    if (advagg_mod_$counter.count >= 40) {
      // Throw the exception if this still fails after running 40 times.
      throw e;
    }
    else {
      // Try again in $ms_wait ms.
      window.setTimeout(advagg_mod_$counter, $ms_wait);
    }
  }
}
function advagg_mod_${counter}_check() {
  if ($check_string) {
    advagg_mod_$counter();
  }
  else {
    window.setTimeout(advagg_mod_${counter}_check, $ms_wait);
  }
}
advagg_mod_${counter}_check();";

  $return = str_replace($matches[2], $new, $matches[0]);
  return $return;
}

/**
 * Rearrange CSS/JS so that aggregates are better grouped.
 *
 * This can move all external assets to the top, thus in one group.
 * This can move all inline assets to the bottom, thus in one group.
 * This can move all browser conditional assets together.
 *
 * @param array $array
 *   The CSS or JS array.
 * @param string $type
 *   String: css or js.
 */
function advagg_mod_sort_css_js(array &$array, $type) {
  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace:3
  if ( ($type === 'js' && variable_get('advagg_mod_js_adjust_sort_external', ADVAGG_MOD_JS_ADJUST_SORT_EXTERNAL))
    || ($type === 'css' && variable_get('advagg_mod_css_adjust_sort_external', ADVAGG_MOD_CSS_ADJUST_SORT_EXTERNAL))
  ) {
    // Find all external items.
    $external = array();
    $group = NULL;
    $every_page = NULL;
    $weight = NULL;
    foreach ($array as $key => $value) {
      // Set values if not set.
      if (is_null($group)) {
        $group = $value['group'];
      }
      if (is_null($every_page)) {
        $every_page = $value['every_page'];
      }
      if (is_null($weight)) {
        $weight = $value['weight'];
      }

      // Find "lightest" item.
      if ($value['group'] < $group) {
        $group = $value['group'];
      }
      if ($value['every_page'] && !$every_page) {
        $every_page = $value['every_page'];
      }
      if ($value['weight'] < $weight) {
        $weight = $value['weight'];
      }

      if (!empty($value['type']) && $value['type'] === 'external') {
        $external[$key] = $value;
        unset($array[$key]);
      }

      if (!empty($value['type']) && $value['type'] === 'inline') {
        // Move jQuery fallback as well.
        if (strpos($value['data'], 'window.jQuery') === 0) {
          $external[$key] = $value;
          unset($array[$key]);
        }
        // Move jQuery ui fallback as well.
        if (strpos($value['data'], 'window.jQuery.ui') === 0) {
          $external[$key] = $value;
          unset($array[$key]);
        }
      }
    }
    // Sort the array so that it appears in the correct order.
    advagg_drupal_sort_css_js_stable($external);

    // Group all external together.
    $offset = 0.0001;
    $weight += -1;
    $found_jquery = FALSE;
    foreach ($external as $key => $value) {
      if (isset($value['movable']) && empty($value['movable'])) {
        $array[$key] = $value;
        continue;
      }
      // If bootstrap is used, it must be loaded after jquery. Don't move
      // bootstrap if jquery is not above it.
      if ( strpos($value['data'], 'jquery.min.js') !== FALSE
        || strpos($value['data'], 'jquery.js') !== FALSE
      ) {
        $found_jquery = TRUE;
      }
      if (!$found_jquery && (strpos($value['data'], 'bootstrap.min.js') !== FALSE || strpos($value['data'], 'bootstrap.js') !== FALSE)) {
        $array[$key] = $value;
        continue;
      }
      $value['group'] = $group;
      $value['every_page'] = $every_page;
      $value['weight'] = $weight;
      $weight += $offset;
      $array[$key] = $value;
    }
  }

  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace:3
  if ( ($type === 'js' && variable_get('advagg_mod_js_adjust_sort_inline', ADVAGG_MOD_JS_ADJUST_SORT_INLINE))
    || ($type === 'css' && variable_get('advagg_mod_css_adjust_sort_inline', ADVAGG_MOD_CSS_ADJUST_SORT_INLINE))
  ) {
    // Find all inline items.
    $inline = array();
    $group = NULL;
    $every_page = NULL;
    $weight = NULL;
    foreach ($array as $key => $value) {
      // Set values if not set.
      if (is_null($group)) {
        $group = $value['group'];
      }
      if (is_null($every_page)) {
        $every_page = $value['every_page'];
      }
      if (is_null($weight)) {
        $weight = $value['weight'];
      }

      // Find "heaviest" item.
      if ($value['group'] > $group) {
        $group = $value['group'];
      }
      if (!$value['every_page'] && $every_page) {
        $every_page = $value['every_page'];
      }
      if ($value['weight'] > $weight) {
        $weight = $value['weight'];
      }

      if (!empty($value['type']) && $value['type'] === 'inline') {
        // Do not move jQuery fallback.
        if (strpos($value['data'], 'window.jQuery') === 0) {
          continue;
        }
        // Do not move jQuery.ui fallback.
        if (strpos($value['data'], 'window.jQuery.ui') === 0) {
          continue;
        }
        $inline[$key] = $value;
        unset($array[$key]);
      }
    }
    // Sort the array so that it appears in the correct order.
    advagg_drupal_sort_css_js_stable($inline);

    // Group all inline together.
    $offset = 0.0001;
    $weight += 1;
    foreach ($inline as $key => $value) {
      if (isset($value['movable']) && empty($value['movable'])) {
        $array[$key] = $value;
        continue;
      }
      $value['group'] = $group;
      $value['every_page'] = $every_page;
      $value['weight'] = $weight;
      $weight += $offset;
      $array[$key] = $value;
    }
  }

  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace:3
  if ( ($type === 'js' && variable_get('advagg_mod_js_adjust_sort_browsers', ADVAGG_MOD_JS_ADJUST_SORT_BROWSERS))
    || ($type === 'css' && variable_get('advagg_mod_css_adjust_sort_browsers', ADVAGG_MOD_CSS_ADJUST_SORT_BROWSERS))
  ) {
    // Get a list of browsers.
    $browsers_list = array();
    foreach ($array as $key => $value) {
      if (isset($value['browsers']['IE']) && $value['browsers']['IE'] !== TRUE) {
        $browsers_list['IE'][] = $value['browsers']['IE'];
      }
    }

    // Group browsers CSS together.
    if (isset($browsers_list['IE'])) {
      $browsers_list['IE'] = array_values(array_unique($browsers_list['IE']));
      foreach ($browsers_list['IE'] as $browser) {
        $browsers = array();
        $group = NULL;
        $every_page = NULL;
        $weight = NULL;
        foreach ($array as $key => $value) {
          if (isset($value['browsers']['IE']) && $browser === $value['browsers']['IE']) {
            // Set values if not set.
            if (is_null($group)) {
              $group = $value['group'];
            }
            if (is_null($every_page)) {
              $every_page = $value['every_page'];
            }
            if (is_null($weight)) {
              $weight = $value['weight'];
            }

            // Find "heaviest" item.
            if ($value['group'] > $group) {
              $group = $value['group'];
            }
            if (!$value['every_page'] && $every_page) {
              $every_page = $value['every_page'];
            }
            if ($value['weight'] > $weight) {
              $weight = $value['weight'];
            }

            $browsers[$key] = $value;
            unset($array[$key]);
          }
        }

        // Sort the array so that it appears in the correct order.
        advagg_drupal_sort_css_js_stable($browsers);

        // Group all browsers together.
        $offset = 0.0001;
        foreach ($browsers as $key => $value) {
          if (isset($value['movable']) && empty($value['movable'])) {
            $array[$key] = $value;
            continue;
          }
          $value['group'] = $group;
          $value['every_page'] = $every_page;
          $value['weight'] = $weight;
          $weight += $offset;
          $array[$key] = $value;
        }
      }
    }
  }
}

/**
 * Returns TRUE if this page should have inline CSS/JS.
 *
 * @return bool
 *   TRUE or FALSE.
 */
function advagg_mod_inline_page() {
  $visibility = variable_get('advagg_mod_inline_visibility', ADVAGG_MOD_VISIBILITY_LISTED);
  $pages = variable_get('advagg_mod_inline_pages', '');
  return advagg_mod_match_path($pages, $visibility);
}

/**
 * Returns TRUE if this page should have inline CSS.
 *
 * @return bool
 *   TRUE or FALSE.
 */
function advagg_mod_inline_page_css() {
  $visibility = variable_get('advagg_mod_inline_css_visibility', ADVAGG_MOD_VISIBILITY_LISTED);
  $pages = variable_get('advagg_mod_inline_css_pages', '');
  return advagg_mod_match_path($pages, $visibility);
}

/**
 * Returns TRUE if this page should have inline JS.
 *
 * @return bool
 *   TRUE or FALSE.
 */
function advagg_mod_inline_page_js() {
  $visibility = variable_get('advagg_mod_inline_js_visibility', ADVAGG_MOD_VISIBILITY_LISTED);
  $pages = variable_get('advagg_mod_inline_js_pages', '');
  return advagg_mod_match_path($pages, $visibility);
}

/**
 * Transforms all JS files into inline JS.
 *
 * @param array $js
 *   JS array.
 */
function advagg_mod_inline_js(array &$js) {
  $aggregate_settings = advagg_current_hooks_hash_array();

  foreach ($js as &$values) {
    // Only process files.
    if ($values['type'] !== 'file') {
      continue;
    }
    $filename = $values['data'];
    if (file_exists($filename)) {
      $contents = file_get_contents($filename);
    }
    // Allow other modules to modify this files contents.
    // Call hook_advagg_get_js_file_contents_alter().
    drupal_alter('advagg_get_js_file_contents', $contents, $filename, $aggregate_settings);

    $values['data'] = $contents;
    $values['type'] = 'inline';
  }
  unset($values);
}

/**
 * Transforms all CSS files into inline CSS.
 *
 * @param array $css
 *   CSS array.
 *
 * @see advagg_get_css_aggregate_contents()
 * @see drupal_build_css_cache()
 */
function advagg_mod_inline_css(array &$css) {
  $aggregate_settings = advagg_current_hooks_hash_array();
  $optimize = TRUE;
  module_load_include('inc', 'advagg', 'advagg');

  foreach ($css as &$values) {
    // Only process files.
    if ($values['type'] !== 'file') {
      continue;
    }

    $file = $values['data'];
    if (file_exists($file)) {
      $contents = advagg_load_css_stylesheet($file, $optimize, $aggregate_settings);

      // Allow other modules to modify this files contents.
      // Call hook_advagg_get_css_file_contents_alter().
      drupal_alter('advagg_get_css_file_contents', $contents, $file, $aggregate_settings);

      // Per the W3C specification at
      // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import rules
      // must proceed any other style, so we move those to the top.
      $regexp = '/@import[^;]+;/i';
      preg_match_all($regexp, $contents, $matches);
      $contents = preg_replace($regexp, '', $contents);
      $contents = implode('', $matches[0]) . $contents;

      $values['data'] = $contents;
      $values['type'] = 'inline';
    }
  }
  unset($values);
}

/**
 * Transforms all CSS files into inline CSS.
 *
 * @param string $pages
 *   String from the advagg_mod_inline_pages variable.
 * @param int $visibility
 *   Visibility setting from the advagg_mod_inline_visibility variable.
 *
 * @return bool
 *   TRUE if the current path matches the given pages.
 *
 * @see block_block_list_alter()
 */
function advagg_mod_match_path($pages, $visibility) {
  // Limited visibility blocks must list at least one page.
  if ($visibility == ADVAGG_MOD_VISIBILITY_LISTED && empty($pages)) {
    $page_match = FALSE;
  }
  elseif ($pages) {
    // Match path if necessary.
    // Convert path to lowercase. This allows comparison of the same path
    // with different case. Ex: /Page, /page, /PAGE.
    $pages = drupal_strtolower($pages);
    if ($visibility < ADVAGG_MOD_VISIBILITY_PHP) {
      // Convert the Drupal path to lowercase.
      $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
      // Compare the lowercase internal and lowercase path alias (if any).
      $page_match = drupal_match_path($path, $pages);
      if ($path != $_GET['q']) {
        $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
      }
      // When $visibility has a value of 0 (ADVAGG_MOD_VISIBILITY_NOTLISTED),
      // the block is displayed on all pages except those listed in $pages.
      // When set to 1 (ADVAGG_MOD_VISIBILITY_LISTED), it is displayed only on
      // those pages listed in $block->pages.
      $page_match = !($visibility xor $page_match);
    }
    elseif (module_exists('php')) {
      $page_match = php_eval($pages);
    }
    else {
      $page_match = FALSE;
    }
  }
  else {
    $page_match = TRUE;
  }

  return $page_match;
}

/**
 * See if JavaScript file contains drupal and/or jquery.
 *
 * @param string $filename
 *   Inline css, full URL, or filename.
 * @param string $type
 *   (Optional) inline, external, or file.
 *
 * @return array
 *   Returns an array stating if this JS file contains drupal or jquery.
 */
function advagg_mod_js_contains_jquery_drupal($filename, $type = '') {
  if (is_string($filename)) {
    if ($type === 'inline') {
      $contents = $filename;
    }
    // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace:1
    elseif ( $type === 'external'
          || strpos($filename, 'http://') === 0
          || strpos($filename, 'https://') === 0
          || strpos($filename, '//') === 0
    ) {
      $result = drupal_http_request($filename);
      if (($result->code == 200 || (isset($result->redirect_code) && $result->redirect_code == 200)) && !empty($result->data)) {
        $contents = $result->data;
      }
    }
    elseif (file_exists($filename)) {
      $contents = file_get_contents($filename);
    }
  }

  $results = array();
  if (!empty($contents) && stripos($contents, 'drupal.') !== FALSE) {
    $results['contents']['drupal'] = TRUE;
    if (stripos($contents, 'drupal.settings.') !== FALSE) {
      $results['contents']['drupal.settings'] = TRUE;
    }
    else {
      $results['contents']['drupal.settings'] = FALSE;
    }
    if (stripos($contents, 'drupal.behaviors.') !== FALSE) {
      $results['contents']['drupal.behaviors'] = TRUE;
    }
    else {
      $results['contents']['drupal.behaviors'] = FALSE;
    }
  }
  else {
    $results['contents']['drupal'] = FALSE;
    $results['contents']['drupal.settings'] = FALSE;
    $results['contents']['drupal.behaviors'] = FALSE;
  }
  if (!empty($contents) && stripos($contents, 'jquery') !== FALSE) {
    $results['contents']['jquery'] = TRUE;
  }
  else {
    $results['contents']['jquery'] = FALSE;
  }
  return $results;
}

/**
 * Move analytics.js to be a file instead of inline.
 *
 * @param array $js
 *   JS array.
 */
function advagg_mod_ga_inline_to_file(array &$js) {
  // Do nothing if the googleanalytics module is not enabled.
  if (!module_exists('googleanalytics')) {
    return;
  }

  // Get inline GA js and put it inside of an aggregrate.
  $ga_script = '';
  $debug = variable_get('googleanalytics_debug', 0);
  $api = googleanalytics_api();
  if ($api['api'] === 'analytics.js') {
    $library_tracker_url = '//www.google-analytics.com/' . ($debug ? 'analytics_debug.js' : 'analytics.js');
    $library_cache_url = 'http:' . $library_tracker_url;
  }
  else {
    // Which version of the tracking library should be used?
    if ($trackdoubleclick = variable_get('googleanalytics_trackdoubleclick', FALSE)) {
      $library_tracker_url = 'stats.g.doubleclick.net/dc.js';
      $library_cache_url = 'http://' . $library_tracker_url;
    }
    else {
      $library_tracker_url = '.google-analytics.com/ga.js';
      $library_cache_url = 'http://www' . $library_tracker_url;
    }
  }
  $ga_script = _googleanalytics_cache($library_cache_url);
  if (variable_get('googleanalytics_cache', 0) && $ga_script) {
    $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);
    $ga_script = substr($ga_script, stripos($ga_script, $mod_base_url) + $mod_base_url_len);
  }
  else {
    $ga_script = $library_cache_url;
    if ($api['api'] === 'ga.js' && $GLOBALS['is_https']) {
      if (!empty($trackdoubleclick)) {
        $ga_script = str_replace('http://', 'https://', $ga_script);
      }
      else {
        $ga_script = str_replace('http://www', 'https://ssl', $ga_script);
      }
    }
  }

  if (!empty($ga_script)) {
    foreach ($js as $key => $value) {
      // Skip if not inline.
      if ($value['type'] !== 'inline') {
        continue;
      }
      $add_ga = FALSE;
      // GoogleAnalytics 2.x inline loader string.
      if ($api['api'] === 'analytics.js') {
        $start = strpos($value['data'], '(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();');
        $end = strpos($value['data'], '})(window,document,"script",');
        if ($start === 0) {
          // Strip loader string.
          $js[$key]['data'] = substr($value['data'], 0, $start + 133) . substr($value['data'], $end);
          $js[$key]['data'] = advagg_mod_defer_inline_js($js[$key]['data']);
          $add_ga = TRUE;
        }
      }
      // GoogleAnalytics 1.x inline loader string.
      if ($api['api'] === 'ga.js') {
        $start = strpos($value['data'], '(function() {var ga = document.createElement("script");ga.type = "text/javascript";ga.async = true;ga.src =');
        $end = strpos($value['data'], '";var s = document.getElementsByTagName("script")[0];s.parentNode.insertBefore(ga, s);})();');
        if ($start !== FALSE && $end !== FALSE) {
          // Strip loader string.
          $js[$key]['data'] = substr($value['data'], 0, $start) . substr($value['data'], $end + 91);
          $js[$key]['no_defer'] = TRUE;
          $add_ga = TRUE;
        }
      }

      if ($add_ga) {
        // Add GA analytics.js file to the $js array.
        $js[$ga_script] = array(
          'data' => $ga_script,
          'type' => 'file',
          'async' => TRUE,
          'defer' => TRUE,
        );
        $js[$ga_script] += $value;
        break;
      }
    }
  }
}

/**
 * Remove JS if not in use on current page.
 *
 * @param array $js
 *   JS array.
 */
function advagg_remove_js_if_not_used(array &$js) {
  $files_skiplist = array(
    'drupal.js',
    'jquery.js',
    'jquery.min.js',
    'jquery.once.js',
  );
  $inline_skiplist = array();
  if (module_exists('jquery_update')) {
    $inline_skiplist[] = 'document.write("<script src=\'' . $GLOBALS['base_path'] . drupal_get_path('module', 'jquery_update') . '/replace/jquery/' . variable_get('jquery_update_jquery_version', '1.10') . '/jquery' . (variable_get('jquery_update_compression_type', 'min') === 'none' ? '' : '.min') . ".js'>";
  }
  if (module_exists('labjs')) {
    $inline_skiplist[] = 'var $L = $LAB.setGlobalDefaults';
  }

  $include_jquery = FALSE;
  $include_drupal = FALSE;
  module_load_include('inc', 'advagg', 'advagg');

  // Look at each JavaScript entry and get the info on it.
  $files_info_filenames = array();
  foreach ($js as &$values) {
    if ($values['type'] !== 'file' || !is_string($values['data'])) {
      continue;
    }
    foreach ($files_skiplist as $skip_name) {
      if (strlen($skip_name) < strlen($values['data']) && substr_compare($values['data'], $skip_name, -strlen($skip_name), strlen($skip_name)) === 0) {
        continue 2;
      }
    }
    $files_info_filenames[] = $values['data'];
  }
  unset($values);
  $files_info = advagg_get_info_on_files($files_info_filenames);

  // Look at each JavaScript entry and see if it uses jquery or drupal.
  foreach ($js as $name => &$values) {
    if ($values['type'] === 'file' || $values['type'] === 'external') {
      foreach ($files_skiplist as $skip_name) {
        if (substr_compare($values['data'], $skip_name, -strlen($skip_name), strlen($skip_name)) === 0) {
          continue 2;
        }
      }
    }
    if ($values['type'] === 'inline' && !empty($inline_skiplist)) {
      foreach ($inline_skiplist as $skip_string) {
        if (stripos($values['data'], $skip_string) !== FALSE) {
          continue 2;
        }
      }
    }
    // Get advagg_mod info if not set; skip external files.
    if (!isset($files_info[$name]['advagg_mod']) && $values['type'] !== 'external') {
      $files_info[$name]['advagg_mod'] = advagg_mod_js_contains_jquery_drupal($values['data'], $values['type']);
    }

    // See what needs to be included.
    if (!empty($files_info[$name]['advagg_mod']['contents']['drupal'])) {
      $include_jquery = TRUE;
      $include_drupal = TRUE;
      break;
    }
    elseif (!empty($files_info[$name]['advagg_mod']['contents']['jquery'])) {
      $include_jquery = TRUE;
    }
    elseif (isset($values['requires_jquery']) && !empty($values['requires_jquery'])) {
      $include_jquery = TRUE;
    }
  }
  unset($values);

  // Kill only drupal JavaScript.
  if (!$include_drupal) {
    unset($js['settings']);
    foreach ($js as $name => &$values) {
      $drupal = 'drupal.js';
      if (substr_compare($name, $drupal, -strlen($drupal), strlen($drupal)) === 0) {
        unset($js[$name]);
      }
    }
    unset($values);

    // Kill all default JavaScript.
    if (!$include_jquery) {
      foreach ($js as $name => &$values) {
        if ($values['type'] === 'file' || $values['type'] === 'external') {
          foreach ($files_skiplist as $skip_name) {
            if (substr_compare($name, $skip_name, -strlen($skip_name), strlen($skip_name)) === 0) {
              unset($js[$name]);
            }
          }
        }
        elseif ($values['type'] === 'inline') {
          foreach ($inline_skiplist as $skip_string) {
            if (stripos($values['data'], $skip_string) !== FALSE) {
              unset($js[$name]);
            }
          }
        }
      }
      unset($values);
    }
  }
}

/**
 * Given html, do some processing on the script tags included inside it.
 *
 * @param string $html
 *   String containing html markup.
 */
function advagg_mod_js_inline_processor(&$html) {
  // Add src script to dns-prefetch.
  if (variable_get('advagg_mod_js_get_external_dns', ADVAGG_MOD_JS_GET_EXTERNAL_DNS)) {
    advagg_mod_xpath_script_external_dns($html);
  }

  // Setting is enabled.
  // All JS is in the footer.
  // OR All JS is defer.
  // OR All JS is async.
  if ( variable_get('advagg_mod_js_footer_inline_alter', ADVAGG_MOD_JS_FOOTER_INLINE_ALTER)
    && ( variable_get('advagg_mod_js_footer', ADVAGG_MOD_JS_FOOTER) >= 1
      || variable_get('advagg_mod_js_defer', ADVAGG_MOD_JS_DEFER)
      || variable_get('advagg_mod_js_async', ADVAGG_MOD_JS_ASYNC)
  )) {
    $pattern = '/<script(?![^<>]*src=)(?:(?![^<>]*type=)|(?=[^<>]*type="?[^<>"]*javascript))(.*?)>(.*?)<\/script>/smix';
    $callback = 'advagg_mod_wrap_inline_js';
    // Wrap inline JS with a check so that it only runs once Drupal.settings &
    // jQuery are not undefined.
    // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
    if (variable_get('advagg_mod_wrap_inline_js_xpath', ADVAGG_MOD_WRAP_INLINE_JS_XPATH)) {
      $html = advagg_mod_xpath_script_wrapper($html);
    }
    else {
      $html = preg_replace_callback($pattern, $callback, $html);
    }
  }
}

// @ignore sniffer_commenting_functioncomment_hookreturndoc:12
// @ignore sniffer_commenting_functioncomment_hookparamdoc:8
/**
 * Implements hook_magic().
 *
 * @param array $magic_settings
 *   The renderable form array of the magic module theme settings. READ ONLY.
 * @param string $theme
 *   The theme that the settings will be editing.
 *
 * @return array
 *   The array of settings within the magic module theme page. Must not contain
 *   anything from the $magic_settings array.
 */
function advagg_mod_magic(array $magic_settings, $theme) {
  $settings = array();

  // If possible disable access and set default to false.
  if (!isset($magic_settings['css']['magic_embedded_mqs']['#access'])) {
    $settings['css']['magic_embedded_mqs']['#access'] = FALSE;
  }
  if (!isset($magic_settings['css']['magic_embedded_mqs']['#default_value'])) {
    $settings['css']['magic_embedded_mqs']['#default_value'] = FALSE;
  }
  if (!isset($magic_settings['js']['magic_footer_js']['#access'])) {
    $settings['js']['magic_footer_js']['#access'] = FALSE;
  }
  if (!isset($magic_settings['js']['magic_footer_js']['#default_value'])) {
    $settings['js']['magic_footer_js']['#default_value'] = FALSE;
  }
  if (!isset($magic_settings['js']['magic_library_head']['#access'])) {
    $settings['js']['magic_library_head']['#access'] = FALSE;
  }
  if (!isset($magic_settings['js']['magic_library_head']['#default_value'])) {
    $settings['js']['magic_library_head']['#default_value'] = FALSE;
  }
  if (!isset($magic_settings['js']['magic_experimental_js']['#access'])) {
    $settings['js']['magic_experimental_js']['#access'] = FALSE;
  }
  if (!isset($magic_settings['js']['magic_experimental_js']['#default_value'])) {
    $settings['js']['magic_experimental_js']['#default_value'] = FALSE;
  }

  // Add in our own validate function so we can preprocess variables before
  // they are saved.
  $settings['#validate'] = array('advagg_mod_magic_form_validate');
  return $settings;
}

/**
 * Form validation handler. Disable certain magic settings before being saved.
 */
function advagg_mod_magic_form_validate($form, &$form_state) {
  // Disable magic functionality if it is a duplicate of AdvAgg.
  $form_state['values']['magic_embedded_mqs'] = 0;
  $form_state['values']['magic_footer_js'] = 0;
  $form_state['values']['magic_library_head'] = 0;
  $form_state['values']['magic_experimental_js'] = 0;
}
