You are here

protected function RecommendationsCheck::basicHttpAuthenticationFound in Acquia Purge 8

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 value

bool Boolean indicating if basic auth was found.

1 call to RecommendationsCheck::basicHttpAuthenticationFound()
RecommendationsCheck::run in src/Plugin/Purge/DiagnosticCheck/RecommendationsCheck.php
Perform the check and determine the severity level.

File

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

Class

RecommendationsCheck
Acquia Purge Recommendations.

Namespace

Drupal\acquia_purge\Plugin\Purge\DiagnosticCheck

Code

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;
}