Subversion Repositories ALCASAR

Rev

Rev 885 | Rev 1300 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
324 richard 1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
<HTML><!-- Written by Rexy, Romero P. & 3abTux -->
3
<HEAD>
4
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
<TITLE>Users import</TITLE>
6
<link rel="stylesheet" href="/css/style.css" type="text/css">
7
</HEAD>
8
<body>
9
<?
10
# Choice of language
11
$Language = 'en';
12
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
13
  $Langue = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
14
  $Language = strtolower(substr(chop($Langue[0]),0,2)); }
15
if($Language == 'fr'){
16
  $l_title = "Import d'usagers";
17
  $l_database_state = "&Eacute;tat actuel de la base : nombre de groupes =";
18
  $l_number_of_users = "Nombre d'usagers";
19
  $l_text_import = "Importer &agrave; partir d'un fichier texte ('.txt')";
934 franck 20
  $l_text_import_help = "Ce fichier ne doit contenir que des noms d'usagers &eacute;crits les uns sous les autres.";
324 richard 21
  $l_file = "Fichier";
22
  $l_users_service = "D&eacute;finissez leur service (facultatif)";
23
  $l_users_group = "D&eacute;finissez leur groupe (conseill&eacute;)";
24
  $l_send = "Envoyer";
25
  $l_imported_files = "Fichiers des identifiants/mot_de_passe import&eacute;s durant les derni&egrave;res 24h :";
26
  $l_db_import = "Importer &agrave; partir d'une sauvegarde de la base d'usagers (format SQL)";
885 richard 27
  $l_db_import_help = "Afin de pouvoir imputer les derni&egrave;res traces de connexion, une sauvegarde de la base actuelle sera automatiquement r&eacute;alis&eacute;e.";
324 richard 28
  $l_db_reset = "Remise &agrave; z&eacute;ro de la base usagers";
847 richard 29
  $l_error_ext_txt = "Erreur! Veuillez s&eacute;lectionner un fichier avec l'extension '.csv' ou '.txt'";
30
  $l_error_ext_sql = "Erreur! Veuillez s&eacute;lectionner un fichier avec l'extension '.sql'";
587 richard 31
  $l_group_empty = "La liste des groupes est vide";
847 richard 32
  $l_out_title = "   ---  Accès à Internet via ALCASAR  ---  ";
33
  $l_out_login = "Nom de connexion :";
34
  $l_out_passwd = "Mot de passe :";
35
  $l_out_mind = "Pensez à changer votre mot de passe (lien sur la page d'authentification)";
324 richard 36
}
37
else {
38
  $l_title = "Users import";
39
  $l_database_state ="State of the database : number of groups =";
40
  $l_number_of_users = "Number of users";
41
  $l_text_import = "Import from a text file ('.txt')";
42
  $l_text_import_help = "In this file, you must write only the user login one below the other.";
43
  $l_file = "File";
44
  $l_users_service = "Define their service (optional)";
45
  $l_users_group = "Define their group (advisable)";
46
  $l_send = "Send";
47
  $l_imported_files = "Logins/passwords file imported during the last 24h :";
48
  $l_db_import = "Import from a saved users database file (SQL format)";
49
  $l_db_import_help = "In order to impute the last connections, the actual users database will be automaticly saved.";
50
  $l_db_reset = "Reset the users database";
847 richard 51
  $l_error_ext_txt = "Error! Please select a file with '.txt' or '.csv' extension";
52
  $l_error_ext_sql = "Error! Please select a file with '.sql' extension";
587 richard 53
  $l_group_empty = "The group list is empty";
847 richard 54
  $l_out_title = "   ---  Internet access via ALCASAR  ---  ";
55
  $l_out_login = "Login :";
56
  $l_out_passwd = "Password :";
57
  $l_out_mind = "Don't forget to change your password (a link is on the authentication window)";
324 richard 58
}
59
function getImportFileList(){
60
	$importFile = array();
61
	if ($handle = opendir('/tmp')) {
62
		while (false !== ($file = readdir($handle))) {
63
			if ($file != "." && $file != "..") {
64
				$ext = pathinfo($file ,PATHINFO_EXTENSION);
65
				$name = substr($file, 0, -(strlen($ext)+1)); //Retirer les lettres de l'extension ET le point
66
				if ($ext=="pwd"){
67
				$importFile[] = $name;
68
				}
69
			}
70
		}
71
		closedir($handle);
72
	}
73
	return $importFile;
74
}
75
function GenPassword($nb_car="8")
76
	{
77
 /* generation aléatoire du mot de passe */
78
	$password = "";
79
	$chaine  = "aAzZeErRtTyYuUIopP152346897mMLkK";
80
	$chaine .= "jJhHgGfFdDsSqQwWxXcCvVbBnN152346897";
81
	while($nb_car != 0)
82
		{
83
		$i = rand(0,71);
84
		$password .= $chaine[$i];
85
		$nb_car --;
86
		}
87
	return $password ;
88
	}
