-
Notifications
You must be signed in to change notification settings - Fork 0
/
or-authenticate.php
227 lines (203 loc) · 7.92 KB
/
or-authenticate.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
<?php
session_start();
$_SESSION["systemid"] = "";
$_SESSION["username"] = "";
$_SESSION["isadministrator"] = "FALSE";
$_SESSION["isreporter"] = "FALSE";
/*
*or-authenticate.php(POST:username,password,ajax_indicator)
*
*This file receives three parameters, a username, a password, and ajax_indicator from $_POST[].
*The file checks db->settings->login_method.
*If "normal" the username and password are checked against db->users.
*If "ldap" the username and password are checked against the provided ldap connection.
*An XML response is formed and the SESSION variable "username" is set on success.
*If ajax_indicator is TRUE(1) this file returns with headers type text/xml, allowing it to be used by AJAX.
*If ajax_indicator is FALSE(0) this file returns all text with no headers, allowing it to be used by PHP and converted to a string.
*
*USAGE
*To use this file with AJAX, simply call it using open() sending the following parameters over POST: username, password, TRUE.
*To pull this file's data into a string using PHP, set the POST variables before including the file:
*$_POST["username"] = "test";
*$_POST["password"] = "test";
*$_POST["ajax_indicator"] = FALSE;
*$xmloutput = include("or-authenticate.php");
*Then you may wish to convert $xmloutput into a simpleXML object
*/
require_once("includes/or-dbinfo.php");
/*
*AuthenticateUser()
*Function Written by: Tim Sprowl
*Date: 2008
*/
function AuthenticateUser($username, $password, $settings){
$Host = $settings["ldap_host"];
$BaseDN = $settings["ldap_baseDN"];
// check for empty username and password
if(empty($username) || empty($password))
{
throw new Exception("Username or password not supplied.", 0xb00b00);
}
$connection = @ldap_connect($Host); // try to make a connection
// if a connection could not be made, throw an exception
if(!$connection)
{
throw new Exception(sprintf("Unable to connect to host '%s'.", $Host), 0x5b);
}
// search the Active Directory for username
$result = @ldap_search($connection, $BaseDN, "sAMAccountname=" . $username);
// if the search fails, throw an exception
if(!$result)
{
throw new Exception(@ldap_error($connection), @ldap_errno($connection));
}
// get the first (and hopefully, only) entry in the results
$entry = @ldap_first_entry($connection, $result);
@ldap_free_result($result); // free up the memory used by the result
// if there are no entries, throw an exception
if(!$entry)
{
throw new Exception(@ldap_error($connection), @ldap_errno($connection));
}
// get the display for associated with the username
$displaynames = @ldap_get_values($connection, $entry, "displayName");
// if the display name is not set, throw an exception
if(!$displaynames)
{
throw new Exception(@ldap_error($connection), @ldap_errno($connection));
}
$_SESSION["displayname"] = $displaynames[0]; // use the first entry only
$dn = @ldap_get_dn($connection, $entry); // get the DN of the entry
// if there was a problem getting the DN, throw an exception
if(!$dn)
{
throw new Exception(@ldap_error($connection), @ldap_errno($connection));
}
// try to bind the username to the current session and if the
// the username could not be bound to the current session
// throw an exception
if(!@ldap_bind($connection, $dn, $password))
{
throw new Exception(@ldap_error($connection), @ldap_errno($connection));
}
}
$username = isset($_POST["username"])?$_POST["username"]:"";
$password = isset($_POST["username"])?$_POST["password"]:"";
$ajax_indicator = isset($_POST["ajax_indicator"])?$_POST["ajax_indicator"]:"FALSE";
$output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<authresponse>\n";
if($username != "" && $password != "" && $ajax_indicator != ""){
//LDAP
if($settings["login_method"] == "ldap"){
try{
AuthenticateUser($username, $password, $settings);
}
catch (Exception $e){
$output .= "\t<errormessage>". $e->getMessage() ."</errormessage>\n";
}
if($e){
$output .= "\t<authenticated>false</authenticated>\n";
}
else{
if(mysql_num_rows(mysql_query("SELECT * FROM bannedusers WHERE username='". $username ."';")) <= 0){
$_SESSION["systemid"] = $settings["systemid"];
$_SESSION["username"] = $username;
$output .= "\t<errormessage></errormessage>\n";
$output .= "\t<authenticated>true</authenticated>\n";
//Check if logged in user is an administrator
$aresult = mysql_query("SELECT * FROM administrators WHERE username='". $username ."';");
if(mysql_num_rows($aresult) == 1){
$_SESSION["isadministrator"] = "TRUE";
$output .= "\t<isadministrator>true</isadministrator>\n";
}
else{
$_SESSION["isadministrator"] = "FALSE";
$output .= "\t<isadministrator>false</isadministrator>\n";
}
//Check if logged in user is a reporter
$rresult = mysql_query("SELECT * FROM reporters WHERE username='". $username ."';");
if(mysql_num_rows($rresult) == 1){
$_SESSION["isreporter"] = "TRUE";
$output .= "\t<isreporter>true</isreporter>\n";
}
else{
$_SESSION["isreporter"] = "FALSE";
$output .= "\t<isreporter>false</isreporter>\n";
}
}
else{
$output .= "\t<errormessage>This user has been banned. Please contact an administrator to fix this problem.</errormessage>\n";
$output .= "\t<authenticated>false</authenticated>\n";
$_SESSION["systemid"] = "";
$_SESSION["username"] = "";
$_SESSION["isadministrator"] = "FALSE";
$_SESSION["isreporter"] = "FALSE";
}
}
}
//Normal
elseif($settings["login_method"] == "normal"){
$encpass = sha1($password);
$lresult = mysql_query("SELECT * FROM users WHERE username='". $username ."' AND password='". $encpass ."';");
if(mysql_num_rows($lresult) == 1){
$isactivea = mysql_fetch_array($lresult);
$isactive = $isactivea["active"];
if(mysql_num_rows(mysql_query("SELECT * FROM bannedusers WHERE username='". $username ."';")) <= 0 && $isactive == "0"){
//Set lastlogin time
$llresult = mysql_query("UPDATE users SET lastlogin=NOW() WHERE username='". $username ."';");
if($llresult){
//Set session for user
$_SESSION["systemid"] = $settings["systemid"];
$_SESSION["username"] = $username;
$output .= "\t<errormessage></errormessage>\n";
$output .= "\t<authenticated>true</authenticated>\n";
//Check if logged in user is an administrator
$aresult = mysql_query("SELECT * FROM administrators WHERE username='". $username ."';");
if(mysql_num_rows($aresult) == 1){
$_SESSION["isadministrator"] = "TRUE";
$output .= "\t<isadministrator>true</isadministrator>\n";
}
else{
$_SESSION["isadministrator"] = "FALSE";
$output .= "\t<isadministrator>false</isadministrator>\n";
}
//Check if logged in user is a reporter
$rresult = mysql_query("SELECT * FROM reporters WHERE username='". $username ."';");
if(mysql_num_rows($rresult) == 1){
$_SESSION["isreporter"] = "TRUE";
$output .= "\t<isreporter>true</isreporter>\n";
}
else{
$_SESSION["isreporter"] = "FALSE";
$output .= "\t<isreporter>false</isreporter>\n";
}
}
else{
$output .= "\t<authenticated>false</authenticated>\n\t<errormessage>Could not set last login time.</errormessage>\n";
}
}
else{
$output .= "\t<errormessage>This user has been banned or has not activated their account. Please contact an administrator to fix this problem.</errormessage>\n";
$output .= "\t<authenticated>false</authenticated>\n";
$_SESSION["systemid"] = "";
$_SESSION["username"] = "";
$_SESSION["isadministrator"] = "FALSE";
$_SESSION["isreporter"] = "FALSE";
}
}
else{
$output .= "\t<authenticated>false</authenticated>\n\t<errormessage>Incorrect username or password. Please try again.</errormessage>\n";
}
}
}
else{
$output .= "\t<authenticated>false</authenticated>\n\t<errormessage>One or more parameters not provided.</errormessage>\n";
}
$output .= "</authresponse>";
if($ajax_indicator == "TRUE"){
header("content-type: text/xml");
echo $output;
}
else{
return $output;
}
?>