diff --git a/image_captcha/js/image_captcha_refresh.js b/image_captcha/js/image_captcha_refresh.js new file mode 100644 index 0000000000000000000000000000000000000000..86ba382f2869cbe28b4dd37039acc2c3e5cd1c55 --- /dev/null +++ b/image_captcha/js/image_captcha_refresh.js @@ -0,0 +1,45 @@ +/** + * @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); diff --git a/image_captcha/src/Controller/CaptchaImageRefresh.php b/image_captcha/src/Controller/CaptchaImageRefresh.php new file mode 100644 index 0000000000000000000000000000000000000000..23d00171767461831a2bd0cb13be25cd3ce2fc2c --- /dev/null +++ b/image_captcha/src/Controller/CaptchaImageRefresh.php @@ -0,0 +1,64 @@ +<?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); + } + +} diff --git a/image_captcha/templates/image-captcha-refresh.html.twig b/image_captcha/templates/image-captcha-refresh.html.twig new file mode 100644 index 0000000000000000000000000000000000000000..9de177b7e7e2c23f5db7196c983b099329f52866 --- /dev/null +++ b/image_captcha/templates/image-captcha-refresh.html.twig @@ -0,0 +1,19 @@ +{# +/** + * @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>