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