| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoBigin Files Api |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoBigin; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Util\FileHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provide functionality for Upload files |
| 15 |
*/ |
| 16 |
final class FilesApiHelper |
| 17 |
{ |
| 18 |
private $_defaultHeader; |
| 19 |
private $_apiDomain; |
| 20 |
private $_payloadBoundary; |
| 21 |
private $_basepath; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param String $module zoho bigin module dname |
| 25 |
* @param Object $tokenDetails Api token details |
| 26 |
* @param Integer $formID ID of the form, for which integration is executing |
| 27 |
* @param Integer $entryID Current submittion ID |
| 28 |
*/ |
| 29 |
public function __construct($tokenDetails, $formID, $entryID) |
| 30 |
{ |
| 31 |
$this->_payloadBoundary = wp_generate_password(24); |
| 32 |
$this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 33 |
$this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary; |
| 34 |
$this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 35 |
$this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Helps to execute upload files api |
| 40 |
* |
| 41 |
* @param Mixed $files Files path |
| 42 |
* @param Mixed $recordID Record id |
| 43 |
* @param String $zohoField zoho bigin upload fieldname |
| 44 |
* |
| 45 |
* @return mixed $uploadedFiles ID's of uploaded file in Zoho Bigin |
| 46 |
*/ |
| 47 |
public function uploadFiles($files, $module, $recordID, $isPhoto = null) |
| 48 |
{ |
| 49 |
$files = self::normalizeFileNames($files); |
| 50 |
$uploadFileEndpoint = ''; |
| 51 |
|
| 52 |
if ($isPhoto) { |
| 53 |
$uploadFileEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}/{$recordID}/photo"; |
| 54 |
} else { |
| 55 |
$uploadFileEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}/{$recordID}/Attachments"; |
| 56 |
} |
| 57 |
|
| 58 |
$payload = ''; |
| 59 |
if (is_array($files)) { |
| 60 |
foreach ($files as $fileIndex => $fileName) { |
| 61 |
if (file_exists("{$this->_basepath}{$fileName}")) { |
| 62 |
$payload .= '--' . $this->_payloadBoundary; |
| 63 |
$payload .= "\r\n"; |
| 64 |
$payload .= 'Content-Disposition: form-data; name="' . 'file' . |
| 65 |
'"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n"; |
| 66 |
$payload .= "\r\n"; |
| 67 |
$payload .= file_get_contents("{$this->_basepath}{$fileName}"); |
| 68 |
$payload .= "\r\n"; |
| 69 |
} |
| 70 |
} |
| 71 |
} elseif (file_exists("{$this->_basepath}{$files}")) { |
| 72 |
$payload .= '--' . $this->_payloadBoundary; |
| 73 |
$payload .= "\r\n"; |
| 74 |
$payload .= 'Content-Disposition: form-data; name="' . 'file' . |
| 75 |
'"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n"; |
| 76 |
$payload .= "\r\n"; |
| 77 |
$payload .= file_get_contents("{$this->_basepath}{$files}"); |
| 78 |
$payload .= "\r\n"; |
| 79 |
} |
| 80 |
if (empty($payload)) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
$payload .= '--' . $this->_payloadBoundary . '--'; |
| 84 |
|
| 85 |
return HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), |
| 90 |
* so reduce each to the stored file name before resolving against the entry directory. |
| 91 |
* |
| 92 |
* @param mixed $files file name(s) or URL(s) |
| 93 |
* |
| 94 |
* @return mixed |
| 95 |
*/ |
| 96 |
private static function normalizeFileNames($files) |
| 97 |
{ |
| 98 |
if (is_array($files)) { |
| 99 |
return array_map([__CLASS__, 'normalizeFileNames'], $files); |
| 100 |
} |
| 101 |
return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; |
| 102 |
} |
| 103 |
} |
| 104 |
|