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.

124 lines
5.5 KiB
Plaintext

8 years ago
<?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.',
8 years ago
'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'),
);
8 years ago
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_find_entity( $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_load( $row->entity_type, array( $row->entity_id ) );
}
module_invoke_all( 'fulcrm_find_entity', $fulcrm_type, $fulcrm_pk );
}
}
function fulcrm_webhook_fulcrm_find_entity( $fulcrm_type, $fulcrm_pk ) {
}
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" ) );
$payload_error = json_last_error();
$payload_error_msg = json_last_error_msg();
}
$client_id = variable_get( 'fulcrm_apiv2_client_id' );
if ( fulcrm_apiv2_prevent_loop( $headers ) ) {
$op = null;
switch ( $method ) {
case 'GET':
drupal_json_output( array( "status" => "error", "info" => "GET requests do nothing; webhooks must POST, PATCH, PUT, or DELETE" ) );
break;
case 'DELETE':
$op = 'DELETE';
break;
case 'POST':
$op = 'CREATE';
break;
case 'PATCH':
case 'PUT':
$op = 'UPDATE';
break;
default:
drupal_json_output( array( 'status' => 'error', 'info' => 'unsupported method' ) );
break;
}
if ( $op ) {
if ( $payload === null ) {
drupal_json_output( array( 'status' => 'error', 'info' => $payload_error_msg ) );
} else {
// actually do something :)
drupal_json_output( array( 'status' => 'ok', 'headers' => $headers, 'method' => $method ) );
}
}
} else {
drupal_json_output( array( 'status' => 'ok', 'info' => 'loop prevention' ) );
}
} else {
drupal_access_denied();
}
}