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 66fe90b
Show file tree
Hide file tree
Showing 93 changed files with 7,218 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Block/Adminhtml/Captcha/DefaultCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

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]
);
}
}
50 changes: 50 additions & 0 deletions Block/Captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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;

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();
}
}
88 changes: 88 additions & 0 deletions Block/Captcha/DefaultCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Block\Captcha;

/**
* Captcha block
*
* @author Magento Core Team <core@magentocommerce.com>
*/
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());
}
}
40 changes: 40 additions & 0 deletions Controller/Adminhtml/Refresh/Refresh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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
{
/**
* {@inheritdoc}
*/
public function execute()
{
$formId = $this->getRequest()->getPost('formId');
$captchaModel = $this->_objectManager->get('Magento\Captcha\Helper\Data')->getCaptcha($formId);
$this->_view->getLayout()->createBlock(
$captchaModel->getBlockName()
)->setFormId(
$formId
)->setIsAjax(
true
)->toHtml();
$this->getResponse()->representJson(json_encode(['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;
}
}
61 changes: 61 additions & 0 deletions Controller/Refresh/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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\Context;

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

/**
* @param Context $context
* @param \Magento\Captcha\Helper\Data $captchaHelper
*/
public function __construct(Context $context, \Magento\Captcha\Helper\Data $captchaHelper)
{
$this->captchaHelper = $captchaHelper;
parent::__construct($context);
}

/**
* @inheritdoc
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function execute()
{
if (!$this->getRequest()->isPost()) {
throw new \Magento\Framework\Exception\NotFoundException(__('Page not found.'));
}

$formId = $this->_request->getPost('formId');
if (null === $formId) {
try {
$params = [];
$content = $this->_request->getContent();
if ($content) {
$params = \Zend_Json::decode($content);
}
$formId = isset($params['formId']) ? $params['formId'] : null;
} catch (\Zend_Json_Exception $exception) {
$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(json_encode(['imgSrc' => $captchaModel->getImgSrc()]));
$this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
}
}
94 changes: 94 additions & 0 deletions Cron/DeleteExpiredImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Captcha\Cron;

use Magento\Framework\App\Filesystem\DirectoryList;

/**
* Captcha cron actions
*/
class DeleteExpiredImages
{
/**
* @var \Magento\Captcha\Helper\Data
*/
protected $_helper;

/**
* CAPTCHA helper
*
* @var \Magento\Captcha\Helper\Adminhtml\Data
*/
protected $_adminHelper;

/**
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
*/
protected $_mediaDirectory;

/**
* @var \Magento\Store\Model\StoreManager
*/
protected $_storeManager;

/**
* @param \Magento\Captcha\Helper\Data $helper
* @param \Magento\Captcha\Helper\Adminhtml\Data $adminHelper
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Store\Model\StoreManager $storeManager
*/
public function __construct(
\Magento\Captcha\Helper\Data $helper,
\Magento\Captcha\Helper\Adminhtml\Data $adminHelper,
\Magento\Framework\Filesystem $filesystem,
\Magento\Store\Model\StoreManager $storeManager
) {
$this->_helper = $helper;
$this->_adminHelper = $adminHelper;
$this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->_storeManager = $storeManager;
}

/**
* Delete Expired Captcha Images
*
* @return \Magento\Captcha\Cron\DeleteExpiredImages
*/
public function execute()
{
foreach ($this->_storeManager->getWebsites() as $website) {
$this->_deleteExpiredImagesForWebsite($this->_helper, $website, $website->getDefaultStore());
}
$this->_deleteExpiredImagesForWebsite($this->_adminHelper);

return $this;
}

/**
* Delete Expired Captcha Images for specific website
*
* @param \Magento\Captcha\Helper\Data $helper
* @param \Magento\Store\Model\Website|null $website
* @param \Magento\Store\Model\Store|null $store
* @return void
*/
protected function _deleteExpiredImagesForWebsite(
\Magento\Captcha\Helper\Data $helper,
\Magento\Store\Model\Website $website = null,
\Magento\Store\Model\Store $store = null
) {
$expire = time() - $helper->getConfig('timeout', $store) * 60;
$imageDirectory = $this->_mediaDirectory->getRelativePath($helper->getImgDir($website));
foreach ($this->_mediaDirectory->read($imageDirectory) as $filePath) {
if ($this->_mediaDirectory->isFile($filePath)
&& pathinfo($filePath, PATHINFO_EXTENSION) == 'png'
&& $this->_mediaDirectory->stat($filePath)['mtime'] < $expire
) {
$this->_mediaDirectory->delete($filePath);
}
}
}
}

0 comments on commit 66fe90b

Please sign in to comment.