Пример #1
0
function ftp_unziptransferfiles($archivesArray)
{
    // --------------
    // Extract the directories and files from the archive to a temporary directory on the web server, and
    // then create the directories and put the files on the FTP server
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_globals, $net2ftp_result, $net2ftp_output;
    // -------------------------------------------------------------------------
    // Open connection
    // -------------------------------------------------------------------------
    $conn_id = ftp_openconnection();
    if ($net2ftp_result["success"] == false) {
        for ($archive_nr = 1; $archive_nr <= sizeof($archivesArray); $archive_nr++) {
            @unlink($archivesArray[$archive_nr]["tmp_name"]);
        }
        return false;
    }
    // -------------------------------------------------------------------------
    // For each archive...
    // -------------------------------------------------------------------------
    for ($archive_nr = 1; $archive_nr <= sizeof($archivesArray); $archive_nr++) {
        // Set status
        setStatus($archive_nr, sizeof($archivesArray), __("Decompressing archives and transferring files"));
        // -------------------------------------------------------------------------
        // Determine the type of archive depending on the filename extension
        // -------------------------------------------------------------------------
        $archive_name = $archivesArray[$archive_nr]["name"];
        $archive_file = $archivesArray[$archive_nr]["tmp_name"];
        $archivename_without_dottext = substr($archivesArray[$archive_nr]["tmp_name"], 0, strlen($archive) - 4);
        $archive_type = get_filename_extension($archivename_without_dottext);
        $net2ftp_output["ftp_unziptransferfiles"][] = __("Processing archive nr %1\$s: <b>%2\$s</b>", $archive_nr, $archive_name);
        $net2ftp_output["ftp_unziptransferfiles"][] = "<ul>";
        if ($archive_type != "zip" && $archive_type != "tar" && $archive_type != "tgz" && $archive_type != "gz") {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Archive <b>%1\$s</b> was not processed because its filename extension was not recognized. Only zip, tar, tgz and gz archives are supported at the moment.", $archive_name);
            continue;
        }
        // -------------------------------------------------------------------------
        // Extract directories and files
        // -------------------------------------------------------------------------
        // ------------------------------
        // Check list of files to see if there are any malicious filenames
        // ------------------------------
        if ($archive_type == "zip") {
            $zip = new PclZip($archive_file);
            $list_to_check = $zip->listContent();
        } elseif ($archive_type == "tar" || $archive_type == "tgz" || $archive_type == "gz") {
            $list_to_check = PclTarList($archive_file);
        }
        if ($list_to_check <= 0) {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to extract the files and directories from the archive");
            continue;
        }
        for ($i = 0; $i < sizeof($list_to_check); $i++) {
            $source = trim($list_to_check[$i]["filename"]);
            if (strpos($source, "../") !== false || strpos($source, "..\\") !== false) {
                $errormessage = __("Archive contains filenames with ../ or ..\\ - aborting the extraction");
                setErrorVars(false, $errormessage, debug_backtrace(), __FILE__, __LINE__);
                return false;
            }
        }
        // ------------------------------
        // Generate random directory
        // ------------------------------
        $tempdir = tempdir2($net2ftp_globals["application_tempdir"], "unzip__", "");
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        registerTempfile("register", "{$tempdir}");
        // ------------------------------
        // Extract
        // ------------------------------
        if ($archive_type == "zip") {
            $zip = new PclZip($archive_file);
            $list = $zip->extract($p_path = $tempdir);
        } elseif ($archive_type == "tar" || $archive_type == "tgz" || $archive_type == "gz") {
            $list = PclTarExtract($archive_file, $tempdir);
        }
        // This code is not needed any more - see above: if ($list_to_check <= 0)
        if ($list <= 0) {
            //			$net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to extract the files and directories from the archive");
            continue;
        }
        // ------------------------------
        // Create the directories and put the files on the FTP server
        // ------------------------------
        for ($i = 0; $i < sizeof($list); $i++) {
            $source = trim($list[$i]["filename"]);
            $unzip_status = trim($list[$i]["status"]);
            $target_relative = substr($source, strlen($tempdir));
            $target = $net2ftp_globals["directory"] . $target_relative;
            $ftpmode = ftpAsciiBinary($source);
            if ($unzip_status != "ok") {
                $net2ftp_output["ftp_unziptransferfiles"][] = __("Could not unzip entry %1\$s (error code %2\$s)", $target_relative, $unzip_status);
                setErrorVars(true, "", "", "", "");
                continue;
            }
            // Directory entry in the archive: create the directory
            if (is_dir($source) == true) {
                ftp_newdirectory($conn_id, $target);
                if ($net2ftp_result["success"] == true) {
                    $net2ftp_output["ftp_unziptransferfiles"][] = __("Created directory %1\$s", $target);
                } else {
                    $net2ftp_output["ftp_unziptransferfiles"][] = __("Could not create directory %1\$s", $target);
                    setErrorVars(true, "", "", "", "");
                }
            } elseif (is_file($source) == true) {
                ftp_putfile($conn_id, dirname($source), basename($source), dirname($target), basename($target), $ftpmode, "move");
                if ($net2ftp_result["success"] == true) {
                    $net2ftp_output["ftp_unziptransferfiles"][] = __("Copied file %1\$s", $target);
                } else {
                    setErrorVars(true, "", "", "", "");
                    $target_relative_parts = explode("/", str_replace("\\", "/", dirname($target_relative)));
                    $directory_to_create = $net2ftp_globals["directory"];
                    for ($j = 0; $j < sizeof($target_relative_parts); $j = $j + 1) {
                        $directory_to_create = $directory_to_create . "/" . $target_relative_parts[$j];
                        $ftp_chdir_result = @ftp_chdir($conn_id, $directory_to_create);
                        if ($ftp_chdir_result == false) {
                            ftp_newdirectory($conn_id, $directory_to_create);
                            if ($net2ftp_result["success"] == true) {
                                $net2ftp_output["ftp_unziptransferfiles"][] = __("Created directory %1\$s", $directory_to_create);
                            } else {
                                setErrorVars(true, "", "", "", "");
                            }
                        }
                        // end if
                    }
                    // end for
                    ftp_putfile($conn_id, dirname($source), basename($source), dirname($target), basename($target), $ftpmode, "copy");
                    if ($net2ftp_result["success"] == true) {
                        $net2ftp_output["ftp_unziptransferfiles"][] = __("Copied file %1\$s", $target);
                    } else {
                        setErrorVars(true, "", "", "", "");
                        $net2ftp_output["ftp_unziptransferfiles"][] = __("Could not copy file %1\$s", $target);
                    }
                }
            }
            // end elseif file
        }
        // end for
        // -------------------------------------------------------------------------
        // Delete the uploaded archive and the temporary files
        // -------------------------------------------------------------------------
        // Delete the temporary directory and its contents
        $delete_dirorfile_result = delete_dirorfile($tempdir);
        if ($delete_dirorfile_result == false) {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to delete the temporary directory");
        } else {
            registerTempfile("unregister", "{$tempdir}");
        }
        // Delete the archive
        $unlink_result = @unlink($archive_file);
        if ($unlink_result == false) {
            $net2ftp_output["ftp_unziptransferfiles"][] = __("Unable to delete the temporary file %1\$s", $archive_file);
        } else {
            registerTempfile("unregister", "{$archive_file}");
        }
        $net2ftp_output["ftp_unziptransferfiles"][] = "</ul>";
    }
    // End for
    // -------------------------------------------------------------------------
    // Close connection
    // -------------------------------------------------------------------------
    ftp_closeconnection($conn_id);
}
Пример #2
0
function net2ftp_module_printBody()
{
    // --------------
    // This function prints the unzip screen
    // --------------
    // -------------------------------------------------------------------------
    // Global variables
    // -------------------------------------------------------------------------
    global $net2ftp_settings, $net2ftp_globals, $net2ftp_messages, $net2ftp_result, $net2ftp_output;
    if (isset($_POST["list"]) == true) {
        $list = getSelectedEntries($_POST["list"]);
    } else {
        $list = "";
    }
    // -------------------------------------------------------------------------
    // Variables for all screens
    // -------------------------------------------------------------------------
    // Title
    $title = __("Unzip archives");
    // Form name, back and forward buttons
    $formname = "UnzipForm";
    $back_onclick = "document.forms['" . $formname . "'].state.value='browse';document.forms['" . $formname . "'].state2.value='main';document.forms['" . $formname . "'].submit();";
    $forward_onclick = "document.forms['" . $formname . "'].submit();";
    // -------------------------------------------------------------------------
    // Variables for screen 1
    // -------------------------------------------------------------------------
    if ($net2ftp_globals["screen"] == 1) {
        // Next screen
        $nextscreen = 2;
    } elseif ($net2ftp_globals["screen"] == 2) {
        $net2ftp_output["unzip"] = array();
        $net2ftp_output["ftp_unziptransferfiles"] = array();
        // ---------------------------------------
        // Initialize variables
        // ---------------------------------------
        $moved_ok = 0;
        // Index of the archives that have been treated successfully
        $moved_notok = 0;
        // Index of the archives that have been treated unsuccessfully
        // ---------------------------------------
        // Open connection to the FTP server
        // ---------------------------------------
        setStatus(2, 10, __("Connecting to the FTP server"));
        $conn_id = ftp_openconnection();
        if ($net2ftp_result["success"] == false) {
            return false;
        }
        // ---------------------------------------
        // Get the archives from the FTP server
        // ---------------------------------------
        for ($i = 1; $i <= $list["stats"]["files"]["total_number"]; $i = $i + 1) {
            // Set the status
            $message = __("Getting archive %1\$s of %2\$s from the FTP server", $i, $list["stats"]["files"]["total_number"]);
            setStatus($i, $list["stats"]["files"]["total_number"], $message);
            // Get the archive from the FTP server
            $localtargetdir = $net2ftp_globals["application_tempdir"];
            $localtargetfile = $list["files"][$i]["dirfilename"] . ".txt";
            $remotesourcedir = $net2ftp_globals["directory"];
            $remotesourcefile = $list["files"][$i]["dirfilename"];
            $ftpmode = ftpAsciiBinary($list["files"][$i]["dirfilename"]);
            $copymove = "copy";
            ftp_getfile($conn_id, $localtargetdir, $localtargetfile, $remotesourcedir, $remotesourcefile, $ftpmode, $copymove);
            if ($net2ftp_result["success"] == false) {
                setErrorVars(true, "", "", "", "");
                $net2ftp_output["unzip"][] = __("Unable to get the archive <b>%1\$s</b> from the FTP server", htmlEncode2($list["files"][$i]["dirfilename"]));
                $moved_notok = $moved_notok + 1;
                continue;
            }
            // Register the temporary file
            registerTempfile("register", glueDirectories($localtargetdir, $localtargetfile));
            // Enter the temporary filename and the real filename in the array
            $moved_ok = $moved_ok + 1;
            $acceptedArchivesArray[$moved_ok]["name"] = $list["files"][$i]["dirfilename"];
            $acceptedArchivesArray[$moved_ok]["tmp_name"] = glueDirectories($localtargetdir, $localtargetfile);
            $acceptedArchivesArray[$moved_ok]["targetdirectory"] = $list["files"][$i]["targetdirectory"];
            $acceptedArchivesArray[$moved_ok]["use_folder_names"] = $list["files"][$i]["use_folder_names"];
        }
        // end for
        // ---------------------------------------
        // Unzip archives and transfer the files (create subdirectories if needed)
        // ---------------------------------------
        if (isset($acceptedArchivesArray) == true && sizeof($acceptedArchivesArray) > 0) {
            ftp_unziptransferfiles($acceptedArchivesArray);
            $net2ftp_output["unzip"] = $net2ftp_output["unzip"] + $net2ftp_output["ftp_unziptransferfiles"];
            if ($net2ftp_result["success"] == false) {
                return false;
            }
        }
        // ---------------------------------------
        // Close the connection to the FTP server
        // ---------------------------------------
        ftp_closeconnection($conn_id);
    }
    // end elseif
    // -------------------------------------------------------------------------
    // Print the output
    // -------------------------------------------------------------------------
    require_once $net2ftp_globals["application_skinsdir"] . "/" . $net2ftp_globals["skin"] . "/manage.template.php";
}