function handleRequest($form) {
global $gallery;
$results = array();
$error = array();
if (isset($form['action']['login'])) {
if (empty($form['username'])) {
$error[] = 'form[error][username][missing]';
}
if (empty($form['password'])) {
$error[] = 'form[error][password][missing]';
}
if (empty($error)) {
list ($ret, $isDisabled) = GalleryCoreApi::isDisabledUsername($form['username']);
if ($ret) {
return array($ret, null);
}
if ($isDisabled) {
$error[] = 'form[error][username][disabled]';
}
}
if (empty($error)) {
list ($ret, $user) = GalleryCoreApi::fetchUserByUsername($form['username']);
if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
return array($ret, null);
}
/* LDAP Code begin */
$ldapRet = $this->ldapAuthentication($form['username'],$form['password']);
if ($ldapRet && !is_array($ldapRet)) {
// any error with LDAP connection.
$error[] = "form[error]$ldapRet";
}
else if(is_array($ldapRet)){ // User found:
// At first login, create new User
if (!isset($user)) {
list ($ret, $user) = GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryUser');
if ($ret) {
return array($ret, null);
}
if (!isset($user)) {
return array(GalleryCoreApi::error(ERROR_MISSING_OBJECT), null);
}
$ret = $user->create($username);
if ($ret) { // this should never happen:
if (!($ret->getErrorCode() & ERROR_COLLISION)) {
return array($ret, null);
}
// Set our error status and fall back to the view
$error[] = 'form[error][userName][exists]';
}
}
// set the users properties and save them:
$user->setEmail($ldapRet['email']);
$user->setFullName($ldapRet['fullName']);
$user->changePassword($ldapRet['password']);
GalleryCoreApi::acquireWriteLock($user->getId());
$ret = $user->save();
GalleryCoreApi::releaseLocks($user->getId());
if ($ret) {
return array($ret, null);
}
}
else {
// User not found in LDAP should not be a problem: normal user autentication
}
/* LDAP Code end */
GalleryUtilities::unsanitizeInputValues($form['password'], false);
$isCorrect = (isset($user) && $user->isCorrectPassword($form['password']));
/* Prepare for validation */
$options = array('pass' => $isCorrect);
list ($ret, $options['level']) =
GalleryCoreApi::getPluginParameter('module', 'core', 'validation.level');
if ($ret) {
return array($ret, null);
}
if ($options['level'] == 'MEDIUM') {
$options['key'] = 'core.UserLogin.' . $form['username'];
}
if ($options['level'] == 'OFF') {
$pluginInstances = array();
} else if (isset($this->_pluginInstances)) {
$pluginInstances = $this->_pluginInstances;
} else {
list ($ret, $pluginInstances) =
GalleryCoreApi::getAllFactoryImplementationIds('GalleryValidationPlugin');
if ($ret) {
return array($ret, null);
}
foreach (array_keys($pluginInstances) as $pluginId) {
list ($ret, $pluginInstances[$pluginId]) =
GalleryCoreApi::newFactoryInstanceById('GalleryValidationPlugin',
$pluginId);
if ($ret) {
return array($ret, null);
}
}
}
/* Let each plugin do its verification */
foreach ($pluginInstances as $plugin) {
list ($ret, $pluginErrors, $continue) =
$plugin->performValidation($form, $options);
if ($ret) {
return array($ret, null);
}
$error = array_merge($error, $pluginErrors);
if (!$continue) {
break;
}
}
}
if (empty($error)) {
if ($isCorrect) {
$gallery->setActiveUser($user);
$event = GalleryCoreApi::newEvent('Gallery::Login');
$event->setEntity($user);
list ($ret, $redirect) = GalleryCoreApi::postEvent($event);
if ($ret) {
return array($ret, null);
}
/* Redirect if requested by event listener, otherwise return */
if (!empty($redirect)) {
$results['redirect'] = array_shift($redirect);
} else {
$results['return'] = 1;
}
} else {
$error[] = 'form[error][invalidPassword]';
}
}
if (!empty($error)) {
if (!empty($form['username'])) {
$event = GalleryCoreApi::newEvent('Gallery::FailedLogin');
$event->setData(array('userName' => $form['username']));
list ($ret, $ignored) = GalleryCoreApi::postEvent($event);
if ($ret) {
return array($ret, null);
}
}
}
} else if (isset($form['action']['cancel'])) {
$results['return'] = 1;
}
if (!empty($error)) {
$results['delegate']['view'] = 'core.UserAdmin';
$results['delegate']['subView'] = 'core.UserLogin';
}
$results['status'] = array();
$results['error'] = $error;
return array(null, $results);