PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.2
Defender Security – Malware Scanner, Login Security & Firewall v6.2.2
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / helper / class-file.php

class-file.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.2, at src/helper/class-file.php

80 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * File related helper utilities.
4 *
5 * @package WP_Defender\Helper
6 */
7
8 namespace WP_Defender\Helper;
9
10 use WP_Error;
11 use WP_Filesystem_Base;
12
13 /**
14 * Handles file related tasks.
15 */
16 class File {
17
18 /**
19 * Check is two files identical.
20 *
21 * @param string $local_file File path of the local file for content comparison.
22 * @param string $remote_file Url of the remote file for content comparison.
23 *
24 * @return bool|string|WP_Error If remote fetch fails return WP_Error object or
25 * true for identical file content or false for non-identical file content.
26 */
27 public function is_identical_content( string $local_file, string $remote_file ) {
28 wp_raise_memory_limit();
29
30 $local_file_content = file( $local_file, FILE_IGNORE_NEW_LINES );
31
32 $tmp = download_url( $remote_file );
33
34 if ( is_wp_error( $tmp ) ) {
35 return $tmp;
36 }
37
38 $remote_file_content = file( $tmp, FILE_IGNORE_NEW_LINES );
39
40 wp_delete_file( $tmp );
41
42 return $local_file_content === $remote_file_content;
43 }
44
45 /**
46 * Deny access for the provided directory.
47 *
48 * @param string $directory File path to the directory.
49 *
50 * @since 4.2.0
51 */
52 public function maybe_dir_access_deny( string $directory ) {
53 global $wp_filesystem;
54 // Initialize the WP filesystem, no more using 'file-put-contents' function.
55 if ( ! $wp_filesystem instanceof WP_Filesystem_Base ) {
56 require_once ABSPATH . '/wp-admin/includes/file.php';
57 WP_Filesystem();
58 }
59 $files = array(
60 array(
61 'base' => $directory,
62 'file' => '.htaccess',
63 'content' => 'deny from all',
64 ),
65 array(
66 'base' => $directory,
67 'file' => 'index.html',
68 'content' => '',
69 ),
70 );
71
72 foreach ( $files as $file ) {
73 $file_path = trailingslashit( $file['base'] ) . $file['file'];
74 if ( ! file_exists( $file_path ) ) {
75 $wp_filesystem->put_contents( $file_path, $file['content'] );
76 }
77 }
78 }
79 }
80