Index.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Refreshes captcha and returns JSON encoded URL to image (AJAX action)
  4. * Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
  5. *
  6. * Copyright © Magento, Inc. All rights reserved.
  7. * See COPYING.txt for license details.
  8. */
  9. namespace Magento\Captcha\Controller\Refresh;
  10. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  11. use Magento\Framework\App\Action\Context;
  12. class Index extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
  13. {
  14. /**
  15. * @var \Magento\Captcha\Helper\Data
  16. */
  17. protected $captchaHelper;
  18. /**
  19. * @var \Magento\Framework\Serialize\Serializer\Json
  20. */
  21. protected $serializer;
  22. /**
  23. * @param Context $context
  24. * @param \Magento\Captcha\Helper\Data $captchaHelper
  25. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  26. * @throws \RuntimeException
  27. */
  28. public function __construct(
  29. Context $context,
  30. \Magento\Captcha\Helper\Data $captchaHelper,
  31. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  32. ) {
  33. $this->captchaHelper = $captchaHelper;
  34. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  35. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  36. parent::__construct($context);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function execute()
  42. {
  43. $formId = $this->_request->getPost('formId');
  44. if (null === $formId) {
  45. $params = [];
  46. $content = $this->_request->getContent();
  47. if ($content) {
  48. $params = $this->serializer->unserialize($content);
  49. }
  50. $formId = isset($params['formId']) ? $params['formId'] : null;
  51. }
  52. $captchaModel = $this->captchaHelper->getCaptcha($formId);
  53. $captchaModel->generate();
  54. $block = $this->_view->getLayout()->createBlock($captchaModel->getBlockName());
  55. $block->setFormId($formId)->setIsAjax(true)->toHtml();
  56. $this->_response->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()]));
  57. $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
  58. }
  59. }