Example #1
0
    public function Authorize()
    {
        $GLOBALS["APPLICATION"]->RestartBuffer();
        $bSuccess = 1;
        $bProcessState = false;
        if (isset($_REQUEST["code"]) && $_REQUEST["code"] != '' && CSocServAuthManager::CheckUniqueKey()) {
            $bProcessState = true;
            $redirect_uri = CSocServUtil::GetCurUrl('auth_service_id=' . self::ID, array("code", "state", "check_key", "backurl"));
            $appID = trim(self::GetOption("mailru_id"));
            $appSecret = trim(self::GetOption("mailru_secret_key"));
            $gAuth = new CMailRuOAuthInterface($appID, $appSecret, $_REQUEST["code"]);
            if ($gAuth->GetAccessToken($redirect_uri) !== false) {
                $arMRUser = $gAuth->GetCurrentUser();
                if (is_array($arMRUser) && $arMRUser['0']['uid'] != '') {
                    $email = $first_name = $last_name = $gender = "";
                    if ($arMRUser['0']['first_name'] != '') {
                        $first_name = $arMRUser['0']['first_name'];
                    }
                    if ($arMRUser['0']['last_name'] != '') {
                        $last_name = $arMRUser['0']['last_name'];
                    }
                    if ($arMRUser['0']['email'] != '') {
                        $email = $arMRUser['0']['email'];
                    }
                    if (isset($arMRUser['0']['sex']) && $arMRUser['0']['sex'] != '') {
                        if ($arMRUser['0']['sex'] == '0') {
                            $gender = 'M';
                        } elseif ($arMRUser['0']['sex'] == '1') {
                            $gender = 'F';
                        }
                    }
                    $arFields = array('EXTERNAL_AUTH_ID' => self::ID, 'XML_ID' => $arMRUser['0']['uid'], 'LOGIN' => "MM_" . $email, 'NAME' => $first_name, 'EMAIL' => $email, 'LAST_NAME' => $last_name, 'PERSONAL_GENDER' => $gender);
                    if (isset($arMRUser['0']['birthday'])) {
                        if ($date = MakeTimeStamp($arMRUser['0']['birthday'], "DD.MM.YYYY")) {
                            $arFields["PERSONAL_BIRTHDAY"] = ConvertTimeStamp($date);
                        }
                    }
                    if (isset($arMRUser['0']['pic_190']) && self::CheckPhotoURI($arMRUser['0']['pic_190'])) {
                        if ($arPic = CFile::MakeFileArray($arMRUser['0']['pic_190'] . '?name=/' . md5($arMRUser['0']['pic_190']) . '.jpg')) {
                            $arFields["PERSONAL_PHOTO"] = $arPic;
                        }
                    }
                    $arFields["PERSONAL_WWW"] = $arMRUser['0']['link'];
                    if (strlen(SITE_ID) > 0) {
                        $arFields["SITE_ID"] = SITE_ID;
                    }
                    $bSuccess = $this->AuthorizeUser($arFields);
                }
            }
        }
        if (!$bProcessState) {
            unset($_REQUEST["state"]);
        }
        $url = $GLOBALS["APPLICATION"]->GetCurDir() == "/login/" ? "" : $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 ($bSuccess !== true) {
            $url = $GLOBALS['APPLICATION']->GetCurPageParam('auth_service_id=' . self::ID . '&auth_service_error=' . $bSuccess, $aRemove);
        }
        echo '
<script type="text/javascript">
if(window.opener)
	window.opener.location = \'' . CUtil::JSEscape($url) . '\';
window.close();
</script>
';
        die;
    }