Subversion Repositories ALCASAR

Rev

Rev 3183 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2990 rexy 1
<?php 
2
 
3003 rexy 3
/************************************************************************
4
*						ALCASAR INSCRIPTION								*
5
*																		*
3022 rexy 6
*	By K@M3L & T3RRY LaPlateforme
7
*	By Rexy																*
3003 rexy 8
*																		*
9
*	Partie back de la page d'inscription des utilisateurs				*
10
*	Récupère les infos de "email_registration_front.php					*
11
*	- Lit le fichier de configuration /usr/local/etc/alcasar.conf		*
3011 rexy 12
*	- Verifie si le login est déjà présent dans la table "radcheck"		*
13
*	- Vérifie si l'@ mail est présent dans la table "userinfo"			*
3003 rexy 14
*	- Vérifie que le domaine du mail est bien WLD (optionnel)			*
3011 rexy 15
*	- Crée l'utilisateur avec un mot de passe aléatoire					*
16
*	- Envoi l'email à l'utilisaeur et à l'admin avec date et IP			*
3003 rexy 17
*																		*
18
*************************************************************************/
2990 rexy 19
 
3022 rexy 20
/****************************************************************
21
*			GLOBAL FILE PATHS			*
22
*****************************************************************/
23
define('CONF_FILE', '/usr/local/etc/alcasar.conf');
24
/****************************************************************
25
*			Conf files reading test			*
26
*****************************************************************/
27
$conf_files = array(CONF_FILE);
28
foreach ($conf_files as $file) {
29
	if (!file_exists($file)) {
30
		exit("Fichier $file non présent");
31
	}
32
	if (!is_readable($file)) {
33
		exit("Vous n'avez pas les droits de lecture sur le fichier $file");
34
	}
35
}
36
/****************************************************************
37
*			Read CONF_FILE				*
38
*****************************************************************/
39
$file_conf = fopen(CONF_FILE, 'r');
40
if (!$file_conf) {
41
	exit('Error opening the file '.CONF_FILE);
42
}
43
while (!feof($file_conf)) {
44
	$buffer = fgets($file_conf, 4096);
45
	if ((strpos($buffer, '=') !== false) && (substr($buffer, 0, 1) !== '#')) {
46
		$tmp = explode('=', $buffer, 2);
47
		$conf[trim($tmp[0])] = trim($tmp[1]);
48
	}
49
}
50
fclose($file_conf);
51
$whiteDomain	= explode(" ", strtolower(trim($conf['MAIL_WHITEDOMAIN'])));
3026 rexy 52
$adminMail		= $conf['MAIL_ADMIN'];
3037 rexy 53
$typeMail		= $conf['MAIL_TYPE'];
54
$fromMail		= $conf['MAIL_ADDR'];
3038 rexy 55
$organism		= $conf['ORGANISM'];
3022 rexy 56
 
57
/****************************************
58
*			Choice of language			*
59
*****************************************/
3011 rexy 60
$Language = 'en';
61
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
62
	$Langue = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
63
	$Language = strtolower(substr(chop($Langue[0]), 0, 2));
64
}
65
if ($Language === 'fr') {
3038 rexy 66
	$l_invalid_Email = "L'adresse e-mail est invalide";
3011 rexy 67
	$l_domain = "Le domaine";
68
	$l_not_authorized = "n'est pas autorisé";
3038 rexy 69
	$l_Email_already_used = "Cette adresse e-mail est déjà utilisée.";
3022 rexy 70
	$l_subject = "Activation de votre compte ALCASAR";
71
	$l_hello = "Bonjour";
3038 rexy 72
	$l_automatic_mail = "Ceci est un e-mail automatique provenant du portail ALCASAR : $organism";
73
	$l_login = "Voici vos indentifiants de connexion :";
3022 rexy 74
	$l_password = "Mot de passe";
3038 rexy 75
	$l_mail_success = "Un mot de passe vient d'être envoyé à votre adresse e-mail.";
3022 rexy 76
	$l_mail_error = "Erreur lors de l'envoi du mail. Renouvelez votre inscription ou contactez votre administrateur.";
3070 rexy 77
} else if ($Language === 'es') {
78
	$l_invalid_Email = "Dirección de correo electrónico no válida";
79
	$l_domain = "El dominio";
80
	$l_not_authorized = "no está autorizado";
81
	$l_Email_already_used = "Esta dirección de correo electrónico ya está utilizada.";
82
	$l_subject = "Activación de su cuenta ALCASAR";
83
	$l_hello = "Hola";
84
	$l_automatic_mail = "Este es un correo electrónico automático del portal ALCASAR : $organism";
85
	$l_login = "Estas son sus credenciales de acceso:";
86
	$l_password = "Contraseña";
87
	$l_mail_success = "Se ha enviado una contraseña a su dirección de correo electrónico.";
88
	$l_mail_error = "Error al enviar el correo electrónico. Renueve su registro o póngase en contacto con su administrador.";
3011 rexy 89
} else {
90
	$l_invalid_Email = "Invalid Email address";
91
	$l_domain = "The domain";
92
	$l_not_authorized = "is not authorized";
93
	$l_Email_already_used = "This Email address is already used.";
3022 rexy 94
	$l_subject = "Activation of your ALCASAR account";
95
	$l_hello = "Hello";
3038 rexy 96
	$l_automatic_mail = "This is an automatic e-mail from ALCASAR portal : $organism";
97
	$l_login = "Here are your login credentials :";
3022 rexy 98
	$l_password = "Password";
3038 rexy 99
	$l_mail_success = "A password has been sent to your e-mail address";
3022 rexy 100
	$l_mail_error = "Error while sending the email. Renew your registration or contact your administrator.";
3011 rexy 101
}
102
 
