Пример #1
0
 /**
  * Check request reCAPTCHA validity
  * This method return `true` or `false` after validation, and set error in
  * helper. If `true` error is set to null, otherwise `'incorrect-captcha-sol'`
  * 
  * Example:
  * {{{
  *		class YourController extends \lithium\action\Controller {
  *			public function index() {
  *				if ($this->request->data) {
  *					if (!Recaptcha::check($this->request)) {
  *						return;
  *					}
  *				}
  *			}
  *		}
  * }}}
  * @param object $request Pass request object to check method
  * @return boolean
  * @throws ConfigException
  * @throws RuntimeException 
  */
 public static function check(\lithium\net\http\Request $request)
 {
     $config = Libraries::get('li3_recaptcha', 'keys');
     if (!$config['private']) {
         throw new ConfigException('To use reCAPTCHA you must get API key from' . 'https://www.google.com/recaptcha/admin/create');
     }
     if (!$request->env('SERVER_ADDR')) {
         throw new RuntimeException('For security reasons, you must pass the remote ip to reCAPTCHA');
     }
     $data = array('privatekey' => $config['private'], 'remoteip' => $request->env('SERVER_ADDR'), 'challenge' => null, 'response' => null);
     if ($request->data) {
         $data['challenge'] = $request->data['recaptcha_challenge_field'];
         $data['response'] = $request->data['recaptcha_response_field'];
     }
     if (!$data['challenge'] || !$data['response']) {
         RecaptchaHelper::$error = 'incorrect-captcha-sol';
         return false;
     }
     $service = Connections::get('recaptcha');
     $serviceRespond = explode("\n", $service->post('/recaptcha/api/verify', $data));
     if ($serviceRespond[0] == 'true') {
         RecaptchaHelper::$error = null;
         return true;
     } else {
         RecaptchaHelper::$error = 'incorrect-captcha-sol';
         return false;
     }
 }