Skip to content

Commit

Permalink
Merge pull request #2 from blackline/five-three-friendly-sw-23241
Browse files Browse the repository at this point in the history
[+1] SW-23241 Changing [] to array(), removing recaptcha resource references
  • Loading branch information
plouiegps authored Mar 23, 2018
2 parents fa2ab15 + 049a371 commit 367a298
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 47 deletions.
13 changes: 0 additions & 13 deletions example/application/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,4 @@ public function _initView()

return $view;
}

public function _initRecaptcha()
{
$config = \Zend_Registry::get('application');
$params = $config->recaptcha->toArray();

$params['messageTemplates'] = [
\Cgsmith\Validate\Recaptcha::INVALID_CAPTCHA => 'The captcha was invalid', // set custom/translated message
\Cgsmith\Validate\Recaptcha::CAPTCHA_EMPTY => 'The captcha must be completed'
];

\Zend_Registry::set('recaptcha', $params);
}
}
19 changes: 1 addition & 18 deletions src/Cgsmith/Form/Element/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ class Recaptcha extends \Zend_Form_Element
* @throws \Zend_Form_Exception
*/
public function __construct($spec = null, $options = null) {
$options = $this->_setKeysFromConfig($options);
$spec = $this->_setDefaultSpec($spec);
if (empty($options['siteKey']) || empty($options['secretKey'])) {
throw new \Zend_Exception('Site key and secret key must be specified.');
}
$this->_siteKey = trim($options['siteKey']); // trim the white space if there is any just to be sure
$this->_secretKey = trim($options['secretKey']); // trim the white space if there is any just to be sure
$this->addValidator('Recaptcha', false, ['secretKey' => $this->_secretKey]);
$this->addValidator('Recaptcha', false, array('secretKey' => $this->_secretKey));
$this->setAllowEmpty(false);
parent::__construct($spec, $options);
}
Expand All @@ -50,22 +49,6 @@ public function init() {
);
}

/**
* @param array $options
*
* @return array
*/
private function _setKeysFromConfig($options) {
$params = \Zend_Registry::get('recaptcha');
if (!empty($params['sitekey'])) {
$options['siteKey'] = $params['sitekey'];
}
if (!empty($params['secretkey'])) {
$options['secretKey'] = $params['secretkey'];
}
return $options;
}

/**
* @param string $spec
*
Expand Down
24 changes: 8 additions & 16 deletions src/Cgsmith/Validate/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ class Recaptcha extends \Zend_Validate_Abstract
/** @const string peer key for communication */
const PEER_KEY = 'www.google.com';

protected $_messageTemplates = [
protected $_messageTemplates = array(
self::INVALID_CAPTCHA => 'The captcha was invalid',
self::CAPTCHA_EMPTY => 'The captcha must be completed'
];
);

/**
* @param $options
*/
public function __construct($options) {
$this->_secretKey = $options['secretKey'];
$this->_overrideMessagesIfExisting();
}

/**
Expand Down Expand Up @@ -78,35 +77,28 @@ public function isValid($value, $context = null) {
* @link https://github.com/google/recaptcha
*/
protected function _verify($value) {
$queryString = http_build_query([
$queryString = http_build_query(array(
'secret' => $this->_secretKey,
'response' => $value,
'remoteIp' => $_SERVER['REMOTE_ADDR']
]);
));

/**
* PHP 5.6.0 changed the way you specify the peer name for SSL context options.
* Using "CN_name" will still work, but it will raise deprecated errors.
*/
$peerKey = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';
$context = stream_context_create([
'http' => [
$context = stream_context_create(array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => self::POST_METHOD,
'content' => $queryString,
'verify_peer' => true,
$peerKey => self::PEER_KEY
]
]);
)
));
$jsonObject = json_decode(file_get_contents(self::SITE_VERIFY_URL, false, $context));

return $jsonObject->success;
}

private function _overrideMessagesIfExisting() {
$params = \Zend_Registry::get('recaptcha');
if (!empty($params['messageTemplates']) && is_array($params['messageTemplates'])) {
$this->_messageTemplates = $params['messageTemplates'];
}
}
}

0 comments on commit 367a298

Please sign in to comment.