| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
global $wpdb; |
| 5 |
global $wp_filesystem; |
| 6 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 7 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 8 |
} |
| 9 |
WP_Filesystem(); |
| 10 |
$upload_dir = wp_upload_dir(); |
| 11 |
$attach_id = (int) $attach_id; |
| 12 |
$attachment = $wpdb->get_row( |
| 13 |
$wpdb->prepare( |
| 14 |
"SELECT * FROM {$wpdb->prefix}wppm_attachments WHERE id = %d", |
| 15 |
$attach_id |
| 16 |
) |
| 17 |
); |
| 18 |
if ( ! $attachment ) { |
| 19 |
wp_die( esc_html__( 'Invalid attachment.', 'taskbuilder' ) ); |
| 20 |
} |
| 21 |
|
| 22 |
if ( ! current_user_can( 'read' ) ) { |
| 23 |
wp_die( esc_html__( 'Unauthorized access.', 'taskbuilder' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
$date_created = $attachment->date_created; |
| 27 |
$timestamp = strtotime( $attachment->date_created ); |
| 28 |
if ( ! $timestamp ) { |
| 29 |
wp_die( esc_html__( 'Invalid file date.', 'taskbuilder' ) ); |
| 30 |
} |
| 31 |
$time = strtotime($date_created); |
| 32 |
$month = wp_date("m",$time); |
| 33 |
$year = wp_date("Y",$time); |
| 34 |
$findStr = ".txt"; |
| 35 |
$filepath = ""; |
| 36 |
$attachment_name = sanitize_file_name($attachment->name); |
| 37 |
$attach_file_name = sanitize_file_name($attachment->file_name); |
| 38 |
$filepath = $attachment->file_path; |
| 39 |
//$file_contents = $wp_filesystem->get_contents( $filepath ); |
| 40 |
while ( ob_get_level() ) { |
| 41 |
ob_end_clean(); |
| 42 |
} |
| 43 |
$filepath = wp_normalize_path($attachment->file_path); |
| 44 |
$base_dir = wp_normalize_path(trailingslashit($upload_dir['basedir']) . 'wppm/'); |
| 45 |
|
| 46 |
if ( |
| 47 |
empty($filepath) || |
| 48 |
!file_exists($filepath) || |
| 49 |
strpos($filepath, $base_dir) !== 0 |
| 50 |
) { |
| 51 |
wp_die(esc_html__('File not found.', 'taskbuilder')); |
| 52 |
} |
| 53 |
$file_contents = $wp_filesystem->get_contents($filepath); |
| 54 |
//Turn off output buffering |
| 55 |
if (ob_get_level()) ob_end_clean(); |
| 56 |
if( !file_exists($filepath)) return; |
| 57 |
$mime_type = wp_check_filetype($filepath); |
| 58 |
$content_type = ! empty( $mime_type['type'] ) |
| 59 |
? $mime_type['type'] |
| 60 |
: 'application/octet-stream'; |
| 61 |
header('Content-Type: ' . $content_type); |
| 62 |
// Check whether attachment is of image type |
| 63 |
if($attachment->is_image){ |
| 64 |
if(ob_get_length() > 0) { |
| 65 |
ob_clean(); |
| 66 |
} |
| 67 |
//readfile( $filepath ); |
| 68 |
echo $file_contents; |
| 69 |
exit; |
| 70 |
} |
| 71 |
header('Content-Description: File Transfer'); |
| 72 |
header('Cache-Control: public'); |
| 73 |
header("Content-Transfer-Encoding: binary"); |
| 74 |
header("Content-Disposition: attachment;filename=".($attach_file_name)); |
| 75 |
header('Content-Length: '.filesize($filepath)); |
| 76 |
if (ob_get_length()) ob_clean(); |
| 77 |
flush(); |
| 78 |
//readfile($filepath); |
| 79 |
echo $file_contents; |
| 80 |
exit; |
| 81 |
?> |
| 82 |
|