示例#1
0
 function createnewuser($data)
 {
     $usersConfig =& JComponentHelper::getParams('com_users');
     $authorize =& JFactory::getACL();
     $user = clone JFactory::getUser();
     $error_messages = array();
     $fieldname = array();
     $response = NULL;
     $validated = true;
     $pt = $this->_validatePT($data['jspt']);
     $pt = true;
     if (!$pt) {
         $validated = false;
         $error_messages[] = array("id" => 1, "fieldname" => "jspt", "message" => "Invalid profile type id");
     }
     if ($data['email'] == "") {
         $validated = false;
         $error_messages[] = array("id" => 1, "fieldname" => "email", "message" => "Email cannot be blank");
     } elseif (false == $this->isValidEmail($data['email'])) {
         $validated = false;
         $error_messages[] = array("id" => 1, "fieldname" => "email", "message" => "Please set valid email id eg.(example@gmail.com). Check 'email' field in request");
     }
     if ($data['password'] == "") {
         $validated = false;
         $error_messages[] = array("id" => 1, "fieldname" => "password", "message" => "Password cannot be blank");
     }
     if ($data['name'] == "") {
         $validated = false;
         $error_messages[] = array("id" => 1, "fieldname" => "name", "message" => "Name cannot be blank");
     }
     if (true == $validated) {
         jimport('joomla.filesystem.file');
         jimport('joomla.utilities.utility');
         CFactory::load('helpers', 'image');
         CFactory::load('libraries', 'avatar');
         $userModel =& CFactory::getModel('user');
         if ($data['username'] == "") {
             $username = $data['email'];
         } else {
             $username = $data['username'];
         }
         $user->set('username', $username);
         $user->set('password', $data['password']);
         $user->set('name', $data['name']);
         $user->set('email', $data['email']);
         // password encryption
         $salt = JUserHelper::genRandomPassword(32);
         $crypt = JUserHelper::getCryptedPassword($user->password, $salt);
         $user->password = "******";
         // user group/type
         $user->set('id', '');
         $user->set('usertype', 'Registered');
         $user->set('gid', $authorize->get_group_id('', 'Registered', 'ARO'));
         $date =& JFactory::getDate();
         $user->set('registerDate', $date->toMySQL());
         $storage = JPATH_ROOT . DS . 'images' . DS . 'avatar';
         //kapil
         if ($data['avatar_imagedata'] != "") {
             file_put_contents(JPATH_ROOT . DS . 'user_images/' . $data['username'], base64_decode($data['imagedata']));
             //kapil
             $imagedata = getimagesize(JPATH_ROOT . DS . 'user_images/' . $data['username']);
             rename(JPATH_ROOT . DS . 'user_images/' . $data['username'], JPATH_ROOT . DS . 'user_images/' . $data['username'] . "." . str_replace("image/", "", $imagedata['mime']));
             $imgtype = explode(".", $data['username'] . '.' . str_replace("image/", "", $imagedata['mime']));
             $imgmimetype = $imagedata['mime'];
         } else {
             if ($data['avatar'] != "") {
                 $imgtype = explode(".", $data['avatar']);
                 $imgmimetype = "image/" . $imgtype[1];
             }
         }
         //kapil
         if (isset($imgtype)) {
             $storageImage = $storage . DS . $imgtype[0] . '.' . $imgtype[1];
             $storageThumbnail = $storage . DS . 'thumb_' . $imgtype[0] . '.' . $imgtype[1];
             $image = 'images/avatar/' . $imgtype[0] . '.' . $imgtype[1];
             $thumbnail = 'images/avatar/' . 'thumb_' . $imgtype[0] . '.' . $imgtype[1];
             $imgpath = JPATH_ROOT . DS . 'user_images/' . $imgtype[0] . '.' . $imgtype[1];
             $filetype = $imgmimetype;
         } else {
             $image = 'components/com_community/assets/default.jpg';
             $thumbnail = 'components/com_community/assets/default_thumb.jpg';
         }
         if ($data['avatar'] == "" && $data['avatar_imagedata'] == "") {
             $user->set('avatar', 'components/com_community/assets/default.jpg');
             $user->set('thumb', 'components/com_community/assets/default_thumb.jpg');
         } else {
             if (!file_exists($imgpath)) {
                 $validated = false;
                 $error_messages[] = array("id" => 1, "fieldname" => "avatar", "message" => "Invalid Avatar image path");
             } else {
                 $user->set('avatar', 'images/avatar/' . $imgtype[0] . '.' . $imgtype[1]);
                 $user->set('avatar', 'images/avatar/thumb_' . $imgtype[0] . '.' . $imgtype[1]);
                 $imageMaxWidth = 160;
                 $imageSize = cImageGetSize($imgpath);
                 // Generate full image
                 if (!cImageResizePropotional($imgpath, $storageImage, $filetype, $imageMaxWidth)) {
                     $error_messages[] = array("id" => 1, "fieldname" => "avatar", "message" => "Fail: Error Moving Uploaded File");
                 }
                 // Generate thumbnail
                 if (!cImageCreateThumb($imgpath, $storageThumbnail, $filetype)) {
                     $error_messages[] = array("id" => 1, "fieldname" => "avatar", "message" => "Fail: Error Moving Uploaded File");
                 }
             }
         }
         if (!$user->save()) {
             $error_messages[] = array("id" => 1, "fieldname" => "usernameoremail", "message" => "username or email already in use.");
         } else {
             $my = CFactory::getUser($user->id);
             $userModel->setImage($my->id, $image, 'avatar');
             $userModel->setImage($my->id, $thumbnail, 'thumb');
             // Update the user object so that the profile picture gets updated.
             $my->set('_avatar', $image);
             $my->set('_thumb', $thumbnail);
             // Update Jomsocial profile type
             if ($user->id) {
                 XiptAPI::setUserProfiletype($user->id, $data['jspt']);
             }
         }
     }
     if (true == isset($error_messages) && 0 < sizeof($error_messages)) {
         $res = array();
         foreach ($error_messages as $key => $error_message) {
             $res[] = $error_message;
         }
         $response = array("id" => 0, 'errors' => $res);
     } else {
         $response = array('id' => $user->id);
     }
     return $response;
 }
