| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Behavior\Scan_Item; |
| 4 |
|
| 5 |
use Calotes\Component\Behavior; |
| 6 |
use WP_Defender\Component\Error_Code; |
| 7 |
use WP_Defender\Model\Scan; |
| 8 |
use WP_Defender\Model\Scan_Item; |
| 9 |
use WP_Defender\Traits\Formats; |
| 10 |
use WP_Defender\Traits\IO; |
| 11 |
use WP_Defender\Traits\Plugin; |
| 12 |
|
| 13 |
class Malware_Result extends Behavior { |
| 14 |
use Formats, IO, Plugin; |
| 15 |
|
| 16 |
public function to_array() { |
| 17 |
$data = $this->owner->raw_data; |
| 18 |
$file = $data['file']; |
| 19 |
$file_created_at = @filemtime( $file ); |
| 20 |
if ( $file_created_at ) { |
| 21 |
$file_created_at = $this->format_date_time( $file_created_at ); |
| 22 |
} else { |
| 23 |
$file_created_at = 'n/a'; |
| 24 |
} |
| 25 |
$file_size = @filesize( $file ); |
| 26 |
if ( ! $file_size ) { |
| 27 |
$file_size = 'n/a'; |
| 28 |
} else { |
| 29 |
$file_size = $this->format_bytes_into_readable( $file_size ); |
| 30 |
} |
| 31 |
|
| 32 |
return array( |
| 33 |
'id' => $this->owner->id, |
| 34 |
'type' => Scan_Item::TYPE_SUSPICIOUS, |
| 35 |
'file_name' => pathinfo( $file, PATHINFO_BASENAME ), |
| 36 |
'full_path' => $file, |
| 37 |
'date_added' => $file_created_at, |
| 38 |
'size' => $file_size, |
| 39 |
'short_desc' => __( 'Suspicious function found', 'wpdef' ), |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public function ignore() { |
| 47 |
$scan = Scan::get_last(); |
| 48 |
$scan->ignore_issue( $this->owner->id ); |
| 49 |
|
| 50 |
return array( |
| 51 |
'message' => __( 'The suspicious file has been successfully ignored.', 'wpdef' ), |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
public function unignore() { |
| 59 |
$scan = Scan::get_last(); |
| 60 |
$scan->unignore_issue( $this->owner->id ); |
| 61 |
|
| 62 |
return array( |
| 63 |
'message' => __( 'The suspicious file has been successfully restored.', 'wpdef' ), |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Delete the file, or whole folder. |
| 69 |
* |
| 70 |
* @return array|\WP_Error |
| 71 |
*/ |
| 72 |
public function delete() { |
| 73 |
$data = $this->owner->raw_data; |
| 74 |
$scan = Scan::get_last(); |
| 75 |
$path = $data['file']; |
| 76 |
/** |
| 77 |
* If the path inside theme or plugin folder, then we remove the whole plugin/theme for prevent fatal error. |
| 78 |
* If the path is to wp-config, we can't delete it. |
| 79 |
*/ |
| 80 |
if ( defender_wp_config_path() === $path ) { |
| 81 |
return new \WP_Error( |
| 82 |
Error_Code::INVALID, |
| 83 |
__( 'wp-config.php can\'t be removed. Please remove the suspicious code manually.', 'wpdef' ) |
| 84 |
); |
| 85 |
} |
| 86 |
// If it's not relative path, which mean it is something lay on wp-content folder, we can delete it. |
| 87 |
if ( file_exists( $path ) ) { |
| 88 |
if ( ! @unlink( $path ) ) { |
| 89 |
return new \WP_Error( Error_Code::NOT_WRITEABLE, __( 'Defender doesn\'t have enough permission to remove this file', 'wpdef' ) ); |
| 90 |
} |
| 91 |
} |
| 92 |
$scan->remove_issue( $this->owner->id ); |
| 93 |
$this->log( sprintf( '%s is deleted', $path ), 'scan.log' ); |
| 94 |
|
| 95 |
return array( |
| 96 |
'message' => __( 'This item has been permanently removed', 'wpdef' ), |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
protected function is_active_plugin( $file_path ) { |
| 101 |
$path_data = explode( DIRECTORY_SEPARATOR, plugin_basename( $file_path ) ); |
| 102 |
if ( ! empty( $path_data ) ) { |
| 103 |
$plugin_slug = $path_data[0]; |
| 104 |
} else { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
$active = false; |
| 109 |
foreach ( $this->get_plugins() as $slug => $data ) { |
| 110 |
if ( $plugin_slug === $slug || 0 === strpos( $slug, $plugin_slug ) ) { |
| 111 |
$active = is_multisite() ? is_plugin_active_for_network( $slug ) : is_plugin_active( $slug ); |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return $active; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Return the source code depending on the type of the issue. |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function pull_src() { |
| 125 |
$data = $this->owner->raw_data; |
| 126 |
$active = false; |
| 127 |
$type_src = ''; |
| 128 |
if ( ! file_exists( $data['file'] ) ) { |
| 129 |
return array( |
| 130 |
'origin' => '', |
| 131 |
'mapper' => array(), |
| 132 |
'active' => $active, |
| 133 |
'type_src' => $type_src, |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
if ( false !== stripos( $data['file'], WP_PLUGIN_DIR ) ) { |
| 138 |
$active = $this->is_active_plugin( $data['file'] ); |
| 139 |
$type_src = 'plugin'; |
| 140 |
} |
| 141 |
|
| 142 |
$markers = array(); |
| 143 |
foreach ( $data as $item ) { |
| 144 |
if ( isset( $item['catches'] ) ) { |
| 145 |
foreach ( $item['catches'] as $catch ) { |
| 146 |
$markers = array_merge( $markers, $catch['mapper'] ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
wp_send_json_success( |
| 151 |
array( |
| 152 |
'origin' => file_get_contents( $data['file'] ), |
| 153 |
'mapper' => $markers, |
| 154 |
'active' => $active, |
| 155 |
'type_src' => $type_src, |
| 156 |
) |
| 157 |
); |
| 158 |
} |
| 159 |
} |
| 160 |
|