web-deployment-webhook/deploy.php

133 lines
4.5 KiB
PHP
Raw Permalink Normal View History

<?php
$secret_key = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
// check for POST request
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
error_log('WebHook usage FAILED - not POST - Request method: '. $_SERVER['REQUEST_METHOD']);
echo "<!DOCTYPE html>
<html>
<head>
<title>701 Meh</title>
</head>
<body>
<h1>Meh</h1>
<p>Error 701: Meh (as specified by the RFC for 700 http codes)</p>
<hr />
<address>Apache (Ubuntu) Server at admin.strassenkind.ip</address>
</body>
</html>
";
exit();
}
// get content type
$content_type = isset($_SERVER['CONTENT_TYPE']) ? strtolower(trim($_SERVER['CONTENT_TYPE'])) : '';
if ($content_type != 'application/json') {
error_log('WebHook usage FAILED - not application/json - Content type: '. $content_type);
echo "<!DOCTYPE html>
<html>
<head>
<title>756 Insufficiently Polite</title>
</head>
<body>
<h1>Insufficiently Polite</h1>
<p>Error 756: Insufficiently polite (as specified by the RFC for 700 http codes)</p>
<hr />
<address>Apache (Ubuntu) Server at admin.strassenkind.ip</address>
</body>
</html>
";
exit();
}
// get payload
$payload = trim(file_get_contents("php://input"));
if (empty($payload)) {
error_log('WebHook usage FAILED - no payload');
echo "<!DOCTYPE html>
<html>
<head>
<title>768 Accidentally Took Sleeping Pills Instead Of Migraine Pills During Crunch Week</title>
</head>
<body>
<h1>Accidentally Took Sleeping Pills Instead Of Migraine Pills During Crunch Week</h1>
<p>Error 768: Accidentally Took Sleeping Pills Instead Of Migraine Pills During Crunch Week (as specified by the RFC for 700 http codes)</p>
<hr />
<address>Apache (Ubuntu) Server at admin.strassenkind.ip</address>
</body>
</html>
";
exit();
}
// get header signature
$header_signature = isset($_SERVER['HTTP_X_GITEA_SIGNATURE']) ? $_SERVER['HTTP_X_GITEA_SIGNATURE'] : '';
if (empty($header_signature)) {
error_log('WebHook usage FAILED - header signature missing');
echo "<!DOCTYPE html>
<html>
<head>
<title>787 Further Funding Required</title>
</head>
<body>
<h1>Further Funding Required</h1>
<p>Error 701: Further Funding Required (as specified by the RFC for 700 http codes)</p>
<hr />
<address>Apache (Ubuntu) Server at admin.strassenkind.ip</address>
</body>
</html>
";
exit();
}
// calculate payload signature
$payload_signature = hash_hmac('sha256', $payload, $secret_key, false);
// check payload signature against header signature
if ($header_signature != $payload_signature) {
error_log('WebHook usage FAILED - payload signature');
echo "<!DOCTYPE html>
<html>
<head>
<title>776 Error on the Exception</title>
</head>
<body>
<h1>Error on the Exception</h1>
<p>Error 776: Error on the Exception (as specified by the RFC for 700 http codes)</p>
<hr />
<address>Apache (Ubuntu) Server at admin.strassenkind.ip</address>
</body>
</html>
";
exit();
}
// convert json to array
$decoded = json_decode($payload, true);
// check for json decode errors
if (json_last_error() !== JSON_ERROR_NONE) {
error_log('WebHook usage FAILED - json decode - '. json_last_error());
echo "<!DOCTYPE html>
<html>
<head>
<title>705 I wrote the code and missed the necessary validation by an oversight</title>
</head>
<body>
<h1>I wrote the code and missed the necessary validation by an oversight</h1>
<p>Error 705: I wrote the code and missed the necessary validation by an oversight (as specified by the RFC for 700 http codes)</p>
<hr />
<address>Apache (Ubuntu) Server at admin.strassenkind.ip</address>
</body>
</html>
";
exit();
}
// success, do something
system("/var/www/git/deploy-now " . $decoded['repository']['name'] . " " . $decoded['repository']['ssh_url'] . " >>stdout.log 2>>stderr.log &");