| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoCrm Files Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoCRM; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 11 |
use BitCode\BitForm\Core\Util\FileHandler; |
| 12 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Upload files |
| 16 |
*/ |
| 17 |
final class FilesApiHelper |
| 18 |
{ |
| 19 |
private $_defaultHeader; |
| 20 |
private $_apiDomain; |
| 21 |
private $_payloadBoundary; |
| 22 |
private $_basepath; |
| 23 |
|
| 24 |
private $_integId; |
| 25 |
|
| 26 |
private $_logID; |
| 27 |
|
| 28 |
private $_logResponse; |
| 29 |
|
| 30 |
private $_formId; |
| 31 |
|
| 32 |
private $_entryId; |
| 33 |
|
| 34 |
/** |
| 35 |
* |
| 36 |
* @param object $tokenDetails Api token details |
| 37 |
* @param integer $formID ID of the form, for which integration is executing |
| 38 |
* @param integer $entryID Current submission ID |
| 39 |
*/ |
| 40 |
public function __construct($tokenDetails, $formID, $entryID, $integId, $logID) |
| 41 |
{ |
| 42 |
$this->_integId = $integId; |
| 43 |
$this->_logID = $logID; |
| 44 |
$this->_logResponse = new UtilApiResponse(); |
| 45 |
$this->_payloadBoundary = wp_generate_password(24); |
| 46 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 47 |
$this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary; |
| 48 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 49 |
$this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; |
| 50 |
|
| 51 |
$this->_formId = $formID; |
| 52 |
$this->_entryId = $entryID; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Helps to execute upload files api |
| 57 |
* |
| 58 |
* @param mixed $files Files path |
| 59 |
* @param bool $isAttachment Check upload type |
| 60 |
* @param mixed $module Attachment Module name |
| 61 |
* @param mixed | boolean $recordID Record id |
| 62 |
* |
| 63 |
* @return mixed $uploadedFiles ID's of uploaded file in Zoho CRM |
| 64 |
*/ |
| 65 |
public function uploadFiles($files, $isAttachment = false, $module = '', $recordID = 0) |
| 66 |
{ |
| 67 |
$files = self::normalizeFileNames($files); |
| 68 |
$entryDetails = [ |
| 69 |
'formId' => $this->_formId, |
| 70 |
'entryId' => $this->_entryId, |
| 71 |
]; |
| 72 |
$uploadFileEndpoint = $isAttachment ? |
| 73 |
"{$this->_apiDomain}/crm/v2/{$module}/{$recordID}/Attachments" |
| 74 |
: "{$this->_apiDomain}/crm/v2/files"; |
| 75 |
$payload = ''; |
| 76 |
if (is_array($files)) { |
| 77 |
foreach ($files as $fileIndex => $fileName) { |
| 78 |
if (file_exists("{$this->_basepath}{$fileName}")) { |
| 79 |
$payload .= '--' . $this->_payloadBoundary; |
| 80 |
$payload .= "\r\n"; |
| 81 |
$payload .= 'Content-Disposition: form-data; name="' . 'file' . |
| 82 |
'"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n"; |
| 83 |
$payload .= "\r\n"; |
| 84 |
$payload .= file_get_contents("{$this->_basepath}{$fileName}"); |
| 85 |
$payload .= "\r\n"; |
| 86 |
} |
| 87 |
} |
| 88 |
} elseif (file_exists("{$this->_basepath}{$files}")) { |
| 89 |
$payload .= '--' . $this->_payloadBoundary; |
| 90 |
$payload .= "\r\n"; |
| 91 |
$payload .= 'Content-Disposition: form-data; name="' . 'file' . |
| 92 |
'"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n"; |
| 93 |
$payload .= "\r\n"; |
| 94 |
$payload .= file_get_contents("{$this->_basepath}{$files}"); |
| 95 |
$payload .= "\r\n"; |
| 96 |
} |
| 97 |
if (empty($payload)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
$payload .= '--' . $this->_payloadBoundary . '--'; |
| 101 |
$uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 102 |
if (!$isAttachment) { |
| 103 |
$uploadedFiles = []; |
| 104 |
if (!empty($uploadResponse->data) && \is_array($uploadResponse->data)) { |
| 105 |
foreach ($uploadResponse->data as $singleFileResponse) { |
| 106 |
if (!empty($singleFileResponse->code) && 'SUCCESS' === $singleFileResponse->code) { |
| 107 |
$uploadedFiles[] = $singleFileResponse->details->id; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
if (isset($uploadResponse->status) && 'error' === $uploadResponse->status) { |
| 112 |
$this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'error', $uploadResponse, $entryDetails); |
| 113 |
} else { |
| 114 |
$this->_logResponse->apiResponse($this->_logID, $this->_integId, ['type' => 'upload', 'type_name' => 'file'], 'success', $uploadResponse, $entryDetails); |
| 115 |
} |
| 116 |
return $uploadedFiles; |
| 117 |
} |
| 118 |
return $uploadResponse; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), |
| 123 |
* so reduce each to the stored file name before resolving against the entry directory. |
| 124 |
* |
| 125 |
* @param mixed $files file name(s) or URL(s) |
| 126 |
* |
| 127 |
* @return mixed |
| 128 |
*/ |
| 129 |
private static function normalizeFileNames($files) |
| 130 |
{ |
| 131 |
if (is_array($files)) { |
| 132 |
return array_map([__CLASS__, 'normalizeFileNames'], $files); |
| 133 |
} |
| 134 |
return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; |
| 135 |
} |
| 136 |
} |
| 137 |
|