PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 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.9.1, at includes/Core/Integration/Telegram/FilesApiHelper.php

155 lines 4.0 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 $mimeType = mime_content_type("{$this->_basepath}{$data['photo']}");
42 $fileType = \explode('/', $mimeType);
43
44 switch ($fileType[0]) {
45 case 'image':
46 $apiMethod = '/sendPhoto';
47 $param = 'photo';
48 break;
49
50 case 'audio':
51 $apiMethod = '/sendAudio';
52 $param = 'audio';
53 break;
54 case 'video':
55 $apiMethod = '/sendVideo';
56 $param = 'video';
57 break;
58
59 default:
60 $apiMethod = '/sendDocument';
61 $param = 'document';
62 break;
63 }
64 $uploadFileEndpoint = $apiEndPoint . $apiMethod;
65
66 $data[$param] = new \CURLFILE("{$this->_basepath}{$data['photo']}");
67 if ('photo' !== $param) {
68 unset($data['photo']);
69 }
70 $curl = curl_init();
71 curl_setopt_array(
72 $curl,
73 [
74 CURLOPT_URL => $uploadFileEndpoint,
75 CURLOPT_RETURNTRANSFER => true,
76 CURLOPT_ENCODING => '',
77 CURLOPT_MAXREDIRS => 10,
78 CURLOPT_TIMEOUT => 0,
79 CURLOPT_FOLLOWLOCATION => true,
80 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
81 CURLOPT_CUSTOMREQUEST => 'POST',
82 CURLOPT_POSTFIELDS => $data,
83 ]
84 );
85
86 $uploadResponse = curl_exec($curl);
87
88 curl_close($curl);
89 return $uploadResponse;
90 }
91
92 public function uploadMultipleFiles($apiEndPoint, $data)
93 {
94 $param = 'media';
95 $uploadMultipleFileEndpoint = $apiEndPoint . '/sendMediaGroup';
96 $postFields = [
97 'chat_id' => $data['chat_id'],
98 'caption' => $data['caption']
99 ];
100
101 foreach ($data['media'] as $key => $value) {
102 $mimeType = mime_content_type("{$this->_basepath}{$value}");
103 $fileType = \explode("/", $mimeType);
104 unset($data['media'][$key]);
105
106 if ($fileType[0] == 'image') {
107 $type = 'photo';
108 } elseif ($fileType[0] == 'application' || $fileType[0] == 'text') {
109 $type = 'document';
110 } elseif ($fileType[0] == 'application') {
111 $type = 'document';
112 } else {
113 $type = $fileType[0];
114 }
115
116 $media[] = [
117 'type' => $type,
118 'media' => "attach://{$key}.path",
119 'caption' => $data['caption'],
120 'parse_mode' => 'HTML'
121 ];
122 $nameK = "{$key}.path";
123 $postFields[$nameK] = new \CURLFILE("{$this->_basepath}{$value}");
124 }
125 $postFields['media'] = json_encode($media);
126
127 if ($param != 'media') {
128 unset($data['media']);
129 }
130
131 $curl = curl_init();
132 curl_setopt_array(
133 $curl,
134 array(
135 CURLOPT_URL => $uploadMultipleFileEndpoint,
136 CURLOPT_RETURNTRANSFER => true,
137 CURLOPT_ENCODING => '',
138 CURLOPT_MAXREDIRS => 10,
139 CURLOPT_TIMEOUT => 0,
140 CURLOPT_FOLLOWLOCATION => true,
141 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
142 CURLOPT_CUSTOMREQUEST => 'POST',
143 CURLOPT_POSTFIELDS => $postFields,
144 CURLOPT_HTTPHEADER => array(
145 'Content-Type: multipart/form-data'
146 ),
147 )
148 );
149
150 $uploadResponse = curl_exec($curl);
151 curl_close($curl);
152 return $uploadResponse;
153 }
154 }
155