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