/**
  * @return str
  */
 public function authControl()
 {
     $config = Config::getInstance();
     Loader::definePathConstants();
     $this->setViewTemplate(THINKUP_WEBAPP_PATH . 'plugins/twitterrealtime/view/twitterrealtime.account.index.tpl');
     $this->view_mgr->addHelp('twitterrealtime', 'userguide/settings/plugins/twitterrealtime');
     $id = DAOFactory::getDAO('InstanceDAO');
     $od = DAOFactory::getDAO('OwnerDAO');
     // get plugin option values if defined...
     $plugin_options = $this->getPluginOptions();
     // get oauth option values from twitter plugin.
     // @TODO -- what is the right way to do this?
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $twitter_options = $plugin_option_dao->getOptionsHash('twitter', true);
     $oauth_consumer_key = null;
     if (isset($twitter_options['oauth_consumer_key'])) {
         $oauth_consumer_key = $twitter_options['oauth_consumer_key']->option_value;
     }
     $oauth_consumer_secret = null;
     if (isset($twitter_options['oauth_consumer_secret'])) {
         $oauth_consumer_secret = $twitter_options['oauth_consumer_secret']->option_value;
     }
     // @TODO - get any other option values as necessary
     // $archive_limit = $this->getPluginOption('archive_limit');
     $auth_from_twitter = '';
     if (isset($oauth_consumer_key) && isset($oauth_consumer_secret)) {
         $to = new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret);
         /* Request tokens from twitter */
         $tok = $to->getRequestToken();
         if (isset($tok['oauth_token'])) {
             $token = $tok['oauth_token'];
             $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];
             /* Build the authorization URL */
             $oauthorize_link = $to->getAuthorizeURL($token);
             // create indication that auth from twitter plugin is okay
             $auth_from_twitter = "Using the Twitter Consumer key and secret as set in " . "the <a href=\"./?p=twitter\">Twitter plugin</a>.";
         } else {
             //set error message here
             $this->addErrorMessage("Unable to obtain OAuth token. Check your Twitter plugin consumer key and secret configuration.");
             $oauthorize_link = '';
         }
     } else {
         $this->addErrorMessage("Missing required settings! Please configure the Twitter plugin.");
         $oauthorize_link = '';
     }
     $owner_instances = $id->getByOwnerAndNetwork($this->owner, 'twitter');
     $this->addToView('owner_instances', $owner_instances);
     $this->addToView('oauthorize_link', $oauthorize_link);
     $this->addToView('auth_from_twitter', $auth_from_twitter);
     // add plugin options from
     $this->addOptionForm();
     $plugin = new TwitterRealtimePlugin();
     $this->addToView('is_configured', $plugin->isConfigured());
     return $this->generateView();
 }
 /**
  * @return str
  */
 public function authControl()
 {
     $config = Config::getInstance();
     Loader::definePathConstants();
     $this->setViewTemplate(THINKUP_WEBAPP_PATH . 'plugins/twitterrealtime/view/twitterrealtime.account.index.tpl');
     $this->view_mgr->addHelp('twitterrealtime', 'userguide/settings/plugins/twitterrealtime');
     $id = DAOFactory::getDAO('InstanceDAO');
     $od = DAOFactory::getDAO('OwnerDAO');
     // get plugin option values if defined...
     $plugin_options = $this->getPluginOptions();
     // get oauth option values from twitter plugin.
     // @TODO -- what is the right way to do this?
     $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
     $twitter_options = $plugin_option_dao->getOptionsHash('twitter', true);
     $oauth_consumer_key = null;
     if (isset($twitter_options['oauth_consumer_key'])) {
         $oauth_consumer_key = $twitter_options['oauth_consumer_key']->option_value;
     }
     $oauth_consumer_secret = null;
     if (isset($twitter_options['oauth_consumer_secret'])) {
         $oauth_consumer_secret = $twitter_options['oauth_consumer_secret']->option_value;
     }
     // @TODO - get any other option values as necessary
     // $archive_limit = $this->getPluginOption('archive_limit');
     //Add public user instance
     if (isset($_GET['twitter_username'])) {
         // if form was submitted
         $logger = Logger::getInstance();
         $api = new TwitterAPIAccessorOAuth('NOAUTH', 'NOAUTH', $oauth_consumer_key, $oauth_consumer_secret, $num_twitter_errors, $max_api_calls_per_crawl);
         $api_call = str_replace("[id]", $_GET['twitter_username'], $api->cURL_source['show_user']);
         list($cURL_status, $data) = $api->apiRequestFromWebapp($api_call);
         if ($cURL_status == 200) {
             $thisFeed = array();
             try {
                 $xml = $api->createParserFromString(utf8_encode($data));
                 $user = array('user_id' => $xml->id, 'user_name' => $xml->screen_name, 'is_protected' => $xml->protected);
             } catch (Exception $e) {
                 $this->addErrorMessage($e->getMessage());
             }
             if (isset($user) && $user["is_protected"] == 'false') {
                 // if so, add to instances table and owners table
                 $i = $id->getByUsernameOnNetwork($_GET['twitter_username'], 'twitter');
                 $oid = DAOFactory::getDAO('OwnerInstanceDAO');
                 $msg = '';
                 if (isset($i)) {
                     //Instance exists
                     $oi = $oid->get($this->owner->id, $i->id);
                     if ($oi == null) {
                         //Owner_instance doesn't exist
                         $oid->insert($this->owner->id, $i->id, '', '');
                     }
                 } else {
                     //Instance does not exist
                     $id->insert($user["user_id"], $user["user_name"]);
                     $i = $id->getByUsernameOnNetwork($user["user_name"], 'twitter');
                     $oid->insert($this->owner->id, $i->id, '', '');
                 }
                 $this->addSuccessMessage($_GET['twitter_username'] . " has been added to ThinkUp.");
                 $this->addSuccessMessage("Added " . $_GET['twitter_username'] . " to ThinkUp.");
             } else {
                 // if not, return error
                 $this->addErrorMessage($_GET['twitter_username'] . " is a private Twitter account; ThinkUp cannot track it without authorization.");
             }
         } else {
             $this->addErrorMessage($_GET['twitter_username'] . " is not a valid Twitter username.");
         }
     }
     $auth_from_twitter = '';
     if (isset($oauth_consumer_key) && isset($oauth_consumer_secret)) {
         $to = new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret);
         /* Request tokens from twitter */
         $tok = $to->getRequestToken();
         if (isset($tok['oauth_token'])) {
             $token = $tok['oauth_token'];
             $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];
             /* Build the authorization URL */
             $oauthorize_link = $to->getAuthorizeURL($token);
             // create indication that auth from twitter plugin is okay
             $auth_from_twitter = "Using the Twitter Consumer key and secret as set in " . "the <a href=\"./?p=twitter\">Twitter plugin</a>.";
         } else {
             //set error message here
             $this->addErrorMessage("Unable to obtain OAuth token. Check your Twitter plugin consumer key and secret configuration.");
             $oauthorize_link = '';
         }
     } else {
         $this->addErrorMessage("Missing required settings! Please configure the Twitter plugin.");
         $oauthorize_link = '';
     }
     $owner_instances = $id->getByOwnerAndNetwork($this->owner, 'twitter');
     $this->addToView('owner_instances', $owner_instances);
     $this->addToView('oauthorize_link', $oauthorize_link);
     $this->addToView('auth_from_twitter', $auth_from_twitter);
     // add plugin options from
     $this->addOptionForm();
     $plugin = new TwitterRealtimePlugin();
     $this->addToView('is_configured', $plugin->isConfigured());
     return $this->generateView();
 }