initial import from onelab svn codebase
[plewww.git] / modules / contact.module
1 <?php
2 // $Id: contact.module 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * @file
6  * Enables the use of personal and site-wide contact forms.
7  */
8
9 /**
10  * Implementation of hook_help().
11  */
12 function contact_help($section) {
13   switch ($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)));
24       return $output;
25     case 'admin/modules#description':
26       return t('Enables the use of both personal and site-wide contact forms.');
27     case 'admin/contact':
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')));
31       }
32       else {
33         $menu_note = '';
34       }
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>';
36       return($output);
37   }
38 }
39
40 /**
41  * Implementation of hook_menu().
42  */
43 function contact_menu($may_cache) {
44   $items = array();
45   if ($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'),
50     );
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,
56     );
57     $items[] = array('path' => 'admin/contact/category/list',
58       'title' => t('list'),
59       'callback' => 'contact_admin_categories',
60       'access' => user_access('administer site configuration'),
61       'type' => MENU_DEFAULT_LOCAL_TASK,
62     );
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,
68       'weight' => 1,
69     );
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,
75     );
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,
81     );
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,
87       'weight' => 1,
88     );
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,
94     );
95   }
96   else {
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,
102         'weight' => 2,
103       );
104     }
105   }
106
107   return $items;
108 }
109
110 /**
111  * Implementation of hook_user().
112  *
113  * Allows the user the option of enabling/disabling his personal contact form.
114  */
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'),
119       '#weight' => 5,
120       '#collapsible' => TRUE,
121     );
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"))),
126     );
127     return $form;
128   }
129   if ($type == 'validate') {
130     return array('contact' => $edit['contact']);
131   }
132 }
133
134 /**
135  * Categories/list tab.
136  */
137 function contact_admin_categories() {
138   $result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category');
139   $rows = array();
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));
142   }
143   $header = array(t('Category'), t('Recipients'), t('Selected'), array('data' => t('Operations'), 'colspan' => 2));
144
145   return theme('table', $header, $rows);
146 }
147
148 /**
149  * Category edit page.
150  */
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));
154   }
155   $form['category'] = array('#type' => 'textfield',
156     '#title' => t('Category'),
157     '#maxlength' => 255,
158     '#default_value' => $edit['category'],
159     '#description' => t("Example: 'website feedback' or 'product information'."),
160     '#required' => TRUE,
161   );
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."),
166     '#required' => TRUE,
167   );
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.'),
172   );
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.'),
177   );
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.'),
183   );
184   $form['cid'] = array('#type' => 'value',
185     '#value' => $edit['cid'],
186   );
187   $form['submit'] = array('#type' => 'submit',
188     '#value' => t('Submit'),
189   );
190
191   return drupal_get_form('contact_admin_edit', $form);
192 }
193
194 /**
195  * Validate the contact category edit page form submission.
196  */
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.'));
200   }
201   if (empty($form_values['recipients'])) {
202     form_set_error('recipients', t('You must enter one or more recipients.'));
203   }
204   else {
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))));
209       }
210     }
211   }
212 }
213
214 /**
215  * Process the contact category edit page form submission.
216  */
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');
221   }
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);
226   }
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'));
232
233   }
234   else {
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'));
238   }
239
240   return 'admin/contact';
241 }
242
243 /**
244  * Category delete page.
245  */
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,
250     );
251
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'));
253   }
254   else {
255     drupal_set_message(t('Category not found.'), 'error');
256     drupal_goto('admin/contact');
257   }
258 }
259
260 /**
261  * Process category delete form submission.
262  */
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);
267
268   return 'admin/contact';
269 }
270
271 /**
272  * Settings tab. Using a form rather than hook_settings().
273  */
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'))),
279   );
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.'),
285   );
286   $form['submit'] = array('#type' => 'submit',
287     '#value' => t('Save configuration'),
288   );
289   $form['reset'] = array('#type' => 'submit',
290     '#value' => t('Reset to defaults'),
291   );
292   // Use system_settings_form for the callback.
293   return drupal_get_form('contact_admin_settings', $form, 'system_settings_form');
294 }
295
296 /**
297  * Personal contact page.
298  */
299 function contact_mail_user() {
300   global $user;
301
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();
306     }
307     else if (!$account->contact && !$admin_access) {
308       $output = t('%name is not accepting e-mails.', array('%name' => $account->name));
309     }
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));
312     }
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")));
315     }
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)));
318     }
319     else {
320       drupal_set_title($account->name);
321
322       $form['#token'] = $user->name . $user->mail;
323       $form['from'] = array('#type' => 'item',
324         '#title' => t('From'),
325         '#value' => $user->name .' &lt;'. $user->mail .'&gt;',
326       );
327       $form['to'] = array('#type' => 'item',
328         '#title' => t('To'),
329         '#value' => $account->name,
330       );
331       $form['subject'] = array('#type' => 'textfield',
332         '#title' => t('Subject'),
333         '#maxlength' => 50,
334         '#required' => TRUE,
335       );
336       $form['message'] = array('#type' => 'textarea',
337         '#title' => t('Message'),
338         '#rows' => 15,
339         '#required' => TRUE,
340       );
341       $form['copy'] = array('#type' => 'checkbox',
342         '#title' => t('Send me a copy.'),
343       );
344       $form['submit'] = array('#type' => 'submit',
345         '#value' => t('Send e-mail'),
346       );
347       $output = drupal_get_form('contact_mail_user', $form);
348     }
349
350     return $output;
351   }
352   else {
353     drupal_not_found();
354   }
355 }
356
357 /**
358  * Process the personal contact page form submission.
359  */
360 function contact_mail_user_submit($form_id, $edit) {
361   global $user;
362
363   $account = user_load(array('uid' => arg(1), 'status' => 1));
364   // Compose the body:
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'];
370
371   // Tidy up the body:
372   foreach ($message as $key => $value) {
373     $message[$key] = wordwrap($value);
374   }
375
376   // Prepare all fields:
377   $to = $account->mail;
378   $from = $user->mail;
379
380   // Format the subject:
381   $subject = '['. variable_get('site_name', 'drupal') .'] '. $edit['subject'];
382
383   // Prepare the body:
384   $body = implode("\n\n", $message);
385
386   // Send the e-mail:
387   user_mail($to, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
388
389   // Send a copy if requested:
390   if ($edit['copy']) {
391     user_mail($from, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
392   }
393
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))));
397
398   // Set a status message:
399   drupal_set_message(t('The message has been sent.'));
400
401   // Jump to the user's profile page:
402   return "user/$account->uid";
403 }
404
405 /**
406  * Site-wide contact page
407  */
408 function contact_mail_page() {
409   global $user;
410
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)));
413   }
414   else {
415     if ($user->uid) {
416       $edit['name'] = $user->name;
417       $edit['mail'] = $user->mail;
418     }
419
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;
425       }
426     }
427
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'),
433         '#maxlength' => 255,
434         '#default_value' => $edit['name'],
435         '#required' => TRUE,
436       );
437       $form['mail'] = array('#type' => 'textfield',
438         '#title' => t('Your e-mail address'),
439         '#maxlength' => 255,
440         '#default_value' => $edit['mail'],
441         '#required' => TRUE,
442       );
443       $form['subject'] = array('#type' => 'textfield',
444         '#title' => t('Subject'),
445         '#maxlength' => 255,
446         '#required' => TRUE,
447       );
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;
453         }
454         $form['cid'] = array('#type' => 'select',
455           '#title' => t('Category'),
456           '#default_value' => $default_category,
457           '#options' => $categories,
458           '#required' => TRUE,
459         );
460       }
461       else {
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),
466         );
467       }
468       $form['message'] = array('#type' => 'textarea',
469         '#title' => t('Message'),
470         '#required' => TRUE,
471       );
472       $form['copy'] = array('#type' => 'checkbox',
473         '#title' => t('Send me a copy.'),
474       );
475       $form['submit'] = array('#type' => 'submit',
476         '#value' => t('Send e-mail'),
477       );
478       $output = drupal_get_form('contact_mail_page', $form);
479     }
480     else {
481       $output = t('The contact form has not been configured.');
482     }
483   }
484
485   return $output;
486 }
487
488 /**
489  * Validate the site-wide contact page form submission.
490  */
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.'));
494   }
495   if (!valid_email_address($form_values['mail'])) {
496     form_set_error('mail', t('You must enter a valid e-mail address.'));
497   }
498 }
499
500 /**
501  * Process the site-wide contact page form submission.
502  */
503 function contact_mail_page_submit($form_id, $edit) {
504
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'];
508
509   // Compose the body:
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'];
512
513   // Tidy up the body:
514   foreach ($message as $key => $value) {
515     $message[$key] = wordwrap($value);
516   }
517
518   // Load the category information:
519   $contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $edit['cid']));
520
521   // Format the category:
522   $subject = t('[%category] %subject', array('%category' => $contact->category, '%subject' => $edit['subject']));
523
524   // Prepare the body:
525   $body = implode("\n\n", $message);
526
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");
529
530   // If the user requests it, send a copy.
531   if ($edit['copy']) {
532     user_mail($from, $subject, $body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
533   }
534
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");
538   }
539
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))));
543
544   // Update user:
545   drupal_set_message(t('Your message has been sent.'));
546
547   // Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
548   return('');
549 }