public function __construct($base_file)
 {
     self::$basedir = dirname($base_file);
     // Creates the class autoloader.
     spl_autoload_register(array($this, 'class_loader'));
     Snapshot_View_Form_Recovery::render_recovery_form();
 }
        public static function render_step_4($form_errors)
        {
            ?>
			<h2>Processing Step: 4 - Database Import</h2>

			<form action="?step=4" method="post" class="restore_form">
				<?php 
            $db_link = mysql_connect($_SESSION['restore_form']['wordpress']['wp-config-db']['DB_HOST'], $_SESSION['restore_form']['wordpress']['wp-config-db']['DB_USER'], $_SESSION['restore_form']['wordpress']['wp-config-db']['DB_PASSWORD']);
            if (!$db_link) {
                echo "Could not connect to MySQL: " . mysql_error();
                die;
            }
            $db_selected = mysql_select_db($_SESSION['restore_form']['wordpress']['wp-config-db']['DB_NAME'], $db_link);
            if (!$db_selected) {
                echo "Can't select database [" . $_SESSION['restore_form']['wordpress']['wp-config-db']['DB_NAME'] . "]: " . mysql_error();
            }
            if ($_SESSION['restore_form']['wordpress']['wp-config-db']['DB_PREFIX'] !== $_SESSION['restore_form']['snapshot']['manifest-data']['WP_DB_PREFIX']) {
                $TABLE_REPLACE_PREFIX_STR = $_SESSION['restore_form']['wordpress']['wp-config-db']['DB_PREFIX'];
            } else {
                $TABLE_REPLACE_PREFIX_STR = '';
            }
            //$TABLE_REPLACE_PREFIX_STR = 'wp_br549_';
            if (isset($_SESSION['restore_form']['snapshot']['manifest-data']['TABLES']) && is_array($_SESSION['restore_form']['snapshot']['manifest-data']['TABLES']) && count($_SESSION['restore_form']['snapshot']['manifest-data']['TABLES'])) {
                foreach ($_SESSION['restore_form']['snapshot']['manifest-data']['TABLES'] as $tables_set_idx => $tables_set_data) {
                    if (is_array($tables_set_data) && count($tables_set_data)) {
                        foreach ($tables_set_data as $table_name) {
                            $table_file = Snapshot_Helper_Recovery::trailingslashit_snapshot($_SESSION['restore_form']['snapshot']['extract-path']) . $table_name . ".sql";
                            if (!file_exists($table_file)) {
                                echo "table_file not found [" . $table_file . "]<br />";
                                continue;
                            }
                            $table_file_handle = fopen($table_file, "r");
                            if (!$table_file_handle) {
                                echo "unable to open table_file [" . $table_file . "]<br />";
                                continue;
                            }
                            $table_name_new = '';
                            $table_name_base = str_replace($_SESSION['restore_form']['snapshot']['manifest-data']['WP_DB_PREFIX'], '', $table_name);
                            if (strlen($TABLE_REPLACE_PREFIX_STR)) {
                                $table_name_new = $TABLE_REPLACE_PREFIX_STR . $table_name_base;
                                echo "Processing database table: " . $table_name . " to " . $table_name_new;
                            } else {
                                echo "Processing database table: " . $table_name;
                            }
                            $table_file_create_sql = '';
                            $table_file_create_done = false;
                            $table_record_count = 0;
                            while (($table_file_buffer = fgets($table_file_handle, 4096)) !== false) {
                                if (!strlen(trim($table_file_buffer))) {
                                    if ($table_file_create_done == false && strlen($table_file_create_sql)) {
                                        $table_file_create_done = true;
                                        $sql_split = explode(';', $table_file_create_sql);
                                        foreach ($sql_split as $sql_str) {
                                            if (strlen($table_name_new)) {
                                                $sql_str = str_replace('`' . $table_name . '`', '`' . $table_name_new . '`', $sql_str);
                                            }
                                            $result = mysql_query($sql_str, $db_link);
                                        }
                                    }
                                } else {
                                    if ($table_file_create_done == false) {
                                        $table_file_create_sql .= $table_file_buffer;
                                    } else {
                                        if (strlen($table_name_new)) {
                                            $table_file_buffer = str_replace('`' . $table_name . '`', '`' . $table_name_new . '`', $table_file_buffer);
                                        }
                                        $result = mysql_query($table_file_buffer, $db_link);
                                        $table_record_count += 1;
                                    }
                                }
                            }
                            fclose($table_file_handle);
                            echo " : total rows: " . $table_record_count . "<br />";
                            //echo "table_name_base=[". $table_name_base ."]<br />";
                            //echo "table_name_new=[". $table_name_new ."]<br />";
                            //echo "table_name=[". $table_name ."]<br />";
                            //echo "site_url=[". $_SESSION['restore_form']['wordpress']['site-url'] ."] [". $_SESSION['restore_form']['snapshot']['manifest-data']['SITEURL']."]<br />";
                            //echo "home_url=[". $_SESSION['restore_form']['wordpress']['home-url'] ."] [". $_SESSION['restore_form']['snapshot']['manifest-data']['HOME']."]<br />";
                            if ($_SESSION['restore_form']['wordpress']['site-url'] != $_SESSION['restore_form']['snapshot']['manifest-data']['WP_SITEURL'] || $_SESSION['restore_form']['wordpress']['home-url'] != $_SESSION['restore_form']['snapshot']['manifest-data']['WP_HOME']) {
                                if ($table_name_base == "options") {
                                    if (strlen($table_name_new)) {
                                        $table_name_replace = $table_name_new;
                                    } else {
                                        $table_name_replace = $table_name;
                                    }
                                    //echo "table_name_replace=[". $table_name_replace ."]<br />";
                                    Snapshot_Helper_Recovery::search_replace_table_data($table_name_replace, $db_link, $_SESSION['restore_form']['snapshot']['manifest-data']['WP_SITEURL'], $_SESSION['restore_form']['wordpress']['site-url']);
                                    //die();
                                }
                            }
                            flush();
                        }
                    }
                }
            } else {
                echo "Snapshot Archive manifest item 'TABLES' not set. Aborting";
            }
            mysql_close($db_link);
            ?>
				<!-- <p><input type="submit" value="Finished"/></p> -->
				<p><strong>Revovery Complete:</strong> <a href="<?php 
            echo $_SESSION['restore_form']['wordpress']['home-url'];
            ?>
">Return to Site</a></p>
			</form>
		<?php 
        }