contact.module 5 KB
Newer Older
Dries's avatar
 
Dries committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?php
// $Id$

/**
 * @file
 * Enables the use of personal contact forms.
 */

// Users are not allowed to send more than x mails/hour:
define('CONTACT_HOURLY_THRESHOLD', 3);

/**
 * Implementation of hook_help().
 */
function contact_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Enables the use of personal contact forms.');
  }
}

/**
 * Implementation of hook_menu().
 */
function contact_menu($may_cache) {
  global $user;
  $items = array();

  if (!$may_cache) {
    if (arg(0) == 'user' && is_numeric(arg(1))) {
      $items[] = array('path' => "user/". arg(1) ."/contact", 'title' => t('contact'),
        'callback' => 'contact_mail_user', 'type' => MENU_LOCAL_TASK, 'weight' => 2);
    }
  }

  return $items;
}

/**
 * Implementation of hook_user().
 *
 * Provides signature customization for the user's comments.
 */
function contact_user($type, $edit, &$user, $category = NULL) {
  if ($type == 'form' && $category == 'account') {
    return array(array('title' => t('Contact settings'), 'data' => form_checkbox(t('Personal contact form'), 'contact', 1, $edit['contact'], t('Allow other users to contact you by e-mail via <a href="%url">your personal contact form</a>.  Note that your e-mail address is not made public and that privileged users such as site administrators are able to contact you even if you choose not to enable this feature.', array('%url' => "user/$user->uid/contact"))), 'weight' => 2));
  }
  if ($type == 'validate') {
    return array('contact' => $edit['contact']);
  }
}

function contact_mail_user() {
  global $user;

  if ($account = user_load(array('uid' => arg(1), 'status' => 1))) {
    if (!$account->contact && !user_access('administer users')) {
      $output = t('%name is not accepting e-mails.', array('%name' => $account->name));
    }
    else if (!$user->uid) {
      $output = t('Please <a href="%login">login</a> or <a href="%register">register</a> to send %name a message.', array('%login' => url('user/login'), '%register' => url('user/register'), '%name' => $account->name));
    }
    else if (!valid_email_address($user->mail)) {
      $output = t('You need to provide a valid e-mail address to contact other users.  Please edit your <a href="%url">user information</a>.', array('%url' => url("user/$user->uid/edit")));
    }
    else if (!flood_is_allowed('contact', CONTACT_HOURLY_THRESHOLD)) {
      $output = t("You can't contact more than %number users per hour.  Please try again later.", array('%number' => CONTACT_HOURLY_THRESHOLD));
    }
    else {
      $edit = $_POST['edit'];

      if ($edit) {
        // Validate the message:
        if (!$edit['message']) {
          form_set_error('message',  t('You must enter a message.'));
        }
77 78 79
  if (!$edit['subject']) {
    form_set_error('subject', t('You must enter a subject.'));
  }
Dries's avatar
 
Dries committed
80 81 82 83 84 85 86 87 88 89 90 91

        if (!form_get_errors()) {
          // 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')));
          $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[] = $edit['message'];

          // Tidy up the body:
          foreach ($message as $key => $value) {
            $message[$key] = wordwrap(strip_tags($value));
92

Dries's avatar
 
Dries committed
93 94 95 96 97
          }

          // Prepare all fields:
          $to = $account->mail;
          $from = $user->mail;
98 99 100 101 102 103

    // Format the subject:
          $subject = '['. variable_get('site_name', 'drupal') .'] '. $edit['subject'];

          // Prepare the body:
    $body = implode("\n\n", $message);
Dries's avatar
 
Dries committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

          // Send the e-mail:
          user_mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");

          // 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)));

          // Set a status message:
          drupal_set_message(t('Your message has been sent.'));

          // Jump to the user's profile page:
          drupal_goto("user/$account->uid");
        }
      }
      else {
        $edit['mail'] = $user->mail;
      }

      $output  = form_item(t('From'), $user->name .' &lt;'. $user->mail .'&gt;');
      $output .= form_item(t('To'), $account->name);
Dries's avatar
Dries committed
125
      $output .= form_textfield(t('Subject'), 'subject', $edit['subject'], 50, 50, NULL, NULL, TRUE);
Dries's avatar
 
Dries committed
126 127 128 129 130 131 132 133 134 135 136 137 138
      $output .= form_textarea(t('Message'), 'message', $edit['message'], 70, 8, NULL, NULL, TRUE);
      $output .= form_submit(t('Send e-mail'));
      $output  = form($output);
    }

    print theme('page', $output, $account->name);
  }
  else {
    drupal_not_found();
  }
}

?>