/[drupal]/contributions/modules/forward/forward.module
ViewVC logotype

Contents of /contributions/modules/forward/forward.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.95 - (show annotations) (download) (as text)
Tue Nov 3 22:51:13 2009 UTC (3 weeks, 1 day ago) by seanr
Branch: MAIN
CVS Tags: HEAD
Changes since 1.94: +45 -38 lines
File MIME type: text/x-php
#545018 - Forward link appears on forward form by joachim
#366926 - Form and Use Proper From and Reply-To Fields by Chad_Dupuis
#582922 - "Allow" personal Message "requires" the Message by derjochenmeyer
#511472 - Provide option for personal messages to be disabled, enabled, and required by koenvw
#617284 - Title from settings page doesn't take effect by justin2pin
#621910 - Improved forward_link by benoit.borrel
#612980 - two small bugs w/ fixes by adixon
#511976 - Fatal error when forwarding a view by orianasarac
1 <?php
2 /* $Id: forward.module,v 1.94 2009/09/01 19:10:49 seanr Exp $ */
3
4 /**
5 * @file
6 * Allows forwarding of nodes by email,
7 * and provides a record of how often each has been forwarded.
8 *
9 * Forward This Page Module
10 * NGP Software
11 *
12 * Written by Peter Feddo & Sean Robertson
13 * Some code borrowed from Nick White's EmailPage module
14 * Updated for Drupal 4.7
15 *
16 * http://www.ngpsystems.com
17 *
18 * Customized by development seed
19 * http://www.developmentseed.org
20 */
21
22 include(drupal_get_path('module', 'forward') .'/forward.theme');
23
24 /**
25 * Permissions
26 */
27 function forward_perm() {
28 return array('access forward', 'access epostcard', 'override email address', 'administer forward', 'override flood control');
29 }
30
31 /**
32 * Menu Hooks
33 */
34 function forward_menu() {
35 $items = array();
36 $items['epostcard'] = array(
37 'title' => variable_get('forward_epostcard_title', 'Send an e-Postcard'),
38 'page callback' => 'drupal_get_form',
39 'page arguments' => array('forward_form', 'epostcard'),
40 'access arguments' => array('access epostcard'),
41 'type' => MENU_NORMAL_ITEM
42 );
43 $items['admin/reports/forward'] = array(
44 'description' => 'Track forwarded posts',
45 'title' => 'Forward tracking',
46 'page callback' => 'forward_tracking',
47 'access arguments' => array('administer forward'),
48 );
49 $items['admin/settings/forward'] = array(
50 'description' => t('Configure settings for forward module.'),
51 'title' => 'Forward',
52 'page callback' => 'drupal_get_form',
53 'page arguments' => array('forward_admin_settings'),
54 'access arguments' => array('administer forward'),
55 'type' => MENU_NORMAL_ITEM
56 );
57 $items['forward/emailref'] = array(
58 'title' => 'Track email clickthrus',
59 'page callback' => 'forward_tracker',
60 'access arguments' => array('access content'),
61 'type' => MENU_CALLBACK
62 );
63 $items['forward'] = array(
64 'title' => variable_get('forward_page_title', 'Forward this page'),
65 'page callback' => 'forward_page',
66 'access arguments' => array('access forward'),
67 'type' => MENU_CALLBACK
68 );
69 return $items;
70 }
71
72 /**
73 * Access function
74 */
75 function forward_access($node) {
76 if (user_access('access content') && node_access('view', $node)) {
77 return TRUE;
78 }
79 else {
80 return FALSE;
81 }
82 }
83
84
85 /**
86 * Administration Page
87 */
88 function forward_admin_settings() {
89 global $base_url;
90
91 $host=parse_url($base_url);
92
93 // Administrative Options
94 $form['forward_options'] = array(
95 '#type' => 'fieldset',
96 '#title' => t('Administrative Options'),
97 '#collapsible' => TRUE,
98 '#collapsed' => FALSE
99 );
100 $form['forward_options']['forward_link_style'] = array(
101 '#type' => 'radios',
102 '#title' => t('Link style'),
103 '#default_value' => variable_get('forward_link_style', 0),
104 '#options' => array(0 => t('Text only'), 1 => t('Icon only'), 2 => t('Icon and text')),
105 '#description' => t('Select the visual style of the link.'),
106 );
107 $form['forward_options']['forward_link_type'] = array(
108 '#type' => 'checkbox',
109 '#title' => t('Use node type in link'),
110 '#return_value' => 1,
111 '#default_value' => variable_get('forward_link_type', FALSE),
112 '#description' => t('If checked, the link will read &quot;email this <em>nodetype</em>&quot;; if not, it will just read &quot;email this page&quot;'),
113 );
114 $form['forward_options']['forward_link_title'] = array(
115 '#type' => 'textfield',
116 '#title' => t('Link title'),
117 '#size' => 40,
118 '#default_value' => variable_get('forward_link_title', 'Email this !type'),
119 '#description' => t('Set the text of the link. Use !type as a place holder for the node\'s content type or \'page\' depending on the setting above.'),
120 );
121 $form['forward_options']['forward_display_nodes'] = array(
122 '#type' => 'checkbox',
123 '#title' => t('Show Forward on nodes'),
124 '#return_value' => 1,
125 '#default_value' => variable_get('forward_display_nodes', TRUE),
126 '#description' => t('If checked, the link/form will appear on nodes'),
127 );
128 $form['forward_options']['forward_display_teasers'] = array(
129 '#type' => 'checkbox',
130 '#title' => t('Show Forward on teasers'),
131 '#return_value' => 1,
132 '#default_value' => variable_get('forward_display_teasers', FALSE),
133 '#description' => t('If checked, the link/form will appear on teasers in lists of nodes'),
134 );
135 $form['forward_options']['forward_display_comments'] = array(
136 '#type' => 'checkbox',
137 '#title' => t('Show Forward on comments'),
138 '#return_value' => 1,
139 '#default_value' => variable_get('forward_display_comments', FALSE),
140 '#description' => t('If checked, the link/form will appear on comments'),
141 );
142 $form['forward_options']['forward_display_nonnode'] = array(
143 '#type' => 'checkbox',
144 '#title' => t('Show Forward on non-node pages'),
145 '#return_value' => 1,
146 '#default_value' => variable_get('forward_display_nonnode', FALSE),
147 '#description' => t('If checked, the link/form will appear on non-node pages like views and other module pages'),
148 );
149 $form['forward_options']['forward_theme_template'] = array(
150 '#type' => 'checkbox',
151 '#title' => t('Use forward.tpl.php template instead of old style theme functions'),
152 '#default_value' => variable_get('forward_theme_template', 0),
153 '#description' => t('If you are upgrading from an old site and customized theme_forward_email or theme_forward_postcard, leave this unchecked unless you wish to convert your customizations to the new forward.tpl.php style. If upgrading, make sure you flush the theme registry before making this change or Drupal may not find the new template.'),
154 );
155 $form['forward_options']['forward_form_type'] = array(
156 '#type' => 'select',
157 '#title' => t('Form type'),
158 '#default_value' => variable_get('forward_form_type', 'link'),
159 '#options' => array('link' => 'link', 'form' => 'form'),
160 '#description' => t('Select link for a forward this page link or form to use a collapsible forwarding form.'),
161 );
162 $form['forward_options']['forward_flood_control'] = array(
163 '#type' => 'select',
164 '#title' => t('Flood control limit'),
165 '#default_value' => variable_get('forward_flood_control', 10),
166 '#options' => array('1' => '1', '5' => '5', '10' => '10', '15' => '15', '20' => '20', '25' => '25', '30' => '30', '35' => '35', '40' => '40', '50' => '50'),
167 '#description' => t('How many times a user can use the form in a one hour period. This will help prevent the forward module from being used for spamming.'),
168 );
169 $form['forward_options']['forward_flood_error'] = array(
170 '#type' => 'textarea',
171 '#title' => t('Flood control error'),
172 '#default_value' => variable_get('forward_flood_error', t('You can\'t send more than !number messages per hour. Please try again later.')),
173 '#cols' => 40,
174 '#rows' => 10,
175 '#description' => t('This text appears if a user exceeds the flood control limit. The value of the flood control limit setting will appear in place of %number in the message presented to users'),
176 );
177 $form['forward_options']['forward_message'] = array(
178 '#type' => 'select',
179 '#title' => t('Personal messages'),
180 '#options' => array(0 => 'Disabled', 1 => 'Optional', 2 => 'Required'),
181 '#default_value' => variable_get('forward_message', 1),
182 '#description' => t('Choose whether the personal message field will be disabled, optional, or required.'),
183 );
184 $form['forward_options']['forward_filter_html'] = array(
185 '#type' => 'checkbox',
186 '#title' => t('Allow HTML in personal messages'),
187 '#return_value' => 1,
188 '#default_value' => variable_get('forward_filter_html', FALSE),
189 '#description' => t('If checked, will filter XSS and all tags not allowed below from the custom body field. Otherwise, the body will converted to plain text with all HTML converted to character entities'),
190 );
191 $form['forward_options']['forward_filter_tags'] = array(
192 '#type' => 'textfield',
193 '#title' => t('Allowed HTML tags'),
194 '#size' => 40,
195 '#default_value' => variable_get('forward_filter_tags', 'p,br,em,strong,cite,code,ul,ol,li,dl,dt,dd'),
196 '#description' => t('List of allowed tags (separated by commas) that will be allowed by the XSS filter if enabled above. The default tags allowed are: p,br,em,strong,cite,code,ul,ol,li,dl,dt,dd'),
197 );
198 $form['forward_options']['forward_sender_address'] = array(
199 '#type' => 'textfield',
200 '#title' => t('From address'),
201 '#default_value' => variable_get('forward_sender_address', ''),
202 '#size' => 40,
203 '#maxlength' => 40,
204 '#description' => t('If left blank, the from address entered in the form will be used as the from address for the email. Enter a valid email address here to override that. The sender\'s email will still appear in the body of the message.'),
205 );
206
207 $types = node_get_types();
208 $options = array();
209 $defaults = array();
210 foreach ($types as $type => $info) {
211 $options[$type] = $info->name;
212 if (variable_get('forward_display_'.$type, TRUE)) {
213 $defaults[] = $type;
214 }
215 }
216 $form['forward_options']['forward_display_types'] = array(
217 '#type' => 'checkboxes',
218 '#title' => t('Content types'),
219 '#options' => $options,
220 '#default_value' => $defaults,
221 '#description' => t('Choose which content types to display the Forward link on.'),
222 );
223
224 // Page Text Values
225 $form['forward_text_values'] = array(
226 '#type' => 'fieldset',
227 '#title' => t('Page Text Values'),
228 '#collapsible' => TRUE,
229 '#collapsed' => FALSE
230 );
231 $form['forward_text_values']['forward_instructions'] = array(
232 '#type' => 'textarea',
233 '#title' => t('Forward Instructions'),
234 '#default_value' => variable_get('forward_instructions', '<p>Thank you for your interest in spreading the word about !site.</p><p>NOTE: We only request your email address so that the person you are recommending the page to knows that you wanted them to see it, and that it is not junk mail. We do not capture any email address.</p>'),
235 '#cols' => 40,
236 '#rows' => 10,
237 '#description' => t('This message will be displayed above the form. The token !site will be replaced with the site name.'),
238 );
239 $form['forward_text_values']['forward_thankyou'] = array(
240 '#type' => 'textarea',
241 '#title' => t('Thank You Message'),
242 '#default_value' => variable_get('forward_thankyou', 'Thank you for your help in spreading the word about !site. We appreciate your help.'),
243 '#cols' => 40,
244 '#rows' => 10,
245 '#description' => t('This message will be displayed after the user successfully submits the form. The token !site will be replaced with the site name.'),
246 );
247
248 // Forward Form Default Values
249 $form['forward_page_defaults'] = array(
250 '#type' => 'fieldset',
251 '#title' => t('Default Values for Email This Page'),
252 '#collapsible' => TRUE,
253 '#collapsed' => FALSE,
254 '#description' => t('These options will set the default values for the "email this page" form.')
255 );
256 $form['forward_page_defaults']['forward_header_image'] = array(
257 '#type' => 'textfield',
258 '#title' => t('Header Image for Email'),
259 '#default_value' => variable_get('forward_header_image', ''),
260 '#size' => 40,
261 '#maxlength' => 256,
262 '#description' => t('Enter the URL of the image to as a logo at the top of forwarded pages.'),
263 '#attributes' => FALSE,
264 );
265 $form['forward_page_defaults']['forward_page_title'] = array(
266 '#type' => 'textfield',
267 '#title' => t('Forward Page Title'),
268 '#default_value' => variable_get('forward_page_title', t('Forward this page')),
269 '#size' => 40,
270 '#maxlength' => 256,
271 '#description' => t('Title to display above the Forward page form'),
272 );
273 $form['forward_page_defaults']['forward_page_subject'] = array(
274 '#type' => 'textfield',
275 '#title' => t('Forward Message Subject'),
276 '#default_value' => variable_get('forward_page_subject', t('!name has forwarded a page to you from !site')),
277 '#size' => 40,
278 '#maxlength' => 256,
279 '#description' => t('Email subject line. The sender\'s name will appear in place of !name in the subject. The web site name will be inserted in place of !site.'),
280 );
281 $form['forward_page_defaults']['forward_page_message'] = array(
282 '#type' => 'textarea',
283 '#title' => t('Forward Message Body'),
284 '#default_value' => variable_get('forward_page_message', t('!name thought you would like to see this page from the !site web site.')),
285 '#cols' => 40,
286 '#rows' => 10,
287 '#description' => t('Email message body. The sender\'s name will appear in place of !name in the message body. The web site name will be inserted in place of !site. The sender will be able to add their own message after this.'),
288 );
289 $form['forward_page_defaults']['forward_full_body'] = array(
290 '#type' => 'checkbox',
291 '#title' => t('Include full node'),
292 '#return_value' => 1,
293 '#default_value' => variable_get('forward_full_body', FALSE),
294 '#description' => t('If checked, Forward will include the full body of the node rather than just the teaser'),
295 );
296
297 // Forward Form Default Values
298 $form['forward_epostcard_defaults'] = array(
299 '#type' => 'fieldset',
300 '#title' => t('Default Values for e-Postcard'),
301 '#collapsible' => TRUE,
302 '#collapsed' => FALSE,
303 '#description' => t('These options will set the default values for the "Send an e-Postcard" form.')
304 );
305 $form['forward_epostcard_defaults']['forward_epostcard_title'] = array(
306 '#type' => 'textfield',
307 '#title' => t('Forward e-Postcard Title'),
308 '#default_value' => variable_get('forward_epostcard_title', t('Send an e-Postcard')),
309 '#size' => 40,
310 '#maxlength' => 256,
311 '#description' => t('Title to display above the Forward page form'),
312 );
313 $form['forward_epostcard_defaults']['forward_epostcard_subject'] = array(
314 '#type' => 'textfield',
315 '#title' => t('e-Postcard Message Subject'),
316 '#default_value' => variable_get('forward_epostcard_subject', t('!name has sent you an e-postcard from !site')),
317 '#size' => 40,
318 '#maxlength' => 256,
319 '#description' => t('Postcard subject line. The sender\'s name will appear in place of !name in the subject. The web site name will be inserted in place of !site.'),
320 );
321 $form['forward_epostcard_defaults']['forward_epostcard_message'] = array(
322 '#type' => 'textarea',
323 '#title' => t('e-Postcard Message Body'),
324 '#default_value' => variable_get('forward_epostcard_message', t('!name has sent you an e-postcard from the !site web site. Please take a moment to visit our web site.')),
325 '#cols' => 40,
326 '#rows' => 10,
327 '#description' => t('Postcard message body. The sender\'s name will appear in place of !name in the message body. The web site name will be inserted in place of !site. The sender will be able to add their own message after this. You can use HTML in this field to add a special postcard image to the email above the sender\'s message.'),
328 );
329 $form['forward_epostcard_defaults']['forward_ad_footer'] = array(
330 '#type' => 'textarea',
331 '#title' => t('Oranization Ad'),
332 '#default_value' => variable_get('forward_ad_footer', " "),
333 '#cols' => 40,
334 '#rows' => 10,
335 '#description' => t('This message will be just above the footer message in the email.'),
336 );
337 $form['forward_epostcard_defaults']['forward_footer'] = array(
338 '#type' => 'textarea',
339 '#title' => t('Footer Message'),
340 '#default_value' => variable_get('forward_footer', " "),
341 '#cols' => 40,
342 '#rows' => 4,
343 '#description' => t('This message will be appended as a footer message to the email.'),
344 );
345 $dyn_options = array(
346 'node' => t('Latest Blog Articles'),
347 'user' => t('Latest Users'),
348 'comment' => t('Latest Comments'),
349 'popular' => t('Most Popular Content'),
350 'none' => t('None')
351 );
352 $form['forward_epostcard_defaults']['forward_dynamic_block'] = array(
353 '#type' => 'radios',
354 '#title' => t('Dynamic Block'),
355 '#default_value' => variable_get('forward_dynamic_block', 'none'),
356 '#options' => $dyn_options,
357 '#description' => t('Choose the dynamic block to send with these emails'),
358 '#required' => TRUE,
359 '#attributes' => NULL,
360 );
361 $form['forward_epostcard_defaults']['forward_epostcard_return'] = array(
362 '#type' => 'textfield',
363 '#title' => t('e-Postcard Return URL'),
364 '#default_value' => variable_get('forward_epostcard_return', ''),
365 '#size' => 40,
366 '#description' => t('URL of path to redirect users to after submitting the epostcard form.'),
367 );
368 $form['#submit'][] = 'forward_admin_settings_submit';
369
370 return system_settings_form($form);
371 }
372
373 function forward_admin_settings_submit($form, &$form_state) {
374 $types = node_get_types();
375 foreach ($types as $type => $info) {
376 if (!empty($form_state['values']['forward_display_types'][$type])) {
377 variable_set('forward_display_'.$type, TRUE);
378 }
379 else {
380 variable_set('forward_display_'.$type, FALSE);
381 }
382 }
383 unset($form_state['values']['forward_display_types']);
384 system_settings_form_submit($form, $form_state);
385 }
386
387
388 /**
389 * Email Tracker
390 */
391 function forward_tracker() {
392 $form_state['values']['path'] = drupal_get_normal_path($_GET['path']);
393 $args = explode('/', $form_state['values']['path']);
394
395 if (($args[0] == 'node') && (!empty($args[1])) && (is_numeric($args[1]))) {
396 $nid = $args[1];
397 db_query("UPDATE {forward_statistics} SET clickthrough_count = clickthrough_count+1 WHERE nid = %d", $nid);
398 }
399
400 if ($form_state['values']['path'] == variable_get('site_frontpage', 'node')) {
401 $form_state['values']['path'] = '<front>';
402 }
403
404 db_query("INSERT INTO {forward_log} (path, type, timestamp) VALUES ('%s', '%s', %d)", $form_state['values']['path'], 'REF', time());
405
406 drupal_goto(drupal_get_path_alias($form_state['values']['path']));
407 }
408
409
410 /**
411 * Page
412 */
413 function forward_page() {
414 drupal_set_html_head('<meta name="robots" content="noindex, nofollow" />');
415 $nid = NULL;
416 if (empty($_GET['path']) || ($_GET['path'] == 'node/0')) {
417 return t('No path was selected to forward');
418 }
419
420 if (!empty($_GET['path'])) {
421 $form_state['values']['path'] = drupal_get_normal_path($_GET['path']);
422 $ret = preg_match("/^node\/(.*)/i", $form_state['values']['path'], $matches);
423 if ($ret == 1) {
424 $nid = $matches[1];
425 }
426 }
427 if (is_numeric($nid)) {
428 // we have a node
429 $node = node_load(array('nid' => $nid));
430 if (!node_access('view', $node)) {
431 // Access is denied
432 return drupal_access_denied();
433 }
434 $form_state['values']['path'] = 'node/'. $node->nid;
435 }
436 else {
437 $args = explode('/', $form_state['values']['path']);
438 if ($args[0] == 'admin') {
439 return drupal_access_denied();
440 }
441 $node = new stdClass();
442 $node->title = $form_state['values']['path'];
443 }
444
445 if ($form_state['values']['path'] == 'epostcard') {
446 $emailtype = 'postcard';
447 drupal_set_title(variable_get('forward_epostcard_title', 'Send an e-Postcard'));
448 }
449 else {
450 $emailtype = 'email';
451 if (!empty($_GET['cid'])) {
452 $cid = '?cid='.$_GET['cid'];
453 }
454 $form_state['values']['path'] .= $cid;
455 drupal_set_title(variable_get('forward_page_title', 'Forward this page'));
456 }
457
458 return drupal_get_form('forward_form', $form_state['values']['path'], $node->title);
459 }
460
461 /**
462 * Form
463 */
464 function forward_form(&$form_state, $path = NULL, $title = NULL, $nodeapi = FALSE) {
465 global $base_url, $user;
466
467 $form = array();
468 $cid = array();
469
470 if (preg_match("/\?cid=/i", $path) == 1) {
471 $paths = explode('?cid=', $path);
472 $cid = array('fragment' => 'comment-'.$paths[1]);
473 $path = $paths[0];
474 }
475
476 if ($nodeapi == TRUE) {
477 $form['message'] = array(
478 '#type' => 'fieldset',
479 '#title' => t(variable_get('forward_link_title', 'Email this !type'), array('!type' => 'page')),
480 '#description' => '',
481 '#collapsed' => TRUE,
482 '#collapsible' => TRUE,
483 );
484 }
485 $form['message']['instructions'] = array(
486 '#type' => 'item',
487 '#value' => t(variable_get('forward_instructions', '<p>Thank you for your interest in spreading the word on !site.</p><p>NOTE: We only request your email address so that the person you are recommending the page to knows that you wanted them to see it, and that it is not junk mail. We do not capture any email address.</p>'), array('!site' => variable_get('site_name', 'drupal'))),
488 );
489 $form['message']['yemail'] = array(
490 '#type' => 'textfield',
491 '#title' => t('Your Email'),
492 '#size' => 58,
493 '#maxlength' => 256,
494 '#required' => TRUE,
495 );
496 $form['message']['yname'] = array(
497 '#type' => 'textfield',
498 '#title' => t('Your Name'),
499 '#size' => 58,
500 '#maxlength' => 256,
501 '#required' => TRUE,
502 );
503 $form['message']['recipients'] = array(
504 '#type' => 'textarea',
505 '#title' => t('Send To'),
506 //'#default_value' => str_replace(', ', "\n", $recipients),
507 '#cols' => 50,
508 '#rows' => 5,
509 '#description' => t('Enter multiple addresses on separate lines or separate them with commas.'),
510 '#required' => TRUE,
511 );
512 if (($emailtype == 'email') && ($nodeapi == FALSE)) {
513 $form['message']['page'] = array(
514 '#type' => 'item',
515 '#title' => t('You are going to email the following'),
516 '#value' => l($title, $path, $cid),
517 );
518 }
519 $form['message']['subject'] = array(
520 '#type' => 'item',
521 '#title' => t('Message Subject'),
522 '#value' => t(variable_get('forward_'. $emailtype .'_subject', '!name has sent you a message from !site'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
523 '#description' => '',
524 );
525 $form['message']['body'] = array(
526 '#type' => 'item',
527 '#title' => t('Message Body'),
528 '#value' => t(variable_get('forward_'. $emailtype .'_message', '!name thought you would like to see the !site web site.'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
529 '#description' => '',
530 );
531 if (variable_get('forward_message', 1) != 0) {
532 $form['message']['message'] = array(
533 '#type' => 'textarea',
534 '#title' => t('Your Personal Message'),
535 '#default_value' => '',
536 '#cols' => 50,
537 '#rows' => 10,
538 '#description' => '',
539 '#required' => (variable_get('forward_message', 1) == 2) ? TRUE : FALSE,
540 );
541 }
542 $form['message']['path'] = array(
543 '#type' => 'hidden',
544 '#value' => $path,
545 );
546 $form['message']['path_cid'] = array(
547 '#type' => 'hidden',
548 '#value' => (!empty($cid['fragment'])) ? '#'.$cid['fragment'] : '',
549 );
550 $form['message']['forward_footer'] = array(
551 '#type' => 'hidden',
552 '#value' => variable_get('forward_footer', ''),
553 );
554 $form['message']['submit'] = array(
555 '#type' => 'submit',
556 '#value' => t('Send Message'),
557 );
558
559 if ($user->uid != 0) {
560 $form['message']['yemail']['#default_value'] = $user->mail;
561 $form['message']['yemail']['#disabled'] = TRUE;
562 $form['message']['yemail']['#value'] = $user->mail;
563 $form['message']['yname']['#default_value'] = $user->name;
564 }
565
566 return $form;
567 }
568
569 /**
570 * Generate nodeapi integration, foward signup
571 */
572 function forward_nodeapi(&$node, $op, $teaser = 0) {
573 if ((variable_get('forward_form_type', 'link') == 'form') && (!$node->in_preview) && (variable_get('forward_display_'. $node->type, '1') == 1) && (!$teaser || variable_get('forward_display_teasers', 0))) {
574 switch ($op) {
575 case 'view':
576 if (user_access('access forward')) {
577 $output = drupal_get_form('forward_form', 'node/'. $node->nid, $node->title, TRUE);
578 $node->content['forward'] = array('#value' => $output, '#weight' => 10);
579 }
580 break;
581 }
582 }
583 switch ($op) {
584 case 'insert':
585 db_query('INSERT INTO {forward_statistics} (nid, last_forward_timestamp, forward_count, clickthrough_count) VALUES (%d, 0, 0, 0)', $node->nid);
586 break;
587
588 case 'delete':
589 db_query('DELETE FROM {forward_statistics} WHERE nid = %d', $node->nid);
590 break;
591 }
592 }
593
594 function forward_form_validate($form, &$form_state) {
595 global $base_url, $user;
596
597 $url = $base_url .'/'. $form_state['values']['path'];
598
599 // normalize address entries
600 $recipients = trim($form_state['values']['recipients']);
601 $recipients = str_replace(array("\r\n", "\n", "\r"), ',', $recipients);
602 $recipients = str_replace(' ', '', $recipients);
603
604 // convert addresses to an array
605 $recipient_addresses = explode(',', $recipients);
606
607 $bad_items = array('Content-Type:', 'MIME-Version:', 'Content-Transfer-Encoding:', 'bcc:', 'cc:');
608 $bad_string = FALSE;
609 foreach ($bad_items as $item) {
610 if (eregi($item, $form_state['values']['yemail'])) {
611 $bad_string = TRUE;
612 }
613 }
614 if (strpos($form_state['values']['yemail'], "\r") !== FALSE || strpos($form_state['values']['yemail'], "\n") !== FALSE || $bad_string == TRUE) {
615 form_set_error('yemail', t('Header injection attempt detected. Do not enter line feed characters into the from field!'));
616 }
617 if (user_validate_mail($form_state['values']['yemail'])) {
618 form_set_error('yemail', t('Your Email address is invalid.'));
619 }
620 if (!$form_state['values']['yname']) {
621 form_set_error('yname', t('You must enter your name.'));
622 }
623 if ($recipients == '') {
624 form_set_error('recipients', t('You did not enter any recipients.'));
625 }
626 else {
627 foreach ($recipient_addresses as $address) {
628 if ((user_validate_mail($address)) && ($address != '')) {
629 form_set_error('recipients', t('One of your Recipient addresses is invalid:') .'<br />'. check_plain($address));
630 }
631 }
632 }
633 if (!user_access('override flood control')) {
634 // Check if it looks like we are going to exceed the flood limit.
635 // It is important to ensure that the number of e-mails to be sent count against the threshold.
636 if (!flood_is_allowed('forward', variable_get('forward_flood_control', 10) - count($recipient_addresses) + 1)) {
637 form_set_error('recipients', t(variable_get('forward_flood_error', 'You can\'t send more than %number messages per hour. Please try again later.'), array('%number' => variable_get('forward_flood_control', 10))));
638 }
639 }
640 if ((variable_get('forward_message', 1) == 2) && (empty($form_state['values']['message']))) {
641 form_set_error('message', t('You must enter a message.'));
642 }
643 }
644
645 function forward_form_submit($form, &$form_state) {
646 global $base_url, $user;
647 $dynamic_content = "";
648
649 // Compose the body:
650 // Note how the form values are accessed the same way they were accessed in the validate function
651
652 //If selected assemble dynamic footer block.
653 switch (variable_get('forward_dynamic_block', '')) {
654 case 'node':
655 $dynamic_content = '<h3>'. t('Recent blog posts') .'</h3>';
656 $query = "SELECT n.nid, n.title FROM {node} n WHERE n.type = 'blog' AND n.status = 1 ORDER BY n.created DESC";
657 $dynamic_content .= forward_top5_list($query, $base_url, 'blog');
658 break;
659 case 'user':
660 $dynamic_content = '<h3>'. t("Who's new") .'</h3>';
661 $query = 'SELECT u.uid, u.name FROM {users} u WHERE status <> 0 ORDER BY uid DESC';
662 $dynamic_content .= forward_top5_list($query, $base_url, 'user');
663 break;
664 case 'comment':
665 $dynamic_content = '<h3>'. t('Recent comments') .'</h3>';
666 $query = 'SELECT c.nid, c.cid, c.subject FROM {comments} c WHERE c.status = 0 ORDER BY c.timestamp DESC';
667 $dynamic_content .= forward_top5_list($query, $base_url, 'comment');
668 break;
669 case 'popular':
670 $dynamic_content = '<h3>'. t('Most Popular Content') .'</h3>';
671 $query = "SELECT n.nid, n.title FROM {node_counter} s INNER JOIN {node} n ON s.nid = n.nid WHERE s.timestamp <> '0' AND n.status = 1 ORDER BY s.timestamp DESC";
672 $dynamic_content .= forward_top5_list($query, $base_url, 'blog');
673 break;
674 }
675
676 if ((!$form_state['values']['path']) || ($form_state['values']['path'] == 'epostcard')) {
677 $emailtype = 'epostcard';
678 $content = '';
679 $returnurl = '';
680 }
681 else {
682 $emailtype = 'email';
683 $returnurl = $form_state['values']['path'];
684 $path_array = explode('/', $form_state['values']['path']);
685 if (($path_array[0] == 'node') && (!empty($path_array[1])) && (is_numeric($path_array[1]))) {
686 $nid = $path_array[1];
687 // we have a node
688 $content = node_load($nid);
689 if (!node_access('view', $content)) {
690 // Access is denied
691 return drupal_access_denied();
692 }
693 $content->teaser = check_markup((variable_get('forward_full_body', FALSE)) ? $content->body : $content->teaser, $content->format, FALSE);
694 }
695 else {
696 $_GET['q'] = $form_state['values']['path'];
697 //_menu_append_contextual_items();
698
699 menu_set_active_item($form_state['values']['path']);
700
701 // Adapted from index.php.
702 $content = new stdClass();
703 $content->body = menu_execute_active_handler();
704 $content->title = menu_get_active_title();
705
706 // It may happen that a drupal_not_found is called in the above call
707 if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
708 return;
709 }
710
711 switch ($content->body) {
712 case MENU_NOT_FOUND:
713 return drupal_not_found();
714 break;
715 case MENU_ACCESS_DENIED:
716 return drupal_access_denied();
717 break;
718 }
719 $content->teaser = '';
720 $content->body = '';
721 }
722 }
723
724 if (variable_get('forward_allow_message', TRUE)) {
725 $message = variable_get('forward_filter_html', FALSE) ? nl2br(filter_xss($form_state['values']['message'], explode(',', variable_get('forward_filter_tags', 'p,br,em,strong,cite,code,ul,ol,li,dl,dt,dd')))) : nl2br(check_plain($form_state['values']['message']));
726 }
727 else {
728 $message = FALSE;
729 }
730
731 global $theme_key;
732 $theme_key = variable_get('theme_default', '');
733 $logo = (variable_get('forward_header_image', '') == '') ? theme_get_setting('logo') : variable_get('forward_header_image', '');
734
735 $vars = array(
736 'type' => $emailtype,
737 'site_name' => check_plain(variable_get('site_name', 'Drupal')),
738 'yname' => check_plain($form_state['values']['yname']),
739 'yemail' => check_plain($form_state['values']['yemail']),
740 'forward_message' => t(variable_get('forward_'. $emailtype .'_message', '!name thought you would like to see the !site web site.'), array('!name' => l($form_state['values']['yname'], 'mailto:'. $form_state['values']['yemail'], array('absolute' => TRUE)), '!site' => variable_get('site_name', 'drupal'))),
741 'message' => $message,
742 'base_url' => $base_url,
743 'content' => $content,
744 'path' => $returnurl.$form_state['values']['path_cid'],
745 'dynamic_content' => $dynamic_content,
746 'forward_ad_footer' => variable_get('forward_ad_footer', ''),
747 'forward_footer' => variable_get('forward_footer', ''),
748
749 // New values for forward.tpl.php
750 'site_url' => url('forward/emailref', array('absolute' => TRUE, 'query' => 'path='.$returnurl.$form_state['values']['path_cid'])),
751 'logo' => '<img src="'.url($logo, array('absolute' => TRUE)).'" alt="" />',
752 'title' => ($emailtype == 'page') ? l($content->title, 'forward/emailref', array('absolute' => TRUE, 'query' => 'path='.$returnurl)) : FALSE,
753 'submitted' => (theme_get_setting('toggle_node_info_'.$content->type)) ? t('by %author', array('%author' => $content->name)) : FALSE,
754 'node' => ($emailtype == 'page') ? $content->teaser : FALSE,
755 'link' => ($emailtype == 'page') ? l(t('Click here to read more on our site'), 'forward/emailref', array('absolute' => TRUE, 'query' => 'path='.$returnurl.$form_state['values']['path_cid'])) : FALSE,
756 );
757
758 if (variable_get('forward_theme_template', 0)) {
759 // New forward.tpl.php
760 $params['body'] = theme('forward', $vars);
761 }
762 else {
763 // Old forward_*_theme functions
764 $params['body'] = theme('forward_'. $emailtype, $vars);
765 }
766
767 $params['subject'] = t(variable_get('forward_'. $emailtype .'_subject', '!name has sent you a message from !site'), array('!name' => $form_state['values']['yname'], '!site' => variable_get('site_name', 'drupal')));
768
769 $params['from'] = trim($form_state['values']['yname'] .' <'. variable_get('forward_sender_address', variable_get('site_mail','')) .'>');
770 $params['headers']['Reply-To'] = trim($form_state['values']['yname'] .' <'. $form_state['values']['yemail'] .'>');
771
772 $recipients = trim($form_state['values']['recipients']);
773 $recipients = str_replace(array("\r\n", "\n", "\r"), ',', $recipients);
774 $recipients = explode(',', $recipients);
775 foreach ($recipients as $to) {
776 drupal_mail('forward', 'forward_page', trim($to), language_default(), $params, $params['from']);
777 // Ensure that we register a flood event for each e-mail.
778 flood_register_event('forward');
779 }
780
781 // insert record into db to record nid, type and timestamp of send
782 db_query("INSERT INTO {forward_log} (path, type, timestamp) VALUES ('%s', '%s', %d)", $form_state['values']['path'], 'SENT', time());
783 // update node forward statistics
784 if (!empty($nid)) {
785 db_query("UPDATE {forward_statistics} SET forward_count = forward_count+1, last_forward_timestamp = %d WHERE nid = %d", time(), $nid);
786 }
787
788 variable_set('forward_total', variable_get('forward_total', 0) +1);
789 variable_set('forward_recipients', variable_get('forward_recipients', 0) + count($recipients));
790
791 drupal_set_message(t(variable_get('forward_thankyou', 'Thank you for your help in spreading the word about !site. We appreciate your help.'), array('!site' => variable_get('site_name', 'drupal'))), 'status');
792
793 $form_state['redirect'] = ($returnurl != '') ? $returnurl : variable_get('forward_epostcard_return', '');
794
795 // CRMAPI hook - saves data to default enabled CRM
796 if (module_exists('crmapi')) {
797 if (!empty($user->crmapi_contact) && is_numeric($user->crmapi_contact)) {
798 $contact = crmapi_contact_load('', $user->crmapi_contact);
799 $contact_id = $user->crmapi_contact;
800 }
801 else {
802 $contact['email'] = $form_state['values']['yemail'];
803 $names = explode(' ', $form_state['values']['yname']);
804 $contact['first_name'] = $names[0];
805 $contact['last_name'] = isset($names[2]) ? $names[2] : $names[1];
806 $contact['mail_name'] = $form_state['values']['yname'];
807 $contact_id = crmapi_contact_save($contact);
808 }
809
810 $activity_params = array(
811 'contact_id' => $contact_id,
812 'activity_id' => 'OLForward',
813 'activity_type' => 'F',
814 'level' => '',
815 'flag' => '',
816 'description' => substr(url($returnurl, array('absolute' => true)), 0, 100),
817 );
818 crmapi_activity_save($activity_params);
819 }
820 }
821
822 function template_preprocess_forward(&$variables) {
823 $vars = $variables['vars'];
824 foreach ($vars as $key => $value) {
825 $variables[$key] = $value;
826 }
827 }
828
829 /**
830 * Implementation of hook_mail().
831 *
832 * Constructs the email notification message when the site is out of date.
833 *
834 * @param $key
835 * Unique key to indicate what message to build, always 'forward_page'.
836 * @param $message
837 * Reference to the message array being built.
838 * @param $params
839 * Array of parameters to indicate what text to include in the message body.
840 *
841 * @see drupal_mail()
842 * @see _update_cron_notify()
843 * @see _update_message_text()
844 */
845 function forward_mail($key, &$message, $params) {
846 $message['subject'] .= $params['subject'];
847 $message['body'][] = $params['body'];
848
849 $message['headers']['Reply-To'] = $params['headers']['Reply-To'];
850 $message['headers']['MIME-Version'] = '1.0';
851 $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
852 }
853
854
855 /**
856 * Implementation of hook_help - adds link to non-node pages
857 */
858 function forward_help($path, $arg) {
859 if (user_access('access forward') && (preg_match("/^node\//i", $path) == 0) && ($path != 'epostcard') && ($path != 'forward') && (variable_get('forward_display_nonnode', FALSE))) {
860 drupal_add_css(drupal_get_path('module', 'forward') .'/forward.css');
861 $title = t(variable_get('forward_link_title', 'Email this !type'), array('!type' => 'page'));
862 $img = drupal_get_path('module', 'forward') .'/forward.gif';
863 $html = FALSE;
864 switch (variable_get('forward_link_style', 0)) {
865 case 1:
866 $title = theme('image', $img, $title, '', array('class' => 'forward-icon'));
867 $html = TRUE;
868 break;
869 case 2:
870 $title = theme('image', $img, $title, '', array('class' => 'forward-icon forward-icon-margin')) . $title;
871 $html = TRUE;
872 break;
873 }
874 $attributes = array('title' => t('Forward this page to a friend'), 'class' => 'forward-page');
875 $query = 'path='. $path;
876 return '<span class="forward">'. l($title, 'forward', array('attributes' => $attributes, 'query' => $query, 'html' => $html)) .'</span>';
877 }
878 }
879
880
881 /**
882 * Generate links for pages
883 */
884 function forward_link($type, $node=0, $teaser=0) {
885 if (('comment' == $type && !variable_get('forward_display_comments', FALSE))
886 ||('node' == $type && (!variable_get('forward_display_nodes', TRUE) && !$teaser))) {
887 return;
888 }
889
890 if ((($type == 'node' && variable_get('forward_display_'. $node->type, '1')) || $type == 'comment')
891 && user_access('access forward') && variable_get('forward_form_type', 'link') == 'link') {
892 $links = array();
893 // FIXME $type could not be 'system' due to condition above
894 // if (($type == 'system')) {
895 // // URL, page title, func called for page content, arg, 1 = don't disp menu
896 // menu('forward', t('Email this page'), 'forward_page', 1, 1);
897 // }
898
899 // This var is set in the settings section under the admin/modules/forward section
900 // It shows 'email this $nodetype' or 'email this page'
901 if (variable_get('forward_link_type', 0)) {
902 $forward_link_type = ($type == 'comment') ? t('comment') : node_get_types("name", $node);
903 }
904 else {
905 $forward_link_type = ($type == 'comment') ? t('comment') : t('page');
906 }
907
908 if (!$teaser || variable_get('forward_display_teasers', 0)) {
909 drupal_add_css(drupal_get_path('module', 'forward') .'/forward.css');
910 $title = t(variable_get('forward_link_title', 'Email this !type'), array('!type' => $forward_link_type));
911 $img = drupal_get_path('module', 'forward') .'/forward.gif';
912 $html = FALSE;
913 switch (variable_get('forward_link_style', 0)) {
914 case 1:
915 $title = theme('image', $img, $title, '', array('class' => 'forward-icon'));
916 $html = TRUE;
917 break;
918 case 2:
919 $title = theme('image', $img, $title, '', array('class' => 'forward-icon forward-icon-margin')) . $title;
920 $html = TRUE;
921 break;
922 }
923 $form_state['values']['path'] = 'node/'. $node->nid;
924 $cid = ($type == 'comment') ? '&cid='. $node->cid : NULL;
925 $links['forward_links'] = array(
926 'title' => $title,
927 'href' => 'forward',
928 'html' => $html,
929 'attributes' => array('title' => variable_get('forward_page_title', t('Forward this page to a friend')), 'class' => 'forward-page'),
930 'query' => array('path' => $form_state['values']['path'] . $cid),
931 );
932 return $links;
933 }
934 }
935 }
936
937 /**
938 * Implementation of hook_theme().
939 */
940 function forward_theme() {
941 return array(
942 'forward' => array(
943 'arguments' => array('vars' => NULL),
944 'template' => 'forward'
945 ),
946 'forward_page' => array(
947 'arguments' => array('vars' => NULL, 'node' => NULL),
948 ),
949 'forward_email' => array(
950 'arguments' => array('vars' => NULL),
951 ),
952 'forward_postcard' => array(
953 'arguments' => array('vars' => NULL),
954 ),
955 );
956 }
957
958 function forward_form_alter(&$form, $form_state, $form_id) {
959 // Add the node-type settings option to activate the email this page link
960 if ($form_id == 'node_type_form') {
961 $form['workflow']['forward_display'] = array(
962 '#type' => 'checkbox',
963 '#title' => t('Show forwarding link/form'),
964 '#return_value' => 1,
965 '#default_value' => variable_get('forward_display_'. $form['#node_type']->type, '1'),
966 '#description' => t('Displays the form/link to allow visitors to forward the page to a friend. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/forward' ))),
967 );
968 }
969 else if ($form_id == 'comment_admin_settings') {
970 $form['viewing_options']['forward_display_comments'] = array(
971 '#type' => 'checkbox',
972 '#title'