PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.1
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 / ZohoBigin / FilesApiHelper.php

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

87 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $uploadFileEndpoint = '';
50
51 if ($isPhoto) {
52 $uploadFileEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}/{$recordID}/photo";
53 } else {
54 $uploadFileEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}/{$recordID}/Attachments";
55 }
56
57 $payload = '';
58 if (is_array($files)) {
59 foreach ($files as $fileIndex => $fileName) {
60 if (file_exists("{$this->_basepath}{$fileName}")) {
61 $payload .= '--' . $this->_payloadBoundary;
62 $payload .= "\r\n";
63 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
64 '"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n";
65 $payload .= "\r\n";
66 $payload .= file_get_contents("{$this->_basepath}{$fileName}");
67 $payload .= "\r\n";
68 }
69 }
70 } elseif (file_exists("{$this->_basepath}{$files}")) {
71 $payload .= '--' . $this->_payloadBoundary;
72 $payload .= "\r\n";
73 $payload .= 'Content-Disposition: form-data; name="' . 'file' .
74 '"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n";
75 $payload .= "\r\n";
76 $payload .= file_get_contents("{$this->_basepath}{$files}");
77 $payload .= "\r\n";
78 }
79 if (empty($payload)) {
80 return false;
81 }
82 $payload .= '--' . $this->_payloadBoundary . '--';
83
84 return HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader);
85 }
86 }
87