| 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 | | */ |
| 13 | 34 | function contact_site_page() { |
| 14 | 23 | global $user; |
| 15 | | |
| 16 | 23 | if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold',
3)) && !user_access('administer site-wide contact form')) { |
| 17 | 1 | $output = t("You cannot send more than %number messages per hour.
Please try again later.", array('%number' =>
variable_get('contact_hourly_threshold', 3))); |
| 18 | 1 | } |
| 19 | | else { |
| 20 | 22 | $output = drupal_get_form('contact_mail_page'); |
| 21 | | } |
| 22 | | |
| 23 | 20 | return $output; |
| 24 | 0 | } |
| 25 | | |
| 26 | 34 | function contact_mail_page() { |
| 27 | 22 | global $user; |
| 28 | | |
| 29 | 22 | $form = $categories = array(); |
| 30 | | |
| 31 | 22 | $result = db_query('SELECT cid, category, selected FROM {contact} ORDER
BY weight, category'); |
| 32 | 22 | while ($category = db_fetch_object($result)) { |
| 33 | 21 | $categories[$category->cid] = $category->category; |
| 34 | 21 | if ($category->selected) { |
| 35 | 0 | $default_category = $category->cid; |
| 36 | 0 | } |
| 37 | 21 | } |
| 38 | | |
| 39 | 22 | if (count($categories) > 0) { |
| 40 | 21 | $form['#token'] = $user->uid ? $user->name . $user->mail : ''; |
| 41 | 21 | $form['contact_information'] = array('#markup' =>
filter_xss_admin(variable_get('contact_form_information', t('You can leave
a message using the contact form below.')))); |
| 42 | 21 | $form['name'] = array('#type' => 'textfield', |
| 43 | 21 | '#title' => t('Your name'), |
| 44 | 21 | '#maxlength' => 255, |
| 45 | 21 | '#default_value' => $user->uid ? $user->name : '', |
| 46 | 21 | '#required' => TRUE, |
| 47 | | ); |
| 48 | 21 | $form['mail'] = array('#type' => 'textfield', |
| 49 | 21 | '#title' => t('Your e-mail address'), |
| 50 | 21 | '#maxlength' => 255, |
| 51 | 21 | '#default_value' => $user->uid ? $user->mail : '', |
| 52 | 21 | '#required' => TRUE, |
| 53 | | ); |
| 54 | 21 | $form['subject'] = array('#type' => 'textfield', |
| 55 | 21 | '#title' => t('Subject'), |
| 56 | 21 | '#maxlength' => 255, |
| 57 | 21 | '#required' => TRUE, |
| 58 | | ); |
| 59 | 21 | 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. |
| 62 | 20 | if (!isset($default_category)) { |
| 63 | 20 | $default_category = t('- Please choose -'); |
| 64 | 20 | $categories = array($default_category) + $categories; |
| 65 | 20 | } |
| 66 | 20 | $form['cid'] = array('#type' => 'select', |
| 67 | 20 | '#title' => t('Category'), |
| 68 | 20 | '#default_value' => $default_category, |
| 69 | 20 | '#options' => $categories, |
| 70 | 20 | '#required' => TRUE, |
| 71 | | ); |
| 72 | 20 | } |
| 73 | | else { |
| 74 | | // If there is only one category, store its cid. |
| 75 | 1 | $category_keys = array_keys($categories); |
| 76 | 1 | $form['cid'] = array('#type' => 'value', |
| 77 | 1 | '#value' => array_shift($category_keys), |
| 78 | | ); |
| 79 | | } |
| 80 | 21 | $form['message'] = array('#type' => 'textarea', |
| 81 | 21 | '#title' => t('Message'), |
| 82 | 21 | '#required' => TRUE, |
| 83 | | ); |
| 84 | | // We do not allow anonymous users to send themselves a copy |
| 85 | | // because it can be abused to spam people. |
| 86 | 21 | if ($user->uid) { |
| 87 | 0 | $form['copy'] = array('#type' => 'checkbox', |
| 88 | 0 | '#title' => t('Send yourself a copy.'), |
| 89 | | ); |
| 90 | 0 | } |
| 91 | | else { |
| 92 | 21 | $form['copy'] = array('#type' => 'value', '#value' => FALSE); |
| 93 | | } |
| 94 | 21 | $form['submit'] = array('#type' => 'submit', |
| 95 | 21 | '#value' => t('Send e-mail'), |
| 96 | | ); |
| 97 | 21 | } |
| 98 | | else { |
| 99 | 1 | 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 | | } |
| 101 | 22 | return $form; |
| 102 | 0 | } |
| 103 | | |
| 104 | | /** |
| 105 | | * Validate the site-wide contact page form submission. |
| 106 | | */ |
| 107 | 34 | function contact_mail_page_validate($form, &$form_state) { |
| 108 | 9 | if (!$form_state['values']['cid']) { |
| 109 | 1 | form_set_error('cid', t('You must select a valid category.')); |
| 110 | 1 | } |
| 111 | 9 | if (!valid_email_address($form_state['values']['mail'])) { |
| 112 | 2 | form_set_error('mail', t('You must enter a valid e-mail address.')); |
| 113 | 2 | } |
| 114 | 9 | } |
| 115 | | |
| 116 | | /** |
| 117 | | * Process the site-wide contact page form submission. |
| 118 | | */ |
| 119 | 34 | function contact_mail_page_submit($form, &$form_state) { |
| 120 | 3 | global $language; |
| 121 | | |
| 122 | 3 | $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. |
| 126 | 3 | $from = $values['mail']; |
| 127 | | |
| 128 | | // Load category properties and save form values for email composition. |
| 129 | 3 | $contact = contact_load($values['cid']); |
| 130 | 3 | $values['contact'] = $contact; |
| 131 | | |
| 132 | | // Send the e-mail to the recipients using the site default language. |
| 133 | 3 | 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. |
| 136 | 3 | if ($values['copy']) { |
| 137 | 0 | drupal_mail('contact', 'page_copy', $from, $language, $values, $from); |
| 138 | 0 | } |
| 139 | | |
| 140 | | // Send an auto-reply if necessary using the current language. |
| 141 | 3 | if ($contact['reply']) { |
| 142 | 3 | drupal_mail('contact', 'page_autoreply', $from, $language, $values,
$contact['recipients']); |
| 143 | 3 | } |
| 144 | | |
| 145 | 3 | flood_register_event('contact'); |
| 146 | 3 | watchdog('mail', '%name-from sent an e-mail regarding %category.',
array('%name-from' => $values['name'] . " [$from]", '%category' =>
$contact['category'])); |
| 147 | 3 | 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. |
| 151 | 3 | $form_state['redirect'] = ''; |
| 152 | 3 | } |
| 153 | | |
| 154 | | /** |
| 155 | | * Personal contact page. |
| 156 | | */ |
| 157 | 34 | function contact_user_page($account) { |
| 158 | 9 | global $user; |
| 159 | | |
| 160 | 9 | if (!valid_email_address($user->mail)) { |
| 161 | 0 | $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())))); |
| 162 | 0 | } |
| 163 | 9 | elseif (!flood_is_allowed('contact',
variable_get('contact_hourly_threshold', 3)) && !user_access('administer
site-wide contact form')) { |
| 164 | 1 | $output = t("You cannot send more than %number messages per hour.
Please try again later.", array('%number' =>
variable_get('contact_hourly_threshold', 3))); |
| 165 | 1 | } |
| 166 | | else { |
| 167 | 8 | drupal_set_title($account->name); |
| 168 | 8 | $output = drupal_get_form('contact_mail_user', $account); |
| 169 | | } |
| 170 | | |
| 171 | 5 | return $output; |
| 172 | 0 | } |
| 173 | | |
| 174 | 34 | function contact_mail_user(&$form_state, $recipient) { |
| 175 | 8 | global $user; |
| 176 | 8 | $form['#token'] = $user->name . $user->mail; |
| 177 | 8 | $form['recipient'] = array('#type' => 'value', '#value' => $recipient); |
| 178 | 8 | $form['from'] = array('#type' => 'item', |
| 179 | 8 | '#title' => t('From'), |
| 180 | 8 | '#markup' => check_plain($user->name) . ' <' .
check_plain($user->mail) . '>', |
| 181 | | ); |
| 182 | 8 | $form['to'] = array('#type' => 'item', |
| 183 | 8 | '#title' => t('To'), |
| 184 | 8 | '#markup' => check_plain($recipient->name), |
| 185 | | ); |
| 186 | 8 | $form['subject'] = array('#type' => 'textfield', |
| 187 | 8 | '#title' => t('Subject'), |
| 188 | 8 | '#maxlength' => 50, |
| 189 | 8 | '#required' => TRUE, |
| 190 | | ); |
| 191 | 8 | $form['message'] = array('#type' => 'textarea', |
| 192 | 8 | '#title' => t('Message'), |
| 193 | 8 | '#rows' => 15, |
| 194 | 8 | '#required' => TRUE, |
| 195 | | ); |
| 196 | 8 | $form['copy'] = array('#type' => 'checkbox', |
| 197 | 8 | '#title' => t('Send yourself a copy.'), |
| 198 | | ); |
| 199 | 8 | $form['submit'] = array('#type' => 'submit', |
| 200 | 8 | '#value' => t('Send e-mail'), |
| 201 | | ); |
| 202 | 8 | return $form; |
| 203 | 0 | } |
| 204 | | |
| 205 | | /** |
| 206 | | * Process the personal contact page form submission. |
| 207 | | */ |
| 208 | 34 | function contact_mail_user_submit($form, &$form_state) { |
| 209 | 4 | global $user, $language; |
| 210 | | |
| 211 | 4 | $account = $form_state['values']['recipient']; |
| 212 | | |
| 213 | | // Send from the current user to the requested user. |
| 214 | 4 | $to = $account->mail; |
| 215 | 4 | $from = $user->mail; |
| 216 | | |
| 217 | | // Save both users and all form values for email composition. |
| 218 | 4 | $values = $form_state['values']; |
| 219 | 4 | $values['account'] = $account; |
| 220 | 4 | $values['user'] = $user; |
| 221 | | |
| 222 | | // Send the e-mail in the requested user language. |
| 223 | 4 | drupal_mail('contact', 'user_mail', $to,
user_preferred_language($account), $values, $from); |
| 224 | | |
| 225 | | // Send a copy if requested, using current page language. |
| 226 | 4 | if ($form_state['values']['copy']) { |
| 227 | 0 | drupal_mail('contact', 'user_copy', $from, $language, $values, $from); |
| 228 | 0 | } |
| 229 | | |
| 230 | 4 | flood_register_event('contact'); |
| 231 | 4 | watchdog('mail', '%name-from sent %name-to an e-mail.',
array('%name-from' => $user->name, '%name-to' => $account->name)); |
| 232 | 4 | drupal_set_message(t('The message has been sent.')); |
| 233 | | |
| 234 | | // Back to the requested users profile page. |
| 235 | 4 | $form_state['redirect'] = "user/$account->uid"; |
| 236 | 4 | } |
| 237 | 34 | |