Exemple #1
0
 public function response($name = '', $user = '', $pass = '', $host = '127.0.0.1', $port = 3306, $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.
         $port_input = $_POST['port'];
         // Make sure that the string version of absint(port) is identical to the string input.
         // This prevents expressions, decimals, spaces, etc.
         $port_as_string = (string) $port_input ? (string) $port_input : "0";
         if ((string) abs((int) $port_input) !== $port_as_string) {
             // Mangled port number: non numeric.
             $this->add_error('Port number must be a positive integer. If you are unsure, try the default port 3306.', 'db');
             // Force a bad run by supplying nonsense.
             $port = "nonsense";
         } else {
             $port = abs((int) $port_input);
         }
         $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', 'port', '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':
             // determine if it's the folder of compiled version
             if (basename(__FILE__) == 'index.php') {
                 $path = str_replace(basename(__FILE__), '', __FILE__);
             } else {
                 $path = __FILE__;
             }
             $delete_script_success = $this->delete_script($path);
             if (self::DELETE_SCRIPT_FAIL_UNSAFE === $delete_script_success) {
                 $this->add_error('Delete aborted! You seem to have placed Search/Replace into your WordPress or Drupal root. Please remove Search/Replace manually.', 'delete');
             } else {
                 if (self::DELETE_SCRIPT_SUCCESS === $delete_script_success && !(is_file(__FILE__) && file_exists(__FILE__))) {
                     $this->add_error('Search/Replace has been successfully removed from your server', 'delete');
                 } else {
                     $this->add_error('Could not fully delete Search/Replace. You will have to delete it manually', 'delete');
                 }
             }
             $html = 'deleted';
             break;
         case 'liverun':
             // bsy-web, 20130621: Check live run was explicitly clicked and only set false then
             $this->set('dry_run', false);
         case 'dryrun':
             // build regex string
             // non UI implements can just pass in complete regex string
             if ($this->regex) {
                 $mods = '';
                 if ($this->regex_i) {
                     $mods .= 'i';
                 }
                 if ($this->regex_s) {
                     $mods .= 's';
                 }
                 if ($this->regex_m) {
                     $mods .= 'm';
                 }
                 if ($this->regex_x) {
                     $mods .= 'x';
                 }
                 $this->search = '/' . $this->search . '/' . $mods;
             }
             // call search replace class
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'port' => $this->get('port'), 'search' => $this->get('search'), 'replace' => $this->get('replace'), 'tables' => $this->get('tables'), 'dry_run' => $this->get('dry_run'), 'regex' => $this->get('regex'), 'exclude_cols' => $this->get('exclude_cols'), 'include_cols' => $this->get('include_cols')));
             break;
         case 'innodb':
             // call search replace class to alter engine
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'port' => $this->get('port'), 'tables' => $this->get('tables'), 'alter_engine' => 'InnoDB'));
             break;
         case 'utf8':
             // call search replace class to alter engine
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'port' => $this->get('port'), 'tables' => $this->get('tables'), 'alter_collation' => 'utf8_unicode_ci'));
             break;
         case 'utf8mb4':
             // call search replace class to alter engine
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'port' => $this->get('port'), 'tables' => $this->get('tables'), 'alter_collation' => 'utf8mb4_unicode_ci'));
             break;
         case 'update':
         default:
             // get tables or error messages
             $this->db_setup();
             if ($this->db_valid()) {
                 // get engines
                 $this->set('engines', $this->get_engines());
                 // get tables
                 $this->set('all_tables', $this->get_tables());
             }
             break;
     }
     $info = array('table_select' => $this->table_select(false), 'engines' => $this->get('engines'));
     // set header again before output in case WP does it's thing
     header('HTTP/1.1 200 OK');
     if (!$ajax) {
         $this->html($html);
     } else {
         // return json version of results
         header('Content-Type: application/json');
         echo json_encode(array('errors' => $this->get('errors'), 'report' => $this->get('report'), 'info' => $info));
         exit;
     }
 }
 public function search_replace($search_replace)
 {
     list($table_list) = $this->query_schema();
     $temp = explode(':', DB_HOST);
     $db_host = !empty($temp['0']) ? $temp['0'] : DB_HOST;
     $db_port = !empty($temp['1']) ? $temp['1'] : null;
     $args = array('name' => DB_NAME, 'user' => DB_USER, 'pass' => DB_PASSWORD, 'host' => $db_host, 'port' => $db_port, 'search' => '', 'replace' => '', 'tables' => $table_list, 'dry_run' => false, 'regex' => false, 'pagesize' => 50000, 'alter_engine' => false, 'alter_collation' => false, 'verbose' => false);
     $srdb = new icit_srdb($args);
     foreach ($search_replace as $search => $replace) {
         $srdb->replacer($search, $replace, $table_list);
     }
 }
