class-wc-product-download.php
12.1 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* Represents a file which can be downloaded.
*
* @package WooCommerce\Classes
* @version 3.0.0
* @since 3.0.0
*/
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Internal\ProductDownloads\ApprovedDirectories\Register as Download_Directories;
use Automattic\WooCommerce\Internal\Utilities\URL;
defined( 'ABSPATH' ) || exit;
/**
* Product download class.
*/
class WC_Product_Download implements ArrayAccess {
/**
* Data array.
*
* @since 3.0.0
* @var array
*/
protected $data = array(
'id' => '',
'name' => '',
'file' => '',
'enabled' => true,
);
/**
* Returns all data for this object.
*
* @return array
*/
public function get_data() {
return $this->data;
}
/**
* Get allowed mime types.
*
* @return array
*/
public function get_allowed_mime_types() {
return apply_filters( 'woocommerce_downloadable_file_allowed_mime_types', get_allowed_mime_types() );
}
/**
* Get type of file path set.
*
* @param string $file_path optional.
* @return string absolute, relative, or shortcode.
*/
public function get_type_of_file_path( $file_path = '' ) {
$file_path = $file_path ? $file_path : $this->get_file();
$parsed_url = wp_parse_url( $file_path );
if (
$parsed_url &&
isset( $parsed_url['host'] ) && // Absolute url means that it has a host.
( // Theoretically we could permit any scheme (like ftp as well), but that has not been the case before. So we allow none or http(s).
! isset( $parsed_url['scheme'] ) ||
in_array( $parsed_url['scheme'], array( 'http', 'https' ), true )
)
) {
return 'absolute';
} elseif ( '[' === substr( $file_path, 0, 1 ) && ']' === substr( $file_path, -1 ) ) {
return 'shortcode';
} else {
return 'relative';
}
}
/**
* Get file type.
*
* @return string
*/
public function get_file_type() {
$type = wp_check_filetype( strtok( $this->get_file(), '?' ), $this->get_allowed_mime_types() );
return $type['type'];
}
/**
* Get file extension.
*
* @return string
*/
public function get_file_extension() {
$parsed_url = wp_parse_url( $this->get_file(), PHP_URL_PATH );
return pathinfo( $parsed_url, PATHINFO_EXTENSION );
}
/**
* Confirms that the download is of an allowed filetype, that it exists and that it is
* contained within an approved directory. Used before adding to a product's list of
* downloads.
*
* @internal
* @throws Exception If the download is determined to be invalid.
*
* @param bool $auto_add_to_approved_directory_list If the download is not already in the approved directory list, automatically add it if possible.
*/
public function check_is_valid( bool $auto_add_to_approved_directory_list = true ) {
$download_file = $this->get_file();
if ( ! $this->data['enabled'] ) {
throw new Exception(
sprintf(
/* translators: %s: Downloadable file. */
__( 'The downloadable file %s cannot be used as it has been disabled.', 'woocommerce' ),
'<code>' . basename( $download_file ) . '</code>'
)
);
}
if ( ! $this->is_allowed_filetype() ) {
throw new Exception(
sprintf(
/* translators: 1: Downloadable file, 2: List of allowed filetypes. */
__( 'The downloadable file %1$s cannot be used as it does not have an allowed file type. Allowed types include: %2$s', 'woocommerce' ),
'<code>' . basename( $download_file ) . '</code>',
'<code>' . implode( ', ', array_keys( $this->get_allowed_mime_types() ) ) . '</code>'
)
);
}
// Validate the file exists.
if ( ! $this->file_exists() ) {
throw new Exception(
sprintf(
/* translators: %s: Downloadable file */
__( 'The downloadable file %s cannot be used as it does not exist on the server.', 'woocommerce' ),
'<code>' . $download_file . '</code>'
)
);
}
$this->approved_directory_checks( $auto_add_to_approved_directory_list );
}
/**
* Check if file is allowed.
*
* @return boolean
*/
public function is_allowed_filetype() {
$file_path = $this->get_file();
// File types for URL-based files located on the server should get validated.
$parsed_file_path = WC_Download_Handler::parse_file_path( $file_path );
$is_file_on_server = ! $parsed_file_path['remote_file'];
$file_path_type = $this->get_type_of_file_path( $file_path );
// Shortcodes are allowed, validations should be done by the shortcode provider in this case.
if ( 'shortcode' === $file_path_type ) {
return true;
}
// Remote paths are allowed.
if ( ! $is_file_on_server && 'relative' !== $file_path_type ) {
return true;
}
// On windows system, local files ending with `.` are not allowed.
// @link https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#naming-conventions.
if ( $is_file_on_server && ! $this->get_file_extension() && 'WIN' === strtoupper( substr( Constants::get_constant( 'PHP_OS' ), 0, 3 ) ) ) {
if ( '.' === substr( $file_path, -1 ) ) {
return false;
}
}
return ! $this->get_file_extension() || in_array( $this->get_file_type(), $this->get_allowed_mime_types(), true );
}
/**
* Validate file exists.
*
* @return boolean
*/
public function file_exists() {
if ( 'relative' !== $this->get_type_of_file_path() ) {
return true;
}
$file_url = $this->get_file();
if ( '..' === substr( $file_url, 0, 2 ) || '/' !== substr( $file_url, 0, 1 ) ) {
$file_url = realpath( ABSPATH . $file_url );
} elseif ( substr( WP_CONTENT_DIR, strlen( untrailingslashit( ABSPATH ) ) ) === substr( $file_url, 0, strlen( substr( WP_CONTENT_DIR, strlen( untrailingslashit( ABSPATH ) ) ) ) ) ) {
$file_url = realpath( WP_CONTENT_DIR . substr( $file_url, 11 ) );
}
return apply_filters( 'woocommerce_downloadable_file_exists', file_exists( $file_url ), $this->get_file() );
}
/**
* Confirms that the download exists within an approved directory.
*
* If it is not within an approved directory but the current user has sufficient
* capabilities, then the method will try to add the download's directory to the
* approved directory list.
*
* @throws Exception If the download is not in an approved directory.
*
* @param bool $auto_add_to_approved_directory_list If the download is not already in the approved directory list, automatically add it if possible.
*/
private function approved_directory_checks( bool $auto_add_to_approved_directory_list = true ) {
$download_directories = wc_get_container()->get( Download_Directories::class );
if ( $download_directories->get_mode() !== Download_Directories::MODE_ENABLED ) {
return;
}
$download_file = $this->get_file();
/**
* Controls whether shortcodes should be resolved and validated using the Approved Download Directory feature.
*
* @param bool $should_validate
*/
if ( apply_filters( 'woocommerce_product_downloads_approved_directory_validation_for_shortcodes', true ) && 'shortcode' === $this->get_type_of_file_path() ) {
$download_file = do_shortcode( $download_file );
}
$is_site_administrator = is_multisite() ? current_user_can( 'manage_sites' ) : current_user_can( 'manage_options' );
$valid_storage_directory = $download_directories->is_valid_path( $download_file );
if ( $valid_storage_directory ) {
return;
}
if ( $auto_add_to_approved_directory_list ) {
try {
// Add the parent URL to the approved directories list, but *do not enable it* unless the current user is a site admin.
$download_directories->add_approved_directory( ( new URL( $download_file ) )->get_parent_url(), $is_site_administrator );
$valid_storage_directory = $download_directories->is_valid_path( $download_file );
} catch ( Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// At this point, $valid_storage_directory will be false. Fall-through so the appropriate exception is
// triggered (same as if the storage directory was invalid and $auto_add_to_approved_directory_list was false.
}
}
if ( ! $valid_storage_directory ) {
throw new Exception(
sprintf(
/* translators: %1$s is the downloadable file path, %2$s is an opening link tag, %3%s is a closing link tag. */
__( 'The downloadable file %1$s cannot be used: it is not located in an approved directory. Please contact a site administrator for help. %2$sLearn more.%3$s', 'woocommerce' ),
'<code>' . $download_file . '</code>',
'<a href="https://woocommerce.com/document/approved-download-directories">',
'</a>'
)
);
}
}
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
/**
* Set ID.
*
* @param string $value Download ID.
*/
public function set_id( $value ) {
$this->data['id'] = wc_clean( $value );
}
/**
* Set name.
*
* @param string $value Download name.
*/
public function set_name( $value ) {
$this->data['name'] = wc_clean( $value );
}
/**
* Set previous_hash.
*
* @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
* @param string $value Previous hash.
*/
public function set_previous_hash( $value ) {
wc_deprecated_function( __FUNCTION__, '3.3' );
$this->data['previous_hash'] = wc_clean( $value );
}
/**
* Set file.
*
* @param string $value File URL/Path.
*/
public function set_file( $value ) {
// A `///` is recognized as an "absolute", but on the filesystem, so it bypasses the mime check in `self::is_allowed_filetype`.
// This will strip extra prepending / to the maximum of 2.
if ( preg_match( '#^//+(/[^/].+)$#i', $value, $matches ) ) {
$value = $matches[1];
}
switch ( $this->get_type_of_file_path( $value ) ) {
case 'absolute':
$this->data['file'] = esc_url_raw( $value );
break;
default:
$this->data['file'] = wc_clean( $value );
break;
}
}
/**
* Sets the status of the download to enabled (true) or disabled (false).
*
* @param bool $enabled True indicates the downloadable file is enabled, false indicates it is disabled.
*/
public function set_enabled( bool $enabled = true ) {
$this->data['enabled'] = $enabled;
}
/*
|--------------------------------------------------------------------------
| Getters
|--------------------------------------------------------------------------
*/
/**
* Get id.
*
* @return string
*/
public function get_id() {
return $this->data['id'];
}
/**
* Get name.
*
* @return string
*/
public function get_name() {
return $this->data['name'];
}
/**
* Get previous_hash.
*
* @deprecated 3.3.0 No longer using filename based hashing to keep track of files.
* @return string
*/
public function get_previous_hash() {
wc_deprecated_function( __FUNCTION__, '3.3' );
return $this->data['previous_hash'];
}
/**
* Get file.
*
* @return string
*/
public function get_file() {
return $this->data['file'];
}
/**
* Get status of the download.
*
* @return bool
*/
public function get_enabled(): bool {
return $this->data['enabled'];
}
/*
|--------------------------------------------------------------------------
| ArrayAccess/Backwards compatibility.
|--------------------------------------------------------------------------
*/
/**
* OffsetGet.
*
* @param string $offset Offset.
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet( $offset ) {
switch ( $offset ) {
default:
if ( is_callable( array( $this, "get_$offset" ) ) ) {
return $this->{"get_$offset"}();
}
break;
}
return '';
}
/**
* OffsetSet.
*
* @param string $offset Offset.
* @param mixed $value Offset value.
*/
#[\ReturnTypeWillChange]
public function offsetSet( $offset, $value ) {
switch ( $offset ) {
default:
if ( is_callable( array( $this, "set_$offset" ) ) ) {
$this->{"set_$offset"}( $value );
}
break;
}
}
/**
* OffsetUnset.
*
* @param string $offset Offset.
*/
#[\ReturnTypeWillChange]
public function offsetUnset( $offset ) {}
/**
* OffsetExists.
*
* @param string $offset Offset.
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists( $offset ) {
return in_array( $offset, array_keys( $this->data ), true );
}
}