You are here

cmis_common.module in CMIS API 7

File

cmis_common/cmis_common.module
View source
<?php

/**
 * Implementation of hook_cmis_info()
 * 
 */
function cmis_common_cmis_info() {
  return array(
    'cmis_common' => array(
      'name' => t('CMIS Common'),
      'module' => 'cmis_common',
      'description' => t('CMIS common client'),
    ),
  );
}

/**
 * Implementation of hook_cmisapi_invoke().
 * 
 */
function cmis_common_cmisapi_invoke() {
  $args = func_get_args();
  $cmis_method = $args[0];
  $repository_id = count($args) > 1 ? $args[1] : 'default';
  unset($args[0]);
  unset($args[1]);
  $cmis_repository = cmis_get_repository($repository_id);

  // pass repository info from cache
  if ($cmis_method == 'getRepositoryInfo' && isset($cmis_repository->info)) {
    return $cmis_repository->info;
  }

  // cache service instance
  if (!isset($cmis_repository->service)) {
    module_load_include('utils.inc', 'cmis_common');

    // CommonCMISService allows other modules to control the way CMISService class
    // calls the CMIS repositories via hook_cmis_invoke()
    $cmis_repository->service = new CommonCMISService($cmis_repository->settings['url'], isset($cmis_repository->settings['user']), isset($cmis_repository->settings['password']));
  }
  return call_user_func_array(array(
    $cmis_repository->service,
    $cmis_method,
  ), $args);
}

/**
 * Implementation of hook_cmis_invoke($url, $properties, $settings)
 *
 */
function cmis_common_cmis_invoke($url, $properties, $settings, $dry_run = FALSE) {
  $session = curl_init($url);
  curl_setopt($session, CURLOPT_HEADER, false);
  curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
  if ($settings['user']) {
    curl_setopt($session, CURLOPT_USERPWD, $settings['user'] . ":" . $settings['password']);
  }
  curl_setopt($session, CURLOPT_CUSTOMREQUEST, $properties['method']);
  if ($properties['headers']) {
    $headers = array();
    foreach ($properties['headers'] as $header_name => $header_value) {
      $headers[] = $header_name . ': ' . $header_value;
    }
    curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
  }
  if ($properties['method'] == "POST" && is_resource($properties['data']) && get_resource_type($properties['data']) == 'stream') {
    curl_setopt($session, CURLOPT_PUT, true);
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($session, CURLOPT_INFILE, $properties['data']);
  }
  else {
    if ($properties['data']) {
      curl_setopt($session, CURLOPT_POSTFIELDS, $properties['data']);
    }
    if ($properties['method'] == "POST") {
      curl_setopt($session, CURLOPT_POST, true);
    }
  }
  $retval = new stdClass();
  $retval->url = $url;
  $retval->method = $properties['method'];
  $retval->content_sent = $properties['data'];
  $retval->content_type_sent = $properties['headers']['Content-Type'];
  $returnwapper = cmisStreamAsString::temporaryStream();
  curl_setopt($session, CURLOPT_FILE, $returnwapper
    ->getStream());
  if (!$dry_run) {
    curl_exec($session);
    $retval->body = $returnwapper;
    $retval->code = curl_getinfo($session, CURLINFO_HTTP_CODE);
    $retval->content_type = curl_getinfo($session, CURLINFO_CONTENT_TYPE);
    $retval->content_length = curl_getinfo($session, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($session);
    if (!in_array($retval->code, array(
      200,
      201,
      204,
    ))) {
      throw new CMISException(t("HTTP call to [@url] returned [@code]. Response: @response", array(
        '@code' => $retval->code,
        '@response' => strip_tags($retval->body),
        '@url' => $url,
      )));
    }
  }
  else {
    $retval->body = NULL;
    $retval->curl_session = $session;
    $retval->code = 0;
    $retval->content_type = NULL;
    $retval->content_length = NULL;
  }
  return $retval;
}

/**
 * A class to hold a reference to a stream, but that can pretend to be a string.
 *
 * We use this class so that we can return a reference to a stream for consumers
 * to use, but if they choose to then they can just treat this object as a
 * string and they get the full contents of the stream. However, this will cause
 * the entire stream to be in PHP's memory, which may cause out of memory
 * issues. Where large streams may be returned, you should check to see if the
 * return value is this class, and then use the getStream method to handle it as
 * a stream, avoiding memory issues.
 */
class cmisStreamAsString {
  protected $stream;

  /**
   * Get the stream represented by this object.
   *
   * @return
   *   A handle the stream passed in in constructor.
   */
  public function getStream() {
    return $this->stream;
  }

  /**
   * Create a new wrapper for the given stream.
   */
  function __construct($stream) {
    $this->stream = $stream;
  }

  /**
   * Create a new temporary stream and wrap it in the cmisStreamAsString class.
   */
  static function temporaryStream() {
    $stream = fopen('php://temp', 'r+');
    return new cmisStreamAsString($stream);
  }

  /**
   * Return the entire stream contents as a string.
   *
   * Note that this function will rewind the stream.
   */
  function __toString() {

    // Rewind our stream.
    rewind($this->stream);
    return stream_get_contents($this->stream);
  }

}

Functions

Namesort descending Description
cmis_common_cmisapi_invoke Implementation of hook_cmisapi_invoke().
cmis_common_cmis_info Implementation of hook_cmis_info()
cmis_common_cmis_invoke Implementation of hook_cmis_invoke($url, $properties, $settings)

Classes

Namesort descending Description
cmisStreamAsString A class to hold a reference to a stream, but that can pretend to be a string.