Subversion Repositories ALCASAR

Rev

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

Rev Author Line No. Line
1300 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
<?php
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')";
20
  $l_text_import_help = "Ce fichier ne doit contenir que des noms d'usagers &eacute;crits les uns sous les autres.";
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)";
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.";
28
  $l_db_reset = "Remise &agrave; z&eacute;ro de la base usagers";
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'";
31
  $l_group_empty = "La liste des groupes est vide";
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)";
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";
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";
53
  $l_group_empty = "The group list is empty";
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)";
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');
112
if ($config['sql_use_operators'] == 'true')
113
	{
114
	include($LIBpath."operators.php");
115
	$text = ',op';
116
	$passwd_op = ",':='";
117
	}
118
$link = @da_sql_pconnect($config);
119
if (isset ($_POST ['choix'])) {	$choix = $_POST ['choix']; }
120
	else { $choix = ''; }
121
if ($choix == "raz")
122
	{
123
	exec ("sudo /usr/local/sbin/alcasar-mysql.sh --raz");
124
	}
125
# un fichier est importé
1502 richard 126
if(isset($_FILES['import-users']) && ($_FILES['import-users']['name'] !=""))
1300 richard 127
	{
128
	unset($result);
129
	if (isset ($_POST['service'])) $service = $_POST['service'];
130
	if (isset ($_POST['groupe'])) $group = $_POST ['groupe'];
131
	$destination = '/tmp/import_file.txt';
1502 richard 132
	list($file_name , $extension) = explode("." , $_FILES['import-users']['name']); 
1300 richard 133
	$extension = strstr($_FILES['import-users']['name'], '.');
134
	if ($choix == "csv")
135
//import d'un fichier txt
136
		{
137
		if (($extension != '.csv') && ($extension != '.txt')) $result = $l_error_ext_txt;
138
		else 
139
			{
140
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
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");			
149
					$tmpdate = date("Ymd-his");
1502 richard 150
					$file_out = "/tmp/$tmpdate-$file_name.pwd" ;
1300 richard 151
					$RS_out = fopen ("$file_out", "wb");
152
					foreach ($RS_in as $no => $ligne)
153
						{
154
						if (substr($ligne,0,3) == pack('CCC',239,187,191)) # remove UTF8-BOM 
155
							{
156
								$ligne = substr ($ligne,3);
157
							}
158
						$tligne = split(" ",$ligne);
159
						$login = trim ($tligne[0]);
160
						$password = trim ($tligne[1]);
161
						if ($login != '')
162
							{
163
							if ($password == "")
164
								{
165
								$password = GenPassword();
166
								}	
167
							$login = da_sql_escape_string($login);
168
							$passwd = da_sql_escape_string($passwd);
169
							$passwd = da_encrypt($password);
170
/* insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe) */
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))
173
								{
174
								echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
175
								$da_abort=1;
176
								}
177
							else
178
								{
179
/* create the user informations file */
180
								fputs($RS_out,"$l_out_title\r\n\r\n");
181
								if ($service != "" ) { 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");
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))
193
									{
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)
208
										{
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
218
									else
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
224
					fclose($RS_out);
225
					} # end if (is file)
226
				} # end if (link)
227
			}
228
		}
229
	else if ($choix == "bdd")
230
//import d'une Bdd
231
		{
232
		if ($extension != '.sql') $result = $l_error_ext_sql;
233
		else 
234
			{
235
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
236
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
237
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --import $destination");
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>";
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>";
282
echo "<input type='hidden' name='choix' value='csv'>";
283
if (($choix == "csv") && isset($result)) echo "<b>".$result."</b><BR>";
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'>";
306
if (($choix == "bdd") && isset($result)) echo "<b>".$result."</b><BR>";
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>