insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $t['findings'], array( 'scan_id' => $scan_id, 'module' => $module, 'type' => $type, 'severity' => in_array( $severity, self::SEVERITIES, true ) ? $severity : 'low', 'item' => substr( (string) $item, 0, 191 ), 'path' => (string) $path, 'hash' => (string) $hash, 'details' => wp_json_encode( $details ), 'created' => current_time( 'mysql', true ), ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ); } /** * Remove rows of a scan for one module (restart or cancel). * * @param string $module Module id. * @param string $scan_id Scan id. */ public static function discard( $module, $scan_id ) { global $wpdb; $t = Schema::tables(); $wpdb->query( $wpdb->prepare( "DELETE FROM {$t['findings']} WHERE module = %s AND scan_id = %s", $module, $scan_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * Module finished: keep only this scan's rows. * * @param string $module Module id. * @param string $scan_id Scan id. */ public static function commit( $module, $scan_id ) { global $wpdb; $t = Schema::tables(); $wpdb->query( $wpdb->prepare( "DELETE FROM {$t['findings']} WHERE module = %s AND scan_id <> %s", $module, $scan_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * Delete selected rows of the running scan (used to collapse many file differences into one). * * @param string $scan_id Scan id. * @param string $module Module id. * @param string $item Item. * @param string[] $types Types to remove. */ public static function remove_types( $scan_id, $module, $item, array $types ) { global $wpdb; $t = Schema::tables(); $place = implode( ',', array_fill( 0, count( $types ), '%s' ) ); $args = array_merge( array( $scan_id, $module, $item ), $types ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$t['findings']} WHERE scan_id = %s AND module = %s AND item = %s AND type IN ($place)", $args ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * Count rows of the running scan for one item and types. * * @param string $scan_id Scan id. * @param string $module Module id. * @param string $item Item. * @param string[] $types Types. * @return int */ public static function count_types( $scan_id, $module, $item, array $types ) { global $wpdb; $t = Schema::tables(); $place = implode( ',', array_fill( 0, count( $types ), '%s' ) ); $args = array_merge( array( $scan_id, $module, $item ), $types ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$t['findings']} WHERE scan_id = %s AND module = %s AND item = %s AND type IN ($place)", $args ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * Findings of the last completed scan of the given modules, with ignore state. * * @param string[] $modules Module ids. * @param string $view 'active' or 'ignored'. * @param int $limit Max rows. * @param int $offset Offset. * @return array{rows: array, total: int} */ public static function query( array $modules, $view = 'active', $limit = 200, $offset = 0 ) { global $wpdb; $t = Schema::tables(); $scope = self::scope( self::completed_scans( $modules ) ); if ( '' === $scope ) { return array( 'rows' => array(), 'total' => 0, ); } $ignored = 'ignored' === $view ? 'IS NOT NULL' : 'IS NULL'; $from = "FROM {$t['findings']} f LEFT JOIN {$t['ignores']} i ON i.module = f.module AND i.path_key = SHA1(CONCAT(f.item, '|', f.path)) AND i.hash = f.hash WHERE ($scope) AND i.id $ignored"; $order = "ORDER BY FIELD(f.severity, 'high', 'medium', 'low'), f.module, f.item, f.path"; // $scope is built from prepare()d fragments in scope(); table names come from $wpdb->prefix. $total = (int) $wpdb->get_var( "SELECT COUNT(*) $from" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $rows = $wpdb->get_results( $wpdb->prepare( "SELECT f.*, i.id AS ignore_id $from $order LIMIT %d OFFSET %d", $limit, $offset ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter foreach ( $rows as &$row ) { $row['details'] = json_decode( (string) $row['details'], true ); if ( ! is_array( $row['details'] ) ) { $row['details'] = array(); } } unset( $row ); return array( 'rows' => $rows, 'total' => $total, ); } /** * Counts of active (not ignored) findings per module and severity. * * @return array> */ public static function counts() { global $wpdb; $t = Schema::tables(); $scope = self::scope( self::completed_scans( array( 'core', 'plugins', 'themes', 'updates', 'database' ) ) ); $out = array(); if ( '' === $scope ) { return $out; } // $scope is built from prepare()d fragments in scope(). $rows = $wpdb->get_results( "SELECT f.module, f.severity, COUNT(*) AS n FROM {$t['findings']} f LEFT JOIN {$t['ignores']} i ON i.module = f.module AND i.path_key = SHA1(CONCAT(f.item, '|', f.path)) AND i.hash = f.hash WHERE ($scope) AND i.id IS NULL GROUP BY f.module, f.severity", ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter foreach ( $rows as $row ) { $out[ $row['module'] ][ $row['severity'] ] = (int) $row['n']; } return $out; } /** * SQL condition "rows of the last completed scan of each module", from prepared fragments. * * @param array $scans module => scan id. * @return string '' when there is nothing to show. */ private static function scope( array $scans ) { global $wpdb; $parts = array(); foreach ( $scans as $module => $scan_id ) { $parts[] = $wpdb->prepare( '(f.module = %s AND f.scan_id = %s)', $module, $scan_id ); } return implode( ' OR ', $parts ); } /** * Ignore a finding (by its path and current hash). * * @param int $finding_id Finding id. * @return bool */ public static function ignore( $finding_id ) { global $wpdb; $t = Schema::tables(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT module, item, path, hash FROM {$t['findings']} WHERE id = %d", $finding_id ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter if ( ! $row ) { return false; } return false !== $wpdb->replace( // phpcs:ignore WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter $t['ignores'], array( 'module' => $row['module'], 'path_key' => sha1( $row['item'] . '|' . $row['path'] ), 'path' => $row['path'], 'hash' => $row['hash'], 'user_id' => get_current_user_id(), 'created' => current_time( 'mysql', true ), ), array( '%s', '%s', '%s', '%s', '%d', '%s' ) ); } /** * Remove an ignore. * * @param int $ignore_id Ignore id. * @return bool */ public static function unignore( $ignore_id ) { global $wpdb; $t = Schema::tables(); return (bool) $wpdb->delete( $t['ignores'], array( 'id' => (int) $ignore_id ), array( '%d' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * Scan ids of the last completed scan per module. * * @param string[] $modules Module ids. * @return array */ private static function completed_scans( array $modules ) { $last = Scan\Runner::last_results(); $out = array(); foreach ( $modules as $module ) { // scan_id is the last scan that completed without error; a failed scan keeps the previous one. if ( ! empty( $last[ $module ]['scan_id'] ) ) { $out[ $module ] = $last[ $module ]['scan_id']; } } return $out; } }