|
|
|
<?php
|
|
|
|
|
|
|
|
function fulcrm_webhook_permission() {
|
|
|
|
return array( 'administer fulcrm webhooks' => array( 'title' => t('Administer fulcrm webhooks'),
|
|
|
|
'description' => t('Perform main installation/administration tasks for fulcrm webhooks.'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_menu() {
|
|
|
|
/*
|
|
|
|
$items[ 'admin/config/services/fulcrm' ] = array( 'page callback' => 'system_admin_menu_block_page',
|
|
|
|
'file' => 'system.admin.inc',
|
|
|
|
'file path' => drupal_get_path('module', 'system'),
|
|
|
|
'title' => 'fulcrm',
|
|
|
|
'description' => 'Configuration integration with fulcrm.org.',
|
|
|
|
'position' => 'right',
|
|
|
|
'access callback' => 'user_access',
|
|
|
|
'access arguments' => array('access administration pages'),
|
|
|
|
);
|
|
|
|
*/
|
|
|
|
$items[ 'admin/config/services/fulcrm/webhook' ] = array( 'page callback' => 'fulcrm_webhook_admin',
|
|
|
|
'file' => 'fulcrm_webhook.admin.inc',
|
|
|
|
'title' => 'fulcrm webhooks',
|
|
|
|
'description' => 'Configure webhooks for fulcrm.org.',
|
|
|
|
'access callback' => 'user_access',
|
|
|
|
'access arguments' => array('administer fulcrm webhooks'),
|
|
|
|
);
|
|
|
|
$items[ 'services/fulcrm/webhook/%' ] = array( 'page callback' => 'fulcrm_webhook_webhook',
|
|
|
|
'page arguments' => array(3),
|
|
|
|
'type' => MENU_CALLBACK,
|
|
|
|
'access callback' => 'user_access',
|
|
|
|
'access arguments' => array('access content'),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_get_pk_for_entity_type( $entity_type, $entity_id, $fulcrm_type ) {
|
|
|
|
$query = db_select( 'fulcrm_webhook_entity_mapping', 'fwem' )
|
|
|
|
->fields( 'fwem', array( 'fulcrm_pk' ) )
|
|
|
|
->condition( 'entity_type', $entity_type )
|
|
|
|
->condition( 'entity_id', $entity_id )
|
|
|
|
->condition( 'fulcrm_type', $fulcrm_type );
|
|
|
|
$result = $query->execute();
|
|
|
|
return $result->fetchField();
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_set_entity_mapping( $entity_type, $bundle, $entity_id, $fulcrm_type, $fulcrm_pk ) {
|
|
|
|
db_merge( 'fulcrm_webhook_entity_mapping' )
|
|
|
|
->key( array( 'entity_type' => $entity_type,
|
|
|
|
'bundle' => $bundle,
|
|
|
|
'entity_id' => $entity_id,
|
|
|
|
) )
|
|
|
|
->insertFields( array( 'fulcrm_type' => $fulcrm_type,
|
|
|
|
'fulcrm_pk' => $fulcrm_pk,
|
|
|
|
'entity_type' => $entity_type,
|
|
|
|
'bundle' => $bundle,
|
|
|
|
'entity_id' => $entity_id,
|
|
|
|
) )
|
|
|
|
->updateFields( array( 'fulcrm_type' => $fulcrm_type,
|
|
|
|
'fulcrm_pk' => $fulcrm_pk,
|
|
|
|
) )
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_load_entity_for_url( $url ) {
|
|
|
|
$fulcrm_type = fulcrm_apiv2_url_to_type( $url );
|
|
|
|
$fulcrm_pk = fulcrm_apiv2_url_to_pk( $url );
|
|
|
|
|
|
|
|
$query = db_select( 'fulcrm_webhook_entity_mapping', 'fwem' )
|
|
|
|
->fields( 'fwem', array( 'entity_type', 'entity_id' ) )
|
|
|
|
->condition( 'fulcrm_type', $fulcrm_type )
|
|
|
|
->condition( 'fulcrm_pk', $fulcrm_pk );
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
foreach ( $result as $row )
|
|
|
|
return entity_load( $row->entity_type, array( $row->entity_id ) )[ $row->entity_id ];
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_createupdate_entity( $data ) {
|
|
|
|
$return = array( 'data' => $data );
|
|
|
|
|
|
|
|
if ( array_key_exists( 'url', $data ) ) {
|
|
|
|
$url = $data[ 'url' ];
|
|
|
|
$fulcrm_type = fulcrm_apiv2_url_to_type( $url );
|
|
|
|
$fulcrm_pk = fulcrm_apiv2_url_to_pk( $url );
|
|
|
|
|
|
|
|
$query = db_select( 'fulcrm_webhook_entity_mapping', 'fwem' )
|
|
|
|
->fields( 'fwem', array( 'entity_type', 'entity_id' ) )
|
|
|
|
->condition( 'fulcrm_type', $fulcrm_type )
|
|
|
|
->condition( 'fulcrm_pk', $fulcrm_pk );
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
$result = $query->execute();
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
$found = true;
|
|
|
|
$return = array( 'data' => $data, 'entity' => entity_load( $row->entity_type, array( $row->entity_id ) ) );
|
|
|
|
|
|
|
|
foreach ( module_implements( 'update_entity_for_fulcrm_' . $fulcrm_type ) as $module ) {
|
|
|
|
$function = $module . '_update_entity_for_fulcrm_' . $fulcrm_type;
|
|
|
|
if ( function_exists( $function ) ) {
|
|
|
|
$function( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $found ) {
|
|
|
|
foreach ( module_implements( 'update_for_fulcrm_' . $fulcrm_type ) as $module ) {
|
|
|
|
$function = $module . '_update_for_fulcrm_' . $fulcrm_type;
|
|
|
|
if ( function_exists( $function ) ) {
|
|
|
|
$function( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( module_implements( 'create_for_fulcrm_' . $fulcrm_type ) as $module ) {
|
|
|
|
$function = $module . '_create_for_fulcrm_' . $fulcrm_type;
|
|
|
|
if ( function_exists( $function ) ) {
|
|
|
|
$function( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( module_implements( 'create_entity_for_fulcrm_' . $fulcrm_type ) as $module ) {
|
|
|
|
$function = $module . '_create_entity_for_fulcrm_' . $fulcrm_type;
|
|
|
|
if ( function_exists( $function ) ) {
|
|
|
|
$function( $return );
|
|
|
|
|
|
|
|
if ( array_key_exists( 'entity', $return ) ) {
|
|
|
|
fulcrm_webhook_set_entity_mapping( $entity_type = $return[ 'entity_type' ],
|
|
|
|
$bundle = array_key_exists( 'bundle', $return ) ? $return[ 'bundle' ] : $return[ 'entity_type' ],
|
|
|
|
$entity_id = $return[ 'entity_id' ],
|
|
|
|
$fulcrm_type = $fulcrm_type,
|
|
|
|
$fulcrm_pk = $fulcrm_pk );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_delete_entity( $data ) {
|
|
|
|
$return = array( 'data' => $data );
|
|
|
|
|
|
|
|
if ( array_key_exists( 'url', $data ) ) {
|
|
|
|
$url = $data[ 'url' ];
|
|
|
|
$fulcrm_type = fulcrm_apiv2_url_to_type( $url );
|
|
|
|
$fulcrm_pk = fulcrm_apiv2_url_to_pk( $url );
|
|
|
|
$return[ 'fulcrm_type' ] = $fulcrm_type;
|
|
|
|
$return[ 'fulcrm_pk' ] = $fulcrm_pk;
|
|
|
|
|
|
|
|
$query = db_select( 'fulcrm_webhook_entity_mapping', 'fwem' )
|
|
|
|
->fields( 'fwem', array( 'entity_type', 'entity_id' ) )
|
|
|
|
->condition( 'fulcrm_type', $fulcrm_type )
|
|
|
|
->condition( 'fulcrm_pk', $fulcrm_pk );
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
$return[ 'entity' ] = entity_load( $row->entity_type, array( $row->entity_id ) );
|
|
|
|
|
|
|
|
foreach ( module_implements( 'delete_entity_for_fulcrm_' . $fulcrm_type ) as $module ) {
|
|
|
|
$function = $module . '_delete_entity_for_fulcrm_' . $fulcrm_type;
|
|
|
|
if ( function_exists( $function ) ) {
|
|
|
|
$function( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( module_implements( 'delete_for_fulcrm_' . $fulcrm_type ) as $module ) {
|
|
|
|
$function = $module . '_delete_for_fulcrm_' . $fulcrm_type;
|
|
|
|
if ( function_exists( $function ) ) {
|
|
|
|
$function( $return );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
db_delete( 'fulcrm_webhook_entity_mapping', 'fwem' )
|
|
|
|
->condition( 'fulcrm_type', $fulcrm_type )
|
|
|
|
->condition( 'fulcrm_pk', $fulcrm_pk )
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fulcrm_webhook_webhook( $uuid ) {
|
|
|
|
if ( $uuid === variable_get( 'fulcrm_webhook_url' ) ) {
|
|
|
|
$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" ), $assoc = TRUE );
|
|
|
|
$payload_error = json_last_error();
|
|
|
|
if ( function_exists( 'json_last_error_msg' ) )
|
|
|
|
$payload_error_msg = json_last_error_msg();
|
|
|
|
else
|
|
|
|
$payload_error_msg = 'JSON error unavailable';
|
|
|
|
} else {
|
|
|
|
$payload_error_msg = 'no payload';
|
|
|
|
}
|
|
|
|
|
|
|
|
$client_id = fulcrm_apiv2_client_id();
|
|
|
|
|
|
|
|
if ( fulcrm_apiv2_prevent_loop( $headers ) ) {
|
|
|
|
$op = null;
|
|
|
|
|
|
|
|
if ( $method === 'GET' ) {
|
|
|
|
drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must POST, PATCH, PUT, or DELETE" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $payload === null ) {
|
|
|
|
drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = null;
|
|
|
|
|
|
|
|
switch ( $method ) {
|
|
|
|
case 'DELETE':
|
|
|
|
$return = fulcrm_webhook_delete_entity( $payload );
|
|
|
|
break;
|
|
|
|
case 'POST':
|
|
|
|
case 'PATCH':
|
|
|
|
case 'PUT':
|
|
|
|
$return = fulcrm_webhook_createupdate_entity( $payload );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
drupal_json_output( array( 'status' => 'error', 'info' => 'unsupported method' ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_array( $return ) ) {
|
|
|
|
if ( array_key_exists( 'response', $return ) )
|
|
|
|
drupal_json_output( array( 'status' => 'ok', 'response' => $return[ 'response' ] ) );
|
|
|
|
else
|
|
|
|
drupal_json_output( array( 'status' => 'ok' ) );
|
|
|
|
} else {
|
|
|
|
drupal_json_output( array( 'status' => 'error', 'info' => 'no return from internal methods' ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
drupal_json_output( array( 'status' => 'ok', 'info' => 'loop prevention' ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
drupal_access_denied();
|
|
|
|
}
|
|
|
|
}
|