View source
<?php
function email_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Defines a field type for email addresses. <em>Note: Requires content.module.</em>');
}
}
function email_field_info() {
return array(
'email' => array(
'label' => t('E-Mail'),
),
);
}
function email_field_settings($op, $field) {
switch ($op) {
case 'database columns':
$columns = array(
'email' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => "''",
),
);
return $columns;
}
}
function email_field_formatter_info() {
$formats = array(
'default' => array(
'label' => 'Default Email-Link',
'field types' => array(
'email',
),
),
'contact' => array(
'label' => 'Email-Contact Form',
'field types' => array(
'email',
),
),
);
if (module_exists('invisimail')) {
$formats['invisi'] = array(
'label' => 'Email-Invisimail',
'field types' => array(
'email',
),
);
}
return $formats;
}
function email_field_formatter($field, $item, $formatter, $node) {
if (empty($item['email'])) {
return '';
}
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;
}
}
function email_widget_info() {
return array(
'email' => array(
'label' => t('Textfield'),
'field types' => array(
'email',
),
),
);
}
function email_widget_settings($op, $widget) {
switch ($op) {
case 'form':
$form = array();
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size'),
'#default_value' => isset($widget['size']) ? $widget['size'] : 60,
'#required' => FALSE,
'#description' => t('Size of textfield'),
);
return $form;
case 'validate':
if (!empty($widget['size']) && (!is_numeric($widget['size']) || intval($widget['size']) != $widget['size'] || $widget['size'] <= 0)) {
form_set_error('size', t('"Size" must be a positive integer.'));
}
break;
case 'save':
return array(
'size',
);
}
}
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_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,
);
}
return $items;
}
function email_mail_page($nid = null, $fieldname = null) {
if (empty($nid) || empty($fieldname)) {
drupal_not_found();
return;
}
$node = node_load(intval($nid));
if (!$node) {
drupal_not_found();
return;
}
$types = content_types($node->type);
if (!isset($types['fields'][$fieldname]) || $types['fields'][$fieldname]['type'] != 'email' || $types['fields'][$fieldname]['display_settings']['teaser']['format'] != 'contact' && $types['fields'][$fieldname]['display_settings']['full']['format'] != 'contact') {
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');
}
return $output;
}
function email_mail_page_form() {
global $user;
if ($user->uid) {
$edit['name'] = $user->name;
$edit['mail'] = $user->mail;
}
$form['#token'] = $user->name . $user->mail;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 255,
'#default_value' => $edit['name'],
'#required' => TRUE,
);
$form['mail'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $edit['mail'],
'#required' => TRUE,
);
$form['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 255,
'#required' => TRUE,
);
$form['message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send e-mail'),
);
return $form;
}
function email_mail_page_form_validate($form_id, $form_values) {
if (!valid_email_address($form_values['mail'])) {
form_set_error('mail', t('You must enter a valid e-mail address.'));
}
if (preg_match("/\r|\n/", $form_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);
}
}
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;
}
$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;
}
$email = $field[0]['email'];
$from = $edit['mail'];
$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'];
foreach ($message as $key => $value) {
$message[$key] = wordwrap($value);
}
$subject = t('[@title - @contact] @subject', array(
'@title' => preg_replace("/\r|\n/", '', $node->title),
'@contact' => $types['fields'][$fieldname]['widget']['label'],
'@subject' => $edit['subject'],
));
$body = implode("\n\n", $message);
drupal_mail($fieldname, $email, $subject, $body, $from);
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),
)));
drupal_set_message(t('Your message has been sent.'));
return 'node/' . $node->nid;
}
function email_token_list($type = 'all') {
if ($type == 'field' || $type == 'all') {
$tokens['email']['email'] = t("Email Address");
return $tokens;
}
}
function email_token_values($type, $object = NULL, $options = array()) {
if ($type == 'field') {
$item = $object[0];
$tokens['email'] = check_plain($item['email']);
return $tokens;
}
}