/** * * @param Gdn_Controller $Sender * @param array $Args */ public function base_connectData_handler($Sender, $Args) { if (val(0, $Args) != 'facebook') { return; } if (isset($_GET['error'])) { // TODO global nope x2 throw new Gdn_UserException(val('error_description', $_GET, t('There was an error connecting to Facebook'))); } $AppID = c('Plugins.Facebook.ApplicationID'); $Secret = c('Plugins.Facebook.Secret'); $Code = val('code', $_GET); // TODO nope $Query = ''; if ($Sender->Request->get('display')) { $Query = 'display=' . urlencode($Sender->Request->get('display')); } $RedirectUri = concatSep('&', $this->redirectUri(), $Query); $AccessToken = $Sender->Form->getFormValue('AccessToken'); // Get the access token. if (!$AccessToken && $Code) { // Exchange the token for an access token. $Code = urlencode($Code); $AccessToken = $this->getAccessToken($Code, $RedirectUri); $NewToken = true; } // Get the profile. try { $Profile = $this->getProfile($AccessToken); } catch (Exception $Ex) { if (!isset($NewToken)) { // There was an error getting the profile, which probably means the saved access token is no longer valid. Try and reauthorize. if ($Sender->deliveryType() == DELIVERY_TYPE_ALL) { redirect($this->authorizeUri()); } else { $Sender->setHeader('Content-type', 'application/json'); $Sender->deliveryMethod(DELIVERY_METHOD_JSON); $Sender->RedirectUrl = $this->authorizeUri(); } } else { $Sender->Form->addError('There was an error with the Facebook connection.'); } } $Form = $Sender->Form; //new Gdn_Form(); $ID = val('id', $Profile); $Form->setFormValue('UniqueID', $ID); $Form->setFormValue('Provider', self::ProviderKey); $Form->setFormValue('ProviderName', 'Facebook'); $Form->setFormValue('FullName', val('name', $Profile)); $Form->setFormValue('Email', val('email', $Profile)); $Form->setFormValue('Photo', "//graph.facebook.com/{$ID}/picture?width=200&height=200"); $Form->addHidden('AccessToken', $AccessToken); if (c('Plugins.Facebook.UseFacebookNames')) { $Form->setFormValue('Name', val('name', $Profile)); saveToConfig(array('Garden.User.ValidationRegex' => UserModel::USERNAME_REGEX_MIN, 'Garden.User.ValidationLength' => '{3,50}', 'Garden.Registration.NameUnique' => false), '', false); } // Save some original data in the attributes of the connection for later API calls. $Attributes = array(); $Attributes[self::ProviderKey] = array('AccessToken' => $AccessToken, 'Profile' => $Profile); $Form->setFormValue('Attributes', $Attributes); $Sender->setData('Verified', true); }
/** * * * @param Gdn_Controller $Sender * @param array $Args */ public function base_connectData_handler($Sender, $Args) { if (val(0, $Args) != 'twitter') { return; } $Form = $Sender->Form; //new Gdn_Form(); $RequestToken = val('oauth_token', $_GET); $AccessToken = $Form->getFormValue('AccessToken'); if ($AccessToken) { $AccessToken = $this->getOAuthToken($AccessToken); $this->accessToken($AccessToken); } // Get the access token. if ($RequestToken && !$AccessToken) { // Get the request secret. $RequestToken = $this->getOAuthToken($RequestToken); $Consumer = new OAuthConsumer(c('Plugins.Twitter.ConsumerKey'), c('Plugins.Twitter.Secret')); $Url = 'https://api.twitter.com/oauth/access_token'; $Params = array('oauth_verifier' => val('oauth_verifier', $_GET)); $Request = OAuthRequest::from_consumer_and_token($Consumer, $RequestToken, 'POST', $Url, $Params); $SignatureMethod = new OAuthSignatureMethod_HMAC_SHA1(); $Request->sign_request($SignatureMethod, $Consumer, $RequestToken); $Post = $Request->to_postdata(); $Curl = $this->_Curl($Request); $Response = curl_exec($Curl); if ($Response === false) { $Response = curl_error($Curl); } $HttpCode = curl_getinfo($Curl, CURLINFO_HTTP_CODE); curl_close($Curl); if ($HttpCode == '200') { $Data = OAuthUtil::parse_parameters($Response); $AccessToken = new OAuthToken(val('oauth_token', $Data), val('oauth_token_secret', $Data)); // Save the access token to the database. $this->setOAuthToken($AccessToken->key, $AccessToken->secret, 'access'); $this->accessToken($AccessToken->key, $AccessToken->secret); // Delete the request token. $this->deleteOAuthToken($RequestToken); } else { // There was some sort of error. throw new Exception('There was an error authenticating with twitter.', 400); } $NewToken = true; } // Get the profile. try { $Profile = $this->getProfile($AccessToken); } catch (Exception $Ex) { if (!isset($NewToken)) { // There was an error getting the profile, which probably means the saved access token is no longer valid. Try and reauthorize. if ($Sender->deliveryType() == DELIVERY_TYPE_ALL) { redirect($this->_AuthorizeHref()); } else { $Sender->setHeader('Content-type', 'application/json'); $Sender->deliveryMethod(DELIVERY_METHOD_JSON); $Sender->RedirectUrl = $this->_authorizeHref(); } } else { throw $Ex; } } $ID = val('id', $Profile); $Form->setFormValue('UniqueID', $ID); $Form->setFormValue('Provider', self::ProviderKey); $Form->setFormValue('ProviderName', 'Twitter'); $Form->setValue('ConnectName', val('screen_name', $Profile)); $Form->setFormValue('Name', val('screen_name', $Profile)); $Form->setFormValue('FullName', val('name', $Profile)); $Form->setFormValue('Photo', val('profile_image_url_https', $Profile)); $Form->addHidden('AccessToken', $AccessToken->key); // Save some original data in the attributes of the connection for later API calls. $Attributes = array(self::ProviderKey => array('AccessToken' => array($AccessToken->key, $AccessToken->secret), 'Profile' => $Profile)); $Form->setFormValue('Attributes', $Attributes); $Sender->setData('Verified', true); }