Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
Add debug infor around comms with google
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas van Staden committed Nov 14, 2018
1 parent d28ed8c commit 8de6aea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
24 changes: 21 additions & 3 deletions app/code/community/ProxiBlue/ReCaptcha/Model/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function generate()
$this->_private_key = $this->_getHelper()->getConfigNode('private_key');
$this->_public_key = $this->_getHelper()->getConfigNode('public_key');
$this->_position = $this->_getHelper()->getConfigNode('position');
$this->_debugEnabled = Mage::getStoreConfigFlag('customer/captcha/debug');
}

public function getLanguage()
Expand Down Expand Up @@ -108,37 +109,46 @@ public function isCorrect($word)
try {
$request = Mage::app()->getRequest();
$this->generate();
$this->_debug(print_r($request->getParams(),true),null,'recapctha.log');
// is this the new 'I am not a robot'?
if($request->getParam('gcr')) {
$request->setParam('g-recaptcha-response', $request->getParam('gcr'));
$this->_debug("gcr request was mapped to g-recaptcha-response");
}
if ($response = $request->getParam('g-recaptcha-response')) {
$path = ProxiBlue_ReCaptcha_Helper_Data::RECAPTCHA_SITEVERIFY_PATH;
$params = array('secret' => $this->_private_key,
'response' => $response
);
$this->_debug("sending to " . $path . " params of " . print_r($params, true));
$result = $this->_sendRequest($path, $params);
$this->_debug("result is : " . $result);
$response = json_decode($result);

if (is_object($response) && $response->success == true) {
return true;
} elseif(is_object($response)) {
$this->_debug("error " . print_r($response,true));
Mage::throwException(print_r($response,true));
}
} else {
$this->_debug("No 'g-recaptcha-response' in request! - building ");
$params = array('privatekey' => $this->_private_key,
'challenge' => $request->getParam('recaptcha_challenge_field'),
'response' => $request->getParam('recaptcha_response_field'),
);

$path = ProxiBlue_ReCaptcha_Helper_Data::RECAPTCHA_VERIFY_PATH;
$this->_debug("sending to " . $path . " params of " . print_r($params, true));
$result = $this->_sendRequest($path, $params);
$this->_debug("result is : " . $result);
$answers = explode("\n", $result);
if (is_array($answers) && array_key_exists('0', $answers)) {
return (trim($answers[0]) == 'true') ? true : false;
}
}
} catch (Exception $e) {
Mage::log($e->getMessage());
$this->_debug("Exception fail : " . $e->getMessage());
//Mage::log($e->getMessage());
}

return false;
Expand All @@ -163,7 +173,8 @@ private function _sendRequest($path, $params)
$httpRequest->setParameterPost(array_merge(array('remoteip' => $_SERVER['REMOTE_ADDR']), $params));
$response = $httpRequest->request('POST');
if ($response->getStatus() != 200) {
Mage::throwException('Bad response from cpatcha gateway. we got ' . $response->getStatus());
$this->_debug('Bad response from captcha gateway. we got ' . $response->getStatus());
Mage::throwException('Bad response from captcha gateway. we got ' . $response->getStatus());
}

return $response->getBody();
Expand Down Expand Up @@ -200,4 +211,11 @@ public function isRequired($login = null)
|| $this->getSession()->getData($this->_getFormIdKey('show_captcha'))
);
}

private function _debug($message) {
if($this->_debugEnabled) {
$message = "Form ID: ". $this->_formId . "=>" . $message;
Mage::log($message, null, 'recapctha.log');
}
}
}
2 changes: 1 addition & 1 deletion app/code/community/ProxiBlue/ReCaptcha/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<ProxiBlue_ReCaptcha>
<version>2.0.0</version>
<version>2.0.1</version>
<depends>
<Mage_Captcha/>
</depends>
Expand Down
9 changes: 9 additions & 0 deletions app/code/community/ProxiBlue/ReCaptcha/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@
<show_in_store>1</show_in_store>
<depends><enable>1</enable><type>recaptcha</type></depends>
</language>
<debug translate="label">
<label>Enable DEBUG to recaptcha.log</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</debug>
</fields>
</captcha>
</groups>
Expand Down

0 comments on commit 8de6aea

Please sign in to comment.