Erster Commit
This commit is contained in:
72
webhook.php
Normal file
72
webhook.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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);
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user