View source
<?php
namespace Drupal\automatic_updates\Services;
use Drupal\automatic_updates\IgnoredPathsTrait;
use Drupal\automatic_updates\ProjectInfoTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Url;
use Drupal\Signify\ChecksumList;
use Drupal\Signify\FailedCheckumFilter;
use Drupal\Signify\Verifier;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Promise\EachPromise;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
class ModifiedFiles implements ModifiedFilesInterface {
use IgnoredPathsTrait;
use ProjectInfoTrait;
protected $logger;
protected $httpClient;
protected $configFactory;
public function __construct(LoggerInterface $logger, ClientInterface $http_client, ConfigFactoryInterface $config_factory) {
$this->logger = $logger;
$this->httpClient = $http_client;
$this->configFactory = $config_factory;
$project_root = drupal_get_path('module', 'automatic_updates');
require_once $project_root . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
}
public function getModifiedFiles(array $extensions = []) {
$modified_files = new \ArrayIterator();
$promises = $this
->getHashRequests($extensions);
(new EachPromise($promises, [
'concurrency' => 4,
'fulfilled' => function (array $resource) use ($modified_files) {
$this
->processHashes($resource, $modified_files);
},
'rejected' => function (RequestException $exception) {
$this
->processFailures($exception);
},
]))
->promise()
->wait();
return $modified_files;
}
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);
}
}
}
protected function processFailures(RequestException $exception) {
watchdog_exception('automatic_updates', $exception, NULL, [], RfcLogLevel::INFO);
if ($exception
->getCode() !== 404) {
throw $exception;
}
}
protected function getHashRequests(array $extensions) {
foreach ($extensions as $info) {
if (!$info['version']) {
continue;
}
$url = $this
->buildUrl($info);
(yield $this
->getPromise($url, $info));
}
}
protected function getPromise($url, array $info) {
return $this->httpClient
->requestAsync('GET', $url, [
'stream' => TRUE,
'read_timeout' => 30,
])
->then(static function (ResponseInterface $response) use ($info) {
return [
'contents' => $response
->getBody()
->getContents(),
'info' => $info,
];
});
}
protected function buildUrl(array $info) {
$version = $info['version'];
$project_name = $info['project'];
$hash_name = $this
->getHashName($info);
$uri = ltrim($this->configFactory
->get('automatic_updates.settings')
->get('hashes_uri'), '/');
return Url::fromUri("{$uri}/{$project_name}/{$version}/{$hash_name}")
->toString();
}
protected function getHashName(array $info) {
$hash_name = 'contents-sha256sums';
if ($info['packaged']) {
$hash_name .= '-packaged';
}
return $hash_name . '.csig';
}
}