_openRead() public method

{{{ _openRead()
public _openRead ( )
function step2($file = "")
{
    global $_CONFIG, $filepath;
    $DBcreated = $_REQUEST[DBcreated];
    if ($DBcreated == 'on') {
        $DBhostname = $_REQUEST['mysql_server'];
        $DBuserName = $_REQUEST['mysql_username'];
        $DBpassword = $_REQUEST['mysql_pass'];
        $DBname = $_REQUEST['mysql_db'];
        $_CONFIG['mysqli'] = new mysqli($DBhostname, $DBuserName, $DBpassword, $DBname);
        /* check connection */
        if ($_CONFIG['mysqli']->connect_errno) {
            printf("Connect failed: %s\n", $_CONFIG['mysqli']->connect_error);
            exit;
        }
        $_CONFIG['mysqli']->query("SET sql_mode='';");
        $_CONFIG['mysqli']->query("SET foreign_key_checks = 0;");
        if ($_REQUEST['charset_of_file'] != "") {
            $_CONFIG['mysqli']->query("SET NAMES " . $_REQUEST['charset_of_file'] . "");
        } else {
            $_CONFIG['mysqli']->query("SET NAMES utf8;");
        }
    }
    if ($_REQUEST['do_database'] != 1) {
        ############ DATABASE ONLY RESTRICTION##################################################
        ########################### START FTP MODE #################################
        if ($_REQUEST[transfer_mode] == 2) {
            $_CONFIG[output_path] = $_CONFIG[output_path] . "/archive_tmp/";
            @mkdir($_CONFIG[output_path]);
            // set up basic connection
            $conn_id = @ftp_connect($_REQUEST[ftp_server], $_REQUEST[ftp_port]) or die("<span class='error'>Could not connect to {$_REQUEST['ftp_server']} on port {$_REQUEST['ftp_port']}! Ftp connection has failed!</span>");
            // login with username and password
            $login_result = @ftp_login($conn_id, $_REQUEST[ftp_user], $_REQUEST[ftp_pass]) or die("<span class='error'>Could not login to ftp server for user {$_REQUEST['ftp_user']} and provided pass! Ftp connection has failed!</span>");
            // check connection
            if (!$conn_id || !$login_result) {
                echo "<b  style='color:red'>FTP connection has failed!</b>";
                echo "<b  style='color:red'>Attempted to connect to " . $_REQUEST[ftp_server] . ":" . $_REQUEST[ftp_port] . " for user " . $_REQUEST[ftp_user] . "</b>";
                return;
            } else {
                echo "<br />Connected to {$_REQUEST['ftp_server']}, for user {$_REQUEST['ftp_user']}, starting transfer...<br />";
            }
            $ftp_dir_original = ftp_pwd($conn_id);
            @ftp_mkdir($conn_id, $_REQUEST[ftp_path]);
            // try to change the directory to somedir
            if (@ftp_chdir($conn_id, $_REQUEST[ftp_path])) {
                echo "Current directory is now: <b>" . ftp_pwd($conn_id) . "</b>\n";
            } else {
                echo "<b  style='color:red'>Couldn't change directory to <b>{$_REQUEST['ftp_path']}</b>, please verify that the ftp location exists or use the \"Install files directly\" option!</b><br />\n";
                return;
            }
            @ftp_chdir($conn_id, $ftp_dir_original);
        }
        ########################## END FTP MODE ######################################
        ##### START extract ######
        if ($_REQUEST['refresh'] < 1) {
            $ext = substr($file, strlen($file) - 4, strlen($file));
            if ($_REQUEST['file_utilities'] != '1') {
                $tar_object = new Archive_Tar($file);
                if ($_REQUEST['manual_ftp']) {
                    $tar_object->_openRead();
                    fseek($tar_object->_file, $_CONFIG['seek']);
                    $seek = $tar_object->_extractList($_CONFIG['output_path'], $return, "partial", "", "", $_CONFIG['filesLimit']);
                    $seek = $seek - 512;
                    //reverse 512 bytes
                    if ($seek > 0) {
                        $backupSize = filesize($file);
                        $rurl = rurl($seek, $backupSize);
                        $percent = sprintf("%.2f", $seek * 100 / $backupSize);
                        echo "<h3>Processing files - " . $percent . "%</h3>";
                        echo "<script> document.location='" . $rurl . "'</script>";
                        return;
                    }
                    $_REQUEST['fpos'] = 0;
                    $_REQUEST['chunk'] = 0;
                } else {
                    $tar_object->extract($_CONFIG['output_path']);
                    #$tar_object->extractList("backups/perm.txt", $_CONFIG['output_path']);
                }
            } else {
                if ($ext == '.tgz') {
                    $compress = 'z';
                } else {
                    $compress = '';
                }
                shell_exec("tar -x" . $compress . "pf {$file} -C {$_CONFIG['output_path']}");
            }
        }
        ##### END extract ######
        $new_arr = array();
        #@chmod($_CONFIG[output_path], 0777);
        $tran_file = $_CONFIG[output_path] . "/transfer.txt";
        if ($_REQUEST[transfer_mode] == 2) {
            # initialise list arrays, directories and files separately and array counters for them
            $excludedFolders = array();
            $d_arr = array();
            $d = 0;
            $ds_arr = array();
            $f_arr = array();
            $f = 0;
            $s_arr = array();
            $s = 0;
            $d_arr[$d] = $_CONFIG[output_path];
            if ($_REQUEST['refresh'] < 1) {
                # obtain the list of files by recursing the mambo file store
                recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $excludedFolders, '', $_CONFIG[output_path]);
                recurseFiles($d_arr, $ds_arr, $f_arr, $s_arr, $d, $f, $s, $d_arr, '', $_CONFIG[output_path]);
                sort($d_arr);
                sort($f_arr);
                $new_arr = array_merge($d_arr, $f_arr);
                if ($_REQUEST[transfer_mode] == 2) {
                    if ($fp = fopen($tran_file, "w")) {
                        foreach ($new_arr as $file) {
                            fwrite($fp, $file . "\n");
                        }
                        fclose($fp);
                    } else {
                        echo "Unable to write to directory " . $_CONFIG[output_path] . "! Please check that this directory is writeable!";
                        return;
                    }
                } else {
                }
            } else {
                $content = "";
                $new_arr = file($tran_file);
            }
            if (sizeof($new_arr) != 0) {
                $percn = sprintf("%.2f", $_REQUEST[next] * 100 / sizeof($new_arr));
            }
            if ($percn > sizeof($new_arr)) {
                $percn = sizeof($new_arr);
            }
        }
        if ($_REQUEST['refresh'] == 1) {
            echo "<h3>Transfering {$percn}% of " . sizeof($new_arr) . " files through ftp</h3>";
            echo "<textarea cols=70 rows=30 name='ftp_list'></textarea>";
        } elseif ($_REQUEST['refresh'] == 2) {
            echo "Files transfer finished!";
        }
        $i = (int) $_REQUEST[next];
        $j = 0;
        ### MOVING THE FILES THROUGH FTP
        #foreach($new_arr as $key=>$file)
        for ($key = $i; $key <= sizeof($new_arr); $key++) {
            $file = str_replace(array("\r", "\n"), array("", ""), $new_arr[$key]);
            if ($_REQUEST['manual_ftp'] == 1) {
                if ($j == 100) {
                    #echo "Transfering files from line: ". (int)$_REQUEST[next];
                    $qstr = explode("&refresh=1&next=", $_SERVER['QUERY_STRING']);
                    $rurl = "XCloner.php?" . $qstr[0] . "&refresh=1&next=" . ($_REQUEST[next] + 100);
                    ftp_close($conn_id);
                    echo "<script> document.location='" . $rurl . "'</script>";
                    exit;
                }
            }
            $j++;
            $sfile = str_replace($_CONFIG[output_path], "", $file);
            $fsource = $_CONFIG[output_path] . "/" . $sfile;
            if ($sfile != "" && $sfile != "/") {
                if ($_REQUEST[transfer_mode] == 2) {
                    $upload = 1;
                    $ftarget = $_REQUEST[ftp_path] . "/" . $sfile;
                    if (is_dir($fsource)) {
                        $upload1 = @ftp_mkdir($conn_id, $ftarget);
                    } else {
                        $upload = ftp_put($conn_id, $ftarget, $fsource, FTP_BINARY);
                    }
                    #echo "$i <br >";
                    if (!$upload) {
                        echo "<br /><font color=red>Transfer fail for {$fsource} to {$ftarget}</font> <br />File already exists and/or doesn't have writing permissions!<br/>";
                    } else {
                        if ($_REQUEST['manual_ftp'] == 1) {
                            $ftarget .= '\\n';
                            echo "<script>document.form.ftp_list.value = document.form.ftp_list.value + '" . $ftarget . "'; </script>";
                            #echo "$ftarget <br />\n";
                        }
                    }
                }
            }
        }
        if ($_REQUEST['refresh'] == 1) {
            $qstr = explode("&refresh=1&next=", $_SERVER['QUERY_STRING']);
            $rurl = "XCloner.php?" . $qstr[0] . "&refresh=2&next=" . ($_REQUEST[next] + 102);
            echo "<script> document.location='" . $rurl . "'</script>";
            exit;
        }
        ############### RESTORING HTACCESS AND CONFIGURATION PERM#####################################
        if ($_REQUEST[transfer_mode] == 2) {
            $cmd = "CHMOD 0777 " . $_REQUEST[ftp_path] . "/" . "wp-config.php";
            @ftp_site($conn_id, $cmd);
        } else {
            @chmod($_REQUEST[output_path] . "/" . "wp-config.php", 0777);
        }
        #### CUSTOM PERMISSIONS #####
        if ($_REQUEST['preserve_perm']) {
            $perm_data = "";
            $perm_file = $_CONFIG[output_path] . "/administrator/backups/perm.txt";
            $per = 1;
            @chmod($perm_file, 0777);
            $fp = @fopen($perm_file, 'r');
            if ($fp) {
                while (!feof($fp)) {
                    $perm_data .= fread($fp, 1024);
                }
                fclose($fp);
            } else {
                echo "Could not set permissions! Permissions file {$perm_file} not found!<br />";
                $per = 0;
            }
            $data = explode("\n", $perm_data);
            foreach ($data as $value) {
                $dir = explode("|", $value);
                if ($dir[1] == "") {
                    $dir[1] = '0755';
                }
                if (strstr($dir[0], "wp-config.php")) {
                    $dir[1] = '0777';
                }
                if ($_REQUEST[transfer_mode] == 2) {
                    $cmd = "CHMOD " . $dir[1] . " " . $_REQUEST[ftp_path] . "/" . $dir[0];
                    @ftp_site($conn_id, $cmd);
                } else {
                    @chmod($_CONFIG[output_path] . "/" . $dir[0], octdec($dir[1]));
                }
            }
            if ($per) {
                echo "<h2>Permissions restored to their initial value...</h2>";
            }
        }
        #### END CUSTOM PERMISSIONS #####
        $_CONFIG[output_path] = str_replace("/archive_tmp/", "", $_CONFIG[output_path]);
        if ($_REQUEST[transfer_mode] == 2) {
            // close the FTP stream
            @ftp_close($conn_id);
            recursive_remove_directory($_CONFIG[output_path] . "/archive_tmp/");
            @unlink($_CONFIG[output_path] . "/archive_tmp/");
            echo "<h2>Files succesfully copied to " . $_REQUEST[ftp_path] . " on {$_REQUEST['ftp_server']} using FTP</h2>";
        } else {
            echo "<h2>Files succesfully copied to " . $_CONFIG[output_path] . "</h2>";
        }
        ######### END DATABASE ONLY RESTRICT #########################################
    }
    ##### RESTORE CONFIGURATION #####
    $update_config = 1;
    if ($_REQUEST['files_skip'] and isset($_REQUEST['fpos'])) {
        $update_config = 0;
    }
    if ($_REQUEST['do_database'] != 1 || $_REQUEST['files_skip'] == 1) {
        $config_file = $_CONFIG[output_path] . "/wp-config.php";
        @chmod($config_file, 0777);
        @unlink($_CONFIG[output_path] . "/administrator/backups/perm.txt");
        if ($_CONFIG['sql_usefile'] == "database-sql.sql" and $update_config) {
            if (write_config($config_file)) {
                echo "<H2>Configuration updated!</H2>";
            } else {
                echo "<span class='error'>Unable to write to configuration file {$config_file}... Aborting...</span>";
                return;
            }
        }
    }
    ############ ATTEMPT DATABASE INSERT #####################################
    if ($DBcreated != 'on') {
        echo "<h2>Database import skipped!</h2>";
        echo "<a href='" . $_CONFIG[output_url] . "'><b>All should be done! Click here to continue...</b></a><br />";
        return;
    }
    if ($DBcreated == 'on' && $_REQUEST['do_database'] == 1) {
        $sqlfile = $_CONFIG[output_path] . "/administrator/backups/" . $_CONFIG['sql_usefile'];
        if (!file_exists($sqlfile)) {
            echo "<span class='error'>Unable to read the database backup file {$sqlfile} , database was not imported!</span>";
        } else {
            if ($_REQUEST['manual_sql']) {
                $errors = populate_db_manual($db, $sqlfile);
            } else {
                $errors = populate_db($db, $sqlfile);
            }
            if (sizeof($errors) > 0) {
                echo "There were some errors while importing the database:<br />";
                echo "<textarea cols=60 rows=30>" . implode("\n", $errors) . "</textarea>";
                exit;
            } else {
                @unlink($_CONFIG[output_path] . "/administrator/backups/" . $_CONFIG['sql_usefile']);
            }
            echo "<h2>Database populated...</h2>";
        }
    }
    ###################################################################################
    if ($_REQUEST['do_database'] != 1) {
        $vars = "";
        foreach ($_REQUEST as $key => $value) {
            $vars .= $key . '=' . @urlencode($value) . '&';
        }
        #$href = "XCloner.php?".$vars."do_database=1";
        $href = rurl() . "&do_database=1";
        echo "<br /><a href='{$href}'>Please click here to continue with database import...</a>";
        return;
    } else {
        echo "<br /><a href='" . $_CONFIG[output_url] . "'><b>All should be done! Click here to continue...<br /></a><br />";
        return;
    }
}