Joomla 5.2.2 Security & Bugfix Release
Joomla 5.2.2 je nyní k dispozici. Jedná se o bezpečnostní vydání pro Joomla 5.x.
Automatický import uživatelů z CSV + cron
03. čvn 2013 16:45 - 03. čvn 2013 16:46 #108202
Prosím o pomoc .
Potřebuji vytvořit PHP script, který by mi načítal uživatele z CSV s těmito podmínkami:
1) nové uživatele přidat
2) uživatele, kteří jsou v CSV i databázi zachovat
3) uživatele, kteří nejsou v CSV vymazat
Scirpt se bude spouštět prostřednictví cronu, protože se databáze neustále aktualizuje v jiném CRM zdroji.
Našel jsem tohle, ale upřímně si s tím nevím moc rady, možná by stačilo šťouchnout.
Kód:
function uploadUserCsv($csvFileSource) {
//Include the Joomla user helper
jimport('joomla.user.helper');
$db = & JFactory::getDBO();
//check if the file exists
if (file_exists($csvFileSource)) {
/* Open the file */
$headerIsSet = FALSE;
if (($handle = fopen($csvFileSource, "r")) !== FALSE) {
/* Counter of users added & skipped */
$added = 0;
$dirty = 0;
$errors = 0;
while (($row = fgetcsv($handle, null, ",")) !== FALSE) {
$newRecord = TRUE;
//if we are working with the first line set the headers, then continue
if ($headerIsSet === FALSE) {
//do some validation to check if the first line is the headers
if (!in_array('name', $row)) {
JError::raiseError(500, 'The first line of the CSV file must be the headers');
}
$headers = $row;
$headerIsSet = TRUE;
continue;
}
$row = (object) array_combine($headers, $row);
/* Build a new user object */
$user = & JTable::getInstance('juser', 'Table');
//check if the user is already in the database.
$query = "SELECT id FROM #__users WHERE email = '{$row->email}'";
$db->setQuery($query);
if ($result = $db->query() !== FALSE && $result->num_rows > 0) {
$result = $db->loadObject();
$user->id = (@$result->id ? : 0);
$newRecord = FALSE;
}
//make sure we set some default values
if (!isset($user->id))
$user->id = 0;
//set the values from the CSV file
$user->name = (@$row->name ? : '');
$user->username = (@$row->username ? : '');
$user->email = (@$row->email ? : '');
//new users
if ($newRecord === TRUE) {
//assign to the "Registered" group
$user->groups = array(2);
if (!isset($row->password))
$row->password = time();
/* Build the password */
$salt = JUserHelper::genRandomPassword(32);
$crypted = JUserHelper::getCryptedPassword(preg_replace("/\s/", "", strtolower($row->password)), $salt);
$newpassword = $crypted . ':' . $salt;
$user->password = $newpassword;
//$user->usertype = "deprecated";
$user->registerDate = date('Y-m-d H:i:s');
}
try {
//try and save the user object
if ($user->store()) {
$added++;
//Create the group allocation to Registered for the new user
if ($newRecord === TRUE) {
$query = "INSERT INTO #__user_usergroup_map (user_id,group_id) values ({$user->id},2)";
$db->setQuery($query);
$db->query();
}
} else {
$errors++;
//JError::raiseError(500, $user->getError());
}
} catch (Exception $exc) {
//echo $exc->getTraceAsString();
$errors++;
}
}
fclose($handle);
} else {
JError::raiseError(500, 'Could not open the CSV file, please check the read access');
}
} else {
die('Could not find users import file: ' . $csvFileSource);
}
die("Uploaded {$added} users, skipped {$dirty} users, and had {$errors} errors.");
}
//to use uploadUserCsv('full/path/to/user/file.scv');
Víc k tomu není: jako např,. kam to vložit, atp.
Díky moc za asistenci.
Potřebuji vytvořit PHP script, který by mi načítal uživatele z CSV s těmito podmínkami:
1) nové uživatele přidat
2) uživatele, kteří jsou v CSV i databázi zachovat
3) uživatele, kteří nejsou v CSV vymazat
Scirpt se bude spouštět prostřednictví cronu, protože se databáze neustále aktualizuje v jiném CRM zdroji.
Našel jsem tohle, ale upřímně si s tím nevím moc rady, možná by stačilo šťouchnout.
Kód:
function uploadUserCsv($csvFileSource) {
//Include the Joomla user helper
jimport('joomla.user.helper');
$db = & JFactory::getDBO();
//check if the file exists
if (file_exists($csvFileSource)) {
/* Open the file */
$headerIsSet = FALSE;
if (($handle = fopen($csvFileSource, "r")) !== FALSE) {
/* Counter of users added & skipped */
$added = 0;
$dirty = 0;
$errors = 0;
while (($row = fgetcsv($handle, null, ",")) !== FALSE) {
$newRecord = TRUE;
//if we are working with the first line set the headers, then continue
if ($headerIsSet === FALSE) {
//do some validation to check if the first line is the headers
if (!in_array('name', $row)) {
JError::raiseError(500, 'The first line of the CSV file must be the headers');
}
$headers = $row;
$headerIsSet = TRUE;
continue;
}
$row = (object) array_combine($headers, $row);
/* Build a new user object */
$user = & JTable::getInstance('juser', 'Table');
//check if the user is already in the database.
$query = "SELECT id FROM #__users WHERE email = '{$row->email}'";
$db->setQuery($query);
if ($result = $db->query() !== FALSE && $result->num_rows > 0) {
$result = $db->loadObject();
$user->id = (@$result->id ? : 0);
$newRecord = FALSE;
}
//make sure we set some default values
if (!isset($user->id))
$user->id = 0;
//set the values from the CSV file
$user->name = (@$row->name ? : '');
$user->username = (@$row->username ? : '');
$user->email = (@$row->email ? : '');
//new users
if ($newRecord === TRUE) {
//assign to the "Registered" group
$user->groups = array(2);
if (!isset($row->password))
$row->password = time();
/* Build the password */
$salt = JUserHelper::genRandomPassword(32);
$crypted = JUserHelper::getCryptedPassword(preg_replace("/\s/", "", strtolower($row->password)), $salt);
$newpassword = $crypted . ':' . $salt;
$user->password = $newpassword;
//$user->usertype = "deprecated";
$user->registerDate = date('Y-m-d H:i:s');
}
try {
//try and save the user object
if ($user->store()) {
$added++;
//Create the group allocation to Registered for the new user
if ($newRecord === TRUE) {
$query = "INSERT INTO #__user_usergroup_map (user_id,group_id) values ({$user->id},2)";
$db->setQuery($query);
$db->query();
}
} else {
$errors++;
//JError::raiseError(500, $user->getError());
}
} catch (Exception $exc) {
//echo $exc->getTraceAsString();
$errors++;
}
}
fclose($handle);
} else {
JError::raiseError(500, 'Could not open the CSV file, please check the read access');
}
} else {
die('Could not find users import file: ' . $csvFileSource);
}
die("Uploaded {$added} users, skipped {$dirty} users, and had {$errors} errors.");
}
//to use uploadUserCsv('full/path/to/user/file.scv');
Víc k tomu není: jako např,. kam to vložit, atp.
Díky moc za asistenci.