Beispiel #1
0
	public function Authorize()
	{
		$GLOBALS["APPLICATION"]->RestartBuffer();
		$bSuccess = 1;
		if((isset($_REQUEST["code"]) && $_REQUEST["code"] <> '') && CSocServAuthManager::CheckUniqueKey())
		{
			$redirect_uri = CSocServUtil::ServerName()."/bitrix/tools/oauth/google.php";
			$appID = trim(self::GetOption("google_appid"));
			$appSecret = trim(self::GetOption("google_appsecret"));

			$gAuth = new CGoogleOAuthInterface($appID, $appSecret, $_REQUEST["code"]);

			if($gAuth->GetAccessToken($redirect_uri) !== false)
			{
				$arGoogleUser = $gAuth->GetCurrentUser();

				if($arGoogleUser['feed']['author']['0']['email']['$t'] <> '')
				{
					$first_name = $last_name = "";
					if($arGoogleUser['feed']['author']['0']['name']['$t'] <> '')
					{
						$aName = explode(" ", $arGoogleUser['feed']['author']['0']['name']['$t']);
						$first_name = $aName[0];
						if(isset($aName[1]))
							$last_name = $aName[1];
					}
					$email = $arGoogleUser['feed']['author']['0']['email']['$t'];

					$arFields = array(
						'EXTERNAL_AUTH_ID' => self::ID,
						'XML_ID' => $email,
						'LOGIN' => "G_".$email,
						'EMAIL' => $email,
						'NAME'=> $first_name,
						'LAST_NAME'=> $last_name,
					);
					$arFields["PERSONAL_WWW"] = "https://accounts.google.com";
					$bSuccess = $this->AuthorizeUser($arFields);
				}
			}
		}
		$url = ($GLOBALS["APPLICATION"]->GetCurDir() == "/login/") ? "/auth/" : $GLOBALS["APPLICATION"]->GetCurDir();
		if(isset($_REQUEST["state"]))
		{
			$arState = array();
			parse_str($_REQUEST["state"], $arState);
		
			if(isset($arState['backurl']))
				$url = parse_url($arState['backurl'], PHP_URL_PATH);
		}
		$aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key");
		if(CModule::IncludeModule("socialnetwork"))
			$url = (preg_match("/\?/", $url)) ? $url."&current_fieldset=SOCSERV" : $url."?current_fieldset=SOCSERV";
		if($bSuccess !== true)
			$url = $GLOBALS['APPLICATION']->GetCurPageParam(('auth_service_id='.self::ID.'&auth_service_error='.$bSuccess), $aRemove);
		if($bSuccess === 2)
			$url = '/?auth_service_id='.self::ID.'&auth_service_error='.$bSuccess;
		echo '
<script type="text/javascript">
if(window.opener)
	window.opener.location = \''.CUtil::JSEscape($url).'\';
window.close();
</script>
';
		die();
	}
Beispiel #2
0
	public function Authorize()
	{
		$GLOBALS["APPLICATION"]->RestartBuffer();
		$bSuccess = false;
		if((isset($_REQUEST["code"]) && $_REQUEST["code"] <> '') && CSocServAuthManager::CheckUniqueKey())
		{
			$redirect_uri = CSocServUtil::ServerName()."/bitrix/tools/oauth/google.php";
			$appID = self::GetOption("google_appid");
			$appSecret = self::GetOption("google_appsecret");

			$gAuth = new CGoogleOAuthInterface($appID, $appSecret, $_REQUEST["code"]);

			if($gAuth->GetAccessToken($redirect_uri) !== false)
			{
				$arGoogleUser = $gAuth->GetCurrentUser();

				if($arGoogleUser['feed']['author']['0']['email']['$t'] <> '')
				{
					$first_name = $last_name = "";
					if($arGoogleUser['feed']['author']['0']['name']['$t'] <> '')
					{
						$aName = explode(" ", $arGoogleUser['feed']['author']['0']['name']['$t']);
						$first_name = $aName[0];
						if(isset($aName[1]))
							$last_name = $aName[1];
					}
					$email = $arGoogleUser['feed']['author']['0']['email']['$t'];

					$arFields = array(
						'EXTERNAL_AUTH_ID' => self::ID,
						'XML_ID' => $email,
						'LOGIN' => "G_".$email,
						'EMAIL' => $email,
						'NAME'=> $first_name,
						'LAST_NAME'=> $last_name,
					);
					$bSuccess = $this->AuthorizeUser($arFields);
				}
			}
		}
		$url = '/personal/profile/';
		if(isset($_REQUEST["state"]))
		{
			$arState = array();
			parse_str($_REQUEST["state"], $arState);
		
			if(isset($arState['backurl']))
				$url = parse_url($arState['backurl'], PHP_URL_PATH);
		}
		$aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key");
		if(!$bSuccess)
			$url = $GLOBALS['APPLICATION']->GetCurPageParam(('auth_service_id='.self::ID.'&auth_service_error=1'), $aRemove);
		echo '
<script type="text/javascript">
if(window.opener)
	window.opener.location = \''.CUtil::JSEscape($url).'\';
window.close();
</script>
';
		die();
	}
Beispiel #3
0
	public function Authorize()
	{
		global $APPLICATION;
		$APPLICATION->RestartBuffer();
		$bSuccess = SOCSERV_AUTHORISATION_ERROR;
		if((isset($_REQUEST["code"]) && $_REQUEST["code"] <> '') && CSocServAuthManager::CheckUniqueKey())
		{
			if(IsModuleInstalled('freetrix24') && defined('BX24_HOST_NAME'))
				$redirect_uri = self::CONTROLLER_URL."/redirect.php";
			else
				$redirect_uri = CSocServUtil::ServerName()."/freetrix/tools/oauth/google.php";

			$appID = trim(self::GetOption("google_appid"));
			$appSecret = trim(self::GetOption("google_appsecret"));

			$gAuth = new CGoogleOAuthInterface($appID, $appSecret, $_REQUEST["code"]);

			$this->entityOAuth = $gAuth;

			if($gAuth->GetAccessToken($redirect_uri) !== false)
			{
				$arGoogleUser = $gAuth->GetCurrentUser();

				if(is_array($arGoogleUser) && ($arGoogleUser['email'] <> ''))
				{
					$first_name = $last_name = $gender = "";
					if($arGoogleUser['name'] <> '')
					{
						$aName = explode(" ", $arGoogleUser['name']);
						if($arGoogleUser['given_name'] <> '')
							$first_name = $arGoogleUser['given_name'];
						else
							$first_name = $aName[0];
						if($arGoogleUser['family_name'] <> '')
							$last_name = $arGoogleUser['family_name'];
						elseif(isset($aName[1]))
							$last_name = $aName[1];
					}
					$email = $arGoogleUser['email'];
					if($arGoogleUser['gender'] <> '')
						if($arGoogleUser['gender'] == 'male')
							$gender = 'M';
						elseif($arGoogleUser['gender'] == 'female')
							$gender = 'F';

					$arFields = array(
						'EXTERNAL_AUTH_ID' => self::ID,
						'XML_ID' => $email,
						'LOGIN' => "G_".$email,
						'EMAIL' => $email,
						'NAME'=> $first_name,
						'LAST_NAME'=> $last_name
					);

					if($gender != "")
						$arFields['PERSONAL_GENDER'] = $gender;

					if(isset($arGoogleUser['picture']) && self::CheckPhotoURI($arGoogleUser['picture']))
						if($arPic = CFile::MakeFileArray($arGoogleUser['picture']))
							$arFields["PERSONAL_PHOTO"] = $arPic;

					$arFields["PERSONAL_WWW"] = $arGoogleUser['link'];

					if(isset($arGoogleUser['access_token']))
						$arFields["OATOKEN"] = $arGoogleUser['access_token'];

					if(isset($arGoogleUser['refresh_token']))
						$arFields["REFRESH_TOKEN"] = $arGoogleUser['refresh_token'];

					if(isset($arGoogleUser['expires_in']))
						$arFields["OATOKEN_EXPIRES"] = $arGoogleUser['expires_in'];

					if(strlen(SITE_ID) > 0)
						$arFields["SITE_ID"] = SITE_ID;
					$bSuccess = $this->AuthorizeUser($arFields);
				}
			}
		}
		$url = ($APPLICATION->GetCurDir() == "/login/") ? "" : $APPLICATION->GetCurDir();
		$aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key", "current_fieldset");

		$mode = 'opener';
		if(isset($_REQUEST["state"]))
		{
			$arState = array();
			parse_str($_REQUEST["state"], $arState);
			if(isset($arState['backurl']))
			{
				$parseUrl = parse_url($arState['backurl']);
				$urlPath = $parseUrl["path"];
				$arUrlQuery = explode('&', $parseUrl["query"]);

				foreach($arUrlQuery as $key => $value)
				{
					foreach($aRemove as $param)
					{
						if(strpos($value, $param."=") === 0)
						{
							unset($arUrlQuery[$key]);
							break;
						}
					}
				}

				$url = (!empty($arUrlQuery)) ? $urlPath.'?'.implode("&", $arUrlQuery) : $urlPath;
			}

			if(isset($arState['mode']))
			{
				$mode = $arState['mode'];
			}
		}
		if($bSuccess === SOCSERV_REGISTRATION_DENY)
		{
			$url = (preg_match("/\?/", $url)) ? $url.'&' : $url.'?';
			$url .= 'auth_service_id='.self::ID.'&auth_service_error='.SOCSERV_REGISTRATION_DENY;
		}
		elseif($bSuccess !== true)
			$url = (isset($urlPath)) ? $urlPath.'?auth_service_id='.self::ID.'&auth_service_error='.$bSuccess : $APPLICATION->GetCurPageParam(('auth_service_id='.self::ID.'&auth_service_error='.$bSuccess), $aRemove);
		if(CModule::IncludeModule("socialnetwork") && strpos($url, "current_fieldset=") === false)
			$url = (preg_match("/\?/", $url)) ? $url."&current_fieldset=SOCSERV" : $url."?current_fieldset=SOCSERV";

		$url = CUtil::JSEscape($url);
		$location = ($mode == "opener") ? 'if(window.opener) window.opener.location = \''.$url.'\'; window.close();' : ' window.location = \''.$url.'\';';

		$JSScript = '
		<script type="text/javascript">
		'.$location.'
		</script>
		';

		echo $JSScript;

		die();
	}
Beispiel #4
0
    public function Authorize()
    {
        $GLOBALS["APPLICATION"]->RestartBuffer();
        $bSuccess = 1;
        if (isset($_REQUEST["code"]) && $_REQUEST["code"] != '' && CSocServAuthManager::CheckUniqueKey()) {
            if (IsModuleInstalled('bitrix24') && defined('BX24_HOST_NAME')) {
                $redirect_uri = self::CONTROLLER_URL . "/redirect.php";
            } else {
                $redirect_uri = CSocServUtil::ServerName() . "/bitrix/tools/oauth/google.php";
            }
            $appID = trim(self::GetOption("google_appid"));
            $appSecret = trim(self::GetOption("google_appsecret"));
            $gAuth = new CGoogleOAuthInterface($appID, $appSecret, $_REQUEST["code"]);
            if ($gAuth->GetAccessToken($redirect_uri) !== false) {
                $arGoogleUser = $gAuth->GetCurrentUser();
                if ($arGoogleUser['email'] != '') {
                    $first_name = $last_name = $gender = "";
                    if ($arGoogleUser['name'] != '') {
                        $aName = explode(" ", $arGoogleUser['name']);
                        if ($arGoogleUser['given_name'] != '') {
                            $first_name = $arGoogleUser['given_name'];
                        } else {
                            $first_name = $aName[0];
                        }
                        if ($arGoogleUser['family_name'] != '') {
                            $last_name = $arGoogleUser['family_name'];
                        } elseif (isset($aName[1])) {
                            $last_name = $aName[1];
                        }
                    }
                    $email = $arGoogleUser['email'];
                    if ($arGoogleUser['gender'] != '') {
                        if ($arGoogleUser['gender'] == 'male') {
                            $gender = 'M';
                        } elseif ($arGoogleUser['gender'] == 'female') {
                            $gender = 'F';
                        }
                    }
                    $arFields = array('EXTERNAL_AUTH_ID' => self::ID, 'XML_ID' => $email, 'LOGIN' => "G_" . $email, 'EMAIL' => $email, 'NAME' => $first_name, 'LAST_NAME' => $last_name);
                    if ($gender != "") {
                        $arFields['PERSONAL_GENDER'] = $gender;
                    }
                    if (isset($arGoogleUser['picture']) && self::CheckPhotoURI($arGoogleUser['picture'])) {
                        if ($arPic = CFile::MakeFileArray($arGoogleUser['picture'])) {
                            $arFields["PERSONAL_PHOTO"] = $arPic;
                        }
                    }
                    $arFields["PERSONAL_WWW"] = $arGoogleUser['link'];
                    if (strlen(SITE_ID) > 0) {
                        $arFields["SITE_ID"] = SITE_ID;
                    }
                    $bSuccess = $this->AuthorizeUser($arFields);
                }
            }
        }
        $url = $GLOBALS["APPLICATION"]->GetCurDir() == "/login/" ? "/auth/" : $GLOBALS["APPLICATION"]->GetCurDir();
        if (isset($_REQUEST["state"])) {
            $arState = array();
            parse_str($_REQUEST["state"], $arState);
            if (isset($arState['backurl'])) {
                $parseUrl = parse_url($arState['backurl'], PHP_URL_PATH);
                $url = $parseUrl;
            }
        }
        $aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key", "current_fieldset");
        if ($bSuccess === 2) {
            $url = preg_match("/\\?/", $url) ? $url . '&' : $url . '?';
            $url .= 'auth_service_id=' . self::ID . '&auth_service_error=' . $bSuccess;
        } elseif ($bSuccess !== true) {
            $url = isset($parseUrl) ? $parseUrl . '?auth_service_id=' . self::ID . '&auth_service_error=' . $bSuccess : $GLOBALS['APPLICATION']->GetCurPageParam('auth_service_id=' . self::ID . '&auth_service_error=' . $bSuccess, $aRemove);
        }
        if (CModule::IncludeModule("socialnetwork")) {
            $url = preg_match("/\\?/", $url) ? $url . "&current_fieldset=SOCSERV" : $url . "?current_fieldset=SOCSERV";
        }
        echo '
<script type="text/javascript">
if(window.opener)
	window.opener.location = \'' . CUtil::JSEscape($url) . '\';
window.close();
</script>
';
        die;
    }