-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
199 lines (181 loc) · 7.1 KB
/
login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/*
* @file login.php
*
* This file is part of ScolariteNet web application
* https://gitorious.org/scolaritenet
*
* Copyright (c) 2011 ULCO http://www.univ-littoral.fr
*
* See the AUTHORS or Authors.txt file for copyright owners and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
?>
<?php
// Pour bloquer l'accès direct à cette page
if (!defined("acces_ok"))
exit;
if (!empty($_SESSION)) {
if (isset($_POST["user_id"]))
$id_user = $_SESSION["user_id"];
$_SESSION = array();
}
// Affichage de l'entête
entete("Identification");
// Formulaire d'identification
function formulaire_login() {
echo "<table width=\"300\" height=\"100\" align=\"center\">\n";
echo "<form method=\"post\">\n";
echo "<tr><td>Nom d'utilisateur</td><td><input type=\"text\" name=\"login\"></td></tr>\n";
echo "<tr><td>Mot de passe</td><td><input type=\"password\" name=\"password\"></td></tr>\n";
echo "<tr><td></td><td><input type=\"submit\" value=\"Go !\"></td></tr>\n";
echo "</form>\n";
echo "</table>\n";
}
// choix du type de login
function choix_type_login() {
global $prefix_tables, $DB;
$req = "select ut.id_type, t.libelle
from ".$prefix_tables."user u, ".$prefix_tables."user_type t, ".
$prefix_tables."user_est_de_type ut
where u.login =? and u.password=? and ut.id_type=t.id_type and ".
"ut.id_user=u.id_user";
$req_array = array($_POST["login"], $_SESSION["password"]);
$res = $DB->Execute($req, $req_array);
echo "<table width=\"300\" height=\"100\" align=\"center\">\n";
echo "<form method=\"post\">\n";
echo "<input type=\"hidden\" name=\"login\" value=\"",$_POST["login"],"\">\n";
echo "<input type=\"hidden\" name=\"id_user\" value=\"",$_SESSION["user_id"],"\">\n";
echo "<tr><td><b>Connexion en tant que :</b><br /><br />\n";
while ($row = $res->FetchRow()) {
echo "<input type=\"radio\" name=\"choix_type\" value=\"",$row[0],"\" id=\"choix_type_",$row[0],"\" onChange=\"submit();\"><label for=\"choix_type_",$row[0],"\">",$row[1],"</label><br />\n";
}
echo "</td></tr>\n";
echo "</form>\n";
echo "</table>\n";
}
// test si l'utilisateur est actif
function est_utilisateur_actif($login) {
global $prefix_tables, $DB;
$login = strtolower(trim($login));
$req = "select id_user from ".$prefix_tables.
"user where login='".$login."' and actif=TRUE";
$rep = $DB->GetOne($req);
return $rep;
}
function verifie_existence_utilisateur($login) {
global $prefix_tables, $DB;
$login = strtolower(trim($login));
$req = "select count(*) from ".$prefix_tables.
"user where login='".$login."'";
return $DB->GetOne($req);
}
// Formulaire pour le changement de mot de passe
function formulaire_password($login) {
echo "<p align=\"center\"><b>Première connexion<br /><br />Choix d'un mot de passe obligatoire pour continuer</b><br /><br /></p>\n";
echo "<form method=\"post\">\n";
echo "<table align='center'>\n";
echo "<input type=\"hidden\" name=\"login\" value=\"",$login,"\">\n";
echo "<tr><td>Nouveau mot de passe :</td><td><input type=\"password\" name=\"nouveau1\"></td></tr>\n";
echo "<tr><td>Confirmer le mot de passe :</td><td><input type=\"password\" name=\"nouveau2\"></td></tr>\n";
echo "<tr><td colspan=\"2\" align='center'><input type=\"submit\" value=\"Changer\"></td></tr>\n";
echo "</table>\n";
echo "</form>\n";
}
function verifie_validite_password($password1, $password2) {
if (empty($password1)) return false;
if (empty($password2)) return false;
if (strcmp($password1, $password2)) return false;
return true;
}
function modifie_donnees($login, $password) {
global $prefix_tables, $DB;
$req = "update ".$prefix_tables.
"user set password=md5(?), actif=TRUE where login=?";
$req_array = array($password, $login);
$DB->Execute($req, $req_array);
}
echo "<img src=\"images/spacer.gif\" width=\"0\" height=\"70\">\n";
if (isset($_POST["login"]) && !est_utilisateur_actif($_POST["login"])) {
if (verifie_existence_utilisateur($_POST["login"])) {
if (isset($_POST["nouveau1"])) {
$password1 = trim($_POST["nouveau1"]);
$password2 = trim($_POST["nouveau2"]);
if (!empty($password1) &&
verifie_validite_password($password1, $password2)) {
modifie_donnees($_POST["login"], $password1);
echo "<p align=\"center\"><b>Vous pouvez maintenant vous connecter".
"</b><br /><br /></p>\n";
formulaire_login();
} else {
erreur("Choix du mot de passe incorrect");
formulaire_password($_POST["login"]);
}
} else {
formulaire_password(strtolower(trim($_POST["login"])));
}
} else {
erreur("Identification incorrecte !");
formulaire_login();
}
} elseif (isset($_POST["choix_type"])) {
$_SESSION["login"] = $_POST["login"];
$_SESSION["user_id"] = $_POST["id_user"];
$_SESSION["usertype"] = $_POST["choix_type"];
$req = "select t.libelle, ut.id
from ".$prefix_tables."user_type t, ".$prefix_tables."user_est_de_type ut
where ut.id_type=? and ut.id_user=? and t.id_type=ut.id_type";
$req_array = array($_POST["choix_type"], $_POST["id_user"]);
$res = $DB->Execute($req, $req_array);
$row = $res->FetchRow();
$_SESSION["usertype_libelle"] = $row[0];
$_SESSION["id"] = $row[1];
// Ouverture du menu
echo "<script language='javascript'>document.location.href='index.php?page=menu'</script>\n";
} elseif (isset($_POST["login"]) && isset($_POST["password"])) {
$req = "select ut.id_type, t.libelle, u.id_user, ut.id
from ".$prefix_tables."user u, ".$prefix_tables."user_type t, ".
$prefix_tables."user_est_de_type ut
where u.login='".$_POST["login"].
"' and u.password=md5('".$_POST["password"].
"') and ut.id_type=t.id_type and
ut.id_user=u.id_user";
$res = $DB->Execute($req);
$ok = $res->RecordCount();
if ($ok) {
$row = $res->FetchRow();
if ($ok == 1) { // Identification réussie
$_SESSION["login"] = $_POST["login"];
$_SESSION["user_id"] = $row[2];
$_SESSION["usertype"] = $row[0];
$_SESSION["usertype_libelle"] = $row[1];
$_SESSION["id"] = $row[3];
// Ouverture du menu
echo "<script language='javascript'>document.location.href='index.php?page=menu'</script>\n";
} else {
// Plusieurs types possibles pour l'utilisateur
$_SESSION["login"] = $_POST["login"];
$_SESSION["user_id"] = $row[2];
$_SESSION["password"] = md5($_POST["password"]);
choix_type_login();
}
} else {
erreur("Identification incorrecte !");
formulaire_login();
}
} else {
formulaire_login();
}
?>