PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.16.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.16.2
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 2.10.2 All 137 releases
bit-form / includes / Core / Integration / Telegram / FilesApiHelper.php

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

165 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Telegram Files Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Telegram;
9
10 /**
11 * Provide functionality for Upload files
12 */
13 final class FilesApiHelper
14 {
15 private $_defaultHeader;
16 private $_payloadBoundary;
17 private $_basepath;
18
19 /**
20 *
21 * @param Integer $formID ID of the form, for which integration is executing
22 * @param Integer $entryID Current submission ID
23 */
24 public function __construct($formID, $entryID)
25 {
26 $this->_payloadBoundary = wp_generate_password(24);
27 $this->_defaultHeader['Content-Type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary;
28 $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
29 }
30
31 /**
32 * Helps to execute upload files api
33 *
34 * @param string $apiEndPoint Telegram API base URL
35 * @param array $data Data to pass to API
36 *
37 * @return array $uploadResponse Telegram API response
38 */
39 public function uploadFiles($apiEndPoint, $data)
40 {
41 $filename = $this->getFileNameWithExtension($data['photo']) ?? $data['photo'];
42 $filePath = "{$this->_basepath}{$filename}";
43 $mimeType = mime_content_type($filePath);
44 $fileType = \explode('/', $mimeType);
45
46 switch ($fileType[0]) {
47 case 'image':
48 $apiMethod = '/sendPhoto';
49 $param = 'photo';
50 break;
51
52 case 'audio':
53 $apiMethod = '/sendAudio';
54 $param = 'audio';
55 break;
56 case 'video':
57 $apiMethod = '/sendVideo';
58 $param = 'video';
59 break;
60
61 default:
62 $apiMethod = '/sendDocument';
63 $param = 'document';
64 break;
65 }
66 $uploadFileEndpoint = $apiEndPoint . $apiMethod;
67
68 $data[$param] = new \CURLFILE($filePath);
69 if ('photo' !== $param) {
70 unset($data['photo']);
71 }
72 $curl = curl_init();
73 curl_setopt_array(
74 $curl,
75 [
76 CURLOPT_URL => $uploadFileEndpoint,
77 CURLOPT_RETURNTRANSFER => true,
78 CURLOPT_ENCODING => '',
79 CURLOPT_MAXREDIRS => 10,
80 CURLOPT_TIMEOUT => 0,
81 CURLOPT_FOLLOWLOCATION => true,
82 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
83 CURLOPT_CUSTOMREQUEST => 'POST',
84 CURLOPT_POSTFIELDS => $data,
85 ]
86 );
87
88 $uploadResponse = curl_exec($curl);
89
90 curl_close($curl);
91 return $uploadResponse;
92 }
93
94 public function uploadMultipleFiles($apiEndPoint, $data)
95 {
96 $param = 'media';
97 $uploadMultipleFileEndpoint = $apiEndPoint . '/sendMediaGroup';
98 $postFields = [
99 'chat_id' => $data['chat_id'],
100 'caption' => $data['caption']
101 ];
102
103 foreach ($data['media'] as $key => $value) {
104 $filename = $this->getFileNameWithExtension($value) ?? $value;
105 $filePath = "{$this->_basepath}{$filename}";
106 $mimeType = mime_content_type($filePath);
107 $fileType = \explode('/', $mimeType);
108 unset($data['media'][$key]);
109
110 if ('image' === $fileType[0]) {
111 $type = 'photo';
112 } elseif ('application' === $fileType[0] || 'text' === $fileType[0]) {
113 $type = 'document';
114 } elseif ('application' === $fileType[0]) {
115 $type = 'document';
116 } else {
117 $type = $fileType[0];
118 }
119
120 $media[] = [
121 'type' => $type,
122 'media' => "attach://{$key}.path",
123 'caption' => $data['caption'],
124 'parse_mode' => 'HTML'
125 ];
126 $nameK = "{$key}.path";
127 $postFields[$nameK] = new \CURLFILE($filePath);
128 }
129 $postFields['media'] = wp_json_encode($media);
130
131 if ('media' !== $param) {
132 unset($data['media']);
133 }
134
135 $curl = curl_init();
136 curl_setopt_array(
137 $curl,
138 [
139 CURLOPT_URL => $uploadMultipleFileEndpoint,
140 CURLOPT_RETURNTRANSFER => true,
141 CURLOPT_ENCODING => '',
142 CURLOPT_MAXREDIRS => 10,
143 CURLOPT_TIMEOUT => 0,
144 CURLOPT_FOLLOWLOCATION => true,
145 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
146 CURLOPT_CUSTOMREQUEST => 'POST',
147 CURLOPT_POSTFIELDS => $postFields,
148 CURLOPT_HTTPHEADER => [
149 'Content-Type: multipart/form-data'
150 ],
151 ]
152 );
153
154 $uploadResponse = curl_exec($curl);
155 curl_close($curl);
156 return $uploadResponse;
157 }
158
159 private function getFileNameWithExtension($url)
160 {
161 $fileName = basename($url);
162 return false !== strpos($fileName, '.') ? $fileName : null;
163 }
164 }
165