Development

#3550: sfReCaptchaValidator.class.php

You must first sign up to be able to contribute.

Ticket #3550: sfReCaptchaValidator.class.php

File sfReCaptchaValidator.class.php, 1.6 kB (added by djoos, 8 months ago)

recaptcha -> sf_re_captcha_plugin

Line 
1 <?php
2
3 /**
4  * Verifies the response.
5  *
6  * @package    symfony
7  * @subpackage sfReCaptchaValidator
8  * @author     Arthur Koziel <arthur@arthurkoziel.com>
9  * @version    SVN: $Id$
10  */
11 class sfReCaptchaValidator extends sfValidator
12 {
13   /**
14    * Execute this validator.
15    *
16    * @param mixed A file or parameter value/array
17    * @param error An error message reference
18    *
19    * @return bool true, if this validator executes successfully, otherwise false
20    */
21   public function execute(&$value, &$error)
22   {
23     // get values of challange and response fields
24     $challenge = $this->getContext()->getRequest()->getParameter('recaptcha_challenge_field');
25     $response = $this->getContext()->getRequest()->getParameter('recaptcha_response_field');
26
27     // validate the given response against the challenge
28     $resp = recaptcha_check_answer (sfConfig::get('app_sf_re_captcha_plugin_privatekey'),
29                                     $_SERVER["REMOTE_ADDR"],
30                                     $challenge,
31                                     $response);
32
33     // if invalid, return error
34     if (!$resp->is_valid)
35     {
36       $error = $resp->error;
37
38       return false;
39     }
40
41     return true;
42   }
43
44   /**
45    * Initializes the validator.
46    *
47    * @param sfContext The current application context
48    * @param array   An associative array of initialization parameters
49    *
50    * @return bool true, if initialization completes successfully, otherwise false
51    */
52   public function initialize($context, $parameters = null)
53   {
54     require ('recaptchalib.php');
55
56     parent::initialize($context);
57
58     return true;
59   }
60 }
61