/
functions_auth.php
286 lines (244 loc) · 8.15 KB
/
functions_auth.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?
session_start();
include_once("inc_define_vars.php");
include_once("inc_connect.php");
include_once("functions.php");
include_once("functions_vue.php");
include_once("functions_cockpit.php");
include_once ("admin_functions.php");
include_once ("admin_functions_db.php");
include_once ("functions_engine.php");
function afficherFormAuth()
{
if ( userIsGuilde() || userIsGroupSpec() )
afficheLogout();
else
afficheAuthentifie();
}
/*
* Affiche la boite d'authentification
* Utilisé dans top.php
*/
function afficheAuthentifie($DEV=FALSE)
{
# si il y a un cookie, on l'utilise !
if (isset($_COOKIE["cookie_id_troll"])) $CHTROLL = $_COOKIE["cookie_id_troll"]; else $CHTROLL = '';
?>
<form name='select_troll' method='POST' action='/cockpit.php'>
<table class='mh_tdborder'>
<tr>
<td class='mh_tdpage'>N° troll
<input type=text name='CHTROLL' size=6 value='<? echo $CHTROLL ?>'>
</td>
<td class='mh_tdpage'>Passe:
<input type=password name='CHPASS' size=6>
auto-login : <input type='checkbox' name='autologin' class="TextboxV2" value="true">
</td>
</tr>
<tr>
<td align='center' class='mh_tdpage' colspan='2'>
<input type=submit name='Submit_Pass' value='Connexion' class='mh_form_submit'>
<a href='change_password.php' title='Changement de Mot de passe, ou nouveau chez les Relais&Mago'>Changement de Mot de Passe ?</a>
<?
$text = "Si vous êtes nouveau ou si vous venez de changer de mot de passe, veuillez cliquer <br>";
$text .= "sur ce lien.";
affiche_popup("Renseignement Mot de Passe","yellow",$text,"",false);
?>
</td>
</tr>
</table>
</form>
<?
}
function afficheLogout()
{
?>
<table class='mh_tdborder'>
<tr>
<td class='mh_tdpage' align='center' title='<? echo addslashes($_SESSION['AuthNomTroll']) ?>'>
<? echo $_SESSION['AuthTroll']?> <br>
<input type='button' value='Déconnexion' onClick='JavaScript:document.location.href="/cockpit.php?logout=true"' class='mh_form_submit'>
</td>
</tr>
</table>
<?
}
/* Initialisation de la connection
*
*/
function initAuth()
{
global $db_vue_rm;
if (isset($_SERVER['REQUEST_URI'])) $REQUEST_URI = $_SERVER['REQUEST_URI']; else $REQUEST_URI="";
if (isset($_SERVER['HTTP_REFERER'])) $HTTP_REFERER = $_SERVER['HTTP_REFERER']; else $HTTP_REFERER="";
if (isset($_REQUEST['CHTROLL'])) $CHTROLL = $_REQUEST['CHTROLL']; else $CHTROLL="";
if (isset($_REQUEST['CHPASS'])) $CHPASS = $_REQUEST['CHPASS']; else $CHPASS="";
if (isset($_REQUEST['logout'])) $logout = $_REQUEST['logout']; else $logout="";
if (isset($_REQUEST['CHTROLL']) && isset($_REQUEST['CHPASS']))
{
setcookie ( 'autologin', $_REQUEST['autologin'], time()+365*24*3600 );
if ($_REQUEST['autologin'])
{
setcookie ( 'num_troll', $_REQUEST['CHTROLL'], time()+365*24*3600 );
setcookie ( 'hash_pass_troll', md5($_REQUEST['CHPASS']), time()+365*24*3600 );
}
}
if (isset($_COOKIE['autologin'])) {
$autologin = $_COOKIE['autologin'];
} else {
$autologin = false;
}
if ( $autologin )
{
$CHTROLL = $_COOKIE['num_troll'];
$CHPASS = $_COOKIE['hash_pass_troll'];
}
if (($HTTP_REFERER=="") || (preg_match("/inc_authent/",$HTTP_REFERER)))
$GOTO = $REQUEST_URI; // pour info, le temps de debug par Bodéga
else
$GOTO = $HTTP_REFERER;
if ( (! preg_match("/inc_authent/",$GOTO)) &&
(! preg_match("/index/",$GOTO))
)
$_SESSION['uri']=$GOTO;
else
$_SESSION['uri']="/cockpit.php";
# submit tout frais ?
if (($CHTROLL>0) || ($CHPASS!="")) {
// Si le mot de passe n'est pas le mot de passe md5
if (strlen ($CHPASS) != 32) {
// On se débarasse du pass en clair
$CHPASS=md5($CHPASS);
}
$_SESSION['AuthTroll'] = $CHTROLL;
$_SESSION['Auth'] = $CHPASS;
}
# demande de logout ?
if ($logout) {
$_SESSION['Status']="";
$_SESSION['AuthTroll']="";
$_SESSION['AuthNomTroll']="";
$_SESSION['AuthGuilde']="";
$_SESSION['Auth']="";
$_SESSION['AuthGroupSpec']="";
session_unset();
session_destroy();
setcookie ( 'autologin', false );
echo "<script language='JavaScript'>";
echo "document.location.href='/index.php'";
echo "</script>";
}
# Si la personne est déjà authentifiée
if (isset($_SESSION['Status']) && $_SESSION['Status']=="authentified") {
return;
}
# Vérification normale
if ( (is_numeric($CHTROLL)) && ($_SESSION['Status']!="authentified")) {
$md5pass="---"; // Initialisation, mais çà sert pas à grand chose ici
// On regarde si le troll existe dans la base de données
$sql = "SELECT pass_outils_troll, guilde_troll, nom_troll, nom_rang_troll, groupe_spec_troll";
$sql .= " FROM trolls WHERE id_troll=$CHTROLL";
$result=mysql_query($sql, $db_vue_rm);
echo mysql_error();
list($md5pass,$AuthGuilde,$AuthNomTroll,$nom_rang_troll,$groupe_spec_troll)=mysql_fetch_array($result);
// S'il existe
if (preg_match("/essai/", $nom_rang_troll)) {
die( "<h2>Vous êtes un troll à l'essai, vous n'avez pas accès aux outils pendant cette période<h2>");
}
if ( (mysql_affected_rows()>0) && ($md5pass!="") ) {
// et que le mot de passe est correct
if ($md5pass == $_SESSION['Auth'])
{
//$_SESSION[AuthTroll]=$_SESSION[AuthTroll]; // pour indiquer ici toutes les valeurs possibles de session
$_SESSION['AuthGuilde'] = $AuthGuilde;
$_SESSION['AuthNomTroll'] = $AuthNomTroll;
$_SESSION['AuthGroupSpec'] = $groupe_spec_troll;
//$_SESSION[Auth]=$_SESSION[Auth];
} else {
setcookie ( 'autologin', false );
die( "<h2><font color='red'>Mot de passe incorrect</font></h2>");
}
// S'il n'existe pas dans la base de données
} else {
/* Premier connection du troll sur les outils */
setcookie ( 'autologin', false );
echo "<script language='JavaScript'>";
echo "document.location.href='/change_password.php'";
echo "</script>";
echo "2 - Si vous n'etes pas redirigé automatiquement, cliquez ici : ";
echo "<a href='/change_password.php?act=premiere'> Là j'tai dis !! </a>";
}
if ( userIsGuilde() || userIsGroupSpec() ) {
// Controle de l'administrateur
// Remplis la variable de session
if ( isDbAdministration() )
$_SESSION['admin'] = "authenticated";
else
$_SESSION['admin'] = "notauthorized";
$_SESSION['Status']="authentified";
setcookie ("cookie_id_troll", $_SESSION['AuthTroll'],time()+31536000); // on garde le cookie 1 an
enregistre_connection( $_SESSION['AuthTroll'] );
redirectAuth();
} else {
setcookie ( 'autologin', false );
session_unset();
session_destroy();
die( "<h2>Soit vous n'êtes pas un Relais&Mago
<br> soit le mot de passe est incorrect
<br> soit c'est votre première connexion (réessayez)<br>
<br><br>Conclusion :
<br>Vous n'avez pas accès à ces pages<br>
Contactez glupglup (51166) pour résoudre le problème.<br><br>
");
}
}
}
function redirectAuth()
{
if (preg_match("/cockpit/",$_SESSION['uri'])) {
if (!preg_match("/id_troll/",$_SESSION['uri'])) {
$_SESSION['uri'] .= "?id_troll=$_SESSION[AuthTroll]";
}
}
if ($_SESSION['Status']=="authentified") {
echo "<script language='JavaScript'>";
echo "document.location.href='".$_SESSION['uri']."'";
echo "</script>";
echo "2 - Si vous n'etes pas redirigé automatiquement, cliquez ici : ";
echo "<a href='/cockpit.php?id_troll=$_SESSION[AuthTroll]'> Là j'tai dis !! </a>";
} else {
afficheAuthentifie();
}
}
/* Mise à jour de la date de connection du troll aux outils
* Permet de déterminer quels sont les trolls qui ne viennent jamais
*/
function enregistre_connection($id_troll)
{
global $db_vue_rm;
$date = date("Y-m-d H-i-s");
$sql = "UPDATE trolls set date_last_visit_troll = '$date' WHERE id_troll=$id_troll";
mysql_query($sql,$db_vue_rm);
echo mysql_error();
}
/**
* Vérifie si un utilisateur est authentifié.
*/
function userIsLogged ()
{
return ( $_SESSION['Status'] == 'authentified' );
}
/**
* Vérifie si un utilisateur est authentifié comme membre de la guilde.
*/
function userIsGuilde ()
{
if (!isset($_SESSION['AuthGuilde'])) return false;
else return ( $_SESSION['AuthGuilde'] == ID_GUILDE );
}
function userIsGroupSpec ()
{
if (!isset($_SESSION['AuthGroupSpec'])) return false;
else return ( $_SESSION['AuthGroupSpec'] == 'oui' );
}
?>