View source
<?php
define('INVITE_SESSION', 'invite_code');
define('INVITE_ADMIN_SESSION', 'invite_admin_filter');
define('INVITE_UNLIMITED', -1);
require_once drupal_get_path('module', 'invite') . '/invite_token.inc';
function invite_help($section) {
if (arg(0) == 'user' && is_numeric(arg(1)) && arg(2) == 'invites') {
return _invite_user_help($section, arg(1));
}
else {
if ($section == 'admin/help#invite') {
return _invite_module_help();
}
}
}
function _invite_user_help($section, $uid) {
switch ($section) {
case "user/{$uid}/invites":
case "user/{$uid}/invites/accepted":
$output = '<p>' . t("The invitations shown on this page have been used to join the site. Clicking on an e-mail address takes you to the user's profile page.");
break;
case "user/{$uid}/invites/pending":
$output = '<p>' . t("The invitations shown on this page haven't been accepted yet.");
break;
case "user/{$uid}/invites/expired":
$output = '<p>' . t('The invitations shown on this page have not been used to register on the site within the expiration period of @count days.', array(
'@count' => variable_get('invite_expiry', 30),
));
break;
default:
return;
}
$output .= ' ' . t('The status <em>deleted</em> means the user account has been terminated.') . '</p>';
if (!user_access('withdraw accepted invitations')) {
$output .= '<p>' . t("At any time, you may withdraw either pending or expired invitations. Accepted invitations can't be withdrawn and count permanently toward your invitation allotment.") . '</p>';
}
return $output;
}
function _invite_module_help() {
$file = drupal_get_path('module', 'invite') . '/README.txt';
if (file_exists($file)) {
return _filter_autop(check_plain(file_get_contents($file)));
}
}
function invite_perm() {
return array(
'administer invitations',
'send invitations',
'send mass invitations',
'track invitations',
'withdraw accepted invitations',
);
}
function invite_menu($may_cache) {
global $user;
$items = array();
$send_access = user_access('send invitations');
$track_access = user_access('track invitations');
$admin_access = user_access('administer invitations');
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/invite',
'title' => t('Invites'),
'callback' => 'invite_admin_overview',
'access' => $admin_access,
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'admin/user/invite/list',
'title' => t('Inviters'),
'access' => $admin_access,
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -5,
);
$items[] = array(
'path' => 'admin/user/invite/settings',
'title' => t('Settings'),
'callback' => 'drupal_get_form',
'callback arguments' => 'invite_settings',
'access' => $admin_access,
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
$items[] = array(
'path' => 'invite',
'title' => variable_get('invite_page_title', t('Invite a friend')),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'invite_form',
'page',
array(),
),
'access' => $send_access,
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'invite/accept',
'callback' => 'invite_controller',
'access' => TRUE,
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'invite/withdraw',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'invite_cancel',
),
'access' => $track_access,
'type' => MENU_CALLBACK,
);
}
else {
if (!empty($user->invite_sent) && !module_invoke('throttle', 'status')) {
invite_notify($user->uid);
}
if (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) == $user->uid) {
$items[] = array(
'path' => 'user/' . arg(1) . '/invites',
'title' => t('Invitations'),
'callback' => 'invite_user_overview',
'access' => $track_access,
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/invites/accepted',
'title' => t('Accepted'),
'callback' => 'invite_user_overview',
'callback arguments' => array(
'accepted',
),
'access' => $track_access,
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -5,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/invites/pending',
'title' => t('Pending'),
'callback' => 'invite_user_overview',
'callback arguments' => array(
'pending',
),
'access' => $track_access,
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/invites/expired',
'title' => t('Expired'),
'callback' => 'invite_user_overview',
'callback arguments' => array(
'expired',
),
'access' => $track_access,
'type' => MENU_LOCAL_TASK,
'weight' => 5,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/invites/new',
'title' => t('New invitation'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'invite_form',
'page',
array(),
),
'access' => $send_access,
'type' => MENU_LOCAL_TASK,
'weight' => 10,
);
}
else {
if (arg(2) == 'invite' && arg(3) == 'details' && arg(4)) {
if ($account = user_load(array(
'uid' => arg(4),
))) {
$items[] = array(
'path' => 'admin/user/invite/details/' . arg(4),
'title' => t('Invitees of @name', array(
'@name' => $account->name,
)),
'callback' => 'invite_admin_details',
'callback arguments' => array(
arg(4),
),
'access' => $admin_access,
'type' => MENU_LOCAL_TASK,
);
}
}
else {
if (arg(0) == 'invite' && arg(1) == 'resend' && arg(2)) {
$items[] = array(
'path' => 'invite/resend/' . arg(2),
'title' => t('Resend invitation'),
'callback' => 'invite_resend',
'callback arguments' => array(
arg(2),
),
'access' => $send_access,
'type' => MENU_CALLBACK,
);
}
}
}
}
return $items;
}
function invite_notify($uid) {
$result = db_query('SELECT invitee FROM {invite_notifications} WHERE uid = %d', $uid);
while ($row = db_fetch_object($result)) {
$account = user_load(array(
'uid' => $row->invitee,
'status' => 1,
));
if ($account) {
drupal_set_message(t('!user (@email) has joined @site-name!', array(
'!user' => theme('username', $account),
'@email' => $account->mail,
'@site-name' => variable_get('site_name', t('Drupal')),
)));
db_query("DELETE FROM {invite_notifications} WHERE uid = %d AND invitee = %d", $uid, $row->invitee);
}
}
}
function invite_controller() {
global $user;
if (!$user->uid) {
$invite = invite_load(arg(2));
if (invite_validate($invite)) {
$_SESSION[INVITE_SESSION] = $invite->reg_code;
drupal_goto('user/register');
}
}
drupal_goto();
}
function invite_form_alter($form_id, &$form) {
switch ($form_id) {
case 'user_admin_settings':
$form['registration']['user_register']['#options']['1-inviteonly'] = t('New user registration by invitation only.');
break;
case 'user_register':
$GLOBALS['conf']['cache'] = CACHE_DISABLED;
$invite = invite_load_from_session();
if (!$invite && ($code = arg(2))) {
if ($invite = invite_load($code)) {
if (invite_validate($invite)) {
$_SESSION[INVITE_SESSION] = $invite->reg_code;
}
}
}
if ($invite) {
if (isset($form['account'])) {
$field =& $form['account'];
}
else {
$field =& $form;
}
if (isset($field['mail'])) {
$field['mail']['#default_value'] = $invite->email;
}
}
else {
if (variable_get('user_register', 1) === '1-inviteonly' && !user_access('administer users')) {
drupal_set_message(t('Sorry, new user registration by invitation only.'));
drupal_goto();
}
}
break;
case 'user_login_block':
if (variable_get('user_register', 1) === '1-inviteonly') {
$new_items = array();
$new_items[] = l(t('Request new password'), 'user/password', array(
'title' => t('Request new password via e-mail.'),
));
$form['links']['#value'] = theme('item_list', $new_items);
}
break;
}
}
function invite_load($code) {
$result = db_query("SELECT * FROM {invite} WHERE reg_code = '%s' AND canceled = 0", $code);
if ($invite = db_fetch_object($result)) {
$invite->inviter = user_load(array(
'uid' => $invite->uid,
));
$invite->data = (array) unserialize($invite->data);
}
return $invite;
}
function invite_load_from_session() {
if (isset($_SESSION[INVITE_SESSION])) {
return invite_load($_SESSION[INVITE_SESSION]);
}
return FALSE;
}
function invite_validate($invite) {
if (!$invite || !$invite->inviter) {
drupal_set_message(t('This invitation has been withdrawn.'));
drupal_goto();
}
else {
if ($invite->joined != 0) {
drupal_set_message(t('This invitation has already been used. Please login now with your username and password.'));
drupal_goto('user');
}
else {
if ($invite->expiry < time()) {
drupal_set_message(t('Sorry, this invitation has expired.'));
drupal_goto();
}
else {
return TRUE;
}
}
}
}
function invite_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
$invite = invite_load_from_session();
if (!$invite) {
$code = db_result(db_query("SELECT reg_code FROM {invite} WHERE email = '%s'", $account->mail));
if ($code) {
$invite = invite_load($code);
}
}
if ($invite) {
invite_accept($invite, $account);
if ($invite->inviter->uid) {
user_save($invite->inviter, array(
'invite_sent' => TRUE,
));
}
unset($_SESSION[INVITE_SESSION]);
}
break;
case 'delete':
invite_delete($account->uid);
break;
}
}
function invite_accept($invite, $account) {
db_query("UPDATE {invite} SET email = '%s', invitee = %d, joined = %d WHERE reg_code = '%s'", $account->mail, $account->uid, time(), $invite->reg_code);
db_query("DELETE FROM {invite} WHERE (email = '%s' OR email = '%s') AND reg_code <> '%s'", $invite->email, $account->mail, $invite->reg_code);
db_query("INSERT INTO {invite_notifications} (uid, invitee) SELECT uid, %d from {invite} WHERE (email = '%s' OR email = '%s') AND canceled = 0", $account->uid, $invite->email, $account->mail);
_invite_escalate_role($account);
db_query("UPDATE {users} SET status = 1 WHERE uid = %d", $account->uid);
}
function _invite_escalate_role($account) {
$roles = array(
'default',
);
$inviter_uid = db_result(db_query("SELECT uid FROM {invite} WHERE invitee = %d", $account->uid));
if ($inviter_uid && ($inviter = user_load(array(
'uid' => $inviter_uid,
)))) {
$roles = array_merge($roles, array_intersect($inviter->roles, user_roles(0, 'send invitations')));
}
$targets = array();
foreach ($roles as $role) {
$role_no_space = str_replace(' ', '_', $role);
$target = variable_get('invite_target_role_' . $role_no_space, DRUPAL_AUTHENTICATED_RID);
if ($target != DRUPAL_AUTHENTICATED_RID) {
$targets[$target] = $target;
}
}
$edit = array(
'roles' => $targets,
);
user_module_invoke('update', $edit, $account);
foreach ($targets as $target) {
db_query("DELETE FROM {users_roles} WHERE uid = %d AND rid = %d", $account->uid, $target);
db_query("INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)", $account->uid, $target);
}
$args = array(
'invitee' => $account,
'inviter' => $inviter,
'roles' => $targets,
);
module_invoke_all('invite', 'escalate', $args);
}
function invite_delete($uid) {
$origin = db_result(db_query("SELECT uid FROM {invite} WHERE invitee = %d", $uid));
if ($origin && ($inviter = user_load(array(
'uid' => $origin,
)))) {
if (user_access('withdraw accepted invitations', $inviter)) {
db_query("DELETE FROM {invite} WHERE invitee = %d", $uid);
}
}
db_query("DELETE FROM {invite} WHERE uid = %d", $uid);
db_query("DELETE FROM {invite_notifications} WHERE uid = %d OR invitee = %d", $uid, $uid);
}
function invite_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0] = array(
'info' => t('Invite a friend'),
);
return $blocks;
}
else {
if ($op == 'view') {
switch ($delta) {
case 0:
if (user_access('send invitations')) {
$block = array(
'subject' => t('Invite a friend'),
'content' => drupal_get_form('invite_form', 'block'),
);
}
break;
}
return $block;
}
}
}
function invite_form($op = 'page', $edit = array()) {
global $user;
if (!is_array($edit)) {
$edit = (array) $edit;
}
$remaining_invites = invite_get_remaining_invites($user);
if ($remaining_invites == 0) {
if ($op == 'block') {
$form['#access'] = FALSE;
return $form;
}
else {
if (!$edit) {
drupal_set_message(t("Sorry, you've reached the maximum number of invitations."), 'error');
drupal_goto(referer_uri());
}
}
}
$form['resent'] = array(
'#type' => 'value',
'#value' => $edit ? $edit['resent'] + 1 : 0,
);
$form['reg_code'] = array(
'#type' => 'value',
'#value' => $edit ? $edit['reg_code'] : NULL,
);
if ($remaining_invites != INVITE_UNLIMITED) {
$form['remaining_invites'] = array(
'#type' => 'value',
'#value' => $remaining_invites,
);
}
switch ($op) {
case 'page':
default:
$form += invite_page_form($remaining_invites, $edit);
break;
case 'block':
$form += invite_block_form($remaining_invites);
break;
}
return $form;
}
function invite_get_remaining_invites($account) {
if ($account->uid == 1) {
return INVITE_UNLIMITED;
}
$data = unserialize($account->data);
if (isset($data['invites'])) {
$remaining = $data['invites'];
}
else {
$remaining = invite_get_role_limit($account);
if ($remaining > 0) {
$sent = db_result(db_query("SELECT COUNT(*) FROM {invite} WHERE uid = %d", $account->uid));
$remaining = max($remaining - $sent, 0);
if ($sent > 0) {
user_save($account, array(
'invites' => $remaining,
));
}
}
}
return $remaining;
}
function invite_get_role_limit($account) {
if (!isset($account->roles)) {
$account = user_load(array(
'uid' => $account->uid,
));
}
$role_limit = 0;
foreach (user_roles(0, 'send invitations') as $role) {
$role_no_space = str_replace(' ', '_', $role);
if (in_array($role, $account->roles)) {
$role_max = variable_get('invite_maxnum_' . $role_no_space, INVITE_UNLIMITED);
if ($role_max == INVITE_UNLIMITED) {
return INVITE_UNLIMITED;
}
$role_limit = max($role_max, $role_limit);
}
}
return $role_limit;
}
function invite_page_form($remaining_invites, $edit = array()) {
global $user;
if ($remaining_invites != INVITE_UNLIMITED) {
$form['remaining_invites_markup']['#value'] = format_plural($remaining_invites, 'You have 1 invite remaining.', 'You have @count invites remaining.');
}
if ($user->uid && variable_get('invite_use_users_email', 0)) {
$from = $user->mail;
}
else {
$from = variable_get('site_mail', ini_get('sendmail_from'));
}
if (module_exists('pmail')) {
$from = personalize_email($from);
}
$form['from'] = array(
'#type' => 'item',
'#title' => t('From'),
'#value' => check_plain($from),
);
if (!$edit) {
$failed_emails = '';
$allow_multiple = user_access('send mass invitations');
if (isset($_SESSION['invite_failed_emails'])) {
$failed_emails = implode("\n", (array) unserialize($_SESSION['invite_failed_emails']));
unset($_SESSION['invite_failed_emails']);
}
$form['email'] = array(
'#title' => t('To'),
'#default_value' => $failed_emails,
'#description' => format_plural($allow_multiple ? 99 : 1, 'Type the e-mail address of the person you would like to invite.', 'Type the e-mail addresses of the persons you would like to invite. Addresses should be separated by newlines or commas.'),
'#required' => TRUE,
);
if ($allow_multiple) {
$form['email']['#type'] = 'textarea';
$form['email']['#rows'] = 3;
}
else {
$form['email']['#type'] = 'textfield';
$form['email']['#maxlength'] = 64;
}
if ($failed_emails) {
$form['email']['#attributes']['class'] = 'error';
}
}
else {
$allow_multiple = FALSE;
$form['email_markup'] = array(
'#type' => 'item',
'#title' => t('To'),
'#value' => check_plain($edit['email']),
);
$form['email'] = array(
'#type' => 'value',
'#value' => $edit['email'],
);
}
if ($edit && !empty($edit['data']['subject'])) {
$subject = $edit['data']['subject'];
}
else {
$subject = invite_get_subject();
}
$prefix = t('Re:');
if ($edit && drupal_substr($subject, 0, strlen($prefix)) != $prefix) {
$subject = $prefix . ' ' . $subject;
}
if (variable_get('invite_subject_editable', FALSE)) {
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#default_value' => $subject,
'#maxlength' => 64,
'#description' => t('Type the subject of the invitation e-mail.'),
'#required' => TRUE,
);
}
else {
$form['subject'] = array(
'#type' => 'item',
'#title' => t('Subject'),
'#value' => check_plain($subject),
);
}
$form['body'] = array(
'#type' => 'item',
'#title' => t('Message'),
);
$form['message'] = array(
'#type' => 'textarea',
'#default_value' => $edit && !empty($edit['data']['message']) ? $edit['data']['message'] : '',
'#description' => format_plural($allow_multiple ? 1 : 99, 'This message will be added to the mail sent to the person you are inviting.', 'This message will be added to the mail sent to the persons you are inviting.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send invite'),
);
return $form;
}
function invite_block_form($remaining_invites) {
global $user;
$form['#action'] = url('invite');
$form['invite'] = array(
'#value' => t('Recommend @site-name to:', array(
'@site-name' => variable_get('site_name', t('Drupal')),
)),
);
$description = '';
if ($remaining_invites != INVITE_UNLIMITED) {
$description = format_plural($remaining_invites, '1 invite remaining', '@count invites remaining');
}
$form['email'] = array(
'#type' => 'textfield',
'#size' => 20,
'#maxlength' => 64,
'#description' => $description,
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send invite'),
);
$form['link'] = array(
'#prefix' => '<div><small>',
'#value' => l(t('View your invites'), "user/{$user->uid}/invites"),
'#suffix' => '</small></div>',
'#access' => user_access('track invitations') && $user->uid,
);
return $form;
}
function theme_invite_form($form) {
$output = '';
$op = $form['#parameters'][1];
if ($op == 'page') {
$output .= drupal_render($form['remaining_invites_markup']);
$output .= drupal_render($form['remaining_invites']);
$output .= drupal_render($form['from']);
if (isset($form['email_markup'])) {
$output .= drupal_render($form['email_markup']);
}
$output .= drupal_render($form['email']);
$output .= drupal_render($form['subject']);
$output .= drupal_render($form['body']);
$output .= '<div class="invite-message"><div class="opening">';
$message_form = "</p></div>\n" . drupal_render($form['message']) . "\n" . '<div class="closing"><p>';
$body = _filter_autop(t(_invite_get_mail_template()));
$types = _invite_token_types(array(
'data' => array(
'message' => $message_form,
),
));
$output .= token_replace_multiple($body, $types);
$output .= "</div></div>\n";
}
$output .= drupal_render($form);
return $output;
}
function invite_form_validate($form_id, &$edit) {
global $user;
$emails = _invite_get_emails($edit['email']);
if (!$edit['resent']) {
if (count($emails) > 0) {
$failed_emails = _invite_validate_emails("SELECT mail AS email FROM {users} WHERE mail IN (%s)", $emails);
if (count($failed_emails)) {
$error = format_plural(count($failed_emails), 'The following recipient is already a member:', 'The following recipients are already members:') . '<br />';
foreach ($failed_emails as $key => $email) {
$account = user_load(array(
'mail' => $email,
));
$failed_emails[$key] = theme('username', $account) . ' (' . check_plain($email) . ')';
}
$error .= implode(', ', $failed_emails);
drupal_set_message($error, 'error');
}
}
if (!empty($emails)) {
$failed_emails = _invite_validate_emails("SELECT email FROM {invite} WHERE email IN (%s) AND uid = %d AND canceled = 0", $emails, $user->uid);
if (count($failed_emails)) {
$error = format_plural(count($failed_emails), 'You did already invite the following recipient:', 'You did already invite the following recipients:') . '<br />';
$error .= implode(', ', array_map('check_plain', $failed_emails));
drupal_set_message($error, 'error');
}
}
if (count($emails) == 0) {
form_set_error('email');
return;
}
if (isset($edit['remaining_invites']) && count($emails) > $edit['remaining_invites']) {
form_set_error('email', format_plural($edit['remaining_invites'], 'You have only 1 invite left.', 'You have only @count invites left.'));
return;
}
if (!user_access('send mass invitations') && count($emails) > 1) {
form_set_error('email', t('You cannot send more than one invitation.'));
return;
}
}
form_set_value(array(
'#parents' => array(
'valid_emails',
),
), $emails);
}
function _invite_get_emails($string) {
$valid_emails = $failed_emails = array();
$user = '[a-zA-Z0-9_\\-\\.\\+\\^!#\\$%&*+\\/\\=\\?\\`\\|\\{\\}~\']+';
$domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.?)+';
$ipv4 = '[0-9]{1,3}(\\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(\\:[0-9a-fA-F]{1,4}){7}';
$rx = "/({$user}@({$domain}|(\\[({$ipv4}|{$ipv6})\\])))>?\$/";
$emails = array_unique(split("[,\n\r]", $string));
foreach ($emails as $email) {
$email = preg_replace('/^.*<(.*)>$/', '${1}', trim($email));
if ($email) {
if (preg_match($rx, $email, $match)) {
$valid_emails[] = $match[1];
}
else {
$failed_emails[] = $email;
}
}
}
if (count($failed_emails)) {
$_SESSION['invite_failed_emails'] = serialize($failed_emails);
}
return $valid_emails;
}
function _invite_validate_emails($sql, &$emails) {
$failed_emails = array();
$args = func_get_args();
array_shift($args);
array_shift($args);
array_unshift($args, "'" . implode("','", array_map('db_escape_string', $emails)) . "'");
$result = db_query(vsprintf($sql, $args));
while ($row = db_fetch_object($result)) {
$failed_emails[] = $row->email;
}
$emails = array_diff($emails, $failed_emails);
return $failed_emails;
}
function invite_form_submit($form_id, $edit) {
global $user;
$failed_emails = array();
$num_failed = $num_succeeded = 0;
if (isset($_SESSION['invite_failed_emails'])) {
$failed_emails = (array) unserialize($_SESSION['invite_failed_emails']);
$num_failed = count($failed_emails);
}
$subject = isset($edit['subject']) ? trim($edit['subject']) : invite_get_subject();
$message = isset($edit['message']) ? trim($edit['message']) : NULL;
foreach ($edit['valid_emails'] as $email) {
$code = $edit['reg_code'] ? $edit['reg_code'] : invite_generate_code();
$invite = _invite_substitutions(array(
'email' => $email,
'code' => $code,
'resent' => $edit['resent'],
'data' => array(
'subject' => $subject,
'message' => $message,
),
));
$body = token_replace_multiple(t(_invite_get_mail_template()), _invite_token_types($invite));
if (invite_send_invite($email, $subject, $body)) {
invite_save($invite);
if (!$edit['resent']) {
$args = array(
'inviter' => $invite->inviter,
'email' => $invite->email,
'code' => $invite->code,
);
module_invoke_all('invite', 'invite', $args);
}
$num_succeeded++;
}
else {
$failed_emails[] = $email;
}
}
if ($failed_emails) {
$_SESSION['invite_failed_emails'] = serialize($failed_emails);
}
if ($num_succeeded) {
if (isset($edit['remaining_invites'])) {
user_save($user, array(
'invites' => $edit['remaining_invites'] - $num_succeeded,
));
}
$message = format_plural($num_succeeded, 'Your invitation has been successfully sent. You will be notified when the invitee joins the site.', '@count invitations have been successfully sent. You will be notified when any invitee joins the site.');
drupal_set_message($message);
}
if ($num_failed) {
$message = format_plural($num_failed, 'The entered e-mail address is invalid. Please correct it.', '@count entered e-mail addresses are invalid. Please correct them.');
drupal_set_message($message, 'error');
}
else {
if (user_access('track invitations') && $user->uid) {
return "user/{$user->uid}/invites/pending";
}
}
return 'invite';
}
function invite_get_subject($substitutions = array()) {
$subject = t(variable_get('invite_subject', t('[inviter-raw] has sent you an invite!')));
return token_replace_multiple($subject, _invite_token_types($substitutions));
}
function invite_generate_code() {
do {
$reg_code = user_password(8);
$result = db_query("SELECT COUNT(*) FROM {invite} WHERE reg_code = '%s'", $reg_code);
} while (db_result($result));
return $reg_code;
}
function invite_send_invite($recipient, $subject, $body) {
global $user;
$headers = array();
$from_site = variable_get('site_mail', ini_get('sendmail_from'));
if ($from_site) {
$headers['Sender'] = $headers['Return-Path'] = $headers['Errors-To'] = $from_site;
}
$from = variable_get('invite_manual_from', NULL);
$reply_to = variable_get('invite_manual_reply_to', NULL);
if (!$from) {
if ($user->uid && variable_get('invite_use_users_email', 0)) {
$from = $user->mail;
}
else {
if ($from_site) {
$from = $from_site;
}
}
}
if (!$reply_to) {
if ($user->uid && variable_get('invite_use_users_email_replyto', 0)) {
$reply_to = $user->mail;
}
else {
if ($from_site) {
$reply_to = $from_site;
}
}
}
if ($reply_to) {
$headers['Reply-To'] = $reply_to;
}
if (!($success = drupal_mail('invite-mail', $recipient, $subject, wordwrap($body, 72), $from, $headers))) {
static $error_shown = FALSE;
if (!$error_shown) {
drupal_set_message(t('Problems occurred while sending the invitation(s). Please contact the site administrator.'), 'error');
$error_shown = TRUE;
}
watchdog('invite', t('Failed sending invitation. To: @email From: @from', array(
'@email' => '<' . $recipient . '>',
'@from' => '<' . $from . '>',
)));
}
return $success;
}
function invite_save($edit) {
$edit = (array) $edit;
$data = serialize($edit['data']);
$now = time();
$expiry = $now + variable_get('invite_expiry', 30) * 60 * 60 * 24;
if ($edit['resent']) {
$result = db_query("UPDATE {invite} SET expiry = %d, resent = %d, data = '%s' WHERE reg_code = '%s' AND uid = %d", $expiry, $edit['resent'], $data, $edit['code'], $edit['inviter']->uid);
}
else {
$result = db_query("INSERT INTO {invite} (reg_code, email, uid, created, expiry, data) VALUES ('%s', '%s', %d, %d, %d, '%s')", $edit['code'], $edit['email'], $edit['inviter']->uid, $now, $expiry, $data);
}
return $result;
}
function invite_cancel($origin, $code) {
global $user;
$invite = invite_load($code);
if ($invite->inviter->uid == $user->uid) {
if (!$invite->joined || user_access('withdraw accepted invitations')) {
$form['#redirect'] = "user/{$user->uid}/invites/{$origin}";
$form['invite'] = array(
'#type' => 'value',
'#value' => $invite,
);
$description = !$invite->joined && $invite->expiry > time() ? t("The invitee won't be able to register any more using this invitation.") : '';
return confirm_form($form, t('Are you sure you want to withdraw the invitation to %email?', array(
'%email' => $invite->email,
)), "user/{$user->uid}/invites/{$origin}", $description . ' ' . t('This action cannot be undone.'), t('Withdraw'), t('Cancel'));
}
else {
drupal_set_message(t('Invitations to registered users cannot be withdrawn.'), 'error');
}
}
else {
watchdog('invite', t('Detected malicious attempt to delete an invitation.'), WATCHDOG_WARNING, l(t('view'), 'user/' . $user->uid));
drupal_access_denied();
}
drupal_goto("user/{$user->uid}/invites/{$origin}");
}
function invite_cancel_submit($form_id, $form_values) {
$invite = $form_values['invite'];
db_query("UPDATE {invite} SET canceled = 1 WHERE reg_code = '%s'", $invite->reg_code);
drupal_set_message(t('Invitation to %email has been withdrawn.', array(
'%email' => $invite->email,
)));
$args = array(
'inviter' => $invite->inviter,
'email' => $invite->email,
'code' => $invite->reg_code,
);
module_invoke_all('invite', 'cancel', $args);
}
function invite_resend($code) {
global $user;
$invite = invite_load($code);
if ($invite->uid == $user->uid && $invite->expiry < time()) {
return drupal_get_form('invite_form', 'page', $invite);
}
else {
drupal_access_denied();
}
}
function invite_count($uid, $op) {
switch ($op) {
case 'accepted':
return db_result(db_query("SELECT COUNT(*) FROM {invite} WHERE uid = %d AND joined <> 0", $uid));
case 'pending':
return db_result(db_query("SELECT COUNT(*) FROM {invite} WHERE uid = %d AND joined = 0 AND expiry >= %d", $uid, time()));
case 'expired':
return db_result(db_query("SELECT COUNT(*) FROM {invite} WHERE uid = %d AND joined = 0 AND expiry < %d", $uid, time()));
}
}
function invite_admin_overview() {
require_once drupal_get_path('module', 'invite') . '/invite_admin.inc';
return _invite_admin_overview();
}
function invite_admin_details($uid) {
require_once drupal_get_path('module', 'invite') . '/invite_admin.inc';
return _invite_admin_details($uid);
}
function invite_settings() {
require_once drupal_get_path('module', 'invite') . '/invite_admin.inc';
return _invite_settings();
}
function invite_user_overview($page = 'accepted') {
require_once drupal_get_path('module', 'invite') . '/invite_admin.inc';
return _invite_user_overview($page);
}
function invite_disable() {
if (variable_get('user_register', 1) === '1-inviteonly') {
variable_set('user_register', 1);
drupal_set_message(t('User registration option reset to %no_approval.', array(
'%no_approval' => t('Visitors can create accounts and no administrator approval is required.'),
)));
}
}
function _invite_get_mail_template() {
$template = t("Your friend, [inviter-raw], has invited you to join [site-name] at [site-url].\n\nTo become a member of [site-name], click the link below or paste it into the address bar of your browser.\n\n[join-link]\n\n----------\n[invite-message-raw]");
return variable_get('invite_default_mail_template', $template);
}
function _invite_token_types($args = array()) {
global $user;
if (!is_array($args)) {
$args = (array) $args;
}
$invite = _invite_substitutions($args);
return array(
'user' => $user,
'profile' => $user,
'invite' => $invite,
);
}
function _invite_substitutions($args = array()) {
global $user;
$defaults = array(
'inviter' => $user,
'email' => '--recipient-email--',
'code' => '--invite-code--',
'resent' => 0,
'data' => array(
'subject' => NULL,
'message' => NULL,
),
);
return (object) array_merge($defaults, $args);
}