2 //----------THINGS WE ALWAYS DO
4 require_once("{$GLOBALS['srcdir']}/log.inc");
5 require_once("{$GLOBALS['srcdir']}/sql.inc");
6 // added for the phpGACL group check -- JRM
7 require_once("{$GLOBALS['srcdir']}/acl.inc");
9 if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
10 isset($_POST['authPass']) && isset($_POST['authProvider']))
12 $ip=$_SERVER['REMOTE_ADDR'];
13 if (!authNewSession($_POST['authUser'], $_POST['authPass'], $_POST['authProvider']))
15 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure: $ip");
16 $_SESSION['loginfailure'] = 1;
19 newEvent("login", $_POST['authUser'], $_POST['authProvider'], "success: $ip");
20 $_SESSION['loginfailure'] = null;
21 unset($_SESSION['loginfailure']);
22 //store the very first initial timestamp for timeout errors
23 $_SESSION["last_update"] = time();
25 else if ( (isset($_GET['auth'])) && ($_GET['auth'] == "logout") )
27 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "success");
33 if (authCheckSession())
35 if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
37 require_once("{$GLOBALS['srcdir']}/patient.inc");
38 $logpatient = getPatientData($_SESSION['pid'], "lname, fname, mname");
39 newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'],
40 "{$logpatient['lname']}, {$logpatient['fname']} {$logpatient['mname']} :: encounter " .
41 $_SESSION['encounter']);
44 //newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
47 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
52 if (!isset($_SESSION["last_update"])) {
55 //if page has not been updated in a given period of time, we call login screen
56 if ((time() - $_SESSION["last_update"]) > $timeout) {
57 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "timeout");
61 if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
65 //----------THINGS WE DO IF WE STILL LIKE YOU
67 function authNewSession ($user, $pass, $provider)
69 // check to see if the user belongs to *any* OpenEMR groups in phpGACL -- JRM
70 global $phpgacl_location;
71 if (isset ($phpgacl_location)) {
72 if (acl_get_group_titles($user) == 0) return false;
75 // get details about the user
76 $authDB = sqlQuery("select id, password, authorized, see_auth".
78 " from users where username = '$user'");
80 // if the user is NOT active, get out
81 if ($authDB['active'] != 1) { return false; }
83 // start the HTTP SESSION
84 if(!session_id()) { session_start(); }
86 // compare the submitted password with the stored password
87 if ($authDB['password'] == $pass)
89 //here, we check to see if the user is in fact a member of the correct group:
90 if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'"))
92 $_SESSION['authUser'] = $user;
93 $_SESSION['authGroup'] = $authGroup['name'];
94 $_SESSION['authUserID'] = $authDB['id'];
95 $_SESSION['authPass'] = $pass;
96 $_SESSION['authProvider'] = $provider;
97 $_SESSION['authId'] = $authDB{'id'};
98 $_SESSION['cal_ui'] = $authDB['cal_ui'];
99 $_SESSION['userauthorized'] = $authDB['authorized'];
100 // Some users may be able to authorize without being providers:
101 if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
111 function authCheckSession ()
113 if (isset($_SESSION['authId'])) {
114 $authDB = sqlQuery("select username, password from users where id = '" .
115 $_SESSION['authId']."'");
116 if ($_SESSION['authUser'] == $authDB['username'] &&
117 $_SESSION['authPass'] == $authDB['password'])
130 function authCloseSession ()
134 // $_SESSION = array();
136 //setcookie(session_name(),"","","/");
137 //the following does the same as the above line:
138 //if(isset($_COOKIE[session_name()])) {
140 // session_destroy();
141 unset($_COOKIE[session_name()]);
145 function authLoginScreen()
147 //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
148 header("Location: {$GLOBALS['login_screen']}?error=1");
152 function addUser ($username, $password_md5, $info, $authorized = 'yes')
154 return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
157 function delUser ($id)
159 return sqlQuery("delete from users where id = '$id' limit 0,1");
162 function changePasword ($id, $new_md5)
164 return sqlQuery("update users set password = '$new_md5' where id = '$id'");
167 function getUserList ($cols = '*', $limit = 'all', $start = '0')
170 $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
172 $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
173 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
178 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
181 $rez = sqlStatement("select $cols from groups order by date DESC");
183 $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
184 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
189 function addGroup ($groupname)
191 return sqlInsert("insert into groups (name) values ('$groupname')");
194 function delGroup ($group_id)
196 return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
199 /***************************************************************
201 //Function currently user by new post calendar code to determine
202 //if a given user is in a group with another user
203 //and if so to allow editing of that users events
205 //*************************************************************/
207 function validateGroupStatus ($user_to_be_checked, $group_user) {
208 if (isset($user_to_be_checked) && isset($group_user)) {
209 if ($user_to_be_checked == $group_user) {
213 elseif ($_SESSION['authorizeduser'] == 1)
216 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
217 "AND users.username = groups.user group by groups.name";
218 $result = sqlStatement($query);
220 $usertbcGroups = array();
222 while ($row = mysql_fetch_array($result)) {
223 $usertbcGroups[] = $row[0];
226 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
227 "AND users.username = groups.user group by groups.name";
228 $result = sqlStatement($query);
230 $usergGroups = array();
232 while ($row = mysql_fetch_array($result)) {
233 $usergGroups[] = $row[0];
235 foreach ($usertbcGroups as $group) {
236 if(in_array($group,$usergGroups)) {