?`~'); ////////////////////////////////////////////////////////////////////////////// // Core API hooks /** * Implementation of hook_help(). */ function ldapprov_help($path, $arg) { switch ($path) { case 'user/validate': return '
'. t('Please check your e-mail and click the link in the message to confirm your address. If you are unable to click the link, you can copy the secret code from the e-mail and enter it below.') .'
'; } } /** * Implementation of hook_init(). */ function ldapprov_init() { // Initiates LDAP object. if (LDAPPROV_ENABLED) { include_once(drupal_get_path('module', 'ldapprov') .'/ldapprov.conf.inc'); _ldapprov_init(); } } /** * Implementation of hook_perm(). */ function ldapprov_perm() { return array(LDAPPROV_PERMISSION, LDAPPROV_ROLE_PERMISSION); } /** * Implementation of hook_theme(). */ function ldapprov_theme() { return array( 'ldapprov_list_form' => array( 'arguments' => array('form' => NULL), 'file' => 'ldapprov.theme.inc', ), ); } /** * Implementation of hook_menu(). */ function ldapprov_menu() { return array( 'admin/settings/ldapprov' => array( 'title' => 'LDAP Provisioning', 'description' => 'Configure LDAP provisioning settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'ldapprov.admin.inc', ), 'admin/user/accounts' => array( 'title' => 'Account management', 'description' => 'Configure LDAP provisioning accounts.', 'page callback' => 'ldapprov_list', 'access arguments' => array(LDAPPROV_PERMISSION), 'weight' => -1, ), 'admin/user/accounts/pending' => array( 'title' => 'Pending', 'type' => MENU_DEFAULT_LOCAL_TASK, ), 'admin/user/accounts/pending/create' => array( 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_create', 3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, ), 'admin/user/accounts/created' => array( 'title' => 'Created', 'page callback' => 'ldapprov_list', 'page arguments' => array(3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, 'weight' => 1, ), 'admin/user/accounts/created/create' => array( 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_create', 3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, ), 'admin/user/accounts/rejected' => array( 'title' => 'Rejected', 'page callback' => 'ldapprov_list', 'page arguments' => array(3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, 'weight' => 2, ), 'admin/user/accounts/rejected/create' => array( 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_create', 3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, ), 'admin/user/accounts/deleted' => array( 'title' => 'Deleted', 'page callback' => 'ldapprov_list', 'page arguments' => array(3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, 'weight' => 3, ), 'admin/user/accounts/deleted/create' => array( 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_create', 3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, ), 'admin/user/accounts/unverified' => array( 'title' => 'Unverified', 'page callback' => 'ldapprov_list', 'page arguments' => array(3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, 'weight' => 4, ), 'admin/user/accounts/unverified/create' => array( 'title' => 'Create', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_create', 3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, ), 'admin/user/accounts/new' => array( 'title' => 'New', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_create', 3), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_LOCAL_TASK, 'weight' => 5, ), 'ldapprov/template' => array( 'title' => 'Batch upload file template', 'page callback' => '_ldapprov_template', 'page arguments' => array(2), 'access arguments' => array(LDAPPROV_PERMISSION), 'type' => MENU_CALLBACK, ), ); } /** * Implementation of hook_menu_alter(). */ function ldapprov_menu_alter(&$callbacks) { // LDAPPROV_ENABLED won't work here because the variable. // is changed on settings save. if (variable_get('ldapprov_enabled', 0)) { // Take over the registration form. $callbacks['user/register']['page arguments'] = array('ldapprov_register', 2); unset($callbacks['user/register']['file']); // Secret code validation. $callbacks['user/validate'] = array( 'title' => 'Validate e-mail', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_code', 2), 'access callback' => 'user_register_access', 'type' => MENU_LOCAL_TASK, ); } if (variable_get('ldapprov_enabled', 0) && variable_get('ldapprov_disable_create', 0)) { unset($callbacks['admin/user/user/create']); } } /** * Implementation of hook_user(). */ function ldapprov_user($op, &$edit, &$account, $category = NULL) { if (LDAPPROV_ENABLED) { switch ($op) { case 'delete': _ldapprov_user_delete($edit, $account); break; } } } /** * Implementation of hook_form_alter(). */ function ldapprov_form_alter(&$form, $form_state, $form_id) { switch ($form_id) { case "user_profile_form": if (LDAPPROV_ENABLED && user_access(LDAPPROV_PERMISSION)) { // Set a custom form validate and submit handlers. $form['#validate'][] = 'ldapprov_account_validate'; $form['#submit'][] = 'ldapprov_account_submit'; } break; } } /** * Implementation of hook_mail(). */ function ldapprov_mail($key, &$message, $params) { $language = $message['language']; $account = $params['account'] ? $params['account'] : (object)array(); $variables = array_merge(user_mail_tokens($account, $language), $params['variables'] ? $params['variables'] : array()); $message['subject'] .= _ldapprov_mail_text($key .'_subject', $language, $variables); $message['body'][] = _ldapprov_mail_text($key .'_body', $language, $variables); } ////////////////////////////////////////////////////////////////////////////// // FAPI /** * Account save validate handler. */ function ldapprov_account_validate($form, &$form_state) { $values = $form_state['values']; $account = $form['_account']['#value']; if (isset($values['name']) && $account->name != $values['name']) { _ldapprov_user_validate($values['name']); } } /** * Account save submit handler. */ function ldapprov_account_submit($form, &$form_state) { $values = $form_state['values']; $account = $form['_account']['#value']; if (isset($values['name']) && $account->name != $values['name']) { _ldapprov_user_update($values['name'], $account); } } ////////////////////////////////////////////////////////////////////////////// // Mail strings /** * Returns a mail string for a variable name. * * Used by ldapprov_mail() and the settings forms to retrieve strings. */ function _ldapprov_mail_text($key, $language = NULL, $variables = array()) { $langcode = isset($language) ? $language->language : NULL; if ($admin_setting = variable_get('ldapprov_mail_'. $key, FALSE)) { // An admin setting overrides the default string. return strtr($admin_setting, $variables); } else { // No override, return default string. switch ($key) { case 'code_subject': return t('Validate your e-mail at !site', $variables, $language->language); case 'code_body': return t("!first_name !last_name,\n\nThank you for registering at !site. You may now validate your e-mail address by entering the code\n\n!code\n\nat !validate_uri (by copying and pasting).\n\nYou may also validate the e-mail by clicking on this link or copying and pasting it in your browser:\n\n!validate_url\n\n-- !site team", $variables, $language->language); case 'reject_subject': return t('Your request at !site has been rejected', $variables, $language->language); case 'reject_body': return t("!first_name !last_name,\n\nSorry, but your account request at !site has been rejected. Please resubmit the registration form with more information.\n\n-----\n\n!message\n\n-- !site team", $variables, $language->language); case 'notify_subject': return t('New account request at !site', $variables, $language->language); case 'notify_body': return t("!first_name !last_name (!mailto) has requested the account at !site.\n\nClick this link !create_url to process the request.", $variables, $language->language); case 'create_subject': return t('The account has been created for you at !site', $variables, $language->language); case 'create_body': return t("!first_name !last_name,\n\nThe account at !site has been created for you. You may now log in to !login_uri using the following username and password:\n\nusername: !username\npassword: !password\n\nYou may also log in by clicking on this link or copying and pasting it in your browser:\n\n!login_url\n\nThis is a one-time login, so it can be used only once.\n\n-----\n\n!message\n\n-- !site team", $variables, $language->language); case 'delete_subject': return t('Your account has been deleted at !site', $variables, $language->language); case 'delete_body': return t("!first_name !last_name,\n\nYour account !username has been deleted at !site.\n\n-- !site team", $variables, $language->language); } } } ////////////////////////////////////////////////////////////////////////////// // Account actions /** * User validate action. */ function _ldapprov_user_validate($name) { global $_ldapprov_ldap; // Search for the entry in LDAP. if (isset($name)) { if (!$_ldapprov_ldap->connect(LDAPPROV_DN, LDAPPROV_PASS)) { watchdog('ldapprov', 'User validate: user data could not be read in the LDAP directory. Could not bind as %dn.', array('%dn' => LDAPPROV_DN), WATCHDOG_ERROR); form_set_error('name', t('User data could not be read in the LDAP directory. Please contact site administrator.')); return; } $basedn = $_ldapprov_ldap->getOption('basedn'); $name_attr = $_ldapprov_ldap->getOption('user_attr') ? $_ldapprov_ldap->getOption('user_attr') : LDAP_DEFAULT_USER_ATTRIBUTE; if ($ret = $_ldapprov_ldap->search($basedn, '('. $name_attr .'='. $name .')', array($name_attr))) { form_set_error('name', t('The DN %dn is already taken in LDAP.', array('%dn' => $ret[0]['dn']))); } $_ldapprov_ldap->disconnect(); } } /** * User update action. */ function _ldapprov_user_update($name, $account) { global $_ldapprov_ldap; if (!$_ldapprov_ldap->connect(LDAPPROV_DN, LDAPPROV_PASS)) { watchdog('ldapprov', 'User update: user data could not be read in the LDAP directory. Could not bind as %dn.', array('%dn' => LDAPPROV_DN), WATCHDOG_ERROR); drupal_set_message(t('User update: user data could not be read in the LDAP directory. Please contact site administrator.'), 'error'); return; } $basedn = $_ldapprov_ldap->getOption('basedn'); $name_attr = $_ldapprov_ldap->getOption('user_attr') ? $_ldapprov_ldap->getOption('user_attr') : LDAP_DEFAULT_USER_ATTRIBUTE; if (!$_ldapprov_ldap->rename_entry($account->ldap_dn, $name_attr .'='. $name, $basedn, TRUE)) { watchdog('ldapprov', 'User update: user %name ldap entry %dn was not renamed to a new name %name_new.', array('%name' => $account->name, '%dn' => $account->ldap_dn, '%name_new' => $name), WATCHDOG_ERROR); } $_ldapprov_ldap->disconnect(); user_save($account, array('ldap_dn' => $name_attr .'='. $name .','. $basedn, 'ldap_config' => $_ldapprov_ldap->getOption('sid'))); db_query("UPDATE {authmap} SET authname = '%s' WHERE module = 'ldapauth' AND uid = %d", $name, $account->uid); } /** * User delete action. */ function _ldapprov_user_delete(&$edit, &$account) { global $user, $_ldapprov_ldap; if (!$_ldapprov_ldap->connect(LDAPPROV_DN, LDAPPROV_PASS)) { watchdog('ldapprov', 'User deletion: user data could not be read in the LDAP directory. Could not bind as %dn.', array('%dn' => LDAPPROV_DN), WATCHDOG_ERROR); drupal_set_message(t('The user !name has not been deleted from the LDAP directory.', array('!name' => theme('username', $account))), 'error'); return; } $basedn = $_ldapprov_ldap->getOption('basedn'); $name_attr = $_ldapprov_ldap->getOption('user_attr') ? $_ldapprov_ldap->getOption('user_attr') : LDAP_DEFAULT_USER_ATTRIBUTE; $name = _ldapprov_get_name_from_dn($account->ldap_dn); if ($ret = $_ldapprov_ldap->search($basedn, '('. $name_attr .'='. $name .')', array($name_attr))) { if ($_ldapprov_ldap->delete_entry($account->ldap_dn)) { watchdog('ldapprov', 'User deletion: user %name has been deleted from the LDAP directory.', array('%name' => $account->name), WATCHDOG_WARNING); } else { watchdog('ldapprov', 'User deletion: user %name has not been deleted from the LDAP directory.', array('%name' => $account->name), WATCHDOG_ERROR); drupal_set_message(t('The user !name has not been deleted from the LDAP directory.', array('!name' => theme('username', $account))), 'error'); } } else { watchdog('ldapprov', 'User deletion: user %name is not found in LDAP directory.', array('%name' => $account->name), WATCHDOG_WARNING, l(t('edit'), 'user/'. $account->uid .'/edit'), WATCHDOG_ERROR); } $_ldapprov_ldap->disconnect(); // Mark registration entry as deleted. $time = time(); $result = db_query("SELECT * FROM {ldapprov} WHERE uid = %d", $account->uid); if ($row = db_fetch_object($result)) { db_query("UPDATE {ldapprov} SET name = '%s', status = '4', cuid = %d, cname = '%s', approved = %d WHERE rid = %d", $account->name, $user->uid, $user->name, $time, $row->rid); } else { db_query("INSERT INTO {ldapprov} (name, mail, status, registered, approved, cuid, cname) VALUES ('%s', '%s', '4', %d, %d, %d, '%s')", $account->name, $account->mail, $time, $time, $user->uid, $user->name); } // Mail one time deletion notification. $variables = array('!first_name' => $row->first_name, '!last_name' => $row->last_name); $params = array('account' => $account, 'variables' => $variables); $message = drupal_mail('ldapprov', 'delete', $account->mail, user_preferred_language($account), $params); if ($message['result']) { watchdog('ldapprov', 'Account deletion notification e-mail mailed to %name at %mail.', array('%name' => $account->name, '%mail' => $account->mail)); } else { watchdog('ldapprov', 'Error mailing account deletion notification to %name at %mail.', array('%name' => $account->name, '%mail' => $account->mail), WATCHDOG_ERROR); } // If this user has created other users, then capture his name in the db for the record. db_query("UPDATE {ldapprov} SET cname = '%s' WHERE cuid = %d", $account->name, $account->uid); } ////////////////////////////////////////////////////////////////////////////// // LDAP related functions /** * Initiates LDAP object. * * @return */ function _ldapprov_init() { global $_ldapprov_ldap; $server = _ldapprov_get_server(); $_ldapprov_ldap = new LDAPInterface(); $_ldapprov_ldap->setOption('sid', $server->sid); $_ldapprov_ldap->setOption('name', $server->name); $_ldapprov_ldap->setOption('server', $server->server); $_ldapprov_ldap->setOption('port', $server->port); $_ldapprov_ldap->setOption('tls', $server->tls); $_ldapprov_ldap->setOption('encrypted', $server->encrypted); $_ldapprov_ldap->setOption('basedn', $server->basedn); $_ldapprov_ldap->setOption('user_attr', $server->user_attr); $_ldapprov_ldap->setOption('mail_attr', $server->mail_attr); } /** * Load server settings. * * @return * An obgject with the server settings. */ function _ldapprov_get_server() { $result = db_query("SELECT * FROM {ldapauth} WHERE sid = %d", LDAPPROV_SERVER); return db_fetch_object($result); } /** * Get a user name from a dn. * * @param $dn * A LDAP dn. * * @return * A username. */ function _ldapprov_get_name_from_dn($dn) { global $_ldapprov_ldap; $name_attr = $_ldapprov_ldap->getOption('user_attr') ? $_ldapprov_ldap->getOption('user_attr') : LDAP_DEFAULT_USER_ATTRIBUTE; foreach (explode(',', $dn) as $entry) { list($key, $val) = explode('=', trim($entry)); if ($key == $name_attr) { $name = $val; break; } } return $name; } ////////////////////////////////////////////////////////////////////////////// // USER REGISTRATION /** * User registration form. * * @param $form_state * A form state array. * @param $code * A secret code. Correct code means that email is already validated. * * @result * A form array. */ function ldapprov_register(&$form_state, $code = FALSE) { $form = array(); // User registration guidelines from User settings. $form['user_registration_help'] = array( '#value' => filter_xss_admin(variable_get('user_registration_help', '')), ); // Main registration form. $form = array_merge($form, _ldapprov_register_form()); $form['submit'] = array( '#type' => 'submit', '#value' => t('Request new account'), '#weight' => 10 ); // Unset several form elements if user is coming from invite. if (LDAPPROV_INVITE_ENABLED && $code) { $row = db_fetch_object(db_query("SELECT l.* FROM {ldapprov} l WHERE code = '%s'", $code)); if ($row->rid) { if ($row->status > 0) { drupal_set_message(t('The code %code has already been validated.', array('%code' => $code)), 'error'); } else { unset($form['account']['mail']); $form['account']['mail'] = array( '#type' => 'hidden', '#value' => $row->mail, ); $form['code'] = array( '#type' => 'hidden', '#value' => $code, ); } } else { drupal_set_message(t('The code %code is not valid or has expired.', array('%code' => $code)), 'error'); } } return $form; } /** * Main registration form. * Printed on the user registration and admin interface for a new account. * * @param $data * An array of the submitted data. * @param $disabled * If form elements should be disabled (when account is already created). * * @return * A form array. */ function _ldapprov_register_form($data = array(), $disabled = FALSE) { $form['account'] = array('#type' => 'fieldset', '#title' => t('Account information')); if (LDAPPROV_ALLOW_USERNAME) { $form['account']['name'] = array( '#type' => 'textfield', '#title' => t('Username'), '#description' => t('Your full name or your preferred username; only letters, numbers and spaces are allowed.'), '#size' => 30, '#maxlength' => 60, '#default_value' => $data['name'], '#required' => !$disabled ? TRUE : FALSE, '#disabled' => $disabled, '#weight' => -2, ); } if (!module_exists('profile') || !LDAPPROV_PROFILE || LDAPPROV_PROFILE_FIRST == '' || LDAPPROV_PROFILE_LAST == '') { $form['account']['first_name'] = array( '#type' => 'textfield', '#title' => t('First Name'), '#size' => 30, '#maxlength' => 100, '#default_value' => $data['first_name'], '#required' => !$disabled ? TRUE : FALSE, '#disabled' => $disabled, '#weight' => -1, ); $form['account']['last_name'] = array( '#type' => 'textfield', '#title' => t('Last Name'), '#size' => 30, '#maxlength' => 100, '#default_value' => $data['last_name'], '#required' => !$disabled ? TRUE : FALSE, '#disabled' => $disabled, '#weight' => 0, ); } $form['account']['mail'] = array( '#type' => 'textfield', '#title' => t('E-mail address'), '#description' => t('A password and instructions will be sent to this e-mail address, so make sure it is accurate.'), '#size' => 30, '#maxlength' => 100, '#default_value' => $data['mail'], '#required' => !$disabled ? TRUE : FALSE, '#disabled' => $disabled, '#weight' => 2, ); // Custom fields. $ldapprov_custom = variable_get('ldapprov_custom', array()); foreach (ldapprov_custom_elements() as $key => $val) { if ($ldapprov_custom[$key] > 0) { switch ($val['type']) { case 'textfield': $form['account']['custom_'. $key] = array( '#type' => 'textfield', '#title' => $val['title'], '#description' => $val['description'], '#size' => $val['size'], '#maxlength' => $val['maxlength'], '#default_value' => $data['custom_'. $key], '#disabled' => $disabled, '#weight' => $val['weigth'], ); break; case 'textarea': $form['account']['custom_'. $key] = array( '#type' => 'textarea', '#title' => $val['title'], '#description' => $val['description'], '#rows' => $val['rows'], '#default_value' => $data['custom_'. $key], '#disabled' => $disabled, '#weight' => $val['weigth'], ); break; } if ($ldapprov_custom[$key] == 2) { $form['account']['custom_'. $key]['#required'] = !$disabled ? TRUE : FALSE; } } } // Print writable ldap fields. if (module_exists('ldapdata')) { $server = _ldapprov_get_server(); $rwattrs = unserialize($server->ldapdata_rwattrs); if (!empty($rwattrs)) { $ldapdata_attributes = ldapdata_attributes(); $ldapdata_options = variable_get('ldapprov_ldapdata', array()); foreach ($rwattrs as $attribute) { if ($attr_info = $ldapdata_attributes[$attribute]) { // if this attribute should be shown ir registration. if ($ldapdata_options[$attribute] > 0) { array_shift($attr_info); $form['account']['ldap_'. $attribute] = _ldapdata_attribute_form($attribute, $data['ldap_'. $attribute], $attr_info); $form['account']['ldap_'. $attribute]['#disabled'] = $disabled; } // If this attribute is required. if ($ldapdata_options[$attribute] > 1) { $form['account']['ldap_'. $attribute]['#required'] = !$disabled ? TRUE : FALSE; } } } } } // Print profile fields. if (module_exists('profile') && LDAPPROV_PROFILE) { $extra = array(_ldapprov_profile($data, $disabled)); $form = array_merge($form, $extra); } // OG integration. if (module_exists('og') && LDAPPROV_OG) { $form_groups = module_invoke('og', 'user', 'register', NULL, NULL); if (is_array($form_groups['og_register']['og_register']) && is_array($data['og_register'])) { $form_groups['og_register']['og_register']['#default_value'] = $data['og_register']; $form_groups['og_register']['og_register']['#disabled'] = $disabled; } $form = array_merge($form, is_array($form_groups) ? $form_groups : array()); } return $form; } /** * User registration form validation. */ function ldapprov_register_validate($form, &$form_state) { // Main registration form validation. _ldapprov_register_validate($form_state['values']); } /** * Main registration form validation. * * @param $values * An array of the values to validate. * @param $messages * A flag if set, then the form errors will be set, otherwise errors will be counted. * * @return * If $messages is not set, then the number of errors. */ function _ldapprov_register_validate($values, $messages = TRUE) { global $_ldapprov_ldap; // When doing mass account creation, check for errors, but don't set form errors. $errors = 0; $basedn = $_ldapprov_ldap->getOption('basedn'); $name_attr = $_ldapprov_ldap->getOption('user_attr') ? $_ldapprov_ldap->getOption('user_attr') : LDAP_DEFAULT_USER_ATTRIBUTE; // When user is allowed to select a username. if (LDAPPROV_ALLOW_USERNAME) { if (preg_match('/^\s+/', $values['name'])) { $errors = ($messages) ? form_set_error('name', t('Username cannot begin with a space.')) : $errors + 1; } if (preg_match('/\s+$/', $values['name'])) { $errors = ($messages) ? form_set_error('name', t('Username cannot end with a space.')) : $errors + 1; } if (preg_match('/[^\w\s]+/', $values['name'])) { $errors = ($messages) ? form_set_error('name', t('Username should contain only letters, numbers and spaces.')) : $errors + 1; } $result = db_query("SELECT uid FROM {users} WHERE name = '%s'", $values['name']); if ($user = db_fetch_object($result)) { $errors = ($messages) ? form_set_error('name', t('The username %name is already taken. Please choose different one.', array('%name' => $values['name']))) : $errors + 1; } // When ldap users are in sync with drupal users, ldap search is not needed. if (!$_ldapprov_ldap->connect(LDAPPROV_DN, LDAPPROV_PASS)) { watchdog('ldapprov', 'User validate: user data could not be read in the LDAP directory. Could not bind as %dn.', array('%dn' => LDAPPROV_DN), WATCHDOG_ERROR); drupal_set_message(t('User validate: user data could not be read in the LDAP directory. Please contact site administrator.'), 'error'); return; } if ($ret = $_ldapprov_ldap->search($basedn, '('. $name_attr .'='. $values['name'] .')', array('mail'))) { $errors = ($messages) ? form_set_error('name', t('The username %name is already taken. Please choose different one.', array('%name' => $values['name']))) : $errors + 1; } $_ldapprov_ldap->disconnect(); } if (!valid_email_address($values['mail'])) { $errors = ($messages) ? form_set_error('mail', t('The e-mail address %mail is not valid.', array('%mail' => $values['mail']))) : $errors + 1; } $result = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $values['mail']); if ($user = db_fetch_object($result)) { $errors = ($messages) ? form_set_error('mail', t('The user with e-mail address %mail is already registered with the system. Click !request if you forgot your login information.', array('%mail' => $values['mail'], '!request' => l(t('request new password'), 'user/password')))) : $errors + 1; } // When ldap users are in sync with drupal users, ldap search is not needed. if (!$_ldapprov_ldap->connect(LDAPPROV_DN, LDAPPROV_PASS)) { watchdog('ldapprov', 'User validate: user data could not be read in the LDAP directory. Could not bind as %dn.', array('%dn' => LDAPPROV_DN), WATCHDOG_ERROR); drupal_set_message(t('User validate: user data could not be read in the LDAP directory. Please contact site administrator.'), 'error'); return; } if ($ret = $_ldapprov_ldap->search($_ldapprov_ldap->getOption('basedn'), '(mail='. $values['mail'] .')', array('mail'))) { $errors = ($messages) ? form_set_error('mail', t('The user with e-mail address %mail is already registered with the system. Click !request if you forgot your login information.', array('%mail' => $values['mail'], '!request' => l(t('request new password'), 'user/password')))) : $errors + 1; } $_ldapprov_ldap->disconnect(); /* foreach (preg_split('//', LDAPPROV_UID_FORBIDDEN_CHAR) as $c) { if (in_array($c, preg_split('//', $values['first_name']))) { $first_bad .= $c; } if (in_array($c, preg_split('//', $values['last_name']))) { $last_bad .= $c; } } */ if (!LDAPPROV_ALLOW_USERNAME) { // Username is constructed for the user. if (preg_match('/^\s+/', $values['first_name'])) { $errors = ($messages) ? form_set_error('first_name', t('First Name cannot begin with a space.')) : $errors + 1; } if (preg_match('/\s+$/', $values['first_name'])) { $errors = ($messages) ? form_set_error('first_name', t('First Name cannot end with a space.')) : $errors + 1; } if (preg_match('/[^a-zA-Z\'-\s]+/', $values['first_name'])) { $errors = ($messages) ? form_set_error('first_name', t('First Name should contain only latin letters, apostrophe, dash or space.')) : $errors + 1; } /* elseif ($first_bad) { $errors = ($messages) ? form_set_error('first_name', t('First Name should not contain %chars characters.', array('%chars' => $first_bad))) : $errors + 1; } */ if (preg_match('/^\s+/', $values['last_name'])) { $errors = ($messages) ? form_set_error('last_name', t('Last Name cannot begin with a space.')) : $errors + 1; } if (preg_match('/\s+$/', $values['last_name'])) { $errors = ($messages) ? form_set_error('last_name', t('Last Name cannot end with a space.')) : $errors + 1; } if (preg_match('/[^a-zA-Z\'-\s]+/', $values['last_name'])) { $errors = ($messages) ? form_set_error('last_name', t('Last Name should contain only latin letters, apostrophe, dash or space.')) : $errors + 1; } /* elseif ($last_bad) { $errors = ($messages) ? form_set_error('last_name', t('Last Name should not contain %chars characters.', array('%chars' => $last_bad))) : $errors + 1; } */ } else { // Custom usernames are not allowed. if (preg_match('/^\s+$/', $values['first_name'])) { $errors = ($messages) ? form_set_error('first_name', t('First Name can not contain only whitespace characters.')) : $errors + 1; } if (preg_match('/^\s+$/', $values['last_name'])) { $errors = ($messages) ? form_set_error('last_name', t('Last Name can not contain only whitespace characters.')) : $errors + 1; } } // Checking for a batch user upload. if (!$messages) { return $errors; } } /** * User registration form submission. */ function ldapprov_register_submit($form, &$form_state) { $time = time(); $values = $form_state['values']; // Prepare data to enter into the database. $data = array(); foreach ($values as $key => $value) { if (preg_match("/(^profile_|^ldap_|^og_register|^custom_)/", $key)) { $data[$key] = $value; } } // First and last names. $first_name = (!module_exists('profile') || !LDAPPROV_PROFILE || LDAPPROV_PROFILE_FIRST == '') ? $values['first_name'] : $values[LDAPPROV_PROFILE_FIRST]; $last_name = (!module_exists('profile') || !LDAPPROV_PROFILE || LDAPPROV_PROFILE_LAST == '') ? $values['last_name'] : $values[LDAPPROV_PROFILE_LAST]; // Check if registering from an invite. if (LDAPPROV_INVITE_ENABLED && isset($values['code'])) { // Registering from an invite. E-mail is valid. $result = db_query("SELECT * FROM {ldapprov} WHERE code = '%s' AND status = '0'", $values['code']); if ($row = db_fetch_object($result)) { $data_initial = unserialize($row->data); db_query("UPDATE {ldapprov} SET name = '%s', first_name = '%s', last_name = '%s', registered = %d, data = '%s', status = '1' WHERE rid = %d", $values['name'], $first_name, $last_name, $time, serialize($data), $row->rid); drupal_set_message(t('Please wait until your account is approved. You will receive login information to your e-mail account.', array('%mail' => $row->mail))); $form_state['redirect'] = 'user'; } else { drupal_set_message(t('The code %code is not valid or has expired.', array('%code' => $values['code'])), 'error'); $form_state['redirect'] = 'user/register'; } } else { // Ordinary registration. // Create a secret code. $hash = _ldapprov_hash($values['name'] . $first_name . $last_name, $time); db_query("INSERT INTO {ldapprov} (name, mail, first_name, last_name, code, registered, data, status) VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '0')", $values['name'], $values['mail'], $first_name, $last_name, $hash, $time, serialize($data)); // Mail one time login URL and instructions. $variables = array('!validate_url' => url('user/validate/'. $hash, array('absolute' => TRUE)), '!validate_uri' => url('user/validate', array('absolute' => TRUE)), '!mailto' => $values['mail'], '!first_name' => $first_name, '!last_name' => $last_name, '!code' => $hash); $params = array('variables' => $variables); $message = drupal_mail('ldapprov', 'code', $values['mail'], language_default(), $params); if ($message['result']) { watchdog('ldapprov', 'E-mail validation request mailed to %first_name %last_name at %mail.', array('%first_name' => $first_name, '%last_name' => $last_name, '%mail' => $values['mail'])); drupal_set_message(t('An e-mail has been sent to the e-mail account %s to verify that you have entered a valid e-mail address.', array('%s' => $values['mail']))); } else { watchdog('ldapprov', 'Error mailing e-mail validation request to %first_name %last_name at %mail.', array('%first_name' => $first_name, '%last_name' => $last_name, '%mail' => $values['mail']), WATCHDOG_ERROR); drupal_set_message(t('Unable to send mail. Please contact the site admin.'), 'error'); } $form_state['redirect'] = 'user/validate'; } } /** * Creats a secret hash. * * @param $string * A string to calculate hash from. * @param $seed * A seed to salt the hash. * * @return * The hash. */ function _ldapprov_hash($string, $seed) { return md5($string . $seed); } ////////////////////////////////////////////////////////////////////////////// // Secret code validateion /** * Code validation form. * * @param $form_state * A form state array. * @param $code * A secret mail validation code. * * @return * A form array if there is no code. */ function ldapprov_code(&$form_state, $code = NULL) { // The code is passed as argument when clicking a link in the validation e-mail. if ($code) { if (!_ldapprov_code_validate(array('code' => $code))) { _ldapprov_code_submit(array('code' => $code)); drupal_goto('user'); } else { drupal_goto('user/validate'); } } // The code validation form. $form = array(); $form['code'] = array('#type' => 'textfield', '#title' => t('Secret Code'), '#size' => 50, '#maxlength' => 100, '#default_value' => $code, '#required' => TRUE, ); $form['submit'] = array('#type' => 'submit', '#value' => t('Validate')); return $form; } /** * Code validation form validation. */ function ldapprov_code_validate($form, &$form_state) { // Main code validation form validation. _ldapprov_code_validate($form_state['values']); } /** * Main code validation form validation. * * @param $values * A submitted data. * * @return * TRUE or FALSE depending if validation was passed. */ function _ldapprov_code_validate($values) { $result = db_query("SELECT rid, status FROM {ldapprov} WHERE code = '%s'", $values['code']); if ($row = db_fetch_object($result)) { if ($row->status > 0) { form_set_error('code', t('The code %s has already been validated.', array('%s' => $values['code']))); return TRUE; } } else { form_set_error('code', t('The code %s is not valid.', array('%s' => $values['code']))); return TRUE; } return FALSE; } /** * Code validation form submission. */ function ldapprov_code_submit($form, &$form_state) { // Main code validation form submission. _ldapprov_code_submit($form_state['values']); $form_state['redirect'] = 'user'; } /** * Main code validation form submission. * * @param $values * A submitted data. * * @return */ function _ldapprov_code_submit($values) { $result = db_query("SELECT * FROM {ldapprov} WHERE code = '%s' AND status = '0'", $values['code']); if ($row = db_fetch_object($result)) { // Check if users should be approved. if (variable_get('user_register', 1) == 2) { // User approval is needed. $time = time(); db_query("UPDATE {ldapprov} SET status = '1' WHERE code = '%s'", $values['code']); // Mail the user managers about the new request. $variables = array('!mailto' => $row->mail, '!first_name' => $row->first_name, '!last_name' => $row->last_name, '!create_url' => url('admin/user/accounts/pending/create/'. $row->rid, array('absolute' => TRUE))); $result = db_query("SELECT DISTINCT u.uid FROM {users} u INNER JOIN {users_roles} ur ON u.uid = ur.uid INNER JOIN {permission} p ON ur.rid = p.rid WHERE p.perm LIKE '%%%s%%'", LDAPPROV_PERMISSION); while ($row2 = db_fetch_object($result)) { $account = user_load($row->uid); $params = array('account' => $account, 'variables' => $variables); $message = drupal_mail('ldapprov', 'notify', $account->mail, user_preferred_language($account), $params); if ($message['result']) { watchdog('ldapprov', 'E-mail notification message about %first_name %last_name account request mailed to %mail.', array('%first_name' => $row->first_name, '%last_name' => $row->last_name, '%mail' => $row2->mail)); } else { watchdog('ldapprov', 'Error mailing notification e-mail about %first_name %last_name account request mailed to %mail.', array('%first_name' => $row->first_name, '%last_name' => $row->last_name, '%mail' => $row2->mail), WATCHDOG_EROR); } } drupal_set_message(t('Your e-mail account %mail has been validated. Please wait until your account is approved. You will receive login information to your e-mail account.', array('%mail' => $row->mail))); } else { // User approval is not needed, account is created. // Only default role can be assigned this way. unset($values['roles']); if (is_array(unserialize($row->data))) { foreach (unserialize($row->data) as $k => $v) { $values[$k] = $v; } } $values['rid'] = $row->rid; $values['name'] = $row->name; $values['first_name'] = $row->first_name; $values['last_name'] = (!empty($row->last_name)) ? $row->last_name : $row->name; $values['mail'] = $row->mail; $account = _ldapprov_create_user($values); if (is_object($account)) { drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.')); } else { drupal_set_message(t('The new user was not created. Please contact site administrator.'), 'error'); } } } else { drupal_set_message(t('The code %code is not valid or has expired.', array('%code' => $values['code'])), 'error'); } } ////////////////////////////////////////////////////////////////////////////// // Account management /** * List account requests. * * @param $status * The tab clicked. * * @return * A HTML page. */ function ldapprov_list($status = 'pending') { $page = drupal_get_form('ldapprov_list_form', $status); // Print batch users upload form. if ($status == 'pending') { $page .= drupal_get_form('ldapprov_attach'); } return $page; } /** * List account requests form. * * @param $form_state * A form state array. * @param $tab * A tab clicked. * * @return * A form array. */ function ldapprov_list_form(&$form_state, $tab) { // Possible $status values are. // unverified(0), pending(1), rejected(2), created(3), deleted(4). $tabs = array('unverified', 'pending', 'rejected', 'created', 'deleted'); $status = array_search($tab, $tabs); // Action options. if ($status != 3) { $form['options'] = array( '#type' => 'fieldset', '#title' => t('Update options'), '#prefix' => '