You can not 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

  1. <?php
  2. function fulcrm_apiv2_permission() {
  3. return array( 'administer fulcrm APIv2' => array( 'title' => t('Administer fulcrm APIv2'),
  4. 'description' => t('Perform main installation/administration tasks for fulcrm APIv2.'),
  5. ),
  6. );
  7. }
  8. function fulcrm_apiv2_menu() {
  9. /*
  10. $items[ 'admin/config/services/fulcrm' ] = array( 'page callback' => 'system_admin_menu_block_page',
  11. 'file' => 'system.admin.inc',
  12. 'file path' => drupal_get_path('module', 'system'),
  13. 'title' => 'fulcrm',
  14. 'description' => 'Configuration integration with fulcrm.org.',
  15. 'position' => 'right',
  16. 'access callback' => 'user_access',
  17. 'access arguments' => array('access administration pages'),
  18. );
  19. */
  20. $items[ 'admin/config/services/fulcrm/apiv2' ] = array( 'page callback' => 'fulcrm_apiv2_admin',
  21. 'file' => 'fulcrm_apiv2.admin.inc',
  22. 'title' => 'fulcrm APIv2',
  23. 'description' => 'Credentials and settings for interacting with fulcrm.org APIv2.',
  24. 'access callback' => 'user_access',
  25. 'access arguments' => array('administer fulcrm APIv2'),
  26. );
  27. return $items;
  28. }
  29. function fulcrm_apiv2_client_id() {
  30. return variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] );
  31. }
  32. function fulcrm_apiv2_prevent_loop( $headers ) {
  33. $incoming_client_ids = &drupal_static(__FUNCTION__);
  34. if ( array_key_exists( 'X-fulcrm-Client-ID', $headers ) ) {
  35. $incoming_client_ids = array_map( "trim", explode( ',', $headers[ 'X-fulcrm-Client-ID' ] ) );
  36. $client_id = fulcrm_apiv2_client_id();
  37. if ( in_array( $client_id, $incoming_client_ids ) )
  38. return false;
  39. } else {
  40. $incoming_client_ids = array();
  41. }
  42. return true;
  43. }
  44. function fulcrm_apiv2_url_to_type( $url ) {
  45. preg_match( '/https:\/\/[^\/]+\/api\/v2\/([a-z_0-9]+)\/([0-9]+)?\//', $url, $matches );
  46. return $matches[ 1 ];
  47. }
  48. function fulcrm_apiv2_url_to_pk( $url ) {
  49. preg_match( '/https:\/\/[^\/]+\/api\/v2\/([a-z_0-9]+)\/([0-9]+)?\//', $url, $matches );
  50. return intval( $matches[ 2 ] );
  51. }
  52. function fulcrm_apiv2_url_is_type( $url, $type ) {
  53. if ( is_string( $url ) ) {
  54. return ( fulcrm_apiv2_url_to_type( $url ) === $type );
  55. } else
  56. return false;
  57. }
  58. function fulcrm_apiv2_make_url( $type, $id = null ) {
  59. $url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . '/api/v2/' . $type . '/';
  60. if ( $id !== null )
  61. $url .= $id . '/';
  62. return $url;
  63. }
  64. function fulcrm_apiv2_http( $method, $object = null, $data = null, $query = null ) {
  65. $url = null;
  66. if ( substr( $object, 0, 8 ) === 'http://' ) {
  67. throw new Exception( "cannot use HTTP on fulcrm APIv2" );
  68. } elseif ( substr( $object, 0, 8 ) === 'https://' ) {
  69. $host = parse_url( $object, PHP_URL_HOST );
  70. if ( ( $host === 'fulcrm.org' ) || ( substr( $host, -10 ) === 'fulcrm.org' ) ) {
  71. $object = parse_url( $object, PHP_URL_PATH );
  72. $url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . $object;
  73. } else
  74. throw new Exception( "cannot point fulcrm APIv2 away from fulcrm.org" );
  75. } elseif ( substr( $object, 0, 8 ) === '/api/v2/' ) {
  76. $url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . $object;
  77. } else {
  78. $url = 'https://' . variable_get( 'fulcrm_apiv2_hostname', 'fulcrm.org' ) . '/api/v2/' . $object;
  79. }
  80. if ( is_null( $object ) && is_array( $data ) ) {
  81. if ( array_key_exists( 'url', $data ) ) {
  82. if ( is_null( $url ) )
  83. $url = $data[ 'url' ];
  84. elseif ( $data[ 'url' ] !== $url )
  85. throw new Exception( '$object URL specified different from $data["url"]' );
  86. }
  87. }
  88. if ( is_array( $query ) )
  89. $url = $url . '?' . http_build_query( $query );
  90. elseif ( is_string( $query ) )
  91. $url = $url . '?' . $query;
  92. $curl = curl_init( $url );
  93. curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method );
  94. $request_headers = array( 'Authorization: ApiKey ' . variable_get( 'fulcrm_apiv2_user' ) . ':' . variable_get( 'fulcrm_apiv2_key' ),
  95. );
  96. if ( $data !== null ) {
  97. if ( is_array( $data ) ) {
  98. $data = json_encode( $data );
  99. $request_headers[] = 'Content-Type: application/json';
  100. }
  101. curl_setopt( $curl, CURLOPT_POSTFIELDS, $data );
  102. }
  103. $incoming_client_ids = &drupal_static( 'fulcrm_apiv2_prevent_loop' );
  104. if ( !is_array( $incoming_client_ids ) )
  105. $incoming_client_ids = array();
  106. $incoming_client_ids[] = fulcrm_apiv2_client_id();
  107. $request_headers[] = 'X-fulcrm-Client-ID: ' . implode( ', ', $incoming_client_ids );
  108. curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  109. curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  110. curl_setopt( $curl, CURLOPT_HEADER, true );
  111. curl_setopt( $curl, CURLOPT_HTTPHEADER, $request_headers );
  112. curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
  113. curl_setopt( $curl, CURLOPT_TCP_NODELAY, true );
  114. try {
  115. curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 5 );
  116. curl_setopt( $curl, CURLOPT_TIMEOUT, 30 );
  117. } catch ( Exception $e ) {
  118. // do nothing
  119. }
  120. $result = curl_exec( $curl );
  121. $response = curl_getinfo( $curl );
  122. $header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE );
  123. $header = substr( $result, 0, $header_size );
  124. $body = substr( $result, $header_size );
  125. $headers = array();
  126. foreach ( preg_split( '/[\r\n]+/', $header ) as $headerline ) {
  127. if ( preg_match( '/([-A-Za-z0-9_]+):\s*(.*)/', $headerline, $matches ) )
  128. $headers[ $matches[ 1 ] ] = $matches[ 2 ];
  129. }
  130. $r = array( 'headers' => $headers,
  131. 'body' => $body,
  132. 'code' => $response[ 'http_code' ],
  133. );
  134. if ( $response[ 'http_code' ] == 200 ) {
  135. $r[ 'success' ] = true; // GET
  136. } else if ( $response[ 'http_code' ] == 201 ) {
  137. $r[ 'success' ] = true; // POST
  138. } else if ( $response[ 'http_code' ] == 202 ) {
  139. $r[ 'success' ] = true; // PATCH
  140. } else if ( $response[ 'http_code' ] == 204 ) {
  141. $r[ 'success' ] = true; // DELETE
  142. } else {
  143. $r[ 'success' ] = false;
  144. }
  145. $r[ 'data' ] = json_decode( $body, $assoc = true );
  146. $r[ 'data_error' ] = json_last_error();
  147. return $r;
  148. }
  149. function fulcrm_apiv2_GET( $object, $query = null ) {
  150. return fulcrm_apiv2_http( 'GET', $object, $data = null, $query = $query );
  151. }
  152. function fulcrm_apiv2_POST( $object, $data, $query = null ) {
  153. return fulcrm_apiv2_http( 'POST', $object, $data = $data, $query = $query );
  154. }
  155. function fulcrm_apiv2_PUT( $object, $data, $query = null ) {
  156. return fulcrm_apiv2_http( 'PUT', $object, $data = $data, $query = $query );
  157. }
  158. function fulcrm_apiv2_PATCH( $object, $data, $query = null ) {
  159. return fulcrm_apiv2_http( 'PATCH', $object, $data = $data, $query = $query );
  160. }
  161. function fulcrm_apiv2_DELETE( $object, $query = null ) {
  162. return fulcrm_apiv2_http( 'DELETE', $object, $data = null, $query = $query );
  163. }