89
?>
90
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
91
<tr><th><? echo "$l_title"; ?></th></tr>
92
<tr bgcolor="#FFCC66"><td><img src="/images/pix.gif" width="1" height="2"></td></tr>
93
</TABLE>
94
<TABLE width="100%" border=1 cellspacing=0 cellpadding=1>
95
<tr><td valign="middle" align="left">
96
<CENTER><H3> 
97
<?php
98
echo "$l_database_state";
99
 
100
$LIBpath = "../lib/";
101
require('/etc/freeradius-web/config.php');
102
if (is_file($LIBpath."sql/drivers/$config[sql_type]/functions.php"))
103
	{
104
	include_once($LIBpath."sql/drivers/$config[sql_type]/functions.php");
105
	}
106
else
107
	{
108
	echo "<b>Could not include SQL library</b><br>\n";
109
	exit();
110
	}
111
include_once($LIBpath.'functions.php');
411 richard 112
if ($config['sql_use_operators'] == 'true')
324 richard 113
	{
114
	include($LIBpath."operators.php");
115
	$text = ',op';
116
	$passwd_op = ",':='";
117
	}
118
$link = @da_sql_pconnect($config);
411 richard 119
if (isset ($_POST ['choix'])) {	$choix = $_POST ['choix']; }
120
	else { $choix = ''; }
324 richard 121
if ($choix == "raz")
122
	{
386 franck 123
	exec ("sudo /usr/local/sbin/alcasar-mysql.sh --raz");
324 richard 124
	}
