|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace ExoUNX\Vasri; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Builds resources for the access class |
|
10
|
|
|
* Class Builder |
|
11
|
|
|
* |
|
12
|
|
|
* @package ExoUNX\Vasri |
|
13
|
|
|
* @author Gaige Lama <[email protected]> |
|
14
|
|
|
* @license MIT License |
|
15
|
|
|
* @link https://github.com/ExoUNX/Vasri |
|
16
|
|
|
*/ |
|
17
|
|
|
class Builder |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* SHA256 Hash Algorithm |
|
22
|
|
|
*/ |
|
23
|
|
|
private const SHA256 = 'sha256'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SHA384 Hash Algorithm |
|
27
|
|
|
*/ |
|
28
|
|
|
private const SHA384 = 'sha384'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* SHA512 Hash Algorithm |
|
32
|
|
|
*/ |
|
33
|
|
|
private const SHA512 = 'sha512'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Builder constructor. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
//TODO Set config options here |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructs a valid SRI string |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $file |
|
47
|
|
|
* |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
* @throws Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function sri(string $file) |
|
53
|
|
|
{ |
|
54
|
|
|
$algorithm = self::selectAlgorithm(); |
|
55
|
|
|
|
|
56
|
|
|
return $algorithm.'-'.base64_encode( |
|
57
|
|
|
hash_file($algorithm, $file, true) |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Constructs a query string containing the md5 hash of the input file |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $file |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function versioning(string $file) |
|
69
|
|
|
{ |
|
70
|
|
|
return '?id='.hash_file('md5', $file); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sets the HTML attribute based on the file extension and throws an exception if invalid |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
* @throws Exception |
|
80
|
|
|
*/ |
|
81
|
|
|
public function attribute(string $path): string |
|
82
|
|
|
{ |
|
83
|
|
|
$extension = self::parseExtension($path); |
|
84
|
|
|
|
|
85
|
|
|
switch ($extension) { |
|
86
|
|
|
case 'css': |
|
87
|
|
|
$attribute = 'href'; |
|
88
|
|
|
break; |
|
89
|
|
|
case 'js': |
|
90
|
|
|
$attribute = 'src'; |
|
91
|
|
|
break; |
|
92
|
|
|
default: |
|
93
|
|
|
throw new Exception('Invalid or Unsupported Extension'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $attribute; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Checks config for hash algorithm and if nothing is found default to SHA384 |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
* @throws Exception |
|
104
|
|
|
*/ |
|
105
|
|
|
private static function selectAlgorithm(): string |
|
106
|
|
|
{ |
|
107
|
|
|
if ( ! empty(config('vasri.hash-algorithm'))) { |
|
108
|
|
|
$algorithm = config('vasri.hash-algorithm'); |
|
109
|
|
|
if ($algorithm !== self::SHA256 |
|
110
|
|
|
&& $algorithm !== self::SHA384 |
|
111
|
|
|
&& $algorithm !== self::SHA512 |
|
112
|
|
|
) { |
|
113
|
|
|
throw new Exception('Invalid or Unsupported Hash Algorithm'); |
|
114
|
|
|
} |
|
115
|
|
|
} else { |
|
116
|
|
|
$algorithm = self::SHA384; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $algorithm; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Removes everything but the short extension |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $path |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
private static function parseExtension(string $path): string |
|
130
|
|
|
{ |
|
131
|
|
|
return preg_replace("#\?.*#", "", pathinfo($path, PATHINFO_EXTENSION)); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|