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