2990 rexy 103
if(!isset($create)) $create=0;
104
if(!isset($show)) $show=0;
105
if(!isset($login)) $login = '';
106
if(!isset($cn)) $cn = '';
107
if(!isset($mail)) $mail = '';
108
if(!isset($langue_imp)) $langue_imp = '';
109
if(!isset($selected)) $selected = array();
110
if(!isset($selected['='])) $selected['='] = '';
111
 
112
require('/etc/freeradius-web/config.php');
113
require('acc/manager/lib/attrshow.php');
114
require('acc/manager/lib/defaults.php');
115
 
116
if (false && /* Hide operator column */ $config['general_lib_type'] == 'sql' && $config['sql_use_operators'] == 'true') {
117
	$colspan = 2;
118
	$show_ops = 1;
119
	require('acc/manager/lib/operators.php');
120
} else {
121
	$show_ops = 0;
122
	$colspan = 1;
123
}
124
 
125
if (is_file("acc/manager/lib/sql/drivers/$config[sql_type]/functions.php"))
126
	require("acc/manager/lib/sql/drivers/$config[sql_type]/functions.php");
127
else{
128
	echo "<b>Could not include SQL library</b><br />\n";
129
	exit();
130
}
131
 
132
require('acc/manager/lib/functions.php');
133
if ($config['sql_use_operators'] == 'true'){
134
	include_once("acc/manager/lib/operators.php");
135
	$text = ',op';
136
	$passwd_op = ",':='";
137
}
138
 
139
$da_abort=0;
140
$op_val2 = '';
141
 
142
function GenPassword($nb_car="8")
143
{
144
// Random password
145
	$password = "";
146
	$chaine  = "aAzZeErRtTyYuUIopP152346897mMLkK";
147
	$chaine .= "jJhHgGfFdDsSqQwWxXcCvVbBnN152346897";
148
	while($nb_car != 0) {
149
		//$i = rand(0,71);
150
		// Bug corrigé
151
		$i = rand(0,66);
152
		$password .= $chaine[$i];
153
		$nb_car--;
154
	}
155
	return $password;
156
}
157
 
