| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\OneDrive; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 6 |
use BitCode\BitForm\Core\Integration\OneDrive\RecordApiHelper as OneDriveRecordApiHelper; |
| 7 |
use BitCode\BitForm\Core\Util\ApiResponse; |
| 8 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 9 |
use BitCode\BitForm\Core\Util\IpTool; |
| 10 |
|
| 11 |
class OneDriveHandler |
| 12 |
{ |
| 13 |
private $formID; |
| 14 |
private $integrationID; |
| 15 |
|
| 16 |
public function __construct($integrationID, $fromID) |
| 17 |
{ |
| 18 |
$this->formID = $fromID; |
| 19 |
$this->integrationID = $integrationID; |
| 20 |
} |
| 21 |
|
| 22 |
public static function registerAjax() |
| 23 |
{ |
| 24 |
add_action('wp_ajax_bitforms_oneDrive_authorization', [__CLASS__, 'authorization']); |
| 25 |
add_action('wp_ajax_bitforms_oneDrive_get_all_folders', [__CLASS__, 'getAllFolders']); |
| 26 |
add_action('wp_ajax_bitforms_oneDrive_get_single_folder', [__CLASS__, 'singleOneDriveFolderList']); |
| 27 |
} |
| 28 |
|
| 29 |
public static function authorization() |
| 30 |
{ |
| 31 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 32 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 33 |
} |
| 34 |
|
| 35 |
$inputJSON = file_get_contents('php://input'); |
| 36 |
$queryParams = json_decode($inputJSON); |
| 37 |
|
| 38 |
if (empty($queryParams->clientId) || empty($queryParams->clientSecret) || empty($queryParams->code) || empty($queryParams->redirectURI)) { |
| 39 |
wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 40 |
} |
| 41 |
|
| 42 |
$body = [ |
| 43 |
'client_id' => $queryParams->clientId, |
| 44 |
'redirect_uri' => urldecode($queryParams->redirectURI), |
| 45 |
'client_secret' => $queryParams->clientSecret, |
| 46 |
'grant_type' => 'authorization_code', |
| 47 |
'code' => urldecode($queryParams->code) |
| 48 |
]; |
| 49 |
|
| 50 |
$apiEndpoint = 'https://login.live.com/oauth20_token.srf'; |
| 51 |
$header['Content-Type'] = 'application/x-www-form-urlencoded'; |
| 52 |
$apiResponse = HttpHelper::post($apiEndpoint, $body, $header); |
| 53 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 54 |
wp_send_json_error(empty($apiResponse->error_description) ? 'Unknown' : $apiResponse->error_description, 400); |
| 55 |
} |
| 56 |
$apiResponse->generates_on = \time(); |
| 57 |
wp_send_json_success($apiResponse, 200); |
| 58 |
} |
| 59 |
|
| 60 |
public static function getAllFolders() |
| 61 |
{ |
| 62 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 63 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 64 |
} |
| 65 |
|
| 66 |
$inputJSON = file_get_contents('php://input'); |
| 67 |
$queryParams = json_decode($inputJSON); |
| 68 |
if (empty($queryParams->tokenDetails) || empty($queryParams->clientId) || empty($queryParams->clientSecret)) { |
| 69 |
wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 70 |
} |
| 71 |
|
| 72 |
$token = self::tokenExpiryCheck($queryParams->tokenDetails, $queryParams->clientId, $queryParams->clientSecret); |
| 73 |
if ($token->access_token !== $queryParams->tokenDetails->access_token) { |
| 74 |
self::saveRefreshedToken($queryParams->formID, $queryParams->id, $token); |
| 75 |
} |
| 76 |
|
| 77 |
$folders = self::getOneDriveFoldersList($token->access_token); |
| 78 |
$foldersOnly = $folders->value; |
| 79 |
|
| 80 |
$data = []; |
| 81 |
if (is_array($foldersOnly)) { |
| 82 |
foreach ($foldersOnly as $folder) { |
| 83 |
if (property_exists($folder, 'folder')) { |
| 84 |
$data[] = $folder; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
$response['oneDriveFoldersList'] = $data; |
| 89 |
$response['tokenDetails'] = $token; |
| 90 |
wp_send_json_success($response, 200); |
| 91 |
} |
| 92 |
|
| 93 |
public static function getOneDriveFoldersList($token) |
| 94 |
{ |
| 95 |
$headers = [ |
| 96 |
'Accept' => 'application/json', |
| 97 |
'Content-Type' => 'application/json;', |
| 98 |
'Authorization' => 'bearer ' . $token, |
| 99 |
]; |
| 100 |
$apiEndpoint = 'https://api.onedrive.com/v1.0/drive/root/children'; |
| 101 |
$apiResponse = HttpHelper::get($apiEndpoint, [], $headers); |
| 102 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
return $apiResponse; |
| 106 |
} |
| 107 |
|
| 108 |
public static function singleOneDriveFolderList() |
| 109 |
{ |
| 110 |
if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 111 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 112 |
} |
| 113 |
|
| 114 |
$inputJSON = file_get_contents('php://input'); |
| 115 |
$queryParams = json_decode($inputJSON); |
| 116 |
|
| 117 |
if (empty($queryParams->tokenDetails) || empty($queryParams->clientId) || empty($queryParams->clientSecret)) { |
| 118 |
wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 119 |
} |
| 120 |
|
| 121 |
$ids = explode('!', $queryParams->folder); |
| 122 |
$token = self::tokenExpiryCheck($queryParams->tokenDetails, $queryParams->clientId, $queryParams->clientSecret); |
| 123 |
if ($token->access_token !== $queryParams->tokenDetails->access_token) { |
| 124 |
self::saveRefreshedToken($queryParams->formID, $queryParams->id, $token); |
| 125 |
} |
| 126 |
|
| 127 |
$headers = [ |
| 128 |
'Accept' => 'application/json', |
| 129 |
'Content-Type' => 'application/json;', |
| 130 |
'Authorization' => 'bearer ' . $queryParams->tokenDetails->access_token, |
| 131 |
]; |
| 132 |
$apiEndpoint = 'https://api.onedrive.com/v1.0/drives/' . $ids[0] . '/items/' . $queryParams->folder . '/children'; |
| 133 |
$apiResponse = HttpHelper::get($apiEndpoint, [], $headers); |
| 134 |
$foldersOnly = $apiResponse->value; |
| 135 |
$data = []; |
| 136 |
if (is_array($foldersOnly)) { |
| 137 |
foreach ($foldersOnly as $folder) { |
| 138 |
if (property_exists($folder, 'folder')) { |
| 139 |
$data[] = $folder; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
$response['folders'] = $data; |
| 144 |
$response['tokenDetails'] = $token; |
| 145 |
wp_send_json_success($response, 200); |
| 146 |
} |
| 147 |
|
| 148 |
private static function tokenExpiryCheck($token, $clientId, $clientSecret) |
| 149 |
{ |
| 150 |
if (!$token) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
if ((intval($token->generates_on) + (55 * 60)) < time()) { |
| 155 |
$refreshToken = self::refreshToken($token->refresh_token, $clientId, $clientSecret); |
| 156 |
if (is_wp_error($refreshToken) || !empty($refreshToken->error)) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
$token->access_token = $refreshToken->access_token; |
| 160 |
$token->expires_in = $refreshToken->expires_in; |
| 161 |
$token->generates_on = $refreshToken->generates_on; |
| 162 |
} |
| 163 |
return $token; |
| 164 |
} |
| 165 |
|
| 166 |
private static function refreshToken($refresh_token, $clientId, $clientSecret) |
| 167 |
{ |
| 168 |
$body = [ |
| 169 |
'client_id' => $clientId, |
| 170 |
'client_secret' => $clientSecret, |
| 171 |
'grant_type' => 'refresh_token', |
| 172 |
'refresh_token' => $refresh_token, |
| 173 |
]; |
| 174 |
|
| 175 |
$apiEndpoint = 'https://login.live.com/oauth20_token.srf'; |
| 176 |
$apiResponse = HttpHelper::post($apiEndpoint, $body); |
| 177 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
$token = $apiResponse; |
| 181 |
$token->generates_on = \time(); |
| 182 |
return $token; |
| 183 |
} |
| 184 |
|
| 185 |
private static function saveRefreshedToken($formID, $integrationID, $tokenDetails) |
| 186 |
{ |
| 187 |
if (empty($formID) || empty($integrationID)) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 192 |
$oneDriveDetails = $integrationHandler->getAIntegration($integrationID); |
| 193 |
if (is_wp_error($oneDriveDetails)) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
$newDetails = json_decode($oneDriveDetails[0]->integration_details); |
| 198 |
$newDetails->tokenDetails = $tokenDetails; |
| 199 |
$integrationHandler->updateIntegration($integrationID, $oneDriveDetails[0]->integration_name, 'OneDrive', \json_encode($newDetails), 'form'); |
| 200 |
} |
| 201 |
|
| 202 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 203 |
{ |
| 204 |
$integrationDetails = json_decode($integrationData->integration_details); |
| 205 |
if (empty($integrationDetails->tokenDetails->access_token)) { |
| 206 |
(new ApiResponse())->apiResponse($logID, $this->integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Not Authorization By OneDrive.'); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$actions = $integrationDetails->actions; |
| 211 |
$folderId = $integrationDetails->folder; |
| 212 |
$fieldMap = $integrationDetails->field_map; |
| 213 |
$tokenDetails = self::tokenExpiryCheck($integrationDetails->tokenDetails, $integrationDetails->clientId, $integrationDetails->clientSecret); |
| 214 |
$parentId = $integrationData->flow_details->folderMap[1]; |
| 215 |
if ($tokenDetails->access_token !== $integrationDetails->tokenDetails->access_token) { |
| 216 |
self::saveRefreshedToken($this->formID, $this->integrationID, $tokenDetails); |
| 217 |
} |
| 218 |
|
| 219 |
(new OneDriveRecordApiHelper($tokenDetails->access_token, $this->formID, $entryID))->executeRecordApi($this->integrationID, $logID, $fieldValues, $fieldMap, $actions, $folderId, $parentId); |
| 220 |
return true; |
| 221 |
} |
| 222 |
} |
| 223 |
|