1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Assets; |
6
|
|
|
|
7
|
|
|
class UrlFixer |
8
|
|
|
{ |
9
|
|
|
/** @var string[] */ |
10
|
|
|
protected $quotes = ['"', "'"]; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $cacheUrl = ''; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* UrlFixer constructor. |
17
|
|
|
* |
18
|
|
|
* @param string $cacheUrl |
19
|
|
|
*/ |
20
|
|
|
public function __construct(string $cacheUrl) |
21
|
|
|
{ |
22
|
|
|
$this->cacheUrl = $cacheUrl; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* FixCSS tries to compensate for minification of CSS stylesheets |
27
|
|
|
* - It looks for URLs |
28
|
|
|
* - Maintains optional quotes used |
29
|
|
|
* - Calls fixUrl to modify the URL if needed |
30
|
|
|
* |
31
|
|
|
* @param string $content |
32
|
|
|
* @param string $path |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function fixCss(string $content, string $path): string |
37
|
|
|
{ |
38
|
|
|
if (!preg_match_all('/url\s*\(\s*(\S*)\s*\)/Umsi', $content, $matches)) { |
39
|
|
|
return $content; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
foreach ($matches[1] as $i => $match) { |
43
|
|
|
if (mb_strlen($match) < 2) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$q = mb_substr($match, 0, 1); |
48
|
|
|
$q = in_array($q, $this->quotes, true) ? $q : ''; |
49
|
|
|
|
50
|
|
|
if ($q && mb_substr($match, -1) !== $q) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$url = $q ? mb_substr($match, 1, -1) : $match; |
55
|
|
|
|
56
|
|
|
$fixedUrl = $this->fixUrl($url, $path); |
57
|
|
|
|
58
|
|
|
if ($fixedUrl == $url) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$search = $matches[0][$i]; |
63
|
|
|
$replace = 'url(' . $q . $fixedUrl . $q . ')'; |
64
|
|
|
|
65
|
|
|
$content = str_replace($search, $replace, $content); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $content; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $url |
73
|
|
|
* @param string $path |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function fixJs(string $url, string $path): string |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return $url; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Fix URL will try to turn a URL into a full qualified URI |
84
|
|
|
* |
85
|
|
|
* - Data URLs skipped |
86
|
|
|
* - Already fully qualitied URIs skipped |
87
|
|
|
* |
88
|
|
|
* @param string $url |
89
|
|
|
* @param string $path |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function fixUrl(string $url, string $path): string |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if (mb_substr($url, 0, 5) === 'data:') { |
96
|
|
|
return $url; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (strpos($url, '://') !== false) { |
100
|
|
|
return $url; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$ch = mb_substr($url, 0, 1); |
104
|
|
|
if ($ch === '/') { |
105
|
|
|
return $this->cacheUrl . $url; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$origUrl = $url; |
|
|
|
|
109
|
|
|
|
110
|
|
|
while (mb_substr($url, 0, 2) == './') { |
111
|
|
|
$url = mb_substr($url, 2); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$out = 0; |
115
|
|
|
while (mb_substr($url, 0, 3) == '../') { |
116
|
|
|
$out++; |
117
|
|
|
$url = mb_substr($url, 3); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$parts = explode(DIRECTORY_SEPARATOR, $url); |
121
|
|
|
if (count($parts) < $out) { |
122
|
|
|
return $this->cacheUrl . DIRECTORY_SEPARATOR . $parts[count($parts) - 1]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$url = implode(DIRECTORY_SEPARATOR, array_slice($parts, $out)); |
126
|
|
|
|
127
|
|
|
return $this->cacheUrl . DIRECTORY_SEPARATOR . $url; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.