Skip to content

Commit

Permalink
Merge pull request #8 from othyn/master
Browse files Browse the repository at this point in the history
Add empty response error message
  • Loading branch information
shevabam authored May 13, 2018
2 parents 805f310 + 553ae72 commit e94090e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"source": "https://github.com/shevabam/recaptcha/tree/master"
},
"license": "GPL-2.0-only",
"version": "1.0.7",
"version": "1.0.8",
"require": {
"php": ">=5.3.0"
},
Expand Down
85 changes: 48 additions & 37 deletions src/reCAPTCHA.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* reCAPTCHA v2 class
*
*
* @author ShevAbam
* @link https://github.com/shevabam/recaptcha
* @license GNU GPL 2.0
Expand All @@ -13,98 +13,98 @@ class reCAPTCHA
{
/**
* ReCAPTCHA URL verifying
*
*
* @var string
*/
const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

/**
* Public key
*
*
* @var string
*/
private $siteKey;

/**
* Private key
*
*
* @var string
*/
private $secretKey;

/**
* Remote IP address
*
*
* @var string
*/
protected $remoteIp = null;

/**
* Supported themes
*
*
* @var array
* @see https://developers.google.com/recaptcha/docs/display#config
*/
protected static $themes = array('light', 'dark');

/**
* Captcha theme. Default : light
*
*
* @var string
* @see https://developers.google.com/recaptcha/docs/display#config
*/
protected $theme = null;

/**
* Supported types
*
*
* @var array
* @see https://developers.google.com/recaptcha/docs/display#config
*/
protected static $types = array('image', 'audio');

/**
* Captcha type. Default : image
*
*
* @var string
* @see https://developers.google.com/recaptcha/docs/display#config
*/
protected $type = null;

/**
* Captcha language. Default : auto-detect
*
*
* @var string
* @see https://developers.google.com/recaptcha/docs/language
*/
protected $language = null;

/**
/**
* CURL timeout (in seconds) to verify response
*
* @var int
*/
private $verifyTimeout = 1;

/**
* Captcha size. Default : normal
*
*
* @var string
* @see https://developers.google.com/recaptcha/docs/display#render_param
*/
protected $size = null;

/**
* List of errors
*
*
* @var array
*/
protected $errorCodes = array();


/**
* Initialize site and secret keys
*
*
* @param string $siteKey Site key from ReCaptcha dashboard
* @param string $secretKey Secret key from ReCaptcha dashboard
* @return void
Expand All @@ -117,7 +117,7 @@ public function __construct($siteKey = null, $secretKey = null)

/**
* Set site key
*
*
* @param string $key
* @return object
*/
Expand All @@ -130,7 +130,7 @@ public function setSiteKey($key)

/**
* Set secret key
*
*
* @param string $key
* @return object
*/
Expand All @@ -143,7 +143,7 @@ public function setSecretKey($key)

/**
* Set remote IP address
*
*
* @param string $ip
* @return object
*/
Expand Down Expand Up @@ -209,10 +209,10 @@ public function setLanguage($language)
public function setVerifyTimeout($timeout)
{
$this->verifyTimeout = $timeout;

return $this;
}

/**
* Set size
*
Expand All @@ -228,7 +228,7 @@ public function setSize($size)

/**
* Generate the JS code of the captcha
*
*
* @return string
*/
public function getScript()
Expand All @@ -242,7 +242,7 @@ public function getScript()

/**
* Generate the HTML code block for the captcha
*
*
* @return string
*/
public function getHtml()
Expand All @@ -266,26 +266,30 @@ public function getHtml()

/**
* Checks the code given by the captcha
*
*
* @param string $response Response code after submitting form (usually $_POST['g-recaptcha-response'])
* @return bool
*/
public function isValid($response)
{
if (is_null($this->secretKey))
throw new \Exception('You must set your secret key');

if (empty($response))

if (empty($response)) {

$this->errorCodes = array('internal-empty-response');

return false;
}

$params = array(
'secret' => $this->secretKey,
'response' => $response,
'remoteip' => $this->remoteIp,
'secret' => $this->secretKey,
'response' => $response,
'remoteip' => $this->remoteIp,
);

$url = self::VERIFY_URL.'?'.http_build_query($params);

if (function_exists('curl_version'))
{
$curl = curl_init($url);
Expand All @@ -299,12 +303,12 @@ public function isValid($response)
{
$response = file_get_contents($url);
}

if (empty($response) || is_null($response) || !$response)
{
return false;
}

$json = json_decode($response, true);

if (isset($json['error-codes']))
Expand All @@ -317,7 +321,7 @@ public function isValid($response)

/**
* Returns the errors encountered
*
*
* @return array Errors code and name
*/
public function getErrorCodes()
Expand All @@ -336,28 +340,28 @@ public function getErrorCodes()
'name' => 'Timeout or duplicate.',
);
break;

case 'missing-input-secret':
$errors[] = array(
'code' => $error,
'name' => 'The secret parameter is missing.',
);
break;

case 'invalid-input-secret':
$errors[] = array(
'code' => $error,
'name' => 'The secret parameter is invalid or malformed.',
);
break;

case 'missing-input-response':
$errors[] = array(
'code' => $error,
'name' => 'The response parameter is missing.',
);
break;

case 'invalid-input-response':
$errors[] = array(
'code' => $error,
Expand All @@ -372,6 +376,13 @@ public function getErrorCodes()
);
break;

case 'internal-empty-response':
$errors[] = array(
'code' => $error,
'name' => 'The recaptcha response is required.',
);
break;

default:
$errors[] = array(
'code' => $error,
Expand All @@ -383,4 +394,4 @@ public function getErrorCodes()

return $errors;
}
}
}

0 comments on commit e94090e

Please sign in to comment.