/**
* This function allows the administrator to import batches of users
*
* $file string file to import
*
*/
function importusers($file)
{
global $_TABLES, $LANG04, $LANG28, $_CONF, $HTTP_POST_FILES;
// Setting this to true will cause import to print processing status to
// webpage and to the error.log file
$verbose_import = true;
$log_import = false;
// First, upload the file
require_once($_CONF['path_system'] . "classes/upload.class.php");
$upload = new upload();
$upload->setPath($_CONF['path']);
$upload->setAllowedMimeTypes(array('text/plain'=>'.txt'));
$upload->setFileNames('user_import_file.txt');
if ($upload->uploadFiles()) {
// Good, file got uploaded, now install everything
$thefile = current($HTTP_POST_FILES);
$filename = $_CONF['path'] . 'user_import_file.txt';
} else {
// A problem occurred, print debug information
print 'ERRORS
';
$upload->printErrors();
exit;
}
$retval = '';
$handle = @fopen($filename,'r');
if (empty ($handle)) {
return $LANG28[34];
}
// retrieve field labels first; result is arry 1 => name
$userlabels=fgets($handle,4096);
$userlabels=rtrim($userlabels);
$filefields=explode("\t",$userlabels);
$imp_label=array();
foreach ($filefields as $akey => $avalue) {
$imp_label[$avalue]=$akey;
}
strtr($userlabels,"\t","=");
if ($verbose_import) {
$retval .= "
Labels found : $userlabels";
}
// Following variables track import processing statistics
$successes = 0;
$failures = 0;
while ($user1 = fgets($handle,4096)) {
$user = rtrim($user1);
$imp_value=explode("\t",$user);
$username = $imp_value[$imp_label['username']];
$fullname = $imp_value[$imp_label['fullname']];
$email = $imp_value[$imp_label['email']];
$ucount = DB_count($_TABLES['users'],'username',$username);
$ecount = DB_count($_TABLES['users'],'email',$email );
if ($verbose_import) {
$retval .="
Working on username=$username, fullname=$fullname, and email=$email
\n";
}
if ($log_import) {
COM_errorLog("Working on username=$username, fullname=$fullname, and email=$email",1);
}
if ($ucount == 0 && ecount == 0) {
// user doesn't already exist
if (COM_isEmail($email)) {
// email is valid form
foreach ($imp_label as $akey => $avalue) {
${$akey} = $imp_value[$avalue];
}
if (empty($regdate)) {
$regdate = strftime('%Y-%m-%d %H:%M:%S',time());
}
$passwd = md5($passwd);
// Create user record
DB_query("INSERT INTO {$_TABLES['users']} (username,fullname,email,passwd,regdate) VALUES ('$username','$fullname','$email','$passwd','$regdate')");
$uid = DB_getItem($_TABLES['users'],'uid',"username = '$username'");
// Add user to Logged-in group (i.e. members) and the All Users
// group (which includes anonymous users)
$normal_grp = DB_getItem($_TABLES['groups'],'grp_id',"grp_name='Logged-in Users'");
$all_grp = DB_getItem($_TABLES['groups'],'grp_id',"grp_name='All Users'");
$this_grp = DB_getItem($_TABLES['groups'],'grp_id',"grp_name='$new_grp'");
DB_query("INSERT INTO {$_TABLES['group_assignments']} (ug_main_grp_id,ug_uid) values ($normal_grp, $uid)");
DB_query("INSERT INTO {$_TABLES['group_assignments']} (ug_main_grp_id,ug_uid) values ($all_grp, $uid)");
if (!empty ($this_grp) ) {
DB_query("INSERT INTO {$_TABLES['group_assignments']} (ug_main_grp_id,ug_uid) values ($this_grp, $uid)");
}
DB_query("INSERT INTO {$_TABLES['userprefs']} (uid) VALUES ($uid)");
if ($_CONF['emailstoriesperdefault'] == 1) {
DB_query("INSERT INTO {$_TABLES['userindex']} (uid) VALUES ($uid)");
} else {
DB_query("INSERT INTO {$_TABLES['userindex']} (uid,etids) VALUES ($uid, '-')");
}
DB_query("INSERT INTO {$_TABLES['usercomment']} (uid) VALUES ($uid)");
DB_query("INSERT INTO {$_TABLES['userinfo']} (uid) VALUES ($uid)");
// Call custom registration and account record create function
// if enabled and exists
if ($_CONF['custom_registration'] AND (function_exists(custom_userimport))) {
custom_userimport($uid,$imp_value);
}
PLG_createUser ($uid);
if ($verbose_import) {
$retval .= "
Account for $username created successfully.
\n";
}
if ($log_import) {
COM_errorLog("Account for $username created successfully",1);
}
$successes++;
} else {
if ($verbose_import) {
$retval .= "
$email is not a valid email address, account not created
\n"; // malformed email
}
if ($log_import) {
COM_errorLog("$email is not a valid email address, account not created",1);
}
$failures++;
} // end if COM_isEmail($email)
} else {
if ($verbose_import) {
$retval .= "
$username already exists, account not created.
\n"; // users already exists
}
if ($log_import) {
COM_errorLog("$username,$email: username or email already exists, account not created",1);
}
$failures++;
} // end if $ucount == 0 && ecount == 0
} // end while
fclose($handle);
unlink($filename);
$report = $LANG28[32];
eval ("\$report = \"$report\";");
$retval .= '
' . $report; return $retval; } // end importusers