PHP-Code:
<?php
/**
* Contao Member Import Query Builder
*
* Generates an sql file to import contao members taken from a CSV file.
*
* Original Script
* @date 2010-09-18
* @author MarcusD (https://www.contao-community.de/member.php?4573-MarcusD)
* @source https://www.contao-community.de/showthread.php?12413-Mitglieder-per-CSV-importieren&p=88187&viewfull=1#post88187
*
* Refactored
* @date 2012-04-27
* @author soenru (https://www.contao-community.de/member.php?8198-soenru)
*/
/*
* The CSV must contain four columns: First name, last name, email and password.
* The order of the columns does not matter. You can specify the indices in the
* configuration section.
*/
/********************************************************************************
*
* Configuration
*
********************************************************************************/
/*
* Error reporting
*/
error_reporting(-1);
ini_set('display_errors', 1);
/*
* Set the CSV file
* e.g. 'members.csv'
*/
$strSourceFile = '';
/*
* Set the export file
* e.g. 'members.sql'
*/
$strExportFile = '';
/*
* If first line on CSV contains header information, set to true else false
*/
$blnFirstLineHeaders = false;
/*
* Set the field delimiter (one character only)
* e.g. ','
*/
$strCsvDelimiter = '';
/*
* Set the member group assignment(s)
* e.g. 'a:1:{i:0;s:1:"2";}'
*/
$strGroups = '';
/*
* Set the column index of first and last name as well as of the password. This
* is an array index, so 0 (zero) is the first column, 1 the second and so forth.
*/
$intFirstNameIndex = 0;
$intLastNameIndex = 1;
$intEmailIndex = 2;
$intPasswordIndex = 3;
/********************************************************************************
*
* Do not change anything below this line, unless you know what you are doing!
*
********************************************************************************/
/**
* Builds password to store in database from raw password
*
* @param string $password
* @return string
*/
function buildPassword($password)
{
$strSalt = substr(md5(uniqid(mt_rand(), true)), 0, 23);
return sha1($strSalt . $password) . ':' . $strSalt;
}
$strInsertQuery = 'INSERT INTO `tl_member` (`tstamp`, `firstname`, `lastname`, `email`, `language`, `groups`, `login`, `username`, `password`, `dateAdded`) VALUES (UNIX_TIMESTAMP(),\'%s\',\'%s\',\'%s\',\'de\',\'%s\',\'1\',\'%s\',\'%s\',UNIX_TIMESTAMP());' . PHP_EOL;
$intLineCounter = 0;
if (($resSourceFile = fopen($strSourceFile, 'r')) !== false)
{
file_put_contents($strExportFile, '');
while (($arrMember = fgetcsv($resSourceFile, 0, $strCsvDelimiter)) !== false)
{
$intLineCounter++;
if ($intLineCounter === 1 && $blnFirstLineHeaders)
{
continue;
}
file_put_contents($strExportFile, sprintf($strInsertQuery, $arrMember[$intFirstNameIndex], $arrMember[$intLastNameIndex], $arrMember[$intEmailIndex], $strGroups, $arrMember[$intFirstNameIndex] . '.' . $arrMember[$intLastNameIndex], buildPassword($arrMember[$intPasswordIndex])), FILE_APPEND);
}
fclose($resSourceFile);
}
Lesezeichen