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

	$bSuccess = false;

	if(!isset($_REQUEST["oauth_token"]) || $_REQUEST["oauth_token"] == '')
	{
		$tw = new CTwitterInterface();
		$callback = CSocServUtil::GetCurUrl('auth_service_id='.self::ID, false, false);

		if($tw->GetRequestToken($callback))
		{
			$tw->RedirectAuthUrl();
		}
	}
	elseif(CSocServAuthManager::CheckUniqueKey())
	{
		$tw = new CTwitterInterface(false, false, $_REQUEST["oauth_token"], $_REQUEST["oauth_verifier"]);
		if(($arResult = $tw->GetAccessToken()) !== false && $arResult["user_id"] <> '')
		{
			$twUser = $tw->GetUserInfo($arResult["user_id"]);

			$first_name = $last_name = "";
			if(is_array($twUser) && ($twUser["name"] <> ''))
			{
				$aName = explode(" ", $twUser["name"]);
				$first_name = $aName[0];
				if(isset($aName[1]))
					$last_name = $aName[1];
			}

			$arFields = array(
				'EXTERNAL_AUTH_ID' => self::ID,
				'XML_ID' => $arResult["user_id"],
				'LOGIN' => $arResult["screen_name"],
				'NAME'=> $first_name,
				'LAST_NAME'=> $last_name,
			);
			if(isset($twUser["profile_image_url"]) && self::CheckPhotoURI($twUser["profile_image_url"]))
			{
				$twUser["profile_image_url"] = preg_replace("/_normal./i", ".", $twUser["profile_image_url"]);
				if ($arPic = CFile::MakeFileArray($twUser["profile_image_url"]))
				{
					$arFields["PERSONAL_PHOTO"] = $arPic;
				}
			}

			$arFields["PERSONAL_WWW"] = "https://twitter.com/".$arResult["screen_name"];
			if(SITE_ID <> '')
				$arFields["SITE_ID"] = SITE_ID;
			if(COption::GetOptionString('socialservices','last_twit_id','1') == 1)
			{
				if(isset($twUser["status"]["id_str"]))
					COption::SetOptionString('socialservices', 'last_twit_id', $twUser["status"]["id_str"]);
			}

			$authError = $this->AuthorizeUser($arFields);
			$bSuccess = $authError === true;
		}
	}

	if($bSuccess)
	{
		$aRemove = array("logout", "auth_service_error", "auth_service_id", "oauth_token", "oauth_verifier", "check_key", "current_fieldset", "ncc");

		$url = isset($_REQUEST['backurl']) ? $_REQUEST['backurl'] : $GLOBALS['APPLICATION']->GetCurPageParam(($bSuccess === true ? '' : 'auth_service_id='.self::ID.'&auth_service_error='.$bSuccess), $aRemove);
		if(CModule::IncludeModule("socialnetwork") && mb_strpos($url, "current_fieldset=") === false)
			$url = (preg_match("/?/", $url)) ? $url."¤t_fieldset=SOCSERV" : $url."?current_fieldset=SOCSERV";

		$url .= (preg_match("/?/", $url) ? '&' : '?').CSocServUtil::getOAuthProxyString();

		echo '

';
		CMain::FinalActions();
	}
	else
	{
		// some error occured

	}
}