1
0
plweb/webhook.php
Anna Christina Naß ce5baaac35 Erster Commit
2025-07-07 16:25:22 +02:00

73 lines
1.6 KiB
PHP

<?php
include 'config.php';
include 'functions.php';
cors();
// Read the raw POST data
$data = json_decode(file_get_contents('php://input'), true)
or die(return_error(400, 'Invalid data'));
switch ($data['event']) {
case "DEVICE_READY":
case "ALREADY_TESTING":
case "TEST_STARTED":
case "CONTINUE_TEST":
echo return_success();
send_notification($data['event']);
break;
case "NEW_DATA":
echo return_success();
break;
case "TEST_FINISHED":
test_finished();
break;
default:
die(return_error(400, 'Unknown event ' . $data['event']));
break;
}
function send_notification($msg, $url = null) {
global $hawebhook;
$post = [ 'message' => $msg ];
if ($url != null) $post['url'] = $url;
$ch = curl_init($hawebhook);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
$rc = curl_exec($ch);
curl_close($ch);
}
function test_finished() {
global $dbconnstring;
global $data;
global $test_url;
$dbconn = pg_connect($dbconnstring)
or die(return_error(500, 'connect Datenbankfehler: ' . pg_last_error()));
$query = "INSERT INTO tests VALUES (DEFAULT, $1, $2) RETURNING id";
$values = [ date('Y-m-d H:i:s'), json_encode($data) ];
$rc = pg_query_params($dbconn, $query, $values);
if ($rc) {
$id = pg_fetch_result($rc, 0, 0);
echo return_success();
send_notification("Test fertig. ID: " . $id, $test_url + $id);
} else {
echo return_error(500, 'insert Datenbankfehler: ' . pg_last_error());
}
pg_close($dbconn);
}
?>