| 1 |
<?php
|
| 2 |
// $Id: contact.pages.inc,v 1.35 2009/10/18 11:34:44 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* User page callbacks for the contact module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Form builder; the site-wide contact form.
|
| 11 |
*
|
| 12 |
* @see contact_site_form_validate()
|
| 13 |
* @see contact_site_form_submit()
|
| 14 |
*/
|
| 15 |
function contact_site_form($form, &$form_state) {
|
| 16 |
global $user;
|
| 17 |
|
| 18 |
// Check if flood control has been activated for sending e-mails.
|
| 19 |
$limit = variable_get('contact_threshold_limit', 5);
|
| 20 |
$window = variable_get('contact_threshold_window', 3600);
|
| 21 |
if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
|
| 22 |
drupal_set_message(t("You cannot send more than %limit messages in @interval. Please try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error');
|
| 23 |
return drupal_access_denied();
|
| 24 |
}
|
| 25 |
|
| 26 |
// Get an array of the categories and the current default category.
|
| 27 |
$categories = db_select('contact', 'c')
|
| 28 |
->addTag('translatable')
|
| 29 |
->fields('c', array('cid', 'category'))
|
| 30 |
->orderBy('weight')
|
| 31 |
->orderBy('category')
|
| 32 |
->execute()
|
| 33 |
->fetchAllKeyed();
|
| 34 |
$default_category = db_query("SELECT cid FROM {contact} WHERE selected = 1")->fetchField();
|
| 35 |
|
| 36 |
// If there are no categories, do not display the form.
|
| 37 |
if (!$categories) {
|
| 38 |
if (user_access('administer contact forms')) {
|
| 39 |
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/structure/contact/add'))), 'error');
|
| 40 |
}
|
| 41 |
else {
|
| 42 |
return drupal_not_found();
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
// If there is more than one category available and no default category has
|
| 47 |
// been selected, prepend a default placeholder value.
|
| 48 |
if (!$default_category) {
|
| 49 |
if (count($categories) > 1) {
|
| 50 |
$categories = array(0 => t('- Please choose -')) + $categories;
|
| 51 |
}
|
| 52 |
else {
|
| 53 |
$default_category = key($categories);
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
if (!$user->uid) {
|
| 58 |
$form['#attached']['library'][] = array('system', 'cookie');
|
| 59 |
$form['#attributes']['class'][] = 'user-info-from-cookie';
|
| 60 |
}
|
| 61 |
|
| 62 |
$form['#token'] = $user->uid ? $user->name . $user->mail : '';
|
| 63 |
$form['name'] = array(
|
| 64 |
'#type' => 'textfield',
|
| 65 |
'#title' => t('Your name'),
|
| 66 |
'#maxlength' => 255,
|
| 67 |
'#default_value' => $user->uid ? format_username($user) : '',
|
| 68 |
'#required' => TRUE,
|
| 69 |
);
|
| 70 |
$form['mail'] = array(
|
| 71 |
'#type' => 'textfield',
|
| 72 |
'#title' => t('Your e-mail address'),
|
| 73 |
'#maxlength' => 255,
|
| 74 |
'#default_value' => $user->uid ? $user->mail : '',
|
| 75 |
'#required' => TRUE,
|
| 76 |
);
|
| 77 |
$form['subject'] = array(
|
| 78 |
'#type' => 'textfield',
|
| 79 |
'#title' => t('Subject'),
|
| 80 |
'#maxlength' => 255,
|
| 81 |
'#required' => TRUE,
|
| 82 |
);
|
| 83 |
$form['cid'] = array(
|
| 84 |
'#type' => 'select',
|
| 85 |
'#title' => t('Category'),
|
| 86 |
'#default_value' => $default_category,
|
| 87 |
'#options' => $categories,
|
| 88 |
'#required' => TRUE,
|
| 89 |
'#access' => count($categories) > 1,
|
| 90 |
);
|
| 91 |
$form['message'] = array(
|
| 92 |
'#type' => 'textarea',
|
| 93 |
'#title' => t('Message'),
|
| 94 |
'#required' => TRUE,
|
| 95 |
);
|
| 96 |
// We do not allow anonymous users to send themselves a copy
|
| 97 |
// because it can be abused to spam people.
|
| 98 |
$form['copy'] = array(
|
| 99 |
'#type' => 'checkbox',
|
| 100 |
'#title' => t('Send yourself a copy.'),
|
| 101 |
'#access' => $user->uid,
|
| 102 |
);
|
| 103 |
$form['submit'] = array(
|
| 104 |
'#type' => 'submit',
|
| 105 |
'#value' => t('Send message'),
|
| 106 |
);
|
| 107 |
|
| 108 |
return $form;
|
| 109 |
}
|
| 110 |
|
| 111 |
/**
|
| 112 |
* Form validation handler for contact_site_form().
|
| 113 |
*/
|
| 114 |
function contact_site_form_validate($form, &$form_state) {
|
| 115 |
if (!$form_state['values']['cid']) {
|
| 116 |
form_set_error('cid', t('You must select a valid category.'));
|
| 117 |
}
|
| 118 |
if (!valid_email_address($form_state['values']['mail'])) {
|
| 119 |
form_set_error('mail', t('You must enter a valid e-mail address.'));
|
| 120 |
}
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Form submission handler for contact_site_form().
|
| 125 |
*/
|
| 126 |
function contact_site_form_submit($form, &$form_state) {
|
| 127 |
global $user, $language;
|
| 128 |
|
| 129 |
$values = $form_state['values'];
|
| 130 |
$values['sender'] = $user;
|
| 131 |
$values['sender']->name = $values['name'];
|
| 132 |
$values['sender']->mail = $values['mail'];
|
| 133 |
$values['category'] = contact_load($values['cid']);
|
| 134 |
|
| 135 |
// Save the anonymous user information to a cookie for reuse.
|
| 136 |
if (!$user->uid) {
|
| 137 |
user_cookie_save($values);
|
| 138 |
}
|
| 139 |
|
| 140 |
// Get the to and from e-mail addresses.
|
| 141 |
$to = $values['category']['recipients'];
|
| 142 |
$from = $values['sender']->mail;
|
| 143 |
|
| 144 |
// Send the e-mail to the recipients using the site default language.
|
| 145 |
drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from);
|
| 146 |
|
| 147 |
// If the user requests it, send a copy using the current language.
|
| 148 |
if ($values['copy']) {
|
| 149 |
drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
|
| 150 |
}
|
| 151 |
|
| 152 |
// Send an auto-reply if necessary using the current language.
|
| 153 |
if ($values['category']['reply']) {
|
| 154 |
drupal_mail('contact', 'page_autoreply', $from, $language, $values, $to);
|
| 155 |
}
|
| 156 |
|
| 157 |
flood_register_event('contact', variable_get('contact_threshold_window', 3600));
|
| 158 |
watchdog('mail', '%sender-name (@sender-from) sent an e-mail regarding %category.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%category' => $values['category']['category']));
|
| 159 |
|
| 160 |
// Jump to home page rather than back to contact page to avoid
|
| 161 |
// contradictory messages if flood control has been activated.
|
| 162 |
drupal_set_message(t('Your message has been sent.'));
|
| 163 |
$form_state['redirect'] = '';
|
| 164 |
}
|
| 165 |
|
| 166 |
/**
|
| 167 |
* Form builder; the personal contact form.
|
| 168 |
*
|
| 169 |
* @see contact_personal_form_validate()
|
| 170 |
* @see contact_personal_form_submit()
|
| 171 |
*/
|
| 172 |
function contact_personal_form($form, &$form_state, stdClass $recipient) {
|
| 173 |
global $user;
|
| 174 |
|
| 175 |
// Check if flood control has been activated for sending e-mails.
|
| 176 |
$limit = variable_get('contact_threshold_limit', 5);
|
| 177 |
$window = variable_get('contact_threshold_window', 3600);
|
| 178 |
if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms') && !user_access('administer users')) {
|
| 179 |
drupal_set_message(t("You cannot send more than %limit messages in @interval. Please try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error');
|
| 180 |
return drupal_access_denied();
|
| 181 |
}
|
| 182 |
|
| 183 |
drupal_set_title(t('Contact @username', array('@username' => format_username($recipient))), PASS_THROUGH);
|
| 184 |
|
| 185 |
if (!$user->uid) {
|
| 186 |
$form['#attached']['library'][] = array('system', 'cookie');
|
| 187 |
$form['#attributes']['class'][] = 'user-info-from-cookie';
|
| 188 |
}
|
| 189 |
|
| 190 |
$form['#token'] = $user->uid ? $user->name . $user->mail : '';
|
| 191 |
$form['recipient'] = array(
|
| 192 |
'#type' => 'value',
|
| 193 |
'#value' => $recipient,
|
| 194 |
);
|
| 195 |
$form['name'] = array(
|
| 196 |
'#type' => 'textfield',
|
| 197 |
'#title' => t('Your name'),
|
| 198 |
'#maxlength' => 255,
|
| 199 |
'#default_value' => $user->uid ? format_username($user) : '',
|
| 200 |
'#required' => TRUE,
|
| 201 |
);
|
| 202 |
$form['mail'] = array(
|
| 203 |
'#type' => 'textfield',
|
| 204 |
'#title' => t('Your e-mail address'),
|
| 205 |
'#maxlength' => 255,
|
| 206 |
'#default_value' => $user->uid ? $user->mail : '',
|
| 207 |
'#required' => TRUE,
|
| 208 |
);
|
| 209 |
$form['to'] = array(
|
| 210 |
'#type' => 'item',
|
| 211 |
'#title' => t('To'),
|
| 212 |
'#markup' => theme('username', array('account' => $recipient)),
|
| 213 |
);
|
| 214 |
$form['subject'] = array(
|
| 215 |
'#type' => 'textfield',
|
| 216 |
'#title' => t('Subject'),
|
| 217 |
'#maxlength' => 50,
|
| 218 |
'#required' => TRUE,
|
| 219 |
);
|
| 220 |
$form['message'] = array(
|
| 221 |
'#type' => 'textarea',
|
| 222 |
'#title' => t('Message'),
|
| 223 |
'#rows' => 15,
|
| 224 |
'#required' => TRUE,
|
| 225 |
);
|
| 226 |
// We do not allow anonymous users to send themselves a copy
|
| 227 |
// because it can be abused to spam people.
|
| 228 |
$form['copy'] = array(
|
| 229 |
'#type' => 'checkbox',
|
| 230 |
'#title' => t('Send yourself a copy.'),
|
| 231 |
'#access' => $user->uid,
|
| 232 |
);
|
| 233 |
$form['submit'] = array(
|
| 234 |
'#type' => 'submit',
|
| 235 |
'#value' => t('Send message'),
|
| 236 |
);
|
| 237 |
return $form;
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Form validation handler for contact_personal_form().
|
| 242 |
*
|
| 243 |
* @see contact_personal_form()
|
| 244 |
*/
|
| 245 |
function contact_personal_form_validate($form, &$form_state) {
|
| 246 |
if (!valid_email_address($form_state['values']['mail'])) {
|
| 247 |
form_set_error('mail', t('You must enter a valid e-mail address.'));
|
| 248 |
}
|
| 249 |
}
|
| 250 |
|
| 251 |
/**
|
| 252 |
* Form submission handler for contact_personal_form().
|
| 253 |
*
|
| 254 |
* @see contact_personal_form()
|
| 255 |
*/
|
| 256 |
function contact_personal_form_submit($form, &$form_state) {
|
| 257 |
global $user, $language;
|
| 258 |
|
| 259 |
$values = $form_state['values'];
|
| 260 |
$values['sender'] = $user;
|
| 261 |
$values['sender']->name = $values['name'];
|
| 262 |
$values['sender']->mail = $values['mail'];
|
| 263 |
|
| 264 |
// Save the anonymous user information to a cookie for reuse.
|
| 265 |
if (!$user->uid) {
|
| 266 |
user_cookie_save($values);
|
| 267 |
}
|
| 268 |
|
| 269 |
// Get the to and from e-mail addresses.
|
| 270 |
$to = $values['recipient']->mail;
|
| 271 |
$from = $values['sender']->mail;
|
| 272 |
|
| 273 |
// Send the e-mail in the requested user language.
|
| 274 |
drupal_mail('contact', 'user_mail', $to, user_preferred_language($values['recipient']), $values, $from);
|
| 275 |
|
| 276 |
// Send a copy if requested, using current page language.
|
| 277 |
if ($values['copy']) {
|
| 278 |
drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
|
| 279 |
}
|
| 280 |
|
| 281 |
flood_register_event('contact', variable_get('contact_threshold_window', 3600));
|
| 282 |
watchdog('mail', '%sender-name (@sender-from) sent %recipient-name an e-mail.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%recipient-name' => $values['recipient']->name));
|
| 283 |
|
| 284 |
// Jump to the contacted user's profile page.
|
| 285 |
drupal_set_message(t('Your message has been sent.'));
|
| 286 |
$form_state['redirect'] = user_access('access user profiles') ? 'user/' . $values['recipient']->uid : '';
|
| 287 |
}
|