*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Service / ReCaptcha / MailHide.php
blobe8cca6c52bd49a9c8d70752a427ddc562f9df9fb
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Service
17 * @subpackage ReCaptcha
18 * @copyright Copyright (c) 2005-2009 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';
25 /**
26 * Zend_Service_ReCaptcha_MailHide
28 * @category Zend
29 * @package Zend_Service
30 * @subpackage ReCaptcha
31 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
33 * @version $Id: MailHide.php 16971 2009-07-22 18:05:45Z mikaelkael $
35 class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
37 /**#@+
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";
44 /**#@-*/
46 /**
47 * Url to the mailhide server
49 * @var string
51 const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
53 /**
54 * The email address to protect
56 * @var string
58 protected $_email = null;
60 /**
61 * Binary representation of the private key
63 * @var string
65 protected $_privateKeyPacked = null;
67 /**
68 * The local part of the email
70 * @var string
72 protected $_emailLocalPart = null;
74 /**
75 * The domain part of the email
77 * @var string
79 protected $_emailDomainPart = null;
81 /**
82 * Local constructor
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();
99 /* Merge if needed */
100 if (is_array($options)) {
101 $options = array_merge($this->getDefaultOptions(), $options);
102 } else {
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.
134 * @return string
136 public function __toString()
138 try {
139 $return = $this->getHtml();
140 } catch (Exception $e) {
141 $return = '';
142 trigger_error($e->getMessage(), E_USER_WARNING);
145 return $return;
149 * Get the default set of parameters
151 * @return array
153 public function getDefaultOptions()
155 return array(
156 'linkTitle' => 'Reveal this e-mail address',
157 'linkHiddenText' => '...',
158 'popupWidth' => 500,
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);
178 return $this;
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);
200 } else {
201 $emailParts[0] = substr($emailParts[0], 0, 4);
204 $this->_emailLocalPart = $emailParts[0];
205 $this->_emailDomainPart = $emailParts[1];
207 return $this;
211 * Get the email property
213 * @return string
215 public function getEmail()
217 return $this->_email;
221 * Get the local part of the email address
223 * @return string
225 public function getEmailLocalPart()
227 return $this->_emailLocalPart;
231 * Get the domain part of the email address
233 * @return string
235 public function getEmailDomainPart()
237 return $this->_emailDomainPart;
241 * Get the HTML code needed for the mail hide
243 * @param string $email
244 * @return string
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);
278 return $html;
282 * Get the url used on the "hidden" part of the email address
284 * @return string
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);
291 /* Pad the email */
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);
297 /* Return the url */
298 return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');