Reading in the import file

Now that the script has checked the template user and the realm id, it starts reading in the actual import file. The file command reads the file and provides the content of the file as an array of the lines. The script then cycles through each line, stripping off the newline character with the rtrim command. The remaining lines will be split at the semi-colon, resulting in an array holding the username and fullname:

if ( file_exists( $import_file ) ) { 
   // read in the import file 
   $lines = file( $import_file ); 
   foreach ($lines as $line) 
   { 
      // cycle through the file 
 
      // remove the new line character from the line 
      $line = rtrim ($line);  
 
      // split the line data at the ";" 
      $data = preg_split("/;/",$line);   
 
      $new_user_username = $data[0]; 
      $new_user_fullname = $data[1]; 

Now the script has all the necessary information to create the new user by copying the template user. The following code does this part of the job:

user_copy($template_user, $new_user_username) 

Copying the template users would leave you with a list of users created, with all of them having the same full name and authentication set to local. The script then goes on and changes the full name and realm id of the newly created user by executing two database SQL UPDATE statements:

db_execute("UPDATE user_auth SET full_name = '".$new_user_fullname."' WHERE username='".$new_user_username."'"); 
 
db_execute("UPDATE user_auth SET realm=".$realm_id." WHERE username='".$new_user_username."'"); 

The first SQL statement updates the full_name field of the new user; the second statement updates the realm field to match your selection from the command line parameter.

This import script is useful for environments where you do not automatically want to create all users based on the same template user, but have different users be assigned different user profiles.