<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* General Controller of JUserImporter component
*/
class JUserImporterController extends JControllerLegacy
{
/**
* display task
*
* @return void
*/
function display($cachable = false, $urlparams = array())
{
// set default view if not set
$input = JFactory::getApplication()->input;
$input->set('view', $input->getCmd('view', 'JUserImporter'));
// call parent behavior
parent::display($cachable, $urlparams);
}
/**
* Import task
*/
function importUsers($source = "e:/xampp/htdocs/users2import.csv")
{
/* Check the user file exists */
/* Open the file */
if(!file_exists($source)) $source = $_REQUEST['srcfile'];
$handle = fopen($source,"r");
$content = fread ($handle,filesize ( $source ));
/* Get the file, line by line */
$lines = explode("\n", $content);
fclose($handle);
/* Counter of users added */
$added = 0;
/* Iterate through each line of the data file */
foreach ($lines as $line){
/* Split the line by comma */
$properties = explode(",",$line);
/* Build a new user object */
$user = new JUser();
$data = array();
$data['name'] = $properties[0];
$data['username'] = $properties[1];
$data['email'] = $properties[2];
$data['password'] = $properties[3];
// Load the users plugin group.
JPluginHelper::importPlugin('user');
/* Set the values */
$user->groups = array(2);
if (!$user->bind($data))
{
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_BIND_FAILED', $user->getError()));
return false;
}
/* Build the password */
$salt = JUserHelper::genRandomPassword(32);
$crypted = JUserHelper::getCryptedPassword( preg_replace("/\s/", "", strtolower($line[3])) , $salt);
$newpassword= $crypted . ':' . $salt;
$user->registerDate = date('Y-m-d H:i:s');
if (!$user->save())
{
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
return false;
}
else $added++;
}
if($added) return $added;
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', "Ci sono stati degli errori nella importazione"));
return false;
}
}
non so perché funzioni solo con l’ultimo utente del csv
se ho questi 3 utenti
luca,luca,luca@alice.it,luca-->no
pippo,pippo@libero.com,pippo@outlook.com,ciao---->no
paolo,paolo@libero,paolo@libero.com,paolo2--> con questo utente fa il login corettamente
come mai???