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