Skip to content

Commit

Permalink
Magento OS Fork commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Shytikov committed Oct 20, 2021
0 parents commit eb8bf25
Show file tree
Hide file tree
Showing 130 changed files with 8,754 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Block/Adminhtml/Captcha/DefaultCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Captcha\Block\Adminhtml\Captcha;

/**
* Captcha block for adminhtml area
*/
class DefaultCaptcha extends \Magento\Captcha\Block\Captcha\DefaultCaptcha
{
/**
* @var \Magento\Backend\Model\UrlInterface
*/
protected $_url;

/**
* @var \Magento\Backend\App\ConfigInterface
*/
protected $_config;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Captcha\Helper\Data $captchaData
* @param \Magento\Backend\Model\UrlInterface $url
* @param \Magento\Backend\App\ConfigInterface $config
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Captcha\Helper\Data $captchaData,
\Magento\Backend\Model\UrlInterface $url,
\Magento\Backend\App\ConfigInterface $config,
array $data = []
) {
parent::__construct($context, $captchaData, $data);
$this->_url = $url;
$this->_config = $config;
}

/**
* Returns URL to controller action which returns new captcha image
*
* @return string
*/
public function getRefreshUrl()
{
return $this->_url->getUrl(
'adminhtml/refresh/refresh',
['_secure' => $this->_config->isSetFlag('web/secure/use_in_adminhtml'), '_nosecret' => true]
);
}
}
54 changes: 54 additions & 0 deletions Block/Captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

/**
* Captcha block
*
* @author Magento Core Team <core@magentocommerce.com>
*/
namespace Magento\Captcha\Block;

/**
* @api
* @since 100.0.2
*/
class Captcha extends \Magento\Framework\View\Element\Template
{
/**
* Captcha data
*
* @var \Magento\Captcha\Helper\Data
*/
protected $_captchaData = null;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Captcha\Helper\Data $captchaData
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Captcha\Helper\Data $captchaData,
array $data = []
) {
$this->_captchaData = $captchaData;
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}

/**
* Renders captcha HTML (if required)
*
* @return string
*/
protected function _toHtml()
{
$blockPath = $this->_captchaData->getCaptcha($this->getFormId())->getBlockName();
$block = $this->getLayout()->createBlock($blockPath);
$block->setData($this->getData());
return $block->toHtml();
}
}
86 changes: 86 additions & 0 deletions Block/Captcha/DefaultCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Block\Captcha;

/**
* Captcha block
*/
class DefaultCaptcha extends \Magento\Framework\View\Element\Template
{
/**
* @var string
*/
protected $_template = 'Magento_Captcha::default.phtml';

/**
* @var string
*/
protected $_captcha;

/**
* @var \Magento\Captcha\Helper\Data
*/
protected $_captchaData;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Captcha\Helper\Data $captchaData
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Captcha\Helper\Data $captchaData,
array $data = []
) {
parent::__construct($context, $data);
$this->_captchaData = $captchaData;
}

/**
* Returns template path
*
* @return string
*/
public function getTemplate()
{
return $this->getIsAjax() ? '' : $this->_template;
}

/**
* Returns URL to controller action which returns new captcha image
*
* @return string
*/
public function getRefreshUrl()
{
$store = $this->_storeManager->getStore();
return $store->getUrl('captcha/refresh', ['_secure' => $store->isCurrentlySecure()]);
}

/**
* Renders captcha HTML (if required)
*
* @return string
*/
protected function _toHtml()
{
if ($this->getCaptchaModel()->isRequired()) {
$this->getCaptchaModel()->generate();
return parent::_toHtml();
}
return '';
}

/**
* Returns captcha model
*
* @return \Magento\Captcha\Model\CaptchaInterface
*/
public function getCaptchaModel()
{
return $this->_captchaData->getCaptcha($this->getFormId());
}
}
66 changes: 66 additions & 0 deletions Controller/Adminhtml/Refresh/Refresh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Refreshes captcha and returns JSON encoded URL to image (AJAX action)
* Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Controller\Adminhtml\Refresh;

class Refresh extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
protected $serializer;

/**
* @var \Magento\Captcha\Helper\Data
*/
protected $captchaHelper;

/**
* Refresh constructor.
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Captcha\Helper\Data $captchaHelper
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Captcha\Helper\Data $captchaHelper,
\Magento\Framework\Serialize\Serializer\Json $serializer
) {
parent::__construct($context);
$this->serializer = $serializer;
$this->captchaHelper = $captchaHelper;
}

/**
* {@inheritdoc}
*/
public function execute()
{
$formId = $this->getRequest()->getPost('formId');
$captchaModel = $this->captchaHelper->getCaptcha($formId);
$this->_view->getLayout()->createBlock(
$captchaModel->getBlockName()
)->setFormId(
$formId
)->setIsAjax(
true
)->toHtml();
$this->getResponse()->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()]));
$this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
}

/**
* Check if user has permissions to access this controller
*
* @return bool
*/
protected function _isAllowed()
{
return true;
}
}
65 changes: 65 additions & 0 deletions Controller/Refresh/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Refreshes captcha and returns JSON encoded URL to image (AJAX action)
* Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Controller\Refresh;

use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
{
/**
* @var \Magento\Captcha\Helper\Data
*/
protected $captchaHelper;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
protected $serializer;

/**
* @param Context $context
* @param \Magento\Captcha\Helper\Data $captchaHelper
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
Context $context,
\Magento\Captcha\Helper\Data $captchaHelper,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->captchaHelper = $captchaHelper;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
parent::__construct($context);
}

/**
* {@inheritdoc}
*/
public function execute()
{
$formId = $this->_request->getPost('formId');
if (null === $formId) {
$params = [];
$content = $this->_request->getContent();
if ($content) {
$params = $this->serializer->unserialize($content);
}
$formId = isset($params['formId']) ? $params['formId'] : null;
}
$captchaModel = $this->captchaHelper->getCaptcha($formId);
$captchaModel->generate();

$block = $this->_view->getLayout()->createBlock($captchaModel->getBlockName());
$block->setFormId($formId)->setIsAjax(true)->toHtml();
$this->_response->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()]));
$this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
}
}

0 comments on commit eb8bf25

Please sign in to comment.