verify_jwt($token); if (!is_wp_error($result)) { wp_set_current_user($result); $is_user_logged_in = true; } } } else { $is_user_logged_in = is_user_logged_in(); } if( !$is_user_logged_in && $pblc != "true" && ($upfp_enable_doc_viewer == 'no' || !str_contains($_SERVER['HTTP_USER_AGENT'], 'Google AppsViewer') )){ status_header(403); die('403 — Not allowed.'); } list($basedir) = array_values(array_intersect_key(wp_upload_dir(), array('basedir' => 1)))+array(NULL); if($srvr == "true") { $file = ABSPATH . $file; } else if($pblc == "true") { $basedir .= '/upf-pblc'; $file = rtrim($basedir,'/').'/'.str_replace('..', '', isset($_GET[ 'file' ])?sanitize_text_field($_GET[ 'file' ]):''); } else { $basedir .= '/upf-docs'; $file = rtrim($basedir,'/').'/'.str_replace('..', '', isset($_GET[ 'file' ])?sanitize_text_field($_GET[ 'file' ]):''); } if (!$basedir || !is_file($file)) { status_header(404); die('404 — File not found.'); } if(isset($_GET[ 'file' ])){ if($srvr == "true") { $private_file = sanitize_text_field($_GET[ 'file' ]); } else if($pblc == "true") { $private_file = 'upf-pblc/' . sanitize_text_field($_GET[ 'file' ]); } else { $private_file = 'upf-docs/' . sanitize_text_field($_GET[ 'file' ]); } $file_raw_name = sanitize_text_field($_GET[ 'file' ]); } $allowed = $doc_id = 0; if( $upfp_enable_doc_viewer == 'yes' && str_contains($_SERVER['HTTP_USER_AGENT'], 'Google AppsViewer') ){ $allowed = 1; } else { $curr_user_id = get_current_user_id(); $user = get_userdata($curr_user_id); $user_roles = array(); if($user) { $user_roles = $user->roles; array_push($user_roles, 'All Users'); } $upfp_file_manager_roles = get_option('upfp_file_manager_roles'); if(!$upfp_file_manager_roles){ $upfp_file_manager_roles = array(); } // allow access to admin users if(user_can( $curr_user_id, 'administrator' ) || array_intersect($user_roles, $upfp_file_manager_roles)){ $allowed = 1; } else{ // if file-author or allowed-user is viewing the file $the_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => array('inherit', 'trash'), 'meta_key' => '_wp_attached_file', 'meta_value' => $private_file ) ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $doc_id = get_the_ID(); $doc_author = get_the_author_meta("ID"); $doc_owner = get_post_meta($doc_id, 'upf_owner', true); $is_public = get_post_meta($doc_id, 'upf_public', true); if($is_public) { $file_pwd = get_post_meta($doc_id, 'upf_file_pwd', true); if($file_pwd) { $pwd = upf_get_password($doc_id); if($pwd == $file_pwd) { $allowed = 1; } } else { $allowed = 1; } } else if($curr_user_id == $doc_author){ $allowed = 1; }else if($curr_user_id == $doc_owner){ $allowed = 1; } else{ $upf_allowed_users = get_post_meta($doc_id, 'upf_allowed', true); $upf_allowed_roles = get_post_meta($doc_id, 'upf_allowed_roles', true); if($upf_allowed_users || $upf_allowed_roles){ if((is_array($upf_allowed_users) && in_array($curr_user_id, $upf_allowed_users)) || (is_array($upf_allowed_roles) && array_intersect($user_roles, $upf_allowed_roles))){ $allowed = 1; } } } } } wp_reset_query(); // If doc is image and not original but different size if(!$doc_id){ $args = array( 'post_type' => 'attachment', 'post_status' => array('inherit', 'trash'), 'meta_query' => array( array( 'key' => '_wp_attachment_metadata', 'value' => $file_raw_name, 'compare' => 'LIKE' ) ) ); $the_query = new WP_Query( $args ); if ( $the_query->have_posts() ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $doc_id = get_the_ID(); $doc_author = get_the_author_meta("ID"); $doc_owner = get_post_meta($doc_id, 'upf_owner', true); $is_public = get_post_meta($doc_id, 'upf_public', true); if($is_public) { $file_pwd = get_post_meta($doc_id, 'upf_file_pwd', true); if($file_pwd) { $pwd = upf_get_password($doc_id); if($pwd == $file_pwd) { $allowed = 1; } } else { $allowed = 1; } } else if($curr_user_id == $doc_author){ $allowed = 1; }else if($curr_user_id == $doc_owner){ $allowed = 1; } else{ $upf_allowed_users = get_post_meta($doc_id, 'upf_allowed', true); $upf_allowed_roles = get_post_meta($doc_id, 'upf_allowed_roles', true); if($upf_allowed_users || $upf_allowed_roles){ if((is_array($upf_allowed_users) && in_array($curr_user_id, $upf_allowed_users)) || (is_array($upf_allowed_roles) && array_intersect($user_roles, $upf_allowed_roles))){ $allowed = 1; } } } } } wp_reset_query(); } } } if(!$allowed){ status_header(403); die('403 — You do not have Permission to view this file.'); } $mime = wp_check_filetype($file); if( false === $mime[ 'type' ] && function_exists( 'mime_content_type' ) ) $mime[ 'type' ] = mime_content_type( $file ); if( $mime[ 'type' ] ) $mimetype = $mime[ 'type' ]; else $mimetype = 'image/' . substr( $file, strrpos( $file, '.' ) + 1 ); header( 'Content-Type: ' . $mimetype ); // always send this if ( false === strpos( $_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS' ) ) header( 'Content-Length: ' . filesize( $file ) ); $last_modified = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ); $etag = '"' . md5( $last_modified ) . '"'; header( "Last-Modified: $last_modified GMT" ); header( 'ETag: ' . $etag ); header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', time() + 100000000 ) . ' GMT' ); // Support for Conditional GET $client_etag = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? stripslashes( sanitize_text_field( $_SERVER['HTTP_IF_NONE_MATCH'] ) ) : false; if( ! isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) $_SERVER['HTTP_IF_MODIFIED_SINCE'] = false; $client_last_modified = trim( rest_sanitize_boolean( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ); // If string is empty, return 0. If not, attempt to parse into a timestamp $client_modified_timestamp = $client_last_modified ? strtotime( $client_last_modified ) : 0; // Make a timestamp for our most recent modification... $modified_timestamp = strtotime($last_modified); if ( ( $client_last_modified && $client_etag ) ? ( ( $client_modified_timestamp >= $modified_timestamp) && ( $client_etag == $etag ) ) : ( ( $client_modified_timestamp >= $modified_timestamp) || ( $client_etag == $etag ) ) ) { status_header( 304 ); exit; } // If we made it this far, just serve the file status_header( 200 ); readfile( $file ); exit;