class-itsec-malware-scan-results-template.php 6.54 KB
Newer Older
John Punzalan's avatar
John Punzalan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
<?php

class ITSEC_Malware_Scan_Results_Template {
	public static function get_html( $results, $show_error_details = false ) {
		$html = "<div class='itsec-malware-scan-results'>\n";

		if ( is_wp_error( $results ) ) {
			$html .= self::get_wp_error_details( $results, $show_error_details );
		} else {
			$html .= self::get_system_error_details( $results );
			$html .= self::get_malware_details( $results );
			$html .= self::get_blacklist_details( $results );
		}

		$html .= "</div>\n";

		return $html;
	}

	protected static function get_wp_error_details( $results, $show_error_details ) {
		$status = 'error';
		$description = __( 'The scan failed to properly scan the site.', 'better-wp-security' );

		$details = '<p>' . sprintf( __( 'Error Message: %s', 'better-wp-security' ), $results->get_error_message() ) . "</p>\n";
		$details .= '<p>' . sprintf( __( 'Error Code: <code>%s</code>', 'better-wp-security' ), esc_html( $results->get_error_code() ) ) . "</p>\n";

		if ( $show_error_details ) {
			$data = $results->get_error_data();

			if ( ! empty( $data ) ) {
				$details .= '<p>' . __( 'If you contact support about this error, please provide the following debug details:', 'better-wp-security' ) . "</p>\n";

				$details .= '<pre>' . esc_html( print_r( array( 'code' => $results->get_error_code(), 'data' => $data ), true ) ) . "</pre>\n";
			}
		}

		return self::get_wrapped_section( 'wp-error', $status, $description, $details );
	}

	protected static function get_blacklist_details( $results ) {
		$status = 'clean';
		$description = __( 'Blacklist', 'better-wp-security' );
		$details = false;

		$list = '';

		if ( ! empty( $results['BLACKLIST']['WARN'] ) ) {
			$status = 'warn';

			foreach ( (array) $results['BLACKLIST']['WARN'] as $entry ) {
				$list .= sprintf( '<li class="itsec-malware-scan-warn"><span><a href="%1$s">%2$s</a><span></li>', $entry[1], $entry[0] ) . "\n";
			}
		}

		if ( ! empty( $results['BLACKLIST']['INFO'] ) ) {
			foreach ( (array) $results['BLACKLIST']['INFO'] as $entry ) {
				$list .= sprintf( '<li class="itsec-malware-scan-clean"><span><a href="%1$s">%2$s</a></span></li>', $entry[1], $entry[0] ) . "\n";
			}
		}

		if ( ! empty( $list ) ) {
			$details = "<ul>\n$list</ul>\n";
		}

		return self::get_wrapped_section( 'malware', $status, $description, $details );
	}

	protected static function get_malware_details( $results ) {
		$status = 'clean';
		$description = __( 'Malware', 'better-wp-security' );
		$details = false;

		$list = '';

		if ( ! empty( $results['MALWARE']['WARN'] ) ) {
			$status = 'warn';

			foreach ( (array) $results['MALWARE']['WARN'] as $entry ) {
				list( $message, $url ) = explode( ':', $entry[0], 2 );
				$message = trim( $message );
				$url = trim( $url );

				$message_lowercase = strtolower( $message );

				if ( 'security warning in the url' === $message_lowercase ) {
					$message = __( 'Security warning in the URL', 'better-wp-security' );
				} else if ( 'malware found on url' === $message_lowercase ) {
					$message = __( 'Malware found on URL', 'better-wp-security' );
				}

				if ( '&amp;' === substr( $url, 0, 5 ) ) {
					$payload = html_entity_decode( $url );
					$url = get_site_url();
				}

				$message .= "<br>\n";
				$message .= sprintf( __( 'Infected URL: <a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>', 'better-wp-security' ), esc_url( $url ), esc_html( $url ) );

				$parts = explode( "\n", $entry[1], 2 );

				if ( isset( $parts[1] ) ) {
					if ( preg_match( '/(.+)\. Details: (.+)/', $parts[0], $match ) ) {
						$type = $match[1];
						$documentation_url = $match[2];

						if ( '*Known Spam detected' === $type ) {
							$type = __( '*Known Spam detected', 'better-wp-security' );
						}

						$message .= "<br>\n";
						$message .= sprintf( __( 'Type: %1$s', 'better-wp-security' ), $type );
						$message .= "<br>\n";
						$message .= sprintf( __( 'Documentation: <a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>', 'better-wp-security' ), esc_url( $documentation_url ), esc_html( $documentation_url ) );
					}

					$payload_raw = trim( $parts[1] );
					$payload_raw = html_entity_decode( $payload_raw );

					if ( preg_match( '/<div id=\'HiddenDiv\'>(.+)<\/div>/', $payload_raw, $match ) ) {
						$payload = trim( $match[1] );
					}
				}

				if ( ! empty( $payload ) ) {
					$message .= "<br>\n";
					$message .= sprintf( __( 'Payload:<pre>%s</pre>', 'better-wp-security' ), esc_html( $payload ) );
				}

				$list .= sprintf( '<li class="itsec-malware-scan-warn"><span>%s</span></li>', $message ) . "\n";
			}
		}

		if ( ! empty( $list ) ) {
			$details = "<ul>\n$list</ul>\n";
		}

		return self::get_wrapped_section( 'malware', $status, $description, $details );
	}

	protected static function get_system_error_details( $results ) {
		if ( empty( $results['SYSTEM']['ERROR'] ) ) {
			return '';
		}

		$status = 'error';
		$description = __( 'The scan failed to properly scan the site.', 'better-wp-security' );

		$details = "<ul>\n";

		foreach ( $results['SYSTEM']['ERROR'] as $entry ) {
			$details .= sprintf( '<li class="itsec-malware-scan-error"><span>%s</span></li>', $entry ) . "\n";
		}

		$details .= "</ul>\n";

		return self::get_wrapped_section( 'system-error', $status, $description, $details );
	}

	protected static function get_wrapped_section( $type, $status, $description, $details = false ) {
		$html = "<div class='itsec-malware-scan-results-section itsec-malware-scan-results-$type-section'>\n";

		if ( 'clean' === $status ) {
			$status_text = __( 'Clean', 'better-wp-security' );
		} else if ( 'warn' === $status ) {
			$status_text = __( 'Warning', 'better-wp-security' );
		} else if ( 'error' === $status ) {
			$status_text = __( 'Error', 'better-wp-security' );
		}

		$status = "<span class='itsec-malware-scan-$status'>$status_text</span>";

		if ( false === $details ) {
			/* translators: Scan result listing. %1$s is the status. %2$s is the description. */
			$html .= "<p>" . sprintf( _x( '%1$s %2$s', 'scan status, scan description', 'better-wp-security' ), $status, $description ) . "</p>\n";
		} else {
			$details_link = "<a href='#' class='itsec-malware-scan-toggle-details'>" . __( 'Show Details', 'better-wp-security' ) . "</a>\n";

			/* translators: Scan result listing. %1$s is the status. %2$s is the description. %3$s is the details link. */
			$html .= "<p>" . sprintf( _x( '%1$s %2$s %3$s', 'scan status, scan description, scan details link', 'better-wp-security' ), $status, $description, $details_link ) . "</p>\n";

			$html .= "<div class='itsec-malware-scan-details'>\n$details\n</div>\n";
		}

		$html .= "</div>\n";

		return $html;
	}
}