← All changes
|
includes/Core/Integration/Telegram/FilesApiHelper.php
+85
-8
2.0
→
2.15.3
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * Telegram Files Api |
| 4 | 5 | * |
| 5 | 6 | */ |
| @@ -8,9 +9,10 @@ | ||
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | 11 | * Provide functionality for Upload files |
| 11 | 12 | */ |
| 12 | -final class FilesApiHelper { | |
| 13 | +final class FilesApiHelper | |
| 14 | +{ | |
| 13 | 15 | private $_defaultHeader; |
| 14 | 16 | private $_payloadBoundary; |
| 15 | 17 | private $_basepath; |
| 16 | 18 | |
| @@ -18,9 +20,10 @@ | ||
| 18 | 20 | * |
| 19 | 21 | * @param Integer $formID ID of the form, for which integration is executing |
| 20 | 22 | * @param Integer $entryID Current submission ID |
| 21 | 23 | */ |
| 22 | - public function __construct($formID, $entryID) { | |
| 24 | + public function __construct($formID, $entryID) | |
| 25 | + { | |
| 23 | 26 | $this->_payloadBoundary = wp_generate_password(24); |
| 24 | 27 | $this->_defaultHeader['Content-Type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; |
| 25 | 28 | $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; |
| 26 | 29 | } |
| @@ -27,15 +30,18 @@ | ||
| 27 | 30 | |
| 28 | 31 | /** |
| 29 | 32 | * Helps to execute upload files api |
| 30 | 33 | * |
| 31 | - * @param String $apiEndPoint Telegram API base URL | |
| 32 | - * @param Array $data Data to pass to API | |
| 34 | + * @param string $apiEndPoint Telegram API base URL | |
| 35 | + * @param array $data Data to pass to API | |
| 33 | 36 | * |
| 34 | - * @return Array $uploadResponse Telegram API response | |
| 37 | + * @return array $uploadResponse Telegram API response | |
| 35 | 38 | */ |
| 36 | - public function uploadFiles($apiEndPoint, $data) { | |
| 37 | - $mimeType = mime_content_type("{$this->_basepath}{$data['photo']}"); | |
| 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); | |
| 38 | 44 | $fileType = \explode('/', $mimeType); |
| 39 | 45 | |
| 40 | 46 | switch ($fileType[0]) { |
| 41 | 47 | case 'image': |
| @@ -58,9 +64,9 @@ | ||
| 58 | 64 | break; |
| 59 | 65 | } |
| 60 | 66 | $uploadFileEndpoint = $apiEndPoint . $apiMethod; |
| 61 | 67 | |
| 62 | - $data[$param] = new \CURLFILE("{$this->_basepath}{$data['photo']}"); | |
| 68 | + $data[$param] = new \CURLFILE($filePath); | |
| 63 | 69 | if ('photo' !== $param) { |
| 64 | 70 | unset($data['photo']); |
| 65 | 71 | } |
| 66 | 72 | $curl = curl_init(); |
| @@ -82,6 +88,77 @@ | ||
| 82 | 88 | $uploadResponse = curl_exec($curl); |
| 83 | 89 | |
| 84 | 90 | curl_close($curl); |
| 85 | 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; | |
| 86 | 163 | } |
| 87 | 164 | } |