Services_ReCaptcha
[ class tree: Services_ReCaptcha ] [ index: Services_ReCaptcha ] [ all elements ]

Source for file MailHide.php

Documentation is available at MailHide.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Services_ReCaptcha package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Services
  15.  * @package   Services_ReCaptcha
  16.  * @author    David Jean Louis <izi@php.net>
  17.  * @copyright 2008-2009 David Jean Louis
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Services_ReCaptcha
  21.  * @link      http://recaptcha.net/apidocs/mailhide/
  22.  * @since     File available since release 0.1.0
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * Dependencies.
  28.  */
  29. require_once 'Services/ReCaptcha/Base.php';
  30.  
  31. /**
  32.  * PHP5 interface to the reCATCHA MailHide API.
  33.  *
  34.  * reCAPTCHA Mailhide helps you protect your inbox by asking people to solve a
  35.  * reCAPTCHA before they can view your email address.
  36.  *
  37.  * To obtain an encryption key, you can go to the
  38.  * {@link http://mailhide.recaptcha.net/apikey key generation service}.
  39.  * This will compute a "public key", which much like a username identifies who
  40.  * you are to the MailHide server, and a "private key" which allows you to
  41.  * encrypt email addresses.
  42.  *
  43.  * @category  Services
  44.  * @package   Services_ReCaptcha
  45.  * @author    David Jean Louis <izi@php.net>
  46.  * @copyright 2008-2009 David Jean Louis
  47.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  48.  * @version   Release: @package_version@
  49.  * @link      http://pear.php.net/package/Services_ReCaptcha
  50.  * @link      http://recaptcha.net/apidocs/mailhide/
  51.  * @since     Class available since release 0.1.0
  52.  * @example   examples/example-03.php Services_ReCaptcha_MailHide example
  53.  */
  54. {
  55.     // properties {{{
  56.  
  57.     /**
  58.      * Url of the reCATCHA MailHide API.
  59.      *
  60.      * @var string $apiURL 
  61.      */
  62.     public $apiURL = 'http://mailhide.recaptcha.net/d';
  63.  
  64.     /**
  65.      * Options to customize the apparence of the generated HTML and the
  66.      * corresponding popup window.
  67.      *
  68.      * Available options are:
  69.      *   - mask_text: string, the chars that will be displayed in the email
  70.      *     address to mask it;
  71.      *   - link_text: string, the text of the link;
  72.      *   - link_title: string, the title (tooltip) of the link;
  73.      *   - popup_width: integer, the popup width in pixels;
  74.      *   - popup_height: integer, the popup height in pixels.
  75.      * 
  76.      * @var array $options 
  77.      * @see Services_ReCaptcha_Base::getOption()
  78.      * @see Services_ReCaptcha_Base::setOption()
  79.      * @see Services_ReCaptcha_Base::getOptions()
  80.      * @see Services_ReCaptcha_Base::setOptions()
  81.      */
  82.     protected $options = array(
  83.         'mask_text'    => '...',
  84.         'link_text'    => null,
  85.         'link_title'   => 'Reveal this e-mail address',
  86.         'popup_width'  => 500,
  87.         'popup_height' => 300,
  88.     );
  89.  
  90.     /**
  91.      * The email address you want to "hide".
  92.      *
  93.      * @var string $email 
  94.      * @see Services_ReCaptcha_MailHide::getEmail()
  95.      * @see Services_ReCaptcha_MailHide::setEmail()
  96.      */
  97.     protected $email;
  98.  
  99.     // }}}
  100.     // __construct() {{{
  101.     
  102.     /**
  103.      * Constructor, you must pass a valid public and private API key.
  104.      *
  105.      * Additionally, you can pass the email you want to hide, and an array of
  106.      * options.
  107.      *
  108.      * @param string $pubKey  The public API key (mandatory)
  109.      * @param string $privKey The private API key (mandatory)
  110.      * @param string $email   The email to hide (optional)
  111.      * @param array  $options An array of options (optional)
  112.      * 
  113.      * @return void 
  114.      * @see Services_ReCaptcha_MailHide::$email
  115.      * @see Services_ReCaptcha_MailHide::$options
  116.      */
  117.     public function __construct($pubKey$privKey$email = null
  118.         array $options = array()
  119.     {
  120.         // check that mcrypt is available
  121.         if (!extension_loaded('mcrypt')) {
  122.             throw new Services_ReCaptcha_Exception(__CLASS__ . ' requires mcrypt.');
  123.         }
  124.         parent::__construct($pubKey$privKey$options);
  125.         if ($email !== null{
  126.             $this->setEmail($email);
  127.         }
  128.     }
  129.     
  130.     // }}}
  131.     // getURL() {{{
  132.     
  133.     /**
  134.      * Returns the URL of the mailhide popup.
  135.      *
  136.      * @return string The mailhide popup URL
  137.      */
  138.     public function getURL()
  139.     {
  140.         $email $this->getEmail();
  141.  
  142.         // aes pad email
  143.         $bsize     = 16;
  144.         $padLength $bsize (strlen($email$bsize);
  145.         $data      str_pad($emailstrlen($email$padLengthchr($padLength));
  146.  
  147.         // encrypt email
  148.         $cypher   = MCRYPT_RIJNDAEL_128;
  149.         $key      pack('H*'$this->apiPrivateKey);
  150.         $mode     = MCRYPT_MODE_CBC;
  151.         $yv       "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  152.         $encEmail = mcrypt_encrypt($cypher$key$data$mode$yv);
  153.         $encEmail strtr(base64_encode($encEmail)'+/''-_');
  154.  
  155.         return $this->apiURL . '?k=' $this->apiPublicKey . '&c=' $encEmail;
  156.     }
  157.     
  158.     // }}}
  159.     // getHTML() {{{
  160.     
  161.     /**
  162.      * Returns the HTML code snippet that will hide your email.
  163.      *
  164.      * Instead of this method you can use the __toString() magic method to get
  165.      * the HTML code, for example:
  166.      *
  167.      * <code>
  168.      * require_once 'Services/ReCaptcha/MailHide.php';
  169.      *
  170.      * $mailhide = new Services_ReCaptcha_MailHide(
  171.      *     'pubkey',
  172.      *     'privkey',
  173.      *     'foo@example.com'
  174.      * );
  175.      * // both are equivalents:
  176.      * $html = $mailhide->getHTML();
  177.      * $html = (string) $mailhide;
  178.      * </code>
  179.      *
  180.      * @return string The HTML code
  181.      * @see Services_ReCaptcha_Base::__toString()
  182.      */
  183.     public function getHTML()
  184.     {
  185.         if ($this->options['link_text'=== null{
  186.             $emailParts     explode('@'$this->getEmail()2);
  187.             $leftPartLength strlen($emailParts[0]);
  188.             if ($leftPartLength <= 4{
  189.                 $emailParts[0substr($emailParts[0]01);
  190.             else if ($leftPartLength <= 6{
  191.                 $emailParts[0substr($emailParts[0]03);
  192.             else {
  193.                 $emailParts[0substr($emailParts[0]04);
  194.             }
  195.             $pre  htmlentities($emailParts[0]);
  196.             $text htmlentities($this->options['mask_text']);
  197.             $post '@' htmlentities($emailParts[1]);
  198.         else {
  199.             $pre  '';
  200.             $text htmlentities($this->options['link_text']);
  201.             $post '';
  202.         }
  203.         $url htmlentities($this->getURL());
  204.  
  205.         return sprintf(
  206.             '%s<a href="%s" onclick="window.open(\'%s\', \'\', ' .
  207.             '\'toolbar=0,scrollbars=0,location=0,statusbar=0,' .
  208.             'menubar=0,resizable=0,width=%s,height=%s\'); return false;" ' .
  209.             'title="%s">%s</a>%s',
  210.             $pre,
  211.             $url,
  212.             $url
  213.             intval($this->getOption('popup_width')),
  214.             intval($this->getOption('popup_height')),
  215.             htmlentities($this->getOption('link_title')),
  216.             $text,
  217.             $post
  218.         );
  219.     }
  220.     
  221.     // }}}
  222.     // getEmail() {{{
  223.  
  224.     /**
  225.      * Returns the email to "hide".
  226.      *
  227.      * @return string The email to hide
  228.      * @see Services_ReCaptcha_MailHide::$email
  229.      */
  230.     public function getEmail()
  231.     {
  232.         return $this->email;
  233.     }
  234.  
  235.     // }}}
  236.     // setEmail() {{{
  237.  
  238.     /**
  239.      * Sets the email to "hide" and returns the current
  240.      * Services_ReCaptcha_MailHide instance.
  241.      *
  242.      * @param string $email The email to hide
  243.      *
  244.      * @return Services_ReCaptcha_MailHide 
  245.      * @see Services_ReCaptcha_MailHide::$email
  246.      */
  247.     public function setEmail($email)
  248.     {
  249.         $this->email = $email;
  250.         return $this;
  251.     }
  252.  
  253.     // }}}
  254. }

Documentation generated on Mon, 11 Mar 2019 15:43:39 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.