PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.3.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.3.2
V3.3.2 3.3.1 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 All 139 releases
bit-form / includes / Core / Integration / Dropbox / RecordApiHelper.php

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

150 lines 4.5 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 use BitCode\BitForm\Core\Util\ApiResponse;
6 use BitCode\BitForm\Core\Util\FileHandler;
7 use BitCode\BitForm\Core\Util\HttpHelper;
8 use WP_Error;
9
10 class RecordApiHelper
11 {
12 protected $token;
13 protected $formId;
14 protected $entryId;
15 protected $apiBaseUri = 'https://api.dropboxapi.com';
16 protected $contentBaseUri = 'https://content.dropboxapi.com';
17 protected $errorApiResponse = [];
18 protected $successApiResponse = [];
19
20 private $logResponse;
21
22 public function __construct($token, $formId, $entryId)
23 {
24 $this->token = $token;
25 $this->formId = $formId;
26 $this->entryId = $entryId;
27 $this->logResponse = new ApiResponse();
28 }
29
30 public function uploadFile($folder, $filePath)
31 {
32 if ('' === $filePath) {
33 return;
34 }
35 $filePath = $this->makeFilePath($filePath);
36 $body = FileHandler::readFile($filePath);
37 if (!$body) {
38 return new WP_Error(423, 'Can\'t open file!');
39 }
40
41 $apiEndPoint = $this->contentBaseUri . '/2/files/upload';
42 $headers = [
43 'Authorization' => 'Bearer ' . $this->token,
44 'Content-Type' => 'application/octet-stream',
45 'Dropbox-API-Arg' => wp_json_encode([
46 'path' => $this->normalizePath($folder) . '/' . $this->fileName($filePath),
47 'mode' => 'add',
48 'autorename' => true,
49 'mute' => true,
50 'strict_conflict' => false
51 ]),
52 ];
53 return HttpHelper::post($apiEndPoint, $body, $headers);
54 }
55
56 public function fileName($filePath)
57 {
58 return basename($filePath);
59 }
60
61 public function normalizePath($path)
62 {
63 return str_replace('\\', '/', $path);
64 }
65
66 public function handleAllFiles($folderWithFile, $actions)
67 {
68 foreach ($folderWithFile as $folder => $filePath) {
69 if ('' === $filePath) {
70 continue;
71 }
72 if (is_array($filePath)) {
73 foreach ($filePath as $singleFilePath) {
74 if ('' === $singleFilePath) {
75 continue;
76 }
77 $this->uploadAndCleanup($folder, $singleFilePath, $actions);
78 }
79 } else {
80 $this->uploadAndCleanup($folder, $filePath, $actions);
81 }
82 }
83 }
84
85 /**
86 * The local copy is the only copy until Dropbox confirms the upload, so a failed
87 * upload (expired token, API error, unreadable file) must keep it on disk.
88 */
89 private function uploadAndCleanup($folder, $filePath, $actions)
90 {
91 $response = $this->uploadFile($folder, $filePath);
92 if ($this->storeInState($response)) {
93 $this->deleteFile($filePath, $actions);
94 }
95 }
96
97 /**
98 * @return bool true when Dropbox returned the uploaded file's metadata
99 */
100 protected function storeInState($response)
101 {
102 if (isset($response->id)) {
103 $this->successApiResponse[] = $response;
104 return true;
105 }
106 $this->errorApiResponse[] = $response;
107 return false;
108 }
109
110 public function deleteFile($filePath, $actions)
111 {
112 if (isset($actions->delete_from_wp) && $actions->delete_from_wp) {
113 $filePath = $this->makeFilePath($filePath);
114 FileHandler::deleteIsFileExists($filePath);
115 }
116 }
117
118 public function makeFilePath($filePath)
119 {
120 // Field values arrive as public file URLs (see IntegrationHandler::handleFileUrl),
121 // so reduce to the stored file name before resolving against the entry directory.
122 $fileName = basename(wp_parse_url($filePath, PHP_URL_PATH) ?: $filePath);
123 return realpath(FileHandler::getEntriesFileUploadDir($this->formId, $this->entryId) . DIRECTORY_SEPARATOR . $fileName);
124 }
125
126 public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions)
127 {
128 $folderWithFile = [];
129 foreach ($fieldMap as $value) {
130 if (!is_null($fieldValues[$value->formField])) {
131 $folderWithFile[$value->dropboxFormField] = $fieldValues[$value->formField];
132 }
133 }
134 $this->handleAllFiles($folderWithFile, $actions);
135
136 $entryDetails = [
137 'formId' => $this->formId,
138 'entryId' => $this->entryId,
139 'fieldValues' => $fieldValues
140 ];
141
142 if (count($this->errorApiResponse) > 0) {
143 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Some Files Can\'t Upload For Some Reason. ' . wp_json_encode($this->errorApiResponse), $entryDetails);
144 }
145 if (count($this->successApiResponse) > 0) {
146 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse), $entryDetails);
147 }
148 }
149 }
150