+
+ function add_twitter_acct() {
+ $twitter_username = $this->trimmed('twitter_username');
+ $twitter_password = $this->trimmed('twitter_password');
+ $noticesync = $this->boolean('noticesync');
+ $replysync = $this->boolean('replysync');
+ $friendsync = $this->boolean('friendsync');
+
+ if (!Validate::string($twitter_username, array('min_length' => 1,
+ 'max_length' => 15,
+ 'format' => VALIDATE_NUM . VALIDATE_ALPHA . '_'))) {
+ $this->show_form(_('Username must have only numbers, upper- and lowercase letters, and underscore (_). 15 chars max.'));
+ return;
+ }
+
+ // Verify this is a real Twitter user.
+ if (!$this->verify_credentials($twitter_username, $twitter_password)) {
+ $this->show_form(_('Could not verify your Twitter credentials!'));
+ return;
+ }
+
+ // Now that we have a valid Twitter user, we have to make another api call to
+ // find its Twitter ID. Dumb, but true.
+ $twitter_id = $this->get_twitter_id($twitter_username, $twitter_password);
+
+ if (!$twitter_id) {
+ $this->show_form(sprintf(_('Unable to retrieve account information for "%s" from Twitter.'), $twitter_username));
+ return;
+ }
+
+ $fuser = DB_DataObject::factory('foreign_user');
+ $fuser->id = $twitter_id;
+ $fuser->service = 1; // Twitter
+ $fuser->uri = "http://www.twitter.com/$twitter_username";
+ $fuser->nickname = $twitter_username;
+ $fuser->created = common_sql_now();
+ $result = $fuser->insert();
+
+ if (!$result) {
+ common_log_db_error($fuser, 'INSERT', __FILE__);
+ $this->show_form(_('Unable to save your Twitter settings!'));
+ return;
+ }
+
+ $user = common_current_user();
+
+ $flink = DB_DataObject::factory('foreign_link');
+ $flink->user_id = $user->id;
+ $flink->foreign_id = $fuser->id;
+ $flink->service = 1; // Twitter
+ $flink->credentials = $twitter_password;
+ $flink->created = common_sql_now();
+
+ $this->set_flags($flink, $noticesync, $replysync, $friendsync);