View source
<?php
include_once 'webform_confirm_email.admin.inc';
function webform_confirm_email_webform_submission_delete($node, $submission) {
db_query('DELETE FROM {webform_confirm_email_code} ' . 'WHERE nid = %d ' . 'AND sid = %d ', $node->nid, $submission->sid);
}
function webform_confirm_email_is_confirmation_request($nid, $eid) {
$obj = db_fetch_object(db_query('SELECT nid ' . 'FROM {webform_confirm_email} ' . 'WHERE nid = %d ' . 'AND eid = %d ', (int) $nid, (int) $eid));
return is_object($obj);
}
function webform_confirm_email_generate_key($nid, $sid, $eid) {
return hash_hmac('md5', serialize(array(
$nid,
$sid,
$eid,
)), drupal_get_private_key());
}
function webform_confirm_email_mail_alter(&$message) {
if (!empty($message['params']['submission'])) {
if (webform_confirm_email_is_confirmation_request($message['params']['email']['nid'], $message['params']['email']['eid']) == TRUE) {
$eid = (int) $message['params']['email']['eid'];
$nid = (int) $message['params']['email']['nid'];
$sid = (int) $message['params']['submission']->sid;
$code = webform_confirm_email_generate_key($nid, $sid, $eid);
$confirm_url = url("node/{$nid}/sid/{$sid}/eid/{$eid}/confirm_email/{$code}", array(
'absolute' => TRUE,
'external' => FALSE,
));
foreach ($message['body'] as $i => $b) {
$message['body'][$i] = str_replace('%confirm_url', $confirm_url, $b);
}
$query = db_query('SELECT eid ' . 'FROM {webform_confirm_email_confirmation_emails} ' . 'WHERE nid = %d ', $nid);
$confirmation_emails = array();
while ($conf_eid = db_fetch_array($query)) {
$conf_eid = array_shift($conf_eid);
$confirmation_emails[$conf_eid] = _webform_confirm_email_build_confirmation_email($nid, $sid, webform_confirmation_email_load($conf_eid, $nid));
}
$obj = array(
'nid' => $nid,
'sid' => $sid,
'eid' => $eid,
'code' => $code,
'confirmation_emails' => serialize($confirmation_emails),
);
drupal_write_record('webform_confirm_email_code', $obj);
}
}
}
function _webform_confirm_email_build_confirmation_email($nid, $sid, $email) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
$node = node_load($nid);
$submission = webform_get_submission($nid, $sid);
$email['html'] = $email['html'] && webform_email_html_capable();
if ($email['template'] == 'default') {
$email['message'] = theme(array(
'webform_mail_' . $node->nid,
'webform_mail',
'webform_mail_message',
), $node, $submission, $email);
}
else {
$email['message'] = $email['template'];
}
$email['message'] = _webform_filter_values($email['message'], $node, $submission, $email, FALSE, TRUE);
$email['headers'] = theme(array(
'webform_mail_headers_' . $nid,
'webform_mail_headers',
), $node, $submission, $email);
if (isset($email['headers']['From'])) {
$email['from'] = $email['headers']['From'];
unset($email['headers']['From']);
}
else {
$email['from'] = webform_format_email_address($email['from_address'], $email['from_name'], $node, $submission);
}
if (isset($email['headers']['Subject'])) {
$email['subject'] = $email['headers']['Subject'];
unset($email['headers']['Subject']);
}
else {
$email['subject'] = webform_format_email_subject($email['subject'], $node, $submission);
}
if (isset($email['headers']['To'])) {
$email['email'] = $email['headers']['To'];
unset($email['headers']['To']);
}
$addresses = array_filter(explode(',', $email['email']));
$addresses_final = array();
foreach ($addresses as $address) {
$address = trim($address);
$address = webform_format_email_address($address, NULL, $node, $submission, TRUE, FALSE, 'short');
if (is_array($address)) {
foreach ($address as $new_address) {
$new_address = trim($new_address);
if (valid_email_address($new_address)) {
$addresses_final[] = $new_address;
}
}
}
elseif (valid_email_address($address)) {
$addresses_final[] = $address;
}
}
$messages = array();
foreach ($addresses_final as $address) {
$message = array();
if (_webform_submission_spam_check($address, $email['subject'], $email['from'], $email['headers'])) {
continue;
}
$language = $user->uid ? user_preferred_language($user) : language_default();
$email['eid'] = 0;
$mail_params = array(
'message' => $email['message'],
'subject' => $email['subject'],
'headers' => $email['headers'],
'node' => $node,
'submission' => $submission,
'email' => $email,
);
if (webform_email_html_capable()) {
$attachments = array();
if ($email['attachments']) {
webform_component_include('file');
foreach ($node->webform['components'] as $component) {
if (webform_component_feature($component['type'], 'attachment') && !empty($submission->data[$component['cid']]['value'][0])) {
if (webform_component_implements($component['type'], 'attachments')) {
$files = webform_component_invoke($component['type'], 'attachments', $component, $submission->data[$component['cid']]['value']);
if ($files) {
$attachments = array_merge($attachments, $files);
}
}
}
}
}
$mail_params['attachments'] = $attachments;
$message['html_capable'] = drupal_mail('webform', 'submission', $address, $language, $mail_params, $email['from'], FALSE);
$message['html_capable']['html'] = !$email['html'];
}
$message['module'] = 'webform';
$message['key'] = 'submission';
$message['to'] = $address;
$message['language'] = $language;
$message['params'] = $mail_params;
$message['from'] = $email['from'];
$messages[] = $message;
}
return $messages;
}
function _webform_confirm_email_confirmed_form_submit($form, &$form_state) {
$nid = (int) $form['nid']['#value'];
$sid = (int) $form['sid']['#value'];
$conf_request_eid = (int) $form['eid']['#value'];
$form_state['redirect'] = 'node/' . $nid;
$emails = unserialize(db_result(db_query('SELECT confirmation_emails ' . 'FROM {webform_confirm_email_code} ' . 'WHERE nid = %d ' . 'AND sid = %d ' . 'AND eid = %d ', $nid, $sid, $conf_request_eid)));
$emails = unserialize($emails);
if (isset($emails) == TRUE) {
foreach ($emails as $confirmation_eid => $messages) {
foreach ($messages as $index => $message) {
if (webform_email_html_capable() && isset($message['html_capable']) == TRUE) {
$html_message = $message['html_capable'];
$result = mimemail($html_message['from'], $html_message['to'], $html_message['subject'], $html_message['body'], $html_message['html'], $html_message['headers'], $html_message['html'] ? $html_message['body'] : NULL, $html_message['params']['attachments'], 'webform');
if (is_array($result) == TRUE && $result['result'] != FALSE || is_array($result) == FALSE && $result != FALSE) {
unset($messages[$index]);
}
else {
watchdog('mail', 'Error sending e-mail (from %from to %to).', array(
'%from' => $html_message['from'],
'%to' => $html_message['to'],
), WATCHDOG_ERROR);
drupal_set_message(t('Unable to send e-mail. Please contact the site administrator if the problem persists.'), 'error');
}
}
else {
$result = drupal_mail($message['module'], $message['key'], $message['to'], $message['language'], $message['params'], $message['from']);
if ($result['result'] == TRUE) {
unset($messages[$index]);
}
else {
watchdog('mail', 'Error sending e-mail (from %from to %to).', array(
'%from' => $message['from'],
'%to' => $message['to'],
), WATCHDOG_ERROR);
drupal_set_message(t('Unable to send e-mail. Please contact the site administrator if the problem persists.'), 'error');
}
}
}
if (count($messages) == 0) {
unset($emails[$confirmation_eid]);
}
}
}
if (count($emails) == 0) {
db_query('DELETE FROM {webform_confirm_email_code} ' . 'WHERE nid = %d ' . 'AND sid = %d ' . 'AND eid = %d ', $nid, $sid, $conf_request_eid);
}
}
function webform_confirm_email_response_form($form_state, $nid, $sid, $eid, $code) {
$nid = (int) $nid;
$sid = (int) $sid;
$eid = (int) $eid;
if (empty($code) == TRUE || webform_confirm_email_generate_key($nid, $sid, $eid) != $code) {
return drupal_not_found();
}
$form = array(
'nid' => array(
'#type' => 'value',
'#value' => $nid,
),
'sid' => array(
'#type' => 'value',
'#value' => $sid,
),
'eid' => array(
'#type' => 'value',
'#value' => $eid,
),
'#submit' => array(
'_webform_confirm_email_confirmed_form_submit',
),
);
$question = t('Confirm your email address?');
$description = t('You are here to confirm your email address to complete a form submission ' . 'process. You can acknowledge this was you, click no to report the abuse ' . 'of your email address or ignore this form knowing no email messages ' . 'were sent from our site in your name.');
$report_abuse_url = 'node/' . $nid . '/report_abuse/' . $sid . "/{$code}";
return confirm_form($form, $question, $report_abuse_url, $description, t('Yes, that was me.'), t('No, someone is using my email address without my knowledge.'));
}
function webform_confirm_email_menu() {
return array(
'node/%/sid/%/eid/%/confirm_email' => array(
'title' => 'Submit email confirmation',
'load arguments' => array(
1,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'webform_confirm_email_response_form',
1,
3,
5,
7,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
'node/%webform_menu/webform/confirmation_request/%webform_menu_email' => array(
'title' => t('Edit confirmation request e-mail settings'),
'load arguments' => array(
1,
),
'page callback' => 'webform_confirm_email_confirmation_request_email_edit',
'page arguments' => array(
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'type' => MENU_CALLBACK,
),
'node/%webform_menu/webform/confirmation_request/%webform_menu_email/delete' => array(
'title' => t('Delete a confirmation request e-mail'),
'load arguments' => array(
1,
),
'page callback' => 'webform_confirm_email_confirmation_request_email_delete',
'page arguments' => array(
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'type' => MENU_CALLBACK,
),
'node/%webform_menu/webform/confirmation/%webform_confirmation_email' => array(
'title' => t('Edit confirmation e-mail settings'),
'load arguments' => array(
1,
),
'page callback' => 'webform_confirm_email_confirmation_email_edit',
'page arguments' => array(
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'type' => MENU_CALLBACK,
),
'node/%webform_menu/webform/confirmation/%webform_confirmation_email/delete' => array(
'title' => t('Delete a confirmation request e-mail'),
'load arguments' => array(
1,
),
'page callback' => 'webform_confirm_email_confirmation_email_delete',
'page arguments' => array(
1,
4,
),
'access callback' => 'node_access',
'access arguments' => array(
'update',
1,
),
'type' => MENU_CALLBACK,
),
'node/%webform_menu/report_abuse/%webform_menu_submission/%' => array(
'title' => t('Report abuse regarding email confirmation'),
'load arguments' => array(
1,
),
'page callback' => 'webform_confirm_email_report_abuse',
'page arguments' => array(
1,
3,
4,
),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
),
);
}
function webform_confirm_email_report_abuse() {
return t('Sorry, not implemented yet. Try <a href="/contact">contact</a>.');
}