Fix PHP 8.2 TypeError crash in create_ticket_api.php on missing title
generateTicketHash() passed $data['title'] to preg_match() before any input validation. In PHP 8.2, preg_match() with null subject throws TypeError, causing HTTP 500 with empty body. hwmonDaemon saw this as "Expecting value: line 1 column 1 (char 0)" and failed to create tickets on all nodes. Moved input validation before the hash call: missing or empty title now returns HTTP 400 with proper JSON error instead of crashing. Also removed the redundant late URL-encoded fallback. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+16
-5
@@ -84,6 +84,22 @@ $conn->query($createTableSQL);
|
|||||||
$rawInput = file_get_contents('php://input');
|
$rawInput = file_get_contents('php://input');
|
||||||
$data = json_decode($rawInput, true);
|
$data = json_decode($rawInput, true);
|
||||||
|
|
||||||
|
// Validate required fields before any processing
|
||||||
|
if (!is_array($data) || empty($data['title'])) {
|
||||||
|
// Try URL-encoded fallback
|
||||||
|
if (empty($data['title'])) {
|
||||||
|
parse_str($rawInput, $urlData);
|
||||||
|
if (!empty($urlData['title'])) {
|
||||||
|
$data = $urlData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!is_array($data) || empty($data['title'])) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(['success' => false, 'error' => 'title is required']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate hash from stable components
|
// Generate hash from stable components
|
||||||
function generateTicketHash($data) {
|
function generateTicketHash($data) {
|
||||||
// Extract device name if present (matches /dev/sdX, /dev/nvmeXnY patterns)
|
// Extract device name if present (matches /dev/sdX, /dev/nvmeXnY patterns)
|
||||||
@@ -167,11 +183,6 @@ if ($result->num_rows > 0) {
|
|||||||
// Force JSON content type for all incoming requests
|
// Force JSON content type for all incoming requests
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
if (!$data) {
|
|
||||||
// Try parsing as URL-encoded data
|
|
||||||
parse_str($rawInput, $data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate ticket ID (9-digit format with leading zeros)
|
// Generate ticket ID (9-digit format with leading zeros)
|
||||||
$ticket_id = sprintf('%09d', mt_rand(1, 999999999));
|
$ticket_id = sprintf('%09d', mt_rand(1, 999999999));
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user