1
|
<?php
|
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.
|
9
|
*
|
10
|
* @ingroup print
|
11
|
*/
|
12
|
|
13
|
require_once(DRUPAL_ROOT . '/' . drupal_get_path('module', 'print') . '/print.pages.inc');
|
14
|
|
15
|
// Include MIME library
|
16
|
@include_once('Mail/mime.php');
|
17
|
|
18
|
/**
|
19
|
* Menu callback for the send by email form.
|
20
|
*
|
21
|
* @ingroup forms
|
22
|
*/
|
23
|
function print_mail_form($form, &$form_state) {
|
24
|
global $user;
|
25
|
|
26
|
// Remove the printmail/ prefix
|
27
|
$path_arr = explode('/', $_GET['q']);
|
28
|
unset($path_arr[0]);
|
29
|
$path = filter_xss(implode('/', $path_arr));
|
30
|
if (empty($path)) {
|
31
|
// If no path was provided, let's try to generate a page for the referer
|
32
|
global $base_url;
|
33
|
|
34
|
$ref = $_SERVER['HTTP_REFERER'];
|
35
|
$path = preg_replace("!^$base_url/!", '', $ref);
|
36
|
if (($path === $ref) || empty($path)) {
|
37
|
$path = variable_get('site_frontpage', 'node');
|
38
|
}
|
39
|
drupal_goto(PRINTMAIL_PATH . '/' . $path);
|
40
|
}
|
41
|
elseif (ctype_digit($path_arr[1])) {
|
42
|
if (drupal_lookup_path('source', $path)) {
|
43
|
// This is a numeric alias
|
44
|
$path = drupal_get_normal_path($path);
|
45
|
}
|
46
|
else {
|
47
|
// normal nid
|
48
|
$path = 'node/' . $path;
|
49
|
}
|
50
|
}
|
51
|
else {
|
52
|
$path = drupal_get_normal_path($path);
|
53
|
}
|
54
|
|
55
|
// Handle the query
|
56
|
$query = $_GET;
|
57
|
unset($query['q']);
|
58
|
|
59
|
$print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
|
60
|
|
61
|
if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold))) {
|
62
|
$form['flood'] = array(
|
63
|
'#type' => 'markup',
|
64
|
'#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>',
|
65
|
);
|
66
|
return $form;
|
67
|
}
|
68
|
|
69
|
$print_mail_teaser_default = variable_get('print_mail_teaser_default', PRINT_MAIL_TEASER_DEFAULT_DEFAULT);
|
70
|
$print_mail_teaser_choice = variable_get('print_mail_teaser_choice', PRINT_MAIL_TEASER_CHOICE_DEFAULT);
|
71
|
$form = array();
|
72
|
|
73
|
$cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
|
74
|
$title = _print_get_title($path);
|
75
|
|
76
|
if (count($form_state['input']) == 0) {
|
77
|
$nodepath = drupal_get_normal_path($path);
|
78
|
db_merge('print_mail_page_counter')
|
79
|
->key(array('path' => $nodepath))
|
80
|
->fields(array(
|
81
|
'totalcount' => 1,
|
82
|
'timestamp' => REQUEST_TIME,
|
83
|
))
|
84
|
->expression('totalcount', 'totalcount + 1')
|
85
|
->execute();
|
86
|
}
|
87
|
|
88
|
$form['path'] = array('#type' => 'value', '#value' => $path);
|
89
|
$form['query'] = array('#type' => 'value', '#value' => $query);
|
90
|
$form['cid'] = array('#type' => 'value', '#value' => $cid);
|
91
|
$form['title'] = array('#type' => 'value', '#value' => $title);
|
92
|
|
93
|
$form['fld_from_addr'] = array(
|
94
|
'#type' => 'textfield',
|
95
|
'#title' => t('Your email'),
|
96
|
'#size' => 62,
|
97
|
'#required' => TRUE,
|
98
|
);
|
99
|
$form['fld_from_name'] = array(
|
100
|
'#type' => 'textfield',
|
101
|
'#title' => t('Your name'),
|
102
|
'#size' => 62,
|
103
|
);
|
104
|
$form['txt_to_addrs'] = array(
|
105
|
'#type' => 'textarea',
|
106
|
'#title' => t('Send to'),
|
107
|
'#rows' => 3,
|
108
|
'#resizable' => FALSE,
|
109
|
'#description' => t('Enter multiple addresses separated by commas and/or different lines.'),
|
110
|
'#required' => TRUE,
|
111
|
);
|
112
|
$form['fld_subject'] = array(
|
113
|
'#type' => 'textfield',
|
114
|
'#title' => t('Subject'),
|
115
|
'#size' => 62,
|
116
|
'#required' => TRUE,
|
117
|
);
|
118
|
if (!empty($title)) {
|
119
|
// To prevent useless translation strings, try to translate only non-node titles
|
120
|
if (drupal_substr($path, 0, 5) != 'node/') {
|
121
|
$title = t($title);
|
122
|
}
|
123
|
|
124
|
$form['fld_title'] = array(
|
125
|
'#type' => 'item',
|
126
|
'#title' => t('Page to be sent'),
|
127
|
'#markup' => l($title, $path, array('attributes' => array('title' => t('View page')), 'query' => $query)),
|
128
|
);
|
129
|
}
|
130
|
$form['txt_message'] = array(
|
131
|
'#type' => 'textarea',
|
132
|
'#title' => t('Your message'),
|
133
|
'#rows' => 6,
|
134
|
'#required' => TRUE,
|
135
|
);
|
136
|
|
137
|
if ($print_mail_teaser_choice) {
|
138
|
$form['chk_teaser'] = array(
|
139
|
'#type' => 'checkbox',
|
140
|
'#title' => t('Send only the teaser'),
|
141
|
'#default_value' => $print_mail_teaser_default,
|
142
|
);
|
143
|
}
|
144
|
else {
|
145
|
$form['chk_teaser'] = array('#type' => 'value', '#value' => $print_mail_teaser_default);
|
146
|
}
|
147
|
|
148
|
$form['btn_submit'] = array(
|
149
|
'#name' => 'submit',
|
150
|
'#type' => 'submit',
|
151
|
'#value' => t('Send email'),
|
152
|
);
|
153
|
$form['btn_cancel'] = array(
|
154
|
'#name' => 'cancel',
|
155
|
'#type' => 'submit',
|
156
|
'#value' => t('Cancel'),
|
157
|
);
|
158
|
|
159
|
if ($user->uid != 0) {
|
160
|
$user_name = check_plain(strip_tags(theme('username', array('account' => $user))));
|
161
|
$form['fld_from_addr']['#default_value'] = $user->mail;
|
162
|
$form['fld_from_addr']['#disabled'] = TRUE;
|
163
|
$form['fld_from_addr']['#value'] = $user->mail;
|
164
|
$form['fld_from_name']['#default_value'] = $user_name;
|
165
|
}
|
166
|
else {
|
167
|
$user_name = t('Someone');
|
168
|
}
|
169
|
$site_name = variable_get('site_name', t('an interesting site'));
|
170
|
$print_mail_text_subject = filter_xss(variable_get('print_mail_text_subject', t('!user has sent you a message from !site')));
|
171
|
$form['fld_subject']['#default_value'] = t($print_mail_text_subject, array('!user' => $user_name, '!site' => $site_name, '!title' => $title));
|
172
|
$print_mail_text_content = filter_xss(variable_get('print_mail_text_content', ''));
|
173
|
$form['txt_message']['#default_value'] = t($print_mail_text_content);
|
174
|
|
175
|
return $form;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Theme function for the send by-email form submission.
|
180
|
*
|
181
|
* Adds a class to the form labels. This class is used to place the label on
|
182
|
* the left of the input fields.
|
183
|
*
|
184
|
* @ingroup forms
|
185
|
*/
|
186
|
function theme_print_mail_form($variables) {
|
187
|
$form = $variables['form'];
|
188
|
|
189
|
drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
|
190
|
$content = '';
|
191
|
foreach (element_children($form) as $key) {
|
192
|
$tmp = drupal_render($form[$key]);
|
193
|
switch ($key) {
|
194
|
case 'fld_from_addr':
|
195
|
case 'fld_from_name':
|
196
|
case 'txt_to_addrs':
|
197
|
case 'fld_subject':
|
198
|
case 'fld_title':
|
199
|
$tmp = str_replace('<label', '<label class ="printmail-label"', $tmp);
|
200
|
break;
|
201
|
}
|
202
|
$content .= $tmp;
|
203
|
}
|
204
|
return $content;
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Validate the send by-email form submission.
|
209
|
*
|
210
|
* @ingroup forms
|
211
|
*/
|
212
|
function print_mail_form_validate($form, &$form_state) {
|
213
|
if (array_key_exists('cancel', $form_state['input'])) {
|
214
|
form_set_error(NULL, '', TRUE);
|
215
|
drupal_get_messages('error');
|
216
|
|
217
|
drupal_goto(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
|
218
|
return;
|
219
|
}
|
220
|
$from_addr = trim($form_state['values']['fld_from_addr']);
|
221
|
$test = user_validate_mail($from_addr);
|
222
|
if ($test) {
|
223
|
form_set_error('fld_from_addr', $test);
|
224
|
}
|
225
|
|
226
|
// All new-lines are replaced by commas
|
227
|
$to_addrs = preg_replace('![\r|\n|,]+!', ',', trim($form_state['values']['txt_to_addrs']));
|
228
|
// Create an array from the string
|
229
|
$to_array = explode(',', $to_addrs);
|
230
|
// Verify each element of the array
|
231
|
foreach ($to_array as $key => $address) {
|
232
|
$address = trim($address);
|
233
|
if (preg_match('/(.*?) <(.*)>/s', $address, $matches)) {
|
234
|
// Address is of the type User Name <user@domain.tld>
|
235
|
$test = user_validate_mail($matches[2]);
|
236
|
$to_array[$key] = trim($matches[1]) . ' <' . $matches[2] . '>';
|
237
|
}
|
238
|
else {
|
239
|
// Address must be user@domain.tld
|
240
|
$test = user_validate_mail($address);
|
241
|
}
|
242
|
if ($test) {
|
243
|
form_set_error('txt_to_addrs', $test);
|
244
|
}
|
245
|
}
|
246
|
|
247
|
$print_mail_hourly_threshold = variable_get('print_mail_hourly_threshold', PRINT_MAIL_HOURLY_THRESHOLD);
|
248
|
|
249
|
if ((!user_access('send unlimited emails')) && (!flood_is_allowed('print_mail', $print_mail_hourly_threshold - count($to_array) + 1))) {
|
250
|
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)));
|
251
|
}
|
252
|
|
253
|
// In all fields, prevent insertion of custom headers
|
254
|
foreach ($form_state['values'] as $key => $string) {
|
255
|
if ( (drupal_substr($key, 0, 4) == 'fld_') && ((strpos($string, "\n") !== FALSE) || (strpos($string, "\r") !== FALSE)) ) {
|
256
|
form_set_error($key, 'Found invalid character');
|
257
|
}
|
258
|
}
|
259
|
|
260
|
$form_state['values']['fld_from_addr'] = $from_addr;
|
261
|
$form_state['values']['fld_from_name'] = trim($form_state['values']['fld_from_name']);
|
262
|
// Re-create the string from the re-organized array
|
263
|
$form_state['values']['txt_to_addrs'] = implode(', ', $to_array);
|
264
|
}
|
265
|
|
266
|
/**
|
267
|
* Process the send by-email form submission.
|
268
|
*
|
269
|
* @ingroup forms
|
270
|
*/
|
271
|
function print_mail_form_submit($form, &$form_state) {
|
272
|
if (!array_key_exists('cancel', $form_state['values'])) {
|
273
|
$cid = isset($form_state['values']['cid']) ? $form_state['values']['cid'] : NULL;
|
274
|
$print_mail_text_message = filter_xss_admin(variable_get('print_mail_text_message', t('Message from sender')));
|
275
|
$sender_message = $print_mail_text_message . ':<br /><br /><em>' . nl2br(check_plain($form_state['values']['txt_message'])) . '</em>';
|
276
|
|
277
|
$print = print_controller($form_state['values']['path'], $form_state['values']['query'], $cid, PRINT_MAIL_FORMAT, $form_state['values']['chk_teaser'], $sender_message);
|
278
|
|
279
|
if ($print !== FALSE) {
|
280
|
$print_mail_send_option_default = variable_get('print_mail_send_option_default', PRINT_MAIL_SEND_OPTION_DEFAULT);
|
281
|
|
282
|
$params = array();
|
283
|
$params['subject'] = $form_state['values']['fld_subject'];
|
284
|
$params['message'] = $sender_message;
|
285
|
$params['link'] = $print['url'];
|
286
|
$params['title'] = $form_state['values']['title'];
|
287
|
|
288
|
// If a name is provided, make From: in the format Common Name <address>
|
289
|
if (!empty($form_state['values']['fld_from_name'])) {
|
290
|
$from = '"' . mime_header_encode($form_state['values']['fld_from_name']) . '" <' . $form_state['values']['fld_from_addr'] . '>';
|
291
|
}
|
292
|
else {
|
293
|
$from = $form_state['values']['fld_from_addr'];
|
294
|
}
|
295
|
|
296
|
// If using reply-to, move the From: info to the params array, so that it is passed to hook_mail later
|
297
|
if (variable_get('print_mail_use_reply_to', PRINT_MAIL_USE_REPLY_TO)) {
|
298
|
$params['from'] = $from;
|
299
|
$from = NULL;
|
300
|
}
|
301
|
|
302
|
// Spaces in img URLs must be replaced with %20
|
303
|
$pattern = '!<(img\s[^>]*?)>!is';
|
304
|
$print['content'] = preg_replace_callback($pattern, '_print_replace_spaces', $print['content']);
|
305
|
|
306
|
$node = $print['node'];
|
307
|
$params['body'] = theme('print', array('print' => $print, 'type' => PRINT_MAIL_FORMAT, 'node' => $node));
|
308
|
|
309
|
// Img elements must be set to absolute
|
310
|
$pattern = '!<(img\s[^>]*?)>!is';
|
311
|
$params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
|
312
|
|
313
|
// Convert the a href elements, to make sure no relative links remain
|
314
|
$pattern = '!<(a\s[^>]*?)>!is';
|
315
|
$params['body'] = preg_replace_callback($pattern, '_print_rewrite_urls', $params['body']);
|
316
|
|
317
|
$ok = FALSE;
|
318
|
$use_job_queue = variable_get('print_mail_job_queue', PRINT_MAIL_JOB_QUEUE_DEFAULT);
|
319
|
if ($use_job_queue) {
|
320
|
$queue = DrupalQueue::get('print_mail_send');
|
321
|
}
|
322
|
|
323
|
$addresses = explode(', ', $form_state['values']['txt_to_addrs']);
|
324
|
foreach ($addresses as $to) {
|
325
|
if ($use_job_queue) {
|
326
|
// Use job queue to send mails during cron runs
|
327
|
$queue->createItem(array('module' => 'print_mail', 'key' => $print_mail_send_option_default, 'to' => $to, 'language' => language_default(), 'params' => $params, 'from' => $from));
|
328
|
}
|
329
|
else {
|
330
|
// Send mail immediately using Drupal's mail handler
|
331
|
$ret = drupal_mail('print_mail', $print_mail_send_option_default, $to, language_default(), $params, $from);
|
332
|
}
|
333
|
if ($use_job_queue || $ret['result']) {
|
334
|
flood_register_event('print_mail');
|
335
|
$ok = TRUE;
|
336
|
}
|
337
|
}
|
338
|
if ($ok) {
|
339
|
$query = empty($form_state['values']['query']) ? '' : '?' . rawurldecode(drupal_http_build_query($form_state['values']['query']));
|
340
|
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'] . $query, '%to' => $form_state['values']['txt_to_addrs']));
|
341
|
$site_name = variable_get('site_name', t('us'));
|
342
|
$print_mail_text_confirmation = variable_get('print_mail_text_confirmation', t('Thank you for spreading the word about !site.'));
|
343
|
drupal_set_message(check_plain(t($print_mail_text_confirmation, array('!site' => $site_name))));
|
344
|
|
345
|
$nodepath = drupal_get_normal_path($form_state['values']['path']);
|
346
|
db_update('print_mail_page_counter')
|
347
|
->fields(array(
|
348
|
'sentcount' => 1,
|
349
|
'sent_timestamp' => REQUEST_TIME,
|
350
|
))
|
351
|
->condition('path', $nodepath, '=')
|
352
|
->expression('sentcount', 'sentcount + :inc', array(':inc' => count($addresses)))
|
353
|
->execute();
|
354
|
}
|
355
|
}
|
356
|
}
|
357
|
|
358
|
$form_state['redirect'] = array(preg_replace('!^book/export/html/!', 'node/', $form_state['values']['path']), array('query' => $form_state['values']['query']));
|
359
|
}
|