7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_Service
17
* @subpackage ReCaptcha
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
22
/** @see Zend_Service_ReCaptcha */
23
require_once 'Zend/Service/ReCaptcha.php';
26
* Zend_Service_ReCaptcha_MailHide
29
* @package Zend_Service
30
* @subpackage ReCaptcha
31
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
32
* @license http://framework.zend.com/license/new-bsd New BSD License
35
class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
38
* Encryption constants
40
const ENCRYPTION_MODE = MCRYPT_MODE_CBC;
41
const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128;
42
const ENCRYPTION_BLOCK_SIZE = 16;
43
const ENCRYPTION_IV = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
47
* Url to the mailhide server
51
const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
54
* The email address to protect
58
protected $_email = null;
61
* Binary representation of the private key
65
protected $_privateKeyPacked = null;
68
* The local part of the email
72
protected $_emailLocalPart = null;
75
* The domain part of the email
79
protected $_emailDomainPart = null;
84
* @param string $publicKey
85
* @param string $privateKey
86
* @param string $email
87
* @param array|Zend_Config $options
89
public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
91
/* Require the mcrypt extension to be loaded */
92
$this->_requireMcrypt();
94
/* If options is a Zend_Config object we want to convert it to an array so we can merge it with the default options */
95
if ($options instanceof Zend_Config) {
96
$options = $options->toArray();
100
if (is_array($options)) {
101
$options = array_merge($this->getDefaultOptions(), $options);
103
$options = $this->getDefaultOptions();
106
parent::__construct($publicKey, $privateKey, null, $options);
108
if ($email !== null) {
109
$this->setEmail($email);
114
* See if the mcrypt extension is available
116
* @throws Zend_Service_ReCaptcha_MailHide_Exception
118
protected function _requireMcrypt()
120
if (!extension_loaded('mcrypt')) {
121
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
122
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
124
throw new Zend_Service_ReCaptcha_MailHide_Exception('Use of the Zend_Service_ReCaptcha_MailHide component requires the mcrypt extension to be enabled in PHP');
129
* Serialize as string
131
* When the instance is used as a string it will display the email address. Since we can't
132
* throw exceptions within this method we will trigger a user warning instead.
136
public function __toString()
139
$return = $this->getHtml();
140
} catch (Exception $e) {
142
trigger_error($e->getMessage(), E_USER_WARNING);
149
* Get the default set of parameters
153
public function getDefaultOptions()
156
'linkTitle' => 'Reveal this e-mail address',
157
'linkHiddenText' => '...',
159
'popupHeight' => 300,
164
* Override the setPrivateKey method
166
* Override the parent method to store a binary representation of the private key as well.
168
* @param string $privateKey
169
* @return Zend_Service_ReCaptcha_MailHide
171
public function setPrivateKey($privateKey)
173
parent::setPrivateKey($privateKey);
175
/* Pack the private key into a binary string */
176
$this->_privateKeyPacked = pack('H*', $this->_privateKey);
182
* Set the email property
184
* This method will set the email property along with the local and domain parts
186
* @param string $email
187
* @return Zend_Service_ReCaptcha_MailHide
189
public function setEmail($email)
191
$this->_email = $email;
193
$emailParts = explode('@', $email, 2);
195
/* Decide on how much of the local part we want to reveal */
196
if (strlen($emailParts[0]) <= 4) {
197
$emailParts[0] = substr($emailParts[0], 0, 1);
198
} else if (strlen($emailParts[0]) <= 6) {
199
$emailParts[0] = substr($emailParts[0], 0, 3);
201
$emailParts[0] = substr($emailParts[0], 0, 4);
204
$this->_emailLocalPart = $emailParts[0];
205
$this->_emailDomainPart = $emailParts[1];
211
* Get the email property
215
public function getEmail()
217
return $this->_email;
221
* Get the local part of the email address
225
public function getEmailLocalPart()
227
return $this->_emailLocalPart;
231
* Get the domain part of the email address
235
public function getEmailDomainPart()
237
return $this->_emailDomainPart;
241
* Get the HTML code needed for the mail hide
243
* @param string $email
245
* @throws Zend_Service_ReCaptcha_MailHide_Exception
247
public function getHtml($email = null)
249
if ($email !== null) {
250
$this->setEmail($email);
251
} else if ($this->_email === null) {
252
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
253
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
255
throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
258
if ($this->_publicKey === null) {
259
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
260
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
262
throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
265
if ($this->_privateKey === null) {
266
/** @see Zend_Service_ReCaptcha_MailHide_Exception */
267
require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
269
throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
272
/* Generate the url */
273
$url = $this->_getUrl();
275
/* Genrate the HTML used to represent the email address */
276
$html = htmlentities($this->_emailLocalPart) . '<a href="' . htmlentities($url) . '" onclick="window.open(\'' . htmlentities($url) . '\', \'\', \'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->_emailDomainPart);
282
* Get the url used on the "hidden" part of the email address
286
protected function _getUrl()
288
/* Figure out how much we need to pad the email */
289
$numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
292
$emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
294
/* Encrypt the email */
295
$emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
298
return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
b'\\ No newline at end of file'