2 // $Id: contact.module 144 2007-03-28 07:52:20Z thierry $
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('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/menu'))) .'</p>';
18 $output .= t('Contact module links:') .'<ul>';
19 $output .= '<li>'. t('Default site-wide <a href="%contact-page">contact page</a>.', array('%contact-page' => url('contact'))) .'</li>';
20 $output .= '<li>'. t('Site-wide contact form <a href="%configuration-page">category configuration</a>.', array('%configuration-page' => url('admin/contact'))) .'</li>';
21 $output .= '<li>'. t('Site-wide contact form <a href="%additional-settings">general settings</a>.', array('%additional-settings' => url('admin/contact/settings'))) .'</li>';
22 $output .= '<li>'. t('Site-wide contact form <a href="%menu-configuration">menu configuration</a>.', array('%menu-configuration' => url('admin/menu'))) .'</li></ul>';
23 $output .= 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)));
25 case 'admin/modules#description':
26 return t('Enables the use of both personal and site-wide contact forms.');
28 $output = 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/contact/settings'), '%form' => url('contact')));
29 if (!module_exist('menu')) {
30 $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/modules')));
35 $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/menu'))) .' '. $menu_note .'</p>';
41 * Implementation of hook_menu().
43 function contact_menu($may_cache) {
46 $items[] = array('path' => 'admin/contact',
47 'title' => t('contact form'),
48 'callback' => 'contact_admin_categories',
49 'access' => user_access('administer site configuration'),
51 $items[] = array('path' => 'admin/contact/category',
52 'title' => t('categories'),
53 'callback' => 'contact_admin_categories',
54 'access' => user_access('administer site configuration'),
55 'type' => MENU_DEFAULT_LOCAL_TASK,
57 $items[] = array('path' => 'admin/contact/category/list',
59 'callback' => 'contact_admin_categories',
60 'access' => user_access('administer site configuration'),
61 'type' => MENU_DEFAULT_LOCAL_TASK,
63 $items[] = array('path' => 'admin/contact/category/add',
64 'title' => t('add category'),
65 'callback' => 'contact_admin_edit',
66 'access' => user_access('administer site configuration'),
67 'type' => MENU_LOCAL_TASK,
70 $items[] = array('path' => 'admin/contact/category/edit',
71 'title' => t('edit contact category'),
72 'callback' => 'contact_admin_edit',
73 'access' => user_access('administer site configuration'),
74 'type' => MENU_CALLBACK,
76 $items[] = array('path' => 'admin/contact/category/delete',
77 'title' => t('delete contact'),
78 'callback' => 'contact_admin_delete',
79 'access' => user_access('administer site configuration'),
80 'type' => MENU_CALLBACK,
82 $items[] = array('path' => 'admin/contact/settings',
83 'title' => t('settings'),
84 'callback' => 'contact_admin_settings',
85 'access' => user_access('administer site configuration'),
86 'type' => MENU_LOCAL_TASK,
89 $items[] = array('path' => 'contact',
90 'title' => t('contact'),
91 'callback' => 'contact_mail_page',
92 'access' => user_access('access content'),
93 'type' => MENU_SUGGESTED_ITEM,
97 if (arg(0) == 'user' && is_numeric(arg(1))) {
98 $items[] = array('path' => "user/". arg(1) ."/contact",
99 'title' => t('contact'),
100 'callback' => 'contact_mail_user',
101 'type' => MENU_LOCAL_TASK,
111 * Implementation of hook_user().
113 * Allows the user the option of enabling/disabling his personal contact form.
115 function contact_user($type, $edit, &$user, $category = NULL) {
116 if ($type == 'form' && $category == 'account') {
117 $form['contact'] = array('#type' => 'fieldset',
118 '#title' => t('Contact settings'),
120 '#collapsible' => TRUE,
122 $form['contact']['contact'] = array('#type' => 'checkbox',
123 '#title' => t('Personal contact form'),
124 '#default_value' => $edit['contact'],
125 '#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"))),
129 if ($type == 'validate') {
130 return array('contact' => $edit['contact']);
135 * Categories/list tab.
137 function contact_admin_categories() {
138 $result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category');
140 while ($category = db_fetch_object($result)) {
141 $rows[] = array($category->category, $category->recipients, ($category->selected ? t('Yes') : t('No')), l(t('edit'), 'admin/contact/category/edit/'. $category->cid), l(t('delete'), 'admin/contact/category/delete/'. $category->cid));
143 $header = array(t('Category'), t('Recipients'), t('Selected'), array('data' => t('Operations'), 'colspan' => 2));
145 return theme('table', $header, $rows);
149 * Category edit page.
151 function contact_admin_edit($cid = NULL) {
152 if (arg(3) == "edit" && $cid > 0) {
153 $edit = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
155 $form['category'] = array('#type' => 'textfield',
156 '#title' => t('Category'),
158 '#default_value' => $edit['category'],
159 '#description' => t("Example: 'website feedback' or 'product information'."),
162 $form['recipients'] = array('#type' => 'textarea',
163 '#title' => t('Recipients'),
164 '#default_value' => $edit['recipients'],
165 '#description' => t("Example: 'webmaster@yoursite.com' or 'sales@yoursite.com'. To specify multiple recipients, separate each e-mail address with a comma."),
168 $form['reply'] = array('#type' => 'textarea',
169 '#title' => t('Auto-reply'),
170 '#default_value' => $edit['reply'],
171 '#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
173 $form['weight'] = array('#type' => 'weight',
174 '#title' => t('Weight'),
175 '#default_value' => $edit['weight'],
176 '#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.'),
178 $form['selected'] = array('#type' => 'select',
179 '#title' => t('Selected'),
180 '#options' => array('0' => t('No'), '1' => t('Yes')),
181 '#default_value' => $edit['selected'],
182 '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
184 $form['cid'] = array('#type' => 'value',
185 '#value' => $edit['cid'],
187 $form['submit'] = array('#type' => 'submit',
188 '#value' => t('Submit'),
191 return drupal_get_form('contact_admin_edit', $form);
195 * Validate the contact category edit page form submission.
197 function contact_admin_edit_validate($form_id, $form_values) {
198 if (empty($form_values['category'])) {
199 form_set_error('category', t('You must enter a category.'));
201 if (empty($form_values['recipients'])) {
202 form_set_error('recipients', t('You must enter one or more recipients.'));
205 $recipients = explode(',', $form_values['recipients']);
206 foreach($recipients as $recipient) {
207 if (!valid_email_address(trim($recipient))) {
208 form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => theme('placeholder', $recipient))));
215 * Process the contact category edit page form submission.
217 function contact_admin_edit_submit($form_id, $form_values) {
218 if ($form_values['selected']) {
219 // Unselect all other contact categories.
220 db_query('UPDATE {contact} SET selected = 0');
222 $recipients = explode(',', $form_values['recipients']);
223 foreach($recipients as $key=>$recipient) {
224 // E-mail address validation has already been done in _validate.
225 $recipients[$key] = trim($recipient);
227 $form_values['recipients'] = implode(',', $recipients);
228 if (arg(3) == 'add') {
229 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']);
230 drupal_set_message(t('Category %category has been added.', array('%category' => theme('placeholder', $form_values['category']))));
231 watchdog('mail', t('Contact form: category %category added.', array('%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE, l(t('view'), 'admin/contact'));
235 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']);
236 drupal_set_message(t('Category %category has been updated.', array('%category' => theme('placeholder', $form_values['category']))));
237 watchdog('mail', t('Contact form: category %category updated.', array('%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE, l(t('view'), 'admin/contact'));
240 return 'admin/contact';
244 * Category delete page.
246 function contact_admin_delete($cid = NULL) {
247 if ($info = db_fetch_object(db_query("SELECT category FROM {contact} WHERE cid = %d", $cid))) {
248 $form['category'] = array('#type' => 'value',
249 '#value' => $info->category,
252 return confirm_form('contact_admin_delete', $form, t('Are you sure you want to delete %category?', array('%category' => theme('placeholder', $info->category))), 'admin/contact', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
255 drupal_set_message(t('Category not found.'), 'error');
256 drupal_goto('admin/contact');
261 * Process category delete form submission.
263 function contact_admin_delete_submit($form_id, $form_values) {
264 db_query("DELETE FROM {contact} WHERE cid = %d", arg(4));
265 drupal_set_message(t('Category %category has been deleted.', array('%category' => theme('placeholder', $form_values['category']))));
266 watchdog('mail', t('Contact form: category %category deleted.', array('%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE);
268 return 'admin/contact';
272 * Settings tab. Using a form rather than hook_settings().
274 function contact_admin_settings() {
275 $form['contact_form_information'] = array('#type' => 'textarea',
276 '#title' => t('Additional information'),
277 '#default_value' => variable_get('contact_form_information', t('You can leave a message using the contact form below.')),
278 '#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'))),
280 $form['contact_hourly_threshold'] = array('#type' => 'select',
281 '#title' => t('Hourly threshold'),
282 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50)),
283 '#default_value' => variable_get('contact_hourly_threshold', 3),
284 '#description' => t('The maximum number of contact form submissions a user can perform per hour.'),
286 $form['submit'] = array('#type' => 'submit',
287 '#value' => t('Save configuration'),
289 $form['reset'] = array('#type' => 'submit',
290 '#value' => t('Reset to defaults'),
292 // Use system_settings_form for the callback.
293 return drupal_get_form('contact_admin_settings', $form, 'system_settings_form');
297 * Personal contact page.
299 function contact_mail_user() {
302 if ($account = user_load(array('uid' => arg(1)))) {
303 $admin_access = user_access('administer users');
304 if (!$account->status && !$admin_access) {
305 drupal_access_denied();
307 else if (!$account->contact && !$admin_access) {
308 $output = t('%name is not accepting e-mails.', array('%name' => $account->name));
310 else if (!$user->uid) {
311 $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));
313 else if (!valid_email_address($user->mail)) {
314 $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")));
316 else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
317 $output = t('You cannot contact more than %number users per hour. Please try again later.', array('%number' => variable_get('contact_hourly_threshold', 3)));
320 drupal_set_title($account->name);
322 $form['#token'] = $user->name . $user->mail;
323 $form['from'] = array('#type' => 'item',
324 '#title' => t('From'),
325 '#value' => $user->name .' <'. $user->mail .'>',
327 $form['to'] = array('#type' => 'item',
329 '#value' => $account->name,
331 $form['subject'] = array('#type' => 'textfield',
332 '#title' => t('Subject'),
336 $form['message'] = array('#type' => 'textarea',
337 '#title' => t('Message'),
341 $form['copy'] = array('#type' => 'checkbox',
342 '#title' => t('Send me a copy.'),
344 $form['submit'] = array('#type' => 'submit',
345 '#value' => t('Send e-mail'),
347 $output = drupal_get_form('contact_mail_user', $form);
358 * Process the personal contact page form submission.
360 function contact_mail_user_submit($form_id, $edit) {
363 $account = user_load(array('uid' => arg(1), 'status' => 1));
365 $message[] = "$account->name,";
366 $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')));
367 $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)));
368 $message[] = t('Message:');
369 $message[] = $edit['message'];
372 foreach ($message as $key => $value) {
373 $message[$key] = wordwrap($value);
376 // Prepare all fields:
377 $to = $account->mail;
380 // Format the subject:
381 $subject = '['. variable_get('site_name', 'drupal') .'] '. $edit['subject'];
384 $body = implode("\n\n", $message);
387 user_mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
389 // Send a copy if requested:
391 user_mail($from, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
394 // Log the operation:
395 flood_register_event('contact');
396 watchdog('mail', t('%name-from sent %name-to an e-mail.', array('%name-from' => theme('placeholder', $user->name), '%name-to' => theme('placeholder', $account->name))));
398 // Set a status message:
399 drupal_set_message(t('The message has been sent.'));
401 // Jump to the user's profile page:
402 return "user/$account->uid";
406 * Site-wide contact page
408 function contact_mail_page() {
411 if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
412 $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
416 $edit['name'] = $user->name;
417 $edit['mail'] = $user->mail;
420 $result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
421 while ($category = db_fetch_object($result)) {
422 $categories[$category->cid] = $category->category;
423 if ($category->selected) {
424 $default_category = $category->cid;
428 if (count($categories) > 0) {
429 $form['#token'] = $user->name . $user->mail;
430 $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.'))));
431 $form['name'] = array('#type' => 'textfield',
432 '#title' => t('Your name'),
434 '#default_value' => $edit['name'],
437 $form['mail'] = array('#type' => 'textfield',
438 '#title' => t('Your e-mail address'),
440 '#default_value' => $edit['mail'],
443 $form['subject'] = array('#type' => 'textfield',
444 '#title' => t('Subject'),
448 if (count($categories) > 1) {
449 // If there is more than one category available and no default category has been selected,
450 // prepend a default placeholder value.
451 if (!isset($default_category)) {
452 $categories = array(t('--')) + $categories;
454 $form['cid'] = array('#type' => 'select',
455 '#title' => t('Category'),
456 '#default_value' => $default_category,
457 '#options' => $categories,
462 // If there is only one category, store its cid.
463 $category_keys = array_keys($categories);
464 $form['cid'] = array('#type' => 'value',
465 '#value' => array_shift($category_keys),
468 $form['message'] = array('#type' => 'textarea',
469 '#title' => t('Message'),
472 $form['copy'] = array('#type' => 'checkbox',
473 '#title' => t('Send me a copy.'),
475 $form['submit'] = array('#type' => 'submit',
476 '#value' => t('Send e-mail'),
478 $output = drupal_get_form('contact_mail_page', $form);
481 $output = t('The contact form has not been configured.');
489 * Validate the site-wide contact page form submission.
491 function contact_mail_page_validate($form_id, $form_values) {
492 if (!$form_values['cid']) {
493 form_set_error('category', t('You must select a valid category.'));
495 if (!valid_email_address($form_values['mail'])) {
496 form_set_error('mail', t('You must enter a valid e-mail address.'));
501 * Process the site-wide contact page form submission.
503 function contact_mail_page_submit($form_id, $edit) {
505 // E-mail address of the sender: as the form field is a text field,
506 // all instances of \r and \n have been automatically stripped from it.
507 $from = $edit['mail'];
510 $message[] = t("%name sent a message using the contact form at %form.", array('%name' => $edit['name'], '%form' => url($_GET['q'], NULL, NULL, TRUE)));
511 $message[] = $edit['message'];
514 foreach ($message as $key => $value) {
515 $message[$key] = wordwrap($value);
518 // Load the category information:
519 $contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $edit['cid']));
521 // Format the category:
522 $subject = t('[%category] %subject', array('%category' => $contact->category, '%subject' => $edit['subject']));
525 $body = implode("\n\n", $message);
527 // Send the e-mail to the recipients:
528 user_mail($contact->recipients, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
530 // If the user requests it, send a copy.
532 user_mail($from, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
535 // Send an auto-reply if necessary:
536 if ($contact->reply) {
537 user_mail($from, $subject, wordwrap($contact->reply), "From: $contact->recipients\nReply-to: $contact->recipients\nX-Mailer: Drupal\nReturn-path: $contact->recipients\nErrors-to: $contact->recipients");
540 // Log the operation:
541 flood_register_event('contact');
542 watchdog('mail', t('%name-from sent an e-mail regarding %category.', array('%name-from' => theme('placeholder', $edit['name'] ." <$from>"), '%category' => theme('placeholder', $contact->category))));
545 drupal_set_message(t('Your message has been sent.'));
547 // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.