forked from Automattic/vip-go-mu-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha8c-files.php
1186 lines (965 loc) · 37.1 KB
/
a8c-files.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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Automattic File Hosting Service
Description: Provides a hosted, distributed and fault tolerant files service
Author: Automattic
Version: 0.2
Author URI: http://automattic.com/
*/
/* Requires at least: 3.9.0
* due to the dependancy on the filter 'wp_insert_attachment_data'
* used to catch imports and push the files to the VIP MogileFS service
*/
if ( ! defined( 'FILE_SERVICE_ENDPOINT' ) )
define( 'FILE_SERVICE_ENDPOINT', 'files.vipv2.net' );
define( 'LOCAL_UPLOADS', '/tmp/uploads' );
define( 'ALLOW_UNFILTERED_UPLOADS', false );
require_once( __DIR__ . '/files/class-path-utils.php' );
require_once( __DIR__ . '/files/init-filesystem.php' );
require_once( __DIR__ . '/files/class-vip-filesystem.php' );
/**
* The class use to update attachment meta data
*/
require_once __DIR__ . '/files/class-meta-updater.php';
use Automattic\VIP\Files\Path_Utils;
use Automattic\VIP\Files\VIP_Filesystem;
use Automattic\VIP\Files\Meta_Updater;
class A8C_Files {
/**
* The name of the scheduled cron event to update attachment metadata
*/
const CRON_EVENT_NAME = 'vip_update_attachment_filesizes';
/**
* Option name to mark all attachment filesize update completed
*/
const OPT_ALL_FILESIZE_PROCESSED = 'vip_all_attachment_filesize_processed_v2';
/**
* Option name to mark next index for starting the next batch of filesize updates.
*/
const OPT_NEXT_FILESIZE_INDEX = 'vip_next_attachment_filesize_index_v2';
/**
* Option name for storing Max ID.
*
* We do not need to keep this updated as new attachments will already have file sizes
* included in their meta.
*/
const OPT_MAX_POST_ID = 'vip_attachment_max_post_id_v2';
function __construct() {
// Upload size limit is 1GB
add_filter( 'upload_size_limit', function() {
return 1073741824; // pow( 2, 30 )
});
// Conditionally load either the new Stream Wrapper implementation or old school a8c-files.
// The old school implementation will be phased out soon.
if ( defined( 'VIP_FILESYSTEM_USE_STREAM_WRAPPER' ) && true === VIP_FILESYSTEM_USE_STREAM_WRAPPER ) {
$this->init_vip_filesystem();
} else {
$this->init_legacy_filesystem();
}
// Initialize Photon-specific filters.
// Wait till `init` to make sure Jetpack and the Photon module are ready.
add_action( 'init', array( $this, 'init_photon' ) );
// ensure we always upload with year month folder layouts
add_filter( 'pre_option_uploads_use_yearmonth_folders', function( $arg ) { return '1'; } );
// ensure the correct upload URL is used even after switch_to_blog is called
add_filter( 'option_upload_url_path', array( $this, 'upload_url_path' ), 10, 2 );
// Conditionally schedule the attachment filesize metaata update job
if ( defined( 'VIP_FILESYSTEM_SCHEDULE_FILESIZE_UPDATE' ) && true === VIP_FILESYSTEM_SCHEDULE_FILESIZE_UPDATE ) {
// add new cron schedule for filesize update
add_filter( 'cron_schedules', array( $this, 'filter_cron_schedules' ), 10, 1 );
// Schedule meta update job
$this->schedule_update_job();
}
}
/**
* Initializes and wires up Stream Wrapper plugin.
*/
private function init_vip_filesystem() {
$vip_filesystem = new VIP_Filesystem();
$vip_filesystem->run();
}
/**
* Initializes our legacy filter-based approach to uploads.
*/
private function init_legacy_filesystem() {
// Hooks for the mu-plugin WordPress Importer
add_filter( 'load-importer-wordpress', array( &$this, 'check_to_download_file' ), 10 );
add_filter( 'wp_insert_attachment_data', array( &$this, 'check_to_upload_file' ), 10, 2 );
add_filter( 'wp_unique_filename', array( $this, 'filter_unique_filename' ), 10, 4 );
add_filter( 'wp_check_filetype_and_ext', array( $this, 'filter_filetype_check' ), 10, 4 );
add_filter( 'upload_dir', array( &$this, 'get_upload_dir' ), 10, 1 );
add_filter( 'wp_handle_upload', array( &$this, 'upload_file' ), 10, 2 );
add_filter( 'wp_delete_file', array( &$this, 'delete_file' ), 20, 1 );
add_filter( 'wp_save_image_file', array( &$this, 'save_image_file' ), 10, 5 );
add_filter( 'wp_save_image_editor_file', array( &$this, 'save_image_file' ), 10, 5 );
}
function init_photon() {
// Limit to certain contexts for the initial testing and roll-out.
// This will be phased out and become the default eventually.
$use_jetpack_photon = $this->use_jetpack_photon();
if ( $use_jetpack_photon ) {
$this->init_jetpack_photon_filters();
} else {
$this->init_vip_photon_filters();
}
}
private function init_jetpack_photon_filters() {
if ( ! class_exists( 'Jetpack_Photon' ) ) {
trigger_error( 'Cannot initialize Photon filters as the Jetpack_Photon class is not loaded. Please verify that Jetpack is loaded and active to restore this functionality.', E_USER_WARNING );
return;
}
// The files service has Photon capabilities, but is served from the same domain.
// Force Jetpack to use the files service instead of the default Photon domains (`i*.wp.com`) for internal files.
// Externally hosted files continue to use the remot Photon service.
add_filter( 'jetpack_photon_domain', [ 'A8C_Files_Utils', 'filter_photon_domain' ], 10, 2 );
// If Jetpack dev mode is enabled, jetpack_photon_url is short-circuited.
// This results in all images being full size (which is not ideal)
add_filter( 'jetpack_photon_development_mode', '__return_false', 9999 );
if ( false === is_vip_go_srcset_enabled() ) {
add_filter( 'wp_get_attachment_metadata', function ( $data, $post_id ) {
if ( isset( $data['sizes'] ) ) {
$data['sizes'] = array();
}
return $data;
}, 10, 2 );
}
// This is our catch-all to strip dimensions from intermediate images in content.
// Since this primarily only impacts post_content we do a little dance to add the filter early to `the_content` and then remove it later on in the same hook.
add_filter( 'the_content', function( $content ) {
add_filter( 'jetpack_photon_pre_image_url', [ 'A8C_Files_Utils', 'strip_dimensions_from_url_path' ] );
return $content;
}, 0 );
add_filter( 'the_content', function( $content ) {
remove_filter( 'jetpack_photon_pre_image_url', [ 'A8C_Files_Utils', 'strip_dimensions_from_url_path' ] );
return $content;
}, 9999999 ); // Jetpack hooks in at 6 9s (999999) so we do 7
// If Photon isn't active, we need to init the necessary filters.
// This takes care of rewriting intermediate images for us.
Jetpack_Photon::instance();
// Jetpack_Photon's downsize filter doesn't run when is_admin(), so we need to fallback to the Go filters.
// This is temporary until Jetpack allows more easily running these filters for is_admin().
if ( is_admin() ) {
$this->init_vip_photon_filters();
}
}
private function init_vip_photon_filters() {
add_filter( 'image_downsize', array( &$this, 'image_resize' ), 5, 3 ); // Ensure this runs before Jetpack, when Photon is active
}
private function use_jetpack_photon() {
if ( defined( 'WPCOM_VIP_USE_JETPACK_PHOTON' ) && true === WPCOM_VIP_USE_JETPACK_PHOTON ) {
return true;
}
if ( isset( $_GET['jetpack-photon'] ) && 'yes' === $_GET['jetpack-photon'] ) {
return true;
}
return false;
}
function check_to_upload_file( $data, $postarr ) {
// Check if this is an import or a local image_editor->save
if ( 0 < intval( $postarr['import_id'] ) || ! $this->attachment_file_exists( $postarr['guid'] ) ) {
$url_parts = parse_url( $postarr['guid'] );
$uploads = wp_upload_dir();
if ( false !== $uploads['error'] )
return $data;
$dir_file_parts = explode( '/', $url_parts['path'] );
if ( 3 > count( $dir_file_parts ) )
return $data;
$local_file_path = implode( '/', array_splice( $dir_file_parts, count( $dir_file_parts ) - 3 ) );
$filename = constant( 'LOCAL_UPLOADS' ) . '/' . $local_file_path;
$file = array(
'file' => $filename,
'url' => $this->get_files_service_hostname() . $url_parts['path'],
'type' => $postarr['post_mime_type'],
'error' => 0,
);
$file = $this->upload_file( $file, 'attachment_import' );
// did the file get renamed due to a name clash, if so record the change
if ( basename( $url_parts['path'] ) != basename( $file['file'] ) ) {
$data['guid'] = str_replace( basename( $url_parts['path'] ),
basename( $file['file'] ), $data['guid'] );
}
}
return $data;
}
// Supports the mu-plugin WordPress Importer
// Ensures a local copy of the imported xml file is available for local reading at the final import step
function check_to_download_file() {
$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
if ( 2 !== $step )
return;
$this->id = (int) $_POST['import_id'];
$file = get_attached_file( $this->id );
if ( ! $file || file_exists( $file ) )
return;
$service_url = $this->get_files_service_hostname() . '/' . $this->get_upload_path();
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$service_url .= '/sites/' . get_current_blog_id();
}
$file_url = str_ireplace( constant( 'LOCAL_UPLOADS' ),
$service_url,
$file );
$opts = array(
'http' => array(
'method' => "GET",
'header' => 'X-Client-Site-ID: ' . FILES_CLIENT_SITE_ID . "\r\n",
)
);
$context = stream_context_create( $opts );
$file_data = file_get_contents( $file_url, false, $context );
if ( $file_data ) {
$directory = pathinfo( $file )['dirname'];
if ( ! file_exists( $directory ) )
mkdir( $directory, 0777, true );
file_put_contents( $file, $file_data );
register_shutdown_function( 'unlink', $file );
}
}
function save_image_file( $override, $filename, $image, $mime_type, $post_id ) {
$return = $image->save( $filename, $mime_type );
if ( ! $return || is_wp_error( $return ) || ! file_exists( $filename ) )
return false;
$url_parts = parse_url( $filename );
if ( false !== stripos( $url_parts['path'], constant( 'LOCAL_UPLOADS' ) ) )
$file_uri = substr( $url_parts['path'], stripos( $url_parts['path'], constant( 'LOCAL_UPLOADS' ) ) + strlen( constant( 'LOCAL_UPLOADS' ) ) );
else
$file_uri = '/' . $url_parts['path'];
$service_url = $this->get_files_service_hostname() . '/' . $this->get_upload_path();
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$service_url .= '/sites/' . get_current_blog_id();
}
$file = array(
'file' => $filename,
'url' => $service_url . $file_uri,
'type' => $mime_type,
'error' => 0,
);
$this->upload_file( $file, 'editor_save' );
return ( 0 === $file['error'] );
}
function get_upload_dir( $upload ) {
$upload['path'] = constant( 'LOCAL_UPLOADS' ) . $upload['subdir'];
$upload['basedir'] = constant( 'LOCAL_UPLOADS' );
return $upload;
}
function attachment_file_exists( $file_url ) {
$url_parts = parse_url( $file_url );
$post_url = $this->get_files_service_hostname() . $url_parts['path'];
$headers = array(
'X-Client-Site-ID: ' . constant( 'FILES_CLIENT_SITE_ID' ),
'X-Access-Token: ' . constant( 'FILES_ACCESS_TOKEN' ),
'X-Action: file_exists',
);
$ch = curl_init( $post_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_VERBOSE, true );
curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
return ( 200 == $http_code );
}
/**
* Filter's the return value of `wp_unique_filename()`
*/
public function filter_unique_filename( $filename, $ext, $dir, $unique_filename_callback ) {
// If a unique filename callback was used, let's just use its results.
// `wp_unique_filename` has fired this already so we don't need to actually call it.
if ( $unique_filename_callback && is_callable( $unique_filename_callback ) ) {
return $filename;
}
if ( '.tmp' === $ext || '/tmp/' === $dir ) {
return $filename;
}
$ext = strtolower( $ext );
$filename = $this->_sanitize_filename( $filename );
$check = $this->_check_uniqueness_with_backend( $filename );
if ( 200 == $check['http_code'] ) {
$obj = json_decode( $check['content'] );
if ( isset( $obj->filename ) && basename( $obj->filename ) != basename( $filename ) ) {
$filename = $obj->filename;
}
}
return $filename;
}
/**
* Check filetype support against Mogile
*
* Leverages Mogile backend, which will return a 406 or other non-200 code if the filetype is unsupported
*/
public function filter_filetype_check( $filetype_data, $file, $filename, $mimes ) {
$filename = $this->_sanitize_filename( $filename );
$check = $this->_check_uniqueness_with_backend( $filename );
// Setting `ext` and `type` to empty will fail the upload because Go doesn't allow unfiltered uploads
// See `_wp_handle_upload()`
if ( 200 != $check['http_code'] ) {
$filetype_data['ext'] = '';
$filetype_data['type'] = '';
$filetype_data['proper_filename'] = false; // Never set this true, which leaves filename changing to dedicated methods in this class
}
return $filetype_data;
}
/**
* Ensure consistent filename sanitization
*/
private function _sanitize_filename( $filename ) {
return sanitize_file_name( $filename );
}
/**
* Common method to return a unique filename from the VIP Go File Service using the provided filename as a starting point
*/
private function _check_uniqueness_with_backend( $filename ) {
$headers = array(
'X-Client-Site-ID: ' . constant( 'FILES_CLIENT_SITE_ID' ),
'X-Access-Token: ' . constant( 'FILES_ACCESS_TOKEN' ),
'X-Action: unique_filename',
);
if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) ) {
$file['error'] = $uploads['error'];
return $file;
}
$url_parts = parse_url( $uploads['url'] . '/' . $filename );
$file_path = $url_parts['path'];
if ( is_multisite() ) {
$sanitized_file_path = Path_Utils::trim_leading_multisite_directory( $file_path, $this->get_upload_path() );
if ( false !== $sanitized_file_path ) {
$file_path = $sanitized_file_path;
unset( $sanitized_file_path );
}
}
$post_url = $this->get_files_service_hostname() . $file_path;
$ch = curl_init( $post_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_VERBOSE, true );
$content = curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
return compact( 'http_code', 'content' );
}
function upload_file( $details, $upload_type ) {
if ( ! file_exists( $details['file'] ) ) {
$details['error'] = sprintf( __( 'The specified local upload file does not exist.' ) );
return $details;
}
if ( 'editor_save' == $upload_type ) {
$post_url = $details['url'];
} else {
$url_parts = parse_url( $details['url'] );
$file_path = $url_parts['path'];
if ( is_multisite() ) {
$sanitized_file_path = Path_Utils::trim_leading_multisite_directory( $file_path, $this->get_upload_path() );
if ( false !== $sanitized_file_path ) {
$file_path = $sanitized_file_path;
unset( $sanitized_file_path );
$details['url'] = $url_parts['scheme'] . '://' . $url_parts['host'] . $file_path;
}
}
$post_url = $this->get_files_service_hostname() . $file_path;
}
$headers = array(
'X-Client-Site-ID: ' . constant( 'FILES_CLIENT_SITE_ID' ),
'X-Access-Token: ' . constant( 'FILES_ACCESS_TOKEN' ),
'Content-Type: ' . $details['type'],
'Content-Length: ' . filesize( $details['file'] ),
'Connection: Keep-Alive',
);
$stream = fopen( $details['file'], 'r' );
$ch = curl_init( $post_url );
curl_setopt( $ch, CURLOPT_PUT, true );
curl_setopt( $ch, CURLOPT_INFILE, $stream );
curl_setopt( $ch, CURLOPT_INFILESIZE, filesize( $details['file'] ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 + (int)( filesize( $details['file'] ) / 512000 ) ); // 10 plus 1 second per 500k
curl_setopt( $ch, CURLOPT_READFUNCTION,
function( $ch, $fd, $length ) use( $stream ) {
$data = fread( $stream, $length );
if ( null == $data )
return 0;
else
return $data;
});
$ret_data = curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
fclose( $stream );
register_shutdown_function( 'unlink', $details['file'] );
switch ( $http_code ) {
case 200:
if ( 0 < strlen( $ret_data ) ) {
$obj = json_decode( $ret_data );
if ( isset( $obj->filename ) && basename( $obj->filename ) != basename( $post_url ) ) {
$uploads = wp_upload_dir();
if ( false === $uploads['error'] ) {
@copy( $details['file'], $uploads['path'] . '/' . $obj->filename );
register_shutdown_function( 'unlink', $uploads['path'] . '/' . $obj->filename );
}
$details['file'] = str_replace( basename( $post_url ), basename( $obj->filename ), $details['file'] );
}
}
break;
case 204:
$details['error'] = sprintf( __( 'You have exceeded your file space quota.' ) );
break;
default:
$details['error'] = sprintf( __( 'Error uploading the file to the remote servers: Code %d' ), $http_code );
break;
}
return $details;
}
function delete_file( $file_name ) {
// To ensure we don't needlessly fire off deletes for all sizes of the same image, of
// which all except the first result in 404's, keep accounting of what we've deleted.
static $deleted_uris = array();
$url_parts = parse_url( $file_name );
if ( false !== stripos( $url_parts['path'], constant( 'LOCAL_UPLOADS' ) ) )
$file_uri = substr( $url_parts['path'], stripos( $url_parts['path'], constant( 'LOCAL_UPLOADS' ) ) + strlen( constant( 'LOCAL_UPLOADS' ) ) );
else
$file_uri = '/' . $url_parts['path'];
$headers = array(
'X-Client-Site-ID: ' . constant( 'FILES_CLIENT_SITE_ID' ),
'X-Access-Token: ' . constant( 'FILES_ACCESS_TOKEN' ),
);
$delete_uri = $file_uri;
$service_url = $this->get_files_service_hostname() . '/' . $this->get_upload_path();
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$service_url .= '/sites/' . get_current_blog_id();
$delete_uri = '/sites/' . get_current_blog_id() . $delete_uri;
}
if ( in_array( $delete_uri, $deleted_uris ) ) {
// This file has already been successfully deleted from the file service in this request
return;
}
$ch = curl_init( $service_url . $file_uri );
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'DELETE' );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
curl_exec( $ch );
$http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
if ( 200 != $http_code ) {
trigger_error( sprintf( __( 'Error deleting the file %s from the remote servers: Code %d' ), $file_uri, $http_code ), E_USER_WARNING );
return;
}
// Set our static so we can later recall that this file has already been deleted and purged
$deleted_uris[] = $delete_uri;
// We successfully deleted the file, purge the file from the caches
$invalidation_url = get_site_url() . '/' . $this->get_upload_path();
if ( is_multisite() && ! ( is_main_network() && is_main_site() ) ) {
$invalidation_url .= '/sites/' . get_current_blog_id();
}
$invalidation_url .= $file_uri;
$this->purge_file_cache( $invalidation_url, 'PURGE' );
}
private function purge_file_cache( $url, $method ) {
global $file_cache_servers;
$parsed = parse_url( $url );
if ( empty( $parsed['host'] ) ) {
return $requests;
}
$uri = '/';
if ( isset( $parsed['path'] ) ) {
$uri = $parsed['path'];
}
if ( isset( $parsed['query'] ) ) {
$uri .= $parsed['query'];
}
$requests = array();
if ( defined( 'PURGE_SERVER_TYPE' ) && 'mangle' == PURGE_SERVER_TYPE ) {
$data = array(
'group' => 'vip-go',
'scope' => 'global',
'type' => $method,
'uri' => $parsed['host'] . $uri,
'cb' => 'nil',
);
$json = json_encode( $data );
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, constant( 'PURGE_SERVER_URL' ) );
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $json );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen( $json )
) );
curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
curl_exec( $curl );
$http_code = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
curl_close( $curl );
if ( 200 != $http_code ) {
trigger_error( sprintf( __( 'Error purging %s from the cache service: Code %d' ), $url, $http_code ), E_USER_WARNING );
}
return;
}
if ( ! isset( $file_cache_servers ) || empty( $file_cache_servers ) ) {
trigger_error( sprintf( __( 'Error purging the file cache for %s: There are no file cache servers defined.' ), $url ), E_USER_WARNING );
return $requests;
}
foreach ( $file_cache_servers as $server ) {
$server = explode( ':', $server[0] );
$requests[] = array(
'ip' => $server[0],
'port' => $server[1],
'host' => $parsed['host'],
'uri' => $uri,
'method' => $method,
);
}
$this->purge_cache_servers( $requests );
}
private function purge_cache_servers( $requests ) {
$curl_multi = curl_multi_init();
foreach ( $requests as $req ) {
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, "http://{$req['ip']}{$req['uri']}" );
curl_setopt( $curl, CURLOPT_PORT, $req['port'] );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Host: {$req['host']}" ) );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $req['method'] );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
curl_multi_add_handle( $curl_multi, $curl );
}
$running = true;
while ( $running ) {
do {
$result = curl_multi_exec( $curl_multi, $running );
} while ( $result == CURLM_CALL_MULTI_PERFORM );
if ( $result != CURLM_OK )
error_log( 'curl_multi_exec() returned something different than CURLM_OK' );
curl_multi_select( $curl_multi, 0.2 );
}
while ( $completed = curl_multi_info_read( $curl_multi ) ) {
$info = curl_getinfo( $completed['handle'] );
if ( ! $info['http_code'] && curl_error( $completed['handle'] ) )
error_log( 'Error on: ' . $info['url'] . ' error: ' . curl_error( $completed['handle'] ) . "\n" );
if ( '200' != $info['http_code'] )
error_log( 'Request to ' . $info['url'] . ' returned HTTP code ' . $info['http_code'] . "\n" );
curl_multi_remove_handle( $curl_multi, $completed['handle'] );
}
curl_multi_close( $curl_multi );
}
private function get_upload_path() {
$upload_path = trim( get_option( 'upload_path' ) );
if ( empty( $upload_path ) )
return 'wp-content/uploads';
else
return $upload_path;
}
function get_files_service_hostname() {
return 'https://' . FILE_SERVICE_ENDPOINT;
}
public function upload_url_path( $upload_url_path, $option ) {
// No modifications needed outside multisite
if ( false === is_multisite() ) {
return $upload_url_path;
}
// Change the upload url path to site's URL + wp-content/uploads without trailing slash
// Related core code: https://core.trac.wordpress.org/browser/tags/4.6.1/src/wp-includes/functions.php#L1929
$upload_url_path = untrailingslashit( get_site_url( null, 'wp-content/uploads' ) );
return $upload_url_path;
}
/**
* Image resizing service. Takes place of image_downsize().
*
* @param bool $ignore Unused.
* @param int $id Attachment ID for image.
* @param array|string $size Optional, default is 'medium'. Size of image, either array or string.
* @return bool|array False on failure, array on success.
* @see image_downsize()
*/
function image_resize( $ignore, $id, $size ) {
global $_wp_additional_image_sizes, $post;
// Don't bother resizing non-image (and non-existent) attachment.
// We fallthrough to core's image_downsize but that bails as well.
$is_img = wp_attachment_is_image( $id );
if ( ! $is_img ) {
return false;
}
$content_width = isset( $GLOBALS['content_width'] ) ? $GLOBALS['content_width'] : null;
$crop = false;
$args = array();
// For resize requests coming from an image's attachment page, override
// the supplied $size and use the user-defined $content_width if the
// theme-defined $content_width has been manually passed in.
if ( is_attachment() && $id === $post->ID ) {
if ( is_array( $size )
&& ! empty ( $size )
&& isset( $GLOBALS['content_width'] )
&& $size[0] == $GLOBALS['content_width'] ) {
$size = array( $content_width, $content_width );
}
}
if ( 'tellyworth' == $size ) { // 'full' is reserved because some themes use it (see image_constrain_size_for_editor)
$_max_w = 4096;
$_max_h = 4096;
} elseif ( 'thumbnail' == $size ) {
$_max_w = get_option( 'thumbnail_size_w' );
$_max_h = get_option( 'thumbnail_size_h' );
if ( !$_max_w && !$_max_h ) {
$_max_w = 128;
$_max_h = 96;
}
if ( get_option( 'thumbnail_crop' ) )
$crop = true;
} elseif ( 'medium' == $size ) {
$_max_w = get_option( 'medium_size_w' );
$_max_h = get_option( 'medium_size_h' );
if ( !$_max_w && !$_max_h ) {
$_max_w = 300;
$_max_h = 300;
}
} elseif ( 'large' == $size ) {
$_max_w = get_option( 'large_size_w' );
$_max_h = get_option( 'large_size_h' );
} elseif ( is_array( $size ) ) {
$_max_w = $w = $size[0];
$_max_h = $h = $size[1];
} elseif ( ! empty( $_wp_additional_image_sizes[$size] ) ) {
$_max_w = $w = $_wp_additional_image_sizes[$size]['width'];
$_max_h = $h = $_wp_additional_image_sizes[$size]['height'];
$crop = $_wp_additional_image_sizes[$size]['crop'];
} elseif ( $content_width > 0 ) {
$_max_w = $content_width;
$_max_h = 0;
} else {
$_max_w = 1024;
$_max_h = 0;
}
// Constrain default image sizes to the theme's content width, if available.
if ( $content_width > 0 && in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) )
$_max_w = min( $_max_w, $content_width );
$resized = false;
$img_url = wp_get_attachment_url( $id );
/**
* Filter the original image Photon-compatible parameters before changes are
*
* @param array|string $args Array of Photon-compatible arguments.
* @param string $img_url Image URL.
*/
$args = apply_filters( 'vip_go_image_resize_pre_args', $args, $img_url );
if ( ! $crop ) {
$imagedata = wp_get_attachment_metadata( $id );
if ( ! empty( $imagedata['width'] ) || ! empty( $imagedata['height'] ) ) {
$h = $imagedata['height'];
$w = $imagedata['width'];
list ($w, $h) = wp_constrain_dimensions( $w, $h, $_max_w, $_max_h );
if ( $w < $imagedata['width'] || $h < $imagedata['height'] )
$resized = true;
} else {
$w = $_max_w;
$h = $_max_h;
}
}
if ( $crop ) {
$constrain = false;
$imagedata = wp_get_attachment_metadata( $id );
if ( $imagedata ) {
$w = $imagedata['width'];
$h = $imagedata['height'];
}
if ( empty( $w ) )
$w = $_max_w;
if ( empty( $h ) )
$h = $_max_h;
// If the image width is bigger than the allowed max, scale it to match
if ( $w >= $_max_w )
$w = $_max_w;
else
$constrain = true;
// If the image height is bigger than the allowed max, scale it to match
if ( $h >= $_max_h )
$h = $_max_h;
else
$constrain = true;
if ( $constrain )
list( $w, $h ) = wp_constrain_dimensions( $w, $h, $_max_w, $_max_h );
$args['w'] = $w;
$args['h'] = $h;
$args['crop'] = '1';
$resized = true;
}
// we want users to be able to resize full size images with tinymce.
// the image_add_wh() filter will add the ?w= query string at display time.
elseif ( 'full' != $size ) {
$args['w'] = $w;
$resized = true;
}
if ( is_array( $args ) ) {
// Convert values that are arrays into strings
foreach ( $args as $arg => $value ) {
if ( is_array( $value ) ) {
$args[ $arg ] = implode( ',', $value );
}
}
// Encode values
// See http://core.trac.wordpress.org/ticket/17923
$args = rawurlencode_deep( $args );
}
$img_url = add_query_arg( $args, $img_url );
return array( $img_url, $w, $h, $resized );
}
/**
* Filter `cron_schedules` output
*
* Add a custom schedule for a 5 minute interval
*
* @param array $schedule
*
* @return mixed
*/
public function filter_cron_schedules( $schedule ) {
if ( isset( $schedule[ 'vip_five_minutes' ] ) ) {
return $schedule;
}
// Not actually five minutes; we want it to run faster though to get through everything.
$schedule['vip_five_minutes'] = [
'interval' => 180,
'display' => __( 'Once every 3 minutes, unlike what the slug says. Originally used to be 5 mins.' ),
];
return $schedule;
}
public function schedule_update_job() {
if ( get_option( self::OPT_ALL_FILESIZE_PROCESSED ) ) {
if ( wp_next_scheduled( self::CRON_EVENT_NAME ) ) {
wp_clear_scheduled_hook( self::CRON_EVENT_NAME );
}
return;
}
if (! wp_next_scheduled ( self::CRON_EVENT_NAME )) {
wp_schedule_event(time(), 'vip_five_minutes', self::CRON_EVENT_NAME );
}
add_action( self::CRON_EVENT_NAME, [ $this, 'update_attachment_meta' ] );
}
/**
* Cron job to update attachment metadata with file size
*/
public function update_attachment_meta() {
wpcom_vip_irc(
'#vip-go-filesize-updates',
sprintf( 'Starting %s on %s... $vip-go-streams-debug',
self::CRON_EVENT_NAME,
home_url() ),
5 );
if ( get_option( self::OPT_ALL_FILESIZE_PROCESSED ) ) {
// already done. Nothing to update
wpcom_vip_irc(
'#vip-go-filesize-updates',
sprintf( 'Already completed updates on %s. Exiting %s... $vip-go-streams-debug',
home_url(),
self::CRON_EVENT_NAME ),
5 );
return;
}
$batch_size = 3000;
if ( defined( 'VIP_FILESYSTEM_FILESIZE_UPDATE_BATCH_SIZE' ) ) {
$batch_size = (int) VIP_FILESYSTEM_FILESIZE_UPDATE_BATCH_SIZE;
}
$updater = new Meta_Updater( $batch_size );
$max_id = (int) get_option( self::OPT_MAX_POST_ID );
if ( ! $max_id ) {
$max_id = $updater->get_max_id();
update_option( self::OPT_MAX_POST_ID, $max_id, false );
}
$num_lookups = 0;
$max_lookups = 10;
$orig_start_index = $start_index = get_option( self::OPT_NEXT_FILESIZE_INDEX, 0 );
$end_index = $start_index + $batch_size;
do {
if ( $start_index > $max_id ) {
// This means all attachments have been processed so marking as done
update_option( self::OPT_ALL_FILESIZE_PROCESSED, 1 );
wpcom_vip_irc(
'#vip-go-filesize-updates',
sprintf( 'Passed max ID (%d) on %s. Exiting %s... $vip-go-streams-debug',
$max_id,
home_url(),
self::CRON_EVENT_NAME
),
5
);
return;
}
$attachments = $updater->get_attachments( $start_index, $end_index );
// Bump the next index in case the cron job dies before we've processed everything
update_option( self::OPT_NEXT_FILESIZE_INDEX, $start_index, false );
$start_index = $end_index + 1;
$end_index = $start_index + $batch_size;
// Avoid infinite loops
$num_lookups++;
if ( $num_lookups >= $max_lookups ) {
break;
}
} while ( empty( $attachments ) );
if ( $attachments ) {
$counts = $updater->update_attachments( $attachments );
}
// All done, update next index option
wpcom_vip_irc(
'#vip-go-filesize-updates',
sprintf( 'Batch %d to %d (of %d) completed on %s. Processed %d attachments (%s) $vip-go-streams-debug',
$orig_start_index, $start_index, $max_id, home_url(), count( $attachments ), json_encode( $counts ) ),
5
);
update_option( self::OPT_NEXT_FILESIZE_INDEX, $start_index, false );
}
}
class A8C_Files_Utils {