You are here

function advagg_check_missing_handler in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg.install \advagg_check_missing_handler()

Check to see if the CSS/JS generator is working.

2 calls to advagg_check_missing_handler()
advagg_admin_settings_form in ./advagg.admin.inc
Form builder; Configure advagg settings.
advagg_requirements in ./advagg.install
Implementation of hook_requirements().
1 string reference to 'advagg_check_missing_handler'
advagg_enable in ./advagg.install
Implementation of hook_enable().

File

./advagg.install, line 303
Handles Advanced Aggregation installation and upgrade tasks.

Code

function advagg_check_missing_handler() {
  drupal_load('module', 'advagg');
  advagg_install_test_async_stream();
  global $base_path;
  $ret = array();
  $async = variable_get('advagg_async_generation', -1);
  $filepath = '/css_missing' . mt_rand() . time() . '_0';

  // Ensure translations don't break at install time
  $t = get_t();

  // Don't run the test if site is offline
  if (variable_get('site_offline', 0)) {
    $ret['advagg_async_offline'] = array(
      'title' => $t('Adv CSS/JS Agg - Asynchronous Mode'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('Unknown. Currently set to @value.', array(
        '@value' => $async ? 'TRUE' : 'FALSE',
      )),
      'description' => $t('This can not be tested while the site is in <a href="@maintenance">offline mode</a>', array(
        '@maintenance' => url('admin/settings/site-maintenance'),
      )),
    );
    return $ret;
  }

  // Setup request
  list($css_path, $js_path) = advagg_get_root_files_dir();
  $url = _advagg_build_url($css_path . $filepath . '.css');
  $htauth_user = variable_get('advagg_async_test_username', '');
  $htauth_pass = variable_get('advagg_async_test_password', '');
  $headers = array(
    'Host' => $_SERVER['HTTP_HOST'],
    'Connection' => 'close',
  );
  if ($htauth_user && $htauth_pass) {
    $headers['Authorization'] = 'Basic ' . base64_encode($htauth_user . ':' . $htauth_pass);
  }

  // Check that the menu router handler is working. If it's not working the rest
  // of the tests are pointless.
  $item = menu_get_item($css_path . $filepath . '.css');
  if (empty($item['page_callback']) || strpos($item['page_callback'], 'advagg') === FALSE) {
    $item = str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($item, TRUE))));
    $ret['advagg_async_generation_menu_issue'] = array(
      'title' => $t('Adv CSS/JS Agg - Async Mode'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('Flush your caches'),
      'description' => $t('You need to flush your menu cache. This can be done near the bottom of the <a href="@performance">performance page</a>. If this does not fix the issue copy this info below when opening up an <a href="http://drupal.org/node/add/project-issue/advagg">issue for advagg</a>: <br /> !info', array(
        '@performance' => url('admin/settings/performance'),
        '!info' => $item,
      )),
    );
    return $ret;
  }
  $item = menu_get_item($js_path . $filepath . '.js');
  if (empty($item['page_callback']) || strpos($item['page_callback'], 'advagg') === FALSE) {
    $item = str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($item, TRUE))));
    $ret['advagg_async_generation_menu_issue'] = array(
      'title' => $t('Adv CSS/JS Agg - Async Mode'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('Flush your caches'),
      'description' => $t('You need to flush your menu cache. This can be done near the bottom of the <a href="@performance">performance page</a>. If this does not fix the issue copy this info below when opening up an <a href="http://drupal.org/node/add/project-issue/advagg">issue for advagg</a>: <br /> !info', array(
        '@performance' => url('admin/settings/performance'),
        '!info' => $item,
      )),
    );
    return $ret;
  }

  // Send request and also time it.
  timer_start(__FUNCTION__ . 'local');
  $data_local = drupal_http_request($url, $headers);
  $time_local = timer_stop(__FUNCTION__ . 'local');

  // Test CDN module; JS & CSS paths.
  if (module_exists('cdn')) {

    // Make sure CDN module is on.
    $status = variable_get(CDN_STATUS_VARIABLE, CDN_DISABLED);
    if (($status == CDN_ENABLED || $status == CDN_TESTING && user_access(CDN_PERM_ACCESS_TESTING)) && !variable_get(CDN_THEME_LAYER_FALLBACK_VARIABLE, FALSE)) {
      global $conf;
      $path_blacklist = variable_get(CDN_EXCEPTION_DRUPAL_PATH_BLACKLIST_VARIABLE, CDN_EXCEPTION_DRUPAL_PATH_BLACKLIST_DEFAULT);
      $conf[CDN_EXCEPTION_DRUPAL_PATH_BLACKLIST_VARIABLE] = '';
      $url_cdn_css = advagg_build_uri($css_path . $filepath . '.css');
      $parts_css = @parse_url($url_cdn_css);

      // Do not test CDN CSS if the hosts are the same.
      if (!empty($parts_css['host']) && strcmp($parts_css['host'], $_SERVER['HTTP_HOST']) == 0) {
        $parts_css = FALSE;
      }
      $url_cdn_js = advagg_build_uri($js_path . $filepath . '.js');
      $parts_js = @parse_url($url_cdn_js);

      // Do not test CDN JS if the hosts are the same.
      if (!empty($parts_js['host']) && strcmp($parts_js['host'], $_SERVER['HTTP_HOST']) == 0) {
        $parts_js = FALSE;
      }
      $conf[CDN_EXCEPTION_DRUPAL_PATH_BLACKLIST_VARIABLE] = $path_blacklist;
      if (!empty($parts_css)) {

        // Send request and also time it.
        timer_start(__FUNCTION__ . 'cdn_css');
        $data_cdn_css = drupal_http_request($url_cdn_css);
        $time_cdn_css = timer_stop(__FUNCTION__ . 'cdn_css');
      }
      if (!empty($parts_js)) {

        // Send request and also time it.
        timer_start(__FUNCTION__ . 'cdn_js');
        $data_cdn_js = drupal_http_request($url_cdn_js);
        $time_cdn_js = timer_stop(__FUNCTION__ . 'cdn_js');
      }
      $mode = variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC);
    }
  }
  $readme = drupal_get_path('module', 'advagg') . '/README.txt';
  if (module_exists('cdn')) {
    $cdn_extra = $t('(both fieldsets)');
  }
  else {
    $cdn_extra = '';
  }
  $extra_404 = $t('If you are still having issues you can go to the <a href="@info">AdvAgg information tab</a> and select Asynchronous debug info. If creating an issue on d.o be sure to include this information @cdn_extra.', array(
    '@info' => url('admin/settings/advagg/info'),
    '@cdn_extra' => $cdn_extra,
  ));
  $extra_404 .= module_exists('stage_file_proxy') ? $t('<a href="http://drupal.org/project/stage_file_proxy">Stage File Proxy</a> needs <a href="http://drupal.org/node/1078218">this patch</a> applied to it in order for advagg to work correctly.') : '';

  // Check CDN responses.
  $cdn_failed = FALSE;
  if (isset($parts_css) && empty($parts_css)) {
    $ret['advagg_async_generation_css_cdn'] = array(
      'title' => $t('Adv CSS/JS Agg - CDN Async Mode'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('CSS CDN Issue'),
      'description' => $t('Your current CDN settings are not rewriting the URL for advagg CSS files to point to your CDN. Most likely you need to adjust your <a href="@cdnother">CDN exceptions</a> to allow CSS files from advagg.', array(
        '@cdnother' => url('admin/settings/cdn/other'),
      )),
    );
  }
  if (isset($parts_js) && empty($parts_js)) {
    $ret['advagg_async_generation_js_cdn'] = array(
      'title' => $t('Adv CSS/JS Agg - CDN Async Mode'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('JS CDN Issue'),
      'description' => $t('Your current CDN settings are not rewriting the URL for advagg JS files to point to your CDN. Most likely you need to adjust your <a href="@cdnother">CDN exceptions</a> to allow JS files from advagg.', array(
        '@cdnother' => url('admin/settings/cdn/other'),
      )),
    );
  }
  if (isset($data_cdn_css)) {
    if ($data_cdn_css->code != 200 && $mode == CDN_MODE_BASIC && (!empty($data_cdn_css->headers['X-AdvAgg']) || !empty($data_cdn_css->data) && strpos($data_cdn_css->data, '<!-- advagg_missing_fast404 -->') !== FALSE)) {
      $ret['advagg_async_generation_css_cdn'] = array(
        'title' => $t('Adv CSS/JS Agg - CDN Async Mode'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('CSS CDN'),
        'description' => $t('Requests through the CDN are getting back to advagg for CSS files.'),
      );
    }
    else {
      $cdn_failed = TRUE;
      $ret['advagg_async_generation_css_cdn'] = array(
        'title' => $t('Adv CSS/JS Agg - CDN Async Mode'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('CSS CDN Issue'),
        'description' => $t('Check your <a href="@cdn">CDN settings</a>; CSS request is not coming back when routed through the CDN. ', array(
          '@cdn' => url('admin/settings/cdn'),
        )) . $extra_404,
      );
    }
  }
  if (isset($data_cdn_js)) {
    if ($data_cdn_js->code != 200 && $mode == CDN_MODE_BASIC && (!empty($data_cdn_js->headers['X-AdvAgg']) || !empty($data_cdn_js->data) && strpos($data_cdn_js->data, '<!-- advagg_missing_fast404 -->') !== FALSE)) {
      $ret['advagg_async_generation_js_cdn'] = array(
        'title' => $t('Adv CSS/JS Agg - CDN Async Mode'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('JS CDN'),
        'description' => $t('Requests through the CDN are getting back to advagg for JS files.'),
      );
    }
    else {
      $cdn_failed = TRUE;
      $ret['advagg_async_generation_js_cdn'] = array(
        'title' => $t('Adv CSS/JS Agg - CDN Async Mode'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('JS CDN Issue'),
        'description' => $t('Check your <a href="@cdn">CDN settings</a>; JS request is not coming back when routed through the CDN. ', array(
          '@cdn' => url('admin/settings/cdn'),
        )) . $extra_404,
      );
    }
  }

  // Check normal response and set async variable accordingly.
  if ($data_local->code != 200 && (!empty($data_local->headers['X-AdvAgg']) || strpos($data_local->data, '<!-- advagg_missing_fast404 -->') !== FALSE)) {

    // Hook menu works.
    if ($async == -1 && !$cdn_failed) {
      variable_set('advagg_async_generation', TRUE);
      $ret['advagg_async_generation'] = array(
        'title' => $t('Adv CSS/JS Agg - Asynchronous Mode'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('Is now set to TRUE.'),
      );
    }
    if ($async == 0) {
      $ret['advagg_async_generation'] = array(
        'title' => $t('Adv CSS/JS Agg - Asynchronous Mode'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('Could be changed to TRUE.'),
      );
    }
    if ($async == 1) {
      $ret['advagg_async_generation'] = array(
        'title' => $t('Adv CSS/JS Agg - Asynchronous Mode'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('Already set to TRUE'),
      );
    }
    if ($cdn_failed) {
      $ret['advagg_async_generation']['description'] = ' ' . $t('Be careful about enabling Asynchronous Mode. Your CDN is not forwarding 404s back to advagg missing file handling functions. There is a chance a user could have a broken experience.');
    }
  }
  else {

    // Set to FALSE if not.
    if ($async != 0) {
      variable_set('advagg_async_generation', FALSE);
    }

    // Files htaccess check. Make sure RewriteEngine is not off.
    $directory = file_directory_path();
    if (is_file($directory . '/.htaccess')) {
      $file = file_get_contents($directory . '/.htaccess');
      if (stripos($file, "RewriteEngine off") !== FALSE) {
        $ret['advagg_htaccess'] = array(
          'title' => $t('Adv CSS/JS Agg - Files Dir htaccess'),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('The htaccess file in %dir is non standard.', array(
            '%dir' => $directory,
          )),
          'description' => $t('You need to modify your htaccess file in your files directory. See <a href="@url">this link</a> for more info.', array(
            '@url' => 'http://drupal.org/node/1171244#comment-4770544',
          )),
        );
      }
    }

    // Build error message.
    if (empty($ret['advagg_htaccess'])) {
      $ret['advagg_async_generation'] = array(
        'title' => $t('Adv CSS/JS Agg - Asynchronous Mode'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('Set to FALSE.'),
        'description' => $t('Check to see if you have fast 404s, if so create an exception for this module. The <a href="@readme">readme file</a> explains what needs to be changed. You can try flushing the menu cache as well. ', array(
          '@readme' => url($readme),
        )) . $extra_404,
      );
    }
  }

  // Set socket timeout relative to local round trip.
  $timeout = variable_get('advagg_socket_timeout', -1);
  $new_time = ceil(($time_local['time'] + 51) / 1000);
  if ($async) {
    if ($timeout == -1 || $timeout != $new_time) {
      variable_set('advagg_socket_timeout', $new_time);
      $ret['advagg_socket_timeout'] = array(
        'title' => $t('Adv CSS/JS Agg - Socket Timeout'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('Set to %time seconds. Raw timer: %raw', array(
          '%time' => $new_time,
          '%raw' => $time_local['time'],
        )),
      );
    }
    else {
      $ret['advagg_socket_timeout'] = array(
        'title' => $t('Adv CSS/JS Agg - Socket Timeout'),
        'severity' => REQUIREMENT_OK,
        'value' => $t('Already set to %time seconds. Raw timer: %raw', array(
          '%time' => $new_time,
          '%raw' => $time_local['time'],
        )),
      );
    }
  }
  return $ret;
}