1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of dispositif/wikibot application (@github) |
5
|
|
|
* 2019/2020 © Philippe M. <[email protected]> |
6
|
|
|
* For the full copyright and MIT license information, please view the license file. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Application\Http; |
11
|
|
|
|
12
|
|
|
use DomainException; |
13
|
|
|
use GuzzleHttp\Client; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
class ExternHttpClient implements HttpClientInterface |
17
|
|
|
{ |
18
|
|
|
private $mapper; |
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
/** |
24
|
|
|
* @var Logger |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
private $log; |
27
|
|
|
|
28
|
|
|
public function __construct(?LoggerInterface $log = null) |
29
|
|
|
{ |
30
|
|
|
$this->log = $log; |
|
|
|
|
31
|
|
|
$this->client = new Client( |
32
|
|
|
[ |
33
|
|
|
'timeout' => 60, |
34
|
|
|
'allow_redirects' => true, |
35
|
|
|
'headers' => ['User-Agent' => getenv('USER_AGENT')], |
36
|
|
|
// 'proxy' => '192.168.16.1:10', |
37
|
|
|
] |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* import source from URL with Guzzle. |
43
|
|
|
* todo abstract + refac async request |
44
|
|
|
* |
45
|
|
|
* @param string $url |
46
|
|
|
* |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
public function getHTML(string $url): ?string |
50
|
|
|
{ |
51
|
|
|
// todo : check banned domains ? |
52
|
|
|
// todo : check DNS record => ban ? |
53
|
|
|
// todo : accept non-ascii URL ? |
54
|
|
|
// idn_to_ascii($url); |
55
|
|
|
// idn_to_ascii('teßt.com',IDNA_NONTRANSITIONAL_TO_ASCII,INTL_IDNA_VARIANT_UTS46) |
56
|
|
|
// checkdnsrr($string, "A") // check DNS record |
57
|
|
|
if (!self::isWebURL($url)) { |
58
|
|
|
throw new DomainException('URL not compatible : '.$url); |
59
|
|
|
} |
60
|
|
|
$response = $this->client->get($url); |
61
|
|
|
|
62
|
|
|
if (200 !== $response->getStatusCode()) { |
63
|
|
|
$this->log->error('HTTP error '.$response->getStatusCode().' '.$response->getReasonPhrase); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return (string)$response->getBody()->getContents() ?? ''; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function isWebURL(string $url): bool |
72
|
|
|
{ |
73
|
|
|
if (!filter_var($url, FILTER_VALIDATE_URL) || !preg_match('#^https?://#i', $url)) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|