Subversion Repositories ALCASAR

Rev

Rev 2414 | Rev 2853 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><!-- Written by Rexy, Romero P. & 3abTux -->
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<TITLE>Users import</TITLE>
<link rel="stylesheet" href="/css/style.css" type="text/css">
</HEAD>
<body>
<?php
// Choice of language
$Language = 'en';
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
  $Langue = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
  $Language = strtolower(substr(chop($Langue[0]),0,2)); }
if($Language == 'fr'){
  $l_title = "Importer et vider la base des usagers";
  $l_database_state = "état actuel de la base : nombre de groupes =";
  $l_number_of_users = "Nombre d'usagers";
  $l_text_import = "Importer à partir d'un fichier texte";
  $l_text_import_help = "Ce fichier ne doit contenir que des noms d'usagers écrits les uns sous les autres.";
  $l_file = "Fichier";
  $l_users_group = "Définissez leur groupe (conseillé)";
  $l_send = "Importer";
  $l_empty = "Vider";
  $l_empty_warn = "Vous êtes sur le point de vider entièrement la base de donnée des utilisateurs... Souhaitez vous continuer?";
  $l_imported_files = "Fichiers des identifiants/mot_de_passe import&eacute;s durant les derni&egrave;res 24h :";
  $l_db_import = "Importer à partir d'une sauvegarde de la base d'usagers";
  $l_db_import_help = "Afin de pouvoir imputer les dernières traces de connexion, une sauvegarde de la base actuelle sera automatiquement réalisée.";
  $l_db_reset = "Vider la base des usagers";
  $l_error_ext_txt = "Erreur! Veuillez sélectionner un fichier avec l'extension '.csv' ou '.txt'";
  $l_error_ext_sql = "Erreur! Veuillez sélectionner un fichier avec l'extension '.sql' ou '.sql.gz'";
  $l_group_empty = "La liste des groupes est vide";
  $l_out_title = "   ---  Accès à Internet via ALCASAR  ---  ";
  $l_out_login = "Nom de connexion :";
  $l_out_passwd = "Mot de passe :";
  $l_out_mind = "Pensez à changer votre mot de passe (lien sur la page d'authentification)";
}
else {
  $l_title = "Import and empty the users database";
  $l_database_state ="State of the database : number of groups =";
  $l_number_of_users = "Number of users";
  $l_text_import = "Import from a text file";
  $l_text_import_help = "In this file, you must write only the user login one below the other.";
  $l_file = "File";
  $l_users_group = "Define their group (advisable)";
  $l_send = "Import";
  $l_empty = "Empty";
  $l_empty_warn = "You are close to empty the whole user database... Would you like to proceed?";
  $l_imported_files = "Logins/passwords file imported during the last 24h :";
  $l_db_import = "Import from a saved users database file";
  $l_db_import_help = "In order to impute the last connections, the actual users database will be automaticly saved.";
  $l_db_reset = "Empty the users database";
  $l_error_ext_txt = "Error! Please select a file with '.txt' or '.csv' extension";
  $l_error_ext_sql = "Error! Please select a file with '.sql' or '.sql.gz' extension";
  $l_group_empty = "The group list is empty";
  $l_out_title = "   ---  Internet access via ALCASAR  ---  ";
  $l_out_login = "Login :";
  $l_out_passwd = "Password :";
  $l_out_mind = "Don't forget to change your password (a link is on the authentication window)";
}

require('/etc/freeradius-web/config.php');

function getImportFileList(){
        $importFile = array();
        if ($handle = opendir('/tmp')) {
                while (false !== ($file = readdir($handle))) {
                        if ($file != "." && $file != "..") {
                                $ext = pathinfo($file ,PATHINFO_EXTENSION);
                                $name = substr($file, 0, -(strlen($ext)+1)); //Retirer les lettres de l'extension ET le point
                                if ($ext=="pwd"){
                                $importFile[] = $name;
                                }
                        }
                }
                closedir($handle);
        }
        return $importFile;
}

function GenPassword($nb_car="8")
{
// Random password
        $password = "";
        $chaine  = "aAzZeErRtTyYuUIopP152346897mMLkK";
        $chaine .= "jJhHgGfFdDsSqQwWxXcCvVbBnN152346897";
        while($nb_car != 0) {
                $i = rand(0,71);
                $password .= $chaine[$i];
                $nb_car--;
        }
        return $password;
}

function freeradiusweb_removeDisallowedChars($login)
{
        global $config;

        if ($login != '')
        {
                $accent = "âêôûéàèùîáâãäçèéêëìíîïñòóôõöùúûü";
                $login = preg_replace("/[^\w$accent\.\/\@\:\-]/",'',$login);
        }
        if ($login != '' && $config['general_strip_realms'] == 'yes'){
                $realm_del = ($config['general_realm_delimiter'] != '') ? $config['general_realm_delimiter'] : '@';
                $realm_for = ($config['general_realm_format'] != '') ? $config['general_realm_format'] : 'suffix';
                $new = explode($realm_del,$login,2);
                if (count($new) == 2)
                        $login = ($realm_for == 'suffix') ? $new[0] : $new[1];
        }

        return $login;
}

