/index.php
PHP | 1222 lines | 751 code | 271 blank | 200 comment | 77 complexity | 9357c81b3bb553d77290929601f88844 MD5 | raw file
- <?php
- /**
- *
- * Safe Search and Replace on Database with Serialized Data v3.0.0
- *
- * This script is to solve the problem of doing database search and replace when
- * some data is stored within PHP serialized arrays or objects.
- *
- * For more information, see
- * http://interconnectit.com/124/search-and-replace-for-wordpress-databases/
- *
- * To contribute go to
- * http://github.com/interconnectit/search-replace-db
- *
- * To use, load the script on your server and point your web browser to it.
- * In some situations, consider using the command line interface version.
- *
- * BIG WARNING! Take a backup first, and carefully test the results of this
- * code. If you don't, and you vape your data then you only have yourself to
- * blame. Seriously. And if your English is bad and you don't fully
- * understand the instructions then STOP. Right there. Yes. Before you do any
- * damage.
- *
- * USE OF THIS SCRIPT IS ENTIRELY AT YOUR OWN RISK. I/We accept no liability
- * from its use.
- *
- * First Written 2009-05-25 by David Coveney of Interconnect IT Ltd (UK)
- * http://www.davidcoveney.com or http://interconnectit.com
- * and released under the GPL v3
- * ie, do what ever you want with the code, and we take no responsibility for it
- * OK? If you don't wish to take responsibility, hire us at Interconnect IT Ltd
- * on +44 (0)151 331 5140 and we will do the work for you at our hourly rate,
- * minimum 1hr
- *
- * License: GPL v3
- * License URL: http://www.gnu.org/copyleft/gpl.html
- *
- *
- * Version 3.0.0:
- * * Major overhaul
- * * Multibyte string replacements
- * * UI completely redesigned
- * * Removed all links from script until 'delete' has been clicked to avoid
- * security risk from our access logs
- * * Search replace functionality moved to it's own separate class
- * * Replacements done table by table to avoid timeouts
- * * Convert tables to InnoDB
- * * Convert tables to utf8_unicode_ci
- * * Use PDO if available
- * * Preview/view changes
- * * Optionally use preg_replace()
- * * Scripts bootstraps WordPress/Drupal to avoid issues with unknown
- * serialised objects/classes
- * * Added marketing stuff to deleted screen (sorry but we're running a
- * business!)
- *
- * Version 2.2.0:
- * * Added remove script patch from David Anderson (wordshell.net)
- * * Added ability to replace strings with nothing
- * * Copy changes
- * * Added code to recursive_unserialize_replace to deal with objects not
- * just arrays. This was submitted by Tina Matter.
- * ToDo: Test object handling. Not sure how it will cope with object in the
- * db created with classes that don't exist in anything but the base PHP.
- *
- * Version 2.1.0:
- * - Changed to version 2.1.0
- * * Following change by Sergei Biryukov - merged in and tested by Dave Coveney
- * - Added Charset Support (tested with UTF-8, not tested on other charsets)
- * * Following changes implemented by James Whitehead with thanks to all the commenters and feedback given!
- * - Removed PHP warnings if you go to step 3+ without DB details.
- * - Added options to skip changing the guid column. If there are other
- * columns that need excluding you can add them to the $exclude_cols global
- * array. May choose to add another option to the table select page to let
- * you add to this array from the front end.
- * - Minor tweak to label styling.
- * - Added comments to each of the functions.
- * - Removed a dead param from icit_srdb_replacer
- * Version 2.0.0:
- * - returned to using unserialize function to check if string is
- * serialized or not
- * - marked is_serialized_string function as deprecated
- * - changed form order to improve usability and make use on multisites a
- * bit less scary
- * - changed to version 2, as really should have done when the UI was
- * introduced
- * - added a recursive array walker to deal with serialized strings being
- * stored in serialized strings. Yes, really.
- * - changes by James R Whitehead (kudos for recursive walker) and David
- * Coveney 2011-08-26
- * Version 1.0.2:
- * - typos corrected, button text tweak - David Coveney / Robert O'Rourke
- * Version 1.0.1
- * - styling and form added by James R Whitehead.
- *
- * Credits: moz667 at gmail dot com for his recursive_array_replace posted at
- * uk.php.net which saved me a little time - a perfect sample for me
- * and seems to work in all cases.
- *
- */
- // always good here
- header( 'HTTP/1.1 200 OK' );
- header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
- header('Pragma: no-cache'); // HTTP 1.0.
- header('Expires: 0'); // Proxies.
- require_once( 'srdb.class.php' );
- class icit_srdb_ui extends icit_srdb {
- /**
- * @var string Root path of the CMS
- */
- public $path;
- public $is_wordpress = false;
- public $is_drupal = false;
- public function __construct() {
- // php 5.4 date timezone requirement, shouldn't affect anything
- date_default_timezone_set( 'Europe/London' );
- // prevent fatals from hiding the UI
- register_shutdown_function( array( $this, 'fatal_handler' ) );
- // flag to bootstrap WP or Drupal
- $bootstrap = true; // isset( $_GET[ 'bootstrap' ] );
- // discover environment
- if ( $bootstrap && $this->is_wordpress() ) {
- // prevent warnings if the charset and collate aren't defined
- if ( !defined( 'DB_CHARSET') ) {
- define( 'DB_CHARSET', 'utf8' );
- }
- if ( !defined( 'DB_COLLATE') ) {
- define( 'DB_COLLATE', '' );
- }
- // populate db details
- $name = DB_NAME;
- $user = DB_USER;
- $pass = DB_PASSWORD;
- $host = DB_HOST;
- $charset = DB_CHARSET;
- $collate = DB_COLLATE;
- $this->response( $name, $user, $pass, $host, $charset, $collate );
- } elseif( $bootstrap && $this->is_drupal() ) {
- $database = Database::getConnection();
- $database_opts = $database->getConnectionOptions();
- // populate db details
- $name = $database_opts[ 'database' ];
- $user = $database_opts[ 'username' ];
- $pass = $database_opts[ 'password' ];
- $host = $database_opts[ 'host' ];
- $charset = 'utf8';
- $collate = '';
- $this->response( $name, $user, $pass, $host, $charset, $collate );
- } else {
- $this->response();
- }
- }
- public function response( $name = '', $user = '', $pass = '', $host = '127.0.0.1', $charset = 'utf8', $collate = '' ) {
- // always override with post data
- if ( isset( $_POST[ 'name' ] ) ) {
- $name = $_POST[ 'name' ]; // your database
- $user = $_POST[ 'user' ]; // your db userid
- $pass = $_POST[ 'pass' ]; // your db password
- $host = $_POST[ 'host' ]; // normally localhost, but not necessarily.
- $charset = 'utf8'; // isset( $_POST[ 'char' ] ) ? stripcslashes( $_POST[ 'char' ] ) : ''; // your db charset
- $collate = '';
- }
- // Search replace details
- $search = isset( $_POST[ 'search' ] ) ? $_POST[ 'search' ] : '';
- $replace = isset( $_POST[ 'replace' ] ) ? $_POST[ 'replace' ] : '';
- // regex options
- $regex = isset( $_POST[ 'regex' ] );
- $regex_i = isset( $_POST[ 'regex_i' ] );
- $regex_m = isset( $_POST[ 'regex_m' ] );
- $regex_s = isset( $_POST[ 'regex_s' ] );
- $regex_x = isset( $_POST[ 'regex_x' ] );
- // Tables to scanned
- $tables = isset( $_POST[ 'tables' ] ) && is_array( $_POST[ 'tables' ] ) ? $_POST[ 'tables' ] : array( );
- if ( isset( $_POST[ 'use_tables' ] ) && $_POST[ 'use_tables' ] == 'all' )
- $tables = array();
- // exclude / include columns
- $exclude_cols = isset( $_POST[ 'exclude_cols' ] ) ? $_POST[ 'exclude_cols' ] : array();
- $include_cols = isset( $_POST[ 'include_cols' ] ) ? $_POST[ 'include_cols' ] : array();
- foreach( array( 'exclude_cols', 'include_cols' ) as $maybe_string_arg ) {
- if ( is_string( $$maybe_string_arg ) )
- $$maybe_string_arg = array_filter( array_map( 'trim', explode( ',', $$maybe_string_arg ) ) );
- }
- // update class vars
- $vars = array(
- 'name', 'user', 'pass', 'host',
- 'charset', 'collate', 'tables',
- 'search', 'replace',
- 'exclude_cols', 'include_cols',
- 'regex', 'regex_i', 'regex_m', 'regex_s', 'regex_x'
- );
- foreach( $vars as $var ) {
- if ( isset( $$var ) )
- $this->set( $var, $$var );
- }
- // are doing something?
- $show = '';
- if ( isset( $_POST[ 'submit' ] ) ) {
- if ( is_array( $_POST[ 'submit' ] ) )
- $show = key( $_POST[ 'submit' ] );
- if ( is_string( $_POST[ 'submit' ] ) )
- $show = preg_replace( '/submit\[([a-z0-9]+)\]/', '$1', $_POST[ 'submit' ] );
- }
- // is it an AJAX call
- $ajax = isset( $_POST[ 'ajax' ] );
- // body callback
- $html = 'ui';
- switch( $show ) {
- // remove search replace
- case 'delete':
-