You are here

function advagg_install_fast_checks in Advanced CSS/JS Aggregation 7.2

Run various checks that are fast.

Parameters

string $phase: Can be install, update, or runtime.

Return value

array An associative array.

2 calls to advagg_install_fast_checks()
advagg_display_message_if_requirements_not_met in ./advagg.module
Display a message if there are requirement issues with AdvAgg.
advagg_requirements in ./advagg.install
Implements hook_requirements().

File

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

Code

function advagg_install_fast_checks($phase = 'runtime') {
  $requirements = array();

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

  // Always check these, independent of the current phase.
  $function_list = array(
    'rename',
  );

  // Check each function to make sure it exists.
  foreach ($function_list as $function_name) {
    if (!function_exists($function_name)) {
      $requirements['advagg_function_' . $function_name] = array(
        'title' => $t('Adv CSS/JS Agg - Function Disabled'),
        'value' => $phase === 'install' ? FALSE : $function_name,
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('<a href="!url">%name()</a> is disabled on this server. Please contact your hosting provider or server administrator and see if they can re-enable this function for you.', array(
          '!url' => 'http://php.net/' . str_replace('_', '-', $function_name),
          '%name' => $function_name,
        )),
      );
    }
  }

  // Check to see if any incompatible modules are installed.
  if (module_exists('agrcache')) {
    $requirements['advagg_module_agrcache'] = array(
      'title' => $t('Adv CSS/JS Agg - Aggregate cache module'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $phase === 'install' ? FALSE : $t('The Aggregate cache module is incompatible with AdvAgg.'),
      'description' => $t('You need to uninstall the agrcache module or uninstall AdvAgg.'),
    );
  }
  if (module_exists('bundle_aggregation')) {
    $requirements['advagg_module_bundle_aggregation'] = array(
      'title' => $t('Adv CSS/JS Agg - Bundle aggregation module'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $phase === 'install' ? FALSE : $t('The Bundle aggregation module is incompatible with AdvAgg.'),
      'description' => $t('You need to uninstall the bundle_aggregation module or uninstall AdvAgg.'),
    );
  }
  if (module_exists('core_library')) {
    $requirements['advagg_module_core_library'] = array(
      'title' => $t('Adv CSS/JS Agg - Core Library module'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $phase === 'install' ? FALSE : $t('The Core Library module is incompatible with AdvAgg.'),
      'description' => $t('You need to uninstall the core_library module or uninstall AdvAgg.'),
    );
  }

  // If not at runtime, return here.
  if ($phase !== 'runtime') {
    return $requirements;
  }

  // Make sure the advagg default values for variable_get are available.
  drupal_load('module', 'advagg');

  // Do the following checks only at runtime.
  list($css_path, $js_path) = advagg_get_root_files_dir();
  $config_path = advagg_admin_config_root_path();

  // Make sure directories are writable.
  if (!file_prepare_directory($css_path[0], FILE_CREATE_DIRECTORY + FILE_MODIFY_PERMISSIONS)) {
    $requirements['advagg_css_path_0_prepare_dir'] = array(
      'title' => $t('Adv CSS/JS Agg - CSS Path'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('CSS directory is not created or writable.'),
      'description' => $t('%path is not setup correctly.', array(
        '%path' => $css_path[0],
      )),
    );
  }
  if (!is_writable($css_path[0])) {
    $requirements['advagg_css_path_0_write'] = array(
      'title' => $t('Adv CSS/JS Agg - CSS Path'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('CSS directory is not writable.'),
      'description' => $t('%path is not setup correctly.', array(
        '%path' => $css_path[0],
      )),
    );
  }
  if (!is_readable($css_path[0])) {
    $requirements['advagg_css_path_0_read'] = array(
      'title' => $t('Adv CSS/JS Agg - CSS Path'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('CSS directory is not readable.'),
      'description' => $t('%path is not setup correctly.', array(
        '%path' => $css_path[0],
      )),
    );
  }
  $css_wrapper = file_stream_wrapper_get_instance_by_uri($css_path[0]);
  if ($css_wrapper instanceof DrupalLocalStreamWrapper) {
    if (!is_writable($css_path[1])) {
      $requirements['advagg_css_path_1_write'] = array(
        'title' => $t('Adv CSS/JS Agg - CSS Path'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('CSS directory is not writable.'),
        'description' => $t('%path is not setup correctly.', array(
          '%path' => $css_path[1],
        )),
      );
    }
    if (!is_readable($css_path[1])) {
      $requirements['advagg_css_path_1_read'] = array(
        'title' => $t('Adv CSS/JS Agg - CSS Path'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('CSS directory is not readable.'),
        'description' => $t('%path is not setup correctly.', array(
          '%path' => $css_path[1],
        )),
      );
    }
  }
  if (!file_prepare_directory($js_path[0], FILE_CREATE_DIRECTORY + FILE_MODIFY_PERMISSIONS)) {
    $requirements['advagg_js_path_0_prepare_dir'] = array(
      'title' => $t('Adv CSS/JS Agg - JS Path'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('JS directory is not created or writable.'),
      'description' => $t('%path is not setup correctly.', array(
        '%path' => $js_path[0],
      )),
    );
  }
  if (!is_writable($js_path[0])) {
    $requirements['advagg_js_path_0_write'] = array(
      'title' => $t('Adv CSS/JS Agg - JS Path'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('JS directory is not writable.'),
      'description' => $t('%path is not setup correctly.', array(
        '%path' => $js_path[0],
      )),
    );
  }
  if (!is_readable($js_path[0])) {
    $requirements['advagg_js_path_0_read'] = array(
      'title' => $t('Adv CSS/JS Agg - JS Path'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('JS directory is not readable.'),
      'description' => $t('%path is not setup correctly.', array(
        '%path' => $js_path[0],
      )),
    );
  }
  $js_wrapper = file_stream_wrapper_get_instance_by_uri($js_path[0]);
  if ($js_wrapper instanceof DrupalLocalStreamWrapper) {
    if (!is_writable($js_path[1])) {
      $requirements['advagg_js_path_1_write'] = array(
        'title' => $t('Adv CSS/JS Agg - JS Path'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('JS directory is not writable.'),
        'description' => $t('%path is not setup correctly.', array(
          '%path' => $js_path[1],
        )),
      );
    }
    if (!is_readable($js_path[1])) {
      $requirements['advagg_js_path_1_read'] = array(
        'title' => $t('Adv CSS/JS Agg - JS Path'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('JS directory is not readable.'),
        'description' => $t('%path is not setup correctly.', array(
          '%path' => $js_path[1],
        )),
      );
    }
  }
  if (!variable_get('advagg_skip_enabled_preprocess_check', ADVAGG_SKIP_ENABLED_PREPROCESS_CHECK)) {

    // Make sure variables are set correctly.
    if (!variable_get('advagg_enabled', ADVAGG_ENABLED)) {
      $requirements['advagg_not_on'] = array(
        'title' => $t('Adv CSS/JS Agg - Enabled'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('Advanced CSS/JS aggregation is disabled.'),
        'description' => $t('Go to the Advanced CSS/JS aggregation <a href="@settings">settings page</a> and enable it.', array(
          '@settings' => url($config_path . '/advagg'),
        )),
      );
    }
    if (!variable_get('preprocess_css', FALSE) || !variable_get('preprocess_js', FALSE)) {
      $requirements['advagg_core_off'] = array(
        'title' => $t('Adv CSS/JS Agg - Core Variables'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('Core CSS and/or JS aggregation is disabled.'),
        'description' => $t('"Aggregate and compress CSS files" and "Aggregate JavaScript files" on the <a href="@performance">performance page</a> should be enabled.', array(
          '@performance' => url('admin/config/development/performance', array(
            'fragment' => 'edit-bandwidth-optimization',
          )),
        )),
      );
    }
  }

  // Check that the menu router handler is working.
  // Paths will vary based on s3fs no_rewrite_cssjs setting.
  if (advagg_s3fs_evaluate_no_rewrite_cssjs(FALSE)) {

    // If using s3fs and no_rewrite_cssjs is not set, external paths are needed.
    // Use $css_path[0] and $js_path[0] since they contain the scheme.
    $menu_path_key = 0;
    $menu_css_path = trim(parse_url(file_create_url($css_path[0] . '/test.css'), PHP_URL_PATH));
    if (strpos($menu_css_path, $GLOBALS['base_path']) === 0) {
      $menu_css_path = substr($menu_css_path, strlen($GLOBALS['base_path']));
    }
    $menu_js_path = trim(parse_url(file_create_url($js_path[0] . '/test.js'), PHP_URL_PATH));
    if (strpos($menu_js_path, $GLOBALS['base_path']) === 0) {
      $menu_js_path = substr($menu_js_path, strlen($GLOBALS['base_path']));
    }
  }
  else {

    // Determine paths if not using s3fs, or no_rewrite_cssjs is set.
    // Use $css_path[1] and $js_path[1] since they are without schemes.
    $menu_path_key = 1;
    $menu_css_path = $css_path[1] . '/test.css';
    $menu_js_path = $js_path[1] . '/test.js';
  }

  // Use the paths set above to check menu router handler.
  $advagg_async_generation_menu_issue = FALSE;
  if (!file_uri_scheme($css_path[$menu_path_key])) {
    $item_css = menu_get_item($menu_css_path);
    if (empty($item_css['page_callback']) || strpos($item_css['page_callback'], 'advagg') === FALSE) {
      $advagg_async_generation_menu_issue = TRUE;
    }
  }
  if (!file_uri_scheme($js_path[$menu_path_key])) {
    $item_js = menu_get_item($menu_js_path);
    if (empty($item_js['page_callback']) || strpos($item_js['page_callback'], 'advagg') === FALSE) {
      $advagg_async_generation_menu_issue = TRUE;
    }
  }
  if ($advagg_async_generation_menu_issue) {
    $requirements['advagg_async_generation_menu_issue'] = array(
      'title' => $t('Adv CSS/JS Agg - Async Mode'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('Flush your caches.'),
      'description' => $t('You need to flush your menu cache. This can be done at the top of the <a href="@performance">performance page</a>; under "Clear cache" press the "Clear all caches" button.', array(
        '@performance' => url('admin/config/development/performance'),
      )),
    );
  }

  // Make hook_element_info_alter worked.
  $styles_info = element_info('styles');
  $scripts_info = element_info('scripts');
  if (empty($styles_info['#pre_render']) || !is_array($styles_info['#pre_render']) || !in_array('advagg_modify_css_pre_render', $styles_info['#pre_render']) || empty($scripts_info['#pre_render']) || !is_array($scripts_info['#pre_render']) || !in_array('advagg_modify_js_pre_render', $scripts_info['#pre_render'])) {
    if (!empty($scripts_info['#group_callback']) && $scripts_info['#group_callback'] === 'omega_group_js') {
      $requirements['advagg_hook_element_info_alter_omega'] = array(
        'title' => $t('Adv CSS/JS Agg - omega theme patch'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('Omega theme needs a patch.'),
        'description' => $t('The <a href="@patch">patch</a> can be found in <a href="@issue">this issue</a>', array(
          '@patch' => 'https://www.drupal.org/files/issues/omega-2492461-1-smarter-element-info-alter.patch',
          '@issue' => 'https://www.drupal.org/node/2492461',
        )),
      );
    }
    else {
      $requirements['advagg_hook_element_info_alter'] = array(
        'title' => $t('Adv CSS/JS Agg - element_info'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('Flush your caches.'),
        'description' => $t('You need to flush your cache_bootstrap cache bin as advagg_hook_element_info_alter() is not working correctly. This can be done near the top of the <a href="@performance">performance page</a> under Clear cache. <br>Styles: <p><code>@styles</code></p>Scripts:<p><code>@scripts</code></p>', array(
          '@performance' => url('admin/config/development/performance'),
          '@styles' => print_r($styles_info, TRUE),
          '@scripts' => print_r($scripts_info, TRUE),
        )),
      );
    }
  }

  // Make sure some modules have the correct patches installed.
  if (module_exists('css_emimage')) {
    $file_path = drupal_get_path('module', 'css_emimage');
    if (!file_exists($file_path . '/css_emimage.advagg.inc')) {
      $requirements['advagg_module_css_emimage_patch'] = array(
        'title' => $t('Adv CSS/JS Agg - CSS Embedded Images module'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('The CSS Embedded Images module needs to be updated.'),
        'description' => $t('<a href="@link">CSS Embedded Images</a> needs to be upgraded to version 1.3 or higher, the currently installed version is incompatible with AdvAgg.', array(
          '@link' => 'http://drupal.org/project/css_emimage',
        )),
      );
    }
  }
  if (module_exists('labjs')) {
    if (!function_exists('labjs_advagg_modify_js_pre_render_alter')) {
      $requirements['advagg_module_labjs_patch'] = array(
        'title' => $t('Adv CSS/JS Agg - LAB.js module'),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('The LAB.js module needs a patch to be compatible with AdvAgg.'),
        'description' => $t('You need to install the latest patch in <a href="@link">this issue</a>.', array(
          '@link' => 'http://drupal.org/node/1977122',
        )),
      );
    }
  }

  // Adjust some modules settings.
  $search404_ignore_query = variable_get('search404_ignore_query', 'gif jpg jpeg bmp png');
  if (module_exists('search404') && (strpos($search404_ignore_query, 'css') === FALSE || strpos($search404_ignore_query, 'js') === FALSE)) {
    $added_ext = array();
    if (strpos($search404_ignore_query, 'css') === FALSE) {
      $added_ext[] = 'css';
    }
    if (strpos($search404_ignore_query, 'js') === FALSE) {
      $added_ext[] = 'js';
    }
    $requirements['advagg_search404_module'] = array(
      'title' => $t('Adv CSS/JS Agg - Search 404 Settings'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('HTTP requests to advagg for css/js files may not be generating correctly.'),
      'description' => $t('The Search 404 module is enabled. You need to change the <code>search404_ignore_query</code> setting, also known as "Extensions to abort search" so advagg will work. Go to the <a href="@config">Search 404 settings</a> page under the "Advanced settings" fieldgroup and look for the "Extensions to abort search" setting. Add <code>@code</code> to the string that looks like this: <p><code>@old</code></p> so it will then look like this: <p><code>@new</code></p>', array(
        '@config' => url('admin/config/search/search404'),
        '@code' => ' ' . implode(' ', $added_ext),
        '@old' => $search404_ignore_query,
        '@new' => trim($search404_ignore_query) . ' ' . implode(' ', $added_ext),
      )),
    );
  }
  if (module_exists('securepages') && variable_get('securepages_enable', 0) && function_exists('securepages_match')) {
    $test_css = securepages_match($css_path[1] . '/test.css');
    $test_js = securepages_match($js_path[1] . '/test.js');
    if ($test_css === 0 || $test_js === 0) {
      $added_paths = array();
      $securepages_ignore = variable_get('securepages_ignore', '');
      if (strpos($securepages_ignore, $css_path[1]) === FALSE) {
        $added_paths[] = $css_path[1] . '/*';
      }
      if (strpos($securepages_ignore, $js_path[1]) === FALSE) {
        $added_paths[] = $js_path[1] . '/*';
      }
      if (!empty($added_paths)) {
        $requirements['advagg_securepages_module'] = array(
          'title' => $t('Adv CSS/JS Agg - Secure Pages Settings'),
          'severity' => REQUIREMENT_ERROR,
          'value' => $t('Requests to advagg for css/js files may be getting redirected to http on a https page.'),
        );
        if (!empty($securepages_ignore)) {
          $requirements['advagg_securepages_module']['description'] = $t('The Secure Pages module is enabled. You need to change the <code>securepages_ignore</code> setting, also known as "Ignore pages" so advagg will work. Go to the <a href="@config">Secure Pages settings</a> page and under the "Ignore pages" setting add <p><code>!code</code></p> to the string that looks like this: <p><code>@old</code></p> so it will then look like this: <p><code>@old<br>!code</code></p>', array(
            '@config' => url('admin/config/system/securepages', array(
              'fragment' => 'edit-securepages-ignore',
            )),
            '!code' => implode("\n<br>", $added_paths),
            '@old' => trim($securepages_ignore),
          ));
        }
        else {
          $requirements['advagg_securepages_module']['description'] = $t('The Secure Pages module is enabled. You need to change the <code>securepages_ignore</code> setting, also known as "Ignore pages" so advagg will work. Go to the <a href="@config">Secure Pages settings</a> page and under the "Ignore pages" setting add <p><code>!code</code></p> to that section.', array(
            '@config' => url('admin/config/system/securepages', array(
              'fragment' => 'edit-securepages-ignore',
            )),
            '!code' => implode("\n<br>", $added_paths),
          ));
        }
      }
    }
  }

  // Check that https is correct.
  if (empty($GLOBALS['is_https']) && (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on' || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' || isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] === 'on')) {
    $requirements['advagg_is_https_check'] = array(
      'title' => $t('Adv CSS/JS Agg - HTTPS'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('The core global $is_https is not TRUE.'),
      'description' => $t('You need to add in this logic near the top your settings.php file: <pre>@code</pre>', array(
        '@code' => 'if ((isset($_SERVER[\'HTTPS\']) && strtolower($_SERVER[\'HTTPS\']) == \'on\')
  || (isset($_SERVER[\'HTTP_X_FORWARDED_PROTO\']) && $_SERVER[\'HTTP_X_FORWARDED_PROTO\'] == \'https\')
  || (isset($_SERVER[\'HTTP_HTTPS\']) && $_SERVER[\'HTTP_HTTPS\'] == \'on\')
) {
  $_SERVER[\'HTTPS\'] = \'on\';
}',
      )),
    );
  }

  // Make sure $base_url is correct.
  // Site is https but $base_url starts with http://.
  if (!empty($GLOBALS['is_https']) && strpos($GLOBALS['base_url'], 'http://') === 0) {
    $requirements['advagg_is_https_check'] = array(
      'title' => $t('Adv CSS/JS Agg - $base_url'),
      'severity' => REQUIREMENT_WARNING,
      'value' => $t('The core global $base_url\'s scheme is incorrect.'),
      'description' => $t('You need to add in this logic near the bottom of your settings.php file: <p><pre>@code</pre></p>', array(
        '@code' => 'if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == "on" && isset($base_url)) {
  $base_url = str_replace("http://", "https://", $base_url);
}',
      )),
    );
  }
  return $requirements;
}