Exemple #3
0
 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':
             // determine if it's the folder of compiled version
             if (basename(__FILE__) == 'index.php') {
                 $path = str_replace(basename(__FILE__), '', __FILE__);
             } else {
                 $path = __FILE__;
             }
             if ($this->delete_script($path)) {
                 if (is_file(__FILE__) && file_exists(__FILE__)) {
                     $this->add_error('Could not delete the search replace script. You will have to delete it manually', 'delete');
                 } else {
                     $this->add_error('Search/Replace has been successfully removed from your server', 'delete');
                 }
             } else {
                 $this->add_error('Could not delete the search replace script automatically. You will have to delete it manually, sorry!', 'delete');
             }
             $html = 'deleted';
             break;
         case 'liverun':
             // bsy-web, 20130621: Check live run was explicitly clicked and only set false then
             $this->set('dry_run', false);
         case 'dryrun':
             // build regex string
             // non UI implements can just pass in complete regex string
             if ($this->regex) {
                 $mods = '';
                 if ($this->regex_i) {
                     $mods .= 'i';
                 }
                 if ($this->regex_s) {
                     $mods .= 's';
                 }
                 if ($this->regex_m) {
                     $mods .= 'm';
                 }
                 if ($this->regex_x) {
                     $mods .= 'x';
                 }
                 $this->search = '/' . $this->search . '/' . $mods;
             }
             // call search replace class
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'search' => $this->get('search'), 'replace' => $this->get('replace'), 'tables' => $this->get('tables'), 'dry_run' => $this->get('dry_run'), 'regex' => $this->get('regex'), 'exclude_cols' => $this->get('exclude_cols'), 'include_cols' => $this->get('include_cols')));
             break;
         case 'innodb':
             // call search replace class to alter engine
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'tables' => $this->get('tables'), 'alter_engine' => 'InnoDB'));
             break;
         case 'utf8':
             // call search replace class to alter engine
             $parent = parent::__construct(array('name' => $this->get('name'), 'user' => $this->get('user'), 'pass' => $this->get('pass'), 'host' => $this->get('host'), 'tables' => $this->get('tables'), 'alter_collation' => 'utf8_unicode_ci'));
             break;
         case 'update':
         default:
             // get tables or error messages
             $this->db_setup();
             if ($this->db_valid()) {
                 // get engines
                 $this->set('engines', $this->get_engines());
                 // get tables
                 $this->set('all_tables', $this->get_tables());
             }
             break;
     }
     $info = array('table_select' => $this->table_select(false), 'engines' => $this->get('engines'));
     // set header again before output in case WP does it's thing
     header('HTTP/1.1 200 OK');
     if (!$ajax) {
         $this->html($html);
     } else {
         // return json version of results
         header('Content-Type: application/json');
         echo json_encode(array('errors' => $this->get('errors'), 'report' => $this->get('report'), 'info' => $info));
         exit;
     }
 }
 /**
  * @test multibyte string replacement method
  */
 public function testMultibyteStrReplace()
 {
     $subject = 'föö ❤ ☀ ☆ ☂ ☻ ♞ ☯';
     $result = 'föö ❤ ☻ ♞ ☯ ☻ ♞ ☯';
     $replaced = icit_srdb::mb_str_replace('☀ ☆ ☂', '☻ ♞ ☯', $subject);
     $this->assertEquals($result, $replaced);
 }