-
Notifications
You must be signed in to change notification settings - Fork 4
/
woo-product-download-from-amazon-s3.php
674 lines (521 loc) · 23.9 KB
/
woo-product-download-from-amazon-s3.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
<?php
/**
* Plugin Name: Woo Product Download from Amazon S3
* Plugin URI: https://wordpress.org/plugins/woo-product-download-from-amazon-s3/
* Description: WooCommerce Product Download / Upload to / from using Amazon S3 service.
* Version: 1.0.4
* Author: Emran
* Author URI: https://emran.me/
* License: GPLv2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* Text Domain: woo-product-download-from-amazon-s3
* Domain Path: /languages/
*/
defined( 'ABSPATH' ) or die( 'Keep Quit' );
if ( ! class_exists( 'EA_WC_Amazon_S3' ) ):
class EA_WC_Amazon_S3 {
private $access_id;
private $secret_key;
private $bucket;
private $endpoint;
private $log_enabled;
private $log;
public function __construct() {
$this->constants();
$this->includes();
$this->hooks();
do_action( 'ea_wc_amazon_s3_loaded', $this );
}
private function constants() {
$this->access_id = trim( get_option( 'ea_wc_amazon_s3_key' ) );
$this->secret_key = trim( get_option( 'ea_wc_amazon_s3_secret_key' ) );
$this->endpoint = trim( get_option( 'ea_wc_amazon_s3_endpoint' ) );
$this->log_enabled = ( get_option( 'ea_wc_amazon_s3_log', 'no' ) === 'yes' ) ? TRUE : FALSE;
}
private function includes() {
if ( ! class_exists( 'S3' ) ) {
require_once dirname( __FILE__ ) . '/includes/S3.php';
}
}
public function init() {
load_plugin_textdomain( 'woo-product-download-from-amazon-s3', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
private function hooks() {
// Init
add_action( 'init', array( $this, 'init' ) );
// Upload Process
add_action( 'wp_loaded', array( $this, 'upload_handler' ) );
// Add Settings Tab
add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_tab' ), 50 );
// Show Settings Fields
add_action( 'woocommerce_settings_tabs_woo-product-download-from-amazon-s3', array( $this, 'settings_fields' ) );
// Save Settings Fields
add_action( 'woocommerce_update_options_woo-product-download-from-amazon-s3', array( $this, 'update_settings' ) );
// File Download Process
add_action( 'woocommerce_download_file_from_ea_wc_amazon_s3', array( $this, 'process_download' ), 10, 2 );
// File Download Method
add_filter( 'woocommerce_file_download_method', array( $this, 'file_download_method' ) );
// Add Admin Script
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
// Add Media Tab
add_filter( 'media_upload_tabs', array( $this, 'media_tabs' ) );
// Bucket List
add_action( 'media_upload_ea_wc_amazon_buckets', array( $this, 'buckets_iframe' ) );
// Upload to bucket
add_action( 'media_upload_ea_wc_amazon_upload', array( $this, 'upload_iframe' ) );
// Plugin Row Meta
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' ), 999 );
}
public function log( $message ) {
if ( $this->log_enabled ) {
if ( empty( $this->log ) ) {
$this->log = new WC_Logger();
}
$this->log->add( 'EA-WC-Amazon-S3', $message );
}
}
public function upload_handler() {
if ( ! is_admin() || ! isset( $_POST[ 'ea_wc_amazon_s3_upload_submit' ] ) ) {
return;
}
if ( ! isset( $_POST[ '_wpnonce' ] ) || ! wp_verify_nonce( $_POST[ '_wpnonce' ], 'ea_wc_amazon_s3_upload_file' ) ) {
wp_die( esc_html__( 'Not Verified', 'woo-product-download-from-amazon-s3' ), esc_html__( 'Error', 'woo-product-download-from-amazon-s3' ), array( 'back_link' => TRUE ) );
}
if ( empty( $_FILES[ 'ea_wc_amazon_s3_file' ] ) || empty( $_FILES[ 'ea_wc_amazon_s3_file' ][ 'name' ] ) ) {
wp_die( esc_html__( 'Please select a file to upload', 'woo-product-download-from-amazon-s3' ), esc_html__( 'Error', 'woo-product-download-from-amazon-s3' ), array( 'back_link' => TRUE ) );
}
$file = array(
'bucket' => $_POST[ 'ea_wc_amazon_s3_bucket' ],
'name' => $_FILES[ 'ea_wc_amazon_s3_file' ][ 'name' ],
'file' => $_FILES[ 'ea_wc_amazon_s3_file' ][ 'tmp_name' ],
'type' => $_FILES[ 'ea_wc_amazon_s3_file' ][ 'type' ]
);
if ( $this->upload_file( $file ) ) {
set_transient( 'ea_wc_amazon_s3_abs_path', $this->get_s3_absolute_path(), MINUTE_IN_SECONDS );
set_transient( 'ea_wc_amazon_s3_file_name', trim( $file[ 'name' ] ), MINUTE_IN_SECONDS );
set_transient( 'ea_wc_amazon_s3_bucket_name', trim( $file[ 'bucket' ] ), MINUTE_IN_SECONDS );
wp_safe_redirect( add_query_arg( 'ea_wc_amazon_s3_success', '1', $_SERVER[ 'HTTP_REFERER' ] ) );
exit;
} else {
delete_transient( 'ea_wc_amazon_s3_abs_path' );
delete_transient( 'ea_wc_amazon_s3_file_name' );
delete_transient( 'ea_wc_amazon_s3_bucket_name' );
wp_die( esc_html__( 'Something went wrong during the upload process.', 'woo-product-download-from-amazon-s3' ), esc_html__( 'Error', 'woo-product-download-from-amazon-s3' ), array( 'back_link' => TRUE ) );
}
}
public function upload_file( $file = array() ) {
$s3 = new S3( $this->access_id, $this->secret_key, FALSE );
$bucket = empty( $file[ 'bucket' ] ) ? $this->bucket : $file[ 'bucket' ];
$resource = $s3->inputFile( $file[ 'file' ] );
$resource[ 'type' ] = $file[ 'type' ];
$push_file = $s3->putObject( $resource, $bucket, $file[ 'name' ] );
if ( $push_file ) {
return TRUE;
} else {
return FALSE;
}
}
public function get_s3_absolute_path() {
$protocol = ( is_ssl() ? 'https://' : 'http://' );
$url = trim( get_option( 'ea_wc_amazon_s3_endpoint' ) );
return trailingslashit( $protocol . $url );
}
public function plugin_action_links( $links ) {
if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
$action_links = apply_filters( 'ea_wc_amazon_s3_action_links', array(
'settings' => sprintf( '<a href="%1$s" title="%2$s">%2$s</a>', esc_url( add_query_arg( array(
'page' => 'wc-settings',
'tab' => 'woo-product-download-from-amazon-s3'
), admin_url( 'admin.php' ) ) ), esc_attr__( 'Amazon S3 Settings', 'woo-product-download-from-amazon-s3' ) ),
) );
return array_merge( $action_links, $links );
}
return (array) $links;
}
public function file_download_method() {
return 'from_ea_wc_amazon_s3';
}
public function admin_enqueue_scripts() {
wp_enqueue_media();
wp_enqueue_script( 'woo-product-download-from-amazon-s3', untrailingslashit( plugin_dir_url( __FILE__ ) ) . '/js/scripts.js', array( 'jquery' ), FALSE, TRUE );
}
public function buckets_iframe() {
wp_iframe( array( $this, 'buckets_iframe_content' ) );
}
public function buckets_iframe_content( $type = 'file', $errors = NULL, $id = NULL ) {
wp_enqueue_style( 'media' );
$page = isset( $_GET[ 'p' ] ) ? $_GET[ 'p' ] : 1;
$per_page = 30;
$offset = $per_page * ( $page - 1 );
$offset = $offset < 1 ? 30 : $offset;
$start = isset( $_GET[ 'start' ] ) ? rawurldecode( $_GET[ 'start' ] ) : '';
$bucket = isset( $_GET[ 'bucket' ] ) ? rawurldecode( $_GET[ 'bucket' ] ) : FALSE;
if ( ! $bucket ) {
$buckets = $this->get_s3_buckets();
} else {
$this->bucket = $bucket;
$files = $this->get_s3_files( $start, $offset );
}
?>
<script type="text/javascript">
jQuery(function ($) {
$(document.body).on('click', '.insert-s3', function (e) {
e.preventDefault();
var file = $(this).data('s3');
$(parent.window.file_name_field).val(file);
$(parent.window.file_path_field).val("<?php echo $this->get_s3_absolute_path() . trailingslashit( $this->bucket ); ?>" + file);
parent.window.tb_remove();
});
});
</script>
<div style="margin: 20px 1em 1em; padding-right:20px;" id="media-items">
<?php
if ( ! $bucket ) { ?>
<h3 class="media-title"><?php esc_html_e( 'Select a Bucket', 'woo-product-download-from-amazon-s3' ); ?></h3>
<?php
if ( is_array( $buckets ) ) {
echo '<table class="wp-list-table widefat fixed striped" style="max-height: 500px;overflow-y:scroll;">';
echo '<tr>';
echo '<th><strong>' . esc_html__( 'Bucket name', 'woo-product-download-from-amazon-s3' ) . '</strong></th>';
echo '<th><strong>' . esc_html__( 'Actions', 'woo-product-download-from-amazon-s3' ) . '</strong></th>';
echo '</tr>';
foreach ( $buckets as $key => $bucket ) {
echo '<tr>';
echo '<td>' . $bucket . '</td>';
echo '<td>';
echo '<a href="' . esc_url( add_query_arg( 'bucket', $bucket ) ) . '">' . esc_html__( 'Browse', 'woo-product-download-from-amazon-s3' ) . '</a>';
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
} else {
$back = admin_url( 'media-upload.php?post_id=' . absint( $_GET[ 'post_id' ] ) );
if ( is_array( $files ) ) {
$i = 0;
$total_items = count( $files );
echo '<p><button class="button-secondary" onclick="history.back();">' . esc_html__( 'Go Back', 'woo-product-download-from-amazon-s3' ) . '</button></p>';
echo '<table class="wp-list-table widefat fixed striped" style="max-height: 500px;overflow-y:scroll;">';
echo '<tr>';
echo '<th>' . esc_html__( 'File name', 'woo-product-download-from-amazon-s3' ) . '</th>';
echo '<th>' . esc_html__( 'File size', 'woo-product-download-from-amazon-s3' ) . '</th>';
echo '<th>' . esc_html__( 'Uploaded on', 'woo-product-download-from-amazon-s3' ) . '</th>';
echo '<th>' . esc_html__( 'Actions', 'woo-product-download-from-amazon-s3' ) . '</th>';
echo '</tr>';
foreach ( $files as $key => $file ) {
echo '<tr>';
if ( $i == 0 ) {
$first_file = $key;
}
if ( $i == 10 ) {
$last_file = $key;
}
if ( $file[ 'name' ][ strlen( $file[ 'name' ] ) - 1 ] === '/' ) {
continue; // Don't show folders
}
echo '<td>' . esc_html( $file[ 'name' ] ) . '</td>';
echo '<td>' . size_format( $file[ 'size' ] ) . '</td>';
echo '<td>' . human_time_diff( $file[ 'time' ] ) . ' ' . esc_html__( 'ago', 'woo-product-download-from-amazon-s3' ) . '</td>';
echo '<td>';
echo '<a class="insert-s3 button-secondary" href="#" data-s3="' . esc_attr( $file[ 'name' ] ) . '">' . esc_html__( 'Use File', 'woo-product-download-from-amazon-s3' ) . '</a>';
echo '</td>';
echo '</tr>';
$i ++;
}
echo '</table>';
}
$base = admin_url( 'media-upload.php?post_id=' . absint( $_GET[ 'post_id' ] ) . '&tab=ea_wc_amazon_buckets' );
if ( $bucket ) {
$base = add_query_arg( 'bucket', $bucket, $base );
}
echo '<div class="s3-pagination tablenav">';
echo '<div class="tablenav-pages alignright">';
if ( isset( $_GET[ 'p' ] ) && $_GET[ 'p' ] > 1 ) {
echo '<a class="page-numbers prev button-secondary" href="' . esc_url( remove_query_arg( 'p', $base ) ) . '">' . esc_html__( 'Start Over', 'woo-product-download-from-amazon-s3' ) . '</a>';
}
if ( $i >= 10 ) {
echo '<a class="page-numbers next button-secondary" href="' . esc_url( add_query_arg( array(
'p' => $page + 1,
'start' => $last_file
), $base ) ) . '">' . esc_html__( 'View More', 'woo-product-download-from-amazon-s3' ) . '</a>';
}
echo '</div>';
echo '</div>';
}
?>
</div>
<?php
}
public function get_s3_buckets( $marker = NULL, $max = NULL ) {
$s3 = new S3( $this->access_id, $this->secret_key, is_ssl(), $this->endpoint );
return $s3->listBuckets();
}
public function get_s3_files( $marker = NULL, $max = NULL ) {
$s3 = new S3( $this->access_id, $this->secret_key, is_ssl(), $this->endpoint );
return $s3->getBucket( $this->bucket, NULL, $marker, $max );
}
public function media_tabs( $tabs ) {
$tabs[ 'ea_wc_amazon_buckets' ] = esc_html__( 'Browse S3 Buckets', 'woo-product-download-from-amazon-s3' );
$tabs[ 'ea_wc_amazon_upload' ] = esc_html__( 'Upload to Amazon S3', 'woo-product-download-from-amazon-s3' );
return $tabs;
}
public function upload_iframe() {
wp_iframe( array( $this, 'upload_iframe_content' ) );
}
public function upload_iframe_content( $type = 'file', $errors = NULL, $id = NULL ) {
wp_enqueue_style( 'media' );
//$form_action_url = add_query_arg( array( 'ea_wc_amazon_s3_action' => 's3_upload' ), admin_url() );
?>
<style>
.ea_wc_amazon_s3_errors {
-webkit-border-radius : 2px;
-moz-border-radius : 2px;
border-radius : 2px;
border : 1px solid #E6DB55;
margin : 0 0 21px 0;
background : #FFFFE0;
color : #333;
}
.ea_wc_amazon_s3_errors p {
margin : 10px 15px;
padding : 10px;
}
.wrap {
margin : 20px 1em 1em;
padding-right : 20px;
}
</style>
<script>
jQuery(document).ready(function ($) {
$('.woo-product-download-from-amazon-s3-insert').on('click', function (e) {
e.preventDefault();
var link = "<?php echo get_transient( 'ea_wc_amazon_s3_abs_path' ) ?>";
var file = "<?php echo get_transient( 'ea_wc_amazon_s3_file_name' ) ?>";
var bucket = "<?php echo trailingslashit( get_transient( 'ea_wc_amazon_s3_bucket_name' ) ); ?>";
$(parent.window.file_name_field).val(file);
$(parent.window.file_path_field).val(link + bucket + file);
parent.window.tb_remove();
$('.ea_wc_amazon_s3_errors').remove();
});
});
</script>
<div class="wrap">
<form enctype="multipart/form-data" method="post" action="<?php echo esc_url( admin_url() ); ?>">
<table class="form-table">
<tbody>
<tr>
<th><label for="ea_wc_amazon_s3_bucket"><?php esc_html_e( 'Select a bucket', 'woo-product-download-from-amazon-s3' ); ?></label></th>
<td>
<select name="ea_wc_amazon_s3_bucket" id="ea_wc_amazon_s3_bucket">
<option value=""><?php esc_html_e( ' - Select a Bucket - ', 'woo-product-download-from-amazon-s3' ); ?></option>
<?php foreach ( $this->get_s3_buckets() as $key => $bucket ) : ?>
<option value="<?php echo esc_attr( $bucket ); ?>"><?php echo esc_html( $bucket ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th><label for="ea_wc_amazon_s3_file"><?php esc_html_e( 'Choose file to upload', 'woo-product-download-from-amazon-s3' ); ?></label></th>
<td>
<input id="ea_wc_amazon_s3_file" type="file" name="ea_wc_amazon_s3_file">
</td>
</tr>
<tr>
<th> </th>
<td>
<input type="submit" name="ea_wc_amazon_s3_upload_submit" class="button-secondary"
value="<?php esc_attr_e( 'Upload to S3', 'woo-product-download-from-amazon-s3' ); ?>"/>
</td>
</tr>
<?php if ( ! empty( $_GET[ 'ea_wc_amazon_s3_success' ] ) && '1' == $_GET[ 'ea_wc_amazon_s3_success' ] ) : ?>
<tr>
<td colspan="2">
<div class="ea_wc_amazon_s3_errors">
<p class="ea_wc_amazon_s3_success"><?php _e( 'Success! <a href="#" class="woo-product-download-from-amazon-s3-insert">Insert uploaded file</a>.' ) ?></p>
</div>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php wp_nonce_field( 'ea_wc_amazon_s3_upload_file' ); ?>
</form>
</div>
<?php
}
public function process_download( $file_path, $filename ) {
$this->log( '===========================================' );
$file_download_method = get_option( 'woocommerce_file_download_method', 'force' );
if ( ! $this->is_aws_hosted_file( $file_path ) ) {
$this->log( 'Non AWS Hosted File path: "' . $file_path . '"' );
$this->log( 'Non AWS Hosted File name: "' . $filename . '"' );
$this->log( 'Non AWS Hosted File download method: "' . $file_download_method . '"' );
// This Hook is used from /woocommerce/includes/class-wc-download-handler.php file
do_action( 'woocommerce_download_file_' . $file_download_method, $file_path, $filename );
} else {
$this->log( 'AWS RAW File Path: "' . $file_path . '"' );
$this->log( 'AWS RAW File Name: "' . $filename . '"' );
$this->log( 'AWS Endpoint: "' . get_option( 'ea_wc_amazon_s3_endpoint' ) . '"' );
$this->log( 'AWS Hosted File download method: "' . $file_download_method . '"' );
$file_path = apply_filters( 'ea_wc_amazon_s3_file_path', rawurldecode( $file_path ), $file_path );
$this->log( 'AWS File Full Path: "' . $file_path . '"' );
$file_path = str_replace( $this->get_s3_absolute_path(), '', $file_path );
$file = $this->get_s3_url( $file_path );
$this->log( 'Actual File: "' . $file_path . '"' );
$this->log( 'Downloadable URL: "' . $file . '"' );
$remote_header = get_headers( $file, TRUE );
preg_match( '/\d{3}/', $remote_header[ 0 ], $remote_code );
$this->log( 'Remote Header: ' . wp_json_encode( $remote_code ) );
if ( $remote_code[ 0 ] == '404' || $remote_code[ 0 ] == '403' ) {
$this->download_error( esc_html__( 'File not found. Please try again.', 'woocommerce' ) );
}
$this->log( 'WC Function Used : "' . sprintf( 'WC_Download_Handler::download_file_%s', $file_download_method ) . '"' );
call_user_func( sprintf( 'WC_Download_Handler::download_file_%s', $file_download_method ), $file, $filename );
// Old $this->download_headers( $file, $filename );
// Old $this->readfile_chunked( $file );
}
exit();
}
public function is_aws_hosted_file( $file_path ) {
$parse_url = parse_url( $file_path );
return ( trim( get_option( 'ea_wc_amazon_s3_endpoint' ) ) == $parse_url[ 'host' ] );
}
public function get_s3_url( $filename ) {
$s3 = new S3( $this->access_id, $this->secret_key, is_ssl(), $this->endpoint );
if ( FALSE !== strpos( $filename, '/' ) ) {
$parts = explode( '/', $filename );
$bucket = $parts[ 0 ];
if ( in_array( $bucket, $this->get_s3_buckets() ) ) {
$filename = preg_replace( '#^' . $parts[ 0 ] . '/#', '', $filename, 1 );
} else {
$bucket = $this->bucket;
}
} else {
$bucket = $this->bucket;
}
$aws_file_expire_time = apply_filters( 'ea_wc_amazon_s3_download_expire_time', ( 1 * HOUR_IN_SECONDS ) );
return $s3->getAuthenticatedURL( $bucket, $filename, $aws_file_expire_time, FALSE, is_ssl() );
}
private function download_error( $message, $title = '', $status = 404 ) {
if ( ! strstr( $message, '<a ' ) ) {
$message .= ' <a href="' . esc_url( home_url() ) . '" class="wc-forward">' . __( 'Go to homepage', 'woocommerce' ) . '</a>';
}
wp_die( $message, $title, array( 'response' => $status ) );
}
// OLD
private function download_headers( $file_path, $filename ) {
$this->check_server_config();
$this->clean_buffers();
nocache_headers();
header( "X-Robots-Tag: noindex, nofollow", TRUE );
header( "Content-Type: " . $this->get_download_content_type( $filename ) );
header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename=\"" . $filename . "\";" );
header( "Content-Transfer-Encoding: binary" );
if ( $size = @filesize( $file_path ) ) {
header( "Content-Length: " . $size );
}
}
private function check_server_config() {
if ( function_exists( 'set_time_limit' ) && FALSE === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) {
@set_time_limit( 0 );
}
if ( function_exists( 'get_magic_quotes_runtime' ) && get_magic_quotes_runtime() && version_compare( phpversion(), '5.4', '<' ) ) {
set_magic_quotes_runtime( 0 );
}
if ( function_exists( 'apache_setenv' ) ) {
@apache_setenv( 'no-gzip', 1 );
}
@ini_set( 'zlib.output_compression', 'Off' );
@session_write_close();
}
private function clean_buffers() {
if ( ob_get_level() ) {
$levels = ob_get_level();
for ( $i = 0; $i < $levels; $i ++ ) {
@ob_end_clean();
}
} else {
@ob_end_clean();
}
}
private function get_download_content_type( $file_path ) {
$file_extension = strtolower( substr( strrchr( $file_path, "." ), 1 ) );
$ctype = "application/force-download";
foreach ( get_allowed_mime_types() as $mime => $type ) {
$mimes = explode( '|', $mime );
if ( in_array( $file_extension, $mimes ) ) {
$ctype = $type;
break;
}
}
return $ctype;
}
// OLD
public function readfile_chunked( $file ) {
$chunksize = 1024 * 1024;
$handle = @fopen( $file, 'r' );
if ( FALSE === $handle ) {
return FALSE;
}
while ( ! @feof( $handle ) ) {
echo @fread( $handle, $chunksize );
if ( ob_get_length() ) {
ob_flush();
flush();
}
}
return @fclose( $handle );
}
public function add_tab( $settings_tabs ) {
$settings_tabs[ 'woo-product-download-from-amazon-s3' ] = esc_html__( 'Amazon S3 Settings', 'woo-product-download-from-amazon-s3' );
return $settings_tabs;
}
public function update_settings() {
woocommerce_update_options( $this->settings_options() );
}
public function settings_options() {
$settings = array();
$settings[] = array(
'title' => esc_html__( 'Amazon S3 Settings', 'woo-product-download-from-amazon-s3' ),
'type' => 'title',
'id' => 'ea_wc_amazon_s3',
);
$settings[] = array(
'name' => esc_html__( 'Amazon S3 Access Key ID', 'woo-product-download-from-amazon-s3' ),
'desc' => esc_html__( 'After logging into your Amazon S3 account, click on "Security Credentials" in the sidebar. Scroll down to "Access Keys (Access Key ID and Secret Access Key)" and you will see your Access Key ID. Copy and paste it here.', 'woo-product-download-from-amazon-s3' ),
'id' => 'ea_wc_amazon_s3_key',
'type' => 'text',
);
$settings[] = array(
'name' => esc_html__( 'Amazon S3 Secret Key', 'woo-product-download-from-amazon-s3' ),
'desc' => esc_html__( 'In the same Access Credentials area, your "Secret Key" will be hidden. You will need to click the "Show" link to see it. Copy and paste it here.', 'woo-product-download-from-amazon-s3' ),
'id' => 'ea_wc_amazon_s3_secret_key',
'type' => 'text',
);
$settings[] = array(
'name' => esc_html__( 'Amazon S3 EndPoint', 'woo-product-download-from-amazon-s3' ),
'desc' => __( '<br>Amazon S3 Endpoint like: <code>s3-us-west-2.amazonaws.com</code> without any http or https. Not S3 Website EndPoint link.<br>
<ul>
<li>If you create a client by specifying any other AWS Region, each of these regions maps to the region-specific endpoint: <code>s3-<region>.amazonaws.com</code>. </li>
</ul>' ),
'id' => 'ea_wc_amazon_s3_endpoint',
'type' => 'text',
);
$settings[] = array(
'name' => esc_html__( 'Enable Log', 'woo-product-download-from-amazon-s3' ),
'desc' => __( 'Enable Log to logging issues.' ),
'id' => 'ea_wc_amazon_s3_log',
'type' => 'checkbox',
'default' => 'no'
);
$settings[] = array( 'type' => 'sectionend', 'id' => 'ea_wc_amazon_s3' );
return apply_filters( 'ea_wc_amazon_s3_setting_options', $settings );
}
public function settings_fields() {
woocommerce_admin_fields( $this->settings_options() );
}
}
new EA_WC_Amazon_S3();
endif;