Bladeren bron

Ticket #933 - reCaptcha Invisible.

AntonLV 9 jaren geleden
bovenliggende
commit
a9d329acc7

+ 2 - 1
install/sql/system.sql

@@ -77,7 +77,8 @@ CREATE TABLE `sys_objects_captcha` (
 
 INSERT INTO `sys_objects_captcha` (`object`, `title`, `override_class_name`, `override_class_file`) VALUES
 ('sys_recaptcha', 'reCAPTCHA', 'BxTemplCaptchaReCAPTCHA', ''),
-('sys_recaptcha_new', 'reCAPTCHA New', 'BxTemplCaptchaReCAPTCHANew', '');
+('sys_recaptcha_new', 'reCAPTCHA New', 'BxTemplCaptchaReCAPTCHANew', ''),
+('sys_recaptcha_invisible', 'reCAPTCHA Invisible', 'BxTemplCaptchaReCAPTCHAInvisible', '');
 
 
 -- --------------------------------------------------------

+ 21 - 0
modules/boonex/decorous/data/template/system/scripts/BxTemplCaptchaReCAPTCHAInvisible.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolCaptcha
+ */
+class BxTemplCaptchaReCAPTCHAInvisible extends BxBaseCaptchaReCAPTCHAInvisible
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/ocean/data/template/system/scripts/BxTemplCaptchaReCAPTCHAInvisible.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolCaptcha
+ */
+class BxTemplCaptchaReCAPTCHAInvisible extends BxBaseCaptchaReCAPTCHAInvisible
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 21 - 0
modules/boonex/protean/data/template/system/scripts/BxTemplCaptchaReCAPTCHAInvisible.php

@@ -0,0 +1,21 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaTemplate UNA Template Classes
+ * @{
+ */
+
+/**
+ * @see BxDolCaptcha
+ */
+class BxTemplCaptchaReCAPTCHAInvisible extends BxBaseCaptchaReCAPTCHAInvisible
+{
+    public function __construct ($aObject, $oTemplate = false)
+    {
+        parent::__construct ($aObject, $oTemplate);
+    }
+}
+
+/** @} */

+ 87 - 0
template/scripts/BxBaseCaptchaReCAPTCHAInvisible.php

@@ -0,0 +1,87 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaBaseView UNA Base Representation Classes
+ * @{
+ */
+
+/**
+ * reCAPTCHA representation.
+ * @see BxDolCaptcha
+ */
+class BxBaseCaptchaReCAPTCHAInvisible extends BxTemplCaptchaReCAPTCHA
+{
+	protected $sApiUrl;
+	protected $sVerifyUrl;
+
+    public function __construct ($aObject, $oTemplate)
+    {
+        parent::__construct ($aObject, $oTemplate);
+
+        $this->sApiUrl = 'https://www.google.com/recaptcha/api.js';
+        $this->sVerifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
+    }
+
+    /**
+     * Display captcha.
+     */
+    public function display ($bDynamicMode = false)
+    {
+        $sCode = '';
+        $sId = 'sys-captcha-' . $this->_sObject;
+
+        $sCode .= '<div id="' . $sId . '"></div>';
+        
+        $sPostfix = bx_gen_method_name($this->_sObject);
+        $sOnLoadFunction = 'onLoadCallback' . $sPostfix;
+        $sOnSubmitFunction = 'onSubmitCallback' . $sPostfix;
+        $sOnLoadCode = "
+        	var " . $sOnLoadFunction . " = function() {
+        		grecaptcha.render('" . $sId . "', {
+        			'sitekey': '" . $this->_sKeyPublic . "',
+        			'size': 'invisible',
+        			'badge': 'inline'
+        		});
+
+        		grecaptcha.execute();
+        	};
+            ";
+        $sCode .= $this->_oTemplate->_wrapInTagJsCode($sOnLoadCode);
+        
+
+        $sCode .= $this->_oTemplate->addJs(bx_append_url_params($this->sApiUrl, array(
+        	'onload' => $sOnLoadFunction,
+        	'render' => 'explicit',
+			'hl' => BxDolLanguages::getInstance()->getCurrentLanguage()
+        )), true);
+
+        return $sCode;
+    }
+
+    /**
+     * Check captcha.
+     */
+    public function check ()
+    {
+    	$mixedResponce = bx_file_get_contents($this->sVerifyUrl, array(
+    		'secret' => $this->_sKeyPrivate, 
+    		'response' => bx_process_input(bx_get('g-recaptcha-response')),
+    		'remoteip' => getVisitorIP()
+    	));
+    	if($mixedResponce === false)
+    		return false;
+
+    	$aResponce = json_decode($mixedResponce, true); 	
+    	if(isset($aResponce['success']) && $aResponce['success'] === true)
+    		return true;
+
+		if(!empty($aResponce['error-codes']))
+			$this->_error = $aResponce['error-codes'];
+
+		return false;
+    }
+}
+
+/** @} */