mirror of
https://github.com/getnamingo/registry.git
synced 2025-07-20 17:46:03 +02:00
Improved the spec11 abuse report, fixed #167
This commit is contained in:
parent
7140be54e7
commit
60ab2b9723
1 changed files with 77 additions and 38 deletions
|
@ -4,32 +4,47 @@ require __DIR__ . '/vendor/autoload.php';
|
|||
$c = require_once 'config.php';
|
||||
require_once 'helpers.php';
|
||||
|
||||
// Connect to the database
|
||||
$dsn = "{$c['db_type']}:host={$c['db_host']};dbname={$c['db_database']}";
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
$logFilePath = '/var/log/namingo/abusereport.log';
|
||||
$log = setupLogger($logFilePath, 'Abuse_Report');
|
||||
$log->info('job started.');
|
||||
$log->info('Job started.');
|
||||
|
||||
try {
|
||||
$dbh = new PDO($dsn, $c['db_username'], $c['db_password'], $options);
|
||||
// Database connection
|
||||
$dsn = "{$c['db_type']}:host={$c['db_host']};dbname={$c['db_database']}";
|
||||
$dbh = new PDO($dsn, $c['db_username'], $c['db_password'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
$log->error('DB Connection failed: ' . $e->getMessage());
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
// Prepare and execute the query
|
||||
$query = "SELECT reported_domain, nature_of_abuse, status, priority, date_of_incident, date_created FROM support_tickets WHERE category_id = '8'";
|
||||
$stmt = $dbh->query($query);
|
||||
// Retrieve tickets by user role
|
||||
function getTicketsByUserRole($dbh, $userRoleMask, $userId = null)
|
||||
{
|
||||
$query = "SELECT reported_domain, nature_of_abuse, status, priority, date_of_incident, date_created
|
||||
FROM support_tickets
|
||||
WHERE category_id = '8'";
|
||||
|
||||
// Fetch all rows
|
||||
$tickets = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
if ($userRoleMask === 4 && $userId) {
|
||||
$query .= " AND user_id = :userId";
|
||||
}
|
||||
|
||||
// Start HTML output
|
||||
$stmt = $dbh->prepare($query);
|
||||
if ($userRoleMask === 4 && $userId) {
|
||||
$stmt->execute([':userId' => $userId]);
|
||||
} else {
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
|
||||
// Generate HTML report for abuse tickets
|
||||
function generateReportHTML($tickets)
|
||||
{
|
||||
$html = "<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
@ -37,12 +52,11 @@ try {
|
|||
</head>
|
||||
<body>
|
||||
<h1>Abuse Report</h1>
|
||||
<p>Report Date: " . date('Y-m-d H:i:s') . "</p>"; // Display report generation date
|
||||
<p>Report Date: " . date('Y-m-d H:i:s') . "</p>";
|
||||
|
||||
if (empty($tickets)) {
|
||||
$html .= "<p>No abuse cases found for the period.</p>"; // Message if no tickets
|
||||
$html .= "<p>No abuse cases found for the period.</p>";
|
||||
} else {
|
||||
// Continue with the table if tickets are found
|
||||
$html .= "<table border='1'>
|
||||
<tr>
|
||||
<th>Reported Domain</th>
|
||||
|
@ -53,7 +67,6 @@ try {
|
|||
<th>Date Reported</th>
|
||||
</tr>";
|
||||
|
||||
// Loop through tickets and add rows to the table
|
||||
foreach ($tickets as $ticket) {
|
||||
$html .= "<tr>
|
||||
<td>" . htmlspecialchars($ticket['reported_domain']) . "</td>
|
||||
|
@ -64,29 +77,30 @@ try {
|
|||
<td>" . htmlspecialchars($ticket['date_created']) . "</td>
|
||||
</tr>";
|
||||
}
|
||||
|
||||
$html .= "</table>"; // Close the table
|
||||
$html .= "</table>";
|
||||
}
|
||||
|
||||
// End HTML
|
||||
$html .= "</body>
|
||||
</html>";
|
||||
$html .= "</body></html>";
|
||||
return $html;
|
||||
}
|
||||
|
||||
// Prepare the data array
|
||||
// Send email via internal API
|
||||
function sendEmail($toEmail, $subject, $htmlContent)
|
||||
{
|
||||
global $log;
|
||||
$data = [
|
||||
'type' => 'sendmail',
|
||||
'toEmail' => $toEmail,
|
||||
'subject' => 'Abuse Report',
|
||||
'body' => $html,
|
||||
'subject' => $subject,
|
||||
'body' => $htmlContent,
|
||||
];
|
||||
|
||||
$url = 'http://127.0.0.1:8250';
|
||||
|
||||
$options = [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_CUSTOMREQUEST => 'POST',
|
||||
CURLOPT_POSTFIELDS => json_encode($data),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
CURLOPT_CUSTOMREQUEST => 'POST',
|
||||
CURLOPT_POSTFIELDS => json_encode($data),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Content-Type: application/json',
|
||||
'Content-Length: ' . strlen(json_encode($data))
|
||||
],
|
||||
|
@ -94,18 +108,43 @@ try {
|
|||
|
||||
$curl = curl_init($url);
|
||||
curl_setopt_array($curl, $options);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
if ($response === false) {
|
||||
throw new Exception(curl_error($curl), curl_errno($curl));
|
||||
$log->error('Email sending failed: ' . curl_error($curl));
|
||||
curl_close($curl);
|
||||
return false;
|
||||
}
|
||||
|
||||
curl_close($curl);
|
||||
return true;
|
||||
}
|
||||
|
||||
$log->info('job finished successfully.');
|
||||
} catch (PDOException $e) {
|
||||
$log->error('Database error: ' . $e->getMessage());
|
||||
// Process report generation and sending based on roles
|
||||
try {
|
||||
$userRoles = [
|
||||
['role' => 0, 'message' => 'Full abuse report for all domains'],
|
||||
['role' => 4, 'message' => 'Abuse report for specific user cases']
|
||||
];
|
||||
|
||||
foreach ($userRoles as $role) {
|
||||
$users = $dbh->prepare("SELECT id, email FROM users WHERE roles_mask = :roleMask");
|
||||
$users->execute([':roleMask' => $role['role']]);
|
||||
|
||||
while ($user = $users->fetch()) {
|
||||
$tickets = getTicketsByUserRole($dbh, $role['role'], $user['id']);
|
||||
$htmlContent = generateReportHTML($tickets);
|
||||
$subject = "Abuse Report - {$role['message']}";
|
||||
|
||||
if (sendEmail($user['email'], $subject, $htmlContent)) {
|
||||
$log->info("Abuse report sent to {$user['email']} for role {$role['role']}");
|
||||
} else {
|
||||
$log->error("Failed to send abuse report to {$user['email']} for role {$role['role']}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$log->info('Job finished successfully.');
|
||||
} catch (Throwable $e) {
|
||||
$log->error('Error: ' . $e->getMessage());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue