PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/backupbuddy/views/_server_tools-database_replace.php

https://bitbucket.org/summitds/bloomsburgpa.org
PHP | 166 lines | 116 code | 32 blank | 18 comment | 13 complexity | d08f9ecfb417f245cd4816f519027852 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. if ( !is_admin() ) { die( 'Access Denied.' ); }
  3. echo '<p><b>Warning: This is an advanced feature. Use with caution; improper use may result in data loss.</b></p>';
  4. if ( pb_backupbuddy::_GET( 'database_replace' ) == '1' ) {
  5. global $pb_backupbuddy_js_status;
  6. $pb_backupbuddy_js_status = true;
  7. echo pb_backupbuddy::status_box( 'Mass replacing in database with Server Tools from BackupBuddy v' . pb_backupbuddy::settings( 'version' ) . '...' );
  8. echo '<div id="pb_importbuddy_working"><img src="' . pb_backupbuddy::plugin_url() . '/images/loading_large.gif" title="Working... Please wait as this may take a moment..."></div>';
  9. // Instantiate database replacement class.
  10. require_once( pb_backupbuddy::plugin_path() . '/lib/dbreplace/dbreplace.php' );
  11. $dbreplace = new pluginbuddy_dbreplace();
  12. // Set up variables by getting POST data.
  13. $needle = mysql_real_escape_string( pb_backupbuddy::_POST( 'needle' ) );
  14. if ( $needle == '' ) {
  15. echo '<b>Error #4456582. Missing needle. You must enter text to search for.';
  16. echo '<br><a href="' . pb_backupbuddy::page_url() . '#database_replace" class="button secondary-button">&larr; ' . __( 'back', 'it-l10n-backupbuddy' ) . '</a>';
  17. return;
  18. }
  19. $replacement = mysql_real_escape_string( pb_backupbuddy::_POST( 'replacement' ) );
  20. pb_backupbuddy::status( 'message', 'Replacing `' . $needle . '` with `' . $replacement . '`.' );
  21. /*
  22. if ( pb_backupbuddy::_POST( 'maybe_serialized' ) == 'true' ) {
  23. pb_backupbuddy::status( 'message', 'Accounting for serialized data based on settings.' );
  24. $maybe_serialized = true;
  25. } else {
  26. pb_backupbuddy::status( 'warning', 'NOT accounting for serialized data based on settings. Use with caution.' );
  27. $maybe_serialized = false;
  28. }
  29. */
  30. // Replace based on the type of table replacement selected.
  31. if ( pb_backupbuddy::_POST( 'table_selection' ) == 'all' ) { // All tables.
  32. pb_backupbuddy::status( 'message', 'Replacing in all tables based on settings.' );
  33. $tables = array();
  34. $result = mysql_query( 'SHOW TABLES' );
  35. while( $rs = mysql_fetch_row( $result ) ) {
  36. $tables[] = $rs[0];
  37. }
  38. mysql_free_result( $result ); // Free memory.
  39. $rows_changed = 0;
  40. foreach( $tables as $table ) {
  41. pb_backupbuddy::status( 'message', 'Replacing in table `' . $table . '`.' );
  42. $rows_changed += $dbreplace->bruteforce_table( $table, array( $needle ), array( $replacement ) );
  43. }
  44. pb_backupbuddy::status( 'message', 'Total rows updated across all tables: ' . $rows_changed . '.' );
  45. pb_backupbuddy::status( 'message', 'Replacement finished.' );
  46. } elseif ( pb_backupbuddy::_POST( 'table_selection' ) == 'single_table' ) {
  47. $table = mysql_real_escape_string( pb_backupbuddy::_POST( 'table' ) ); // Single specified table.
  48. pb_backupbuddy::status( 'message', 'Replacing in single table `' . $table . '` based on settings.' );
  49. $dbreplace->bruteforce_table( $table, array( $needle ), array( $replacement ) );
  50. pb_backupbuddy::status( 'message', 'Replacement finished.' );
  51. } elseif ( pb_backupbuddy::_POST( 'table_selection' ) == 'prefix' ) { // Matching table prefix.
  52. $prefix = mysql_real_escape_string( pb_backupbuddy::_POST( 'table_prefix' ) );
  53. pb_backupbuddy::status( 'message', 'Replacing in all tables matching prefix `' . $prefix . '`.' );
  54. $tables = array();
  55. $escaped_prefix = str_replace( '_', '\_', $prefix );
  56. $result = mysql_query( "SHOW TABLES LIKE '{$escaped_prefix}%'" );
  57. while( $rs = mysql_fetch_row( $result ) ) {
  58. $tables[] = $rs[0];
  59. }
  60. mysql_free_result( $result ); // Free memory.
  61. $rows_changed = 0;
  62. foreach( $tables as $table ) {
  63. pb_backupbuddy::status( 'message', 'Replacing in table `' . $table . '`.' );
  64. $rows_changed += $dbreplace->bruteforce_table( $table, array( $needle ), array( $replacement ) );
  65. }
  66. pb_backupbuddy::status( 'message', 'Total rows updated across all tables: ' . $rows_changed . '.' );
  67. pb_backupbuddy::status( 'message', 'Replacement finished.' );
  68. } else {
  69. die( 'Error #4456893489349834. Unknown method.' );
  70. }
  71. echo '<br><a href="' . pb_backupbuddy::page_url() . '#database_replace" class="button secondary-button">&larr; ' . __( 'back', 'it-l10n-backupbuddy' ) . '</a>';
  72. $pb_backupbuddy_js_status = false;
  73. return;
  74. }
  75. echo '<p><b>Tip:</b> When replacing a site address there may be more than one URL. Ie. http://site.com, http://www.site.com, https://site.com, etc.</p>';
  76. $tables = array();
  77. $prefixes = array();
  78. // Make sure this WP's prefix is in there for sure (useful if someone uses a prefix that has an underscore in it; they shouldnt but they do).
  79. global $table_prefix;
  80. $prefixes[] = $table_prefix;
  81. // Calculate prefixes foudn in this database. Does not handle multiple-underscore
  82. $result = mysql_query( 'SHOW TABLES' );
  83. while( $rs = mysql_fetch_row( $result ) ) {
  84. $tables[] = $rs[0];
  85. if ( preg_match( '/[a-zA-Z0-9]*_([0-9]+_)*/i', $rs[0], $matches ) ) {
  86. $prefixes[] = $matches[0];
  87. }
  88. }
  89. mysql_free_result( $result ); // Free memory.
  90. $prefixes = array_unique( $prefixes );
  91. natsort( $prefixes );
  92. ?>
  93. <div>
  94. <form action="<?php echo pb_backupbuddy::page_url();?>&database_replace=1#pb_backupbuddy_getting_started_tab_tools" method="post">
  95. <input type="hidden" name="action" value="replace">
  96. <h4>Replace <?php pb_backupbuddy::tip( 'Text you want to be searched for and replaced. Everything in the box is considered one match and may span multiple lines.' ); ?></h4>
  97. <textarea name="needle" style="width: 100%;"></textarea>
  98. <br>
  99. <h4>With <?php pb_backupbuddy::tip( 'Text you want to replace with. Any text found matching the box above will be replaced with this text. Everything in the box is considered one match and may span multiple lines.' ); ?></h4>
  100. <textarea name="replacement" style="width: 100%;"></textarea>
  101. <h4>In table(s)</h4>
  102. <label for="table_selection_all"><input id="table_selection_all" checked='checked' type="radio" name="table_selection" value="all"> all tables</label>
  103. <label for="table_selection_prefix"><input id="table_selection_prefix" type="radio" name="table_selection" value="prefix"> with prefix:</label>
  104. <select name="table_prefix" id="table_selection_prefix" onclick="jQuery('#table_selection_prefix').click();">
  105. <?php
  106. foreach( $prefixes as $prefix ) {
  107. echo '<option value="' . $prefix . '">' . $prefix . '</option>';
  108. }
  109. ?>
  110. </select>
  111. <label for="table_selection_table"><input id="table_selection_table" type="radio" name="table_selection" value="single_table"> single:</label>
  112. <select name="table" id="table_selection_table" onclick="jQuery('#table_selection_table').click();">
  113. <?php
  114. foreach( $tables as $table ) {
  115. echo '<option value="' . $table . '">' . $table . '</option>';
  116. }
  117. ?>
  118. </select>
  119. <?php
  120. if ( substr_count( $table_prefix, '_' ) > 1 ) {
  121. echo '<span class="pb_label pb_label-warning">Warning</span> ';
  122. _e( "Site table prefix contains multiple underscores. Prefix list may be inaccurate if these are not Multisite subsites.", 'it-l10n-backupbuddy' );
  123. }
  124. /*
  125. <h4>With advanced options</h4>
  126. <label for="maybe_serialized"><input id="maybe_serialized" type="checkbox" name="maybe_serialized" value="true" checked="checked"> Treat fields as possibly containing serialized data (uncheck with caution; slower).</label>
  127. */
  128. ?><br><br>
  129. <p>
  130. <input type="submit" name="submit" value="Begin Replacement" class="button button-primary" /> <span class="description">Caution; this cannot be undone. Serialized data is handled by this replacement.</span>
  131. </p>
  132. </form>
  133. </div>
  134. <br>