Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class consumer {
- public $key;
- public $secret;
- public function __construct($key, $secret) {
- $this->key = $key;
- $this->secret = $secret;
- }
- }
- class makerequest {
- public static $version = '1.0';
- private $parameters;
- private $http_method;
- private $http_url;
- public $base_string;
- public function __construct($http_method, $http_url, $parameters=NULL)
- {
- $this->parameters = $parameters;
- $this->http_method = $http_method;
- $this->http_url = $http_url;
- }
- public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters=NULL)
- {
- @$parameters or $parameters = array();
- $defaults = array("oauth_version" => self::$version,
- "oauth_nonce" => self::generate_nonce(),
- "oauth_timestamp" => self::generate_timestamp(),
- "oauth_consumer_key" => $consumer->key);
- /*if ($token)
- $defaults['oauth_token'] = $token->key;*/
- $parameters = array_merge($defaults, $parameters);
- return new makerequest($http_method, $http_url, $parameters);
- }
- public function sign_request($signature_method, $consumer, $token)
- {
- $this->parameters['oauth_signature_method'] = $signature_method;
- $signature = $this->build_signature($consumer, $token);
- $this->parameters['oauth_signature'] = $signature;
- }
- public function build_signature($consumer, $token)
- {
- $base_string = $this->get_signature_base_string();
- $this->base_string = $base_string;
- $key_parts = array(
- $consumer->secret,
- ($token) ? $token->secret : ""
- );
- $key_parts = util::safe_encode($key_parts);
- $key = implode('&', $key_parts);
- return base64_encode(hash_hmac('sha1', $base_string, $key, true));
- }
- public function get_signature_base_string()
- {
- $parts = array(
- $this->get_normalized_http_method(),
- $this->get_normalized_http_url(),
- $this->get_signable_parameters()
- );
- $parts = util::safe_encode($parts);
- return implode('&', $parts);
- }
- public function get_normalized_http_method()
- {
- return strtoupper($this->http_method);
- }
- public function get_normalized_http_url()
- {
- $parts = parse_url($this->http_url);
- $port = @$parts['port'];
- $scheme = $parts['scheme'];
- $host = $parts['host'];
- $path = @$parts['path'];
- $port or $port = ($scheme == 'https') ? '443' : '80';
- if (($scheme == 'https' && $port != '443')
- || ($scheme == 'http' && $port != '80')) {
- $host = "$host:$port";
- }
- return "$scheme://$host$path";
- }
- public function get_signable_parameters() {
- $params = $this->parameters;
- if (isset($params['oauth_signature'])) {
- unset($params['oauth_signature']);
- }
- return util::build_http_query($params);
- }
- public function to_url() {
- $post_data = util::build_http_query($this->parameters);
- $out = $this->http_url;
- if ($post_data) {
- $out .= '?'.$post_data;
- }
- return $out;
- }
- private static function generate_nonce()
- {
- $mt = microtime();
- $rand = mt_rand();
- return md5($mt . $rand);
- }
- private static function generate_timestamp()
- {
- return time();
- }
- }
- class util {
- public static function parse_parameters($input)
- {
- if (!isset($input) || !$input) return array();
- $pairs = explode('&', $input);
- $parsed_parameters = array();
- foreach ($pairs as $pair) {
- $split = explode('=', $pair, 2);
- $parameter = self::safe_encode($split[0]);
- $value = isset($split[1]) ? self::safe_encode($split[1]) : '';
- if (isset($parsed_parameters[$parameter])) {
- if (is_scalar($parsed_parameters[$parameter])) {
- $parsed_parameters[$parameter] = array($parsed_parameters[$parameter]);
- }
- $parsed_parameters[$parameter][] = $value;
- }
- else {
- $parsed_parameters[$parameter] = $value;
- }
- }
- return $parsed_parameters;
- }
- public static function safe_encode($data)
- {
- if (is_array($data)) {
- return array_map(array('util', 'safe_encode'), $data);
- } else if (is_scalar($data)) {
- return str_ireplace(
- array('+', '%7E'),
- array(' ', '~'),
- rawurlencode($data)
- );
- } else {
- return '';
- }
- }
- public static function build_http_query($params) {
- if (!$params) return '';
- $keys = array_keys($params);
- $values = array_values($params);
- $params = array_combine($keys, $values);
- uksort($params, 'strcmp');
- $pairs = array();
- foreach ($params as $parameter => $value) {
- if (is_array($value)) {
- natsort($value);
- foreach ($value as $duplicate_value) {
- $pairs[] = $parameter . '=' . $duplicate_value;
- }
- } else {
- $pairs[] = $parameter . '=' . $value;
- }
- }
- return implode('&', $pairs);
- }
- }
- ?>
- <?php
- require_once('oauth.php');
- class api {
- const REQUEST_TOKEN_URL = 'http://www.abc.loc/oauth/requesttoken';
- public function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null)
- {
- $this->sha1_method = 'HMAC-SHA1';
- $this->consumer = new consumer($consumer_key, $consumer_secret);
- $this->token = null;
- }
- public function getRequestToken($oauth_callback = NULL)
- {
- $parameters = array();
- if (!empty($oauth_callback)) {
- //$parameters['oauth_callback'] = $oauth_callback;
- }
- $request = $this->oAuthRequest(self::REQUEST_TOKEN_URL, 'GET', $parameters);
- $token = util::parse_parameters($request);
- $this->token = new consumer($token['oauth_token'], $token['oauth_token_secret']);
- return $token;
- }
- public function oAuthRequest($url, $method, $parameters) {
- $request = makerequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);
- $request->sign_request($this->sha1_method, $this->consumer, $this->token);
- switch ($method) {
- case 'GET':
- return $this->http($request->to_url(), 'GET');
- default:
- return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());
- }
- }
- private function http($url, $method, $postfields = NULL)
- {
- $this->http_info = array();
- $ci = curl_init();
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
- curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
- curl_setopt($ci, CURLOPT_HEADER, FALSE);
- switch ($method) {
- case 'POST':
- curl_setopt($ci, CURLOPT_POST, TRUE);
- if (!empty($postfields)) {
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
- }
- break;
- case 'DELETE':
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
- if (!empty($postfields)) {
- $url = "{$url}?{$postfields}";
- }
- }
- curl_setopt($ci, CURLOPT_URL, $url);
- $response = curl_exec($ci);
- $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
- $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
- $this->url = $url;
- curl_close ($ci);
- return $response;
- }
- public function getHeader($ch, $header) {
- $i = strpos($header, ':');
- if (!empty($i)) {
- $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
- $value = trim(substr($header, $i + 2));
- $this->http_header[$key] = $value;
- }
- return strlen($header);
- }
- }
- $api = new api('1d7259a770e0732d191bb566b5cf9e', '7e662975b2');
- $temp_credential = $api->getRequestToken('http://www.abc.loc');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement