/**
  *
  *
  * @param EntryController $Sender
  * @param array $Args
  */
 public function entryController_openID_create($Sender, $Args)
 {
     $this->EventArguments = $Args;
     try {
         $OpenID = $this->getOpenID();
     } catch (Gdn_UserException $ex) {
         $Sender->Form->addError('@' . $ex->getMessage());
         $Sender->render('Url', '', 'plugins/OpenID');
     }
     $Mode = $Sender->Request->get('openid_mode');
     switch ($Mode) {
         case 'cancel':
             $Sender->render('Cancel', '', 'plugins/OpenID');
             break;
         case 'id_res':
             if ($OpenID->validate()) {
                 $Attributes = $OpenID->getAttributes();
                 print_r($_GET);
             }
             break;
         default:
             if (!$OpenID->identity) {
                 $Sender->CssClass = 'Dashboard Entry connect';
                 $Sender->setData('Title', t('Sign In with OpenID'));
                 $Sender->render('Url', '', 'plugins/OpenID');
             } else {
                 try {
                     $Url = $OpenID->authUrl();
                     redirect($Url);
                 } catch (Exception $Ex) {
                     $Sender->Form->addError($Ex);
                     $Sender->render('Url', '', 'plugins/OpenID');
                 }
             }
             break;
     }
 }
 /**
  *
  *
  * @param EntryController $Sender
  * @param array $Args
  */
 public function base_connectData_handler($Sender, $Args)
 {
     if (val(0, $Args) != 'disqus') {
         return;
     }
     if (isset($_GET['error'])) {
         throw new Gdn_UserException(val('error_description', $_GET, t('There was an error connecting to Disqus')));
     }
     $Provider = $this->provider();
     if (!$Provider) {
         throw new Gdn_UserException('The Disqus plugin has not been configured correctly.');
     }
     $AppID = $Provider['AuthenticationKey'];
     $Secret = $Provider['AssociationSecret'];
     $Code = val('code', $_GET);
     $Query = '';
     if ($Sender->Request->get('display')) {
         $Query = 'display=' . urlencode($Sender->Request->get('display'));
     }
     $RedirectUri = concatSep('&', $this->redirectUri(), $Query);
     $Form = $Sender->Form;
     $AccessToken = $Form->getFormValue('AccessToken');
     //Gdn::Session()->Stash('Disqus.AccessToken', NULL, NULL);
     // Get the access token.
     if ($Code && !$AccessToken) {
         // Exchange the token for an access token.
         $Qs = array('grant_type' => 'authorization_code', 'client_id' => $AppID, 'client_secret' => $Secret, 'redirect_uri' => $RedirectUri, 'code' => $Code);
         $Url = 'https://disqus.com/api/oauth/2.0/access_token/';
         //.http_build_query($Qs);
         // Get the redirect URI.
         $C = curl_init();
         curl_setopt($C, CURLOPT_POST, true);
         curl_setopt($C, CURLOPT_POSTFIELDS, $Qs);
         curl_setopt($C, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($C, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($C, CURLOPT_URL, $Url);
         $Contents = curl_exec($C);
         $Info = curl_getinfo($C);
         if (strpos(val('content_type', $Info, ''), '/json') !== false) {
             $Tokens = json_decode($Contents, true);
         } else {
             parse_str($Contents, $Tokens);
         }
         if (val('error', $Tokens)) {
             throw new Gdn_UserException('Disqus returned the following error: ' . valr('error.message', $Tokens, 'Unknown error.'), 400);
         }
         $AccessToken = val('access_token', $Tokens);
         $Expires = val('expires_in', $Tokens, null);
         $Form->addHidden('AccessToken', $AccessToken);
     }
     if ($AccessToken) {
         // Grab the user's profile.
         $Qs = array('access_token' => $AccessToken, 'api_key' => $AppID, 'api_secret' => $Secret);
         $Url = 'https://disqus.com/api/3.0/users/details.json?' . http_build_query($Qs);
         $C = curl_init();
         curl_setopt($C, CURLOPT_RETURNTRANSFER, true);
         curl_setopt($C, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($C, CURLOPT_URL, $Url);
         $Contents = curl_exec($C);
         $Info = curl_getinfo($C);
         if (strpos(val('content_type', $Info, ''), '/json') !== false) {
             $Profile = json_decode($Contents, true);
             $Profile = $Profile['response'];
         } else {
             throw new Gdn_UserException('There was an error trying to get your profile information from Disqus.');
         }
     } else {
         throw new Gdn_UserException('There was an error trying to get an access token from Disqus.');
     }
     $Form->setFormValue('UniqueID', val('id', $Profile));
     $Form->setFormValue('Provider', 'disqus');
     $Form->setFormValue('ProviderName', 'Disqus');
     $Form->setFormValue('FullName', val('name', $Profile));
     $Form->setFormValue('Name', val('username', $Profile));
     $Form->setFormValue('Photo', valr('avatar.permalink', $Profile));
     $Sender->setData('Verified', true);
 }
 /**
  *
  *
  * @param EntryController $Sender
  * @param array $Args
  */
 public function base_connectData_handler($Sender, $Args)
 {
     if (val(0, $Args) != 'googleplus') {
         return;
     }
     // Grab the google plus profile from the session staff.
     $GooglePlus = Gdn::session()->stash(self::ProviderKey, '', false);
     $AccessToken = val('AccessToken', $GooglePlus);
     $Profile = val('Profile', $GooglePlus);
     $Form = $Sender->Form;
     $Form->setFormValue('UniqueID', val('id', $Profile));
     $Form->setFormValue('Provider', self::ProviderKey);
     $Form->setFormValue('ProviderName', 'Google+');
     $Form->setFormValue('FullName', val('name', $Profile));
     $Form->setFormValue('Email', val('email', $Profile));
     if (c('Plugins.GooglePlus.UseAvatars', true)) {
         $Form->setFormValue('Photo', val('picture', $Profile));
     }
     if (c('Plugins.GooglePlus.UseFullNames')) {
         $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);
     $this->EventArguments['Form'] = $Form;
     $this->fireEvent('AfterConnectData');
 }
 /**
  * An intermediate page for jsConnect that checks SSO against and then posts the information to /entry/connect.
  *
  * @param EntryController $Sender
  * @param string $Action A specific action. It can be one of the following:
  *
  * - blank: The default action.
  * - guest: There is no user signed in.
  * -
  * @param string $Target The url to redirect to after a successful connect.
  * @throws /Exception Throws an exception when the jsConnect provider is not found.
  */
 public function entryController_jsConnect_create($Sender, $Action = '', $Target = '')
 {
     $Sender->setData('_NoMessages', true);
     if ($Action) {
         if ($Action == 'guest') {
             $Sender->addDefinition('CheckPopup', true);
             $Target = $Sender->Form->getFormValue('Target', '/');
             $Sender->RedirectUrl = $Target;
             $Sender->render('JsConnect', '', 'plugins/jsconnect');
         } else {
             parse_str($Sender->Form->getFormValue('JsConnect'), $JsData);
             $Error = val('error', $JsData);
             $Message = val('message', $JsData);
             if ($Error === 'timeout' && !$Message) {
                 $Message = t('Your sso timed out.', 'Your sso timed out during the request. Please try again.');
             }
             Gdn::dispatcher()->passData('Exception', $Message ? htmlspecialchars($Message) : htmlspecialchars($Error))->dispatch('home/error');
         }
     } else {
         $client_id = $Sender->setData('client_id', $Sender->Request->get('client_id', 0));
         $Provider = self::getProvider($client_id);
         if (empty($Provider)) {
             throw NotFoundException('Provider');
         }
         $Get = arrayTranslate($Sender->Request->get(), ['client_id', 'display']);
         $Sender->addDefinition('JsAuthenticateUrl', self::connectUrl($Provider, true));
         $Sender->addJsFile('jsconnect.js', 'plugins/jsconnect');
         $Sender->setData('Title', t('Connecting...'));
         $Sender->Form->Action = url('/entry/connect/jsconnect?' . http_build_query($Get));
         $Sender->Form->addHidden('JsConnect', '');
         $Sender->Form->addHidden('Target', $Target);
         $Sender->MasterView = 'empty';
         $Sender->Render('JsConnect', '', 'plugins/jsconnect');
     }
 }