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

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

70 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\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 $_payloadBoundary;
20 private $_basepath;
21
22 public function __construct($tokenDetails, $formID, $entryID)
23 {
24 $this->_payloadBoundary = wp_generate_password(24);
25 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
26 $this->_defaultHeader['Content-Type'] = 'application/octet-stream; boundary=' . $this->_payloadBoundary;
27 $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
28 }
29
30 public function uploadFiles($files, $accountId, $dataCenter)
31 {
32 $files = self::normalizeFileNames($files);
33 $uploadFileEndpoint = "https://mail.zoho.{$dataCenter}/api/accounts/{$accountId}/messages/attachments";
34
35 $payload = '';
36
37 if (file_exists("{$this->_basepath}{$files}")) {
38 $uploadFileEndpoint .= '?fileName=' . basename("{$this->_basepath}{$files}");
39 $payload .= '--' . $this->_payloadBoundary;
40 $payload .= "\r\n";
41 $payload .= 'Content-Disposition: form-data; Content-Transfer-Encoding: binary; name="' . 'file' .
42 '"; fileName="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n";
43 $payload .= "\r\n";
44 $payload .= file_get_contents("{$this->_basepath}{$files}");
45 $payload .= "\r\n";
46 $payload .= '--' . $this->_payloadBoundary . '--';
47
48 $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader);
49
50 return $uploadResponse;
51 }
52 }
53
54 /**
55 * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl),
56 * so reduce each to the stored file name before resolving against the entry directory.
57 *
58 * @param mixed $files file name(s) or URL(s)
59 *
60 * @return mixed
61 */
62 private static function normalizeFileNames($files)
63 {
64 if (is_array($files)) {
65 return array_map([__CLASS__, 'normalizeFileNames'], $files);
66 }
67 return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files;
68 }
69 }
70