• Модуль: socialservices
  • Путь к файлу: ~/bitrix/modules/socialservices/classes/general/google.php
  • Класс: CSocServGoogleOAuth
  • Вызов: CSocServGoogleOAuth::Authorize
public function Authorize()
{
	global $APPLICATION;
	$APPLICATION->RestartBuffer();

	$bSuccess = false;
	$bProcessState = false;

	$authError = SOCSERV_AUTHORISATION_ERROR;

	if(
		isset($_REQUEST["code"]) && $_REQUEST["code"] <> ''
		&& CSocServAuthManager::CheckUniqueKey()
	)
	{
		$this->getEntityOAuth()->setCode($_REQUEST["code"]);

		$bProcessState = true;

		if($this->getEntityOAuth()->GetAccessToken() !== false)
		{
			$arGoogleUser = $this->getEntityOAuth()->GetCurrentUser();

			if(is_array($arGoogleUser) && !isset($arGoogleUser["error"]))
			{
				$arFields = self::prepareUser($arGoogleUser);
				$authError = $this->AuthorizeUser($arFields);
			}
		}
	}

	if(!$bProcessState)
	{
		unset($_REQUEST["state"]);
	}

	$bSuccess = $authError === true;

	$aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key", "current_fieldset");

	if($bSuccess)
	{
		CSocServUtil::checkOAuthProxyParams();

		$url = ($APPLICATION->GetCurDir() == "/login/") ? "" : $APPLICATION->GetCurDir();
		$mode = 'opener';
		$addParams = true;
		if(isset($_REQUEST["state"]))
		{
			$arState = array();
			parse_str($_REQUEST["state"], $arState);

			if(isset($arState['backurl']) || isset($arState['redirect_url']))
			{
				$url = !empty($arState['redirect_url']) ? $arState['redirect_url'] : $arState['backurl'];
				if(mb_substr($url, 0, 1) !== "#")
				{
					$parseUrl = parse_url($url);

					$urlPath = $parseUrl["path"];
					$arUrlQuery = explode('&', $parseUrl["query"]);

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

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

			if(isset($arState['mode']))
			{
				$mode = $arState['mode'];
			}
		}
	}

	if($authError === SOCSERV_REGISTRATION_DENY)
	{
		$url = (preg_match("/?/", $url)) ? $url.'&' : $url.'?';
		$url .= 'auth_service_id='.static::ID.'&auth_service_error='.SOCSERV_REGISTRATION_DENY;
	}
	elseif($bSuccess !== true)
	{
		$url = (isset($urlPath)) ? $urlPath.'?auth_service_id='.static::ID.'&auth_service_error='.$authError : $APPLICATION->GetCurPageParam(('auth_service_id='.static::ID.'&auth_service_error='.$authError), $aRemove);
	}

	if($addParams && CModule::IncludeModule("socialnetwork") && mb_strpos($url, "current_fieldset=") === false)
	{
		$url = (preg_match("/?/", $url)) ? $url."¤t_fieldset=SOCSERV" : $url."?current_fieldset=SOCSERV";
	}

	$url = CUtil::JSEscape($url);

	if($addParams)
	{
		$location = ($mode == "opener") ? 'if(window.opener) window.opener.location = ''.$url.''; window.close();' : ' window.location = ''.$url.'';';
	}
	else
	{
		//fix for chrome
		$location = ($mode == "opener") ? 'if(window.opener) window.opener.location = window.opener.location.href + ''.$url.''; window.close();' : ' window.location = window.location.href + ''.$url.'';';
	}

	$JSScript = '
	
	';

	echo $JSScript;

	CMain::FinalActions();
}