Advertisement
battlemidget

Untitled

Feb 29th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.08 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @file
  4.  * Classes to implement the full Launchpad API
  5.  */
  6.  
  7. /**
  8.  * Class LaunchpadConf
  9.  *
  10.  * Singleton which stores common configuration
  11.  * @see http://php.net/manual/en/language.oop5.patterns.php
  12.  */
  13. class LaunchpadConf {
  14.   private static $instance;
  15.   private $attributes = array(
  16.     'host'       => 'staging.launchpad.net',
  17.     'api'        => 'api.staging.launchpad.net',
  18.     'api_version'=> '1.0',
  19.   );
  20.  
  21.   private function __construct() {}
  22.  
  23.   public static function instance() {
  24.     if (!isset(self::$instance)) {
  25.       $className = __CLASS__;
  26.       self::$instance = new $className;
  27.     }
  28.     return self::$instance;
  29.   }
  30.  
  31.   /**
  32.    * Generic getter
  33.    *
  34.    * @param $attribute
  35.    *   string attribute name to return
  36.    * @return
  37.    *   mixed value or NULL
  38.    */
  39.   public function get($attribute) {
  40.     if (array_key_exists($attribute, $this->attributes)) {
  41.       return $this->attributes[$attribute];
  42.     }
  43.   }
  44.  
  45.   /**
  46.    * Generic setter
  47.    * @param $attribute
  48.    *   string attribute name to be set
  49.    * @param $value
  50.    *   mixed value
  51.    */
  52.   public function set($attribute, $value) {
  53.     if (array_key_exists($attribute, $this->attributes)) {
  54.       $this->attributes[$attribute] = $value;
  55.     }
  56.   }
  57. }
  58.  
  59. /**
  60.  * Exception handling class.
  61.  */
  62. class LaunchpadException extends Exception {}
  63.  
  64. /**
  65.  * Primary Launchpad API implementation class
  66.  */
  67. class Launchpad {
  68.  
  69.   /**
  70.    * @var $format API format to use: can be json or xml
  71.    */
  72.   protected $format = 'json';
  73.  
  74.   /**
  75.    * @var $source the launchpad api 'source'
  76.    */
  77.   protected $source = 'drupal';
  78.  
  79.   /**
  80.    * Constructor for the Launchpad class
  81.    */
  82.   public function __construct() {}
  83.  
  84.   /**
  85.    * Method for calling any launchpad api resource
  86.    */
  87.   public function call($path, $params = array(), $method = 'GET', $use_auth = FALSE) {
  88.     $url = $this->create_url($path);
  89.  
  90.     $response = $this->request($url, $params, $method);
  91.  
  92.     if (!$response) {
  93.       return FALSE;
  94.     }
  95.  
  96.     return $this->parse_response($response);
  97.   }
  98.  
  99.   /**
  100.    *
  101.    * @see
  102.    */
  103.   public function person($name) {
  104.     $params = array();
  105.     $params['display_name'] = $name;
  106.  
  107.     $values = $this->call('~', $params, 'GET');
  108.     return new LaunchpadUser($values);
  109.   }
  110.  
  111.   /**
  112.    * get me
  113.    * @see
  114.    */
  115.   public function me() {
  116.  
  117.     $values = $this->call('people/+me');
  118.     debug($values);
  119.     return new LaunchpadUser($values);
  120.   }  
  121.  
  122.   /**
  123.    * Perform a request
  124.    */
  125.   protected function request($url, $params = array(), $method = 'POST', $use_auth = FALSE) {
  126.     $data = '';
  127.     if (count($params) > 0) {
  128.       $data = http_build_query($params, '', '&');
  129.     }
  130.  
  131.     $headers = array();
  132.  
  133.     if ($use_auth) {
  134.       $headers['Content-type'] = 'application/x-www-form-urlencoded';
  135.     }
  136.  
  137.     $response = drupal_http_request($url, array('headers' => $headers, 'method' => $method, 'data' => $data));
  138.     debug($response);
  139.     if (!isset($response->error)) {
  140.       return $response->data;
  141.     } else {
  142.       $error = $response->error;
  143.       $data = $this->parse_response($response->data);
  144.       if (isset($data['error'])) {
  145.         $error = $data['error'];
  146.       }
  147.       throw new LaunchpadException($error);
  148.     }
  149.   }
  150.  
  151.   protected function parse_response($response, $format = NULL) {
  152.     if (empty($format)) {
  153.       $format = $this->format;
  154.     }
  155.  
  156.     switch ($format) {
  157.       case 'json':
  158.         // http://drupal.org/node/985544 - json_decode large integer issue
  159.         $length = strlen(PHP_INT_MAX);
  160.         $response = preg_replace('/"(id|in_reply_to_status_id)":(\d{' . $length . ',})/', '"\1":"\2"', $response);
  161.         return json_decode($response, TRUE);
  162.     }
  163.   }
  164.  
  165.   protected function create_url($path) {
  166.     $conf = LaunchpadConf::instance();
  167.     $url =  'https://'. $conf->get('api') .'/'. $conf->get('api_version') .'/'. $path;
  168.     return $url;
  169.   }
  170.  
  171.   // Launchpad doesn't use same api url for OAuth  
  172.   protected function create_lp_auth_url($path) {
  173.     $conf = LaunchpadConf::instance();
  174.     $url =  'https://'. $conf->get('host') .'/'. $path;
  175.     return $url;
  176.   }
  177. }
  178.  
  179. /**
  180.  * A class to provide OAuth enabled access to the launchpad API
  181.  */
  182. class LaunchpadOAuth extends Launchpad {
  183.  
  184.   protected $signature_method;
  185.  
  186.   protected $consumer;
  187.  
  188.   protected $token;
  189.  
  190.   public function __construct($consumer_key, $oauth_token = NULL, $oauth_token_secret = NULL) {
  191.     $this->signature_method = new OAuthSignatureMethod_PLAINTEXT();
  192.     $this->consumer = new OAuthConsumer($consumer_key, '');
  193.     if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  194.       $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  195.     }
  196.   }
  197.  
  198.   public function get_request_token() {
  199.     $url = $this->create_lp_auth_url('+request-token', '');
  200.     try {
  201.       $response = $this->auth_request($url);
  202.     } catch (LaunchpadException $e) {}
  203.     parse_str($response, $token);
  204.     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  205.     return $token;
  206.   }
  207.  
  208.   public function get_authorize_url($token) {
  209.     $url = $this->create_lp_auth_url('+authorize-token', '');
  210.     $url.= '?oauth_token=' . $token['oauth_token'] . '&oauth_callback=' . urlencode(url('launchpad/oauth', array('absolute' => TRUE)) . '?oauth_token=' . $token['oauth_token']);
  211.  
  212.     return $url;
  213.   }
  214.  
  215.   /**
  216.    * Launchpad doesn't use this routine.
  217.    *
  218.    *public function get_authenticate_url($token) {
  219.    *  $url = $this->create_url('oauth/authenticate', '');
  220.    *  $url.= '?oauth_token=' . $token['oauth_token'];
  221.    *
  222.    *  return $url;
  223.    *}
  224.    */
  225.  
  226.   public function get_access_token() {
  227.     $url = $this->create_lp_auth_url('+access-token', '');
  228.     try {
  229.       $response = $this->auth_request($url);
  230.     } catch (LaunchpadException $e) {}
  231.     parse_str($response, $token);
  232.     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  233.     return $token;
  234.   }
  235.  
  236.   public function auth_request($url, $params = array(), $method = 'POST') {
  237.     $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $params);
  238.     $request->sign_request($this->signature_method, $this->consumer, $this->token);
  239.     switch ($method) {
  240.       case 'GET':
  241.         return $this->request($request->to_url());
  242.       case 'POST':
  243.         return $this->request($request->get_normalized_http_url(), $request->get_parameters(), 'POST');
  244.     }
  245.   }
  246.  
  247. }
  248.  
  249. class LaunchpadUser {
  250.  
  251.   public $id;
  252.  
  253.   public $display_name;
  254.  
  255.   protected $oauth_token;
  256.  
  257.   protected $oauth_token_secret;
  258.  
  259.   public function __construct($values = array()) {
  260.     $this->id = $values['id'];
  261.     $this->display_name = $values['display_name'];
  262.   }
  263.  
  264.   public function get_auth() {
  265.     return array('oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret);
  266.   }
  267.  
  268.   public function set_auth($values) {
  269.     $this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL;
  270.     $this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL;
  271.   }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement