Validate.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Mageplaza
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Mageplaza.com license that is
  8. * available through the world-wide-web at this URL:
  9. * https://www.mageplaza.com/LICENSE.txt
  10. *
  11. * DISCLAIMER
  12. *
  13. * Do not edit or add to this file if you wish to upgrade this extension to newer
  14. * version in the future.
  15. *
  16. * @category Mageplaza
  17. * @package Mageplaza_Core
  18. * @copyright Copyright (c) 2016-2018 Mageplaza (http://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\Core\Helper;
  22. use Magento\Framework\App\Helper\Context;
  23. use Magento\Framework\Module\ModuleListInterface;
  24. use Magento\Framework\ObjectManagerInterface;
  25. use Magento\Store\Model\StoreManagerInterface;
  26. /**
  27. * Class AbstractData
  28. * @package Mageplaza\Core\Helper
  29. */
  30. class Validate extends AbstractData
  31. {
  32. const DEV_ENV = ['localhost', 'dev', '127.0.0.1', '192.168.', 'demo.'];
  33. /**
  34. * @var array
  35. */
  36. protected $configModulePath = [];
  37. /**
  38. * @var array
  39. */
  40. protected $_mageplazaModules;
  41. /**
  42. * @var \Magento\Framework\Module\ModuleListInterface
  43. */
  44. protected $_moduleList;
  45. /**
  46. * Validate constructor.
  47. * @param \Magento\Framework\App\Helper\Context $context
  48. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  49. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  50. * @param \Magento\Framework\Module\ModuleListInterface $moduleList
  51. */
  52. public function __construct(
  53. Context $context,
  54. ObjectManagerInterface $objectManager,
  55. StoreManagerInterface $storeManager,
  56. ModuleListInterface $moduleList
  57. )
  58. {
  59. $this->_moduleList = $moduleList;
  60. parent::__construct($context, $objectManager, $storeManager);
  61. }
  62. /**
  63. * @param $moduleName
  64. * @return bool
  65. */
  66. public function needActive($moduleName)
  67. {
  68. $type = $this->getModuleType($moduleName);
  69. if (!$type || !in_array($type, ['1', '2'])) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * @param $moduleName
  76. * @return mixed
  77. */
  78. public function getModuleType($moduleName)
  79. {
  80. $configModulePath = $this->getConfigModulePath($moduleName);
  81. return $this->getConfigValue($configModulePath . '/module/type');
  82. }
  83. /**
  84. * @param $moduleName
  85. * @return bool
  86. */
  87. public function getConfigModulePath($moduleName)
  88. {
  89. if (!isset($this->configModulePath[$moduleName])) {
  90. $this->configModulePath[$moduleName] = false;
  91. $helperClassName = str_replace('_', '\\', $moduleName) . '\Helper\Data';
  92. if (class_exists($helperClassName)) {
  93. $helper = $this->objectManager->get($helperClassName);
  94. if ($helper instanceof AbstractData) {
  95. $this->configModulePath[$moduleName] = $helper::CONFIG_MODULE_PATH;
  96. }
  97. }
  98. }
  99. return $this->configModulePath[$moduleName];
  100. }
  101. /**
  102. * @param $moduleName
  103. * @return bool
  104. */
  105. public function isModuleActive($moduleName)
  106. {
  107. $configModulePath = $this->getConfigModulePath($moduleName);
  108. return $this->getConfigValue($configModulePath . '/module/active')
  109. && $this->getConfigValue($configModulePath . '/module/product_key');
  110. }
  111. /**
  112. * @param $moduleName
  113. * @return array
  114. */
  115. public function getModuleCheckbox($moduleName)
  116. {
  117. $configModulePath = $this->getConfigModulePath($moduleName);
  118. $create = $this->getConfigValue($configModulePath . '/module/create');
  119. if (is_null($create)) {
  120. $create = 1;
  121. }
  122. $subscribe = $this->getConfigValue($configModulePath . '/module/subscribe');
  123. if (is_null($subscribe)) {
  124. $subscribe = 1;
  125. }
  126. return [
  127. 'create' => (int)$create,
  128. 'subscribe' => (int)$subscribe
  129. ];
  130. }
  131. /**
  132. * @return array
  133. */
  134. public function getModuleList()
  135. {
  136. if (is_null($this->_mageplazaModules)) {
  137. $this->_mageplazaModules = [];
  138. $allowList = true;
  139. $hostName = $this->_urlBuilder->getBaseUrl();
  140. foreach (self::DEV_ENV as $env) {
  141. if (strpos($hostName, $env) !== false) {
  142. $allowList = false;
  143. break;
  144. }
  145. }
  146. if ($allowList) {
  147. $moduleList = $this->_moduleList->getNames();
  148. foreach ($moduleList as $name) {
  149. if (strpos($name, 'Mageplaza_') === false) {
  150. continue;
  151. }
  152. $this->_mageplazaModules[] = $name;
  153. }
  154. }
  155. }
  156. return $this->_mageplazaModules;
  157. }
  158. }