1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage ReCaptcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** @see Zend_Service_ReCaptcha */
require_once 'Zend/Service/ReCaptcha.php';
/**
* Zend_Service_ReCaptcha_MailHide
*
* @category Zend
* @package Zend_Service
* @subpackage ReCaptcha
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: MailHide.php 20108 2010-01-06 22:05:31Z matthew $
*/
class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
{
/**#@+
* Encryption constants
*/
const ENCRYPTION_MODE = MCRYPT_MODE_CBC;
const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128;
const ENCRYPTION_BLOCK_SIZE = 16;
const ENCRYPTION_IV = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
/**#@-*/
/**
* Url to the mailhide server
*
* @var string
*/
const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
/**
* The email address to protect
*
* @var string
*/
protected $_email = null;
/**
* @var Zend_Validate_Interface
*/
protected $_emailValidator;
/**
* Binary representation of the private key
*
* @var string
*/
protected $_privateKeyPacked = null;
/**
* The local part of the email
*
* @var string
*/
protected $_emailLocalPart = null;
/**
* The domain part of the email
*
* @var string
*/
protected $_emailDomainPart = null;
/**
* Local constructor
*
* @param string $publicKey
* @param string $privateKey
* @param string $email
* @param array|Zend_Config $options
*/
public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
{
/* Require the mcrypt extension to be loaded */
$this->_requireMcrypt();
/* If options is a Zend_Config object we want to convert it to an array so we can merge it with the default options */
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
/* Merge if needed */
if (is_array($options)) {
$options = array_merge($this->getDefaultOptions(), $options);
} else {
$options = $this->getDefaultOptions();
}
parent::__construct($publicKey, $privateKey, null, $options);
if ($email !== null) {
$this->setEmail($email);
}
}
/**
* Get emailValidator
*
* @return Zend_Validate_Interface
*/
public function getEmailValidator()
{
if (null === $this->_emailValidator) {
require_once 'Zend/Validate/EmailAddress.php';
$this->setEmailValidator(new Zend_Validate_EmailAddress());
}
return $this->_emailValidator;
}
/**
* Set email validator
*
* @param Zend_Validate_Interface $validator
* @return Zend_Service_ReCaptcha_MailHide
*/
public function setEmailValidator(Zend_Validate_Interface $validator)
{
$this->_emailValidator = $validator;
return $this;
}
/**
* See if the mcrypt extension is available
*
* @throws Zend_Service_ReCaptcha_MailHide_Exception
*/
protected function _requireMcrypt()
{
if (!extension_loaded('mcrypt')) {
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
throw new Zend_Service_ReCaptcha_MailHide_Exception('Use of the Zend_Service_ReCaptcha_MailHide component requires the mcrypt extension to be enabled in PHP');
}
}
/**
* Serialize as string
*
* When the instance is used as a string it will display the email address. Since we can't
* throw exceptions within this method we will trigger a user warning instead.
*
* @return string
*/
public function __toString()
{
try {
$return = $this->getHtml();
} catch (Exception $e) {
$return = '';
trigger_error($e->getMessage(), E_USER_WARNING);
}
return $return;
}
/**
* Get the default set of parameters
*
* @return array
*/
public function getDefaultOptions()
{
return array(
'encoding' => 'UTF-8',
'linkTitle' => 'Reveal this e-mail address',
'linkHiddenText' => '...',
'popupWidth' => 500,
'popupHeight' => 300,
);
}
/**
* Override the setPrivateKey method
*
* Override the parent method to store a binary representation of the private key as well.
*
* @param string $privateKey
* @return Zend_Service_ReCaptcha_MailHide
*/
public function setPrivateKey($privateKey)
{
parent::setPrivateKey($privateKey);
/* Pack the private key into a binary string */
$this->_privateKeyPacked = pack('H*', $this->_privateKey);
return $this;
}
/**
* Set the email property
*
* This method will set the email property along with the local and domain parts
*
* @param string $email
* @return Zend_Service_ReCaptcha_MailHide
*/
public function setEmail($email)
{
$this->_email = $email;
$validator = $this->getEmailValidator();
if (!$validator->isValid($email)) {
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
throw new Zend_Service_ReCaptcha_MailHide_Exception('Invalid email address provided');
}
$emailParts = explode('@', $email, 2);
/* Decide on how much of the local part we want to reveal */
if (strlen($emailParts[0]) <= 4) {
$emailParts[0] = substr($emailParts[0], 0, 1);
} else if (strlen($emailParts[0]) <= 6) {
$emailParts[0] = substr($emailParts[0], 0, 3);
} else {
$emailParts[0] = substr($emailParts[0], 0, 4);
}
$this->_emailLocalPart = $emailParts[0];
$this->_emailDomainPart = $emailParts[1];
return $this;
}
/**
* Get the email property
*
* @return string
*/
public function getEmail()
{
return $this->_email;
}
/**
* Get the local part of the email address
*
* @return string
*/
public function getEmailLocalPart()
{
return $this->_emailLocalPart;
}
/**
* Get the domain part of the email address
*
* @return string
*/
public function getEmailDomainPart()
{
return $this->_emailDomainPart;
}
/**
* Get the HTML code needed for the mail hide
*
* @param string $email
* @return string
* @throws Zend_Service_ReCaptcha_MailHide_Exception
*/
public function getHtml($email = null)
{
if ($email !== null) {
$this->setEmail($email);
} elseif (null === ($email = $this->getEmail())) {
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
}
if ($this->_publicKey === null) {
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
}
if ($this->_privateKey === null) {
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
}
/* Generate the url */
$url = $this->_getUrl();
$enc = $this->getOption('encoding');
/* Genrate the HTML used to represent the email address */
$html = htmlentities($this->getEmailLocalPart(), ENT_COMPAT, $enc)
. '<a href="'
. htmlentities($url, ENT_COMPAT, $enc)
. '" onclick="window.open(\''
. htmlentities($url, ENT_COMPAT, $enc)
. '\', \'\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='
. $this->_options['popupWidth']
. ',height='
. $this->_options['popupHeight']
. '\'); return false;" title="'
. $this->_options['linkTitle']
. '">' . $this->_options['linkHiddenText'] . '</a>@'
. htmlentities($this->getEmailDomainPart(), ENT_COMPAT, $enc);
return $html;
}
/**
* Get the url used on the "hidden" part of the email address
*
* @return string
*/
protected function _getUrl()
{
/* Figure out how much we need to pad the email */
$numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
/* Pad the email */
$emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
/* Encrypt the email */
$emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
/* Return the url */
return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
}
}
|