You are here

function advagg_fix_type in Advanced CSS/JS Aggregation 7.2

Alter the js array fixing the type key if set incorrectly.

Parameters

array $array: CSS or JS array.

string $type: CSS or JS.

2 calls to advagg_fix_type()
advagg_get_css in ./advagg.module
Returns a themed representation of all stylesheets to attach to the page.
advagg_get_full_js in ./advagg.module
Get full JS array.

File

./advagg.module, line 5878
Advanced CSS/JS aggregation module.

Code

function advagg_fix_type(array &$array, $type) {

  // Skip if advagg is disabled.
  if (!advagg_enabled()) {
    return;
  }

  // Skip if setting is turned off.
  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;
  }

  // Fix type if it was incorrectly set.
  // Get hostname and base path.
  $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) {

    // Skip if the data is empty or not a string.
    if (empty($value['data']) || !is_string($value['data'])) {
      continue;
    }

    // Default to file if type is not set.
    if (!isset($value['type'])) {
      $value['type'] = 'file';
    }

    // If inline, be done with processing.
    if ($value['type'] === 'inline') {
      continue;
    }

    // Default to file if not file, inline, external, setting.
    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 type is external but doesn't start with http, https, or // change it
    // to file.
    if ($value['type'] === 'external' && $http_pos !== 0 && $https_pos !== 0 && $double_slash_pos !== 0) {
      if (is_readable($value['data'])) {
        $value['type'] = 'file';
      }
      else {

        // Fix for subdir issues.
        $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 type is file but it starts with http, https, or // change it to
    // external. Skip tripple slash for local files.
    if ($value['type'] === 'file' && ($http_pos === 0 || $https_pos === 0 || $double_slash_pos === 0 && $tripple_slash_pos === FALSE)) {
      $value['type'] = 'external';
    }

    // If type is external and starts with http, https, or // but points to
    // this host change to file, but move it to the top of the aggregation
    // stack.
    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 {

        // Fix for subdir issues.
        $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);
}