125
# un fichier est importé
126
if(isset($_FILES['import-users']))
127
	{
128
	unset($result);
534 richard 129
	if (isset ($_POST['service'])) $service = $_POST['service'];
130
	if (isset ($_POST['groupe'])) $group = $_POST ['groupe'];
324 richard 131
	$destination = '/tmp/import_file.txt';
132
	list($name_file , $extension) = explode("." , $_FILES['import-users']['name']); 
133
	$extension = strstr($_FILES['import-users']['name'], '.');
134
	if ($choix == "csv")
135
//import d'un fichier txt
136
		{
534 richard 137
		if (($extension != '.csv') && ($extension != '.txt')) $result = $l_error_ext_txt;
324 richard 138
		else 
139
			{
386 franck 140
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
324 richard 141
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
142
			$RS_in   = file ($destination);
143
			$da_abort=0;
144
			if ($link)
145
				{
146
				if (is_file($LIBpath."crypt/$config[general_encryption_method].php"))
147
					{
148
					include($LIBpath."crypt/$config[general_encryption_method].php");			
847 richard 149
					$tmpdate = date("Ymd-his");
150
					$file_out = "/tmp/$tmpdate-$name_file.pwd" ;
324 richard 151
					$RS_out = fopen ("$file_out", "wb");
152
					foreach ($RS_in as $no => $ligne)
153
						{
847 richard 154
						if (substr($ligne,0,3) == pack('CCC',239,187,191)) # remove UTF8-BOM 
155
							{
156
								$ligne = substr ($ligne,3);
157
							}
324 richard 158
						$tligne = split(" ",$ligne);
847 richard 159
						$login = trim ($tligne[0]);
160
						$password = trim ($tligne[1]);
584 richard 161
						if ($login != '')
162
							{
768 richard 163
							if ($password == "")
164
								{
165
								$password = GenPassword();
166
								}	
847 richard 167
							$login = da_sql_escape_string($login);
168
							$passwd = da_sql_escape_string($passwd);
584 richard 169
							$passwd = da_encrypt($password);
324 richard 170
/* insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe) */
584 richard 171
							$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);");
172
							if (!$res || !@da_sql_affected_rows($link,$res,$config))
324 richard 173
								{
584 richard 174
								echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
175
								$da_abort=1;
324 richard 176
								}
177
							else
178
								{
847 richard 179
/* create the user informations file */
180
								fputs($RS_out,"$l_out_title\r\n\r\n");
181
								fputs($RS_out,"Service          : $service\r\n\r\n");
182
								fputs($RS_out,"$l_out_login $login   |   $l_out_passwd $password\r\n\r\n");
183
								fputs($RS_out,"$l_out_mind\r\n\r\n");
184
								fputs($RS_out,"--------------------------------------------------------------------------------\r\n\r\n");
584 richard 185
								}	
186
/* insertion de l'usager dans la table "userinfo" */
187
							if ($config[sql_use_user_info_table] == 'true' && !$da_abort)
188
								{
189
							$res = @da_sql_query($link,$config, "SELECT username FROM $config[sql_user_info_table] WHERE username = '$login';");
190
							if ($res)
191
								{
192
								if (!@da_sql_num_rows($res,$config))
324 richard 193
									{
584 richard 194
									$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_user_info_table] (username,department) VALUES ('$login','$service');");
195
									if (!$res || !@da_sql_affected_rows($link,$res,$config))
196
										echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
197
									}	
198
								else
199
									echo "<b>User already exists in user info table.</b><br>\n";
200
								}
201
							else
202
								echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
203
								if ($group != '')
204
									{
205
									$group = da_sql_escape_string($group);
206
									$res = @da_sql_query($link,$config,"SELECT username FROM $config[sql_usergroup_table] WHERE username = '$login' AND groupname = '$group';");
207
									if ($res)
324 richard 208
										{
584 richard 209
										if (!@da_sql_num_rows($res,$config))
210
											{
211
											$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_usergroup_table] (username,groupname) VALUES ('$login','$group');");
212
											if (!$res || !@da_sql_affected_rows($link,$res,$config))
213
												echo "<b>Could not add user to group $group. SQL Error</b><br>\n";
214
											} # end if 
215
										else
216
											echo "<b>User already is a member of group $group</b><br>\n";
217
										} # end if
324 richard 218
									else
584 richard 219
										echo "<b>Could not add user to group $group: " . da_sql_error($link,$config) . "</b><br>\n";
220
									} # end if ($group)
221
						 		} # end if ($config)		
222
							} # end if ($login !='')
223
						} # end foreach
324 richard 224
					fclose($RS_out);
584 richard 225
					} # end if (is file)
226
				} # end if (link)
324 richard 227
			}
228
		}
229
	else if ($choix == "bdd")
230
//import d'une Bdd
231
		{
534 richard 232
		if ($extension != '.sql') $result = $l_error_ext_sql;
324 richard 233
		else 
234
			{
386 franck 235
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
324 richard 236
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
386 franck 237
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --import $destination");
324 richard 238
			}
239
		}
240
	}
