2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 //----------THINGS WE ALWAYS DO
9 require_once("{$GLOBALS['srcdir']}/log.inc");
10 require_once("{$GLOBALS['srcdir']}/sql.inc");
11 // added for the phpGACL group check -- JRM
12 require_once("{$GLOBALS['srcdir']}/acl.inc");
13 require_once("$srcdir/formdata.inc.php");
15 $incoming_site_id = '';
17 if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
18 isset($_POST['authPass']) && isset($_POST['authProvider']))
21 if (!empty($_POST['languageChoice'])) {
22 $_SESSION['language_choice'] = $_POST['languageChoice'];
25 $_SESSION['language_choice'] = 1;
27 //If password expiration option is enabled call authCheckExpired() to check whether login user password is expired or not
28 if($GLOBALS['password_expiration_days'] != 0){
31 $ip=$_SERVER['REMOTE_ADDR'];
32 if (!authNewSession($_POST['authUser'], $_POST['authPass'], $_POST['authProvider']))
34 newEvent("login",$_POST['authUser'], $_POST['authProvider'], 0, "failure: $ip");
35 $_SESSION['loginfailure'] = 1;
38 newEvent("login", $_POST['authUser'], $_POST['authProvider'], 1, "success: $ip");
39 $_SESSION['loginfailure'] = null;
40 unset($_SESSION['loginfailure']);
41 //store the very first initial timestamp for timeout errors
42 $_SESSION["last_update"] = time();
44 else if ( (isset($_GET['auth'])) && ($_GET['auth'] == "logout") )
46 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "success");
52 if (authCheckSession())
54 if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
56 require_once("{$GLOBALS['srcdir']}/patient.inc");
58 $logpatient = getPatientData($_SESSION['pid'], "lname, fname, mname");
59 newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'],
60 "{$logpatient['lname']}, {$logpatient['fname']} {$logpatient['mname']} :: encounter " .
61 $_SESSION['encounter']);
65 //newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
68 newEvent("login",$_POST['authUser'], $_POST['authProvider'], 0, "insufficient data sent");
73 if (!isset($_SESSION["last_update"])) {
76 //if page has not been updated in a given period of time, we call login screen
77 if ((time() - $_SESSION["last_update"]) > $timeout) {
78 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], 0, "timeout");
82 if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
86 //----------THINGS WE DO IF WE STILL LIKE YOU
88 function authNewSession ($user, $pass, $provider)
90 // check to see if the user belongs to *any* OpenEMR groups in phpGACL -- JRM
91 global $phpgacl_location;
92 if (isset ($phpgacl_location)) {
93 if (acl_get_group_titles($user) == 0) return false;
96 // get details about the user
97 $authDB = sqlQuery("select id, password, authorized, see_auth".
99 " from users where username = '$user'");
101 // if the user is NOT active, get out
102 if ($authDB['active'] != 1) { return false; }
104 // compare the submitted password with the stored password
105 if ($authDB['password'] == $pass)
107 //here, we check to see if the user is in fact a member of the correct group:
108 if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'"))
110 $_SESSION['authUser'] = $user;
111 $_SESSION['authGroup'] = $authGroup['name'];
112 $_SESSION['authUserID'] = $authDB['id'];
113 $_SESSION['authPass'] = $pass;
114 $_SESSION['authProvider'] = $provider;
115 $_SESSION['authId'] = $authDB{'id'};
116 $_SESSION['cal_ui'] = $authDB['cal_ui'];
117 $_SESSION['userauthorized'] = $authDB['authorized'];
118 // Some users may be able to authorize without being providers:
119 if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
129 function authCheckSession ()
131 if (isset($_SESSION['authId'])) {
132 $authDB = sqlQuery("select username, password from users where id = '" .
133 $_SESSION['authId']."'");
134 if ($_SESSION['authUser'] == $authDB['username'] &&
135 $_SESSION['authPass'] == $authDB['password'])
148 function authCloseSession ()
150 // Before destroying the session, save its site_id so that the next
151 // login will default to that same site.
152 global $incoming_site_id;
153 $incoming_site_id = $_SESSION['site_id'];
157 unset($_COOKIE[session_name()]);
160 function authLoginScreen()
162 // See comment in authCloseSession().
163 global $incoming_site_id;
164 header("Location: {$GLOBALS['login_screen']}?error=1&site=$incoming_site_id");
168 // Check if the user's password has expired beyond the grace limit.
169 // If so, deactivate the user
170 function authCheckExpired()
172 $auser=formData('authUser','P');
173 $result = sqlStatement("select pwd_expiration_date from users where username = '".$auser."'");
174 if($row = sqlFetchArray($result))
176 $pwd_expires = $row['pwd_expiration_date'];
178 $current_date = date("Y-m-d");
179 if($pwd_expires != "0000-00-00")
181 $grace_time1 = date("Y-m-d", strtotime($pwd_expires . "+".$GLOBALS['password_grace_time'] ."days"));
183 if(($grace_time1 != "") && strtotime($current_date) > strtotime($grace_time1))
185 sqlStatement("update users set active=0 where username = '".$auser."'");
186 $_SESSION['loginfailure'] = 1;
191 function addUser ($username, $password_md5, $info, $authorized = 'yes')
193 return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
196 function delUser ($id)
198 return sqlQuery("delete from users where id = '$id' limit 0,1");
201 function changePasword ($id, $new_md5)
203 return sqlQuery("update users set password = '$new_md5' where id = '$id'");
206 function getUserList ($cols = '*', $limit = 'all', $start = '0')
209 $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
211 $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
212 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
217 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
220 $rez = sqlStatement("select $cols from groups order by date DESC");
222 $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
223 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
228 function addGroup ($groupname)
230 return sqlInsert("insert into groups (name) values ('$groupname')");
233 function delGroup ($group_id)
235 return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
238 /***************************************************************
240 //Function currently user by new post calendar code to determine
241 //if a given user is in a group with another user
242 //and if so to allow editing of that users events
244 //*************************************************************/
246 function validateGroupStatus ($user_to_be_checked, $group_user) {
247 if (isset($user_to_be_checked) && isset($group_user)) {
248 if ($user_to_be_checked == $group_user) {
252 elseif ($_SESSION['authorizeduser'] == 1)
255 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
256 "AND users.username = groups.user group by groups.name";
257 $result = sqlStatement($query);
259 $usertbcGroups = array();
261 while ($row = mysql_fetch_array($result)) {
262 $usertbcGroups[] = $row[0];
265 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
266 "AND users.username = groups.user group by groups.name";
267 $result = sqlStatement($query);
269 $usergGroups = array();
271 while ($row = mysql_fetch_array($result)) {
272 $usergGroups[] = $row[0];
274 foreach ($usertbcGroups as $group) {
275 if(in_array($group,$usergGroups)) {
286 // Attempt to update the user's password, password history, and password expiration.
287 // Verify that the new password does not match the last three passwords used.
288 // Return true if successfull, false on failure
289 function UpdatePasswordHistory($userid,$pwd)
291 $result = sqlStatement("select password, pwd_history1, pwd_history2 from users where id = $userid");
292 if ($row = sqlFetchArray($result)) {
293 $previous_pwd1=$row['password'];
294 $previous_pwd2=$row['pwd_history1'];
295 $previous_pwd3=$row['pwd_history2'];
297 if (($pwd != $previous_pwd1) && ($pwd != $previous_pwd2) && ($pwd != $previous_pwd3)) {
298 sqlStatement("update users set pwd_history2='$previous_pwd2', pwd_history1='$previous_pwd1',password='$pwd' where id=$userid");
299 if($GLOBALS['password_expiration_days'] != 0){
300 $exp_days=$GLOBALS['password_expiration_days'];
301 $exp_date = date('Y-m-d', strtotime("+$exp_days days"));
302 sqlStatement("update users set pwd_expiration_date='$exp_date' where id=$userid");