mon2matrix/index.php

51 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2023-09-12 21:09:33 +02:00
<?php
$config = yaml_parse_file("../config.yaml");
$remoteIp = $_SERVER["REMOTE_ADDR"];
$reverseDns = gethostbyaddr($remoteIp);
$match = false;
foreach ($config["allowed_sender"] as $host) {
if (fnmatch($host, $remoteIp)) {
echo "match $host, $remoteIp\n";
$match = true;
break;
}
$forwardResolve = gethostbyname($reverseDns);
if ($forwardResolve != $remoteIp) {
header(
"X-Auth: reverse dns of $remoteIp ($reverseDns) does not resolve to $remoteIp ($forwardResolve)"
);
continue;
}
if (fnmatch($host, $reverseDns)) {
echo "match $host, $reverseDns\n";
$match = true;
break;
}
}
if (!$match) {
header("HTTP/1.0 403 Forbidden");
die("403 Forbidden");
}
send("$reverseDns: " . file_get_contents("php://input"), $config);
function send($message, $config)
{
$target = $config["matrix"]["target"];
$token = $config["matrix"]["token"];
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_URL,
"https://matrix.c3re.de/_matrix/client/r0/rooms/$target/send/m.room.message/123?access_token=$token"
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
json_encode(["msgtype" => "m.text", "body" => $message])
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_exec($ch);
}