| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoMail Files Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoMail; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
|
| 12 |
/** |
| 13 |
* Provide functionality for Upload files |
| 14 |
*/ |
| 15 |
final class FilesApiHelper |
| 16 |
{ |
| 17 |
private $_defaultHeader; |
| 18 |
private $_payloadBoundary; |
| 19 |
private $_basepath; |
| 20 |
|
| 21 |
public function __construct($tokenDetails, $formID, $entryID) |
| 22 |
{ |
| 23 |
$this->_payloadBoundary = wp_generate_password(24); |
| 24 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 25 |
$this->_defaultHeader['Content-Type'] = 'application/octet-stream; boundary=' . $this->_payloadBoundary; |
| 26 |
$this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 27 |
} |
| 28 |
|
| 29 |
public function uploadFiles($files, $accountId, $dataCenter) |
| 30 |
{ |
| 31 |
$uploadFileEndpoint = "https://mail.zoho.{$dataCenter}/api/accounts/{$accountId}/messages/attachments"; |
| 32 |
|
| 33 |
$payload = ''; |
| 34 |
|
| 35 |
if (file_exists("{$this->_basepath}{$files}")) { |
| 36 |
$uploadFileEndpoint .= '?fileName=' . basename("{$this->_basepath}{$files}"); |
| 37 |
$payload .= '--' . $this->_payloadBoundary; |
| 38 |
$payload .= "\r\n"; |
| 39 |
$payload .= 'Content-Disposition: form-data; Content-Transfer-Encoding: binary; name="' . 'file' . |
| 40 |
'"; fileName="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n"; |
| 41 |
$payload .= "\r\n"; |
| 42 |
$payload .= file_get_contents("{$this->_basepath}{$files}"); |
| 43 |
$payload .= "\r\n"; |
| 44 |
$payload .= '--' . $this->_payloadBoundary . '--'; |
| 45 |
|
| 46 |
$uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 47 |
|
| 48 |
return $uploadResponse; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|