/
auth.php
executable file
·157 lines (150 loc) · 4.65 KB
/
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
<?
// Copyright 2002 Brian Ronald. All rights reserved.
// Portable PHP/MySQL Corporate Intranet System
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////////////
// This file does the password checking and session variable setting
// required to create a logged in session.
// Session stuff, and general paranoia
extract($_REQUEST);
if(isset($userid))
{
unset($userid);
unset($username);
unset($firstname);
unset($lastname);
}
session_name('PPCIS');
session_start();
$loginhost=$_SERVER["SERVER_NAME"];
$userid = 0; // 0 = not logged in
if(!(@ include("settings.inc")))
{
header("location:".$_SERVER["HTTP_REFERER"]);
die("Database not configured.");
}
function showerror()
{
if(mysqli_connect_errno())
{
print("Couldn't connect to database.");
if(@mysqli_error($intranet_db))
print("Error number " . mysqli_errno($intranet_db) . " (" . mysqli_error($intranet_db) . ")");
}
}
function safe_escape($str)
{
if (1 == (int) ini_get("magic_quotes_gpc"))
{
return $str;
}
else
{
return addslashes($str);
}
}
// Connect to the database, run a select query. Retrieve any row
// with matching usernanme, and verify the password. Set the userid
// and the user is logged in.
if(isset($form_user))
{
// Connect to the database
$intranet_db = @ mysqli_connect($db_hostname, $db_username, $db_password, $db_name);
showerror();
$sql = "SELECT * FROM users WHERE username = '".safe_escape(trim($form_user))."'";
$result = @ mysqli_query($intranet_db, $sql);
showerror();
if(@ mysqli_num_rows($result) != 0)
{
while($row = @ mysqli_fetch_array($result,MYSQLI_ASSOC))
{
if($row["enabled"]=='y' and password_verify($form_pass, $row["password"]))
{
$userid = $row["userid"];
$username = $form_user;
$firstname = $row["firstname"];
$lastname = $row["lastname"];
}
}
}
if($userid == 0 and isset($hash_function))
{
$sql = "SELECT * FROM users WHERE username = '".safe_escape(trim($form_user))."' AND password = $hash_function( '".safe_escape($form_pass)."')";
$result = @ mysqli_query($intranet_db,$sql);
showerror();
if(@ mysqli_num_rows($result) != 0)
{
while($row = @ mysqli_fetch_array($result,MYSQLI_ASSOC))
{
if($row["enabled"]=='y')
{
$userid = $row["userid"];
$username = $form_user;
$firstname = $row["firstname"];
$lastname = $row["lastname"];
}
$sql = "UPDATE users SET password = '".password_hash($form_pass,PASSWORD_DEFAULT)."' WHERE userid = ".$row["userid"];
$result = @ mysqli_query($intranet_db,$sql);
$updates = "Updated password from $hash_function to secure hash with salt.";
}
}
}
if ($userid == 0)
{
$username = "failed";
$firstname = "";
$lastname = "";
session_destroy();
}
}
else
{
$userid = 0;
$username = "logout";
$firstname = "";
$lastname = "";
session_destroy();
}
$_SESSION["userid"]=$userid;
$_SESSION["firstname"]=$firstname;
$_SESSION["lastname"]=$lastname;
$_SESSION["loginhost"]=$loginhost;
$_SESSION["help_keyword"]=$help_keyword;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<? // This returns to the main system.
if(isset($logout))
{
print("<META HTTP-EQUIV=\"refresh\" content=\"0;URL=$siteprefix" . "index.php?auth=out\">");
}
else
{
print("<META HTTP-EQUIV=\"refresh\" content=\"0;URL=$siteprefix" . "index.php?auth=in\">");
}
?>
<title>Authentication</title>
</head>
<body>
<?
print("Authorisation returned: userid=$userid, username=$username.<br>\n");
print("If you aren't automatically returned, click <a href=\"$site_prefix" . "index.php\">here</a><br>\n");
print($updates);
?>
</body>
</html>