cgchamila

Untitled

Oct 3rd, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.76 KB | None | 0 0
  1. <?php
  2. class consumer {
  3.     public $key;
  4.     public $secret;
  5.    
  6.     public function __construct($key, $secret) {
  7.         $this->key = $key;
  8.         $this->secret = $secret;
  9.     }
  10. }
  11.  
  12. class makerequest {
  13.     public static $version = '1.0';
  14.     private $parameters;
  15.     private $http_method;
  16.     private $http_url;
  17.     public $base_string;
  18.    
  19.     public function __construct($http_method, $http_url, $parameters=NULL)
  20.     {
  21.         $this->parameters = $parameters;
  22.         $this->http_method = $http_method;
  23.         $this->http_url = $http_url;
  24.     }
  25.    
  26.     public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL)
  27.     {
  28.         @$parameters or $parameters = array();
  29.         $defaults = array("oauth_version" => self::$version,
  30.                           "oauth_nonce" => self::generate_nonce(),
  31.                           "oauth_timestamp" => self::generate_timestamp(),
  32.                           "oauth_consumer_key" => $consumer->key);
  33.         /*if ($token)
  34.           $defaults['oauth_token'] = $token->key;*/
  35.    
  36.         $parameters = array_merge($defaults, $parameters);
  37.         return new makerequest($http_method, $http_url, $parameters);
  38.     }
  39.    
  40.     public function sign_request($signature_method, $consumer, $token)
  41.     {
  42.         $this->parameters['oauth_signature_method'] = $signature_method;
  43.         $signature = $this->build_signature($consumer, $token);
  44.         $this->parameters['oauth_signature'] = $signature;
  45.     }
  46.    
  47.     public function build_signature($consumer, $token)
  48.     {
  49.         $base_string = $this->get_signature_base_string();
  50.         $this->base_string = $base_string;
  51.    
  52.         $key_parts = array(
  53.           $consumer->secret,
  54.           ($token) ? $token->secret : ""
  55.         );
  56.    
  57.         $key_parts = util::safe_encode($key_parts);
  58.         $key = implode('&', $key_parts);
  59.         return base64_encode(hash_hmac('sha1', $base_string, $key, true));
  60.     }
  61.    
  62.     public function get_signature_base_string()
  63.     {
  64.         $parts = array(
  65.           $this->get_normalized_http_method(),
  66.           $this->get_normalized_http_url(),
  67.           $this->get_signable_parameters()
  68.         );
  69.         $parts = util::safe_encode($parts);
  70.         return implode('&', $parts);
  71.     }
  72.    
  73.     public function get_normalized_http_method()
  74.     {
  75.         return strtoupper($this->http_method);
  76.     }
  77.    
  78.     public function get_normalized_http_url()
  79.     {
  80.         $parts = parse_url($this->http_url);
  81.    
  82.         $port = @$parts['port'];
  83.         $scheme = $parts['scheme'];
  84.         $host = $parts['host'];
  85.         $path = @$parts['path'];
  86.    
  87.         $port or $port = ($scheme == 'https') ? '443' : '80';
  88.    
  89.         if (($scheme == 'https' && $port != '443')
  90.             || ($scheme == 'http' && $port != '80')) {
  91.           $host = "$host:$port";
  92.         }
  93.         return "$scheme://$host$path";
  94.     }
  95.    
  96.     public function get_signable_parameters() {
  97.         // Grab all parameters
  98.         $params = $this->parameters;
  99.        
  100.         // Remove oauth_signature if present
  101.         // Ref: Spec: 9.1.1 ("The oauth_signature parameter MUST be excluded.")
  102.         if (isset($params['oauth_signature'])) {
  103.           unset($params['oauth_signature']);
  104.         }
  105.    
  106.         return util::build_http_query($params);
  107.     }
  108.    
  109.     public function to_url() {
  110.         $post_data = util::build_http_query($this->parameters);
  111.         $out = $this->http_url;
  112.         if ($post_data) {
  113.           $out .= '?'.$post_data;
  114.         }
  115.         return $out;
  116.     }
  117.    
  118.     private static function generate_nonce()
  119.     {
  120.         $mt = microtime();
  121.         $rand = mt_rand();
  122.         return md5($mt . $rand);
  123.     }
  124.    
  125.     private static function generate_timestamp()
  126.     {
  127.         return time();
  128.     }
  129. }
  130.  
  131. class util {
  132.    
  133.     public static function parse_parameters($input)
  134.     {
  135.         if (!isset($input) || !$input) return array();
  136.    
  137.         $pairs = explode('&', $input);
  138.    
  139.         $parsed_parameters = array();
  140.         foreach ($pairs as $pair) {
  141.             $split = explode('=', $pair, 2);
  142.             $parameter = self::safe_encode($split[0]);
  143.             $value = isset($split[1]) ? self::safe_encode($split[1]) : '';
  144.    
  145.             if (isset($parsed_parameters[$parameter])) {
  146.                 // We have already recieved parameter(s) with this name, so add to the list
  147.                 // of parameters with this name
  148.    
  149.             if (is_scalar($parsed_parameters[$parameter])) {
  150.                 // This is the first duplicate, so transform scalar (string) into an array
  151.                 // so we can add the duplicates
  152.                 $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
  153.             }
  154.    
  155.             $parsed_parameters[$parameter][] = $value;
  156.             }
  157.             else {
  158.                 $parsed_parameters[$parameter] = $value;
  159.             }
  160.         }
  161.         return $parsed_parameters;
  162.     }
  163.    
  164.     public static function safe_encode($data)
  165.     {
  166.         if (is_array($data)) {
  167.           return array_map(array('util', 'safe_encode'), $data);
  168.         } else if (is_scalar($data)) {
  169.           return str_ireplace(
  170.             array('+', '%7E'),
  171.             array(' ', '~'),
  172.             rawurlencode($data)
  173.           );
  174.         } else {
  175.           return '';
  176.         }
  177.      }
  178.      
  179.     public static function build_http_query($params) {
  180.         if (!$params) return '';
  181.         // Urlencode both keys and values
  182.         $keys = array_keys($params);
  183.         $values = array_values($params);
  184.         $params = array_combine($keys, $values);
  185.    
  186.         // Parameters are sorted by name, using lexicographical byte value ordering.
  187.         // Ref: Spec: 9.1.1 (1)
  188.         uksort($params, 'strcmp');
  189.    
  190.         $pairs = array();
  191.         foreach ($params as $parameter => $value) {
  192.           if (is_array($value)) {
  193.             // If two or more parameters share the same name, they are sorted by their value
  194.             // Ref: Spec: 9.1.1 (1)
  195.             natsort($value);
  196.             foreach ($value as $duplicate_value) {
  197.               $pairs[] = $parameter . '=' . $duplicate_value;
  198.             }
  199.           } else {
  200.             $pairs[] = $parameter . '=' . $value;
  201.           }
  202.         }
  203.        
  204.         // For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
  205.         // Each name-value pair is separated by an '&' character (ASCII code 38)
  206.         return implode('&', $pairs);
  207.     }
  208. }
  209. ?>
  210.  
  211.  
  212.  
  213. <?php
  214. require_once('oauth.php');
  215. class api {
  216.     const REQUEST_TOKEN_URL = 'http://www.abc.loc/oauth/requesttoken';
  217.    
  218.     public function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
  219.     {
  220.         $this->sha1_method = 'HMAC-SHA1';
  221.         $this->consumer = new consumer($consumer_key, $consumer_secret);
  222.         $this->token = null;
  223.     }
  224.    
  225.     public function getRequestToken($oauth_callback = NULL)
  226.     {
  227.         $parameters = array();
  228.         if (!empty($oauth_callback)) {
  229.           //$parameters['oauth_callback'] = $oauth_callback;
  230.         }
  231.        
  232.         $request = $this->oAuthRequest(self::REQUEST_TOKEN_URL, 'GET', $parameters);
  233.         $token = util::parse_parameters($request);
  234.         $this->token = new consumer($token['oauth_token'], $token['oauth_token_secret']);
  235.         return $token;
  236.     }
  237.    
  238.     public function oAuthRequest($url, $method, $parameters) {
  239.        
  240.         $request = makerequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  241.         $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  242.         switch ($method) {
  243.         case 'GET':
  244.           return $this->http($request->to_url(), 'GET');
  245.         default:
  246.           return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
  247.         }
  248.     }
  249.    
  250.     private function http($url, $method, $postfields = NULL)
  251.     {
  252.         $this->http_info = array();
  253.         $ci = curl_init();
  254.         curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  255.         curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  256.         curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  257.         curl_setopt($ci, CURLOPT_HEADER, FALSE);
  258.    
  259.         switch ($method) {
  260.           case 'POST':
  261.             curl_setopt($ci, CURLOPT_POST, TRUE);
  262.             if (!empty($postfields)) {
  263.               curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  264.             }
  265.             break;
  266.           case 'DELETE':
  267.             curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  268.             if (!empty($postfields)) {
  269.               $url = "{$url}?{$postfields}";
  270.             }
  271.         }
  272.        
  273.         curl_setopt($ci, CURLOPT_URL, $url);
  274.         $response = curl_exec($ci);
  275.         $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  276.         $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  277.         $this->url = $url;
  278.         curl_close ($ci);
  279.         return $response;
  280.     }
  281.    
  282.     public function getHeader($ch, $header) {
  283.         $i = strpos($header, ':');
  284.         if (!empty($i)) {
  285.           $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  286.           $value = trim(substr($header, $i + 2));
  287.           $this->http_header[$key] = $value;
  288.         }
  289.         return strlen($header);
  290.     }
  291. }
  292.  
  293.     $api = new api('1d7259a770e0732d191bb566b5cf9e', '7e662975b2');
  294.     $temp_credential = $api->getRequestToken('http://www.abc.loc');
  295. ?>
  296.  
  297.  
Advertisement
Add Comment
Please, Sign In to add comment