Commit 6aa25be3 authored by gg24's avatar gg24 Committed by elachlan

Issue #2608540 by gg24, bpresles, RavindraSingh, sakural, ddrozdik, wundo,...

Issue #2608540 by gg24, bpresles, RavindraSingh, sakural, ddrozdik, wundo, rsvelko, naveenvalecha: Add Image CAPTCHA Refresh to the module patch
parent 1ece9427
/**
* @file
* Attaches behaviors for the zipang captcha refresh module.
*/
(function ($) {
/**
* Attaches jQuery validate behavoir to forms.
*
* @type {Drupal~behavior}
*
* @prop {Drupal~behaviorAttach} attach
* Attaches the outline behavior to the right context.
*/
Drupal.behaviors.CaptchaRefresh = {
attach: function (context) {
$('.reload-captcha', context).not('.processed').bind('click', function () {
$(this).addClass('processed');
var $form = $(this).parents('form');
// Send post query for getting new captcha data.
var date = new Date();
var baseUrl = document.location.origin;
var url = baseUrl + '/' + $(this).attr('href') + '?' + date.getTime();
$.get(
url,
{},
function (response) {
if(response.status == 1) {
$('.captcha', $form).find('img').attr('src', response.data.url);
$('input[name=captcha_sid]', $form).val(response.data.sid);
$('input[name=captcha_token]', $form).val(response.data.token);
}
else {
alert(response.message);
}
},
'json'
);
return false;
});
}
};
})(jQuery);
<?php
/*
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Drupal\image_captcha\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Database\Database;
/**
* Description of CaptchaImageRefresh.
*/
class CaptchaImageRefresh extends ControllerBase {
/**
* Put your code here.
*/
public function refreshCaptcha($form_id = NULL) {
$result = [
'status' => 0,
'message' => '',
];
try {
module_load_include('inc', 'captcha', 'captcha');
$config = \Drupal::config('image_captcha.settings');
$captcha_sid = _captcha_generate_captcha_session($form_id);
$captcha_token = md5(mt_rand());
$allowed_chars = _image_captcha_utf8_split($config->get('image_captcha_image_allowed_chars', IMAGE_CAPTCHA_ALLOWED_CHARACTERS));
$code_length = (int) $config->get('image_captcha_code_length');
$code = '';
for ($i = 0; $i < $code_length; $i++) {
$code .= $allowed_chars[array_rand($allowed_chars)];
}
$connection = Database::getConnection();
$connection->update('captcha_sessions')
->fields([
'token' => $captcha_token,
'solution' => $code,
])
->condition('csid', $captcha_sid, '=')
->execute();
$result['data'] = [
'url' => \Drupal::url('image_captcha.generator', ['session_id' => $captcha_sid, 'timestamp' => \Drupal::time()->getRequestTime()]),
'token' => $captcha_token,
'sid' => $captcha_sid,
];
$result['status'] = 1;
}
catch (\Exception $e) {
if ($message = $e->getMessage()) {
$result['message'] = $message;
}
else {
$result['message'] = $this->t('Error has occurred. Please contact to site administrator.');
}
}
return new JsonResponse($result);
}
}
{#
/**
* @file
* Custom theme implementation for custom field type defined.
*
* Available variables:
* - uri: An optional URL the image can be linked to.
* - title: An optional Title value which will be shown as text.
* - link_value: Value used to open the link in new or same tab.
*
* @see template_preprocess_custom_zipang_link_formatter()
*
* @ingroup themeable
*/
#}
{{ attach_library('image_captcha/image-captcha-refresh') }}
<div class="reload-captcha-wrapper">
{{captcha_refresh_link}}
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment