Index: modules/contact/contact.js =================================================================== RCS file: modules/contact/contact.js diff -N modules/contact/contact.js --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ modules/contact/contact.js 13 Sep 2008 05:33:04 -0000 @@ -0,0 +1,36 @@ +// $Id$ + +Drupal.behaviors.contact = function(context) { + var cookie_parts = new Array("name", "mail"); + var form_parts = new Array("from", "mail"); + var cookie = ''; + for (i=0;i<2;i++) { + cookie = Drupal.contact.getCookie('comment_info_' + cookie_parts[i]); + if (cookie != '') { + $("#contact-mail-user input[name=" + form_parts[i] + "]:not(.comment-processed)", context) + .val(cookie) + .addClass('comment-processed'); + } + } +}; + +Drupal.contact = {}; + +Drupal.contact.getCookie = function(name) { + var search = name + '='; + var returnValue = ''; + + if (document.cookie.length > 0) { + offset = document.cookie.indexOf(search); + if (offset != -1) { + offset += search.length; + var end = document.cookie.indexOf(';', offset); + if (end == -1) { + end = document.cookie.length; + } + returnValue = decodeURIComponent(document.cookie.substring(offset, end).replace(/\+/g, '%20')); + } + } + + return returnValue; +}; 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 13 Sep 2008 05:33:06 -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('Contact users via email using their 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'); } /** @@ -177,10 +173,11 @@ function contact_mail($key, &$message, $ case 'user_mail': case 'user_copy': $user = $params['user']; + $user->url = $user->uid ? url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)) : $user->mail; $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->url, '!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 13 Sep 2008 05:33:07 -0000 @@ -157,7 +157,7 @@ function contact_mail_page_submit($form, function contact_user_page($account) { global $user; - 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", 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')) { @@ -173,35 +173,85 @@ function contact_user_page($account) { 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) . ' <' . check_plain($user->mail) . '>', - ); - $form['to'] = array('#type' => 'item', + + $form['recipient'] = array( + '#type' => 'value', + '#value' => $recipient + ); + + if ($user->uid) { + $form['#token'] = $user->name . $user->mail; + $form['from'] = array( + '#type' => 'item', + '#title' => t('From'), + '#markup' => check_plain($user->name) . ' <' . check_plain($user->mail) . '>', + ); + $form['mail'] = array( + '#type' => 'value', + '#value' => $user->mail, + ); + } + else { + drupal_add_js(drupal_get_path('module', 'contact') . '/contact.js'); + $form['#token'] = $recipient->name . $recipient->mail; + $form['from'] = 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, + ); + } + + $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', + $form['copy'] = array( + '#type' => 'checkbox', '#title' => t('Send yourself a copy.'), + '#disabled' => !$user->uid, ); - $form['submit'] = array('#type' => 'submit', + $form['submit'] = array( + '#type' => 'submit', '#value' => t('Send e-mail'), ); return $form; } +function contact_mail_user_validate($form, &$form_state) { + global $user; + if (!$user->uid) { + if (!valid_email_address($form_state['values']['mail'])) { + form_set_error('mail', t('You must enter a valid e-mail address.')); + } + 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. */ @@ -212,11 +262,15 @@ function contact_mail_user_submit($form, // Send from the current user to the requested user. $to = $account->mail; - $from = $user->mail; + $from = $form_state['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. 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 13 Sep 2008 05:33:13 -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(' [permission] Role "' . $role . '" not found.')); + } + + // 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(' [permission] Saved changes.')); + } +}