Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Split CAPTCHA checking method into library class

  • Loading branch information...
commit c36f1e4878536dad95aeff2df788bd7be16f7e14 1 parent bde1d21
@rintaun authored
Showing with 63 additions and 57 deletions.
  1. +56 −0 Lib/Recaptcha.php
  2. +7 −57 Model/Behavior/CaptchaValidationBehavior.php
View
56 Lib/Recaptcha.php
@@ -0,0 +1,56 @@
+<?php
+
+class Recaptcha {
+ const VERIFY_SERVER = 'www.google.com';
+
+ /**
+ * Issues an HTTP GET request to verify if the user's guess was correct
+ *
+ * @param string $challenge
+ * @param string $response
+ * @param array $extra_params an array of extra variables to post to the server
+ * @return RecaptchaResponse
+ */
+ public static function check($challenge, $response, $extra_params = array()) {
+ Configure::load('Recaptcha.key');
+ $privateKey = Configure::read('Recaptcha.Private');
+ if (empty($privateKey)) {
+ throw new RuntimeException(__('Private key not available. Get one from https://www.google.com/recaptcha/admin/create'));
+ }
+
+ $remoteip = $_SERVER['REMOTE_ADDR'];
+ if (empty($remoteip)) {
+ throw new RuntimeException(__('Remote IP is not available'));
+ }
+
+ if (empty($challenge) || empty($response)) {
+ return false;
+ }
+
+ $http = new HttpSocket(array(
+ 'request' => array(
+ 'uri' => array(
+ 'scheme' => 'http',
+ 'host' => static::VERIFY_SERVER,
+ 'port' => 80
+ ),
+ ),
+ ));
+ $data = array (
+ 'privatekey' => $privateKey,
+ 'remoteip' => $remoteip,
+ 'challenge' => $challenge,
+ 'response' => $response
+ ) + $extra_params;
+ $options = array(
+ 'header' => array('User-Agent' => 'reCAPTCHA/CakePHP')
+ );
+ $http_response = $http->get('/recaptcha/api/verify', $data, $options);
+
+ $answers = explode("\n", $http_response);
+ if (trim($answers[0]) === 'true') {
+ return true;
+ }
+ return false;
+ }
+}
View
64 Model/Behavior/CaptchaValidationBehavior.php
@@ -1,7 +1,5 @@
<?php
class CaptchaValidationBehavior extends ModelBehavior {
- const VERIFY_SERVER = 'www.google.com';
-
public function beforeValidate(Model $model) {
$model->validate += array(
'recaptcha_response_field' => array(
@@ -16,65 +14,17 @@ public function beforeValidate(Model $model) {
}
public function checkRecaptcha(Model $model, $field) {
+ App::uses('Recaptcha', 'Recaptcha.Lib');
+ if (!class_exists('Recaptcha')) {
+ throw new InternalErrorException(__('Recaptcha library not found'));
+ }
try {
- return $this->getRecaptchaResponse(
- $model->data['User']['recaptcha_challenge_field'],
- $field['recaptcha_response_field']
+ return Recaptcha::check(
+ $model->data[$model->alias]['recaptcha_challenge_field'],
+ $model->data[$model->alias]['recaptcha_response_field']
);
} catch (Exception $e) {
return false;
}
}
-
- /**
- * Issues an HTTP GET request to verify if the user's guess was correct
- *
- * @param string $challenge
- * @param string $response
- * @param array $extra_params an array of extra variables to post to the server
- * @return RecaptchaResponse
- */
- protected function getRecaptchaResponse($challenge, $response, $extra_params = array())
- {
- Configure::load('Recaptcha.key');
- $privateKey = Configure::read('Recaptcha.Private');
- if (empty($privateKey)) {
- throw new RuntimeException(__('Private key not available. Get one from https://www.google.com/recaptcha/admin/create'));
- }
-
- $remoteip = $_SERVER['REMOTE_ADDR'];
- if (empty($remoteip)) {
- throw new RuntimeException(__('Remote IP is not available'));
- }
-
- if (empty($challenge) || empty($response)) {
- return false;
- }
-
- $http = new HttpSocket(array(
- 'request' => array(
- 'uri' => array(
- 'scheme' => 'http',
- 'host' => static::VERIFY_SERVER,
- 'port' => 80
- ),
- ),
- ));
- $data = array (
- 'privatekey' => $privateKey,
- 'remoteip' => $remoteip,
- 'challenge' => $challenge,
- 'response' => $response
- ) + $extra_params;
- $options = array(
- 'header' => array('User-Agent' => 'reCAPTCHA/CakePHP')
- );
- $http_response = $http->get('/recaptcha/api/verify', $data, $options);
-
- $answers = explode("\n", $http_response);
- if (trim($answers[0]) === 'true') {
- return true;
- }
- return false;
- }
}
Please sign in to comment.
Something went wrong with that request. Please try again.