| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Services\Includes; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Meta; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
|
| 8 |
class UploadService |
| 9 |
{ |
| 10 |
|
| 11 |
public static function copyFileTicketFolder($tempPath, $ticketId) |
| 12 |
{ |
| 13 |
if (!file_exists($tempPath)) { |
| 14 |
return false; |
| 15 |
} |
| 16 |
|
| 17 |
return FileSystem::setSubDir('ticket_' . $ticketId)->copy($tempPath); |
| 18 |
} |
| 19 |
|
| 20 |
public static function handleTempFileUpload($file) |
| 21 |
{ |
| 22 |
|
| 23 |
$uploadDir = wp_upload_dir(); |
| 24 |
$fluentSupportFolder = $uploadDir['basedir'] . DIRECTORY_SEPARATOR . FLUENT_SUPPORT_UPLOAD_DIR; |
| 25 |
if (!file_exists($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html')) { |
| 26 |
if (!is_dir($fluentSupportFolder)) { |
| 27 |
// create the folder |
| 28 |
@mkdir($fluentSupportFolder, 0755); |
| 29 |
} |
| 30 |
|
| 31 |
@file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html', ''); |
| 32 |
@file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . '.htaccess', 'deny from all'); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
$uploadInfo = FileSystem::setSubDir('temp_files')->put($file); |
| 37 |
if (!empty($uploadInfo) && is_array($uploadInfo)) { |
| 38 |
return $uploadInfo; |
| 39 |
} |
| 40 |
|
| 41 |
return new \WP_Error('file_upload_error', __('File upload failed', 'fluent-support')); |
| 42 |
} |
| 43 |
|
| 44 |
private function handleUploadToLocal($ticketId, $file) |
| 45 |
{ |
| 46 |
$uploadInfo = FileSystem::setSubDir('ticket_' . $ticketId)->put($file); |
| 47 |
if (!empty($uploadInfo) && is_array($uploadInfo)) { |
| 48 |
return $uploadInfo; |
| 49 |
} else { |
| 50 |
return [ |
| 51 |
'file_path' => '', |
| 52 |
'url' => '', |
| 53 |
'name' => '', |
| 54 |
'type' => '', |
| 55 |
'size' => '', |
| 56 |
]; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* |
| 62 |
* |
| 63 |
* @param array $file file data from request |
| 64 |
* @param int $ticketId ticket id |
| 65 |
* @return array $uploadedFiles uploaded file data |
| 66 |
*/ |
| 67 |
public function _handleFileUpload($file, $ticketId) |
| 68 |
{ |
| 69 |
$this->uploadedFiles = $this->handleUploadToLocal($ticketId, $file); |
| 70 |
|
| 71 |
return $this->uploadedFiles; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Handle email attachments |
| 76 |
* @param array $file file data from request |
| 77 |
* @param int $ticketId ticket id |
| 78 |
* @param array $acceptedMimes accepted mime types for file upload |
| 79 |
* @return array|null $uploadedFiles uploaded file data |
| 80 |
*/ |
| 81 |
public function _handleEmailAttachments($file, $ticketId = null, $acceptedMimes = []) |
| 82 |
{ |
| 83 |
$fileContent = $this->requestContent($file['url'], $acceptedMimes); |
| 84 |
|
| 85 |
if (!$fileContent) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
$targetFolder = 'temp_files'; |
| 90 |
|
| 91 |
if ($ticketId) { |
| 92 |
$targetFolder = 'ticket_' . $ticketId; |
| 93 |
} |
| 94 |
|
| 95 |
$fileData = FileSystem::setSubDir($targetFolder)->putAsContent($file['filename'], $fileContent); |
| 96 |
|
| 97 |
if (!$fileData || empty($fileData['file'])) { |
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
$contentType = $fileData['type']; |
| 102 |
if (!$contentType) { |
| 103 |
$contentType = $file['contentType']; |
| 104 |
} |
| 105 |
|
| 106 |
// this is the file data from email attachment and required for upload to cloud |
| 107 |
return [ |
| 108 |
'name' => $file['filename'], |
| 109 |
'type' => $contentType, |
| 110 |
'file' => $fileData['file'], |
| 111 |
'url' => $fileData['url'], |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get file content from url |
| 117 |
* @param array $attachment file data from request |
| 118 |
* @param array $acceptedMimes accepted mime types for file upload |
| 119 |
* @return array $uploadedFiles uploaded file data |
| 120 |
*/ |
| 121 |
public function requestContent($contentUrl, $acceptedMimes = []) |
| 122 |
{ |
| 123 |
$response = wp_remote_request($contentUrl, [ |
| 124 |
'sslverify' => false, |
| 125 |
'method' => 'GET', |
| 126 |
'timeout' => 30 |
| 127 |
]); |
| 128 |
|
| 129 |
if (is_wp_error($response)) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$contentType = wp_remote_retrieve_header($response, 'content-type'); |
| 134 |
|
| 135 |
if ($acceptedMimes && !in_array($contentType, $acceptedMimes)) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if (wp_remote_retrieve_response_code($response) >= 300) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
return wp_remote_retrieve_body($response); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
// Verify if local upload is enable or not |
| 148 |
public static function isLocalUploadDisable() |
| 149 |
{ |
| 150 |
return Meta::where('key', 'disable_local_upload') |
| 151 |
->where('object_type', 'enabled_upload_drivers') |
| 152 |
->exists(); |
| 153 |
} |
| 154 |
|
| 155 |
// Verify if there's any integrated drivers enable |
| 156 |
public static function isIntegratedDriversEnable() |
| 157 |
{ |
| 158 |
return Meta::where('key', '!=', 'disable_local_upload') |
| 159 |
->where('object_type', 'enabled_upload_drivers') |
| 160 |
->exists(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
public static function __callStatic($method, $params) |
| 165 |
{ |
| 166 |
$instance = new static; |
| 167 |
|
| 168 |
return call_user_func_array([$instance, $method], $params); |
| 169 |
} |
| 170 |
|
| 171 |
public function __call($method, $params) |
| 172 |
{ |
| 173 |
$hiddenMethod = "_" . $method; |
| 174 |
|
| 175 |
$method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method; |
| 176 |
|
| 177 |
return call_user_func_array([$this, $method], $params); |
| 178 |
} |
| 179 |
} |
| 180 |
|