| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Dropbox; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Integration\Dropbox\RecordApiHelper as DropboxRecordApiHelper; |
| 6 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 7 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 8 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 9 |
use BitCode\BitForm\Core\Util\IpTool; |
| 10 |
use WP_Error; |
| 11 |
|
| 12 |
class DropboxHandler |
| 13 |
{ |
| 14 |
private $formID; |
| 15 |
private $integrationID; |
| 16 |
protected static $apiBaseUri = 'https://api.dropboxapi.com'; |
| 17 |
protected static $contentBaseUri = 'https://content.dropboxapi.com'; |
| 18 |
|
| 19 |
public function __construct($integrationID, $fromID) |
| 20 |
{ |
| 21 |
$this->formID = $fromID; |
| 22 |
$this->integrationID = $integrationID; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Helps to register ajax function's with wp |
| 27 |
* |
| 28 |
* @return null |
| 29 |
*/ |
| 30 |
public static function registerAjax() |
| 31 |
{ |
| 32 |
add_action('wp_ajax_bitforms_dropbox_authorization', [__CLASS__, 'checkAuthorization']); |
| 33 |
add_action('wp_ajax_bitforms_dropbox_get_all_folders', [__CLASS__, 'getAllFolders']); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* authorize dropbox |
| 38 |
* |
| 39 |
* @return JSON |
| 40 |
*/ |
| 41 |
public static function checkAuthorization() |
| 42 |
{ |
| 43 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 44 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 45 |
} |
| 46 |
|
| 47 |
$inputJSON = file_get_contents('php://input'); |
| 48 |
$queryParams = json_decode($inputJSON); |
| 49 |
|
| 50 |
if (empty($queryParams->accessCode) || empty($queryParams->apiKey) || empty($queryParams->apiSecret)) { |
| 51 |
wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 52 |
} |
| 53 |
|
| 54 |
$body = [ |
| 55 |
'code' => $queryParams->accessCode, |
| 56 |
'grant_type' => 'authorization_code', |
| 57 |
'client_id' => $queryParams->apiKey, |
| 58 |
'client_secret' => $queryParams->apiSecret, |
| 59 |
]; |
| 60 |
|
| 61 |
$apiEndpoint = self::$apiBaseUri . '/oauth2/token'; |
| 62 |
$apiResponse = HttpHelper::post($apiEndpoint, $body); |
| 63 |
|
| 64 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 65 |
wp_send_json_error(empty($apiResponse->error_description) ? 'Unknown' : $apiResponse->error_description, 400); |
| 66 |
} |
| 67 |
$apiResponse->generates_on = \time(); |
| 68 |
wp_send_json_success($apiResponse, 200); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* get dropbox folders List |
| 73 |
* |
| 74 |
* @return JSON |
| 75 |
*/ |
| 76 |
public static function getAllFolders() |
| 77 |
{ |
| 78 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 79 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 80 |
} |
| 81 |
|
| 82 |
$inputJSON = file_get_contents('php://input'); |
| 83 |
$queryParams = json_decode($inputJSON); |
| 84 |
|
| 85 |
if (empty($queryParams->tokenDetails) || empty($queryParams->apiKey) || empty($queryParams->apiSecret)) { |
| 86 |
wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 87 |
} |
| 88 |
|
| 89 |
$token = self::tokenExpiryCheck($queryParams->tokenDetails, $queryParams->apiKey, $queryParams->apiSecret); |
| 90 |
if ($token->access_token !== $queryParams->tokenDetails->access_token) { |
| 91 |
self::saveRefreshedToken($queryParams->formID, $queryParams->id, $token); |
| 92 |
} |
| 93 |
|
| 94 |
$folders = self::getDropboxFoldersList($token->access_token); |
| 95 |
$data = []; |
| 96 |
if ($folders->entries) { |
| 97 |
foreach ($folders->entries as $folder) { |
| 98 |
$folder = (array)$folder; |
| 99 |
if ('folder' === $folder['.tag']) { |
| 100 |
$data[] = (object) [ |
| 101 |
'name' => $folder['name'], |
| 102 |
'lower_path' => $folder['path_lower'], |
| 103 |
]; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
$response['dropboxFoldersList'] = $data; |
| 109 |
$response['tokenDetails'] = $token; |
| 110 |
wp_send_json_success($response, 200); |
| 111 |
} |
| 112 |
|
| 113 |
public static function getDropboxFoldersList($token) |
| 114 |
{ |
| 115 |
$headers = [ |
| 116 |
'Content-Type' => 'application/json; charset=utf-8', |
| 117 |
'Authorization' => 'Bearer ' . $token, |
| 118 |
]; |
| 119 |
$options = [ |
| 120 |
'path' => '', |
| 121 |
'recursive' => true, |
| 122 |
'include_deleted' => false, |
| 123 |
'include_mounted_folders' => true, |
| 124 |
'include_non_downloadable_files' => true |
| 125 |
]; |
| 126 |
$options = json_encode($options); |
| 127 |
|
| 128 |
$recipientApiEndpoint = self::$apiBaseUri . '/2/files/list_folder'; |
| 129 |
$apiResponse = HttpHelper::post($recipientApiEndpoint, $options, $headers); |
| 130 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
return $apiResponse; |
| 134 |
} |
| 135 |
|
| 136 |
private static function tokenExpiryCheck($token, $apiKey, $apiSecret) |
| 137 |
{ |
| 138 |
if (!$token) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if (($token->generates_on + $token->expires_in - 30) < time()) { |
| 143 |
$refreshToken = self::refreshToken($token->refresh_token, $apiKey, $apiSecret); |
| 144 |
if (is_wp_error($refreshToken) || !empty($refreshToken->error)) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
$token->access_token = $refreshToken->access_token; |
| 149 |
$token->expires_in = $refreshToken->expires_in; |
| 150 |
$token->generates_on = $refreshToken->generates_on; |
| 151 |
} |
| 152 |
return $token; |
| 153 |
} |
| 154 |
|
| 155 |
private static function refreshToken($refresh_token, $apiKey, $apiSecret) |
| 156 |
{ |
| 157 |
$body = [ |
| 158 |
'grant_type' => 'refresh_token', |
| 159 |
'client_id' => $apiKey, |
| 160 |
'client_secret' => $apiSecret, |
| 161 |
'refresh_token' => $refresh_token, |
| 162 |
]; |
| 163 |
|
| 164 |
$apiEndpoint = self::$apiBaseUri . '/oauth2/token'; |
| 165 |
$apiResponse = HttpHelper::post($apiEndpoint, $body); |
| 166 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
$token = $apiResponse; |
| 170 |
$token->generates_on = \time(); |
| 171 |
return $token; |
| 172 |
} |
| 173 |
|
| 174 |
private static function saveRefreshedToken($formID, $integrationID, $tokenDetails) |
| 175 |
{ |
| 176 |
if (empty($formID) || empty($integrationID)) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 181 |
$dropboxDetails = $integrationHandler->getAIntegration($integrationID); |
| 182 |
if (is_wp_error($dropboxDetails)) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
$newDetails = json_decode($dropboxDetails[0]->integration_details); |
| 187 |
$newDetails->tokenDetails = $tokenDetails; |
| 188 |
$integrationHandler->updateIntegration($integrationID, $dropboxDetails[0]->integration_name, 'Dropbox', \json_encode($newDetails), 'form'); |
| 189 |
} |
| 190 |
|
| 191 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 192 |
{ |
| 193 |
$integrationDetails = json_decode($integrationData->integration_details); |
| 194 |
|
| 195 |
if (empty($integrationDetails->tokenDetails->access_token)) { |
| 196 |
(new ApiResponse())->apiResponse($logID, $this->integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Not Authorization By Dropbox.'); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
$actions = $integrationDetails->actions; |
| 201 |
$fieldMap = $integrationDetails->field_map; |
| 202 |
$tokenDetails = self::tokenExpiryCheck($integrationDetails->tokenDetails, $integrationDetails->apiKey, $integrationDetails->apiSecret); |
| 203 |
if ($tokenDetails->access_token !== $integrationDetails->tokenDetails->access_token) { |
| 204 |
self::saveRefreshedToken($this->formID, $this->integrationID, $tokenDetails); |
| 205 |
} |
| 206 |
|
| 207 |
if (empty($fieldMap)) { |
| 208 |
return new WP_Error('REQ_FIELD_EMPTY', __('Required data not found.', 'bit-form')); |
| 209 |
} |
| 210 |
|
| 211 |
$dropboxResponse = (new DropboxRecordApiHelper($tokenDetails->access_token, $this->formID, $entryID)); |
| 212 |
$apiResponse = $dropboxResponse->executeRecordApi($this->integrationID, $logID, $fieldValues, $fieldMap, $actions); |
| 213 |
if (is_wp_error($dropboxResponse)) { |
| 214 |
return $dropboxResponse; |
| 215 |
} |
| 216 |
return $apiResponse; |
| 217 |
} |
| 218 |
} |
| 219 |
|