Skip to content

Commit

Permalink
RecaptchaComponent::verify() does not always return CakeDC#33
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Krämer committed Jul 6, 2014
1 parent 5fb9794 commit 4888cc5
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions Controller/Component/RecaptchaComponent.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
/**
* Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
* Copyright 2009-2014, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2009-2010, Cake Development Corporation (http://cakedc.com)
* @copyright Copyright 2009-2014, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

Expand Down Expand Up @@ -62,21 +62,22 @@ class RecaptchaComponent extends Component {
*/
public $settings = array();

/**
/**
* Default Options
*
* @var array
*/
protected $_defaults = array(
protected $_defaults = array(
'errorField' => 'recaptcha',
'actions' => array()
);
);

/**
* Constructor
*
* @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
* @param array $settings Array of configuration settings
* @return RecaptchaComponent
*/
public function __construct(ComponentCollection $collection, $settings = array()) {
parent::__construct($collection, $settings);
Expand All @@ -87,13 +88,15 @@ public function __construct(ComponentCollection $collection, $settings = array()
unset($this->settings['actions']);
}

/**
/**
* Callback
*
* @param object Controller object
* @param Array $settings
* @param Controller $controller
* @param array $settings
* @throws Exception Throws an exception if Recaptchas config is not present
* @return void
*/
public function initialize(Controller $controller) {
public function initialize(Controller $controller, $settings = array()) {
if ($controller->name == 'CakeError') {
return;
}
Expand All @@ -105,14 +108,15 @@ public function initialize(Controller $controller) {
}

if (empty($this->privateKey)) {
throw new Exception(__d('recaptcha', "You must set your private recaptcha key using Configure::write('Recaptcha.privateKey', 'your-key');!", true));
throw new Exception(__d('recaptcha', "You must set your private Recaptcha key using Configure::write('Recaptcha.privateKey', 'your-key');!", true));
}
}

/**
/**
* Callback
*
* @param object Controller object
* @param Controller $controller
* @return void
*/
public function startup(Controller $controller) {
extract($this->settings);
Expand All @@ -139,7 +143,7 @@ public function startup(Controller $controller) {
* @return boolean True if the response was correct
*/
public function verify() {
if (isset($this->Controller->request->data['recaptcha_challenge_field']) &&
if (isset($this->Controller->request->data['recaptcha_challenge_field']) &&
isset($this->Controller->request->data['recaptcha_response_field'])) {

$response = $this->_getApiResponse();
Expand All @@ -159,9 +163,8 @@ public function verify() {
} else {
$this->error = $response[1];
}

return false;
}
return false;
}

/**
Expand All @@ -172,10 +175,11 @@ public function verify() {
protected function _getApiResponse() {
$Socket = new HttpSocket();
return $Socket->post($this->apiUrl, array(
'privatekey'=> $this->privateKey,
'privatekey' => $this->privateKey,
'remoteip' => env('REMOTE_ADDR'),
'challenge' => $this->Controller->request->data['recaptcha_challenge_field'],
'response' => $this->Controller->request->data['recaptcha_response_field']));
'response' => $this->Controller->request->data['recaptcha_response_field']
));
}

}

0 comments on commit 4888cc5

Please sign in to comment.