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.

196 lines
7.8 KiB
Plaintext

<?php
function fulcrm_apiv2_permission() {
return array( 'administer fulcrm APIv2' => array( 'title' => t('Administer fulcrm APIv2'),
'description' => t('Perform main installation/administration tasks for fulcrm APIv2.'),
),
);
}
function fulcrm_apiv2_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/apiv2' ] = array( 'page callback' => 'fulcrm_apiv2_admin',
'file' => 'fulcrm_apiv2.admin.inc',
'title' => 'fulcrm APIv2',
'description' => 'Credentials and settings for interacting with fulcrm.org APIv2.',
'access callback' => 'user_access',
'access arguments' => array('administer fulcrm APIv2'),
);
return $items;
}
function fulcrm_apiv2_client_id() {
return variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] );
}
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' ] ) );
$client_id = fulcrm_apiv2_client_id();
if ( in_array( $client_id, $incoming_client_ids ) )
return false;
} else {
$incoming_client_ids = array();
}
return true;
}
function fulcrm_apiv2_url_to_type( $url ) {
preg_match( '/https:\/\/[^\/]+\/api\/v2\/([a-z_0-9]+)\/([0-9]+)?\//', $url, $matches );
return $matches[ 1 ];
}
function fulcrm_apiv2_url_to_pk( $url ) {
preg_match( '/https:\/\/[^\/]+\/api\/v2\/([a-z_0-9]+)\/([0-9]+)?\//', $url, $matches );
return intval( $matches[ 2 ] );
}
function fulcrm_apiv2_url_is_type( $url, $type ) {
if ( is_string( $url ) ) {
return ( fulcrm_apiv2_url_to_type( $url ) === $type );
} else
return false;
}
function fulcrm_apiv2_make_url( $type, $id = null ) {
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . '/api/v2/' . $type . '/';
if ( $id !== null )
$url .= $id . '/';
return $url;
}
function fulcrm_apiv2_http( $method, $object = null, $data = null, $query = null ) {
$url = null;
if ( substr( $object, 0, 8 ) === 'http://' ) {
throw new Exception( "cannot use HTTP on fulcrm APIv2" );
} elseif ( substr( $object, 0, 8 ) === 'https://' ) {
$host = parse_url( $object, PHP_URL_HOST );
if ( ( $host === 'fulcrm.org' ) || ( substr( $host, -10 ) === 'fulcrm.org' ) ) {
$object = parse_url( $object, PHP_URL_PATH );
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . $object;
} else
throw new Exception( "cannot point fulcrm APIv2 away from fulcrm.org" );
} elseif ( substr( $object, 0, 8 ) === '/api/v2/' ) {
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . $object;
} else {
$url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . '/api/v2/' . $object;
}
if ( is_null( $object ) && is_array( $data ) ) {
if ( array_key_exists( 'url', $data ) ) {
if ( is_null( $url ) )
$url = $data[ 'url' ];
elseif ( $data[ 'url' ] !== $url )
throw new Exception( '$object URL specified different from $data["url"]' );
}
}
if ( is_array( $query ) )
$url = $url . '?' . http_build_query( $query );
elseif ( is_string( $query ) )
$url = $url . '?' . $query;
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method );
$request_headers = array( 'Authorization: ApiKey ' . variable_get( 'fulcrm_apiv2_user' ) . ':' . variable_get( 'fulcrm_apiv2_key' ),
);
if ( $data !== null ) {
if ( is_array( $data ) ) {
$data = json_encode( $data );
$request_headers[] = 'Content-Type: application/json';
}
curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
}
$incoming_client_ids = &drupal_static( 'fulcrm_apiv2_prevent_loop' );
if ( !is_array( $incoming_client_ids ) )
$incoming_client_ids = array();
$incoming_client_ids[] = fulcrm_apiv2_client_id();
$request_headers[] = 'X-fulcrm-Client-ID: ' . implode( ', ', $incoming_client_ids );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_HTTPHEADER, $request_headers );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_TCP_NODELAY, true );
try {
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 5 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 30 );
} catch ( Exception $e ) {
// do nothing
}
$result = curl_exec( $curl );
$response = curl_getinfo( $curl );
$header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
$header = substr( $result, 0, $header_size );
$body = substr( $result, $header_size );
$headers = array();
foreach ( preg_split( '/[\r\n]+/', $header ) as $headerline ) {
if ( preg_match( '/([-A-Za-z0-9_]+):\s*(.*)/', $headerline, $matches ) )
$headers[ $matches[ 1 ] ] = $matches[ 2 ];
}
$r = array( 'headers' => $headers,
'body' => $body,
'code' => $response[ 'http_code' ],
);
if ( $response[ 'http_code' ] == 200 ) {
$r[ 'success' ] = true; // GET
} else if ( $response[ 'http_code' ] == 201 ) {
$r[ 'success' ] = true; // POST
} else if ( $response[ 'http_code' ] == 202 ) {
$r[ 'success' ] = true; // PATCH
} else if ( $response[ 'http_code' ] == 204 ) {
$r[ 'success' ] = true; // DELETE
} else {
$r[ 'success' ] = false;
}
$r[ 'data' ] = json_decode( $body, $assoc = true );
$r[ 'data_error' ] = json_last_error();
return $r;
}
function fulcrm_apiv2_GET( $object, $query = null ) {
return fulcrm_apiv2_http( 'GET', $object, $data = null, $query = $query );
}
function fulcrm_apiv2_POST( $object, $data, $query = null ) {
return fulcrm_apiv2_http( 'POST', $object, $data = $data, $query = $query );
}
function fulcrm_apiv2_PUT( $object, $data, $query = null ) {
return fulcrm_apiv2_http( 'PUT', $object, $data = $data, $query = $query );
}
function fulcrm_apiv2_PATCH( $object, $data, $query = null ) {
return fulcrm_apiv2_http( 'PATCH', $object, $data = $data, $query = $query );
}
function fulcrm_apiv2_DELETE( $object, $query = null ) {
return fulcrm_apiv2_http( 'DELETE', $object, $data = null, $query = $query );
}