示例#2
0
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); 

}
示例#3
0
 function uploadAndSetImage($file, $id, $what)
 {
     $mainframe =& JFactory::getApplication();
     CFactory::load('helpers', 'image');
     $config = CFactory::getConfig();
     $uploadLimit = (double) $config->get('maxuploadsize');
     $uploadLimit = $uploadLimit * 1024 * 1024;
     // @rule: Limit image size based on the maximum upload allowed.
     if (filesize($file['tmp_name']) > $uploadLimit) {
         $mainframe->enqueueMessage(XiptText::_('IMAGE_FILE_SIZE_EXCEEDED'), 'error');
         $mainframe->redirect(CRoute::_('index.php?option=com_xipt&view=profiletypes&task=edit&id=' . $id, false));
     }
     if (!cValidImage($file['tmp_name'])) {
         $mainframe->enqueueMessage(XiptText::_('IMAGE_FILE_NOT_SUPPORTED'), 'error');
     } else {
         switch ($what) {
             case 'avatar':
                 $imageMaxWidth = AVATAR_WIDTH;
                 $thumbWidth = AVATAR_WIDTH_THUMB;
                 $thumbHeight = AVATAR_HEIGHT_THUMB;
                 $imgPrefix = 'avatar_';
                 break;
             case 'watermark':
                 $imageMaxWidth = WATERMARK_WIDTH;
                 $thumbWidth = WATERMARK_WIDTH_THUMB;
                 $thumbHeight = WATERMARK_HEIGHT_THUMB;
                 $imgPrefix = 'watermark_';
                 break;
         }
         $storage = PROFILETYPE_AVATAR_STORAGE_PATH;
         $storageImage = $storage . DS . $imgPrefix . $id . cImageTypeToExt($file['type']);
         $storageThumbnail = $storage . DS . $imgPrefix . $id . '_thumb' . cImageTypeToExt($file['type']);
         $image = PROFILETYPE_AVATAR_STORAGE_REFERENCE_PATH . DS . $imgPrefix . $id . cImageTypeToExt($file['type']);
         //$thumbnail			= PROFILETYPE_AVATAR_STORAGE_REFERENCE_PATH . $imgPrefix . $id.'_thumb' . cImageTypeToExt( $file['type'] );
         //here check if folder exist or not. if not then create it.
         if (JFolder::exists($storage) == false) {
             JFolder::create($storage);
         }
         // Only resize when the width exceeds the max.
         if (!cImageResizePropotional($file['tmp_name'], $storageImage, $file['type'], $imageMaxWidth)) {
             $mainframe->enqueueMessage(XiptText::sprintf('COM_XIPT_ERROR_MOVING_UPLOADED_FILE', $storageImage), 'error');
         }
         // Generate thumbnail
         if (!cImageCreateThumb($file['tmp_name'], $storageThumbnail, $file['type'], $thumbWidth, $thumbHeight)) {
             $mainframe->enqueueMessage(XiptText::sprintf('COM_XIPT_ERROR_MOVING_UPLOADED_FILE', $storageThumbnail), 'error');
         }
         $oldFile = XiptLibProfiletypes::getProfiletypeData($id, $what);
         // If old file is default_thumb or default, we should not remove it.
         if (!Jstring::stristr($oldFile, DEFAULT_AVATAR) && !Jstring::stristr($oldFile, DEFAULT_AVATAR_THUMB) && $oldFile != $image && $oldFile != '') {
             // File exists, try to remove old files first.
             $oldFile = JString::str_ireplace('/', DS, $oldFile);
             //only delete when required
             if (JFile::exists($oldFile)) {
                 JFile::delete($oldFile);
             }
         }
         //here due to extension mismatch we can break the functionality of avatar
         if ($what === 'avatar') {
             /* No need to update thumb here , script will update both avatar and thumb */
             //$newThumb   = XiptHelperImage::getThumbAvatarFromFull($newAvatar);
             $oldAvatar = XiptLibProfiletypes::getProfiletypeData($id, 'avatar');
             $allUsers = XiptLibProfiletypes::getAllUsers($id);
             if ($allUsers) {
                 $filter[] = 'avatar';
                 $newData['avatar'] = $image;
                 $oldData['avatar'] = $oldAvatar;
                 foreach ($allUsers as $userid) {
                     XiptLibProfiletypes::updateUserProfiletypeFilteredData($userid, $filter, $oldData, $newData);
                 }
             }
         }
         //now update profiletype with new avatar or watermark
         if (!XiptFactory::getInstance('profiletypes', 'model')->save(array($what => XiptHelperUtils::getUrlpathFromFilePath($image)), $id)) {
             XiptError::raiseError(__CLASS__ . '.' . __LINE__, XiptText::_("ERROR_IN_DATABASE"));
         }
     }
 }