~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Captcha/ReCaptcha.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_Captcha
 
17
 * @subpackage Adapter
 
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
/** Zend_Captcha_Base */
 
23
require_once 'Zend/Captcha/Base.php';
 
24
 
 
25
/** Zend_Service_ReCaptcha */
 
26
require_once 'Zend/Service/ReCaptcha.php';
 
27
 
 
28
/**
 
29
 * ReCaptcha adapter
 
30
 * 
 
31
 * Allows to insert captchas driven by ReCaptcha service
 
32
 * 
 
33
 * @see http://recaptcha.net/apidocs/captcha/
 
34
 *
 
35
 * @category   Zend
 
36
 * @package    Zend_Captcha
 
37
 * @subpackage Adapter
 
38
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
39
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
40
 * @version    $Id: $
 
41
 */
 
42
class Zend_Captcha_ReCaptcha extends Zend_Captcha_Base 
 
43
{
 
44
    /**
 
45
     * Recaptcha public key
 
46
     *
 
47
     * @var string
 
48
     */
 
49
    protected $_pubkey;
 
50
 
 
51
    /**
 
52
     * Recaptcha private key
 
53
     *
 
54
     * @var string
 
55
     */
 
56
    protected $_privkey;
 
57
    
 
58
    /**@+
 
59
     * ReCaptcha Field names 
 
60
     * @var string
 
61
     */
 
62
    protected $_CHALLENGE = 'recaptcha_challenge_field';
 
63
    protected $_RESPONSE  = 'recaptcha_response_field';
 
64
    /**@-*/
 
65
     
 
66
    /**
 
67
     * Recaptcha service object
 
68
     *
 
69
     * @var Zend_Service_Recaptcha
 
70
     */
 
71
    protected $_service;
 
72
 
 
73
    /**
 
74
     * Parameters defined by the service
 
75
     * 
 
76
     * @var array
 
77
     */
 
78
    protected $_serviceParams = array();
 
79
 
 
80
    /**#@+
 
81
     * Error codes
 
82
     * @const string
 
83
     */
 
84
    const MISSING_VALUE = 'missingValue';
 
85
    const ERR_CAPTCHA   = 'errCaptcha';
 
86
    const BAD_CAPTCHA   = 'badCaptcha';
 
87
    /**#@-*/
 
88
 
 
89
    /**
 
90
     * Error messages
 
91
     * @var array
 
92
     */
 
93
    protected $_messageTemplates = array(
 
94
        self::MISSING_VALUE => 'Missing captcha fields',
 
95
        self::ERR_CAPTCHA   => 'Failed to validate captcha',
 
96
        self::BAD_CAPTCHA   => 'Captcha value is wrong: %value%',
 
97
    );
 
98
    
 
99
    /**
 
100
     * Retrieve ReCaptcha Private key
 
101
     *
 
102
     * @return string
 
103
     */
 
104
    public function getPrivkey() 
 
105
    {
 
106
        return $this->_privkey;
 
107
    }
 
108
    
 
109
    /**
 
110
     * Retrieve ReCaptcha Public key
 
111
     *
 
112
     * @return string
 
113
     */
 
114
    public function getPubkey() 
 
115
    {
 
116
        return $this->_pubkey;
 
117
    }
 
118
    
 
119
    /**
 
120
     * Set ReCaptcha Private key
 
121
     *
 
122
     * @param string $_privkey
 
123
     * @return Zend_Captcha_ReCaptcha
 
124
     */
 
125
    public function setPrivkey($privkey) 
 
126
    {
 
127
        $this->_privkey = $privkey;
 
128
        return $this;
 
129
    }
 
130
    
 
131
    /**
 
132
     * Set ReCaptcha public key
 
133
     *
 
134
     * @param string $_pubkey
 
135
     * @return Zend_Captcha_ReCaptcha
 
136
     */
 
137
    public function setPubkey($pubkey) 
 
138
    {
 
139
        $this->_pubkey = $pubkey;
 
140
        return $this;
 
141
    }
 
142
    
 
143
    /**
 
144
     * Constructor
 
145
     *
 
146
     * @param  array|Zend_Config $options 
 
147
     * @return void
 
148
     */
 
149
    public function __construct($options = null)
 
150
    {
 
151
        parent::__construct($options);
 
152
 
 
153
        $this->setService(new Zend_Service_ReCaptcha($this->getPubKey(), $this->getPrivKey()));
 
154
        $this->_serviceParams = $this->getService()->getParams();
 
155
 
 
156
        if ($options instanceof Zend_Config) {
 
157
            $options = $options->toArray();
 
158
        }
 
159
        if (!empty($options)) {
 
160
            $this->setOptions($options);
 
161
        }
 
162
    }
 
163
 
 
164
    /**
 
165
     * Set service object
 
166
     * 
 
167
     * @param  Zend_Service_ReCaptcha $service 
 
168
     * @return Zend_Captcha_ReCaptcha
 
169
     */
 
170
    public function setService(Zend_Service_ReCaptcha $service)
 
171
    {
 
172
        $this->_service = $service;
 
173
        return $this;
 
174
    }
 
175
 
 
176
    /**
 
177
     * Retrieve ReCaptcha service object
 
178
     * 
 
179
     * @return Zend_Service_ReCaptcha
 
180
     */
 
181
    public function getService()
 
182
    {
 
183
        return $this->_service;
 
184
    }
 
185
 
 
186
    /**
 
187
     * Set option
 
188
     *
 
189
     * If option is a service parameter, proxies to the service.
 
190
     * 
 
191
     * @param  string $key 
 
192
     * @param  mixed $value 
 
193
     * @return Zend_Captcha_ReCaptcha
 
194
     */
 
195
    public function setOption($key, $value)
 
196
    {
 
197
        $service = $this->getService();
 
198
        if (isset($this->_serviceParams[$key])) {
 
199
            $service->setParam($key, $value);
 
200
            return $this;
 
201
        }
 
202
        return parent::setOption($key, $value);
 
203
    }
 
204
    
 
205
    /**
 
206
     * Generate captcha
 
207
     *
 
208
     * @see Zend_Form_Captcha_Adapter::generate()
 
209
     * @return string
 
210
     */
 
211
    public function generate()
 
212
    {
 
213
        return "";
 
214
    }
 
215
 
 
216
    /**
 
217
     * Validate captcha
 
218
     *
 
219
     * @see    Zend_Validate_Interface::isValid()
 
220
     * @param  mixed $value
 
221
     * @return boolean
 
222
     */
 
223
    public function isValid($value, $context = null)
 
224
    {
 
225
        if (!is_array($value) && !is_array($context)) {
 
226
            $this->_error(self::MISSING_VALUE);
 
227
            return false;
 
228
        }
 
229
        if (!is_array($value) && is_array($context)) {
 
230
            $value = $context;
 
231
        }
 
232
 
 
233
        if (empty($value[$this->_CHALLENGE]) || empty($value[$this->_RESPONSE])) {
 
234
            $this->_error(self::MISSING_VALUE);
 
235
            return false;
 
236
        }
 
237
 
 
238
        $service = $this->getService();
 
239
        
 
240
        $res = $service->verify($value[$this->_CHALLENGE], $value[$this->_RESPONSE]); 
 
241
        
 
242
        if (!$res) {
 
243
            $this->_error(self::ERR_CAPTCHA);
 
244
            return false;
 
245
        }
 
246
        
 
247
        if (!$res->isValid()) {
 
248
            $this->_error(self::BAD_CAPTCHA, $res->getErrorCode());
 
249
            $service->setParam('error', $res->getErrorCode());
 
250
            return false;
 
251
        }
 
252
 
 
253
        return true;
 
254
    }
 
255
    
 
256
    /**
 
257
     * Render captcha
 
258
     * 
 
259
     * @param  Zend_View $view 
 
260
     * @param  mixed $element 
 
261
     * @return string
 
262
     */
 
263
    public function render(Zend_View_Interface $view, $element = null)
 
264
    {
 
265
        return $this->getService()->getHTML();
 
266
    }
 
267
}