1
|
|
|
<?php |
2
|
|
|
namespace LE_ACME2\Utilities; |
3
|
|
|
|
4
|
|
|
use LE_ACME2\Exception; |
5
|
|
|
|
6
|
|
|
class ChallengeHTTP { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @param string $domain |
10
|
|
|
* @param string $token |
11
|
|
|
* @return string |
12
|
|
|
* @throws Exception\HTTPAuthorizationInvalid |
13
|
|
|
*/ |
14
|
|
|
public static function fetch(string $domain, string $token) : string { |
15
|
|
|
|
16
|
|
|
$requestURL = 'http://' . $domain . '/.well-known/acme-challenge/' . $token; |
17
|
|
|
$handle = curl_init(); |
18
|
|
|
curl_setopt($handle, CURLOPT_URL, $requestURL); |
19
|
|
|
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true); |
20
|
|
|
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); |
21
|
|
|
|
22
|
|
|
$response = curl_exec($handle); |
23
|
|
|
|
24
|
|
|
if(curl_errno($handle)) { |
25
|
|
|
|
26
|
|
|
throw new Exception\HTTPAuthorizationInvalid( |
27
|
|
|
'Error while testing HTTP challenge for "' . $domain . '"": ' . |
28
|
|
|
$domain . '/.well-known/acme-challenge/' . $token . PHP_EOL . |
29
|
|
|
'- CURL error: ' . curl_error($handle) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
34
|
|
|
if(substr($statusCode, 0, 1) != 2) { |
35
|
|
|
throw new Exception\HTTPAuthorizationInvalid( |
36
|
|
|
'Error while testing HTTP challenge for "' . $domain . '"": ' . |
37
|
|
|
$domain . '/.well-known/acme-challenge/' . $token . |
38
|
|
|
' - unexpected status code: ' . $statusCode |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if($response === false) { |
43
|
|
|
throw new Exception\HTTPAuthorizationInvalid( |
44
|
|
|
'HTTP challenge for "' . $domain . '"": ' . |
45
|
|
|
$domain . '/.well-known/acme-challenge/' . $token . |
46
|
|
|
' tested, found invalid. CURL response: ' . var_export($response, true) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $response; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |