root / src / lib / session.lib.php @ d072e29c
Historique | Voir | Annoter | Télécharger (4,78 ko)
1 |
<?php
|
---|---|
2 |
// Base sur Ph. Rigaux, OReilly 3e edition.
|
3 |
// table visitors (login/pwd)
|
4 |
// table websession
|
5 |
// Les privileges de labmember_add sont:
|
6 |
// SELECT sur visitors,
|
7 |
// et ALL sur sessionWeb
|
8 |
|
9 |
require_once 'lib/form.cls.php'; |
10 |
|
11 |
function execQry ($qry, $bd) |
12 |
{ |
13 |
$result = mysql_query($qry, $bd); |
14 |
if (!$result) |
15 |
{ |
16 |
echo "error in execQry ". mysql_error ($db); |
17 |
exit;
|
18 |
} |
19 |
else
|
20 |
return $result; |
21 |
} |
22 |
|
23 |
|
24 |
function CleanOldSessions ($bd) |
25 |
{ |
26 |
//erase sessions outdated since 15 days
|
27 |
$tooOld = date ("U") - 1296000; |
28 |
$qry = "DELETE FROM websession WHERE time_limit < $tooOld"; |
29 |
$resultat = execQry ($qry, $bd); |
30 |
} |
31 |
|
32 |
// uses form class from "form.cls.php"
|
33 |
function LoginForm ($nom_script, $login_default = "view") |
34 |
{ |
35 |
$form = new form (); |
36 |
|
37 |
$form -> openForm (array ('action' => "$nom_script", 'id' => 'LoginForm')); |
38 |
$form -> openFieldset (array ('style' => 'border:1px dotted red; width: 300px;')); |
39 |
$form -> addLegend ('Please Login'); |
40 |
|
41 |
$form -> addInput ('text', array ('id' => 'Login', 'value' => "$login_default", 'name' => 'visitor_login', 'test' => 'test')); |
42 |
$form -> addLabel (' login', array ('for' => 'MyText', 'style' => 'margin: 5px;')); |
43 |
$form -> addAnything ('<br /><br />'); |
44 |
|
45 |
$form -> addInput ('password', array ('id' => 'Pwd', 'value' => '', 'name' => 'visitor_pwd', 'test' => 'test')); |
46 |
$form -> addLabel (' password', array ('for' => 'MyText', 'style' => 'margin: 5px;')); |
47 |
$form -> addAnything ('<br /><br />'); |
48 |
|
49 |
$form -> addInput ('submit', array ('id' => 'MyButton', 'value' => 'Submit', 'test' => 'test')); |
50 |
$form -> closeFieldset ();
|
51 |
$form -> closeForm ();
|
52 |
|
53 |
echo '<br><I>To login, cookies must be enabled on your browser</I><br><br>'; |
54 |
echo '<div >';//style="border: 1px solid darkgrey; text-align: center; width: 310px;">'; |
55 |
// on l'affiche
|
56 |
echo $form; |
57 |
echo '</div>'; |
58 |
|
59 |
echo '</body>'; |
60 |
echo '</html>'; |
61 |
} |
62 |
|
63 |
|
64 |
function get_visitor ($login, $bd) { |
65 |
$log = substr($login, 0, 3); |
66 |
$qry = "SELECT * FROM visitors WHERE `login` LIKE '%$log%'"; |
67 |
$result = mysql_query($qry, $bd); |
68 |
while($vis = mysql_fetch_object($result)) { |
69 |
if ($vis->login == substr($login, 0, strlen($vis->login))) { |
70 |
if ($vis->target_table != "all"){ |
71 |
$vis->target_table = substr($login, strlen($vis->login)); |
72 |
} |
73 |
return $vis; |
74 |
} |
75 |
} |
76 |
return NULL; |
77 |
} |
78 |
|
79 |
function create_session ($bd, $login, $pwd, $id_session){ |
80 |
$visitor = get_visitor ($login, $bd); |
81 |
//L'internaute existe-t-il?
|
82 |
if (is_object($visitor)) { |
83 |
//verif du mot de passe
|
84 |
if ($visitor->pwd == md5($pwd)) { |
85 |
// on insere une session de trente minutes dans table websession
|
86 |
$time_limit = date ("U") + 1800; |
87 |
$insSession = "INSERT INTO websession (id_session, login, " |
88 |
. "time_limit, target_table, "
|
89 |
. "mode) VALUES ('$id_session', '$login', '$time_limit', '$visitor->target_table', '$visitor->mode')";
|
90 |
$resultat = execQry ($insSession, $bd); |
91 |
return TRUE; |
92 |
} else {
|
93 |
echo "<B> Sorry, incorrect password for $login !</B><P>"; |
94 |
return FALSE; |
95 |
} |
96 |
} else {
|
97 |
echo "<B>Sorry, $login is not a registered login!</B><P>"; |
98 |
return FALSE; |
99 |
} |
100 |
} |
101 |
|
102 |
//check session validity, destroy if not
|
103 |
function is_valid_session ($session, $bd) { |
104 |
//is time over?
|
105 |
$now = date ("U"); |
106 |
if ($session->time_limit < $now) { |
107 |
session_destroy(); |
108 |
$destr = "DELETE FROM websession WHERE id_session='$session->id_session'"; |
109 |
$resultat = execQry ($destr, $bd); |
110 |
} else { //session is valid |
111 |
return TRUE; |
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
function get_session ($id_session, $bd) { |
117 |
$qry = "SELECT * FROM websession WHERE `id_session` = '$id_session'"; |
118 |
while($sess = mysql_fetch_object(execQry ($qry, $bd))) { |
119 |
if ($sess->id_session == $id_session) { |
120 |
return $sess; |
121 |
} |
122 |
} |
123 |
return NULL; |
124 |
} |
125 |
|
126 |
// main function for access control
|
127 |
function control_access ($nom_script, $infos_login, $id_session, $bd) { |
128 |
//recherche la session
|
129 |
$session_courante = get_session ($id_session, $bd); |
130 |
//cas 1: la session existe, on verifie sa validite
|
131 |
if (is_object($session_courante)) { |
132 |
// la session existe, est-elle valide?
|
133 |
if (is_valid_session ($session_courante, $bd)) { |
134 |
// on renvoie l'objet session
|
135 |
return $session_courante; |
136 |
} else {
|
137 |
echo "<B> Your session is not (or no longer) valid.<P></B>\n"; |
138 |
} |
139 |
} |
140 |
// Cas 2.a: La session n'existe pas mais un login et pwd ont ete fournis
|
141 |
if (isset($infos_login['visitor_login']) & isset($infos_login['visitor_pwd'])) { |
142 |
// Les login/pwd sont-ils corrects?
|
143 |
if (create_session ($bd, $infos_login['visitor_login'], $infos_login['visitor_pwd'], $id_session)) { |
144 |
// on renvoie l'object session
|
145 |
return get_session ($id_session, $bd); |
146 |
} else {
|
147 |
echo "<B> Identification failed.<P></B>\n"; |
148 |
} |
149 |
} |
150 |
// Cas 2.b: La session n'existe pas
|
151 |
// et il faut afficher le formulaire d'identification
|
152 |
LoginForm ($nom_script); |
153 |
} |
154 |
|
155 |
|
156 |
?>
|