PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / Dropbox / DropboxHandler.php

DropboxHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.2.2, at includes/Core/Integration/Dropbox/DropboxHandler.php

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