66 lines
1.3 KiB
PHP
66 lines
1.3 KiB
PHP
<?php
|
|
|
|
function httpCode($code) {
|
|
http_response_code($code);
|
|
headers_sent();
|
|
}
|
|
|
|
function response(array $content, $code = 200) {
|
|
httpCode($code);
|
|
header('Content-type: application/json');
|
|
echo json_encode($content);
|
|
|
|
exit;
|
|
}
|
|
|
|
function badSecret() {
|
|
httpCode(403);
|
|
response([
|
|
'error' => 'Invalid secret',
|
|
]);
|
|
}
|
|
|
|
function badRequest(array $content = []) {
|
|
httpCode(400);
|
|
response($content);
|
|
}
|
|
|
|
function requiredBagVar($bag, $index) {
|
|
if (!array_key_exists($index, $bag)) {
|
|
badRequest([
|
|
'error' => 'Bad request',
|
|
'missing_param' => [$index, $bag],
|
|
]);
|
|
}
|
|
}
|
|
|
|
function isValidSecret($secret, $accounts) {
|
|
return in_array(
|
|
$secret,
|
|
$accounts
|
|
);
|
|
}
|
|
|
|
requiredBagVar($_POST, 'payload');
|
|
$payload = json_decode($_POST['payload'], true);
|
|
|
|
requiredBagVar($payload, 'secret');
|
|
|
|
$accounts = require 'accounts.php';
|
|
|
|
if (!isValidSecret($payload['secret'], $accounts)) {
|
|
badSecret();
|
|
}
|
|
|
|
$repository = preg_replace('/-project$/', '', $payload['repository']['name']);
|
|
$repository = str_replace(['/', '.'], '', $repository);
|
|
|
|
$command = 'sudo -u git $HOME/bin/webhook-project '.escapeshellarg($repository);
|
|
$execute = shell_exec($command);
|
|
|
|
response([
|
|
'data' => $repository,
|
|
'exec' => $command,
|
|
]);
|
|
|