prepare("SELECT id FROM cases WHERE fir_no = :fir_no"); $stmt_case->execute([':fir_no' => $fir_no]); $case = $stmt_case->fetch(); if ($case) { $case_id = $case['id']; $upload_dir = __DIR__ . '/uploads/'; // Sanitize the filename to prevent directory traversal issues $file_basename = basename($file['name']); $file_name = uniqid() . '-' . preg_replace("/[^a-zA-Z0-9._-]", "", $file_basename); $file_path = $upload_dir . $file_name; $relative_path = 'uploads/' . $file_name; if (move_uploaded_file($file['tmp_name'], $file_path)) { $stmt_insert = $pdo->prepare("INSERT INTO case_files (case_id, file_name, file_path) VALUES (:case_id, :file_name, :file_path)"); $stmt_insert->execute([ ':case_id' => $case_id, ':file_name' => $file_basename, // Store original filename ':file_path' => $relative_path ]); echo json_encode(['success' => true, 'message' => 'File uploaded successfully.', 'file_path' => $relative_path]); } else { echo json_encode(['success' => false, 'message' => 'Failed to move uploaded file. Check directory permissions.']); } } else { echo json_encode(['success' => false, 'message' => 'Case with FIR No ' . htmlspecialchars($fir_no) . ' not found.']); } } else { $error_message = 'Invalid request.'; if ($file && $file['error'] !== UPLOAD_ERR_OK) { $error_message = 'File upload error: ' . $file['error']; } echo json_encode(['success' => false, 'message' => $error_message]); } exit; } $pdo = db(); if (isset($_GET['action']) && $_GET['action'] == 'get_reports') { $stmt = $pdo->query("SELECT r.id, r.report_type, r.created_at, c.fir_no FROM reports r JOIN cases c ON r.case_id = c.id ORDER BY r.created_at DESC"); $reports = $stmt->fetchAll(); echo json_encode($reports); exit; } if (isset($_GET['action']) && $_GET['action'] == 'get_case' && isset($_GET['fir_no'])) { $stmt = $pdo->prepare("SELECT * FROM cases WHERE fir_no = :fir_no"); $stmt->execute([':fir_no' => $_GET['fir_no']]); $case = $stmt->fetch(); if ($case) { echo json_encode($case); } else { echo json_encode(['error' => 'Case not found']); } exit; } if (isset($_GET['action']) && $_GET['action'] == 'get_report' && isset($_GET['id'])) { $stmt = $pdo->prepare("SELECT content FROM reports WHERE id = :id"); $stmt->execute([':id' => $_GET['id']]); $report = $stmt->fetch(); if ($report) { echo json_encode($report); } else { echo json_encode(['error' => 'Report not found']); } exit; } $input = json_decode(file_get_contents('php://input'), true); $message = $input['message'] ?? ''; $reply = 'I am sorry, I do not understand. Please ask me to find a case, for example: \'find case 684/25\'.'; // Simple intent parsing if (preg_match('/(find|search|get|show)\s+(case|fir|for|about)\s+(.+)/i', $message, $matches)) { $search_term = trim($matches[3]); $search_query = "%{$search_term}%"; $stmt = $pdo->prepare("SELECT * FROM cases WHERE fir_no LIKE :term OR accused_name LIKE :term OR sections LIKE :term OR ps LIKE :term"); $stmt->execute([':term' => $search_query]); $found_cases = $stmt->fetchAll(); if ($found_cases) { if (count($found_cases) == 1) { $found_case = $found_cases[0]; $reply = "Found Case File: FIR-" . $found_case['fir_no'] . "\n"; $reply .= "PS: " . $found_case['ps'] . "\n"; $reply .= "Accused: " . $found_case['accused_name'] . "\n"; $reply .= "Sections: " . $found_case['sections'] . "\n"; $reply .= "Facts: " . $found_case['facts']; } else { $reply = "I found multiple cases. Please be more specific.\n\n"; foreach ($found_cases as $case) { $reply .= "- FIR " . $case['fir_no'] . " (" . $case['accused_name'] . ")\n"; } } } else { $reply = "I could not find any case matching '{$search_term}'."; } } elseif (preg_match('/^update case ([\d\/-]+) set (.+)/i', $message, $matches)) { $fir_no = trim($matches[1]); $update_string = trim($matches[2]); // Check if case exists $stmt = $pdo->prepare("SELECT id FROM cases WHERE fir_no = :fir_no"); $stmt->execute([':fir_no' => $fir_no]); $case_to_update = $stmt->fetch(); if ($case_to_update) { $allowed_fields = ['ps', 'accused_name', 'sections', 'complainant', 'address', 'facts']; $updates = []; $params = [':fir_no' => $fir_no]; // Parse the update string for key-value pairs preg_match_all('/(\w+):((?:(?!, \w+:).)+)/i', $update_string, $update_matches, PREG_SET_ORDER); foreach ($update_matches as $match) { $field = strtolower(trim($match[1])); $value = trim($match[2]); if (in_array($field, $allowed_fields)) { $updates[] = "{$field} = :{$field}"; $params[":{$field}"] = $value; } } if (!empty($updates)) { $sql = "UPDATE cases SET " . implode(', ', $updates) . " WHERE fir_no = :fir_no"; try { $stmt = $pdo->prepare($sql); $stmt->execute($params); $reply = "Successfully updated case FIR No: {$fir_no}."; } catch (PDOException $e) { $reply = "Error updating case: " . $e->getMessage(); } } else { $reply = "No valid fields to update. You can update: ps, accused_name, sections, complainant, address, facts."; } } else { $reply = "Sorry, I couldn't find a case with FIR No: {$fir_no} to update."; } } elseif (preg_match('/^create case/i', $message)) { // Parsing key-value pairs from the message $fir_no = preg_match('/fir_no:([^,]+)/i', $message, $m) ? trim($m[1]) : ''; $ps = preg_match('/ps:([^,]+)/i', $message, $m) ? trim($m[1]) : ''; $accused_name = preg_match('/accused:([^,]+)/i', $message, $m) ? trim($m[1]) : ''; $sections = preg_match('/sections:([^,]+)/i', $message, $m) ? trim($m[1]) : ''; $complainant = preg_match('/complainant:([^,]+)/i', $message, $m) ? trim($m[1]) : ''; $address = preg_match('/address:([^,]+)/i', $message, $m) ? trim($m[1]) : ''; $facts = preg_match('/facts:(.+)/i', $message, $m) ? trim($m[1]) : ''; if ($fir_no && $ps && $accused_name && $sections) { try { $stmt = $pdo->prepare("INSERT INTO cases (fir_no, ps, accused_name, sections, complainant, address, facts) VALUES (:fir_no, :ps, :accused_name, :sections, :complainant, :address, :facts)"); $stmt->execute([ ':fir_no' => $fir_no, ':ps' => $ps, ':accused_name' => $accused_name, ':sections' => $sections, ':complainant' => $complainant, ':address' => $address, ':facts' => $facts ]); $reply = "Successfully created new case with FIR No: {$fir_no}."; } catch (PDOException $e) { $reply = "Error creating case: " . $e->getMessage(); } } else { $reply = "Sorry, I couldn't create the case. Please provide at least 'fir_no', 'ps', 'accused', and 'sections'."; } } elseif (preg_match('/generate\s+(.+?)\s+for\s+(case|fir)\s+([\d\/-]+)/i', $message, $matches)) { $report_type = trim($matches[1]); $fir_no = str_replace('-', '/', $matches[3]); $stmt = $pdo->prepare("SELECT * FROM cases WHERE fir_no = :fir_no"); $stmt->execute([':fir_no' => $fir_no]); $found_case = $stmt->fetch(); if ($found_case) { $generated_report = null; $report_type_code = ''; if (strcasecmp($report_type, 'arrest memo') === 0) { $generated_report = generate_arrest_memo($found_case); $report_type_code = 'ARREST_MEMO'; } elseif (strcasecmp($report_type, 'bail reply') === 0) { $generated_report = generate_bail_reply($found_case); $report_type_code = 'BAIL_REPLY'; } elseif (strcasecmp($report_type, 'chargesheet') === 0) { $generated_report = generate_chargesheet($found_case); $report_type_code = 'CHARGESHEET'; } else { $reply = "I can generate 'arrest memo', 'bail reply', or 'chargesheet' reports."; } if ($generated_report) { $stmt = $pdo->prepare("INSERT INTO reports (case_id, report_type, content) VALUES (:case_id, :report_type, :content)"); $stmt->execute([ ':case_id' => $found_case['id'], ':report_type' => $report_type_code, ':content' => $generated_report ]); $report_id = $pdo->lastInsertId(); $reply = "I have generated the '{$report_type}' (ID: {$report_id}) for FIR {$fir_no}. You can view it in the reports section."; } } else { $reply = "I could not find any case file with FIR number: " . $fir_no . " to generate a report."; } } echo json_encode(['reply' => $reply]);