Subversion Repositories ALCASAR

Rev

Rev 324 | Rev 411 | 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')";
20
  $l_text_import_help = "Ce fichier ne doit contenir que des noms d'usager &eacute;crit 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;re 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
}
30
else {
31
  $l_title = "Users import";
32
  $l_database_state ="State of the database : number of groups =";
33
  $l_number_of_users = "Number of users";
34
  $l_text_import = "Import from a text file ('.txt')";
35
  $l_text_import_help = "In this file, you must write only the user login one below the other.";
36
  $l_file = "File";
37
  $l_users_service = "Define their service (optional)";
38
  $l_users_group = "Define their group (advisable)";
39
  $l_send = "Send";
40
  $l_imported_files = "Logins/passwords file imported during the last 24h :";
41
  $l_db_import = "Import from a saved users database file (SQL format)";
42
  $l_db_import_help = "In order to impute the last connections, the actual users database will be automaticly saved.";
43
  $l_db_reset = "Reset the users database";
44
}
45
function getImportFileList(){
46
	$importFile = array();
47
	if ($handle = opendir('/tmp')) {
48
		while (false !== ($file = readdir($handle))) {
49
			if ($file != "." && $file != "..") {
50
				$ext = pathinfo($file ,PATHINFO_EXTENSION);
51
				$name = substr($file, 0, -(strlen($ext)+1)); //Retirer les lettres de l'extension ET le point
52
				if ($ext=="pwd"){
53
				$importFile[] = $name;
54
				}
55
			}
56
		}
57
		closedir($handle);
58
	}
59
	return $importFile;
60
}
61
function creatlog ($login,$password,$service,$RS_out)
62
	{
63
 /* génère un fichier en sortie avec les info de connexion en clair */
64
	fputs($RS_out,"   ---  Accès à Internet via ALCASAR  ---  "."\r\n\r\n");
65
	fputs($RS_out,"Service          : $service"."\r\n\r\n");
66
	fputs($RS_out,"Nom de connexion : $login   |  Mot de passe : $password\r\n\r\n");
67
	fputs($RS_out,"Pensez à changer votre mot de passe (lien sur la page d'authentification)"."\r\n\r\n");
68
	fputs($RS_out,"--------------------------------------------------------------------------------"."\r\n\r\n");
69
	}
70
function GenPassword($nb_car="8")
71
	{
72
 /* generation aléatoire du mot de passe */
73
	$password = "";
74
	$chaine  = "aAzZeErRtTyYuUIopP152346897mMLkK";
75
	$chaine .= "jJhHgGfFdDsSqQwWxXcCvVbBnN152346897";
76
	while($nb_car != 0)
77
		{
78
		$i = rand(0,71);
79
		$password .= $chaine[$i];
80
		$nb_car --;
81
		}
82
	return $password ;
83
	}
84
?>
85
<TABLE width="100%" border="0" cellspacing="0" cellpadding="0">
86
<tr><th><? echo "$l_title"; ?></th></tr>
87
<tr bgcolor="#FFCC66"><td><img src="/images/pix.gif" width="1" height="2"></td></tr>
88
</TABLE>
89
<TABLE width="100%" border=1 cellspacing=0 cellpadding=1>
90
<tr><td valign="middle" align="left">
91
<CENTER><H3> 
92
<?php
93
echo "$l_database_state";
94
 
95
$LIBpath = "../lib/";
96
require('/etc/freeradius-web/config.php');
97
if (is_file($LIBpath."sql/drivers/$config[sql_type]/functions.php"))
98
	{
99
	include_once($LIBpath."sql/drivers/$config[sql_type]/functions.php");
100
	}
101
else
102
	{
103
	echo "<b>Could not include SQL library</b><br>\n";
104
	exit();
105
	}
106
include_once($LIBpath.'functions.php');
107
if ($config[sql_use_operators] == 'true')
108
	{
109
	include($LIBpath."operators.php");
110
	$text = ',op';
111
	$passwd_op = ",':='";
112
	}
