@@ 19-64 (lines=46) @@ | ||
16 | use Humbug\SelfUpdate\Exception\HttpRequestException; |
|
17 | use Humbug\SelfUpdate\Exception\InvalidArgumentException; |
|
18 | ||
19 | final class Sha256Strategy extends ShaStrategyAbstract |
|
20 | { |
|
21 | /** |
|
22 | * Retrieve the current version available remotely. |
|
23 | * |
|
24 | * @param Updater $updater |
|
25 | * @return string|bool |
|
26 | */ |
|
27 | public function getCurrentRemoteVersion(Updater $updater) |
|
28 | { |
|
29 | /** Switch remote request errors to HttpRequestExceptions */ |
|
30 | set_error_handler(array($updater, 'throwHttpRequestException')); |
|
31 | $version = humbug_get_contents($this->getVersionUrl()); |
|
32 | restore_error_handler(); |
|
33 | if (false === $version) { |
|
34 | throw new HttpRequestException(sprintf( |
|
35 | 'Request to URL failed: %s', $this->getVersionUrl() |
|
36 | )); |
|
37 | } |
|
38 | if (empty($version)) { |
|
39 | throw new HttpRequestException( |
|
40 | 'Version request returned empty response.' |
|
41 | ); |
|
42 | } |
|
43 | if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) { |
|
44 | throw new HttpRequestException( |
|
45 | 'Version request returned incorrectly formatted response.' |
|
46 | ); |
|
47 | } |
|
48 | ||
49 | return $matches[0]; |
|
50 | } |
|
51 | ||
52 | /** |
|
53 | * Retrieve the current version of the local phar file. |
|
54 | * |
|
55 | * @param Updater $updater |
|
56 | * @return string |
|
57 | */ |
|
58 | public function getCurrentLocalVersion(Updater $updater) |
|
59 | { |
|
60 | return hash_file('sha256', $updater->getLocalPharFile()); |
|
61 | } |
|
62 | } |
|
63 |
@@ 22-67 (lines=46) @@ | ||
19 | /** |
|
20 | * @deprecated 1.0.4 SHA-1 is increasingly susceptible to collision attacks; use SHA-256 |
|
21 | */ |
|
22 | class ShaStrategy extends ShaStrategyAbstract |
|
23 | { |
|
24 | ||
25 | /** |
|
26 | * Retrieve the current version available remotely. |
|
27 | * |
|
28 | * @param Updater $updater |
|
29 | * @return string|bool |
|
30 | */ |
|
31 | public function getCurrentRemoteVersion(Updater $updater) |
|
32 | { |
|
33 | /** Switch remote request errors to HttpRequestExceptions */ |
|
34 | set_error_handler(array($updater, 'throwHttpRequestException')); |
|
35 | $version = humbug_get_contents($this->getVersionUrl()); |
|
36 | restore_error_handler(); |
|
37 | if (false === $version) { |
|
38 | throw new HttpRequestException(sprintf( |
|
39 | 'Request to URL failed: %s', $this->getVersionUrl() |
|
40 | )); |
|
41 | } |
|
42 | if (empty($version)) { |
|
43 | throw new HttpRequestException( |
|
44 | 'Version request returned empty response.' |
|
45 | ); |
|
46 | } |
|
47 | if (!preg_match('%^[a-z0-9]{40}%', $version, $matches)) { |
|
48 | throw new HttpRequestException( |
|
49 | 'Version request returned incorrectly formatted response.' |
|
50 | ); |
|
51 | } |
|
52 | ||
53 | return $matches[0]; |
|
54 | } |
|
55 | ||
56 | /** |
|
57 | * Retrieve the current version of the local phar file. |
|
58 | * |
|
59 | * @param Updater $updater |
|
60 | * @return string |
|
61 | */ |
|
62 | public function getCurrentLocalVersion(Updater $updater) |
|
63 | { |
|
64 | return sha1_file($updater->getLocalPharFile()); |
|
65 | } |
|
66 | } |
|
67 |