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

Contents of /contributions/modules/invisimail/invisimail.module

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


Revision 1.3 - (show annotations) (download) (as text)
Mon Oct 1 23:07:12 2007 UTC (2 years, 1 month ago) by crell
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
Changes since 1.2: +2 -11 lines
File MIME type: text/x-php
#112785 by Crell, Grugnog2, et al.  Drupal 5 port. :-)
1 <?php
2
3 // $Id: invisimail.module,v 1.2.2.1 2007/10/01 22:59:58 crell Exp $
4
5 /**
6 * @file
7 * This module provides a filter that will search content for email addresses
8 * and replace them with their ascii equivalents before display. This is not
9 * a complete protection from spam harvesters, but it is some help.
10 */
11
12 /**
13 * Implementation of hook_help().
14 *
15 */
16 function invisimail_help($section) {
17 switch ($section) {
18 case 'admin/help#invisimail':
19 return t('<p>The invisimail module privides a filter to hide email addresses from email harvesting spam-bots.</p><p>How it works: Invisimail scans content for email addresses and then converts each character of the address to its ASCII-code equivalent. The email address will appear normally on the page, but the source html will not appear as an email address. For even more security, the filter can use a JavaScript write command to further obscure the email address.</p><p>For example:<br /><i>you@example.com</i> will appear in the html source as: <div style="font-family:courier;border:1px solid #666;padding: 5px">&amp;#121;&amp;#111;&amp;#117;&amp;#64;&amp;#101;&amp;#120;&amp;#97;&amp;#109;&amp;#112;&amp;#108;&amp;#101;&amp;#46;&amp;#99;&amp;#111;&amp;#109;</div></p><p>With the JavaScript and Auto-link options enabled, the source would become: <div style="font-family:courier;border:1px solid #666;padding: 5px">&lt;script type=\'text/javascript\'&gt;&lt;!--<br />
20 document.write(\'&lt;a href= "&amp;#109;&amp;#97;&amp;#105;&amp;#108;&amp;#116;&amp;#111;&amp;#58;\' + \'&amp;#121;&amp;#111;&amp;#117;&amp;#64;\' + \'&amp;#101;&amp;#120;&amp;#97;&amp;#109;&amp;#112;&amp;#108;&amp;#101;&amp;#46;\' + \'&amp;#99;&amp;#111;&amp;#109;\' + \'"&gt;\' + \'&amp;#121;&amp;#111;&amp;#117;&amp;#64;\' + \'&amp;#101;&amp;#120;&amp;#97;&amp;#109;&amp;#112;&amp;#108;&amp;#101;&amp;#46;\' + \'&amp;#99;&amp;#111;&amp;#109;\' + \'&lt;/a&gt;\');<br />
21 //--&gt;<br />
22 &lt;/script&gt;</div></p><p>Doesn\'t look like an email address, does it?</p><p>Of course the best protection against spam-bots is to not publish an email address at all, but on a community site some users are going to publish email addresses. Invisimail provides another level of security to keep them from recieving spam.</p><p>To configure Invisimail, select "configure" next to the <a href="%url">input format</a> you\'d like to use. Enable "Encode Email Addresses" and submit the form. Then select the "configure" tab to choose options for Invisimail.</p>', array('%url' => url('admin/filters')));
23 }
24 }
25
26 /**
27 * Implementation of hook_filter_tips().
28 *
29 */
30 function invisimail_filter_tips($delta, $format, $long = FALSE) {
31 switch ($delta) {
32 case 0:
33 if ($long) {
34 return t('Every email address in the input text will be replaced with its ascii equivalent.');
35 }
36 break;
37 }
38 }
39
40 /**
41 * Implementation of hook_filter().
42 *
43 */
44 function invisimail_filter($op, $delta = 0, $format = -1, $text = '') {
45 if ($op == 'list') {
46 return array(0 => t('Encode email addresses'));
47 }
48
49 switch ($delta) {
50 case 0:
51 switch ($op) {
52 case 'description':
53 return t('Hide email addresses from spam-bots.');
54
55 case 'prepare':
56 return $text;
57
58 case 'process':
59 return invisimail($text, $format);
60
61 case 'settings':
62 $form['invisimail_settings'] = array(
63 '#type' => 'fieldset',
64 '#title' => t('Invisimail email address encoding filter'),
65 '#collapsible' => true, '#collapsed' => true
66 );
67
68 $form['invisimail_settings']['invisimail_js_'.$format] = array(
69 '#type' => 'radios',
70 '#title' => t('JavaScript'),
71 '#default_value' => variable_get('invisimail_js_'.$format, FALSE),
72 '#options' => array(FALSE => t('No JavaScript - greater compatibility'), TRUE => t('Use JavaScript - greater security')),
73 '#description' => t('Selecting "Use JavaScript" will nearly guarantee protection from spam harvesters. However email addresses will not appear for browsers without JavaScript capability.'),
74 );
75
76 $form['invisimail_settings']['invisimail_link_'.$format] = array(
77 '#type' => 'radios',
78 '#title' => t('Auto-link Emails'),
79 '#default_value' => variable_get('invisimail_link_'.$format, FALSE),
80 '#options' => array(FALSE => t('Do not create links.'), TRUE => t('Automatically create links from email addresses.')),
81 '#description' => t('Selecting "Automatically create links" will convert email addresses into a clickable "mailto:" link.'),
82 );
83 return $form;
84 }
85 break;
86 }
87 }
88
89 function invisimail($string, $format) {
90 $pattern = "!(<p>|<li>|<br />|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4})([.,]?)(?=(</p>|</li>|<br />|[ \n\r\t\)]))!i";
91
92 // the callback needs to know what filter we're using
93 // however there's no way to hand off that variable
94 // so we'll set a global variable
95 $GLOBALS['invisimail_format'] = $format;
96
97 return preg_replace_callback($pattern, 'invisimail_callback', $string);
98 }
99
100 function invisimail_callback($matches) {
101 return $matches[1] . invisimail_ascii_encode($matches[2]) . $matches[3];
102 }
103
104 function invisimail_ascii_encode($string) {
105 $format = $GLOBALS['invisimail_format'];
106 $js = variable_get('invisimail_js_'.$format, FALSE);
107 $link = variable_get('invisimail_link_'.$format, FALSE);
108
109 if ($js) {
110 $output = "<script type='text/javascript'><!--
111 document.write('";
112 }
113
114 for ($i=0; $i < strlen($string); $i++) {
115 $char = substr($string, $i, 1);
116 $encode .= '&#'.ord($char).';';
117 if ($js) {
118 if (in_array($char, array('.', '@'))) {
119 // break strings after ats and dots
120 $encode .= "'+'";
121 }
122 }
123 }
124
125 if ($link && !$js) {
126 // ascii in this next line is "mailto:"
127 $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;$encode\">$encode</a>";
128 }
129 elseif ($link && $js) {
130 $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+'$encode'+'\">'+'$encode'+'</a>";
131 }
132 else {
133 $output .= $encode;
134 }
135
136 if ($js) {
137 $output .= "');
138 //-->
139 </script>";
140 }
141
142 return $output;
143 }
144
145 ?>

  ViewVC Help
Powered by ViewVC 1.1.2