View source
<?php
require_once drupal_get_path('module', 'print') . '/print.pages.inc';
function print_mail_form() {
global $user;
$print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
if (!user_access('administer print') && !flood_is_allowed('print_mail', $print_mail_hourly_threshold)) {
$form['flood'] = array(
'#type' => 'markup',
'#value' => '<p>' . t('You cannot send more than %number messages per hour. Please try again later.', array(
'%number' => $print_mail_hourly_threshold,
)) . '</p>',
);
return $form;
}
$print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
$print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
$form = array();
$path = explode('/', $_GET['q']);
unset($path[0]);
$path = implode('/', $path);
if (is_numeric($path)) {
$path = 'node/' . $path;
}
$cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
$title = _print_get_title($path);
$nodepath = drupal_get_normal_path($path);
db_query("UPDATE {print_mail_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
if (!db_affected_rows()) {
db_query("INSERT INTO {print_mail_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
}
$form['path'] = array(
'#type' => 'value',
'#value' => $path,
);
$form['cid'] = array(
'#type' => 'value',
'#value' => $cid,
);
$form['title'] = array(
'#type' => 'value',
'#value' => $title,
);
$form['fld_from_addr'] = array(
'#type' => 'textfield',
'#title' => t('Your e-mail'),
'#size' => 62,
'#required' => TRUE,
);
$form['fld_from_name'] = array(
'#type' => 'textfield',
'#title' => t('Your name'),
'#size' => 62,
);
$form['txt_to_addrs'] = array(
'#type' => 'textarea',
'#title' => t('Send to'),
'#rows' => 3,
'#resizable' => FALSE,
'#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
'#required' => TRUE,
);
$form['fld_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
'#size' => 62,
'#required' => TRUE,
);
if (!empty($title)) {
$form['fld_title'] = array(
'#type' => 'item',
'#title' => t('Page to be sent'),
'#value' => l($title, $path, array(
'title' => t('View page'),
), NULL, NULL, FALSE, FALSE),
);
}
$form['fld_send_option'] = array(
'#type' => 'select',
'#title' => t('Send page as'),
'#default_value' => variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT),
'#options' => array(
'sendlink' => t('Link'),
'sendpage' => t('Inline HTML'),
),
);
$form['txt_message'] = array(
'#type' => 'textarea',
'#title' => t('Your message'),
'#rows' => 6,
'#required' => TRUE,
);
if ($print_mail_teaser_choice) {
$form['chk_teaser'] = array(
'#type' => 'checkbox',
'#title' => t('Send only the teaser'),
'#default_value' => $print_mail_teaser_default,
);
}
else {
$form['chk_teaser'] = array(
'#type' => 'value',
'#value' => $print_mail_teaser_default,
);
}
$form['btn_submit'] = array(
'#name' => 'submit',
'#type' => 'submit',
'#value' => t('Send e-mail'),
);
$form['btn_clear'] = array(
'#type' => 'markup',
'#value' => '<input type="reset" name="clear" value="' . t('Clear form') . '" class="form-submit" /> ',
);
$form['btn_cancel'] = array(
'#name' => 'cancel',
'#type' => 'submit',
'#value' => t('Cancel'),
);
if ($user->uid != 0) {
$user_name = check_plain(strip_tags(theme('username', $user)));
$form['fld_from_addr']['#default_value'] = $user->mail;
$form['fld_from_addr']['#disabled'] = TRUE;
$form['fld_from_addr']['#value'] = $user->mail;
$form['fld_from_name']['#default_value'] = $user_name;
}
else {
$user_name = t('Someone');
}
$site_name = variable_get('site_name', t('an interesting site'));
$print_mail_text_subject = filter_xss(variable_get('print_mail_text_subject', t('!user has sent you a message from !site')));
$form['fld_subject']['#default_value'] = t($print_mail_text_subject, array(
'!user' => $user_name,
'!site' => $site_name,
'!title' => $title,
));
$print_mail_text_content = filter_xss(variable_get('print_mail_text_content', ''));
$form['txt_message']['#default_value'] = t($print_mail_text_content);
return $form;
}
function theme_print_mail_form($form) {
drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
$content = '';
foreach (element_children($form) as $key) {
$tmp = drupal_render($form[$key]);
switch ($key) {
case 'fld_from_addr':
case 'fld_from_name':
case 'txt_to_addrs':
case 'fld_subject':
case 'fld_title':
case 'fld_send_option':
$tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
break;
}
$content .= $tmp;
}
return $content;
}
function print_mail_form_validate($form_id, $form_values, $form) {
if (array_key_exists('cancel', $form['#post'])) {
return;
}
$from_addr = trim($form_values['fld_from_addr']);
$test = user_validate_mail($from_addr);
if ($test) {
form_set_error('fld_from_addr', $test);
}
$to_addrs = preg_replace('![\\r|\\n|,]+!', ',', trim($form_values['txt_to_addrs']));
$to_array = explode(',', $to_addrs);
foreach ($to_array as $key => $address) {
$address = trim($address);
if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
$test = user_validate_mail($matches[2]);
$to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
}
else {
$test = user_validate_mail($address);
}
if ($test) {
form_set_error('txt_to_addrs', $test);
}
}
$print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
if (!user_access('administer print') && !flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1)) {
form_set_error('txt_to_addrs', t('You cannot send more than %number messages per hour. Please reduce the number of recipients.', array(
'%number' => $print_mail_hourly_threshold,
)));
}
foreach ($form_values as $key => $string) {
if (substr($key, 0, 4) == 'fld_' && (strpos($string, "\n") !== FALSE || strpos($string, "\r") !== FALSE)) {
form_set_error($key, 'Found invalid character');
}
}
form_set_value($form['fld_from_addr'], $from_addr);
form_set_value($form['fld_from_name'], trim($form_values['fld_from_name']));
form_set_value($form['txt_to_addrs'], implode(', ', $to_array));
}
function print_mail_form_submit($form_id, $form_values) {
if (!array_key_exists('cancel', $form_values)) {
if (!empty($form_values['fld_from_name'])) {
$from = '"' . $form_values['fld_from_name'] . '" <' . $form_values['fld_from_addr'] . '>';
}
else {
$from = $form_values['fld_from_addr'];
}
$cid = isset($form_values['cid']) ? $form_values['cid'] : NULL;
$print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
$sender_message = $print_mail_text_message . ':<br /><br /><em>' . nl2br(check_plain($form_values['txt_message'])) . '</em>';
$print = print_controller($form_values['path'], $cid, PRINT_MAIL_FORMAT, $form_values['chk_teaser'], $sender_message);
$pattern = '!<(img\\s[^>]*?)>!is';
$print['content'] = preg_replace_callback($pattern, '_print_mail_encode_urls', $print['content']);
if ($print !== FALSE) {
$params = array();
$params['subject'] = $form_values['fld_subject'];
$params['message'] = $form_values['txt_message'];
$params['link'] = $print['url'];
$params['title'] = $form_values['title'];
$headers = array(
'Content-Type' => 'text/html; charset=utf-8',
);
$node = $print['node'];
ob_start();
include_once _print_get_template(PRINT_MAIL_FORMAT, $print['type']);
$params['body'] = ob_get_contents();
ob_end_clean();
$params['body'] = drupal_final_markup($params['body']);
switch ($form_values['fld_send_option']) {
case 'sendpage':
break;
case 'sendlink':
$sendlink_html = $params['message'] . '<br/><br/>' . l($params['title'], $params['link']);
$params['body'] = $sendlink_html;
break;
}
$ok = FALSE;
if (function_exists('job_queue_add') && variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT)) {
$use_job_queue = TRUE;
$this_file = drupal_get_path('module', 'print_mail') . '/print_mail.inc';
}
else {
$use_job_queue = FALSE;
}
$addresses = explode(', ', $form_values['txt_to_addrs']);
foreach ($addresses as $to) {
$can_send = module_invoke_all('print_mail_before_send', $node, $to, $from, $params);
if (!in_array(FALSE, $can_send)) {
if ($use_job_queue) {
job_queue_add('drupal_mail', t('print_mail: From %from', array(
'%from' => $from,
)), array(
'print_mail_sendpage',
$to,
$params['subject'],
$params['body'],
$from,
$headers,
), $this_file, TRUE);
}
else {
$ret = drupal_mail('print_mail_sendpage', $to, $params['subject'], $params['body'], $from, $headers);
}
if ($ret || $use_job_queue) {
module_invoke_all('print_mail_after_send', $node, $to, $from, $params);
flood_register_event('print_mail');
$ok = TRUE;
}
}
}
if ($ok) {
watchdog('print_mail', t('%name [%from] sent %page to [%to]', array(
'%name' => $form_values['fld_from_name'],
'%from' => $form_values['fld_from_addr'],
'%page' => $form_values['path'],
'%to' => $form_values['txt_to_addrs'],
)));
$site_name = variable_get('site_name', t('us'));
$print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
drupal_set_message(check_plain(t($print_mail_text_confirmation, array(
'!site' => $site_name,
))));
$nodepath = drupal_get_normal_path($form_values['path']);
db_query("UPDATE {print_mail_page_counter} SET sentcount = sentcount + %d, sent_timestamp = %d WHERE path = '%s'", count($addresses), time(), $nodepath);
}
}
}
return preg_replace('!^book/export/html/!', 'node/', $form_values['path']);
}
function _print_mail_encode_urls($matches) {
$pattern = '!\\s*(\\w+\\s*=\\s*"(?:\\\\"|[^"])*")\\s*|\\s*(\\w+\\s*=\\s*\'(?:\\\\\'|[^\'])*\')\\s*|\\s*(\\w+\\s*=\\s*\\w+)\\s*|\\s+!';
$attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach ($attribs as $key => $value) {
$attribs[$key] = preg_replace('!(\\w)\\s*=\\s*(.*)!', '$1=$2', $value);
}
$size = count($attribs);
for ($i = 1; $i < $size; $i++) {
if (preg_match('!^(?:href|src)\\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
$url = trim($urls[1], " \t\n\r\0\v\"'");
$new_url = str_replace(' ', '%20', $url);
$matches[1] = str_replace($url, $new_url, $matches[1]);
}
}
$ret = '<' . $matches[1] . '>';
if (count($matches) == 4) {
$ret .= $matches[2] . $matches[3];
}
return $ret;
}