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