PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 2.21.13, at includes/Core/Integration/Dropbox/DropboxHandler.php

243 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
53
54 GlobalHelper::requirePostMethod();
55
56 try {
57 $queryParams = GlobalHelper::formatRequestData();
58 } catch (\InvalidArgumentException $e) {
59 wp_send_json_error($e->getMessage(), 400);
60 }
61
62 if (empty($queryParams->accessCode) || empty($queryParams->apiKey) || empty($queryParams->apiSecret)) {
63 wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400);
64 }
65
66 $body = [
67 'code' => $queryParams->accessCode,
68 'grant_type' => 'authorization_code',
69 'client_id' => $queryParams->apiKey,
70 'client_secret' => $queryParams->apiSecret,
71 ];
72
73 $apiEndpoint = self::$apiBaseUri . '/oauth2/token';
74 $apiResponse = HttpHelper::post($apiEndpoint, $body);
75
76 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
77 wp_send_json_error(empty($apiResponse->error_description) ? 'Unknown' : $apiResponse->error_description, 400);
78 }
79 $apiResponse->generates_on = \time();
80 wp_send_json_success($apiResponse, 200);
81 }
82
83 /**
84 * get dropbox folders List
85 *
86 * @return JSON
87 */
88 public static function getAllFolders()
89 {
90 if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
91 wp_send_json_error(__('Token expired', 'bit-form'), 401);
92 }
93
94
95
96 GlobalHelper::requirePostMethod();
97
98 try {
99 $queryParams = GlobalHelper::formatRequestData();
100 } catch (\InvalidArgumentException $e) {
101 wp_send_json_error($e->getMessage(), 400);
102 }
103
104 if (empty($queryParams->tokenDetails) || empty($queryParams->apiKey) || empty($queryParams->apiSecret)) {
105 wp_send_json_error(__('Requested parameter is empty', 'bit-form'), 400);
106 }
107
108 $token = self::tokenExpiryCheck($queryParams->tokenDetails, $queryParams->apiKey, $queryParams->apiSecret);
109 if ($token->access_token !== $queryParams->tokenDetails->access_token) {
110 self::saveRefreshedToken($queryParams->formID, $queryParams->id, $token);
111 }
112
113 $folders = self::getDropboxFoldersList($token->access_token);
114 $data = [];
115 if ($folders->entries) {
116 foreach ($folders->entries as $folder) {
117 $folder = (array)$folder;
118 if ('folder' === $folder['.tag']) {
119 $data[] = (object) [
120 'name' => $folder['name'],
121 'lower_path' => $folder['path_lower'],
122 ];
123 }
124 }
125 }
126
127 $response['dropboxFoldersList'] = $data;
128 $response['tokenDetails'] = $token;
129 wp_send_json_success($response, 200);
130 }
131
132 public static function getDropboxFoldersList($token)
133 {
134 $headers = [
135 'Content-Type' => 'application/json; charset=utf-8',
136 'Authorization' => 'Bearer ' . $token,
137 ];
138 $options = [
139 'path' => '',
140 'recursive' => true,
141 'include_deleted' => false,
142 'include_mounted_folders' => true,
143 'include_non_downloadable_files' => true
144 ];
145 $options = wp_json_encode($options);
146
147 $recipientApiEndpoint = self::$apiBaseUri . '/2/files/list_folder';
148 $apiResponse = HttpHelper::post($recipientApiEndpoint, $options, $headers);
149 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
150 return false;
151 }
152 return $apiResponse;
153 }
154
155 private static function tokenExpiryCheck($token, $apiKey, $apiSecret)
156 {
157 if (!$token) {
158 return false;
159 }
160
161 if (($token->generates_on + $token->expires_in - 30) < time()) {
162 $refreshToken = self::refreshToken($token->refresh_token, $apiKey, $apiSecret);
163 if (is_wp_error($refreshToken) || !empty($refreshToken->error)) {
164 return false;
165 }
166
167 $token->access_token = $refreshToken->access_token;
168 $token->expires_in = $refreshToken->expires_in;
169 $token->generates_on = $refreshToken->generates_on;
170 }
171 return $token;
172 }
173
174 private static function refreshToken($refresh_token, $apiKey, $apiSecret)
175 {
176 $body = [
177 'grant_type' => 'refresh_token',
178 'client_id' => $apiKey,
179 'client_secret' => $apiSecret,
180 'refresh_token' => $refresh_token,
181 ];
182
183 $apiEndpoint = self::$apiBaseUri . '/oauth2/token';
184 $apiResponse = HttpHelper::post($apiEndpoint, $body);
185 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
186 return false;
187 }
188 $token = $apiResponse;
189 $token->generates_on = \time();
190 return $token;
191 }
192
193 private static function saveRefreshedToken($formID, $integrationID, $tokenDetails)
194 {
195 if (empty($formID) || empty($integrationID)) {
196 return;
197 }
198
199 $integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail());
200 $dropboxDetails = $integrationHandler->getAIntegration($integrationID);
201 if (is_wp_error($dropboxDetails)) {
202 return;
203 }
204
205 $newDetails = json_decode($dropboxDetails[0]->integration_details);
206 $newDetails->tokenDetails = $tokenDetails;
207 $integrationHandler->updateIntegration($integrationID, $dropboxDetails[0]->integration_name, 'Dropbox', wp_json_encode($newDetails), 'form');
208 }
209
210 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
211 {
212 $integrationDetails = json_decode($integrationData->integration_details);
213 $entryDetails = [
214 'formId' => $this->formID,
215 'entryId' => $entryID,
216 'fieldValues' => $fieldValues
217 ];
218
219 if (empty($integrationDetails->tokenDetails->access_token)) {
220 (new ApiResponse())->apiResponse($logID, $this->integrationID, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Not Authorization By Dropbox.', $entryDetails);
221 return;
222 }
223
224 $actions = $integrationDetails->actions;
225 $fieldMap = $integrationDetails->field_map;
226 $tokenDetails = self::tokenExpiryCheck($integrationDetails->tokenDetails, $integrationDetails->apiKey, $integrationDetails->apiSecret);
227 if ($tokenDetails->access_token !== $integrationDetails->tokenDetails->access_token) {
228 self::saveRefreshedToken($this->formID, $this->integrationID, $tokenDetails);
229 }
230
231 if (empty($fieldMap)) {
232 return new WP_Error('REQ_FIELD_EMPTY', __('Required data not found.', 'bit-form'));
233 }
234
235 $dropboxResponse = (new DropboxRecordApiHelper($tokenDetails->access_token, $this->formID, $entryID));
236 $apiResponse = $dropboxResponse->executeRecordApi($this->integrationID, $logID, $fieldValues, $fieldMap, $actions);
237 if (is_wp_error($dropboxResponse)) {
238 return $dropboxResponse;
239 }
240 return $apiResponse;
241 }
242 }
243