158
if(isset($_POST['Fmail'])){
3183 rexy 159
	extract($_POST,EXTR_SKIP);
2990 rexy 160
	$Fmail = htmlentities(strtolower(trim($Fmail)));
161
	if(!filter_var($Fmail, FILTER_VALIDATE_EMAIL)){  
3011 rexy 162
		echo "<b>$l_invalid_Email</b><br />\n";
2990 rexy 163
		exit();
164
	}
165
 
3022 rexy 166
	// Retrieve the domainName of the new user
2990 rexy 167
	list($user, $domain) = explode('@', $Fmail);
168
 
3324 rexy 169
	// Extract main domain  (allows sub_domains)
170
	$domainParts = explode('.', $domain);
171
	if (count($domainParts) >= 2) {
172
		$mainDomain = implode('.', array_slice($domainParts, -2));
173
	} else {
174
		$mainDomain = $domain; // Cas où le domaine n'a pas de sous-domaine (ex: exemple.com)
175
	}
176
 
3022 rexy 177
	// check if the domainName is in the whitelist
2990 rexy 178
	if (!empty($whiteDomain)){
3324 rexy 179
		if (!in_array($mainDomain, $whiteDomain)){
3011 rexy 180
			echo "$l_domain $domain $l_not_authorized";
2990 rexy 181
			exit();
182
		}
183
	}
184
	$login  = $Fmail;
185
 
3022 rexy 186
	// check if the new user already exist
2990 rexy 187
	$link = @da_sql_pconnect($config);
188
	if ($link) {
189
		$sql = "SELECT id FROM $config[sql_check_table] WHERE username = '$login';";
190
		$res = @da_sql_query($link,$config, $sql);
191
	}
3022 rexy 192
	$login_check = da_sql_num_rows($res,$config);
193
	da_sql_close($link,$config);
2990 rexy 194
 
3022 rexy 195
	// check if the new user is already in the profile of an existing user 
2990 rexy 196
	$link = @da_sql_pconnect($config);
197
	if ($link) {
198
		$sql = "SELECT id FROM $config[sql_user_info_table] WHERE mail = '$Fmail';";
199
		$res = @da_sql_query($link,$config, $sql);
200
	}
3022 rexy 201
	$email_check = da_sql_num_rows($res,$config);
202
	da_sql_close($link,$config);
203
	if($login_check > 0) { // user already exist
3011 rexy 204
		echo "<b>$l_Email_already_used</b><br />\n";
3022 rexy 205
	} else if($email_check > 0) { // email already used
3011 rexy 206
		echo "<b>$l_Email_already_used</b><br />\n";
2990 rexy 207
	} else {
208
		$password = GenPassword();
209
 
3022 rexy 210
		// if we want to enrich the new user profile
2990 rexy 211
/*		$Fcn = "$prenom".".$nom";
212
		$Fou = "";
213
		$Fhomephone = "";
214
		$Ftelephonenumber = "";
215
		$Fmobile = "";
216
*/
217
		$link = da_sql_pconnect($config);
218
		if ($link){
219
			mysqli_set_charset($link,"utf8");
220
			if (is_file("acc/manager/lib/crypt/$config[general_encryption_method].php")){
221
				include_once("acc/manager/lib/crypt/$config[general_encryption_method].php");
222
 
223
				$passwd = da_encrypt($password);
224
				$passwd = da_sql_escape_string($link, $passwd);
225
				$res = da_sql_query($link,$config,
226
				"INSERT INTO $config[sql_check_table] (attribute,value,username $text)
227
				VALUES ('$config[sql_password_attribute]','$passwd','$login' $passwd_op);");
228
				if (!$res || !da_sql_affected_rows($link,$res,$config)){
229
					echo "<b>Erreur lors de la création de l'utilisateur $login: " . da_sql_error($link,$config) . "</b><br />\n";
230
					$da_abort=1;
231
				}
232
 
233
				if ($config['sql_use_user_info_table'] == 'true' && !$da_abort){
234
					$res = da_sql_query($link,$config,
235
					"SELECT username FROM $config[sql_user_info_table] WHERE
236
					username = '$login';");
237
					if ($res){
238
						if (!da_sql_num_rows($res,$config)){
239
							$Fcn = (isset($Fcn)) ? da_sql_escape_string($link, $Fcn) : '';
240
							$Fmail = (isset($Fmail)) ? da_sql_escape_string($link, $Fmail) : '';
241
							$Fou = (isset($Fou)) ? da_sql_escape_string($link, $Fou) : '';
242
							$Fhomephone = (isset($Fhomephone)) ? da_sql_escape_string($link, $Fhomephone) : '';
243
							$Ftelephonenumber = (isset($Ftelephonenumber)) ? da_sql_escape_string($link, $Ftelephonenumber) : '';
244
							$Fmobile = (isset($Fmobile)) ? da_sql_escape_string($link, $Fmobile) : '';
245
							$res = da_sql_query($link,$config,
246
							"INSERT INTO $config[sql_user_info_table]
247
							(username,name,mail,department,homephone,workphone,mobile) VALUES
248
							('$login','$Fcn','$Fmail','$Fou','$Fhomephone','$Ftelephonenumber','$Fmobile');");
249
							if (!$res || !da_sql_affected_rows($link,$res,$config))
250
								// Erreur sql à supprimer : l'info ne devrait pas être communiquer au client.
251
								echo "<b>Une erreur s'est produite lors de la création du compte : " . da_sql_error($link,$config) . "</b><br />\n";
252
						}
253
						else
3022 rexy 254
							echo "<b>User already exist</b><br />\n";
2990 rexy 255
					}
256
					else
257
						echo "<b>Une erreur s'est produite lors de la création du compte : " . da_sql_error($link,$config) . "</b><br />\n";
258
				}
3022 rexy 259
				// if the new user must be in a group
2990 rexy 260
				if (isset($Fgroup) && $Fgroup != ''){
261
					$Fgroup = da_sql_escape_string($link, $Fgroup);
262
					$res = da_sql_query($link,$config,
263
					"SELECT username FROM $config[sql_usergroup_table]
264
					WHERE username = '$login' AND groupname = '$Fgroup';");
265
					if ($res){
266
						if (!da_sql_num_rows($res,$config)){
267
							$res = da_sql_query($link,$config,
268
							"INSERT INTO $config[sql_usergroup_table]
269
							(username,groupname) VALUES ('$login','$Fgroup');");
270
							if (!$res || !da_sql_affected_rows($link,$res,$config))
271
								echo "<b>Impossible d'ajouter l'utilisateur dans le groupe $Fgroup.</b><br />\n";
272
						}
273
						else
274
							echo "<b>L'utilisateur est déjà présent dans le groupe $Fgroup</b><br />\n";
275
					}
276
					else
277
						echo "<b>Impossible d'ajouter l'utilisateur dans le groupe $Fgroup: " . da_sql_error($link,$config) . "</b><br />\n";
278
				}
3022 rexy 279
				// Creation of the email with the new user login & passwd
2990 rexy 280
				$ip = $_SERVER['REMOTE_ADDR'];
281
				$time = date_create('now')->format('d-m-Y H:i:s');
282
				$domain = $conf["DOMAIN"];
3022 rexy 283
				$hostname  = $conf["HOSTNAME"];
2990 rexy 284
				$to = $Fmail;
3037 rexy 285
				if ($typeMail == "3") { // using an existing @mail
286
					$from = $fromMail; }
287
				else {
288
					$from = "administrator";
289
				}
3022 rexy 290
				$subject = $l_subject;
2990 rexy 291
				$message = "<!DOCTYPE html>
3038 rexy 292
<html>
3062 rexy 293
	<head>
294
		<meta charset=\"utf-8\">
3038 rexy 295
	</head>
296
		<body>
297
			$l_hello,<br/>
298
			<p>$l_automatic_mail<br/>
299
			<h3>$l_login</h3>
300
			<pre>
301
				Login : $login
302
				$l_password : $password
303
			</pre>
304
		</body>
305
</html>";
2990 rexy 306
				$header = "From: $from\n";
307
				$header .= "MIME-Version: 1.0\n";
308
				$header .= "Content-type: text/html; charset=utf-8\n";
309
				if(mail($to, $subject, $message, $header)){
3038 rexy 310
					echo "<center>success : <b>$l_mail_success</b><br>";
311
					if (!empty($adminMail)){ // Creation of the email for the administrator (if enabled)
3026 rexy 312
						$to = $adminMail;
3022 rexy 313
						$subject = "New registration on ALCASAR";
2990 rexy 314
						$message = "<!DOCTYPE html>
3038 rexy 315
<html>
3062 rexy 316
	<head>
317
		<meta charset=\"utf-8\">
3038 rexy 318
	</head>
319
	<body>
3062 rexy 320
		Hello,<br>
321
		<p>$l_automatic_mail<br>
3038 rexy 322
		<h3>A new registration on ALCASAR '$organism' has been made :</h3>
323
		<pre>
324
			@IP   : $ip
325
			Hour  : $time
326
			Login : $login
327
			Email : $Fmail
328
		</pre>
329
	</body>
330
</html>";
2990 rexy 331
						mail($to, $subject, $message, $header);
332
					}
333
				} else {
3022 rexy 334
					// On smtp error, we remove the new user
335
					$link = da_sql_pconnect($config);
2990 rexy 336
					$res2 = da_sql_query($link,$config,
3022 rexy 337
						"DELETE FROM $config[sql_user_info_table] WHERE username = '$login';");
2990 rexy 338
					$res3 = da_sql_query($link,$config,
339
						"DELETE FROM $config[sql_check_table] WHERE username = '$login';");
3022 rexy 340
					echo "<b>$l_mail_error</b><br />\n";
2990 rexy 341
				}
342
			}
343
			else // Could not open encryption library file
3022 rexy 344
				echo "<b>Error during the account creation process</b><br />\n";
2990 rexy 345
		}
346
		else // Could not connect to SQL database
3022 rexy 347
			echo "<b>Error during the account creation process</b><br />\n";
348
		da_sql_close($link,$config);
2990 rexy 349
	}
350
}
351
?>