HTTP Code issue, response message

This commit is contained in:
Simon Vieille 2016-01-30 21:42:21 +01:00
parent 08227b3802
commit 15fb754801

View file

@ -6,23 +6,23 @@ function httpCode($code) {
} }
function response(array $content, $code = 200) { function response(array $content, $code = 200) {
httpCode($code);
header('Content-type: application/json'); header('Content-type: application/json');
httpCode($code);
echo json_encode($content); echo json_encode($content);
exit; exit;
} }
function badSecret() { function badSecret() {
httpCode(403); response(
response([ [
'error' => 'Invalid secret', 'error' => 'Invalid secret',
]); ],
403
);
} }
function badRequest(array $content = []) { function badRequest(array $content = []) {
httpCode(400); response($content, 404);
response($content);
} }
function requiredBagVar($bag, $index) { function requiredBagVar($bag, $index) {
@ -45,9 +45,7 @@ requiredBagVar($_POST, 'payload');
$payload = json_decode($_POST['payload'], true); $payload = json_decode($_POST['payload'], true);
requiredBagVar($payload, 'secret'); requiredBagVar($payload, 'secret');
$accounts = require 'accounts.php'; $accounts = require 'accounts.php';
if (!isValidSecret($payload['secret'], $accounts)) { if (!isValidSecret($payload['secret'], $accounts)) {
badSecret(); badSecret();
} }
@ -59,7 +57,7 @@ $command = 'sudo -u git $HOME/bin/webhook-project '.escapeshellarg($repository);
$execute = shell_exec($command); $execute = shell_exec($command);
response([ response([
'data' => $repository, 'action' => 'Gitnet project push event',
'exec' => $command, 'output' => $execute,
]); ]);