Просмотр исходного кода

#3371 Add captcha other than google recaptcha - core

RomanL 5 лет назад
Родитель
Сommit
d8d8afba2a

+ 4 - 4
install/sql/system.sql

@@ -114,9 +114,9 @@ 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_invisible', 'reCAPTCHA Invisible', 'BxTemplCaptchaReCAPTCHAInvisible', '');
+('sys_recaptcha_new', 'reCAPTCHA', 'BxTemplCaptchaReCAPTCHANew', ''),
+('sys_recaptcha_invisible', 'reCAPTCHA Invisible', 'BxTemplCaptchaReCAPTCHAInvisible', ''),
+('sys_hcaptcha', 'hCaptcha', 'BxTemplCaptchaHCaptcha', '');
 
 
 -- --------------------------------------------------------
@@ -313,7 +313,7 @@ INSERT INTO `sys_options`(`category_id`, `name`, `caption`, `value`, `type`, `ex
 (@iCategoryId, 'sys_redirect_after_email_confirmation', '_adm_stg_cpt_option_sys_redirect_after_email_confirmation', '', 'digit', '', '', '', '', 41),
 
 (@iCategoryId, 'sys_editor_default', '_adm_stg_cpt_option_sys_editor_default', 'sys_tinymce', 'digit', '', '', '', '', 50),
-(@iCategoryId, 'sys_captcha_default', '_adm_stg_cpt_option_sys_captcha_default', 'sys_recaptcha_new', 'digit', '', '', '', '', 51),
+(@iCategoryId, 'sys_captcha_default', '_adm_stg_cpt_option_sys_captcha_default', 'sys_recaptcha_new', 'select', 'sys_recaptcha_new,sys_recaptcha_invisible,sys_hcaptcha', '', '', '', 51),
 (@iCategoryId, 'sys_player_default', '_adm_stg_cpt_option_sys_player_default', 'sys_html5', 'digit', '', '', '', '', 55),
 (@iCategoryId, 'sys_player_default_format', '_adm_stg_cpt_option_sys_player_default_quality', 'sd', 'select', 'sd,hd', '', '', '', 56),
 

+ 2 - 2
modules/boonex/english/data/langs/system/en.xml

@@ -1569,8 +1569,8 @@
 	<string name="_adm_stg_cpt_option_sys_twilio_gate_from_number"><![CDATA[Default 'From' number for SMS]]></string>    
 	<string name="_adm_stg_cpt_option_sys_nominatim_server"><![CDATA[Nominatim server URL]]></string>    
 	<string name="_adm_stg_cpt_option_sys_nominatim_email"><![CDATA[Nominatim email to identify requests]]></string>
-	<string name="_adm_stg_cpt_option_sys_recaptcha_key_private"><![CDATA[reCAPTCHA private key]]></string>
-	<string name="_adm_stg_cpt_option_sys_recaptcha_key_public"><![CDATA[reCAPTCHA public key]]></string>
+	<string name="_adm_stg_cpt_option_sys_recaptcha_key_private"><![CDATA[reCAPTCHA/hCaptcha private key]]></string>
+	<string name="_adm_stg_cpt_option_sys_recaptcha_key_public"><![CDATA[reCAPTCHA/hCaptcha public key]]></string>
 	<string name="_adm_stg_cpt_option_sys_redirect_after_account_added"><![CDATA[Redirect after account creation to]]></string>
 	<string name="_adm_stg_cpt_option_sys_redirect_after_email_confirmation"><![CDATA[Redirect after email confirmation to]]></string>
         <string name="_adm_stg_cpt_option_sys_relations"><![CDATA[Enable relationships between]]></string>

+ 2 - 2
modules/boonex/russian/data/langs/system/ru.xml

@@ -1553,8 +1553,8 @@
 	<string name="_adm_stg_cpt_option_sys_twilio_gate_from_number"><![CDATA[Номер для отправки SMS по умолчанию]]></string>
 	<string name="_adm_stg_cpt_option_sys_nominatim_server"><![CDATA[Nominatim сервер (URL)]]></string>    
 	<string name="_adm_stg_cpt_option_sys_nominatim_email"><![CDATA[Nominatim Email адрес (чтобы идентифицировать запросы)]]></string>
-	<string name="_adm_stg_cpt_option_sys_recaptcha_key_private"><![CDATA[reCAPTCHA - приватный ключ]]></string>
-	<string name="_adm_stg_cpt_option_sys_recaptcha_key_public"><![CDATA[reCAPTCHA - публичный ключ]]></string>
+	<string name="_adm_stg_cpt_option_sys_recaptcha_key_private"><![CDATA[reCAPTCHA/hCaptcha - приватный ключ]]></string>
+	<string name="_adm_stg_cpt_option_sys_recaptcha_key_public"><![CDATA[reCAPTCHA/hCaptcha - публичный ключ]]></string>
 	<string name="_adm_stg_cpt_option_sys_redirect_after_account_added"><![CDATA[Перенаправлять после создания аккаунта на]]></string>
     <string name="_adm_stg_cpt_option_sys_redirect_after_email_confirmation"><![CDATA[Перенаправлять после подтверждения email-а на]]></string>
     <string name="_adm_stg_cpt_option_sys_relations"><![CDATA[Разрешить связи между]]></string>

+ 95 - 0
template/scripts/BxBaseCaptchaHCaptcha.php

@@ -0,0 +1,95 @@
+<?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 BxBaseCaptchaHCaptcha extends BxDolCaptcha
+{
+	protected $sApiUrl;
+	protected $sVerifyUrl;
+
+    public function __construct ($aObject, $oTemplate)
+    {
+        parent::__construct ($aObject, $oTemplate);
+		
+		$this->_sSkin = 'light';
+        $this->sApiUrl = 'https://js.hcaptcha.com/1/api.js';
+        $this->sVerifyUrl = 'https://hcaptcha.com/siteverify';
+    }
+
+    /**
+     * Display captcha.
+     */
+    public function display ($bDynamicMode = false)
+    {
+       $sCode = '';
+        $aApiParams = array();
+        if($bDynamicMode)  {
+        	$sPostfix = $this->_sObject;
+        	$sId = 'sys-captcha-' . $sPostfix;
+
+        	$sOnLoadFunction = 'onLoadCallback' . $sPostfix;
+        	$sOnLoadCode = "
+	        	var " . $sOnLoadFunction . " = function() {
+					hcaptcha.render('" . $sId . "', {
+						'sitekey': '" . $this->_sKeyPublic . "',
+						'theme': '" . $this->_sSkin . "'
+					});
+				};
+		        ";
+
+        	$aApiParams = array(
+        		'onload' => $sOnLoadFunction,
+        		'render' => 'explicit'
+        	);
+
+        	$sCode .= $this->_oTemplate->_wrapInTagJsCode($sOnLoadCode);
+        	$sCode .= '<div id="' . $sId . '"></div>';
+        }
+        else {
+        	$aApiParams = array(
+        		'render' => 'onload'
+        	);
+
+        	$sCode .= '<div class="h-captcha" data-sitekey="' . $this->_sKeyPublic . '" data-theme="' . $this->_sSkin . '"></div>';
+        }
+
+        $aApiParams['hl'] = BxDolLanguages::getInstance()->getCurrentLanguage();
+        $sCodeJs = $this->_oTemplate->addJs(bx_append_url_params($this->sApiUrl, $aApiParams), true);
+
+        return $sCodeJs . $sCode;
+    }
+
+    /**
+     * Check captcha.
+     */
+    public function check ()
+    {
+    	$mixedResponce = bx_file_get_contents($this->sVerifyUrl, array(
+    		'secret' => $this->_sKeyPrivate, 
+    		'response' => bx_process_input(bx_get('h-captcha-response'))
+    	), 'post');
+	
+    	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;
+    }
+}
+
+/** @} */