<?php
define('IN_PHPBB', true);
$phpbb_root_path =  '../';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
require($phpbb_root_path . 'includes/functions_user.' . $phpEx);
$user->session_begin();
$auth->acl($user->data);
$user->setup('ucp');
$import_dbname = 'login';
$import_table = 'bb_users';
$champ_user = 'pseudo';
$champ_password = 'password';
$champ_email = 'email';
$champ_activer ='activer';
define('MD5_PASSWORD', true);
include($phpbb_root_path . 'config.' . $phpEx);
if ($import_dbname == $dbname)
{
	$import_db = $db;
}
else
{
	$db_passwd = '';
	$import_db = new $dbms();
	$import_db->sql_connect($dbhost, $dbuser, $dbpasswd, $import_dbname, $dbport, false, true);
	unset($db_passwd);
}
$success_list=array();
$ignore_list = array();

$group_name =  'REGISTERED';
$sql = 'SELECT group_id
		FROM ' . GROUPS_TABLE . "
		WHERE group_name = '" . $db->sql_escape($group_name) . "'
			AND group_type = " . GROUP_SPECIAL;
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if (!$row)
{
	trigger_error('NO_GROUP');
}
$group_id = $row['group_id'];

$sql = "SELECT $champ_user, $champ_password, $champ_email,$champ_activer FROM $import_table";
$result = $import_db->sql_query($sql);
while($row = $import_db->sql_fetchrow($result))
{
    if ($row[$champ_activer]==0)
    {
        $error = array();
        $data = array(
            'username'			=> utf8_normalize_nfc($row[$champ_user]),
            'password'			=> $row[$champ_password],
            'email'				=> strtolower($row[$champ_email]),
        );
        $error = validate_data($data, array(
            'username'			=> array(
                array('string', false, $config['min_name_chars'], $config['max_name_chars']),
                array('username', '')),
            'email'				=> array(
                array('string', false, 6, 60),
                array('email')),
        ));
        $error = preg_replace('#^([A-Z_]+)$#e', "(!empty(\$user->lang['\\1'])) ? \$user->lang['\\1'] : '\\1'", $error);
        if (!sizeof($error))
        {
            $user_row = array(
                'username'				=> $data['username'],
                'user_password'			=> (defined('MD5_PASSWORD')) ? $data['password'] : phpbb_hash($data['password']),
                'user_email'			=> $data['email'],
                'group_id'				=> (int) $group_id,
                'user_timezone'			=> (float) $config['board_timezone'],
                'user_lang'				=> basename($user->lang_name),
                'user_type'				=> USER_NORMAL,
                'user_actkey'			=> '',
                'user_ip'				=> $user->ip,
                'user_regdate'			=> time(),
                'user_inactive_reason'	=> 0,
                'user_inactive_time'	=> 0,
            );
            $user_id = user_add($user_row);
            if ($user_id === false)
            {
                trigger_error('NO_USER', E_USER_ERROR);
            }
            $success_list[] = $data['username'];
        }
        else
        {
            $ignore_list[] = '<td>' . $data['username'] . '</td><td>' . implode('<br />', $error) . '</td>';
        }
} 
}
$sql= "UPDATE  $import_table  set activer=1 where 1"; 
    $import_db->sql_query($sql);
echo '<html><head><META http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body>';
if (sizeof($success_list))
{
	Echo '<b>Les utilisateurs suivants ont été importés dans la base phpbb avec succès:</b>' . implode(' - ', $success_list);
}
if (sizeof($ignore_list))
{
	echo '<hr /><b>les utilisateurs suivants n\'ont pu être enregistrés:</b><br />
	<table border="1">
	<tr><th>Pseudo</th><th>Raison de l\'échec</th>
	<tr>' . implode('</tr><tr>', $ignore_list) . '</tr></table>';
}
echo '</body></html>';
?>