113
$link = @da_sql_pconnect($config);
114
$choix = $_POST ['choix'];
115
if ($choix == "raz")
116
	{
386 franck 117
	exec ("sudo /usr/local/sbin/alcasar-mysql.sh --raz");
324 richard 118
	}
119
# un fichier est importé
120
if(isset($_FILES['import-users']))
121
	{
122
	unset($result);
123
	$service = $_POST['service'];
124
	$group = $_POST ['groupe'];
125
	$destination = '/tmp/import_file.txt';
126
	list($name_file , $extension) = explode("." , $_FILES['import-users']['name']); 
127
	$extension = strstr($_FILES['import-users']['name'], '.');
128
	$tmpdate = date("Ymd-hms");
129
	$file_out = "/tmp/$tmpdate-$name_file.pwd" ;
130
	if ($choix == "csv")
131
//import d'un fichier txt
132
		{
133
		if (($extension != '.csv') && ($extension != '.txt')) $result = 'Veuillez s&eacute;lectionner un fichier de type csv ou txt !';
134
		else 
135
			{
386 franck 136
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
324 richard 137
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
138
			$RS_in   = file ($destination);
139
			$da_abort=0;
140
			if ($link)
141
				{
142
				if (is_file($LIBpath."crypt/$config[general_encryption_method].php"))
143
					{
144
					include($LIBpath."crypt/$config[general_encryption_method].php");			
145
					$RS_out = fopen ("$file_out", "wb");
146
					foreach ($RS_in as $no => $ligne)
147
						{
148
						$tligne = split(" ",$ligne);
149
						$login  = str_replace("%0D","",str_replace("%0A","",urlencode ($tligne[0])));
150
						$password = GenPassword();		
151
						$passwd = da_encrypt($password);
152
						$passwd = da_sql_escape_string($passwd);
153
/* insertion (login + password) dans la table "radcheck" (si l'usager existe --> changement de mot de passe) */
154
						$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);");
155
						if (!$res || !@da_sql_affected_rows($link,$res,$config))
156
							{
157
							echo "<b>Unable to add user $login: " . da_sql_error($link,$config) . "</b><br>\n";
158
							$da_abort=1;
159
							}
160
						else
161
							{
162
							creatlog ($login,$password,$service,$RS_out);
163
							/*echo $login." : ".$password." , ";*/
164
							}	
165
/* insertion de l'usager dans la table "userinfo" */
166
						if ($config[sql_use_user_info_table] == 'true' && !$da_abort)
167
							{
168
						$res = @da_sql_query($link,$config, "SELECT username FROM $config[sql_user_info_table] WHERE username = '$login';");
169
						if ($res)
170
							{
171
							if (!@da_sql_num_rows($res,$config))
172
								{
173
								$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_user_info_table] (username,department) VALUES ('$login','$service');");
174
								if (!$res || !@da_sql_affected_rows($link,$res,$config))
175
									echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
176
								}
177
							else
178
								echo "<b>User already exists in user info table.</b><br>\n";
179
							}
180
						else
181
							echo "<b>Could not add user information in user info table: " . da_sql_error($link,$config) . "</b><br>\n";
182
							if ($group != '')
183
								{
184
								$group = da_sql_escape_string($group);
185
								$res = @da_sql_query($link,$config,"SELECT username FROM $config[sql_usergroup_table] WHERE username = '$login' AND groupname = '$group';");
186
								if ($res)
187
									{
188
									if (!@da_sql_num_rows($res,$config))
189
										{
190
										$res = @da_sql_query($link,$config,"INSERT INTO $config[sql_usergroup_table] (username,groupname) VALUES ('$login','$group');");
191
										if (!$res || !@da_sql_affected_rows($link,$res,$config))
192
											echo "<b>Could not add user to group $group. SQL Error</b><br>\n";
193
										} # end if 
194
									else
195
											echo "<b>User already is a member of group $group</b><br>\n";
196
								} # end if
197
								else
198
									echo "<b>Could not add user to group $group: " . da_sql_error($link,$config) . "</b><br>\n";
199
							} # end if ($group)
200
						 } # end if ($config)		
201
					} # end foreach
202
					fclose($RS_out);
203
				}
204
				} # end if (is_file ...
205
			}
