*/ public static function targets() { global $wpdb; return array( 'posts' => array( 'table' => $wpdb->posts, 'pk' => 'ID', 'columns' => array( 'post_title', 'post_content', 'post_excerpt' ), 'where' => "post_type NOT IN ('revision', 'nav_menu_item', 'customize_changeset', 'oembed_cache', 'wp_global_styles')", 'label' => __( 'Posts and pages', 'safetybis-security' ), ), 'postmeta' => array( 'table' => $wpdb->postmeta, 'pk' => 'meta_id', 'columns' => array( 'meta_value' ), 'where' => "meta_key NOT IN ('_edit_lock', '_edit_last', '_wp_attachment_metadata', '_wp_attached_file', '_thumbnail_id', '_wp_old_slug')", 'label' => __( 'Post metadata', 'safetybis-security' ), ), 'options' => array( 'table' => $wpdb->options, 'pk' => 'option_id', 'columns' => array( 'option_value' ), 'where' => "option_name NOT LIKE '\\_transient\\_%' AND option_name NOT LIKE '\\_site\\_transient\\_%' AND option_name NOT LIKE 'safetybis\\_security\\_%' AND option_name NOT IN ('cron', 'rewrite_rules', 'recently_edited', 'active_plugins', 'uninstall_plugins')", 'label' => __( 'Settings (options)', 'safetybis-security' ), ), 'comments' => array( 'table' => $wpdb->comments, 'pk' => 'comment_ID', 'columns' => array( 'comment_content', 'comment_author_url' ), 'where' => "comment_approved NOT IN ('spam', 'trash')", 'label' => __( 'Comments', 'safetybis-security' ), ), 'users' => array( 'table' => $wpdb->users, 'pk' => 'ID', 'columns' => array( 'user_url' ), 'where' => '1=1', 'label' => __( 'User profiles', 'safetybis-security' ), ), ); } /** * Start. * * @param Context $ctx Context. * @return array * @throws \RuntimeException When no list is available. */ public function start( Context $ctx ) { $list = DetectionList::current(); if ( ! $list['categories'] ) { throw new \RuntimeException( esc_html__( 'The detection list could not be loaded or its signature is not valid.', 'safetybis-security' ) ); } global $wpdb; $max = array(); foreach ( self::targets() as $key => $t ) { $max[ $key ] = (int) $wpdb->get_var( "SELECT MAX({$t['pk']}) FROM {$t['table']}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter } $ctx->stats['list_version'] = $list['version']; return array( 'target' => 'posts', 'cursor' => 0, 'max' => $max, ); } /** * Step. * * @param array $state State. * @param Context $ctx Context. * @return bool */ public function step( array &$state, Context $ctx ) { global $wpdb; $list = DetectionList::current(); $matcher = new Matcher( $list['categories'], (string) wp_parse_url( home_url(), PHP_URL_HOST ), (array) Settings::get( 'skip_categories' ) ); $targets = self::targets(); $keys = array_keys( $targets ); while ( null !== $state['target'] ) { $key = $state['target']; $t = $targets[ $key ]; $this->progress( $ctx, $state, $keys ); $cols = implode( ', ', array_merge( array( $t['pk'] ), $t['columns'], 'postmeta' === $key ? array( 'post_id', 'meta_key' ) : array(), 'options' === $key ? array( 'option_name' ) : array(), 'comments' === $key ? array( 'comment_post_ID' ) : array(), 'posts' === $key ? array( 'post_type', 'post_status' ) : array() ) ); // The fixed filter contains LIKE wildcards: escape "%" before prepare(). $where = str_replace( '%', '%%', $t['where'] ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT $cols FROM {$t['table']} WHERE {$t['pk']} > %d AND $where ORDER BY {$t['pk']} ASC LIMIT %d", $state['cursor'], self::CHUNK ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery, PluginCheck.Security.DirectDB.UnescapedDBParameter if ( ! $rows ) { $next = array_search( $key, $keys, true ) + 1; $state['target'] = isset( $keys[ $next ] ) ? $keys[ $next ] : null; $state['cursor'] = 0; if ( $ctx->out_of_time() ) { break; } continue; } foreach ( $rows as $row ) { $id = (int) $row[ $t['pk'] ]; foreach ( $t['columns'] as $column ) { foreach ( $matcher->scan( (string) $row[ $column ] ) as $hit ) { $path = $key . ':' . $id . ':' . $column; $ctx->add( $hit['category'], $matcher->severity( $hit['category'] ), $key, $path, md5( $hit['category'] . '|' . $hit['term'] . '|' . $path ), array( 'table' => $key, 'id' => $id, 'column' => $column, 'kind' => $hit['kind'], 'term' => $hit['term'], 'snippet' => $hit['snippet'], 'object' => self::object( $key, $row ), ) ); } } $state['cursor'] = $id; $ctx->count( 'rows' ); } if ( $matcher->regex_errors ) { $ctx->count( 'regex_errors', $matcher->regex_errors ); $matcher->regex_errors = 0; } if ( $ctx->out_of_time() ) { break; } } $this->progress( $ctx, $state, $keys ); return null === $state['target']; } /** * Information to build a link to the object on the screen. * * @param string $key Target key. * @param array $row Row. * @return array */ private static function object( $key, array $row ) { switch ( $key ) { case 'posts': return array( 'post_id' => (int) $row['ID'], 'post_type' => (string) $row['post_type'], 'status' => (string) $row['post_status'], 'title' => wp_strip_all_tags( (string) $row['post_title'] ), ); case 'postmeta': return array( 'post_id' => (int) $row['post_id'], 'meta_key' => (string) $row['meta_key'], // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key ); case 'options': return array( 'option_name' => (string) $row['option_name'] ); case 'comments': return array( 'comment_id' => (int) $row['comment_ID'], 'post_id' => (int) $row['comment_post_ID'], ); case 'users': return array( 'user_id' => (int) $row['ID'] ); } return array(); } /** * Progress: tables done plus position in the current table. * * @param Context $ctx Context. * @param array $state State. * @param array $keys Target keys. */ private function progress( Context $ctx, array $state, array $keys ) { $targets = self::targets(); if ( null === $state['target'] ) { $ctx->progress = array( 'done' => count( $keys ), 'total' => count( $keys ), 'label' => __( 'Database', 'safetybis-security' ), ); return; } $index = (int) array_search( $state['target'], $keys, true ); $max = max( 1, (int) $state['max'][ $state['target'] ] ); $ctx->progress = array( 'done' => $index + min( 1, $state['cursor'] / $max ), 'total' => count( $keys ), 'label' => $targets[ $state['target'] ]['label'], ); } }