-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathCdnEngine_Base.php
More file actions
699 lines (600 loc) · 16.4 KB
/
CdnEngine_Base.php
File metadata and controls
699 lines (600 loc) · 16.4 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
<?php
/**
* File: CdnEngine_Base.php
*
* @package W3TC
*/
namespace W3TC;
/**
* W3 CDN Base class
*/
define( 'W3TC_CDN_RESULT_HALT', -1 );
define( 'W3TC_CDN_RESULT_ERROR', 0 );
define( 'W3TC_CDN_RESULT_OK', 1 );
define( 'W3TC_CDN_HEADER_NONE', 'none' );
define( 'W3TC_CDN_HEADER_UPLOADABLE', 'uploadable' );
define( 'W3TC_CDN_HEADER_MIRRORING', 'mirroring' );
/**
* Class CdnEngine_Base
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
* phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
* phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
* phpcs:disable WordPress.WP.AlternativeFunctions
* phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
*/
class CdnEngine_Base {
/**
* Engine configuration
*
* @var array
*/
protected $_config = array();
/**
* Gzip extension
*
* @var string
*/
protected $_gzip_extension = '.gzip';
/**
* Last error
*
* @var string
*/
protected $_last_error = '';
/**
* Constructor method for initializing the CdnEngine_Base object with configuration settings.
*
* @param array $config Optional. An array of configuration options to override default values.
* Defaults include 'debug', 'ssl', 'compression', and 'headers'.
*/
public function __construct( $config = array() ) {
$this->_config = array_merge(
array(
'debug' => false,
'ssl' => 'auto',
'compression' => false,
'headers' => array(),
),
$config
);
}
/**
* Upload files to the CDN.
*
* @param array $files An array of files to upload.
* @param array $results A reference to an array where results will be stored.
* @param bool $force_rewrite Optional. Whether to force a rewrite. Default is false.
* @param int $timeout_time Optional. The timeout time in seconds. Default is null.
*
* @return bool False on failure.
*/
public function upload( $files, &$results, $force_rewrite = false, $timeout_time = null ) {
$results = $this->_get_results(
$files,
W3TC_CDN_RESULT_HALT,
'Not implemented.'
);
return false;
}
/**
* Delete files from the CDN.
*
* @param array $files An array of files to delete.
* @param array $results A reference to an array where results will be stored.
*
* @return bool False on failure.
*/
public function delete( $files, &$results ) {
$results = $this->_get_results(
$files,
W3TC_CDN_RESULT_HALT,
'Not implemented.'
);
return false;
}
/**
* Purge files from the CDN.
*
* @param array $files An array of files to purge.
* @param array $results A reference to an array where results will be stored.
*
* @return bool False on failure.
*/
public function purge( $files, &$results ) {
return $this->upload( $files, $results, true );
}
/**
* Purge all files from the CDN.
*
* @param array $results A reference to an array where results will be stored.
*
* @return bool False on failure.
*/
public function purge_all( &$results ) {
$results = $this->_get_results(
array(),
W3TC_CDN_RESULT_HALT,
'Not implemented.'
);
return false;
}
/**
* Test the connection to the CDN.
*
* @param string $error A reference to a variable where any error message will be stored.
*
* @return bool True if the test is successful, false otherwise.
*/
public function test( &$error ) {
if ( ! $this->_test_domains( $error ) ) {
return false;
}
return true;
}
/**
* Create a container on the CDN.
*
* @throws \Exception If the method is not implemented.
*/
public function create_container() {
throw new \Exception( \esc_html__( 'Not implemented.', 'w3-total-cache' ) );
}
/**
* Get the appropriate domain for a given path.
*
* @param string $path Optional. The path to check. Default is an empty string.
*
* @return string|false The selected domain or false if no domain is found.
*/
public function get_domain( $path = '' ) {
$domains = $this->get_domains();
$count = count( $domains );
if ( $count ) {
switch ( true ) {
/**
* Reserved CSS
*/
case ( isset( $domains[0] ) && $this->_is_css( $path ) ):
$domain = $domains[0];
break;
/**
* Reserved JS after body
*/
case ( isset( $domains[2] ) && $this->_is_js_body( $path ) ):
$domain = $domains[2];
break;
/**
* Reserved JS before /body
*/
case ( isset( $domains[3] ) && $this->_is_js_footer( $path ) ):
$domain = $domains[3];
break;
/**
* Reserved JS in head, moved here due to greedy regex
*/
case ( isset( $domains[1] ) && $this->_is_js( $path ) ):
$domain = $domains[1];
break;
default:
if ( ! isset( $domains[0] ) ) {
$scheme = $this->_get_scheme();
if ( 'https' === $scheme && ! empty( $domains['https_default'] ) ) {
return $domains['https_default'];
} else {
return isset( $domains['http_default'] ) ? $domains['http_default'] :
$domains['https_default'];
}
} elseif ( $count > 4 ) {
$domain = $this->_get_domain( array_slice( $domains, 4 ), $path );
} else {
$domain = $this->_get_domain( $domains, $path );
}
}
/**
* Custom host for SSL
*/
list( $domain_http, $domain_https ) = array_map( 'trim', explode( ',', $domain . ',' ) );
$scheme = $this->_get_scheme();
switch ( $scheme ) {
case 'http':
$domain = $domain_http;
break;
case 'https':
$domain = ( $domain_https ? $domain_https : $domain_http );
break;
}
return $domain;
}
return false;
}
/**
* Get all available domains.
*
* @return array An array of domains.
*/
public function get_domains() {
return array();
}
/**
* Get the domain used for accessing the CDN.
*
* @return string The domain URL.
*/
public function get_via() {
$domain = $this->get_domain();
if ( $domain ) {
return $domain;
}
return 'N/A';
}
/**
* Format a URL for the given path.
*
* @param string $path The path to format.
*
* @return string|false The formatted URL or false on failure.
*/
public function format_url( $path ) {
$url = $this->_format_url( $path );
if ( $url && $this->_config['compression'] && ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ? stristr( sanitize_text_field( wp_unslash( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ), 'gzip' ) !== false : false ) && $this->_may_gzip( $path ) ) {
$qpos = strpos( $url, '?' );
if ( false !== $qpos ) {
$url = substr_replace( $url, $this->_gzip_extension, $qpos, 0 );
} else {
$url .= $this->_gzip_extension;
}
}
return $url;
}
/**
* Get the URL to prepend to a given path.
*
* @param string $path The path to prepend the URL to.
*
* @return string|false The full URL or false if no domain is found.
*/
public function get_prepend_path( $path ) {
$domain = $this->get_domain( $path );
if ( $domain ) {
$scheme = $this->_get_scheme();
$url = sprintf( '%s://%s', $scheme, $domain );
return $url;
}
return false;
}
/**
* Format a URL for the given path, with the appropriate scheme and domain.
*
* @param string $path The path to format.
*
* @return string|false The formatted URL or false if no domain is found.
*/
public function _format_url( $path ) {
$domain = $this->get_domain( $path );
if ( $domain ) {
$scheme = $this->_get_scheme();
$url = sprintf( '%s://%s/%s', $scheme, $domain, $path );
return $url;
}
return false;
}
/**
* Get results for a set of files.
*
* @param array $files The files for which results are generated.
* @param string $result Optional. The result status. Default is W3TC_CDN_RESULT_OK.
* @param string $error Optional. The error message. Default is 'OK'.
*
* @return array An array of results for each file.
*/
public function _get_results( $files, $result = W3TC_CDN_RESULT_OK, $error = 'OK' ) {
$results = array();
foreach ( $files as $key => $file ) {
if ( is_array( $file ) ) {
$local_path = $file['local_path'];
$remote_path = $file['remote_path'];
} else {
$local_path = $key;
$remote_path = $file;
}
$results[] = $this->_get_result(
$local_path,
$remote_path,
$result,
$error,
$file
);
}
return $results;
}
/**
* Retrieves the result data for a local and remote file path.
*
* @param string $local_path The local file path.
* @param string $remote_path The remote file path.
* @param int $result The result status (default is W3TC_CDN_RESULT_OK).
* @param string $error The error message (default is 'OK').
* @param mixed|null $descriptor Additional descriptor (default is null).
*
* @return array The result array containing local path, remote path, result, error, and descriptor.
*/
public function _get_result( $local_path, $remote_path, $result = W3TC_CDN_RESULT_OK, $error = 'OK', $descriptor = null ) {
if ( $this->_config['debug'] ) {
$this->_log( $local_path, $remote_path, $error );
}
return array(
'local_path' => $local_path,
'remote_path' => $remote_path,
'result' => $result,
'error' => $error,
'descriptor' => $descriptor,
);
}
/**
* Checks if any of the results contain an error.
*
* @param array $results The results to check.
*
* @return bool True if any result is an error, otherwise false.
*/
public function _is_error( $results ) {
foreach ( $results as $result ) {
if ( W3TC_CDN_RESULT_OK !== $result['result'] ) {
return true;
}
}
return false;
}
/**
* Retrieves the HTTP headers for a given file.
*
* @param array $file The file data array containing local path and original URL.
* @param array $whitelist Optional whitelist for specific headers (default is empty).
*
* @return array The HTTP headers for the file.
*/
public function get_headers_for_file( $file, $whitelist = array() ) {
$local_path = $file['local_path'];
$mime_type = Util_Mime::get_mime_type( $local_path );
$link = $file['original_url'];
$headers = array(
'Content-Type' => $mime_type,
'Last-Modified' => Util_Content::http_date( time() ),
'Access-Control-Allow-Origin' => '*',
'Link' => '<' . $link . '>; rel="canonical"',
);
$section = Util_Mime::mime_type_to_section( $mime_type );
if ( isset( $this->_config['headers'][ $section ] ) ) {
$hc = $this->_config['headers'][ $section ];
if ( isset( $whitelist['ETag'] ) && $hc['etag'] ) {
$headers['ETag'] = '"' . @md5_file( $local_path ) . '"';
}
if ( $hc['expires'] ) {
$headers['Expires'] = Util_Content::http_date( time() + $hc['lifetime'] );
$expires_set = true;
}
$headers = array_merge( $headers, $hc['static'] );
}
return $headers;
}
/**
* Determines whether a file may be compressed using Gzip.
*
* @param string $file The file path.
*
* @return bool True if the file may be gzipped, otherwise false.
*/
public function _may_gzip( $file ) {
/**
* Remove query string
*/
$file = preg_replace( '~\?.*$~', '', $file );
/**
* Check by file extension
*/
if ( preg_match( '~\.(ico|js|css|xml|xsd|xsl|svg|htm|html|txt)$~i', $file ) ) {
return true;
}
return false;
}
/**
* Tests the configured domains for valid hostnames.
*
* @param string $error A reference to store the error message if any domain is invalid.
*
* @return bool True if all domains are valid, otherwise false.
*/
public function _test_domains( &$error ) {
$domains = $this->get_domains();
if ( ! count( $domains ) ) {
$error = 'Empty hostname / CNAME list.';
return false;
}
foreach ( $domains as $domain ) {
$_domains = array_map( 'trim', explode( ',', $domain ) );
foreach ( $_domains as $_domain ) {
$matches = null;
if ( preg_match( '~^([a-z0-9\-\.]*)~i', $_domain, $matches ) ) {
$hostname = $matches[1];
} else {
$hostname = $_domain;
}
if ( empty( $hostname ) ) {
continue;
}
if ( gethostbyname( $hostname ) === $hostname ) {
$error = sprintf( 'Unable to resolve hostname: %s.', $hostname );
return false;
}
}
}
return true;
}
/**
* Checks if a file is a CSS file.
*
* @param string $path The file path.
*
* @return bool True if the file is a CSS file, otherwise false.
*/
public function _is_css( $path ) {
return preg_match( '~[a-zA-Z0-9\-_]*(\.include\.[0-9]+)?\.css$~', $path );
}
/**
* Checks if a file is a JavaScript file.
*
* @param string $path The file path.
*
* @return bool True if the file is a JavaScript file, otherwise false.
*/
public function _is_js( $path ) {
return preg_match( '~([a-z0-9\-_]+(\.include\.[a-z0-9]+)\.js)$~', $path ) || preg_match( '~[\w\d\-_]+\.js~', $path );
}
/**
* Checks if a file is a JavaScript file that should be included in the body.
*
* @param string $path The file path.
*
* @return bool True if the file is a JavaScript file for the body, otherwise false.
*/
public function _is_js_body( $path ) {
return preg_match( '~[a-z0-9\-_]+(\.include-body\.[a-z0-9]+)\.js$~', $path );
}
/**
* Checks if a file is a JavaScript file that should be included in the footer.
*
* @param string $path The file path.
*
* @return bool True if the file is a JavaScript file for the footer, otherwise false.
*/
public function _is_js_footer( $path ) {
return preg_match( '~[a-z0-9\-_]+(\.include-footer\.[a-z0-9]+)\.js$~', $path );
}
/**
* Retrieves the domain for a specific file path from a list of domains.
*
* @param array $domains The list of domains.
* @param string $path The file path.
*
* @return string|false The selected domain or false if no domain is found.
*/
public function _get_domain( $domains, $path ) {
$count = count( $domains );
if ( isset( $domains['http_default'] ) ) {
--$count;
}
if ( isset( $domains['https_default'] ) ) {
--$count;
}
if ( $count ) {
/**
* Use for equal URLs same host to allow caching by browser
*/
$hash = $this->_get_hash( $path );
$domain = $domains[ $hash % $count ];
return $domain;
}
return false;
}
/**
* Generates a hash from a given key.
*
* @param string $key The key to hash.
*
* @return int The generated hash value.
*/
public function _get_hash( $key ) {
$hash = abs( crc32( $key ) );
return $hash;
}
/**
* Retrieves the scheme (HTTP or HTTPS) based on the configuration.
*
* @return string The scheme ('http' or 'https').
*/
public function _get_scheme() {
switch ( $this->_config['ssl'] ) {
default:
case 'auto':
$scheme = ( Util_Environment::is_https() ? 'https' : 'http' );
break;
case 'enabled':
$scheme = 'https';
break;
case 'disabled':
$scheme = 'http';
break;
case 'rejected':
$scheme = 'http';
break;
}
return $scheme;
}
/**
* Logs a message with local and remote file paths and an error.
*
* @param string $local_path The local file path.
* @param string $remote_path The remote file path.
* @param string $error The error message.
*
* @return int|false The number of bytes written to the log file, or false on failure.
*/
public function _log( $local_path, $remote_path, $error ) {
$data = sprintf( "[%s] [%s => %s] %s\n", gmdate( 'r' ), $local_path, $remote_path, $error );
$data = strtr( $data, '<>', '..' );
$filename = Util_Debug::log_filename( 'cdn' );
return @file_put_contents( $filename, $data, FILE_APPEND );
}
/**
* Handles errors by saving the error message.
*
* @param int $errno The error number.
* @param string $errstr The error message.
*
* @return bool Always returns false.
*/
public function _error_handler( $errno, $errstr ) {
$this->_last_error = $errstr;
return false;
}
/**
* Retrieves the last error message.
*
* @return string The last error message.
*/
public function _get_last_error() {
return $this->_last_error;
}
/**
* Sets a custom error handler.
*
* phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler
*
* @return void
*/
public function _set_error_handler() {
set_error_handler(
array(
$this,
'_error_handler',
)
);
}
/**
* Restores the default error handler.
*
* @return void
*/
public function _restore_error_handler() {
restore_error_handler();
}
/**
* Retrieves the header support status.
*
* @return string The header support status (W3TC_CDN_HEADER_NONE).
*/
public function headers_support() {
return W3TC_CDN_HEADER_NONE;
}
}