forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.inc
288 lines (258 loc) · 9.6 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
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
287
288
<?php
/**
* Authorization functions.
*
* LICENSE: 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, see <https://opensource.org/licenses/gpl-license.php>;.
*
* @package OpenEMR
* @author Rod Roark <[email protected]>
* @author Brady Miller <[email protected]>
* @author Kevin Yeh <[email protected]>
* @author ViCarePlus <[email protected]>
* @author cfapress
* @link https://www.open-emr.org
*/
//----------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");
require_once("$srcdir/formdata.inc.php");
require_once("$srcdir/authentication/login_operations.php");
$incoming_site_id = '';
if(isset($_POST['authPass']))
{
require_once("authentication/rsa.php");
$cp=$_POST['authPass'];
$rsa=new rsa_key_manager();
$pubKey=$_POST['pk'];
$rsa->load_from_db($pubKey);
$clearPass=$rsa->decrypt($cp);
}
if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
isset($_POST['authPass']) && isset($_POST['authProvider']))
{
// set the language
if (!empty($_POST['languageChoice'])) {
$_SESSION['language_choice'] = $_POST['languageChoice'];
}
else {
$_SESSION['language_choice'] = 1;
}
if(!validate_user_password($_POST['authUser'],$clearPass,$_POST['authProvider']) || !verify_user_gacl_group($_POST['authUser']))
{
$_SESSION['loginfailure'] = 1;
authLoginScreen();
}
//If password expiration option is enabled call authCheckExpired() to check whether login user password is expired or not
if($GLOBALS['password_expiration_days'] != 0){
if(authCheckExpired($_POST['authUser']))
{
authLoginScreen();
}
}
$ip=$_SERVER['REMOTE_ADDR'];
$_SESSION['loginfailure'] = null;
unset($_SESSION['loginfailure']);
//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'], 1, "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'], 0, "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'], 0, "timeout");
authCloseSession();
authLoginScreen();
} else {
// Have a mechanism to skip the timeout reset mechanism if a skip_timeout_reset parameter exists. This
// can be used by scripts that continually request information from the server; for example the Messages
// and Reminders automated intermittent requests that happen in the Messages Center script and in
// the left navigation menu script.
if (empty($GLOBALS['DAEMON_FLAG']) && empty($_REQUEST['skip_timeout_reset'])) $_SESSION["last_update"] = time();
}
}
//----------THINGS WE DO IF WE STILL LIKE YOU
function authCheckSession ()
{
if (isset($_SESSION['authId'])) {
$authDB = sqlQuery("select username, password from users where id = ?",array($_SESSION['authId']));
if ($_SESSION['authUser'] == $authDB['username'] )
{
return true;
}
else {
return false;
}
}
else {
return false;
}
}
function authCloseSession ()
{
// Before destroying the session, save its site_id so that the next
// login will default to that same site.
global $incoming_site_id;
$incoming_site_id = $_SESSION['site_id'];
ob_start();
session_unset();
session_destroy();
unset($_COOKIE[session_name()]);
}
function authLoginScreen()
{
// See comment in authCloseSession().
global $incoming_site_id;
header("Location: {$GLOBALS['login_screen']}?error=1&site=$incoming_site_id");
exit;
}
// Check if the user's password has expired beyond the grace limit.
// If so, deactivate the user
function authCheckExpired($user)
{
$result = sqlStatement("select pwd_expiration_date from users where username = ?",array($user));
if($row = sqlFetchArray($result))
{
$pwd_expires = $row['pwd_expiration_date'];
}
$current_date = date("Y-m-d");
if($pwd_expires != "0000-00-00")
{
$grace_time1 = date("Y-m-d", strtotime($pwd_expires . "+".$GLOBALS['password_grace_time'] ."days"));
}
if(($grace_time1 != "") && strtotime($current_date) > strtotime($grace_time1))
{
sqlStatement("update users set active=0 where username = ?",array($user));
$_SESSION['loginfailure'] = 1;
return true;
}
return false;
}
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 (?)", array($groupname));
}
function delGroup ($group_id)
{
return sqlQuery("delete from groups where id = ? limit 0,1", array($group_id));
}
/***************************************************************
//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 = ? " .
"AND users.username = groups.user group by groups.name";
$result = sqlStatement($query, array($user_to_be_checked));
$usertbcGroups = array();
while ($row = sqlFetchArray($result)) {
$usertbcGroups[] = $row[0];
}
$query = "SELECT groups.name FROM users,groups WHERE users.username = ? " .
"AND users.username = groups.user group by groups.name";
$result = sqlStatement($query, array($group_user));
$usergGroups = array();
while ($row = sqlFetchArray($result)) {
$usergGroups[] = $row[0];
}
foreach ($usertbcGroups as $group) {
if(in_array($group,$usergGroups)) {
return true;
}
}
}
return false;
}
// Attempt to update the user's password, password history, and password expiration.
// Verify that the new password does not match the last three passwords used.
// Return true if successfull, false on failure
function UpdatePasswordHistory($userid,$pwd)
{
$result = sqlStatement("select password, pwd_history1, pwd_history2 from users where id = ?",array($userid));
if ($row = sqlFetchArray($result)) {
$previous_pwd1=$row['password'];
$previous_pwd2=$row['pwd_history1'];
$previous_pwd3=$row['pwd_history2'];
}
if (($pwd != $previous_pwd1) && ($pwd != $previous_pwd2) && ($pwd != $previous_pwd3)) {
sqlStatement("update users set pwd_history2=?, pwd_history1=?,password=? where id=?",array($previous_pwd2,$previous_pwd1,$pwd,$userid));
if($GLOBALS['password_expiration_days'] != 0){
$exp_days=$GLOBALS['password_expiration_days'];
$exp_date = date('Y-m-d', strtotime("+$exp_days days"));
sqlStatement("update users set pwd_expiration_date=? where id=?",array($exp_date,$userid));
}
return true;
}
else {
return false;
}
}
?>