71 lines
1.9 KiB
PHP
71 lines
1.9 KiB
PHP
<?php
|
|
|
|
define('EMAILS_BASE', __DIR__ . '/../data/emails');
|
|
|
|
require_once __DIR__."/lib.php";
|
|
|
|
$is_cli = (php_sapi_name() === 'cli');
|
|
$output_target = $is_cli ? '/dev/stdout' : null;
|
|
|
|
$input = get_input($is_cli);
|
|
|
|
|
|
$inputJSON = json_decode($input, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
fail("Invalid JSON", $is_cli, $output_target);
|
|
}
|
|
|
|
$raw_msg = $inputJSON['raw'] ?? null;
|
|
$from = $inputJSON['from']['value'][0]['address'] ?? null;
|
|
|
|
$filename = uniqid('', true) . ".eml";
|
|
|
|
// --- Save to domain/sender folder ---
|
|
list($from_local,$from_domain) = explode("@",$from);
|
|
|
|
$sender_folder = EMAILS_BASE . "/$from_domain/$from_local";
|
|
if (!is_dir($sender_folder)) {
|
|
mkdir($sender_folder, 0775, true);
|
|
}
|
|
|
|
$target_path = "$sender_folder/$filename";
|
|
file_put_contents($target_path, $raw_msg);
|
|
|
|
// --- Run index.sh or index.php if exists ---
|
|
$index_sh = "$sender_folder/index.sh";
|
|
$index_php = "$sender_folder/index.php";
|
|
|
|
if (file_exists($index_sh)) {
|
|
$proc = proc_open("/bin/bash $index_sh", [
|
|
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);
|
|
}
|
|
} 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.";
|
|
}
|