PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / ZohoDesk / FilesApiHelper.php

FilesApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.2.2, at includes/Core/Integration/ZohoDesk/FilesApiHelper.php

102 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoDesk Files Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoDesk;
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 *
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 submission ID
28 */
29 public function __construct($tokenDetails, $orgId, $formID, $entryID)
30 {
31 $this->_payloadBoundary = wp_generate_password(24);
32 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
33 $this->_defaultHeader['orgId'] = $orgId;
34 $this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary;
35 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
36 $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
37 }
38
39 /**
40 * Helps to execute upload files api
41 *
42 * @param mixed $files Files path
43 * @param bool $isAttachment Check upload type
44 * @param mixed $module Attachment Module name
45 * @param mixed $recordID Record id
46 *
47 * @return mixed $uploadedFiles ID's of uploaded file in Zoho Desk
48 */
49 public function uploadFiles($files, $ticketId, $dataCenter)
50 {
51 $files = self::normalizeFileNames($files);
52 $uploadFileEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/tickets/{$ticketId}/attachments";
53 $payload = '';
54 if (is_array($files)) {
55 foreach ($files as $fileIndex => $fileName) {
56 if (file_exists("{$this->_basepath}{$fileName}")) {
57 $payload .= '--' . $this->_payloadBoundary;
58 $payload .= "\r\n";
59 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
60 '"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n";
61 $payload .= "\r\n";
62 $payload .= file_get_contents("{$this->_basepath}{$fileName}");
63 $payload .= "\r\n";
64 }
65 }
66 } elseif (file_exists("{$this->_basepath}{$files}")) {
67 $payload .= '--' . $this->_payloadBoundary;
68 $payload .= "\r\n";
69 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
70 '"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n";
71 $payload .= "\r\n";
72 $payload .= file_get_contents("{$this->_basepath}{$files}");
73 $payload .= "\r\n";
74 }
75
76 if (empty($payload)) {
77 return false;
78 }
79 $payload .= '--' . $this->_payloadBoundary . '--';
80
81 $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader);
82
83 return $uploadResponse;
84 }
85
86 /**
87 * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl),
88 * so reduce each to the stored file name before resolving against the entry directory.
89 *
90 * @param mixed $files file name(s) or URL(s)
91 *
92 * @return mixed
93 */
94 private static function normalizeFileNames($files)
95 {
96 if (is_array($files)) {
97 return array_map([__CLASS__, 'normalizeFileNames'], $files);
98 }
99 return is_string($files) ? basename(parse_url($files, PHP_URL_PATH) ?: $files) : $files;
100 }
101 }
102