AssetsLocalCache.php
4.15 KB
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
<?php
namespace WP_Rocket\Engine\Optimization;
use WP_Filesystem_Direct;
/**
* Cache locally 3rd party assets.
*
* @since 3.1
*/
class AssetsLocalCache {
/**
* 3rd party assets cache folder path
*
* @since 3.1
*
* @var string
*/
protected $cache_path;
/**
* Filesystem instance.
*
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* Cache contents with url.
*
* @var array
*/
private $url_cache = [];
/**
* Constructor
*
* @since 3.1
*
* @param string $cache_path 3rd party assets cache folder path.
* @param WP_Filesystem_Direct $filesystem Filesysten instance.
*/
public function __construct( $cache_path, $filesystem ) {
$this->cache_path = "{$cache_path}3rd-party/";
$this->filesystem = $filesystem;
}
/**
* Get remote file contents.
*
* @param string $url Url of the file to get contents for.
*
* @return string Raw file contents.
*/
private function get_raw_content( $url ) {
$cache_key = md5( $url );
if ( ! isset( $this->url_cache[ $cache_key ] ) ) {
$this->url_cache[ $cache_key ] = wp_remote_retrieve_body( wp_remote_get( $url ) );
}
return $this->url_cache[ $cache_key ];
}
/**
* Gets content for the provided URL.
* Use the local cache file if it exists, else get it from the 3rd party URL and save it locally for future use.
*
* @since 3.1
*
* @param string $url URL to get the content from.
*
* @return string
*/
public function get_content( $url ) {
$filepath = $this->get_filepath( $url );
if ( empty( $filepath ) ) {
return '';
}
if ( $this->filesystem->is_readable( $filepath ) ) {
return $this->filesystem->get_contents( $filepath );
}
$content = $this->get_raw_content( $url );
if ( empty( $content ) ) {
return '';
}
$this->write_file( $content, $filepath );
return $content;
}
/**
* Gets the filepath of the local copy for the given URL
*
* @since 3.7
*
* @param string $url URL to get filepath for.
* @return string
*/
public function get_filepath( $url ) {
$parts = wp_parse_url( $url );
if ( empty( $parts['path'] ) ) {
return '';
}
$filename = $parts['host'] . str_replace( '/', '-', $parts['path'] );
return $this->cache_path . $filename;
}
/**
* Writes the content to a file
*
* @since 3.1
*
* @param string $content Content to write.
* @param string $file Path to the file to write in.
* @return bool
*/
protected function write_file( $content, $file ) {
if ( $this->filesystem->is_readable( $file ) ) {
return true;
}
if ( ! rocket_mkdir_p( dirname( $file ) ) ) {
return false;
}
return rocket_put_content( $file, $content );
}
/**
* Check if this link HTML has integrity attribute or not?
*
* @since 3.7.5
*
* @param string $asset Link HTML to be tested.
*
* @return array|false Matched array with integrityhashmethod, integrityhash keys.
*/
private function has_integrity( $asset ) {
if ( ! preg_match( '#\s*integrity\s*=[\'"](?<integrityhashmethod>.*)-(?<integrityhash>.*)[\'"]#Ui', $asset, $integrity_matches ) ) {
return false;
}
if ( ! isset( $integrity_matches['integrityhashmethod'], $integrity_matches['integrityhash'] ) ) {
return false;
}
return $integrity_matches;
}
/**
* Validate the integrity attribute if the content matches with the hashed integrity value.
*
* @param array $asset_matched the matched array which has 0, url keys.
*
* @return bool|string
*/
public function validate_integrity( $asset_matched ) {
$integrity_matches = $this->has_integrity( $asset_matched[0] );
if ( false === $integrity_matches ) {
return $asset_matched[0];
}
// validate the hash algorithm.
if ( ! in_array( $integrity_matches['integrityhashmethod'], hash_algos(), true ) ) {
return false;
}
$content = $this->get_raw_content( $asset_matched['url'] );
$content_hash = base64_encode( hash( $integrity_matches['integrityhashmethod'], $content, true ) );// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
if ( $integrity_matches['integrityhash'] !== $content_hash ) {
return false;
}
return str_replace( $integrity_matches[0], '', $asset_matched[0] );
}
}