-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Fingerprint.php
251 lines (222 loc) · 6.08 KB
/
Fingerprint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace Bnomei;
use Exception;
use Kirby\Cms\Url;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\A;
use function array_key_exists;
final class Fingerprint
{
/**
* @var array
*/
private $options;
/**
* Fingerprint constructor.
* @param array $options
*/
public function __construct(array $options = [])
{
$defaults = [
'debug' => option('debug'),
'query' => option('bnomei.fingerprint.query'),
'hash' => option('bnomei.fingerprint.hash'),
'integrity' => option('bnomei.fingerprint.integrity'),
'https' => option('bnomei.fingerprint.https'),
];
$this->options = array_merge($defaults, $options);
foreach ($this->options as $key => $call) {
if (is_callable($call) && !in_array($key, ['hash', 'integrity'])) {
$this->options[$key] = $call();
}
}
if ($this->option('debug')) {
try {
kirby()->cache('bnomei.fingerprint')->flush();
} catch (Exception $e) {
//
}
}
}
/**
* @param string|null $key
* @return array|mixed
*/
public function option(?string $key = null)
{
if ($key) {
return A::get($this->options, $key);
}
return $this->options;
}
/**
* @param string $option
* @param $file
* @return mixed|null
*/
public function apply(string $option, $file)
{
$callback = $this->option($option);
if ($callback && is_callable($callback)) {
if ($option === 'integrity') {
return call_user_func_array($callback, [$file]);
} elseif ($option === 'hash') {
return call_user_func_array($callback, [$file, $this->option('query')]);
}
}
return null;
}
/**
* @param $url
* @return mixed
*/
public function https($url)
{
if ($this->option('https') && !kirby()->system()->isLocal()) {
$url = str_replace('http://', 'https://', $url);
}
return $url;
}
/**
* @param $file
* @return mixed
* @throws InvalidArgumentException
*/
public function process($file)
{
$needsPush = false;
$lookup = $this->read();
if (! $lookup) {
$lookup = [];
$needsPush = true;
}
$finFile = new FingerprintFile($file);
$id = $finFile->id();
$mod = $finFile->modified();
if (! array_key_exists($id, $lookup)) {
$needsPush = true;
} elseif ($mod && $lookup[$id]['modified'] < $mod) {
$needsPush = true;
}
if ($needsPush) {
$lookup[$id] = [
'modified' => $mod,
'root' => $finFile->fileRoot(),
'integrity' => $this->apply('integrity', $file),
'hash' => $this->apply('hash', $file),
];
$this->write($lookup);
}
return A::get($lookup, $id);
}
/**
* @param array $attrs
* @param array $lookup
* @return array
*/
public function attrs(array $attrs, array $lookup)
{
$sri = A::get($attrs, 'integrity', false);
if ($sri === true) {
$sri = A::get($lookup, 'integrity');
}
if ($sri && strlen($sri) > 0) {
$attrs['integrity'] = $sri;
$attrs['crossorigin'] = A::get($attrs, 'crossorigin', 'anonymous');
} elseif (! $sri) {
if (array_key_exists('integrity', $attrs)) {
unset($attrs['integrity']);
}
if (array_key_exists('crossorigin', $attrs)) {
unset($attrs['crossorigin']);
}
}
return $attrs;
}
/**
* @param string $extension
* @param string $url
* @param array $attrs
* @return string|null
*/
public function helper(string $extension, string $url, array $attrs = []): ?string
{
if (! is_callable($extension)) {
return null;
}
if ($url === '@auto') {
$assetUrl = Url::toTemplateAsset($extension.'/templates', $extension);
if ($assetUrl) {
$url = $assetUrl;
}
}
$lookup = $this->process($url);
$attrs = $this->attrs($attrs, $lookup);
return $this->https($extension($lookup['hash'], $attrs));
}
/**
* @return string
*/
public function cacheKey(): string
{
return implode('-', [
'lookup',
str_replace('.', '-', kirby()->plugin('bnomei/fingerprint')->version()),
$this->option('query') ? 'query' : 'redirect'
]);
}
/**
* @return array|null
*/
public function read(): ?array
{
if ($this->option('debug')) {
return null;
}
return kirby()->cache('bnomei.fingerprint')->get($this->cacheKey());
}
/**
* @param array $lookup
* @return bool
*/
private function write(array $lookup): bool
{
if ($this->option('debug')) {
return false;
}
return kirby()->cache('bnomei.fingerprint')->set($this->cacheKey(), $lookup);
}
/**
* @param $url
* @param array $attrs
* @return mixed
*/
public static function css($url, $attrs = []): string
{
if (is_string( $attrs)) {
$attrs = ['media' => $attrs];
}
return (new Fingerprint())->helper('css', $url, $attrs);
}
/**
* @param $url
* @param array $attrs
* @return mixed
*/
public static function js($url, $attrs = []): string
{
return (new Fingerprint())->helper('js', $url, $attrs);
}
/**
* @param $url
* @param array $attrs
* @return mixed
*/
public static function url($url): string
{
$fingerprint = new Fingerprint();
$url = $fingerprint->process($url)['hash'];
return $fingerprint->https($url);
}
}