b2evolution PHP Cross Reference Blogging Systems

Source: /plugins/twitter_plugin/twitteroauth/twitteroauth.php - 249 lines - 7692 bytes - Summary - Text - Print

   1  <?php
   2  /*
   3  * Abraham Williams (abraham@abrah.am) http://abrah.am
   4  *
   5  * The first PHP Library to support OAuth for Twitter's REST API.
   6  */
   7  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
   8  
   9  /* Load OAuth lib. You can find it at http://oauth.net */
  10  require_once ('oauth.php');
  11  
  12  /**
  13  * Twitter OAuth class
  14  */
  15  class TwitterOAuth {
  16    /* Contains the last HTTP status code returned. */
  17    public $http_code;
  18    /* Contains the last API call. */
  19    public $url;
  20    /* Set up the API root URL. */
  21    public $host = "https://api.twitter.com/1.1/";
  22    /* Set timeout default. */
  23    public $timeout = 30;
  24    /* Set connect timeout. */
  25    public $connecttimeout = 30;
  26    /* Verify SSL Cert. */
  27    public $ssl_verifypeer = FALSE;
  28    /* Respons format. */
  29    public $format = 'json';
  30    /* Decode returned json data. */
  31    public $decode_json = TRUE;
  32    /* Contains the last HTTP headers returned. */
  33    public $http_info;
  34    /* Set the useragnet. */
  35    public $useragent = 'TwitterOAuth v0.2.0-beta2';
  36    /* Immediately retry the API call if the response was not successful. */
  37    //public $retry = TRUE;
  38  
  39  
  40  
  41  
  42    /**
  43  * Set API URLS
  44  */
  45    function accessTokenURL() { return 'https://api.twitter.com/oauth/access_token'; }
  46    function authenticateURL() { return 'https://twitter.com/oauth/authenticate'; }
  47    function authorizeURL() { return 'https://twitter.com/oauth/authorize'; }
  48    function requestTokenURL() { return 'https://api.twitter.com/oauth/request_token'; }
  49  
  50    /**
  51  * Debug helpers
  52  */
  53    function lastStatusCode() { return $this->http_status; }
  54    function lastAPICall() { return $this->last_api_call; }
  55  
  56    /**
  57  * construct TwitterOAuth object
  58  */
  59    function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
  60      $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
  61      $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);
  62      if (!empty($oauth_token) && !empty($oauth_token_secret)) {
  63        $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
  64      } else {
  65        $this->token = NULL;
  66      }
  67    }
  68  
  69  
  70    /**
  71  * Get a request_token from Twitter
  72  *
  73  * @returns a key/value array containing oauth_token and oauth_token_secret
  74  */
  75    function getRequestToken($oauth_callback = NULL) {
  76      $parameters = array();
  77      if (!empty($oauth_callback)) {
  78        $parameters['oauth_callback'] = $oauth_callback;
  79      }
  80      $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);
  81      $token = OAuthUtil::parse_parameters($request);
  82      if( !isset( $token['oauth_token'] ) || !isset( $token['oauth_token_secret'] ) )
  83      {
  84          return NULL;
  85      }
  86      $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
  87      return $token;
  88    }
  89  
  90    /**
  91  * Get the authorize URL
  92  *
  93  * @returns a string
  94  */
  95    function getAuthorizeURL($token, $sign_in_with_twitter = TRUE) {
  96      if (is_array($token)) {
  97        $token = $token['oauth_token'];
  98      }
  99      if (empty($sign_in_with_twitter)) {
 100        return $this->authorizeURL() . "?oauth_token={$token}";
 101      } else {
 102         return $this->authenticateURL() . "?oauth_token={$token}";
 103      }
 104    }
 105  
 106    /**
 107  * Exchange request token and secret for an access token and
 108  * secret, to sign API calls.
 109  *
 110  * @returns array("oauth_token" => "the-access-token",
 111  * "oauth_token_secret" => "the-access-secret",
 112  * "user_id" => "9436992",
 113  * "screen_name" => "abraham")
 114  */
 115    function getAccessToken($oauth_verifier = FALSE) {
 116      $parameters = array();
 117      if (!empty($oauth_verifier)) {
 118        $parameters['oauth_verifier'] = $oauth_verifier;
 119      }
 120      $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
 121      $token = OAuthUtil::parse_parameters($request);
 122      $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
 123      return $token;
 124    }
 125  
 126    /**
 127  * One time exchange of username and password for access token and secret.
 128  *
 129  * @returns array("oauth_token" => "the-access-token",
 130  * "oauth_token_secret" => "the-access-secret",
 131  * "user_id" => "9436992",
 132  * "screen_name" => "abraham",
 133  * "x_auth_expires" => "0")
 134  */
 135    function getXAuthToken($username, $password) {
 136      $parameters = array();
 137      $parameters['x_auth_username'] = $username;
 138      $parameters['x_auth_password'] = $password;
 139      $parameters['x_auth_mode'] = 'client_auth';
 140      $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
 141      $token = OAuthUtil::parse_parameters($request);
 142      $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
 143      return $token;
 144    }
 145  
 146    /**
 147  * GET wrapper for oAuthRequest.
 148  */
 149    function get($url, $parameters = array()) {
 150      $response = $this->oAuthRequest($url, 'GET', $parameters);
 151      if ($this->format === 'json' && $this->decode_json) {
 152        return json_decode($response);
 153      }
 154      return $response;
 155    }
 156  
 157    /**
 158  * POST wrapper for oAuthRequest.
 159  */
 160    function post($url, $parameters = array()) {
 161      $response = $this->oAuthRequest($url, 'POST', $parameters);
 162      if ($this->format === 'json' && $this->decode_json) {
 163        return json_decode($response);
 164      }
 165      return $response;
 166    }
 167  
 168    /**
 169  * DELETE wrapper for oAuthReqeust.
 170  */
 171    function delete($url, $parameters = array()) {
 172      $response = $this->oAuthRequest($url, 'DELETE', $parameters);
 173      if ($this->format === 'json' && $this->decode_json) {
 174        return json_decode($response);
 175      }
 176      return $response;
 177    }
 178  
 179    /**
 180  * Format and sign an OAuth / API request
 181  */
 182    function oAuthRequest($url, $method, $parameters) {
 183      if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {
 184        $url = "{$this->host}{$url}.{$this->format}";
 185      }
 186      $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
 187      $request->sign_request($this->sha1_method, $this->consumer, $this->token);
 188      switch ($method) {
 189      case 'GET':
 190        return $this->http($request->to_url(), 'GET');
 191      default:
 192        return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
 193      }
 194    }
 195  
 196    /**
 197  * Make an HTTP request
 198  *
 199  * @return API results
 200  */
 201    function http($url, $method, $postfields = NULL) {
 202      $this->http_info = array();
 203      $ci = curl_init();
 204      /* Curl settings */
 205      curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
 206      curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
 207      curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
 208      curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
 209      curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
 210      curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
 211      curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
 212      curl_setopt($ci, CURLOPT_HEADER, FALSE);
 213  
 214      switch ($method) {
 215        case 'POST':
 216          curl_setopt($ci, CURLOPT_POST, TRUE);
 217          if (!empty($postfields)) {
 218            curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
 219          }
 220          break;
 221        case 'DELETE':
 222          curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
 223          if (!empty($postfields)) {
 224            $url = "{$url}?{$postfields}";
 225          }
 226      }
 227  
 228      curl_setopt($ci, CURLOPT_URL, $url);
 229      $response = curl_exec($ci);
 230      $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
 231      $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
 232      $this->url = $url;
 233      curl_close ($ci);
 234      return $response;
 235    }
 236  
 237    /**
 238  * Get the header info to store.
 239  */
 240    function getHeader($ch, $header) {
 241      $i = strpos($header, ':');
 242      if (!empty($i)) {
 243        $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
 244        $value = trim(substr($header, $i + 2));
 245        $this->http_header[$key] = $value;
 246      }
 247      return strlen($header);
 248    }
 249  }

title

Description

title

Description

title

Description

title

title

Body