You are here

protected function ModifiedFiles::processHashes in Automatic Updates 8

Process checking hashes of files from external URL.

Parameters

array $hash: An array of http response and project info.

\ArrayIterator $modified_files: The list of modified files.

Throws

\SodiumException

File

src/Services/ModifiedFiles.php, line 95

Class

ModifiedFiles
Modified files service.

Namespace

Drupal\automatic_updates\Services

Code

protected function processHashes(array $hash, \ArrayIterator $modified_files) {
  $contents = $hash['contents'];
  $info = $hash['info'];
  $directory_root = $info['install path'];
  if ($info['project'] === 'drupal') {
    $directory_root = '';
  }
  $module_path = drupal_get_path('module', 'automatic_updates');
  $key = file_get_contents($module_path . '/artifacts/keys/root.pub');
  $verifier = new Verifier($key);
  $files = $verifier
    ->verifyCsigMessage($contents);
  $checksums = new ChecksumList($files, TRUE);
  foreach (new FailedCheckumFilter($checksums, $directory_root) as $failed_checksum) {
    $file_path = implode(DIRECTORY_SEPARATOR, array_filter([
      $directory_root,
      $failed_checksum->filename,
    ]));
    if (!file_exists($file_path)) {
      $modified_files
        ->append($file_path);
      continue;
    }
    $actual_hash = @hash_file(strtolower($failed_checksum->algorithm), $file_path);
    if ($actual_hash === FALSE || empty($actual_hash) || strlen($actual_hash) < 64 || strcmp($actual_hash, $failed_checksum->hex_hash) !== 0) {
      $modified_files
        ->append($file_path);
    }
  }
}