Index: modules/contact/contact.module =================================================================== RCS file: /cvs/drupal/drupal/modules/contact/contact.module,v retrieving revision 1.111 diff -u -p -r1.111 contact.module --- modules/contact/contact.module 9 Oct 2008 15:15:51 -0000 1.111 +++ modules/contact/contact.module 12 Feb 2009 06:10:20 -0000 @@ -45,6 +45,10 @@ function contact_perm() { 'title' => t('Access site-wide contact form'), 'description' => t('Send feedback to administrators via e-mail using the site-wide contact form.'), ), + 'access personal contact form' => array( + 'title' => t('Access personal contact form'), + 'description' => t('Send e-mail to registered users via their personal contact form.'), + ), ); } @@ -116,15 +120,39 @@ function contact_menu() { */ function _contact_user_tab_access($account) { global $user; + + + // user administrators always have access to the contact form. + if (user_access('administer users')) { + return true; + } + + // never show the contact form when it is turned off if (!isset($account->contact)) { - $account->contact = FALSE; + return false; } - return - $account && $user->uid && - ( - ($user->uid != $account->uid && $account->contact) || - user_access('administer users') - ); + + // the user himself always has access to his own contact form, + // even if he is not allowed to access the contact forms of other users, + // unless he has tured his own contact form off. + if ($user->uid == $account->uid) { + return true; + } + + // don't show the form if you're not allowed to access it. + // this does not count for the user's own contact form. + if (!user_access('access personal contact form')) { + return false; + } + + // Show the contact form, but only show to anonymous users + // when the user has explicitly selected to show it to them. + if ((user_is_logged_in() && $account->contact) || $account->contact_public) { + return true; + } + + // for privacy, do not show contact form by default. + return false; } /** @@ -150,6 +178,11 @@ function contact_user_form(&$edit, &$use '#default_value' => !empty($edit['contact']) ? $edit['contact'] : FALSE, '#description' => t('Allow other users to contact you by e-mail via your personal contact form. Note that while your e-mail address is not made public to other members of the community, privileged users such as site administrators are able to contact you even if you choose not to enable this feature.', array('@url' => url("user/$user->uid/contact"))), ); + $form['contact']['contact_public'] = array('#type' => 'checkbox', + '#title' => t('Allow contact from anonymous users'), + '#default_value' => !empty($edit['contact_public']) ? $edit['contact_public'] : FALSE, + '#description' => t('Allow anonymous users to contact you by e-mail via your personal contact form. Note that while your e-mail address is not made public to anonymous users, privileged users such as site administrators are able to contact you even if you choose not to enable this feature.', array('@url' => url("user/$user->uid/conact"))), + ); return $form; } } @@ -178,7 +211,11 @@ function contact_mail($key, &$message, $ case 'page_copy': $contact = $params['contact']; $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language); - $message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language); + if (!$user->uid == 0) { + $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); + } else { + $message['body'][] = t("!name has sent you a message via your contact form (!form-url) at !site.", array('!name' => $params['from'], '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language); + } $message['body'][] = $params['message']; break; case 'page_autoreply': @@ -192,7 +229,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.15 diff -u -p -r1.15 contact.pages.inc --- modules/contact/contact.pages.inc 13 Oct 2008 00:33:02 -0000 1.15 +++ modules/contact/contact.pages.inc 12 Feb 2009 06:10:20 -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,7 +160,11 @@ function contact_mail_page_submit($form, function contact_user_page($account) { global $user; - if (!valid_email_address($user->mail)) { + if ($user->uid == 0 && $account->contact_public) { + drupal_set_title($account->name); + $output = drupal_get_form('contact_mail_user', $account); + } + elseif (!valid_email_address($user->mail) && !$account->contact_public) { $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())))); } elseif (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) { @@ -171,66 +178,128 @@ 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; + if (!$user->uid == 0) { + $form['#token'] = $user->name . $user->mail; + $form['from'] = array( + '#type' => 'item', + '#title' => t('From'), + '#markup' => check_plain($user->name) . ' <' . check_plain($user->mail) . '>', + ); + } else { + $form['#token'] = 'Anonymous' . variable_get('site_mail'); + $form['from'] = array( + '#type' => 'textfield', + '#title' => t('From'), + '#maxlength' => 255, + '#required' => TRUE, + ); + $form['mail'] = array( + '#type' => 'textfield', + '#title' => t('E-mail'), + '#maxlength' => 255, + '#required' => TRUE, + ); + } $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', '#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']; + $account = $values['recipient']; // Send from the current user to the requested user. $to = $account->mail; - $from = $user->mail; + if (!$user->uid == 0) { + $from = $user->mail; + $username = $user->name; + } else { + $from = variable_get('site_mail'); + $username = 'anonymous'; + } // Save both users and all form values for email composition. - $values = $form_state['values']; $values['account'] = $account; + if (!$user->uid) { + $user->mail = $values['mail']; + $user->name = $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); } flood_register_event('contact'); - watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name)); + watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $username, '%name-to' => $account->name)); drupal_set_message(t('The message has been sent.')); - // Back to the requested users profile page. - $form_state['redirect'] = "user/$account->uid"; + // Back to the requested users profile page or the homepage if the + // user does not have access to user profiles. + $form_state['redirect'] = user_access('access user profiles') ? "user/$account->uid" : ''; }