Advertisement
cgchamila

Untitled

Oct 3rd, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.95 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.        
  98.         $params = $this->parameters;
  99.  
  100.         if (isset($params['oauth_signature'])) {
  101.           unset($params['oauth_signature']);
  102.         }
  103.    
  104.         return util::build_http_query($params);
  105.     }
  106.    
  107.     public function to_url() {
  108.         $post_data = util::build_http_query($this->parameters);
  109.         $out = $this->http_url;
  110.         if ($post_data) {
  111.           $out .= '?'.$post_data;
  112.         }
  113.         return $out;
  114.     }
  115.    
  116.     private static function generate_nonce()
  117.     {
  118.         $mt = microtime();
  119.         $rand = mt_rand();
  120.         return md5($mt . $rand);
  121.     }
  122.    
  123.     private static function generate_timestamp()
  124.     {
  125.         return time();
  126.     }
  127. }
  128.  
  129. class util {
  130.    
  131.     public static function parse_parameters($input)
  132.     {
  133.         if (!isset($input) || !$input) return array();
  134.    
  135.         $pairs = explode('&', $input);
  136.    
  137.         $parsed_parameters = array();
  138.         foreach ($pairs as $pair) {
  139.             $split = explode('=', $pair, 2);
  140.             $parameter = self::safe_encode($split[0]);
  141.             $value = isset($split[1]) ? self::safe_encode($split[1]) : '';
  142.    
  143.             if (isset($parsed_parameters[$parameter])) {
  144.                
  145.    
  146.             if (is_scalar($parsed_parameters[$parameter])) {
  147.                
  148.                 $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
  149.             }
  150.    
  151.             $parsed_parameters[$parameter][] = $value;
  152.             }
  153.             else {
  154.                 $parsed_parameters[$parameter] = $value;
  155.             }
  156.         }
  157.         return $parsed_parameters;
  158.     }
  159.    
  160.     public static function safe_encode($data)
  161.     {
  162.         if (is_array($data)) {
  163.           return array_map(array('util', 'safe_encode'), $data);
  164.         } else if (is_scalar($data)) {
  165.           return str_ireplace(
  166.             array('+', '%7E'),
  167.             array(' ', '~'),
  168.             rawurlencode($data)
  169.           );
  170.         } else {
  171.           return '';
  172.         }
  173.      }
  174.      
  175.     public static function build_http_query($params) {
  176.         if (!$params) return '';
  177.        
  178.         $keys = array_keys($params);
  179.         $values = array_values($params);
  180.         $params = array_combine($keys, $values);
  181.    
  182.        
  183.         uksort($params, 'strcmp');
  184.    
  185.         $pairs = array();
  186.         foreach ($params as $parameter => $value) {
  187.           if (is_array($value)) {
  188.            
  189.             natsort($value);
  190.             foreach ($value as $duplicate_value) {
  191.               $pairs[] = $parameter . '=' . $duplicate_value;
  192.             }
  193.           } else {
  194.             $pairs[] = $parameter . '=' . $value;
  195.           }
  196.         }
  197.        
  198.        
  199.         return implode('&', $pairs);
  200.     }
  201. }
  202. ?>
  203.  
  204.  
  205.  
  206. <?php
  207. require_once('oauth.php');
  208. class api {
  209.     const REQUEST_TOKEN_URL = 'http://www.abc.loc/oauth/requesttoken';
  210.    
  211.     public function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
  212.     {
  213.         $this->sha1_method = 'HMAC-SHA1';
  214.         $this->consumer = new consumer($consumer_key, $consumer_secret);
  215.         $this->token = null;
  216.     }
  217.    
  218.     public function getRequestToken($oauth_callback = NULL)
  219.     {
  220.         $parameters = array();
  221.         if (!empty($oauth_callback)) {
  222.           //$parameters['oauth_callback'] = $oauth_callback;
  223.         }
  224.        
  225.         $request = $this->oAuthRequest(self::REQUEST_TOKEN_URL, 'GET', $parameters);
  226.         $token = util::parse_parameters($request);
  227.         $this->token = new consumer($token['oauth_token'], $token['oauth_token_secret']);
  228.         return $token;
  229.     }
  230.    
  231.     public function oAuthRequest($url, $method, $parameters) {
  232.        
  233.         $request = makerequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
  234.         $request->sign_request($this->sha1_method, $this->consumer, $this->token);
  235.         switch ($method) {
  236.         case 'GET':
  237.           return $this->http($request->to_url(), 'GET');
  238.         default:
  239.           return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
  240.         }
  241.     }
  242.    
  243.     private function http($url, $method, $postfields = NULL)
  244.     {
  245.         $this->http_info = array();
  246.         $ci = curl_init();
  247.         curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  248.         curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
  249.         curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  250.         curl_setopt($ci, CURLOPT_HEADER, FALSE);
  251.    
  252.         switch ($method) {
  253.           case 'POST':
  254.             curl_setopt($ci, CURLOPT_POST, TRUE);
  255.             if (!empty($postfields)) {
  256.               curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  257.             }
  258.             break;
  259.           case 'DELETE':
  260.             curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  261.             if (!empty($postfields)) {
  262.               $url = "{$url}?{$postfields}";
  263.             }
  264.         }
  265.        
  266.         curl_setopt($ci, CURLOPT_URL, $url);
  267.         $response = curl_exec($ci);
  268.         $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  269.         $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
  270.         $this->url = $url;
  271.         curl_close ($ci);
  272.         return $response;
  273.     }
  274.    
  275.     public function getHeader($ch, $header) {
  276.         $i = strpos($header, ':');
  277.         if (!empty($i)) {
  278.           $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  279.           $value = trim(substr($header, $i + 2));
  280.           $this->http_header[$key] = $value;
  281.         }
  282.         return strlen($header);
  283.     }
  284. }
  285.  
  286.     $api = new api('1d7259a770e0732d191bb566b5cf9e', '7e662975b2');
  287.     $temp_credential = $api->getRequestToken('http://www.abc.loc');
  288. ?>
  289.  
  290.  
  291.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement