forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.inc
239 lines (211 loc) · 7.79 KB
/
auth.inc
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
<?php
//----------THINGS WE ALWAYS DO
require_once("{$GLOBALS['srcdir']}/log.inc");
require_once("{$GLOBALS['srcdir']}/sql.inc");
// added for the phpGACL group check -- JRM
require_once("{$GLOBALS['srcdir']}/acl.inc");
if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
isset($_POST['authPass']) && isset($_POST['authProvider']))
{
if (!authNewSession($_POST['authUser'], $_POST['authPass'], $_POST['authProvider']))
{
newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure");
authLoginScreen();
}
newEvent("login", $_POST['authUser'], $_POST['authProvider'], "success");
//store the very first initial timestamp for timeout errors
$_SESSION["last_update"] = time();
}
else if ( (isset($_GET['auth'])) && ($_GET['auth'] == "logout") )
{
newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "success");
authCloseSession();
authLoginScreen();
}
else
{
if (authCheckSession())
{
if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
{
require_once("{$GLOBALS['srcdir']}/patient.inc");
$logpatient = getPatientData($_SESSION['pid'], "lname, fname, mname");
newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'],
"{$logpatient['lname']}, {$logpatient['fname']} {$logpatient['mname']} :: encounter " .
$_SESSION['encounter']);
}
//LOG EVERYTHING
//newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
}
else {
newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
authLoginScreen();
}
}
if (!isset($_SESSION["last_update"])) {
authLoginScreen();
} else {
//if page has not been updated in a given period of time, we call login screen
if ((time() - $_SESSION["last_update"]) > $timeout) {
newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "timeout");
authCloseSession();
authLoginScreen();
} else {
if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
}
}
//----------THINGS WE DO IF WE STILL LIKE YOU
function authNewSession ($user, $pass, $provider)
{
// check to see if the user belongs to *any* OpenEMR groups in phpGACL -- JRM
global $phpgacl_location;
if (isset ($phpgacl_location)) {
if (acl_get_group_titles($user) == 0) return false;
}
//session_name("OpenEMR");
//session_id("81279258720".str_replace(".", "", $_SERVER['REMOTE_ADDR']));
if(!session_id()) { session_start(); }
//echo "user is: $user pass is: $pass provider is: $provider<br />";
//(CHEMED) added cal_ui to the list of fields, so we can change calendar UI for this user.
// Is this the right place to do it?
$authDB = sqlQuery("select id, password, authorized, see_auth, cal_ui from users " .
"where username = '$user'");
//echo "<br>auth pass: ".$authDB['password'];
if ($authDB['password'] == $pass)
{
//here, we check to see if the user is in fact a member of the correct group:
if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'"))
{
$_SESSION['authUser'] = $user;
$_SESSION['authGroup'] = $authGroup['name'];
$_SESSION['authUserID'] = $authDB['id'];
$_SESSION['authPass'] = $pass;
$_SESSION['authProvider'] = $provider;
$_SESSION['authId'] = $authDB{'id'};
$_SESSION['cal_ui'] = $authDB['cal_ui'];
$_SESSION['userauthorized'] = $authDB['authorized'];
// Some users may be able to authorize without being providers:
if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
return true;
} else {
return false;
}
}
else
return false;
}
function authCheckSession ()
{
if (isset($_SESSION['authId'])) {
$authDB = sqlQuery("select username, password from users where id = '" .
$_SESSION['authId']."'");
if ($_SESSION['authUser'] == $authDB['username'] &&
$_SESSION['authPass'] == $authDB['password'])
{
return true;
}
else {
return false;
}
}
else {
return false;
}
}
function authCloseSession ()
{
ob_start();
session_unset();
// $_SESSION = array();
session_destroy();
//setcookie(session_name(),"","","/");
//the following does the same as the above line:
//if(isset($_COOKIE[session_name()])) {
// session_start();
// session_destroy();
unset($_COOKIE[session_name()]);
//}
}
function authLoginScreen()
{
//header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
header("Location: {$GLOBALS['login_screen']}");
exit;
}
function addUser ($username, $password_md5, $info, $authorized = 'yes')
{
return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
}
function delUser ($id)
{
return sqlQuery("delete from users where id = '$id' limit 0,1");
}
function changePasword ($id, $new_md5)
{
return sqlQuery("update users set password = '$new_md5' where id = '$id'");
}
function getUserList ($cols = '*', $limit = 'all', $start = '0')
{
if ($limit = "all")
$rez = sqlStatement("select $cols from users where username != '' order by date DESC");
else
$rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
$tbl[$iter] = $row;
return $tbl;
}
function getProviderList ($cols = '*', $limit= 'all', $start = '0')
{
if ($limit = "all")
$rez = sqlStatement("select $cols from groups order by date DESC");
else
$rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
$tbl[$iter] = $row;
return $tbl;
}
function addGroup ($groupname)
{
return sqlInsert("insert into groups (name) values ('$groupname')");
}
function delGroup ($group_id)
{
return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
}
/***************************************************************
//pennfirm
//Function currently user by new post calendar code to determine
//if a given user is in a group with another user
//and if so to allow editing of that users events
//
//*************************************************************/
function validateGroupStatus ($user_to_be_checked, $group_user) {
if (isset($user_to_be_checked) && isset($group_user)) {
if ($user_to_be_checked == $group_user) {
return true;
}
elseif ($_SESSION['authorizeduser'] == 1)
return true;
$query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
"AND users.username = groups.user group by groups.name";
$result = sqlStatement($query);
$usertbcGroups = array();
while ($row = mysql_fetch_array($result)) {
$usertbcGroups[] = $row[0];
}
$query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
"AND users.username = groups.user group by groups.name";
$result = sqlStatement($query);
$usergGroups = array();
while ($row = mysql_fetch_array($result)) {
$usergGroups[] = $row[0];
}
foreach ($usertbcGroups as $group) {
if(in_array($group,$usergGroups)) {
return true;
}
}
}
return false;
}
?>