Esempio n. 1
0
 function loginFacebookUser()
 {
     $app = JFactory::getApplication();
     $jfbcLibrary = JFBConnectFacebookLibrary::getInstance();
     $fbUserId = $jfbcLibrary->getUserId(FALSE);
     #echo "Con. Logging in FB User : "******"<br>";
     require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_jfbconnect' . DS . 'models' . DS . 'usermap.php';
     $userMapModel = new JFBConnectModelUserMap();
     $configModel = $jfbcLibrary->configModel;
     $user = JFactory::getUser();
     if ($user->guest) {
         # Guest: Check if they have a Joomla user and log that user in. If not, create them one
         $jUserId = $userMapModel->getJoomlaUserId($fbUserId);
         if (!$fbUserId) {
             # Facebook isn't returning information about this user.  Redirect them.
             $app->enqueueMessage('We were unable to retrieve your Facebook account information. Please try again');
             $app->redirect('index.php');
         }
         # Check if automatic email mapping is allowed, and see if that email is registered
         # AND the Facebook user doesn't already have a Joomla account
         if ($configModel->getSetting('facebook_auto_map_by_email') && !$jUserId) {
             $fbProfileFields = $jfbcLibrary->getUserProfile($fbUserId, array('email'));
             if ($fbProfileFields != null && $fbProfileFields['email']) {
                 $fbEmail = $fbProfileFields['email'];
                 $jUserEmailId = $userMapModel->getJoomlaUserIdFromEmail($fbEmail);
                 if ($jUserEmailId && $jUserEmailId != 0) {
                     // Found a user with the same email address
                     // do final check to make sure there isn't a FB account already mapped to it
                     $jUserId = $userMapModel->getFacebookUserId($jUserEmailId);
                     if (!$jUserId) {
                         if ($userMapModel->mapUser($fbUserId, $jUserEmailId)) {
                             $app->enqueueMessage(JText::_('MAP USER SUCCESS'));
                         } else {
                             $app->enqueueMessage(JText::_('MAP USER FAIL'));
                         }
                         $app->enqueueMessage(JText::_('MAP USER SUCCESS'));
                         $jUserId = $jUserEmailId;
                         // Update the temp jId so that we login below
                     }
                 }
             }
         }
         if ($configModel->getSetting('create_new_users') && $jUserId == null) {
             # User not in system, create new users setting enabled. Redirect to the login/register form
             $app->redirect(JRoute::_('index.php?option=com_jfbconnect&view=loginregister', false));
         }
         #set the initial registration variable so that the user plugin knows to map user and trigger onAfterRegistration
         if ($jUserId == null) {
             $jfbcLibrary->setInitialRegistration();
         }
         require_once JPATH_COMPONENT . DS . 'controllers' . DS . 'loginregister.php';
         $loginController = new JFBConnectControllerLoginRegister();
         $loginController->login();
         // Perform the login function
     } else {
         #echo "Updating mapping only";
         if ($userMapModel->mapUser($fbUserId)) {
             $app->enqueueMessage(JText::_('MAP USER SUCCESS'));
         } else {
             $app->enqueueMessage(JText::_('MAP USER FAIL'));
         }
     }
     $return = base64_decode(JRequest::getVar('return'));
     $app->redirect($return);
 }