PluginProbe ʕ •ᴥ•ʔ
Transferito: WP Migration / 13.1.0
Transferito: WP Migration v13.1.0
trunk 11.4.0 12.0.0 13.1.0 14.0.0 14.0.11 14.0.7 14.1.0 14.1.1 14.1.2 14.1.3 14.1.4
transferito / src / Models / Core / Api.php
transferito / src / Models / Core Last commit date
Api.php 11 months ago Config.php 11 months ago
Api.php
376 lines
1 <?php
2
3 namespace Transferito\Models\Core;
4
5 use Transferito\Models\Core\Config;
6
7 class Api {
8
9 private $savedAPI;
10 private $planDetail;
11 private $statusUrl;
12 private $createMigrationFreeURL;
13 private $createMigrationPaidURL;
14 private $startMigrationFreeURL;
15 private $startMigrationPaidURL;
16 private $directDownloadCheckURL;
17 private $envCheckURL;
18 private $completeUploadURL;
19 private $sslCheckURL;
20 private $errorReportingURL;
21 private $serverCheckURL;
22 private $cPanelAuthURL;
23
24 private $dbConnectionTestURL;
25 private $hostingGuideRequestURL;
26 private $telemetryURL;
27
28 private $serverVerificationURL;
29
30 /**
31 * Keys
32 */
33 private $publicKey;
34 private $secretKey;
35
36 private $freeUser;
37 private $maxSizeExceeded;
38
39 public function __construct()
40 {
41 $settings = get_option('transferito_settings_option');
42 $this->savedAPI = Config::getEndpoint('information');
43 $this->planDetail = Config::getEndpoint('detail');
44 $this->statusUrl = Config::getEndpoint('status');
45 $this->createMigrationFreeURL = Config::getEndpoint('free-migration/create');
46 $this->createMigrationPaidURL = Config::getEndpoint('paid-migration/create');
47 $this->startMigrationFreeURL = Config::getEndpoint('free-migration/start');
48 $this->startMigrationPaidURL = Config::getEndpoint('paid-migration/start');
49 $this->envCheckURL = Config::getEndpoint('environment-check');
50
51 $this->completeUploadURL = Config::getEndpoint('upload/complete');
52 $this->sslCheckURL = Config::getEndpoint('ssl/check');
53 $this->errorReportingURL = Config::getEndpoint('error/reporting');
54 $this->cPanelAuthURL = Config::getEndpoint('domain/check');
55 $this->serverVerificationURL = Config::getEndpoint('verification');
56
57 $this->hostingGuideRequestURL = Config::getEndpoint('request/hosting-guide');
58 $this->telemetryURL = Config::getEndpoint('telemetry');
59
60
61 // v2 Checks
62 $this->dbConnectionTestURL = Config::getEndpoint('check/database');
63 $this->directDownloadCheckURL = Config::getEndpoint('check/download');
64 $this->serverCheckURL = Config::getEndpoint('check/server');
65
66 /**
67 * API Keys
68 */
69 $publicKey = isset($settings['public_transferito_key']) ? $settings['public_transferito_key'] : '';
70 $secretKey = isset($settings['secret_transferito_key']) ? $settings['secret_transferito_key'] : '';
71 $this->publicKey = sanitize_text_field($publicKey);
72 $this->secretKey = sanitize_text_field($secretKey);
73
74 /**
75 * If user doesn't have API keys set as a free user
76 */
77 $this->freeUser = !($this->publicKey && $this->secretKey);
78 }
79
80 public function setFreeUser()
81 {
82 $this->freeUser = true;
83 }
84
85 public function setMaxSizeExceeded($maxSizeExceeded)
86 {
87 $this->maxSizeExceeded = $maxSizeExceeded;
88 }
89
90 public function createMigration(array $transferDetail)
91 {
92 $migrationUrl = $this->freeUser ? $this->createMigrationFreeURL : $this->createMigrationPaidURL;
93 return $this->post($migrationUrl, $transferDetail);
94 }
95
96 public function startMigration(array $transferDetail)
97 {
98 $migrationUrl = $this->freeUser ? $this->startMigrationFreeURL : $this->startMigrationPaidURL;
99 return $this->post($migrationUrl, $transferDetail);
100 }
101
102 public function getStatus($token)
103 {
104 $transferStatus = $this->statusUrl . '/' . $token;
105 return $this->get($transferStatus);
106 }
107
108 public function getMaxExceeded()
109 {
110 return $this->maxSizeExceeded;
111 }
112
113 public function planInformation($apiKeys)
114 {
115 return $this->post(
116 $this->planDetail,
117 [
118 'publicKey' => $apiKeys['publicTransferito_APIKey'],
119 'secretKey' => $apiKeys['secretTransferito_APIKey'],
120 ],
121 true,
122 true
123 );
124 }
125
126 public function canFindSite()
127 {
128 return $this->post($this->envCheckURL, [ 'url' => TRANSFERITO_UPLOAD_URL ], true);
129 }
130
131 public function directDownloadCheck(array $migrationInfo)
132 {
133 return $this->post($this->directDownloadCheckURL, $migrationInfo, true);
134 }
135
136 public function sslCheck()
137 {
138 return $this->get($this->sslCheckURL, true);
139 }
140
141 public function failedMigration($reason)
142 {
143 $errorInformation = buildErrorReporting($reason);
144 return $this->post($this->errorReportingURL, $errorInformation, true);
145 }
146
147 public function checkDestinationServerRequirements($migrationInfo)
148 {
149 return $this->post($this->serverCheckURL, $migrationInfo, true);
150 }
151
152 public function cPanelAuth(array $cPanelAuthInfo)
153 {
154 return $this->post($this->cPanelAuthURL, $cPanelAuthInfo, true);
155 }
156
157 public function databaseValidation(array $databaseDetails)
158 {
159 return $this->post($this->dbConnectionTestURL, $databaseDetails);
160 }
161
162 public function completeUpload(array $uploadInfo)
163 {
164 return $this->post($this->completeUploadURL, $uploadInfo, true);
165 }
166
167 public function hostingGuideRequest(array $guideRequestInfo) {
168 return $this->post($this->hostingGuideRequestURL, $guideRequestInfo);
169 }
170
171 public function pushTelemetry(array $telemetry) {
172 return $this->post($this->telemetryURL, $telemetry);
173 }
174
175 public function getSavedDetail()
176 {
177 $result = array(
178 'savedFTP' => [],
179 'savedDBS' => []
180 );
181
182 /**
183 * Call api - to get saved details
184 */
185 $savedDetail = $this->post($this->savedAPI, array());
186
187 /**
188 * If the response is success
189 * Then create the result array
190 */
191 if ($savedDetail['code'] === 200) {
192 $response = $savedDetail['message'];
193 $result = array(
194 'savedFTP' => isset($response->newFTP) ? $response->newFTP : [],
195 'savedDBs' => isset($response->database) ? $response->database : []
196 );
197 }
198
199 return $result;
200 }
201
202 public function validateServerConnection($url, array $connectionDetails)
203 {
204 return $this->post($url, $connectionDetails);
205 }
206
207 public function createVerificationRequest()
208 {
209 return $this->post($this->serverVerificationURL, []);
210 }
211
212 public function cPanelAvailabilityCheck($domain)
213 {
214 $ch = curl_init();
215 curl_setopt($ch, CURLOPT_URL, $domain);
216 curl_setopt($ch, CURLOPT_HEADER, true);
217 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
218 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
219 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
220 curl_setopt($ch, CURLOPT_TIMEOUT, 15); //timeout in seconds
221
222 $result = curl_exec($ch);
223
224 $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
225 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
226
227 return array(
228 'message' => $result,
229 'url' => $url,
230 'code' => $httpCode
231 );
232 }
233
234 private function getRequestHeaders()
235 {
236 return array(
237 'pKey' => $this->publicKey,
238 'sKey' => $this->secretKey,
239 'platform' => 'wp_plugin',
240 'token' => wp_create_nonce('wp_plugin')
241 );
242 }
243
244 private function stringifyHeaders($additionalHeaders = [])
245 {
246 $headers = array_merge($this->getRequestHeaders(), $additionalHeaders);
247 $updatedHeaders = [];
248 foreach ($headers as $key => $headerValue) {
249 $updatedHeaders[] = $key . ':' . $headerValue;
250 }
251 return $updatedHeaders;
252 }
253
254 private function post($url, $body, $returnResultProperty = false, $overrideAPIKeys = false)
255 {
256 $useFallback = get_transient('transferito_request_fallback');
257
258 /**
259 * Default to use PHP cURL
260 */
261 if ($useFallback) {
262 return $this->curlRequest($url, $returnResultProperty, $body, 'POST');
263 } else {
264 /**
265 * Set the headers as a variable
266 */
267 $headers = $this->getRequestHeaders();
268
269 /**
270 * Override the API Keys
271 * The body must have the array elements publicKey & secretKey
272 */
273 if ($overrideAPIKeys) {
274 $headers['pKey'] = $body['publicKey'];
275 $headers['sKey'] = $body['secretKey'];
276 }
277
278 /**
279 * Call the endpoint
280 */
281 $response = wp_remote_post($url, array(
282 'method' => 'POST',
283 'timeout' => 15000,
284 'blocking' => true,
285 'body' => $body,
286 'headers' => $headers
287 ));
288 return $this->handleWPResponse($response, $returnResultProperty);
289 }
290 }
291
292 private function get($url, $returnResultProperty = false, $rawResult = false)
293 {
294 $useFallback = get_transient('transferito_request_fallback');
295
296 /**
297 * Default to use PHP cURL
298 */
299 if ($useFallback) {
300 return $this->curlRequest($url, $returnResultProperty);
301 } else {
302 $response = wp_remote_get($url, array(
303 'headers' => $this->getRequestHeaders()
304 ));
305 return $this->handleWPResponse($response, $returnResultProperty, $rawResult);
306 }
307 }
308
309 private function handleWPResponse($response, $returnResultProperty = false, $rawResult = false)
310 {
311 /**
312 *
313 */
314 $httpCode = wp_remote_retrieve_response_code($response);
315
316 /**
317 * Check on the result of the transaction
318 */
319 if (is_wp_error($response)) {
320 $responseMessage = $response->get_error_message();
321 } else {
322 $result = wp_remote_retrieve_body($response);
323 $jsonResponse = json_decode($result);
324 $responseMessage = ($returnResultProperty) ? $jsonResponse->result : $jsonResponse;
325
326 if ($rawResult) {
327 $responseMessage = $result;
328 }
329 }
330
331 // close the session
332 return array(
333 'message' => $responseMessage,
334 'code' => $httpCode,
335 );
336 }
337
338 private function curlRequest($url, $returnResultProperty = false, $body = [], $method = 'GET')
339 {
340 $request = curl_init($url);
341
342 if ($method === 'POST') {
343 curl_setopt($request, CURLOPT_POST, true);
344 curl_setopt($request, CURLOPT_POSTFIELDS, $body);
345 }
346
347 curl_setopt($request, CURLOPT_HTTPHEADER, $this->stringifyHeaders());
348 curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
349 curl_setopt($request, CURLOPT_SSLVERSION, 3);
350
351 $result = curl_exec($request);
352 $httpCode = curl_getinfo($request, CURLINFO_HTTP_CODE);
353
354 // close the session
355 curl_close($request);
356
357 return $this->handleFallbackResponse($httpCode, $result, $returnResultProperty);
358 }
359
360 private function handleFallbackResponse($httpCode, $response, $returnResultProperty = false)
361 {
362 /**
363 * Check on the result of the transaction
364 */
365 $jsonResponse = json_decode($response);
366 $responseMessage = ($returnResultProperty) ? $jsonResponse->result : $jsonResponse;
367
368 // Return formatted response
369 return array(
370 'message' => $responseMessage,
371 'code' => $httpCode,
372 );
373 }
374
375 }
376