--- contact.module 2007-06-05 01:18:05.000000000 -0600 +++ contact.new.module 2008-07-28 23:34:06.000000000 -0600 @@ -35,7 +35,9 @@ * Implementation of hook_perm */ function contact_perm() { - return array('access site-wide contact form'); + return array('access site-wide contact form', 'access personal contact forms'); } /** * Implementation of hook_menu(). @@ -101,7 +103,9 @@ 'title' => t('Contact'), 'callback' => 'contact_user_page', 'type' => MENU_LOCAL_TASK, - 'access' => $user->uid, + 'access' => user_access('access personal contact forms'), 'weight' => 2, ); } @@ -303,7 +307,9 @@ global $user; if ($account = user_load(array('uid' => arg(1)))) { - if (!valid_email_address($user->mail)) { + if ($user->uid && !valid_email_address($user->mail)) { $output = t('You need to provide a valid e-mail address to contact other users. Please update your user information and try again.', array('@url' => url("user/$user->uid/edit"))); } else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) { @@ -323,11 +329,39 @@ function contact_mail_user($recipient) { global $user; - $form['#token'] = $user->name . $user->mail; - $form['from'] = array('#type' => 'item', - '#title' => t('From'), - '#value' => check_plain($user->name) .' <'. check_plain($user->mail) .'>', - ); + + if ($user->uid) { + // User is logged in so we'll use the account e-mail address. + $form['#token'] = $user->name . $user->mail; + $form['from'] = array('#type' => 'item', + '#title' => t('From'), + '#value' => check_plain($user->name) .' <'. check_plain($user->mail) .'>', + ); + } + else + { + // User is not logged in so we must ask for e-mail address and validate it. + $form['#token'] = $recipient->name . $recipient->mail; + $form['name'] = array('#type' => 'textfield', + '#title' => t('Your name'), + '#maxlength' => 255, + '#required' => TRUE, + ); + $form['mail'] = array('#type' => 'textfield', + '#title' => t('Your e-mail address'), + '#maxlength' => 255, + '#required' => TRUE, + '#validate' => array('contact_email_validate' => array()), + ); + } + + $form['to'] = array('#type' => 'item', '#title' => t('To'), '#value' => check_plain($recipient->name), @@ -342,15 +376,37 @@ '#rows' => 15, '#required' => TRUE, ); - $form['copy'] = array('#type' => 'checkbox', - '#title' => t('Send yourself a copy.'), - ); + // We do not allow anonymous users to send themselves a copy + // because it can be abused to spam people. + if ($user->uid) { + $form['copy'] = array('#type' => 'checkbox', + '#title' => t('Send yourself a copy.'), + ); + } + $form['submit'] = array('#type' => 'submit', '#value' => t('Send e-mail'), ); return $form; } +/** + * Validate the personal contact page form submission. + */ +function contact_email_validate($form) { + $address = $form['#value']; + if (!valid_email_address($address)) { + form_error($form, t('%address is an invalid e-mail address.', array('%address' => $address))); + } +} + + /** * Process the personal contact page form submission. */ @@ -360,7 +416,16 @@ $account = user_load(array('uid' => arg(1), 'status' => 1)); // Compose the body: $message[] = "$account->name,"; - $message[] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", NULL, NULL, TRUE), '!form-url' => url($_GET['q'], NULL, NULL, TRUE), '!site' => variable_get('site_name', 'Drupal'))); + + if ($user->uid) { + $message[] = t("@name (@name-url) has sent you a message via your contact form (!form-url) at !site.", array('@name' => $user->name, '@name-url' => url("user/$user->uid", NULL, NULL, TRUE), '!form-url' => url($_GET['q'], NULL, NULL, TRUE), '!site' => variable_get('site_name', 'Drupal'))); + } + else { + $message[] = t("@name (@name-url) has sent you a message via your contact form (!form-url) at !site.", array('@name' => $form_values['name'], '@name-url' => $form_values['mail'], '!form-url' => url($_GET['q'], NULL, NULL, TRUE), '!site' => variable_get('site_name', 'Drupal'))); + } + $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", NULL, NULL, TRUE))); $message[] = t('Message:'); $message[] = $form_values['message']; @@ -372,7 +437,14 @@ // Prepare all fields: $to = $account->mail; - $from = $user->mail; + if ($user->uid) { + $from = $user->mail; + } + else { + $from = $form_values['mail']; + } // Format the subject: $subject = '['. variable_get('site_name', 'Drupal') .'] '. $form_values['subject']; @@ -390,7 +462,14 @@ // Log the operation: flood_register_event('contact'); - watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name))); + if ($user->uid) { + watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name))); + } + else { + watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => $form_values['name'] . ', ' . $form_values['mail'] . ',', '%name-to' => $account->name))); + } // Set a status message: drupal_set_message(t('The message has been sent.')); @@ -509,8 +588,10 @@ $from = $form_values['mail']; // Compose the body: - $message[] = t("!name sent a message using the contact form at !form.", array('!name' => $form_values['name'], '!form' => url($_GET['q'], NULL, NULL, TRUE))); + $message[] = t("@name sent a message using the contact form at !form.", array('@name' => $form_values['name'], '!form' => url($_GET['q'], NULL, NULL, TRUE))); $message[] = $form_values['message']; // Tidy up the body: foreach ($message as $key => $value) {