ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2023 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
|
||||
/**
|
||||
* @since 2.9.4
|
||||
*/
|
||||
class AdminConfig
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $configUtils;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
|
||||
*/
|
||||
public function __construct(
|
||||
RequestInterface $request,
|
||||
ConfigUtils $configUtils
|
||||
) {
|
||||
$this->request = $request;
|
||||
$this->configUtils = $configUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config for current scope selected in admin.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function getScopedConfig(string $path): string
|
||||
{
|
||||
if ($scopeCode = $this->request->getParam('website')) {
|
||||
$scopeType = ScopeInterface::SCOPE_WEBSITE;
|
||||
} elseif ($scopeCode = $this->request->getParam('store')) {
|
||||
$scopeType = ScopeInterface::SCOPE_STORE;
|
||||
} else {
|
||||
$scopeCode = null;
|
||||
$scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
|
||||
}
|
||||
return (string) $this->configUtils->getConfig($path, $scopeCode, $scopeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2021 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Plumrocket\Base\Api\ConfigUtilsInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
|
||||
/**
|
||||
* @since 2.5.2
|
||||
* @deprecated since 2.10.0
|
||||
* @see \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
class Config extends ConfigUtils implements ConfigUtilsInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
/**
|
||||
* @since 2.6.0
|
||||
*/
|
||||
class Debug
|
||||
{
|
||||
|
||||
/**
|
||||
* Get backtrace as a table
|
||||
*
|
||||
* @param string $title
|
||||
* @return string
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function backtrace(string $title = 'Debug Backtrace:'): string
|
||||
{
|
||||
$rows = '';
|
||||
foreach (debug_backtrace() as $stack) {
|
||||
if (! isset($stack['file'])) {
|
||||
$stack['file'] = '[PHP Kernel]';
|
||||
}
|
||||
if (! isset($stack['line'])) {
|
||||
$stack['line'] = '';
|
||||
}
|
||||
$rows .= '<tr>' .
|
||||
"<td>{$stack['file']}</td>" .
|
||||
"<td>{$stack['line']}</td>".
|
||||
"<td>{$stack['function']}</td>" .
|
||||
'</tr>';
|
||||
}
|
||||
|
||||
return <<<HTML
|
||||
<hr>
|
||||
<style>
|
||||
#pr_debug_bactrace,
|
||||
#pr_debug_bactrace th,
|
||||
#pr_debug_bactrace td {
|
||||
border: 1px solid black;
|
||||
border-collapse: collapse;
|
||||
padding: 4px;
|
||||
}
|
||||
#pr_debug_bactrace tr:nth-child(2n) {
|
||||
background-color: #e5e5e5;
|
||||
}
|
||||
</style>
|
||||
<p>$title</p>
|
||||
<table id="pr_debug_bactrace">
|
||||
<tr>
|
||||
<th><strong>File</strong></th>
|
||||
<th><strong>Line</strong></th>
|
||||
<th><strong>Function</strong></th>
|
||||
</tr>
|
||||
$rows
|
||||
</table>
|
||||
<hr>
|
||||
HTML;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2021 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Framework\App\Request\Http;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Plumrocket\Base\Library\Mobile\Detect;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class DeviceDetect implements DeviceDetectInterface
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Library\Mobile\Detect
|
||||
*/
|
||||
private $mobileDetect;
|
||||
|
||||
/**
|
||||
* @var bool[]
|
||||
*/
|
||||
private $cache = [];
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Library\Mobile\Detect $mobileDetect
|
||||
*/
|
||||
public function __construct(Detect $mobileDetect)
|
||||
{
|
||||
$this->mobileDetect = $mobileDetect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isMobile(RequestInterface $request): bool
|
||||
{
|
||||
if (! $request instanceof Http) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = 'mobile';
|
||||
if (! isset($this->cache[$key])) {
|
||||
$this->cache[$key] = $this->mobileDetect->isMobile();
|
||||
}
|
||||
|
||||
return $this->cache[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isTablet(RequestInterface $request): bool
|
||||
{
|
||||
if (! $request instanceof Http) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = 'tablet';
|
||||
if (! isset($this->cache[$key])) {
|
||||
$this->cache[$key] = $this->mobileDetect->isTablet();
|
||||
}
|
||||
|
||||
return $this->cache[$key];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2021 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
interface DeviceDetectInterface
|
||||
{
|
||||
/**
|
||||
* Check whether current device is mobile
|
||||
*
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @return bool
|
||||
*/
|
||||
public function isMobile(RequestInterface $request): bool;
|
||||
|
||||
/**
|
||||
* Check whether current device is tablet
|
||||
*
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @return bool
|
||||
*/
|
||||
public function isTablet(RequestInterface $request): bool;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2021 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class GetEnabledStoresUrls
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $configUtils;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(
|
||||
ConfigUtils $configUtils,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->storeManager = $storeManager;
|
||||
$this->configUtils = $configUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve list of unique enabled stores urls
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function execute(): array
|
||||
{
|
||||
$storesUrls = [];
|
||||
foreach ($this->storeManager->getStores() as $store) {
|
||||
if ($store->getIsActive()) {
|
||||
$storesUrls[] = $this->configUtils->getStoreConfig(
|
||||
Store::XML_PATH_SECURE_BASE_URL,
|
||||
$store->getId()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($storesUrls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
/**
|
||||
* @since 2.3.1
|
||||
*/
|
||||
class GetRelativePathFromUrl
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(StoreManagerInterface $storeManager)
|
||||
{
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove base url from giving url.
|
||||
*
|
||||
* @param string $url
|
||||
* @param bool $removeGetParams
|
||||
* @param bool $removeFragment
|
||||
* @return string
|
||||
*/
|
||||
public function execute(string $url, bool $removeGetParams = true, bool $removeFragment = true): string
|
||||
{
|
||||
$baseUrl = $this->storeManager->getStore()->getBaseUrl();
|
||||
|
||||
$url = str_replace(["\n", "\r"], '', $url);
|
||||
|
||||
if ($removeGetParams && $this->hasParams($url)) {
|
||||
$paramsWithFragment = strstr($url, '?');
|
||||
if ($removeFragment) {
|
||||
$url = str_replace($paramsWithFragment, '', $url);
|
||||
} else {
|
||||
$url = str_replace(strstr($paramsWithFragment, '#', true), '', $url);
|
||||
}
|
||||
}
|
||||
|
||||
if ($removeFragment && $this->hasFragment($url)) {
|
||||
$url = str_replace(strstr($url, '#'), '', $url);
|
||||
}
|
||||
|
||||
$url = str_replace($baseUrl, '', $url);
|
||||
if ('' === $url || $url[0] !== '/') {
|
||||
$url = '/' . $url;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url has get params.
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool
|
||||
*/
|
||||
private function hasParams(string $url): bool
|
||||
{
|
||||
return strpos($url, '?') !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url has fragment (hash).
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool
|
||||
*/
|
||||
private function hasFragment(string $url): bool
|
||||
{
|
||||
return strpos($url, '#') !== false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,492 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2023 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @since 2.11.0
|
||||
*/
|
||||
class HtmlTag
|
||||
{
|
||||
|
||||
/**
|
||||
* Get first tag from html.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function get(string $tagName, string $html): string
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
if (! preg_match("/<$tagName>|<$tagName\s[^>]*?>/", $html, $results)) {
|
||||
return '';
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
if (! preg_match("/(<$tagName>|<$tagName\s[^>]*?>)[\s\S]*?<\/$tagName>/", $html, $results)) {
|
||||
return '';
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all image tags from html.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function getAll(string $tagName, string $html): array
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
if (! preg_match_all("/<$tagName>|<$tagName\s[^>]*?>/", $html, $results)) {
|
||||
return [];
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
if (! preg_match_all("/(<$tagName>|<$tagName\s[^>]*?>)[\s\S]*?<\/$tagName>/", $html, $results)) {
|
||||
return [];
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tags with specific content.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @param string $tagContent
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function getAllWithContent(string $tagName, string $html, string $tagContent): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
$result = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (false !== strpos($tag, $tagContent)) {
|
||||
$result[] = $tag;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tag is exist in html.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return bool
|
||||
*/
|
||||
public function isExistElement(string $tagName, string $html): bool
|
||||
{
|
||||
return (bool) $this->get($tagName, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse html
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function getTagContent(string $tagName, string $html): string
|
||||
{
|
||||
preg_match('#<' .$tagName . '[\s\S]*?>([^`]*?)<\/' . $tagName . '>#', $html, $matches);
|
||||
return (string) $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse html
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function getTagContents(string $tagName, string $html): array
|
||||
{
|
||||
preg_match_all('#<' .$tagName . '[\s\S]*?>([^`]*?)<\/' . $tagName . '>#', $html, $matches);
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tag is self-closing.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @return bool
|
||||
*/
|
||||
public function isSelfClosingTag(string $tagName): bool
|
||||
{
|
||||
return in_array(
|
||||
$tagName,
|
||||
[
|
||||
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
||||
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
||||
],
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut tag from HTML.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function cut(string $tagName, string $html): array
|
||||
{
|
||||
$tag = $this->get($tagName, $html);
|
||||
if (! $tag) {
|
||||
return ['', $html];
|
||||
}
|
||||
$html = str_replace($tag, '', $html);
|
||||
return [$tag, $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut tags from HTML.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function cutAll(string $tagName, string $html): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
if (! $tags) {
|
||||
return [[], $html];
|
||||
}
|
||||
$html = str_replace($tags, '', $html);
|
||||
return [$tags, $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut first tag with specific content.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @param string $tagContent
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function cutWithContent(string $tagName, string $html, string $tagContent): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
foreach ($tags as $tag) {
|
||||
if (false !== strpos($tag, $tagContent)) {
|
||||
$html = str_replace($tag, '', $html);
|
||||
return [$tag, $html];
|
||||
}
|
||||
}
|
||||
return ['', $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut tags with specific content.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @param string $tagContent
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function cutAllWithContent(string $tagName, string $html, string $tagContent): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
$result = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (false !== strpos($tag, $tagContent)) {
|
||||
$html = str_replace($tag, '', $html);
|
||||
$result[] = $tag;
|
||||
}
|
||||
}
|
||||
return [$result, $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a html string before the first child of the tag.
|
||||
*
|
||||
* @param string $html
|
||||
* @param string $tagName
|
||||
* @param string $htmlString
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function prepend(string $html, string $tagName, string $htmlString): string
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
throw new LocalizedException(__('Cannot prepend html for self-closing tag %1.', $tagName));
|
||||
}
|
||||
|
||||
if (! $this->isExistElement($tagName, $html)) {
|
||||
throw new LocalizedException(__('Cannot find element %1 in which we have to prepend html.', $tagName));
|
||||
}
|
||||
|
||||
if (! $htmlString) {
|
||||
return $html;
|
||||
}
|
||||
return preg_replace("/(<$tagName>|<$tagName\s[^>]*?>)/", "$1\n$htmlString", $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a html string after the last child of the tag.
|
||||
*
|
||||
* @param string $html
|
||||
* @param string $tagName
|
||||
* @param string $htmlString
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function append(string $html, string $tagName, string $htmlString): string
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
throw new LocalizedException(__('Cannot append html for self-closing tag %1.', $tagName));
|
||||
}
|
||||
|
||||
if (! $this->isExistElement($tagName, $html)) {
|
||||
throw new LocalizedException(__('Cannot find element %1 in which we have to append html.', $tagName));
|
||||
}
|
||||
|
||||
if (! $htmlString) {
|
||||
return $html;
|
||||
}
|
||||
return str_replace("</$tagName>", "\n$htmlString\n</$tagName>", $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove empty lines from html.
|
||||
*
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function removeEmptyLines(string $html): string
|
||||
{
|
||||
return preg_replace('/\n{2,}/', '', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute value.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @param bool $useDomDocument
|
||||
* @return string
|
||||
*/
|
||||
public function getAttribute(string $tagHtml, string $attributeName, bool $useDomDocument = true): string
|
||||
{
|
||||
if (! $tagHtml || false === strpos($tagHtml, $attributeName)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$tagName = $this->getTagName($tagHtml);
|
||||
if (! $tagName) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (! $useDomDocument || in_array($tagName, ['source', 'section', 'figure'], true)) {
|
||||
return $this->getAttributeByRegex($tagHtml, $attributeName, $tagName);
|
||||
}
|
||||
|
||||
try {
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($tagHtml);
|
||||
/** @var \DOMElement $element */
|
||||
$element = $doc->getElementsByTagName($tagName)[0];
|
||||
return $element->getAttribute($attributeName);
|
||||
} catch (\Exception $e) {
|
||||
return $this->getAttributeByRegex($tagHtml, $attributeName, $tagName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute value by regex.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @param string $tagName
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeByRegex(string $tagHtml, string $attributeName, string $tagName = ''): string
|
||||
{
|
||||
$tagName = $tagName ?: $this->getTagName($tagHtml);
|
||||
$found = preg_match(
|
||||
"/<{$tagName}[\s\S]*?\s$attributeName\s*?=\s*?['\"]([\s\S]*?)['\"][\s\S]*?>/",
|
||||
$tagHtml,
|
||||
$result
|
||||
);
|
||||
return $found ? trim((string) $result[1]) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attributes from tag.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param array $attributes
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributes(string $tagHtml, array $attributes): array
|
||||
{
|
||||
if (! $tagHtml) {
|
||||
return array_fill_keys($attributes, '');
|
||||
}
|
||||
|
||||
$tagName = $this->getTagName($tagHtml);
|
||||
|
||||
try {
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($tagHtml);
|
||||
/** @var \DOMElement $element */
|
||||
$element = $doc->getElementsByTagName($tagName)[0];
|
||||
$result = [];
|
||||
foreach ($attributes as $attributeName) {
|
||||
$result[$attributeName] = $element->getAttribute($attributeName);
|
||||
}
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
$result = [];
|
||||
foreach ($attributes as $attributeName) {
|
||||
$result[$attributeName] = $this->getAttributeByRegex($tagHtml, $attributeName, $tagName);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attribute value to tag.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function setAttribute(string $tagHtml, string $attributeName, string $value): string
|
||||
{
|
||||
if ($this->getAttribute($tagHtml, $attributeName)) {
|
||||
$tagHtml = preg_replace(
|
||||
"/(<\S[\s\S]*?)\s$attributeName\s*?=\s*?['\"][\s\S]*?['\"]([\s\S]*?>)/",
|
||||
"$1 $attributeName=\"$value\"$2",
|
||||
$tagHtml
|
||||
);
|
||||
} else {
|
||||
$tagName = $this->getTagName($tagHtml);
|
||||
$tagHtml = preg_replace("/^<$tagName/", "<$tagName $attributeName=\"$value\"", $tagHtml);
|
||||
}
|
||||
return $tagHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of first tag in html.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @return string
|
||||
*/
|
||||
public function getTagName(string $tagHtml): string
|
||||
{
|
||||
if (! preg_match('/(?:<(\w+?)\s|<(\S+?)>)/', $tagHtml, $result)) {
|
||||
return '';
|
||||
}
|
||||
return $result[1] ?: $result[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove attribute from tag.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
public function removeAttribute(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
if (! $this->getAttribute($tagHtml, $attributeName, false)) {
|
||||
if (false === strpos($tagHtml, $attributeName)) {
|
||||
return $tagHtml;
|
||||
}
|
||||
$tagHtml = $this->removeEmptyAttribute($tagHtml, $attributeName);
|
||||
return $this->removeAttributeWithoutValue($tagHtml, $attributeName);
|
||||
}
|
||||
return $this->removeExistingAttribute($tagHtml, $attributeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Join two parts of the tag nicely.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $beginning
|
||||
* @param string $end
|
||||
* @return string
|
||||
*/
|
||||
private function joinTag(string $tagHtml, string $beginning, string $end): string
|
||||
{
|
||||
$beginning = trim($beginning);
|
||||
$end = trim($end, "\t\n\r");
|
||||
$lineCount = substr_count($tagHtml, "\n");
|
||||
if (substr_count($beginning, "\n") + substr_count($end, "\n") !== $lineCount) {
|
||||
return "$beginning\n$end";
|
||||
}
|
||||
if (0 === strpos($end, '>')) {
|
||||
return "$beginning$end";
|
||||
}
|
||||
return "$beginning $end";
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove attributes like src in <img src="">.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
private function removeEmptyAttribute(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
return $this->removeExistingAttribute($tagHtml, $attributeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove attributes like required in <input required>.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
private function removeAttributeWithoutValue(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
preg_match("/(<\S[\s\S]*?)\s$attributeName\s*?([\s\S]*?>)/", $tagHtml, $result);
|
||||
if (empty($result[1]) || empty($result[2])) {
|
||||
return $tagHtml;
|
||||
}
|
||||
return $this->joinTag($tagHtml, $result[1], $result[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not check if attribute is exists.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
private function removeExistingAttribute(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
if (false === strpos($tagHtml, "\n")) {
|
||||
return preg_replace(
|
||||
"/(<\S[\s\S]*?)\s$attributeName\s*?=\s*?['\"][\s\S]*?['\"]([\s\S]*?>)/",
|
||||
"$1$2",
|
||||
$tagHtml
|
||||
);
|
||||
}
|
||||
preg_match("/(<\S[\s\S]*?)\s$attributeName\s*?=\s*?['\"][\s\S]*?['\"]([\s\S]*?>)/", $tagHtml, $result);
|
||||
if (empty($result[1]) || empty($result[2])) {
|
||||
return $tagHtml;
|
||||
}
|
||||
return $this->joinTag($tagHtml, $result[1], $result[2]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
/**
|
||||
* @since 2.9.3
|
||||
*/
|
||||
class IsMatchFilePath
|
||||
{
|
||||
|
||||
/**
|
||||
* Check if file path match given pattern.
|
||||
*
|
||||
* @param string $filePath
|
||||
* @param string $pattern
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $filePath, string $pattern): bool
|
||||
{
|
||||
if ('' === $pattern) {
|
||||
return false;
|
||||
}
|
||||
return 1 === preg_match($this->patternToRegex($pattern), $filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file path match at least one of patterns.
|
||||
*
|
||||
* @param string $filePath
|
||||
* @param array $patterns
|
||||
* @return bool
|
||||
*/
|
||||
public function executeList(string $filePath, array $patterns): bool
|
||||
{
|
||||
foreach ($patterns as $pattern) {
|
||||
if ($this->execute($filePath, $pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert pattern to regex.
|
||||
*
|
||||
* @param string $pattern
|
||||
* @return string
|
||||
*/
|
||||
public function patternToRegex(string $pattern): string
|
||||
{
|
||||
if (0 === strpos($pattern, 'regex::')) {
|
||||
return str_replace('regex::', '', $pattern);
|
||||
}
|
||||
|
||||
$mapping = [
|
||||
'\\\\\\+' => '[+]',
|
||||
'\\\\\\*' => '[*]',
|
||||
'/' => '\/',
|
||||
'\*' => '(?:.*)',
|
||||
'\+' => '(?:.+)',
|
||||
];
|
||||
|
||||
$regex = str_replace(
|
||||
array_keys($mapping),
|
||||
array_values($mapping),
|
||||
preg_quote($pattern, '~')
|
||||
);
|
||||
|
||||
return '~^' . $regex . '$~';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
/**
|
||||
* Check if url is matched by pattern, compare url without "get" params and fragment
|
||||
*
|
||||
* @since 2.3.1
|
||||
*/
|
||||
class IsMatchUrl
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Utils\GetRelativePathFromUrl
|
||||
*/
|
||||
private $getRelativePathFromUrl;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Utils\GetRelativePathFromUrl $getRelativePathFromUrl
|
||||
*/
|
||||
public function __construct(GetRelativePathFromUrl $getRelativePathFromUrl)
|
||||
{
|
||||
$this->getRelativePathFromUrl = $getRelativePathFromUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url match the pattern.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $pattern
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $url, string $pattern): bool
|
||||
{
|
||||
if ('' === $pattern) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$relativeUrl = $this->getRelativePathFromUrl->execute($url);
|
||||
$regex = $this->patternToRegex($pattern);
|
||||
return 1 === preg_match($regex, $relativeUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url match at least one of patterns.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $patterns
|
||||
* @return bool
|
||||
*/
|
||||
public function executeList(string $url, array $patterns): bool
|
||||
{
|
||||
foreach ($patterns as $pattern) {
|
||||
if ($this->execute($url, $pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert pattern to regex.
|
||||
*
|
||||
* @param string $pattern
|
||||
* @return string|string[]
|
||||
*/
|
||||
private function patternToRegex(string $pattern): string
|
||||
{
|
||||
if (0 === strpos($pattern, 'regex::')) {
|
||||
return str_replace('regex::', '', $pattern);
|
||||
}
|
||||
|
||||
if (false !== strpos($pattern, 'http://') || false !== strpos($pattern, 'https://')) {
|
||||
$pattern = $this->getRelativePathFromUrl->execute($pattern);
|
||||
} elseif ('' === $pattern || ! in_array($pattern[0], ['/', '*', '+'], true)) {
|
||||
$pattern = '/' . $pattern;
|
||||
}
|
||||
|
||||
$mapping = [
|
||||
'\\\\\\+' => '[+]',
|
||||
'\\\\\\*' => '[*]',
|
||||
'/' => '\/',
|
||||
'\*' => '(?:.*)',
|
||||
'\+' => '(?:.+)',
|
||||
];
|
||||
|
||||
$regex = str_replace(
|
||||
array_keys($mapping),
|
||||
array_values($mapping),
|
||||
preg_quote($pattern, '~')
|
||||
);
|
||||
|
||||
return '~^' . $regex . '$~';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user