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

Line #Times calledCode
1
<?php
2
// $Id: contact.pages.inc,v 1.15 2008/10/13 00:33:02 webchick Exp $
3
4
/**
5
 * @file
6
 * User page callbacks for the contact module.
7
 */
8
9
10
/**
11
 * Site-wide contact page.
12
 */
1334
function contact_site_page() {
1423
  global $user;
15
1623
  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 {
2022
    $output = drupal_get_form('contact_mail_page');
21
  }
22
2320
  return $output;
240
}
25
2634
function contact_mail_page() {
2722
  global $user;
28
2922
  $form = $categories = array();
30
3122
  $result = db_query('SELECT cid, category, selected FROM {contact} ORDER
BY weight, category');
3222
  while ($category = db_fetch_object($result)) {
3321
    $categories[$category->cid] = $category->category;
3421
    if ($category->selected) {
350
      $default_category = $category->cid;
360
    }
3721
  }
38
3922
  if (count($categories) > 0) {
4021
    $form['#token'] = $user->uid ? $user->name . $user->mail : '';
4121
    $form['contact_information'] = array('#markup' =>
filter_xss_admin(variable_get('contact_form_information', t('You can leave
a message using the contact form below.'))));
4221
    $form['name'] = array('#type' => 'textfield',
4321
      '#title' => t('Your name'),
4421
      '#maxlength' => 255,
4521
      '#default_value' => $user->uid ? $user->name : '',
4621
      '#required' => TRUE,
47
    );
4821
    $form['mail'] = array('#type' => 'textfield',
4921
      '#title' => t('Your e-mail address'),
5021
      '#maxlength' => 255,
5121
      '#default_value' => $user->uid ? $user->mail : '',
5221
      '#required' => TRUE,
53
    );
5421
    $form['subject'] = array('#type' => 'textfield',
5521
      '#title' => t('Subject'),
5621
      '#maxlength' => 255,
5721
      '#required' => TRUE,
58
    );
5921
    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.
6220
      if (!isset($default_category)) {
6320
        $default_category = t('- Please choose -');
6420
        $categories = array($default_category) + $categories;
6520
      }
6620
      $form['cid'] = array('#type' => 'select',
6720
        '#title' => t('Category'),
6820
        '#default_value' => $default_category,
6920
        '#options' => $categories,
7020
        '#required' => TRUE,
71
      );
7220
    }
73
    else {
74
      // If there is only one category, store its cid.
751
      $category_keys = array_keys($categories);
761
      $form['cid'] = array('#type' => 'value',
771
        '#value' => array_shift($category_keys),
78
      );
79
    }
8021
    $form['message'] = array('#type' => 'textarea',
8121
      '#title' => t('Message'),
8221
      '#required' => TRUE,
83
    );
84
    // We do not allow anonymous users to send themselves a copy
85
    // because it can be abused to spam people.
8621
    if ($user->uid) {
870
      $form['copy'] = array('#type' => 'checkbox',
880
        '#title' => t('Send yourself a copy.'),
89
      );
900
    }
91
    else {
9221
      $form['copy'] = array('#type' => 'value', '#value' => FALSE);
93
    }
9421
    $form['submit'] = array('#type' => 'submit',
9521
      '#value' => t('Send e-mail'),
96
    );
9721
  }
98
  else {
991
    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
  }
10122
  return $form;
1020
}
103
104
/**
105
 * Validate the site-wide contact page form submission.
106
 */
10734
function contact_mail_page_validate($form, &$form_state) {
1089
  if (!$form_state['values']['cid']) {
1091
    form_set_error('cid', t('You must select a valid category.'));
1101
  }
1119
  if (!valid_email_address($form_state['values']['mail'])) {
1122
    form_set_error('mail', t('You must enter a valid e-mail address.'));
1132
  }
1149
}
115
116
/**
117
 * Process the site-wide contact page form submission.
118
 */
11934
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
 */
15734
function contact_user_page($account) {
1589
  global $user;
159
1609
  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
  }
1639
  elseif (!flood_is_allowed('contact',
variable_get('contact_hourly_threshold', 3)) && !user_access('administer
site-wide contact form')) {
1641
    $output = t("You cannot send more than %number messages per hour.
Please try again later.", array('%number' =>
variable_get('contact_hourly_threshold', 3)));
1651
  }
166
  else {
1678
    drupal_set_title($account->name);
1688
    $output = drupal_get_form('contact_mail_user', $account);
169
  }
170
1715
  return $output;
1720
}
173
17434
function contact_mail_user(&$form_state, $recipient) {
1758
  global $user;
1768
  $form['#token'] = $user->name . $user->mail;
1778
  $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
1788
  $form['from'] = array('#type' => 'item',
1798
    '#title' => t('From'),
1808
    '#markup' => check_plain($user->name) . ' &lt;' .
check_plain($user->mail) . '&gt;',
181
  );
1828
  $form['to'] = array('#type' => 'item',
1838
    '#title' => t('To'),
1848
    '#markup' => check_plain($recipient->name),
185
  );
1868
  $form['subject'] = array('#type' => 'textfield',
1878
    '#title' => t('Subject'),
1888
    '#maxlength' => 50,
1898
    '#required' => TRUE,
190
  );
1918
  $form['message'] = array('#type' => 'textarea',
1928
    '#title' => t('Message'),
1938
    '#rows' => 15,
1948
    '#required' => TRUE,
195
  );
1968
  $form['copy'] = array('#type' => 'checkbox',
1978
    '#title' => t('Send yourself a copy.'),
198
  );
1998
  $form['submit'] = array('#type' => 'submit',
2008
    '#value' => t('Send e-mail'),
201
  );
2028
  return $form;
2030
}
204
205
/**
206
 * Process the personal contact page form submission.
207
 */
20834
function contact_mail_user_submit($form, &$form_state) {
2094
  global $user, $language;
210
2114
  $account = $form_state['values']['recipient'];
212
213
  // Send from the current user to the requested user.
2144
  $to = $account->mail;
2154
  $from = $user->mail;
216
217
  // Save both users and all form values for email composition.
2184
  $values = $form_state['values'];
2194
  $values['account'] = $account;
2204
  $values['user'] = $user;
221
222
  // Send the e-mail in the requested user language.
2234
  drupal_mail('contact', 'user_mail', $to,
user_preferred_language($account), $values, $from);
224
225
  // Send a copy if requested, using current page language.
2264
  if ($form_state['values']['copy']) {
2270
    drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
2280
  }
229
2304
  flood_register_event('contact');
2314
  watchdog('mail', '%name-from sent %name-to an e-mail.',
array('%name-from' => $user->name, '%name-to' => $account->name));
2324
  drupal_set_message(t('The message has been sent.'));
233
234
  // Back to the requested users profile page.
2354
  $form_state['redirect'] = "user/$account->uid";
2364
}
23734