_storeManager = $storeManager; $this->_filesystem = $filesystem; $this->_factory = $factory; parent::__construct($context); } /** * Get Captcha * * @param string $formId * @return \Magento\Captcha\Model\CaptchaInterface */ public function getCaptcha($formId) { if (!array_key_exists($formId, $this->_captcha)) { $captchaType = ucfirst($this->getConfig('type')); if (!$captchaType) { $captchaType = self::DEFAULT_CAPTCHA_TYPE; } elseif ($captchaType == 'Default') { $captchaType = $captchaType . 'Model'; } $this->_captcha[$formId] = $this->_factory->create($captchaType, $formId); } return $this->_captcha[$formId]; } /** * Returns config value * * @param string $key The last part of XML_PATH_$area_CAPTCHA_ constant (case insensitive) * @param \Magento\Store\Model\Store $store * @return \Magento\Framework\App\Config\Element */ public function getConfig($key, $store = null) { return $this->scopeConfig->getValue( 'customer/captcha/' . $key, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store ); } /** * Get list of available fonts. * * Return format: * [['arial'] => ['label' => 'Arial', 'path' => '/www/magento/fonts/arial.ttf']] * * @return array */ public function getFonts() { $fontsConfig = $this->scopeConfig->getValue(\Magento\Captcha\Helper\Data::XML_PATH_CAPTCHA_FONTS, 'default'); $fonts = []; if ($fontsConfig) { $libDir = $this->_filesystem->getDirectoryRead(DirectoryList::LIB_INTERNAL); foreach ($fontsConfig as $fontName => $fontConfig) { $fonts[$fontName] = [ 'label' => $fontConfig['label'], 'path' => $libDir->getAbsolutePath($fontConfig['path']), ]; } } return $fonts; } /** * Get captcha image directory * * @param mixed $website * @return string */ public function getImgDir($website = null) { $mediaDir = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA); $captchaDir = '/captcha/' . $this->_getWebsiteCode($website); $mediaDir->create($captchaDir); return $mediaDir->getAbsolutePath($captchaDir) . '/'; } /** * Get website code * * @param mixed $website * @return string */ protected function _getWebsiteCode($website = null) { return $this->_storeManager->getWebsite($website)->getCode(); } /** * Get captcha image base URL * * @param mixed $website * @return string */ public function getImgUrl($website = null) { return $this->_storeManager->getStore()->getBaseUrl( DirectoryList::MEDIA ) . 'captcha' . '/' . $this->_getWebsiteCode( $website ) . '/'; } }