File indexing completed on 2024-05-26 06:03:27

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage ReCaptcha
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /** @see Zend_Service_ReCaptcha */
0023 // require_once 'Zend/Service/ReCaptcha.php';
0024 
0025 /**
0026  * Zend_Service_ReCaptcha_MailHide
0027  *
0028  * @category   Zend
0029  * @package    Zend_Service
0030  * @subpackage ReCaptcha
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  * @version    $Id$
0034  */
0035 class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
0036 {
0037     /**#@+
0038      * Encryption constants
0039      */
0040     const ENCRYPTION_MODE = MCRYPT_MODE_CBC;
0041     const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128;
0042     const ENCRYPTION_BLOCK_SIZE = 16;
0043     const ENCRYPTION_IV = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
0044     /**#@-*/
0045 
0046     /**
0047      * Url to the mailhide server
0048      *
0049      * @var string
0050      */
0051     const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
0052 
0053     /**
0054      * The email address to protect
0055      *
0056      * @var string
0057      */
0058     protected $_email = null;
0059 
0060     /**
0061      * @var Zend_Validate_Interface
0062      */
0063     protected $_emailValidator;
0064 
0065     /**
0066      * Binary representation of the private key
0067      *
0068      * @var string
0069      */
0070     protected $_privateKeyPacked = null;
0071 
0072     /**
0073      * The local part of the email
0074      *
0075      * @var string
0076      */
0077     protected $_emailLocalPart = null;
0078 
0079     /**
0080      * The domain part of the email
0081      *
0082      * @var string
0083      */
0084     protected $_emailDomainPart = null;
0085 
0086     /**
0087      * Local constructor
0088      *
0089      * @param string $publicKey
0090      * @param string $privateKey
0091      * @param string $email
0092      * @param array|Zend_Config $options
0093      */
0094     public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
0095     {
0096         /* Require the mcrypt extension to be loaded */
0097         $this->_requireMcrypt();
0098 
0099         /* If options is a Zend_Config object we want to convert it to an array so we can merge it with the default options */
0100         if ($options instanceof Zend_Config) {
0101             $options = $options->toArray();
0102         }
0103 
0104         /* Merge if needed */
0105         if (is_array($options)) {
0106             $options = array_merge($this->getDefaultOptions(), $options);
0107         } else {
0108             $options = $this->getDefaultOptions();
0109         }
0110 
0111         parent::__construct($publicKey, $privateKey, null, $options);
0112 
0113         if ($email !== null) {
0114             $this->setEmail($email);
0115         }
0116     }
0117 
0118 
0119     /**
0120      * Get emailValidator
0121      *
0122      * @return Zend_Validate_Interface
0123      */
0124     public function getEmailValidator()
0125     {
0126         if (null === $this->_emailValidator) {
0127             // require_once 'Zend/Validate/EmailAddress.php';
0128             $this->setEmailValidator(new Zend_Validate_EmailAddress());
0129         }
0130         return $this->_emailValidator;
0131     }
0132 
0133     /**
0134      * Set email validator
0135      *
0136      * @param  Zend_Validate_Interface $validator
0137      * @return Zend_Service_ReCaptcha_MailHide
0138      */
0139     public function setEmailValidator(Zend_Validate_Interface $validator)
0140     {
0141         $this->_emailValidator = $validator;
0142         return $this;
0143     }
0144 
0145 
0146     /**
0147      * See if the mcrypt extension is available
0148      *
0149      * @throws Zend_Service_ReCaptcha_MailHide_Exception
0150      */
0151     protected function _requireMcrypt()
0152     {
0153         if (!extension_loaded('mcrypt')) {
0154             /** @see Zend_Service_ReCaptcha_MailHide_Exception */
0155             // require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
0156 
0157             throw new Zend_Service_ReCaptcha_MailHide_Exception('Use of the Zend_Service_ReCaptcha_MailHide component requires the mcrypt extension to be enabled in PHP');
0158         }
0159     }
0160 
0161     /**
0162      * Serialize as string
0163      *
0164      * When the instance is used as a string it will display the email address. Since we can't
0165      * throw exceptions within this method we will trigger a user warning instead.
0166      *
0167      * @return string
0168      */
0169     public function __toString()
0170     {
0171         try {
0172             $return = $this->getHtml();
0173         } catch (Exception $e) {
0174             $return = '';
0175             trigger_error($e->getMessage(), E_USER_WARNING);
0176         }
0177 
0178         return $return;
0179     }
0180 
0181     /**
0182      * Get the default set of parameters
0183      *
0184      * @return array
0185      */
0186     public function getDefaultOptions()
0187     {
0188         return array(
0189             'encoding'       => 'UTF-8',
0190             'linkTitle'      => 'Reveal this e-mail address',
0191             'linkHiddenText' => '...',
0192             'popupWidth'     => 500,
0193             'popupHeight'    => 300,
0194         );
0195     }
0196 
0197     /**
0198      * Override the setPrivateKey method
0199      *
0200      * Override the parent method to store a binary representation of the private key as well.
0201      *
0202      * @param string $privateKey
0203      * @return Zend_Service_ReCaptcha_MailHide
0204      */
0205     public function setPrivateKey($privateKey)
0206     {
0207         parent::setPrivateKey($privateKey);
0208 
0209         /* Pack the private key into a binary string */
0210         $this->_privateKeyPacked = pack('H*', $this->_privateKey);
0211 
0212         return $this;
0213     }
0214 
0215     /**
0216      * Set the email property
0217      *
0218      * This method will set the email property along with the local and domain parts
0219      *
0220      * @param string $email
0221      * @return Zend_Service_ReCaptcha_MailHide
0222      */
0223     public function setEmail($email)
0224     {
0225         $this->_email = $email;
0226 
0227         $validator = $this->getEmailValidator();
0228         if (!$validator->isValid($email)) {
0229             // require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
0230             throw new Zend_Service_ReCaptcha_MailHide_Exception('Invalid email address provided');
0231         }
0232 
0233         $emailParts = explode('@', $email, 2);
0234 
0235         /* Decide on how much of the local part we want to reveal */
0236         if (strlen($emailParts[0]) <= 4) {
0237             $emailParts[0] = substr($emailParts[0], 0, 1);
0238         } else if (strlen($emailParts[0]) <= 6) {
0239             $emailParts[0] = substr($emailParts[0], 0, 3);
0240         } else {
0241             $emailParts[0] = substr($emailParts[0], 0, 4);
0242         }
0243 
0244         $this->_emailLocalPart = $emailParts[0];
0245         $this->_emailDomainPart = $emailParts[1];
0246 
0247         return $this;
0248     }
0249 
0250     /**
0251      * Get the email property
0252      *
0253      * @return string
0254      */
0255     public function getEmail()
0256     {
0257         return $this->_email;
0258     }
0259 
0260     /**
0261      * Get the local part of the email address
0262      *
0263      * @return string
0264      */
0265     public function getEmailLocalPart()
0266     {
0267         return $this->_emailLocalPart;
0268     }
0269 
0270     /**
0271      * Get the domain part of the email address
0272      *
0273      * @return string
0274      */
0275     public function getEmailDomainPart()
0276     {
0277         return $this->_emailDomainPart;
0278     }
0279 
0280     /**
0281      * Get the HTML code needed for the mail hide
0282      *
0283      * @param string $email
0284      * @return string
0285      * @throws Zend_Service_ReCaptcha_MailHide_Exception
0286      */
0287     public function getHtml($email = null)
0288     {
0289         if ($email !== null) {
0290             $this->setEmail($email);
0291         } elseif (null === ($email = $this->getEmail())) {
0292             /** @see Zend_Service_ReCaptcha_MailHide_Exception */
0293             // require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
0294             throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
0295         }
0296 
0297         if ($this->_publicKey === null) {
0298             /** @see Zend_Service_ReCaptcha_MailHide_Exception */
0299             // require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
0300             throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
0301         }
0302 
0303         if ($this->_privateKey === null) {
0304             /** @see Zend_Service_ReCaptcha_MailHide_Exception */
0305             // require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
0306             throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
0307         }
0308 
0309         /* Generate the url */
0310         $url = $this->_getUrl();
0311 
0312         $enc = $this->getOption('encoding');
0313 
0314         /* Genrate the HTML used to represent the email address */
0315         $html = htmlentities($this->getEmailLocalPart(), ENT_COMPAT, $enc)
0316             . '<a href="'
0317                 . htmlentities($url, ENT_COMPAT, $enc)
0318                 . '" onclick="window.open(\''
0319                     . htmlentities($url, ENT_COMPAT, $enc)
0320                     . '\', \'\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='
0321                     . $this->_options['popupWidth']
0322                     . ',height='
0323                     . $this->_options['popupHeight']
0324                 . '\'); return false;" title="'
0325                 . $this->_options['linkTitle']
0326                 . '">' . $this->_options['linkHiddenText'] . '</a>@'
0327                 . htmlentities($this->getEmailDomainPart(), ENT_COMPAT, $enc);
0328 
0329         return $html;
0330     }
0331 
0332     /**
0333      * Get the url used on the "hidden" part of the email address
0334      *
0335      * @return string
0336      */
0337     protected function _getUrl()
0338     {
0339         /* Figure out how much we need to pad the email */
0340         $numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
0341 
0342         /* Pad the email */
0343         $emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
0344 
0345         /* Encrypt the email */
0346         $emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
0347 
0348         /* Return the url */
0349         return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
0350     }
0351 }