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
fluent-support / app / Http / Controllers / MailBoxController.php

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

253 lines 8.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\MailBox;
6 use FluentSupport\App\Modules\PermissionManager;
7 use FluentSupport\App\Services\EmailNotification\Settings;
8 use FluentSupport\App\Services\Helper;
9 use FluentSupport\App\Services\MailerInbox\MailBoxService;
10 use FluentSupport\Framework\Http\Request\Request;
11
12 class MailBoxController extends Controller
13 {
14 /**
15 * @param MailBoxService $mailboxService
16 * @return array
17 */
18 public function index(MailBoxService $mailboxService)
19 {
20 return $mailboxService->getMailBoxes();
21 }
22
23 /**
24 * @param MailBox $mailBox
25 * @param int $id
26 * @return mixed
27 */
28 public function get( MailBox $mailBox, $id )
29 {
30 return [
31 'mailbox' => $mailBox->getMailBox( $id )
32 ];
33
34 }
35
36
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)
44 {
45 $data = wp_unslash( $request->get('business', null) );
46 $data = $this->sanitizeMailboxData($data);
47
48 $this->validate($data, [
49 'name' => 'required',
50 'email' => 'required'
51 ]);
52
53 return [
54 'message' => __('Mailbox has been created successfully', 'fluent-support'),
55 'mailbox' => $mailBox->createMailBox( $data )
56 ];
57 }
58
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 {
68 try{
69 $data = wp_unslash( $request->get('business', null) );
70 $data = $this->sanitizeMailboxData($data);
71
72 $this->validate($data, [
73 'name' => 'required',
74 'email' => 'required'
75 ]);
76
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 ];
85 }
86 }
87
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)
98 {
99 try {
100 return $mailBoxService->deleteMailBox( $id, $request->getSafe('fallback_id', 'intval') );
101 } catch (\Exception $e) {
102 return [
103 'message' => Helper::getSafeErrorMessage($e),
104 ];
105 }
106 }
107
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']);
120
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 }
129
130 return $mailBoxService->moveTickets( $data, $id );
131 } catch (\Exception $e) {
132 return $this->sendError(Helper::getSafeErrorMessage($e));
133 }
134 }
135
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');
146
147 return [
148 'email_settings' => $settings->getBoxEmailSettings($box, $emailType)
149 ];
150 }
151
152 /**
153 * @param MailBoxService $mailBoxService
154 * @param int $id
155 * @return array
156 */
157 public function getEmailsSetups( MailBoxService $mailBoxService, $id )
158 {
159 return $mailBoxService->getEmailsSetups($id);
160 }
161
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 )
170 {
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 ] : [];
181
182 $emailType = $request->getSafe('email_type', 'sanitize_text_field');
183
184 $this->validate($data, [
185 'email_subject' => 'required',
186 'email_body' => 'required'
187 ]);
188
189 return $mailBoxService->saveEmailSettings( $emailType, $id, $data );
190 }
191
192 /**
193 * @param MailBoxService $mailBoxService
194 * @param int $id
195 * @return array
196 */
197 public function setAsDefault( MailBoxService $mailBoxService, $id )
198 {
199 return $mailBoxService->setAsDefault( $id );
200 }
201
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 ] : [];
219
220 return $mailBoxService->getTickets( $filters, $id );
221 }
222
223 /**
224 * @param mixed $data
225 * @return array
226 */
227 private function sanitizeMailboxData($data)
228 {
229 if (!is_array($data)) {
230 return [];
231 }
232
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',
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;
251 }
252 }
253