Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Classes to implement the full Launchpad API
- */
- /**
- * Class LaunchpadConf
- *
- * Singleton which stores common configuration
- * @see http://php.net/manual/en/language.oop5.patterns.php
- */
- class LaunchpadConf {
- private static $instance;
- private $attributes = array(
- 'host' => 'staging.launchpad.net',
- 'api' => 'api.staging.launchpad.net',
- 'api_version'=> '1.0',
- );
- private function __construct() {}
- public static function instance() {
- if (!isset(self::$instance)) {
- $className = __CLASS__;
- self::$instance = new $className;
- }
- return self::$instance;
- }
- /**
- * Generic getter
- *
- * @param $attribute
- * string attribute name to return
- * @return
- * mixed value or NULL
- */
- public function get($attribute) {
- if (array_key_exists($attribute, $this->attributes)) {
- return $this->attributes[$attribute];
- }
- }
- /**
- * Generic setter
- * @param $attribute
- * string attribute name to be set
- * @param $value
- * mixed value
- */
- public function set($attribute, $value) {
- if (array_key_exists($attribute, $this->attributes)) {
- $this->attributes[$attribute] = $value;
- }
- }
- }
- /**
- * Exception handling class.
- */
- class LaunchpadException extends Exception {}
- /**
- * Primary Launchpad API implementation class
- */
- class Launchpad {
- /**
- * @var $format API format to use: can be json or xml
- */
- protected $format = 'json';
- /**
- * @var $source the launchpad api 'source'
- */
- protected $source = 'drupal';
- /**
- * Constructor for the Launchpad class
- */
- public function __construct() {}
- /**
- * Method for calling any launchpad api resource
- */
- public function call($path, $params = array(), $method = 'GET', $use_auth = FALSE) {
- $url = $this->create_url($path);
- $response = $this->request($url, $params, $method);
- if (!$response) {
- return FALSE;
- }
- return $this->parse_response($response);
- }
- /**
- *
- * @see
- */
- public function person($name) {
- $params = array();
- $params['display_name'] = $name;
- $values = $this->call('~', $params, 'GET');
- return new LaunchpadUser($values);
- }
- /**
- * get me
- * @see
- */
- public function me() {
- $values = $this->call('people/+me');
- debug($values);
- return new LaunchpadUser($values);
- }
- /**
- * Perform a request
- */
- protected function request($url, $params = array(), $method = 'POST', $use_auth = FALSE) {
- $data = '';
- if (count($params) > 0) {
- $data = http_build_query($params, '', '&');
- }
- $headers = array();
- if ($use_auth) {
- $headers['Content-type'] = 'application/x-www-form-urlencoded';
- }
- $response = drupal_http_request($url, array('headers' => $headers, 'method' => $method, 'data' => $data));
- debug($response);
- if (!isset($response->error)) {
- return $response->data;
- } else {
- $error = $response->error;
- $data = $this->parse_response($response->data);
- if (isset($data['error'])) {
- $error = $data['error'];
- }
- throw new LaunchpadException($error);
- }
- }
- protected function parse_response($response, $format = NULL) {
- if (empty($format)) {
- $format = $this->format;
- }
- switch ($format) {
- case 'json':
- // http://drupal.org/node/985544 - json_decode large integer issue
- $length = strlen(PHP_INT_MAX);
- $response = preg_replace('/"(id|in_reply_to_status_id)":(\d{' . $length . ',})/', '"\1":"\2"', $response);
- return json_decode($response, TRUE);
- }
- }
- protected function create_url($path) {
- $conf = LaunchpadConf::instance();
- $url = 'https://'. $conf->get('api') .'/'. $conf->get('api_version') .'/'. $path;
- return $url;
- }
- // Launchpad doesn't use same api url for OAuth
- protected function create_lp_auth_url($path) {
- $conf = LaunchpadConf::instance();
- $url = 'https://'. $conf->get('host') .'/'. $path;
- return $url;
- }
- }
- /**
- * A class to provide OAuth enabled access to the launchpad API
- */
- class LaunchpadOAuth extends Launchpad {
- protected $signature_method;
- protected $consumer;
- protected $token;
- public function __construct($consumer_key, $oauth_token = NULL, $oauth_token_secret = NULL) {
- $this->signature_method = new OAuthSignatureMethod_PLAINTEXT();
- $this->consumer = new OAuthConsumer($consumer_key, '');
- if (!empty($oauth_token) && !empty($oauth_token_secret)) {
- $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
- }
- }
- public function get_request_token() {
- $url = $this->create_lp_auth_url('+request-token', '');
- try {
- $response = $this->auth_request($url);
- } catch (LaunchpadException $e) {}
- parse_str($response, $token);
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
- return $token;
- }
- public function get_authorize_url($token) {
- $url = $this->create_lp_auth_url('+authorize-token', '');
- $url.= '?oauth_token=' . $token['oauth_token'] . '&oauth_callback=' . urlencode(url('launchpad/oauth', array('absolute' => TRUE)) . '?oauth_token=' . $token['oauth_token']);
- return $url;
- }
- /**
- * Launchpad doesn't use this routine.
- *
- *public function get_authenticate_url($token) {
- * $url = $this->create_url('oauth/authenticate', '');
- * $url.= '?oauth_token=' . $token['oauth_token'];
- *
- * return $url;
- *}
- */
- public function get_access_token() {
- $url = $this->create_lp_auth_url('+access-token', '');
- try {
- $response = $this->auth_request($url);
- } catch (LaunchpadException $e) {}
- parse_str($response, $token);
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
- return $token;
- }
- public function auth_request($url, $params = array(), $method = 'POST') {
- $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $params);
- $request->sign_request($this->signature_method, $this->consumer, $this->token);
- switch ($method) {
- case 'GET':
- return $this->request($request->to_url());
- case 'POST':
- return $this->request($request->get_normalized_http_url(), $request->get_parameters(), 'POST');
- }
- }
- }
- class LaunchpadUser {
- public $id;
- public $display_name;
- protected $oauth_token;
- protected $oauth_token_secret;
- public function __construct($values = array()) {
- $this->id = $values['id'];
- $this->display_name = $values['display_name'];
- }
- public function get_auth() {
- return array('oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret);
- }
- public function set_auth($values) {
- $this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL;
- $this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement