-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtuxedo_big_file_uploads.php
1439 lines (1259 loc) · 45.7 KB
/
tuxedo_big_file_uploads.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: Big File Uploads
* Description: Enable large file uploads in the built-in WordPress media uploader via multipart uploads, and set maximum upload file size to any value based on user role. Uploads can be as large as available disk space allows.
* Version: 2.1.6
* Author: Infinite Uploads
* Author URI: https://infiniteuploads.com/?utm_source=bfu_plugin&utm_medium=plugin&utm_campaign=bfu_plugin&utm_content=meta
* Network: true
* License: GPLv2 or later
* Domain Path: /languages
* Text Domain: tuxedo-big-file-uploads
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright 2021-2025 ClikIT, LLC
*
* @package BigFileUploads
* @version 2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
die();
}
define( 'BIG_FILE_UPLOADS_VERSION', '2.1.4' );
/**
* Big File Uploads manager class.
*
* Bootstraps the plugin by hooking into plupload defaults and
* media settings.
*
* @since 1.0.0
*/
class BigFileUploads {
/**
* BigFileUploads instance.
*
* @since 1.0.0
* @access private
* @static
* @var BigFileUploads
*/
private static $instance = false;
/**
* The API server.
*
* @var string (URL)
*/
public $server_root = 'https://infiniteuploads.com/';
protected $capability;
protected $max_upload_size;
public $ajax_timelimit = 20;
/**
* Get the instance.
*
* Returns the current instance, creates one if it
* doesn't exist. Ensures only one instance of
* BigFileUploads is loaded or can be loaded.
*
* @return BigFileUploads
* @since 1.0.0
* @static
*
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*
* Initializes and adds functions to filter and action hooks.
*
* @since 1.0.0
*/
public function __construct() {
//save default before we filter it
$this->max_upload_size = wp_max_upload_size();
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
add_action( 'admin_notices', array( $this, 'init_review_notice' ) );
add_filter( 'plupload_init', array( $this, 'filter_plupload_settings' ) );
add_filter( 'upload_post_params', array( $this, 'filter_plupload_params' ) );
add_filter( 'plupload_default_settings', array( $this, 'filter_plupload_settings' ) );
add_filter( 'plupload_default_params', array( $this, 'filter_plupload_params' ) );
add_filter( 'upload_size_limit', array( $this, 'filter_upload_size_limit' ) );
//add_filter( 'ext2type', array( $this, 'filter_ext_types' ) );
add_action( 'wp_ajax_bfu_chunker', array( $this, 'ajax_chunk_receiver' ) );
add_action( 'post-upload-ui', array( $this, 'upload_output' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'gutenberg_notice' ) );
add_filter( 'block_editor_settings_all', array( $this, 'gutenberg_size_filter' ) );
//single site
add_action( 'admin_menu', [ &$this, 'admin_menu' ] );
add_filter( 'plugin_action_links_tuxedo-big-file-uploads/tuxedo_big_file_uploads.php', [ &$this, 'plugins_list_links' ] );
//multisite
add_action( 'network_admin_menu', [ &$this, 'admin_menu' ] );
add_filter( 'network_admin_plugin_action_links_tuxedo-big-file-uploads/tuxedo_big_file_uploads.php', [ &$this, 'plugins_list_links' ] );
if ( is_main_site() ) {
add_action( 'wp_ajax_bfu_file_scan', [ &$this, 'ajax_file_scan' ] );
add_action( 'wp_ajax_bfu_upload_dismiss', [ &$this, 'ajax_upload_dismiss' ] );
add_action( 'wp_ajax_bfu_upgrade_dismiss', [ &$this, 'ajax_upgrade_dismiss' ] );
add_action( 'wp_ajax_bfu_subscribe_dismiss', [ &$this, 'ajax_subscribe_dismiss' ] );
}
if ( is_multisite() ) {
add_action( 'network_admin_notices', [ &$this, 'upgrade_notice' ] );
} else {
add_action( 'admin_notices', [ &$this, 'upgrade_notice' ] );
}
require_once dirname( __FILE__ ) . '/classes/class-file-scan.php';
/**
* Filters the capability that is checked for access to Big File Uploads settings page.
*
* @param {string} $capability The capability checked for access and editing settings. Default `manage_network_options` or `manage_options` depending on if multisite.
*
* @return {string} $capability The capability checked for access and editing settings.
* @since 1.0
* @hook big_file_uploads_settings_capability
*
*/
$this->capability = apply_filters( 'big_file_uploads_settings_capability', ( is_multisite() ? 'manage_network_options' : 'manage_options' ) );
}
/**
* Filter plupload params.
*
* @since 1.2.0
*/
public function filter_plupload_params( $plupload_params ) {
$plupload_params['action'] = 'bfu_chunker';
return $plupload_params;
}
/**
* Filter plupload settings.
*
* @since 1.0.0
*/
public function filter_plupload_settings( $plupload_settings ) {
$max_chunk = ( MB_IN_BYTES * 20 ); //20MB max chunk size (to avoid timeouts)
if ( $max_chunk > $this->max_upload_size ) {
$default_chunk = ( $this->max_upload_size * 0.8 ) / KB_IN_BYTES;
} else {
$default_chunk = $max_chunk / KB_IN_BYTES;
}
if ( ! defined( 'BIG_FILE_UPLOADS_CHUNK_SIZE_KB' ) ) {
define( 'BIG_FILE_UPLOADS_CHUNK_SIZE_KB', $default_chunk );
}
if ( ! defined( 'BIG_FILE_UPLOADS_RETRIES' ) ) {
define( 'BIG_FILE_UPLOADS_RETRIES', 1 );
}
$plupload_settings['url'] = admin_url( 'admin-ajax.php' );
$plupload_settings['filters']['max_file_size'] = $this->filter_upload_size_limit( '' ) . 'b';
$plupload_settings['chunk_size'] = BIG_FILE_UPLOADS_CHUNK_SIZE_KB . 'kb';
$plupload_settings['max_retries'] = BIG_FILE_UPLOADS_RETRIES;
return $plupload_settings;
}
/**
* Load Localization files.
*
* @since 1.0.0
*/
public function load_textdomain() {
$domain = 'tuxedo-big-file-uploads';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Load Localization files.
*
* @since 1.0.0
*/
public function init_review_notice() {
require_once dirname( __FILE__ ) . '/classes/class-review-notice.php';
// Setup notice.
$notice = Big_File_Uploads_Review_Notice::get(
'tuxedo-big-file-uploads', // Plugin slug on wp.org (eg: hello-dolly).
__( 'Big File Uploads', 'tuxedo-big-file-uploads' ), // Plugin name (eg: Hello Dolly).
array(
'days' => 14,
'screens' => [ 'plugins', 'settings_page_big_file_uploads', 'upload' ],
'cap' => 'install_plugins',
'domain' => 'tuxedo-big-file-uploads',
'prefix' => 'bfu'
) // Notice options.
);
// Render notice.
$notice->render();
}
/**
* Return max upload size.
*
* Free space of temp directory.
*
* @since 1.0.0
*
* @return int $bytes Free disk space in bytes.
*/
public function filter_upload_size_limit( $unused ) {
return $this->get_upload_limit();
}
/**
* Free space of temp directory.
*
* @since 2.0
*
* @return false|int $bytes Free disk space in bytes.
*/
public function temp_available_size() {
if ( function_exists( 'disk_free_space' ) ) {
$bytes = disk_free_space( sys_get_temp_dir() );
} else {
$bytes = false;
}
return $bytes;
}
/**
* Add the js to Gutenberg to add our custom upload size notice.
*
* @return void
*/
function gutenberg_notice() {
wp_enqueue_script(
'bfu-block-upload-notice',
plugin_dir_url( __FILE__ ) . 'assets/js/block-notice.js',
[ 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ],
BIG_FILE_UPLOADS_VERSION
);
wp_set_script_translations( 'bfu-block-upload-notice', 'tuxedo-big-file-uploads' );
}
/**
* Always pass the original size limit to Gutenberg so it can show our error (BFU only works inside media library via plupload).
*
* @param $editor_settings
*
* @return mixed
*/
function gutenberg_size_filter( $editor_settings ) {
$editor_settings['maxUploadFileSize'] = $this->max_upload_size;
return $editor_settings;
}
/**
* Enqueue html on the upload form.
*
* @since 2.0
*/
public function upload_output() {
global $pagenow;
if ( ! current_user_can( $this->capability ) || is_null( $pagenow ) || ! in_array( $pagenow, array( 'post-new.php', 'post.php', 'upload.php', 'media-new.php' ) ) ) {
return;
}
?>
<script type="text/javascript">
//When each chunk is uploaded, check if there were any errors or not and stop the rest
jQuery(function() {
if ( typeof uploader !== 'undefined' ) {
uploader.bind('ChunkUploaded', function (up, file, response) {
//Stop the upload!
if (response.status === 202) {
up.removeFile(file);
uploadSuccess(file, response.response);
}
});
}
});
jQuery(".max-upload-size").append(' <small><a style="text-decoration:none;" href="<?php echo esc_url( $this->settings_url() ); ?>"><?php esc_html_e( 'Change', 'tuxedo-big-file-uploads' ); ?></a></small>');
<?php
$dismissed = get_user_option( 'bfu_notice_dismissed', get_current_user_id() );
if ( ! class_exists( 'Infinite_Uploads' ) && ! $dismissed ) {
?>
(function ($) {
'use strict';
$(".max-upload-size").after('<span class="bfu-upload-notice"><small><?php esc_html_e( 'Want unlimited storage space?', 'tuxedo-big-file-uploads' ); ?> <a href="<?php echo esc_url( $this->settings_url() ); ?>#upgrade-modal"><?php esc_html_e( 'Move your media files to the Infinite Uploads cloud', 'tuxedo-big-file-uploads' ); ?>.</a></small><a style="width:12px;height:12px;font-size:12px;vertical-align:middle;" class="dashicons dashicons-no" title="<?php esc_attr_e( 'Dismiss', 'tuxedo-big-file-uploads' ); ?>" href="#"><span class="screen-reader-text"><?php esc_html_e( 'Dismiss', 'tuxedo-big-file-uploads' ); ?></span></a></span>');
$(function () {
var $notice = $('.bfu-upload-notice');
$notice.children('a.dashicons').on('click', function (event, el) {
$.get(ajaxurl + '?action=bfu_upload_dismiss');
$notice.hide();
});
});
})(jQuery);
<?php
}
?>
</script>
<?php
}
/**
* Output one time upgrade notice to previous users.
*
* @since 2.0
*/
public function upgrade_notice() {
if ( ! current_user_can( $this->capability ) ) {
return;
}
$old_max_upload_size = get_site_option( 'tuxbfu_max_upload_size' );
if ( $old_max_upload_size !== false ) {
$dismissed = get_user_option( 'bfu_upgrade_notice_dismissed', get_current_user_id() );
if ( ! $dismissed ) {
?>
<script>
(function ($) {
'use strict';
$(function () {
$('.bfu-upgrade-notice').on('click', '.notice-dismiss', function (event, el) {
$.get(ajaxurl + '?action=bfu_upgrade_dismiss');
});
});
})(jQuery);
</script>
<div class="bfu-upgrade-notice notice notice-info is-dismissible">
<p><?php _e( 'Tuxedo Big File Uploads has a new maintainer and <strong>new free features</strong>!', 'tuxedo-big-file-uploads' ); ?> <a
href="<?php echo esc_url( $this->settings_url() ); ?>"><?php esc_html_e( 'Review your upload settings now, configure different limits per role, or analyze your uploads storage usage.', 'tuxedo-big-file-uploads' ); ?></a></p>
</div>
<?php
}
}
}
/**
* AJAX endpoint to dismiss upload page notice.
*
* @since 2.0
*/
public function ajax_upload_dismiss() {
if ( ! current_user_can( $this->capability ) ) {
wp_send_json_error();
}
update_user_option( get_current_user_id(), 'bfu_notice_dismissed', 1 );
wp_send_json_success();
}
/**
* AJAX endpoint to dismiss upgrade notice.
*
* @since 2.0
*/
public function ajax_upgrade_dismiss() {
if ( ! current_user_can( $this->capability ) ) {
wp_send_json_error();
}
update_user_option( get_current_user_id(), 'bfu_upgrade_notice_dismissed', 1 );
wp_send_json_success();
}
/**
* AJAX endpoint to dismiss subscribe notice.
*
* @since 2.0
*/
public function ajax_subscribe_dismiss() {
if ( ! current_user_can( $this->capability ) ) {
wp_send_json_error();
}
update_user_option( get_current_user_id(), 'bfu_subscribe_notice_dismissed', 1 );
wp_send_json_success();
}
/**
* AJAX chunk receiver.
*
* Ajax callback for plupload to handle chunked uploads.
* Based on code by Davit Barbakadze
* https://gist.github.com/jayarjo/5846636
*
* Mirrors /wp-admin/async-upload.php
*
* @todo Figure out a way to stop furthur chunks from uploading when there is an error in gutenberg
*
* @since 1.2.0
*/
public function ajax_chunk_receiver() {
/** Check that we have an upload and there are no errors. */
if ( empty( $_FILES ) || $_FILES['async-upload']['error'] ) {
/** Failed to move uploaded file. */
die();
}
/** Authenticate user. */
if ( ! is_user_logged_in() || ! current_user_can( 'upload_files' ) ) {
wp_die( __( 'Sorry, you are not allowed to upload files.' ) );
}
check_admin_referer( 'media-form' );
/** Check and get file chunks. */
$chunk = isset( $_REQUEST['chunk'] ) ? intval( $_REQUEST['chunk'] ) : 0; //zero index
$current_part = $chunk + 1;
$chunks = isset( $_REQUEST['chunks'] ) ? intval( $_REQUEST['chunks'] ) : 0;
/** Get file name and path + name. */
$fileName = isset( $_REQUEST['name'] ) ? $_REQUEST['name'] : $_FILES['async-upload']['name'];
$bfu_temp_dir = apply_filters( 'bfu_temp_dir', WP_CONTENT_DIR . '/bfu-temp' );
//only run on first chunk
if ( $chunk === 0 ) {
// Create temp directory if it doesn't exist
if ( ! @is_dir( $bfu_temp_dir ) ) {
wp_mkdir_p( $bfu_temp_dir );
}
// Protect temp directory from browsing.
$index_pathname = $bfu_temp_dir . '/index.php';
if ( ! file_exists( $index_pathname ) ) {
$file = fopen( $index_pathname, 'w' );
if ( false !== $file ) {
fwrite( $file, "<?php\n// Silence is golden.\n" );
fclose( $file );
}
}
//scan temp dir for files older than 24 hours and delete them.
$files = glob( $bfu_temp_dir . '/*.part' );
if ( is_array( $files ) ) {
foreach ( $files as $file ) {
if ( @filemtime( $file ) < time() - DAY_IN_SECONDS ) {
@unlink( $file );
}
}
}
}
$filePath = sprintf( '%s/%d-%s.part', $bfu_temp_dir, get_current_blog_id(), sha1( $fileName ) );
//debugging
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$size = file_exists( $filePath ) ? size_format( filesize( $filePath ), 3 ) : '0 B';
error_log( "BFU: Processing \"$fileName\" part $current_part of $chunks as $filePath. $size processed so far." );
}
$tuxbfu_max_upload_size = $this->get_upload_limit();
if ( file_exists( $filePath ) && filesize( $filePath ) + filesize( $_FILES['async-upload']['tmp_name'] ) > $tuxbfu_max_upload_size ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "BFU: File size limit exceeded." );
}
if ( ! $chunks || $chunk == $chunks - 1 ) {
@unlink( $filePath );
if ( ! isset( $_REQUEST['short'] ) || ! isset( $_REQUEST['type'] ) ) {
echo wp_json_encode( array(
'success' => false,
'data' => array(
'message' => __( 'The file size has exceeded the maximum file size setting.', 'tuxedo-big-file-uploads' ),
'filename' => $fileName,
),
) );
wp_die();
} else {
status_header( 202 );
printf(
'<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
sprintf(
'<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
__( 'Dismiss' )
),
sprintf(
/* translators: %s: Name of the file that failed to upload. */
__( '“%s” has failed to upload.' ),
esc_html( $fileName )
),
__( 'The file size has exceeded the maximum file size setting.', 'tuxedo-big-file-uploads' )
);
exit;
}
}
die();
}
/** Open temp file. */
if ( $chunk == 0 ) {
$out = @fopen( $filePath, 'wb');
} elseif ( is_writable( $filePath ) ) { //
$out = @fopen( $filePath, 'ab' );
} else {
$out = false;
}
if ( $out ) {
/** Read binary input stream and append it to temp file. */
$in = @fopen( $_FILES['async-upload']['tmp_name'], 'rb' );
if ( $in ) {
while ( $buff = fread( $in, 4096 ) ) {
fwrite( $out, $buff );
}
} else {
/** Failed to open input stream. */
/** Attempt to clean up unfinished output. */
@fclose( $out );
@unlink( $filePath );
error_log( "BFU: Error reading uploaded part $current_part of $chunks." );
if ( ! isset( $_REQUEST['short'] ) || ! isset( $_REQUEST['type'] ) ) {
echo wp_json_encode(
array(
'success' => false,
'data' => array(
'message' => sprintf( __( 'There was an error reading uploaded part %d of %d.', 'tuxedo-big-file-uploads' ), $current_part, $chunks ),
'filename' => esc_html( $fileName ),
),
)
);
wp_die();
} else {
status_header( 202 );
printf(
'<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
sprintf(
'<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
__( 'Dismiss' )
),
sprintf(
/* translators: %s: Name of the file that failed to upload. */
__( '“%s” has failed to upload.' ),
esc_html( $fileName )
),
sprintf( __( 'There was an error reading uploaded part %d of %d.', 'tuxedo-big-file-uploads' ), $current_part, $chunks )
);
exit;
}
}
@fclose( $in );
@fclose( $out );
@unlink( $_FILES['async-upload']['tmp_name'] );
} else {
/** Failed to open output stream. */
error_log( "BFU: Failed to open output stream $filePath to write part $current_part of $chunks." );
if ( ! isset( $_REQUEST['short'] ) || ! isset( $_REQUEST['type'] ) ) {
echo wp_json_encode(
array(
'success' => false,
'data' => array(
'message' => __( 'There was an error opening the temp file for writing. Available temp directory space may be exceeded or the temp file was cleaned up before the upload completed.', 'tuxedo-big-file-uploads' ),
'filename' => esc_html( $fileName ),
),
)
);
wp_die();
} else {
status_header( 202 );
printf(
'<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
sprintf(
'<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
__( 'Dismiss' )
),
sprintf(
/* translators: %s: Name of the file that failed to upload. */
__( '“%s” has failed to upload.' ),
esc_html( $fileName )
),
__( 'There was an error opening the temp file for writing. Available temp directory space may be exceeded or the temp file was cleaned up before the upload completed.', 'tuxedo-big-file-uploads' )
);
exit;
}
}
/** Check if file has finished uploading all parts. */
if ( ! $chunks || $chunk == $chunks - 1 ) {
//debugging
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$size = file_exists( $filePath ) ? size_format( filesize( $filePath ), 3 ) : '0 B';
error_log( "BFU: Completing \"$fileName\" upload with a $size final size." );
}
/** Recreate upload in $_FILES global and pass off to WordPress. */
$_FILES['async-upload']['tmp_name'] = $filePath;
$_FILES['async-upload']['name'] = $fileName;
$_FILES['async-upload']['size'] = filesize( $_FILES['async-upload']['tmp_name'] );
$wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] );
$_FILES['async-upload']['type'] = $wp_filetype['type'];
header( 'Content-Type: text/plain; charset=' . get_option( 'blog_charset' ) );
if ( ! isset( $_REQUEST['short'] ) || ! isset( $_REQUEST['type'] ) ) { //ajax like media uploader in modal
// Compatibility with Easy Digital Downloads plugin.
if ( function_exists( 'edd_change_downloads_upload_dir' ) ) {
global $pagenow;
$pagenow = 'async-upload.php';
edd_change_downloads_upload_dir();
}
send_nosniff_header();
nocache_headers();
$this->wp_ajax_upload_attachment();
die( '0' );
} else { //non-ajax like add new media page
$post_id = 0;
if ( isset( $_REQUEST['post_id'] ) ) {
$post_id = absint( $_REQUEST['post_id'] );
if ( ! get_post( $post_id ) || ! current_user_can( 'edit_post', $post_id ) )
$post_id = 0;
}
$id = media_handle_upload( 'async-upload', $post_id, [], [ 'action' => 'wp_handle_sideload', 'test_form' => false ] );
if ( is_wp_error( $id ) ) {
printf(
'<div class="error-div error">%s <strong>%s</strong><br />%s</div>',
sprintf(
'<button type="button" class="dismiss button-link" onclick="jQuery(this).parents(\'div.media-item\').slideUp(200, function(){jQuery(this).remove();});">%s</button>',
__( 'Dismiss' )
),
sprintf(
/* translators: %s: Name of the file that failed to upload. */
__( '“%s” has failed to upload.' ),
esc_html( $_FILES['async-upload']['name'] )
),
esc_html( $id->get_error_message() )
);
exit;
}
if ( $_REQUEST['short'] ) {
// Short form response - attachment ID only.
echo $id;
} else {
// Long form response - big chunk of HTML.
$type = $_REQUEST['type'];
/**
* Filters the returned ID of an uploaded attachment.
*
* The dynamic portion of the hook name, `$type`, refers to the attachment type.
*
* Possible hook names include:
*
* - `async_upload_audio`
* - `async_upload_file`
* - `async_upload_image`
* - `async_upload_video`
*
* @since 2.5.0
*
* @param int $id Uploaded attachment ID.
*/
echo apply_filters( "async_upload_{$type}", $id );
}
}
}
die();
}
/**
* Copied from wp-admin/includes/ajax-actions.php because we have to override the args for
* the media_handle_upload function. As of WP 6.0.1
*/
function wp_ajax_upload_attachment() {
check_ajax_referer( 'media-form' );
/*
* This function does not use wp_send_json_success() / wp_send_json_error()
* as the html4 Plupload handler requires a text/html content-type for older IE.
* See https://core.trac.wordpress.org/ticket/31037
*/
if ( ! current_user_can( 'upload_files' ) ) {
echo wp_json_encode(
array(
'success' => false,
'data' => array(
'message' => __( 'Sorry, you are not allowed to upload files.' ),
'filename' => esc_html( $_FILES['async-upload']['name'] ),
),
)
);
wp_die();
}
if ( isset( $_REQUEST['post_id'] ) ) {
$post_id = $_REQUEST['post_id'];
if ( ! current_user_can( 'edit_post', $post_id ) ) {
echo wp_json_encode(
array(
'success' => false,
'data' => array(
'message' => __( 'Sorry, you are not allowed to attach files to this post.' ),
'filename' => esc_html( $_FILES['async-upload']['name'] ),
),
)
);
wp_die();
}
} else {
$post_id = null;
}
$post_data = ! empty( $_REQUEST['post_data'] ) ? _wp_get_allowed_postdata( _wp_translate_postdata( false, (array) $_REQUEST['post_data'] ) ) : array();
if ( is_wp_error( $post_data ) ) {
wp_die( $post_data->get_error_message() );
}
// If the context is custom header or background, make sure the uploaded file is an image.
if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ), true ) ) {
$wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] );
if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
echo wp_json_encode(
array(
'success' => false,
'data' => array(
'message' => __( 'The uploaded file is not a valid image. Please try again.' ),
'filename' => esc_html( $_FILES['async-upload']['name'] ),
),
)
);
wp_die();
}
}
//this is the modded function from wp-admin/includes/ajax-actions.php
$attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data, [ 'action' => 'wp_handle_sideload', 'test_form' => false ] );
if ( is_wp_error( $attachment_id ) ) {
echo wp_json_encode(
array(
'success' => false,
'data' => array(
'message' => $attachment_id->get_error_message(),
'filename' => esc_html( $_FILES['async-upload']['name'] ),
),
)
);
wp_die();
}
if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) {
if ( 'custom-background' === $post_data['context'] ) {
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] );
}
if ( 'custom-header' === $post_data['context'] ) {
update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] );
}
}
$attachment = wp_prepare_attachment_for_js( $attachment_id );
if ( ! $attachment ) {
wp_die();
}
echo wp_json_encode(
array(
'success' => true,
'data' => $attachment,
)
);
wp_die();
}
/**
* Return the maximum upload limit in bytes for the current user.
*
* @since 2.0
*
* @return integer
*/
function get_upload_limit() {
$settings = $this->get_settings();
if ( $settings['by_role'] && is_user_logged_in() ) {
$limit = 0;
$user = wp_get_current_user();
foreach ( (array) $user->roles as $role ) {
if ( isset( $settings['limits'][ $role ]['bytes'] ) && $settings['limits'][ $role ]['bytes'] > $limit ) { //choose the highest limit for the roles they have.
$limit = $settings['limits'][ $role ]['bytes'];
}
}
if ( $limit ) {
return $limit;
} else {
return $settings['limits']['all']['bytes'];
}
} else {
return $settings['limits']['all']['bytes'];
}
}
/**
* Return a cleaned up settings array with defaults if needed.
*
* @since 2.0
*
* @param false $format
*
* @return array
*/
function get_settings( $format = false ) {
$settings = get_site_option( 'tuxbfu_settings' );
if ( ! is_array( $settings ) ) {
$settings = [];
}
if ( ! isset( $settings['by_role'] ) ) {
$settings['by_role'] = false;
}
if ( ! isset( $settings['limits']['all']['bytes'] ) ) {
$old_max_upload_size = get_site_option( 'tuxbfu_max_upload_size' );
if ( $old_max_upload_size ) {
$settings['limits']['all']['bytes'] = $old_max_upload_size * MB_IN_BYTES;
} elseif ( $old_max_upload_size === 0 ) {
$settings['limits']['all']['bytes'] = GB_IN_BYTES * 5; //default to 5GB if they had unlimited set before
} else {
$settings['limits']['all']['bytes'] = $this->max_upload_size;
}
}
if ( ! isset( $settings['limits']['all']['format'] ) ) {
$settings['limits']['all']['format'] = $settings['limits']['all']['bytes'] >= GB_IN_BYTES ? 'GB' : 'MB';
}
foreach ( wp_roles()->roles as $role_key => $role ) {
if ( isset( $role['capabilities']['upload_files'] ) && $role['capabilities']['upload_files'] ) {
if ( ! isset( $settings['limits'][ $role_key ]['bytes'] ) ) {
$settings['limits'][ $role_key ]['bytes'] = $this->max_upload_size;
}
if ( ! isset( $settings['limits'][ $role_key ]['format'] ) ) {
$settings['limits'][ $role_key ]['format'] = $settings['limits'][ $role_key ]['bytes'] >= GB_IN_BYTES ? 'GB' : 'MB';
}
}
}
if ( $format ) {
foreach ( $settings['limits'] as $role_key => $value ) {
$divisor = ( $value['format'] == 'MB' ? MB_IN_BYTES : GB_IN_BYTES );
$settings['limits'][ $role_key ]['bytes'] = round( $value['bytes'] / $divisor, 1 );
}
}
return $settings;
}
/**
* Adds settings links to plugin row.
*
* @since 2.0
*/
function plugins_list_links( $actions ) {
// Build and escape the URL.
$url = esc_url( $this->settings_url() );
// Create the link.
$custom_links = [];
$custom_links['settings'] = "<a href='$url'>" . esc_html__( 'Settings', 'tuxedo-big-file-uploads' ) . '</a>';
$custom_links['support'] = '<a href="' . esc_url( $this->api_url( '/support/?utm_source=bfu_plugin&utm_medium=plugin&utm_campaign=bfu_plugin&utm_term=support&utm_content=meta' ) ) . '">' . esc_html__( 'Support', 'tuxedo-big-file-uploads' ) . '</a>';
// Adds the links to the beginning of the array.
return array_merge( $custom_links, $actions );
}
/**
* Get the settings url with optional url args.
*
* @since 2.0
*
* @param array $args Optional. Same as for add_query_arg()
*
* @return string Unescaped url to settings page.
*/
function settings_url( $args = [] ) {
if ( is_multisite() ) {
$base = network_admin_url( 'settings.php?page=big_file_uploads' );
} else {
$base = admin_url( 'options-general.php?page=big_file_uploads' );
}
return add_query_arg( $args, $base );
}
/**
* Get a url to the public Infinite Uploads site.
*
* @since 2.0
*
* @param string $path Optional path on the site.
*
* @return string
*/
function api_url( $path = '' ) {
$url = trailingslashit( $this->server_root );
if ( $path && is_string( $path ) ) {
$url .= ltrim( $path, '/' );
}
return $url;
}
/**
* Registers a new settings page under Settings.
*
* @since 2.0
*/
function admin_menu() {
if ( is_multisite() ) {
$page = add_submenu_page(
'settings.php',
__( 'Big File Uploads', 'tuxedo-big-file-uploads' ),
__( 'Big File Uploads', 'tuxedo-big-file-uploads' ),
$this->capability,
'big_file_uploads',
[
$this,
'settings_page',
]
);
} else {
$page = add_options_page(
__( 'Big File Uploads', 'tuxedo-big-file-uploads' ),
__( 'Big File Uploads', 'tuxedo-big-file-uploads' ),
$this->capability,
'big_file_uploads',
[
$this,
'settings_page',
]
);
}