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