PageRenderTime 23ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/better-wp-security/core/modules/database-prefix/utility.php

https://gitlab.com/najomie/fit-hippie
PHP | 140 lines | 68 code | 58 blank | 14 comment | 12 complexity | 54da1ce4728c907c3751a090add185ae MD5 | raw file
  1. <?php
  2. final class ITSEC_Database_Prefix_Utility {
  3. public static function change_database_prefix() {
  4. global $wpdb;
  5. require_once( $GLOBALS['itsec_globals']['plugin_dir'] . 'core/lib/class-itsec-lib-config-file.php' );
  6. require_once( $GLOBALS['itsec_globals']['plugin_dir'] . 'core/lib/class-itsec-lib-file.php' );
  7. $response = array(
  8. 'errors' => array(),
  9. 'new_prefix' => false,
  10. );
  11. //suppress error messages due to timing
  12. // error_reporting( 0 );
  13. // @ini_set( 'display_errors', 0 );
  14. $check_prefix = true; //Assume the first prefix we generate is unique
  15. //generate a new table prefix that doesn't conflict with any other in use in the database
  16. while ( $check_prefix ) {
  17. $avail = 'abcdefghijklmnopqrstuvwxyz0123456789';
  18. //first character should be alpha
  19. $new_prefix = $avail[ mt_rand( 0, 25 ) ];
  20. //length of new prefix
  21. $prelength = mt_rand( 4, 9 );
  22. //generate remaning characters
  23. for ( $i = 0; $i < $prelength; $i ++ ) {
  24. $new_prefix .= $avail[ mt_rand( 0, 35 ) ];
  25. }
  26. //complete with underscore
  27. $new_prefix .= '_';
  28. $new_prefix = esc_sql( $new_prefix ); //just be safe
  29. $check_prefix = $wpdb->get_results( 'SHOW TABLES LIKE "' . $new_prefix . '%";', ARRAY_N ); //if there are no tables with that prefix in the database set checkPrefix to false
  30. }
  31. $config_file_path = ITSEC_Lib_Config_File::get_wp_config_file_path();
  32. $config = ITSEC_Lib_File::read( $config_file_path );
  33. if ( is_wp_error( $config ) ) {
  34. /* translators: 1: Specific error details */
  35. $response['errors'][] = new WP_Error( $confix->get_error_code(), sprintf( __( 'Unable to read the <code>wp-config.php</code> file in order to update the Database Prefix. Error details as follows: %1$s', 'better-wp-security' ), $config->get_error_message() ) );
  36. return $response;
  37. }
  38. $regex = '/(\$table_prefix\s*=\s*)([\'"]).+?\\2(\s*;)/';
  39. $config = preg_replace( $regex, "\${1}'$new_prefix'\${3}", $config );
  40. $write_result = ITSEC_Lib_File::write( $config_file_path, $config );
  41. if ( is_wp_error( $write_result ) ) {
  42. /* translators: 1: Specific error details */
  43. $response['errors'][] = new WP_Error( $confix->get_error_code(), sprintf( __( 'Unable to update the <code>wp-config.php</code> file in order to update the Database Prefix. Error details as follows: %1$s', 'better-wp-security' ), $config->get_error_message() ) );
  44. return $response;
  45. }
  46. $response['new_prefix'] = $new_prefix;
  47. $tables = $wpdb->get_results( 'SHOW TABLES LIKE "' . $wpdb->base_prefix . '%"', ARRAY_N ); //retrieve a list of all tables in the DB
  48. //Rename each table
  49. foreach ( $tables as $table ) {
  50. $table = substr( $table[0], strlen( $wpdb->base_prefix ), strlen( $table[0] ) ); //Get the table name without the old prefix
  51. //rename the table and generate an error if there is a problem
  52. if ( $wpdb->query( 'RENAME TABLE `' . $wpdb->base_prefix . $table . '` TO `' . $new_prefix . $table . '`;' ) === false ) {
  53. $response['errors'][] = new WP_Error( 'itsec-database-prefix-utility-change-database-prefix-failed-table-rename', sprintf( __( 'Could not rename table %1$s. You may have to rename the table manually.', 'better-wp-security' ), $wpdb->base_prefix . $table ) );
  54. }
  55. }
  56. if ( is_multisite() ) { //multisite requires us to rename each blogs' options
  57. $blogs = $wpdb->get_col( "SELECT blog_id FROM `" . $new_prefix . "blogs` WHERE public = '1' AND archived = '0' AND mature = '0' AND spam = '0' ORDER BY blog_id DESC" ); //get list of blog id's
  58. if ( is_array( $blogs ) ) { //make sure there are other blogs to update
  59. //update each blog's user_roles option
  60. foreach ( $blogs as $blog ) {
  61. $wpdb->query( 'UPDATE `' . $new_prefix . $blog . '_options` SET option_name = "' . $new_prefix . $blog . '_user_roles" WHERE option_name = "' . $wpdb->base_prefix . $blog . '_user_roles" LIMIT 1;' );
  62. }
  63. }
  64. }
  65. $upOpts = $wpdb->query( 'UPDATE `' . $new_prefix . 'options` SET option_name = "' . $new_prefix . 'user_roles" WHERE option_name = "' . $wpdb->base_prefix . 'user_roles" LIMIT 1;' ); //update options table and set flag to false if there's an error
  66. if ( $upOpts === false ) { //set an error
  67. $response['errors'][] = new WP_Error( 'itsec-database-prefix-utility-change-database-prefix-failed-options-update', __( 'Could not update prefix references in options table.', 'better-wp-security' ) );
  68. }
  69. $rows = $wpdb->get_results( 'SELECT * FROM `' . $new_prefix . 'usermeta`' ); //get all rows in usermeta
  70. //update all prefixes in usermeta
  71. foreach ( $rows as $row ) {
  72. if ( substr( $row->meta_key, 0, strlen( $wpdb->base_prefix ) ) == $wpdb->base_prefix ) {
  73. $pos = $new_prefix . substr( $row->meta_key, strlen( $wpdb->base_prefix ), strlen( $row->meta_key ) );
  74. $result = $wpdb->query( 'UPDATE `' . $new_prefix . 'usermeta` SET meta_key="' . $pos . '" WHERE meta_key= "' . $row->meta_key . '" LIMIT 1;' );
  75. if ( $result == false ) {
  76. $response['errors'][] = new WP_Error( 'itsec-database-prefix-utility-change-database-prefix-failed-usermeta-update', __( 'Could not update prefix references in usermeta table.', 'better-wp-security' ) );
  77. }
  78. }
  79. }
  80. return $response;
  81. }
  82. }