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']))
13 if (!empty($_POST['languageChoice'])) {
14 $_SESSION['language_choice'] = $_POST['languageChoice'];
17 $_SESSION['language_choice'] = 1;
20 $ip=$_SERVER['REMOTE_ADDR'];
21 if (!authNewSession($_POST['authUser'], $_POST['authPass'], $_POST['authProvider']))
23 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure: $ip");
24 $_SESSION['loginfailure'] = 1;
27 newEvent("login", $_POST['authUser'], $_POST['authProvider'], "success: $ip");
28 $_SESSION['loginfailure'] = null;
29 unset($_SESSION['loginfailure']);
30 //store the very first initial timestamp for timeout errors
31 $_SESSION["last_update"] = time();
33 else if ( (isset($_GET['auth'])) && ($_GET['auth'] == "logout") )
35 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "success");
41 if (authCheckSession())
43 if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
45 require_once("{$GLOBALS['srcdir']}/patient.inc");
46 $logpatient = getPatientData($_SESSION['pid'], "lname, fname, mname");
47 newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'],
48 "{$logpatient['lname']}, {$logpatient['fname']} {$logpatient['mname']} :: encounter " .
49 $_SESSION['encounter']);
52 //newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
55 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
60 if (!isset($_SESSION["last_update"])) {
63 //if page has not been updated in a given period of time, we call login screen
64 if ((time() - $_SESSION["last_update"]) > $timeout) {
65 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "timeout");
69 if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
73 //----------THINGS WE DO IF WE STILL LIKE YOU
75 function authNewSession ($user, $pass, $provider)
77 // check to see if the user belongs to *any* OpenEMR groups in phpGACL -- JRM
78 global $phpgacl_location;
79 if (isset ($phpgacl_location)) {
80 if (acl_get_group_titles($user) == 0) return false;
83 // get details about the user
84 $authDB = sqlQuery("select id, password, authorized, see_auth".
86 " from users where username = '$user'");
88 // if the user is NOT active, get out
89 if ($authDB['active'] != 1) { return false; }
91 // start the HTTP SESSION
92 if(!session_id()) { session_start(); }
94 // compare the submitted password with the stored password
95 if ($authDB['password'] == $pass)
97 //here, we check to see if the user is in fact a member of the correct group:
98 if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'"))
100 $_SESSION['authUser'] = $user;
101 $_SESSION['authGroup'] = $authGroup['name'];
102 $_SESSION['authUserID'] = $authDB['id'];
103 $_SESSION['authPass'] = $pass;
104 $_SESSION['authProvider'] = $provider;
105 $_SESSION['authId'] = $authDB{'id'};
106 $_SESSION['cal_ui'] = $authDB['cal_ui'];
107 $_SESSION['userauthorized'] = $authDB['authorized'];
108 // Some users may be able to authorize without being providers:
109 if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
119 function authCheckSession ()
121 if (isset($_SESSION['authId'])) {
122 $authDB = sqlQuery("select username, password from users where id = '" .
123 $_SESSION['authId']."'");
124 if ($_SESSION['authUser'] == $authDB['username'] &&
125 $_SESSION['authPass'] == $authDB['password'])
138 function authCloseSession ()
142 // $_SESSION = array();
144 //setcookie(session_name(),"","","/");
145 //the following does the same as the above line:
146 //if(isset($_COOKIE[session_name()])) {
148 // session_destroy();
149 unset($_COOKIE[session_name()]);
153 function authLoginScreen()
155 //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
156 header("Location: {$GLOBALS['login_screen']}?error=1");
160 function addUser ($username, $password_md5, $info, $authorized = 'yes')
162 return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
165 function delUser ($id)
167 return sqlQuery("delete from users where id = '$id' limit 0,1");
170 function changePasword ($id, $new_md5)
172 return sqlQuery("update users set password = '$new_md5' where id = '$id'");
175 function getUserList ($cols = '*', $limit = 'all', $start = '0')
178 $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
180 $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
181 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
186 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
189 $rez = sqlStatement("select $cols from groups order by date DESC");
191 $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
192 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
197 function addGroup ($groupname)
199 return sqlInsert("insert into groups (name) values ('$groupname')");
202 function delGroup ($group_id)
204 return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
207 /***************************************************************
209 //Function currently user by new post calendar code to determine
210 //if a given user is in a group with another user
211 //and if so to allow editing of that users events
213 //*************************************************************/
215 function validateGroupStatus ($user_to_be_checked, $group_user) {
216 if (isset($user_to_be_checked) && isset($group_user)) {
217 if ($user_to_be_checked == $group_user) {
221 elseif ($_SESSION['authorizeduser'] == 1)
224 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
225 "AND users.username = groups.user group by groups.name";
226 $result = sqlStatement($query);
228 $usertbcGroups = array();
230 while ($row = mysql_fetch_array($result)) {
231 $usertbcGroups[] = $row[0];
234 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
235 "AND users.username = groups.user group by groups.name";
236 $result = sqlStatement($query);
238 $usergGroups = array();
240 while ($row = mysql_fetch_array($result)) {
241 $usergGroups[] = $row[0];
243 foreach ($usertbcGroups as $group) {
244 if(in_array($group,$usergGroups)) {