_payloadBoundary = wp_generate_password(24); $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; $this->_defaultHeader['orgId'] = $orgId; $this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; $this->_apiDomain = \urldecode($tokenDetails->api_domain); $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; } /** * Helps to execute upload files api * * @param mixed $files Files path * @param bool $isAttachment Check upload type * @param mixed $module Attachment Module name * @param mixed $recordID Record id * * @return mixed $uploadedFiles ID's of uploaded file in Zoho Desk */ public function uploadFiles($files, $ticketId, $dataCenter) { $files = self::normalizeFileNames($files); $uploadFileEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/tickets/{$ticketId}/attachments"; $payload = ''; if (is_array($files)) { foreach ($files as $fileIndex => $fileName) { if (file_exists("{$this->_basepath}{$fileName}")) { $payload .= '--' . $this->_payloadBoundary; $payload .= "\r\n"; $payload .= 'Content-Disposition: form-data; name="' . 'file' . '"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n"; $payload .= "\r\n"; $payload .= file_get_contents("{$this->_basepath}{$fileName}"); $payload .= "\r\n"; } } } elseif (file_exists("{$this->_basepath}{$files}")) { $payload .= '--' . $this->_payloadBoundary; $payload .= "\r\n"; $payload .= 'Content-Disposition: form-data; name="' . 'file' . '"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n"; $payload .= "\r\n"; $payload .= file_get_contents("{$this->_basepath}{$files}"); $payload .= "\r\n"; } if (empty($payload)) { return false; } $payload .= '--' . $this->_payloadBoundary . '--'; $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); return $uploadResponse; } /** * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), * so reduce each to the stored file name before resolving against the entry directory. * * @param mixed $files file name(s) or URL(s) * * @return mixed */ private static function normalizeFileNames($files) { if (is_array($files)) { return array_map([__CLASS__, 'normalizeFileNames'], $files); } return is_string($files) ? basename(parse_url($files, PHP_URL_PATH) ?: $files) : $files; } }