Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Ambersport\Strava;
- use Bitrix\Main\Localization\Loc;
- Loc::loadMessages(__FILE__);
- class Auth extends \CSocServAuth {
- const ID = 'Strava';
- protected $flexSSL = true;
- protected $entityOAuth;
- public function isFlexSSL(){
- return $this->flexSSL;
- }
- public static function GetDescription() {
- return [
- [
- 'ID' => 'Strava',
- 'CLASS' => '\\Ambersport\\Strava\\Auth',
- 'NAME' => 'Strava',
- 'ICON' => 'strava',
- ]
- ];
- }
- public function GetSettings() {
- return [
- [
- 'client_id', 'Client ID', '', ['text', 40],
- ],
- [
- 'client_token', 'Client Token', '', ['text', 40],
- ],
- [
- 'client_secret', 'Client Secret', '', ['text', 40],
- ]
- ];
- }
- public function GetFormHtml($arParams) {
- $onclickJS = $this->GetOnClickJs($arParams);
- return $arParams["FOR_INTRANET"]
- ? array("ON_CLICK" => 'onclick="'.$onclickJS.'"')
- : '<a href="javascript:void(0)" onclick="'.$onclickJS.'" class="strava-button"></a>';
- }
- public function GetOnClickJs($arParams) {
- $redirect_uri = \CSocServUtil::GetCurUrl('auth_service_id='.self::ID.'&check_key='.$_SESSION['UNIQUE_KEY']);
- // Only for CloudFlare flexible SSL
- if($this->isFlexSSL()){
- $redirect_uri = str_replace('http://', 'https://', $redirect_uri);
- }
- $queryParams = [
- 'redirect_uri' => $redirect_uri,
- 'client_id' => trim(self::getOption('client_id')),
- 'response_type' => 'code',
- ];
- $url = 'https://www.strava.com/oauth/authorize?'.http_build_query($queryParams);
- return "BX.util.popup('".$url."', 660, 425)";
- }
- public function getEntityOAuth($code = false)
- {
- if (!$this->entityOAuth)
- {
- $this->entityOAuth = new \Ambersport\Strava\OAuth();
- }
- if ($code !== false)
- {
- $this->entityOAuth->setCode($code);
- }
- return $this->entityOAuth;
- }
- public function prepareUser($arStravaUser, $short = false)
- {
- $first_name = $last_name = $gender = "";
- if ($arStravaUser['firstname'] <> '')
- {
- $first_name = $arStravaUser['firstname'];
- }
- if ($arStravaUser['lastname'] <> '')
- {
- $last_name = $arStravaUser['lastname'];
- }
- if (isset($arStravaUser['sex']) && $arStravaUser['sex'] != '')
- {
- $gender = $arStravaUser['sex'];
- }
- $arFields = array(
- 'EXTERNAL_AUTH_ID' => self::ID,
- 'XML_ID' => $arStravaUser['id'],
- 'LOGIN' => "StravaUser_" . $arStravaUser['id'],
- 'EMAIL' => $this->entityOAuth->GetCurrentUserEmail(),
- 'NAME' => $first_name,
- 'LAST_NAME' => $last_name,
- 'PERSONAL_GENDER' => $gender,
- 'OATOKEN' => $this->entityOAuth->getToken(),
- 'OATOKEN_EXPIRES' => $this->entityOAuth->getAccessTokenExpires(),
- );
- if (isset($arStravaUser['profile']) && self::CheckPhotoURI($arStravaUser['profile']))
- {
- if (!$short)
- {
- $arPic = \CFile::MakeFileArray($arStravaUser['profile']);
- if ($arPic)
- {
- $arFields["PERSONAL_PHOTO"] = $arPic;
- }
- }
- if (strlen(SITE_ID) > 0)
- {
- $arFields["SITE_ID"] = SITE_ID;
- }
- }
- return $arFields;
- }
- public function Authorize()
- {
- $GLOBALS["APPLICATION"]->RestartBuffer();
- $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($GLOBALS['APPLICATION']->GetCurPage()) . '?auth_service_id=' . self::ID;
- if($this->isFlexSSL()){
- $redirect_uri = str_replace('http://', 'https://', $redirect_uri);
- }
- $this->entityOAuth = $this->getEntityOAuth($_REQUEST['code']);
- if ($this->entityOAuth->GetAccessToken($redirect_uri) !== false)
- {
- $arStravaUser = $this->entityOAuth->GetCurrentUser();
- if (is_array($arStravaUser) && ($arStravaUser['id'] <> ''))
- {
- $arFields = $this->prepareUser($arStravaUser);
- $bSuccess = $this->AuthorizeUser($arFields);
- }
- }
- }
- $url = ($GLOBALS["APPLICATION"]->GetCurDir() == "/login/") ? "" : $GLOBALS["APPLICATION"]->GetCurDir();
- $aRemove = array("logout", "auth_service_error", "auth_service_id", "code", "error_reason", "error", "error_description", "check_key", "current_fieldset");
- if (isset($_REQUEST['backurl']) || isset($_REQUEST['redirect_url']))
- {
- $parseUrl = parse_url(isset($_REQUEST['redirect_url']) ? $_REQUEST['redirect_url'] : $_REQUEST['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 ($bSuccess === SOCSERV_REGISTRATION_DENY)
- {
- $url = (preg_match("/\?/", $url)) ? $url . '&' : $url . '?';
- $url .= 'auth_service_id=' . self::ID . '&auth_service_error=' . $bSuccess;
- }
- elseif ($bSuccess !== true)
- {
- $url = (isset($urlPath)) ? $urlPath . '?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") && strpos($url, "current_fieldset=") === false)
- {
- $url = (preg_match("/\?/", $url)) ? $url . "¤t_fieldset=SOCSERV" : $url . "?current_fieldset=SOCSERV";
- }
- echo '
- <script type="text/javascript">
- if(window.opener)
- {
- window.opener.location = \'' . \CUtil::JSEscape($url) . '\';
- }
- window.close();
- </script>
- ';
- die();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement