|
| 1 | +<?php |
| 2 | + |
| 3 | +// all response are JSON encoded |
| 4 | +header('Content-Type: application/json; charset=UTF-8'); |
| 5 | + |
| 6 | +require_once __DIR__ . '/../conf.php'; |
| 7 | + |
| 8 | +if (!defined('API_KEY')) { |
| 9 | + http_response_code(401); // Unauthorized |
| 10 | + echo json_encode(['error' => 'Unauthorized - Set an API KEY to use this API']); |
| 11 | + exit; |
| 12 | +} |
| 13 | + |
| 14 | +require_once __DIR__ . '/../database.php'; |
| 15 | +require_once __DIR__ . '/MailLogEntry.php'; |
| 16 | + |
| 17 | +/** |
| 18 | + * @param ?string $apiKey |
| 19 | + * |
| 20 | + * @return bool |
| 21 | + */ |
| 22 | +function isValidApiKey($apiKey) |
| 23 | +{ |
| 24 | + if (null === $apiKey) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + if (!defined('API_KEY')) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + return API_KEY === $apiKey; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @return ?string |
| 37 | + */ |
| 38 | +function getApiKeyToken() |
| 39 | +{ |
| 40 | + if (isset($_SERVER['HTTP_X_MAILWATCH_API_KEY'])) { |
| 41 | + return $_SERVER['HTTP_X_MAILWATCH_API_KEY']; |
| 42 | + } |
| 43 | + |
| 44 | + return null; |
| 45 | +} |
| 46 | + |
| 47 | +// Check if is POST request |
| 48 | +if ('POST' !== $_SERVER['REQUEST_METHOD']) { |
| 49 | + http_response_code(405); // Method Not Allowed |
| 50 | + echo json_encode(['error' => 'Method Not Allowed']); |
| 51 | + exit; |
| 52 | +} |
| 53 | + |
| 54 | +// Verify API key |
| 55 | +$apiKeyToken = getApiKeyToken(); |
| 56 | +if (null === $apiKeyToken || !isValidApiKey($apiKeyToken)) { |
| 57 | + http_response_code(401); // Unauthorized |
| 58 | + echo json_encode(['error' => 'Unauthorized']); |
| 59 | + exit; |
| 60 | +} |
| 61 | + |
| 62 | +// Get JSON payload |
| 63 | +$json = file_get_contents('php://input'); |
| 64 | +$data = json_decode($json, true); |
| 65 | + |
| 66 | +if (JSON_ERROR_NONE !== json_last_error()) { |
| 67 | + http_response_code(400); // Bad Request |
| 68 | + echo json_encode(['error' => 'Invalid JSON']); |
| 69 | + exit; |
| 70 | +} |
| 71 | +$mailLogEntry = new MailLogEntry($data); |
| 72 | +if (!$mailLogEntry->isValid()) { |
| 73 | + http_response_code(400); // Bad Request |
| 74 | + echo json_encode(['error' => 'Invalid data']); |
| 75 | + exit; |
| 76 | +} |
| 77 | + |
| 78 | +// Prepare insert query |
| 79 | +$dbLink = Database::connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); |
| 80 | + |
| 81 | +$query = 'INSERT INTO maillog (timestamp, id, size, from_address, from_domain, to_address, to_domain, subject, clientip, archive, isspam, ishighspam, issaspam, isrblspam, spamwhitelisted, spamblacklisted, sascore, spamreport, virusinfected, nameinfected, otherinfected, report, ismcp, ishighmcp, issamcp, mcpwhitelisted, mcpblacklisted, mcpsascore, mcpreport, hostname, date, time, headers, quarantined, rblspamreport, token, messageid) |
| 82 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; |
| 83 | + |
| 84 | +$stmt = $dbLink->prepare($query); |
| 85 | +if (!$stmt) { |
| 86 | + http_response_code(500); // Internal Server Error |
| 87 | + echo json_encode(['error' => 'Failed to prepare statement']); |
| 88 | + exit; |
| 89 | +} |
| 90 | +$stmt->bind_param( |
| 91 | + 'ssisssssssiiiiiiiiiiiissiiiissssssss', |
| 92 | + $mailLogEntry->timestamp, |
| 93 | + $mailLogEntry->id, |
| 94 | + $mailLogEntry->size, |
| 95 | + $mailLogEntry->from, |
| 96 | + $mailLogEntry->from_domain, |
| 97 | + $mailLogEntry->to, |
| 98 | + $mailLogEntry->to_domain, |
| 99 | + $mailLogEntry->subject, |
| 100 | + $mailLogEntry->clientip, |
| 101 | + $mailLogEntry->archiveplaces, |
| 102 | + $mailLogEntry->isspam, |
| 103 | + $mailLogEntry->ishigh, |
| 104 | + $mailLogEntry->issaspam, |
| 105 | + $mailLogEntry->isrblspam, |
| 106 | + $mailLogEntry->spamwhitelisted, |
| 107 | + $mailLogEntry->spamblacklisted, |
| 108 | + $mailLogEntry->sascore, |
| 109 | + $mailLogEntry->spamreport, |
| 110 | + $mailLogEntry->virusinfected, |
| 111 | + $mailLogEntry->nameinfected, |
| 112 | + $mailLogEntry->otherinfected, |
| 113 | + $mailLogEntry->reports, |
| 114 | + $mailLogEntry->ismcp, |
| 115 | + $mailLogEntry->ishighmcp, |
| 116 | + $mailLogEntry->issamcp, |
| 117 | + $mailLogEntry->mcpwhitelisted, |
| 118 | + $mailLogEntry->mcpblacklisted, |
| 119 | + $mailLogEntry->mcpsascore, |
| 120 | + $mailLogEntry->mcpreport, |
| 121 | + $mailLogEntry->hostname, |
| 122 | + $mailLogEntry->date, |
| 123 | + $mailLogEntry->time, |
| 124 | + $mailLogEntry->headers, |
| 125 | + $mailLogEntry->quarantined, |
| 126 | + $mailLogEntry->rblspamreport, |
| 127 | + $mailLogEntry->token, |
| 128 | + $mailLogEntry->messageid |
| 129 | +); |
| 130 | + |
| 131 | +if ($stmt->execute()) { |
| 132 | + http_response_code(201); // Created |
| 133 | + echo json_encode(['success' => 'Data inserted successfully']); |
| 134 | +} else { |
| 135 | + http_response_code(500); // Internal Server Error |
| 136 | + echo json_encode(['error' => 'Failed to insert data']); |
| 137 | +} |
| 138 | + |
| 139 | +$stmt->close(); |
| 140 | +$dbLink->close(); |
0 commit comments