PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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 / Telegram / FilesApiHelper.php

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

88 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Telegram Files Api
4 *
5 */
6
7 namespace BitCode\BitForm\Core\Integration\Telegram;
8
9 /**
10 * Provide functionality for Upload files
11 */
12 final class FilesApiHelper {
13 private $_defaultHeader;
14 private $_payloadBoundary;
15 private $_basepath;
16
17 /**
18 *
19 * @param Integer $formID ID of the form, for which integration is executing
20 * @param Integer $entryID Current submission ID
21 */
22 public function __construct($formID, $entryID) {
23 $this->_payloadBoundary = wp_generate_password(24);
24 $this->_defaultHeader['Content-Type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary;
25 $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR;
26 }
27
28 /**
29 * Helps to execute upload files api
30 *
31 * @param String $apiEndPoint Telegram API base URL
32 * @param Array $data Data to pass to API
33 *
34 * @return Array $uploadResponse Telegram API response
35 */
36 public function uploadFiles($apiEndPoint, $data) {
37 $mimeType = mime_content_type("{$this->_basepath}{$data['photo']}");
38 $fileType = \explode('/', $mimeType);
39
40 switch ($fileType[0]) {
41 case 'image':
42 $apiMethod = '/sendPhoto';
43 $param = 'photo';
44 break;
45
46 case 'audio':
47 $apiMethod = '/sendAudio';
48 $param = 'audio';
49 break;
50 case 'video':
51 $apiMethod = '/sendVideo';
52 $param = 'video';
53 break;
54
55 default:
56 $apiMethod = '/sendDocument';
57 $param = 'document';
58 break;
59 }
60 $uploadFileEndpoint = $apiEndPoint . $apiMethod;
61
62 $data[$param] = new \CURLFILE("{$this->_basepath}{$data['photo']}");
63 if ('photo' !== $param) {
64 unset($data['photo']);
65 }
66 $curl = curl_init();
67 curl_setopt_array(
68 $curl,
69 [
70 CURLOPT_URL => $uploadFileEndpoint,
71 CURLOPT_RETURNTRANSFER => true,
72 CURLOPT_ENCODING => '',
73 CURLOPT_MAXREDIRS => 10,
74 CURLOPT_TIMEOUT => 0,
75 CURLOPT_FOLLOWLOCATION => true,
76 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
77 CURLOPT_CUSTOMREQUEST => 'POST',
78 CURLOPT_POSTFIELDS => $data,
79 ]
80 );
81
82 $uploadResponse = curl_exec($curl);
83
84 curl_close($curl);
85 return $uploadResponse;
86 }
87 }
88