Config.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_ReCaptcha
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. namespace MSP\ReCaptcha\Model;
  21. use Magento\Framework\App\Config\ScopeConfigInterface;
  22. use Magento\Framework\Phrase;
  23. use Magento\Store\Model\ScopeInterface;
  24. use MSP\ReCaptcha\Model\Config\Source\Type;
  25. class Config
  26. {
  27. const XML_PATH_ENABLED_BACKEND = 'msp_securitysuite_recaptcha/backend/enabled';
  28. const XML_PATH_ENABLED_FRONTEND = 'msp_securitysuite_recaptcha/frontend/enabled';
  29. const XML_PATH_TYPE_FRONTEND = 'msp_securitysuite_recaptcha/frontend/type';
  30. const XML_PATH_LANGUAGE_CODE = 'msp_securitysuite_recaptcha/frontend/lang';
  31. const XML_PATH_POSITION_FRONTEND = 'msp_securitysuite_recaptcha/frontend/position';
  32. const XML_PATH_SIZE_BACKEND = 'msp_securitysuite_recaptcha/backend/size';
  33. const XML_PATH_SIZE_FRONTEND = 'msp_securitysuite_recaptcha/frontend/size';
  34. const XML_PATH_THEME_BACKEND = 'msp_securitysuite_recaptcha/backend/theme';
  35. const XML_PATH_THEME_FRONTEND = 'msp_securitysuite_recaptcha/frontend/theme';
  36. const XML_PATH_PUBLIC_KEY = 'msp_securitysuite_recaptcha/general/public_key';
  37. const XML_PATH_PRIVATE_KEY = 'msp_securitysuite_recaptcha/general/private_key';
  38. const XML_PATH_ENABLED_FRONTEND_LOGIN = 'msp_securitysuite_recaptcha/frontend/enabled_login';
  39. const XML_PATH_ENABLED_FRONTEND_FORGOT = 'msp_securitysuite_recaptcha/frontend/enabled_forgot';
  40. const XML_PATH_ENABLED_FRONTEND_CONTACT = 'msp_securitysuite_recaptcha/frontend/enabled_contact';
  41. const XML_PATH_ENABLED_FRONTEND_CREATE = 'msp_securitysuite_recaptcha/frontend/enabled_create';
  42. /**
  43. * @var ScopeConfigInterface
  44. */
  45. private $scopeConfig;
  46. /**
  47. * Config constructor.
  48. * @param ScopeConfigInterface $scopeConfig
  49. */
  50. public function __construct(ScopeConfigInterface $scopeConfig)
  51. {
  52. $this->scopeConfig = $scopeConfig;
  53. }
  54. /**
  55. * Get error
  56. * @return Phrase
  57. */
  58. public function getErrorDescription()
  59. {
  60. return __('Incorrect reCAPTCHA');
  61. }
  62. /**
  63. * Get google recaptcha public key
  64. * @return string
  65. */
  66. public function getPublicKey()
  67. {
  68. return trim($this->scopeConfig->getValue(static::XML_PATH_PUBLIC_KEY));
  69. }
  70. /**
  71. * Get google recaptcha private key
  72. * @return string
  73. */
  74. public function getPrivateKey()
  75. {
  76. return trim($this->scopeConfig->getValue(static::XML_PATH_PRIVATE_KEY));
  77. }
  78. /**
  79. * Return true if enabled on backend
  80. * @return bool
  81. */
  82. public function isEnabledBackend()
  83. {
  84. if (!$this->getPrivateKey() || !$this->getPublicKey()) {
  85. return false;
  86. }
  87. return (bool) $this->scopeConfig->getValue(static::XML_PATH_ENABLED_BACKEND);
  88. }
  89. /**
  90. * Return true if enabled on frontend
  91. * @return bool
  92. */
  93. public function isEnabledFrontend()
  94. {
  95. if (!$this->getPrivateKey() || !$this->getPublicKey()) {
  96. return false;
  97. }
  98. return (bool) $this->scopeConfig->getValue(static::XML_PATH_ENABLED_FRONTEND);
  99. }
  100. /**
  101. * Return true if enabled on frontend login
  102. * @return bool
  103. */
  104. public function isEnabledFrontendLogin()
  105. {
  106. if (!$this->isEnabledFrontend()) {
  107. return false;
  108. }
  109. return (bool) $this->scopeConfig->getValue(static::XML_PATH_ENABLED_FRONTEND_LOGIN);
  110. }
  111. /**
  112. * Return true if enabled on frontend forgot password
  113. * @return bool
  114. */
  115. public function isEnabledFrontendForgot()
  116. {
  117. if (!$this->isEnabledFrontend()) {
  118. return false;
  119. }
  120. return (bool) $this->scopeConfig->getValue(static::XML_PATH_ENABLED_FRONTEND_FORGOT);
  121. }
  122. /**
  123. * Return true if enabled on frontend contact
  124. * @return bool
  125. */
  126. public function isEnabledFrontendContact()
  127. {
  128. if (!$this->isEnabledFrontend()) {
  129. return false;
  130. }
  131. return (bool) $this->scopeConfig->getValue(static::XML_PATH_ENABLED_FRONTEND_CONTACT);
  132. }
  133. /**
  134. * Return true if enabled on frontend create user
  135. * @return bool
  136. */
  137. public function isEnabledFrontendCreate()
  138. {
  139. if (!$this->isEnabledFrontend()) {
  140. return false;
  141. }
  142. return (bool) $this->scopeConfig->getValue(static::XML_PATH_ENABLED_FRONTEND_CREATE);
  143. }
  144. /**
  145. * Get data size
  146. * @return string
  147. */
  148. public function getFrontendSize()
  149. {
  150. if ($this->getFrontendType() == Type::TYPE_INVISIBLE) {
  151. return 'invisible';
  152. }
  153. return $this->scopeConfig->getValue(static::XML_PATH_SIZE_FRONTEND);
  154. }
  155. /**
  156. * Get data size
  157. * @return string
  158. */
  159. public function getBackendSize()
  160. {
  161. return $this->scopeConfig->getValue(static::XML_PATH_SIZE_BACKEND);
  162. }
  163. /**
  164. * Get data size
  165. * @return string
  166. */
  167. public function getFrontendTheme()
  168. {
  169. if ($this->getFrontendType() == Type::TYPE_INVISIBLE) {
  170. return null;
  171. }
  172. return $this->scopeConfig->getValue(static::XML_PATH_THEME_FRONTEND);
  173. }
  174. /**
  175. * Get data size
  176. * @return string
  177. */
  178. public function getBackendTheme()
  179. {
  180. return $this->scopeConfig->getValue(static::XML_PATH_THEME_BACKEND);
  181. }
  182. /**
  183. * Get data size
  184. * @return string
  185. */
  186. public function getFrontendPosition()
  187. {
  188. if ($this->getFrontendType() != Type::TYPE_INVISIBLE) {
  189. return null;
  190. }
  191. return $this->scopeConfig->getValue(static::XML_PATH_POSITION_FRONTEND);
  192. }
  193. /**
  194. * Get data size
  195. * @return string
  196. */
  197. public function getFrontendType()
  198. {
  199. return $this->scopeConfig->getValue(static::XML_PATH_TYPE_FRONTEND);
  200. }
  201. /**
  202. * Get language code
  203. * @return string
  204. */
  205. public function getLanguageCode()
  206. {
  207. return $this->scopeConfig->getValue(static::XML_PATH_LANGUAGE_CODE, ScopeInterface::SCOPE_STORE);
  208. }
  209. }