Commit 1ebc7112 authored by Matthias Hutterer's avatar Matthias Hutterer
Browse files

port to drupal-6, thanks to klausi for the patch (#235109)

parent 63bf50f2
; $Id$
name = EMail
name = Email
description = Defines an email field type for cck
dependencies = content
dependencies[] = content
package = CCK
core = 6.x
......@@ -12,14 +12,39 @@ function email_help($section) {
}
}
/**
* Implementation of hook_theme().
*/
function email_theme() {
return array(
'email_textfield' => array(
'arguments' => array('element' => NULL),
),
'email_formatter_default' => array(
'arguments' => array('element' => NULL),
),
'email_formatter_spamspan' => array(
'arguments' => array('element' => NULL),
),
'email_formatter_contact' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Implementation of hook_field_info().
*/
function email_field_info() {
return array(
'email' => array('label' => t('E-Mail')),
);
'email' => array(
'label' => 'E-Mail',
'callbacks' => array(
'tables' => CONTENT_CALLBACK_DEFAULT,
'arguments' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
......@@ -29,13 +54,40 @@ function email_field_info() {
function email_field_settings($op, $field) {
switch ($op) {
case 'database columns':
$columns = array(
'email' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => "''"),
);
$columns['email'] = array('type' => 'varchar', 'length' => 255, 'not null' => FALSE);
return $columns;
}
}
function email_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
if (is_array($items)) {
foreach ($items as $delta => $item) {
if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
form_set_error($field['field_name'],t('"%mail" is not a valid email address',array('%mail' => $item['email'])));
}
}
}
break;
case 'sanitize':
foreach ($items as $delta => $item) {
$items[$delta]['nid'] = $node->nid;
}
break;
}
}
/**
* Implementation of hook_content_is_empty().
*/
function email_content_is_empty($item, $field) {
if (empty($item['email'])) {
return TRUE;
}
return FALSE;
}
/**
* Implementation of hook_field_formatter_info().
......@@ -44,60 +96,84 @@ function email_field_settings($op, $field) {
function email_field_formatter_info() {
$formats = array(
'default' => array(
'label' => 'Default Email-Link',
'label' => t('Default email link'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
),
'contact' => array(
'label' => 'Email-Contact Form',
'label' => t('Email contact form'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
if (module_exists('invisimail')) {
$formats['invisi'] = array(
'label' => 'Email-Invisimail',
'field types' => array('email'),
if (module_exists('spamspan')) {
$formats += array(
'spamspan' => array(
'label' => t('Email SpamSpan'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
),
);
}
return $formats;
}
function email_field_formatter($field, $item, $formatter, $node) {
if (empty($item['email'])) {
return '';
/**
* Theme function for 'default' email field formatter.
*/
function theme_email_formatter_default($element) {
return '<a href="mailto:'. $element['#item']['email'] .'">'. check_plain($element['#item']['email']) .'</a>';
}
/**
* Theme function for 'spamspan' email field formatter.
*/
function theme_email_formatter_spamspan($element) {
if (module_exists('spamspan')) {
return spamspan($element['#item']['email']);
}
else {
if ($formatter == 'contact') {
$mailto = l(t('Email Contact Form'), 'email/'.$node->nid.'/'.$field['field_name']);
}
elseif ($formatter == 'invisi' && module_exists('invisimail')) {
$format = $GLOBALS['invisimail_format'];
if (!(variable_get('invisimail_link_'.$format, TRUE))) {
variable_set('invisimail_link_'.$format, TRUE);
variable_set('invisimail_js_'.$format, TRUE);
}
$mailto = invisimail_ascii_encode($item['email']);
}
else {
$mailto = '<a href="mailto:'. $item['email']. '">'. check_plain($item['email']) .'</a>';
}
return $mailto;
return '<a href="mailto:'. $element['#item']['email'] .'">'. check_plain($element['#item']['email']) .'</a>';
}
}
/**
* Theme function for 'contact' email field formatter.
*/
function theme_email_formatter_contact($element) {
return l(t('Email contact form'), 'email/'. $element['#item']['nid'] .'/'. $element['#field_name']);
}
/**
* Implementation of hook_widget_info().
*/
function email_widget_info() {
return array(
'email' => array(
'label' => t('Textfield'),
'email_textfield' => array(
'label' => t('Text field'),
'field types' => array('email'),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_DEFAULT,
),
),
);
}
/**
* Implementation of FAPI hook_elements().
*/
function email_elements() {
return array(
'email_textfield' => array(
'#input' => TRUE,
'#columns' => array('email'),
'#delta' => 0,
'#process' => array('email_textfield_process'),
),
);
}
/**
* Implementation of hook_widget_settings().
......@@ -127,90 +203,62 @@ function email_widget_settings($op, $widget) {
}
}
/**
* Implementation of hook_widget().
*/
function email_widget($op, &$node, $field, &$node_field) {
switch ($op) {
case 'form':
$form = array();
$form[$field['field_name']] = array(
'#tree' => TRUE,
'#weight' => $field['widget']['weight'],
);
if ($field['multiple']) {
$form[$field['field_name']]['#type'] = 'fieldset';
$form[$field['field_name']]['#title'] = t($field['widget']['label']);
foreach (range(0,2) as $delta) {
$default_value = "";
if (isset($field['widget']['default_value'][$delta]['email'])) {
$default_value = $field['widget']['default_value'][$delta]['email'];
}
$form[$field['field_name']][$delta]['email'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => isset($node_field[$delta]['email']) ? $node_field[$delta]['email'] : $default_value,
'#required' => $field['required'] ? $field['required'] : FALSE,
'#maxlength' => 255,
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
'#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
);
}
}
else {
$default_value = "";
if (isset($field['widget']['default_value'][0]['email'])) {
$default_value = $field['widget']['default_value'][0]['email'];
}
$form[$field['field_name']][0]['email'] = array(
'#type' => 'textfield',
'#title' => $field['widget']['label'],
'#default_value' => isset($node_field[0]['email']) ? $node_field[0]['email'] : $default_value,
'#required' => $field['required'] ? $field['required'] : FALSE,
'#maxlength' => 255,
'#size' => isset($field['widget']['size']) ? $field['widget']['size'] : 60,
'#description' => isset($field['widget']['description']) ? $field['widget']['description'] : '',
);
}
return $form;
case 'validate':
if (is_array($node_field)) {
foreach ($node_field as $delta => $item) {
if ($item['email'] != '' && !valid_email_address(trim($item['email']))) {
form_set_error($field['field_name'],t('"%mail" is not a valid email address',array('%mail' => $item['email'])));
}
}
}
break;
}
function email_widget(&$form, &$form_state, $field, $items, $delta = 0) {
$element = array(
'#type' => $field['widget']['type'],
'#default_value' => isset($items[$delta]) ? $items[$delta] : '',
);
return $element;
}
/**
* Process an individual element.
*/
function email_textfield_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
$field_key = $element['#columns'][0];
$delta = $element['#delta'];
$element[$field_key] = array(
'#type' => 'textfield',
'#title' => t($field['widget']['label']),
'#description' => t($field['widget']['description']),
'#required' => $element['#required'],
'#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
);
return $element;
}
/**
* FAPI theme for an individual text elements.
*/
function theme_email_textfield($element) {
return $element['#children'];
}
/**
* Implementation of hook_menu().
*/
function email_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'email',
'title' => t('Email Contact Form'),
'callback' => 'email_mail_page',
'access' => user_access('access content'),
'type' => MENU_CALLBACK,
);
}
function email_menu() {
$items['email/%/%'] = array(
'title' => t('Email Contact Form'),
'page callback' => 'email_mail_page',
'page arguments' => array(1, 2),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* The contact form page.
*/
function email_mail_page($nid=null, $fieldname=null) {
if (empty($nid) || empty($fieldname)) {
function email_mail_page($nid, $fieldname) {
if (!is_numeric($nid)) {
drupal_not_found();
return;
}
......@@ -219,6 +267,7 @@ function email_mail_page($nid=null, $fieldname=null) {
drupal_not_found();
return;
}
$types = content_types($node->type);
// Validate field name
$types = content_types($node->type);
if (!isset($types['fields'][$fieldname]) ||
......@@ -228,54 +277,60 @@ function email_mail_page($nid=null, $fieldname=null) {
drupal_not_found();
return;
}
$field = $node->$fieldname;
if (empty($field) || empty($field[0]['email'])) {
drupal_not_found();
return;
}
if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
$output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
}
else {
$output = drupal_get_form('email_mail_page_form');
$output = drupal_get_form('email_mail_page_form', $node, $field);
}
return $output;
}
function email_mail_page_form() {
function email_mail_page_form($form_state, $node, $field) {
global $user;
if ($user->uid) {
$edit['name'] = $user->name;
$edit['mail'] = $user->mail;
}
$form['#token'] = $user->name . $user->mail;
$form['name'] = array('#type' => 'textfield',
$form['node'] = array(
'#type' => 'value',
'#value' => $node,
);
$form['field'] = array(
'#type' => 'value',
'#value' => $field,
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 255,
'#default_value' => $edit['name'],
'#default_value' => $user->uid ? $user->name : '',
'#required' => TRUE,
);
$form['mail'] = array('#type' => 'textfield',
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $edit['mail'],
'#default_value' => $user->uid ? $user->mail : '',
'#required' => TRUE,
);
$form['subject'] = array('#type' => 'textfield',
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 255,
'#required' => TRUE,
);
$form['message'] = array('#type' => 'textarea',
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
);
$form['submit'] = array('#type' => 'submit',
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send e-mail'),
'#validate' => array('email_mail_page_form_validate'),
'#submit' => array('email_mail_page_form_submit'),
);
return $form;
}
......@@ -283,76 +338,73 @@ function email_mail_page_form() {
/**
* Validate the site-wide contact page form submission.
*/
function email_mail_page_form_validate($form_id, $form_values) {
if (!valid_email_address($form_values['mail'])) {
function email_mail_page_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.'));
}
if (preg_match("/\r|\n/", $form_values['subject'])) {
if (preg_match("/\r|\n/", $form_state['values']['subject'])) {
form_set_error('subject', t('The subject cannot contain linebreaks.'));
watchdog('mail', 'Email injection exploit attempted in email form subject: '.check_plain($form_values['subject']), WATCHDOG_NOTICE);
watchdog('mail', 'Email injection exploit attempted in email form subject: '. check_plain($form_state['values']['subject']), WATCHDOG_NOTICE);
}
}
/**
* Process the site-wide contact page form submission.
*/
function email_mail_page_form_submit($form_id, $edit) {
$nid = intval(arg(1));
$fieldname = arg(2);
if (empty($nid) || empty($fieldname)) {
drupal_not_found();
return;
}
$node = node_load($nid);
if (!$node) {
drupal_not_found();
return;
}
// Validate field name
$types = content_types($node->type);
if (!isset($types['fields'][$fieldname]) ||
$types['fields'][$fieldname]['type'] != 'email') {
drupal_not_found();
return;
}
$field = $node->$fieldname;
if (empty($field) || empty($field[0]['email'])) {
drupal_not_found();
return;
}
function email_mail_page_form_submit($form, &$form_state) {
$node = $form_state['values']['node'];
$field = $form_state['values']['field'];
$email = $field[0]['email'];
$types = content_types($node->type);
// E-mail address of the sender: as the form field is a text field,
// all instances of \r and \n have been automatically stripped from it.
$from = $edit['mail'];
// Compose the body:
$message[] = t("@name sent a message using the contact form at @form.", array('@name' => $edit['name'], '@form' => url($_GET['q'], NULL, NULL, TRUE)));
$message[] = $edit['message'];
// Tidy up the body:
foreach ($message as $key => $value) {
$message[$key] = wordwrap($value);
}
// Format the category:
$subject = t('[@title - @contact] @subject', array('@title' => preg_replace("/\r|\n/",'',$node->title), '@contact' => $types['fields'][$fieldname]['widget']['label'], '@subject' => $edit['subject']));
// Prepare the body:
$body = implode("\n\n", $message);
$from = $form_state['values']['mail'];
$params['node'] = $node;
$params['subject'] = $form_state['values']['subject'];
$params['name'] = $form_state['values']['name'];
$params['message'] = $form_state['values']['message'];
$params['url'] = url('node/' . $node->nid, array('absolute' => TRUE));
// Send the e-mail to the recipients:
drupal_mail($fieldname, $email, $subject, $body, $from);
drupal_mail('email', 'contact', $email, language_default(), $params, $from);
// Log the operation:
flood_register_event('email');
watchdog('mail', t('%name-from sent an e-mail at %form.', array('%name-from' => theme('placeholder', $edit['name'] ." <$from>"), '%form' => url($_GET['q'], NULL, NULL, TRUE))));
watchdog('mail', t('%name-from sent an e-mail at %form.', array('%name-from' => theme('placeholder', $form_state['values']['name'] ." <$from>"), '%form' => url($_GET['q'], array('absolute' => TRUE)))));
// Update user:
drupal_set_message(t('Your message has been sent.'));
// Jump to home page rather than back to contact page to avoid contradictory messages if flood control has been activated.
return 'node/'.$node->nid;
$form_state['redirect'] = 'node/'. $node->nid;
}
/**
* Implementation of hook_mail()
*/
function email_mail($key, &$message, $params) {
$language = $message['language'];
switch($key) {
case 'contact':
$node = $params['node'];
// Compose the body:
$msg[] = t('@name sent a message using the contact form at @node.', array('@name' => $params['name'], '@node' => $params['url']), $language->language);
$msg[] = $params['message'];
// Tidy up the body:
foreach ($msg as $index_key => $value) {
$msg[$index_key] = wordwrap($value);
}
// Prepare the body:
$message['body'] = implode("\n\n", $msg);
$message['subject'] = t('[@title] @subject', array('@title' => preg_replace("/\r|\n/",'', $node->title), '@subject' => $params['subject']), $language->language);
break;
}
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment