예제 #1
0
 /**
  * @param \RainLoop\Model\Account|null $oAccount
  * @param string $sEmail
  * @param string $sPassword
  *
  * @return int
  */
 private function loginProcess(&$oAccount, $sEmail, $sPassword)
 {
     $iErrorCode = \RainLoop\Notifications::UnknownError;
     try {
         $oAccount = $this->oActions->LoginProcess($sEmail, $sPassword);
         if ($oAccount instanceof \RainLoop\Model\Account) {
             $this->oActions->AuthToken($oAccount);
             $iErrorCode = 0;
         } else {
             $oAccount = null;
             $iErrorCode = \RainLoop\Notifications::AuthError;
         }
     } catch (\RainLoop\Exceptions\ClientException $oException) {
         $iErrorCode = $oException->getCode();
     } catch (\Exception $oException) {
         unset($oException);
         $iErrorCode = \RainLoop\Notifications::UnknownError;
     }
     return $iErrorCode;
 }
 /**
  * @return string
  */
 public function ServiceExternalLogin()
 {
     $this->oHttp->ServerNoCache();
     $oException = null;
     $oAccount = null;
     $bLogout = true;
     if ($this->oActions->Config()->Get('labs', 'allow_external_login', false)) {
         $sEmail = \trim($this->oHttp->GetRequest('Email', ''));
         $sPassword = $this->oHttp->GetRequest('Password', '');
         try {
             $oAccount = $this->oActions->LoginProcess($sEmail, $sPassword);
             $this->oActions->AuthToken($oAccount);
             $bLogout = !$oAccount instanceof \RainLoop\Model\Account;
         } catch (\Exception $oException) {
             $this->oActions->Logger()->WriteException($oException);
         }
         if ($bLogout) {
             $this->oActions->SetAuthLogoutToken();
         }
     }
     switch (\strtolower($this->oHttp->GetRequest('Output', 'Redirect'))) {
         case 'json':
             @\header('Content-Type: application/json; charset=utf-8');
             $aResult = array('Action' => 'ExternalLogin', 'Result' => $oAccount instanceof \RainLoop\Model\Account ? true : false, 'ErrorCode' => 0);
             if (!$aResult['Result']) {
                 if ($oException instanceof \RainLoop\Exceptions\ClientException) {
                     $aResult['ErrorCode'] = $oException->getCode();
                 } else {
                     $aResult['ErrorCode'] = \RainLoop\Notifications::AuthError;
                 }
             }
             return \MailSo\Base\Utils::Php2js($aResult, $this->Logger());
         case 'redirect':
         default:
             $this->oActions->Location('./');
             break;
     }
     return '';
 }
