![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Helper/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Helper; use DOMDocument; use Error; use Exception; use Extmag\Shiplab\Model\Cache\Type; use Extmag\Shiplab\Model\Configuration; use Extmag\Shiplab\Model\ConfigurationFactory; use Extmag\Shiplab\Model\ConfigurationScopeFactory; use Extmag\Shiplab\Model\ResourceModel\ConfigurationScope\CollectionFactory; use Extmag\Shiplab\Model\ResourceModel\Label\Collection; use Magento\Backend\Model\UrlInterface; use Magento\Directory\Model\CurrencyFactory; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\AuthorizationInterface; use Magento\Framework\Serialize\SerializerInterface; use Magento\Store\Model\ScopeInterface; use Psr\Log\LoggerInterface; class Manager extends AbstractHelper { /** * @var Configuration */ protected $config; /** * @var Type */ protected $cache; /** * @var string */ protected $cacheConfigId = 'shiplab_config'; /** * @var ScopeConfigInterface */ protected $systemConfig; /** * @var AuthorizationInterface */ protected $auth; /** * @var CurrencyFactory */ protected $currencyFactory; /** * @var LoggerInterface */ protected $debugLogger; /** * @var LoggerInterface */ protected $infoLogger; /** * @var LoggerInterface */ protected $warningLogger; /** * @var SerializerInterface */ protected $serializer; protected $configStoreId = null; protected $configDirection = null; protected $configCountry = null; protected $backendUrl; protected $debugReplacePrivateDataKeys = [ 'UserId', 'Username', 'UserName', 'Password', 'AccessLicenseNumber', 'AccessNumber', 'AccessKey', 'SiteID', 'Key', 'MeterNumber', 'PDF417', 'InternationalSignatureGraphicImage', 'GraphicImage', 'DocImageVal', 'HTMLImage', 'account_user_id', 'account_password', 'account_license', ]; /** * @var CollectionFactory */ protected $collectConfigScopeFactory; /** * @param Context $context * @param ConfigurationFactory $config * @param CollectionFactory $collectConfigScopeFactory * @param Type $cache * @param ScopeConfigInterface $systemConfig * @param AuthorizationInterface $auth * @param UrlInterface $backendUrl * @param CurrencyFactory $currencyFactory * @param LoggerInterface $debugLogger * @param LoggerInterface $infoLogger * @param LoggerInterface $warningLogger * @param SerializerInterface $serializer */ public function __construct( Context $context, ConfigurationFactory $config, CollectionFactory $collectConfigScopeFactory, Type $cache, ScopeConfigInterface $systemConfig, AuthorizationInterface $auth, UrlInterface $backendUrl, CurrencyFactory $currencyFactory, LoggerInterface $debugLogger, LoggerInterface $infoLogger, LoggerInterface $warningLogger, SerializerInterface $serializer ) { parent::__construct($context); $this->config = $config; $this->cache = $cache; $this->systemConfig = $systemConfig; $this->auth = $auth; $this->backendUrl = $backendUrl; $this->currencyFactory = $currencyFactory; $this->debugLogger = $debugLogger; $this->infoLogger = $infoLogger; $this->warningLogger = $warningLogger; $this->serializer = $serializer; $this->collectConfigScopeFactory = $collectConfigScopeFactory; } /** * @param $countryCode * @return bool */ public function isCountryInEU($countryCode) { $countriesEU = $this->getStoreConfig('general/country/eu_countries'); if (!empty($countriesEU)) { if (!is_array($countriesEU)) { $countriesEU = explode(",", $countriesEU); } if (in_array($countryCode, $countriesEU)) { return true; } } return false; } /** * @param $path * @param null $scopeCode * @param null $scopeType * @return mixed */ public function getStoreConfig($path, $scopeCode = null, $scopeType = null) { return $this->systemConfig->getValue( $path, $scopeType ?? ScopeInterface::SCOPE_STORE, $scopeCode ?? $this->getConfigScopeFilter('store') ); } /** * @param null $type * @return array|mixed|null */ public function getConfigScopeFilter($type = null) { $scopes = [ 'store' => $this->configStoreId, 'direction' => $this->configDirection, 'country' => $this->configCountry, ]; if ($type !== null) { return $scopes[$type] ?? null; } return $scopes; } /** * @param $data */ public function debug($data) { if ($this->getStoreConfig('extmag_developer/general/debug') == 1) { $data = $this->filterPrivateKeys($data); $this->debugLogger->info(var_export($data, true)); } } /** * @param $message * @param array $context */ public function info($message, array $context = []) { if ($this->getStoreConfig('extmag_developer/general/debug') == 1) { $this->infoLogger->info($message, $context); } } /** * @param $message * @param array $context */ public function warning($message, array $context = []) { if ($this->getStoreConfig('extmag_developer/general/debug') == 1) { $this->warningLogger->warning($message, $context); } } /** * @param $data * @return array|mixed */ public function filterPrivateKeys($data) { if (!is_array($data)) { if ($this->isJson($data)) { $data = json_decode($data, true); } elseif ($this->isXml($data)) { $data = json_decode( json_encode(simplexml_load_string(mb_convert_encoding($data, "UTF-8"), 'SimpleXMLElement', LIBXML_NOCDATA)), true ); } elseif (is_string($data)) { $data = $this->modifyPrivateDataInString($data); } } if (is_array($data)) { $data = $this->multiKeyExistsAndModify($data); } return $data; } /** * @param string $str * @return string|string[]|null */ public function modifyPrivateDataInString(string $str) { $patterns = []; foreach ($this->debugReplacePrivateDataKeys as $key) { $patterns[] = "/(<.*?:?" . $key . ">).*?(<\/)/is"; } return preg_replace($patterns, '$1***$2', $str); } /** * @param array $arr * @return array */ public function multiKeyExistsAndModify(array $arr) { foreach ($this->debugReplacePrivateDataKeys as $key) { if (isset($arr[$key])) { $arr[$key] = '***'; } } foreach ($arr as $k => $element) { if (!is_array($element) && $this->isJson($element)) { $element = json_decode($element, true); } if (is_array($element)) { $arr[$k] = $this->multiKeyExistsAndModify($element); } else { $arr[$k] = substr($element??'', 0, 300); } } return $arr; } /** * @param $string * @return bool */ public function isJson($string) { if(!is_string($string)) { return false; } json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } /** * @param $string * @return bool */ public function isXml($string) { try { $xml_handle = new DOMDocument(); $xml_handle->loadXML($string); } catch (Exception | Error $e) { return false; } return true; } /** * @param $configStoreId * @param $configDirection * @param $configCountry * @return bool */ public function setConfigScopeFilter($configStoreId, $configDirection, $configCountry) { $this->configStoreId = $configStoreId; $this->configDirection = $configDirection; $this->configCountry = $configCountry; return true; } /** * @param $resourceId * @return bool */ public function checkPermission($resourceId) { return $this->auth->isAllowed($resourceId); } /** * @param $path * @param $params * @return string */ public function getBackendUrl($path = null, $params = null) { return $this->backendUrl->getUrl($path, $params); } /** * @param $path * @param $storeId * @param $directionId * @param $countryId * @return mixed|null * @throws Exception */ public function getConfig( $path, $storeId = null, $directionId = null, $countryId = null ) { if ($storeId === null) { $storeId = $this->configStoreId; } if ($directionId === null) { $directionId = $this->configDirection; } if ($countryId === null) { $countryId = $this->configCountry; } $cacheId = $storeId . '_' . $directionId . '_' . $countryId . '_::' . $this->cacheConfigId; $scope = null; $cache = $this->cache->load($cacheId); if (!$cache) { if ($storeId !== null) { /** * @var \Extmag\Shiplab\Model\ResourceModel\ConfigurationScope\Collection $scopeCollection */ $scopeCollection = $this->collectConfigScopeFactory->create(); $scopeCollection->addScopeFilter( $storeId, $directionId, $countryId ); if (count($scopeCollection) > 0) { $item = $scopeCollection->getFirstItem(); $scope['store'] = $item->getStore(); if ($directionId !== null) { $scope['direction'] = $item->getDirection(); } if ($countryId !== null) { $scope['country'] = $item->getCountry(); } } } $this->cache->save($this->serializer->serialize($scope), $cacheId); } else { $scope = $this->serializer->unserialize($cache); } $configValues = $this->collectConfiguration(); if ($scope) { if (isset($scope['country']) && isset($configValues['country/' . $scope['country'] . '/' . $path])) { return $configValues['country/' . $scope['country'] . '/' . $path]; } elseif (isset($scope['direction']) && isset($configValues['direction/' . $scope['direction'] . '/' . $path]) ) { return $configValues['direction/' . $scope['direction'] . '/' . $path]; } elseif (isset($scope['store']) && isset($configValues['store/' . $scope['store'] . '/' . $path])) { return $configValues['store/' . $scope['store'] . '/' . $path]; } } if (isset($configValues['default/0/' . $path])) { return $configValues['default/0/' . $path]; } return null; } /** * @return array|bool|float|int|string|null */ protected function collectConfiguration() { $items = []; /** * @var Configuration $cache */ $cache = $this->cache->load('full_config::' . $this->cacheConfigId); if (!$cache) { $config = $this->config->create()->getCollection(); if (count($config) > 0) { foreach ($config as $data) { $items[$data->getScope() . '/' . $data->getScopeId() . '/' . $data->getPath()] = $data->getValue(); } $this->cache->save($this->serializer->serialize($items), 'full_config::' . $this->cacheConfigId); } } else { $items = $this->serializer->unserialize($cache); } return $items; } /** * @return void */ public function clearConfigCache() { $this->cache->clean(); } /** * @param $str * @return string */ public function toCamelCaseGetter($str) { return 'get' . str_replace("_", "", ucwords($str, "_")); } public function convertBaseToCurrency($amount, $baseCode, $currencyTo) { $baseCode = $this->mappingCurrencyCode($baseCode); $currencyTo = $this->mappingCurrencyCode($currencyTo); $allowedCurrencies = $this->currencyFactory->create()->getConfigAllowCurrencies(); if ($currencyTo != $baseCode && in_array($baseCode, $allowedCurrencies) && in_array($currencyTo, $allowedCurrencies) ) { $amount = $amount * $this->currencyFactory->create()->load($baseCode)->getAnyRate($currencyTo); } return $amount; } /** * @param $code * @return string */ public function mappingCurrencyCode($code) { if (empty($code)) { return $code; } $currencyMapping = [ 'RMB' => 'CNY', 'CNH' => 'CNY', ]; return (!empty($currencyMapping[$code]) ? $currencyMapping[$code] : $code); } /** * @param $ext * @return bool */ public function isThermalLabel($ext) { $thermalExts = ['zpl', 'epl', 'spl', 'starpl']; if ($ext instanceof Collection) { foreach ($ext as $item) { if (in_array(strtolower($item->getExtension()), $thermalExts)) { return true; } } return false; } else { if (is_object($ext)) { $ext = $ext->getExtension(); } return in_array(strtolower($ext), $thermalExts); } } /** * @param $ext * @return bool */ public function isNotThermalLabel($ext) { $thermalExts = ['zpl', 'epl', 'spl', 'starpl']; if ($ext instanceof Collection) { foreach ($ext as $item) { if (!in_array(strtolower($item->getExtension()), $thermalExts)) { return true; } } return false; } else { if (is_object($ext)) { $ext = $ext->getExtension(); } return !in_array(strtolower($ext), $thermalExts); } } /** * @return false|mixed */ public function isAllowedLocalPrint() { $printType = $this->getStoreConfig('extmag_printers/local/type'); $port = $this->getStoreConfig('extmag_printers/local/port'); return ((!empty($port) && $printType == 'local') ? $port : false); } /** * @param $ext * @return string */ public function normalizeExtension($ext) { $ext = strtolower($ext); $extensions = [ "gif" => "gif", "png" => "png", "pdf" => "pdf", "zpl" => "zpl", "zpl2" => "zpl", "zplii" => "zpl", "epl" => "epl", "epl2" => "epl", "spl" => "spl", "starpl" => "starpl", "link" => "link", "virtual" => "virtual", ]; return $extensions[$ext] ?? $ext; } }