1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mautic\CoreBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
7
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
8
|
|
|
|
9
|
|
|
class MaxMindDoNotSellDownloadHelper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @const REMOTE_DATA |
13
|
|
|
*/ |
14
|
|
|
const REMOTE_DATA = 'https://api.maxmind.com/privacy/exclusions'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $auth; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var LoggerInterface |
23
|
|
|
*/ |
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var HttpClientInterface |
28
|
|
|
*/ |
29
|
|
|
private $httpClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $listPath; |
35
|
|
|
|
36
|
|
|
public function __construct($auth, LoggerInterface $logger, HttpClientInterface $httpClient, CoreParametersHelper $coreParametersHelper) |
37
|
|
|
{ |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
$this->auth = explode(':', $auth, 2); |
|
|
|
|
40
|
|
|
$this->httpClient = $httpClient; |
41
|
|
|
$this->listPath = $coreParametersHelper->get('maxmind_do_not_sell_list_path') ?? ''; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function downloadRemoteDataStore() |
48
|
|
|
{ |
49
|
|
|
if (empty($this->getUser()) || empty($this->getPassword())) { |
50
|
|
|
$this->logger->error('Missing user ID or license key for MaxMind'); |
51
|
|
|
|
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (empty($this->listPath)) { |
56
|
|
|
$this->logger->error('Missing file path'); |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$httpClientOptions = [ |
62
|
|
|
'auth_basic' => [$this->getUser(), $this->getPassword()], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$response = $this->httpClient->request('GET', |
67
|
|
|
$this->getRemoteDataStoreDownloadUrl(), |
68
|
|
|
$httpClientOptions); |
69
|
|
|
} catch (TransportExceptionInterface $e) { |
70
|
|
|
$this->logger->error('Failed to fetch remote Do Not Sell data: '.$e->getMessage()); |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { |
77
|
|
|
$this->logger->error('Wrong status code for Do Not Sell data: '.$response->getStatusCode()); |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
$responseContent = $response->getContent(); |
82
|
|
|
} catch (\Throwable $e) { |
83
|
|
|
$this->logger->error('Failed to get content from remote Do Not Sell data: '.$e->getMessage()); |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return (bool) file_put_contents($this->getLocalDataStoreFilepath(), $responseContent); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Service URL. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getRemoteDataStoreDownloadUrl() |
97
|
|
|
{ |
98
|
|
|
return self::REMOTE_DATA; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Filepath to the local file. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
public function getLocalDataStoreFilepath() |
107
|
|
|
{ |
108
|
|
|
return $this->listPath; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function getUser() |
115
|
|
|
{ |
116
|
|
|
return $this->getAuthPart(0); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
protected function getPassword() |
123
|
|
|
{ |
124
|
|
|
return $this->getAuthPart(1); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $position |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
private function getAuthPart($position) |
133
|
|
|
{ |
134
|
|
|
if (array_key_exists($position, $this->auth)) { |
135
|
|
|
return $this->auth[$position]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return ''; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..