PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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 / OneDrive / OneDriveHandler.php

OneDriveHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.0, at includes/Core/Integration/OneDrive/OneDriveHandler.php

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