Commit 4d06305b authored by Riccardo Padovani's avatar Riccardo Padovani

Added invisimail module

parent 69a83a6a
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave,
Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute
verbatim copies of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your freedom to
share and change it. By contrast, the GNU General Public License is
intended to guarantee your freedom to share and change free software--to
make sure the software is free for all its users. This General Public License
applies to most of the Free Software Foundation's software and to any other
program whose authors commit to using it. (Some other Free Software
Foundation software is covered by the GNU Library General Public License
instead.) You can apply it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our
General Public Licenses are designed to make sure that you have the
freedom to distribute copies of free software (and charge for this service if
you wish), that you receive source code or can get it if you want it, that you
can change the software or use pieces of it in new free programs; and that
you know you can do these things.
To protect your rights, we need to make restrictions that forbid anyone to
deny you these rights or to ask you to surrender the rights. These restrictions
translate to certain responsibilities for you if you distribute copies of the
software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or for
a fee, you must give the recipients all the rights that you have. You must make
sure that they, too, receive or can get the source code. And you must show
them these terms so they know their rights.
We protect your rights with two steps: (1) copyright the software, and (2)
offer you this license which gives you legal permission to copy, distribute
and/or modify the software.
Also, for each author's protection and ours, we want to make certain that
everyone understands that there is no warranty for this free software. If the
software is modified by someone else and passed on, we want its recipients
to know that what they have is not the original, so that any problems
introduced by others will not reflect on the original authors' reputations.
Finally, any free program is threatened constantly by software patents. We
wish to avoid the danger that redistributors of a free program will individually
obtain patent licenses, in effect making the program proprietary. To prevent
this, we have made it clear that any patent must be licensed for everyone's
free use or not licensed at all.
The precise terms and conditions for copying, distribution and modification
follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND
MODIFICATION
0. This License applies to any program or other work which contains a notice
placed by the copyright holder saying it may be distributed under the terms
of this General Public License. The "Program", below, refers to any such
program or work, and a "work based on the Program" means either the
Program or any derivative work under copyright law: that is to say, a work
containing the Program or a portion of it, either verbatim or with
modifications and/or translated into another language. (Hereinafter, translation
is included without limitation in the term "modification".) Each licensee is
addressed as "you".
Activities other than copying, distribution and modification are not covered
by this License; they are outside its scope. The act of running the Program is
not restricted, and the output from the Program is covered only if its contents
constitute a work based on the Program (independent of having been made
by running the Program). Whether that is true depends on what the Program
does.
1. You may copy and distribute verbatim copies of the Program's source
code as you receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice and
disclaimer of warranty; keep intact all the notices that refer to this License
and to the absence of any warranty; and give any other recipients of the
Program a copy of this License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you
may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion of it,
thus forming a work based on the Program, and copy and distribute such
modifications or work under the terms of Section 1 above, provided that you
also meet all of these conditions:
a) You must cause the modified files to carry prominent notices stating that
you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in whole or in
part contains or is derived from the Program or any part thereof, to be
licensed as a whole at no charge to all third parties under the terms of this
License.
c) If the modified program normally reads commands interactively when run,
you must cause it, when started running for such interactive use in the most
ordinary way, to print or display an announcement including an appropriate
copyright notice and a notice that there is no warranty (or else, saying that
you provide a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this License.
(Exception: if the Program itself is interactive but does not normally print such
an announcement, your work based on the Program is not required to print
an announcement.)
These requirements apply to the modified work as a whole. If identifiable
sections of that work are not derived from the Program, and can be
reasonably considered independent and separate works in themselves, then
this License, and its terms, do not apply to those sections when you distribute
them as separate works. But when you distribute the same sections as part
of a whole which is a work based on the Program, the distribution of the
whole must be on the terms of this License, whose permissions for other
licensees extend to the entire whole, and thus to each and every part
regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest your rights to
work written entirely by you; rather, the intent is to exercise the right to
control the distribution of derivative or collective works based on the
Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of a
storage or distribution medium does not bring the other work under the scope
of this License.
3. You may copy and distribute the Program (or a work based on it, under
Section 2) in object code or executable form under the terms of Sections 1
and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable source
code, which must be distributed under the terms of Sections 1 and 2 above
on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three years, to give
any third party, for a charge no more than your cost of physically performing
source distribution, a complete machine-readable copy of the corresponding
source code, to be distributed under the terms of Sections 1 and 2 above on
a medium customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer to distribute
corresponding source code. (This alternative is allowed only for
noncommercial distribution and only if you received the program in object
code or executable form with such an offer, in accord with Subsection b
above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source code
means all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation and
installation of the executable. However, as a special exception, the source
code distributed need not include anything that is normally distributed (in
either source or binary form) with the major components (compiler, kernel,
and so on) of the operating system on which the executable runs, unless that
component itself accompanies the executable.
If distribution of executable or object code is made by offering access to
copy from a designated place, then offering equivalent access to copy the
source code from the same place counts as distribution of the source code,
even though third parties are not compelled to copy the source along with the
object code.
4. You may not copy, modify, sublicense, or distribute the Program except as
expressly provided under this License. Any attempt otherwise to copy,
modify, sublicense or distribute the Program is void, and will automatically
terminate your rights under this License. However, parties who have received
copies, or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
5. You are not required to accept this License, since you have not signed it.
However, nothing else grants you permission to modify or distribute the
Program or its derivative works. These actions are prohibited by law if you
do not accept this License. Therefore, by modifying or distributing the
Program (or any work based on the Program), you indicate your acceptance
of this License to do so, and all its terms and conditions for copying,
distributing or modifying the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the original
licensor to copy, distribute or modify the Program subject to these terms and
conditions. You may not impose any further restrictions on the recipients'
exercise of the rights granted herein. You are not responsible for enforcing
compliance by third parties to this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues), conditions
are imposed on you (whether by court order, agreement or otherwise) that
contradict the conditions of this License, they do not excuse you from the
conditions of this License. If you cannot distribute so as to satisfy
simultaneously your obligations under this License and any other pertinent
obligations, then as a consequence you may not distribute the Program at all.
For example, if a patent license would not permit royalty-free redistribution
of the Program by all those who receive copies directly or indirectly through
you, then the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply and
the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents or
other property right claims or to contest validity of any such claims; this
section has the sole purpose of protecting the integrity of the free software
distribution system, which is implemented by public license practices. Many
people have made generous contributions to the wide range of software
distributed through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing to
distribute software through any other system and a licensee cannot impose
that choice.
This section is intended to make thoroughly clear what is believed to be a
consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in certain
countries either by patents or by copyrighted interfaces, the original copyright
holder who places the Program under this License may add an explicit
geographical distribution limitation excluding those countries, so that
distribution is permitted only in or among countries not thus excluded. In such
case, this License incorporates the limitation as if written in the body of this
License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will be
similar in spirit to the present version, but may differ in detail to address new
problems or concerns.
Each version is given a distinguishing version number. If the Program specifies
a version number of this License which applies to it and "any later version",
you have the option of following the terms and conditions either of that
version or of any later version published by the Free Software Foundation. If
the Program does not specify a version number of this License, you may
choose any version ever published by the Free Software Foundation.
10. If you wish to incorporate parts of the Program into other free programs
whose distribution conditions are different, write to the author to ask for
permission. For software which is copyrighted by the Free Software
Foundation, write to the Free Software Foundation; we sometimes make
exceptions for this. Our decision will be guided by the two goals of
preserving the free status of all derivatives of our free software and of
promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE,
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT
PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT
WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR
AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR
ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL,
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA
OR DATA BEING RENDERED INACCURATE OR LOSSES
SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE
PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN
IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF
THE POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
<?php
// $Id: invisimail.api.php,v 1.1.2.1 2010/11/18 22:36:56 crell Exp $
/**
* Defines encoders available to invisimail to get turned into formatters.
*/
function hook_invisimail_encoder_info() {
}
/**
* Allows modules to alter the encoder definitions provided by other modules.
*/
function hook_invisimail_encoder_info_alter(&$encoders) {
}
\ No newline at end of file
; $Id: invisimail.info,v 1.1.4.1.2.2 2010/11/18 21:01:13 crell Exp $
name = Invisimail
description = Attempts to thwart spam-bots by encoding email addresses.
dependencies[] = filter
core = 7.x
files[] = invisimail.module
; Information added by drupal.org packaging script on 2011-01-06
version = "7.x-1.1"
core = "7.x"
project = "invisimail"
datestamp = "1294290657"
<?php
// $Id: invisimail.module,v 1.3.4.17.2.19 2011/01/06 05:08:42 crell Exp $
define('INVISIMAIL_MAILTO_ASCII', '&#109;&#97;&#105;&#108;&#116;&#111;&#58;');
/**
* @file
* This module provides a filter that will search content for email addresses
* and replace them with their ascii equivalents before display. This is not
* a complete protection from spam harvesters, but it is some help.
*/
/**
* Gets a list of all encoders defined in the system.
*
* Encoders are defined by hook_invisimail_encoder_info() and use nearly the
* same keys as filter definitions. The main difference is the "label" key is
* used in place of "title" to avoid confusion. All callbacks will be called
* from common callbacks defined by invisimail.
*
* @return array
* An array of all defined encoders.
*/
function invisimail_get_encoders() {
$encoders = &drupal_static(__FUNCTION__, array());
if (empty($encoders)) {
$encoders = module_invoke_all('invisimail_encoder_info');
foreach (array_keys($encoders) as $encoder) {
$encoders[$encoder] += array(
'settings callback' => '',
'prepare callback' => '',
'encode callback' => '',
'default settings' => array(),
);
}
drupal_alter('invisimail_encoder_info', $encoders);
}
return $encoders;
}
/**
* Gets the definition of the specified encoder.
*
* @param string $encoder
* The machine name of the encoder to load.
* @return
* An encoder definition array or NULL if not found.
*/
function invisimail_get_encoder($encoder) {
$encoders = invisimail_get_encoders();
return isset($encoders[$encoder]) ? $encoders[$encoder] : NULL;
}
/**
* Implements hook_filter_info().
*/
function invisimail_filter_info() {
$filters = array();
foreach (invisimail_get_encoders() as $encoder => $encoder_info) {
$filters['invisimail_encode_' . $encoder] = array(
'title' => t('Encode email addresses: @label', array('@label' => $encoder_info['label'])),
'description' => t('Email addresses will be obfuscated in the page source to reduce the chances of being harvested by spammers.'),
'prepare callback' => 'invisimail_encode_prepare',
'process callback' => 'invisimail_encode_process',
'settings callback' => 'invisimail_encode_settings',
'default settings' => array(
'encoder' => $encoder,
'chunk' => FALSE,
) + $encoder_info['default settings'],
'tips callback' => 'invisimail_encode_tips',
);
}
return $filters;
}
/**
* Process callback for all invisimail encoders.
*
* This callback will route the text to be processed to the general processor,
* which in turn will route individual email addresses to the appropriate encoder.
*/
function invisimail_encode_process($text, $filter, $format, $langcode, $cache, $cache_id) {
$encoder = invisimail_get_encoder($filter->settings['encoder']);
if (function_exists($encoder['encode callback'])) {
return invisimail_encode_string($text, $filter->settings['encoder'], $filter->settings);
}
elseif ($encoder['encode callback']) {
// Process is required, so there's no default passthrough option here.
watchdog('invisimail', 'Invalid encode callback: @callback', array('@callback' => $encoder['encode callback']));
return $text;
}
}
/**
* Prepare callback for all invisimail encoders.
*
* This callback will route the text to the appropriate prepare callback for
* the appropriate encoder. If one is not specified it will return the text
* unaltered.
*/
function invisimail_encode_prepare($text, $filter, $format, $langcode, $cache, $cache_id) {
$encoder = invisimail_get_encoder($filter->settings['encoder']);
if (function_exists($encoder['prepare callback'])) {
return $encoder['prepare callback']($text, $filter, $format, $langcode, $cache, $cache_id);
}
elseif ($encoder['prepare callback']) {
watchdog('invisimail', 'Invalid prepare callback: @callback', array('@callback' => $encoder['prepare callback']));
}
else {
return $text;
}
}
/**
* Settings callback for all encoding filters.
*
* This callback will call encoder-specific filtering if appropriate.
*/
function invisimail_encode_settings($form, &$form_state, $filter, $format, $defaults) {
$settings = $filter->settings + $defaults;
return invisimail_settings_form($form, $form_state, $defaults['encoder'], $settings);
}
/**
* Returns the form fragment for a given encoder's settings form.
*
* @param $form
* The form we are adding to.
* @param $form_state
* The state of the form we are adding to.
* @param string $encoder
* The name of the encoder whose settings form we want.
* @param array $settings
* The current settings for the form fragment values.
* @return array
* A form fragment.
*/
function invisimail_settings_form($form, &$form_state, $encoder, array $settings) {
$element['chunk'] = array(
'#type' => 'checkbox',
'#title' => t('Break up text for filtering'),
'#weight' => 5,
'#default_value' => $settings['chunk'],
'#description' => t('Break up the text to be filtered into chunks with and
without anchor tags. Selecting this option may slow
down the filtering of text slightly, but will provide
better error messages in the <em>Recent log entries</em>
should content not render as you expect it when there
are e-mail addresses to obfuscate.'),
);
$encoder = invisimail_get_encoder($settings['encoder']);
if (function_exists($encoder['settings callback'])) {
$element += $encoder['settings callback']($form, $form_state, $settings);
}
elseif ($encoder['settings callback']) {
watchdog('invisimail', 'Invalid settings callback: @callback', array('@callback' => $encoder['settings callback']));
}
// If there was nothing specified, that's not an error and we just continue normally.
return $element;
}
/**
* Returns the "tips" help text for all invisimail encoders.
*/
function invisimail_encode_tips($filter, $format, $long = FALSE) {
return t('Email addresses will be obfuscated in the page source to reduce the chances of being harvested by spammers.');
}
/**
* Generates the two pattern matching regexes for finding email addresses.
*
* This is moved to a separate function for cleanliness, and because it's
* a hugely complex regex that we want to be able to encapsulate separately.
*
* @return array
*/
function _invisimail_email_matching_regexes() {
// The check for the user/name portion of the email address. This is
// encapsulable regex that looks for at least one valid character (in most
// cases, a space), preceded by one invalid character, followed by at least
// one valid character.
$valid_user_chars = 'a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\'';
$user = "(?<![$valid_user_chars])[$valid_user_chars]+";
// For the domain portion of an email addy, you can have a string domain,
// an ipv4 address, or an ipv6 address. These three regex capture each of
// those possibilities, respectively.
$domain = '(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}';
$ipv4 = '[0-9]{1,3}(?:\.[0-9]{1,3}){3}';
$ipv6 = '[0-9a-fA-F]{1,4}(?:[0-9a-fA-F]{1,4}){7}';
// Now we stick it all together into a generalized, encapsulated, portable,
// and non-subitem-capturing (hence all the '(?:', which mark subpatterns as
// non-capturing) regex for grabbing email addresses.
$mail = "(?:$user)+\@(?:$domain|(?:\[(?:$ipv4|$ipv6)\]))";
// PCRE pattern modifiers; 'i' enables case-insensitivity, and 'S' enables
// the additional pattern analysis, as our regex is one that can benefit
// (it is a non-anchored pattern without a single fixed starting character.
// see http://us2.php.net/manual/en/reference.pcre.pattern.modifiers.php).
// Global case insensitivity is a little sloppy to use, but selectively
// toggling it within only some of the subpatterns isn't really worth the
// effort.
$modifiers = 'iS';
// The final pattern. We deal with these as an entire group because invisimail
// allows options that require us to deal with both an href and its text
// in relation to one another.
$pattern = "@(?:(<a [^>]*href=['\"](mailto:$mail)['\"][^>]*>)?((?".">(?<!mailto:))$mail))|(<a [^>]*href=['\"](mailto:$mail)['\"][^>]*>(.*?)</a>)@$modifiers";
$pattern_diff_link_text = "@(<a [^>]*href=['\"](mailto:$mail)['\"][^>]*>(.*?)</a>)@$modifiers";
$pattern_same_link_text = "@(?:(<a [^>]*href=['\"](mailto:$mail)['\"][^>]*>)?((?".">(?<!mailto:))$mail))@$modifiers";
return array(
'diff_link' => $pattern_diff_link_text,
'same_link' => $pattern_same_link_text,
);
}
/**
* Encodes all email addresses in a string using the specified encoder.
*
* @param string $string
* The string to encode.
* @param string $encoder_id
* The machine name of the encoder to use.
* @param array $settings
* The settings to use for the specified encoder.
* @return string
* The original string with email addresses obfuscated from spambots.
*/
function invisimail_encode_string($string, $encoder_id, array $settings = array()) {
$encoder = invisimail_get_encoder($encoder_id);
// PHP doens't allow preg callbacks to take arguments, so we have to toss
// the configuration options into a global temporarily. This is so disgusting.
$settings['callback'] = $encoder['encode callback'];
$GLOBALS['invisimail_settings'] = $settings;
$patterns = _invisimail_email_matching_regexes();
// Chunking will help determine in which portion of the overall string a call
// to preg_replace_callback may fail.
if (isset($settings['chunk']) && $settings['chunk']) {
$substrings = preg_split( '/(<a [^>]*>.+?<\/a>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE );
}
else {
$substrings = array( $string );
}
$return = '';
foreach ( $substrings as $phrase ) {
// Call preg_replace_callback twice to avoid prolems with hitting the
// backtrack limit, as the more complex $pattern has issues with domains more
// than 13 characters.
$replaced_phrase = preg_replace_callback($patterns['diff_link'], '_invisimail_process_emails_callback', $phrase);
if ($error_code = preg_last_error()) {
watchdog('invisimail', 'preg_replace_callback failed on first pass with preg_last_error() of !code with pattern %pattern on text: !text',
array(
'!code'=>$error_code,
'%pattern' => check_plain($pattern_diff_link_text),
'!text' => check_plain($phrase),
),
WATCHDOG_ERROR,
request_uri());
}
$replaced_phrase = preg_replace_callback($patterns['same_link'], '_invisimail_process_emails_callback', $replaced_phrase);
if ($error_code = preg_last_error()) {
watchdog('invisimail', 'preg_replace_callback failed on second pass with preg_last_error() of !code with pattern %pattern on text: !text',
array(
'!code'=>$error_code,
'%pattern' => check_plain($pattern_same_link_text),
'!text' => check_plain($phrase),
),
WATCHDOG_ERROR,
request_uri());
}
$return .= $replaced_phrase;
}
// Clean up our global.
unset($GLOBALS['invisimail_settings']);
return $return;
}
/**
* Callback function used by preg_replace_callback.
*
* Expects the regex pattern defined in _invisimail_email_matching_regexes() to be used.
*
* @param array $matches
* An array of matched patterns from our regex. Any match will have four keys,
* numbered 0-3, which correspond to the capturing groups in our regex. For all
* these examples, assume the following string:
* <p><a href="mailto:user@domain.com">user@domain.com</a></p>
* - 0: The entire matched string. This will include the email address, as
* well as that email address' corresponding href mailto iff it exists. In
* the example, it would be
* <a href="mailto:user@domain.com">user@domain.com
* - 1: The full string of the <a> tag portion of the match, iff that portion
* was matched; otherwise an empty string. In the example, it would be
* <a href="mailto:user@domain.com">
* - 2: The mailto + email address string from the href portion of the match,
* iff an href was found. In the example, it would be:
* mailto:user@domain.com
* - 3: The email address itself. In the example, just:
* user@domain.com
* for the next set of array elements, assume the following string:
* <p><a href="mailto:user@domain.com">click here to eMail</a></p>
* - 1: The entire matched string, but this time where the link text does not
* match the mailto address. In the example, it would be
* <a href="mailto:user@domain.com">click here to eMail
* - 2:The mailto + email address string from the href portion of the match,
* iff an href was found. In the example, it would be:
* mailto:user@domain.com
* - 3: The link text. In the example, just:
* click here to eMail
*
* @return string
* The full ASCII-encoded string that will replace the entire match.
*/
function _invisimail_process_emails_callback(array $matches) {
$settings = $GLOBALS['invisimail_settings'];
// If $matches[1] is empty, no link portion was matched for this item, so
// it's a simple replace operation.
if (empty($matches[1])) {
return $settings['callback']($matches[3], $settings);
//return invisimail_ascii_encode($matches[3], $format['js'], $format['link']);
}
// If $matches[3] is something other than the eMail address, then we
// matched on an <a> tag that didn't have the link text the same as the
// mailto eMail address, so just replace the eMail address
if ( $matches[2] != 'mailto:'.$matches[3] ) {
return str_replace($matches[2], $settings['callback']($matches[2], $settings, TRUE), $matches[0]);
}
// We DO have an existing link portion. Do our best to preserve it; that means
// ignoring the js setting, since it makes for heinous string transformations.
return str_replace(array($matches[2], $matches[3]),
array($settings['callback']($matches[2], $settings, TRUE), $settings['callback']($matches[3], $settings, TRUE)), $matches[0]);
}
/**
* Translates a string into its HTML entity version.
*
* Unlike PHP's htmlentities() and htmlspecialchars() functions, this function
* will convert all characters in the string unequivocably. That makes it useful
* for obfuscation, since browsers will treat the string exactly the same but
* most humans and spambots won't have a clue how to read it.
*
* @param string $string
* An arbitrary string to encode as HTML entities.
* @return string
* The same string encoded in raw, obfuscated HTML entities.
*/
function invisimail_encode_html($string) {
$encode = '';
for ($i=0; $i < strlen($string); $i++) {
$char = substr($string, $i, 1);
$encode .= '&#'.ord($char).';';
}
return $encode;
}
/**
* Encodes a single email address using the specified encoder.
*
* @param string $email
* The email address to encode.
* @param string $encoder
* The machine name of the encoder to use.
* @param array $settings
* An array of settings for this encoder. If not specified, the encoder
* defaults will be used.
* @return string
* The encoded email address.
*/
function invisimail_encode_email($email, $encoder, array $settings = array()) {
$encoder_info = invisimail_get_encoder($encoder);
$encoder_settings = $settings + $encoder_info['default settings'];
return $encoder_info['encode callback']($email, $settings, FALSE);
}
// ---------- Field Formatter implementations ---------- //
/**
* Implements hook_field_formatter_info().
*/
function invisimail_field_formatter_info() {
foreach (invisimail_get_encoders() as $encoder => $encoder_info) {
$formats['invisimail_' . $encoder] = array(
'label' => t('Invisimail: @encoder', array('@encoder' => $encoder_info['label'])),
'field types' => array('email'),
'settings' => $encoder_info['default settings'] + array('encoder' => $encoder),
);
}
return $formats;
}
/**
* Implements callback_field_formatter_view().
*/
function invisimail_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
foreach ($items as $delta => $item) {
$element[$delta] = array('#markup' => invisimail_encode_email($item['email'], $settings['encoder'], $settings));
}
return $element;
}
/**
* Implements callback_field_formatter_settings_form().
*/
function invisimail_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
return invisimail_settings_form($form, $form_state, $settings['encoder'], $settings);
}
/**
* Implements callback_field_formatter_settings_summary().
*/
function invisimail_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'invisimail') {
$encoder = invisimail_get_encoder($settings['encoder']);
$summary = t('Email addresses will be encoded using the %encoder encoder.', array('%encoder' => $encoder['label']));
}
return t('Hello world');
}
// ---------- Implementations of the core engine ---------- //
/**
* Implements hook_invisimail_encoder_info().
*/
function invisimail_invisimail_encoder_info() {
$encoders['html_entities'] = array(
'label' => t('HTML entities'),
'settings callback' => 'invisimail_encoder_html_entities_settings',
'encode callback' => 'invisimail_encoder_html_entities_encode',
'default settings' => array('link' => 0),
);
$encoders['js_entities'] = array(
'label' => t('Javascript-wrapped HTML entities'),
'settings callback' => 'invisimail_encoder_js_entities_settings',
'encode callback' => 'invisimail_encoder_js_entities_encode',
'default settings' => array('link' => 0),
);
return $encoders;
}
// ----- HTML Entities encoder ----- //
/**
* Settings form callback for the html_entities encoder.
*/
function invisimail_encoder_html_entities_settings($form, &$form_state, $settings) {
$element['link'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically create links from email addresses.'),
'#default_value' => $settings['link'],
'#description' => t('Selecting "Automatically create links" will convert email addresses into a clickable "mailto:" link.'),
);
return $element;
}
/**
* Encoding callback for the html_entities encoder.
*/
function invisimail_encoder_html_entities_encode($string, $settings = array(), $is_link = FALSE) {
$encode = invisimail_encode_html($string);
if ($settings['link'] && !$is_link) {
$encode = '<a href="' . INVISIMAIL_MAILTO_ASCII . "$encode\">$encode</a>";
}
return $encode;
}
// ----- Javascript encoder ----- //
/**
* Settings form callback for the js_entities encoder.
*/
function invisimail_encoder_js_entities_settings($form, &$form_state, $settings) {
$element['link'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically create links from email addresses.'),
'#default_value' => $settings['link'],
'#description' => t('Selecting "Automatically create links" will convert email addresses into a clickable "mailto:" link.'),
);
return $element;
}
/**
* Encoding callback for the js_entities encoder.
*
* Note that a link that is already a link cannot be Javascript encoded, since
* the link element is already created. Instead we just fallback to HTML
* encoding in that case. A best-attempt will be made, however, to handle
* non-existing-links properly, possibly turning them into links if so
* configured.
*
*/
function invisimail_encoder_js_entities_encode($string, $settings = array(), $is_link = FALSE) {
static $counter = 1;
// First encode the string as HTML entities, which will then be Javascript-
// injected into the page.
$encode = invisimail_encode_html($string);
// If the email address is already inside a link we cannot use JS encoding on
// it, since we cannot modify the <a> element and cannot inject our placeholder
// span inside the href itself. We therefore don't bother and just use the
// basic HTML encoding.
if ($is_link) {
return $encode;
}
if ($settings['link']) {
$mailto = INVISIMAIL_MAILTO_ASCII;
$encode = "<a href=\"{$mailto}{$encode}\">{$encode}</a>";
}
// We have to obfuscate the ID to avoid having a regular pattern for the names.
// if we had an ID format like "invisimail_$counter", then spambots could
// easily detect that string and know to parse the HTML entities that follow
// back into an email address. We use sha1() instead of md5() because md5()
// is not allowed at all on government sites due to paranoia.
$link_id = sha1("mailto_link_{$counter}");
$counter++;
$output = <<<JS
<span id="{$link_id}"></span>
<script type="text/javascript"> <!--
document.getElementById('{$link_id}').innerHTML = '{$encode}';
// --> </script>
JS;
return $output;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment