Added discord webhooks, better filtering, and automated ticket creation

This commit is contained in:
2024-12-02 21:21:10 -05:00
parent 67c0c68f6c
commit 5120afddf5
4 changed files with 178 additions and 31 deletions

View File

@ -1,6 +1,11 @@
<?php
header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 1);
file_put_contents('debug.log', print_r($_POST, true) . "\n" . file_get_contents('php://input') . "\n", FILE_APPEND);
// Load environment variables with error check
$envFile = __DIR__ . '/.env';
if (!file_exists($envFile)) {
@ -22,10 +27,10 @@ if (!$envVars) {
// Database connection with detailed error handling
$conn = new mysqli(
$envVars['REACT_APP_DB_HOST'],
$envVars['REACT_APP_DB_USER'],
$envVars['REACT_APP_DB_PASS'],
$envVars['REACT_APP_DB_NAME']
$envVars['DB_HOST'],
$envVars['DB_USER'],
$envVars['DB_PASS'],
$envVars['DB_NAME']
);
if ($conn->connect_error) {
@ -36,8 +41,17 @@ if ($conn->connect_error) {
exit;
}
// Get POST data
$data = json_decode(file_get_contents('php://input'), true);
// Force JSON content type for all incoming requests
header('Content-Type: application/json');
// Parse input regardless of content-type header
$rawInput = file_get_contents('php://input');
$data = json_decode($rawInput, true);
if (!$data) {
// Try parsing as URL-encoded data
parse_str($rawInput, $data);
}
// Generate ticket ID (9-digit format with leading zeros)
$ticket_id = sprintf('%09d', mt_rand(1, 999999999));
@ -82,3 +96,37 @@ if ($stmt->execute()) {
$stmt->close();
$conn->close();
// Discord webhook
$discord_webhook_url = $envVars['DISCORD_WEBHOOK_URL'];
// Map priorities to Discord colors (decimal format)
$priorityColors = [
"1" => 16736589, // --priority-1: #ff4d4d
"2" => 16753958, // --priority-2: #ffa726
"3" => 4363509, // --priority-3: #42a5f5
"4" => 6736490 // --priority-4: #66bb6a
];
$discord_data = [
"content" => "",
"embeds" => [[
"title" => "New Ticket Created: #" . $ticket_id,
"description" => $title,
"url" => "http://10.10.10.45/ticket.php?id=" . $ticket_id,
"color" => $priorityColors[$priority],
"fields" => [
["name" => "Priority", "value" => $priority, "inline" => true],
["name" => "Category", "value" => $category, "inline" => true],
["name" => "Type", "value" => $type, "inline" => true]
]
]]
];
$ch = curl_init($discord_webhook_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($discord_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);