You are here

class RecommendationsCheck in Acquia Purge 8

Acquia Purge Recommendations.

Plugin annotation


@PurgeDiagnosticCheck(
  id = "acquia_purge_recommendations_check",
  title = @Translation("Acquia Purge Recommendations"),
  description = @Translation(""),
  dependent_queue_plugins = {},
  dependent_purger_plugins = {}
)

Hierarchy

Expanded class hierarchy of RecommendationsCheck

File

src/Plugin/Purge/DiagnosticCheck/RecommendationsCheck.php, line 27

Namespace

Drupal\acquia_purge\Plugin\Purge\DiagnosticCheck
View source
class RecommendationsCheck extends DiagnosticCheckBase implements DiagnosticCheckInterface {

  /**
   * The instantiated Cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * A config factory object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The path to Drupal's main .htaccess file in the app root.
   *
   * @var string
   */
  protected $htaccess;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The purge processors service.
   *
   * @var \Drupal\purge\Plugin\Purge\Processor\ProcessorsServiceInterface
   */
  protected $purgeProcessors;

  /**
   * The purge queuers service.
   *
   * @var \Drupal\purge\Plugin\Purge\Queuer\QueuersServiceInterface
   */
  protected $purgeQueuers;

  /**
   * The purge purgers service.
   *
   * @var \Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface
   */
  protected $purgePurgers;

  /**
   * The global Drupal settings object.
   *
   * @var \Drupal\Core\Site\Settings
   */
  protected $settings;

  /**
   * Constructs a RecommendationsCheck object.
   *
   * @param string $root
   *   The app root.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\purge\Plugin\Purge\Processor\ProcessorsServiceInterface $purge_processors
   *   The purge processors service.
   * @param \Drupal\purge\Plugin\Purge\Queuer\QueuersServiceInterface $purge_queuers
   *   The purge queuers service.
   * @param \Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface $purge_purgers
   *   The purge purgers service.
   * @param \Drupal\Core\Site\Settings $settings
   *   Drupal site settings object.
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   */
  public final function __construct($root, CacheBackendInterface $cache_backend, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, ProcessorsServiceInterface $purge_processors, QueuersServiceInterface $purge_queuers, PurgersServiceInterface $purge_purgers, Settings $settings, array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->cache = $cache_backend;
    $this->configFactory = $config_factory;
    $this->htaccess = $root . '/.htaccess';
    $this->moduleHandler = $module_handler;
    $this->purgeProcessors = $purge_processors;
    $this->purgeQueuers = $purge_queuers;
    $this->purgePurgers = $purge_purgers;
    $this->settings = $settings;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($container
      ->get('app.root'), $container
      ->get('cache.default'), $container
      ->get('config.factory'), $container
      ->get('module_handler'), $container
      ->get('purge.processors'), $container
      ->get('purge.queuers'), $container
      ->get('purge.purgers'), $container
      ->get('settings'), $configuration, $plugin_id, $plugin_definition);
  }

  /**
   * Analyze the current Drupal site for signs of applied HTTP Authentication.
   *
   * On Acquia Cloud, all requests using basic HTTP authentication will skip
   * caching and this becomes a problem when still invalidating caches using
   * Acquia Purge. Nothing will fail, but because the invalidations just succeed
   * it creates a false sense of effectiveness.
   *
   * @return bool
   *   Boolean indicating if basic auth was found.
   */
  protected function basicHttpAuthenticationFound() {
    $cid = 'acquia_purge_recommendations_basicauth';

    // Attempt to recycle a previously cached answer.
    if ($cache = $this->cache
      ->get($cid)) {
      $found = $cache->data;
    }
    else {
      $found = FALSE;

      // Test for the shield module and whether it is activated using a user
      // name. This module puts entire sites behind HTTP auth.
      if ($this->moduleHandler
        ->moduleExists('shield')) {
        if ($this->configFactory
          ->get('shield.settings')
          ->get('credentials.shield.user')) {
          $found = TRUE;
        }
      }

      // Else, wade through .htaccess for signs of active HTTP auth directives.
      if (!$found && file_exists($this->htaccess) && is_readable($this->htaccess)) {
        $handle = fopen($this->htaccess, "r");
        if ($handle) {
          while ($found == FALSE && ($line = fgets($handle)) !== FALSE) {
            $line = trim($line);
            $not_a_comment = strpos($line, '#') === FALSE;
            if ($not_a_comment && strpos($line, 'AuthType') !== FALSE) {
              $found = TRUE;
            }
            elseif ($not_a_comment && strpos($line, 'AuthName') !== FALSE) {
              $found = TRUE;
            }
            elseif ($not_a_comment && strpos($line, 'AuthUserFile') !== FALSE) {
              $found = TRUE;
            }
            elseif ($not_a_comment && strpos($line, 'Require valid-user') !== FALSE) {
              $found = TRUE;
            }
          }
          fclose($handle);
        }
      }

      // Cache the bool for at least two hours to prevent straining the system.
      $this->cache
        ->set($cid, $found, time() + 7200);
    }
    return $found;
  }

