From aacf4b23aedb4acdb72b5cf7288babb0a20cb5e4 Mon Sep 17 00:00:00 2001 From: Frederico Falcao Date: Tue, 17 Jun 2025 14:20:43 +0100 Subject: [PATCH] first commit --- index.php | 70 +++++++++++++++++++++++++ lib.php | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 index.php create mode 100644 lib.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..1b0469f --- /dev/null +++ b/index.php @@ -0,0 +1,70 @@ + ['pipe', 'r'], + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'] + ], $pipes); + if (is_resource($proc)) { + fwrite($pipes[0], $raw_msg); + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($proc); + } + } elseif (file_exists($index_php)) { + $cmd = "php $index_php"; + $proc = proc_open($cmd, [ + 0 => ['pipe', 'r'], + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'] + ], $pipes); + if (is_resource($proc)) { + fwrite($pipes[0], $raw_msg); + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + proc_close($proc); + } + } + +if (!$is_cli) { + http_response_code(200); + echo "Webhook processed successfully."; +} diff --git a/lib.php b/lib.php new file mode 100644 index 0000000..eda6af3 --- /dev/null +++ b/lib.php @@ -0,0 +1,150 @@ +exec(" + CREATE TABLE emails ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + direction TEXT CHECK(direction IN ('incoming', 'outgoing')) NOT NULL, + user TEXT, + domain TEXT, + subject TEXT, + date TEXT, + attachments_no INTEGER, + sizeKb INTEGER + ) + "); + } + + // Extract local part and domain from the email address + $local = preg_replace('/[^a-zA-Z0-9_.+-]/', '_', strstr($email, '@', true)); + $domain = preg_replace('/[^a-zA-Z0-9_.+-]/', '_', substr(strstr($email, '@'), 1)); + + // Attempt to parse the Subject header from the raw message + $subject = ''; + if (preg_match('/^Subject:\s*(.*)$/mi', $raw_msg, $matches)) { + $subject = trim($matches[1]); + } + + // Calculate message size in kilobytes + $size_kb = (int)(strlen($raw_msg) / 1024); + + // Use the current timestamp as the 'date' + $timestamp = date('Y-m-d H:i:s'); + + // Prepare and execute the insert query + $stmt = $db->prepare(" + INSERT INTO emails (direction, user, domain, subject, date, attachments_no, sizeKb) + VALUES (:direction, :user, :domain, :subject, :date, :attachments, :size) + "); + $stmt->bindValue(':direction', $direction, SQLITE3_TEXT); + $stmt->bindValue(':user', $local, SQLITE3_TEXT); + $stmt->bindValue(':domain', $domain, SQLITE3_TEXT); + $stmt->bindValue(':subject', $subject, SQLITE3_TEXT); + $stmt->bindValue(':date', $timestamp, SQLITE3_TEXT); + $stmt->bindValue(':attachments', 0, SQLITE3_INTEGER); // Can be extended later to count real attachments + $stmt->bindValue(':size', $size_kb, SQLITE3_INTEGER); + $stmt->execute(); + + // Close the database connection + $db->close(); + + } catch (Exception $e) { + // Log any unexpected SQLite errors (e.g., permission issues) + error_log("SQLite error: " . $e->getMessage()); + } +}