1
|
|
|
<?php |
2
|
|
|
namespace Checkdomain\Comodo; |
3
|
|
|
|
4
|
|
|
use Checkdomain\Comodo\Model\Account; |
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class CommunicationAdapter |
9
|
|
|
* |
10
|
|
|
* Manages the communication with comodo |
11
|
|
|
*/ |
12
|
|
|
class CommunicationAdapter |
13
|
|
|
{ |
14
|
|
|
const RESPONSE_NEW_LINE = 0; |
15
|
|
|
const RESPONSE_URL_ENCODED = 1; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Client |
19
|
|
|
*/ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Account |
24
|
|
|
*/ |
25
|
|
|
protected $account; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Checkdomain\Comodo\Model\Account $account |
29
|
|
|
* |
30
|
|
|
* @return CommunicationAdapter |
31
|
|
|
*/ |
32
|
|
|
public function setAccount($account) |
33
|
|
|
{ |
34
|
|
|
$this->account = $account; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return \Checkdomain\Comodo\Model\Account |
41
|
|
|
*/ |
42
|
|
|
public function getAccount() |
43
|
|
|
{ |
44
|
|
|
return $this->account; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructs a communication adapter with an account |
49
|
|
|
* |
50
|
|
|
* @param Account $account |
51
|
|
|
*/ |
52
|
|
|
public function __construct(Account $account = null) |
53
|
|
|
{ |
54
|
|
|
$this->account = $account; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \GuzzleHttp\Client $client |
59
|
|
|
* |
60
|
|
|
* @return CommunicationAdapter |
61
|
|
|
*/ |
62
|
|
|
public function setClient($client) |
63
|
|
|
{ |
64
|
|
|
$this->client = $client; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \GuzzleHttp\Client |
71
|
|
|
*/ |
72
|
|
|
public function getClient() |
73
|
|
|
{ |
74
|
|
|
if ($this->client === null) { |
75
|
|
|
$this->client = new Client(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->client; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sends a query to the provided url and return the response body. |
83
|
|
|
* |
84
|
|
|
* @param string $url |
85
|
|
|
* @param array $params |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function sendToWebsite($url, array $params) |
90
|
|
|
{ |
91
|
|
|
// Sending request |
92
|
|
|
$client = $this->getClient(); |
93
|
|
|
$response = $client->request('POST', $url, [ |
94
|
|
|
'query' => http_build_query($params, '', '&') |
95
|
|
|
]); |
96
|
|
|
|
97
|
|
|
// Getting response body |
98
|
|
|
$responseString = $response->getBody()->getContents(); |
99
|
|
|
$responseString = trim($responseString); |
100
|
|
|
|
101
|
|
|
return $responseString; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Send a request to the comodo API, and decodes the response as given |
106
|
|
|
* |
107
|
|
|
* @param string $url |
108
|
|
|
* @param array $params |
109
|
|
|
* @param int $responseType |
110
|
|
|
* @param array|null $notDecode |
111
|
|
|
* @param array $forceArray |
112
|
|
|
* |
113
|
|
|
* @return array|bool |
114
|
|
|
*/ |
115
|
|
|
public function sendToApi($url, array $params, $responseType = self::RESPONSE_NEW_LINE, array $notDecode = null, $forceArray = array()) |
116
|
|
|
{ |
117
|
|
|
$this->preSendToApiCheck(); |
118
|
|
|
|
119
|
|
|
// Merging post-data |
120
|
|
|
$fields = array(); |
121
|
|
|
$fields['loginName'] = $this->getAccount()->getLoginName(); |
122
|
|
|
$fields['loginPassword'] = $this->getAccount()->getLoginPassword(); |
123
|
|
|
$fields = array_merge($fields, $params); |
124
|
|
|
|
125
|
|
|
// Sending request |
126
|
|
|
$client = $this->getClient(); |
127
|
|
|
$response = $client->request( |
128
|
|
|
'POST', |
129
|
|
|
$url, |
130
|
|
|
['form_params' => $fields] |
131
|
|
|
); |
132
|
|
|
$query = http_build_query($params); |
133
|
|
|
|
134
|
|
|
// Getting response body |
135
|
|
|
$responseString = $response->getBody()->getContents(); |
136
|
|
|
$responseString = trim($responseString); |
137
|
|
|
|
138
|
|
|
// Decoding and returning response |
139
|
|
|
if ($responseType == self::RESPONSE_NEW_LINE) { |
140
|
|
|
return $this->decodeNewLineEncodedResponse($responseString, $query, $forceArray); |
141
|
|
|
} else { |
142
|
|
|
return $this->decodeUrlEncodedResponse($responseString, $query, $notDecode, $forceArray); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Checks, if a valid account has been provided |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
* @throws \Exception |
151
|
|
|
*/ |
152
|
|
|
protected function preSendToApiCheck() |
153
|
|
|
{ |
154
|
|
|
if ($this->getAccount() === null) { |
155
|
|
|
throw new \Exception('Please provided an account'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!$this->getAccount()->isValid()) { |
159
|
|
|
throw new \Exception('Please provided valid account'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Decodes a responseString, separated by new lines and returns an response array |
167
|
|
|
* |
168
|
|
|
* @param string $responseString |
169
|
|
|
* @param string $requestQuery |
170
|
|
|
* @param array $forceArray |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
protected function decodeNewLineEncodedResponse($responseString, $requestQuery, array $forceArray = array()) |
175
|
|
|
{ |
176
|
|
|
// Splitting response body |
177
|
|
|
$parts = explode(chr(10), $responseString); |
178
|
|
|
|
179
|
|
|
// Getting the status |
180
|
|
|
$status = trim($parts[0]); |
181
|
|
|
|
182
|
|
|
$responseArray = []; |
183
|
|
|
|
184
|
|
|
// Valid answer? |
185
|
|
|
if (is_numeric($status)) { |
186
|
|
|
// Successful? |
187
|
|
|
if ($status != '0') { |
188
|
|
|
$responseArray['errorCode'] = $status; |
189
|
|
|
$responseArray['errorMessage'] = trim($parts[1]); |
190
|
|
|
} else { |
191
|
|
|
$responseArray['errorCode'] = $status; |
192
|
|
|
|
193
|
|
|
$partCount = count($parts); |
194
|
|
|
for ($i = 1; $i < $partCount; $i++) { |
195
|
|
|
$tmp = preg_split('/[\s\t]+/', $parts[$i], 2); |
196
|
|
|
|
197
|
|
|
$key = trim($tmp[0]); |
198
|
|
|
$value = trim($tmp[1]); |
199
|
|
|
|
200
|
|
|
// if key already exists, open new array dimension |
201
|
|
|
if (isset($responseArray[$key])) { |
202
|
|
|
if (!is_array($responseArray[$key])) { |
203
|
|
|
$tmpValue = $responseArray[$key]; |
204
|
|
|
$responseArray[$key] = array(); |
205
|
|
|
$responseArray[$key][] = $tmpValue; |
206
|
|
|
$responseArray[$key][] = $value; |
207
|
|
|
} else { |
208
|
|
|
$responseArray[$key][] = $value; |
209
|
|
|
} |
210
|
|
|
} else { |
211
|
|
|
// Just save |
212
|
|
|
$responseArray[$key] = $value; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} else { |
217
|
|
|
$responseArray['errorCode'] = ''; |
218
|
|
|
$responseArray['errorMessage'] = trim($responseString); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
foreach ($forceArray as $value) { |
222
|
|
|
if (isset($responseArray[$value]) && !is_array($responseArray[$value])) { |
223
|
|
|
$responseArray[$value] = array($responseArray[$value]); |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$responseArray['responseString'] = $responseString; |
228
|
|
|
$responseArray['requestQuery'] = $requestQuery; |
229
|
|
|
|
230
|
|
|
return $responseArray; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Decodes a responseString, encoded in query-string-format and returns an response array |
235
|
|
|
* |
236
|
|
|
* @param string $responseString |
237
|
|
|
* @param string $requestQuery |
238
|
|
|
* @param array $notDecode |
239
|
|
|
* @param string[] $forceArray |
240
|
|
|
* |
241
|
|
|
* @return mixed |
242
|
|
|
*/ |
243
|
|
|
protected function decodeUrlEncodedResponse( |
244
|
|
|
$responseString, |
245
|
|
|
$requestQuery, |
246
|
|
|
array $notDecode = null, |
247
|
|
|
$forceArray = array() |
248
|
|
|
) |
249
|
|
|
{ |
250
|
|
|
if (empty($notDecode)) { |
251
|
|
|
$responseString = urldecode($responseString); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (!empty($forceArray)) { |
255
|
|
|
foreach ($forceArray as $param) { |
256
|
|
|
$responseString = str_replace($param.'=', $param.'[]=', $responseString); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
// Splitting response body |
261
|
|
|
$responseArray = []; |
262
|
|
|
parse_str($responseString, $responseArray); |
263
|
|
|
|
264
|
|
|
if (!empty($notDecode) && is_array($responseArray)) { |
265
|
|
|
foreach ($responseArray as $index => $value) { |
266
|
|
|
if (!in_array($index, $notDecode)) { |
267
|
|
|
$value = urldecode($value); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$responseArray[$index] = $value; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$responseArray['responseString'] = $responseString; |
275
|
|
|
$responseArray['requestQuery'] = $requestQuery; |
276
|
|
|
|
277
|
|
|
if (!isset($responseArray['errorCode'])) { |
278
|
|
|
$responseArray['errorCode'] = 99; |
279
|
|
|
$responseArray['errorMessage'] = $responseString; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $responseArray; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|