| @@ 19-83 (lines=65) @@ | ||
| 16 | use Humbug\SelfUpdate\Exception\HttpRequestException; |
|
| 17 | use Humbug\SelfUpdate\Exception\InvalidArgumentException; |
|
| 18 | ||
| 19 | final class Sha256Strategy extends ShaStrategyAbstract |
|
| 20 | { |
|
| 21 | /** |
|
| 22 | * Download the remote Phar file. |
|
| 23 | * |
|
| 24 | * @param Updater $updater |
|
| 25 | * @return void |
|
| 26 | */ |
|
| 27 | public function download(Updater $updater) |
|
| 28 | { |
|
| 29 | parent::download($updater); |
|
| 30 | ||
| 31 | $tmpVersion = hash_file('sha256', $updater->getTempPharFile()); |
|
| 32 | ||
| 33 | if ($tmpVersion !== $updater->getNewVersion()) { |
|
| 34 | throw new HttpRequestException(sprintf( |
|
| 35 | 'Download file appears to be corrupted or outdated. The file ' |
|
| 36 | . 'received does not have the expected SHA-256 hash: %s.', |
|
| 37 | $updater->getNewVersion() |
|
| 38 | )); |
|
| 39 | } |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Retrieve the current version available remotely. |
|
| 44 | * |
|
| 45 | * @param Updater $updater |
|
| 46 | * @return string|bool |
|
| 47 | */ |
|
| 48 | public function getCurrentRemoteVersion(Updater $updater) |
|
| 49 | { |
|
| 50 | /** Switch remote request errors to HttpRequestExceptions */ |
|
| 51 | set_error_handler(array($updater, 'throwHttpRequestException')); |
|
| 52 | $version = file_get_contents($this->getVersionUrl()); |
|
| 53 | restore_error_handler(); |
|
| 54 | if (false === $version) { |
|
| 55 | throw new HttpRequestException(sprintf( |
|
| 56 | 'Request to URL failed: %s', $this->getVersionUrl() |
|
| 57 | )); |
|
| 58 | } |
|
| 59 | if (empty($version)) { |
|
| 60 | throw new HttpRequestException( |
|
| 61 | 'Version request returned empty response.' |
|
| 62 | ); |
|
| 63 | } |
|
| 64 | if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) { |
|
| 65 | throw new HttpRequestException( |
|
| 66 | 'Version request returned incorrectly formatted response.' |
|
| 67 | ); |
|
| 68 | } |
|
| 69 | ||
| 70 | return $matches[0]; |
|
| 71 | } |
|
| 72 | ||
| 73 | /** |
|
| 74 | * Retrieve the current version of the local phar file. |
|
| 75 | * |
|
| 76 | * @param Updater $updater |
|
| 77 | * @return string |
|
| 78 | */ |
|
| 79 | public function getCurrentLocalVersion(Updater $updater) |
|
| 80 | { |
|
| 81 | return hash_file('sha256', $updater->getLocalPharFile()); |
|
| 82 | } |
|
| 83 | } |
|
| 84 | ||
| @@ 22-86 (lines=65) @@ | ||
| 19 | /** |
|
| 20 | * @deprecated 1.0.4 SHA-1 is increasingly susceptible to collision attacks; use SHA-256 |
|
| 21 | */ |
|
| 22 | final class ShaStrategy extends ShaStrategyAbstract |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * Download the remote Phar file. |
|
| 26 | * |
|
| 27 | * @param Updater $updater |
|
| 28 | * @return void |
|
| 29 | */ |
|
| 30 | public function download(Updater $updater) |
|
| 31 | { |
|
| 32 | parent::download($updater); |
|
| 33 | ||
| 34 | $tmpVersion = sha1_file($updater->getTempPharFile()); |
|
| 35 | ||
| 36 | if ($tmpVersion !== $updater->getNewVersion()) { |
|
| 37 | throw new HttpRequestException(sprintf( |
|
| 38 | 'Download file appears to be corrupted or outdated. The file ' |
|
| 39 | . 'received does not have the expected SHA-1 hash: %s.', |
|
| 40 | $updater->getNewVersion() |
|
| 41 | )); |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||
| 45 | /** |
|
| 46 | * Retrieve the current version available remotely. |
|
| 47 | * |
|
| 48 | * @param Updater $updater |
|
| 49 | * @return string|bool |
|
| 50 | */ |
|
| 51 | public function getCurrentRemoteVersion(Updater $updater) |
|
| 52 | { |
|
| 53 | /** Switch remote request errors to HttpRequestExceptions */ |
|
| 54 | set_error_handler(array($updater, 'throwHttpRequestException')); |
|
| 55 | $version = file_get_contents($this->getVersionUrl()); |
|
| 56 | restore_error_handler(); |
|
| 57 | if (false === $version) { |
|
| 58 | throw new HttpRequestException(sprintf( |
|
| 59 | 'Request to URL failed: %s', $this->getVersionUrl() |
|
| 60 | )); |
|
| 61 | } |
|
| 62 | if (empty($version)) { |
|
| 63 | throw new HttpRequestException( |
|
| 64 | 'Version request returned empty response.' |
|
| 65 | ); |
|
| 66 | } |
|
| 67 | if (!preg_match('%^[a-z0-9]{40}%', $version, $matches)) { |
|
| 68 | throw new HttpRequestException( |
|
| 69 | 'Version request returned incorrectly formatted response.' |
|
| 70 | ); |
|
| 71 | } |
|
| 72 | ||
| 73 | return $matches[0]; |
|
| 74 | } |
|
| 75 | ||
| 76 | /** |
|
| 77 | * Retrieve the current version of the local phar file. |
|
| 78 | * |
|
| 79 | * @param Updater $updater |
|
| 80 | * @return string |
|
| 81 | */ |
|
| 82 | public function getCurrentLocalVersion(Updater $updater) |
|
| 83 | { |
|
| 84 | return sha1_file($updater->getLocalPharFile()); |
|
| 85 | } |
|
| 86 | } |
|
| 87 | ||