function saveJanrainEngageUser($auth_info) 
{
	global $mainframe;
	jimport('joomla.user.helper');
	$db		=& JFactory::getDBO();
	$my 	=& JFactory::getUser();
	$uri 	=& JFactory::getURI();
	$host 	= $uri->getHost();

	// process the auth_info response
	$profileValues 	= $auth_info['profile'];
	$identifier 	= $profileValues['identifier'];	
	
	if( !isset($auth_info['profile']['email'] )) 
	{
		$nameDisp = str_replace(' ','_',$auth_info['profile']['displayName']);
		$auth_info['profile']['email'] = $nameDisp.'@'.$host;
	}
	
	$query = "SELECT `id` FROM #__users WHERE `email`='".$auth_info['profile']['email']."'";
	$db->setQuery($query);
	$userid = $db->loadResult();
	
	$newuser = true;
	if( isset($userid) ) 
	{
		$user =& JFactory::getUser($userid);
		if ($user->id == $userid) 
		{
            $newuser = false;
        }
	}
	if($newuser == true) 
	{
		//save the user
		$user 			= new JUser();
		$authorize 		=& JFactory::getACL();
		$date 			=& JFactory::getDate();
		$uriInfo 		= JFactory::getURI();
		$host 			= $uriInfo->getHost();
		$usersConfig	=& JComponentHelper::getParams( 'com_users' );
		$newUsertype	= $usersConfig->get( 'new_usertype' );
		
		$user->set('id', 0);
		$user->set('usertype', $newUsertype);
		$user->set('gid', $authorize->get_group_id('',$newUsertype, 'ARO'));
		$user->set('registerDate', $date->toMySQL());
		
		if(isset($auth_info['profile']['displayName'])) 
		{
			$displayName = $auth_info['profile']['displayName'];
		} 
		elseif(isset($auth_info['profile']['name']['displayName'])) 
		{
			$displayName = $auth_info['profile']['name']['displayName'];
		}
		
		if(isset($auth_info['profile']['preferredUsername'])) 
		{
			$preferredUsername = $auth_info['profile']['preferredUsername'];
		} 
		elseif(isset($auth_info['profile']['name']['preferredUsername'])) 
		{
			$preferredUsername = $auth_info['profile']['name']['preferredUsername'];
		}

		$user->set('name', $displayName);
		// if username already exists, just add an index to it
		$nameexists = true;
		$index 		= 0;
		$userName 	= $preferredUsername;
		while ($nameexists == true) 
		{
			if(JUserHelper::getUserId($userName) != 0) 
			{
				$index++;
				$userName = $preferredUsername.$index;
			} 
			else 
			{
				$nameexists = false;
			}
		}
		$user->set('username', $userName);
	  
		$sEmail = '';
		if(isset($auth_info['profile']['email'])) 
		{
			$sEmail = $auth_info['profile']['email'];
			$user->set('email', $auth_info['profile']['email']);
		} 
		elseif (isset($auth_info['profile']['name']['email'])) 
		{
		  	$sEmail = $auth_info['profile']['email'];
		  	$user->set('email', $auth_info['profile']['email']);
		} 
		
		$pwd = JUserHelper::genRandomPassword();
		$user->set('password', $pwd);
		
		if (!$user->save()) 
		{
			echo "ERROR: ";
			echo $user->getError();
		}
		
		// admin users gid
		$gid 		= 25;
		$query 		= "SELECT `email`, `name` FROM `#__users` WHERE `gid` = '".$gid."'";
		$db->setQuery( $query );		
		$adminRows 	= $db->loadObjectList();
	
		// send email notification to admins
		if( !empty($adminRows) ) 
		{
			foreach($adminRows as $adminRow) 
			{
				$sitename 	= $mainframe->getCfg( 'sitename' );
				$siteRoot   = JURI::base();
			
				$userName	= $user->get('username');
				$userID		= $user->get('id');
				$userTupe	= $user->get('usertype');
				$userEmail	= $user->get('email');
				$adminName 	= $adminRow->name;
				$adminEmail = $adminRow->email;
				
				$subject	= JText::_('New user registered via JAINARAIN ENGANGE at')." ".$sitename;
				$subject 	= html_entity_decode($subject, ENT_QUOTES);	
		
				$message 	= JText::_('Hello')." ".$adminName."\n";
				$message 	.= JText::_('New user registered via JAINARAIN ENGANGE at')." ".$siteRoot."\n\n";
				$message 	.= JText::_('User Detail:')."\n";
				$message 	.= JText::_('User ID :')." ".$userID."\n";
				$message 	.= JText::_('Usertype :')." ".$userTupe."\n";
				$message 	.= JText::_('Name :')." ".$displayName."\n";
				$message 	.= JText::_('Username :'******'Email :')." ".$sEmail."\n";
				$message 	= html_entity_decode($message, ENT_QUOTES);
	
				JUtility::sendMail( $userName, $userEmail, $adminEmail,  $subject, $message );
			}
		}
			
		// check if the community builder tables are there
		$query 			= "SHOW TABLES LIKE '%__comprofiler'";
		$db->setQuery($query);
		$tableexists	= $db->loadResult();

		if( isset($tableexists) ) 
		{
			 $cbquery = "INSERT IGNORE INTO #__comprofiler(id,user_id,firstname,lastname) VALUES ('".$user->get('id')."','".$user->get('id')."','".$auth_info['profile']['name']['givenName']."','".$auth_info['profile']['name']['familyName']."')";
			$db->setQuery($cbquery);
			if (!$db->query()) 
			{
				JERROR::raiseError(500, $db->stderror());
			}
			else 
			{
				if($auth_info['profile']['photo']) 
				{
					global $_CB_database, $_CB_framework,   $ueConfig, $_PLUGINS ;
					if ( defined( 'JPATH_ADMINISTRATOR' ) ) 
					{
						include_once JPATH_ADMINISTRATOR . '/components/com_comprofiler/plugin.foundation.php';
						require_once $_CB_framework->getCfg( 'absolute_path' ) . '/components/com_comprofiler/comprofiler.html.php';						
					} 
					else 
					{						
						include_once $mainframe->getCfg( 'absolute_path' ). '/administrator/components/com_comprofiler/plugin.foundation.php';						
						require_once $mainframe->getPath( 'front_html' );						
					}
					$filename		=	urldecode(uniqid($user->get('id')."_"));
					
					// replace every space-character with a single "_"
					$filename		=	preg_replace( "/ /", "_",	 $filename );				
					// Get rid of extra underscores						
					$filename		=	preg_replace( "/_+/", "_",	 $filename );						
					$filename		=	preg_replace( "/(^_|_$)/", "", $filename );						
					$tag			=	preg_replace( "/^.*\\.([^\\.]*)$/", "\\1", $auth_info['profile']['photo'] );	
					$tag			=	strtolower( $tag );						
					$newFileName		=	$filename . ".jpg";					 
					$file		=	$_CB_framework->getCfg('absolute_path') . '/images/comprofiler/' . $newFileName;						
					copy( $auth_info['profile']['photo'], $file );						
					
					$db->setQuery("UPDATE #__comprofiler SET avatar='" .$newFileName . "', avatarapproved=1, lastupdatedate='".date('Y-m-d\TH:i:s')."' WHERE id=" . (int) $user->get('id'));						
					$db->query();
				}
			}
		}
					
		// check if the Jomsocial tables are there, then set avatar
		$query = "SHOW TABLES LIKE '%__community_users'";
		$db->setQuery($query);
		$Jomtableexists = $db->loadResult();

		if (isset($Jomtableexists) && $auth_info['profile']['photo']) 
		{
			jimport('joomla.filesystem.file');
			jimport('joomla.utilities.utility');
			require_once(JPATH_ROOT.DS.'components'.DS.'com_community'.DS.'helpers'.DS.'image.php');
				
			$fileName	= JUtility::getHash( $auth_info['profile']['photo'] . time() );
			$fileName	= JString::substr( $fileName , 0 , 24 );
								   
			$avatarimage		= 'images/avatar/' . $fileName.'.jpg' ;
			$thumbavatar		= 'images/avatar/' . 'thumb_' . $fileName.'.jpg' ;
							

			$st = JPATH_ROOT;
			$jPath = split('\administrator',$st);
			 
			$storage	 = $jPath[0] . DS . 'images' . DS . 'avatar'. DS .   $fileName.'.jpg';
			$storageThumbnail	= $jPath[0] . DS .'images'.DS . 'avatar'. DS . 'thumb_' . $fileName.'.jpg' ;
			$destType = 'image/jpg';
			$imageMaxWidth	= 140; 
			   
			// Only resize when the width exceeds the max.
			if( !cImageResizePropotional( $auth_info['profile']['photo'] , $storage , $destType , $imageMaxWidth ) ) 
			{
				global $option,$mainframe;
				$msg = JText::sprintf( 'Image Upload Error '); 	
			}
	
			// Generate thumbnail
			if(!cImageCreateThumb( $auth_info['profile']['photo'] , $storageThumbnail , $destType  )) 
			{
				global $option,$mainframe;
				$msg = JText::sprintf( 'Image Upload Error '); 	
			}			
														 
			$query = "SELECT `userid` FROM `#__community_users` WHERE `userid`='" . $user->get('id') . "'";
			$db->setQuery( $query );
			if($db->loadResult()) 
			{				
				$query = "UPDATE `#__community_users` SET `avatar` = '" . $avatarimage . "', `thumb` = '" .$thumbavatar . "' WHERE `userid`='" . $user->get('id') . "'";
			}
			else 
			{
				$query = "INSERT INTO `#__community_users` SET `userid`='" . $user->get('id') . "', `avatar` = '" . $avatarimage . "', `thumb` = '" .$thumbavatar . "'";
			}
			$db->setQuery( $query );
			$db->query();		 
		}
	}

	// Get an ACL object
	$acl =& JFactory::getACL();

	// Get the user group from the ACL
	if ($user->get('tmp_user') == 1) 
	{
		$grp = new JObject;
		// This should be configurable at some point
		$grp->set('name', 'Registered');
	} 
	else 
	{
		$grp = $acl->getAroGroup($user->get('id'));
	}

	//Mark the user as logged in
	$user->set( 'guest', 0 );
	$user->set( 'aid', 1 );

	// Fudge Authors, Editors, Publishers and Super Administrators into the special access group
	if($acl->is_group_child_of($grp->name, 'Registered') || $acl->is_group_child_of($grp->name, 'Public Backend')) 
	{
		 $user->set('aid', 2);
	}

	//Set the usertype based on the ACL group name
	$user->set('usertype', $grp->name);

	// Register the needed session variables
	$session =& JFactory::getSession();
	$session->set('user', $user);

	// Get the session object
	$table =& JTable::getInstance('session');
	$table->load( $session->getId() );
	$table->guest           = $user->get('guest');
	$table->username        = $user->get('username');
	$table->userid          = intval($user->get('id'));
	$table->usertype        = $user->get('usertype');
	$table->gid             = intval($user->get('gid'));

	$table->update();

	// Hit the user last visit field
	$user->setLastVisit();
	 
	// redirect
	global $redirectUrl;
	$returnURL = $redirectUrl;
	$mainframe->redirect($returnURL); 

}
Example #2
0
 function saveUser($auth_info)
 {
     // process the auth_info response
     if ($auth_info['stat'] == 'ok') {
         $db =& JFactory::getDBO();
         $rpxid = 'rpx' . md5($auth_info['profile']['identifier']);
         $query = "SELECT userid FROM #__rpx_mapping WHERE rpxid='" . $rpxid . "'";
         $db->setQuery($query);
         $userid = $db->loadResult();
         $newuser = true;
         if (isset($userid)) {
             $user =& JFactory::getUser($userid);
             if ($user->id == $userid) {
                 $newuser = false;
             } else {
                 // possible if previous registered, but meanwhile removed
                 // we have a userid without user...remove from the rpx_mapping
                 $query = "DELETE FROM #__rpx_mapping WHERE userid='" . $userid . "'";
                 $db->setQuery($query);
                 $db->query();
             }
         }
         if ($newuser == true) {
             // save the user
             $user = new JUser();
             $authorize =& JFactory::getACL();
             $newUsertype = 'Registered';
             $user->set('id', 0);
             $user->set('usertype', '');
             $user->set('gid', $authorize->get_group_id('', $newUsertype, 'ARO'));
             $date =& JFactory::getDate();
             $user->set('registerDate', $date->toMySQL());
             if (isset($auth_info['profile']['displayName'])) {
                 $displayName = $auth_info['profile']['displayName'];
             } else {
                 if (isset($auth_info['profile']['name']['displayName'])) {
                     $displayName = $auth_info['profile']['name']['displayName'];
                 }
             }
             if (isset($auth_info['profile']['preferredUsername'])) {
                 $preferredUsername = $auth_info['profile']['preferredUsername'];
             } else {
                 if (isset($auth_info['profile']['name']['preferredUsername'])) {
                     $preferredUsername = $auth_info['profile']['name']['preferredUsername'];
                 }
             }
             $user->set('name', $displayName);
             // if username already exists, just add an index to it
             $nameexists = true;
             $index = 0;
             $userName = $preferredUsername;
             while ($nameexists == true) {
                 if (JUserHelper::getUserId($userName) != 0) {
                     $index++;
                     $userName = $preferredUsername . $index;
                 } else {
                     $nameexists = false;
                 }
             }
             $user->set('username', $userName);
             $host = JFactory::getURI()->getHost();
             $domain = substr($host, 4);
             // strips the www.
             if ($this->params->get('fakemail') == 0) {
                 if (isset($auth_info['profile']['email'])) {
                     $user->set('email', $auth_info['profile']['email']);
                 } else {
                     if (isset($auth_info['profile']['name']['email'])) {
                         $user->set('email', $auth_info['profile']['email']);
                     } else {
                         $user->set('email', str_replace(" ", "_", $userName) . "@" . $domain);
                     }
                 }
             } else {
                 $user->set('email', str_replace(" ", "_", $userName) . "@" . $domain);
             }
             $pwd = JUserHelper::genRandomPassword();
             $user->set('password', $pwd);
             if (!$user->save()) {
                 echo "ERROR: ";
                 echo $user->getError();
             } else {
                 $query = "INSERT INTO #__rpx_mapping (userid, rpxid) VALUES ('" . $user->get('id') . "','" . $rpxid . "')";
                 $db->setQuery($query);
                 if (!$db->query()) {
                     JERROR::raiseError(500, $db->stderror());
                 }
             }
             // check if the community builder tables are there
             $query = "SHOW TABLES LIKE '%__comprofiler'";
             $db->setQuery($query);
             $tableexists = $db->loadResult();
             if (isset($tableexists)) {
                 $cbquery = "INSERT IGNORE INTO #__comprofiler(id,user_id) VALUES ('" . $user->get('id') . "','" . $user->get('id') . "')";
                 $db->setQuery($cbquery);
                 if (!$db->query()) {
                     JERROR::raiseError(500, $db->stderror());
                 }
             }
         }
         // Get an ACL object
         $acl =& JFactory::getACL();
         // Get the user group from the ACL
         if ($user->get('tmp_user') == 1) {
             $grp = new JObject();
             // This should be configurable at some point
             $grp->set('name', 'Registered');
         } else {
             $grp = $acl->getAroGroup($user->get('id'));
         }
         //Mark the user as logged in
         $user->set('guest', 0);
         $user->set('aid', 1);
         // Fudge Authors, Editors, Publishers and Super Administrators into the special access group
         if ($acl->is_group_child_of($grp->name, 'Registered') || $acl->is_group_child_of($grp->name, 'Public Backend')) {
             $user->set('aid', 2);
         }
         //Set the usertype based on the ACL group name
         $user->set('usertype', $grp->name);
         // Register the needed session variables
         $session =& JFactory::getSession();
         $session->set('user', $user);
         // Get the session object
         $table =& JTable::getInstance('session');
         $table->load($session->getId());
         $table->guest = $user->get('guest');
         $table->username = $user->get('username');
         $table->userid = intval($user->get('id'));
         $table->usertype = $user->get('usertype');
         $table->gid = intval($user->get('gid'));
         $table->update();
         // Hit the user last visit field
         $user->setLastVisit();
     }
 }