?>
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><th><? echo "$l_title"; ?></th></tr>
<tr bgcolor="#FFCC66"><td><img src="/images/pix.gif" width="1" height="2"></td></tr>
</TABLE>
<TABLE width="100%" border=1 cellspacing=0 cellpadding=1>
<tr><td valign="middle" align="left">
<CENTER><H3> 
<?php
echo "$l_database_state ";

$LIBpath = "../lib/";
if (is_file($LIBpath."sql/drivers/$config[sql_type]/functions.php"))
        {
        include_once($LIBpath."sql/drivers/$config[sql_type]/functions.php");
        }
else
        {
        echo "<b>Could not include SQL library</b><br>\n";
        exit();
        }
include_once($LIBpath.'functions.php');
if ($config['sql_use_operators'] == 'true')
        {
        include($LIBpath."operators.php");
        $text = ',op';
        $passwd_op = ",':='";
        }
$link = @da_sql_pconnect($config);
if (isset ($_POST ['choix'])) { $choix = $_POST ['choix']; }
        else { $choix = ''; }
if ($choix == "raz")
        {
        exec ("sudo /usr/local/bin/alcasar-logout.sh all");
        exec ("sudo /usr/local/bin/alcasar-mysql.sh --raz");
        }
// a file is downloaded
if(isset($_FILES['import-users']) && ($_FILES['import-users']['name'] !=""))
        {
        unset($result);
        if (isset ($_POST['groupe'])) $group = $_POST ['groupe'];
        list($file_name , $extension) = explode("." , $_FILES['import-users']['name'] , 2); 
        if ($choix == "csv") // import of a txt file
                {
                if (($extension != 'csv') && ($extension != 'txt')) $result = $l_error_ext_txt;
                else 
                        {
                        $destination = '/tmp/import_file.txt';
                        exec ("sudo /usr/local/bin/alcasar-mysql.sh --dump");
                        move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
                        $RS_in   = file ($destination);
                        $da_abort=0;
                        if ($link)
                                {
                                if (is_file($LIBpath."crypt/$config[general_encryption_method].php"))
                                        {
                                        include($LIBpath."crypt/$config[general_encryption_method].php");                       
                                        $tmpdate = date("Ymd-his");
                                        $file_out = "/tmp/$tmpdate-$file_name.pwd" ;
                                        $RS_out = fopen ("$file_out", "wb");
                                        foreach ($RS_in as $no => $ligne)
                                                {
                                                if (substr($ligne,0,3) == pack('CCC',239,187,191)) # remove UTF8-BOM 
                                                        {
                                                                $ligne = substr ($ligne,3);
                                                        }
                                                $tligne = explode(" ",$ligne);
                                                $login = trim ($tligne[0]);
                                                $password = trim ($tligne[1]);
                                                if ($login != '')
                                                        {
                                                        if ($password == "")
                                                                {
                                                                $password = GenPassword();
                                                                }
                                                        $login = freeradiusweb_removeDisallowedChars($login);
                                                        $login = da_sql_escape_string($link,$login);
                                                        $passwd = da_encrypt($password);
                                                        $passwd = da_sql_escape_string($link,$passwd);
// insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe)
                                                        $res = @da_sql_query($link,$config,"INSERT INTO $config[sql_check_table] (attribute,value,username $text) VALUES ('$config[sql_password_attribute]','$passwd','$login' $passwd_op);");
                                                        if (!$res || !@da_sql_affected_rows($link,$res,$config))
                                                                {
                                                                echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
                                                                $da_abort=1;
                                                                }
                                                        else
                                                                {
// create the user informations file
                                                                fputs($RS_out,"$l_out_title\r\n\r\n");
                                                                fputs($RS_out,"$l_out_login $login   |   $l_out_passwd $password\r\n\r\n");
                                                                fputs($RS_out,"$l_out_mind\r\n\r\n");
                                                                fputs($RS_out,"--------------------------------------------------------------------------------\r\n\r\n");
                                                                }       
// insertion de l'usager dans la table "userinfo"
                                                        if ($config['sql_use_user_info_table'] == 'true' && !$da_abort)
                                                                {
                                                        $res = @da_sql_query($link,$config, "SELECT username FROM $config[sql_user_info_table] WHERE username = '$login';");
                                                        if ($res)
                                                                {
                                                                if (!@da_sql_num_rows($res,$config))
                                                                        {
                                                                        $res = @da_sql_query($link,$config,"INSERT INTO $config[sql_user_info_table] (username) VALUES ('$login');");
                                                                        if (!$res || !@da_sql_affected_rows($link,$res,$config))
                                                                                echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
                                                                        }       
                                                                else
                                                                        echo "<b>User already exists in user info table.</b><br>\n";
                                                                }
                                                        else
                                                                echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
                                                                if ((isset($group)) && (!empty($group)))
                                                                        {
                                                                        $group = da_sql_escape_string($link,$group);
                                                                        $res = @da_sql_query($link,$config,"SELECT username FROM $config[sql_usergroup_table] WHERE username = '$login' AND groupname = '$group';");
                                                                        if ($res)
                                                                                {
                                                                                if (!@da_sql_num_rows($res,$config))
                                                                                        {
                                                                                        $res = @da_sql_query($link,$config,"INSERT INTO $config[sql_usergroup_table] (username,groupname) VALUES ('$login','$group');");
                                                                                        if (!$res || !@da_sql_affected_rows($link,$res,$config))
                                                                                                echo "<b>Could not add user to group $group. SQL Error</b><br>\n";
                                                                                        } # end if 
                                                                                else
                                                                                        echo "<b>User already is a member of group $group</b><br>\n";
                                                                                } # end if
                                                                        else
                                                                                echo "<b>Could not add user to group $group: " . da_sql_error($link,$config) . "</b><br>\n";
                                                                        } // end if ($group)
                                                                } // end if ($config)           
                                                        } // end if ($login !='')
                                                } // end foreach
                                        fclose($RS_out);
                                        } // end if (is file)
                                } // end if (link)
                        unlink ($destination);
                        } // end if (extension = (txt or cvs))
                }
        else if ($choix == "bdd") // database import
                {
                if (($extension == 'sql') || ($extension == 'sql.gz')) 
                        {
                        $destination = '/tmp/import_file.'.$extension;
                        exec ("sudo /usr/local/bin/alcasar-mysql.sh --dump");
                        move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
                        exec ("sudo /usr/local/bin/alcasar-mysql.sh --import $destination");
                        //echo "OK : $destination";
                        unlink ($destination);
                        }
                else $result = $l_error_ext_sql;
                }
        }
if ($link)
        {
        $res = @da_sql_query($link,$config,"SELECT GroupName FROM radusergroup GROUP BY GroupName");
        if ($res)
                {
                $nb_group = @da_sql_num_rows($res,$config);
                echo $nb_group;
                }
        }
echo ", $l_number_of_users = ";
if ($link)
        {
        $res = @da_sql_query($link,$config,"SELECT UserName FROM userinfo");
        if ($res)
                {
                $nb_user = @da_sql_num_rows($res,$config);
                echo "$nb_user";
                }
        }
echo "</td></tr><tr><td>";
echo "<TABLE width=\"100%\" border=0 cellspacing=0 cellpadding=1>";
echo "<tr><td valign=\"middle\" align=\"left\" colspan=\"2\">";
echo "<CENTER><H3>$l_text_import</H3></CENTER></td></tr>";
echo "<tr><td valign=\"middle\" align=\"left\">";
echo "$l_text_import_help<br>";
echo "<tr><td valign=\"middle\" align=\"left\">";
echo "<br><FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
echo "$l_file (.txt or .csv) : <input type=\"file\" name=\"import-users\"><br>";
echo "$l_users_group : ";
require("../lib/defaults.php");
include_once("../lib/$config[general_lib_type]/group_info.php");
if (isset($existing_groups)){
        echo "<select name=\"groupe\">";
        echo "<option value=\"\" selected>";
        foreach ($member_groups as $group)
                echo "<option value=\"$group\">$group\n";
        echo " </select>";
        }
else echo "$l_group_empty";
echo "<br>";
echo "<input type='hidden' name='choix' value='csv'>";
if (($choix == "csv") && isset($result)) echo "<b>".$result."</b><BR>";
echo "<input type=\"submit\" value=\"$l_send\">";
echo "</FORM></td>";
echo "<td>";
$ImportFileList = getImportFileList();
if (count($ImportFileList) > 0){
        echo "$l_imported_files";
        echo "<ul>";
        foreach ( $ImportFileList as $ImportFile ) //on parcours le tableau 
        {
        echo "<li>".$ImportFile." ( <a href=\"import_file.php?file=$ImportFile\">txt</a> - <a href=\"import_file.php?file=$ImportFile&format=pdf\">pdf</a> )</li>";
        } 
        echo "</ul>";
} else {
        echo "<br>";
}
echo "</td></tr></table>";
echo "<tr><td valign=\"middle\" align=\"left\">";
echo "<H3><CENTER>$l_db_import</CENTER></H3>";
echo "$l_db_import_help <br><br>";
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
echo "$l_file (.sql or .sql.gz) : <input type=\"file\" name=\"import-users\"><br>";
echo "<input type='hidden' name='choix' value='bdd'>";
if (($choix == "bdd") && isset($result)) echo "<b>".$result."</b><BR>";
echo "<input type=\"submit\" value=\"$l_send\">";
echo "</FORM>";
echo "</td></tr>";
echo "<tr><td valign=\"middle\" align=\"left\">";
echo "<H3><CENTER>$l_db_reset</CENTER></H3>";
echo "$l_db_import_help<br><br>";
echo "<FORM onsubmit=\"return confirm('$l_empty_warn');\" action='$_SERVER[PHP_SELF]' method=POST>";
echo "<input type='hidden' name='choix' value='raz'>";
echo "<input type=\"submit\" value=\"$l_empty\">";
echo "</FORM>";
echo "</TD></TR></TABLE>";
?>
</BODY>
</HTML>