From 00c06fea92264a1a64a5ac8671300d48e6300ea9 Mon Sep 17 00:00:00 2001 From: Marek Isalski Date: Sun, 13 Mar 2016 20:30:32 +0100 Subject: [PATCH] add fulcrm_apiv2_prevent_loop to track inbound webhooks/etc --- fulcrm_apiv2.module | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/fulcrm_apiv2.module b/fulcrm_apiv2.module index 4903e26..9f43045 100644 --- a/fulcrm_apiv2.module +++ b/fulcrm_apiv2.module @@ -30,6 +30,20 @@ function fulcrm_apiv2_menu() { return $items; } +function fulcrm_apiv2_prevent_loop( $headers ) { + $incoming_client_ids = &drupal_static(__FUNCTION__); + + if ( array_key_exists( 'X-fulcrm-Client-ID', $headers ) ) { + $incoming_client_ids = array_map( "trim", explode( ',', $headers[ 'X-fulcrm-Client-ID' ] ) ); + if ( in_array( $client_id, $incoming_client_ids ) ) + return false; + } else { + $incoming_client_ids = array(); + } + + return true; +} + function fulcrm_apiv2_http( $method, $object = null, $data = null, $query = null ) { $url = null; @@ -76,7 +90,12 @@ function fulcrm_apiv2_http( $method, $object = null, $data = null, $query = null $request_headers[] = 'Content-Length: ' . strlen( $data ); } - $request_headers[] = 'X-fulcrm-Client-ID: ' . variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] ); + $incoming_client_ids = &drupal_static( 'fulcrm_apiv2_prevent_loop' ); + if ( !is_array( $incoming_client_ids ) ) + $incoming_client_ids = array(); + $incoming_client_ids[] = variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] ); + + $request_headers[] = 'X-fulcrm-Client-ID: ' . implode( ', ', $incoming_client_ids ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );