You are here

class clients_connection_flickr in Web Service Clients 7

Hierarchy

Expanded class hierarchy of clients_connection_flickr

File

backends/clients_flickr/clients_flickr.inc, line 11
Defines Flickr services and calls

View source
class clients_connection_flickr extends clients_connection_base {

  /**
   * Use for testing
   */
  public static function connect($connection) {
    $session = drupal_http_request($connection->endpoint . '?method=flickr.test.echo');
    if ($session === FALSE) {
      drupal_set_message('Error connecting');
    }
    return $session;
  }

  /**
   * Prepares a hashed token for the service, based on current time,
   * the required service and config values; serviceKey and serviceDomain
   *
   * @param $serviceConfig
   *   stdClass: A service configuration as returned by Clients::load()
   * @param $serviceMethod
   *   string: Name of service method to access
   *
   * @return
   *   array: a valid token
   */
  public static function getToken($connection, $serviceMethod) {

    // @todo - http://www.flickr.com/services/api/auth.howto.web.html
    $timestamp = (string) time();
    $nonce = uniqid();
    $hashParameters = array(
      $timestamp,
      $connection->configuration['domain'],
      $nonce,
      $serviceMethod,
    );
    $hash = hash_hmac("sha256", implode(';', $hashParameters), $connection->configuration['servicekey']);
    return array(
      'hash' => $hash,
      'domain' => $connection->configuration['domain'],
      'timestamp' => $timestamp,
      'nonce' => $nonce,
    );
  }

  /**
   * Connects to Drupal Services and logs in the user provided in the config.
   * Returns a session for the user.
   * @todo needs error catching in case service is down
   *
   * @return
   *   array
   */
  public static function getUser($connection) {
    $session = self::connect($connection);
    $userToken = self::getToken($connection, 'user.login');
    $user = xmlrpc($connection->baseurl, 'user.login', $userToken['hash'], $userToken['domain'], $userToken['timestamp'], $userToken['nonce'], $session['sessid'], $connection->configuration['username'], $connection->configuration['password']);
    if ($user === FALSE) {
      return xmlrpc_error();
    }
    return $user;
  }

  /**
   * Executes call and processes data
   */
  public static function call($serviceConfig, $serviceOptions) {
    if ($serviceOptions->configuration['options']['method'] == 'flickr.photos.search') {

      // gets raw result
      $cache_id = md5($serviceConfig->name . implode($serviceOptions->configuration['options']));
      $fetch_url = $serviceConfig->endpoint . '?method=' . $serviceOptions->configuration['options']['method'] . '&api_key=' . $serviceConfig->configuration['api_key'] . '&text=' . $serviceOptions->configuration['options']['text'] . '&page=' . $serviceOptions->configuration['options']['page'] . '&per_page=' . $serviceOptions->configuration['options']['per_page'];

      // etc http://www.flickr.com/services/api/flickr.photos.search.html
      $result = parent::doCall('rest', $cache_id, $fetch_url);
      return $result;
    }

    // else method not supported yet
  }

}

Members

Namesort descending Modifiers Type Description Overrides
clients_connection_base::$cid public property
clients_connection_base::$configuration public property
clients_connection_base::$endpoint public property
clients_connection_base::$name public property
clients_connection_base::connectionSettingsForm_submit static function Submit handler for saving/updating connections of this class. 1
clients_connection_base::doCall protected static function Takes variable number of params after cacheid.
clients_connection_base::getTestOperations function Provide buttons for the connection testing page. 1
clients_connection_base::__construct function Constructor method. 1
clients_connection_flickr::call public static function Executes call and processes data Overrides clients_connection_base::call
clients_connection_flickr::connect public static function Use for testing
clients_connection_flickr::getToken public static function Prepares a hashed token for the service, based on current time, the required service and config values; serviceKey and serviceDomain
clients_connection_flickr::getUser public static function Connects to Drupal Services and logs in the user provided in the config. Returns a session for the user. @todo needs error catching in case service is down