PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 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 All 69 releases
← All changes | app/Http/Controllers/MailBoxController.php +190 -109 1.5.1 → 2.4.5 View file →
@@ -2,170 +2,251 @@
2 2
3 3 namespace FluentSupport\App\Http\Controllers;
4 4
5 5 use FluentSupport\App\Models\MailBox;
6 -use FluentSupport\App\Models\Ticket;
6 +use FluentSupport\App\Modules\PermissionManager;
7 7 use FluentSupport\App\Services\EmailNotification\Settings;
8 -use FluentSupport\Framework\Request\Request;
8 +use FluentSupport\App\Services\Helper;
9 +use FluentSupport\App\Services\MailerInbox\MailBoxService;
10 +use FluentSupport\Framework\Http\Request\Request;
9 11
10 12 class MailBoxController extends Controller
11 13 {
12 - public function index(Request $request)
14 + /**
15 + * @param MailBoxService $mailboxService
16 + * @return array
17 + */
18 + public function index(MailBoxService $mailboxService)
13 19 {
14 - $mailboxes = MailBox::all();
20 + return $mailboxService->getMailBoxes();
21 + }
15 22
16 - foreach ($mailboxes as $mailbox) {
17 - $mailbox->tickets_count = Ticket::where('mailbox_id', $mailbox->id)->count();
18 - }
19 -
23 + /**
24 + * @param MailBox $mailBox
25 + * @param int $id
26 + * @return mixed
27 + */
28 + public function get( MailBox $mailBox, $id )
29 + {
20 30 return [
21 - 'mailboxes' => $mailboxes
31 + 'mailbox' => $mailBox->getMailBox( $id )
22 32 ];
33 +
23 34 }
24 35
25 - public function get(Request $request, $id)
26 - {
27 - $mailbox = MailBox::findOrFail($id);
28 36
29 - return $this->sendSuccess([
30 - 'mailbox' => $mailbox
31 - ]);
32 - }
33 -
34 - public function save(Request $request)
37 + /**
38 + * @param Request $request
39 + * @param MailBox $mailBox
40 + * @return array
41 + * @throws \FluentSupport\Framework\Validator\ValidationException
42 + */
43 + public function save(Request $request, MailBox $mailBox)
35 44 {
36 - $data = $request->get('business');
37 - $data = wp_unslash($data);
45 + $data = wp_unslash( $request->get('business', null) );
46 + $data = $this->sanitizeMailboxData($data);
38 47
39 48 $this->validate($data, [
40 - 'name' => 'required',
49 + 'name' => 'required',
41 50 'email' => 'required'
42 51 ]);
43 52
44 - if($data['box_type'] == 'email') {
45 - $data['settings'] = [
46 - 'admin_email_address' => ''
47 - ];
48 - } else {
49 - $data['settings'] = [
50 - 'admin_email_address' => $data['email']
51 - ];
52 - }
53 -
54 - if (!MailBox::first()) {
55 - $data['is_default'] = 'yes';
56 - }
57 -
58 - $mailbox = MailBox::create($data);
59 -
60 53 return [
61 54 'message' => __('Mailbox has been created successfully', 'fluent-support'),
62 - 'mailbox' => $mailbox
55 + 'mailbox' => $mailBox->createMailBox( $data )
63 56 ];
64 57 }
65 58
66 - public function update(Request $request, $mailBoxId)
59 + /**
60 + * @param Request $request
61 + * @param MailBox $mailBox
62 + * @param int $id
63 + * @return array
64 + * @throws \Exception
65 + */
66 + public function update(Request $request, MailBox $mailBox, $id)
67 67 {
68 - $data = $request->get('business');
69 - $data = wp_unslash($data);
68 + try{
69 + $data = wp_unslash( $request->get('business', null) );
70 + $data = $this->sanitizeMailboxData($data);
70 71
71 - $this->validate($data, [
72 - 'name' => 'required',
73 - 'email' => 'required'
74 - ]);
72 + $this->validate($data, [
73 + 'name' => 'required',
74 + 'email' => 'required'
75 + ]);
75 76
76 - $mailbox = MailBox::findOrFail($mailBoxId);
77 -
78 - if ($data['box_type'] == 'email' && empty($data['mapped_email'])) {
79 - return $this->sendError([
80 - 'message' => __('Mapped Email Address is required', 'fluent-support')
81 - ]);
77 + return [
78 + 'message' => __( 'Mailbox has been saved', 'fluent-support' ),
79 + 'mailbox' => $mailBox->updateMailBox( $data, $id )
80 + ];
81 + }catch (\Exception $e){
82 + return [
83 + 'message' => Helper::getSafeErrorMessage($e),
84 + ];
82 85 }
83 -
84 - $mailbox->fill($data);
85 - $mailbox->save();
86 -
87 - return [
88 - 'message' => __('Mailbox has been saved', 'fluent-support'),
89 - 'mailbox' => $mailbox
90 - ];
91 86 }
92 87
93 - public function delete(Request $request, $mailBoxId)
88 + /**
89 + * Tickets move to the mailbox given by fallback_id.
90 + *
91 + * @param Request $request
92 + * @param MailBoxService $mailBoxService
93 + * @param int $id
94 + * @throws \Exception
95 + * @return array
96 + */
97 + public function delete(Request $request, MailBoxService $mailBoxService, $id)
94 98 {
95 - $fallbackId = $request->get('fallback_id');
96 -
97 - if($fallbackId == $mailBoxId) {
98 - return $this->sendError([
99 - 'message' => __('Fallback Box can not be the same as MailBox ID', 'fluent-support')
100 - ]);
99 + try {
100 + return $mailBoxService->deleteMailBox( $id, $request->getSafe('fallback_id', 'intval') );
101 + } catch (\Exception $e) {
102 + return [
103 + 'message' => Helper::getSafeErrorMessage($e),
104 + ];
101 105 }
106 + }
102 107
103 - $box = MailBox::findOrFail($mailBoxId);
104 - $fallbackBox = MailBox::findOrFail($fallbackId);
105 108
109 + /**
110 + * @param Request $request
111 + * @param MailBoxService $mailBoxService
112 + * @param int $id
113 + * @throws \Exception
114 + * @return array
115 + */
116 + public function moveTickets(Request $request, MailBoxService $mailBoxService, $id)
117 + {
118 + try {
119 + $data = $request->only(['ticket_ids', 'new_box_id', 'move_type']);
106 120
107 - do_action('fluent_support/before_delete_email_box', $box, $fallbackBox);
108 - $box->deleteAllMeta();
109 - MailBox::where('id', $mailBoxId)->delete();
121 + // Current-user mailbox access. Checked here rather than inside
122 + // MailBoxService::moveTickets(), which is also called by Pro
123 + // workflows and MCP on behalf of an automation/agent-tool call
124 + // that isn't the logged-in browser session.
125 + $restrictedForCurrentUser = array_map('intval', PermissionManager::getRestrictedMailboxIds());
126 + if (in_array((int) $data['new_box_id'], $restrictedForCurrentUser, true)) {
127 + return $this->sendError(__('You do not have access to the specified mailbox', 'fluent-support'));
128 + }
110 129
111 - // lets transfer the tickets now
112 - Ticket::where('mailbox_id', $mailBoxId)
113 - ->update([
114 - 'mailbox_id' => $fallbackBox->id
115 - ]);
130 + return $mailBoxService->moveTickets( $data, $id );
131 + } catch (\Exception $e) {
132 + return $this->sendError(Helper::getSafeErrorMessage($e));
133 + }
134 + }
116 135
117 - do_action('fluent_support/mailbox_deleted', $mailBoxId, $fallbackBox);
136 + /**
137 + * @param Request $request
138 + * @param Settings $settings
139 + * @param int $id
140 + * @return array
141 + */
142 + public function getEmailSettings(Request $request, Settings $settings, $id)
143 + {
144 + $box = MailBox::findOrFail($id);
145 + $emailType = $request->getSafe('email_type', 'sanitize_text_field');
118 146
119 147 return [
120 - 'message' => __('Selected Business has been deleted', 'fluent-support')
148 + 'email_settings' => $settings->getBoxEmailSettings($box, $emailType)
121 149 ];
122 -
123 150 }
124 151
125 - public function getEmailSettings(Request $request, Settings $settings, $boxId)
152 + /**
153 + * @param MailBoxService $mailBoxService
154 + * @param int $id
155 + * @return array
156 + */
157 + public function getEmailsSetups( MailBoxService $mailBoxService, $id )
126 158 {
127 - $box = MailBox::findOrFail($boxId);
128 - $emailType = $request->get('email_type');
129 -
130 - return [
131 - 'email_settings' => $settings->getBoxEmailSettings($box, $emailType)
132 - ];
159 + return $mailBoxService->getEmailsSetups($id);
133 160 }
134 161
135 - public function getEmailsSetups(Request $request, Settings $settings, $boxId)
162 + /**
163 + * @param Request $request
164 + * @param MailBoxService $mailBoxService
165 + * @param int $id
166 + * @return array
167 + * @throws \FluentSupport\Framework\Validator\ValidationException
168 + */
169 + public function saveEmailSettings( Request $request, MailBoxService $mailBoxService, $id )
136 170 {
137 - $box = MailBox::findOrFail($boxId);
171 + $data = wp_unslash($request->get('email_settings', null));
172 + $data = is_array($data) ? [
173 + 'key' => isset($data['key']) ? sanitize_key($data['key']) : '',
174 + 'title' => isset($data['title']) ? sanitize_text_field($data['title']) : '',
175 + 'email_subject' => isset($data['email_subject']) ? sanitize_text_field($data['email_subject']) : '',
176 + 'email_body' => isset($data['email_body']) ? wp_kses_post($data['email_body']) : '',
177 + 'status' => isset($data['status']) ? sanitize_text_field($data['status']) : '',
178 + 'can_edit_subject' => isset($data['can_edit_subject']) ? sanitize_text_field($data['can_edit_subject']) : '',
179 + 'send_attachments' => isset($data['send_attachments']) ? sanitize_text_field($data['send_attachments']) : '',
180 + ] : [];
138 181
139 - $types = $settings->getEmailSettingsKeys();
182 + $emailType = $request->getSafe('email_type', 'sanitize_text_field');
140 183
141 - $req = [];
142 - foreach($types as $type) {
143 - $req[] = $settings->getBoxEmailSettings($box, $type);
144 - }
184 + $this->validate($data, [
185 + 'email_subject' => 'required',
186 + 'email_body' => 'required'
187 + ]);
145 188
146 - return [
147 - 'email_configs' => $req,
148 - 'email_keys' => $types
149 - ];
189 + return $mailBoxService->saveEmailSettings( $emailType, $id, $data );
150 190 }
151 191
152 - public function saveEmailSettings(Request $request, Settings $settings, $boxId)
192 + /**
193 + * @param MailBoxService $mailBoxService
194 + * @param int $id
195 + * @return array
196 + */
197 + public function setAsDefault( MailBoxService $mailBoxService, $id )
153 198 {
154 - $data = wp_unslash($request->get('email_settings'));
155 - $this->validate($data, [
156 - 'email_subject' => 'required',
157 - 'email_body' => 'required'
158 - ]);
199 + return $mailBoxService->setAsDefault( $id );
200 + }
159 201
160 - $data['email_body'] = wp_kses_post($data['email_body']);
202 + /**
203 + * @param Request $request
204 + * @param MailBoxService $mailBoxService
205 + * @param int $id
206 + * @return array
207 + */
208 + public function getTickets(Request $request, MailBoxService $mailBoxService, $id)
209 + {
210 + $filters = $request->get('filters', null);
211 + $filters = is_array($filters) ? [
212 + 'status_type' => isset($filters['status_type']) ? sanitize_text_field($filters['status_type']) : '',
213 + 'customer_id' => isset($filters['customer_id']) ? intval($filters['customer_id']) : 0,
214 + 'product_id' => isset($filters['product_id']) ? intval($filters['product_id']) : 0,
215 + 'mailbox_id' => isset($filters['mailbox_id']) ? intval($filters['mailbox_id']) : 0,
216 + 'ticket_title' => isset($filters['ticket_title']) ? sanitize_text_field($filters['ticket_title']) : '',
217 + 'notes' => isset($filters['notes']) ? sanitize_text_field($filters['notes']) : '',
218 + ] : [];
161 219
162 - $box = MailBox::findOrFail($boxId);
163 - $emailType = $request->get('email_type');
220 + return $mailBoxService->getTickets( $filters, $id );
221 + }
164 222
165 - $settings->saveBoxEmailSettings($box, $emailType, $data);
223 + /**
224 + * @param mixed $data
225 + * @return array
226 + */
227 + private function sanitizeMailboxData($data)
228 + {
229 + if (!is_array($data)) {
230 + return [];
231 + }
166 232
167 - return [
168 - 'message' => __('Settings has been updated', 'fluent-support')
233 + $sanitized = [
234 + 'name' => isset($data['name']) ? sanitize_text_field($data['name']) : '',
235 + 'email' => isset($data['email']) ? sanitize_email($data['email']) : '',
236 + 'box_type' => isset($data['box_type']) ? sanitize_key($data['box_type']) : '',
237 + 'mapped_email' => isset($data['mapped_email']) ? sanitize_email($data['mapped_email']) : '',
238 + 'email_footer' => isset($data['email_footer']) ? wp_kses_post($data['email_footer']) : '',
239 + 'is_default' => isset($data['is_default']) ? sanitize_text_field($data['is_default']) : 'no',
169 240 ];
241 +
242 + if (isset($data['settings']) && is_array($data['settings'])) {
243 + $sanitized['settings'] = map_deep($data['settings'], 'sanitize_text_field');
244 + // Re-sanitize as an email, not plain text.
245 + if (isset($data['settings']['admin_email_address'])) {
246 + $sanitized['settings']['admin_email_address'] = sanitize_email($data['settings']['admin_email_address']);
247 + }
248 + }
249 +
250 + return $sanitized;
170 251 }
171 252 }