Subversion Repositories ALCASAR

Rev

Rev 3011 | Rev 3023 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log

<?php 

/************************************************************************
*                                               ALCASAR INSCRIPTION                                                             *
*                                                                                                                                               *
*       By K@M3L & T3RRY LaPlateforme
*       By Rexy                                                                                                                         *
*                                                                                                                                               *
*       Partie back de la page d'inscription des utilisateurs                           *
*       Récupère les infos de "email_registration_front.php                                   *
*       - Lit le fichier de configuration /usr/local/etc/alcasar.conf           *
*       - Verifie si le login est déjà présent dans la table "radcheck"              *
*       - Vérifie si l'@ mail est présent dans la table "userinfo"                    *
*       - Vérifie que le domaine du mail est bien WLD (optionnel)                      *
*       - Crée l'utilisateur avec un mot de passe aléatoire                                   *
*       - Envoi l'email à l'utilisaeur et à l'admin avec date et IP                   *
*                                                                                                                                               *
*************************************************************************/

/****************************************************************
*                       GLOBAL FILE PATHS                       *
*****************************************************************/
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
/****************************************************************
*                       Conf files reading test                 *
*****************************************************************/
$conf_files = array(CONF_FILE);
foreach ($conf_files as $file) {
        if (!file_exists($file)) {
                exit("Fichier $file non présent");
        }
        if (!is_readable($file)) {
                exit("Vous n'avez pas les droits de lecture sur le fichier $file");
        }
}
/****************************************************************
*                       Read CONF_FILE                          *
*****************************************************************/
$file_conf = fopen(CONF_FILE, 'r');
if (!$file_conf) {
        exit('Error opening the file '.CONF_FILE);
}
while (!feof($file_conf)) {
        $buffer = fgets($file_conf, 4096);
        if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
                $tmp = explode('=', $buffer, 2);
                $conf[trim($tmp[0])] = trim($tmp[1]);
        }
}
fclose($file_conf);
$whiteDomain    = explode(" ", strtolower(trim($conf['MAIL_WHITEDOMAIN'])));
$adminMail              = $conf['MAIL_ADMIN'];

/****************************************
*                       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_invalid_Email = "L'adresse email est invalide";
        $l_domain = "Le domaine";
        $l_not_authorized = "n'est pas autorisé";
        $l_Email_already_used = "Cette adresse email est déjà utilisée.";
        $l_subject = "Activation de votre compte ALCASAR";
        $l_hello = "Bonjour";
        $l_automatic_mail = "Ceci est un e-mail automatique provenant d'un portail ALCASAR";
        $l_login = "Vos indentifiants de connexion :";
        $l_email = "Adresse e-mail";
        $l_password = "Mot de passe";
        $l_go_home = "Rendez-vous sur la page d'accueil";
        $l_mail_error = "Erreur lors de l'envoi du mail. Renouvelez votre inscription ou contactez votre administrateur.";
} else {
        $l_invalid_Email = "Invalid Email address";
        $l_domain = "The domain";
        $l_not_authorized = "is not authorized";
        $l_Email_already_used = "This Email address is already used.";
        $l_subject = "Activation of your ALCASAR account";
        $l_hello = "Hello";
        $l_automatic_mail = "This is an automatic e-mail from an ALCASAR portal";
        $l_login = "Your login credentials :";
        $l_email = "e-mail address";
        $l_password = "Password";
        $l_go_home = "Go to the home page";
        $l_mail_error = "Error while sending the email. Renew your registration or contact your administrator.";
}

if (is_file("acc/manager/lib/langues.php"))
        include("acc/manager/lib/langues.php");

if(!isset($create)) $create=0;
if(!isset($show)) $show=0;
if(!isset($login)) $login = '';
if(!isset($cn)) $cn = '';
if(!isset($mail)) $mail = '';
if(!isset($langue_imp)) $langue_imp = '';
if(!isset($selected)) $selected = array();
if(!isset($selected['='])) $selected['='] = '';

require('/etc/freeradius-web/config.php');
require('acc/manager/lib/attrshow.php');
require('acc/manager/lib/defaults.php');

if (false && /* Hide operator column */ $config['general_lib_type'] == 'sql' && $config['sql_use_operators'] == 'true') {
        $colspan = 2;
        $show_ops = 1;
        require('acc/manager/lib/operators.php');
} else {
        $show_ops = 0;
        $colspan = 1;
}

