You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
9.1 KiB
Plaintext

<?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_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 );
$result = $query->execute();
foreach ( $result as $row ) {
$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 );
}
}
return $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 ) ) {
db_insert( 'fulcrm_webhook_entity_mapping' )->fields( array( 'fulcrm_type' => $fulcrm_type,
'fulcrm_pk' => $fulcrm_pk,
'entity_type' => $return[ 'entity_type' ],
'bundle' => array_key_exists( 'bundle', $return ) ? $return[ 'bundle' ] : $return[ 'entity_type' ],
'entity_id' => $return[ 'entity_id' ],
) )->execute();
return $return;
}
}
}
}
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 );
$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 );
}
}
}
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();
$payload_error_msg = json_last_error_msg();
} 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();
}
}