~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Service/ReCaptcha/MailHide.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
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.
 
14
 *
 
15
 * @category   Zend
 
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
 
20
 */
 
21
 
 
22
/** @see Zend_Service_ReCaptcha */
 
23
require_once 'Zend/Service/ReCaptcha.php';
 
24
 
 
25
/**
 
26
 * Zend_Service_ReCaptcha_MailHide
 
27
 *
 
28
 * @category   Zend
 
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
 
33
 * @version    $Id$
 
34
 */
 
35
class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
 
36
{
 
37
    /**#@+
 
38
     * Encryption constants
 
39
     */
 
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
    /**#@-*/
 
45
 
 
46
    /**
 
47
     * Url to the mailhide server
 
48
     *
 
49
     * @var string
 
50
     */
 
51
    const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
 
52
 
 
53
    /**
 
54
     * The email address to protect
 
55
     *
 
56
     * @var string
 
57
     */
 
58
    protected $_email = null;
 
59
 
 
60
    /**
 
61
     * Binary representation of the private key
 
62
     *
 
63
     * @var string
 
64
     */
 
65
    protected $_privateKeyPacked = null;
 
66
 
 
67
    /**
 
68
     * The local part of the email
 
69
     *
 
70
     * @var string
 
71
     */
 
72
    protected $_emailLocalPart = null;
 
73
 
 
74
    /**
 
75
     * The domain part of the email
 
76
     *
 
77
     * @var string
 
78
     */
 
79
    protected $_emailDomainPart = null;
 
80
 
 
81
    /**
 
82
     * Local constructor
 
83
     *
 
84
     * @param string $publicKey
 
85
     * @param string $privateKey
 
86
     * @param string $email
 
87
     * @param array|Zend_Config $options
 
88
     */
 
89
    public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
 
90
    {
 
91
        /* Require the mcrypt extension to be loaded */
 
92
        $this->_requireMcrypt();
 
93
 
 
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();
 
97
        }
 
98
 
 
99
        /* Merge if needed */
 
100
        if (is_array($options)) {
 
101
            $options = array_merge($this->getDefaultOptions(), $options);
 
102
        } else {
 
103
            $options = $this->getDefaultOptions();
 
104
        }
 
105
 
 
106
        parent::__construct($publicKey, $privateKey, null, $options);
 
107
 
 
108
        if ($email !== null) {
 
109
            $this->setEmail($email);
 
110
        }
 
111
    }
 
112
 
 
113
    /**
 
114
     * See if the mcrypt extension is available
 
115
     *
 
116
     * @throws Zend_Service_ReCaptcha_MailHide_Exception
 
117
     */
 
118
    protected function _requireMcrypt()
 
119
    {
 
120
        if (!extension_loaded('mcrypt')) {
 
121
            /** @see Zend_Service_ReCaptcha_MailHide_Exception */
 
122
            require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
 
123
 
 
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');
 
125
        }
 
126
    }
 
127
 
 
128
    /**
 
129
     * Serialize as string
 
130
     *
 
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.
 
133
     *
 
134
     * @return string
 
135
     */
 
136
    public function __toString()
 
137
    {
 
138
        try {
 
139
            $return = $this->getHtml();
 
140
        } catch (Exception $e) {
 
141
            $return = '';
 
142
            trigger_error($e->getMessage(), E_USER_WARNING);
 
143
        }
 
144
 
 
145
        return $return;
 
146
    }
 
147
 
 
148
    /**
 
149
     * Get the default set of parameters
 
150
     *
 
151
     * @return array
 
152
     */
 
153
    public function getDefaultOptions()
 
154
    {
 
155
        return array(
 
156
            'linkTitle'      => 'Reveal this e-mail address',
 
157
            'linkHiddenText' => '...',
 
158
            'popupWidth'     => 500,
 
159
            'popupHeight'    => 300,
 
160
        );
 
161
    }
 
162
 
 
163
    /**
 
164
     * Override the setPrivateKey method
 
165
     *
 
166
     * Override the parent method to store a binary representation of the private key as well.
 
167
     *
 
168
     * @param string $privateKey
 
169
     * @return Zend_Service_ReCaptcha_MailHide
 
170
     */
 
171
    public function setPrivateKey($privateKey)
 
172
    {
 
173
        parent::setPrivateKey($privateKey);
 
174
 
 
175
        /* Pack the private key into a binary string */
 
176
        $this->_privateKeyPacked = pack('H*', $this->_privateKey);
 
177
 
 
178
        return $this;
 
179
    }
 
180
 
 
181
    /**
 
182
     * Set the email property
 
183
     *
 
184
     * This method will set the email property along with the local and domain parts
 
185
     *
 
186
     * @param string $email
 
187
     * @return Zend_Service_ReCaptcha_MailHide
 
188
     */
 
189
    public function setEmail($email)
 
190
    {
 
191
        $this->_email = $email;
 
192
 
 
193
        $emailParts = explode('@', $email, 2);
 
194
 
 
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);
 
202
        }
 
203
 
 
204
        $this->_emailLocalPart = $emailParts[0];
 
205
        $this->_emailDomainPart = $emailParts[1];
 
206
 
 
207
        return $this;
 
208
    }
 
209
 
 
210
    /**
 
211
     * Get the email property
 
212
     *
 
213
     * @return string
 
214
     */
 
215
    public function getEmail()
 
216
    {
 
217
        return $this->_email;
 
218
    }
 
219
 
 
220
    /**
 
221
     * Get the local part of the email address
 
222
     *
 
223
     * @return string
 
224
     */
 
225
    public function getEmailLocalPart()
 
226
    {
 
227
        return $this->_emailLocalPart;
 
228
    }
 
229
 
 
230
    /**
 
231
     * Get the domain part of the email address
 
232
     *
 
233
     * @return string
 
234
     */
 
235
    public function getEmailDomainPart()
 
236
    {
 
237
        return $this->_emailDomainPart;
 
238
    }
 
239
 
 
240
    /**
 
241
     * Get the HTML code needed for the mail hide
 
242
     *
 
243
     * @param string $email
 
244
     * @return string
 
245
     * @throws Zend_Service_ReCaptcha_MailHide_Exception
 
246
     */
 
247
    public function getHtml($email = null)
 
248
    {
 
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';
 
254
 
 
255
            throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
 
256
        }
 
257
 
 
258
        if ($this->_publicKey === null) {
 
259
            /** @see Zend_Service_ReCaptcha_MailHide_Exception */
 
260
            require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
 
261
 
 
262
            throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
 
263
        }
 
264
 
 
265
        if ($this->_privateKey === null) {
 
266
            /** @see Zend_Service_ReCaptcha_MailHide_Exception */
 
267
            require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
 
268
 
 
269
            throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
 
270
        }
 
271
 
 
272
        /* Generate the url */
 
273
        $url = $this->_getUrl();
 
274
 
 
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);
 
277
 
 
278
        return $html;
 
279
    }
 
280
 
 
281
    /**
 
282
     * Get the url used on the "hidden" part of the email address
 
283
     *
 
284
     * @return string
 
285
     */
 
286
    protected function _getUrl()
 
287
    {
 
288
        /* Figure out how much we need to pad the email */
 
289
        $numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
 
290
 
 
291
        /* Pad the email */
 
292
        $emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
 
293
 
 
294
        /* Encrypt the email */
 
295
        $emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
 
296
 
 
297
        /* Return the url */
 
298
        return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
 
299
    }
 
300
}
 
 
b'\\ No newline at end of file'