Your IP : 216.73.216.224


Current Path : /var/www/html/plugins/system/jchatlogin/connectors/
Upload File :
Current File : /var/www/html/plugins/system/jchatlogin/connectors/facebook.php

<?php
/** 
 * Manage login/logout for social networks connect
 * @package JCHAT::plugins::system
 * @subpackage jchatlogin
 * @author Joomla! Extensions Store
 * @copyright (C) 2024 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html  
 */
defined ( '_JEXEC' ) or die ();
use Joomla\CMS\Language\Text;
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\User\UserHelper;
use JExtstore\Component\JChat\Administrator\Framework\Helpers\Users as JChatHelpersUsers;

/**
 * Facebook connector for the social login
 *
 * @package JCHAT::plugins::system
 * @subpackage jchatlogin
 * @since 2.1
 */
class JChatLoginConnectorFacebook extends JChatLoginConnector {
	/**
	 * Main connector function
	 *
	 * @access public
	 * @return Void
	 */
	public function execute() {
		// Facebook Login - Execute only on self page reload url after Facebook JS SDK login completed
		if ($this->joomlaUserObject->guest && ( bool ) $this->app->getInput()->getInt ( 'fblogin', false )) {
			try {
				$filter = InputFilter::getInstance ();
	
				// Instantiate main Facebook API library class object, pass in Application ID and secret doce
				$facebookAPI = new \JChatFacebook ( array (
						'appId' => $this->appId,
						'secret' => $this->secret
				) );
	
				// Check if SSL peer verification is enabled
				if(!$this->cParams->get('curl_ssl_verifypeer', true)) {
					JChatFacebookBase::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
				}
	
				// Retrieve info about current Facebook user using API to get user integer identifier
				$fbUserID = $facebookAPI->getUser ();
				if(!$fbUserID) {
					throw new \Exception(Text::_('COM_JCHAT_FBUSERID_NOTFOUND'));
				}
	
				// Retrieve full Facebook user profile informations using Facebook API
				$fbUserProfileArray = $facebookAPI->api ( '/me?fields=id,email,name,first_name,last_name,picture' );
	
				// Check if users exists already in the Joomla database using email address as primary key, retrieve user id if exists
				$authType = $this->cParams->get('auth_type', 'id');
				$alreadyCreatedJoomlaUserID = JChatHelpersUsers::getJoomlaId ( $fbUserProfileArray [$authType], $authType );
	
				if (! $alreadyCreatedJoomlaUserID) {
					$name = @$fbUserProfileArray ['name'];
					$username = strtolower ( $filter->clean ( @$fbUserProfileArray ['name'], 'cmd' ) );
					// This Facebook account as no username, for example a Facebook page
					if (! $username) {
						$name = $fbUserProfileArray ['email'];
						$username = $fbUserProfileArray ['email'];
					}
					$password = UserHelper::genRandomPassword ( 5 );
					$email = $fbUserProfileArray ['email'] ? $fbUserProfileArray ['email'] : $fbUserProfileArray ['id'] . '@facebook.com'; 
					$fbUserProfileArray ['picture'] = 'https://graph.facebook.com/' .$fbUserProfileArray['id']. '/picture?type=normal';
					
					// Trigger to create a new Joomla user aggregating data from Facebook user profile, pre-populate bind $this->joomlaUserObject
					$this->onNewUser(
							$this->joomlaUserObject,
							$name,
							$username,
							$password,
							$email,
							$fbUserProfileArray,
							$this->cParams
					);
	
					// Do instant Joomla login authentication with new user, aggregated data and random generated password only for login purpouse
					$this->onLogin(
							$this->joomlaUserObject,
							$this->cParams
					);
				} else {
					// get already populated $this->joomlaUserObject
					$this->joomlaUserObject = Factory::getContainer()->get(\Joomla\CMS\User\UserFactoryInterface::class)->loadUserById ( $alreadyCreatedJoomlaUserID );
					// Do instant Joomla login authentication with new user, aggregated data and random generated password only for login purpouse
					$this->onLogin(
							$this->joomlaUserObject,
							$this->cParams
					);
				}
			} catch ( \Exception $e ) {
				$this->app->enqueueMessage(Text::sprintf('COM_JCHAT_ERROR_FACEBOOK_LOGIN', $e->getMessage ()), 'warning');
			}
		}
	}
	
	/**
	 * Class Constructor
	 * @param Object $cParams
	 * 
	 * @access public
	 */
	public function __construct($cParams) {
		parent::__construct($cParams);
				
		$this->appId = $this->cParams->get('appId', null);
		$this->secret = $this->cParams->get('secret', null);
		
		// Load framework classes without autoloading
		require_once JPATH_ROOT . '/plugins/system/jchatlogin/social/facebook/base.php';
		require_once JPATH_ROOT . '/plugins/system/jchatlogin/social/facebook/facebook.php';
		require_once JPATH_ROOT . '/plugins/system/jchatlogin/social/facebook/exception.php';
	}
}