| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Ticket; |
| 6 |
use FluentSupport\Framework\Http\Request\Request; |
| 7 |
|
| 8 |
class WidgetsController extends Controller |
| 9 |
{ |
| 10 |
public function __invoke(Request $request) |
| 11 |
{ |
| 12 |
$filter = $request->get('filter', ''); |
| 13 |
$filter = sanitize_text_field($filter); |
| 14 |
$filter = str_replace('fluent_support_', '', $filter); |
| 15 |
|
| 16 |
// Only allow alphanumeric, underscores, and hyphens in filter names |
| 17 |
if (!$filter || !preg_match('/^[a-zA-Z0-9_\-]+$/', $filter)) { |
| 18 |
return $this->sendError([ |
| 19 |
'message' => __('Invalid filter name', 'fluent-support') |
| 20 |
]); |
| 21 |
} |
| 22 |
|
| 23 |
$data = $request->get('data', []); |
| 24 |
|
| 25 |
if (!is_array($data)) { |
| 26 |
$data = []; |
| 27 |
} |
| 28 |
|
| 29 |
// Sanitize all scalar values in data |
| 30 |
$data = array_map(function ($value) { |
| 31 |
return is_scalar($value) ? sanitize_text_field($value) : $value; |
| 32 |
}, $data); |
| 33 |
|
| 34 |
if ($filter === 'ticket_sidebar') { |
| 35 |
$ticketId = intval($data['ticket_id'] ?? 0); |
| 36 |
if (!$ticketId) { |
| 37 |
return $this->sendError([ |
| 38 |
'message' => __('Invalid ticket ID', 'fluent-support') |
| 39 |
]); |
| 40 |
} |
| 41 |
|
| 42 |
$ticket = Ticket::with('customer')->find($ticketId); |
| 43 |
if (!$ticket) { |
| 44 |
return $this->sendError([ |
| 45 |
'message' => __('Ticket not found', 'fluent-support') |
| 46 |
]); |
| 47 |
} |
| 48 |
|
| 49 |
$this->ensureCanAccessTicket($ticket); |
| 50 |
|
| 51 |
$data['ticket'] = $ticket; |
| 52 |
} |
| 53 |
|
| 54 |
return $this->sendSuccess([ |
| 55 |
'widgets' => apply_filters('fluent_support/widgets/' . $filter, [], $data) |
| 56 |
]); |
| 57 |
} |
| 58 |
} |
| 59 |
|