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

	$APPLICATION->RestartBuffer();

	$bProcessState = false;
	$bSuccess = SOCSERV_AUTHORISATION_ERROR;

	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 = CHTTP::URN2URI("/bitrix/tools/oauth/liveid.php");

		$appID = trim(self::GetOption("liveid_appid"));
		$appSecret = trim(self::GetOption("liveid_appsecret"));

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

		$bProcessState = true;

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

			$arLiveIDUser = $gAuth->GetCurrentUser();
			if(is_array($arLiveIDUser) &&  ($arLiveIDUser['id'] <> ''))
			{
				$email = $first_name = $last_name = "";
				$login = "LiveID".$arLiveIDUser['id'];
				$uId = $arLiveIDUser['id'];
				if($arLiveIDUser['first_name'] <> '')
					$first_name = $arLiveIDUser['first_name'];
				if($arLiveIDUser['last_name'] <> '')
					$last_name = $arLiveIDUser['last_name'];
				if($arLiveIDUser['emails']['preferred'] <> '')
				{
					$email = $arLiveIDUser['emails']['preferred'];
					$login = $arLiveIDUser['emails']['preferred'];
					$uId = $arLiveIDUser['emails']['preferred'];
				}
				$arFields = array(
					'EXTERNAL_AUTH_ID' => self::ID,
					'XML_ID' => $uId,
					'LOGIN' => $login,
					'EMAIL' => $email,
					'NAME'=> $first_name,
					'LAST_NAME'=> $last_name,
				);
				$arFields["PERSONAL_WWW"] = $arLiveIDUser["link"];
				if(isset($arLiveIDUser['access_token']))
					$arFields["OATOKEN"] = $arLiveIDUser['access_token'];

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

				if(isset($arLiveIDUser['expires_in']))
					$arFields["OATOKEN_EXPIRES"] = time() + $arLiveIDUser['expires_in'];
				if(SITE_ID <> '')
					$arFields["SITE_ID"] = SITE_ID;
				$bSuccess = $this->AuthorizeUser($arFields);

			}
		}
	}

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

	$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';
	$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($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($parseUrl))
			? $urlPath.'?auth_service_id='.self::ID.'&auth_service_error='.$bSuccess
			: $APPLICATION->GetCurPageParam(('auth_service_id='.self::ID.'&auth_service_error='.$bSuccess), $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();
}