/** * Modify the WordPress configuration file and change the keys that were defined * by a new random-generated list of keys retrieved from the official WordPress * API. The result of the operation will be either FALSE in case of error, or an * array containing multiple indexes explaining the modification, among them you * will find the old and new keys. * * @return false|array Either FALSE in case of error, or an array with the old and new keys. */ public static function set_new_config_keys() { $new_wpconfig = ''; $config_path = self::get_wpconfig_path(); if ($config_path) { $pattern = self::secret_key_pattern(); $define_tpl = "define('%s',%s'%s');"; $config_lines = SucuriScanFileInfo::file_lines($config_path); $new_keys = SucuriScanAPI::getNewSecretKeys(); $old_keys = array(); $old_keys_string = ''; $new_keys_string = ''; foreach ((array) $config_lines as $config_line) { if (preg_match($pattern, $config_line, $match)) { $key_name = $match[1]; if (array_key_exists($key_name, $new_keys)) { $white_spaces = $match[2]; $old_keys[$key_name] = $match[3]; $config_line = sprintf($define_tpl, $key_name, $white_spaces, $new_keys[$key_name]); $old_keys_string .= sprintf($define_tpl, $key_name, $white_spaces, $old_keys[$key_name]) . "\n"; $new_keys_string .= $config_line . "\n"; } } $new_wpconfig .= $config_line . "\n"; } $response = array('updated' => is_writable($config_path), 'old_keys' => $old_keys, 'old_keys_string' => $old_keys_string, 'new_keys' => $new_keys, 'new_keys_string' => $new_keys_string, 'new_wpconfig' => $new_wpconfig); if ($response['updated']) { file_put_contents($config_path, $new_wpconfig, LOCK_EX); } return $response; } return false; }