Data.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Helper;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Framework\Filesystem;
  9. use Magento\Framework\Filesystem\DriverInterface;
  10. /**
  11. * Captcha image model
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  17. {
  18. /**
  19. * Used for "name" attribute of captcha's input field
  20. */
  21. const INPUT_NAME_FIELD_VALUE = 'captcha';
  22. /**
  23. * Always show captcha
  24. */
  25. const MODE_ALWAYS = 'always';
  26. /**
  27. * Show captcha only after certain number of unsuccessful attempts
  28. */
  29. const MODE_AFTER_FAIL = 'after_fail';
  30. /**
  31. * Captcha fonts path
  32. */
  33. const XML_PATH_CAPTCHA_FONTS = 'captcha/fonts';
  34. /**
  35. * Default captcha type
  36. */
  37. const DEFAULT_CAPTCHA_TYPE = 'Zend';
  38. /**
  39. * List uses Models of Captcha
  40. * @var array
  41. */
  42. protected $_captcha = [];
  43. /**
  44. * @var Filesystem
  45. */
  46. protected $_filesystem;
  47. /**
  48. * @var \Magento\Store\Model\StoreManagerInterface
  49. */
  50. protected $_storeManager;
  51. /**
  52. * @var \Magento\Captcha\Model\CaptchaFactory
  53. */
  54. protected $_factory;
  55. /**
  56. * @param \Magento\Framework\App\Helper\Context $context
  57. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  58. * @param Filesystem $filesystem
  59. * @param \Magento\Captcha\Model\CaptchaFactory $factory
  60. */
  61. public function __construct(
  62. \Magento\Framework\App\Helper\Context $context,
  63. \Magento\Store\Model\StoreManagerInterface $storeManager,
  64. Filesystem $filesystem,
  65. \Magento\Captcha\Model\CaptchaFactory $factory
  66. ) {
  67. $this->_storeManager = $storeManager;
  68. $this->_filesystem = $filesystem;
  69. $this->_factory = $factory;
  70. parent::__construct($context);
  71. }
  72. /**
  73. * Get Captcha
  74. *
  75. * @param string $formId
  76. * @return \Magento\Captcha\Model\CaptchaInterface
  77. */
  78. public function getCaptcha($formId)
  79. {
  80. if (!array_key_exists($formId, $this->_captcha)) {
  81. $captchaType = ucfirst($this->getConfig('type'));
  82. if (!$captchaType) {
  83. $captchaType = self::DEFAULT_CAPTCHA_TYPE;
  84. } elseif ($captchaType == 'Default') {
  85. $captchaType = $captchaType . 'Model';
  86. }
  87. $this->_captcha[$formId] = $this->_factory->create($captchaType, $formId);
  88. }
  89. return $this->_captcha[$formId];
  90. }
  91. /**
  92. * Returns config value
  93. *
  94. * @param string $key The last part of XML_PATH_$area_CAPTCHA_ constant (case insensitive)
  95. * @param \Magento\Store\Model\Store $store
  96. * @return \Magento\Framework\App\Config\Element
  97. */
  98. public function getConfig($key, $store = null)
  99. {
  100. return $this->scopeConfig->getValue(
  101. 'customer/captcha/' . $key,
  102. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  103. $store
  104. );
  105. }
  106. /**
  107. * Get list of available fonts.
  108. *
  109. * Return format:
  110. * [['arial'] => ['label' => 'Arial', 'path' => '/www/magento/fonts/arial.ttf']]
  111. *
  112. * @return array
  113. */
  114. public function getFonts()
  115. {
  116. $fontsConfig = $this->scopeConfig->getValue(\Magento\Captcha\Helper\Data::XML_PATH_CAPTCHA_FONTS, 'default');
  117. $fonts = [];
  118. if ($fontsConfig) {
  119. $libDir = $this->_filesystem->getDirectoryRead(DirectoryList::LIB_INTERNAL);
  120. foreach ($fontsConfig as $fontName => $fontConfig) {
  121. $fonts[$fontName] = [
  122. 'label' => $fontConfig['label'],
  123. 'path' => $libDir->getAbsolutePath($fontConfig['path']),
  124. ];
  125. }
  126. }
  127. return $fonts;
  128. }
  129. /**
  130. * Get captcha image directory
  131. *
  132. * @param mixed $website
  133. * @return string
  134. */
  135. public function getImgDir($website = null)
  136. {
  137. $mediaDir = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
  138. $captchaDir = '/captcha/' . $this->_getWebsiteCode($website);
  139. $mediaDir->create($captchaDir);
  140. return $mediaDir->getAbsolutePath($captchaDir) . '/';
  141. }
  142. /**
  143. * Get website code
  144. *
  145. * @param mixed $website
  146. * @return string
  147. */
  148. protected function _getWebsiteCode($website = null)
  149. {
  150. return $this->_storeManager->getWebsite($website)->getCode();
  151. }
  152. /**
  153. * Get captcha image base URL
  154. *
  155. * @param mixed $website
  156. * @return string
  157. */
  158. public function getImgUrl($website = null)
  159. {
  160. return $this->_storeManager->getStore()->getBaseUrl(
  161. DirectoryList::MEDIA
  162. ) . 'captcha' . '/' . $this->_getWebsiteCode(
  163. $website
  164. ) . '/';
  165. }
  166. }