2007-07-16 06:37:49 +00:00
< ? php
/**
* @ file
2011-12-05 14:23:20 +00:00
* Page callbacks for the Contact module .
2007-07-16 06:37:49 +00:00
*/
2012-06-04 12:06:09 +00:00
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException ;
2012-06-02 19:41:40 +00:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2007-07-16 06:37:49 +00:00
/**
2012-10-18 23:06:48 +00:00
* Page callback : Form constructor for the site - wide contact form .
*
* @ throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @ throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
2009-10-11 18:34:10 +00:00
*
2011-12-05 14:23:20 +00:00
* @ see contact_menu ()
2009-10-11 18:34:10 +00:00
* @ see contact_site_form_submit ()
2011-12-05 14:23:20 +00:00
* @ ingroup forms
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.
2012-08-01 02:20:36 +00:00
$config = config ( 'contact.settings' );
$limit = $config -> get ( 'flood.limit' );
$interval = $config -> get ( 'flood.interval' );
2012-11-22 11:07:09 +00:00
if ( ! drupal_container () -> get ( 'flood' ) -> isAllowed ( 'contact' , $limit , $interval ) && ! user_access ( 'administer contact forms' )) {
2012-08-01 02:20:36 +00:00
drupal_set_message ( t ( " You cannot send more than %limit messages in @interval. Try again later. " , array ( '%limit' => $limit , '@interval' => format_interval ( $interval ))), 'error' );
2012-06-04 12:06:09 +00:00
throw new AccessDeniedHttpException ();
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.
2012-10-18 08:55:55 +00:00
$categories = entity_load_multiple ( 'contact_category' );
2009-10-11 01:06:27 +00:00
// 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 {
2012-06-02 19:41:40 +00:00
throw new NotFoundHttpException ();
2009-04-21 09:27:52 +00:00
}
}
2009-03-08 05:08:22 +00:00
2012-10-18 08:55:55 +00:00
// Prepare array for select options.
uasort ( $categories , 'Drupal\Core\Config\Entity\ConfigEntityBase::sort' );
$options = array ();
foreach ( $categories as $category ) {
$options [ $category -> id ()] = $category -> label ();
}
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.
2012-10-18 08:55:55 +00:00
$default_category = $config -> get ( 'default_category' );
2009-04-21 09:27:52 +00:00
if ( ! $default_category ) {
2012-10-18 08:55:55 +00:00
$default_category = ! empty ( $categories ) ? key ( $categories ) : NULL ;
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 ) {
2010-11-30 17:16:37 +00:00
$form [ '#attached' ][ 'library' ][] = array ( 'system' , 'jquery.cookie' );
2009-10-16 15:54:33 +00:00
$form [ '#attributes' ][ 'class' ][] = 'user-info-from-cookie' ;
2009-10-09 15:39:12 +00:00
}
2010-10-08 03:36:03 +00:00
$form [ '#attributes' ][ 'class' ][] = 'contact-form' ;
2009-04-21 09:27:52 +00:00
$form [ 'name' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Your name' ),
'#maxlength' => 255 ,
2012-01-16 02:18:26 +00:00
'#default_value' => $user -> uid ? user_format_name ( $user ) : '' ,
2009-04-21 09:27:52 +00:00
'#required' => TRUE ,
);
$form [ 'mail' ] = array (
2012-03-02 07:29:16 +00:00
'#type' => 'email' ,
2009-04-21 09:27:52 +00:00
'#title' => t ( 'Your e-mail address' ),
'#default_value' => $user -> uid ? $user -> mail : '' ,
'#required' => TRUE ,
);
2012-07-28 16:47:04 +00:00
2012-09-11 05:19:03 +00:00
// Do not allow authenticated users to alter the name or e-mail values to
2012-07-28 16:47:04 +00:00
// prevent the impersonation of other users.
2012-09-11 05:19:03 +00:00
if ( $user -> uid ) {
// Hide the original name and e-mail address fields and display read-only
// versions in their place.
$form [ 'name' ][ '#access' ] = $form [ 'mail' ][ '#access' ] = FALSE ;
2012-07-28 16:47:04 +00:00
$form [ 'name_display' ] = array (
'#type' => 'item' ,
'#title' => t ( 'Your name' ),
2012-09-18 03:54:22 +00:00
'#markup' => check_plain ( $form [ 'name' ][ '#default_value' ]),
2012-07-28 16:47:04 +00:00
);
$form [ 'mail_display' ] = array (
'#type' => 'item' ,
'#title' => t ( 'Your e-mail address' ),
2012-09-18 03:54:22 +00:00
'#markup' => check_plain ( $form [ 'mail' ][ '#default_value' ]),
2012-07-28 16:47:04 +00:00
);
}
2009-04-21 09:27:52 +00:00
$form [ 'subject' ] = array (
'#type' => 'textfield' ,
'#title' => t ( 'Subject' ),
'#maxlength' => 255 ,
'#required' => TRUE ,
);
2012-10-18 08:55:55 +00:00
$form [ 'category' ] = array (
2009-04-21 09:27:52 +00:00
'#type' => 'select' ,
'#title' => t ( 'Category' ),
'#default_value' => $default_category ,
2012-10-18 08:55:55 +00:00
'#options' => $options ,
'#empty_value' => 0 ,
2009-04-21 09:27:52 +00:00
'#required' => TRUE ,
'#access' => count ( $categories ) > 1 ,
);
$form [ 'message' ] = array (
'#type' => 'textarea' ,
'#title' => t ( 'Message' ),
'#required' => TRUE ,
);
2012-07-28 16:47:04 +00:00
// Do not allow anonymous users to send themselves a copy because it can be
// abused to spam people.
2009-04-21 09:27:52 +00:00
$form [ 'copy' ] = array (
'#type' => 'checkbox' ,
'#title' => t ( 'Send yourself a copy.' ),
'#access' => $user -> uid ,
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2009-04-21 09:27:52 +00:00
'#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 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 ) {
2012-06-13 02:31:11 +00:00
global $user ;
2012-08-09 20:17:01 +00:00
$language_interface = language ( LANGUAGE_TYPE_INTERFACE );
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' ];
2012-10-18 08:55:55 +00:00
$values [ 'category' ] = entity_load ( 'contact_category' , $values [ 'category' ]);
2007-07-16 06:37:49 +00:00
2009-10-09 15:39:12 +00:00
if ( ! $user -> uid ) {
2012-09-11 05:19:03 +00:00
$values [ 'sender' ] -> name .= ' (' . t ( 'not verified' ) . ')' ;
// Save the anonymous user information to a cookie for reuse.
2010-04-07 16:35:03 +00:00
user_cookie_save ( array_intersect_key ( $values , array_flip ( array ( 'name' , 'mail' ))));
2009-10-09 15:39:12 +00:00
}
2009-10-11 18:34:10 +00:00
// Get the to and from e-mail addresses.
2012-10-18 08:55:55 +00:00
$to = implode ( ', ' , $values [ 'category' ] -> recipients );
2009-10-11 18:34:10 +00:00
$from = $values [ 'sender' ] -> mail ;
2007-07-16 06:37:49 +00:00
// Send the e-mail to the recipients using the site default language.
2012-09-13 08:48:24 +00:00
drupal_mail ( 'contact' , 'page_mail' , $to , language_default () -> langcode , $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' ]) {
2012-09-13 08:48:24 +00:00
drupal_mail ( 'contact' , 'page_copy' , $from , $language_interface -> langcode , $values , $from );
2007-07-16 06:37:49 +00:00
}
// Send an auto-reply if necessary using the current language.
2012-10-18 08:55:55 +00:00
if ( $values [ 'category' ] -> reply ) {
2012-09-13 08:48:24 +00:00
drupal_mail ( 'contact' , 'page_autoreply' , $from , $language_interface -> langcode , $values , $to );
2007-07-16 06:37:49 +00:00
}
2012-11-22 11:07:09 +00:00
drupal_container () -> get ( 'flood' ) -> register ( 'contact' , config ( 'contact.settings' ) -> get ( 'flood.interval' ));
2012-10-18 08:55:55 +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' ] -> label ()));
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' ] = '' ;
}
/**
2012-10-18 23:06:48 +00:00
* Page callback : Form constructor for the personal contact form .
*
* @ param $recipient
* The account for which a personal contact form should be generated .
*
* @ throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
2011-12-05 14:23:20 +00:00
*
* @ see contact_menu ()
2009-10-11 18:34:10 +00:00
* @ see contact_personal_form_submit ()
2012-11-07 15:35:36 +00:00
*
2011-12-05 14:23:20 +00:00
* @ ingroup forms
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.
2012-08-01 02:20:36 +00:00
$config = config ( 'contact.settings' );
$limit = $config -> get ( 'flood.limit' );
$interval = $config -> get ( 'flood.interval' );
2012-11-22 11:07:09 +00:00
if ( ! drupal_container () -> get ( 'flood' ) -> isAllowed ( 'contact' , $limit , $interval ) && ! user_access ( 'administer contact forms' ) && ! user_access ( 'administer users' )) {
2012-08-01 02:20:36 +00:00
drupal_set_message ( t ( " You cannot send more than %limit messages in @interval. Try again later. " , array ( '%limit' => $limit , '@interval' => format_interval ( $interval ))), 'error' );
2012-06-04 12:06:09 +00:00
throw new AccessDeniedHttpException ();
2007-07-16 06:37:49 +00:00
}
2009-10-11 18:34:10 +00:00
2012-01-16 02:18:26 +00:00
drupal_set_title ( t ( 'Contact @username' , array ( '@username' => user_format_name ( $recipient ))), PASS_THROUGH );
2009-10-11 18:34:10 +00:00
if ( ! $user -> uid ) {
2010-11-30 17:16:37 +00:00
$form [ '#attached' ][ 'library' ][] = array ( 'system' , 'jquery.cookie' );
2009-10-16 15:54:33 +00:00
$form [ '#attributes' ][ 'class' ][] = 'user-info-from-cookie' ;
2009-10-11 18:34:10 +00:00
}
2010-10-08 03:36:03 +00:00
$form [ '#attributes' ][ 'class' ][] = 'contact-form' ;
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 ,
2012-01-16 02:18:26 +00:00
'#default_value' => $user -> uid ? user_format_name ( $user ) : '' ,
2009-10-11 18:34:10 +00:00
'#required' => TRUE ,
);
$form [ 'mail' ] = array (
2012-03-02 07:29:16 +00:00
'#type' => 'email' ,
2009-10-11 18:34:10 +00:00
'#title' => t ( 'Your e-mail address' ),
'#default_value' => $user -> uid ? $user -> mail : '' ,
'#required' => TRUE ,
2007-07-16 06:37:49 +00:00
);
2012-07-28 16:47:04 +00:00
// Do not allow authenticated users to alter the name or e-mail values to
// prevent the impersonation of other users.
2012-09-11 05:19:03 +00:00
if ( $user -> uid ) {
// Hide the original name and e-mail address fields and display read-only
// versions in their place.
$form [ 'name' ][ '#access' ] = $form [ 'mail' ][ '#access' ] = FALSE ;
2012-07-28 16:47:04 +00:00
$form [ 'name_display' ] = array (
'#type' => 'item' ,
'#title' => t ( 'Your name' ),
2012-09-18 03:54:22 +00:00
'#markup' => check_plain ( $form [ 'name' ][ '#default_value' ]),
2012-07-28 16:47:04 +00:00
);
$form [ 'mail_display' ] = array (
'#type' => 'item' ,
'#title' => t ( 'Your e-mail address' ),
2012-09-18 03:54:22 +00:00
'#markup' => check_plain ( $form [ 'mail' ][ '#default_value' ]),
2012-07-28 16:47:04 +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 ,
);
2012-07-28 16:47:04 +00:00
// Do not allow anonymous users to send themselves a copy
2009-10-11 18:34:10 +00:00
// 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
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-02-17 08:48:18 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2009-10-11 01:06:27 +00:00
'#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-05-06 10:38:40 +00:00
* Form submission handler for 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 ) {
2012-06-13 02:31:11 +00:00
global $user ;
2012-08-09 20:17:01 +00:00
$language_interface = language ( LANGUAGE_TYPE_INTERFACE );
2007-07-16 06:37:49 +00:00
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
if ( ! $user -> uid ) {
2012-09-11 05:19:03 +00:00
$values [ 'sender' ] -> name .= ' (' . t ( 'not verified' ) . ')' ;
// Save the anonymous user information to a cookie for reuse.
2010-04-07 16:35:03 +00:00
user_cookie_save ( array_intersect_key ( $values , array_flip ( array ( 'name' , 'mail' ))));
2009-10-11 18:34:10 +00:00
}
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.
2012-09-13 08:48:24 +00:00
drupal_mail ( 'contact' , 'user_mail' , $to , user_preferred_langcode ( $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' ]) {
2012-09-13 08:48:24 +00:00
drupal_mail ( 'contact' , 'user_copy' , $from , $language_interface -> langcode , $values , $from );
2007-07-16 06:37:49 +00:00
}
2012-11-22 11:07:09 +00:00
drupal_container () -> get ( 'flood' ) -> register ( 'contact' , config ( 'contact.settings' ) -> get ( 'flood.interval' ));
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
}