| @@ -18,14 +18,28 @@ | ||
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public static function handleTempFileUpload($file) |
| 21 | 21 | { |
| 22 | + self::ensureProtectedUploadDir(); | |
| 22 | 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 | + { | |
| 23 | 38 | $uploadDir = wp_upload_dir(); |
| 24 | 39 | $fluentSupportFolder = $uploadDir['basedir'] . DIRECTORY_SEPARATOR . FLUENT_SUPPORT_UPLOAD_DIR; |
| 25 | 40 | if (!file_exists($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html')) { |
| 26 | 41 | if (!is_dir($fluentSupportFolder)) { |
| 27 | - // create the folder | |
| 28 | 42 | @mkdir($fluentSupportFolder, 0755); |
| 29 | 43 | } |
| 30 | 44 | |
| 31 | 45 | @file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html', ''); |
| @@ -30,16 +44,8 @@ | ||
| 30 | 44 | |
| 31 | 45 | @file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . 'index.html', ''); |
| 32 | 46 | @file_put_contents($fluentSupportFolder . DIRECTORY_SEPARATOR . '.htaccess', 'deny from all'); |
| 33 | 47 | } |
| 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 | 48 | } |
| 43 | 49 | |
| 44 | 50 | private function handleUploadToLocal($ticketId, $file) |
| 45 | 51 | { |
| @@ -57,10 +63,8 @@ | ||
| 57 | 63 | } |
| 58 | 64 | } |
| 59 | 65 | |
| 60 | 66 | /** |
| 61 | - * | |
| 62 | - * | |
| 63 | 67 | * @param array $file file data from request |
| 64 | 68 | * @param int $ticketId ticket id |
| 65 | 69 | * @return array $uploadedFiles uploaded file data |
| 66 | 70 | */ |
| @@ -71,9 +75,10 @@ | ||
| 71 | 75 | return $this->uploadedFiles; |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | 78 | /** |
| 75 | - * Handle email attachments | |
| 79 | + * Download an email attachment into the ticket folder (or temp_files when there's no ticket). | |
| 80 | + * | |
| 76 | 81 | * @param array $file file data from request |
| 77 | 82 | * @param int $ticketId ticket id |
| 78 | 83 | * @param array $acceptedMimes accepted mime types for file upload |
| 79 | 84 | * @return array|null $uploadedFiles uploaded file data |
| @@ -102,9 +107,9 @@ | ||
| 102 | 107 | if (!$contentType) { |
| 103 | 108 | $contentType = $file['contentType']; |
| 104 | 109 | } |
| 105 | 110 | |
| 106 | - // this is the file data from email attachment and required for upload to cloud | |
| 111 | + // Shape expected by the cloud upload drivers. | |
| 107 | 112 | return [ |
| 108 | 113 | 'name' => $file['filename'], |
| 109 | 114 | 'type' => $contentType, |
| 110 | 115 | 'file' => $fileData['file'], |
| @@ -112,19 +117,17 @@ | ||
| 112 | 117 | ]; |
| 113 | 118 | } |
| 114 | 119 | |
| 115 | 120 | /** |
| 116 | - * Get file content from url | |
| 117 | - * @param array $attachment file data from request | |
| 121 | + * @param string $contentUrl | |
| 118 | 122 | * @param array $acceptedMimes accepted mime types for file upload |
| 119 | - * @return array $uploadedFiles uploaded file data | |
| 123 | + * @return string|null Response body, or null on error, rejected type or non-2xx status. | |
| 120 | 124 | */ |
| 121 | 125 | public function requestContent($contentUrl, $acceptedMimes = []) |
| 122 | 126 | { |
| 123 | - $response = wp_remote_request($contentUrl, [ | |
| 124 | - 'sslverify' => false, | |
| 125 | - 'method' => 'GET', | |
| 126 | - 'timeout' => 30 | |
| 127 | + $response = wp_safe_remote_request($contentUrl, [ | |
| 128 | + 'method' => 'GET', | |
| 129 | + 'timeout' => 30 | |
| 127 | 130 | ]); |
| 128 | 131 | |
| 129 | 132 | if (is_wp_error($response)) { |
| 130 | 133 | return; |
| @@ -143,9 +146,8 @@ | ||
| 143 | 146 | return wp_remote_retrieve_body($response); |
| 144 | 147 | } |
| 145 | 148 | |
| 146 | 149 | |
| 147 | - // Verify if local upload is enable or not | |
| 148 | 150 | public static function isLocalUploadDisable() |
| 149 | 151 | { |
| 150 | 152 | return Meta::where('key', 'disable_local_upload') |
| 151 | 153 | ->where('object_type', 'enabled_upload_drivers') |
| @@ -151,9 +153,8 @@ | ||
| 151 | 153 | ->where('object_type', 'enabled_upload_drivers') |
| 152 | 154 | ->exists(); |
| 153 | 155 | } |
| 154 | 156 | |
| 155 | - // Verify if there's any integrated drivers enable | |
| 156 | 157 | public static function isIntegratedDriversEnable() |
| 157 | 158 | { |
| 158 | 159 | return Meta::where('key', '!=', 'disable_local_upload') |
| 159 | 160 | ->where('object_type', 'enabled_upload_drivers') |