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 //session_name("OpenEMR");
76 //session_id("81279258720".str_replace(".", "", $_SERVER['REMOTE_ADDR']));
77 if(!session_id()) { session_start(); }
79 //(CHEMED) added cal_ui to the list of fields, so we can change calendar UI for this user.
80 // Is this the right place to do it?
81 $authDB = sqlQuery("select id, password, authorized, see_auth, cal_ui from users " .
82 "where username = '$user'");
83 //echo "user is: $user pass is: $pass provider is: $provider<br />";
84 //echo "<br>auth pass: ".$authDB['password'];
85 if ($authDB['password'] == $pass)
87 //here, we check to see if the user is in fact a member of the correct group:
88 if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'"))
90 $_SESSION['authUser'] = $user;
91 $_SESSION['authGroup'] = $authGroup['name'];
92 $_SESSION['authUserID'] = $authDB['id'];
93 $_SESSION['authPass'] = $pass;
94 $_SESSION['authProvider'] = $provider;
95 $_SESSION['authId'] = $authDB{'id'};
96 $_SESSION['cal_ui'] = $authDB['cal_ui'];
97 $_SESSION['userauthorized'] = $authDB['authorized'];
98 // Some users may be able to authorize without being providers:
99 if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
109 function authCheckSession ()
111 if (isset($_SESSION['authId'])) {
112 $authDB = sqlQuery("select username, password from users where id = '" .
113 $_SESSION['authId']."'");
114 if ($_SESSION['authUser'] == $authDB['username'] &&
115 $_SESSION['authPass'] == $authDB['password'])
128 function authCloseSession ()
132 // $_SESSION = array();
134 //setcookie(session_name(),"","","/");
135 //the following does the same as the above line:
136 //if(isset($_COOKIE[session_name()])) {
138 // session_destroy();
139 unset($_COOKIE[session_name()]);
143 function authLoginScreen()
145 //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
146 header("Location: {$GLOBALS['login_screen']}?error=1");
150 function addUser ($username, $password_md5, $info, $authorized = 'yes')
152 return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
155 function delUser ($id)
157 return sqlQuery("delete from users where id = '$id' limit 0,1");
160 function changePasword ($id, $new_md5)
162 return sqlQuery("update users set password = '$new_md5' where id = '$id'");
165 function getUserList ($cols = '*', $limit = 'all', $start = '0')
168 $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
170 $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
171 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
176 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
179 $rez = sqlStatement("select $cols from groups order by date DESC");
181 $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
182 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
187 function addGroup ($groupname)
189 return sqlInsert("insert into groups (name) values ('$groupname')");
192 function delGroup ($group_id)
194 return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
197 /***************************************************************
199 //Function currently user by new post calendar code to determine
200 //if a given user is in a group with another user
201 //and if so to allow editing of that users events
203 //*************************************************************/
205 function validateGroupStatus ($user_to_be_checked, $group_user) {
206 if (isset($user_to_be_checked) && isset($group_user)) {
207 if ($user_to_be_checked == $group_user) {
211 elseif ($_SESSION['authorizeduser'] == 1)
214 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
215 "AND users.username = groups.user group by groups.name";
216 $result = sqlStatement($query);
218 $usertbcGroups = array();
220 while ($row = mysql_fetch_array($result)) {
221 $usertbcGroups[] = $row[0];
224 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
225 "AND users.username = groups.user group by groups.name";
226 $result = sqlStatement($query);
228 $usergGroups = array();
230 while ($row = mysql_fetch_array($result)) {
231 $usergGroups[] = $row[0];
233 foreach ($usertbcGroups as $group) {
234 if(in_array($group,$usergGroups)) {