206
		}
207
	else if ($choix == "bdd")
208
//import d'une Bdd
209
		{
210
		echo $extention;
211
		if ($extension != '.sql') $result = 'Veuillez s&eacute;lectionner un fichier de type sql !';
212
		else 
213
			{
386 franck 214
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --dump");
324 richard 215
			move_uploaded_file($_FILES['import-users']['tmp_name'], $destination);
386 franck 216
			exec ("sudo /usr/local/sbin/alcasar-mysql.sh --import $destination");
324 richard 217
			}
218
		}
219
	}
220
if ($link)
221
	{
222
	$res = @da_sql_query($link,$config,"SELECT GroupName FROM radusergroup GROUP BY GroupName");
223
	if ($res)
224
		{
225
		$nb_group = @da_sql_num_rows($res,$config);
226
		echo $nb_group;
227
		}
228
	}
229
echo ", $l_number_of_users = ";
230
if ($link)
231
	{
232
	$res = @da_sql_query($link,$config,"SELECT UserName FROM userinfo");
233
	if ($res)
234
		{
235
		$nb_user = @da_sql_num_rows($res,$config);
236
		echo "$nb_user";
237
		}
238
	}
239
echo "</td></tr><tr><td>";
240
echo "<TABLE width=\"100%\" border=0 cellspacing=0 cellpadding=1>";
241
echo "<tr><td valign=\"middle\" align=\"left\" colspan=\"2\">";
242
echo "<CENTER><H3>$l_text_import</H3></CENTER></td></tr>";
243
echo "<tr><td valign=\"middle\" align=\"left\">";
244
echo "$l_text_import_help<br>";
245
echo "<tr><td valign=\"middle\" align=\"left\">";
246
echo "<br><FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
247
echo "$l_file (.txt) : <input type=\"file\" name=\"import-users\"><br>";
248
echo "$l_users_service : <input type=\"input\" name=\"service\" value=\"\"><br>";
249
echo "$l_users_group : <input type=\"input\" name=\"groupe\" value=\"\"><br>";
250
echo "<input type='hidden' name='choix' value='csv'>";
251
if (($choix == "csv") && isset($result)) echo $result."<BR>";
252
echo "<input type=\"submit\" value=\"$l_send\">";
253
echo "</FORM></td>";
254
echo "<td>";
255
$ImportFileList = getImportFileList();
256
if (count($ImportFileList) > 0){
257
	echo "$l_imported_files";
258
	echo "<ul>";
259
	foreach ( $ImportFileList as $ImportFile ) //on parcours le tableau 
260
	{
261
	echo "<li>".$ImportFile." ( <a href=\"import_file.php?file=$ImportFile\">txt</a> - <a href=\"import_file.php?file=$ImportFile&format=pdf\">pdf</a> )</li>";
262
	} 
263
	echo "</ul>";
264
} else {
265
	echo "<br>";
266
}
267
echo "</td></tr></table>";
268
echo "<tr><td valign=\"middle\" align=\"left\">";
269
echo "<H3><CENTER>$l_db_import</CENTER></H3>";
270
echo "$l_db_import_help <br><br>";
271
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST ENCTYPE=\"multipart/form-data\">";
272
echo "$l_file (.sql) : <input type=\"file\" name=\"import-users\"><br>";
273
echo "<input type='hidden' name='choix' value='bdd'>";
274
if (($choix == "bdd") && isset($result)) echo $result."<BR>";
275
echo "<input type=\"submit\" value=\"$l_send\">";
276
echo "</FORM>";
277
echo "</td></tr>";
278
echo "<tr><td valign=\"middle\" align=\"left\">";
279
echo "<H3><CENTER>$l_db_reset</CENTER></H3>";
280
echo "$l_db_import_help<br><br>";
281
echo "<FORM action='$_SERVER[PHP_SELF]' method=POST>";
282
echo "<input type='hidden' name='choix' value='raz'>";
283
echo "<input type=\"submit\" value=\"$l_send\">";
284
echo "</FORM>";
285
echo "</TD></TR></TABLE>";
286
?>
287
</BODY>
288
</HTML>
289
<?php