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 / ZohoRecruit / 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/ZohoRecruit/FilesApiHelper.php

108 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoRecruit Files Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ZohoRecruit;
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 private $_module;
23
24 private $_dataCenter;
25
26 /**
27 * @param string $module zoho recruit module dname
28 * @param object $tokenDetails Api token details
29 * @param integer $formID ID of the form, for which integration is executing
30 * @param integer $entryID Current submission ID
31 */
32 public function __construct($module, $dataCenter, $tokenDetails, $formID, $entryID)
33 {
34 $this->_module = $module;
35 $this->_dataCenter = $dataCenter;
36 $this->_payloadBoundary = wp_generate_password(24);
37 $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}";
38 $this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary;
39 $this->_apiDomain = \urldecode($tokenDetails->api_domain);
40 $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR;
41 }
42
43 /**
44 * Helps to execute upload files api
45 *
46 * @param mixed $files Files path
47 * @param mixed $recordID Record id
48 * @param string $zohoField zoho recruit upload fieldname
49 *
50 * @return array $uploadedFiles ID's of uploaded file in Zoho Recruit
51 */
52 public function uploadFiles($files, $recordID, $zohoField)
53 {
54 $files = self::normalizeFileNames($files);
55 $uploadFileEndpoint = '';
56
57 if ('Candidate Photo' === $zohoField) {
58 $uploadFileEndpoint = "https://recruit.zoho.{$this->_dataCenter}/recruit/private/xml/{$this->_module}/uploadPhoto?Scope=recruitapi&type={$zohoField}&version=2&id={$recordID}";
59 } else {
60 $uploadFileEndpoint = "https://recruit.zoho.{$this->_dataCenter}/recruit/private/json/{$this->_module}/uploadFile?Scope=ZohoRecruit.modules.all&type={$zohoField}&version=2&id={$recordID}";
61 }
62
63 $payload = '';
64 if (is_array($files)) {
65 foreach ($files as $fileIndex => $fileName) {
66 if (file_exists("{$this->_basepath}{$fileName}")) {
67 $payload .= '--' . $this->_payloadBoundary;
68 $payload .= "\r\n";
69 $payload .= 'Content-Disposition: form-data; name="' . 'content' .
70 '"; filename="' . basename("{$this->_basepath}{$fileName}") . '"' . "\r\n";
71 $payload .= "\r\n";
72 $payload .= file_get_contents("{$this->_basepath}{$fileName}");
73 $payload .= "\r\n";
74 }
75 }
76 } elseif (file_exists("{$this->_basepath}{$files}")) {
77 $payload .= '--' . $this->_payloadBoundary;
78 $payload .= "\r\n";
79 $payload .= 'Content-Disposition: form-data; name="' . 'content' .
80 '"; filename="' . basename("{$this->_basepath}{$files}") . '"' . "\r\n";
81 $payload .= "\r\n";
82 $payload .= file_get_contents("{$this->_basepath}{$files}");
83 $payload .= "\r\n";
84 }
85 if (empty($payload)) {
86 return false;
87 }
88 $payload .= '--' . $this->_payloadBoundary . '--';
89 return HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader);
90 }
91
92 /**
93 * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl),
94 * so reduce each to the stored file name before resolving against the entry directory.
95 *
96 * @param mixed $files file name(s) or URL(s)
97 *
98 * @return mixed
99 */
100 private static function normalizeFileNames($files)
101 {
102 if (is_array($files)) {
103 return array_map([__CLASS__, 'normalizeFileNames'], $files);
104 }
105 return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files;
106 }
107 }
108