EndG-2048-multiplayer/webhook/webhook.php

82 lines
2.3 KiB
PHP
Raw Normal View History

2022-10-17 09:24:21 +02:00
<?php
# Powered By EndMove to work with Git-linux and Gitea
# Version 1.0.1
# Last Update 7-15-2022
#====================================#
# CONFIG #
#====================================#
$secret_key = file_get_contents('./secret-key.secret');
$debug = true;
#====================================#
// check HTTP requete method type
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
if ($debug) echo('FAILED - not POST : '. $_SERVER['REQUEST_METHOD']);
exit();
}
// get content type
$content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
// check HTTP requete content type
if ($content_type != 'application/json') {
if ($debug) echo('FAILED - not application/json : '. $content_type);
exit();
}
// get payload
$payload = trim(file_get_contents("php://input"));
// check if payload is not empty
if (empty($payload)) {
if ($debug) echo('FAILED - no payload');
exit();
}
// get header signature
$header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : '';
// check header signature
if (empty($header_signature)) {
if ($debug) echo('FAILED - header signature missing');
exit();
}
// calculate payload signature
$payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
// check payload signature against header signature
if ($header_signature !== $payload_signature) {
if ($debug) echo('FAILED - payload signature');
exit();
}
// convert json to array
$decoded = json_decode($payload, true);
// check for json decode errors
if (json_last_error() !== JSON_ERROR_NONE) {
if ($debug) echo('FAILED - json decode : '. json_last_error());
exit();
}
// verifications succeeded, process the repository updating...
$commands = [
'whoami',
'git reset --hard HEAD',
'git clean -f ../',
'git pull',
'git status',
'git submodule sync',
'git submodule update',
'git submodule status',
];
// run the commands to update and get the result
$full_output = '';
foreach($commands AS $command) {
// run it
$out = exec($command);
// beautify output result
$full_output .= "CMD [$command] -> RESPONSE (".htmlentities(trim($out)).")\n";
}
// display complete output if debug on, else show OK
echo($debug ? $full_output : "OK.");
?>