Office + recaptchaV2

Как интегрировать recaptchaV2 с фомрмой авторизации/регистрации office? В доках и на форуме ничего подобного не нашел…
Ярослав
23 октября 2017, 10:09
modx.pro
897
0

Комментарии: 4

Эрадж Шамс
06 ноября 2017, 10:46
0
Здравствуйте, не нашли решения?
    Ярослав
    06 ноября 2017, 12:05
    0
    к сожалению нет(
    Прохор
    11 декабря 2017, 10:24
    0
    День добрый. Есть у кого то мысли как прикрутить капчу?
      Михаил
      25 октября 2018, 16:20
      0
      Можно прикрутить таким образом:
      1. Делаем всё по инструкции — www.createit.ru/blog/modx/2017/recaptchav2-i-neskolko-form-na-odnoj-stranicze/
      2. Создаем 2 плагина с одинаковым выводом кроме системного события
      Первый — recaptchav2_OfficeAuth, и включаем ему событие «OnWebAuthentication»
      <?php
      if ($modx->context->key != 'mgr') {
          switch ($modx->event->name) {
              case 'OnWebAuthentication':
                  //$modx->log(1,print_r($_POST,1));
                  if(!isset($_POST['g-recaptcha-response'])){
                      $modx->event->output('Enter recapcha!');
                  }
                  // Register API keys at https://www.google.com/recaptcha/admin
                  $site_key = $modx->getOption('recaptchav2.site_key', null, '');
                  $secret = $modx->getOption('recaptchav2.secret_key', null, '');
                  // reCAPTCHA supported 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
                  $lang = $modx->getOption('cultureKey', null, 'en');
                  // make sure the modLexicon class is loaded by instantiating 
                  $modx->getService('lexicon','modLexicon');
                  // load lexicon
                  $modx->lexicon->load('recaptchav2:default');
                  // get the message from default.inc.php from the correct lang
                  $tech_err_msg = $modx->lexicon('recaptchav2.technical_error_message');
                  $recaptcha_err_msg = $modx->lexicon('recaptchav2.recaptcha_error_message');
                  
                  // Get the class
                  $recaptchav2Path = $modx->getOption('recaptchav2.core_path', null, $modx->getOption('core_path') . 'components/recaptchav2/');
                  $recaptchav2Path .= 'model/recaptchav2/';
                  if (!file_exists($recaptchav2Path . 'autoload.php')) {
                      $modx->log(modX::LOG_LEVEL_WARN, 'Cannot find required RecaptchaV2 autoload.php file.'); 
                      return false;
                  }
                  require_once($recaptchav2Path . 'autoload.php');
                  $recaptchav2 = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
                  if (!($recaptchav2 instanceof \ReCaptcha\ReCaptcha)) {
                      //$hook->addError('recaptchav2_error', $tech_err_msg);
                      
                      $modx->log(modX::LOG_LEVEL_WARN, 'Failed to load recaptchav2 class.'); 
                      $modx->event->output($tech_err_msg);
                  }
                  
                  // The response from reCAPTCHA
                  $resp = null;
                  // The error code from reCAPTCHA, if any
                  $error = null;
                  
                  // Was there a reCAPTCHA response?
                  if ($_POST['g-recaptcha-response']) {
                      $resp = $recaptchav2->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"]);
                  }
                  
                  // Hook pass/fail
                  if ($resp != null && $resp->isSuccess()) {
                      
                  } else {
                      //$hook->addError('recaptchav2_error', $recaptcha_err_msg);
                      //DEBUG INFO: $modx->log(modX::LOG_LEVEL_ERROR, print_r($resp, true));
                      $modx->event->output($recaptcha_err_msg);
                  }
                  break;
          }
      }
      Второй — recaptchav2_Officeregist, событие — OnBeforeUserFormSave
      <?php
      if ($modx->context->key != 'mgr') {
          switch ($modx->event->name) {
              case 'OnBeforeUserFormSave':
                  //$modx->log(1,print_r($_POST,1));
                  if(!isset($_POST['g-recaptcha-response'])){
                      $modx->event->output('Enter recapcha!');
                  }
                  // Register API keys at https://www.google.com/recaptcha/admin
                  $site_key = $modx->getOption('recaptchav2.site_key', null, '');
                  $secret = $modx->getOption('recaptchav2.secret_key', null, '');
                  // reCAPTCHA supported 40+ languages listed here: https://developers.google.com/recaptcha/docs/language
                  $lang = $modx->getOption('cultureKey', null, 'en');
                  // make sure the modLexicon class is loaded by instantiating 
                  $modx->getService('lexicon','modLexicon');
                  // load lexicon
                  $modx->lexicon->load('recaptchav2:default');
                  // get the message from default.inc.php from the correct lang
                  $tech_err_msg = $modx->lexicon('recaptchav2.technical_error_message');
                  $recaptcha_err_msg = $modx->lexicon('recaptchav2.recaptcha_error_message');
                  
                  // Get the class
                  $recaptchav2Path = $modx->getOption('recaptchav2.core_path', null, $modx->getOption('core_path') . 'components/recaptchav2/');
                  $recaptchav2Path .= 'model/recaptchav2/';
                  if (!file_exists($recaptchav2Path . 'autoload.php')) {
                      $modx->log(modX::LOG_LEVEL_WARN, 'Cannot find required RecaptchaV2 autoload.php file.'); 
                      return false;
                  }
                  require_once($recaptchav2Path . 'autoload.php');
                  $recaptchav2 = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
                  if (!($recaptchav2 instanceof \ReCaptcha\ReCaptcha)) {
                      //$hook->addError('recaptchav2_error', $tech_err_msg);
                      
                      $modx->log(modX::LOG_LEVEL_WARN, 'Failed to load recaptchav2 class.'); 
                      $modx->event->output($tech_err_msg);
                  }
                  
                  // The response from reCAPTCHA
                  $resp = null;
                  // The error code from reCAPTCHA, if any
                  $error = null;
                  
                  // Was there a reCAPTCHA response?
                  if ($_POST['g-recaptcha-response']) {
                      $resp = $recaptchav2->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"]);
                  }
                  
                  // Hook pass/fail
                  if ($resp != null && $resp->isSuccess()) {
                      
                  } else {
                      //$hook->addError('recaptchav2_error', $recaptcha_err_msg);
                      //DEBUG INFO: $modx->log(modX::LOG_LEVEL_ERROR, print_r($resp, true));
                      $modx->event->output($recaptcha_err_msg);
                  }
                  break;
          }
      }

      У меня работает так
        Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
        4