2 //----------THINGS WE ALWAYS DO
4 require_once("{$GLOBALS['srcdir']}/log.inc");
5 require_once("{$GLOBALS['srcdir']}/sql.inc");
6 require_once("{$GLOBALS['srcdir']}/adldap/adLDAP.php");
7 require_once("{$GLOBALS['srcdir']}/adldap/adLDAP_conf.inc");
8 // added for the phpGACL group check -- JRM
9 require_once("{$GLOBALS['srcdir']}/acl.inc");
11 /* to use adLDAP authentication we require the password in cleartext
15 if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
16 isset($_POST['clearPass']) && isset($_POST['authProvider']))
19 if (!authNewSession($_POST['authUser'], $_POST['clearPass'], $_POST['authProvider']))
21 // rejected back to login screen
22 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure");
23 $_SESSION['loginfailure'] = 1;
26 newEvent("login", $_POST['authUser'], $_POST['authProvider'], "success");
27 $_SESSION['loginfailure'] = null;
28 unset($_SESSION['loginfailure']);
29 //store the very first initial timestamp for timeout errors
30 $_SESSION["last_update"] = time();
32 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 // kick back to the login screen without an authenticated Session
56 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
61 if (!isset($_SESSION["last_update"])) {
64 //if page has not been updated in a given period of time, we call login screen
65 if ((time() - $_SESSION["last_update"]) > $timeout) {
66 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "timeout");
70 if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
74 //----------THINGS WE DO IF WE STILL LIKE YOU
76 function authNewSession ($user, $pass, $provider)
78 // check to see if the user belongs to *any* OpenEMR groups in phpGACL -- JRM
79 global $phpgacl_location;
80 if (isset ($phpgacl_location)) {
81 if (acl_get_group_titles($user) == 0) return false;
84 // adldap_options defined in adLDAP_conf.inc
85 global $adldap_options;
86 $adldap = new adLDAP($adldap_options);
88 // get details about the user
89 $authDB = sqlQuery("select id, password, authorized, see_auth".
91 " from users where username = '$user'");
93 // if the user is NOT active, get out
94 if ($authDB['active'] != 1) { return false; }
96 // invalid adLDAP credentials
97 if (! $adldap->authenticate($user, $pass)) { return false; }
99 // start the HTTP SESSION
100 if(!session_id()) { session_start(); }
102 if ($authDB != null) {
103 //here, we check to see if the user is in fact a member of the correct group:
104 if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'")) {
105 $_SESSION['authUser'] = $user;
106 $_SESSION['authGroup'] = $authGroup['name'];
107 $_SESSION['authUserID'] = $authDB['id'];
108 $_SESSION['authProvider'] = $provider;
109 $_SESSION['authId'] = $authDB['id'];
110 $_SESSION['userauthorized'] = $authDB['authorized'];
111 $_SESSION['cal_ui'] = $authDB['cal_ui'];
112 // Some users may be able to authorize without being providers:
113 if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
124 * Based on the Session authId value determine if the user
125 * is authenticated by comparing session authUser
126 * with what is stored in the database
128 function authCheckSession ()
130 if (isset($_SESSION['authId'])) {
131 $authDB = sqlQuery("select username from users where id = '" .
132 $_SESSION['authId']."'");
133 if ($_SESSION['authUser'] == $authDB['username'])
146 function authCloseSession ()
150 // $_SESSION = array();
152 //setcookie(session_name(),"","","/");
153 //the following does the same as the above line:
154 //if(isset($_COOKIE[session_name()])) {
156 // session_destroy();
157 unset($_COOKIE[session_name()]);
161 function authLoginScreen()
163 //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
164 header("Location: {$GLOBALS['login_screen']}");
168 function addUser ($username, $password_md5, $info, $authorized = 'yes')
170 return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
173 function delUser ($id)
175 return sqlQuery("delete from users where id = '$id' limit 0,1");
178 function changePasword ($id, $new_md5)
180 return sqlQuery("update users set password = '$new_md5' where id = '$id'");
183 function getUserList ($cols = '*', $limit = 'all', $start = '0')
186 $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
188 $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
189 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
194 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
197 $rez = sqlStatement("select $cols from groups order by date DESC");
199 $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
200 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
205 function addGroup ($groupname)
207 return sqlInsert("insert into groups (name) values ('$groupname')");
210 function delGroup ($group_id)
212 return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
215 /***************************************************************
217 //Function currently user by new post calendar code to determine
218 //if a given user is in a group with another user
219 //and if so to allow editing of that users events
221 //*************************************************************/
223 function validateGroupStatus ($user_to_be_checked, $group_user) {
224 if (isset($user_to_be_checked) && isset($group_user)) {
225 if ($user_to_be_checked == $group_user) {
229 elseif ($_SESSION['authorizeduser'] == 1)
232 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
233 "AND users.username = groups.user group by groups.name";
234 $result = sqlStatement($query);
236 $usertbcGroups = array();
238 while ($row = mysql_fetch_array($result)) {
239 $usertbcGroups[] = $row[0];
242 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
243 "AND users.username = groups.user group by groups.name";
244 $result = sqlStatement($query);
246 $usergGroups = array();
248 while ($row = mysql_fetch_array($result)) {
249 $usergGroups[] = $row[0];
251 foreach ($usertbcGroups as $group) {
252 if(in_array($group,$usergGroups)) {