2
// $Id: contact.module,v 1.74 2007/01/10 15:17:51 dries Exp $
6
* Enables the use of personal and site-wide contact forms.
10
* Implementation of hook_help().
12
function contact_help($section) {
14
case 'admin/help#contact':
15
$output = '<p>'. t('The contact module enables the use of both personal and site-wide contact forms, thereby facilitating easy communication within the community. While personal contact forms allow users to contact each other by e-mail, site-wide forms allow community members to contact the site administration from a central location. Users can specify a subject and message in the contact form, and also request that a copy of the e-mail be sent to their own address.') .'</p>';
16
$output .= '<p>'. t("Users can activate/deactivate their personal contact forms in their account settings. Upon activation, a contact tab will appear in their user profiles. Privileged users such as site administrators are able to contact users even if they have chosen not to enable this feature.") .'</p>';
17
$output .= '<p>'. t("Note that the contact tab will not appear when a user views his or her own profile; only when viewing another user's profile, if that user's contact form is enabled.") .'</p>';
18
$output .= '<p>'. t('If the menu module is enabled, a menu item linking to the site-wide contact page is added to the navigation block. It is disabled by default, but can be enabled via the <a href="@menu-module">menu management</a> page. Links to the contact page may also be added to the primary and secondary links using the same page.', array('@menu-module' => url('admin/build/menu'))) .'</p>';
19
$output .= '<p>'. t('For more information, please read the configuration and customization handbook page for the <a href="@contact">contact module</a>.', array('@contact' => url('http://drupal.org/handbook/modules/contact/', NULL, NULL, TRUE))) .'</p>';
21
case 'admin/build/contact':
22
$output = '<p>'. t('This page lets you setup <a href="@form">your site-wide contact form</a>. To do so, add one or more categories. You can associate different recipients with each category to route e-mails to different people. For example, you can route website feedback to the webmaster and direct product information requests to the sales department. On the <a href="@settings">settings page</a>, you can customize the information shown above the contact form. This can be useful to provide additional contact information such as your postal address and telephone number.', array('@settings' => url('admin/build/contact/settings'), '@form' => url('contact'))) .'</p>';
23
if (!module_exists('menu')) {
24
$menu_note = t('The menu item can be customized and configured only once the menu module has been <a href="@modules-page">enabled</a>.', array('@modules-page' => url('admin/settings/modules')));
29
$output .= '<p>'. t('The contact module also adds a <a href="@menu-settings">menu item</a> (disabled by default) to the navigation block.', array('@menu-settings' => url('admin/build/menu'))) .' '. $menu_note .'</p>';
35
* Implementation of hook_perm
37
function contact_perm() {
38
return array('access site-wide contact form');
41
* Implementation of hook_menu().
43
function contact_menu($may_cache) {
46
$items[] = array('path' => 'admin/build/contact',
47
'title' => t('Contact form'),
48
'description' => t('Create a system contact form and set up categories for the form to use.'),
49
'callback' => 'contact_admin_categories',
50
'access' => user_access('administer site configuration'),
52
$items[] = array('path' => 'admin/build/contact/list',
54
'callback' => 'contact_admin_categories',
55
'access' => user_access('administer site configuration'),
56
'type' => MENU_DEFAULT_LOCAL_TASK,
58
$items[] = array('path' => 'admin/build/contact/add',
59
'title' => t('Add category'),
60
'callback' => 'drupal_get_form',
61
'callback arguments' => array('contact_admin_edit'),
62
'access' => user_access('administer site configuration'),
63
'type' => MENU_LOCAL_TASK,
66
$items[] = array('path' => 'admin/build/contact/edit',
67
'title' => t('Edit contact category'),
68
'callback' => 'drupal_get_form',
69
'callback arguments' => array('contact_admin_edit'),
70
'access' => user_access('administer site configuration'),
71
'type' => MENU_CALLBACK,
73
$items[] = array('path' => 'admin/build/contact/delete',
74
'title' => t('Delete contact'),
75
'callback' => 'drupal_get_form',
76
'callback arguments' => array('contact_admin_delete'),
77
'access' => user_access('administer site configuration'),
78
'type' => MENU_CALLBACK,
80
$items[] = array('path' => 'admin/build/contact/settings',
81
'title' => t('Settings'),
82
'callback' => 'drupal_get_form',
83
'callback arguments' => array('contact_admin_settings'),
84
'access' => user_access('administer site configuration'),
85
'type' => MENU_LOCAL_TASK,
88
$items[] = array('path' => 'contact',
89
'title' => t('Contact'),
90
'callback' => 'contact_site_page',
91
'access' => user_access('access site-wide contact form'),
92
'type' => MENU_SUGGESTED_ITEM,
96
if (arg(0) == 'user' && is_numeric(arg(1))) {
98
$account = user_load(array('uid' => arg(1)));
99
if (($user->uid != $account->uid && $account->contact) || user_access('administer users')) {
100
$items[] = array('path' => 'user/'. arg(1) .'/contact',
101
'title' => t('Contact'),
102
'callback' => 'contact_user_page',
103
'type' => MENU_LOCAL_TASK,
104
'access' => $user->uid,
115
* Implementation of hook_user().
117
* Allows the user the option of enabling/disabling his personal contact form.
119
function contact_user($type, &$edit, &$user, $category = NULL) {
120
if ($type == 'form' && $category == 'account') {
121
$form['contact'] = array('#type' => 'fieldset',
122
'#title' => t('Contact settings'),
124
'#collapsible' => TRUE,
126
$form['contact']['contact'] = array('#type' => 'checkbox',
127
'#title' => t('Personal contact form'),
128
'#default_value' => $edit['contact'],
129
'#description' => t('Allow other users to contact you by e-mail via <a href="@url">your personal contact form</a>. 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"))),
133
elseif ($type == 'validate') {
134
return array('contact' => $edit['contact']);
136
elseif ($type == 'insert') {
137
$edit['contact'] = variable_get('contact_default_status', 1);
142
* Categories/list tab.
144
function contact_admin_categories() {
145
$result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category');
147
while ($category = db_fetch_object($result)) {
148
$rows[] = array($category->category, $category->recipients, ($category->selected ? t('Yes') : t('No')), l(t('edit'), 'admin/build/contact/edit/'. $category->cid), l(t('delete'), 'admin/build/contact/delete/'. $category->cid));
150
$header = array(t('Category'), t('Recipients'), t('Selected'), array('data' => t('Operations'), 'colspan' => 2));
152
return theme('table', $header, $rows);
156
* Category edit page.
158
function contact_admin_edit($cid = NULL) {
159
if (arg(3) == "edit" && $cid > 0) {
160
$edit = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
162
$form['category'] = array('#type' => 'textfield',
163
'#title' => t('Category'),
165
'#default_value' => $edit['category'],
166
'#description' => t("Example: 'website feedback' or 'product information'."),
169
$form['recipients'] = array('#type' => 'textarea',
170
'#title' => t('Recipients'),
171
'#default_value' => $edit['recipients'],
172
'#description' => t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com'. To specify multiple recipients, separate each e-mail address with a comma."),
175
$form['reply'] = array('#type' => 'textarea',
176
'#title' => t('Auto-reply'),
177
'#default_value' => $edit['reply'],
178
'#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
180
$form['weight'] = array('#type' => 'weight',
181
'#title' => t('Weight'),
182
'#default_value' => $edit['weight'],
183
'#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'),
185
$form['selected'] = array('#type' => 'select',
186
'#title' => t('Selected'),
187
'#options' => array('0' => t('No'), '1' => t('Yes')),
188
'#default_value' => $edit['selected'],
189
'#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
191
$form['cid'] = array('#type' => 'value',
192
'#value' => $edit['cid'],
194
$form['submit'] = array('#type' => 'submit',
195
'#value' => t('Submit'),
202
* Validate the contact category edit page form submission.
204
function contact_admin_edit_validate($form_id, $form_values) {
205
if (empty($form_values['category'])) {
206
form_set_error('category', t('You must enter a category.'));
208
if (empty($form_values['recipients'])) {
209
form_set_error('recipients', t('You must enter one or more recipients.'));
212
$recipients = explode(',', $form_values['recipients']);
213
foreach ($recipients as $recipient) {
214
if (!valid_email_address(trim($recipient))) {
215
form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
222
* Process the contact category edit page form submission.
224
function contact_admin_edit_submit($form_id, $form_values) {
225
if ($form_values['selected']) {
226
// Unselect all other contact categories.
227
db_query('UPDATE {contact} SET selected = 0');
229
$recipients = explode(',', $form_values['recipients']);
230
foreach ($recipients as $key=>$recipient) {
231
// E-mail address validation has already been done in _validate.
232
$recipients[$key] = trim($recipient);
234
$form_values['recipients'] = implode(',', $recipients);
235
if (arg(3) == 'add') {
236
db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected']);
237
drupal_set_message(t('Category %category has been added.', array('%category' => $form_values['category'])));
238
watchdog('mail', t('Contact form: category %category added.', array('%category' => $form_values['category'])), WATCHDOG_NOTICE, l(t('view'), 'admin/build/contact'));
242
db_query("UPDATE {contact} SET category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d WHERE cid = %d", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected'], $form_values['cid']);
243
drupal_set_message(t('Category %category has been updated.', array('%category' => $form_values['category'])));
244
watchdog('mail', t('Contact form: category %category updated.', array('%category' => $form_values['category'])), WATCHDOG_NOTICE, l(t('view'), 'admin/build/contact'));
247
return 'admin/build/contact';
251
* Category delete page.
253
function contact_admin_delete($cid = NULL) {
254
if ($info = db_fetch_object(db_query("SELECT category FROM {contact} WHERE cid = %d", $cid))) {
255
$form['category'] = array('#type' => 'value',
256
'#value' => $info->category,
259
return confirm_form($form, t('Are you sure you want to delete %category?', array('%category' => $info->category)), 'admin/build/contact', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
262
drupal_set_message(t('Category not found.'), 'error');
263
drupal_goto('admin/build/contact');
268
* Process category delete form submission.
270
function contact_admin_delete_submit($form_id, $form_values) {
271
db_query("DELETE FROM {contact} WHERE cid = %d", arg(4));
272
drupal_set_message(t('Category %category has been deleted.', array('%category' => $form_values['category'])));
273
watchdog('mail', t('Contact form: category %category deleted.', array('%category' => $form_values['category'])), WATCHDOG_NOTICE);
275
return 'admin/build/contact';
278
function contact_admin_settings() {
279
$form['contact_form_information'] = array('#type' => 'textarea',
280
'#title' => t('Additional information'),
281
'#default_value' => variable_get('contact_form_information', t('You can leave a message using the contact form below.')),
282
'#description' => t('Information to show on the <a href="@form">contact page</a>. Can be anything from submission guidelines to your postal address or telephone number.', array('@form' => url('contact'))),
284
$form['contact_hourly_threshold'] = array('#type' => 'select',
285
'#title' => t('Hourly threshold'),
286
'#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50)),
287
'#default_value' => variable_get('contact_hourly_threshold', 3),
288
'#description' => t('The maximum number of contact form submissions a user can perform per hour.'),
290
$form['contact_default_status'] = array(
291
'#type' => 'checkbox',
292
'#title' => t('Enable personal contact form by default'),
293
'#default_value' => variable_get('contact_default_status', 1),
294
'#description' => t('Default status of the personal contact form for new users.'),
296
return system_settings_form($form);
300
* Personal contact page.
302
function contact_user_page() {
305
if ($account = user_load(array('uid' => arg(1)))) {
306
if (!valid_email_address($user->mail)) {
307
$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")));
309
else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
310
$output = t('You cannot contact more than %number users per hour. Please try again later.', array('%number' => variable_get('contact_hourly_threshold', 3)));
313
drupal_set_title(check_plain($account->name));
314
$output = drupal_get_form('contact_mail_user', $account);
324
function contact_mail_user($recipient) {
326
$form['#token'] = $user->name . $user->mail;
327
$form['from'] = array('#type' => 'item',
328
'#title' => t('From'),
329
'#value' => check_plain($user->name) .' <'. check_plain($user->mail) .'>',
331
$form['to'] = array('#type' => 'item',
333
'#value' => check_plain($recipient->name),
335
$form['subject'] = array('#type' => 'textfield',
336
'#title' => t('Subject'),
340
$form['message'] = array('#type' => 'textarea',
341
'#title' => t('Message'),
345
$form['copy'] = array('#type' => 'checkbox',
346
'#title' => t('Send yourself a copy.'),
348
$form['submit'] = array('#type' => 'submit',
349
'#value' => t('Send e-mail'),
355
* Process the personal contact page form submission.
357
function contact_mail_user_submit($form_id, $form_values) {
360
$account = user_load(array('uid' => arg(1), 'status' => 1));
362
$message[] = "$account->name,";
363
$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')));
364
$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)));
365
$message[] = t('Message:');
366
$message[] = $form_values['message'];
369
foreach ($message as $key => $value) {
370
$message[$key] = wordwrap($value);
373
// Prepare all fields:
374
$to = $account->mail;
377
// Format the subject:
378
$subject = '['. variable_get('site_name', 'Drupal') .'] '. $form_values['subject'];
381
$body = implode("\n\n", $message);
384
drupal_mail('contact-user-mail', $to, $subject, $body, $from);
386
// Send a copy if requested:
387
if ($form_values['copy']) {
388
drupal_mail('contact-user-copy', $from, $subject, $body, $from);
391
// Log the operation:
392
flood_register_event('contact');
393
watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name)));
395
// Set a status message:
396
drupal_set_message(t('The message has been sent.'));
398
// Jump to the user's profile page:
399
return "user/$account->uid";
403
* Site-wide contact page
405
function contact_site_page() {
408
if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
409
$output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
412
$output = drupal_get_form('contact_mail_page');
418
function contact_mail_page() {
421
$result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
422
while ($category = db_fetch_object($result)) {
423
$categories[$category->cid] = $category->category;
424
if ($category->selected) {
425
$default_category = $category->cid;
429
if (count($categories) > 0) {
430
$form['#token'] = $user->name . $user->mail;
431
$form['contact_information'] = array('#value' => filter_xss_admin(variable_get('contact_form_information', t('You can leave us a message using the contact form below.'))));
432
$form['name'] = array('#type' => 'textfield',
433
'#title' => t('Your name'),
435
'#default_value' => $user->uid ? $user->name : '',
438
$form['mail'] = array('#type' => 'textfield',
439
'#title' => t('Your e-mail address'),
441
'#default_value' => $user->uid ? $user->mail : '',
444
$form['subject'] = array('#type' => 'textfield',
445
'#title' => t('Subject'),
449
if (count($categories) > 1) {
450
// If there is more than one category available and no default category has been selected,
451
// prepend a default placeholder value.
452
if (!isset($default_category)) {
453
$categories = array(t('--')) + $categories;
455
$form['cid'] = array('#type' => 'select',
456
'#title' => t('Category'),
457
'#default_value' => $default_category,
458
'#options' => $categories,
463
// If there is only one category, store its cid.
464
$category_keys = array_keys($categories);
465
$form['cid'] = array('#type' => 'value',
466
'#value' => array_shift($category_keys),
469
$form['message'] = array('#type' => 'textarea',
470
'#title' => t('Message'),
473
// We do not allow anonymous users to send themselves a copy
474
// because it can be abused to spam people.
476
$form['copy'] = array('#type' => 'checkbox',
477
'#title' => t('Send yourself a copy.'),
480
$form['submit'] = array('#type' => 'submit',
481
'#value' => t('Send e-mail'),
485
$form['#error'] = array('#value' => t('The contact form has not been configured.'));
491
* Validate the site-wide contact page form submission.
493
function contact_mail_page_validate($form_id, $form_values) {
494
if (!$form_values['cid']) {
495
form_set_error('category', t('You must select a valid category.'));
497
if (!valid_email_address($form_values['mail'])) {
498
form_set_error('mail', t('You must enter a valid e-mail address.'));
503
* Process the site-wide contact page form submission.
505
function contact_mail_page_submit($form_id, $form_values) {
507
// E-mail address of the sender: as the form field is a text field,
508
// all instances of \r and \n have been automatically stripped from it.
509
$from = $form_values['mail'];
512
$message[] = t("!name sent a message using the contact form at !form.", array('!name' => $form_values['name'], '!form' => url($_GET['q'], NULL, NULL, TRUE)));
513
$message[] = $form_values['message'];
516
foreach ($message as $key => $value) {
517
$message[$key] = wordwrap($value);
520
// Load the category information:
521
$contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $form_values['cid']));
523
// Format the category:
524
$subject = t('[!category] !subject', array('!category' => $contact->category, '!subject' => $form_values['subject']));
527
$body = implode("\n\n", $message);
529
// Send the e-mail to the recipients:
530
drupal_mail('contact-page-mail', $contact->recipients, $subject, $body, $from);
532
// If the user requests it, send a copy.
533
if ($form_values['copy']) {
534
drupal_mail('contact-page-copy', $from, $subject, $body, $from);
537
// Send an auto-reply if necessary:
538
if ($contact->reply) {
539
drupal_mail('contact-page-autoreply', $from, $subject, wordwrap($contact->reply), $contact->recipients);
542
// Log the operation:
543
flood_register_event('contact');
544
watchdog('mail', t('%name-from sent an e-mail regarding %category.', array('%name-from' => $form_values['name'] ." <$from>", '%category' => $contact->category)));
547
drupal_set_message(t('Your message has been sent.'));
549
// Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.