+++ /dev/null
-<?php\r
-\r
-/*\r
- * Abraham Williams (abraham@abrah.am) http://abrah.am\r
- *\r
- * The first PHP Library to support OAuth for Tumblr's REST API. (Originally for Twitter, modified for Tumblr by Lucas)\r
- */\r
-\r
-/* Load OAuth lib. You can find it at http://oauth.net */\r
-//require_once('OAuth.php');\r
-\r
-/**\r
- * Tumblr OAuth class\r
- */\r
-class TumblrOAuth {\r
- /* Contains the last HTTP status code returned. */\r
- public $http_code;\r
- /* Contains the last API call. */\r
- public $url;\r
- /* Set up the API root URL. */\r
- public $host = "http://api.tumblr.com/v2/";\r
- /* Set timeout default. */\r
- public $timeout = 30;\r
- /* Set connect timeout. */\r
- public $connecttimeout = 30; \r
- /* Verify SSL Cert. */\r
- public $ssl_verifypeer = FALSE;\r
- /* Respons format. */\r
- public $format = 'json';\r
- /* Decode returned json data. */\r
- public $decode_json = TRUE;\r
- /* Contains the last HTTP headers returned. */\r
- public $http_info;\r
- /* Set the useragnet. */\r
- public $useragent = 'TumblrOAuth v0.2.0-beta2';\r
- /* Immediately retry the API call if the response was not successful. */\r
- //public $retry = TRUE;\r
-\r
-\r
-\r
-\r
- /**\r
- * Set API URLS\r
- */\r
- function accessTokenURL() { return 'http://www.tumblr.com/oauth/access_token'; }\r
- function authenticateURL() { return 'http://www.tumblr.com/oauth/authorize'; }\r
- function authorizeURL() { return 'http://www.tumblr.com/oauth/authorize'; }\r
- function requestTokenURL() { return 'http://www.tumblr.com/oauth/request_token'; }\r
-\r
- /**\r
- * Debug helpers\r
- */\r
- function lastStatusCode() { return $this->http_status; }\r
- function lastAPICall() { return $this->last_api_call; }\r
-\r
- /**\r
- * construct TumblrOAuth object\r
- */\r
- function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {\r
- $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();\r
- $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);\r
- if (!empty($oauth_token) && !empty($oauth_token_secret)) {\r
- $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);\r
- } else {\r
- $this->token = NULL;\r
- }\r
- }\r
-\r
-\r
- /**\r
- * Get a request_token from Tumblr\r
- *\r
- * @returns a key/value array containing oauth_token and oauth_token_secret\r
- */\r
- function getRequestToken($oauth_callback = NULL) {\r
- $parameters = array();\r
- if (!empty($oauth_callback)) {\r
- $parameters['oauth_callback'] = $oauth_callback;\r
- } \r
- $request = $this->oAuthRequest($this->requestTokenURL(), 'GET', $parameters);\r
- $token = OAuthUtil::parse_parameters($request);\r
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);\r
- return $token;\r
- }\r
-\r
- /**\r
- * Get the authorize URL\r
- *\r
- * @returns a string\r
- */\r
- function getAuthorizeURL($token, $sign_in_with_tumblr = TRUE) {\r
- if (is_array($token)) {\r
- $token = $token['oauth_token'];\r
- }\r
- if (empty($sign_in_with_tumblr)) {\r
- return $this->authorizeURL() . "?oauth_token={$token}";\r
- } else {\r
- return $this->authenticateURL() . "?oauth_token={$token}";\r
- }\r
- }\r
-\r
- /**\r
- * Exchange request token and secret for an access token and\r
- * secret, to sign API calls.\r
- *\r
- * @returns array("oauth_token" => "the-access-token",\r
- * "oauth_token_secret" => "the-access-secret",\r
- * "user_id" => "9436992",\r
- * "screen_name" => "abraham")\r
- */\r
- function getAccessToken($oauth_verifier = FALSE) {\r
- $parameters = array();\r
- if (!empty($oauth_verifier)) {\r
- $parameters['oauth_verifier'] = $oauth_verifier;\r
- }\r
- $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);\r
- $token = OAuthUtil::parse_parameters($request);\r
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);\r
- return $token;\r
- }\r
-\r
- /**\r
- * One time exchange of username and password for access token and secret.\r
- *\r
- * @returns array("oauth_token" => "the-access-token",\r
- * "oauth_token_secret" => "the-access-secret",\r
- * "user_id" => "9436992",\r
- * "screen_name" => "abraham",\r
- * "x_auth_expires" => "0")\r
- */ \r
- function getXAuthToken($username, $password) {\r
- $parameters = array();\r
- $parameters['x_auth_username'] = $username;\r
- $parameters['x_auth_password'] = $password;\r
- $parameters['x_auth_mode'] = 'client_auth';\r
- $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);\r
- $token = OAuthUtil::parse_parameters($request);\r
- $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);\r
- return $token;\r
- }\r
-\r
- /**\r
- * GET wrapper for oAuthRequest.\r
- */\r
- function get($url, $parameters = array()) {\r
- $response = $this->oAuthRequest($url, 'GET', $parameters);\r
- if ($this->format === 'json' && $this->decode_json) {\r
- return json_decode($response);\r
- }\r
- return $response;\r
- }\r
- \r
- /**\r
- * POST wrapper for oAuthRequest.\r
- */\r
- function post($url, $parameters = array()) {\r
- $response = $this->oAuthRequest($url, 'POST', $parameters);\r
- if ($this->format === 'json' && $this->decode_json) {\r
- return json_decode($response);\r
- }\r
- return $response;\r
- }\r
-\r
- /**\r
- * DELETE wrapper for oAuthReqeust.\r
- */\r
- function delete($url, $parameters = array()) {\r
- $response = $this->oAuthRequest($url, 'DELETE', $parameters);\r
- if ($this->format === 'json' && $this->decode_json) {\r
- return json_decode($response);\r
- }\r
- return $response;\r
- }\r
-\r
- /**\r
- * Format and sign an OAuth / API request\r
- */\r
- function oAuthRequest($url, $method, $parameters) {\r
- if (strrpos($url, 'https://') !== 0 && strrpos($url, 'http://') !== 0) {\r
- $url = "{$this->host}{$url}";\r
- }\r
- $request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, $url, $parameters);\r
- $request->sign_request($this->sha1_method, $this->consumer, $this->token);\r
- switch ($method) {\r
- case 'GET':\r
- return $this->http($request->to_url(), 'GET');\r
- default:\r
- return $this->http($request->get_normalized_http_url(), $method, $request->to_postdata());\r
- }\r
- }\r
-\r
- /**\r
- * Make an HTTP request\r
- *\r
- * @return API results\r
- */\r
- function http($url, $method, $postfields = NULL) {\r
- $this->http_info = array();\r
- $ci = curl_init();\r
- /* Curl settings */\r
- curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);\r
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);\r
- curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);\r
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);\r
- curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));\r
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);\r
- curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));\r
- curl_setopt($ci, CURLOPT_HEADER, FALSE);\r
-\r
- switch ($method) {\r
- case 'POST':\r
- curl_setopt($ci, CURLOPT_POST, TRUE);\r
- if (!empty($postfields)) {\r
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);\r
- }\r
- break;\r
- case 'DELETE':\r
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');\r
- if (!empty($postfields)) {\r
- $url = "{$url}?{$postfields}";\r
- }\r
- }\r
-\r
- curl_setopt($ci, CURLOPT_URL, $url);\r
- $response = curl_exec($ci);\r
- $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);\r
- $this->http_info = array_merge($this->http_info, curl_getinfo($ci));\r
- $this->url = $url;\r
- curl_close ($ci);\r
- return $response;\r
- }\r
-\r
- /**\r
- * Get the header info to store.\r
- */\r
- function getHeader($ch, $header) {\r
- $i = strpos($header, ':');\r
- if (!empty($i)) {\r
- $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));\r
- $value = trim(substr($header, $i + 2));\r
- $this->http_header[$key] = $value;\r
- }\r
- return strlen($header);\r
- }\r
-}\r