1 | <?php |
||||
2 | |||||
3 | namespace Azine\MailgunWebhooksBundle\Services\HetrixtoolsService; |
||||
4 | |||||
5 | use Psr\Log\LoggerInterface; |
||||
6 | |||||
7 | /** |
||||
8 | * AzineMailgunHetrixtoolsService. |
||||
9 | * |
||||
10 | * This service is a wrapper for using Hetrixtools blacklist check functionality https://hetrixtools.com/. |
||||
11 | */ |
||||
12 | class AzineMailgunHetrixtoolsService |
||||
13 | { |
||||
14 | /** |
||||
15 | * @var string |
||||
16 | */ |
||||
17 | private $apiKey; |
||||
18 | |||||
19 | /** |
||||
20 | * @var string |
||||
21 | */ |
||||
22 | private $blacklistIpCheckUrl; |
||||
23 | |||||
24 | /** |
||||
25 | * @var LoggerInterface |
||||
26 | */ |
||||
27 | private $logger; |
||||
28 | |||||
29 | /** |
||||
30 | * AzineMailgunHetrixtoolsService constructor. |
||||
31 | * |
||||
32 | * @param LoggerInterface $logger |
||||
33 | * @param string $apiKey |
||||
34 | * @param string $url |
||||
35 | */ |
||||
36 | 4 | public function __construct(LoggerInterface $logger, $apiKey, $url) |
|||
37 | { |
||||
38 | 4 | $this->apiKey = $apiKey; |
|||
39 | 4 | $this->blacklistIpCheckUrl = $url; |
|||
40 | 4 | $this->logger = $logger; |
|||
41 | 4 | } |
|||
42 | |||||
43 | /** |
||||
44 | * @param string $ip |
||||
45 | * |
||||
46 | * @return HetrixtoolsServiceResponse $response |
||||
47 | */ |
||||
48 | 4 | public function checkIpAddressInBlacklist($ip) |
|||
49 | { |
||||
50 | 4 | $url = $this->prepareBlacklistIpCheckUrl($ip); |
|||
51 | |||||
52 | 1 | $jsonResponse = $this->executeCheck($url); |
|||
53 | 1 | $hetrixtoolsServiceResponse = HetrixtoolsServiceResponse::fromJson($jsonResponse); |
|||
54 | |||||
55 | 1 | return $hetrixtoolsServiceResponse; |
|||
56 | } |
||||
57 | |||||
58 | /** |
||||
59 | * sends a get request to the given url. |
||||
60 | * |
||||
61 | * @param string $url |
||||
62 | * |
||||
63 | * @return string $response| null |
||||
64 | */ |
||||
65 | protected function executeCheck($url) |
||||
66 | { |
||||
67 | $this->logger->debug("Calling $url to check HetrixTools for SPAM-List entries."); |
||||
68 | $ch = curl_init($url); |
||||
69 | |||||
70 | curl_setopt($ch, CURLOPT_HEADER, true); |
||||
71 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
72 | |||||
73 | $server_output = curl_exec($ch); |
||||
74 | $info = curl_getinfo($ch); |
||||
75 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||||
76 | curl_close($ch); |
||||
77 | |||||
78 | $this->logger->debug('Hetrix returned: '.print_r($info, true)); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
79 | |||||
80 | if (200 != $info['http_code']) { |
||||
81 | return null; |
||||
82 | } |
||||
83 | |||||
84 | $response = substr($server_output, $header_size); |
||||
0 ignored issues
–
show
It seems like
$server_output can also be of type true ; however, parameter $string of substr() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
85 | |||||
86 | return $response; |
||||
87 | } |
||||
88 | |||||
89 | /** |
||||
90 | * @param string $ip |
||||
91 | * |
||||
92 | * @throws \InvalidArgumentException |
||||
93 | * |
||||
94 | * @return string $url |
||||
95 | */ |
||||
96 | 4 | private function prepareBlacklistIpCheckUrl($ip) |
|||
97 | { |
||||
98 | 4 | if (null == $ip || !filter_var($ip, FILTER_VALIDATE_IP)) { |
|||
99 | 2 | throw new \InvalidArgumentException('Given Ip address is invalid'); |
|||
100 | } |
||||
101 | |||||
102 | 2 | if (null == $this->apiKey) { |
|||
103 | 1 | throw new \InvalidArgumentException('Api key for Hetrixtools blacklist check service is not set'); |
|||
104 | } |
||||
105 | |||||
106 | 1 | $url = str_replace('<API_TOKEN>', $this->apiKey, $this->blacklistIpCheckUrl); |
|||
107 | 1 | $url = str_replace('<IP_ADDRESS>', $ip, $url); |
|||
108 | |||||
109 | 1 | return $url; |
|||
110 | } |
||||
111 | } |
||||
112 |