1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Azine\MailgunWebhooksBundle\Services\HetrixtoolsService; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* AzineMailgunHetrixtoolsService |
7
|
|
|
* |
8
|
|
|
* This service is a wrapper for using Hetrixtools blacklist check functionality https://hetrixtools.com/. |
9
|
|
|
*/ |
10
|
|
|
class AzineMailgunHetrixtoolsService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $apiKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $blacklistIpCheckUrl; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* AzineMailgunHetrixtoolsService constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $apiKey |
26
|
|
|
* @param string $url |
27
|
|
|
*/ |
28
|
4 |
|
public function __construct($apiKey, $url) |
29
|
|
|
{ |
30
|
4 |
|
$this->apiKey = $apiKey; |
31
|
4 |
|
$this->blacklistIpCheckUrl = $url; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $ip |
36
|
|
|
* @return HetrixtoolsServiceResponse $response |
37
|
|
|
*/ |
38
|
4 |
|
public function checkIpAddressInBlacklist($ip) |
39
|
|
|
{ |
40
|
4 |
|
$url = $this->prepareBlacklistIpCheckUrl($ip); |
41
|
|
|
|
42
|
1 |
|
$jsonResponse = $this->executeCheck($url); |
43
|
1 |
|
$hetrixtoolsServiceResponse = HetrixtoolsServiceResponse::fromJson($jsonResponse); |
44
|
|
|
|
45
|
1 |
|
return $hetrixtoolsServiceResponse; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* sends a get request to the given url |
50
|
|
|
* |
51
|
|
|
* @param string $url |
52
|
|
|
* @return string $response| null |
53
|
|
|
*/ |
54
|
|
|
protected function executeCheck($url) |
55
|
|
|
{ |
56
|
|
|
$ch = curl_init($url); |
57
|
|
|
|
58
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
59
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
60
|
|
|
|
61
|
|
|
$server_output = curl_exec($ch); |
62
|
|
|
$info = curl_getinfo($ch); |
63
|
|
|
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
64
|
|
|
curl_close($ch); |
65
|
|
|
|
66
|
|
|
if($info['http_code'] != 200){ |
67
|
|
|
|
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$response = substr($server_output, $header_size); |
72
|
|
|
|
73
|
|
|
return $response; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $ip |
78
|
|
|
* @throws \InvalidArgumentException |
79
|
|
|
* @return string $url |
80
|
|
|
*/ |
81
|
4 |
|
private function prepareBlacklistIpCheckUrl($ip) |
82
|
|
|
{ |
83
|
4 |
|
if ($ip == null || !filter_var($ip, FILTER_VALIDATE_IP)) { |
84
|
|
|
|
85
|
2 |
|
throw new \InvalidArgumentException('Given Ip address is invalid'); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
if($this->apiKey == null){ |
89
|
|
|
|
90
|
1 |
|
throw new \InvalidArgumentException('Api key for Hetrixtools blacklist check service is not set'); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$url = str_replace('<API_TOKEN>', $this->apiKey, $this->blacklistIpCheckUrl); |
94
|
1 |
|
$url = str_replace('<IP_ADDRESS>', $ip, $url); |
95
|
|
|
|
96
|
1 |
|
return $url; |
97
|
|
|
} |
98
|
|
|
} |