log in -> is logged in if (isset($_POST['login']) || (isset($_SERVER['REMOTE_USER']) && (!isset($_SESSION['loggedin']) || !$_SESSION['loggedin']))) { // Check username and password if ($user = validateUser()) { $_SESSION['loggedin'] = $user; $content = htmlRequestForm($user);; } else { $_SESSION['loggedin'] = false; $content = htmlLoginForm(); } } else if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] && isset($_POST['send']) && $_POST['send']) { // User is already logged in, check the form's status if ($result = checkFormErrors()) { // display the form (with errors if applicable) $content = htmlRequestForm((isset($_GET['new']) ? $_SESSION['loggedin'] : NULL), (is_bool($result) ? '' : $result)); } else { sendFormData(); // get a success message $content = htmlSuccessMessage(); } } else { $_SESSION['loggedin'] = false; // Not logged in, not logging in: display login $content = htmlLoginForm(); } $title = 'Faculty Study Room Application'; // Attach a template and output $template = new Template('../../templates/public-find-sidebar.tmpl'); $template->AddParam('TITLE', $title); $template->AddParam('BODY', $content); $template->AddParam('SIDEBAR', ''); $template->EchoOutput(); // We're done. Proceed no further. exit(0); // this will return an array of required fields function getRequiredFields() { return array('first', 'last', 'banner', 'dept', 'phone', 'email'); } // this will return true if a value is required function checkIfRequired($element) { return in_array($element, getRequiredFields()); } // this will return a HTML form for the actual request content function htmlRequestForm($prepopulate = NULL, $error = '') { // define array indexes $vars = Array(); foreach (getRequiredFields() as $val) { $vars[$val.'_missing'] = ''; } // Prepopulate any provided info (probably user info from LDAP) if ($prepopulate) { foreach ($prepopulate as $k => $val) { $vars[$k] = htmlentities($val); } } // copy over POST stream for user and course foreach (array_merge(getRequiredFields(), array('assigned', 'room', 'downtown', 'describe', 'needs')) as $val) { if (!isset($vars[$val])) { $vars[$val] = ''; } if (isset($_POST[$val]) && $_POST[$val]) { $vars[$val] = htmlentities($_POST[$val]); } else if (isset($_POST['send']) && $_POST['send'] && checkIfRequired($val)) { $vars[$val.'_missing'] = '(required)'; } } if (isset($_POST['email']) && $_POST['email'] && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { $vars['email_missing'] = '(invalid)'; } ob_start(); ?>
Successfully sent this request!
$html = ob_get_contents(); ob_end_clean(); return $html; } // This will validate a username/password combination, returns a hash of user values if successful, false if not function validateUser() { // disable error reporting. ldap_search will return an operations error if the bind had a blank password. $old_error_reporting_value = error_reporting(); error_reporting(0); // default return value to false $user = false; // ensure username and password given if (!isset($_POST['username']) || !isset($_POST['password'])) { if (!isset($_SERVER['REMOTE_USER'])) { return false; } } // validate that user info is formatted correctly if (isset($_SERVER['REMOTE_USER']) && preg_match('/^[a-z]{3}[0-9]{3}@utsa[.]edu$/i', $_SERVER['REMOTE_USER'])) { $myid = str_replace('@utsa.edu', '', $_SERVER['REMOTE_USER']); } else if (isset($_SERVER['REMOTE_USER']) && preg_match('/^[a-z]{3}[0-9]{3}$/i', $_SERVER['REMOTE_USER'])) { $myid = $_SERVER['REMOTE_USER']; } else if (preg_match('/[a-z]{3}[0-9]{3}/i', $_POST['username'])) { $myid = $_POST['username']; } else { return false; } // Open conenction to ldap if ($ldap = ldap_connect('adauth.it.utsa.edu')) { // Start TLS for password security if (!ldap_start_tls($ldap)) { error_log(ldap_error($ldap)); return false; } // Disable referral chasing (or deal with search errors from AD) ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0); // try to bind with user-supplied values. $dn = ''; $pw = ''; if (isset($_POST['password'])) { $dn = $myid.'@UTSARR.NET'; $pw = $_POST['password']; } else if (isset($_SERVER['Shib-Session-ID'])) { $dn = 'cn=ezproxy ldap auth, ou=idms,ou=service accounts,ou=administrative computing services,dc=utsarr,dc=net'; $pw = 'hSD9cE9iMY6virKNRQJk'; } if (ldap_bind($ldap, $dn, $pw)) { // search for user info // Note: Will bind successfully but not allow searching with blank password. if ($rs = ldap_search($ldap, 'dc=UTSARR, dc=NET', '(sAMAccountName='.$myid.')', array('dn', 'employeeid', 'sn', 'givenname', 'telephonenumber', 'mail'))) { // fetch user info if ($entries = ldap_get_entries($ldap, $rs)) { // if one exact match, use it if ($entries['count'] == 1) { // connect to database with permission information $db = new mysqli("localhost","newref",'5%45Xs0x:b=~6T3', 'ldapusers'); // query for faculty, professional staff, or library staff, based on myUTSA ID $id = $db->real_escape_string($myid); // $result = mysqlI->query("select * from lib_users where utid='$id' and (type in ('fac', 'pstf') or (type = 'staf' and dept='01160'))"); $result = $db->query("select * from lib_users where utid='$id' and (type in ('fac', 'pstf') or (type = 'staf'))"); // check query result if ($row = $result->fetch_assoc()) { // if good, assign values from ldap $user['first'] = $entries[0]['givenname'][0]; $user['last'] = $entries[0]['sn'][0]; $user['phone'] = $entries[0]['telephonenumber'][0]; $user['email'] = $entries[0]['mail'][0]; // transform the banner id into @01234567 format $user['banner'] = '@'.substr($entries[0]['employeeid'][0], 1); $user['myutsa'] = $myid; } else { // if not, blank user $user = false; } } } else { error_log(ldap_error($ldap)); } } else { error_log(ldap_error($ldap)); } } else { error_log(ldap_error($ldap)); } } error_reporting($old_error_reporting_value); return $user; } // This will return the HTML of a login form, with error messages as needed function htmlLoginForm() { $vars = Array(); foreach (array('username', 'password') as $key) { $vars[$key] = ''; $vars[$key.'_missing'] = ''; } $vars['login_invalid'] = ''; // If login information was tried, indicate errors if (isset($_POST['login']) && $_POST['login']) { // check each required form element foreach (array('Username', 'Password') as $key) { $vars[strtolower($key)] = htmlentities($_POST[strtolower($key)]); if (! $_POST[strtolower($key)] ) { $vars[strtolower($key).'_missing'] = $key.' is required'; } } // warn user if username and password were supplied - this means they are invalid if ($_POST['username'] && $_POST['password']) { $vars['login_invalid'] = 'Your username and password were not authenticated. Please try again.'; } } ob_start(); ?>Faculty may apply to be given use of a Library Study Room.
Please enter your myUTSA ID and password and press the "Validate" button.