Import Users'. Based on DDImportUsers Author: boonebgorges, Dagon Design Version: 0.1 Author URI: http://www.teleogistic.net/ */ /* 2009-01-24 - Modified by Robert McKenzie (rmckenzi@rpmdp.com) http://www.gammaray-tech.com Modifications to this script include the ability to specify a password as well as first and last names. Firstname, Lastname and Password are optional but the fields must be delimited, ie: bill|Bill|Smith|blahblah|bill.smith@blah.com jim|Jim|||Jim@blah.com In the case of a missing password the script will generate one as the original script did and email that password to the new user. If the password has been specified it will also be sent to the user */ $ddui_version = '1.2'; function ddiu_add_management_pages() { if (function_exists('add_management_page')) { add_management_page('Import Users', 'Import Users', 8, __FILE__, 'ddiu_management_page'); } } #can specify how to parse submitted file by editing this function function fileParseFunction($filename){ return file($filename); } #modify this function to specify how to parse text in field #could change format or add validation function fieldParseFunction($text){ return explode("\n", trim($text)); } #specify format information to be displayed to the user $formatinfo = '

The data you enter MUST be in the following format:
   username|email
   username|email
   etc...

'; function ddiu_management_page() { global $wpdb, $wp_roles, $formatinfo, $ddui_version, $the_role; $result = ""; if (isset($_POST['info_update'])) { ?>

No names entered in field.

"; } if ($_FILES['ddui_file']['error'] != UPLOAD_ERR_NO_FILE){#Earlier versions of PHP may use $HTTP_POST_FILES $file = $_FILES['ddui_file']; if($file['error']){ $result .= '

Errors!

'; switch ($file['error']){ case UPLOAD_ERR_INI_SIZE: $result .= "File of ".$file['size']."exceeds max size ".upload_max_filesize; break; case UPLOAD_ERR_FORM_SIZE: $result .= "File of ".$file['size']."exceeds max size ".upload_max_filesize; break; case UPLOAD_ERR_PARTIAL: $result .= "File not fully uploaded"; break; default: } $result.='.

'; } elseif(!is_uploaded_file($file['tmp_name'])){ $result = "File ".$file['name']." was not uploaded via the form."; } else{ #should be ok to read the file now $u_temp = array_merge($u_temp, fileParseFunction($file['tmp_name'])); } } else{ $result .= "

No file submitted.

"; } $u_data = array(); $i = 0; foreach ($u_temp as $ut) { if (trim($ut) != '') { if (! (list($u_n, $u_e ) = @split($delimiter, $ut, 6))){ $result .= "

Regex ".$delimiter." not valid.

"; } $u_n = trim($u_n); $u_e = trim($u_e); $u_data[$i]['username'] = $u_n; $u_data[$i]['email'] = $u_e; $i++; } } // process each user $errors = array(); $complete = 0; $results = array(); foreach ($u_data as $ud) { // check for errors $u_errors = 0; if (!validate_username($ud['username'])) { $results[] = array( 'error' => 'Invalid username: ' . $ud['username'], 'ud' => $ud ); } else if (!is_email($ud['email'])) { $results[] = array( 'error' => 'Invalid email address: ' . $ud['email'], 'ud' => $ud ); $u_errors++; } else { $results[] = ddiu_process_user( $ud ); } } } ?>

Import Users Deluxe!

'; echo '

Results

'; $has_errors = false; $has_new = false; $has_added = false; foreach ( $results as $r ) { if ( $r['error'] ) $has_errors = true; if ( $r['create_success'] ) $has_new = true; if ( $r['added_success'] ) $has_added = true; } ?>

The following new users have been created:

The following users were added to the blog:

These users could not be processed.

Data for these users has been placed back in the User Data box below. Please reconcile and errors and try submitting again.

'; } ?>
" >

User Data




Role for these users:
ID && !$user_by_name->ID ) { // This is a new user and must be created $return = ddiu_add_new_user( $ud ); } else if ( $user_by_email->ID == $user_by_name->ID ) { // If the IDs match, then we're all clear, and the user can be added to the blog $return = ddiu_add_existing_user( $user_by_email->ID, $ud, false ); } else if ( $user_by_name && !$user_by_email ) { // Found a user by name, but not by email // Iterate usernames until we find a unique one $counter = 1; $username = $ud['username']; while ( username_exists( $username ) ) { $username = $ud['username'] . $counter; $counter++; } $ud['username'] = $username; $return = ddiu_add_new_user( $ud ); $return['create_success'] = sprintf( 'The user with the email address %s was created with the username %s and has been added to your blog.', $ud['email'], $ud['username'] ); } else if ( !$user_by_name && $user_by_email ) { // Found a user by email, but not by name // In this case, email is probably a unique enough identifier that we can create a new user and add it to the blog $return = ddiu_add_existing_user( $user_by_email->ID, $ud, false ); $return = array( 'error' => $message ); } else if ( $user_by_email->ID != $user_by_name->ID ) { // Both users exist, but they're different // Throw back to the user for verification $message = 'Username ' . $user_by_name->user_login . ' belongs to email address ' . $user_by_name->user_email . ', and username ' . $user_by_email->user_login . ' belongs to email address ' . $user_by_email->user_email . '. Please reconcile.'; $return = array( 'error' => $message ); } $return['ud'] = $ud; return $return; } function ddiu_add_new_user( $user ) { // generate passwords if none were provided in the import if ($user['password'] == '') { $user['password'] = substr(md5(uniqid(microtime())), 0, 7); } else { $password = $user['password']; } $args = array( "user_login" => $user['username'], "user_pass" => $user['password'], "user_email" => $user['email'] ); // create user $user_id = wp_insert_user( $args ); if (!$user_id) { $message = 'Could not create user ' . $user['username'] . ''; $return = array( 'error' => $message ); } else { $message = 'Username ' . $user['username'] . ' was successfully created.'; $return = array( 'create_success' => $message ); $mailinfo = $args; $added = ddiu_add_existing_user( $user_id, $user, $mailinfo ); if ( isset( $added['added_success'] ) ) $return['added_success'] = $added['added_success']; else $return['added_error'] = 'Could not be added'; } return $return; } function ddiu_add_existing_user( $user_id, $ud, $mailinfo = false ) { global $current_blog, $the_role; // set role if ( $ud['role'] ) $role = $ud['role']; else $role = $the_role; add_user_to_blog( $current_blog->blog_id, $user_id, $role ); // Send the welcome mail $blog_name = get_bloginfo( 'name' ); $blog_url = get_bloginfo( 'url' ); $profile_edit_url = bp_core_get_user_domain( $user_id ) . 'profile/edit/'; // Subjects // The subject of emails to newly created accounts $new_account_subject = 'Your Blogs@Baruch account has beeen created'; // The subject of emails to existing members who've been added to the blog $newly_added_subject = sprintf( 'You have been added to %s', $blog_name ); $subject = ( $mailinfo ) ? $new_account_subject : $newly_added_subject; // The content of the mail $mail_message = ''; // Newly created users get the following text at the top of their email if ( $mailinfo ) { $mail_message .= sprintf( 'Your Blogs@Baruch account has been created. Here is your login info: Username: %s Password: %s Customize your Blogs@Baruch profile at %s ', $ud['username'], $ud['password'], $profile_edit_url ); } // Both existing and newly created users get the following $newly_added_message = sprintf( 'You have been added as a user on the blog %s at %s. Log into %s at %s', $blog_name, $blog_url, $blog_name, $blog_url . '/wp-admin' ); $mail_message .= $newly_added_message; $to = $ud['email']; wp_mail( $to, $subject, $mail_message ); $message = 'Username ' . $ud['username'] . ' has been added successfully to the blog'; return array( 'added_success' => $message ); } function ddiu_save_import( $results ) { if ( !$imports = get_option( 'ddiu_imports' ) ) { add_option( 'ddiu_imports', '', 'Previously imported users', 'no' ); $imports = array(); } $time = time(); $imports[$time] = $results; update_option( 'ddiu_imports', $imports ); } ?>