  /**
   * {@inheritdoc}
   */
  public function run() {

    // Check for the use of basic HTTP authentication.
    if ($this
      ->basicHttpAuthenticationFound()) {
      $this->recommendation = $this
        ->t('Acquia Purge detected that you are protecting your website with basic HTTP authentication. However, on Acquia Cloud all HTTP responses with access authentication deliberately MISS cache to prevent sensitive content from getting served to prying eyes. Acquia Purge cannot detect if specific parts of the site are protected or all pages, but does recommend you to temporarily disable invalidating caches if indeed your full site is protected. Please wipe Drupal\'s "default" cache bin when this warning persists after you updated your .htaccess file or uninstalled the Shield module!');
      return self::SEVERITY_WARNING;
    }

    // Issue a warning when the user forgot to add the AcquiaCloudPurger.
    if (!in_array('acquia_purge', $this->purgePurgers
      ->getPluginsEnabled())) {
      $this->recommendation = $this
        ->t("The 'Acquia Cloud' purger is not installed!");
      return self::SEVERITY_WARNING;
    }

    // The purge_queuer_url can quickly cause issues.
    if ($this->moduleHandler
      ->moduleExists('purge_queuer_url')) {
      $this->recommendation = $this
        ->t("For an optimal experience, you're recommended to not use the URLs queuer (and module) as this module creates a very high load. If you keep using it, make sure your website has only a small number of content so that the risks of using it, are contained.");
      return self::SEVERITY_WARNING;
    }

    // Test for the existence of the lateruntime and cron processors.
    if (!$this->purgeProcessors
      ->get('lateruntime') || !$this->purgeProcessors
      ->get('cron')) {
      $this->recommendation = $this
        ->t("For an optimal experience, you're recommended to enable the cron processor and the late runtime processors simultaneously. These two processors will complement each other and assure that the queue is processed as fast as possible.");
      return self::SEVERITY_WARNING;
    }

    // Test for the existence of the tags queuer, to ensure we're queuing tags!
    if (!$this->purgeQueuers
      ->get('coretags')) {
      $this->recommendation = $this
        ->t("For an optimal experience, you're recommended to enable the coretags queuer as this queues cache tags for Acquia Purge to process.");
      return self::SEVERITY_WARNING;
    }

    // All okay!
    $this->value = $this
      ->t("Nothing to recommend!");
    return self::SEVERITY_OK;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DiagnosticCheckBase::$description private property The description of the check as described in the plugin's metadata.
DiagnosticCheckBase::$recommendation protected property A recommendation matching the severity level, may contain NULL.
DiagnosticCheckBase::$severity private property The severity of the outcome of this check.
DiagnosticCheckBase::$title private property The title of the check as described in the plugin's metadata.
DiagnosticCheckBase::$value protected property Optional check outcome / value (e.g. version numbers), may contain NULL.
DiagnosticCheckBase::getDescription public function Gets the description of the check. Overrides DiagnosticCheckInterface::getDescription
DiagnosticCheckBase::getRecommendation public function Get a recommendation matching the severity level, may return NULL. Overrides DiagnosticCheckInterface::getRecommendation
DiagnosticCheckBase::getRequirementsArray public function Generates a individual Drupal-like requirements array. Overrides DiagnosticCheckInterface::getRequirementsArray
DiagnosticCheckBase::getRequirementsSeverity public function Get the severity level, expressed as a status_report severity. Overrides DiagnosticCheckInterface::getRequirementsSeverity
DiagnosticCheckBase::getSeverity public function Get the severity level. Overrides DiagnosticCheckInterface::getSeverity
DiagnosticCheckBase::getSeverityString public function Get the severity level as unprefixed string. Overrides DiagnosticCheckInterface::getSeverityString
DiagnosticCheckBase::getTitle public function Gets the title of the check. Overrides DiagnosticCheckInterface::getTitle
DiagnosticCheckBase::getValue public function Get an optional value for the check output, may return NULL. Overrides DiagnosticCheckInterface::getValue
DiagnosticCheckBase::runCheck protected function Late runtime helper to assure that ::run() got called (and only once).
DiagnosticCheckInterface::SEVERITY_ERROR constant BLOCKING severity -- Error condition; purge.purgers service cannot operate.
DiagnosticCheckInterface::SEVERITY_INFO constant Non-blocking severity -- Informational message only.
DiagnosticCheckInterface::SEVERITY_OK constant Non-blocking severity -- check successfully passed.
DiagnosticCheckInterface::SEVERITY_WARNING constant Non-blocking severity -- Warning condition; proceed but flag warning.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
RecommendationsCheck::$cache protected property The instantiated Cache backend.
RecommendationsCheck::$configFactory protected property A config factory object.
RecommendationsCheck::$htaccess protected property The path to Drupal's main .htaccess file in the app root.
RecommendationsCheck::$moduleHandler protected property The module handler.
RecommendationsCheck::$purgeProcessors protected property The purge processors service.
RecommendationsCheck::$purgePurgers protected property The purge purgers service.
RecommendationsCheck::$purgeQueuers protected property The purge queuers service.
RecommendationsCheck::$settings protected property The global Drupal settings object.
RecommendationsCheck::basicHttpAuthenticationFound protected function Analyze the current Drupal site for signs of applied HTTP Authentication.
RecommendationsCheck::create public static function Creates an instance of the plugin. Overrides DiagnosticCheckBase::create
RecommendationsCheck::run public function Perform the check and determine the severity level. Overrides DiagnosticCheckInterface::run
RecommendationsCheck::__construct final public function Constructs a RecommendationsCheck object. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.