Data.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © 2015 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Smartwave\Porto\Helper;
  7. use Magento\Framework\Registry;
  8. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  9. {
  10. protected $_objectManager;
  11. private $_registry;
  12. protected $_filterProvider;
  13. private $_checkedPurchaseCode;
  14. private $_messageManager;
  15. protected $_configFactory;
  16. public function __construct(
  17. \Magento\Framework\App\Helper\Context $context,
  18. \Magento\Framework\ObjectManagerInterface $objectManager,
  19. \Magento\Store\Model\StoreManagerInterface $storeManager,
  20. \Magento\Cms\Model\Template\FilterProvider $filterProvider,
  21. \Magento\Framework\Message\ManagerInterface $messageManager,
  22. \Magento\Framework\App\Config\ConfigResource\ConfigInterface $configFactory,
  23. Registry $registry
  24. ) {
  25. $this->_storeManager = $storeManager;
  26. $this->_objectManager = $objectManager;
  27. $this->_filterProvider = $filterProvider;
  28. $this->_registry = $registry;
  29. $this->_messageManager = $messageManager;
  30. $this->_configFactory = $configFactory;
  31. parent::__construct($context);
  32. }
  33. public function checkPurchaseCode($save = false) {
  34. if($this->isLocalhost()){
  35. return "localhost";
  36. }
  37. if(!$this->_checkedPurchaseCode){
  38. $code = $this->scopeConfig->getValue('porto_license/general/purchase_code');
  39. $code_confirm = $this->scopeConfig->getValue('porto_license/general/purchase_code_confirm');
  40. if($save) {
  41. $site_url = $this->scopeConfig->getValue('web/unsecure/base_url');
  42. $domain = trim(preg_replace('/^.*?\\/\\/(.*)?\\//', '$1', $site_url));
  43. if(strpos($domain, "/"))
  44. $domain = substr($domain, 0, strpos($domain, "/"));
  45. if(!$code || base64_encode($code) != $code_confirm) {
  46. $this->curlPurchaseCode(base64_decode($code_confirm), "", "remove");
  47. }
  48. if($code) {
  49. $result = $this->curlPurchaseCode($code, $domain, "add");
  50. if(!$result || $result['result'] == 0) {
  51. $this->_checkedPurchaseCode = "";
  52. $code_confirm = "";
  53. $this->_messageManager->getMessages(true);
  54. $this->_messageManager->addWarning(__('Purchase code is not valid!'));
  55. } else if($result['result'] == 1) {
  56. $code_confirm = base64_encode($code);
  57. $this->_checkedPurchaseCode = "verified";
  58. } else {
  59. $this->_checkedPurchaseCode = "";
  60. $code_confirm = "";
  61. $this->_messageManager->getMessages(true);
  62. $this->_messageManager->addWarning(__($result['message']));
  63. }
  64. } else {
  65. $code_confirm = "";
  66. $this->_checkedPurchaseCode = "";
  67. }
  68. $this->_configFactory->saveConfig('porto_license/general/purchase_code_confirm',$code_confirm,"default",0);
  69. } else {
  70. if($code && $code_confirm && base64_encode($code) == $code_confirm)
  71. $this->_checkedPurchaseCode = "verified";
  72. }
  73. }
  74. return $this->_checkedPurchaseCode;
  75. }
  76. public function curlPurchaseCode($code, $domain, $act) {
  77. $ch = curl_init();
  78. // Set cURL options
  79. curl_setopt($ch, CURLOPT_URL, "http://www.portotheme.com/envato/verify_purchase_new.php?item=9725864&version=m2&code=$code&domain=$domain&act=$act");
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  81. curl_setopt($ch, CURLOPT_USERAGENT, 'PORTO-PURCHASE-VERIFY');
  82. // Decode returned JSON
  83. $result = json_decode( curl_exec($ch) , true );
  84. return $result;
  85. }
  86. public function isLocalhost() {
  87. $whitelist = array(
  88. '127.0.0.1',
  89. '::1'
  90. );
  91. return in_array($_SERVER['REMOTE_ADDR'], $whitelist);
  92. }
  93. public function isAdmin() {
  94. $om = \Magento\Framework\App\ObjectManager::getInstance();
  95. $app_state = $om->get('\Magento\Framework\App\State');
  96. $area_code = $app_state->getAreaCode();
  97. if($area_code == \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE)
  98. {
  99. return true;
  100. }
  101. else
  102. {
  103. return false;
  104. }
  105. }
  106. public function getBaseUrl()
  107. {
  108. return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
  109. }
  110. public function getBaseLinkUrl()
  111. {
  112. return $this->_storeManager->getStore()->getBaseUrl();
  113. }
  114. public function getConfig($config_path)
  115. {
  116. return $this->scopeConfig->getValue(
  117. $config_path,
  118. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  119. );
  120. }
  121. public function getModel($model) {
  122. return $this->_objectManager->create($model);
  123. }
  124. public function getCurrentStore() {
  125. return $this->_storeManager->getStore();
  126. }
  127. public function filterContent($content) {
  128. return $this->_filterProvider->getPageFilter()->filter($content);
  129. }
  130. public function getCategoryProductIds($current_category) {
  131. $category_products = $current_category->getProductCollection()
  132. ->addAttributeToSelect('*')
  133. ->addAttributeToSort('position','asc');
  134. $cat_prod_ids = $category_products->getAllIds();
  135. return $cat_prod_ids;
  136. }
  137. public function getPrevProduct($product) {
  138. $current_category = $product->getCategory();
  139. if(!$current_category) {
  140. foreach($product->getCategoryCollection() as $parent_cat) {
  141. $current_category = $parent_cat;
  142. }
  143. }
  144. if(!$current_category)
  145. return false;
  146. $cat_prod_ids = $this->getCategoryProductIds($current_category);
  147. $_pos = array_search($product->getId(), $cat_prod_ids);
  148. if (isset($cat_prod_ids[$_pos - 1])) {
  149. $prev_product = $this->getModel('Magento\Catalog\Model\Product')->load($cat_prod_ids[$_pos - 1]);
  150. return $prev_product;
  151. }
  152. return false;
  153. }
  154. public function getNextProduct($product) {
  155. $current_category = $product->getCategory();
  156. if(!$current_category) {
  157. foreach($product->getCategoryCollection() as $parent_cat) {
  158. $current_category = $parent_cat;
  159. }
  160. }
  161. if(!$current_category)
  162. return false;
  163. $cat_prod_ids = $this->getCategoryProductIds($current_category);
  164. $_pos = array_search($product->getId(), $cat_prod_ids);
  165. if (isset($cat_prod_ids[$_pos + 1])) {
  166. $next_product = $this->getModel('Magento\Catalog\Model\Product')->load($cat_prod_ids[$_pos + 1]);
  167. return $next_product;
  168. }
  169. return false;
  170. }
  171. public function getMasonryItemClass($arr) {
  172. $item_class = "";
  173. foreach ($arr as $key => $value) {
  174. $item_class .= ' ' . $key . '-' . $value;
  175. }
  176. return $item_class;
  177. }
  178. }