PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/recaptcha-plugins/perl/Captcha-reCAPTCHA-Mailhide/tags/0.94/lib/Captcha/reCAPTCHA/Mailhide.pm

http://recaptcha.googlecode.com/
Perl | 275 lines | 190 code | 82 blank | 3 comment | 5 complexity | 76ee59b63acbb3fbdc0caf23f0d0ab17 MD5 | raw file
  1. package Captcha::reCAPTCHA::Mailhide;
  2. use warnings;
  3. use strict;
  4. use Carp;
  5. use Crypt::Rijndael;
  6. use MIME::Base64;
  7. use HTML::Tiny;
  8. our $VERSION = '0.94';
  9. use constant API_MAILHIDE_SERVER =>
  10. 'http://www.google.com/recaptcha/mailhide';
  11. sub new {
  12. my $class = shift;
  13. my $self = bless {}, $class;
  14. croak "new takes no parameters" if @_;
  15. return $self;
  16. }
  17. sub _aes_encrypt {
  18. my ( $val, $ky ) = @_;
  19. my $val_len = length( $val );
  20. my $pad_len = int( ( $val_len + 15 ) / 16 ) * 16;
  21. # Pad value
  22. $val .= chr( 16 - $val_len % 16 ) x ( $pad_len - $val_len )
  23. if $val_len < $pad_len;
  24. my $cipher = Crypt::Rijndael->new( $ky, Crypt::Rijndael::MODE_CBC );
  25. $cipher->set_iv( "\0" x 16 );
  26. return $cipher->encrypt( $val );
  27. }
  28. sub _urlbase64 {
  29. my $str = shift;
  30. chomp( my $enc = encode_base64( $str ) );
  31. $enc =~ tr{+/}{-_};
  32. return $enc;
  33. }
  34. sub mailhide_url {
  35. my $self = shift;
  36. my ( $pubkey, $privkey, $email ) = @_;
  37. croak
  38. "To use reCAPTCHA::Mailhide, you have to sign up for a public and "
  39. . "private key. You can do so at http://www.google.com/recaptcha/mailhide/apikey."
  40. unless $pubkey && $privkey;
  41. croak "You must supply an email address"
  42. unless $email;
  43. my $h = HTML::Tiny->new();
  44. return
  45. API_MAILHIDE_SERVER . '/d?'
  46. . $h->query_encode(
  47. {
  48. k => $pubkey,
  49. c => _urlbase64( _aes_encrypt( $email, pack( 'H*', $privkey ) ) )
  50. }
  51. );
  52. }
  53. sub _email_parts {
  54. my ( $user, $dom ) = split( /\@/, shift, 2 );
  55. my $ul = length( $user );
  56. return ( substr( $user, 0, $ul <= 4 ? 1 : $ul <= 6 ? 3 : 4 ),
  57. '...', '@', $dom );
  58. }
  59. sub mailhide_html {
  60. my $self = shift;
  61. my ( $pubkey, $privkey, $email ) = @_;
  62. my $h = HTML::Tiny->new();
  63. my $url = $self->mailhide_url( $pubkey, $privkey, $email );
  64. my ( $user, $dots, $at, $dom ) = _email_parts( $email );
  65. my %window_options = (
  66. toolbar => 0,
  67. scrollbars => 0,
  68. location => 0,
  69. statusbar => 0,
  70. menubar => 0,
  71. resizable => 0,
  72. width => 500,
  73. height => 300
  74. );
  75. my $options = join ',',
  76. map { "$_=$window_options{$_}" } sort keys %window_options;
  77. return join(
  78. '',
  79. $h->entity_encode( $user ),
  80. $h->a(
  81. {
  82. href => $url,
  83. onclick => "window.open('$url', '', '$options'); return false;",
  84. title => 'Reveal this e-mail address'
  85. },
  86. $dots
  87. ),
  88. $at,
  89. $h->entity_encode( $dom )
  90. );
  91. }
  92. 1;
  93. __END__
  94. =head1 NAME
  95. Captcha::reCAPTCHA::Mailhide - A Perl implementation of the reCAPTCHA Mailhide API
  96. =head1 VERSION
  97. This document describes Captcha::reCAPTCHA::Mailhide version 0.94
  98. =head1 SYNOPSIS
  99. use Captcha::reCAPTCHA::Mailhide;
  100. my $m = Captcha::reCAPTCHA::Mailhide->new;
  101. # Get the URL that reveals the email
  102. my $url = $m->mailhide_url( MAIL_PUBLIC_KEY, MAIL_PRIVATE_KEY, 'someone@example.com' );
  103. # Or - even easier - get the formatted HTML for an email link
  104. print $m->mailhide_html( MAIL_PUBLIC_KEY, MAIL_PRIVATE_KEY, 'someone@example.com' );
  105. For complete examples see the /examples subdirectory
  106. =head1 DESCRIPTION
  107. reCAPTCHA is a hybrid mechanical turk and captcha that allows visitors
  108. who complete the captcha to assist in the digitization of books.
  109. From L<http://recaptcha.net/learnmore.html>:
  110. reCAPTCHA improves the process of digitizing books by sending words that
  111. cannot be read by computers to the Web in the form of CAPTCHAs for
  112. humans to decipher. More specifically, each word that cannot be read
  113. correctly by OCR is placed on an image and used as a CAPTCHA. This is
  114. possible because most OCR programs alert you when a word cannot be read
  115. correctly.
  116. This Perl implementation is modelled on the PHP interface that can be
  117. found here:
  118. L<http://recaptcha.net/plugins/php/>
  119. =head1 INTERFACE
  120. To use reCAPTCHA Mailhide you need to get a public, private key pair
  121. from this page:
  122. L<http://www.google.com/recaptcha/mailhide/apikey>
  123. The Mailhide API consists of two methods C<< mailhide_html >>
  124. and C<< mailhide_url >>. The methods have the same parameters.
  125. The _html version returns HTML that can be directly put on your web
  126. page. The username portion of the email that is passed in is
  127. truncated and replaced with a link that calls Mailhide. The _url
  128. version gives you the url to decode the email and leaves it up to you
  129. to place the email in HTML.
  130. =over
  131. =item C<< new >>
  132. Create a new C<< Captcha::reCAPTCHA::Mailhide >>.
  133. =item C<< mailhide_url( $pubkey, $privkey, $email ) >>
  134. Generate a link that will decode the specified email address.
  135. =over
  136. =item C<< $pubkey >>
  137. The Mailhide public key from the signup page
  138. =item C<< $privkey >>
  139. The Mailhide private key from the signup page
  140. =item C<< $email >>
  141. The email address you want to hide.
  142. =back
  143. Returns a URL that when clicked will allow the user to decode the hidden
  144. email address.
  145. =item C<< mailhide_html( $pubkey, $privkey, $email ) >>
  146. Generates HTML markup to embed a Mailhide protected email address
  147. on a page.
  148. The arguments are the same as for C<mailhide_url>.
  149. Returns a string containing HTML that may be embedded directly in
  150. a web page.
  151. =back
  152. =head1 CONFIGURATION AND ENVIRONMENT
  153. Captcha::reCAPTCHA::Mailhide requires no configuration files or environment
  154. variables.
  155. To use Mailhide get a public/private key pair here:
  156. L<http://www.google.com/recaptcha/mailhide/apikey>
  157. =head1 DEPENDENCIES
  158. Crypt::Rijndael,
  159. MIME::Base64,
  160. HTML::Tiny
  161. =head1 INCOMPATIBILITIES
  162. None reported .
  163. =head1 BUGS AND LIMITATIONS
  164. No bugs have been reported.
  165. Please report any bugs or feature requests to
  166. C<bug-captcha-recaptcha@rt.cpan.org>, or through the web interface at
  167. L<http://rt.cpan.org>.
  168. =head1 AUTHOR
  169. Andy Armstrong C<< <andy@hexten.net> >>
  170. =head1 LICENCE AND COPYRIGHT
  171. Copyright (c) 2007, Andy Armstrong C<< <andy@hexten.net> >>. All rights reserved.
  172. This module is free software; you can redistribute it and/or
  173. modify it under the same terms as Perl itself. See L<perlartistic>.
  174. =head1 DISCLAIMER OF WARRANTY
  175. BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  176. FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
  177. OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  178. PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
  179. EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  180. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
  181. ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
  182. YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
  183. NECESSARY SERVICING, REPAIR, OR CORRECTION.
  184. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  185. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  186. REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
  187. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
  188. OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
  189. THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
  190. RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
  191. FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
  192. SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
  193. SUCH DAMAGES.