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