PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.1
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.1
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 / Http / Controllers / UploaderController.php

UploaderController.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.1, at app/Http/Controllers/UploaderController.php

194 lines 6.5 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\Http\Controllers;
4
5 use FluentSupport\App\Models\Attachment;
6 use FluentSupport\App\Models\Ticket;
7 use FluentSupport\App\Services\EmailNotification\Settings;
8 use FluentSupport\App\Services\Helper;
9 use FluentSupport\Framework\Request\Request;
10 use FluentSupport\App\Services\Includes\UploadService;
11
12 /**
13 * UploaderController class is responsible for uploading file
14 * @package FluentSupport\App\Http\Controllers
15 *
16 * @version 1.0.0
17 */
18 class UploaderController extends Controller
19 {
20 /**
21 * uploadTicketFiles method will upload all the attached file in a ticket
22 * @param Request $request
23 * @return array[]
24 * @throws \FluentSupport\Framework\Validator\ValidationException
25 */
26 public function uploadTicketFiles(Request $request)
27 {
28 $settings = (new Settings())->globalBusinessSettings();
29 $maxFileSize = floatval($settings['max_file_size']);
30 $mimeHeadings = Helper::getAcceptedMimeHeadings();
31 $maxSizeBytes = $maxFileSize * 1024;
32 $imageType = $request->type ? $request->type : null;
33
34 $this->validateUploadedFiles($request->files(), $maxSizeBytes, $mimeHeadings, $maxFileSize);
35 $ticketId = $this->resolveTicketId($request);
36 $person = $this->resolvePerson($ticketId, $request);
37
38 $this->checkPermissionToUploadFile($person);
39
40 try {
41 $uploadedFiles = UploadService::handleTempFileUpload($request->files());
42 } catch (\Exception $e) {
43 return $this->sendError([
44 'message' => $e->getMessage(),
45 ]);
46 }
47
48 if (is_wp_error($uploadedFiles)) {
49 return $this->sendError([
50 'message' => $uploadedFiles->get_error_message(),
51 ]);
52 }
53
54 $attachmentHashes = $this->createAttachmentRecords($uploadedFiles, $ticketId, $person, $imageType);
55
56 return [
57 'attachments' => $attachmentHashes,
58 ];
59 }
60
61 private function validateUploadedFiles($files, $maxSizeBytes, $mimeHeadings, $maxFileSize)
62 {
63 $validationRules = [
64 'file' => 'max:' . $maxSizeBytes . '|mimetypes:' . implode(',', Helper::ticketAcceptedFileMiles()),
65 ];
66
67 $validationMessages = [
68 'file.mimetypes' => sprintf(__('Only %s files are allowed.', 'fluent-support'), implode(', ', $mimeHeadings)),
69 'file.max' => sprintf(__('The file cannot be more than %.01fMB. Please upload somewhere like Dropbox/Google Drive and paste the link in the response', 'fluent-support'), $maxFileSize),
70 ];
71
72 $this->validate($files, $validationRules, $validationMessages);
73 }
74
75 private function resolveTicketId($request)
76 {
77 $ticketId = $request->getSafe('ticket_id', 'intval');
78 return $ticketId == 'undefined' ? null : $ticketId;
79 }
80
81 private function resolvePerson($ticketId, Request $request)
82 {
83 if ($request->get('is_agent') == 'yes') {
84 return Helper::getCurrentAgent();
85 }
86
87 if ($ticketId && Helper::isPublicSignedTicketEnabled()) {
88 $intendedTicketHash = $request->getSafe('intended_ticket_hash', 'sanitize_text_field');
89 if ($intendedTicketHash && $intendedTicketHash != 'undefined') {
90 $ticket = Ticket::with(['customer'])
91 ->where('hash', $intendedTicketHash)
92 ->find($ticketId);
93
94 if ($ticket && $ticket->customer) {
95 return $ticket->customer;
96 }
97 }
98 }
99
100 return Helper::getCurrentPerson();
101 }
102
103 private function checkPermissionToUploadFile($person)
104 {
105 if (!$person) {
106 return $this->sendError([
107 'message' => __('You do not have permission to upload a file', 'fluent-support'),
108 ]);
109 }
110
111 if ($person->person_type === 'customer') {
112 $disabledFields = apply_filters('fluent_support/disabled_ticket_fields', []);
113 if (in_array('file_upload', $disabledFields)) {
114 return $this->sendError([
115 'message' => __('You do not have permission to upload a file', 'fluent-support'),
116 ]);
117 }
118 }
119 }
120
121 private function createAttachmentRecords($uploadedFiles, $ticketId, $person, $imageType)
122 {
123 $attachments = [];
124 $full_path = null;
125
126 foreach ($uploadedFiles as $file) {
127 if (empty($file['file_path'])) continue;
128
129 $fileData = [
130 'ticket_id' => intval($ticketId) ?: NULL,
131 'person_id' => intval($person->id),
132 'file_type' => $file['type'],
133 'file_path' => $file['file_path'],
134 'full_url' => esc_url($file['url']),
135 'title' => sanitize_file_name($file['name']),
136 'driver' => 'local',
137 'status' => 'in-active',
138 'settings' => [
139 'local_temp_path' => $file['file_path'],
140 ]
141 ];
142
143 if($imageType == 'direct_paste'){
144 $full_path = esc_url($file['url']);
145 }
146
147 try {
148 $attachment = Attachment::create($fileData);
149 $attachments[] = $attachment->file_hash;
150 do_action('fluent_support/attachment_uploaded_as_temp', $attachment, $ticketId);
151 $driver = Helper::getUploadDriverKey();
152
153 do_action_ref_array('fluent_support/attachment_uploaded_as_temp_' . $driver, [&$attachment, $ticketId]);
154 } catch (\Exception $exception) {
155 continue;
156 }
157 }
158
159 return $imageType == 'direct_paste' ? $full_path : $attachments;
160 }
161
162 public function uploadImage(Request $request)
163 {
164 $images = $request->files();
165 $ticketId = $this->resolveTicketId($request);
166 $this->isValidImageType($images);
167
168 try {
169 $uploadedFiles = UploadService::handleUploadToLocal($ticketId, $images);
170 } catch (\Exception $e) {
171 return $this->sendError([
172 'message' => $e->getMessage(),
173 ]);
174 }
175
176 return [
177 'images' => $uploadedFiles,
178 ];
179
180 }
181
182 private function isValidImageType($image)
183 {
184 $imageType = $image['image']->getClientOriginalExtension();
185 $supportedTypes = ['gif', 'ief', 'jpeg', 'webp', 'pjpeg', 'ktx', 'png'];
186
187 if (! in_array($imageType, $supportedTypes)) {
188 return $this->sendError([
189 'message' => 'Invalid image file.',
190 ]);
191 }
192 }
193 }
194