/**
  * Login or create a new user using whatever data we get from Twitter.
  */
 function handle_twitter_login()
 {
     header("Content-type: application/json");
     $resp = array("status" => 0);
     $secret = @$_POST['secret'];
     $data_str = @$_POST['data'];
     $data_str = '?' == substr($data_str, 0, 1) ? substr($data_str, 1) : $data_str;
     $data = array();
     parse_str($data_str, $data);
     if (!$data) {
         die(json_encode($resp));
     }
     $twitter = $this->_get_twitter_object($data['oauth_token'], $secret);
     /* --- Start delta time correction --- */
     if (method_exists('OAuthRequest', 'generate_raw_timestamp')) {
         $test_time = OAuthRequest::generate_raw_timestamp();
         $test_url = "https://api.twitter.com/1/help/test.json";
         $request = wp_remote_get($test_url, array('sslverify' => false));
         $headers = wp_remote_retrieve_headers($request);
         if (!empty($headers['date'])) {
             $twitter_time = strtotime($headers['date']);
             $delta = $twitter_time - $test_time;
             if (abs($delta) > EAB_OAUTH_TIMESTAMP_DELTA_THRESHOLD) {
                 add_action('eab-oauth-twitter-generate_timestamp', create_function('$time', 'return $time + ' . $delta . ';'));
             }
         }
     }
     /* --- End delta time correction --- */
     $access = $twitter->getAccessToken($data['oauth_verifier']);
     $twitter = $this->_get_twitter_object($access['oauth_token'], $access['oauth_token_secret']);
     $tw_user = $twitter->get('account/verify_credentials');
     // Have user, now register him/her
     $domain = preg_replace('/www\\./', '', parse_url(site_url(), PHP_URL_HOST));
     $username = preg_replace('/[^_0-9a-z]/i', '_', strtolower($tw_user->name));
     $email = $username . '@twitter.' . $domain;
     //STUB email
     $wordp_user = get_user_by('email', $email);
     if (!$wordp_user) {
         // Not an existing user, let's create a new one
         $password = wp_generate_password(12, false);
         $count = 0;
         while (username_exists($username)) {
             $username .= rand(0, 9);
             if (++$count > 10) {
                 break;
             }
         }
         $wordp_user = wp_create_user($username, $password, $email);
         if (is_wp_error($wordp_user)) {
             die(json_encode($resp));
         } else {
             list($first_name, $last_name) = explode(' ', @$tw_user->name, 2);
             update_user_meta($wordp_user, 'first_name', $first_name);
             update_user_meta($wordp_user, 'last_name', $last_name);
         }
     } else {
         $wordp_user = $wordp_user->ID;
     }
     update_user_meta($wordp_user, '_eab_tw', array('id' => $tw_user->id, 'avatar' => $tw_user->profile_image_url, 'token' => $access));
     do_action('eab-user_logged_in-twitter', $wordp_user, $tw_user->id, $tw_user->profile_image_url, $access);
     $user = get_userdata($wordp_user);
     wp_set_current_user($user->ID, $user->user_login);
     wp_set_auth_cookie($user->ID);
     // Logged in with Twitter, yay
     do_action('wp_login', $user->user_login);
     die(json_encode(array("status" => 1)));
 }