← All changes
|
includes/Core/Integration/OneDrive/OneDriveHandler.php
+64
-24
2.0
→
3.3.1
View file →
| @@ -1,37 +1,51 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Integration\OneDrive; |
| 4 | 4 | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 6 | 10 | use BitCode\BitForm\Core\Integration\OneDrive\RecordApiHelper as OneDriveRecordApiHelper; |
| 7 | 11 | use BitCode\BitForm\Core\Util\ApiResponse; |
| 8 | 12 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 9 | 13 | use BitCode\BitForm\Core\Util\IpTool; |
| 14 | +use BitCode\BitForm\GlobalHelper; | |
| 10 | 15 | |
| 11 | -class OneDriveHandler { | |
| 16 | +class OneDriveHandler | |
| 17 | +{ | |
| 12 | 18 | private $formID; |
| 13 | 19 | private $integrationID; |
| 14 | 20 | |
| 15 | - public function __construct($integrationID, $fromID) { | |
| 21 | + public function __construct($integrationID, $fromID) | |
| 22 | + { | |
| 16 | 23 | $this->formID = $fromID; |
| 17 | 24 | $this->integrationID = $integrationID; |
| 18 | 25 | } |
| 19 | 26 | |
| 20 | - public static function registerAjax() { | |
| 27 | + public static function registerAjax() | |
| 28 | + { | |
| 21 | 29 | add_action('wp_ajax_bitforms_oneDrive_authorization', [__CLASS__, 'authorization']); |
| 22 | 30 | add_action('wp_ajax_bitforms_oneDrive_get_all_folders', [__CLASS__, 'getAllFolders']); |
| 23 | 31 | add_action('wp_ajax_bitforms_oneDrive_get_single_folder', [__CLASS__, 'singleOneDriveFolderList']); |
| 24 | 32 | } |
| 25 | 33 | |
| 26 | - public static function authorization() { | |
| 27 | - if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 34 | + public static function authorization() | |
| 35 | + { | |
| 36 | + if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 28 | 37 | wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 29 | 38 | } |
| 30 | 39 | |
| 31 | - $inputJSON = file_get_contents('php://input'); | |
| 32 | - $queryParams = json_decode($inputJSON); | |
| 40 | + GlobalHelper::requirePostMethod(); | |
| 33 | 41 | |
| 42 | + try { | |
| 43 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 44 | + } catch (\InvalidArgumentException $e) { | |
| 45 | + wp_send_json_error($e->getMessage(), 400); | |
| 46 | + } | |
| 47 | + | |
| 34 | 48 | if (empty($queryParams->clientId) || empty($queryParams->clientSecret) || empty($queryParams->code) || empty($queryParams->redirectURI)) { |
| 35 | 49 | wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 36 | 50 | } |
| 37 | 51 | |
| @@ -52,15 +66,21 @@ | ||
| 52 | 66 | $apiResponse->generates_on = \time(); |
| 53 | 67 | wp_send_json_success($apiResponse, 200); |
| 54 | 68 | } |
| 55 | 69 | |
| 56 | - public static function getAllFolders() { | |
| 57 | - if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 70 | + public static function getAllFolders() | |
| 71 | + { | |
| 72 | + if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 58 | 73 | wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 59 | 74 | } |
| 60 | 75 | |
| 61 | - $inputJSON = file_get_contents('php://input'); | |
| 62 | - $queryParams = json_decode($inputJSON); | |
| 76 | + GlobalHelper::requirePostMethod(); | |
| 77 | + | |
| 78 | + try { | |
| 79 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 80 | + } catch (\InvalidArgumentException $e) { | |
| 81 | + wp_send_json_error($e->getMessage(), 400); | |
| 82 | + } | |
| 63 | 83 | if (empty($queryParams->tokenDetails) || empty($queryParams->clientId) || empty($queryParams->clientSecret)) { |
| 64 | 84 | wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 65 | 85 | } |
| 66 | 86 | |
| @@ -84,9 +104,10 @@ | ||
| 84 | 104 | $response['tokenDetails'] = $token; |
| 85 | 105 | wp_send_json_success($response, 200); |
| 86 | 106 | } |
| 87 | 107 | |
| 88 | - public static function getOneDriveFoldersList($token) { | |
| 108 | + public static function getOneDriveFoldersList($token) | |
| 109 | + { | |
| 89 | 110 | $headers = [ |
| 90 | 111 | 'Accept' => 'application/json', |
| 91 | 112 | 'Content-Type' => 'application/json;', |
| 92 | 113 | 'Authorization' => 'bearer ' . $token, |
| @@ -98,16 +119,22 @@ | ||
| 98 | 119 | } |
| 99 | 120 | return $apiResponse; |
| 100 | 121 | } |
| 101 | 122 | |
| 102 | - public static function singleOneDriveFolderList() { | |
| 103 | - if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 123 | + public static function singleOneDriveFolderList() | |
| 124 | + { | |
| 125 | + if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 104 | 126 | wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 105 | 127 | } |
| 106 | 128 | |
| 107 | - $inputJSON = file_get_contents('php://input'); | |
| 108 | - $queryParams = json_decode($inputJSON); | |
| 129 | + GlobalHelper::requirePostMethod(); | |
| 109 | 130 | |
| 131 | + try { | |
| 132 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 133 | + } catch (\InvalidArgumentException $e) { | |
| 134 | + wp_send_json_error($e->getMessage(), 400); | |
| 135 | + } | |
| 136 | + | |
| 110 | 137 | if (empty($queryParams->tokenDetails) || empty($queryParams->clientId) || empty($queryParams->clientSecret)) { |
| 111 | 138 | wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400); |
| 112 | 139 | } |
| 113 | 140 | |
| @@ -137,9 +164,10 @@ | ||
| 137 | 164 | $response['tokenDetails'] = $token; |
| 138 | 165 | wp_send_json_success($response, 200); |
| 139 | 166 | } |
| 140 | 167 | |
| 141 | - private static function tokenExpiryCheck($token, $clientId, $clientSecret) { | |
| 168 | + private static function tokenExpiryCheck($token, $clientId, $clientSecret) | |
| 169 | + { | |
| 142 | 170 | if (!$token) { |
| 143 | 171 | return false; |
| 144 | 172 | } |
| 145 | 173 | |
| @@ -154,9 +182,10 @@ | ||
| 154 | 182 | } |
| 155 | 183 | return $token; |
| 156 | 184 | } |
| 157 | 185 | |
| 158 | - private static function refreshToken($refresh_token, $clientId, $clientSecret) { | |
| 186 | + private static function refreshToken($refresh_token, $clientId, $clientSecret) | |
| 187 | + { | |
| 159 | 188 | $body = [ |
| 160 | 189 | 'client_id' => $clientId, |
| 161 | 190 | 'client_secret' => $clientSecret, |
| 162 | 191 | 'grant_type' => 'refresh_token', |
| @@ -172,9 +201,10 @@ | ||
| 172 | 201 | $token->generates_on = \time(); |
| 173 | 202 | return $token; |
| 174 | 203 | } |
| 175 | 204 | |
| 176 | - private static function saveRefreshedToken($formID, $integrationID, $tokenDetails) { | |
| 205 | + private static function saveRefreshedToken($formID, $integrationID, $tokenDetails) | |
| 206 | + { | |
| 177 | 207 | if (empty($formID) || empty($integrationID)) { |
| 178 | 208 | return; |
| 179 | 209 | } |
| 180 | 210 | |
| @@ -185,15 +215,23 @@ | ||
| 185 | 215 | } |
| 186 | 216 | |
| 187 | 217 | $newDetails = json_decode($oneDriveDetails[0]->integration_details); |
| 188 | 218 | $newDetails->tokenDetails = $tokenDetails; |
| 189 | - $integrationHandler->updateIntegration($integrationID, $oneDriveDetails[0]->integration_name, 'OneDrive', \json_encode($newDetails), 'form'); | |
| 219 | + $integrationHandler->updateIntegration($integrationID, $oneDriveDetails[0]->integration_name, 'OneDrive', wp_json_encode($newDetails), 'form'); | |
| 190 | 220 | } |
| 191 | 221 | |
| 192 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 222 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 223 | + { | |
| 193 | 224 | $integrationDetails = json_decode($integrationData->integration_details); |
| 225 | + | |
| 226 | + $entryDetails = [ | |
| 227 | + 'formID' => $this->formID, | |
| 228 | + 'entryID' => $entryID, | |
| 229 | + 'fieldValues' => $fieldValues | |
| 230 | + ]; | |
| 231 | + | |
| 194 | 232 | if (empty($integrationDetails->tokenDetails->access_token)) { |
| 195 | - (new ApiResponse())->apiResponse($logID, $this->integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Not Authorization By OneDrive.'); | |
| 233 | + (new ApiResponse())->apiResponse($logID, $this->integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Not Authorization By OneDrive.', $entryDetails); | |
| 196 | 234 | return; |
| 197 | 235 | } |
| 198 | 236 | |
| 199 | 237 | $actions = $integrationDetails->actions; |
| @@ -199,13 +237,15 @@ | ||
| 199 | 237 | $actions = $integrationDetails->actions; |
| 200 | 238 | $folderId = $integrationDetails->folder; |
| 201 | 239 | $fieldMap = $integrationDetails->field_map; |
| 202 | 240 | $tokenDetails = self::tokenExpiryCheck($integrationDetails->tokenDetails, $integrationDetails->clientId, $integrationDetails->clientSecret); |
| 203 | - $parentId = $integrationData->flow_details->folderMap[1]; | |
| 241 | + $parentId = $integrationDetails->folderMap[0]; | |
| 204 | 242 | if ($tokenDetails->access_token !== $integrationDetails->tokenDetails->access_token) { |
| 205 | 243 | self::saveRefreshedToken($this->formID, $this->integrationID, $tokenDetails); |
| 206 | 244 | } |
| 207 | 245 | |
| 208 | - (new OneDriveRecordApiHelper($tokenDetails->access_token, $this->formID, $entryID))->executeRecordApi($this->integrationID, $logID, $fieldValues, $fieldMap, $actions, $folderId, $parentId); | |
| 246 | + $oneDriveRecordApiHelper = new OneDriveRecordApiHelper($tokenDetails->access_token, $this->formID, $entryID); | |
| 247 | + | |
| 248 | + $oneDriveRecordApiHelper->executeRecordApi($this->integrationID, $logID, $fieldValues, $fieldMap, $actions, $folderId, $parentId, $this->formID); | |
| 209 | 249 | return true; |
| 210 | 250 | } |
| 211 | 251 | } |