public function testVerifyCredentials404Response()
 {
     $api = new TwitterAPIAccessorOAuth($oauth_access_token = '111', $oauth_access_token_secret = '222', $oauth_consumer_key = 1234, $oauth_consumer_secret = 1234, $num_twitter_errors = 5, $log = true);
     //Malformed JSON
     $api->to->setDataPathFolder('testoftwitterapiaccessoroauth/testverifycredentials4/');
     $this->expectException('APIErrorException');
     $result = $api->verifyCredentials();
     $this->assertNull($result);
 }
 public function authControl()
 {
     if (!$this->is_missing_param) {
         $request_token = $_GET['oauth_token'];
         $request_token_secret = SessionCache::get('oauth_request_token_secret');
         // get oauth values
         $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
         $options = $plugin_option_dao->getOptionsHash('twitter', true);
         //get cached
         $to = new TwitterOAuth($options['oauth_consumer_key']->option_value, $options['oauth_consumer_secret']->option_value, $request_token, $request_token_secret);
         $tok = $to->getAccessToken();
         if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) {
             $api = new TwitterAPIAccessorOAuth($tok['oauth_token'], $tok['oauth_token_secret'], $options['oauth_consumer_key']->option_value, $options['oauth_consumer_secret']->option_value, $options['num_twitter_errors']->option_value, $options['max_api_calls_per_crawl']->option_value, false);
             $authed_twitter_user = $api->verifyCredentials();
             //                echo "User ID: ". $authed_twitter_user['user_id'];
             //                echo "User name: ". $authed_twitter_user['user_name'];
             $owner_dao = DAOFactory::getDAO('OwnerDAO');
             $owner = $owner_dao->getByEmail($this->getLoggedInUser());
             if ((int) $authed_twitter_user['user_id'] > 0) {
                 $instance_dao = DAOFactory::getDAO('TwitterInstanceDAO');
                 $instance = $instance_dao->getByUsername($authed_twitter_user['user_name'], 'twitter');
                 $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
                 if (isset($instance)) {
                     $owner_instance = $owner_instance_dao->get($owner->id, $instance->id);
                     if ($owner_instance != null) {
                         $owner_instance_dao->updateTokens($owner->id, $instance->id, $tok['oauth_token'], $tok['oauth_token_secret']);
                         $this->addSuccessMessage($authed_twitter_user['user_name'] . " on Twitter is already set up in ThinkUp! To add a different Twitter account, " . "log out of Twitter.com in your browser and authorize ThinkUp again.");
                     } else {
                         if ($owner_instance_dao->insert($owner->id, $instance->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                             $this->addSuccessMessage("Success! " . $authed_twitter_user['user_name'] . " on Twitter has been added to ThinkUp!");
                         } else {
                             $this->addErrorMessage("Error: Could not create an owner instance.");
                         }
                     }
                 } else {
                     $instance_dao->insert($authed_twitter_user['user_id'], $authed_twitter_user['user_name']);
                     $instance = $instance_dao->getByUsername($authed_twitter_user['user_name']);
                     if ($owner_instance_dao->insert($owner->id, $instance->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                         $this->addSuccessMessage("Success! " . $authed_twitter_user['user_name'] . " on Twitter has been added to ThinkUp!");
                     } else {
                         $this->addErrorMessage("Error: Could not create an owner instance.");
                     }
                 }
             }
         } else {
             $msg = "Error: Twitter authorization did not complete successfully. Check if your account already " . " exists. If not, please try again.";
             $this->addErrorMessage($msg);
         }
         $this->view_mgr->clear_all_cache();
     }
     return $this->generateView();
 }
 public function authControl()
 {
     $msg = "";
     if (!$this->is_missing_param) {
         $request_token = $_GET['oauth_token'];
         $request_token_secret = $_SESSION['oauth_request_token_secret'];
         // get oauth values
         $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
         $options = $plugin_option_dao->getOptionsHash('twitter', true);
         //get cached
         $to = new TwitterOAuth($options['oauth_consumer_key']->option_value, $options['oauth_consumer_secret']->option_value, $request_token, $request_token_secret);
         $tok = $to->getAccessToken();
         if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) {
             $api = new TwitterAPIAccessorOAuth($tok['oauth_token'], $tok['oauth_token_secret'], $options['oauth_consumer_key']->option_value, $options['oauth_consumer_secret']->option_value, $options['num_twitter_errors']->option_value, $options['max_api_calls_per_crawl']->option_value, false);
             $u = $api->verifyCredentials();
             //echo "User ID: ". $u['user_id'];
             //echo "User name: ". $u['user_name'];
             $twitter_id = $u['user_id'];
             $tu = $u['user_name'];
             $od = DAOFactory::getDAO('OwnerDAO');
             $owner = $od->getByEmail($this->getLoggedInUser());
             if ($twitter_id > 0) {
                 $msg = "<h2 class=\"subhead\">Twitter authentication successful!</h2>";
                 $id = DAOFactory::getDAO('InstanceDAO');
                 $i = $id->getByUsername($tu);
                 $oid = DAOFactory::getDAO('OwnerInstanceDAO');
                 if (isset($i)) {
                     $msg .= "Instance already exists.<br />";
                     $oi = $oid->get($owner->id, $i->id);
                     if ($oi != null) {
                         $msg .= "Owner already has this instance, no insert  required.<br />";
                         if ($oid->updateTokens($owner->id, $i->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                             $msg .= "OAuth Tokens updated.";
                         } else {
                             $msg .= "OAuth Tokens NOT updated.";
                         }
                     } else {
                         if ($oid->insert($owner->id, $i->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                             $msg .= "Added owner instance.<br />";
                         } else {
                             $msg .= "PROBLEM Did not add owner instance.<br />";
                         }
                     }
                 } else {
                     $msg .= "Instance does not exist.<br />";
                     $id->insert($twitter_id, $tu);
                     $msg .= "Created instance.<br />";
                     $i = $id->getByUsername($tu);
                     if ($oid->insert($owner->id, $i->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                         $msg .= "Created an owner instance.<br />";
                     } else {
                         $msg .= "Did NOT create an owner instance.<br />";
                     }
                 }
             }
         } else {
             $msg = "PROBLEM! Twitter authorization did not complete successfully. Check if your account already " . " exists. If not, please try again.";
         }
         $this->view_mgr->clear_all_cache();
         $config = Config::getInstance();
         $msg .= '<br /><br /><a href="' . $config->getValue('site_root_path') . 'account/index.php?p=twitter" class="tt-button ui-state-default tt-button-icon-left ui-corner-all"><span 
     class="ui-icon ui-icon-circle-arrow-e"></span>Back to your account</a>';
         $this->addInfoMessage($msg);
     }
     return $this->generateView();
 }
Esempio n. 4
0
require_once 'config.webapp.inc.php';
ini_set("include_path", ini_get("include_path") . PATH_SEPARATOR . $INCLUDE_PATH);
require_once "init.php";
$cfg = new Config();
$request_token = $_GET['oauth_token'];
$request_token_secret = $_SESSION['oauth_request_token_secret'];
/*
echo "DEBUG:"
echo "URL Request Token: ".$request_token."<br />";
echo "Session Request Token: ".$request_token_secret."<br />";
*/
$to = new TwitterOAuth($cfg->oauth_consumer_key, $cfg->oauth_consumer_secret, $request_token, $request_token_secret);
$tok = $to->getAccessToken();
if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) {
    $api = new TwitterAPIAccessorOAuth($tok['oauth_token'], $tok['oauth_token_secret'], $THINKTANK_CFG['oauth_consumer_key'], $THINKTANK_CFG['oauth_consumer_secret']);
    $u = $api->verifyCredentials();
    //	echo "User ID: ". $u['user_id'];
    //	echo "User name: ". $u['user_name'];
    $twitter_id = $u['user_id'];
    $tu = $u['user_name'];
    $db = new Database($THINKTANK_CFG);
    $conn = $db->getConnection();
    $od = new OwnerDAO($db);
    $owner = $od->getByEmail($_SESSION['user']);
    if ($twitter_id > 0) {
        echo "Twitter authentication successful.<br />";
        $id = new InstanceDAO($db);
        $i = $id->getByUsername($tu);
        $oid = new OwnerInstanceDAO($db);
        if (isset($i)) {
            echo "Instance already exists.<br />";
 /**
  * Add user who just returned from Twitter.com OAuth authorization and populate view with error/success messages.
  * @param str $oauth_consumer_key
  * @param str $oauth_consumer_secret
  * @param str $num_twitter_errors
  * @return void
  */
 private function addAuthorizedUser($oauth_consumer_key, $oauth_consumer_secret, $num_twitter_errors)
 {
     if (isset($_GET['oauth_token']) && SessionCache::isKeySet('oauth_request_token_secret')) {
         $request_token = $_GET['oauth_token'];
         $request_token_secret = SessionCache::get('oauth_request_token_secret');
         $twitter_oauth = new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret, $request_token, $request_token_secret);
         if (isset($_GET['oauth_verifier'])) {
             $tok = $twitter_oauth->getAccessToken($_GET['oauth_verifier']);
         } else {
             $tok = null;
         }
         if (isset($tok['oauth_token']) && isset($tok['oauth_token_secret'])) {
             $api = new TwitterAPIAccessorOAuth($tok['oauth_token'], $tok['oauth_token_secret'], $oauth_consumer_key, $oauth_consumer_secret, $num_twitter_errors, false);
             $authed_twitter_user = $api->verifyCredentials();
             //                echo "User ID: ". $authed_twitter_user['user_id']."<br>";
             //                echo "User name: ". $authed_twitter_user['user_name']."<br>";
             $owner_dao = DAOFactory::getDAO('OwnerDAO');
             $owner = $owner_dao->getByEmail($this->getLoggedInUser());
             if (isset($authed_twitter_user) && isset($authed_twitter_user['user_name']) && isset($authed_twitter_user['user_id'])) {
                 $instance_dao = DAOFactory::getDAO('TwitterInstanceDAO');
                 $instance = $instance_dao->getByUsername($authed_twitter_user['user_name'], 'twitter');
                 $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
                 if (isset($instance)) {
                     $owner_instance = $owner_instance_dao->get($owner->id, $instance->id);
                     if ($owner_instance != null) {
                         $owner_instance_dao->updateTokens($owner->id, $instance->id, $tok['oauth_token'], $tok['oauth_token_secret']);
                         $this->addSuccessMessage($authed_twitter_user['user_name'] . " on Twitter is already set up in ThinkUp! To add a different Twitter account, " . "log out of Twitter.com in your browser and authorize ThinkUp again.", 'user_add');
                     } else {
                         if ($owner_instance_dao->insert($owner->id, $instance->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                             $this->addSuccessMessage("Success! " . $authed_twitter_user['user_name'] . " on Twitter has been added to ThinkUp!", "user_add");
                         } else {
                             $this->addErrorMessage("Error: Could not create an owner instance.", "user_add");
                         }
                     }
                 } else {
                     $instance_dao->insert($authed_twitter_user['user_id'], $authed_twitter_user['user_name']);
                     $instance = $instance_dao->getByUsername($authed_twitter_user['user_name']);
                     if ($owner_instance_dao->insert($owner->id, $instance->id, $tok['oauth_token'], $tok['oauth_token_secret'])) {
                         $this->addSuccessMessage("Success! " . $authed_twitter_user['user_name'] . " on Twitter has been added to ThinkUp!", "user_add");
                     } else {
                         $this->addErrorMessage("Error: Could not create an owner instance.", "user_add");
                     }
                 }
             }
         } else {
             $msg = "Error: Twitter authorization did not complete successfully. Check if your account already " . " exists. If not, please try again.";
             $this->addErrorMessage($msg, "user_add");
         }
         $this->view_mgr->clear_all_cache();
     }
 }