Advertisement
Guest User

Untitled

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