1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2020. Volodymyr Hryvinskyi. All rights reserved. |
4
|
|
|
* @author: <mailto:[email protected]> |
5
|
|
|
* @github: <https://github.com/hryvinskyi> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Hryvinskyi\DeferJs\Model\PassesValidator\Validators; |
11
|
|
|
|
12
|
|
|
use Hryvinskyi\Base\Helper\ArrayHelper; |
13
|
|
|
use Hryvinskyi\Base\Helper\Json; |
14
|
|
|
use Hryvinskyi\DeferJs\Helper\Config; |
15
|
|
|
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidatorInterface; |
16
|
|
|
use Magento\Framework\App\Request\Http as RequestHttp; |
17
|
|
|
use Magento\Framework\App\Response\Http; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class SkipScriptsByURLPattern |
21
|
|
|
*/ |
22
|
|
|
class SkipScriptsByURLPattern implements ValidatorInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Config |
26
|
|
|
*/ |
27
|
|
|
private $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RequestHttp |
31
|
|
|
*/ |
32
|
|
|
private $request; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* SkipScriptsByController constructor. |
36
|
|
|
* |
37
|
|
|
* @param Config $config |
38
|
|
|
* @param RequestHttp $request |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
Config $config, |
42
|
|
|
RequestHttp $request |
43
|
|
|
) { |
44
|
|
|
$this->config = $config; |
45
|
|
|
$this->request = $request; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Validator function, handle javascript or not |
50
|
|
|
* |
51
|
|
|
* @param string $script |
52
|
|
|
* @param Http $http |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function validate(string $script, Http $http): bool |
57
|
|
|
{ |
58
|
|
|
$executeUrlPattern = Json::decode($this->config->getExcludeUrlPattern()); |
59
|
|
|
$executeUrlPattern = ArrayHelper::getColumn($executeUrlPattern, 'pattern', false); |
60
|
|
|
|
61
|
|
|
$return = false; |
62
|
|
|
|
63
|
|
|
foreach ($executeUrlPattern as $pattern) { |
64
|
|
|
if ($this->checkPattern($this->request->getRequestUri(), $pattern)) { |
65
|
|
|
$return = true; |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $string |
75
|
|
|
* @param $pattern |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function checkPattern(string $string, string $pattern): bool |
80
|
|
|
{ |
81
|
|
|
$parts = explode('*', $pattern); |
82
|
|
|
$index = 0; |
83
|
|
|
|
84
|
|
|
$shouldBeFirst = true; |
85
|
|
|
|
86
|
|
|
foreach ($parts as $part) { |
87
|
|
|
if ($part == '') { |
88
|
|
|
$shouldBeFirst = false; |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$index = strpos($string, $part, $index); |
93
|
|
|
|
94
|
|
|
if ($index === false) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($shouldBeFirst && $index > 0) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$shouldBeFirst = false; |
103
|
|
|
$index += strlen($part); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (count($parts) == 1) { |
107
|
|
|
return $string == $pattern; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$last = end($parts); |
111
|
|
|
|
112
|
|
|
if ($last == '') { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (strrpos($string, $last) === false) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (strlen($string) - strlen($last) - strrpos($string, $last) > 0) { |
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return true; |
125
|
|
|
} |
126
|
|
|
} |