Index: modules/contact/contact.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.module,v
retrieving revision 1.108
diff -u -p -r1.108 contact.module
--- modules/contact/contact.module	24 Jul 2008 16:25:17 -0000	1.108
+++ modules/contact/contact.module	14 Sep 2008 23:17:14 -0000
@@ -38,6 +38,7 @@ function contact_help($path, $arg) {
 function contact_perm() {
   return array(
     'access site-wide contact form' => t('Send feedback to administrators via e-mail using the site-wide contact form.'),
+    'access personal contact form' => t('Send feedback to users via e-mail using their personal contact form.'),
     'administer site-wide contact form' => t('Configure site-wide contact form administration settings.'),
   );
 }
@@ -113,12 +114,7 @@ function _contact_user_tab_access($accou
   if (!isset($account->contact)) {
     $account->contact = FALSE;
   }
-  return
-    $account && $user->uid &&
-    (
-      ($user->uid != $account->uid && $account->contact) ||
-      user_access('administer users')
-    );
+  return ($user->uid != $account->uid && $account->contact && user_access('access personal contact form')) || user_access('administer users');
 }
 
 /**
@@ -180,7 +176,7 @@ function contact_mail($key, &$message, $
       $account = $params['account'];
       $message['subject'] .= '[' . variable_get('site_name', 'Drupal') . '] ' . $params['subject'];
       $message['body'][] = "$account->name,";
-      $message['body'][] = 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", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
+      $message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => $user->uid ? url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)) : $user->mail, '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
       $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), $language->language);
       $message['body'][] = t('Message:', NULL, $language->language);
       $message['body'][] = $params['message'];
Index: modules/contact/contact.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.pages.inc,v
retrieving revision 1.11
diff -u -p -r1.11 contact.pages.inc
--- modules/contact/contact.pages.inc	16 Jul 2008 21:59:26 -0000	1.11
+++ modules/contact/contact.pages.inc	14 Sep 2008 23:17:15 -0000
@@ -23,6 +23,9 @@ function contact_site_page() {
   return $output;
 }
 
+/**
+ * Provides the form definition for the site-wide contact page.
+ */
 function contact_mail_page() {
   global $user;
 
@@ -157,10 +160,7 @@ function contact_mail_page_submit($form,
 function contact_user_page($account) {
   global $user;
 
-  if (!valid_email_address($user->mail)) {
-    $output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array('@url' => url("user/$user->uid/edit", array('query' => 'destination=' . drupal_get_destination()))));
-  }
-  else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) {
+  if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) {
     $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
   }
   else {
@@ -171,59 +171,109 @@ function contact_user_page($account) {
   return $output;
 }
 
+/**
+ * Provides the form definition for the user contact page.
+ */
 function contact_mail_user(&$form_state, $recipient) {
   global $user;
-  $form['#token'] = $user->name . $user->mail;
-  $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
-  $form['from'] = array('#type' => 'item',
-    '#title' => t('From'),
-    '#markup' => check_plain($user->name) . ' &lt;' . check_plain($user->mail) . '&gt;',
+  $form['#token'] = $user->uid ? $user->name . $user->mail : '';
+  $form['recipient'] = array(
+    '#type' => 'value',
+    '#value' => $recipient
+  );
+  $form['from'] = array('#type' => 'textfield',
+    '#title' => t('Your name'),
+    '#maxlength' => 255,
+    '#default_value' => $user->uid ? $user->name : '',
+    '#required' => TRUE,
+  );
+  $form['mail'] = array('#type' => 'textfield',
+    '#title' => t('Your e-mail address'),
+    '#maxlength' => 255,
+    '#default_value' => $user->uid ? $user->mail : '',
+    '#required' => TRUE,
   );
-  $form['to'] = array('#type' => 'item',
+  $form['to'] = array(
+    '#type' => 'item',
     '#title' => t('To'),
     '#markup' => check_plain($recipient->name),
   );
-  $form['subject'] = array('#type' => 'textfield',
+  $form['subject'] = array(
+    '#type' => 'textfield',
     '#title' => t('Subject'),
     '#maxlength' => 50,
     '#required' => TRUE,
   );
-  $form['message'] = array('#type' => 'textarea',
+  $form['message'] = array(
+    '#type' => 'textarea',
     '#title' => t('Message'),
     '#rows' => 15,
     '#required' => TRUE,
   );
-  $form['copy'] = array('#type' => 'checkbox',
-    '#title' => t('Send yourself a copy.'),
-  );
-  $form['submit'] = array('#type' => 'submit',
+  if ($user->uid) {
+    $form['copy'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Send yourself a copy.'),
+    );
+  }
+  else {
+    drupal_add_js(drupal_get_path('module', 'contact') . '/contact.js');
+    $form['copy'] = array(
+      '#type' => 'value',
+      '#value' => FALSE,
+    );
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
     '#value' => t('Send e-mail'),
   );
   return $form;
 }
 
 /**
+ * Validate the user contact page form submission.
+ */
+function contact_mail_user_validate($form, &$form_state) {
+  global $user;
+  if (!valid_email_address($form_state['values']['mail'])) {
+    form_set_error('mail', t('You must enter a valid e-mail address.'));
+  }
+  if (!$user->uid) {
+    foreach (array('from' => 'name', 'mail' => 'mail') as $form_field => $cookie_field) {
+      // Set cookie for 365 days.
+      if (isset($form_state['values'][$form_field])) {
+        setcookie('comment_info_' . $cookie_field, $form_state['values'][$form_field], $_SERVER['REQUEST_TIME'] + 31536000, '/');
+      }
+    }
+  }
+}
+
+/**
  * Process the personal contact page form submission.
  */
 function contact_mail_user_submit($form, &$form_state) {
   global $user, $language;
-
-  $account = $form_state['values']['recipient'];
+  
+  $values = $form_state['values'];
 
   // Send from the current user to the requested user.
+  $account = $values['recipient'];
   $to = $account->mail;
-  $from = $user->mail;
+  $from = $values['mail'];
 
   // Save both users and all form values for email composition.
-  $values = $form_state['values'];
   $values['account'] = $account;
+  if (!$user->uid) {
+    $user->mail = $form_state['values']['mail'];
+    $user->name = $form_state['values']['from'];
+  }
   $values['user'] = $user;
 
   // Send the e-mail in the requested user language.
   drupal_mail('contact', 'user_mail', $to, user_preferred_language($account), $values, $from);
 
   // Send a copy if requested, using current page language.
-  if ($form_state['values']['copy']) {
+  if ($values['copy']) {
     drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
   }
 
@@ -232,5 +282,5 @@ function contact_mail_user_submit($form,
   drupal_set_message(t('The message has been sent.'));
 
   // Back to the requested users profile page.
-  $form_state['redirect'] = "user/$account->uid";
+  $form_state['redirect'] = user_access('access user profiles') ? "user/$account->uid" : '';
 }
Index: modules/contact/contact.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.test,v
retrieving revision 1.8
diff -u -p -r1.8 contact.test
--- modules/contact/contact.test	2 Aug 2008 05:16:47 -0000	1.8
+++ modules/contact/contact.test	14 Sep 2008 23:17:15 -0000
@@ -60,7 +60,7 @@ class ContactSitewideTestCase extends Dr
     $recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
     $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
     $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
-    
+
     // Test update contact form category
     $categories = $this->getCategories();
     $category_id = $this->updateCategory($categories, $category = $this->randomName(16), $recipients_str = implode(',', array($recipients[0], $recipients[1])), $reply = $this->randomName(30), FALSE);
@@ -70,7 +70,7 @@ class ContactSitewideTestCase extends Dr
     $this->assertEqual($category_array['reply'], $reply);
     $this->assertFalse($category_array['selected']);
     $this->assertRaw(t('Category %category has been updated.', array('%category' => $category)), t('Category successfully updated.'));
-		
+
     $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
     $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
 
@@ -310,3 +310,132 @@ class ContactPersonalTestCase extends Dr
     $this->assertResponse(403, t('Access to personal contact form denied.'));
   }
 }
+
+/**
+ * Test the personal contact form for anonymous users.
+ */
+class ContactPersonalAnonymousTestCase extends DrupalWebTestCase {
+
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Anonmyous personal contact form'),
+      'description' => t('Tests personal contact form functionality for anonymous users.'),
+      'group' => t('Contact'),
+    );
+  }
+
+  /**
+   * Implementation of setUp().
+   */
+  function setUp() {
+    parent::setUp('contact');
+  }
+
+  /**
+   * Test configuration options and site-wide contact form.
+   */
+  function testPersonalAnonymousContact() {
+    // Create and login administative user. Also create user that will have a contact form.
+    $admin_user = $this->drupalCreateUser(array('administer site-wide contact form', 'administer permissions'));
+    $web_user = $this->drupalCreateUser(array());
+    $this->drupalLogin($admin_user);
+
+    // Set settings.
+    $edit = array();
+    $edit['contact_form_information'] = $this->randomName(100);
+    $edit['contact_hourly_threshold'] = 3;
+    $edit['contact_default_status'] = TRUE;
+    $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
+    $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
+
+    // Check to see that anonymous user cannot see contact page without permission.
+    $this->setPermission('anonymous user', array('access personal contact form' => FALSE));
+    $this->drupalLogout();
+
+    $this->drupalGet('user/' . $web_user->uid . '/contact');
+    $this->assertResponse(403, t('Access to personal contact form denied.'));
+
+    // Give anonymous user permission and see that page is viewable.
+    $this->drupalLogin($admin_user);
+    $this->setPermission('anonymous user', array('access personal contact form' => TRUE));
+    $this->drupalLogout();
+
+    $this->drupalGet('user/' . $web_user->uid . '/contact');
+    $this->assertResponse(200, t('Access granted to anonymous user with permission.'));
+
+    $valid_email = 'simpletest@example.com';
+
+    // Submit contact form with invalid values.
+    $this->submitPersonalContact($web_user->uid, '', $valid_email, $this->randomName(16), $this->randomName(64));
+    $this->assertText(t('Your name field is required.'), t('Name required.'));
+
+    $this->submitPersonalContact($web_user->uid, $this->randomName(16), '', $this->randomName(16), $this->randomName(64));
+    $this->assertText(t('Your e-mail address field is required.'), t('E-mail required.'));
+
+    // Test invalid recipients.
+    $invalid_recipients = array('invalid', 'invalid@', /*'invalid@site', 'invalid@site.',*/ '@site.', '@site.com');
+    foreach ($invalid_recipients as $invalid_recipient) {
+      $this->submitPersonalContact($web_user->uid, $this->randomName(16), $invalid_recipient, $this->randomName(16), $this->randomName(64));
+      $this->assertText(t('You must enter a valid e-mail address.'), t('Valid e-mail required.'));
+    }
+
+    $this->submitPersonalContact($web_user->uid, $this->randomName(16), $valid_email, '', $this->randomName(64));
+    $this->assertText(t('Subject field is required.'), t('Subject required.'));
+
+    $this->submitPersonalContact($web_user->uid, $this->randomName(16), $valid_email, $this->randomName(16), '');
+    $this->assertText(t('Message field is required.'), t('Message required.'));
+
+    // Submit contact form with correct values and check flood interval.
+    for ($i = 0; $i < $edit['contact_hourly_threshold']; $i++) {
+      $this->submitPersonalContact($web_user->uid, $this->randomName(16), $valid_email, $this->randomName(16), $this->randomName(64));
+      $this->assertText(t('The message has been sent.'), t('Message sent.'));
+    }
+    // Submit contact form one over limit.
+    $this->drupalGet('user/' . $web_user->uid . '/contact');
+    $this->assertRaw(t('You cannot send more than %number messages per hour. Please try again later.', array('%number' => $edit['contact_hourly_threshold'])), t('Message threshold reached.'));
+  }
+
+  /**
+   * Submit personal contact form.
+   *
+   * @param string $uid User ID of the contactee.
+   * @param string $from Name.
+   * @param string $mail E-mail address.
+   * @param string $subject Subject.
+   * @param string $message Message.
+   */
+  function submitPersonalContact($uid, $from, $mail, $subject, $message) {
+    $edit = array();
+    $edit['from'] = $from;
+    $edit['mail'] = $mail;
+    $edit['subject'] = $subject;
+    $edit['message'] = $message;
+    $this->drupalPost('user/' . $uid . '/contact', $edit, t('Send e-mail'));
+  }
+
+  /**
+   * Set permission.
+   *
+   * @param string $role User role to set permissions for.
+   * @param array $permissions Key-value array of permissions to set.
+   */
+  function setPermission($role, $permissions) {
+    // Get role id (rid) for specified role.
+    $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array($role)));
+    if ($rid === FALSE) {
+      $this->fail(t('Role %role not found.', array('%role' => $role)), t('Permission'));
+    }
+
+    // Create edit array from permission.
+    $edit = array();
+    foreach ($permissions as $name => $value) {
+      $edit[$rid . '[' . $name . ']'] = $value;
+    }
+
+    $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
+    $this->assertText(t('The changes have been saved.'), t('Saved changes.'), t('Permission'));
+  }
+}