if (is_file("acc/manager/lib/sql/drivers/$config[sql_type]/functions.php"))
        require("acc/manager/lib/sql/drivers/$config[sql_type]/functions.php");
else{
        echo "<b>Could not include SQL library</b><br />\n";
        exit();
}

require('acc/manager/lib/functions.php');
if ($config['sql_use_operators'] == 'true'){
        include_once("acc/manager/lib/operators.php");
        $text = ',op';
        $passwd_op = ",':='";
}

$da_abort=0;
$op_val2 = '';

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

if(isset($_POST['Fmail'])){
        extract($_POST);
        $Fmail = htmlentities(strtolower(trim($Fmail)));
        if(!filter_var($Fmail, FILTER_VALIDATE_EMAIL)){  
                echo "<b>$l_invalid_Email</b><br />\n";
                exit();
        }
        
        // Retrieve the domainName of the new user
        list($user, $domain) = explode('@', $Fmail);

        // check if the domainName is in the whitelist
        if (!empty($whiteDomain)){
                if (!in_array($domain, $whiteDomain)){
                        echo "$l_domain $domain $l_not_authorized";
                        exit();
                }
        }
        $login  = $Fmail;
        
        // check if the new user already exist
        $link = @da_sql_pconnect($config);
        if ($link) {
                $sql = "SELECT id FROM $config[sql_check_table] WHERE username = '$login';";
                $res = @da_sql_query($link,$config, $sql);
        }
        $login_check = da_sql_num_rows($res,$config);
        da_sql_close($link,$config);
        
        // check if the new user is already in the profile of an existing user 
        $link = @da_sql_pconnect($config);
        if ($link) {
                $sql = "SELECT id FROM $config[sql_user_info_table] WHERE mail = '$Fmail';";
                $res = @da_sql_query($link,$config, $sql);
        }
        $email_check = da_sql_num_rows($res,$config);
        da_sql_close($link,$config);
        if($login_check > 0) { // user already exist
                echo "<b>$l_Email_already_used</b><br />\n";
        } else if($email_check > 0) { // email already used
                echo "<b>$l_Email_already_used</b><br />\n";
        } else {
                $password = GenPassword();
                
                // if we want to enrich the new user profile
/*              $Fcn = "$prenom".".$nom";
                $Fou = "";
                $Fhomephone = "";
                $Ftelephonenumber = "";
                $Fmobile = "";
*/
                $link = da_sql_pconnect($config);
                if ($link){
                        mysqli_set_charset($link,"utf8");
                        if (is_file("acc/manager/lib/crypt/$config[general_encryption_method].php")){
                                include_once("acc/manager/lib/crypt/$config[general_encryption_method].php");

                                $passwd = da_encrypt($password);
                                $passwd = da_sql_escape_string($link, $passwd);
                                $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>Erreur lors de la création de l'utilisateur $login: " . da_sql_error($link,$config) . "</b><br />\n";
                                        $da_abort=1;
                                }

                                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)){
                                                        $Fcn = (isset($Fcn)) ? da_sql_escape_string($link, $Fcn) : '';
                                                        $Fmail = (isset($Fmail)) ? da_sql_escape_string($link, $Fmail) : '';
                                                        $Fou = (isset($Fou)) ? da_sql_escape_string($link, $Fou) : '';
                                                        $Fhomephone = (isset($Fhomephone)) ? da_sql_escape_string($link, $Fhomephone) : '';
                                                        $Ftelephonenumber = (isset($Ftelephonenumber)) ? da_sql_escape_string($link, $Ftelephonenumber) : '';
                                                        $Fmobile = (isset($Fmobile)) ? da_sql_escape_string($link, $Fmobile) : '';
                                                        $res = da_sql_query($link,$config,
                                                        "INSERT INTO $config[sql_user_info_table]
                                                        (username,name,mail,department,homephone,workphone,mobile) VALUES
                                                        ('$login','$Fcn','$Fmail','$Fou','$Fhomephone','$Ftelephonenumber','$Fmobile');");
                                                        if (!$res || !da_sql_affected_rows($link,$res,$config))
                                                                // Erreur sql à supprimer : l'info ne devrait pas être communiquer au client.
                                                                echo "<b>Une erreur s'est produite lors de la création du compte : " . da_sql_error($link,$config) . "</b><br />\n";
                                                }
                                                else
                                                        echo "<b>User already exist</b><br />\n";
                                        }
                                        else
                                                echo "<b>Une erreur s'est produite lors de la création du compte : " . da_sql_error($link,$config) . "</b><br />\n";
                                }
                                // if the new user must be in a group
                                if (isset($Fgroup) && $Fgroup != ''){
                                        $Fgroup = da_sql_escape_string($link, $Fgroup);
                                        $res = da_sql_query($link,$config,
                                        "SELECT username FROM $config[sql_usergroup_table]
                                        WHERE username = '$login' AND groupname = '$Fgroup';");
                                        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','$Fgroup');");
                                                        if (!$res || !da_sql_affected_rows($link,$res,$config))
                                                                echo "<b>Impossible d'ajouter l'utilisateur dans le groupe $Fgroup.</b><br />\n";
                                                }
                                                else
                                                        echo "<b>L'utilisateur est déjà présent dans le groupe $Fgroup</b><br />\n";
                                        }
                                        else
                                                echo "<b>Impossible d'ajouter l'utilisateur dans le groupe $Fgroup: " . da_sql_error($link,$config) . "</b><br />\n";
                                }
