/**
  * Saves a Foreign_link between Twitter user and local user,
  * which includes the access token and secret.
  *
  * @param int        $user_id StatusNet user ID
  * @param int        $twuid   Twitter user ID
  * @param OAuthToken $token   the access token to save
  *
  * @return nothing
  */
 function saveForeignLink($user_id, $twuid, $access_token)
 {
     $flink = new Foreign_link();
     $flink->user_id = $user_id;
     $flink->service = TWITTER_SERVICE;
     // delete stale flink, if any
     $result = $flink->find(true);
     if (!empty($result)) {
         $flink->safeDelete();
     }
     $flink->user_id = $user_id;
     $flink->foreign_id = $twuid;
     $flink->service = TWITTER_SERVICE;
     $creds = TwitterOAuthClient::packToken($access_token);
     $flink->credentials = $creds;
     $flink->created = common_sql_now();
     // Defaults: noticesync on, everything else off
     $flink->set_flags(true, false, false, false);
     $flink_id = $flink->insert();
     if (empty($flink_id)) {
         common_log_db_error($flink, 'INSERT', __FILE__);
         // TRANS: Server error displayed when linking to a Twitter account fails.
         $this->serverError(_m('Could not link your Twitter account.'));
     }
     return $flink_id;
 }
Exemple #2
0
 /**
  * Associate a Twitter account with the user's account
  *
  * Validates post input; verifies it against Twitter; and if
  * successful stores in the database.
  *
  * @return void
  */
 function addTwitterAccount()
 {
     $screen_name = $this->trimmed('twitter_username');
     $password = $this->trimmed('twitter_password');
     $noticesync = $this->boolean('noticesync');
     $replysync = $this->boolean('replysync');
     $friendsync = $this->boolean('friendsync');
     if (!Validate::string($screen_name, array('min_length' => 1, 'max_length' => 15, 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
         $this->showForm(_('Username must have only numbers, ' . 'upper- and lowercase letters, ' . 'and underscore (_). 15 chars max.'));
         return;
     }
     if (!$this->verifyCredentials($screen_name, $password)) {
         $this->showForm(_('Could not verify your Twitter credentials!'));
         return;
     }
     $twit_user = twitter_user_info($screen_name, $password);
     if (!$twit_user) {
         $this->showForm(sprintf(_('Unable to retrieve account information ' . 'For "%s" from Twitter.'), $screen_name));
         return;
     }
     if (!save_twitter_user($twit_user->id, $screen_name)) {
         $this->showForm(_('Unable to save your Twitter settings!'));
         return;
     }
     $user = common_current_user();
     $flink = new Foreign_link();
     $flink->user_id = $user->id;
     $flink->foreign_id = $twit_user->id;
     $flink->service = TWITTER_SERVICE;
     $flink->credentials = $password;
     $flink->created = common_sql_now();
     $flink->set_flags($noticesync, $replysync, $friendsync);
     $flink_id = $flink->insert();
     if (!$flink_id) {
         common_log_db_error($flink, 'INSERT', __FILE__);
         $this->showForm(_('Unable to save your Twitter settings!'));
         return;
     }
     if ($friendsync) {
         save_twitter_friends($user, $twit_user->id, $screen_name, $password);
     }
     $this->showForm(_('Twitter settings saved.'), true);
 }