예제 #3
0
 /**
  * @return string
  */
 public function TwitterPopupService()
 {
     $sResult = '';
     $sLoginUrl = '';
     $sSocialName = '';
     $bLogin = false;
     $iErrorCode = \RainLoop\Notifications::UnknownError;
     $sRedirectUrl = $this->oHttp->GetFullUrl() . '?SocialTwitter';
     if (0 < strlen($this->oActions->GetSpecAuthToken())) {
         $sRedirectUrl .= '&rlah=' . $this->oActions->GetSpecAuthToken();
     } else {
         if ($this->oHttp->HasQuery('rlah')) {
             $this->oActions->SetSpecAuthToken($this->oHttp->GetQuery('rlah', ''));
             $sRedirectUrl .= '&rlah=' . $this->oActions->GetSpecAuthToken();
         }
     }
     try {
         $oTwitter = $this->TwitterConnector();
         if ($oTwitter) {
             $sSessionKey = \implode('_', array('twitter', \md5($oTwitter->config['consumer_secret']), \md5(\RainLoop\Utils::GetConnectionToken()), 'AuthSessionData'));
             $oAccount = $this->oActions->GetAccount();
             if ($oAccount) {
                 if (isset($_REQUEST['oauth_verifier'])) {
                     $sAuth = $this->oActions->Cacher()->Get($sSessionKey);
                     $oAuth = $sAuth ? \json_decode($sAuth, true) : null;
                     if ($oAuth && !empty($oAuth['oauth_token']) && !empty($oAuth['oauth_token_secret'])) {
                         $oTwitter->config['user_token'] = $oAuth['oauth_token'];
                         $oTwitter->config['user_secret'] = $oAuth['oauth_token_secret'];
                         $iCode = $oTwitter->request('POST', $oTwitter->url('oauth/access_token', ''), array('oauth_callback' => $sRedirectUrl, 'oauth_verifier' => $_REQUEST['oauth_verifier']));
                         if (200 === $iCode && isset($oTwitter->response['response'])) {
                             $this->oActions->Logger()->WriteDump($oTwitter->response['response']);
                             $aAccessToken = $oTwitter->extract_params($oTwitter->response['response']);
                             $this->oActions->Logger()->WriteDump($aAccessToken);
                             if ($aAccessToken && isset($aAccessToken['oauth_token']) && !empty($aAccessToken['user_id'])) {
                                 $oTwitter->config['user_token'] = $aAccessToken['oauth_token'];
                                 $oTwitter->config['user_secret'] = $aAccessToken['oauth_token_secret'];
                                 $sSocialName = !empty($aAccessToken['screen_name']) ? '@' . $aAccessToken['screen_name'] : $aAccessToken['user_id'];
                                 $sSocialName = \trim($sSocialName);
                                 $aUserData = array('Email' => $oAccount->Email(), 'Password' => $oAccount->Password());
                                 $oSettings = $this->oActions->SettingsProvider()->Load($oAccount);
                                 $oSettings->SetConf('TwitterAccessToken', \RainLoop\Utils::EncodeKeyValues($aAccessToken));
                                 $oSettings->SetConf('TwitterSocialName', $sSocialName);
                                 $this->oActions->SettingsProvider()->Save($oAccount, $oSettings);
                                 $this->oActions->StorageProvider()->Put(null, \RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY, $this->TwitterUserLoginStorageKey($oTwitter, $aAccessToken['user_id']), \RainLoop\Utils::EncodeKeyValues($aUserData));
                                 $iErrorCode = 0;
                             }
                         }
                     }
                 } else {
                     $aParams = array('oauth_callback' => $sRedirectUrl, 'x_auth_access_type' => 'read');
                     $iCode = $oTwitter->request('POST', $oTwitter->url('oauth/request_token', ''), $aParams);
                     if (200 === $iCode && isset($oTwitter->response['response'])) {
                         $oAuth = $oTwitter->extract_params($oTwitter->response['response']);
                         if (!empty($oAuth['oauth_token'])) {
                             $this->oActions->Cacher()->Set($sSessionKey, \json_encode($oAuth));
                             $sLoginUrl = $oTwitter->url('oauth/authenticate', '') . '?oauth_token=' . $oAuth['oauth_token'];
                         }
                     }
                 }
             } else {
                 $bLogin = true;
                 if (isset($_REQUEST['oauth_verifier'])) {
                     $sAuth = $this->oActions->Cacher()->Get($sSessionKey);
                     $oAuth = $sAuth ? \json_decode($sAuth, true) : null;
                     if ($oAuth && !empty($oAuth['oauth_token']) && !empty($oAuth['oauth_token_secret'])) {
                         $oTwitter->config['user_token'] = $oAuth['oauth_token'];
                         $oTwitter->config['user_secret'] = $oAuth['oauth_token_secret'];
                         $iCode = $oTwitter->request('POST', $oTwitter->url('oauth/access_token', ''), array('oauth_callback' => $sRedirectUrl, 'oauth_verifier' => $_REQUEST['oauth_verifier']));
                         if (200 === $iCode && isset($oTwitter->response['response'])) {
                             $aAccessToken = $oTwitter->extract_params($oTwitter->response['response']);
                             if ($aAccessToken && isset($aAccessToken['oauth_token']) && !empty($aAccessToken['user_id'])) {
                                 $sUserData = $this->oActions->StorageProvider()->Get(null, \RainLoop\Providers\Storage\Enumerations\StorageType::NOBODY, $this->TwitterUserLoginStorageKey($oTwitter, $aAccessToken['user_id']));
                                 $aUserData = \RainLoop\Utils::DecodeKeyValues($sUserData);
                                 if ($aUserData && \is_array($aUserData) && !empty($aUserData['Email']) && isset($aUserData['Password'])) {
                                     $oAccount = $this->oActions->LoginProcess($aUserData['Email'], $aUserData['Password']);
                                     if ($oAccount instanceof \RainLoop\Account) {
                                         $this->oActions->AuthProcess($oAccount);
                                         $iErrorCode = 0;
                                     }
                                 } else {
                                     $iErrorCode = \RainLoop\Notifications::SocialTwitterLoginAccessDisable;
                                 }
                                 $this->oActions->Cacher()->Delete($sSessionKey);
                             }
                         }
                     }
                 } else {
                     $aParams = array('oauth_callback' => $sRedirectUrl, 'x_auth_access_type' => 'read');
                     $iCode = $oTwitter->request('POST', $oTwitter->url('oauth/request_token', ''), $aParams);
                     if (200 === $iCode && isset($oTwitter->response['response'])) {
                         $oAuth = $oTwitter->extract_params($oTwitter->response['response']);
                         if (!empty($oAuth['oauth_token'])) {
                             $this->oActions->Cacher()->Set($sSessionKey, \json_encode($oAuth));
                             $sLoginUrl = $oTwitter->url('oauth/authenticate', '') . '?oauth_token=' . $oAuth['oauth_token'];
                         }
                     }
                 }
             }
         }
     } catch (\Exception $oException) {
         $this->oActions->Logger()->WriteException($oException, \MailSo\Log\Enumerations\Type::ERROR);
     }
     if ($sLoginUrl) {
         $this->oActions->Location($sLoginUrl);
     } else {
         @\header('Content-Type: text/html; charset=utf-8');
         $sCallBackType = $bLogin ? '_login' : '';
         $sConnectionFunc = 'rl_' . \md5(\RainLoop\Utils::GetConnectionToken()) . '_twitter' . $sCallBackType . '_service';
         $sResult = '<script type="text/javascript" data-cfasync="false">opener && opener.' . $sConnectionFunc . ' && opener.' . $sConnectionFunc . '(' . $iErrorCode . '); self && self.close && self.close();</script>';
     }
     return $sResult;
 }