array( 'title' => 'Invites', 'description' => 'Configure invites for LDAP provisioning settings.', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_invite_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'ldapprov_invite.admin.inc', ), 'invite' => array( 'title' => 'Invites', 'page callback' => 'drupal_get_form', 'page arguments' => array('ldapprov_invite_form'), 'access callback' => 'ldapprov_invite_access', ), 'invite/new' => array( 'title' => 'Invite', 'type' => MENU_DEFAULT_LOCAL_TASK, ), 'invite/manage' => array( 'title' => 'Manage invites', 'page callback' => 'ldapprov_invite_list', 'access callback' => 'ldapprov_invite_access', 'type' => MENU_LOCAL_TASK, ), 'invite/resend' => array( 'title' => 'Resend', 'page callback' => 'ldapprov_invite_resend', 'page arguments' => array(2), 'access callback' => 'ldapprov_invite_access', 'type' => MENU_CALLBACK, ), 'invite/delete' => array( 'title' => 'Delete', 'page callback' => 'ldapprov_invite_delete', 'page arguments' => array(2), 'access callback' => 'ldapprov_invite_access', 'type' => MENU_CALLBACK, ), ); } /** * Access control. */ function ldapprov_invite_access() { return LDAPPROV_INVITE_ENABLED && user_access(LDAPPROV_INVITE_PERMISSION); } /** * Implementation of hook_mail(). */ function ldapprov_invite_mail($key, &$message, $params) { $language = $message['language']; $account = isset($params['account']) ? $params['account'] : (object)array(); $variables = array_merge(user_mail_tokens($account, $language), isset($params['variables']) ? $params['variables'] : array()); $function = isset($params['type']) ? '_ldapprov_invite_'. $params['type'] .'_mail_text' : '_ldapprov_invite_mail_text'; $message['subject'] .= $function($key .'_subject', $language, $variables); $message['body'][] = $function($key .'_body', $language, $variables); } ////////////////////////////////////////////////////////////////////////////// // Mail strings /** * Returns a mail string for a variable name. * * Used by ldapprov_invite_mail() and the settings forms to retrieve strings. */ function _ldapprov_invite_mail_text($key, $language = NULL, $variables = array()) { $langcode = isset($language) ? $language->language : NULL; if ($admin_setting = variable_get('ldapprov_invite_mail_'. $key, FALSE)) { // An admin setting overrides the default string. return t($admin_setting, $variables, $langcode); } else { // No override, return default string. switch ($key) { case 'invite_subject': return t('You have been invited to the !site', $variables, $langcode); case 'invite_body': return t("Your friend, !name has invited you to join !site site. You may now register to the site at \n\n!register_uri\n\n----------\n\n!message\n\n-- !site team", $variables, $langcode); } } } ////////////////////////////////////////////////////////////////////////////// // Invites /** * Invites form. */ function ldapprov_invite_form() { $form = array(); $form['invite'] = array( '#type' => 'fieldset', ); $form['invite']['#title'] = t('Invite a contact to the site'); $form['invite']['mail'] = array( '#type' => 'textfield', '#title' => t('To'), '#required' => TRUE, ); $form['invite']['mail']['#description'] = t('Type the comma separated list of e-mails of the people you would like to invite to the site.'); $form['invite']['message'] = array( '#type' => 'textarea', '#title' => t('Your message'), '#required' => FALSE, '#description' => t('This message will be added to the e-mails sent to the people you are inviting to the site.'), ); $form['invite']['submit'] = array( '#type' => 'submit', '#value' => t('Invite'), ); return $form; } /** * Invite form validation. */ function ldapprov_invite_form_validate($form, &$form_state) { global $user; foreach (array_filter(array_map(create_function('$a', 'return trim($a);'), explode(',', $form_state['values']['mail']))) as $mail) { if (!valid_email_address($mail)) form_set_error('mail', t('The e-mail address %mail is not valid.', array('%mail' => $mail))); $result = db_query("SELECT rid FROM {ldapprov_invite} WHERE uid = %d AND mail = '%s'", $user->uid, $mail); if ($row = db_fetch_object($result)) form_set_error('mail', t('The invitation already was sent to user with e-mail address %mail.', array('%mail' => $mail))); // The user with this e-mails address already exist. if (user_load(array('mail' => $mail))) form_set_error('mail', t('The user with e-mail address %mail is already registered with the system.', array('%mail' => $mail))); $form_state['mails'][] = $mail; } } /** * Invite form submission. */ function ldapprov_invite_form_submit($form, &$form_state, $data = array()) { global $user; $values = $form_state['values']; $time = time(); foreach ($form_state['mails'] as $mail) { // Sending invite. $hash = _ldapprov_hash($mail, $time); $result = db_query("SELECT * FROM {ldapprov} WHERE mail = '%s' AND status < %d", $mail, 2); if ($row = db_fetch_object($result)) { $hash = $row->code; $rid = $row->rid; } else { $registration_data = array(); db_query("INSERT INTO {ldapprov} (mail, code, status, data) VALUES ('%s', '%s', '0', '%s')", $mail, $hash, serialize($registration_data)); $rid = db_result(db_query("SELECT rid FROM {ldapprov} WHERE code = '%s' AND mail = '%s'", $hash, $mail)); } db_query("INSERT INTO {ldapprov_invite} (uid, mail, message, data, rid, sent) VALUES (%d, '%s', '%s', '%s', %d, %d)", $user->uid, $mail, $values['message'], serialize($data), $rid, $time); // Mail one time login URL and instructions. _ldapprov_invite_mail($mail, $hash, $values['message'], $data); } } /** * Mail invite. */ function _ldapprov_invite_mail($mail, $hash, $message, $data = array()) { global $user; // Mail one time login URL and instructions. $variables = array('!name' => $user->name, '!register_uri' => url('user/register/'. $hash, array('absolute' => TRUE)), '!mail' => $mail, '!message' => $message); $params = array('account' => $user, 'variables' => array_merge($variables, isset($data['variables']) ? $data['variables'] : array()), 'type' => isset($data['type']) ? $data['type'] : NULL); $message = drupal_mail('ldapprov_invite', 'invite', $mail, user_preferred_language($user), $params, !LDAPPROV_INVITE_FROM ? $user->mail : NULL); if ($message['result']) { watchdog('ldapprov_invite', 'Invite e-mail was mailed from %name to %mail.', array('%name' => $user->name, '%mail' => $mail)); drupal_set_message(t('The invite has been mailed to e-mail address %mail.', array('%mail' => $mail))); } else { watchdog('ldapprov_invite', 'Error mailing invite e-mail from %user to %mail.', array('%name' => $user->name, '%mail' => $mail), WATCHDOG_ERROR); drupal_set_message(t('Unable to send mail. Please contact the site admin.'), 'error'); } } /** * Invites list. */ function ldapprov_invite_list() { global $user; $header = array( array('data' => t('E-mail'), 'field' => 'mail'), array('data' => t('Sent'), 'field' => 'sent', 'sort' => 'desc'), array('data' => t('To')), array('data' => t('Status')), array('data' => t('Action')), ); // Show the invites mailed out. $sql = "SELECT li.iid, li.mail, li.sent, li.data, l.rid, l.uid, l.status, u.name, u.status AS u_status FROM {ldapprov_invite} li INNER JOIN {ldapprov} l ON li.rid = l.rid LEFT JOIN {users} u ON l.uid = u.uid WHERE li.uid = %d"; $query_count = "SELECT COUNT(li.iid) FROM {ldapprov_invite} li INNER JOIN {ldapprov} l ON li.rid = l.rid WHERE li.uid = %d"; $result = pager_query($sql . tablesort_sql($header), LDAPPROV_INVITE_MAX_ENTRIES, 0, $query_count, $user->uid); $entries = _ldapprov_invite_list_entry($result); $page = theme_table($header, $entries) . theme_pager(array(), LDAPPROV_INVITE_MAX_ENTRIES, 0); return $page; } /** * Formats an invite list entry. */ function _ldapprov_invite_list_entry($result) { while ($row = db_fetch_object($result)) { if ($row->uid > 0) { if ($row->status == 4) { $status = t('Deleted'); $action = ''; } else if (!$row->u_status) { $status = t('Blocked'); $action = ''; } else { $status = t('Joined'); $action = theme('username', user_load($row->uid)); } } else { $status = t('Pending'); $action = l(t("resend"), 'invite/resend/'. $row->iid) .', '. l(t("delete"), 'invite/delete/'. $row->iid); } $data = unserialize($row->data); $entry = array( check_plain($row->mail), format_date($row->sent, LDAPPROV_DATE_FORMAT), !empty($data) ? $data['string'] : 'Site', $status, $action, ); $entries[$row->rid] = $entry; } if (!isset($entry)) { $entries[] = array(array('data' => t('No invites'), 'colspan' => 5)); } return $entries; } /** * Resend invitation. */ function ldapprov_invite_resend($iid) { global $user; $result = db_query("SELECT li.mail, li.message, li.data, l.code FROM {ldapprov_invite} li INNER JOIN {ldapprov} l ON li.rid = l.rid WHERE li.iid = %d AND li.uid = %d", $iid, $user->uid); if ($row = db_fetch_object($result)) { $data = unserialize($row->data); _ldapprov_invite_mail($row->mail, $row->code, $row->message, $data); db_query("UPDATE {ldapprov_invite} SET sent = %d WHERE iid = %d", time(), $iid); } drupal_goto('invite/manage'); } /** * Delete invitation. */ function ldapprov_invite_delete($iid) { global $user; $result = db_query("SELECT * FROM {ldapprov_invite} WHERE iid = %d AND uid = %d", $iid, $user->uid); if ($row = db_fetch_object($result)) { $result = db_result(db_query("SELECT COUNT(li.iid) FROM {ldapprov_invite} li INNER JOIN {ldapprov} l ON li.rid = l.rid WHERE li.rid = %d AND l.status = '0'", $row->rid)); if ($result == 1) db_query("DELETE FROM {ldapprov} WHERE rid = %d", $row->rid); db_query("DELETE FROM {ldapprov_invite} WHERE iid = %d", $iid); drupal_set_message(t('The invitation to %mail has been deleted.', array('%mail' => $row->mail))); } else { drupal_set_message(t('Unable to delete the invite.'), 'error'); } drupal_goto('invite/manage'); } ////////////////////////////////////////////////////////////////////////////// // ldapprov hooks /** * Implementation of hook_ldapuser(). */ function ldapprov_invite_ldapuser($account) { if (!isset($account->rid)) return; $result = db_query("SELECT * FROM {ldapprov_invite} WHERE rid = %d", $account->rid); while ($row = db_fetch_object($result)) { $data = unserialize($row->data); if (!empty($data)) { module_invoke_all('ldapinvite', $row->uid, $account, $data); } } } /** * Implementation of hook_ldapinvites(). */ function ldapprov_invite_ldapinvites($rid) { $rows = array(); $result = db_query("SELECT * FROM {ldapprov_invite} WHERE rid = %d", $rid); while ($row = db_fetch_object($result)) { $row->data = unserialize($row->data); $rows[] = $row; } return $rows; } /** * Implementation of hook_ldapdelete(). */ function ldapprov_invite_ldapdelete($rid) { db_query("DELETE FROM {ldapprov_invite} WHERE rid = %d", $rid); }