$count = ldap_count_entries($socket, $result);
// Ensure count is definitely equal to 1
if ($count === 1) {
// Get the entry from the search result, and bind using its DN.
$data = ldap_get_entries($socket, $result);
$auth = ldap_bind($socket, $data[0]['dn'], $User->password);
// Check the return value of the bind action.
if ($auth === -1) {
// Deny login and send message, An LDAP error occurred.
die(formatJSEND("error", "An LDAP error has occurred: " . ldap_error($socket)));
} elseif ($auth == false) {
// Invalid login.
die(formatJSEND("error", "Invalid user name or password."));
} elseif ($auth == true) {
// Check if user already exists within users.php.
if ($User->CheckDuplicate()) {
// Check if we can create a user within users.php.
if ($createuser == true) {
// Save array back to JSON and set the session username.
$User->users[] = array('username' => $User->username, 'password' => null, 'project' => "");
saveJSON("users.php", $User->users);
$_SESSION['user'] = $User->username;
} else {
// Deny login and send message, the user doesn't exist within users.php.
die(formatJSEND("error", "User " . $User->username . " does not exist within Codiad."));
}
} else {
// Set the session username.
$_SESSION['user'] = $User->username;
}
// Set the session language, if given, or set it to english as default.