Issue #1047718: add id tag to clear button in mail submission form
[project/print.git] / print_mail / print_mail.inc
CommitLineData
7be5aba1 1<?php
3483245d
JV
2
3/**
4 * @file
5 * Displays and processes the mail send form.
6 *
7 * This file is included by the print_mail module and includes the
8 * mail form display, validation and submit hooks.
d08475b8
JV
9 *
10 * @ingroup print
3483245d 11 */
7be5aba1 12
f03e3602 13require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
7be5aba1 14
65cf1df9
JV
15// Include MIME library
16@include_once('Mail/mime.php');
17
0706a465
JV
18/**
19 * Menu callback for the send by e-mail form.
20 *
21 * @ingroup forms
22 */
c933f342 23function print_mail_form($form, &$form_state) {
7be5aba1 24 global $user;
0d9893f9
JV
25
26 $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
27
f03e3602 28 if ((!user_access('administer print')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold))) {
0d9893f9
JV
29 $form['flood'] = array(
30 '#type' => 'markup',
6678a30b 31 '#markup' => '<p>' . format_plural($print_mail_hourly_threshold, 'You cannot send more than 1 message per hour. Please try again later.', 'You cannot send more than @count messages per hour. Please try again later.') . '</p>',
0d9893f9
JV
32 );
33 return $form;
34 }
35
7d62e7c4
JV
36 $print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
37 $print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
7be5aba1
JV
38 $form = array();
39
40 // Remove the printmail/ prefix
bb0645f8
JV
41 $path = explode('/', $_GET['q']);
42 unset($path[0]);
43 $path = implode('/', $path);
55a8c392
JV
44 if (ctype_digit($path)) {
45 if (drupal_lookup_path('source', $path)) {
46 // This is a numeric alias
47 $path = drupal_get_normal_path($path);
48 }
49 else {
50 // normal nid
51 $path = 'node/' . $path;
52 }
7ae14099 53 }
20ab24e6 54 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
3d1f2e45 55 $title = _print_get_title($path);
855b243a 56
31b24d6c 57 if (count($form_state['input']) == 0) {
855b243a
JV
58 $nodepath = drupal_get_normal_path($path);
59 db_merge('print_mail_page_counter')
60 ->key(array('path' => $nodepath))
61 ->fields(array(
62 'totalcount' => 1,
63 'timestamp' => REQUEST_TIME,
64 ))
65 ->expression('totalcount', 'totalcount + :inc', array(':inc' => 1))
66 ->execute();
67 }
68
7be5aba1 69 $form['path'] = array('#type' => 'value', '#value' => $path);
20ab24e6 70 $form['cid'] = array('#type' => 'value', '#value' => $cid);
65cf1df9 71 $form['title'] = array('#type' => 'value', '#value' => $title);
7be5aba1
JV
72
73 $form['fld_from_addr'] = array(
74 '#type' => 'textfield',
27dc0ea1
JV
75 '#title' => t('Your e-mail'),
76 '#size' => 62,
69b6836d 77 '#required' => TRUE,
7be5aba1
JV
78 );
79 $form['fld_from_name'] = array(
80 '#type' => 'textfield',
81 '#title' => t('Your name'),
27dc0ea1 82 '#size' => 62,
7be5aba1 83 );
27dc0ea1 84 $form['txt_to_addrs'] = array(
7be5aba1 85 '#type' => 'textarea',
27dc0ea1 86 '#title' => t('Send to'),
7be5aba1 87 '#rows' => 3,
27dc0ea1 88 '#resizable' => FALSE,
7be5aba1 89 '#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
69b6836d 90 '#required' => TRUE,
7be5aba1
JV
91 );
92 $form['fld_subject'] = array(
93 '#type' => 'textfield',
94 '#title' => t('Subject'),
27dc0ea1 95 '#size' => 62,
69b6836d 96 '#required' => TRUE,
27dc0ea1 97 );
95e0e9e4
JV
98 if (!empty($title)) {
99 $form['fld_title'] = array(
100 '#type' => 'item',
101 '#title' => t('Page to be sent'),
102 '#markup' => l($title, $path, array('attributes' => array('title' => t('View page')))),
103 );
104 }
65cf1df9
JV
105 $form['fld_send_option'] = array(
106 '#type' => 'select',
107 '#title' => t('Send page as'),
39610ea7 108 '#default_value' => variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT),
65cf1df9 109 '#options' => array(
528d930a
JV
110 'sendlink' => t('Link'),
111 'sendpage' => t('Inline HTML'),
65cf1df9
JV
112 ),
113 );
114 if (class_exists('Mail_mime')) {
528d930a
JV
115 $form['fld_send_option']['#options']['inline-attachment'] = t('Inline HTML with Attachment');
116 $form['fld_send_option']['#options']['plain-attachment'] = t('Plain Text with Attachment');
65cf1df9 117 }
27dc0ea1
JV
118 $form['txt_message'] = array(
119 '#type' => 'textarea',
120 '#title' => t('Your message'),
121 '#rows' => 6,
69b6836d 122 '#required' => TRUE,
7be5aba1 123 );
0706a465 124
7d62e7c4 125 if ($print_mail_teaser_choice) {
0706a465
JV
126 $form['chk_teaser'] = array(
127 '#type' => 'checkbox',
128 '#title' => t('Send only the teaser'),
7d62e7c4 129 '#default_value' => $print_mail_teaser_default,
0706a465
JV
130 );
131 }
132 else {
7d62e7c4 133 $form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default);
0706a465
JV
134 }
135
7be5aba1 136 $form['btn_submit'] = array(
6936e5f6 137 '#name' => 'submit',
7be5aba1 138 '#type' => 'submit',
474d264b 139 '#value' => t('Send e-mail'),
7be5aba1
JV
140 );
141 $form['btn_clear'] = array(
6936e5f6 142 '#type' => 'markup',
79c61a1d 143 '#markup' => '<input type="reset" name="clear" id="edit-btn-clear" value="' . t('Clear form') . '" class="form-submit" />',
7be5aba1
JV
144 );
145 $form['btn_cancel'] = array(
6936e5f6 146 '#name' => 'cancel',
7be5aba1 147 '#type' => 'submit',
474d264b 148 '#value' => t('Cancel'),
7be5aba1
JV
149 );
150
151 if ($user->uid != 0) {
c933f342 152 $user_name = check_plain(strip_tags(theme('username', array('account' => $user))));
7be5aba1
JV
153 $form['fld_from_addr']['#default_value'] = $user->mail;
154 $form['fld_from_addr']['#disabled'] = TRUE;
155 $form['fld_from_addr']['#value'] = $user->mail;
5e262b18 156 $form['fld_from_name']['#default_value'] = $user_name;
7be5aba1 157 }
0706a465
JV
158 else {
159 $user_name = t('Someone');
160 }
161 $site_name = variable_get('site_name', t('an interesting site'));
937c5ab6 162 $print_mail_text_subject = filter_xss(variable_get('print_mail_text_subject', t('!user has sent you a message from !site')));
3d1f2e45 163 $form['fld_subject']['#default_value'] = t($print_mail_text_subject, array('!user' => $user_name, '!site' => $site_name, '!title' => $title));
937c5ab6 164 $print_mail_text_content = filter_xss(variable_get('print_mail_text_content', ''));
ff47cf5a 165 $form['txt_message']['#default_value'] = t($print_mail_text_content);
7be5aba1
JV
166
167 return $form;
168}
169
6a4c89f9
JV
170/**
171 * Theme function for the send by-email form submission.
172 *
173 * Adds a class to the form labels. This class is used to place the label on
174 * the left of the input fields.
175 *
176 * @ingroup forms
177 */
0706a465 178function theme_print_mail_form($form) {
f03e3602 179 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
0706a465
JV
180 $content = '';
181 foreach (element_children($form) as $key) {
182 $tmp = drupal_render($form[$key]);
27dc0ea1
JV
183 switch ($key) {
184 case 'fld_from_addr':
185 case 'fld_from_name':
186 case 'txt_to_addrs':
187 case 'fld_subject':
3d1f2e45 188 case 'fld_title':
65cf1df9 189 case 'fld_send_option':
c92040a3 190 $tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
27dc0ea1
JV
191 break;
192 }
0706a465
JV
193 $content .= $tmp;
194 }
195 return $content;
196}
197
7be5aba1
JV
198/**
199 * Validate the send by-email form submission.
0706a465
JV
200 *
201 * @ingroup forms
7be5aba1
JV
202 */
203function print_mail_form_validate($form, &$form_state) {
31b24d6c 204 if (array_key_exists('cancel', $form_state['input'])) {
55a8c392
JV
205 form_set_error(NULL,'',TRUE);
206 drupal_get_messages('error');
207
208 drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']));
d54079c6
JV
209 return;
210 }
7be5aba1
JV
211 $from_addr = trim($form_state['values']['fld_from_addr']);
212 $test = user_validate_mail($from_addr);
213 if ($test) {
214 form_set_error('fld_from_addr', $test);
215 }
216
217 // All new-lines are replaced by commas
b616ca31 218 $to_addrs = preg_replace('![\r|\n|,]+!', ',', trim($form_state['values']['txt_to_addrs']));
7be5aba1
JV
219 // Create an array from the string
220 $to_array = explode(',', $to_addrs);
221 // Verify each element of the array
222 foreach ($to_array as $key => $address) {
223 $address = trim($address);
224 if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
225 // Address is of the type User Name <user@domain.tld>
226 $test = user_validate_mail($matches[2]);
f03e3602 227 $to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
7be5aba1
JV
228 }
229 else {
230 // Address must be user@domain.tld
231 $test = user_validate_mail($address);
232 }
233 if ($test) {
27dc0ea1 234 form_set_error('txt_to_addrs', $test);
7be5aba1
JV
235 }
236 }
237
f03e3602
JV
238 $print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
239
240 if ((!user_access('administer print')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1))) {
241 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)));
242 }
243
7be5aba1
JV
244 // In all fields, prevent insertion of custom headers
245 foreach ($form_state['values'] as $key => $string) {
7d62e7c4 246 if ( (substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE)) ) {
7be5aba1
JV
247 form_set_error($key, 'Found invalid character');
248 }
249 }
250
251 $form_state['values']['fld_from_addr'] = $from_addr;
252 $form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);
253 // Re-create the string from the re-organized array
27dc0ea1 254 $form_state['values']['txt_to_addrs'] = implode(', ', $to_array);
7be5aba1
JV
255}
256
257/**
258 * Process the send by-email form submission.
0706a465
JV
259 *
260 * @ingroup forms
7be5aba1
JV
261 */
262function print_mail_form_submit($form, &$form_state) {
d54079c6
JV
263 if (!array_key_exists('cancel', $form_state['values'])) {
264 if (!empty($form_state['values']['fld_from_name'])) {
f03e3602 265 $from = '"' . $form_state['values']['fld_from_name'] . '" <' . $form_state['values']['fld_from_addr'] . '>';
d54079c6
JV
266 }
267 else {
268 $from = $form_state['values']['fld_from_addr'];
269 }
d54079c6
JV
270 $cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
271
937c5ab6 272 $print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
f03e3602 273 $sender_message = $print_mail_text_message . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
d54079c6
JV
274
275 $print = print_controller($form_state['values']['path'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
276
277 // Spaces in img URLs must be replaced with %20
278 $pattern = '!<(img\s[^>]*?)>!is';
279 $print['content'] = preg_replace_callback($pattern, '_print_mail_encode_urls', $print['content']);
280
281 if ($print !== FALSE) {
282 $params = array();
283 $params['subject'] = $form_state['values']['fld_subject'];
65cf1df9
JV
284 $params['message'] = $form_state['values']['txt_message'];
285 $params['link'] = $print['url'];
286 $params['title'] = $form_state['values']['title'];
d54079c6
JV
287
288 $node = $print['node'];
23a45c9b 289 $params['body'] = theme('print', array('print' => $print, 'type' => PRINT_MAIL_FORMAT, 'node' => $node));
d54079c6 290
f03e3602 291 $ok = FALSE;
caaeb140
JV
292 if (function_exists('job_queue_add') && variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT)) {
293 $use_job_queue = TRUE;
937c5ab6 294 $this_file = drupal_get_path('module', 'print_mail') . '/print_mail.inc';
caaeb140
JV
295 }
296 else {
297 $use_job_queue = FALSE;
298 }
299
f03e3602
JV
300 $addresses = explode(', ', $form_state['values']['txt_to_addrs']);
301 foreach ($addresses as $to) {
4bc318fc
JV
302 // Call to hook_print_mail_before_send in order to know if the mail can be sent
303 // Handlers must return TRUE or FALSE
304 $can_send = module_invoke_all('print_mail_before_send', $node, $to, $from, $params);
305
306 if (!in_array(FALSE, $can_send)) {
caaeb140
JV
307 if ($use_job_queue) {
308 // Use job queue to send mails during cron runs
65cf1df9 309 job_queue_add('drupal_mail', t('print_mail: From %from', array('%from' => $from)), array('print_mail', $form_state['values']['fld_send_option'], $to, language_default(), $params, $from, TRUE), $this_file, TRUE);
caaeb140
JV
310 }
311 else {
312 // Send mail immediately using Drupal's mail handler
65cf1df9 313 $ret = drupal_mail('print_mail', $form_state['values']['fld_send_option'], $to, language_default(), $params, $from, TRUE);
caaeb140
JV
314 }
315 if ($ret['result'] || $use_job_queue) {
4bc318fc
JV
316 // Call to hook_print_mail_after_send in order to provide information to other modules.
317 module_invoke_all('print_mail_after_send', $node, $to, $from, $params);
318
319 flood_register_event('print_mail');
320 $ok = TRUE;
321 }
f03e3602
JV
322 }
323 }
324 if ($ok) {
325 watchdog('print_mail', '%name [%from] sent %page to [%to]', array('%name' => $form_state['values']['fld_from_name'], '%from' => $form_state['values']['fld_from_addr'], '%page' => $form_state['values']['path'], '%to' => $form_state['values']['txt_to_addrs']));
d54079c6
JV
326 $site_name = variable_get('site_name', t('us'));
327 $print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
937c5ab6 328 drupal_set_message(check_plain(t($print_mail_text_confirmation, array('!site' => $site_name))));
d54079c6
JV
329
330 $nodepath = drupal_get_normal_path($form_state['values']['path']);
331 db_update('print_mail_page_counter')
332 ->fields(array(
333 'sentcount' => 1,
334 'sent_timestamp' => REQUEST_TIME,
335 ))
336 ->condition('path', $nodepath, '=')
f03e3602 337 ->expression('sentcount', 'sentcount + :inc', array(':inc' => count($addresses)))
d54079c6
JV
338 ->execute();
339 }
f2c4fb21 340 }
27dc0ea1 341 }
f2c4fb21 342
902d30af 343 $form_state['redirect'] = preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']);
7be5aba1
JV
344}
345
346/**
937c5ab6 347 * Implements hook_mail().
7be5aba1
JV
348 */
349function print_mail_mail($key, &$message, $params) {
65cf1df9
JV
350 $message['subject'] = $params['subject'];
351
7be5aba1
JV
352 switch ($key) {
353 case 'sendpage':
c933f342 354 $message['body'][] = check_plain($params['body']);
7be5aba1 355 $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
65cf1df9
JV
356 break;
357 case 'sendlink':
358 // Generate plain-text and html versions of message with link
359 $sendlink_plain = $params['message'] . '\n\n' . $params['link'];
360 $sendlink_html = $params['message'] . '<br/><br/>' . l($params['title'], $params['link']);
361
362 // Send HTML-only version if MIME library not present
363 if (!class_exists('Mail_mime')) {
c933f342 364 $message['body'][] = check_plain($sendlink_html);
65cf1df9
JV
365 $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
366 break;
367 }
368 // no break on purpose
369 case 'plain-attachment':
370 case 'inline-attachment':
371 // Configure new MIME object
372 $mime = new Mail_mime("\n");
373 $mime_params['html_encoding'] = '7bit';
374
375 // Pass message contents into MIME object
376 switch ($key) {
377 case 'sendlink':
c933f342 378 $mime->setTxtBody($sendlink_plain);
65cf1df9
JV
379 $mime->setHTMLBody($sendlink_html);
380 break;
381 case 'inline-attachment':
382 $mime->setHTMLBody($params['body']);
383 // no break on purpose
384 case 'plain-attachment':
c933f342 385 $mime->setTxtBody($params['message']);
65cf1df9
JV
386 $mime->addAttachment($params['body'], 'text/html', 'Attachment.html', FALSE);
387 break;
388 }
389
390 // Store MIME message output in message array
c933f342 391 $message['body'][] = check_plain($mime->get($mime_params));
65cf1df9
JV
392 $message['headers'] = $mime->headers($message['headers']);
393
394 // Strip special characters from Content-Type header
395 // Required to prevent mime_header_encode() from disrupting Content-Type header
396 $message['headers']['Content-Type'] = preg_replace('/[^\x20-\x7E]/', '', $message['headers']['Content-Type']);
397 break;
7be5aba1
JV
398 }
399}
f2c4fb21 400
0706a465 401/**
a43c9735
JV
402 * Callback function for the preg_replace_callback replacing spaces with %20
403 *
404 * Replace spaces in URLs with %20
405 *
406 * @param $matches
407 * array with the matched tag patterns, usually <a...>+text+</a>
408 * @return
409 * tag with re-written URL
410 */
411function _print_mail_encode_urls($matches) {
412 // first, split the html into the different tag attributes
413 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
414 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
415 foreach ($attribs as $key => $value) {
416 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
417 }
418
419 $size = count($attribs);
420 for ($i=1; $i < $size; $i++) {
421 // If the attribute is href or src, we may need to rewrite the URL in the value
422 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
423 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
424 $new_url = str_replace(' ', '%20', $url);
425 $matches[1] = str_replace($url, $new_url, $matches[1]);
426 }
427 }
428
f03e3602 429 $ret = '<' . $matches[1] . '>';
a43c9735
JV
430 if (count($matches) == 4) {
431 $ret .= $matches[2] . $matches[3];
432 }
433
434 return $ret;
435}