/*
                                if (!$da_abort){
                                        if (isset($Fgroup) && $Fgroup != '')
                                                require('acc/manager/lib/defaults.php');
                                        foreach($show_attrs as $key => $attr){
                                                if ($attrmap["$key"] == 'none')
                                                        continue;
                                                if ($key == "Filter-Id" && $$attrmap["$key"] == "None")
                                                        continue;
                                                if ($attrmap["$key"] == ''){
                                                        $attrmap["$key"] = $key;
                                                        $attr_type["$key"] = 'replyItem';
                                                        $rev_attrmap["$key"] = $key;
                                                }
                                                if (isset($attr_type["$key"]) && $attr_type["$key"] == 'checkItem'){
                                                        $table = "$config[sql_check_table]";
                                                        $type = 1;
                                                }
                                                else if (isset($attr_type["$key"]) && $attr_type["$key"] == 'replyItem'){
                                                        $table = "$config[sql_reply_table]";
                                                        $type = 2;
                                                }
                                                $val = (isset($_POST[$attrmap["$key"]])) ? $_POST[$attrmap["$key"]] : '';
                                                $val = da_sql_escape_string($link, $val);
                                                $op_name = $attrmap["$key"] . '_op';
                                                $op_val = (isset($$op_name)) ? $$op_name : '';
                                                if ($op_val != ''){
                                                        $op_val = da_sql_escape_string($link, $op_val);
                                                        if (check_operator($op_val,$type) == -1){
                                                                echo "<b>Invalid operator ($op_val) for attribute $key</b><br />\n";
                                                                continue;
                                                        }
                                                        $op_val2 = ",'$op_val'";
                                                }
                                                $chkdef = (isset($default_vals["$key"])) ? check_defaults($val,$op_val,$default_vals["$key"]) : 0;
                                                if ($val == '' || $chkdef)
                                                        continue;
                                                $sqlquery = "INSERT INTO $table (attribute,value,username $text)
                                                        VALUES ('$attrmap[$key]','$val','$login' $op_val2);";
                                                $res = da_sql_query($link,$config,$sqlquery);
                                                if (!$res || !da_sql_affected_rows($link,$res,$config))
                                                        echo "<b>Query failed for attribute $key: " . da_sql_error($link,$config) . "</b><br />\n";
                                        }
                                }
*/
                                // Creation of the email with the new user login & passwd
                                $ip = $_SERVER['REMOTE_ADDR'];
                                $time = date_create('now')->format('d-m-Y H:i:s');
                                $domain = $conf["DOMAIN"];
                                $hostname  = $conf["HOSTNAME"];
                                $to = $Fmail;
                                $from = "administrator";
                                $subject = $l_subject;
                                $message = "<!DOCTYPE html>
                                                <html>
                                                        <head>  
                                                                <meta charset=\"UTF-8\" />
                                                        </head>
                                                        <body>
                                                                $l_hello,<br/><br/>
                                                                <p>$l_automatic_mail ($hostname.$domain)<br/>
                                                                <h4>$l_login</h4>
                                                                <pre>
                                                                        $l_email : $Fmail 
                                                                        Login : $login
                                                                        $l_password : $password
                                                                </pre>
                                                                <p>$l_go_home : <a href=\"https://$hostname.$domain\"></a></p>
                                                        </body>
                                                </html>";
                                $header = "From: $from\n";
                                $header .= "MIME-Version: 1.0\n";
                                $header .= "Content-type: text/html; charset=utf-8\n";
                                if(mail($to, $subject, $message, $header)){
                                        echo "<center>success : <b>Vous y êtes presque ! $l_user '$login' $l_created</b></center><br />";
                                        echo "<center>success : <b>Un email contenant vos informations de connexion vient de vous être envoyé.</b></center><br />";
                                        // Creation of the email for the administrator (if enabled)
                                        if (!empty($adminMail)){
                                                $to = $adminMail;
                                                $from = "administrator";
                                                $subject = "New registration on ALCASAR";
                                                $message = "<!DOCTYPE html>
                                                        <html>
                                                                <head>  
                                                                        <meta charset=\"UTF-8\" />
                                                                </head>
                                                                <body>
                                                                        Hello,<br/><br/>
                                                                        <p>This is an automatic e-mail from an ALCASAR portal.<br/>
                                                                        <h3>A new registration on <strong>$hostname.$domain</strong> has been made :</h3>
                                                                        <pre>
                                                                                @IP   : $ip
                                                                                Hour :  $time
                                                                                Login : $login
                                                                                Email : $Fmail 
                                                                        </pre>
                                                                        <p><a href=\"https://$hostname\">$domain</a></p>
                                                                </body>
                                                        </html>";
                                                $header = "From: $from\n";
                                                $header .= "MIME-Version: 1.0\n";
                                                $header .= "Content-type: text/html; charset=utf-8\n";
                                                mail($to, $subject, $message, $header);
                                        }
                                } else {
                                        // On smtp error, we remove the new user
                                        $link = da_sql_pconnect($config);
                                        $res2 = da_sql_query($link,$config,
                                                "DELETE FROM $config[sql_user_info_table] WHERE username = '$login';");
                                        $res3 = da_sql_query($link,$config,
                                                "DELETE FROM $config[sql_check_table] WHERE username = '$login';");
                                        echo "<b>$l_mail_error</b><br />\n";
                                }
                        }
                        else // Could not open encryption library file
                                echo "<b>Error during the account creation process</b><br />\n";
                }
                else // Could not connect to SQL database
                        echo "<b>Error during the account creation process</b><br />\n";
                da_sql_close($link,$config);
        }
}
?>