Code type module improvements:
[openemr.git] / library / adldap / adLDAP_auth.inc
blob391be1e62888df7daadcb5146f9af5254514b5d0
1 <?php
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 
12  */
15 if (isset($_GET['auth']) && ($_GET['auth'] == "login") && isset($_POST['authUser']) &&
16     isset($_POST['clearPass']) && isset($_POST['authProvider']))
18     // handle the LOGIN
19     if (!authNewSession($_POST['authUser'], $_POST['clearPass'], $_POST['authProvider']))
20     {
21             // rejected back to login screen
22         newEvent("login",$_POST['authUser'], $_POST['authProvider'], "failure");
23         $_SESSION['loginfailure'] = 1;
24         authLoginScreen();
25     }
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") )
34     // handle the LOGOUT
35     newEvent("logout", $_SESSION['authUser'], $_SESSION['authProvider'], "success");
36     authCloseSession();
37     authLoginScreen();
39 else
41     if (authCheckSession())
42     {
43         if (isset($_SESSION['pid']) && empty($GLOBALS['DAEMON_FLAG']))
44         {
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']);
50         }
51         //LOG EVERYTHING
52         //newEvent("view", $_SESSION['authUser'], $_SESSION['authProvider'], $_SERVER['REQUEST_URI']);
53     }
54     else {
55             // kick back to the login screen without an authenticated Session
56         newEvent("login",$_POST['authUser'], $_POST['authProvider'], "insufficient data sent");
57         authLoginScreen();
58     }
61 if (!isset($_SESSION["last_update"])) {
62     authLoginScreen();
63 } else {
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");
67         authCloseSession();
68         authLoginScreen();
69     } else {
70         if (empty($GLOBALS['DAEMON_FLAG'])) $_SESSION["last_update"] = time();
71     }
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;
82     }
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".
90                         ", cal_ui, active ".
91                         " from users where username = '$user'");
92     
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; }
98     
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';
114             return true;
115         } else {
116             return false;
117         }
118     }
119     else
120         return false;
123 /* 
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
127  */
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'])
134         {
135             return true;
136         }
137         else {
138             return false;
139         }
140     }
141     else {
142         return false;
143     }
146 function authCloseSession ()
148     ob_start();
149     session_unset();
150 //    $_SESSION = array();
151     session_destroy();
152     //setcookie(session_name(),"","","/");
153     //the following does the same as the above line:
154     //if(isset($_COOKIE[session_name()])) {
155     // session_start();
156     // session_destroy();
157     unset($_COOKIE[session_name()]);
158     //}
161 function authLoginScreen()
163     //header("Location: https://{$_SERVER['HTTP_HOST']}{$GLOBALS['login_screen']}");
164     header("Location: {$GLOBALS['login_screen']}");
165     exit;
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')
185     if ($limit = "all")
186         $rez = sqlStatement("select $cols from users where username != '' order by date DESC");
187     else
188         $rez = sqlStatement("select $cols from users where username != '' order by date DESC limit $limit, $start");
189     for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
190         $tbl[$iter] = $row;
191     return $tbl;
194 function getProviderList ($cols = '*', $limit= 'all', $start = '0')
196     if ($limit = "all")
197         $rez = sqlStatement("select $cols from groups order by date DESC");
198     else
199         $rez = sqlStatement("select $cols from groups order by date DESC limit $limit, $start");
200     for ($iter = 0; $row = sqlFetchArray($rez); $iter++)
201         $tbl[$iter] = $row;
202     return $tbl;
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 /***************************************************************
216 //pennfirm
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) {
227             return true;
228         }
229         elseif ($_SESSION['authorizeduser'] == 1)
230             return true;
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];
240         }
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];
250         }
251         foreach ($usertbcGroups as $group) {
252               if(in_array($group,$usergGroups)) {
253               return true;
254             }
255         }
257     }
259     return false;