2007-07-16 06:37:49 +00:00
< ? php
// $Id$
/**
* @ file
* User page callbacks for the contact module .
*/
/**
2009-10-11 01:06:27 +00:00
* Form builder ; the site - wide contact form .
2009-10-11 18:34:10 +00:00
*
* @ see contact_site_form_validate ()
* @ see contact_site_form_submit ()
2007-07-16 06:37:49 +00:00
*/
2009-10-11 01:06:27 +00:00
function contact_site_form ( $form , & $form_state ) {
global $user ;
// Check if flood control has been activated for sending e-mails.
2009-10-09 02:34:07 +00:00
$limit = variable_get ( 'contact_threshold_limit' , 5 );
$window = variable_get ( 'contact_threshold_window' , 3600 );
if ( ! flood_is_allowed ( 'contact' , $limit , $window ) && ! user_access ( 'administer contact forms' )) {
2009-10-11 01:06:27 +00:00
drupal_set_message ( t ( " You cannot send more than %limit messages in @interval. Please try again later. " , array ( '%limit' => $limit , '@interval' => format_interval ( $window ))), 'error' );
2009-12-06 23:56:47 +00:00
drupal_access_denied ();
drupal_exit ();
2007-07-16 06:37:49 +00:00
}
2009-10-11 18:34:10 +00:00
2009-10-11 01:06:27 +00:00
// Get an array of the categories and the current default category.
2009-10-16 19:06:25 +00:00
$categories = db_select ( 'contact' , 'c' )
-> addTag ( 'translatable' )
-> fields ( 'c' , array ( 'cid' , 'category' ))
-> orderBy ( 'weight' )
-> orderBy ( 'category' )
-> execute ()
-> fetchAllKeyed ();
2009-10-11 01:06:27 +00:00
$default_category = db_query ( " SELECT cid FROM { contact} WHERE selected = 1 " ) -> fetchField ();
// If there are no categories, do not display the form.
if ( ! $categories ) {
2009-10-09 00:54:33 +00:00
if ( user_access ( 'administer contact forms' )) {
2009-10-11 01:06:27 +00:00
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' );
2009-04-21 09:27:52 +00:00
}
else {
2009-12-06 23:56:47 +00:00
drupal_not_found ();
drupal_exit ();
2009-04-21 09:27:52 +00:00
}
}
2009-03-08 05:08:22 +00:00
2009-10-09 02:34:07 +00:00
// If there is more than one category available and no default category has
// been selected, prepend a default placeholder value.
2009-04-21 09:27:52 +00:00
if ( ! $default_category ) {
2007-07-16 06:37:49 +00:00
if ( count ( $categories ) > 1 ) {
2009-04-21 09:27:52 +00:00
$categories = array ( 0 => t ( '- Please choose -' )) + $categories ;
2007-07-16 06:37:49 +00:00
}
2008-01-16 22:56:21 +00:00
else {
2009-04-21 09:27:52 +00:00
$default_category = key ( $categories );
2008-01-16 22:56:21 +00:00
}
2007-07-16 06:37:49 +00:00
}
2009-04-21 09:27:52 +00:00
2009-10-09 15:39:12 +00:00
if ( ! $user -> uid ) {
$form [ '#attached' ][ 'library' ][] = array ( 'system' , 'cookie' );
2009-10-16 15:54:33 +00:00
$form [ '#attributes' ][ 'class' ][] = 'user-info-from-cookie' ;
2009-10-09 15:39:12 +00:00
}
2009-04-21 09:27:52 +00:00
$form [ 'name' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Your name' ),
'#maxlength' => 255 ,
2009-11-01 21:26:44 +00:00
'#default_value' => $user -> uid ? format_username ( $user ) : '' ,
2009-04-21 09:27:52 +00:00
'#required' => TRUE ,
);
$form [ 'mail' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Your e-mail address' ),
'#maxlength' => 255 ,
'#default_value' => $user -> uid ? $user -> mail : '' ,
'#required' => TRUE ,
);
$form [ 'subject' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Subject' ),
'#maxlength' => 255 ,
'#required' => TRUE ,
);
$form [ 'cid' ] = array (
'#type' => 'select' ,
'#title' => t ( 'Category' ),
'#default_value' => $default_category ,
'#options' => $categories ,
'#required' => TRUE ,
'#access' => count ( $categories ) > 1 ,
);
$form [ 'message' ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Message' ),
'#required' => TRUE ,
);
// We do not allow anonymous users to send themselves a copy
// because it can be abused to spam people.
$form [ 'copy' ] = array (
'#type' => 'checkbox' ,
'#title' => t ( 'Send yourself a copy.' ),
'#access' => $user -> uid ,
);
$form [ 'submit' ] = array (
'#type' => 'submit' ,
2009-05-10 05:06:50 +00:00
'#value' => t ( 'Send message' ),
2009-04-21 09:27:52 +00:00
);
2007-07-16 06:37:49 +00:00
return $form ;
}
/**
2009-05-06 10:38:40 +00:00
* Form validation handler for contact_site_form () .
2007-07-16 06:37:49 +00:00
*/
2009-05-06 10:38:40 +00:00
function contact_site_form_validate ( $form , & $form_state ) {
2007-07-16 06:37:49 +00:00
if ( ! $form_state [ 'values' ][ 'cid' ]) {
2008-02-17 19:29:07 +00:00
form_set_error ( 'cid' , t ( 'You must select a valid category.' ));
2007-07-16 06:37:49 +00:00
}
if ( ! valid_email_address ( $form_state [ 'values' ][ 'mail' ])) {
form_set_error ( 'mail' , t ( 'You must enter a valid e-mail address.' ));
}
}
/**
2009-05-06 10:38:40 +00:00
* Form submission handler for contact_site_form () .
2007-07-16 06:37:49 +00:00
*/
2009-05-06 10:38:40 +00:00
function contact_site_form_submit ( $form , & $form_state ) {
2009-10-09 15:39:12 +00:00
global $user , $language ;
2007-07-16 06:37:49 +00:00
$values = $form_state [ 'values' ];
2009-10-11 18:34:10 +00:00
$values [ 'sender' ] = $user ;
$values [ 'sender' ] -> name = $values [ 'name' ];
$values [ 'sender' ] -> mail = $values [ 'mail' ];
$values [ 'category' ] = contact_load ( $values [ 'cid' ]);
2007-07-16 06:37:49 +00:00
2009-10-09 15:39:12 +00:00
// Save the anonymous user information to a cookie for reuse.
if ( ! $user -> uid ) {
user_cookie_save ( $values );
}
2009-10-11 18:34:10 +00:00
// Get the to and from e-mail addresses.
$to = $values [ 'category' ][ 'recipients' ];
$from = $values [ 'sender' ] -> mail ;
2007-07-16 06:37:49 +00:00
// Send the e-mail to the recipients using the site default language.
2009-10-11 18:34:10 +00:00
drupal_mail ( 'contact' , 'page_mail' , $to , language_default (), $values , $from );
2007-07-16 06:37:49 +00:00
// If the user requests it, send a copy using the current language.
if ( $values [ 'copy' ]) {
drupal_mail ( 'contact' , 'page_copy' , $from , $language , $values , $from );
}
// Send an auto-reply if necessary using the current language.
2009-10-11 18:34:10 +00:00
if ( $values [ 'category' ][ 'reply' ]) {
drupal_mail ( 'contact' , 'page_autoreply' , $from , $language , $values , $to );
2007-07-16 06:37:49 +00:00
}
2009-10-18 11:34:45 +00:00
flood_register_event ( 'contact' , variable_get ( 'contact_threshold_window' , 3600 ));
2009-10-11 18:34:10 +00:00
watchdog ( 'mail' , '%sender-name (@sender-from) sent an e-mail regarding %category.' , array ( '%sender-name' => $values [ 'name' ], '@sender-from' => $from , '%category' => $values [ 'category' ][ 'category' ]));
2007-07-16 06:37:49 +00:00
// Jump to home page rather than back to contact page to avoid
// contradictory messages if flood control has been activated.
2009-10-11 18:34:10 +00:00
drupal_set_message ( t ( 'Your message has been sent.' ));
2007-07-16 06:37:49 +00:00
$form_state [ 'redirect' ] = '' ;
}
/**
2009-10-11 01:06:27 +00:00
* Form builder ; the personal contact form .
2009-10-11 18:34:10 +00:00
*
* @ see contact_personal_form_validate ()
* @ see contact_personal_form_submit ()
2007-07-16 06:37:49 +00:00
*/
2009-12-14 13:51:57 +00:00
function contact_personal_form ( $form , & $form_state , $recipient ) {
2007-07-16 06:37:49 +00:00
global $user ;
2009-10-11 01:06:27 +00:00
// Check if flood control has been activated for sending e-mails.
2009-10-09 02:34:07 +00:00
$limit = variable_get ( 'contact_threshold_limit' , 5 );
$window = variable_get ( 'contact_threshold_window' , 3600 );
2009-10-11 01:06:27 +00:00
if ( ! flood_is_allowed ( 'contact' , $limit , $window ) && ! user_access ( 'administer contact forms' ) && ! user_access ( 'administer users' )) {
drupal_set_message ( t ( " You cannot send more than %limit messages in @interval. Please try again later. " , array ( '%limit' => $limit , '@interval' => format_interval ( $window ))), 'error' );
2009-12-06 23:56:47 +00:00
drupal_access_denied ();
drupal_exit ();
2007-07-16 06:37:49 +00:00
}
2009-10-11 18:34:10 +00:00
2009-11-01 21:26:44 +00:00
drupal_set_title ( t ( 'Contact @username' , array ( '@username' => format_username ( $recipient ))), PASS_THROUGH );
2009-10-11 18:34:10 +00:00
if ( ! $user -> uid ) {
$form [ '#attached' ][ 'library' ][] = array ( 'system' , 'cookie' );
2009-10-16 15:54:33 +00:00
$form [ '#attributes' ][ 'class' ][] = 'user-info-from-cookie' ;
2009-10-11 18:34:10 +00:00
}
2009-10-11 01:06:27 +00:00
$form [ 'recipient' ] = array (
'#type' => 'value' ,
'#value' => $recipient ,
);
2009-10-11 18:34:10 +00:00
$form [ 'name' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Your name' ),
'#maxlength' => 255 ,
2009-11-01 21:26:44 +00:00
'#default_value' => $user -> uid ? format_username ( $user ) : '' ,
2009-10-11 18:34:10 +00:00
'#required' => TRUE ,
);
$form [ 'mail' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Your e-mail address' ),
'#maxlength' => 255 ,
'#default_value' => $user -> uid ? $user -> mail : '' ,
'#required' => TRUE ,
2007-07-16 06:37:49 +00:00
);
2009-10-11 01:06:27 +00:00
$form [ 'to' ] = array (
'#type' => 'item' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'To' ),
2009-10-09 01:00:08 +00:00
'#markup' => theme ( 'username' , array ( 'account' => $recipient )),
2007-07-16 06:37:49 +00:00
);
2009-10-11 01:06:27 +00:00
$form [ 'subject' ] = array (
'#type' => 'textfield' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Subject' ),
'#maxlength' => 50 ,
'#required' => TRUE ,
);
2009-10-11 01:06:27 +00:00
$form [ 'message' ] = array (
'#type' => 'textarea' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Message' ),
'#rows' => 15 ,
'#required' => TRUE ,
);
2009-10-11 18:34:10 +00:00
// We do not allow anonymous users to send themselves a copy
// because it can be abused to spam people.
2009-10-11 01:06:27 +00:00
$form [ 'copy' ] = array (
'#type' => 'checkbox' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Send yourself a copy.' ),
2009-10-11 18:34:10 +00:00
'#access' => $user -> uid ,
2007-07-16 06:37:49 +00:00
);
2009-10-11 01:06:27 +00:00
$form [ 'submit' ] = array (
'#type' => 'submit' ,
2009-05-10 05:06:50 +00:00
'#value' => t ( 'Send message' ),
2007-07-16 06:37:49 +00:00
);
return $form ;
}
2009-10-11 18:34:10 +00:00
/**
* Form validation handler for contact_personal_form () .
*
* @ see contact_personal_form ()
*/
function contact_personal_form_validate ( $form , & $form_state ) {
if ( ! valid_email_address ( $form_state [ 'values' ][ 'mail' ])) {
form_set_error ( 'mail' , t ( 'You must enter a valid e-mail address.' ));
}
}
2007-07-16 06:37:49 +00:00
/**
2009-05-06 10:38:40 +00:00
* Form submission handler for contact_personal_form () .
2009-10-11 18:34:10 +00:00
*
* @ see contact_personal_form ()
2007-07-16 06:37:49 +00:00
*/
2009-05-06 10:38:40 +00:00
function contact_personal_form_submit ( $form , & $form_state ) {
2007-07-16 06:37:49 +00:00
global $user , $language ;
2009-10-11 18:34:10 +00:00
$values = $form_state [ 'values' ];
$values [ 'sender' ] = $user ;
$values [ 'sender' ] -> name = $values [ 'name' ];
$values [ 'sender' ] -> mail = $values [ 'mail' ];
2007-07-16 06:37:49 +00:00
2009-10-11 18:34:10 +00:00
// Save the anonymous user information to a cookie for reuse.
if ( ! $user -> uid ) {
user_cookie_save ( $values );
}
2007-07-16 06:37:49 +00:00
2009-10-11 18:34:10 +00:00
// Get the to and from e-mail addresses.
$to = $values [ 'recipient' ] -> mail ;
$from = $values [ 'sender' ] -> mail ;
2007-07-16 06:37:49 +00:00
// Send the e-mail in the requested user language.
2009-10-11 18:34:10 +00:00
drupal_mail ( 'contact' , 'user_mail' , $to , user_preferred_language ( $values [ 'recipient' ]), $values , $from );
2007-07-16 06:37:49 +00:00
// Send a copy if requested, using current page language.
2009-10-11 18:34:10 +00:00
if ( $values [ 'copy' ]) {
2007-07-16 06:37:49 +00:00
drupal_mail ( 'contact' , 'user_copy' , $from , $language , $values , $from );
}
2009-10-18 11:34:45 +00:00
flood_register_event ( 'contact' , variable_get ( 'contact_threshold_window' , 3600 ));
2009-10-11 18:34:10 +00:00
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 ));
2007-07-16 06:37:49 +00:00
2009-10-11 18:34:10 +00:00
// Jump to the contacted user's profile page.
drupal_set_message ( t ( 'Your message has been sent.' ));
$form_state [ 'redirect' ] = user_access ( 'access user profiles' ) ? 'user/' . $values [ 'recipient' ] -> uid : '' ;
2007-07-16 06:37:49 +00:00
}