function searchandreplace_sall($search_text, $replace_text = FALSE)
{
    global $wpdb;
    if (empty($wpdb->dbname)) {
        $wpdb->dbname = DB_NAME;
    }
    $search_text = esc_sql($search_text);
    # this appears to be escaped already
    if ($replace_text) {
        $replace_text = esc_sql($replace_text);
    }
    # this appears to be escaped already
    $result_in_tables = 0;
    $myecho = '
	<script language="JavaScript">
		var table_id = new Array();
	
		function hide_all() {
			for(i=0;i<table_id.length;i++){
				document.getElementById(table_id[i]).style.display = \'none\';
			}
		}
		
		function show_all() {
			for(i=0;i<table_id.length;i++){
				document.getElementById(table_id[i]).style.display = \'block\';
			}
		}
		
		function toggle(id) {
			if (get_style(id,\'display\') == \'block\') {
				document.getElementById(id).style.display = \'none\';
			} else {
				document.getElementById(id).style.display = \'block\';
			}
		}
		
		function get_style(el,styleProp) {
			var x = document.getElementById(el);
			if (x.currentStyle)
				var y = x.currentStyle[styleProp];
			else if (window.getComputedStyle)
				var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
			return y;
		}
	</script>';
    $myecho .= '<p><a href="javascript:hide_all()">' . __('Collapse All', FB_SAR_TEXTDOMAIN) . '</a>
		 <a href="javascript:show_all()">' . __('Expand All', FB_SAR_TEXTDOMAIN) . '</a></p>';
    $myecho .= '<p>' . __('Results for', FB_SAR_TEXTDOMAIN) . ': <code>' . stripslashes($search_text) . '</code></p><p>' . __('Please note search text may appear (and be replaced) more than one time in each row.', FB_SAR_TEXTDOMAIN) . '</p>';
    $sql = 'SHOW TABLES';
    $tables = $wpdb->get_results($sql, ARRAY_A);
    $replace_sql = '';
    for ($i = 0; $i < count($tables); $i++) {
        //@abstract query building of each table
        if ($wpdb->get_var("SELECT COUNT(*) FROM " . $tables[$i]['Tables_in_' . $wpdb->dbname]) > 0) {
            //@abstract get the table data type information
            $sql = 'desc ' . $tables[$i]['Tables_in_' . $wpdb->dbname];
            $column = $wpdb->get_results($sql, ARRAY_A);
            $search_sql = 'SELECT * FROM ' . $tables[$i]['Tables_in_' . $wpdb->dbname] . ' WHERE ';
            // replace string
            if ($replace_text) {
                $replace_sql = 'UPDATE ' . $tables[$i]['Tables_in_' . $wpdb->dbname] . ' SET ';
            }
            $no_varchar_field = 0;
            for ($j = 0; $j < count($column); $j++) {
                if ($no_varchar_field != 0) {
                    $search_sql .= 'or ';
                    //if ( $replace_text ) {
                    //	$replace_sql .= ', ';
                    //}
                }
                //  COLLATE utf8_bin  for search case sensitive
                $search_sql .= '`' . $column[$j]['Field'] . '` like \'%' . $search_text . '%\' COLLATE utf8_bin ';
                if ($replace_text) {
                    $search_result = $wpdb->get_results($search_sql, ARRAY_A);
                    // Loop about the result and check for serialized data
                    // Unset serialized data, no changes
                    foreach ($search_result as $key => $values) {
                        foreach ($values as $field => $value) {
                            if (is_serialized($value)) {
                                /*
                                $value = @unserialize( $value );
                                $value = json_decode(
                                	str_replace( $search_text, $replace_text, json_encode( $value ) )
                                );
                                $value = serialize( $value );
                                */
                                if (isset($column[$j]['Field']) && $field === $column[$j]['Field']) {
                                    unset($column[$j]['Field']);
                                }
                            }
                        }
                    }
                    if (!empty($column[$j]['Field'])) {
                        $replace_sql .= $column[$j]['Field'] . ' = ';
                        // Note that when searching for text to replace, MySQL uses case-sensitive match to perform search for string to be replaced.
                        $replace_sql .= 'REPLACE(' . $column[$j]['Field'] . ', "' . $search_text . '", "' . $replace_text . '"), ';
                    }
                }
                $no_varchar_field++;
            }
            if ($no_varchar_field > 0) {
                $search_result = $wpdb->get_results($search_sql, ARRAY_A);
                if ($replace_text) {
                    $replace_sql = rtrim($replace_sql, ", ");
                    $wpdb->get_results($replace_sql, ARRAY_A);
                }
                if (count($search_result)) {
                    $result_in_tables++;
                    $myecho .= '<p><strong>' . __('Table:', FB_SAR_TEXTDOMAIN) . ' </strong><code>' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '</code> ... ';
                    $myecho .= __('Total rows for', FB_SAR_TEXTDOMAIN) . ' <code>"' . stripslashes($search_text) . '"</code>: <strong>' . $wpdb->num_rows . '</strong></p>';
                    $myecho .= '<p><a href="javascript:toggle(\'' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '_sql' . '\')">SQL</a></p>';
                    $myecho .= '<script language="JavaScript">
						table_id.push("' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '_sql");
					</script>';
                    // Display sql statement
                    $sql = $search_sql;
                    if ($replace_text) {
                        $sql = $replace_sql;
                    }
                    $myecho .= '<div id="' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '_sql" style="display:none;"><code>' . $sql . '</code></div>';
                    $myecho .= '<p><a href="javascript:toggle(\'' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '_wrapper' . '\')">Result</a></p>';
                    $myecho .= '<script language="JavaScript">
						table_id.push("' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '_wrapper");
					</script>';
                    $myecho .= '<div id="' . $tables[$i]['Tables_in_' . $wpdb->dbname] . '_wrapper" style="display:none;">';
                    $myecho .= searchandreplace_table_arrange($search_result);
                    $myecho .= '</div>';
                }
                // @endof showing found search
            }
        }
    }
    if (!$result_in_tables) {
        $myecho = '<p style="color:red;">' . __('Sorry,') . ' <code>' . stripslashes_deep(stripslashes_deep(htmlentities2($search_text))) . '</code> ' . __('is not found in this database', FB_SAR_TEXTDOMAIN) . '(<code>' . $wpdb->dbname . '</code>)!</p>';
    }
    return $myecho;
}
Example #2
0
function searchandreplace_all($search_text) {
	global $wpdb;
	
	if ( empty($wpdb->dbname) )
		$wpdb->dbname = DB_NAME;
	
	$search_text = mysql_real_escape_string($search_text);
	$result_in_tables = 0;
	
	$myecho = '
	<script language="JavaScript">
		var table_id = new Array();
	
		function hide_all() {
			for(i=0;i<table_id.length;i++){
				document.getElementById(table_id[i]).style.display = \'none\';
			}
		}
		
		function show_all() {
			for(i=0;i<table_id.length;i++){
				document.getElementById(table_id[i]).style.display = \'block\';
			}
		}
		
		function toggle(id) {
			if (get_style(id,\'display\') == \'block\') {
				document.getElementById(id).style.display = \'none\';
			} else {
				document.getElementById(id).style.display = \'block\';
			}
		}
		
		function get_style(el,styleProp) {
			var x = document.getElementById(el);
			if (x.currentStyle)
				var y = x.currentStyle[styleProp];
			else if (window.getComputedStyle)
				var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
			return y;
		}
	</script>';
	
	$myecho .= '<p><a href="javascript:hide_all()">Collapse All</a> 
		 <a href="javascript:show_all()">Expand All</a></p>';
	$myecho .= '<p>Results for: <code>' . $search_text . '</code></p>';
	
	$sql= 'show tables';
	$res = mysql_query($sql);
	$tables = searchandreplace_fetch_array($res);
	
	for ($i=0; $i<sizeof($tables); $i++) {
		//@abstract querry bliding of each table
		$sql = 'select count(*) from '.$tables[$i]['Tables_in_'.$wpdb->dbname];
		$res = mysql_query($sql);
		
		if (mysql_num_rows($res)>0) {
			//@abstract taking the table data type information
			$sql = 'desc '.$tables[$i]['Tables_in_'.$wpdb->dbname]; 
			$res = mysql_query($sql);
			$collum = searchandreplace_fetch_array($res);
			
			$search_sql = 'select * from '.$tables[$i]['Tables_in_'.$wpdb->dbname].' where ';
			$no_varchar_field = 0;
			
			for ($j=0;$j<sizeof($collum);$j++) {
					if ($no_varchar_field!=0){
					$search_sql .= ' or ' ;
					}
					$search_sql .= '`'.$collum[$j]['Field'] .'` like \'%'.$search_text.'%\' ';
					$no_varchar_field++;
			}
			
			if ($no_varchar_field > 0) {
				$res = mysql_query($search_sql);
				$search_result = searchandreplace_fetch_array($res);
				if ( sizeof($search_result) ) {
					$result_in_tables++;
				
					$myecho .= '<p><strong>Table: </strong><code>' . $tables[$i]['Tables_in_'.$wpdb->dbname] . '</code> ... ';
					$myecho .= 'Total Results for <code>"' . $search_text . '"</code>: <strong>'. mysql_affected_rows() . '</strong></p>';
					$myecho .= '<p><a href="javascript:toggle(\'' . $tables[$i]['Tables_in_'.$wpdb->dbname].'_sql'.'\')">SQL</a></p>';
					$myecho .= '<script language="JavaScript">
						table_id.push("'.$tables[$i]['Tables_in_'.$wpdb->dbname].'_sql");
					</script>';
					$myecho .= '<div id="'.$tables[$i]['Tables_in_'.$wpdb->dbname].'_sql" style="display:none;"><code>'.$search_sql.'</code></div>';
					$myecho .= '<p><a href="javascript:toggle(\''.$tables[$i]['Tables_in_'.$wpdb->dbname].'_wrapper'.'\')">Result</a></p>';
					$myecho .= '<script language="JavaScript">
						table_id.push("'.$tables[$i]['Tables_in_'.$wpdb->dbname].'_wrapper");
					</script>';
					$myecho .= '<div id="'.$tables[$i]['Tables_in_'.$wpdb->dbname].'_wrapper" style="display:none;">';
					
					$myecho .= searchandreplace_table_arrange($search_result);
					$myecho .= '</div>';
				}// @endof showing found search  
				
			}
		}
	}
	
	if (!$result_in_tables) {
		$myecho = '<p style="color:red;">Sorry, <code>'.
			$search_text . '</code> ' . 
			__( 'is not found in this Database', FB_SAR_TEXTDOMAIN ) . 
			'(<code>' . $wpdb->dbname . '</code>)!</p>';
	}
	
	return $myecho;
}