function twitterLogin() { $request = $_REQUEST; $site = $this->siteUrl(); $callBackUrl = $this->callBackUrl(); $response = new stdClass(); $exploder = explode('_', $_GET['apsl_login_id']); $action = $exploder[1]; @session_start(); $options = get_option(APSL_SETTINGS); if ($action == 'login') { // Get identity from user and redirect browser to OpenID Server if (!isset($request['oauth_token']) || $request['oauth_token'] == '') { $twitterObj = new TwitterOAuth($options['apsl_twitter_settings']['apsl_twitter_api_key'], $options['apsl_twitter_settings']['apsl_twitter_api_secret']); $encoded_url = isset($_GET['redirect_to']) ? $_GET['redirect_to'] : ''; if (isset($encoded_url) && $encoded_url != '') { $callback = $callBackUrl . 'apsl_login_id' . '=twitter_check&redirect_to=' . $encoded_url; } else { $callback = $callBackUrl . 'apsl_login_id' . '=twitter_check'; } $request_token = $twitterObj->getRequestToken($callback); $_SESSION['oauth_twitter'] = array(); /* Save temporary credentials to session. */ $_SESSION['oauth_twitter']['oauth_token'] = $token = $request_token['oauth_token']; $_SESSION['oauth_twitter']['oauth_token_secret'] = $request_token['oauth_token_secret']; /* If last connection failed don't display authorization link. */ switch ($twitterObj->http_code) { case 200: try { $url = $twitterObj->getAuthorizeUrl($token); $this->redirect($url); } catch (Exception $e) { $response->status = 'ERROR'; $response->error_code = 2; $response->error_message = 'Could not get AuthorizeUrl.'; } break; default: $response->status = 'ERROR'; $response->error_code = 2; $response->error_message = 'Could not connect to Twitter. Refresh the page or try again later.'; break; } } else { $response->status = 'ERROR'; $response->error_code = 2; $response->error_message = 'INVALID AUTHORIZATION'; } } else { if (isset($request['oauth_token']) && isset($request['oauth_verifier'])) { /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */ $twitterObj = new TwitterOAuth($options['apsl_twitter_settings']['apsl_twitter_api_key'], $options['apsl_twitter_settings']['apsl_twitter_api_secret'], $_SESSION['oauth_twitter']['oauth_token'], $_SESSION['oauth_twitter']['oauth_token_secret']); /* Remove no longer needed request tokens */ unset($_SESSION['oauth_twitter']); try { $access_token = $twitterObj->getAccessToken($request['oauth_verifier']); /* If HTTP response is 200 continue otherwise send to connect page to retry */ if (200 == $twitterObj->http_code) { $user_profile = $twitterObj->get('users/show', array('screen_name' => $access_token['screen_name'], 'include_entities' => true)); /* Request access twitterObj from twitter */ $response->status = 'SUCCESS'; $response->deuid = $user_profile->id; $response->deutype = 'twitter'; $response->name = explode(' ', $user_profile->name, 2); $response->first_name = $response->name[0]; $response->last_name = isset($response->name[1]) ? $response->name[1] : ''; $response->deuimage = $user_profile->profile_image_url_https; $response->email = $user_profile->screen_name . '@twitter.com'; $response->username = $user_profile->screen_name . '@twitter.com'; $response->url = $user_profile->url; $response->about = $user_profile->description; $response->gender = $user_profile->gender; $response->location = $user_profile->location; $response->error_message = ''; } else { $response->status = 'ERROR'; $response->error_code = 2; $response->error_message = 'Could not connect to Twitter. Refresh the page or try again later.'; } } catch (Exception $e) { $response->status = 'ERROR'; $response->error_code = 2; $response->error_message = 'Could not get AccessToken.'; } } else { // User Canceled your Request $response->status = 'ERROR'; $response->error_code = 1; $response->error_message = "USER CANCELED REQUEST"; } } return $response; }