Recent Topics

1 Jun 07, 2012 08:04    

I started using B2 Evo again after a number of years because I have a project requiring multiple blogs and multiple users which B2 is excellent for. However, to me there is a glaring omission in B2 and this is an easy way to create multiple users.

I found a script that tantalisingly almost works which I will post below. It is by Mark Berthelemy Based on: MAMBO USER IMPORT SCRIPT by W. Stewart. I had a discussion with Mark last night and he is happy for me to post this.

The problem with the script is that it calls up a CSV file with users but it only imports the first user in the CSV. I don't know if this is a problem with the script or the CSV

Can anyone spot what is stopping this working?

FIRST THE CSV FILE (just a set of test users)

user2,password,Name1,llask,da,sdd@myteesuni.com
user3,password,namedd,hhads,dadada,zxxzzx@myteesuni.com
user4,password,namessn,hhjad,sffs,gh@myteesuni.com
user5,password,namebsjn,hhjbad,sffsfs,ygjyg@myteesuni.com
user6,password,nameshhs,jkjkad,fssffsfs,hgghda@myteesuni.com
user7,password,namehhs,ad,fsfsfs,ghghds@myteesuni.com

AND HERE IS THE SCRIPT
it downloads entitled b2evo_import.php which I simplified to import.php as it's meant to be run in a browser. Ideally it would be called index.php so that the two files could be placed in a directory say "import" and called up in the browse. I could then drop a new CSV file in for each new batch of users (naturally removing the files after each use for security) - well that's the plan - if I can get it to work.


<html>
<head>
<title>B2evo Import Routine</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php 
/*

Version 0.1

B2evo user import script by M. Berthelemy
Based on: MAMBO USER IMPORT SCRIPT by W. Stewart

This script will import users into b2evolution by updating the evo_users table with data from a .csv text file.

This script:
- imports the users as regular users, 
- sets their password as an MD5 hash from the csv file 
- sets their date of registration to the time of the users import into the database
- sets them up into the basic users group
- prints information and any error messages to the browser window so you can see what's going on
- and has a test mode allowing you to run the script without writing to your database

This script assumes that there is NO header record in the .csv file.

This script assumes that there are six fields per record in the .csv file.  Each record in
the .csv file must be in the format:
"mambo_username", "User's Full Name", "users_email_address"
"Username","Password","Firstname","Lastname","Nickname","Email address"


For example: 
"testuser3","password3","Test","User3","Test User nickname3","test.user3@testing.co.uk"
.
.
. (etc)

If there are more than 6 fields in a record the extra fields will be ignored. An example .csv file 
named "test_userlist.csv" is provided along with this script for testing purposes.

You have to set some variables in the USER VARIABLES section so the script can connect to 
your database and find your .csv file.

TIP: You can test the script without actually writing to the database by setting the 
$testing_mode variable to 1 in the USER VARIABLES section below. THE SCRIPT WILL ONLY WRITE TO THE
DATABASE IF THE $testing_mode VARIABLE IS SET TO 0 (zero). Testing mode allows you to run the script
and see what will happen without affecting your database.

DISCLAMER:
This script is offered as-is. Use it at your own risk. I make no claims regarding its suitability for your particular situation. The havoc you create with it is totally your own responsibility.

Feel free to modify this script to suit your particular needs and to distribute it to those who may find it useful.

Good luck!

Mark
*/

// BEGIN USER VARIABLES ------------------------------------------------------------------------------------------------------|
// Change the variables below to connect to your Mambo database and to point to your .csv file

$sql_hostname = 'localhost';      // hostname or ip address of the mySQL database b2evo uses
                                     // this is usually 'localhost'
$sql_user = 'user';                 // username b2evo uses to connect to your mySQL database
$sql_password = 'password';         // password b2evo uses to connect to your mySQL database
$b2evo_database_name = 'database_name';      // name of your B2evo database on the mySQL server


$path_to_csv = 'userlist.csv';  // path to your CSV text file
$csv_delimiter = ",";	             // character used to separate fields in your .csv file
                                     // this character is usually a comma ","

$testing_mode = 0;                   // set testing mode to a number as follows:
									 // testing = 0 (zero) -> WRITE to the database
									 // testing = 1        -> do NOT WRITE to the database
									 // IF $testing_mode IS SET TO ANY NUMBER OTHER THAN 0 (zero) 
									 // THE SCRIPT WILL NOT WRITE TO THE DATABASE!	
                   
// Set default values for new users

  $user_level = 1; // leave this alone unless you know what you're doing
	$user_locale = "en-GB"; // Change this to your normal locale
	$user_idmode = "namefl"; // Sets to display user details in firstname/surname format
	$user_allow_msgform = "0"; // Switch off user message form by default
	$user_notify = "1"; // Set to send all new posts by email to users
	$user_showonline = "1"; // User will appear in list of online users
	$user_grp_ID = "4"; // Basic Users group
	$user_validated = "1"; // User has been validated                   								 									 									 

// END USER VARIABLES --------------------------------------------------------------------------------------------------------|


// Set up connection to b2evo database
$b2evodb = mysql_connect($sql_hostname, $sql_user, $sql_password) 
      or die ('Not connected to B2evo database: ' . mysql_error());
	  print("Connected to B2evo Database at $sql_hostname<br>");

mysql_select_db($b2evo_database_name, $b2evodb) or die ('Can\'t select B2evo database ' .$b2evo_database_name . ': ' . mysql_error()); 

// Create new starting "id" number for evo_users table by 
// getting the current highest id number and adding 1
$current_id = mysql_query('SELECT MAX(user_ID) AS max_id FROM evo_users');
//print 'current ID = '.$current_id.'<br />';

$row = mysql_fetch_array($current_id);
// print 'Max id = '.$row[max_id];

 	$old_id = $row[max_id];
	$new_id = $old_id + 1;   
	print ("<p>Current highest 'id' in evo_users is: " . $old_id);
	print ("<br>The new 'id' numbers will start at: " . $new_id) . "<br>";

// open the .csv file
$handle = fopen ($path_to_csv,"r"); 

// Check to see if user already exists

while ($datacheck = fgetcsv ($handle, 1000, $csv_delimiter)) {
  $user_logintest = mysql_escape_string($datacheck[0]);
  Print 'Testing for duplicate userid: ' . $user_logintest .' : ';
  $query = "SELECT user_login FROM evo_users WHERE user_login LIKE '$user_logintest'";

  $result = mysql_query ($query);
  $row = mysql_fetch_array ($result);
//  print $row[0] . "<br />";
    if ($row[0] !== NULL) {
//      while ($row = mysql_fetch_array ($result)) {
//        echo $row[0] . "<br />";
//        }
    $fail = 1;
    print '<b>Already exists</b><br />';
    }    else    {
    print 'OK - New user<br />';
    }
}

if ($fail == 1) {
  die ("<p><b>Please check for existing users in the CSV file!</b></p>");
  }




 
// Rewind CSV file back to the start
        rewind($handle);
       
// set up container for CSV data
 
while ($data = fgetcsv ($handle, 1000, $csv_delimiter)) {

	// Here we assign some values for the SQL statements
	// We have to escape the name strings so the SQL doesn't throw errors when it sees 
	// names with embedded quotes like "O'Brien"
	$user_login = mysql_escape_string($data[0]);
	$plain_pwd = $data[1];
	$user_firstname = mysql_escape_string($data[2]);
	$user_lastname = mysql_escape_string($data[3]);
	$user_nickname = mysql_escape_string($data[4]);
	$user_email = $data[5];
	$dateYMDhour = date("Y-m-d H:i:s");
	
	
  $user_pass = md5($plain_pwd);



	if ($testing_mode === 0) { 

			// add users to the B2evo SQL databases		
			mysql_query("INSERT INTO evo_users (user_login, user_pass, user_firstname, user_lastname, user_nickname, user_email, dateYMDhour, user_level, user_locale, user_idmode, user_allow_msgform, user_notify, user_showonline, user_grp_ID, user_validated)
      
      VALUES ('$user_login', '$user_pass', '$user_firstname', '$user_lastname', '$user_nickname', '$user_email', '$dateYMBhour', '$user_level', '$user_locale', '$user_idmode', '$user_allow_msgform', '$user_notify', '$user_showonline', '$user_grp_ID', '$user_validated')")
      
			or die("<br> evo_users not updated. Error is: " . mysql_error());
			
	} 
		
	switch ($testing_mode) { 
		case 0: 
			print ("<br> User: " . $user_firstname . " " . $user_lastname . " added to b2evolution");
			break;
		case 1: 
			print ("<br> In testing mode: User: " . $user_firstname . ' ' . $user_lastname . " would have been added to b2evolution");
			break;
		default: 
			print ("<br> \$testing_mode variable not set properly: User " . $user_firstname . ' ' . $user_lastname . " would have been added to b2evolution");
	} 
	
	// increment id counter
	$new_id++;
}
 
fclose ($handle);

mysql_close($b2evodb);

$num_records = $new_id - $old_id - 1;
print ( "<br><br>" . $num_records . " users added to the b2evolution database");
print("<br> Processing complete at: " . $dateYMDhour);

?>



</body>
</html>

2 Jun 07, 2012 09:27

This script inserts data directly into mysql database ignoring all b2evolution error and dependency checks, plus it doesn't call required actions after user is added.

You should NEVER use this method, don't even bother fixing this script. You can easily adapt the code I posted in another topic to read user data from CSV file.

3 Jun 07, 2012 09:31

OK I'll look to adapt the other code but I'm not a coder and not even a very good hacker. Will go back and take a look at that other one


Form is loading...