Exemple #1
0
function OnAfterUserAuthorizeHandler(&$arFields)
{
    $f = fopen($_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/md/log.txt', 'a+');
    global $USER;
    $rsUser = CUser::GetByID($USER->GetId());
    $arUser = $rsUser->Fetch();
    fwrite($f, "---start\n");
    if ($arUser['ID'] > 0 && $arUser['EXTERNAL_AUTH_ID'] == 'Facebook' && $arUser['UF_RULES'] != '1') {
        $facebook = new Facebook_Facebook(array('appId' => '447579571927341', 'secret' => '2f2cf9cd60f9e98d6cf3309e6b7bde5d'));
        $id = $arUser["XML_ID"];
        $user_info = $facebook->api('/' . $id . '?fields=id,name,first_name,middle_name,last_name,gender,birthday,email,picture');
        $props = array();
        if (!$arUser['PERSONAL_GENDER']) {
            if (isset($user_info['gender']) && $user_info['gender']) {
                $props['PERSONAL_GENDER'] = $user_info['gender'] == 'male' ? 'M' : 'F';
            }
        }
        if (!empty($props)) {
            $user = new CUser();
            $user->Update($arUser["ID"], $props);
        }
        CModule::IncludeModule("blog");
        $blogUser = CBlogUser::GetByID($USER->GetId(), BLOG_BY_USER_ID);
        $props = array();
        if (!$blogUser['AVATAR']) {
            if (isset($user_info['picture']) && $user_info['picture']) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://graph.facebook.com/{$id}/picture?type=large");
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
                $output = curl_exec($ch);
                if ($output) {
                    $fileName = md5($user_info['picture']);
                    $fullPath = $_SERVER['DOCUMENT_ROOT'] . "/bitrix/cache/social_pictures/{$fileName}.jpg";
                    if (file_put_contents($fullPath, $output) !== false) {
                        $picture = CFile::MakeFileArray($fullPath);
                        $props['AVATAR'] = $picture;
                        $user = new CBlogUser();
                        $user->Update($blogUser["ID"], $props);
                        unlink($fullPath);
                    }
                }
            }
        }
    }
    fwrite($f, "---finish\n");
    fclose($f);
}
 public function loginAction()
 {
     $error = false;
     $facebook = new Facebook_Facebook(array('appId' => Mage::helper('facebooklogin')->getAppID(), 'secret' => Mage::helper('facebooklogin')->getAppSecret()));
     $fb_user = $facebook->getUser();
     if ($fb_user) {
         try {
             $user_profile = $facebook->api('/me');
         } catch (FacebookApiException $e) {
             $error = $this->__('Could not load user Facebook profile data.');
             $fb_user = null;
         }
     }
     if ($fb_user) {
         $error = false;
         $session = Mage::getSingleton('customer/session');
         $uid = $user_profile['id'];
         $email = $user_profile['email'];
         $first_name = $user_profile['first_name'];
         $last_name = $user_profile['last_name'];
         $customer = Mage::getModel('customer/customer');
         $customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->loadByEmail($email);
         if ($customer->getId() !== NULL) {
             try {
                 $session->loginById($customer->getId());
             } catch (Exception $e) {
                 $error = $this->__($e->getMessage());
             }
         } else {
             $customer = Mage::getModel('customer/customer')->setId(null);
             $customer->setData('facebook_uid', $uid);
             $customer->setData('firstname', $first_name);
             $customer->setData('lastname', $last_name);
             $customer->setData('email', $email);
             $customer->getGroupId();
             $customer->save();
             if ($customer->isConfirmationRequired()) {
                 $customer->sendNewAccountEmail('confirmation', $this->_getSession()->getBeforeAuthUrl());
             } else {
                 $session->setCustomerAsLoggedIn($customer);
             }
         }
     }
     // 		die($this->getAfterLoginUrl());
     Mage::app()->getFrontController()->getResponse()->setRedirect($this->getAfterLoginUrl());
     // 		$this->_redirect($this->getAfterLoginUrl());
 }
 public function logoutAction()
 {
     $this->_helper->viewRenderer->setNoRender();
     $this->_helper->getHelper("layout")->disableLayout();
     $oFacebook = new Facebook_Facebook();
     $oModelUser = new Admin_Model_User();
     if (is_string($oFacebook->getUser())) {
         $sLogoutUrl = $oFacebook->getLogoutUrl();
         if ($this->_oAuth->hasIdentity()) {
             $this->_oAuth->clearIdentity();
             $oModelUser->setAskOnline($this->_nUserId, 0);
         }
         $oFacebook->destroySession();
         $this->_redirect($sLogoutUrl);
     } else {
         if ($this->_oAuth->hasIdentity()) {
             $this->_oAuth->clearIdentity();
             $oModelUser->setAskOnline($this->_nUserId, 0);
         }
     }
     $this->_redirect("/");
 }