PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Services / Includes / UploadService.php

UploadService.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at app/Services/Includes/UploadService.php

179 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_safe_remote_request($contentUrl, [
124 'method' => 'GET',
125 'timeout' => 30
126 ]);
127
128 if (is_wp_error($response)) {
129 return;
130 }
131
132 $contentType = wp_remote_retrieve_header($response, 'content-type');
133
134 if ($acceptedMimes && !in_array($contentType, $acceptedMimes)) {
135 return;
136 }
137
138 if (wp_remote_retrieve_response_code($response) >= 300) {
139 return;
140 }
141
142 return wp_remote_retrieve_body($response);
143 }
144
145
146 // Verify if local upload is enable or not
147 public static function isLocalUploadDisable()
148 {
149 return Meta::where('key', 'disable_local_upload')
150 ->where('object_type', 'enabled_upload_drivers')
151 ->exists();
152 }
153
154 // Verify if there's any integrated drivers enable
155 public static function isIntegratedDriversEnable()
156 {
157 return Meta::where('key', '!=', 'disable_local_upload')
158 ->where('object_type', 'enabled_upload_drivers')
159 ->exists();
160 }
161
162
163 public static function __callStatic($method, $params)
164 {
165 $instance = new static;
166
167 return call_user_func_array([$instance, $method], $params);
168 }
169
170 public function __call($method, $params)
171 {
172 $hiddenMethod = "_" . $method;
173
174 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
175
176 return call_user_func_array([$this, $method], $params);
177 }
178 }
179