OwlCyberSecurity - MANAGER
Edit File: wp-cli-helper.php
<?php if (!defined('WP_CLI') || !WP_CLI) { return; } require_once __DIR__ . '/wp-cli-helper-prefix.php'; $wpCliTimeout = max( (int) getenv('WP_CLI_COMMAND_TIMEOUT'), 0 ) ?: 600; ini_set('max_execution_time', $wpCliTimeout); define('WPAAS_MHP_CLI_HELPER_BOUNDARY_START', '////////WPAAS_MHP_START////////'); define('WPAAS_MHP_CLI_HELPER_BOUNDARY_END', '////////WPAAS_MHP_END////////'); /** * wp-cli cmds to add boundary to */ $boundaryWpCliCmds = [ 'cache', 'cli', 'config', 'core', 'cron', 'db', 'godaddy', 'nux', 'option', 'plugin', 'post', 'rewrite', 'search', 'theme', 'user', 'wpaas', ]; // Get error key function wpaas_mhp_cli_helper_get_error_key($errno, $errstr) { // Maybe generalize the unserialize offset notices. if ((E_NOTICE === $errno || E_WARNING === $errno) && false !== strpos($errstr, 'unserialize(): Error at offset')) { $errstr = 'unserialize(): Error at offset X of Y bytes (generalized)'; } return "{$errno}-{$errstr}"; } function wpaas_mhp_cli_helper_error_handler($errno, $errstr, $errfile = null, $errline = null) { static $errorCounter = []; static $errorTypeCounter = []; $errorKey = wpaas_mhp_cli_helper_get_error_key($errno, $errstr); if (!isset($errorCounter[$errorKey])) { $errorCounter[$errorKey] = 0; } if (!isset($errorTypeCounter[$errno])) { $errorTypeCounter[$errno] = 0; } // Swallow the error if there are large numbers of the same one. if (20 < $errorCounter[$errorKey]) { return; } // Swallow the error if there are large numbers of the same type. if (100 < $errorTypeCounter[$errno]) { return; } $errorCounter[$errorKey]++; $errorTypeCounter[$errno]++; $formattedError = wpaas_mhp_cli_helper_format_error($errno, $errstr, $errfile, $errline); fwrite(STDERR, $formattedError); $isFatal = $errno & (E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR); if ($isFatal) { exit(1); } } set_error_handler('wpaas_mhp_cli_helper_error_handler'); foreach ($boundaryWpCliCmds as $cmd) { WP_CLI::add_hook('before_invoke:' . $cmd, static function () { fwrite(STDOUT, WPAAS_MHP_CLI_HELPER_BOUNDARY_START . PHP_EOL); }); WP_CLI::add_hook('after_invoke:' . $cmd, static function () { fwrite(STDOUT, PHP_EOL . WPAAS_MHP_CLI_HELPER_BOUNDARY_END); }); } function wpaas_mhp_cli_helper_format_error($errno, $errstr, $errfile = null, $errline = null) { $errType = wpaas_mhp_cli_helper_error_type($errno); return "{$errType}({$errno}): {$errstr} {$errfile}#{$errline}" . PHP_EOL; } function wpaas_mhp_cli_helper_error_type($errno) { switch ($errno) { case E_ERROR: return 'Error'; case E_WARNING: return 'Warning'; case E_PARSE: return 'Parse'; case E_NOTICE: return 'Notice'; case E_CORE_ERROR: return 'Core Error'; case E_CORE_WARNING: return 'Core Warning'; case E_COMPILE_ERROR: return 'Compile Error'; case E_COMPILE_WARNING: return 'Compile Warning'; case E_DEPRECATED: return 'Deprecated'; case E_USER_ERROR: return 'User Error'; case E_USER_WARNING: return 'User Warning'; case E_USER_NOTICE: return 'User Notice'; case E_USER_DEPRECATED: return 'User Deprecated'; case E_STRICT: return 'Strict'; default: return 'Unknown'; } } /** * Backup Prime Mover constants file (WPVTWO-6946). * * The Prime Mover plugin creates wp-content/mu-plugins/000-prime-mover-constants.php * with hardcoded paths that cause breakage during domain modifications. * This function backs up the file before domain changes. * * @return array Status information with keys: success, message, backup_filename (optional) */ function wpaas_mhp_cli_helper_backup_prime_mover_constants() { $filename = '000-prime-mover-constants.php'; $file = ABSPATH . 'wp-content/mu-plugins/' . $filename; $timestamp = time(); $backup_filename = $filename . '.' . $timestamp . '.bak'; $backup_file = ABSPATH . 'wp-content/mu-plugins/' . $backup_filename; if (file_exists($file)) { if (@rename($file, $backup_file)) { return [ 'success' => true, 'message' => "Backed up $filename to $backup_filename", 'backup_filename' => $backup_filename, ]; } else { $error = error_get_last(); $errorMessage = (is_array($error) && isset($error['message'])) ? $error['message'] : 'Unknown error'; return [ 'success' => false, 'message' => "Failed to backup $filename", 'error' => $errorMessage, ]; } } return [ 'success' => true, 'message' => "File $filename does not exist, no action needed", ]; } /** * Restore Prime Mover constants file from backup (WPVTWO-6946). * * Restores the Prime Mover constants file from its backup after domain modifications. * * @param string $backup_filename Filename only (no path) of the backup file to restore (e.g., 000-prime-mover-constants.php.1234567890.bak) * @return array Status information with keys: success, message */ function wpaas_mhp_cli_helper_restore_prime_mover_constants($backup_filename) { if (empty($backup_filename)) { return [ 'success' => false, 'message' => 'No backup filename specified', ]; } // Sanitize: ensure it only contains the filename, no path separators if (strpos($backup_filename, '/') !== false || strpos($backup_filename, '\\') !== false || strpos($backup_filename, '..') !== false) { return [ 'success' => false, 'message' => 'Invalid backup filename: must not contain path separators', ]; } // Validate filename format: should be 000-prime-mover-constants.php.{timestamp}.bak if (!preg_match('/^000-prime-mover-constants\.php\.\d+\.bak$/', $backup_filename)) { return [ 'success' => false, 'message' => 'Invalid backup filename format', ]; } $original_filename = '000-prime-mover-constants.php'; $original_file = ABSPATH . 'wp-content/mu-plugins/' . $original_filename; $backup_file = ABSPATH . 'wp-content/mu-plugins/' . $backup_filename; if (!file_exists($backup_file)) { return [ 'success' => false, 'message' => "Backup file $backup_filename does not exist", ]; } if (@rename($backup_file, $original_file)) { return [ 'success' => true, 'message' => "Restored $original_filename from $backup_filename", ]; } else { $error = error_get_last(); $errorMessage = (is_array($error) && isset($error['message'])) ? $error['message'] : 'Unknown error'; return [ 'success' => false, 'message' => "Failed to restore $original_filename from $backup_filename", 'error' => $errorMessage, ]; } }