rtrim( $uploads_basis, '/\\' ) . '/aigeschikt-bot-spool', 'secret' => ( defined( 'AUTH_KEY' ) && defined( 'SECURE_AUTH_KEY' ) && '' !== AUTH_KEY && '' !== SECURE_AUTH_KEY ) ? substr( hash_hmac( 'sha256', 'aigeschikt-spool', AUTH_KEY . SECURE_AUTH_KEY ), 0, 32 ) : substr( hash( 'sha256', 'aigeschikt-spool-geen-auth-key' ), 0, 32 ), ); if ( empty( $config['spool_dir'] ) || empty( $config['secret'] ) || ! preg_match( '/^[a-f0-9]{24,64}$/', $config['secret'] ) ) { return; } // 3b. De zekering — PER REQUEST (0.8.8). $spool_dir = rtrim( $config['spool_dir'], '/\\' ); $secret = $config['secret']; $prefix = 'breaker-' . $secret; $plen = strlen( $prefix ); $stale = 60; $pause_for = 600; $now = time(); if ( ! function_exists( 'opendir' ) || ! function_exists( 'readdir' ) ) { return; } $dh = @opendir( $spool_dir ); if ( ! $dh ) { return; } $paused = false; $seen = 0; $retired = 0; while ( false !== ( $name = readdir( $dh ) ) ) { if ( 0 !== strncmp( $name, $prefix, $plen ) || '.flag' !== substr( $name, -5 ) ) { continue; } $file = $spool_dir . '/' . $name; $mtime = @filemtime( $file ); if ( ! is_int( $mtime ) || $mtime <= 0 ) { continue; } $age = $now - $mtime; if ( $age >= $stale + $pause_for ) { if ( @unlink( $file ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink,WordPress.PHP.NoSilencedErrors -- runs before WordPress is loaded; wp_delete_file() does not exist yet. ++$retired; continue; } } elseif ( $age >= $stale ) { $paused = true; break; } if ( ++$seen >= 64 ) { $paused = true; break; } } closedir( $dh ); if ( $retired > 0 ) { @file_put_contents( $spool_dir . '/trips-' . $secret . '.log', str_repeat( '.', $retired ), FILE_APPEND ); } if ( $paused ) { return; } $https = ! empty( $_SERVER['HTTPS'] ) && 'off' !== strtolower( $schoon( $_SERVER['HTTPS'], 8 ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- cleaned by $schoon(); WordPress is not loaded yet, so wp_unslash()/sanitize_*() do not exist. $host = isset( $_SERVER['HTTP_HOST'] ) ? $schoon( $_SERVER['HTTP_HOST'], 255 ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- cleaned by $schoon(); WordPress is not loaded yet, so wp_unslash()/sanitize_*() do not exist. $event = array( 'ts' => gmdate( 'Y-m-d H:i:s' ), 'family' => $bot['family'], 'name' => $bot['name'], 'category' => $bot['category'], 'ip' => isset( $_SERVER['REMOTE_ADDR'] ) ? $schoon( $_SERVER['REMOTE_ADDR'], 45 ) : '', // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- cleaned by $schoon(); WordPress is not loaded yet, so wp_unslash()/sanitize_*() do not exist. 'url' => substr( ( $https ? 'https://' : 'http://' ) . $host . $uri, 0, 2000 ), 'method' => $method, ); try { $token = bin2hex( random_bytes( 8 ) ); } catch ( \Throwable $e ) { $token = substr( md5( uniqid( '', true ) ), 0, 16 ); } $own = $spool_dir . '/' . $prefix . '-' . $token . '.flag'; if ( ! @touch( $own, $now ) && ! @is_file( $own ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch,WordPress.PHP.NoSilencedErrors -- runs before WordPress is loaded; WP_Filesystem does not exist yet. return; } register_shutdown_function( function () use ( $event, $spool_dir, $secret, $own ) { try { if ( ! @is_dir( $spool_dir ) ) { return; } $status = function_exists( 'http_response_code' ) ? http_response_code() : 200; $event['status'] = ( is_int( $status ) && $status > 0 ) ? $status : 200; $spool = $spool_dir . '/events-' . $secret . '.ndjson'; $dropped = $spool_dir . '/dropped-' . $secret . '.log'; $size = @filesize( $spool ); $line = @json_encode( $event, JSON_INVALID_UTF8_SUBSTITUTE ); $vol = is_int( $size ) && $size >= 2097152; if ( ! $vol && is_string( $line ) && '' !== $line && false !== @file_put_contents( $spool, $line . "\n", FILE_APPEND | LOCK_EX ) ) { return; } $dsize = @filesize( $dropped ); if ( ! is_int( $dsize ) || $dsize < 1048576 ) { @file_put_contents( $dropped, '.', FILE_APPEND | LOCK_EX ); } } catch ( \Throwable $e ) { return; } finally { @unlink( $own ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink,WordPress.PHP.NoSilencedErrors -- runs before WordPress is loaded. } } ); if ( ! defined( 'AIGESCHIKT_EARLY_RAN' ) ) { define( 'AIGESCHIKT_EARLY_RAN', true ); } } catch ( \Throwable $e ) { return; } } );