Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Service/ReCaptcha/MailHide.php
Go to the documentation of this file.
00001 <?php
00023 require_once 'Zend/Service/ReCaptcha.php';
00024 
00035 class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
00036 {
00040     const ENCRYPTION_MODE = MCRYPT_MODE_CBC;
00041     const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128;
00042     const ENCRYPTION_BLOCK_SIZE = 16;
00043     const ENCRYPTION_IV = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
00051     const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
00052 
00058     protected $_email = null;
00059 
00063     protected $_emailValidator;
00064 
00070     protected $_privateKeyPacked = null;
00071 
00077     protected $_emailLocalPart = null;
00078 
00084     protected $_emailDomainPart = null;
00085 
00094     public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
00095     {
00096         /* Require the mcrypt extension to be loaded */
00097         $this->_requireMcrypt();
00098 
00099         /* If options is a Zend_Config object we want to convert it to an array so we can merge it with the default options */
00100         if ($options instanceof Zend_Config) {
00101             $options = $options->toArray();
00102         }
00103 
00104         /* Merge if needed */
00105         if (is_array($options)) {
00106             $options = array_merge($this->getDefaultOptions(), $options);
00107         } else {
00108             $options = $this->getDefaultOptions();
00109         }
00110 
00111         parent::__construct($publicKey, $privateKey, null, $options);
00112 
00113         if ($email !== null) {
00114             $this->setEmail($email);
00115         }
00116     }
00117 
00118 
00124     public function getEmailValidator()
00125     {
00126         if (null === $this->_emailValidator) {
00127             require_once 'Zend/Validate/EmailAddress.php';
00128             $this->setEmailValidator(new Zend_Validate_EmailAddress());
00129         }
00130         return $this->_emailValidator;
00131     }
00132 
00139     public function setEmailValidator(Zend_Validate_Interface $validator)
00140     {
00141         $this->_emailValidator = $validator;
00142         return $this;
00143     }
00144 
00145 
00151     protected function _requireMcrypt()
00152     {
00153         if (!extension_loaded('mcrypt')) {
00155             require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
00156 
00157             throw new Zend_Service_ReCaptcha_MailHide_Exception('Use of the Zend_Service_ReCaptcha_MailHide component requires the mcrypt extension to be enabled in PHP');
00158         }
00159     }
00160 
00169     public function __toString()
00170     {
00171         try {
00172             $return = $this->getHtml();
00173         } catch (Exception $e) {
00174             $return = '';
00175             trigger_error($e->getMessage(), E_USER_WARNING);
00176         }
00177 
00178         return $return;
00179     }
00180 
00186     public function getDefaultOptions()
00187     {
00188         return array(
00189             'encoding'       => 'UTF-8',
00190             'linkTitle'      => 'Reveal this e-mail address',
00191             'linkHiddenText' => '...',
00192             'popupWidth'     => 500,
00193             'popupHeight'    => 300,
00194         );
00195     }
00196 
00205     public function setPrivateKey($privateKey)
00206     {
00207         parent::setPrivateKey($privateKey);
00208 
00209         /* Pack the private key into a binary string */
00210         $this->_privateKeyPacked = pack('H*', $this->_privateKey);
00211 
00212         return $this;
00213     }
00214 
00223     public function setEmail($email)
00224     {
00225         $this->_email = $email;
00226 
00227         $validator = $this->getEmailValidator();
00228         if (!$validator->isValid($email)) {
00229             require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
00230             throw new Zend_Service_ReCaptcha_MailHide_Exception('Invalid email address provided');
00231         }
00232 
00233         $emailParts = explode('@', $email, 2);
00234 
00235         /* Decide on how much of the local part we want to reveal */
00236         if (strlen($emailParts[0]) <= 4) {
00237             $emailParts[0] = substr($emailParts[0], 0, 1);
00238         } else if (strlen($emailParts[0]) <= 6) {
00239             $emailParts[0] = substr($emailParts[0], 0, 3);
00240         } else {
00241             $emailParts[0] = substr($emailParts[0], 0, 4);
00242         }
00243 
00244         $this->_emailLocalPart = $emailParts[0];
00245         $this->_emailDomainPart = $emailParts[1];
00246 
00247         return $this;
00248     }
00249 
00255     public function getEmail()
00256     {
00257         return $this->_email;
00258     }
00259 
00265     public function getEmailLocalPart()
00266     {
00267         return $this->_emailLocalPart;
00268     }
00269 
00275     public function getEmailDomainPart()
00276     {
00277         return $this->_emailDomainPart;
00278     }
00279 
00287     public function getHtml($email = null)
00288     {
00289         if ($email !== null) {
00290             $this->setEmail($email);
00291         } elseif (null === ($email = $this->getEmail())) {
00293             require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
00294             throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
00295         }
00296 
00297         if ($this->_publicKey === null) {
00299             require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
00300             throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
00301         }
00302 
00303         if ($this->_privateKey === null) {
00305             require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
00306             throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
00307         }
00308 
00309         /* Generate the url */
00310         $url = $this->_getUrl();
00311 
00312         $enc = $this->getOption('encoding');
00313 
00314         /* Genrate the HTML used to represent the email address */
00315         $html = htmlentities($this->getEmailLocalPart(), ENT_COMPAT, $enc) 
00316             . '<a href="' 
00317                 . htmlentities($url, ENT_COMPAT, $enc) 
00318                 . '" onclick="window.open(\'' 
00319                     . htmlentities($url, ENT_COMPAT, $enc) 
00320                     . '\', \'\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='
00321                     . $this->_options['popupWidth'] 
00322                     . ',height=' 
00323                     . $this->_options['popupHeight'] 
00324                 . '\'); return false;" title="' 
00325                 . $this->_options['linkTitle'] 
00326                 . '">' . $this->_options['linkHiddenText'] . '</a>@' 
00327                 . htmlentities($this->getEmailDomainPart(), ENT_COMPAT, $enc);
00328 
00329         return $html;
00330     }
00331 
00337     protected function _getUrl()
00338     {
00339         /* Figure out how much we need to pad the email */
00340         $numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
00341 
00342         /* Pad the email */
00343         $emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
00344 
00345         /* Encrypt the email */
00346         $emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
00347 
00348         /* Return the url */
00349         return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
00350     }
00351 }
 All Data Structures Namespaces Files Functions Variables Enumerations