Files
bio-concept-labo/web/modules/invisiblerecaptcha/invisiblerecaptcha.php
2019-11-20 07:44:43 +01:00

198 lines
6.6 KiB
PHP

<?php
/**
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2017 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once 'src/loader.php';
class Invisiblerecaptcha extends Module
{
protected $config_form = false;
public function __construct()
{
$this->name = 'invisiblerecaptcha';
$this->tab = 'front_office_features';
$this->version = '1.0.3';
$this->author = 'Wiktor Koźmiński';
$this->need_instance = 0;
$this->module_key = 'd61d218797b110c7691f596ee1f19533';
/**
* Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
*/
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Google Invisible reCAPTCHA');
$this->description = $this->l('Protects your shop from SPAM using invisible Google reCAPTCHA algorithm');
}
/**
* Don't forget to create update methods if needed:
* http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
*/
public function install()
{
return parent::install() &&
$this->registerHook('header') &&
$this->registerHook('actionFrontControllerSetMedia') &&
$this->registerHook('validateCustomerFormFields');
}
public function uninstall()
{
return parent::uninstall();
}
/**
* Load the configuration form
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
*/
if (((bool)Tools::isSubmit('submitInvisiblerecaptchaModule')) == true) {
$this->postProcess();
}
$this->context->smarty->assign('module_dir', $this->_path);
$output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
return $output.$this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$config = new Invisiblerecaptcha\Common\Config();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitInvisiblerecaptchaModule';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $config->getConfigFormValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array(Invisiblerecaptcha\Common\Config::getConfigForm($this)));
}
/**
* Save form data.
*/
protected function postProcess()
{
$valid = true;
if (!Validate::isString(Tools::getValue('config_sitekey')) || !Tools::strlen(Tools::getValue('config_sitekey'))) {
$this->context->controller->errors[] = $this->l('Please provide valid sitekey');
$valid = false;
}
if (!Validate::isString(Tools::getValue('config_secretkey')) || !Tools::strlen(Tools::getValue('config_secretkey'))) {
$this->context->controller->errors[] = $this->l('Please provide valid secretkey');
$valid = false;
}
if ($valid) {
$config = new Invisiblerecaptcha\Common\Config();
$config->update();
$this->context->controller->confirmations[] = $this->l('Configuration form was saved');
}
}
/**
* Add the CSS & JavaScript files you want to be added on the FO.
*/
public function hookHeader()
{
if (trim($this->context->controller->php_self) != 'contact') {
return;
}
$config = new Invisiblerecaptcha\Common\Config();
$this->context->controller->addJS($this->_path.'/views/js/front.min.js');
$this->context->smarty->assign(array(
'recaptchaSiteKey' => $config->sitekey
));
return $this->display(__FILE__, 'header_hook.tpl');
}
/**
* We are checking "recaptcha" code in "setMedia", because this is the only hook
* which is executeed BEFORE "postProcess" (where the mails are sent).
* If recaptcha code is wrong we removing 'from' key from POST and GET array to
* prevent message from being sent and from being added to conversation table.
*
* @see Controller::run
* @see FrontController::setMedia
* @return void
*/
public function hookActionFrontControllerSetMedia()
{
if (trim($this->context->controller->php_self) != 'contact') {
return;
}
$config = new Invisiblerecaptcha\Common\Config();
if (Tools::isSubmit('submitMessage')) {
$code = Tools::getValue('g-recaptcha-response');
$recaptcha = new Invisiblerecaptcha\Recaptcha\Recaptcha($config->secretkey);
if (!$code || !$recaptcha->verifyResponse($code)) {
$this->context->controller->errors[] = $this->l("Sorry, but it looks like you are not a human");
unset($_POST['from']);
unset($_GET['from']);
}
}
}
public function hookValidateCustomerFormFields()
{
/* Place your code here. */
}
}