PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.91.6
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.91.6
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / app / Http / Controllers / WebhookController.php

WebhookController.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.91.6, at app/Http/Controllers/WebhookController.php

329 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\App\Http\Controllers;
4
5 use FluentBoards\App\Models\Webhook;
6 use FluentBoards\App\Models\Meta;
7 use FluentBoards\App\Models\Relation;
8 use FluentBoards\Framework\Http\Request\Request;
9
10
11 class WebhookController extends Controller
12 {
13 public function index(Request $request, Webhook $webhook)
14 {
15 $fields = $webhook->getFields();
16 $search = $request->getSafe('search', 'sanitize_text_field', '');
17
18 $webhooks = $webhook->latest()->get()->toArray();
19 if ( ! empty($search)) {
20 $search = strtolower($search);
21 $webhooks = array_map(function ($row) use ($search) {
22 $name = strtolower($row['value']['name']);
23 if ($row['value'] && str_contains($name, $search)) {
24 return $row;
25 }
26
27 return null;
28 }, $webhooks);
29 }
30
31 $rows = [];
32 foreach ($webhooks as $row) {
33 if ($row) {
34 $rows[] = $row;
35 }
36 }
37
38
39 $response = [
40 'webhooks' => $rows,
41 'fields' => $fields['fields']
42 ];
43
44 return $response;
45 }
46
47 public function create(Request $request, Webhook $webhook)
48 {
49 $name = $request->name;
50 $boardId = $request->getSafe('board', 'intval');
51 $stageId = $request->getSafe('stage', 'intval');
52 // Sanitize name field
53 $name = sanitize_text_field($name);
54
55 if(!$name || !$boardId || !$stageId) {
56 return $this->sendError('Name, board and stage are required');
57 }
58
59 $webhook = $webhook->store([
60 'name' => $name,
61 'board' => $boardId,
62 'stage' => $stageId,
63 ]);
64
65 return [
66 'id' => $webhook->id,
67 'webhook' => $webhook->value,
68 'webhooks' => $webhook->latest()->get(),
69 'message' => __('Successfully Created the WebHook', 'fluent-boards'),
70 ];
71 }
72
73 public function update(Request $request, Webhook $webhook, $id)
74 {
75 $id = absint($id);
76 $data = $request->only(['name']);
77 // Sanitize data if needed based on webhook structure
78 if (isset($data['name'])) {
79 $data['name'] = sanitize_text_field($data['name']);
80 }
81 $webhook->find($id)->saveChanges($data);
82
83 return [
84 'webhooks' => $webhook->latest()->get(),
85 'message' => __('Successfully updated the webhook', 'fluent-boards'),
86 ];
87 }
88
89 public function delete(Webhook $webhook, $id)
90 {
91 $id = absint($id);
92 $webhook->where('id', $id)->delete();
93
94 return [
95 'webhooks' => $webhook->latest()->get(),
96 'message' => __('Successfully deleted the webhook', 'fluent-boards'),
97 ];
98 }
99
100 // Outgoing Webhook Methods
101 public function outgoingWebhooks(Request $request, Meta $meta)
102 {
103 $search = $request->getSafe('search', 'sanitize_text_field', '');
104 $boardId = $request->getSafe('board_id', 'intval');
105
106 $webhooks = $meta->where('object_type', 'outgoing_webhook')
107 ->latest()
108 ->get()
109 ->toArray();
110
111 // Add board filtering logic
112 if ($boardId) {
113 $webhooks = array_filter($webhooks, function($row) use ($boardId) {
114 if (!isset($row['value']['board_id'])) {
115 return false;
116 }
117
118 $webhookBoardId = $row['value']['board_id'];
119
120 // Handle array of board IDs
121 if (is_array($webhookBoardId)) {
122 return in_array($boardId, array_map('intval', $webhookBoardId));
123 }
124
125 // Handle single board ID or null (all boards)
126 return $webhookBoardId === null || intval($webhookBoardId) === $boardId;
127 });
128 }
129
130 if (!empty($search)) {
131 $search = strtolower($search);
132 $webhooks = array_map(function ($row) use ($search) {
133 $name = strtolower($row['value']['name']);
134 if ($row['value'] && str_contains($name, $search)) {
135 return $row;
136 }
137 return null;
138 }, $webhooks);
139 }
140
141 $rows = [];
142 foreach ($webhooks as $row) {
143 if ($row) {
144 $rows[] = $row;
145 }
146 }
147
148 return [
149 'webhooks' => $rows
150 ];
151 }
152
153 public function createOutgoingWebhook(Request $request)
154 {
155 $data = $this->validate(
156 $request->only(['name', 'url', 'board_id', 'triggered_events', 'header_type', 'headers']),
157 [
158 'name' => 'required',
159 'url' => 'required',
160 'board_id' => 'array',
161 'triggered_events' => 'array',
162 ]
163 );
164
165 $data['name'] = sanitize_text_field($data['name']);
166 $data['url'] = sanitize_url($data['url']);
167
168 // Sanitize header_type if present
169 if (isset($data['header_type'])) {
170 $data['header_type'] = sanitize_text_field($data['header_type']);
171 }
172
173 if(isset($data['header_type']) && $data['header_type'] == 'with_headers') {
174 // sanitize all headers key and value
175 if (!empty($data['headers']) && is_array($data['headers'])) {
176 foreach ($data['headers'] as $index => $header) {
177 if (isset($header['name'])) {
178 $data['headers'][$index]['name'] = sanitize_text_field($header['name']);
179 }
180 if (isset($header['value'])) {
181 $data['headers'][$index]['value'] = sanitize_text_field($header['value']);
182 }
183 }
184 }
185 }
186
187 // events cannot be null
188 if (!isset($data['triggered_events']) || empty($data['triggered_events'])) {
189 return $this->sendError('At least one event must be selected for the webhook to be triggered.');
190 }
191
192 // Sanitize triggered_events array
193 if (is_array($data['triggered_events'])) {
194 $data['triggered_events'] = array_filter(array_map('sanitize_text_field', $data['triggered_events']));
195 }
196 $data['triggered_events'] = array_values($data['triggered_events']);
197
198 // Sanitize board_id array to integers
199 if (!empty($data['board_id']) && is_array($data['board_id'])) {
200 $data['board_id'] = array_filter(array_map('intval', $data['board_id']));
201 } else {
202 $data['board_id'] = [];
203 }
204
205 $webhook = Meta::create([
206 'object_type' => 'outgoing_webhook',
207 'value' => $data
208 ]);
209
210 foreach ($data['board_id'] as $boardId) {
211 $boardId = absint($boardId);
212 Relation::create([
213 'object_id' => $webhook->id,
214 'object_type' => 'outgoing_webhook_board',
215 'foreign_id' => $boardId,
216 'settings' => \maybe_serialize($data['triggered_events'] ?? []),
217 'preferences' => ''
218 ]);
219 }
220
221
222 return [
223 'id' => $webhook->id,
224 'webhook' => $webhook->value,
225 'webhooks' => Meta::where('object_type', 'outgoing_webhook')->latest()->get(),
226 'message' => __('Successfully Created the Outgoing WebHook', 'fluent-boards'),
227 ];
228 }
229
230 public function updateOutgoingWebhook(Request $request, $id)
231 {
232 $id = absint($id);
233 $data = $this->validate(
234 $request->all(),
235 [
236 'name' => 'required',
237 'url' => 'required',
238 'board_id' => 'array',
239 'triggered_events' => 'array',
240 ]
241 );
242
243 $data['name'] = sanitize_text_field($data['name']);
244 $data['url'] = sanitize_url($data['url']);
245
246 // Sanitize header_type if present
247 if (isset($data['header_type'])) {
248 $data['header_type'] = sanitize_text_field($data['header_type']);
249 }
250
251 if(isset($data['header_type']) && $data['header_type'] == 'with_headers') {
252 // sanitize all headers key and value
253 if (!empty($data['headers']) && is_array($data['headers'])) {
254 foreach ($data['headers'] as $index => $header) {
255 if (isset($header['name'])) {
256 $data['headers'][$index]['name'] = sanitize_text_field($header['name']);
257 }
258 if (isset($header['value'])) {
259 $data['headers'][$index]['value'] = sanitize_text_field($header['value']);
260 }
261 }
262 }
263 }
264
265 // Sanitize triggered_events array
266 if (is_array($data['triggered_events'])) {
267 $data['triggered_events'] = array_filter(array_map('sanitize_text_field', $data['triggered_events']));
268 }
269
270 // Sanitize board_id array to integers
271 if (!empty($data['board_id']) && is_array($data['board_id'])) {
272 $data['board_id'] = array_filter(array_map('intval', $data['board_id']));
273 } else {
274 $data['board_id'] = [];
275 }
276
277 $webhook = Meta::find($id);
278 $webhook->value = $data;
279 $webhook->save();
280
281
282 // Update relations
283 Relation::where('object_type', 'outgoing_webhook_board')
284 ->where('object_id', $webhook->id)
285 ->delete();
286
287 // Recreate relations
288 foreach ($data['board_id'] as $boardId) {
289 $boardId = absint($boardId);
290 Relation::create([
291 'object_id' => $webhook->id,
292 'object_type' => 'outgoing_webhook_board',
293 'foreign_id' => $boardId,
294 'settings' => \maybe_serialize($data['triggered_events'] ?? []),
295 'preferences' => ''
296 ]);
297 }
298
299
300 return [
301 'webhooks' => Meta::where('object_type', 'outgoing_webhook')->latest()->get(),
302 'message' => __('Successfully updated the outgoing webhook', 'fluent-boards'),
303 ];
304 }
305
306 public function deleteOutgoingWebhook($id)
307 {
308 $id = absint($id);
309 $webhook = Meta::find($id);
310
311 if (!$webhook || $webhook->object_type !== 'outgoing_webhook') {
312 return $this->sendError('Outgoing webhook not found.', 404);
313 }
314
315 // Delete associated relations
316 Relation::where('object_type', 'outgoing_webhook_board')
317 ->where('object_id', $webhook->id)
318 ->delete();
319
320
321 $webhook->delete();
322
323 return [
324 'webhooks' => Meta::where('object_type', 'outgoing_webhook')->latest()->get(),
325 'message' => __('Successfully deleted the outgoing webhook', 'fluent-boards'),
326 ];
327 }
328 }
329