From a0eb7178c86c060968ba4c61739131dbb6ee0318 Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sun, 13 Mar 2016 12:39:58 +0100 Subject: [PATCH] flesh out webhook framework --- fulcrm_webhook.module | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/fulcrm_webhook.module b/fulcrm_webhook.module index 7c801d3..291592d 100644 --- a/fulcrm_webhook.module +++ b/fulcrm_webhook.module @@ -38,8 +38,35 @@ function fulcrm_webhook_menu() { function fulcrm_webhook_webhook( $uuid ) { 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 { - drupal_json_output( array( 'error' => 'incorrect webhook UUID' ) ); + drupal_access_denied(); } }