2 //----------THINGS WE ALWAYS DO
4 require_once("{$GLOBALS['srcdir']}/log.inc");
5 require_once("{$GLOBALS['srcdir']}/sql.inc");
7 if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
8 isset($_POST['authPass']) && isset($_POST['authProvider']))
10 if (!authNewSession($_POST['authUser'], $_POST['authPass'], $_POST['authProvider']))
12 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure");
15 newEvent("login", $_POST['authUser'], $_POST['authProvider'], "success");
16 //store the very first initial timestamp for timeout errors
17 $_SESSION["last_update"] = time();
19 else if ( (isset($_GET['auth'])) && ($_GET['auth'] == "logout") )
21 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "success");
27 if (authCheckSession())
29 if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
31 require_once("{$GLOBALS['srcdir']}/patient.inc");
32 $logpatient = getPatientData($_SESSION['pid'], "lname, fname, mname");
33 newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'],
34 "{$logpatient['lname']}, {$logpatient['fname']} {$logpatient['mname']} :: encounter " .
35 $_SESSION['encounter']);
38 //newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
41 newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
46 if (!isset($_SESSION["last_update"])) {
49 //if page has not been updated in a given period of time, we call login screen
50 if ((time() - $_SESSION["last_update"]) > $timeout) {
51 newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "timeout");
55 if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
59 //----------THINGS WE DO IF WE STILL LIKE YOU
61 function authNewSession ($user, $pass, $provider)
63 //session_name("OpenEMR");
64 //session_id("81279258720".str_replace(".", "", $_SERVER['REMOTE_ADDR']));
68 //echo "user is: $user pass is: $pass provider is: $provider<br />";
69 $authDB = sqlQuery("select id, password, authorized, see_auth from users " .
70 "where username = '$user'");
71 //echo "<br>auth pass: ".$authDB['password'];
72 if ($authDB['password'] == $pass)
74 //here, we check to see if the user is in fact a member of the correct group:
75 if ($authGroup = sqlQuery("select * from groups where user='$user' and name='$provider'")) {
76 $_SESSION['authUser'] = $user;
77 $_SESSION['authGroup'] = $authGroup['name'];
78 $_SESSION['authUserID'] = $authDB['id'];
79 $_SESSION['authPass'] = $pass;
80 $_SESSION['authProvider'] = $provider;
81 $_SESSION['authId'] = $authDB{'id'};
82 $_SESSION['userauthorized'] = $authDB['authorized'];
83 // Some users may be able to authorize without being providers:
84 if ($authDB['see_auth'] > '2') $_SESSION['userauthorized'] = '1';
94 function authCheckSession ()
96 if (isset($_SESSION['authId'])) {
97 $authDB = sqlQuery("select username, password from users where id = '" .
98 $_SESSION['authId']."'");
99 if ($_SESSION['authUser'] == $authDB['username'] &&
100 $_SESSION['authPass'] == $authDB['password'])
113 function authCloseSession ()
117 // $_SESSION = array();
119 //setcookie(session_name(),"","","/");
120 //the following does the same as the above line:
121 //if(isset($_COOKIE[session_name()])) {
123 // session_destroy();
124 unset($_COOKIE[session_name()]);
128 function authLoginScreen()
130 //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
131 header("Location: {$GLOBALS['login_screen']}");
135 function addUser ($username, $password_md5, $info, $authorized = 'yes')
137 return sqlInsert("insert into users (username, password, info, authorized) values ('$username', '$password_md5', '$info', '$authorized')");
140 function delUser ($id)
142 return sqlQuery("delete from users where id = '$id' limit 0,1");
145 function changePasword ($id, $new_md5)
147 return sqlQuery("update users set password = '$new_md5' where id = '$id'");
150 function getUserList ($cols = '*', $limit = 'all', $start = '0')
153 $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
155 $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
156 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
161 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
164 $rez = sqlStatement("select $cols from groups order by date DESC");
166 $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
167 for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
172 function addGroup ($groupname)
174 return sqlInsert("insert into groups (name) values ('$groupname')");
177 function delGroup ($group_id)
179 return sqlQuery("delete from groups where id = '$group_id' limit 0,1");
182 /***************************************************************
184 //Function currently user by new post calendar code to determine
185 //if a given user is in a group with another user
186 //and if so to allow editing of that users events
188 //*************************************************************/
190 function validateGroupStatus ($user_to_be_checked, $group_user) {
191 if (isset($user_to_be_checked) && isset($group_user)) {
192 if ($user_to_be_checked == $group_user) {
196 elseif ($_SESSION['authorizeduser'] == 1)
199 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($user_to_be_checked) . "\" " .
200 "AND users.username = groups.user group by groups.name";
201 $result = sqlStatement($query);
203 $usertbcGroups = array();
205 while ($row = mysql_fetch_array($result)) {
206 $usertbcGroups[] = $row[0];
209 $query = "SELECT groups.name FROM users,groups WHERE users.username = \"" . mysql_real_escape_string($group_user) . "\" " .
210 "AND users.username = groups.user group by groups.name";
211 $result = sqlStatement($query);
213 $usergGroups = array();
215 while ($row = mysql_fetch_array($result)) {
216 $usergGroups[] = $row[0];
218 foreach ($usertbcGroups as $group) {
219 if(in_array($group,$usergGroups)) {