Ejemplo n.º 1
0
 public function callback()
 {
     // Abort if denied
     if ($this->request->get("denied")) {
         $this->session->message('twitterDenied');
         throw $this->session->finalRedirect();
     }
     // Retrieve request token and secret from the session
     $session_oauth_token = $this->session->delete('twitter_oauth_token');
     $session_oauth_token_secret = $this->session->delete('twitter_oauth_token_secret');
     if (!$session_oauth_token || !$session_oauth_token_secret) {
         throw new HttpStatus\BadRequest("Missing Twitter session credentials");
     }
     // Verify request token
     $authToken = $this->request->get("oauth_token");
     $authTokenVerifier = $this->request->get("oauth_verifier");
     if (!$authToken) {
         throw new HttpStatus\BadRequest('Missing oauth_token parameter in Twitter callback.');
     }
     if ($authToken !== $session_oauth_token) {
         throw new HttpStatus\BadRequest("Mismatched Twitter auth tokens: {$authToken} / {$session_oauth_token}");
     }
     // Step 3 - Exchange request token stored in the session for an oAuth token and secret.
     // http://oauth.net/core/1.0a/#auth_step3
     try {
         $twitter = new Twitter();
         $twitter->setCredentials($session_oauth_token, $session_oauth_token_secret);
         $accessTokenParams = $twitter->getAccessToken($authTokenVerifier);
     } catch (TwitterException $e) {
         throw HttpStatus\Base::mapAuthException($e);
     }
     // Store access token and secret in the session
     $finalToken = $accessTokenParams['oauth_token'];
     $finalSecret = $accessTokenParams['oauth_token_secret'];
     $authedTwitterUserId = $accessTokenParams['user_id'];
     $authedTwitterUserName = $accessTokenParams['screen_name'];
     // Now we decide what to do with our brand new token
     // a) Already logged in: done
     if ($this->session->isLoggedIn()) {
         throw $this->session->finalRedirect();
     }
     // b) Existing user linked to this Twitter account: Save Twitter account details and sign in
     $authedPerson = new Model\Person();
     if ($authedPerson->loadByTwitter_Id($authedTwitterUserId)) {
         $authedPerson->twitter_name = $authedTwitterUserName;
         $authedPerson->setTwitterCredentials($finalToken, $finalSecret);
         $authedPerson->save();
         throw $this->session->signIn($authedPerson);
     }
     // c) No account: Create and link
     $newPerson = new Model\Person();
     $newPerson->twitter_id = $authedTwitterUserId;
     $newPerson->twitter_name = $authedTwitterUserName;
     $newPerson->setTwitterCredentials($finalToken, $finalSecret);
     $newPerson->save();
     // Sign in and throw the final redirect
     throw $this->session->signIn($newPerson);
 }
Ejemplo n.º 2
0
 public function send($url, $method = 'GET', $requestParams = array(), $headers = array())
 {
     // Initialise curl
     $this->setHeaders($headers);
     curl_setopt_array($this->curl, array(CURLOPT_FOLLOWLOCATION => $this->followLocation, CURLOPT_COOKIE => $this->cookieString, CURLOPT_CUSTOMREQUEST => $method));
     $this->response_headers = array();
     // Build the request
     switch ($method) {
         case 'POST':
             $this->preparePost($requestParams);
             break;
         case 'PUT':
             $this->preparePut($requestParams);
             break;
         case 'DELETE':
             $this->prepareDelete($requestParams);
             break;
         case 'HEAD':
             $url = $this->prepareHead($url, $requestParams);
             break;
         case 'GET':
             $url = $this->prepareGet($url, $requestParams);
             break;
         default:
             throw new HttpRequestException(HttpStatus\Base::mapCodeToStatus(501), $method, $url, $requestParams, '', array(), "The HttpRequest class doesn’t know how to make {$method} requests");
     }
     curl_setopt($this->curl, CURLOPT_URL, $url);
     // Send request response
     $response = curl_exec($this->curl);
     $httpInfo = curl_getinfo($this->curl);
     $httpInfo['response_headers'] = $this->response_headers;
     // echo("$response\n==============\nurl: $url\nparams: " . Url::encodePairsToString($requestParams) . "\n");
     // if (isset($httpInfo['request_header'])) {
     //     echo("==============\n{$httpInfo['request_header']}\n");
     // }
     self::$lastRequestInfo = $httpInfo;
     $httpCode = $httpInfo['http_code'];
     // Store cookies
     if (isset($this->response_headers[$url]['set_cookie'])) {
         $this->response_headers[$url]['set_cookie'] = (array) $this->response_headers[$url]['set_cookie'];
         foreach ($this->response_headers[$url]['set_cookie'] as $cookie) {
             $cookieParts = explode(';', $cookie);
             $cookieKV = explode('=', $cookieParts[0]);
             $this->cookies[$cookieKV[0]] = $cookieKV[1];
             $this->cookieString .= "{$cookieKV[0]}={$cookieKV[1]}; ";
         }
     }
     $httpInfo['cookies'] = $this->cookies;
     $httpInfo['cookieString'] = $this->cookieString;
     // Throw exception for errors
     if ($response === false) {
         // cURL error
         $curlError = "cURL error: " . curl_error($this->curl) . " (" . curl_errno($this->curl) . ")";
         throw new HttpRequestException(HttpStatus\Base::mapCodeToStatus(504), $method, $url, $requestParams, null, null, $curlError);
     }
     if (!$this->followLocation && $httpCode > 300 && $httpCode < 400) {
         // Helpfully extract the location header
         if (isset($this->response_headers[$url]) && isset($this->response_headers[$url]['location'])) {
             $httpInfo['location_header'] = $this->response_headers[$url]['location'];
         }
     } else {
         if ($httpCode !== 200) {
             $message = '';
             if (!($httpErrorClass = HttpStatus\Base::mapCodeToStatus($httpCode))) {
                 $httpErrorClass = HttpStatus\Base::mapCodeToStatus(502);
                 // BadGateway
                 $message = "Unhandled HTTP Error: {$httpCode}";
             }
             throw new HttpRequestException($httpErrorClass, $method, $url, $requestParams, $response, $this->response_headers[$url], $message);
         }
     }
     // Close handle
     curl_close($this->curl);
     // Return response data
     return array($response, $httpInfo);
 }
Ejemplo n.º 3
0
 public function __construct($message = null, Exception $previous = null)
 {
     parent::__construct($message, $this->code, $previous);
 }