Code coverage for /20080809/modules/contact/contact.pages.inc

Line #Times calledCode
1
<?php
2
// $Id: contact.pages.inc,v 1.11 2008/07/16 21:59:26 dries Exp $
3
4
/**
5
 * @file
6
 * User page callbacks for the contact module.
7
 */
8
9
10
/**
11
 * Site-wide contact page.
12
 */
1321
function contact_site_page() {
1418
  global $user;
15
1618
  if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold',
3)) && !user_access('administer site-wide contact form')) {
171
    $output = t("You cannot send more than %number messages per hour.
Please try again later.", array('%number' =>
variable_get('contact_hourly_threshold', 3)));
181
  }
19
  else {
2017
    $output = drupal_get_form('contact_mail_page');
21
  }
22
2315
  return $output;
240
}
25
2621
function contact_mail_page() {
2717
  global $user;
28
2917
  $form = $categories = array();
30
3117
  $result = db_query('SELECT cid, category, selected FROM {contact} ORDER
BY weight, category');
3217
  while ($category = db_fetch_object($result)) {
3317
    $categories[$category->cid] = $category->category;
3417
    if ($category->selected) {
350
      $default_category = $category->cid;
360
    }
3717
  }
38
3917
  if (count($categories) > 0) {
4017
    $form['#token'] = $user->uid ? $user->name . $user->mail : '';
4117
    $form['contact_information'] = array('#markup' =>
filter_xss_admin(variable_get('contact_form_information', t('You can leave
a message using the contact form below.'))));
4217
    $form['name'] = array('#type' => 'textfield',
4317
      '#title' => t('Your name'),
4417
      '#maxlength' => 255,
4517
      '#default_value' => $user->uid ? $user->name : '',
4617
      '#required' => TRUE,
47
    );
4817
    $form['mail'] = array('#type' => 'textfield',
4917
      '#title' => t('Your e-mail address'),
5017
      '#maxlength' => 255,
5117
      '#default_value' => $user->uid ? $user->mail : '',
5217
      '#required' => TRUE,
53
    );
5417
    $form['subject'] = array('#type' => 'textfield',
5517
      '#title' => t('Subject'),
5617
      '#maxlength' => 255,
5717
      '#required' => TRUE,
58
    );
5917
    if (count($categories) > 1) {
60
      // If there is more than one category available and no default
category has been selected,
61
      // prepend a default placeholder value.
6217
      if (!isset($default_category)) {
6317
        $default_category = t('- Please choose -');
6417
        $categories = array($default_category) + $categories;
6517
      }
6617
      $form['cid'] = array('#type' => 'select',
6717
        '#title' => t('Category'),
6817
        '#default_value' => $default_category,
6917
        '#options' => $categories,
7017
        '#required' => TRUE,
71
      );
7217
    }
73
    else {
74
      // If there is only one category, store its cid.
750
      $category_keys = array_keys($categories);
760
      $form['cid'] = array('#type' => 'value',
770
        '#value' => array_shift($category_keys),
78
      );
79
    }
8017
    $form['message'] = array('#type' => 'textarea',
8117
      '#title' => t('Message'),
8217
      '#required' => TRUE,
83
    );
84
    // We do not allow anonymous users to send themselves a copy
85
    // because it can be abused to spam people.
8617
    if ($user->uid) {
870
      $form['copy'] = array('#type' => 'checkbox',
880
        '#title' => t('Send yourself a copy.'),
89
      );
900
    }
91
    else {
9217
      $form['copy'] = array('#type' => 'value', '#value' => FALSE);
93
    }
9417
    $form['submit'] = array('#type' => 'submit',
9517
      '#value' => t('Send e-mail'),
96
    );
9717
  }
98
  else {
990
    drupal_set_message(t('The contact form has not been configured. <a
href="@add">Add one or more categories</a> to the form.', array('@add' =>
url('admin/build/contact/add'))), 'error');
100
  }
10117
  return $form;
1020
}
103
104
/**
105
 * Validate the site-wide contact page form submission.
106
 */
10721
function contact_mail_page_validate($form, &$form_state) {
1088
  if (!$form_state['values']['cid']) {
1090
    form_set_error('cid', t('You must select a valid category.'));
1100
  }
1118
  if (!valid_email_address($form_state['values']['mail'])) {
1122
    form_set_error('mail', t('You must enter a valid e-mail address.'));
1132
  }
1148
}
115
116
/**
117
 * Process the site-wide contact page form submission.
118
 */
11921
function contact_mail_page_submit($form, &$form_state) {
1203
  global $language;
121
1223
  $values = $form_state['values'];
123
124
  // E-mail address of the sender: as the form field is a text field,
125
  // all instances of \r and \n have been automatically stripped from it.
1263
  $from = $values['mail'];
127
128
  // Load category properties and save form values for email composition.
1293
  $contact = contact_load($values['cid']);
1303
  $values['contact'] = $contact;
131
132
  // Send the e-mail to the recipients using the site default language.
1333
  drupal_mail('contact', 'page_mail', $contact['recipients'],
language_default(), $values, $from);
134
135
  // If the user requests it, send a copy using the current language.
1363
  if ($values['copy']) {
1370
    drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
1380
  }
139
140
  // Send an auto-reply if necessary using the current language.
1413
  if ($contact['reply']) {
1423
    drupal_mail('contact', 'page_autoreply', $from, $language, $values,
$contact['recipients']);
1433
  }
144
1453
  flood_register_event('contact');
1463
  watchdog('mail', '%name-from sent an e-mail regarding %category.',
array('%name-from' => $values['name'] . " [$from]", '%category' =>
$contact['category']));
1473
  drupal_set_message(t('Your message has been sent.'));
148
149
  // Jump to home page rather than back to contact page to avoid
150
  // contradictory messages if flood control has been activated.
1513
  $form_state['redirect'] = '';
1523
}
153
154
/**
155
 * Personal contact page.
156
 */
15721
function contact_user_page($account) {
1582
  global $user;
159
1602
  if (!valid_email_address($user->mail)) {
1610
    $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", array('query' =>
'destination=' . drupal_get_destination()))));
1620
  }
1632
  else if (!flood_is_allowed('contact',
variable_get('contact_hourly_threshold', 3)) && !user_access('administer
site-wide contact form')) {
1640
    $output = t("You cannot send more than %number messages per hour.
Please try again later.", array('%number' =>
variable_get('contact_hourly_threshold', 3)));
1650
  }
166
  else {
1672
    drupal_set_title(check_plain($account->name));
1682
    $output = drupal_get_form('contact_mail_user', $account);
169
  }
170
1711
  return $output;
1720
}
173
17421
function contact_mail_user(&$form_state, $recipient) {
1752
  global $user;
1762
  $form['#token'] = $user->name . $user->mail;
1772
  $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
1782
  $form['from'] = array('#type' => 'item',
1792
    '#title' => t('From'),
1802
    '#markup' => check_plain($user->name) . ' &lt;' .
check_plain($user->mail) . '&gt;',
181
  );
1822
  $form['to'] = array('#type' => 'item',
1832
    '#title' => t('To'),
1842
    '#markup' => check_plain($recipient->name),
185
  );
1862
  $form['subject'] = array('#type' => 'textfield',
1872
    '#title' => t('Subject'),
1882
    '#maxlength' => 50,
1892
    '#required' => TRUE,
190
  );
1912
  $form['message'] = array('#type' => 'textarea',
1922
    '#title' => t('Message'),
1932
    '#rows' => 15,
1942
    '#required' => TRUE,
195
  );
1962
  $form['copy'] = array('#type' => 'checkbox',
1972
    '#title' => t('Send yourself a copy.'),
198
  );
1992
  $form['submit'] = array('#type' => 'submit',
2002
    '#value' => t('Send e-mail'),
201
  );
2022
  return $form;
2030
}
204
205
/**
206
 * Process the personal contact page form submission.
207
 */
20821
function contact_mail_user_submit($form, &$form_state) {
2091
  global $user, $language;
210
2111
  $account = $form_state['values']['recipient'];
212
213
  // Send from the current user to the requested user.
2141
  $to = $account->mail;
2151
  $from = $user->mail;
216
217
  // Save both users and all form values for email composition.
2181
  $values = $form_state['values'];
2191
  $values['account'] = $account;
2201
  $values['user'] = $user;
221
222
  // Send the e-mail in the requested user language.
2231
  drupal_mail('contact', 'user_mail', $to,
user_preferred_language($account), $values, $from);
224
225
  // Send a copy if requested, using current page language.
2261
  if ($form_state['values']['copy']) {
2270
    drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
2280
  }
229
2301
  flood_register_event('contact');
2311
  watchdog('mail', '%name-from sent %name-to an e-mail.',
array('%name-from' => $user->name, '%name-to' => $account->name));
2321
  drupal_set_message(t('The message has been sent.'));
233
234
  // Back to the requested users profile page.
2351
  $form_state['redirect'] = "user/$account->uid";
2361
}
23721