Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmigf committed Jun 25, 2024
1 parent e73db85 commit 7ccff49
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 66 deletions.
28 changes: 10 additions & 18 deletions includes/documents/abstract-wcpdf-order-document.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,34 +856,26 @@ public function header_logo(): void {
$attachment_src = $attachment[0] ?? '';
$attachment_path = realpath( get_attached_file( $attachment_id ) );

if ( apply_filters( 'wpo_wcpdf_use_path', true ) && ! empty( $attachment_path ) ) {
$src = $attachment_path;
} else {
if ( ! empty( $attachment_src ) ) {
$src = $attachment_src;
} elseif ( ! empty( $attachment_path ) ) {
$src = str_replace( trailingslashit( WP_CONTENT_DIR ), trailingslashit( WP_CONTENT_URL ), $attachment_path );
} else {
wcpdf_log_error( 'Header logo file not found.', 'critical' );
return;
}
if ( empty( $attachment_src ) || empty( $attachment_path ) ) {
wcpdf_log_error( 'Header logo file not found.', 'critical' );
return;
}

$src = apply_filters( 'wpo_wcpdf_use_path', true ) ? $attachment_path : $attachment_src;

if ( ! wpo_wcpdf_is_file_readable( $src ) ) {
// convert path to URL
if ( apply_filters( 'wpo_wcpdf_use_path', true ) && false !== strpos( $src, WP_CONTENT_DIR ) ) {
$src = str_replace( trailingslashit( WP_CONTENT_DIR ), trailingslashit( WP_CONTENT_URL ), $src );

// convert url to path
} elseif ( false !== strpos( $src, WP_CONTENT_URL ) ) {
$src = str_replace( trailingslashit( WP_CONTENT_URL ), trailingslashit( WP_CONTENT_DIR ), $src );
}

// last check
if ( ! wpo_wcpdf_is_file_readable( $src ) ) {
// convert to path again if necessary
if ( false !== strpos( $src, WP_CONTENT_URL ) ) {
$src = str_replace( trailingslashit( WP_CONTENT_URL ), trailingslashit( WP_CONTENT_DIR ), $src );
}

$permissions_info = wpo_wcpdf_get_path_permissions_info( $src );
wcpdf_log_error( 'Header logo file not readable: ' . $src . "\n" . $permissions_info, 'critical' );
wcpdf_log_error( 'Header logo file not readable: ' . $src, 'critical' );
return;
}
}
Expand Down
6 changes: 0 additions & 6 deletions includes/views/advanced-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@
'result' => function_exists( 'base64_decode' ),
'fallback' => __( 'base64_decode disabled', 'woocommerce-pdf-invoices-packing-slips' ),
),
'POSIX' => array (
'required' => __( 'To get file or directory owner and group names', 'woocommerce-pdf-invoices-packing-slips' ),
'value' => null,
'result' => extension_loaded( 'posix' ),
'fallback' => __( 'posix disabled', 'woocommerce-pdf-invoices-packing-slips' ),
),
) );

if ( ( $xc = extension_loaded( 'xcache' ) ) || ( $apc = extension_loaded( 'apc' ) ) || ( $zop = extension_loaded( 'Zend OPcache' ) ) || ( $op = extension_loaded( 'opcache' ) ) ) {
Expand Down
60 changes: 18 additions & 42 deletions includes/wcpdf-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -853,54 +853,30 @@ function wpo_wcpdf_is_file_readable( string $path ): bool {
if ( filter_var( $path, FILTER_VALIDATE_URL ) ) {
$response = wp_safe_remote_head( $path );

if ( ! is_wp_error( $response ) ) {
$status_code = wp_remote_retrieve_response_code( $response );

if ( $status_code === 200 ) {
return true;
}
if ( is_wp_error( $response ) ) {
wcpdf_log_error( 'Failed to access file URL: ' . $path . ' Error: ' . $response->get_error_message(), 'critical' );
return false;
}

wcpdf_log_error( 'Failed to access file URL: ' . $path . ' Error: ' . ( is_wp_error( $response ) ? $response->get_error_message() : 'HTTP status code: ' . $status_code ), 'critical' );
$status_code = wp_remote_retrieve_response_code( $response );
return ( $status_code === 200 );

// Local path file check
} else {
if ( is_readable( $path ) ) {
return true;
}
}
} else {
// Fallback to fopen if first check fails
$handle = @fopen( $path, 'r' );

// Fallback to fopen if initial methods fail
$handle = @fopen( $path, 'r' );
if ( $handle ) {
fclose( $handle );
return true;
if ( $handle ) {
fclose( $handle );
return true;
} else {
wcpdf_log_error( 'Failed to open local file with both methods: ' . $path, 'critical' );
return false;
}
}
}

return false;
}

/**
* Get path permissions and ownership information
* Requires PHP POSIX extension to be enabled on the server to get owner and group names
*
* @param string $path
* @return string
*/
function wpo_wcpdf_get_path_permissions_info( string $path ): string {
if ( ! file_exists( $path ) ) {
return 'Path does not exist.';
}

$permissions = substr( sprintf( '%o', fileperms( $path ) ), -4 );
$owner_id = fileowner( $path );
$group_id = filegroup( $path );
$owner_info = function_exists( 'posix_getpwuid' ) ? posix_getpwuid( $owner_id ) : null;
$group_info = function_exists( 'posix_getgrgid' ) ? posix_getgrgid( $group_id ) : null;
$owner_name = $owner_info ? $owner_info['name'] : $owner_id;
$group_name = $group_info ? $group_info['name'] : $group_id;

$info = "Permissions: $permissions\n";
$info .= "Owner: $owner_name\n";
$info .= "Group: $group_name\n";

return $info;
}

0 comments on commit 7ccff49

Please sign in to comment.