Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 18th, 2012  |  syntax: None  |  size: 27.58 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  *
  4.  * Safe Search and Replace on Database with Serialized Data v2.0.1
  5.  *
  6.  * This script is to solve the problem of doing database search and replace when
  7.  * developers have only gone and used the non-relational concept of serializing
  8.  * PHP arrays into single database columns.  It will search for all matching
  9.  * data on the database and change it, even if it's within a serialized PHP
  10.  * array.
  11.  *
  12.  * The big problem with serialised arrays is that if you do a normal DB style
  13.  * search and replace the lengths get mucked up.  This search deals with the
  14.  * problem by unserializing and reserializing the entire contents of the
  15.  * database you're working on.  It then carries out a search and replace on the
  16.  * data it finds, and dumps it back to the database.  So far it appears to work
  17.  * very well.  It was coded for our WordPress work where we often have to move
  18.  * large databases across servers, but I designed it to work with any database.
  19.  * Biggest worry for you is that you may not want to do a search and replace on
  20.  * every damn table - well, if you want, simply add some exclusions in the table
  21.  * loop and you'll be fine.  If you don't know how, you possibly shouldn't be
  22.  * using this script anyway.
  23.  *
  24.  * To use, simply configure the settings below and off you go.  I wouldn't
  25.  * expect the script to take more than a few seconds on most machines.
  26.  *
  27.  * BIG WARNING!  Take a backup first, and carefully test the results of this
  28.  * code. If you don't, and you vape your data then you only have yourself to
  29.  * blame. Seriously.  And if you're English is bad and you don't fully
  30.  * understand the instructions then STOP. Right there. Yes. Before you do any
  31.  * damage.
  32.  *
  33.  * USE OF THIS SCRIPT IS ENTIRELY AT YOUR OWN RISK. I/We accept no liability
  34.  * from its use.
  35.  *
  36.  * First Written 2009-05-25 by David Coveney of Interconnect IT Ltd (UK)
  37.  * http://www.davidcoveney.com or http://www.interconnectit.com
  38.  * and released under the WTFPL
  39.  * ie, do what ever you want with the code, and we take no responsibility for it
  40.  * OK? If you don't wish to take responsibility, hire us at Interconnect IT Ltd
  41.  * on +44 (0)151 331 5140 and we will do the work for you, but at a cost,
  42.  * minimum 1hr
  43.  *
  44.  * To view the WTFPL go to http://sam.zoy.org/wtfpl/ (WARNING: it's a little
  45.  * rude, if you're sensitive);
  46.  *
  47.  *
  48.  * Version 2.1.0:
  49.  *              - Changed to version 2.1.0
  50.  *              * Following change by Sergei Biryukov - merged in and tested by Dave Coveney
  51.  *              - Added Charset Support (tested with UTF-8, not tested on other charsets)
  52.  *              * Following changes implemented by James Whitehead with thanks to all the commenters and feedback given!
  53.  *              - Removed PHP warnings if you go to step 3+ without DB details.
  54.  *              - Added options to skip changing the guid column. If there are other
  55.  *              columns that need excluding you can add them to the $exclude_cols global
  56.  *              array. May choose to add another option to the table select page to let
  57.  *              you add to this array from the front end.
  58.  *              - Minor tweak to label styling.
  59.  *              - Added comments to each of the functions.
  60.  *              - Removed a dead param from icit_srdb_replacer
  61.  * Version 2.0.0:
  62.  *              - returned to using unserialize function to check if string is
  63.  *              serialized or not
  64.  *              - marked is_serialized_string function as deprecated
  65.  *              - changed form order to improve usability and make use on multisites a
  66.  *              bit less scary
  67.  *              - changed to version 2, as really should have done when the UI was
  68.  *              introduced
  69.  *              - added a recursive array walker to deal with serialized strings being
  70.  *              stored in serialized strings. Yes, really.
  71.  *              - changes by James R Whitehead (kudos for recursive walker) and David
  72.  *              Coveney 2011-08-26
  73.  *  Version 1.0.2:
  74.  *      - typos corrected, button text tweak - David Coveney / Robert O'Rourke
  75.  *  Version 1.0.1
  76.  *      - styling and form added by James R Whitehead.
  77.  *
  78.  *  Credits:  moz667 at gmail dot com for his recursive_array_replace posted at
  79.  *            uk.php.net which saved me a little time - a perfect sample for me
  80.  *            and seems to work in all cases.
  81.  *
  82.  */
  83.  
  84.  
  85. /**
  86.  * Form action attribute.
  87.  *
  88.  * @return null
  89.  */
  90. function icit_srdb_form_action( ) {
  91.         global $step;
  92.         echo basename( __FILE__ ) . '?step=' . intval( $step + 1 );
  93. }
  94.  
  95.  
  96. /**
  97.  * Used to check the $_post tables array and remove any that don't exist.
  98.  *
  99.  * @param array $table The list of tables from the $_post var to be checked.
  100.  *
  101.  * @return array        Same array as passed in but with any tables that don'e exist removed.
  102.  */
  103. function check_table_array( $table = '' ){
  104.         global $all_tables;
  105.         return in_array( $table, $all_tables );
  106. }
  107.  
  108.  
  109. /**
  110.  * Simply create a submit button with a JS confirm popup if there is need.
  111.  *
  112.  * @param string $text    Button string.
  113.  * @param string $warning Submit warning pop up text.
  114.  *
  115.  * @return null
  116.  */
  117. function icit_srdb_submit( $text = 'Submit', $warning = '' ){
  118.         $warning = str_replace( "'", "\'", $warning ); ?>
  119.         <input type="submit" class="button" value="<?php echo htmlentities( $text, ENT_QUOTES, 'UTF-8' ); ?>" <?php echo ! empty( $warning ) ? 'onclick="if (confirm(\'' . htmlentities( $warning, ENT_QUOTES, 'UTF-8' ) . '\')){return true;}return false;"' : ''; ?>/> <?php
  120. }
  121.  
  122.  
  123. /**
  124.  * Simple html esc
  125.  *
  126.  * @param string $string Thing that needs escaping
  127.  * @param bool $echo   Do we echo or return?
  128.  *
  129.  * @return string    Escaped string.
  130.  */
  131. function esc_html_attr( $string = '', $echo = false ){
  132.         $output = htmlentities( $string, ENT_QUOTES, 'UTF-8' );
  133.         if ( $echo )
  134.                 echo $output;
  135.         else
  136.                 return $output;
  137. }
  138.  
  139.  
  140. /**
  141.  * Walk and array replacing one element for another. ( NOT USED ANY MORE )
  142.  *
  143.  * @param string $find    The string we want to replace.
  144.  * @param string $replace What we'll be replacing it with.
  145.  * @param array $data    Used to pass any subordinate arrays back to the
  146.  * function for searching.
  147.  *
  148.  * @return array    The original array with the replacements made.
  149.  */
  150. function recursive_array_replace( $find, $replace, $data ) {
  151.     if ( is_array( $data ) ) {
  152.         foreach ( $data as $key => $value ) {
  153.             if ( is_array( $value ) ) {
  154.                 recursive_array_replace( $find, $replace, $data[ $key ] );
  155.             } else {
  156.                 // have to check if it's string to ensure no switching to string for booleans/numbers/nulls - don't need any nasty conversions
  157.                 if ( is_string( $value ) )
  158.                                         $data[ $key ] = str_replace( $find, $replace, $value );
  159.             }
  160.         }
  161.     } else {
  162.         if ( is_string( $data ) )
  163.                         $data = str_replace( $find, $replace, $data );
  164.     }
  165. }
  166.  
  167.  
  168. /**
  169.  * Take a serialised array and unserialise it replacing elements as needed and
  170.  * unserialising any subordinate arrays and performing the replace on those too.
  171.  *
  172.  * @param string $from       String we're looking to replace.
  173.  * @param string $to         What we want it to be replaced with
  174.  * @param array  $data       Used to pass any subordinate arrays back to in.
  175.  * @param bool   $serialised Does the array passed via $data need serialising.
  176.  *
  177.  * @return array        The original array with all elements replaced as needed.
  178.  */
  179. function recursive_unserialize_replace( $from = '', $to = '', $data = '', $serialised = false ) {
  180.  
  181.         // some unseriliased data cannot be re-serialised eg. SimpleXMLElements
  182.         try {
  183.  
  184.                 if ( is_string( $data ) && ( $unserialized = @unserialize( $data ) ) !== false ) {
  185.                         $data = recursive_unserialize_replace( $from, $to, $unserialized, true );
  186.                 }
  187.  
  188.                 elseif ( is_array( $data ) ) {
  189.                         $_tmp = array( );
  190.                         foreach ( $data as $key => $value ) {
  191.                                 $_tmp[ $key ] = recursive_unserialize_replace( $from, $to, $value, false );
  192.                         }
  193.  
  194.                         $data = $_tmp;
  195.                         unset( $_tmp );
  196.                 }
  197.  
  198.                 else {
  199.                         if ( is_string( $data ) )
  200.                                 $data = str_replace( $from, $to, $data );
  201.                 }
  202.  
  203.                 if ( $serialised )
  204.                         return serialize( $data );
  205.  
  206.         } catch( Exception $error ) {
  207.  
  208.         }
  209.  
  210.         return $data;
  211. }
  212.  
  213.  
  214. /**
  215.  * Is the string we're dealing with a serialised string? ( NOT USED ANY MORE )
  216.  *
  217.  * @param string $data The string we want to check
  218.  *
  219.  * @return bool    true if serialised.
  220.  */
  221. function is_serialized_string( $data ) {
  222.         // if it isn't a string, it isn't a serialized string
  223.         if ( !is_string( $data ) )
  224.                 return false;
  225.         $data = trim( $data );
  226.         if ( preg_match( '/^s:[0-9]+:.*;$/s', $data ) ) // this should fetch all serialized strings
  227.                 return true;
  228.         return false;
  229. }
  230.  
  231.  
  232. /**
  233.  * The main loop triggered in step 5. Up here to keep it out of the way of the
  234.  * HTML. This walks every table in the db that was selected in step 3 and then
  235.  * walks every row and column replacing all occurences of a string with another.
  236.  * We split large tables into 50,000 row blocks when dealing with them to save
  237.  * on memmory consumption.
  238.  *
  239.  * @param mysql  $connection The db connection object
  240.  * @param string $search     What we want to replace
  241.  * @param string $replace    What we want to replace it with.
  242.  * @param array  $tables     The tables we want to look at.
  243.  *
  244.  * @return array    Collection of information gathered during the run.
  245.  */
  246. function icit_srdb_replacer( $connection, $search = '', $replace = '', $tables = array( ) ) {
  247.         global $guid, $exclude_cols;
  248.  
  249.         $report = array( 'tables' => 0,
  250.                                          'rows' => 0,
  251.                                          'change' => 0,
  252.                                          'updates' => 0,
  253.                                          'start' => microtime( ),
  254.                                          'end' => microtime( ),
  255.                                          'errors' => array( ),
  256.                                          );
  257.  
  258.         if ( is_array( $tables ) && ! empty( $tables ) ) {
  259.                 foreach( $tables as $table ) {
  260.                         $report[ 'tables' ]++;
  261.  
  262.                         $columns = array( );
  263.  
  264.                         // Get a list of columns in this table
  265.                     $fields = mysql_query( 'DESCRIBE ' . $table, $connection );
  266.                         while( $column = mysql_fetch_array( $fields ) )
  267.                                 $columns[ $column[ 'Field' ] ] = $column[ 'Key' ] == 'PRI' ? true : false;
  268.  
  269.                         // Count the number of rows we have in the table if large we'll split into blocks, This is a mod from Simon Wheatley
  270.                         $row_count = mysql_query( 'SELECT COUNT(*) FROM ' . $table, $connection );
  271.                         $rows_result = mysql_fetch_array( $row_count );
  272.                         $row_count = $rows_result[ 0 ];
  273.                         if ( $row_count == 0 )
  274.                                 continue;
  275.  
  276.                         $page_size = 50000;
  277.                         $pages = ceil( $row_count / $page_size );
  278.  
  279.                         for( $page = 0; $page < $pages; $page++ ) {
  280.  
  281.                                 $current_row = 0;
  282.                                 $start = $page * $page_size;
  283.                                 $end = $start + $page_size;
  284.                                 // Grab the content of the table
  285.                                 $data = mysql_query( sprintf( 'SELECT * FROM %s LIMIT %d, %d', $table, $start, $end ), $connection );
  286.  
  287.                                 if ( ! $data )
  288.                                         $report[ 'errors' ][] = mysql_error( );
  289.  
  290.                                 while ( $row = mysql_fetch_array( $data ) ) {
  291.  
  292.                                         $report[ 'rows' ]++; // Increment the row counter
  293.                                         $current_row++;
  294.  
  295.                                         $update_sql = array( );
  296.                                         $where_sql = array( );
  297.                                         $upd = false;
  298.  
  299.                                         foreach( $columns as $column => $primary_key ) {
  300.                                                 if ( $guid == 1 && in_array( $column, $exclude_cols ) )
  301.                                                         continue;
  302.  
  303.                                                 $edited_data = $data_to_fix = $row[ $column ];
  304.  
  305.                                                 // Run a search replace on the data that'll respect the serialisation.
  306.                                                 $edited_data = recursive_unserialize_replace( $search, $replace, $data_to_fix );
  307.  
  308.                                                 // Something was changed
  309.                                                 if ( $edited_data != $data_to_fix ) {
  310.                                                         $report[ 'change' ]++;
  311.                                                         $update_sql[] = $column . ' = "' . mysql_real_escape_string( $edited_data ) . '"';
  312.                                                         $upd = true;
  313.                                                 }
  314.  
  315.                                                 if ( $primary_key )
  316.                                                         $where_sql[] = $column . ' = "' . mysql_real_escape_string( $data_to_fix ) . '"';
  317.                                         }
  318.  
  319.                                         if ( $upd && ! empty( $where_sql ) ) {
  320.                                                 $sql = 'UPDATE ' . $table . ' SET ' . implode( ', ', $update_sql ) . ' WHERE ' . implode( ' AND ', array_filter( $where_sql ) );
  321.                                                 $result = mysql_query( $sql, $connection );
  322.                                                 if ( ! $result )
  323.                                                         $report[ 'errors' ][] = mysql_error( );
  324.                                                 else
  325.                                                         $report[ 'updates' ]++;
  326.  
  327.                                         } elseif ( $upd ) {
  328.                                                 $report[ 'errors' ][] = sprintf( '"%s" has no primary key, manual change needed on row %s.', $table, $current_row );
  329.                                         }
  330.  
  331.                                 }
  332.                         }
  333.                 }
  334.  
  335.         }
  336.         $report[ 'end' ] = microtime( );
  337.  
  338.         return $report;
  339. }
  340.  
  341.  
  342. /**
  343.  * Take an array and turn it into an English formatted list. Like so:
  344.  * array( 'a', 'b', 'c', 'd' ); = a, b, c, or d.
  345.  *
  346.  * @param array $input_arr The source array
  347.  *
  348.  * @return string    English formatted string
  349.  */
  350. function eng_list( $input_arr = array( ), $sep = ', ', $before = '"', $after = '"' ) {
  351.         if ( ! is_array( $input_arr ) )
  352.                 return false;
  353.  
  354.         $_tmp = $input_arr;
  355.  
  356.         if ( count( $_tmp ) >= 2 ) {
  357.                 $end2 = array_pop( $_tmp );
  358.                 $end1 = array_pop( $_tmp );
  359.                 array_push( $_tmp, $end1 . $after . ' or ' . $before . $end2 );
  360.         }
  361.  
  362.         return $before . implode( $before . $sep . $after, $_tmp ) . $after;
  363. }
  364.  
  365.  
  366. /**
  367.  * Search through the file name passed for a set of defines used to set up
  368.  * WordPress db access.
  369.  *
  370.  * @param string $filename The file name we need to scan for the defines.
  371.  *
  372.  * @return array    List of db connection details.
  373.  */
  374. function icit_srdb_define_find( $filename = 'wp-config.php' ) {
  375.  
  376.         $filename = dirname( __FILE__ ) . '/' . basename( $filename );
  377.  
  378.         if ( file_exists( $filename ) && is_file( $filename ) && is_readable( $filename ) ) {
  379.                 $file = @fopen( $filename, 'r' );
  380.                 $file_content = fread( $file, filesize( $filename ) );
  381.                 @fclose( $file );
  382.         }
  383.  
  384.         preg_match_all( '/define\s*?\(\s*?([\'"])(DB_NAME|DB_USER|DB_PASSWORD|DB_HOST|DB_CHARSET)\1\s*?,\s*?([\'"])([^\3]*?)\3\s*?\)\s*?;/si', $file_content, $defines );
  385.  
  386.         if ( ( isset( $defines[ 2 ] ) && ! empty( $defines[ 2 ] ) ) && ( isset( $defines[ 4 ] ) && ! empty( $defines[ 4 ] ) ) ) {
  387.                 foreach( $defines[ 2 ] as $key => $define ) {
  388.  
  389.                         switch( $define ) {
  390.                                 case 'DB_NAME':
  391.                                         $name = $defines[ 4 ][ $key ];
  392.                                         break;
  393.                                 case 'DB_USER':
  394.                                         $user = $defines[ 4 ][ $key ];
  395.                                         break;
  396.                                 case 'DB_PASSWORD':
  397.                                         $pass = $defines[ 4 ][ $key ];
  398.                                         break;
  399.                                 case 'DB_HOST':
  400.                                         $host = $defines[ 4 ][ $key ];
  401.                                         break;
  402.                                 case 'DB_CHARSET':
  403.                                         $char = $defines[ 4 ][ $key ];
  404.                                         break;
  405.                         }
  406.                 }
  407.         }
  408.  
  409.         return array( $host, $name, $user, $pass, $char );
  410. }
  411.  
  412. /*
  413.  Check and clean all vars, change the step we're at depending on the quality of
  414.  the vars.
  415. */
  416. $errors = array( );
  417. $step = isset( $_REQUEST[ 'step' ] ) ? intval( $_REQUEST[ 'step' ] ) : 0; // Set the step to the request, we'll change it as we need to.
  418.  
  419. // Check that we need to scan wp-config.
  420. $loadwp = isset( $_POST[ 'loadwp' ] ) ? true : false;
  421.  
  422. // DB details
  423. $host = isset( $_POST[ 'host' ] ) ? stripcslashes( $_POST[ 'host' ] ) : 'localhost';    // normally localhost, but not necessarily.
  424. $data = isset( $_POST[ 'data' ] ) ? stripcslashes( $_POST[ 'data' ] ) : '';     // your database
  425. $user = isset( $_POST[ 'user' ] ) ? stripcslashes( $_POST[ 'user' ] ) : '';     // your db userid
  426. $pass = isset( $_POST[ 'pass' ] ) ? stripcslashes( $_POST[ 'pass' ] ) : '';     // your db password
  427. $char = isset( $_POST[ 'char' ] ) ? stripcslashes( $_POST[ 'char' ] ) : '';     // your db charset
  428. // Search replace details
  429. $srch = isset( $_POST[ 'srch' ] ) ? stripcslashes( $_POST[ 'srch' ] ) : '';
  430. $rplc = isset( $_POST[ 'rplc' ] ) ? stripcslashes( $_POST[ 'rplc' ] ) : '';
  431. // Tables to scanned
  432. $tables = isset( $_POST[ 'tables' ] ) && is_array( $_POST[ 'tables' ] ) ? array_map( 'stripcslashes', $_POST[ 'tables' ] ) : array( );
  433. // Do we want to skip changing the guid column
  434. $guid = isset( $_POST[ 'guid' ] ) && $_POST[ 'guid' ] == 1 ? 1 : 0;
  435. $exclude_cols = array( 'guid' ); // Add columns to be excluded from changes to this array. If the GUID checkbox is ticked they'll be skipped.
  436.  
  437. // If we're at the start we'll check to see if wp is about so we can get the details from the wp-config.
  438. if ( $step == 0 || $step == 1 )
  439.         $step = file_exists( dirname( __FILE__ ) . '/wp-config.php' ) ? 1 : 2;
  440.  
  441. // Scan wp-config for the defines. We can't just include it as it will try and load the whole of wordpress.
  442. if ( $loadwp && file_exists( dirname( __FILE__ ) . '/wp-config.php' ) )
  443.         list( $host, $data, $user, $pass, $char ) = icit_srdb_define_find( 'wp-config.php' );
  444.  
  445. // Check the db connection else go back to step two.
  446. if ( $step >= 3 ) {
  447.         $connection = @mysql_connect( $host, $user, $pass );
  448.         if ( ! $connection ) {
  449.                 $errors[] = mysql_error( );
  450.                 $step = 2;
  451.         }
  452.        
  453.         if ( ! empty( $char ) ) {
  454.                 if ( function_exists( 'mysql_set_charset' ) )
  455.                         mysql_set_charset( $char, $connection );
  456.                 else
  457.                         mysql_query( 'SET NAMES ' . $char, $connection );  // Shouldn't really use this, but there for backwards compatibility 
  458.         }
  459.        
  460.         // Do we have any tables and if so build the all tables array
  461.         $all_tables = array( );
  462.         @mysql_select_db( $data, $connection );
  463.         $all_tables_mysql = @mysql_query( 'SHOW TABLES', $connection );
  464.  
  465.         if ( ! $all_tables_mysql ) {
  466.                 $errors[] = mysql_error( );
  467.                 $step = 2;
  468.         } else {
  469.                 while ( $table = mysql_fetch_array( $all_tables_mysql ) ) {
  470.                         $all_tables[] = $table[ 0 ];
  471.                 }
  472.         }
  473. }
  474.  
  475. // Check and clean the tables array
  476. $tables = array_filter( $tables, 'check_table_array' );
  477. if ( $step >= 4 && empty( $tables ) ) {
  478.         $errors[] = 'You didn\'t select any tables.';
  479.         $step = 3;
  480. }
  481.  
  482. // Make sure we're searching for something.
  483. if ( $step >= 5 ) {
  484.         if ( empty( $srch ) ) {
  485.                 $errors[] = 'Missing search string.';
  486.                 $step = 4;
  487.         }
  488.  
  489.         if ( empty( $rplc ) ) {
  490.                 $errors[] = 'Replace string is blank.';
  491.                 $step = 4;
  492.         }
  493.  
  494.         if ( ! ( empty( $rplc ) && empty( $srch ) ) && $rplc == $srch ) {
  495.                 $errors[] = 'Search and replace are the same, please check your values.';
  496.                 $step = 4;
  497.         }
  498. }
  499.  
  500.  
  501. /*
  502.  Send the HTML to the screen.
  503. */
  504. @header('Content-Type: text/html; charset=UTF-8');?>
  505. <!DOCTYPE html>
  506. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/terms/" dir="ltr" lang="en-US">
  507. <head profile="http://gmpg.org/xfn/11">
  508.         <title>Search and replace DB.</title>
  509.         <style type="text/css">
  510.         body {
  511.                 background-color: #E5E5E5;
  512.                 color: #353231;
  513.                 font: 14px/18px "Gill Sans MT","Gill Sans",Calibri,sans-serif;
  514.         }
  515.  
  516.         p {
  517.             line-height: 18px;
  518.             margin: 18px 0;
  519.             max-width: 520px;
  520.         }
  521.  
  522.         p.byline {
  523.             margin: 0 0 18px 0;
  524.             padding-bottom: 9px;
  525.             border-bottom: 1px dashed #999999;
  526.             max-width: 100%;
  527.         }
  528.  
  529.         h1,h2,h3 {
  530.             font-weight: normal;
  531.             line-height: 36px;
  532.             font-size: 24px;
  533.             margin: 9px 0;
  534.             text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.8);
  535.         }
  536.  
  537.         h2 {
  538.             font-weight: normal;
  539.             line-height: 24px;
  540.             font-size: 21px;
  541.             margin: 9px 0;
  542.             text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.8);
  543.         }
  544.  
  545.         h3 {
  546.             font-weight: normal;
  547.             line-height: 18px;
  548.             margin: 9px 0;
  549.             text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.8);
  550.         }
  551.  
  552.         a {
  553.             -moz-transition: color 0.2s linear 0s;
  554.             color: #DE1301;
  555.             text-decoration: none;
  556.             font-weight: normal;
  557.         }
  558.  
  559.         a:visited {
  560.            -moz-transition: color 0.2s linear 0s;
  561.             color: #AE1301;
  562.         }
  563.  
  564.         a:hover, a:visited:hover {
  565.             -moz-transition: color 0.2s linear 0s;
  566.             color: #FE1301;
  567.             text-decoration: underline;
  568. }
  569.  
  570.         #container {
  571.                 display:block;
  572.                 width: 768px;
  573.                 padding: 10px;
  574.                 margin: 0px auto;
  575.                 border:solid 10px 0px 0px 0px #ccc;
  576.                 border-top: 18px solid #DE1301;
  577.                 background-color: #F5F5F5;
  578.         }
  579.  
  580.         fieldset {
  581.                 border: 0 none;
  582.         }
  583.  
  584.         .error {
  585.                 border: solid 1px #c00;
  586.                 padding: 5px;
  587.                 background-color: #FFEBE8;
  588.                 text-align: center;
  589.                 margin-bottom: 10px;
  590.         }
  591.  
  592.         label {
  593.                 display:block;
  594.                 line-height: 18px;
  595.                 cursor: pointer;
  596.         }
  597.  
  598.         select.multi,
  599.         input.text {
  600.                 margin-bottom: 1em;
  601.                 display:block;
  602.                 width: 90%;
  603.         }
  604.  
  605.         select.multi {
  606.                 height: 144px;
  607.         }
  608.  
  609.  
  610.         input.button {
  611.         }
  612.  
  613.         div.help {
  614.                 border-top: 1px dashed #999999;
  615.                 margin-top: 9px;
  616.         }
  617.  
  618.         </style>
  619. </head>
  620. <body>
  621.         <div id="container"><?php
  622.                 if ( ! empty( $errors ) && is_array( $errors ) ) {
  623.                         echo '<div class="error">';
  624.                         foreach( $errors as $error )
  625.                                 echo '<p>' . $error . '</p>';
  626.                         echo '</div>';
  627.                 }?>
  628.  
  629.  
  630.         <h1>Safe Search Replace</h1>
  631.         <p class="byline">by interconnect/<strong>it</strong></p>
  632.         <?php
  633. /*
  634.  The bit that does all the work.
  635. */
  636. switch ( $step ) {
  637.         case 1:
  638.                 // Prompt for the loading of WordPress or not.  ?>
  639.                 <h2>Load DB connection values from WordPress?</h2>
  640.                 <form action="<?php icit_srdb_form_action( ); ?>" method="post">
  641.                         <fieldset>
  642.                                 <p><label for="loadwp"><input type="checkbox" checked="checked" value="1" name="loadwp" id="loadwp" /> Pre-populate the DB values form with the ones used in wp-config?  It is possible to edit them later.</label></p> <?php
  643.                                 icit_srdb_submit( 'Submit' ); ?>
  644.                         </fieldset>
  645.                 </form> <?php
  646.                 break;
  647.  
  648.  
  649.         case 2:
  650.                 // Ask for db username password. ?>
  651.                 <h2>Database details</h2>
  652.                 <form action="<?php icit_srdb_form_action( ); ?>" method="post">
  653.                         <fieldset>
  654.                                 <p>
  655.                                         <label for="host">Server Name:</label>
  656.                                         <input class="text" type="text" name="host" id="host" value="<?php esc_html_attr( $host, true ) ?>" />
  657.                                 </p>
  658.  
  659.                                 <p>
  660.                                         <label for="data">Database Name:</label>
  661.                                         <input class="text" type="text" name="data" id="data" value="<?php esc_html_attr( $data, true ) ?>" />
  662.                                 </p>
  663.  
  664.                                 <p>
  665.                                         <label for="user">Username:</label>
  666.                                         <input class="text" type="text" name="user" id="user" value="<?php esc_html_attr( $user, true ) ?>" />
  667.                                 </p>
  668.  
  669.                                 <p>
  670.                                         <label for="pass">Password:</label>
  671.                                         <input class="text" type="password" name="pass" id="pass" value="<?php esc_html_attr( $pass, true ) ?>" />
  672.                                 </p>
  673.                                
  674.                                 <p>
  675.                                         <label for="pass">Charset:</label>
  676.                                         <input class="text" type="text" name="char" id="char" value="<?php esc_html_attr( $char, true ) ?>" />
  677.                                 </p>
  678.                                 <?php icit_srdb_submit( 'Submit DB details' ); ?>
  679.                         </fieldset>
  680.                 </form> <?php
  681.                 break;
  682.  
  683.  
  684.         case 3:
  685.                 // Ask which tables to deal with ?>
  686.                 <h2>Which tables do you want to scan?</h2>
  687.                 <form action="<?php icit_srdb_form_action( ); ?>" method="post">
  688.  
  689.                         <fieldset>
  690.  
  691.                                 <input type="hidden" name="host" value="<?php esc_html_attr( $host, true ) ?>" />
  692.                                 <input type="hidden" name="data" value="<?php esc_html_attr( $data, true ) ?>" />
  693.                                 <input type="hidden" name="user" value="<?php esc_html_attr( $user, true ) ?>" />
  694.                                 <input type="hidden" name="pass" value="<?php esc_html_attr( $pass, true ) ?>" />
  695.                                 <input type="hidden" name="char" value="<?php esc_html_attr( $char, true ) ?>" />
  696.                                 <p>
  697.                                         <label for="tables">Tables:</label>
  698.                                         <select id="tables" name="tables[]" multiple="multiple" class="multi"><?php
  699.                                         foreach( $all_tables as $table ) {
  700.                                                 echo '<option selected="selected" value="' . esc_html_attr( $table ) . '">' . $table . '</option>';
  701.                                         } ?>
  702.                                         </select>
  703.                                         <em>Multiple tables can be selected with ( CTRL/CMD + click ).</em>
  704.                                 </p>
  705.  
  706.                                 <p>
  707.                                         <label for="guid">
  708.                                         <input type="checkbox" name="guid" id="guid" value="1" <?php echo $guid == 1 ? 'checked="checked"' : '' ?>/> Leave GUID column unchanged? </label>
  709.                                         <em>If the values in the GUID column from the WordPress posts table change RSS readers and other tools will be under the impression that the posts are new and may show them in feeds again. <br />
  710.                                         All columns titled <?php echo eng_list( $exclude_cols ) ?> will be skipped if this it ticked.</em>
  711.                                 </p>
  712.  
  713.                                 <?php icit_srdb_submit( 'Continue', 'Do be sure that you have selected the correct tables - especially important on multi-sites installs.' );   ?>
  714.                         </fieldset>
  715.                 </form> <?php
  716.                 break;
  717.  
  718.  
  719.         case 4:
  720.                 // Ask for the search replace strings. ?>
  721.                 <h2>What to replace?</h2>
  722.                 <form action="<?php icit_srdb_form_action( ); ?>" method="post">
  723.                         <fieldset>
  724.                                 <input type="hidden" name="host" id="host" value="<?php esc_html_attr( $host, true ) ?>" />
  725.                                 <input type="hidden" name="data" id="data" value="<?php esc_html_attr( $data, true ) ?>" />
  726.                                 <input type="hidden" name="user" id="user" value="<?php esc_html_attr( $user, true ) ?>" />
  727.                                 <input type="hidden" name="pass" id="pass" value="<?php esc_html_attr( $pass, true ) ?>" />
  728.                                 <input type="hidden" name="char" id="char" value="<?php esc_html_attr( $char, true ) ?>" />
  729.                                 <input type="hidden" name="guid" id="guid" value="<?php esc_html_attr( $guid, true ) ?>" /> <?php
  730.  
  731.                                 foreach( $tables as $i => $tab ) {
  732.                                         printf( '<input type="hidden" name="tables[%s]" value="%s" />', esc_html_attr( $i, false ), esc_html_attr( $tab, false ) );
  733.                                 } ?>
  734.  
  735.                                 <p>
  736.                                         <label for="srch">Search for (case sensitive string):</label>
  737.                                         <input class="text" type="text" name="srch" id="srch" value="<?php esc_html_attr( $srch, true ) ?>" />
  738.                                 </p>
  739.  
  740.                                 <p>
  741.                                         <label for="rplc">Replace with:</label>
  742.                                         <input class="text" type="text" name="rplc" id="rplc" value="<?php esc_html_attr( $rplc, true ) ?>" />
  743.                                 </p>
  744.  
  745.                                 <?php icit_srdb_submit( 'Submit Search string', 'Are you REALLY sure you want to go ahead and do this?' ); ?>
  746.                         </fieldset>
  747.                 </form> <?php
  748.                 break;
  749.  
  750.  
  751.         case 5:
  752.  
  753.                 @ set_time_limit( 60 * 10 );
  754.                 // Try to push the allowed memory up, while we're at it
  755.                 @ ini_set( 'memory_limit', '1024M' );
  756.  
  757.                 // Process the tables
  758.                 if ( isset( $connection ) )
  759.                         $report = icit_srdb_replacer( $connection, $srch, $rplc, $tables );
  760.  
  761.                 // Output any errors encountered during the db work.
  762.                 if ( ! empty( $report[ 'errors' ] ) && is_array( $report[ 'errors' ] ) ) {
  763.                         echo '<div class="error">';
  764.                         foreach( $report[ 'errors' ] as $error )
  765.                                 echo '<p>' . $error . '</p>';
  766.                         echo '</div>';
  767.                 }
  768.  
  769.                 // Calc the time taken.
  770.                 $time = array_sum( explode( ' ', $report[ 'end' ] ) ) - array_sum( explode( ' ', $report[ 'start' ] ) ); ?>
  771.  
  772.                 <h2>Completed</h2>
  773.                 <p><?php printf( 'In the process of replacing <strong>"%s"</strong> with <strong>"%s"</strong> we scanned <strong>%d</strong> tables with a total of <strong>%d</strong> rows, <strong>%d</strong> cells were changed and <strong>%d</strong> db update performed and it all took <strong>%f</strong> seconds.', $srch, $rplc, $report[ 'tables' ], $report[ 'rows' ], $report[ 'change' ], $report[ 'updates' ], $time ); ?></p> <?php
  774.                 break;
  775.  
  776.  
  777.         default: ?>
  778.                 <h2>No idea how we got here.</h2>
  779.                 <p>Something strange has happened.</p> <?php
  780.                 break;
  781. }
  782.  
  783. if ( isset( $connection ) && $connection )
  784.         mysql_close( $connection );
  785.  
  786.  
  787. // Warn if we're running in safe mode as we'll probably time out.
  788. if ( ini_get( 'safe_mode' ) ) {
  789.         echo '<h4>Warning</h4>';
  790.         printf( '<p style="color:red;">Safe mode is on so you may run into problems if it takes longer than %s seconds to process your request.</p>', ini_get( 'max_execution_time' ) );
  791. }
  792. /*
  793.  Close out the html and exit.
  794. */ ?>
  795.                 <div class="help">
  796.                         <h4><a href="http://interconnectit.com/">interconnect/it</a> <a href="http://interconnectit.com/124/search-and-replace-for-wordpress-databases/">Safe Search and Replace on Database with Serialized Data v2.0.0</a></h4>
  797.                         <p>This developer/sysadmin tool helps solve the problem of doing a search and replace on a
  798.                         WordPress site when doing a migration to a domain name with a different length.</p>
  799.  
  800.                         <p><style="color:red">WARNING!</strong> Take a backup first, and carefully test the results of this code.
  801.                         If you don't, and you vape your data then you only have yourself to blame.
  802.                         Seriously.  And if you're English is bad and you don't fully understand the
  803.                         instructions then STOP.  Right there.  Yes.  Before you do any damage.
  804.  
  805.                         <h2>Don't Forget to Remove Me!</h3>
  806.  
  807.                         <p style="color:red">Delete this utility from your
  808.                         server after use.  It represents a major security threat to your database if
  809.                         maliciously used.</p>
  810.  
  811.                         <h2>Use Of This Script Is Entirely At Your Own Risk</h2>
  812.  
  813.                         <p> We accept no liability from the use of this tool.</p>
  814.  
  815.                         <p>If you're not comfortable with this kind of stuff, get an expert, like us, to do
  816.                         this work for you.  You do this ENTIRELY AT YOUR OWN RISK!  We accept no responsibility
  817.                         if you mess up your data.  There is NO UNDO here!</p>
  818.  
  819.                         <p>The easiest way to use it is to copy your site's files and DB to the new location.
  820.                         You then, if required, fix up your .htaccess and wp-config.php appropriately.  Once
  821.                         done, run this script, select your tables (in most cases all of them) and then
  822.                         enter the search replace strings.  You can press back in your browser to do
  823.                         this several times, as may be required in some cases.</p>
  824.  
  825.                         <p>Of course, you can use the script in many other ways - for example, finding
  826.                         all references to a company name and changing it when a rebrand comes along.  Or
  827.                         perhaps you changed your name.  Whatever you want to search and replace the code will help.</p>
  828.  
  829.                         <p><a href="http://interconnectit.com/124/search-and-replace-for-wordpress-databases/">Got feedback on this script? Come tell us!</a>
  830.  
  831.                 </div>
  832.         </div>
  833. </body>
  834. </html>