| 1 |
<?php |
| 2 |
/** |
| 3 |
* Helper functions for file related tasks. |
| 4 |
* |
| 5 |
* @package WP_Defender\Traits |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Traits; |
| 9 |
|
| 10 |
use WP_Error; |
| 11 |
use WP_Defender\Model\Scan; |
| 12 |
use WP_Defender\Component\Error_Code; |
| 13 |
|
| 14 |
trait File_Operations { |
| 15 |
|
| 16 |
/** |
| 17 |
* Deletes a file if it exists and is writable. |
| 18 |
* |
| 19 |
* @param string $file The path to the file to be deleted. |
| 20 |
* |
| 21 |
* @return bool Returns true if the file was successfully deleted, false otherwise. |
| 22 |
*/ |
| 23 |
public function delete_infected_file( string $file ): bool { |
| 24 |
global $wp_filesystem; |
| 25 |
// Initialize the WP filesystem, no more using 'file-put-contents' function. |
| 26 |
if ( empty( $wp_filesystem ) ) { |
| 27 |
require_once ABSPATH . '/wp-admin/includes/file.php'; |
| 28 |
WP_Filesystem(); |
| 29 |
} |
| 30 |
return file_exists( $file ) && $wp_filesystem->is_writable( $file ) && $wp_filesystem->delete( $file ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Handle actions after deleting a file or folder. |
| 35 |
* |
| 36 |
* @param string $deleted_file The file or folder that was deleted. |
| 37 |
* @param Scan|null $related_scan The scan instance related to the deletion. |
| 38 |
* @param string $scan_type The type of scan. |
| 39 |
* |
| 40 |
* @return array An array with a message confirming the deletion. |
| 41 |
*/ |
| 42 |
public function after_delete( string $deleted_file, ?Scan $related_scan, string $scan_type ): array { |
| 43 |
$this->log( sprintf( '%s is deleted', $deleted_file ), \WP_Defender\Controller\Scan::SCAN_LOG ); |
| 44 |
$related_scan->remove_issue( $this->owner->id ); |
| 45 |
$related_scan->remove_related_issue_by( $deleted_file, $scan_type ); |
| 46 |
do_action( 'wpdef_fixed_scan_issue', $scan_type, 'delete' ); |
| 47 |
|
| 48 |
return array( 'message' => esc_html__( 'This item has been permanently removed', 'defender-security' ) ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns a WP_Error object with an error code indicating that the file is not writeable. |
| 53 |
* |
| 54 |
* @param string $file The path to the file that is not writeable. |
| 55 |
* |
| 56 |
* @return WP_Error The WP_Error object with the error code and the basename of the file. |
| 57 |
*/ |
| 58 |
public function get_permission_error( string $file ): WP_Error { |
| 59 |
return new WP_Error( Error_Code::NOT_WRITEABLE, wp_basename( $file ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get file meta details: created_at, size, and deleted flag. |
| 64 |
* |
| 65 |
* @param string $file The path to the file. |
| 66 |
* @return array An array containing the file's created_at date, size, and a deleted flag. |
| 67 |
*/ |
| 68 |
private function get_file_meta( string $file ): array { |
| 69 |
if ( ! file_exists( $file ) || ! is_readable( $file ) ) { |
| 70 |
return array( 'n/a', 'n/a', true ); |
| 71 |
} |
| 72 |
|
| 73 |
$file_created_at = filemtime( $file ); |
| 74 |
$file_created_at = $file_created_at ? $this->format_date_time( $file_created_at ) : 'n/a'; |
| 75 |
|
| 76 |
$file_size = filesize( $file ); |
| 77 |
$file_size = $file_size ? $this->format_bytes_into_readable( $file_size ) : 'n/a'; |
| 78 |
|
| 79 |
return array( $file_created_at, $file_size, false ); |
| 80 |
} |
| 81 |
} |
| 82 |
|