GRRR attachment didn't stick...
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class JFBConnectCommunityBuilderHelper
{
function importFBProfile()
{
$configModel = JModel::getInstance('JFBConnectModelConfig');
if ($configModel->getSetting('cb_enable'))
{
require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_comprofiler'.DS.'plugin.foundation.php' );
require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_comprofiler'.DS.'plugin.class.php' );
require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_comprofiler'.DS.'comprofiler.class.php' );
require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_comprofiler'.DS.'library'.DS.'cb'.DS.'cb.database.php' );
require_once( JPATH_ADMINISTRATOR.DS.'components'.DS.'com_comprofiler'.DS.'library'.DS.'cb'.DS.'cb.tables.php' );
include_once(JPATH_SITE.DS.'components'.DS.'com_comprofiler'.DS.'plugin'.DS.'language'.DS.'default_language'.DS.'default_language.php');
require_once(JPATH_ROOT.DS.'components'.DS.'com_jfbconnect'.DS.'helpers'.DS.'facebookhelper.php');
$fbClient = JFBConnectFacebookHelper::getFbClient();
$fbUserInfo = JFBConnectFacebookHelper::_getUserInfo(JFBConnectFacebookHelper::$_OrigFbUser);
$jUser = JFactory::getUser();
$test = $jUser->id;
$test2 = $fbUserInfo;
$array = array(
'id' => $jUser->id,
'name' => $jUser->name,
'firstname' => $fbUserInfo,
'lastname' => $fbUserInfo,
'username' => $jUser->username,
'email' => $jUser->email
//'password' => 'h',
//'password__verify' => 'blahblah'
);
global $_CB_database;
$user = new moscomprofilerUser($_CB_database);
$user->load($jUser->id);
$ui = "1";
$reason = "edit";
$user->saveSafely( $array, $ui, $reason );
//Copy over the FB Avatar to CB...
####### REMOVED THIS COS I WANT TO IMPORT ALWAYS AND I NEED TO MOVE THIS BELOW THE DB STUFF
//if ($configModel->getSetting('cb_import_avatar'))
// Wish I could use CB's changeUsersStatus call, but it has a redirect at the end
$dbo = JFactory::getDBO();
$dbo->execute("UPDATE #__comprofiler SET approved=1,confirmed=1,cbactivation='' WHERE user_id = ".$jUser->id);
// Reload user again. Could just set the values written above directly, but it's nice to know the db txn succeeeded.
$user->load($jUser->id);
$test = $jUser->id;
// Trigger OnBeforeActivate/onUserActive plugins.
// Send Welcome email and notify admins of new user.
activateUser($user, 0, "UserRegistration", true, true, true);
JFBConnectCommunityBuilderHelper::migrateFBFieldsToCB();
#### REPLACED THIS HERE COS IT MUST GO AFTER THE FUNCTION ABOVE
JFBConnectCommunityBuilderHelper::migrateFBAvatarToCB();
}
}
function migrateFBFieldsToCB()
{
$user = JFactory::getUser();
$userId = $user->get('id');
$dbo = JFactory::getDBO();
require_once (JPATH_ROOT.DS.'components'.DS.'com_jfbconnect'.DS.'helpers'.DS.'facebookhelper.php');
$fbClient = JFBConnectFacebookHelper::getFbClient();
$configModel = JModel::getInstance('JFBConnectModelConfig');
$fbFieldMap = $configModel->getSetting('cbFieldMap');
$fbFields = array();
foreach($fbFieldMap as $fbField)
{
$fbFieldArray = explode('.', $fbField);
$fbFields[] = $fbFieldArray[0];
}
if (empty($fbFields))
return;
$fbProfileFields = $fbClient->users_getInfo($fbClient->user, $fbFields);
$sql = "";
foreach($fbFieldMap as $fieldId=>$fbField)
{
$addRow = true;
$fbFieldArray = explode('.', $fbField);
if (count($fbFieldArray) == 1)
{
if (isset($fbProfileFields[0][$fbFieldArray[0]]))
$value = $fbProfileFields[0][$fbFieldArray[0]];
else
$addRow = false;
}
else if (count($fbFieldArray) == 2)
{
if (isset($fbProfileFields[0][$fbFieldArray[0]][$fbFieldArray[1]]))
$value = $fbProfileFields[0][$fbFieldArray[0]][$fbFieldArray[1]];
else
$addRow = false;
}
else if (count($fbFieldArray) == 3)
{
if (isset($fbProfileFields[0][$fbFieldArray[0]][$fbFieldArray[1]][$fbFieldArray[2]]))
$value = $fbProfileFields[0][$fbFieldArray[0]][$fbFieldArray[1]][$fbFieldArray[2]];
else
$addRow = false;
}
if ($addRow)
{
//query the com_profiler_fields to get the column names
$query = 'SELECT name FROM #__comprofiler_fields WHERE fieldid='.$fieldId;
$dbo->setQuery($query);
$colName = $dbo->loadResult();
$userValue = $dbo->quote($value);
//Update jos_comprofiler for value for each column by id=$userId ### tweaked this a bit
$sql .= "UPDATE jos_comprofiler SET `$colName` = $userValue WHERE id = '$userId' ;";
}
}
// this removes the dodgey username and email forcing them to enter a new one
$userQuery = "UPDATE jos_users SET `username` = '', `email` = '' WHERE id = '$userId' ;";
// this creates the comprofiler profile so that it can be mapped with data by the $sql query
$userQuery .= " INSERT INTO jos_comprofiler (`id`, `user_id`) VALUES ('$userId', '$userId');";
$sql = $userQuery." ".$sql;
//this gets appended to the end instead of looping every time in the loop above
$sql .= " UPDATE jos_comprofiler SET approved = 1, confirmed = 1 WHERE id = '$userId';";
$dbo->setQuery($sql);
$dbo->queryBatch();
}
function migrateFBAvatarToCB()
{
require_once (JPATH_ROOT.DS.'components'.DS.'com_jfbconnect'.DS.'helpers'.DS.'facebookhelper.php');
$fbClient = JFBConnectFacebookHelper::getFbClient();
$avatarField = array(0=>'pic_big');
$avatarUrlArray = $fbClient->users_getInfo($fbClient->user, $avatarField);
$avatarUrl = $avatarUrlArray[0];
$fbImgPath = 'jfbc_'.$fbClient->user.'pic_tmp.jpg';
if (function_exists('curl_init'))
{
$fp = fopen (JPATH_SITE.DS.'images'.DS.'comprofiler'.DS.$fbImgPath, 'w+'); // Save location
$ch = curl_init($avatarUrl); // Download location
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
else
copy($avatarUrl, JPATH_SITE.DS.'images'.DS.$fbImgPath);
jimport('joomla.filesystem.file');
jimport('joomla.utilities.utility');
global $mainframe;
$user = JFactory::getUser();
$userId = $user->get('id');
$dbo = JFactory::getDBO();
//require_once(JPATH_SITE.DS.'components'.DS.'com_comprofiler'.'comprofiler.php');
if (JFBConnectCommunityBuilderHelper::_addCBImage($fbImgPath, $userId))
{
$mainframe->enqueueMessage("Successfully imported avatar from Facebook.");
}
else
{ # there was a problem adding the image, use the default
$query = "UPDATE #__comprofiler SET `avatar`=NULL WHERE id = ".$userId;
$dbo->execute($query);
$mainframe->enqueueMessage("There was an issue importing your Facebook avatar. The default avatar will be used.", 'error');
}
return true;
}
function _addCBImage($fbAvatar, $userId)
{
global $mainframe;
global $ueConfig;
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_comprofiler'.DS.'imgToolbox.class.php');
$dbo = JFactory::getDBO();
$errorDetected = false;
// Get a hash for the file name.
$fbAvatarFileName = substr( $fbAvatar, 0, strpos($fbAvatar, '.') );
//@todo: configurable path for avatar storage?
$fbExtension = substr($fbAvatar, strpos($fbAvatar, '.'));
$storage = JPATH_ROOT . DS . 'images' . DS . 'comprofiler' . DS;
$storageImage = $storage . DS . $fbExtension ;
$storageThumbnail = $storage . DS . 'tn' . $fbExtension ;
$image = $fbExtension ;
$thumbnail = 'tn' . $fbExtension ;
$imgToolBox = new imgToolBox();
$imgToolBox->_conversiontype = $ueConfig;
$imgToolBox->_IM_path = $ueConfig;
$imgToolBox->_NETPBM_path = $ueConfig;
$imgToolBox->_maxsize = $ueConfig;
$imgToolBox->_maxwidth = $ueConfig;
$imgToolBox->_maxheight = $ueConfig;
$imgToolBox->_thumbwidth = $ueConfig;
$imgToolBox->_thumbheight = $ueConfig;
$imgToolBox->_debug = 0;
$allwaysResize = ( isset( $ueConfig ) ? $ueConfig : 1 );
$imageArray = array();
$imageArray = $fbAvatar;
$imageArray = $fbAvatarFileName;
$imageArray = $fbExtension;
$newFileName = $imgToolBox->processImage( $imageArray, $fbAvatarFileName, $storage, 0, 0, 29, $allwaysResize );
//tweaked the sytax here
$query = "UPDATE jos_comprofiler SET `avatar` = '$newFileName' WHERE `id`= '$userId'";
$dbo->execute($query);
return true;
}
}