PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
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 2.10.1 All 138 releases
bit-form / includes / Core / Integration / OneDrive / RecordApiHelper.php

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

139 lines 4.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\Util\ApiResponse;
6 use BitCode\BitForm\Core\Util\FileHandler;
7 use WP_Error;
8
9 class RecordApiHelper
10 {
11 protected $token;
12 protected $formId;
13 protected $entryId;
14 protected $errorApiResponse = [];
15 protected $successApiResponse = [];
16
17 private $logResponse;
18
19 public function __construct($token, $formId, $entryId)
20 {
21 $this->token = $token;
22 $this->formId = $formId;
23 $this->entryId = $entryId;
24 $this->logResponse = new ApiResponse();
25 }
26
27 public function uploadFile($folder, $filePath, $folderId, $parentId)
28 {
29 if ('' === $filePath) {
30 return;
31 }
32
33 $filePath = $this->makeFilePath($filePath);
34 $filesize = filesize($filePath);
35 $fp = fopen($filePath, 'rb');
36 $body = fread($fp, $filesize);
37 if (!$body) {
38 return new WP_Error(423, 'Can\'t open file!');
39 }
40 if (is_null($parentId)) {
41 $parentId = $folderId;
42 }
43 $ids = explode('!', $folderId);
44 if ('' === $filePath) {
45 return false;
46 }
47 $apiEndpoint = 'https://api.onedrive.com/v1.0/drives/' . $ids[0] . '/items/' . $parentId . ':/' . basename($filePath) . ':/content';
48 $headers = [
49 'Authorization: Bearer ' . $this->token,
50 'Content-Type: application/octet-stream',
51 'Content-Length: ' . filesize($filePath),
52 'Prefer: respond-async',
53 'X-HTTP-Method: PUT'
54 ];
55
56 $ch = curl_init();
57 curl_setopt($ch, CURLOPT_URL, $apiEndpoint);
58 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
59 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
60 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
61 curl_setopt($ch, CURLOPT_POST, true);
62 curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
63 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
64 $response = curl_exec($ch);
65 curl_close($ch);
66 $response = json_decode($response);
67 return $response;
68 }
69
70 public function handleAllFiles($folderWithFile, $actions, $folderId, $parentId)
71 {
72 foreach ($folderWithFile as $folder => $filePath) {
73 if ('' === $filePath) {
74 continue;
75 }
76 if (is_array($filePath)) {
77 foreach ($filePath as $singleFilePath) {
78 if ('' === $singleFilePath) {
79 continue;
80 }
81 $response = $this->uploadFile($folder, $singleFilePath, $folderId, $parentId);
82 $this->storeInState($response);
83 $this->deleteFile($singleFilePath, $actions);
84 }
85 } else {
86 $response = $this->uploadFile($folder, $filePath, $folderId, $parentId);
87 $this->storeInState($response);
88 $this->deleteFile($filePath, $actions);
89 }
90 }
91 }
92
93 protected function storeInState($response)
94 {
95 $response = is_string($response) ? json_decode($response) : $response;
96 if (isset($response->id)) {
97 $this->successApiResponse[] = $response;
98 } else {
99 $this->errorApiResponse[] = $response;
100 }
101 }
102
103 public function deleteFile($filePath, $actions)
104 {
105 if (isset($actions->delete_from_wp) && $actions->delete_from_wp) {
106 $filePath = $this->makeFilePath($filePath);
107 FileHandler::deleteIsFileExists($filePath);
108 }
109 }
110
111 public function makeFilePath($filePath)
112 {
113 $upDir = wp_upload_dir();
114 return $upDir['basedir'] . '/bitforms/uploads/' . $this->formId . '/' . $this->entryId . '/' . $filePath;
115 }
116
117 public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions, $folderId, $parentId)
118 {
119 $folderWithFile = [];
120 $actionsAttachments = explode(',', "$actions->attachments");
121
122 if (is_array($actionsAttachments)) {
123 foreach ($actionsAttachments as $actionAttachment) {
124 foreach ($fieldValues[$actionAttachment] as $value) {
125 $folderWithFile[] = ["$actionAttachment" => $value];
126 }
127 $this->handleAllFiles($folderWithFile, $actions, $folderId, $parentId);
128 }
129 }
130
131 if (count($this->errorApiResponse) > 0) {
132 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Some Files Can\'t Upload For Some Reason. ' . wp_json_encode($this->errorApiResponse));
133 }
134 if (count($this->successApiResponse) > 0) {
135 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse));
136 }
137 }
138 }
139