241
if ($link)
242
	{
243
	$res = @da_sql_query($link,$config,"SELECT GroupName FROM radusergroup GROUP BY GroupName");
244
	if ($res)
245
		{
246
		$nb_group = @da_sql_num_rows($res,$config);
247
		echo $nb_group;
248
		}
249
	}
250
echo ", $l_number_of_users = ";
251
if ($link)
252
	{
253
	$res = @da_sql_query($link,$config,"SELECT UserName FROM userinfo");
254
	if ($res)
255
		{
256
		$nb_user = @da_sql_num_rows($res,$config);
257
		echo "$nb_user";
258
		}
259
	}
260
echo "</td></tr><tr><td>";
261
echo "<TABLE width=\"100%\" border=0 cellspacing=0 cellpadding=1>";
262
echo "<tr><td valign=\"middle\" align=\"left\" colspan=\"2\">";
263
echo "<CENTER><H3>$l_text_import</H3></CENTER></td></tr>";
264
echo "<tr><td valign=\"middle\" align=\"left\">";
265
echo "$l_text_import_help<br>";
266
echo "<tr><td valign=\"middle\" align=\"left\">";
267
echo "<br><FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
268
echo "$l_file (.txt) : <input type=\"file\" name=\"import-users\"><br>";
269
echo "$l_users_service : <input type=\"input\" name=\"service\" value=\"\"><br>";
587 richard 270
echo "$l_users_group : ";
271
require("../lib/defaults.php");
272
include_once("../lib/$config[general_lib_type]/group_info.php");
273
if (isset($existing_groups)){
274
	echo "<select name=\"groupe\">";
275
	echo "<option value=\"\" selected>";
276
	foreach ($member_groups as $group)
277
		echo "<option value=\"$group\">$group\n";
278
	echo " </select>";
279
	}
280
else echo "$l_group_empty";
281
echo "<br>";
324 richard 282
echo "<input type='hidden' name='choix' value='csv'>";
534 richard 283
if (($choix == "csv") && isset($result)) echo "<b>".$result."</b><BR>";
324 richard 284
echo "<input type=\"submit\" value=\"$l_send\">";
285
echo "</FORM></td>";
286
echo "<td>";
287
$ImportFileList = getImportFileList();
288
if (count($ImportFileList) > 0){
289
	echo "$l_imported_files";
290
	echo "<ul>";
291
	foreach ( $ImportFileList as $ImportFile ) //on parcours le tableau 
292
	{
293
	echo "<li>".$ImportFile." ( <a href=\"import_file.php?file=$ImportFile\">txt</a> - <a href=\"import_file.php?file=$ImportFile&format=pdf\">pdf</a> )</li>";
294
	} 
295
	echo "</ul>";
296
} else {
297
	echo "<br>";
298
}
299
echo "</td></tr></table>";
300
echo "<tr><td valign=\"middle\" align=\"left\">";
301
echo "<H3><CENTER>$l_db_import</CENTER></H3>";
302
echo "$l_db_import_help <br><br>";
303
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
304
echo "$l_file (.sql) : <input type=\"file\" name=\"import-users\"><br>";
305
echo "<input type='hidden' name='choix' value='bdd'>";
534 richard 306
if (($choix == "bdd") && isset($result)) echo "<b>".$result."</b><BR>";
324 richard 307
echo "<input type=\"submit\" value=\"$l_send\">";
308
echo "</FORM>";
309
echo "</td></tr>";
310
echo "<tr><td valign=\"middle\" align=\"left\">";
311
echo "<H3><CENTER>$l_db_reset</CENTER></H3>";
312
echo "$l_db_import_help<br><br>";
313
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST>";
314
echo "<input type='hidden' name='choix' value='raz'>";
315
echo "<input type=\"submit\" value=\"$l_send\">";
316
echo "</FORM>";
317
echo "</TD></TR></TABLE>";
318
?>
319
</BODY>
320
</HTML>
321
<?php