/** * Create a new user * * @param $fbUserId A Facebook User ID * * @return User id */ public function store($fbUserId, $fbUserData) { settype($fbUserId, "string"); $fbUserId = JString::trim($fbUserId); if (!$fbUserId) { throw new ItpException(JText::_('ITP_ERROR_FB_ID'), 404); } // Check for existing e-mail (user) $userId = ItpcHelper::getJUserIdByEmail($fbUserData['email']); // Initialise the table with JUser. $user = JUser::getInstance(); if (!$userId) { $config = JFactory::getConfig(); // Initialise the table with JUser. $user = new JUser(); $data = (array) $this->getData(); jimport('joomla.user.helper'); // Prepare the data for the user object. $data['name'] = $fbUserData['name']; $data['email'] = $fbUserData['email']; $data['username'] = substr($fbUserData['email'], 0, strpos($fbUserData['email'], "@")); $data['password'] = $password = JUserHelper::genRandomPassword(); $data['block'] = 0; // Bind the data. if (!$user->bind($data)) { throw new ItpException($user->getError(), 500); } // Load the users plugin group. JPluginHelper::importPlugin('user'); // Store the data. if (!$user->save()) { throw new ItpException($user->getError(), 500); } // Send a confirmation mail $this->sendConfirmationMail($data, $password); } else { $user->load($userId); } // Loads a record from database $row = $this->getTable("itpcuser", "ItpConnectTable"); $row->load($fbUserId, "facebook"); // Initialize object for new record if (!$row->id) { $row = $this->getTable("itpcuser", "ITPConnectTable"); } $row->set("users_id", $user->id); $row->set("fbuser_id", $fbUserId); if (!$row->store()) { throw new ItpException($row->getError(), 500); } return $row->users_id; }
/** * Put a code that connect to site with authentication services - Facebook, Twitter,... */ public function onAfterRender() { if (!$this->itpConnectParams->get("facebookOn", 0)) { return; } if (!JComponentHelper::isEnabled('com_itpconnect', true)) { return; } $app = JFactory::getApplication(); /* @var $app JApplication */ if ($app->isAdmin()) { return; } $doc = JFactory::getDocument(); /* @var $doc JDocumentHTML */ $docType = $doc->getType(); // Joomla! must render content of this plugin only in HTML document if (strcmp("html", $docType) != 0) { return; } // Get language if ($this->itpConnectParams->get("fbDynamicLocale", 0)) { $lang = JFactory::getLanguage(); $locale = $lang->getTag(); $locale = str_replace("-", "_", $locale); } else { $locale = $this->itpConnectParams->get("fbLocale", "en_US"); } // Set AJAX loader $ajaxLoader = ""; if ($this->itpConnectParams->get("fbDisplayAjaxLoader")) { $ajaxLoader = "document.id('itpconnect-ajax-loader').show();"; } // Get return page $return = ItpcHelper::getReturn(); $return = base64_encode($return); $buffer = JResponse::getBody(); // $pattern = "/<body[^>]*>/s"; $pattern = "/<\\/body[^>]*>/s"; $matches = array(); if (preg_match($pattern, $buffer, $matches)) { // Add Facebook tags description into html tag // $newHtmlAttr = '<html xmlns:fb="http://www.facebook.com/2008/fbml" '; // $buffer = str_replace("<html",$newHtmlAttr,$buffer); // $newBodyTag = $matches[0] . " $newBodyTag = "\n <div id='fb-root'></div>\n\n <script>\n\tfunction itpLogin() {\n \twindow.location = 'index.php?option=com_itpconnect&task=facebook.connect&return=" . $return . "'\n }\n \n window.fbAsyncInit = function() {\n FB.init({\n appId : '" . $this->itpConnectParams->get("facebookAppId") . "', // App ID\n status : true, // check login status\n cookie : true, // enable cookies to allow the server to access the session\n xfbml : true, // parse XFBML\n oauth \t: true\n });\n \n // Additional initialization code here\n // whenever the user logs in, we refresh the page\n FB.Event.subscribe('auth.login', function() {\n {$ajaxLoader}\n });\n \n // whenever the user logs in, we refresh the page\n FB.Event.subscribe('auth.logout', function() {\n \twindow.location.reload();\n });\n \n };\n \n </script>\n \n <script>(function(d, s, id) {\n var js, fjs = d.getElementsByTagName(s)[0];\n if (d.getElementById(id)) return;\n js = d.createElement(s); js.id = id;\n js.src = \"//connect.facebook.net/" . $locale . "/all.js\";\n fjs.parentNode.insertBefore(js, fjs);\n }(document, 'script', 'facebook-jssdk'));</script>\n\n "; $newBodyTag .= $matches[0]; $buffer = str_replace($matches[0], $newBodyTag, $buffer); } JResponse::setBody($buffer); return; }
public function connect() { // Get return page $return = base64_decode(JRequest::getVar('return', '', 'GET', 'BASE64')); if (empty($return)) { $return = 'index.php?option=com_users&view=profile'; } $facebook = ItpcHelper::getFB(); $fbUserId = $facebook->getUser(); $me = null; // Session based API call. if ($fbUserId) { try { $me = $facebook->api('/me'); } catch (FacebookApiException $e) { $itpSecurity = new ItpSecurity($e); $itpSecurity->AlertMe(); $me = null; } } if ($me) { try { // Get model $model = $this->getModel("Facebook", "ITPConnectModel"); $data = $model->getItem($fbUserId); if (!$data) { // Create new user or Connect existing user with the facebook profile $userId = $model->store($fbUserId, $me); $data = $model->getItem($fbUserId); } $user = JUser::getInstance($data->users_id); $credentials['username'] = $user->get("username"); $credentials['password'] = $user->get("password"); $options = array(); $options['remember'] = JRequest::getBool('remember', true); $options['return'] = $return; $app =& JFactory::getApplication(); /* @var $app JApplication */ //preform the login action $error = $app->login($credentials, $options); if (JError::isError($error)) { throw new ItpException($error->getMessages(), 500); } } catch (Exception $e) { $itpSecurity = new ItpSecurity($e); $itpSecurity->AlertMe(); return $this->setRedirect(JRoute::_("index.php")); } } $this->setRedirect(JRoute::_($return)); }
public function connect() { $facebook = ItpcHelper::getFB(); $session = $facebook->getSession(); $me = null; // Session based API call. if ($session) { try { $uid = $facebook->getUser(); $me = $facebook->api('/me'); } catch (FacebookApiException $e) { $itpSecurity = new ItpSecurity($e); $itpSecurity->AlertMe(); $me = null; } } if ($me) { try { // Get model $model = $this->getModel("Facebook", "ITPConnectModel"); $data = $model->getItem($uid); if (!$data) { // Create new user or Connect existing user with the facebook.json profile $userId = $model->store($uid, $me); $data = $model->getItem($uid); } $user = JUser::getInstance($data->users_id); $credentials['username'] = $user->get("username"); $credentials['password'] = $user->get("password"); $options = array(); $options['remember'] = JRequest::getBool('remember', true); $options['return'] = ""; $app =& JFactory::getApplication(); /* @var $app JApplication */ //preform the login action $error = $app->login($credentials, $options); if (!JError::isError($error)) { ItpResponse::sendJsonMsg("All is OK", 1); } else { throw new ItpException($error->getMessages(), 500); } } catch (Exception $e) { $itpSecurity = new ItpSecurity($e); $itpSecurity->AlertMe(); ItpResponse::sendJsonMsg("Error on login", 0); } } }
/** * Get a return URL for the current page * * @return string Return page */ public static function getReturn() { $module = JModuleHelper::getModule("itpconnect"); $return = ""; if (!empty($module->params)) { $params = new JRegistry($module->params); $type = ItpcHelper::getType(); $return = ItpcHelper::getReturnURL($params, $type); $return = base64_decode($return); $return = JRoute::_($return, false); } if (!$return or !JURI::isInternal($return)) { $return = "/"; } return $return; }
/** * Set the indicator for logout. * I will use the indicator for excetuting the a Facebook function * that will destroy the session. * * @access public * @param array holds the user data * @return boolean True on success */ public function onUserLogout($user) { $itpConnectParams = JComponentHelper::getParams('com_itpconnect'); if (!$itpConnectParams->get("facebookOn", 0)) { return true; } if (!JComponentHelper::isEnabled('com_itpconnect', true)) { return true; } $app = JFactory::getApplication(); /* @var $app JApplication */ if ($app->isAdmin()) { return true; } $facebook = ItpcHelper::getFB(); $facebook->destroySession(); return true; }
/** * This method should handle any authentication and report back to the subject * * @access public * @param array $credentials Array holding the user credentials * @param array $options Array of extra options * @param object $response Authentication response object * @return boolean * @since 1.5 */ public function onUserAuthenticate($credentials, $options, &$response) { $success = false; $itpConnectParams = JComponentHelper::getParams('com_itpconnect'); if (!$itpConnectParams->get("facebookOn")) { return $success; } if (!JComponentHelper::isEnabled('com_itpconnect', true)) { return $success; } $app =& JFactory::getApplication(); /* @var $app JApplication */ if ($app->isAdmin()) { return $success; } $response->type = 'ItpConnect'; $fbUserId = ItpcHelper::getFB()->getUser(); if (!$fbUserId) { $response->status = JAUTHENTICATE_STATUS_FAILURE; $response->error_message = 'Could not authenticate'; } else { $userId = ItpcHelper::getJUserId($fbUserId); $user = JUser::getInstance($userId); // Bring this in line with the rest of the system if (!$user) { $response->status = JAUTHENTICATE_STATUS_FAILURE; $response->error_message = 'Could not authenticate'; } else { $response->email = $user->email; $response->fullname = $user->name; if (JFactory::getApplication()->isAdmin()) { $response->language = $user->getParam('admin_language'); } else { $response->language = $user->getParam('language'); } $response->status = JAUTHENTICATE_STATUS_SUCCESS; $response->error_message = ''; $success = true; } } return $success; }
* to the GNU General Public License, and as distributed it includes or * is derivative of works licensed under the GNU General Public License or * other free or open source software licenses. */ // no direct access defined('_JEXEC') or die('Restricted access'); if (!class_exists('ItpcHelper')) { // Register Component helpers JLoader::register("ItpcHelper", JPATH_ROOT . DS . "administrator" . DS . "components" . DS . "com_itpconnect" . DS . "helpers" . DS . "itpchelper.php"); } $params->def('greeting', 1); $type = ItpcHelper::getType(); $return = ItpcHelper::getReturnURL($params, $type); $user = JFactory::getUser(); $itpConnectParams = JComponentHelper::getParams('com_itpconnect'); if ($itpConnectParams->get("facebookOn", 0)) { $facebook = ItpcHelper::getFB(); // Get User ID $fbUserId = $facebook->getUser(); $me = null; if ($fbUserId) { try { // Proceed knowing you have a logged in user who's authenticated. $me = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } } require JModuleHelper::getLayoutPath('mod_itpconnect', $params->get('layout', 'default'));