| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Traits; |
| 4 |
|
| 5 |
use WP_Defender\Component\Logger\Rotation_Logger as Logger; |
| 6 |
|
| 7 |
trait IO { |
| 8 |
/** |
| 9 |
* A simple function to create & return the folder that we can use to write tmp files. |
| 10 |
* |
| 11 |
* @return string |
| 12 |
*/ |
| 13 |
protected function get_tmp_path() { |
| 14 |
$upload_dir = wp_upload_dir()['basedir']; |
| 15 |
$tmp_dir = $upload_dir . DIRECTORY_SEPARATOR . 'wp-defender'; |
| 16 |
if ( ! is_dir( $tmp_dir ) ) { |
| 17 |
wp_mkdir_p( $tmp_dir ); |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! is_file( $tmp_dir . DIRECTORY_SEPARATOR . 'index.php' ) ) { |
| 21 |
file_put_contents( $tmp_dir . DIRECTORY_SEPARATOR . 'index.php', '' ); |
| 22 |
} |
| 23 |
|
| 24 |
return $tmp_dir; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* @param $category |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public function get_log_path( $category = '' ) { |
| 33 |
$file = empty( $category ) ? 'defender.log' : $category; |
| 34 |
|
| 35 |
$logger = new Logger(); |
| 36 |
$file_name = $logger->generate_file_name( $file ); |
| 37 |
|
| 38 |
return $this->get_tmp_path() . DIRECTORY_SEPARATOR . $file_name; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Create a lock, this will be use in scanning. |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
protected function get_lock_path() { |
| 47 |
return $this->get_tmp_path() . DIRECTORY_SEPARATOR . 'scan.lock'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Delete a folder with every content inside. |
| 52 |
* |
| 53 |
* @param $dir |
| 54 |
*/ |
| 55 |
public function delete_dir( $dir ) { |
| 56 |
if ( ! is_dir( $dir ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
$it = new \RecursiveDirectoryIterator( $dir, \RecursiveDirectoryIterator::SKIP_DOTS ); |
| 60 |
$files = new \RecursiveIteratorIterator( |
| 61 |
$it, |
| 62 |
\RecursiveIteratorIterator::CHILD_FIRST |
| 63 |
); |
| 64 |
foreach ( $files as $file ) { |
| 65 |
if ( $file->isDir() ) { |
| 66 |
$ret = rmdir( $file->getPathname() ); |
| 67 |
} else { |
| 68 |
$ret = unlink( $file->getPathname() ); |
| 69 |
} |
| 70 |
if ( false === $ret ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
} |
| 74 |
rmdir( $dir ); |
| 75 |
|
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @return string|\WP_Error |
| 81 |
*/ |
| 82 |
public function get_tmp_folder() { |
| 83 |
$tmp_dir = $this->get_tmp_path() . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; |
| 84 |
if ( ! is_dir( $tmp_dir ) ) { |
| 85 |
wp_mkdir_p( $tmp_dir ); |
| 86 |
if ( ! wp_mkdir_p( $tmp_dir ) ) { |
| 87 |
return new \WP_Error( 'defender_dir', sprintf( __( 'Unable to create the tmp directory %s', 'wpdef' ), $tmp_dir ) ); |
| 88 |
} |
| 89 |
} |
| 90 |
return $tmp_dir; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Not remove double quotes inside str_replace(). |
| 95 |
* @param string $file_path |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
protected function convert_end_lines_dos_to_linux( $file_path ) { |
| 100 |
return str_replace( array( "\r\n", "\r" ), "\n", file_get_contents( $file_path ) ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Not remove double quotes inside str_replace(). |
| 105 |
* @param string $file_path |
| 106 |
* |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
protected function convert_end_lines_linux_to_dos( $file_path ) { |
| 110 |
return str_replace( "\n", "\r\n", $this->convert_end_lines_dos_to_linux( $file_path ) ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Compare hashes on different OS. |
| 115 |
* @param string $file_path |
| 116 |
* @param string|array $file_hash |
| 117 |
* |
| 118 |
* @return bool |
| 119 |
*/ |
| 120 |
protected function compare_hashes_on_different_os( $file_path, $file_hash ) { |
| 121 |
if ( hash_equals( md5_file( $file_path ), $file_hash ) ) { |
| 122 |
return true; |
| 123 |
} |
| 124 |
if ( hash_equals( md5( $this->convert_end_lines_dos_to_linux( $file_path ) ), $file_hash ) ) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
if ( hash_equals( md5( $this->convert_end_lines_linux_to_dos( $file_path ) ), $file_hash ) ) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* @param string $file_path Path to file. |
| 136 |
* @param string|array $file_hash Hash or some hashes of file2, e.g. for readme.txt. |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public function compare_hashes( $file_path, $file_hash ) { |
| 141 |
if ( is_string( $file_hash ) ) { |
| 142 |
|
| 143 |
return $this->compare_hashes_on_different_os( $file_path, $file_hash ); |
| 144 |
} elseif ( is_array( $file_hash ) ) { |
| 145 |
// Sometimes file has some hashes. |
| 146 |
foreach ( $file_hash as $hash_value ) { |
| 147 |
if ( $this->compare_hashes_on_different_os( $file_path, $hash_value ) ) { |
| 148 |
return true; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} else { |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|