public function callbackAction() { $oauth_token = $this->getRequest()->getParam('oauth_token'); $oauth_verifier = $this->getRequest()->getParam('oauth_verifier'); if (!$oauth_token || !$oauth_verifier) { return $this->_redirectUrl(); } if ($oauth_token != Mage::getSingleton('core/session')->getData('oauth_token')) { return $this->_redirectUrl(); } $connection = new TumblrOAuth(Mage::getStoreConfig('gomage_social/tumblr/id'), Mage::getStoreConfig('gomage_social/tumblr/secret'), Mage::getSingleton('core/session')->getData('oauth_token'), Mage::getSingleton('core/session')->getData('oauth_token_secret')); $access_token = $connection->getAccessToken($oauth_verifier); Mage::getSingleton('core/session')->unsetData('oauth_token'); Mage::getSingleton('core/session')->unsetData('oauth_token_secret'); $profile = null; switch ($connection->http_code) { case 200: $profile = $connection->get("http://api.tumblr.com/v2/user/info"); break; default: $this->getSession()->addError($this->__('Could not connect to Tumblr. Refresh the page or try again later.')); return $this->_redirectUrl(); } if ($profile) { $profile->name = $profile->response->user->name; $profile->id = $profile->name; if ($profile->id) { $social_collection = Mage::getModel('gomage_social/entity')->getCollection()->addFieldToFilter('social_id', $profile->id)->addFieldToFilter('type_id', GoMage_Social_Model_Type::TUMBLR); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $social_collection->addFieldToFilter('website_id', Mage::app()->getWebsite()->getId()); } $social = $social_collection->getFirstItem(); if ($social && $social->getId()) { if ($social->social_id == $profile->id) { $customer = Mage::getModel('customer/customer'); if (Mage::getSingleton('customer/config_share')->isWebsiteScope()) { $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); } $customer->load($social->getData('customer_id')); if ($customer && $customer->getId()) { if (!$customer->getConfirmation()) { $this->getSession()->loginById($customer->getId()); } else { $this->getSession()->addError($this->__('This account is not confirmed.')); } } } } else { $profile->url = Mage::getUrl('gomage_social/tumblr/checkEmail', array('_secure' => true)); $profile->urlEmailClose = Mage::getUrl('gomage_social/twitter/emailClose', array('_secure' => true)); $profile->type_id = GoMage_Social_Model_Type::TUMBLR; Mage::getSingleton('core/session')->setGsProfile($profile); } } } return $this->_redirectUrl(); }
/** * This method is used to process the second part of authentication workflow, after redirect * * @return array Array with status and user details */ public function processAuth() { $ngConnectINI = eZINI::instance('ngconnect.ini'); $http = eZHTTPTool::instance(); $consumerKey = trim($ngConnectINI->variable('LoginMethod_tumblr', 'AppConsumerKey')); $consumerSecret = trim($ngConnectINI->variable('LoginMethod_tumblr', 'AppConsumerSecret')); if (empty($consumerKey) || empty($consumerSecret)) { return array('status' => 'error', 'message' => 'Consumer key or consumer secret undefined.'); } $oAuthToken = trim($http->getVariable('oauth_token', '')); $oAuthVerifier = trim($http->getVariable('oauth_verifier', '')); $state = trim($http->getVariable('state', '')); if (empty($oAuthToken) || empty($oAuthVerifier) || empty($state)) { return array('status' => 'error', 'message' => 'oauth_token, oauth_verifier or state GET parameters undefined.'); } if (!$http->hasSessionVariable('NGConnectOAuthState') || $state != $http->sessionVariable('NGConnectOAuthState')) { $http->removeSessionVariable('NGConnectOAuthState'); return array('status' => 'error', 'message' => 'State parameter does not match stored value.'); } else { $http->removeSessionVariable('NGConnectOAuthState'); } if (!$http->hasSessionVariable('NGConnectOAuthToken') || !$http->hasSessionVariable('NGConnectOAuthTokenSecret') || $oAuthToken != $http->sessionVariable('NGConnectOAuthToken')) { $http->removeSessionVariable('NGConnectOAuthToken'); $http->removeSessionVariable('NGConnectOAuthTokenSecret'); return array('status' => 'error', 'message' => 'Token does not match stored value.'); } else { $oAuthTokenSecret = $http->sessionVariable('NGConnectOAuthTokenSecret'); $http->removeSessionVariable('NGConnectOAuthToken'); $http->removeSessionVariable('NGConnectOAuthTokenSecret'); } $connection = new TumblrOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthTokenSecret); $accessToken = $connection->getAccessToken($oAuthVerifier); if (!(isset($accessToken['oauth_token']) && isset($accessToken['oauth_token_secret']))) { return array('status' => 'error', 'message' => 'Error while retrieving access token.'); } $connection = new TumblrOAuth($consumerKey, $consumerSecret, $accessToken['oauth_token'], $accessToken['oauth_token_secret']); $userData = $connection->get('http://api.tumblr.com/v2/user/info'); $userData = json_decode($userData, true); if (!is_array($userData) || empty($userData['response']['user']['name'])) { return array('status' => 'error', 'message' => 'Invalid Tumblr user.'); } $name = $userData['response']['user']['name']; $result = array('status' => 'success', 'login_method' => 'tumblr', 'id' => $name, 'first_name' => $name, 'last_name' => $name, 'email' => '', 'picture' => ''); return $result; }
<?php // Start a session, load the library session_start(); require 'vendor/autoload.php'; require_once 'vendor/tumblroauthnonnative/tumblroauth.php'; // Define the needed keys $consumer_key = 'vliXIm52B9BG2L1hfRAy6SvPbekkOk5JEsRITW5MeOH1Xnknl2'; $consumer_secret = '3Pqe75MjkrvqdrHAyKuZ9Z3nWMlCuLKjfMnT5E1H0rrz8WXVay'; // Once the user approves your app at Tumblr, they are sent back to this script. // This script is passed two parameters in the URL, oauth_token (our Request Token) // and oauth_verifier (Key that we need to get Access Token). // We'll also need out Request Token Secret, which we stored in a session. // Create instance of TumblrOAuth. // It'll need our Consumer Key and Secret as well as our Request Token and Secret $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $_SESSION['request_token'], $_SESSION['request_token_secret']); $access_token = $tum_oauth->getAccessToken($_REQUEST['oauth_verifier']); // We're done with the Request Token and Secret so let's remove those. unset($_SESSION['request_token']); unset($_SESSION['request_token_secret']); // Make sure nothing went wrong. if (200 == $tum_oauth->http_code) { // good to go } else { die('Unable to authenticate'); } $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $access_token['oauth_token'], $access_token['oauth_token_secret']); echo $access_token['oauth_token']; echo $access_token['oauth_token_secret'];
* @file * Take the user when they return from Tumblr. Get access tokens. * Verify credentials and redirect to based on response from Tumblr. */ /* Start session and load lib */ session_start(); require_once 'tumblroauth/tumblroauth.php'; require_once 'config.php'; /* If the oauth_token is old redirect to the connect page. */ if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) { $_SESSION['oauth_status'] = 'oldtoken'; header('Location: ./clearsessions.php'); } /* Create TumblroAuth object with app key/secret and token key/secret from default phase */ $connection = new TumblrOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); /* Request access tokens from tumblr */ $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); /* Save the access tokens. Normally these would be saved in a database for future use. */ $_SESSION['access_token'] = $access_token; /* Remove no longer needed request tokens */ unset($_SESSION['oauth_token']); unset($_SESSION['oauth_token_secret']); /* If HTTP response is 200 continue otherwise send to connect page to retry */ if (200 == $connection->http_code) { /* The user has been verified and the access tokens can be saved for future use */ $_SESSION['status'] = 'verified'; header('Location: ./index.php'); } else { /* Save HTTP status for error dialog on connnect page.*/ header('Location: ./clearsessions.php'); }
function showGenNTSettings($ntOpts) { global $nxs_snapSetPgURL, $nxs_plurl, $nxs_gOptions; $ntInfo = $this->ntInfo; if (isset($_GET['auth']) && $_GET['auth'] == 'tr') { require_once 'apis/trOAuth.php'; $options = $ntOpts[$_GET['acc']]; $consumer_key = $options['trConsKey']; $consumer_secret = $options['trConsSec']; $callback_url = $nxs_snapSetPgURL . "&auth=tra&acc=" . $_GET['acc']; $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret); prr($tum_oauth); $request_token = $tum_oauth->getRequestToken($callback_url); echo "####"; prr($request_token); $options['trOAuthToken'] = $request_token['oauth_token']; $options['trOAuthTokenSecret'] = $request_token['oauth_token_secret']; // prr($tum_oauth ); die(); switch ($tum_oauth->http_code) { case 200: $url = $tum_oauth->getAuthorizeURL($options['trOAuthToken']); if (function_exists('get_option')) { $nxs_gOptions = get_option('NS_SNAutoPoster'); } if (!empty($nxs_gOptions)) { $nxs_gOptions['tr'][$_GET['acc']] = $options; nxs_settings_save($nxs_gOptions); } echo '<script type="text/javascript">window.location = "' . $url . '"</script>'; break; default: echo '<br/><b style="color:red">Could not connect to Tumblr. Refresh the page or try again later.</b>'; die; } die; } if (isset($_GET['auth']) && $_GET['auth'] == 'tra') { require_once 'apis/trOAuth.php'; $options = $ntOpts[$_GET['acc']]; prr($options); $consumer_key = $options['trConsKey']; $consumer_secret = $options['trConsSec']; $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $options['trOAuthToken'], $options['trOAuthTokenSecret']); $options['trAccessTocken'] = $tum_oauth->getAccessToken($_REQUEST['oauth_verifier']); // prr($_GET); prr($_REQUEST); prr($options['trAccessTocken']); $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $options['trAccessTocken']['oauth_token'], $options['trAccessTocken']['oauth_token_secret']); if (function_exists('get_option')) { $nxs_gOptions = get_option('NS_SNAutoPoster'); } if (!empty($nxs_gOptions)) { $nxs_gOptions['tr'][$_GET['acc']] = $options; nxs_settings_save($nxs_gOptions); } $userinfo = $tum_oauth->get('http://api.tumblr.com/v2/user/info'); prr($userinfo); prr($tum_oauth); // prr($url); die(); if (is_array($userinfo->response->user->blogs)) { foreach ($userinfo->response->user->blogs as $blog) { if (stripos($blog->url, $options['trPgID']) !== false) { $gGet = $_GET; unset($gGet['auth']); unset($gGet['acc']); unset($gGet['oauth_token']); unset($gGet['oauth_verifier']); unset($gGet['post_type']); $sturl = explode('?', $nxs_snapSetPgURL); $nxs_snapSetPgURL = $sturl[0] . (!empty($gGet) ? '?' . http_build_query($gGet) : ''); echo '<script type="text/javascript">window.location = "' . $nxs_snapSetPgURL . '"</script>'; die; } } prr($userinfo); die("<span style='color:red;'>ERROR: Authorized USER don't have access to the specified blog: <span style='color:darkred; font-weight: bold;'>" . $options['trPgID'] . "</span></span>"); } } ?> <div class="nxs_box"> <div class="nxs_box_header"> <div class="nsx_iconedTitle" style="margin-bottom:1px;background-image:url(<?php echo $nxs_plurl; ?> img/<?php echo $ntInfo['lcode']; ?> 16.png);"><?php echo $ntInfo['name']; ?> <?php $cbo = count($ntOpts); ?> <?php if ($cbo > 1) { ?> <div class="nsBigText"><?php echo "(" . ($cbo == '0' ? 'No' : $cbo) . " "; _e('accounts', 'nxs_snap'); echo ")"; ?> </div><?php } ?> </div> </div> <div class="nxs_box_inside"> <?php foreach ($ntOpts as $indx => $pbo) { if (trim($pbo['nName'] == '')) { $pbo['nName'] = str_ireplace('https://', '', str_ireplace('http://', '', $pbo['trURL'])); } if (!isset($pbo[$ntInfo['lcode'] . 'OK']) || $pbo[$ntInfo['lcode'] . 'OK'] == '') { $pbo[$ntInfo['lcode'] . 'OK'] = isset($pbo['trOAuthTokenSecret']) && $pbo['trOAuthTokenSecret'] != '' ? '1' : ''; } ?> <p style="margin:0px;margin-left:5px;"> <img id="<?php echo $ntInfo['code'] . $indx; ?> LoadingImg" style="display: none;" src='<?php echo $nxs_plurl; ?> img/ajax-loader-sm.gif' /> <input value="0" name="<?php echo $ntInfo['lcode']; ?> [<?php echo $indx; ?> ][apDo<?php echo $ntInfo['code']; ?> ]" type="hidden" /> <?php if ((int) $pbo['do' . $ntInfo['code']] == 1 && isset($pbo['catSel']) && (int) $pbo['catSel'] == 1) { ?> <input type="radio" name="<?php echo $ntInfo['lcode']; ?> [<?php echo $indx; ?> ][apDo<?php echo $ntInfo['code']; ?> ]" id="rbtn<?php echo $ntInfo['lcode'] . $indx; ?> " value="1" checked="checked" onmouseout="nxs_hidePopUpInfo('popOnlyCat');" onmouseover="nxs_showPopUpInfo('popOnlyCat', event);" /> <?php } else { ?> <input value="1" name="<?php echo $ntInfo['lcode']; ?> [<?php echo $indx; ?> ][apDo<?php echo $ntInfo['code']; ?> ]" type="checkbox" <?php if ((int) $pbo['do' . $ntInfo['code']] == 1 && $pbo['catSel'] != '1') { echo "checked"; } ?> /> <?php } ?> <?php if (isset($pbo['catSel']) && (int) $pbo['catSel'] == 1) { ?> <span onmouseout="nxs_hidePopUpInfo('popOnlyCat');" onmouseover="nxs_showPopUpInfo('popOnlyCat', event);"><?php echo "*[" . (substr_count($pbo['catSelEd'], ",") + 1) . "]*"; ?> </span><?php } ?> <?php if (isset($pbo['rpstOn']) && (int) $pbo['rpstOn'] == 1) { ?> <span onmouseout="nxs_hidePopUpInfo('popReActive');" onmouseover="nxs_showPopUpInfo('popReActive', event);"><?php echo "*[R]*"; ?> </span><?php } ?> <strong><?php _e('Auto-publish to', 'nxs_snap'); ?> <?php echo $ntInfo['name']; ?> <i style="color: #005800;"><?php if ($pbo['nName'] != '') { echo "(" . $pbo['nName'] . ")"; } ?> </i></strong> <?php if ($ntInfo['tstReq'] && (!isset($pbo[$ntInfo['lcode'] . 'OK']) || $pbo[$ntInfo['lcode'] . 'OK'] == '')) { ?> <b style="color: #800000"><?php _e('Attention requred. Unfinished setup', 'nxs_snap'); ?> ==></b><?php } ?> <a id="do<?php echo $ntInfo['code'] . $indx; ?> AG" href="#" onclick="doGetHideNTBlock('<?php echo $ntInfo['code']; ?> ' , '<?php echo $indx; ?> ');return false;">[<?php _e('Show Settings', 'nxs_snap'); ?> ]</a> <a href="#" onclick="doDelAcct('<?php echo $ntInfo['lcode']; ?> ', '<?php echo $indx; ?> ', '<?php if (isset($pbo['bgBlogID'])) { echo $pbo['nName']; } ?> ');return false;">[<?php _e('Remove Account', 'nxs_snap'); ?> ]</a> </p><div id="nxsNTSetDiv<?php echo $ntInfo['code'] . $indx; ?> "></div><?php //$pbo['ntInfo'] = $ntInfo; $this->showNTSettings($indx, $pbo); } ?> </div> </div> <?php }
function tumblr_callback($a) { // Start a session, load the library session_start(); //require_once('addon/tumblr/tumblroauth/tumblroauth.php'); // Define the needed keys $consumer_key = get_config('tumblr', 'consumer_key'); $consumer_secret = get_config('tumblr', 'consumer_secret'); // Once the user approves your app at Tumblr, they are sent back to this script. // This script is passed two parameters in the URL, oauth_token (our Request Token) // and oauth_verifier (Key that we need to get Access Token). // We'll also need out Request Token Secret, which we stored in a session. // Create instance of TumblrOAuth. // It'll need our Consumer Key and Secret as well as our Request Token and Secret $tum_oauth = new TumblrOAuth($consumer_key, $consumer_secret, $_SESSION['request_token'], $_SESSION['request_token_secret']); // Ok, let's get an Access Token. We'll need to pass along our oauth_verifier which was given to us in the URL. $access_token = $tum_oauth->getAccessToken($_REQUEST['oauth_verifier']); // We're done with the Request Token and Secret so let's remove those. unset($_SESSION['request_token']); unset($_SESSION['request_token_secret']); // Make sure nothing went wrong. if (200 == $tum_oauth->http_code) { // good to go } else { return 'Unable to authenticate'; } // What's next? Now that we have an Access Token and Secret, we can make an API call. set_pconfig(local_user(), "tumblr", "oauth_token", $access_token['oauth_token']); set_pconfig(local_user(), "tumblr", "oauth_token_secret", $access_token['oauth_token_secret']); $o = t("You are now authenticated to tumblr."); $o .= '<br /><a href="' . $a->get_baseurl() . '/settings/connectors">' . t("return to the connector page") . '</a>'; return $o; }