Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class userTokenTumblr {
- var $consumer_key;
- var $consumer_secret;
- var $login_url; // Tumblr url for require login permission
- var $request_token; // Tumblr Request Token
- var $access_token; // Tumblr Access token array(oauth_token, oauth_token_secret)
- var $oauth_token;
- var $oauth_token_secret;
- var $username;
- var $service_url = 'http://www.tumblr.com';
- var $request_token_url = 'http://www.tumblr.com/oauth/request_token';
- var $authorize_url = 'http://www.tumblr.com/oauth/authorize';
- var $access_token_url = 'http://www.tumblr.com/oauth/access_token';
- var $authenticate_url = 'http://www.tumblr.com/api/authenticate';
- var $read_url = 'http://www.tumblr.com/api/read';
- var $write_url = 'http://www.tumblr.com/api/write';
- var $tumblrOauthObject;
- function __construct() {
- $this->consumer_key = $GLOBALS['tumblrConsumerKey'];
- $this->consumer_secret = $GLOBALS['tumblrConsumerSecret'];
- // We create the Oauth object as a mehtod's property.
- //Very important, the use of OAUTH_AUTH_TYPE_URI for general purpose, although we may need to use a diferent method for diferent tasks
- $this->tumblrOauthObject = new OAuth($this->consumer_key,$this->consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
- //$this->tumblrOauthObject->enableDebug();
- }
- //Retrieve the tokens from DB.
- function GetDBAccessTokens($uIdentifier, $uId) {
- $usertokenservice =& ServiceFactory::getServiceInstance('UserTokenService');
- $data = $usertokenservice->GetData($uIdentifier, $uId, 'tumblr');
- $unserializedata=unserialize($data['data']);
- $tokens['oauth_token']=$unserializedata['oauth_token'];
- $tokens['oauth_token_secret']=$unserializedata['oauth_token_secret'];
- return $tokens;
- }
- function RequestLogin() {
- //With this lines, we retrieve the login url, and redirect the user there.
- $response = $this->tumblrOauthObject->getRequestToken($this->request_token_url);
- $_SESSION['secret'] = $response['oauth_token_secret'];
- header("Location: {$this->authorize_url}?oauth_token={$response["oauth_token"]}");
- exit();
- }
- function CallBackAuth() {
- if (isset($_GET['oauth_token'])
- || (isset($_SESSION['oauth_token']) && isset($_SESSION['oauth_token_secret'])) ) {
- if( !isset($_SESSION['oauth_token']) || !isset($_SESSION['oauth_token_secret']) ) {
- //If we havent sessions tokens, its the first time here for the user, so we put in the oauth object,
- // The oauth_token from $_GET and the secret token retrieved in the RequestLogin method. (saved in session)
- // This is a temporary token, how allows us to retrieve the definitive tokens, the one that we will save to db.
- $this->tumblrOauthObject->setToken($_GET['oauth_token'], $_SESSION['secret']);
- //GetAccessToken retrieves the permanent tokens for the tumblr user.
- $token = $this->tumblrOauthObject->getAccessToken($this->access_token_url);
- //Once we have the definitive tokens, we save it temporarly in SESSION, to avoid loosing data between redirections.
- $_SESSION['oauth_token'] = $token['oauth_token'];
- $_SESSION['oauth_token_secret'] = $token['oauth_token_secret'];
- // pass deffinitive tokens to Tumblr object
- $this->tumblrOauthObject->setToken($token['oauth_token'], $token['oauth_token_secret']);
- $token['oauth_token'] = $_SESSION['oauth_token'];
- $token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
- } else {
- //If we already have SESSION stored tokens, we set this tokens for the Oauth Object.
- $token['oauth_token']=$_SESSION['oauth_token'];
- $token['oauth_token_secret']=$_SESSION['oauth_token_secret'];
- $this->tumblrOauthObject->setToken($token->oauth_token, $token->oauth_token_secret);
- }
- } else {
- //If no GET oauth token is set, or no SESSION token, we proceed retrieveng permissions to the user
- $this->RequestLogin('tumblr');
- }
- $params = array(
- 'oauth_token' => $_SESSION['oauth_token']
- );
- $tumblrInfo = $this->tumblrOauthObject->fetch($this->authenticate_url, $params);
- $tumblrInfo = $this->tumblrOauthObject->getLastResponse();
- $xml = new SimpleXMLElement($tumblrInfo);
- //Now in $xml we have the user information.
- //The next steps are optional, and we use it to normalize the diferent social apis output.
- //This class is called from a wrapeer how encapsulates the diferent API calls, so we provide the wrapper with a normalized
- //output always.
- $response['provider'] = 'tumblr';
- $response['username'] = (string) $xml->tumblelog['name'];
- $response['name'] = (string) $xml->tumblelog['name'];
- $response['email'] = '';
- $response['avatar'] = (string) $xml->tumblelog['avatar-url'];
- $response['location'] = $tumblrInfo->location;
- $response['desc'] = $tumblrInfo->description;
- $response['url'] = (string) $xml->tumblelog['avatar-url'];
- $response['oauth_token'] = $token['oauth_token'];
- $response['oauth_token_secret'] = $token['oauth_token_secret'];
- $response['uIdentifier'] = (string) $xml->tumblelog['name'];
- return $response;
- }
- // In this function is the quizz, we retrieve permanent tokens from DB and set it on the oauth object.
- function AutoShareThis($uId, $data) {
- //We redefine the tumblrOauthObject because we need to change the OAUTH_AUTH_TYPE_URI for OAUTH_AUTH_TYPE_FORM, because the write requests
- //must pass the parameters trough POST
- $this->tumblrOauthObject = new OAuth($this->consumer_key,$this->consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_FORM);
- if ($dbtokens=$this->GetDBAccessTokens(0, $uId)) {
- // We use a Try/catch structure for handle possible revoked or non existent tokenns.
- try {
- //Here we set the permanent tokens previously retrieved from database.
- $this->tumblrOauthObject->setToken($dbtokens['oauth_token'], $dbtokens['oauth_token_secret']);
- $message = sprintf(T_("I've just found this awesome picture on %s: %s"), $GLOBALS['sitename'], $data['link']);
- //Seeking int he tumblr api docs, we can see the parameters we need to pass for the Oauth fetch call.
- $params = array(
- 'oauth_token' => $dbtokens['oauth_token'],
- 'type' => 'photo',
- 'source' => $data['picture'],
- 'url' => $data['link'],
- 'caption' => $message,
- 'click-through-url' =>$data['link'],
- 'generator' => 'Testing example'
- );
- $status=$this->tumblrOauthObject->fetch($this->write_url, $params);
- if ($status) $result=true;
- } catch(Exception $e) {
- //print_r($e);
- $result=false;
- $message='revoked';
- }
- } else {
- $result=false;
- $message='dberror';
- }
- return array(
- 'result' => $result,
- 'message' => $message
- );
- }
- function RequestLogout() {
- session_destroy();
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement