CRE Loaded Community

Banner


Board index » Loaded Commerce Support » Tips & Tricks

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Import Customers from CSV that has Plain Text Passwords
PostPosted: Mon Aug 24, 2009 5:11 pm 
Offline
CRE Newbie
User avatar

Joined: Wed Feb 25, 2009 4:49 pm
Posts: 4
I successfully imported customers from a CSV from an old database that stored passwords in plain text.
This will put them in CRE Loaded using encryption.

1) First off, make sure db.php has your database connection/selection info.

2) $plaintext = $data[3];
Change 3 to whichever column has the password. Keep in mind the first column is 0, so 3 would be the 4th column.

3) You can add/remove columns from the statement below.
You may want to add customers_validation with a value of '1' and customers_account_approval with a value of "Approve" if you're importing valid customers.

For me, data[0] was the customers_id. If your CSV doesn't have this, you may want to add it. Just make sure you don't overwrite existing customers' customers_id. Check phpMyAdmin for the last entry.
There is an autonumber function in Excel that you can paste into the cells. I think you can paste into all selected cells at once, so you don't have to paste a thousand times.
Without this column, you will probably need to look into mysql_insert_id().

$import="INSERT IGNORE INTO customers(customers_id,customers_firstname,customers_lastname,customers_password,customers_email_address,customers_email_registered,customers_group_id) values('$data[0]','$data[1]','$data[2]','$password','$data[4]','$data[4]','$data[6]')";

4) Make sure the customers_info_id value matches the previous statement.
$import2="INSERT IGNORE INTO customers_info(customers_info_id) values('$data[0]')";

5) Create a directory & chmod it to 777. In my example, I used 'temp'. Make sure the path is correct.


uploader.php
Code:
<?php

include("db.php");

// Return a random value
  function tep_rand($min = null, $max = null) {
    static $seeded;

    if (!isset($seeded)) {
      mt_srand((double)microtime()*1000000);
      $seeded = true;
    }

    if (isset($min) && isset($max)) {
      if ($min >= $max) {
        return $min;
      } else {
        return mt_rand($min, $max);
      }
    } else {
      return mt_rand();
    }
  }
 
// Upload/Import stuff
if(!isset($_POST['submit'])){
   $message = "<table align=center><tr><td width=18><img align=left src=\"images/info.gif\"></td><td><b>Please select a file to import.</b></td></tr></table>";
   } else {
      $target_path = "temp/";
      $target_path = $target_path . basename( $_FILES['csv_file']['name']);
      if(move_uploaded_file($_FILES['csv_file']['tmp_name'], $target_path)) {
          $message = "<img src=\"images/check.gif\"> The file <b>" . basename( $_FILES['csv_file']['name']) . "</b> has been imported if there are no errors below.";
         // Read the CSV file, and import into database
         $filename=$target_path . $_POST['csv_file'];
         $handle = fopen("$filename", "r");
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
            if($data[0] != null){
// encrypt password
   $plaintext = $data[3];
   $password = '';

    for ($i=0; $i<10; $i++) {
      $password .= tep_rand();
    }

    $salt = substr(md5($password), 0, 2);

    $password = md5($salt . $plaintext) . ':' . $salt;

// end password encryption

               $import="INSERT IGNORE INTO customers(customers_id,customers_firstname,customers_lastname,customers_password,customers_email_address,customers_email_registered,customers_group_id) values('$data[0]','$data[1]','$data[2]','$password','$data[4]','$data[4]','$data[6]')";
               $import2="INSERT IGNORE INTO customers_info(customers_info_id) values('$data[0]')";
               }
            mysql_query($import) or die(mysql_error());
            mysql_query($import2) or die(mysql_error());
            }
         fclose($handle);
         // If something is wrong with the file, let them know.
         } else {
             $message = "<span style=\"color:#FF0000;\">There was an error uploading the file, please try again!</span>";
            }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSV Uploader</title>
<link rel="stylesheet" href="uploader.css" type="text/css">
</head>
<body>
<h1> Import Customers </h1>
<form method="post" enctype="multipart/form-data">
<table border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="left"><br>
      <div class="message" align="center"><?php echo $message; ?></div>
      <br>
      <br>
     Select the list (CSV) you would like to import.<br>
      <br>
      <input type="file" name="csv_file">
      <br>
      <span style="font-size:10px; font-weight:bold;">CSV files only. Max file size: 5mb.</span><br>
      <br>      <input type="submit" name="submit" value="Import List">
    </td>
  </tr>
</table>
</form>
</body>
</html>


Feel free to add more statements to write to other tables if needed. Thankfully I only needed the 2 :)
I was in a hurry when I wrote the instructions, so let me know if it needs clarification.


Top
 Profile  
 
 Post subject: Re: Import Customers from CSV that has Plain Text Passwords
PostPosted: Mon Jun 07, 2010 9:28 pm 
Offline
CRE Newbie
User avatar

Joined: Mon Mar 29, 2010 12:46 pm
Posts: 8
Your solutions sounds like something I could use! I have a few questions, however.

I want to test this on my local computer (MAMP PHP/MySQL), but I need some clarification on your solution.

1. You mention db.php. I can't find such a file on my local environment. What exactly needs to be in there?
2. - 4. I see that you are referring to changes to your code. Are you saying to create a new file called uploaded.php with the code provided?
5. Create a directory. Is this just to hold the output file from this code? This isn't clear...

I'm also assuming that you need to have your CSV file properly mapped to the CRE Loaded fields, correct?

This sounds really useful, but your instructions need some clarification.

Help! I could really use this!

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

Board index » Loaded Commerce Support » Tips & Tricks

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
It is currently Tue Feb 07, 2012 11:27 am
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

Login

Top Listing

1. Cart2Cart - Shopping...
    Category: Shopping Cart Database Conversion Scripts
    
2. Points & Rewards PLUS!...
    Category: Add-Ons
    
3. Configuration Server...
    Category: Fixes
    
4. Credit Card with CCV
    Category: Payment Modules
    
5. CC7333_ATS
    Category: Templates
    
Show more...

Follow Us on Twitter

An error occurred

Oops, an error seems to have occurred. We're sorry for any inconvenience this might have caused. If the error persists, feel free to tell us about it.

Twitter could not be reached, the server response code was: 401

CRE Loaded Community Chat hosted by CRE Loaded.

Join now


Chat about what's on your mind. More about public chats.


© CRE Loaded is a product of Chain Reaction Ecommerce, Inc. Usage & Privacy Policy