When logging login attempts, record the machine ip address in log table along with...
[openemr.git] / library / auth.inc
blob0102962f932f3853929a32d1f3c642969845d50a
1 <?php
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']))
14     {
15         newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure: $ip");
16         $_SESSION['loginfailure'] = 1;
17         authLoginScreen();
18     }
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");
28     authCloseSession();
29     authLoginScreen();
31 else
33     if (authCheckSession())
34     {
35         if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
36         {
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']);
42         }
43         //LOG EVERYTHING
44         //newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
45     }
46     else {
47         newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
48         authLoginScreen();
49     }
52 if (!isset($_SESSION["last_update"])) {
53     authLoginScreen();
54 } else {
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");
58         authCloseSession();
59         authLoginScreen();
60     } else {
61         if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
62     }
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;
73     }
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)
86     {
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'"))
89         {
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';
100             return true;
101         } else {
102             return false;
103         }
104     }
105     else
106         return false;
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'])
116         {
117             return true;
118         }
119         else {
120             return false;
121         }
122     }
123     else {
124         return false;
125     }
128 function authCloseSession ()
130     ob_start();
131     session_unset();
132 //    $_SESSION = array();
133     session_destroy();
134     //setcookie(session_name(),"","","/");
135     //the following does the same as the above line:
136     //if(isset($_COOKIE[session_name()])) {
137     // session_start();
138     // session_destroy();
139     unset($_COOKIE[session_name()]);
140     //}
143 function authLoginScreen()
145     //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
146     header("Location: {$GLOBALS['login_screen']}?error=1");
147     exit;
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')
167     if ($limit = "all")
168         $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
169     else
170         $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
171     for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
172         $tbl[$iter] = $row;
173     return $tbl;
176 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
178     if ($limit = "all")
179         $rez = sqlStatement("select $cols from groups order by date DESC");
180     else
181         $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
182     for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
183         $tbl[$iter] = $row;
184     return $tbl;
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 /***************************************************************
198 //pennfirm
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) {
209             return true;
210         }
211         elseif ($_SESSION['authorizeduser'] == 1)
212             return true;
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];
222         }
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];
232         }
233         foreach ($usertbcGroups as $group) {
234               if(in_array($group,$usergGroups)) {
235               return true;
236             }
237         }
239     }
241     return false;