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