diff --git a/example/application/Bootstrap.php b/example/application/Bootstrap.php index fc13b4f..252f589 100644 --- a/example/application/Bootstrap.php +++ b/example/application/Bootstrap.php @@ -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); - } } diff --git a/src/Cgsmith/Form/Element/Recaptcha.php b/src/Cgsmith/Form/Element/Recaptcha.php index 0aa4fa8..19ef164 100644 --- a/src/Cgsmith/Form/Element/Recaptcha.php +++ b/src/Cgsmith/Form/Element/Recaptcha.php @@ -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); } @@ -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 * diff --git a/src/Cgsmith/Validate/Recaptcha.php b/src/Cgsmith/Validate/Recaptcha.php index db6189f..0913a3e 100644 --- a/src/Cgsmith/Validate/Recaptcha.php +++ b/src/Cgsmith/Validate/Recaptcha.php @@ -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(); } /** @@ -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']; - } - } }