| 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 |
self::ensureProtectedUploadDir(); |
| 23 |
|
| 24 |
|
| 25 |
$uploadInfo = FileSystem::setSubDir('temp_files')->put($file); |
| 26 |
if (!empty($uploadInfo) && is_array($uploadInfo)) { |
| 27 |
return $uploadInfo; |
| 28 |
} |
| 29 |
|
| 30 |
return new \WP_Error('file_upload_error', __('File upload failed', 'fluent-support')); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Create the plugin's upload folder with a blank index and a deny-all .htaccess. |
| 35 |
*/ |
| 36 |
public static function ensureProtectedUploadDir() |
| 37 |
{ |
| 38 |
$uploadDir = wp_upload_dir(); |
| 39 |
$fluentSupportFolder = $uploadDir['basedir'] . DIRECTORY_SEPARATOR . FLUENT_SUPPORT_UPLOAD_DIR; |
| 40 |
if (!file_exists($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html')) { |
| 41 |
if (!is_dir($fluentSupportFolder)) { |
| 42 |
@mkdir($fluentSupportFolder, 0755); |
| 43 |
} |
| 44 |
|
| 45 |
@file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html', ''); |
| 46 |
@file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . '.htaccess', 'deny from all'); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
private function handleUploadToLocal($ticketId, $file) |
| 51 |
{ |
| 52 |
$uploadInfo = FileSystem::setSubDir('ticket_' . $ticketId)->put($file); |
| 53 |
if (!empty($uploadInfo) && is_array($uploadInfo)) { |
| 54 |
return $uploadInfo; |
| 55 |
} else { |
| 56 |
return [ |
| 57 |
'file_path' => '', |
| 58 |
'url' => '', |
| 59 |
'name' => '', |
| 60 |
'type' => '', |
| 61 |
'size' => '', |
| 62 |
]; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param array $file file data from request |
| 68 |
* @param int $ticketId ticket id |
| 69 |
* @return array $uploadedFiles uploaded file data |
| 70 |
*/ |
| 71 |
public function _handleFileUpload($file, $ticketId) |
| 72 |
{ |
| 73 |
$this->uploadedFiles = $this->handleUploadToLocal($ticketId, $file); |
| 74 |
|
| 75 |
return $this->uploadedFiles; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Download an email attachment into the ticket folder (or temp_files when there's no ticket). |
| 80 |
* |
| 81 |
* @param array $file file data from request |
| 82 |
* @param int $ticketId ticket id |
| 83 |
* @param array $acceptedMimes accepted mime types for file upload |
| 84 |
* @return array|null $uploadedFiles uploaded file data |
| 85 |
*/ |
| 86 |
public function _handleEmailAttachments($file, $ticketId = null, $acceptedMimes = []) |
| 87 |
{ |
| 88 |
$fileContent = $this->requestContent($file['url'], $acceptedMimes); |
| 89 |
|
| 90 |
if (!$fileContent) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
$targetFolder = 'temp_files'; |
| 95 |
|
| 96 |
if ($ticketId) { |
| 97 |
$targetFolder = 'ticket_' . $ticketId; |
| 98 |
} |
| 99 |
|
| 100 |
$fileData = FileSystem::setSubDir($targetFolder)->putAsContent($file['filename'], $fileContent); |
| 101 |
|
| 102 |
if (!$fileData || empty($fileData['file'])) { |
| 103 |
return null; |
| 104 |
} |
| 105 |
|
| 106 |
$contentType = $fileData['type']; |
| 107 |
if (!$contentType) { |
| 108 |
$contentType = $file['contentType']; |
| 109 |
} |
| 110 |
|
| 111 |
// Shape expected by the cloud upload drivers. |
| 112 |
return [ |
| 113 |
'name' => $file['filename'], |
| 114 |
'type' => $contentType, |
| 115 |
'file' => $fileData['file'], |
| 116 |
'url' => $fileData['url'], |
| 117 |
]; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* @param string $contentUrl |
| 122 |
* @param array $acceptedMimes accepted mime types for file upload |
| 123 |
* @return string|null Response body, or null on error, rejected type or non-2xx status. |
| 124 |
*/ |
| 125 |
public function requestContent($contentUrl, $acceptedMimes = []) |
| 126 |
{ |
| 127 |
$response = wp_safe_remote_request($contentUrl, [ |
| 128 |
'method' => 'GET', |
| 129 |
'timeout' => 30 |
| 130 |
]); |
| 131 |
|
| 132 |
if (is_wp_error($response)) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$contentType = wp_remote_retrieve_header($response, 'content-type'); |
| 137 |
|
| 138 |
if ($acceptedMimes && !in_array($contentType, $acceptedMimes)) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (wp_remote_retrieve_response_code($response) >= 300) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
return wp_remote_retrieve_body($response); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public static function isLocalUploadDisable() |
| 151 |
{ |
| 152 |
return Meta::where('key', 'disable_local_upload') |
| 153 |
->where('object_type', 'enabled_upload_drivers') |
| 154 |
->exists(); |
| 155 |
} |
| 156 |
|
| 157 |
public static function isIntegratedDriversEnable() |
| 158 |
{ |
| 159 |
return Meta::where('key', '!=', 'disable_local_upload') |
| 160 |
->where('object_type', 'enabled_upload_drivers') |
| 161 |
->exists(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
public static function __callStatic($method, $params) |
| 166 |
{ |
| 167 |
$instance = new static; |
| 168 |
|
| 169 |
return call_user_func_array([$instance, $method], $params); |
| 170 |
} |
| 171 |
|
| 172 |
public function __call($method, $params) |
| 173 |
{ |
| 174 |
$hiddenMethod = "_" . $method; |
| 175 |
|
| 176 |
$method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method; |
| 177 |
|
| 178 |
return call_user_func_array([$this, $method], $params); |
| 179 |
} |
| 180 |
} |
| 181 |
|