contact.pages.inc 9.6 KB

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