initial version of fulcrm_apiv2

master
Marek Isalski 8 年之前
當前提交 5de63a22bf

@ -0,0 +1,46 @@
<?php
function fulcrm_apiv2_admin_form_submit( $form, &$form_state ) {
variable_set( 'fulcrm_apiv2_user', $form_state[ 'values' ][ 'apiuser' ] );
variable_set( 'fulcrm_apiv2_key', $form_state[ 'values' ][ 'apikey' ] );
variable_set( 'fulcrm_apiv2_client_id', $form_state[ 'values' ][ 'clientid' ] );
}
function fulcrm_apiv2_admin_form( $form, &$form_state ) {
$form[ 'title' ] = array( '#type' => 'item',
'#title' => 'fulcrm APIv2 Settings',
'#description' => '',
);
$form[ 'api' ] = array( '#type' => 'fieldset',
'#title' => 'fulcrm.org APIv2 Credentials',
);
$form[ 'api' ][ 'apiuser' ] = array( '#type' => 'textfield',
'#title' => 'API Username',
'#description' => 'fulcrm.org APIv2 username',
'#default_value' => variable_get( 'fulcrm_apiv2_user', '' ),
);
$form[ 'api' ][ 'apikey' ] = array( '#type' => 'textfield',
'#title' => 'API Key',
'#description' => 'fulcrm.org APIv2 key',
'#default_value' => variable_get( 'fulcrm_apiv2_key', '' ),
);
$form[ 'api' ][ 'clientid' ] = array( '#type' => 'textfield',
'#title' => 'Client ID',
'#description' => 'fulcrm.org APIv2 client ID',
'#default_value' => variable_get( 'fulcrm_apiv2_client_id', '' ),
);
$form[ 'actions' ] = array( '#type' => 'actions' );
$form[ 'actions' ][ 'save' ] = array( '#type' => 'submit',
'#value' => t('Save'),
'#submit' => array( 'fulcrm_apiv2_admin_form_submit' ),
);
return $form;
}
function fulcrm_apiv2_admin() {
return drupal_get_form( 'fulcrm_apiv2_admin_form' );
}

@ -0,0 +1,7 @@
name = fulcrm APIv2
description = fulcrm API version 2
core = 7.x
package = fulcrm
;files[] = fulcrm_apiv2.admin.inc
configure = admin/config/services/fulcrm/apiv2
dependencies[] = fulcrm_uuid

@ -0,0 +1,11 @@
<?php /* -*- php -*- */
function fulcrm_apiv2_install() {
$url = $GLOBALS[ 'base_url' ];
$url = parse_url( $url, PHP_URL_HOST );
$url = explode( '.', $url );
$url = array_reverse( $url );
$url[] = fulcrm_uuid_uuid4();
$url = implode( '.', $url );
set_variable( 'fulcrm_apiv2_client_id', $url );
}

@ -0,0 +1,106 @@
<?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_http( $method, $object, $data = null ) {
$curl = curl_init( 'https://fulcrm.org' . $object );
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 );
$request_headers[] = 'Content-Length: ' . strlen( $data );
}
$request_headers[] = 'X-fulcrm-Client-ID: ' . variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] );
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 );
$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;
}
return $r;
}
function fulcrm_apiv2_GET( $object ) {
return fulcrm_apiv2_http( 'GET', $object );
}
function fulcrm_apiv2_POST( $object, $data ) {
return fulcrm_apiv2_http( 'POST', $object, $data );
}
function fulcrm_apiv2_PUT( $object, $data ) {
return fulcrm_apiv2_http( 'PUT', $object, $data );
}
function fulcrm_apiv2_PATCH( $object, $data ) {
return fulcrm_apiv2_http( 'PATCH', $object, $data );
}
function fulcrm_apiv2_DELETE( $object ) {
return fulcrm_apiv2_http( 'DELETE', $object );
}
Loading…
取消
儲存