*/ const MODULES = array( 'updates' => UpdatesModule::class, 'core' => CoreModule::class, 'plugins' => PluginsModule::class, 'themes' => ThemesModule::class, 'database' => DatabaseModule::class, ); /** * Start a job unless one is already running. * * @param string[] $modules Module ids. * @param string $trigger manual|cron. * @return array Job. */ public static function start( array $modules, $trigger = 'manual' ) { $job = self::job(); if ( $job ) { return $job; } $modules = array_values( array_intersect( array_keys( self::MODULES ), $modules ) ); if ( ! $modules ) { $modules = array_keys( self::MODULES ); } $job = array( 'id' => bin2hex( random_bytes( 8 ) ), 'modules' => $modules, 'index' => 0, 'state' => null, 'stats' => array(), 'progress' => array( 'done' => 0, 'total' => 0, 'label' => '', ), 'trigger' => $trigger, 'started' => time(), 'updated' => time(), ); update_option( self::JOB_OPTION, $job, false ); // Cron keeps the job going if the browser tab is closed. if ( ! wp_next_scheduled( self::CRON_TICK ) ) { wp_schedule_single_event( time() + 120, self::CRON_TICK ); } return $job; } /** * Running job or null. * * @return array|null */ public static function job() { $job = get_option( self::JOB_OPTION ); return is_array( $job ) && ! empty( $job['id'] ) ? $job : null; } /** * Work on the running job for one time budget. * * @return array Status for the screen. */ public static function tick() { $job = self::job(); if ( ! $job ) { return array( 'status' => 'idle' ); } if ( get_transient( self::LOCK ) ) { return self::status( $job, 'busy' ); } set_transient( self::LOCK, 1, 90 ); $deadline = microtime( true ) + self::budget(); $total = count( $job['modules'] ); while ( $job['index'] < $total ) { $id = $job['modules'][ $job['index'] ]; $class = self::MODULES[ $id ]; $module = new $class(); $ctx = new Context( $job['id'], $id, $deadline ); $done = false; $error = ''; try { if ( null === $job['state'] ) { Findings::discard( $id, $job['id'] ); $job['state'] = $module->start( $ctx ); } $state = $job['state']; $done = $module->step( $state, $ctx ); $job['state'] = $state; } catch ( \Throwable $e ) { $done = true; $error = $e instanceof \RuntimeException ? $e->getMessage() : __( 'Unexpected error during the scan.', 'safetybis-security' ); } $job['stats'][ $id ] = self::merge_stats( isset( $job['stats'][ $id ] ) ? $job['stats'][ $id ] : array(), $ctx->stats ); $job['progress'] = $ctx->progress; if ( $done ) { self::finish_module( $id, $job, $error ); $job['index']++; $job['state'] = null; } $job['updated'] = time(); update_option( self::JOB_OPTION, $job, false ); if ( ! $done || microtime( true ) >= $deadline ) { break; } } delete_transient( self::LOCK ); if ( $job['index'] >= $total ) { delete_option( self::JOB_OPTION ); do_action( 'safetybis_security_scan_finished', $job ); return array( 'status' => 'done' ); } return self::status( $job, 'running' ); } /** * Stop the running job; results of the previous scans stay. */ public static function cancel() { $job = self::job(); if ( $job && isset( $job['modules'][ $job['index'] ] ) ) { Findings::discard( $job['modules'][ $job['index'] ], $job['id'] ); } delete_option( self::JOB_OPTION ); delete_transient( self::LOCK ); wp_clear_scheduled_hook( self::CRON_TICK ); } /** * WP-Cron tick: continue the job and plan the next tick. */ public static function cron_tick() { $job = self::job(); if ( ! $job ) { return; } // Leave the job to the open admin screen while it is active. if ( 'manual' === $job['trigger'] && time() - (int) $job['updated'] < 60 ) { wp_schedule_single_event( time() + 120, self::CRON_TICK ); return; } $status = self::tick(); if ( 'done' !== $status['status'] ) { wp_schedule_single_event( time() + 60, self::CRON_TICK ); } } /** * Last result per module. * * @return array */ public static function last_results() { $last = get_option( self::LAST_OPTION, array() ); return is_array( $last ) ? $last : array(); } /** * Public status of a job. * * @param array $job Job. * @param string $status running|busy. * @return array */ public static function status( array $job, $status = 'running' ) { $count = max( 1, count( $job['modules'] ) ); $part = 0.0; if ( ! empty( $job['progress']['total'] ) ) { $part = min( 1, $job['progress']['done'] / $job['progress']['total'] ); } $module = isset( $job['modules'][ $job['index'] ] ) ? $job['modules'][ $job['index'] ] : ''; return array( 'status' => $status, 'module' => $module, 'label' => (string) $job['progress']['label'], 'percent' => (int) floor( 100 * ( $job['index'] + $part ) / $count ), 'step' => $job['index'] + 1, 'steps' => $count, ); } /** * Store the module result. * * @param string $id Module id. * @param array $job Job. * @param string $error Error message or ''. */ private static function finish_module( $id, array $job, $error ) { $last = self::last_results(); $prev = isset( $last[ $id ] ) ? $last[ $id ] : array(); if ( '' === $error ) { Findings::commit( $id, $job['id'] ); $last[ $id ] = array( 'scan_id' => $job['id'], 'status' => 'ok', 'time' => time(), 'message' => '', 'stats' => isset( $job['stats'][ $id ] ) ? $job['stats'][ $id ] : array(), ); } else { Findings::discard( $id, $job['id'] ); $last[ $id ] = array( 'scan_id' => isset( $prev['scan_id'] ) ? $prev['scan_id'] : '', 'status' => 'error', 'time' => isset( $prev['time'] ) ? $prev['time'] : 0, 'message' => $error, 'stats' => isset( $prev['stats'] ) ? $prev['stats'] : array(), ); } update_option( self::LAST_OPTION, $last, false ); } /** * Add numeric counters of one step to the module totals. * * @param array $total Totals. * @param array $step Step counters. * @return array */ private static function merge_stats( array $total, array $step ) { foreach ( $step as $key => $value ) { $total[ $key ] = ( isset( $total[ $key ] ) ? (int) $total[ $key ] : 0 ) + (int) $value; } return $total; } /** * Seconds of work per request, below typical hosting limits. * * @return float */ private static function budget() { $max = (int) ini_get( 'max_execution_time' ); $budget = 8.0; if ( $max > 0 ) { $budget = min( $budget, max( 2.0, $max / 3 ) ); } return (float) apply_filters( 'safetybis_security_step_seconds', $budget ); } }