flesh out webhook framework

master
Marek Isalski 9 years ago
parent 55975f03dc
commit a0eb7178c8

@ -38,8 +38,35 @@ function fulcrm_webhook_menu() {
function fulcrm_webhook_webhook( $uuid ) { function fulcrm_webhook_webhook( $uuid ) {
if ( $uuid === variable_get( 'fulcrm_webhook_url' ) ) { if ( $uuid === variable_get( 'fulcrm_webhook_url' ) ) {
drupal_json_output( array( 'status' => 'ok' ) ); $headers = getallheaders();
$payload = null;
$payload_error = null;
$method = $_SERVER[ 'REQUEST_METHOD' ];
if ( array_key_exists( 'Content-Type', $headers ) && ( $headers[ 'Content-Type' ] === 'application/json' ) ) {
$payload = json_decode( file_get_contents( "php://input" ) );
$payload_error = json_last_error();
$payload_error_msg = json_last_error_msg();
}
switch ( $method ) {
case 'GET':
drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must be POST, PATCH, PUT, or DELETE" ) );
break;
case 'DELETE':
case 'POST':
case 'PATCH':
case 'PUT':
if ( $payload === null ) {
// actually do something :)
drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) );
} else {
drupal_json_output( array( 'status' => 'ok', 'headers' => $headers, 'method' => $method ) );
}
break;
default:
}
} else { } else {
drupal_json_output( array( 'error' => 'incorrect webhook UUID' ) ); drupal_access_denied();
} }
} }

Loading…
Cancel
Save